]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/staging/comedi/comedi_fops.c
comedi: get rid of compat_alloc_user_space() mess in COMEDI_CHANINFO compat
[thirdparty/linux.git] / drivers / staging / comedi / comedi_fops.c
CommitLineData
e184e2be 1// SPDX-License-Identifier: GPL-2.0+
ed9eccbe 2/*
f6fef5df
IA
3 * comedi/comedi_fops.c
4 * comedi kernel module
5 *
6 * COMEDI - Linux Control and Measurement Device Interface
e0d0bf8a
AV
7 * Copyright (C) 1997-2007 David A. Schleef <ds@schleef.org>
8 * compat ioctls:
9 * Author: Ian Abbott, MEV Ltd. <abbotti@mev.co.uk>
10 * Copyright (C) 2007 MEV Ltd. <http://www.mev.co.uk/>
f6fef5df 11 */
ed9eccbe 12
c2ad078b
HS
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
ed9eccbe
DS
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/kernel.h>
174cd4b1 18#include <linux/sched/signal.h>
ed9eccbe
DS
19#include <linux/fcntl.h>
20#include <linux/delay.h>
ed9eccbe
DS
21#include <linux/mm.h>
22#include <linux/slab.h>
ed9eccbe 23#include <linux/poll.h>
ed9eccbe 24#include <linux/device.h>
ed9eccbe
DS
25#include <linux/fs.h>
26#include "comedidev.h"
27#include <linux/cdev.h>
28
476b8477
GKH
29#include <linux/io.h>
30#include <linux/uaccess.h>
e0d0bf8a 31#include <linux/compat.h>
ed9eccbe 32
3a5fa275 33#include "comedi_internal.h"
ed9eccbe 34
63107ce8 35/*
eb340aca 36 * comedi_subdevice "runflags"
63107ce8
IA
37 * COMEDI_SRF_RT: DEPRECATED: command is running real-time
38 * COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
eb340aca 39 * since the last command was started
63107ce8
IA
40 * COMEDI_SRF_RUNNING: command is running
41 * COMEDI_SRF_FREE_SPRIV: free s->private on detach
eb340aca 42 *
63107ce8 43 * COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
eb340aca
IA
44 */
45#define COMEDI_SRF_RT BIT(1)
46#define COMEDI_SRF_ERROR BIT(2)
47#define COMEDI_SRF_RUNNING BIT(27)
48#define COMEDI_SRF_FREE_SPRIV BIT(31)
49
50#define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
51
20f083c0 52/**
a3e39942
IA
53 * struct comedi_file - Per-file private data for COMEDI device
54 * @dev: COMEDI device.
55 * @read_subdev: Current "read" subdevice.
56 * @write_subdev: Current "write" subdevice.
57 * @last_detach_count: Last known detach count.
58 * @last_attached: Last known attached/detached state.
20f083c0
IA
59 */
60struct comedi_file {
61 struct comedi_device *dev;
62 struct comedi_subdevice *read_subdev;
63 struct comedi_subdevice *write_subdev;
64 unsigned int last_detach_count;
b3c16227 65 unsigned int last_attached:1;
20f083c0
IA
66};
67
eda56825 68#define COMEDI_NUM_MINORS 0x100
5b7dba1b
IA
69#define COMEDI_NUM_SUBDEVICE_MINORS \
70 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
eda56825 71
d1d78d20
CKC
72static unsigned short comedi_num_legacy_minors;
73module_param(comedi_num_legacy_minors, ushort, 0444);
4d7df821
IA
74MODULE_PARM_DESC(comedi_num_legacy_minors,
75 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
76 );
77
234bb3c6 78unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
6e302939 79module_param(comedi_default_buf_size_kb, uint, 0644);
4d7df821
IA
80MODULE_PARM_DESC(comedi_default_buf_size_kb,
81 "default asynchronous buffer size in KiB (default "
234bb3c6 82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
4d7df821 83
642e0692
NK
84unsigned int comedi_default_buf_maxsize_kb =
85 CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
6e302939 86module_param(comedi_default_buf_maxsize_kb, uint, 0644);
4d7df821
IA
87MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
88 "default maximum size of asynchronous buffer in KiB (default "
234bb3c6 89 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
1dd33ab8 90
5b7dba1b 91static DEFINE_MUTEX(comedi_board_minor_table_lock);
cb6b79de 92static struct comedi_device
5b7dba1b
IA
93*comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
94
95static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
96/* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
bd5b4173 97static struct comedi_subdevice
5b7dba1b 98*comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
476b8477 99
d9740a03
IA
100static struct class *comedi_class;
101static struct cdev comedi_cdev;
102
103static void comedi_device_init(struct comedi_device *dev)
104{
5b13ed94 105 kref_init(&dev->refcount);
d9740a03
IA
106 spin_lock_init(&dev->spinlock);
107 mutex_init(&dev->mutex);
2f3fdcd7 108 init_rwsem(&dev->attach_lock);
d9740a03
IA
109 dev->minor = -1;
110}
111
5b13ed94
IA
112static void comedi_dev_kref_release(struct kref *kref)
113{
114 struct comedi_device *dev =
115 container_of(kref, struct comedi_device, refcount);
116
117 mutex_destroy(&dev->mutex);
8f988d87 118 put_device(dev->class_dev);
5b13ed94
IA
119 kfree(dev);
120}
121
dd630cde 122/**
a3e39942
IA
123 * comedi_dev_put() - Release a use of a COMEDI device
124 * @dev: COMEDI device.
dd630cde 125 *
a3e39942
IA
126 * Must be called when a user of a COMEDI device is finished with it.
127 * When the last user of the COMEDI device calls this function, the
128 * COMEDI device is destroyed.
dd630cde 129 *
a3e39942
IA
130 * Return: 1 if the COMEDI device is destroyed by this call or @dev is
131 * NULL, otherwise return 0. Callers must not assume the COMEDI
dd630cde
IA
132 * device is still valid if this function returns 0.
133 */
5b13ed94
IA
134int comedi_dev_put(struct comedi_device *dev)
135{
136 if (dev)
137 return kref_put(&dev->refcount, comedi_dev_kref_release);
138 return 1;
139}
b449c1ca
IA
140EXPORT_SYMBOL_GPL(comedi_dev_put);
141
142static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
143{
144 if (dev)
145 kref_get(&dev->refcount);
146 return dev;
147}
5b13ed94 148
d9740a03
IA
149static void comedi_device_cleanup(struct comedi_device *dev)
150{
151 struct module *driver_module = NULL;
152
88cc30cf 153 if (!dev)
d9740a03
IA
154 return;
155 mutex_lock(&dev->mutex);
156 if (dev->attached)
157 driver_module = dev->driver->module;
158 comedi_device_detach(dev);
1363e4fb
IA
159 if (driver_module && dev->use_count)
160 module_put(driver_module);
d9740a03 161 mutex_unlock(&dev->mutex);
d9740a03
IA
162}
163
db210da2
IA
164static bool comedi_clear_board_dev(struct comedi_device *dev)
165{
166 unsigned int i = dev->minor;
167 bool cleared = false;
168
77c21b62 169 lockdep_assert_held(&dev->mutex);
db210da2
IA
170 mutex_lock(&comedi_board_minor_table_lock);
171 if (dev == comedi_board_minor_table[i]) {
172 comedi_board_minor_table[i] = NULL;
173 cleared = true;
174 }
175 mutex_unlock(&comedi_board_minor_table_lock);
176 return cleared;
177}
178
d4d47895 179static struct comedi_device *comedi_clear_board_minor(unsigned int minor)
d9740a03 180{
cb6b79de 181 struct comedi_device *dev;
d9740a03 182
5b7dba1b 183 mutex_lock(&comedi_board_minor_table_lock);
cb6b79de 184 dev = comedi_board_minor_table[minor];
5b7dba1b
IA
185 comedi_board_minor_table[minor] = NULL;
186 mutex_unlock(&comedi_board_minor_table_lock);
cb6b79de 187 return dev;
d9740a03
IA
188}
189
cb6b79de 190static void comedi_free_board_dev(struct comedi_device *dev)
d9740a03 191{
cb6b79de 192 if (dev) {
52ef9e7c 193 comedi_device_cleanup(dev);
cb6b79de
IA
194 if (dev->class_dev) {
195 device_destroy(comedi_class,
196 MKDEV(COMEDI_MAJOR, dev->minor));
d9740a03 197 }
5b13ed94 198 comedi_dev_put(dev);
d9740a03
IA
199 }
200}
8ab4ed6e 201
b2073dcb
IA
202static struct comedi_subdevice *
203comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned int minor)
5b7dba1b 204{
bd5b4173 205 struct comedi_subdevice *s;
5b7dba1b
IA
206 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
207
5b7dba1b 208 mutex_lock(&comedi_subdevice_minor_table_lock);
bd5b4173 209 s = comedi_subdevice_minor_table[i];
63ab0395
IA
210 if (s && s->device != dev)
211 s = NULL;
5b7dba1b 212 mutex_unlock(&comedi_subdevice_minor_table_lock);
bd5b4173 213 return s;
5b7dba1b
IA
214}
215
d4d47895 216static struct comedi_device *comedi_dev_get_from_board_minor(unsigned int minor)
b449c1ca
IA
217{
218 struct comedi_device *dev;
219
b449c1ca
IA
220 mutex_lock(&comedi_board_minor_table_lock);
221 dev = comedi_dev_get(comedi_board_minor_table[minor]);
222 mutex_unlock(&comedi_board_minor_table_lock);
223 return dev;
224}
225
b2073dcb
IA
226static struct comedi_device *
227comedi_dev_get_from_subdevice_minor(unsigned int minor)
b449c1ca
IA
228{
229 struct comedi_device *dev;
230 struct comedi_subdevice *s;
231 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
232
b449c1ca
IA
233 mutex_lock(&comedi_subdevice_minor_table_lock);
234 s = comedi_subdevice_minor_table[i];
235 dev = comedi_dev_get(s ? s->device : NULL);
236 mutex_unlock(&comedi_subdevice_minor_table_lock);
237 return dev;
238}
239
dd630cde 240/**
a3e39942
IA
241 * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
242 * @minor: Minor device number.
dd630cde 243 *
a3e39942
IA
244 * Finds the COMEDI device associated with the minor device number, if any,
245 * and increments its reference count. The COMEDI device is prevented from
dd630cde
IA
246 * being freed until a matching call is made to comedi_dev_put().
247 *
a3e39942
IA
248 * Return: A pointer to the COMEDI device if it exists, with its usage
249 * reference incremented. Return NULL if no COMEDI device exists with the
dd630cde
IA
250 * specified minor device number.
251 */
d4d47895 252struct comedi_device *comedi_dev_get_from_minor(unsigned int minor)
b449c1ca
IA
253{
254 if (minor < COMEDI_NUM_BOARD_MINORS)
255 return comedi_dev_get_from_board_minor(minor);
cb3aadae
KH
256
257 return comedi_dev_get_from_subdevice_minor(minor);
b449c1ca
IA
258}
259EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
260
43bd33f2 261static struct comedi_subdevice *
da56fdc6 262comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
43bd33f2 263{
bd5b4173 264 struct comedi_subdevice *s;
da56fdc6 265
77c21b62 266 lockdep_assert_held(&dev->mutex);
da56fdc6 267 if (minor >= COMEDI_NUM_BOARD_MINORS) {
63ab0395 268 s = comedi_subdevice_from_minor(dev, minor);
88cc30cf 269 if (!s || (s->subdev_flags & SDF_CMD_READ))
bd5b4173 270 return s;
da56fdc6
IA
271 }
272 return dev->read_subdev;
43bd33f2
HS
273}
274
275static struct comedi_subdevice *
da56fdc6 276comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
43bd33f2 277{
bd5b4173 278 struct comedi_subdevice *s;
da56fdc6 279
77c21b62 280 lockdep_assert_held(&dev->mutex);
da56fdc6 281 if (minor >= COMEDI_NUM_BOARD_MINORS) {
63ab0395 282 s = comedi_subdevice_from_minor(dev, minor);
88cc30cf 283 if (!s || (s->subdev_flags & SDF_CMD_WRITE))
bd5b4173 284 return s;
da56fdc6
IA
285 }
286 return dev->write_subdev;
43bd33f2
HS
287}
288
20f083c0
IA
289static void comedi_file_reset(struct file *file)
290{
291 struct comedi_file *cfp = file->private_data;
292 struct comedi_device *dev = cfp->dev;
293 struct comedi_subdevice *s, *read_s, *write_s;
294 unsigned int minor = iminor(file_inode(file));
295
296 read_s = dev->read_subdev;
297 write_s = dev->write_subdev;
298 if (minor >= COMEDI_NUM_BOARD_MINORS) {
299 s = comedi_subdevice_from_minor(dev, minor);
88cc30cf 300 if (!s || s->subdev_flags & SDF_CMD_READ)
20f083c0 301 read_s = s;
88cc30cf 302 if (!s || s->subdev_flags & SDF_CMD_WRITE)
20f083c0
IA
303 write_s = s;
304 }
305 cfp->last_attached = dev->attached;
306 cfp->last_detach_count = dev->detach_count;
34d34732
SS
307 WRITE_ONCE(cfp->read_subdev, read_s);
308 WRITE_ONCE(cfp->write_subdev, write_s);
20f083c0
IA
309}
310
311static void comedi_file_check(struct file *file)
312{
313 struct comedi_file *cfp = file->private_data;
314 struct comedi_device *dev = cfp->dev;
315
316 if (cfp->last_attached != dev->attached ||
317 cfp->last_detach_count != dev->detach_count)
318 comedi_file_reset(file);
319}
320
321static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
322{
323 struct comedi_file *cfp = file->private_data;
324
325 comedi_file_check(file);
34d34732 326 return READ_ONCE(cfp->read_subdev);
20f083c0
IA
327}
328
329static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
330{
331 struct comedi_file *cfp = file->private_data;
332
333 comedi_file_check(file);
34d34732 334 return READ_ONCE(cfp->write_subdev);
20f083c0
IA
335}
336
883db3d9 337static int resize_async_buffer(struct comedi_device *dev,
b2073dcb
IA
338 struct comedi_subdevice *s,
339 unsigned int new_size)
a5011a26 340{
482f0a21 341 struct comedi_async *async = s->async;
a5011a26
HS
342 int retval;
343
77c21b62
IA
344 lockdep_assert_held(&dev->mutex);
345
a5011a26
HS
346 if (new_size > async->max_bufsize)
347 return -EPERM;
348
349 if (s->busy) {
272850f0
HS
350 dev_dbg(dev->class_dev,
351 "subdevice is busy, cannot resize buffer\n");
a5011a26
HS
352 return -EBUSY;
353 }
d4526ab4 354 if (comedi_buf_is_mmapped(s)) {
272850f0
HS
355 dev_dbg(dev->class_dev,
356 "subdevice is mmapped, cannot resize buffer\n");
a5011a26
HS
357 return -EBUSY;
358 }
359
a2aab8b4 360 /* make sure buffer is an integral number of pages (we round up) */
a5011a26
HS
361 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
362
363 retval = comedi_buf_alloc(dev, s, new_size);
364 if (retval < 0)
365 return retval;
366
367 if (s->buf_change) {
d546b896 368 retval = s->buf_change(dev, s);
a5011a26
HS
369 if (retval < 0)
370 return retval;
371 }
372
272850f0
HS
373 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
374 s->index, async->prealloc_bufsz);
a5011a26
HS
375 return 0;
376}
377
378/* sysfs attribute files */
379
e56341ad 380static ssize_t max_read_buffer_kb_show(struct device *csdev,
a5011a26
HS
381 struct device_attribute *attr, char *buf)
382{
c88db469 383 unsigned int minor = MINOR(csdev->devt);
7f4656c5
IA
384 struct comedi_device *dev;
385 struct comedi_subdevice *s;
72fd9fac 386 unsigned int size = 0;
a5011a26 387
be535c9a 388 dev = comedi_dev_get_from_minor(minor);
5e04c254 389 if (!dev)
c88db469
IA
390 return -ENODEV;
391
7f4656c5 392 mutex_lock(&dev->mutex);
da56fdc6 393 s = comedi_read_subdevice(dev, minor);
72fd9fac
HS
394 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
395 size = s->async->max_bufsize / 1024;
7f4656c5 396 mutex_unlock(&dev->mutex);
a5011a26 397
be535c9a 398 comedi_dev_put(dev);
ecd56ff9 399 return snprintf(buf, PAGE_SIZE, "%u\n", size);
a5011a26
HS
400}
401
e56341ad 402static ssize_t max_read_buffer_kb_store(struct device *csdev,
a5011a26
HS
403 struct device_attribute *attr,
404 const char *buf, size_t count)
405{
c88db469 406 unsigned int minor = MINOR(csdev->devt);
7f4656c5
IA
407 struct comedi_device *dev;
408 struct comedi_subdevice *s;
72fd9fac
HS
409 unsigned int size;
410 int err;
411
412 err = kstrtouint(buf, 10, &size);
413 if (err)
414 return err;
415 if (size > (UINT_MAX / 1024))
a5011a26 416 return -EINVAL;
72fd9fac 417 size *= 1024;
a5011a26 418
be535c9a 419 dev = comedi_dev_get_from_minor(minor);
5e04c254 420 if (!dev)
c88db469
IA
421 return -ENODEV;
422
7f4656c5 423 mutex_lock(&dev->mutex);
da56fdc6 424 s = comedi_read_subdevice(dev, minor);
72fd9fac
HS
425 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
426 s->async->max_bufsize = size;
427 else
428 err = -EINVAL;
7f4656c5 429 mutex_unlock(&dev->mutex);
a5011a26 430
be535c9a 431 comedi_dev_put(dev);
72fd9fac 432 return err ? err : count;
a5011a26 433}
e56341ad 434static DEVICE_ATTR_RW(max_read_buffer_kb);
a5011a26 435
e56341ad 436static ssize_t read_buffer_kb_show(struct device *csdev,
a5011a26
HS
437 struct device_attribute *attr, char *buf)
438{
c88db469 439 unsigned int minor = MINOR(csdev->devt);
7f4656c5
IA
440 struct comedi_device *dev;
441 struct comedi_subdevice *s;
72fd9fac 442 unsigned int size = 0;
a5011a26 443
be535c9a 444 dev = comedi_dev_get_from_minor(minor);
5e04c254 445 if (!dev)
c88db469
IA
446 return -ENODEV;
447
7f4656c5 448 mutex_lock(&dev->mutex);
da56fdc6 449 s = comedi_read_subdevice(dev, minor);
72fd9fac
HS
450 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
451 size = s->async->prealloc_bufsz / 1024;
7f4656c5 452 mutex_unlock(&dev->mutex);
a5011a26 453
be535c9a 454 comedi_dev_put(dev);
ecd56ff9 455 return snprintf(buf, PAGE_SIZE, "%u\n", size);
a5011a26
HS
456}
457
e56341ad 458static ssize_t read_buffer_kb_store(struct device *csdev,
a5011a26
HS
459 struct device_attribute *attr,
460 const char *buf, size_t count)
461{
c88db469 462 unsigned int minor = MINOR(csdev->devt);
7f4656c5
IA
463 struct comedi_device *dev;
464 struct comedi_subdevice *s;
72fd9fac
HS
465 unsigned int size;
466 int err;
467
468 err = kstrtouint(buf, 10, &size);
469 if (err)
470 return err;
471 if (size > (UINT_MAX / 1024))
a5011a26 472 return -EINVAL;
72fd9fac 473 size *= 1024;
a5011a26 474
be535c9a 475 dev = comedi_dev_get_from_minor(minor);
5e04c254 476 if (!dev)
c88db469
IA
477 return -ENODEV;
478
7f4656c5 479 mutex_lock(&dev->mutex);
da56fdc6 480 s = comedi_read_subdevice(dev, minor);
72fd9fac 481 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
482f0a21 482 err = resize_async_buffer(dev, s, size);
72fd9fac
HS
483 else
484 err = -EINVAL;
7f4656c5 485 mutex_unlock(&dev->mutex);
a5011a26 486
be535c9a 487 comedi_dev_put(dev);
72fd9fac 488 return err ? err : count;
a5011a26 489}
e56341ad 490static DEVICE_ATTR_RW(read_buffer_kb);
883db3d9 491
e56341ad 492static ssize_t max_write_buffer_kb_show(struct device *csdev,
a5011a26
HS
493 struct device_attribute *attr,
494 char *buf)
495{
c88db469 496 unsigned int minor = MINOR(csdev->devt);
7f4656c5
IA
497 struct comedi_device *dev;
498 struct comedi_subdevice *s;
72fd9fac 499 unsigned int size = 0;
a5011a26 500
be535c9a 501 dev = comedi_dev_get_from_minor(minor);
5e04c254 502 if (!dev)
c88db469
IA
503 return -ENODEV;
504
7f4656c5 505 mutex_lock(&dev->mutex);
da56fdc6 506 s = comedi_write_subdevice(dev, minor);
72fd9fac
HS
507 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
508 size = s->async->max_bufsize / 1024;
7f4656c5 509 mutex_unlock(&dev->mutex);
a5011a26 510
be535c9a 511 comedi_dev_put(dev);
ecd56ff9 512 return snprintf(buf, PAGE_SIZE, "%u\n", size);
a5011a26
HS
513}
514
e56341ad 515static ssize_t max_write_buffer_kb_store(struct device *csdev,
a5011a26
HS
516 struct device_attribute *attr,
517 const char *buf, size_t count)
518{
c88db469 519 unsigned int minor = MINOR(csdev->devt);
7f4656c5
IA
520 struct comedi_device *dev;
521 struct comedi_subdevice *s;
72fd9fac
HS
522 unsigned int size;
523 int err;
524
525 err = kstrtouint(buf, 10, &size);
526 if (err)
527 return err;
528 if (size > (UINT_MAX / 1024))
a5011a26 529 return -EINVAL;
72fd9fac 530 size *= 1024;
a5011a26 531
be535c9a 532 dev = comedi_dev_get_from_minor(minor);
5e04c254 533 if (!dev)
c88db469
IA
534 return -ENODEV;
535
7f4656c5 536 mutex_lock(&dev->mutex);
da56fdc6 537 s = comedi_write_subdevice(dev, minor);
72fd9fac
HS
538 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
539 s->async->max_bufsize = size;
540 else
541 err = -EINVAL;
7f4656c5 542 mutex_unlock(&dev->mutex);
a5011a26 543
be535c9a 544 comedi_dev_put(dev);
72fd9fac 545 return err ? err : count;
a5011a26 546}
e56341ad 547static DEVICE_ATTR_RW(max_write_buffer_kb);
a5011a26 548
e56341ad 549static ssize_t write_buffer_kb_show(struct device *csdev,
a5011a26
HS
550 struct device_attribute *attr, char *buf)
551{
c88db469 552 unsigned int minor = MINOR(csdev->devt);
7f4656c5
IA
553 struct comedi_device *dev;
554 struct comedi_subdevice *s;
72fd9fac 555 unsigned int size = 0;
a5011a26 556
be535c9a 557 dev = comedi_dev_get_from_minor(minor);
5e04c254 558 if (!dev)
c88db469
IA
559 return -ENODEV;
560
7f4656c5 561 mutex_lock(&dev->mutex);
da56fdc6 562 s = comedi_write_subdevice(dev, minor);
72fd9fac
HS
563 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
564 size = s->async->prealloc_bufsz / 1024;
7f4656c5 565 mutex_unlock(&dev->mutex);
a5011a26 566
be535c9a 567 comedi_dev_put(dev);
ecd56ff9 568 return snprintf(buf, PAGE_SIZE, "%u\n", size);
a5011a26
HS
569}
570
e56341ad 571static ssize_t write_buffer_kb_store(struct device *csdev,
a5011a26
HS
572 struct device_attribute *attr,
573 const char *buf, size_t count)
574{
c88db469 575 unsigned int minor = MINOR(csdev->devt);
7f4656c5
IA
576 struct comedi_device *dev;
577 struct comedi_subdevice *s;
72fd9fac
HS
578 unsigned int size;
579 int err;
580
581 err = kstrtouint(buf, 10, &size);
582 if (err)
583 return err;
584 if (size > (UINT_MAX / 1024))
a5011a26 585 return -EINVAL;
72fd9fac 586 size *= 1024;
a5011a26 587
be535c9a 588 dev = comedi_dev_get_from_minor(minor);
5e04c254 589 if (!dev)
c88db469
IA
590 return -ENODEV;
591
7f4656c5 592 mutex_lock(&dev->mutex);
da56fdc6 593 s = comedi_write_subdevice(dev, minor);
72fd9fac 594 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
482f0a21 595 err = resize_async_buffer(dev, s, size);
72fd9fac
HS
596 else
597 err = -EINVAL;
7f4656c5 598 mutex_unlock(&dev->mutex);
a5011a26 599
be535c9a 600 comedi_dev_put(dev);
72fd9fac 601 return err ? err : count;
a5011a26 602}
e56341ad
GKH
603static DEVICE_ATTR_RW(write_buffer_kb);
604
605static struct attribute *comedi_dev_attrs[] = {
606 &dev_attr_max_read_buffer_kb.attr,
607 &dev_attr_read_buffer_kb.attr,
608 &dev_attr_max_write_buffer_kb.attr,
609 &dev_attr_write_buffer_kb.attr,
610 NULL,
a5011a26 611};
e56341ad 612ATTRIBUTE_GROUPS(comedi_dev);
ed9eccbe 613
ef4b4b27 614static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
d4d47895 615 unsigned int bits)
ef4b4b27
IA
616{
617 s->runflags &= ~bits;
618}
619
620static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
d4d47895 621 unsigned int bits)
ef4b4b27
IA
622{
623 s->runflags |= bits;
624}
625
cc64ea42 626static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
b2073dcb
IA
627 unsigned int mask,
628 unsigned int bits)
2aae0076
HS
629{
630 unsigned long flags;
631
632 spin_lock_irqsave(&s->spin_lock, flags);
ef4b4b27
IA
633 __comedi_clear_subdevice_runflags(s, mask);
634 __comedi_set_subdevice_runflags(s, bits & mask);
2aae0076
HS
635 spin_unlock_irqrestore(&s->spin_lock, flags);
636}
637
d4d47895 638static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
ef4b4b27
IA
639{
640 return s->runflags;
641}
642
d4d47895 643static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice *s)
74120719
HS
644{
645 unsigned long flags;
d4d47895 646 unsigned int runflags;
74120719
HS
647
648 spin_lock_irqsave(&s->spin_lock, flags);
ef4b4b27 649 runflags = __comedi_get_subdevice_runflags(s);
74120719
HS
650 spin_unlock_irqrestore(&s->spin_lock, flags);
651 return runflags;
652}
74120719 653
d4d47895 654static bool comedi_is_runflags_running(unsigned int runflags)
b183a836
IA
655{
656 return runflags & COMEDI_SRF_RUNNING;
657}
658
d4d47895 659static bool comedi_is_runflags_in_error(unsigned int runflags)
b183a836
IA
660{
661 return runflags & COMEDI_SRF_ERROR;
662}
663
dd630cde 664/**
a3e39942
IA
665 * comedi_is_subdevice_running() - Check if async command running on subdevice
666 * @s: COMEDI subdevice.
dd630cde 667 *
a3e39942
IA
668 * Return: %true if an asynchronous COMEDI command is active on the
669 * subdevice, else %false.
dd630cde 670 */
e0dac318
HS
671bool comedi_is_subdevice_running(struct comedi_subdevice *s)
672{
d4d47895 673 unsigned int runflags = comedi_get_subdevice_runflags(s);
e0dac318 674
b183a836 675 return comedi_is_runflags_running(runflags);
e0dac318
HS
676}
677EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
678
ef4b4b27
IA
679static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
680{
d4d47895 681 unsigned int runflags = __comedi_get_subdevice_runflags(s);
ef4b4b27
IA
682
683 return comedi_is_runflags_running(runflags);
684}
685
8fc369ae
IA
686bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
687{
d4d47895 688 unsigned int runflags = __comedi_get_subdevice_runflags(s);
8fc369ae
IA
689
690 return runflags & COMEDI_SRF_FREE_SPRIV;
691}
692
693/**
a3e39942
IA
694 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
695 * @s: COMEDI subdevice.
8fc369ae
IA
696 *
697 * Mark the subdevice as having a pointer to private data that can be
a3e39942
IA
698 * automatically freed when the COMEDI device is detached from the low-level
699 * driver.
8fc369ae
IA
700 */
701void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
702{
703 __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
704}
705EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
706
588ba6dc 707/**
a3e39942
IA
708 * comedi_alloc_spriv - Allocate memory for the subdevice private data
709 * @s: COMEDI subdevice.
710 * @size: Size of the memory to allocate.
588ba6dc 711 *
a3e39942
IA
712 * Allocate memory for the subdevice private data and point @s->private
713 * to it. The memory will be freed automatically when the COMEDI device
714 * is detached from the low-level driver.
715 *
716 * Return: A pointer to the allocated memory @s->private on success.
717 * Return NULL on failure.
588ba6dc 718 */
0480bcb9 719void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
588ba6dc 720{
0480bcb9
HS
721 s->private = kzalloc(size, GFP_KERNEL);
722 if (s->private)
8fc369ae 723 comedi_set_spriv_auto_free(s);
0480bcb9 724 return s->private;
588ba6dc 725}
0480bcb9 726EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
588ba6dc 727
2aae0076 728/*
a2aab8b4 729 * This function restores a subdevice to an idle state.
2aae0076
HS
730 */
731static void do_become_nonbusy(struct comedi_device *dev,
732 struct comedi_subdevice *s)
733{
734 struct comedi_async *async = s->async;
735
77c21b62 736 lockdep_assert_held(&dev->mutex);
cc64ea42 737 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
2aae0076 738 if (async) {
fcc18a9a 739 comedi_buf_reset(s);
2aae0076
HS
740 async->inttrig = NULL;
741 kfree(async->cmd.chanlist);
742 async->cmd.chanlist = NULL;
8da8c86f 743 s->busy = NULL;
258c1dd7 744 wake_up_interruptible_all(&async->wait_head);
2aae0076
HS
745 } else {
746 dev_err(dev->class_dev,
e4f857f7 747 "BUG: (?) %s called with async=NULL\n", __func__);
8da8c86f 748 s->busy = NULL;
2aae0076 749 }
2aae0076
HS
750}
751
752static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
753{
754 int ret = 0;
755
77c21b62 756 lockdep_assert_held(&dev->mutex);
f0124630 757 if (comedi_is_subdevice_running(s) && s->cancel)
2aae0076
HS
758 ret = s->cancel(dev, s);
759
760 do_become_nonbusy(dev, s);
761
762 return ret;
763}
764
d19db51a
IA
765void comedi_device_cancel_all(struct comedi_device *dev)
766{
767 struct comedi_subdevice *s;
768 int i;
769
77c21b62 770 lockdep_assert_held(&dev->mutex);
d19db51a
IA
771 if (!dev->attached)
772 return;
773
774 for (i = 0; i < dev->n_subdevices; i++) {
775 s = &dev->subdevices[i];
776 if (s->async)
777 do_cancel(dev, s);
778 }
779}
780
2aae0076
HS
781static int is_device_busy(struct comedi_device *dev)
782{
783 struct comedi_subdevice *s;
784 int i;
785
77c21b62 786 lockdep_assert_held(&dev->mutex);
2aae0076
HS
787 if (!dev->attached)
788 return 0;
789
790 for (i = 0; i < dev->n_subdevices; i++) {
791 s = &dev->subdevices[i];
792 if (s->busy)
793 return 1;
d4526ab4 794 if (s->async && comedi_buf_is_mmapped(s))
2aae0076
HS
795 return 1;
796 }
797
798 return 0;
799}
800
ed9eccbe 801/*
18e01b24
IA
802 * COMEDI_DEVCONFIG ioctl
803 * attaches (and configures) or detaches a legacy device
804 *
805 * arg:
806 * pointer to comedi_devconfig structure (NULL if detaching)
807 *
808 * reads:
809 * comedi_devconfig structure (if attaching)
810 *
811 * writes:
812 * nothing
813 */
0a85b6f0 814static int do_devconfig_ioctl(struct comedi_device *dev,
92d0127c 815 struct comedi_devconfig __user *arg)
ed9eccbe 816{
0707bb04 817 struct comedi_devconfig it;
ed9eccbe 818
77c21b62 819 lockdep_assert_held(&dev->mutex);
ed9eccbe
DS
820 if (!capable(CAP_SYS_ADMIN))
821 return -EPERM;
822
88cc30cf 823 if (!arg) {
ed9eccbe
DS
824 if (is_device_busy(dev))
825 return -EBUSY;
476b8477 826 if (dev->attached) {
ed9eccbe 827 struct module *driver_module = dev->driver->module;
4bac39f6 828
ed9eccbe
DS
829 comedi_device_detach(dev);
830 module_put(driver_module);
831 }
832 return 0;
833 }
834
bc252fd1 835 if (copy_from_user(&it, arg, sizeof(it)))
ed9eccbe
DS
836 return -EFAULT;
837
838 it.board_name[COMEDI_NAMELEN - 1] = 0;
839
d1843132
HS
840 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
841 dev_warn(dev->class_dev,
842 "comedi_config --init_data is deprecated\n");
843 return -EINVAL;
ed9eccbe
DS
844 }
845
8ab4ed6e
IA
846 if (dev->minor >= comedi_num_legacy_minors)
847 /* don't re-use dynamically allocated comedi devices */
848 return -EBUSY;
849
b2a644b4
IA
850 /* This increments the driver module count on success. */
851 return comedi_device_attach(dev, &it);
ed9eccbe
DS
852}
853
854/*
18e01b24
IA
855 * COMEDI_BUFCONFIG ioctl
856 * buffer configuration
857 *
858 * arg:
859 * pointer to comedi_bufconfig structure
860 *
861 * reads:
862 * comedi_bufconfig structure
863 *
864 * writes:
865 * modified comedi_bufconfig structure
866 */
92d0127c
GKH
867static int do_bufconfig_ioctl(struct comedi_device *dev,
868 struct comedi_bufconfig __user *arg)
ed9eccbe 869{
be6aba4a 870 struct comedi_bufconfig bc;
d163679c 871 struct comedi_async *async;
34c43922 872 struct comedi_subdevice *s;
883db3d9 873 int retval = 0;
ed9eccbe 874
77c21b62 875 lockdep_assert_held(&dev->mutex);
bc252fd1 876 if (copy_from_user(&bc, arg, sizeof(bc)))
ed9eccbe
DS
877 return -EFAULT;
878
11f80ddf 879 if (bc.subdevice >= dev->n_subdevices)
ed9eccbe
DS
880 return -EINVAL;
881
b077f2cd 882 s = &dev->subdevices[bc.subdevice];
ed9eccbe
DS
883 async = s->async;
884
885 if (!async) {
272850f0
HS
886 dev_dbg(dev->class_dev,
887 "subdevice does not have async capability\n");
ed9eccbe
DS
888 bc.size = 0;
889 bc.maximum_size = 0;
890 goto copyback;
891 }
892
893 if (bc.maximum_size) {
894 if (!capable(CAP_SYS_ADMIN))
895 return -EPERM;
896
897 async->max_bufsize = bc.maximum_size;
898 }
899
900 if (bc.size) {
482f0a21 901 retval = resize_async_buffer(dev, s, bc.size);
883db3d9
FMH
902 if (retval < 0)
903 return retval;
ed9eccbe
DS
904 }
905
906 bc.size = async->prealloc_bufsz;
907 bc.maximum_size = async->max_bufsize;
908
476b8477 909copyback:
bc252fd1 910 if (copy_to_user(arg, &bc, sizeof(bc)))
ed9eccbe
DS
911 return -EFAULT;
912
913 return 0;
914}
915
916/*
18e01b24
IA
917 * COMEDI_DEVINFO ioctl
918 * device info
919 *
920 * arg:
921 * pointer to comedi_devinfo structure
922 *
923 * reads:
924 * nothing
925 *
926 * writes:
927 * comedi_devinfo structure
928 */
0a85b6f0 929static int do_devinfo_ioctl(struct comedi_device *dev,
92d0127c
GKH
930 struct comedi_devinfo __user *arg,
931 struct file *file)
ed9eccbe 932{
0e700923
HS
933 struct comedi_subdevice *s;
934 struct comedi_devinfo devinfo;
ed9eccbe 935
77c21b62 936 lockdep_assert_held(&dev->mutex);
ed9eccbe
DS
937 memset(&devinfo, 0, sizeof(devinfo));
938
939 /* fill devinfo structure */
940 devinfo.version_code = COMEDI_VERSION_CODE;
941 devinfo.n_subdevs = dev->n_subdevices;
819cbb12
VK
942 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
943 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
ed9eccbe 944
20f083c0 945 s = comedi_file_read_subdevice(file);
0e700923 946 if (s)
90a35c15 947 devinfo.read_subdevice = s->index;
476b8477 948 else
ed9eccbe 949 devinfo.read_subdevice = -1;
476b8477 950
20f083c0 951 s = comedi_file_write_subdevice(file);
0e700923 952 if (s)
90a35c15 953 devinfo.write_subdevice = s->index;
476b8477 954 else
ed9eccbe 955 devinfo.write_subdevice = -1;
ed9eccbe 956
bc252fd1 957 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
ed9eccbe
DS
958 return -EFAULT;
959
960 return 0;
961}
962
963/*
18e01b24
IA
964 * COMEDI_SUBDINFO ioctl
965 * subdevices info
966 *
967 * arg:
968 * pointer to array of comedi_subdinfo structures
969 *
970 * reads:
971 * nothing
972 *
973 * writes:
974 * array of comedi_subdinfo structures
975 */
0a85b6f0 976static int do_subdinfo_ioctl(struct comedi_device *dev,
92d0127c 977 struct comedi_subdinfo __user *arg, void *file)
ed9eccbe
DS
978{
979 int ret, i;
bd52efbb 980 struct comedi_subdinfo *tmp, *us;
34c43922 981 struct comedi_subdevice *s;
ed9eccbe 982
77c21b62 983 lockdep_assert_held(&dev->mutex);
bc252fd1 984 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
ed9eccbe
DS
985 if (!tmp)
986 return -ENOMEM;
987
988 /* fill subdinfo structs */
989 for (i = 0; i < dev->n_subdevices; i++) {
b077f2cd 990 s = &dev->subdevices[i];
ed9eccbe
DS
991 us = tmp + i;
992
993 us->type = s->type;
994 us->n_chan = s->n_chan;
995 us->subd_flags = s->subdev_flags;
f0124630 996 if (comedi_is_subdevice_running(s))
ed9eccbe
DS
997 us->subd_flags |= SDF_RUNNING;
998#define TIMER_nanosec 5 /* backwards compatibility */
999 us->timer_type = TIMER_nanosec;
1000 us->len_chanlist = s->len_chanlist;
1001 us->maxdata = s->maxdata;
1002 if (s->range_table) {
1003 us->range_type =
476b8477 1004 (i << 24) | (0 << 16) | (s->range_table->length);
ed9eccbe
DS
1005 } else {
1006 us->range_type = 0; /* XXX */
1007 }
ed9eccbe
DS
1008
1009 if (s->busy)
1010 us->subd_flags |= SDF_BUSY;
1011 if (s->busy == file)
1012 us->subd_flags |= SDF_BUSY_OWNER;
1013 if (s->lock)
1014 us->subd_flags |= SDF_LOCKED;
1015 if (s->lock == file)
1016 us->subd_flags |= SDF_LOCK_OWNER;
1017 if (!s->maxdata && s->maxdata_list)
1018 us->subd_flags |= SDF_MAXDATA;
ed9eccbe
DS
1019 if (s->range_table_list)
1020 us->subd_flags |= SDF_RANGETYPE;
1021 if (s->do_cmd)
1022 us->subd_flags |= SDF_CMD;
1023
1024 if (s->insn_bits != &insn_inval)
1025 us->insn_bits_support = COMEDI_SUPPORTED;
1026 else
1027 us->insn_bits_support = COMEDI_UNSUPPORTED;
ed9eccbe
DS
1028 }
1029
bc252fd1 1030 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
ed9eccbe
DS
1031
1032 kfree(tmp);
1033
1034 return ret ? -EFAULT : 0;
1035}
1036
1037/*
18e01b24
IA
1038 * COMEDI_CHANINFO ioctl
1039 * subdevice channel info
1040 *
1041 * arg:
1042 * pointer to comedi_chaninfo structure
1043 *
1044 * reads:
1045 * comedi_chaninfo structure
1046 *
1047 * writes:
1048 * array of maxdata values to chaninfo->maxdata_list if requested
1049 * array of range table lengths to chaninfo->range_table_list if requested
1050 */
0a85b6f0 1051static int do_chaninfo_ioctl(struct comedi_device *dev,
3fbfd222 1052 struct comedi_chaninfo *it)
ed9eccbe 1053{
34c43922 1054 struct comedi_subdevice *s;
ed9eccbe 1055
77c21b62 1056 lockdep_assert_held(&dev->mutex);
ed9eccbe 1057
3fbfd222 1058 if (it->subdev >= dev->n_subdevices)
ed9eccbe 1059 return -EINVAL;
3fbfd222 1060 s = &dev->subdevices[it->subdev];
ed9eccbe 1061
3fbfd222 1062 if (it->maxdata_list) {
ed9eccbe
DS
1063 if (s->maxdata || !s->maxdata_list)
1064 return -EINVAL;
3fbfd222 1065 if (copy_to_user(it->maxdata_list, s->maxdata_list,
790c5541 1066 s->n_chan * sizeof(unsigned int)))
ed9eccbe
DS
1067 return -EFAULT;
1068 }
1069
3fbfd222 1070 if (it->flaglist)
64d9b1d2 1071 return -EINVAL; /* flaglist not supported */
ed9eccbe 1072
3fbfd222 1073 if (it->rangelist) {
ed9eccbe
DS
1074 int i;
1075
1076 if (!s->range_table_list)
1077 return -EINVAL;
1078 for (i = 0; i < s->n_chan; i++) {
1079 int x;
1080
3fbfd222 1081 x = (dev->minor << 28) | (it->subdev << 24) | (i << 16) |
476b8477 1082 (s->range_table_list[i]->length);
3fbfd222 1083 if (put_user(x, it->rangelist + i))
81604d43 1084 return -EFAULT;
ed9eccbe 1085 }
ed9eccbe
DS
1086 }
1087
1088 return 0;
1089}
1090
18e01b24
IA
1091/*
1092 * COMEDI_BUFINFO ioctl
1093 * buffer information
1094 *
1095 * arg:
1096 * pointer to comedi_bufinfo structure
1097 *
1098 * reads:
1099 * comedi_bufinfo structure
1100 *
1101 * writes:
1102 * modified comedi_bufinfo structure
1103 */
92d0127c 1104static int do_bufinfo_ioctl(struct comedi_device *dev,
53fa827e 1105 struct comedi_bufinfo __user *arg, void *file)
ed9eccbe 1106{
9aa5339a 1107 struct comedi_bufinfo bi;
34c43922 1108 struct comedi_subdevice *s;
d163679c 1109 struct comedi_async *async;
36a51170
IA
1110 unsigned int runflags;
1111 int retval = 0;
06578561 1112 bool become_nonbusy = false;
ed9eccbe 1113
77c21b62 1114 lockdep_assert_held(&dev->mutex);
bc252fd1 1115 if (copy_from_user(&bi, arg, sizeof(bi)))
ed9eccbe
DS
1116 return -EFAULT;
1117
7b1a5e2f 1118 if (bi.subdevice >= dev->n_subdevices)
ed9eccbe
DS
1119 return -EINVAL;
1120
b077f2cd 1121 s = &dev->subdevices[bi.subdevice];
53fa827e 1122
ed9eccbe
DS
1123 async = s->async;
1124
57c563bf
IA
1125 if (!async || s->busy != file)
1126 return -EINVAL;
ed9eccbe 1127
be611a1d 1128 runflags = comedi_get_subdevice_runflags(s);
66c36502
IA
1129 if (!(async->cmd.flags & CMDF_WRITE)) {
1130 /* command was set up in "read" direction */
1131 if (bi.bytes_read) {
1132 comedi_buf_read_alloc(s, bi.bytes_read);
1133 bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read);
ed9eccbe 1134 }
36a51170
IA
1135 /*
1136 * If nothing left to read, and command has stopped, and
1137 * {"read" position not updated or command stopped normally},
1138 * then become non-busy.
1139 */
36a51170
IA
1140 if (comedi_buf_read_n_available(s) == 0 &&
1141 !comedi_is_runflags_running(runflags) &&
1142 (bi.bytes_read == 0 ||
1143 !comedi_is_runflags_in_error(runflags))) {
f3aa8c0b 1144 become_nonbusy = true;
36a51170
IA
1145 if (comedi_is_runflags_in_error(runflags))
1146 retval = -EPIPE;
1147 }
66c36502
IA
1148 bi.bytes_written = 0;
1149 } else {
1150 /* command was set up in "write" direction */
be611a1d 1151 if (!comedi_is_runflags_running(runflags)) {
bb0c6bfa 1152 bi.bytes_written = 0;
be611a1d
IA
1153 become_nonbusy = true;
1154 if (comedi_is_runflags_in_error(runflags))
1155 retval = -EPIPE;
1156 } else if (bi.bytes_written) {
66c36502
IA
1157 comedi_buf_write_alloc(s, bi.bytes_written);
1158 bi.bytes_written =
1159 comedi_buf_write_free(s, bi.bytes_written);
1160 }
1161 bi.bytes_read = 0;
ed9eccbe
DS
1162 }
1163
1164 bi.buf_write_count = async->buf_write_count;
1165 bi.buf_write_ptr = async->buf_write_ptr;
1166 bi.buf_read_count = async->buf_read_count;
1167 bi.buf_read_ptr = async->buf_read_ptr;
1168
06578561
IA
1169 if (become_nonbusy)
1170 do_become_nonbusy(dev, s);
1171
36a51170
IA
1172 if (retval)
1173 return retval;
1174
bc252fd1 1175 if (copy_to_user(arg, &bi, sizeof(bi)))
ed9eccbe
DS
1176 return -EFAULT;
1177
1178 return 0;
1179}
1180
0a85b6f0
MT
1181static int check_insn_config_length(struct comedi_insn *insn,
1182 unsigned int *data)
ed9eccbe 1183{
476b8477
GKH
1184 if (insn->n < 1)
1185 return -EINVAL;
ed9eccbe
DS
1186
1187 switch (data[0]) {
1188 case INSN_CONFIG_DIO_OUTPUT:
1189 case INSN_CONFIG_DIO_INPUT:
1190 case INSN_CONFIG_DISARM:
1191 case INSN_CONFIG_RESET:
1192 if (insn->n == 1)
1193 return 0;
1194 break;
1195 case INSN_CONFIG_ARM:
1196 case INSN_CONFIG_DIO_QUERY:
1197 case INSN_CONFIG_BLOCK_SIZE:
1198 case INSN_CONFIG_FILTER:
1199 case INSN_CONFIG_SERIAL_CLOCK:
1200 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1201 case INSN_CONFIG_ALT_SOURCE:
1202 case INSN_CONFIG_SET_COUNTER_MODE:
1203 case INSN_CONFIG_8254_READ_STATUS:
1204 case INSN_CONFIG_SET_ROUTING:
1205 case INSN_CONFIG_GET_ROUTING:
1206 case INSN_CONFIG_GET_PWM_STATUS:
1207 case INSN_CONFIG_PWM_SET_PERIOD:
1208 case INSN_CONFIG_PWM_GET_PERIOD:
1209 if (insn->n == 2)
1210 return 0;
1211 break;
1212 case INSN_CONFIG_SET_GATE_SRC:
1213 case INSN_CONFIG_GET_GATE_SRC:
1214 case INSN_CONFIG_SET_CLOCK_SRC:
1215 case INSN_CONFIG_GET_CLOCK_SRC:
1216 case INSN_CONFIG_SET_OTHER_SRC:
1217 case INSN_CONFIG_GET_COUNTER_STATUS:
1218 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1219 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1220 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1221 if (insn->n == 3)
1222 return 0;
1223 break;
1224 case INSN_CONFIG_PWM_OUTPUT:
1225 case INSN_CONFIG_ANALOG_TRIG:
e3b9ea9a 1226 case INSN_CONFIG_TIMER_1:
ed9eccbe
DS
1227 if (insn->n == 5)
1228 return 0;
1229 break;
b0a2b6d8
IA
1230 case INSN_CONFIG_DIGITAL_TRIG:
1231 if (insn->n == 6)
1232 return 0;
1233 break;
832f3336
SO
1234 case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS:
1235 if (insn->n >= 4)
1236 return 0;
1237 break;
a2aab8b4
IA
1238 /*
1239 * by default we allow the insn since we don't have checks for
1240 * all possible cases yet
1241 */
ed9eccbe 1242 default:
c2ad078b 1243 pr_warn("No check for data length of config insn id %i is implemented\n",
4f870fe6 1244 data[0]);
c2ad078b
HS
1245 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1246 pr_warn("Assuming n=%i is correct\n", insn->n);
ed9eccbe 1247 return 0;
ed9eccbe
DS
1248 }
1249 return -EINVAL;
1250}
1251
d7569ad7
SO
1252static int check_insn_device_config_length(struct comedi_insn *insn,
1253 unsigned int *data)
1254{
1255 if (insn->n < 1)
1256 return -EINVAL;
1257
1258 switch (data[0]) {
1259 case INSN_DEVICE_CONFIG_TEST_ROUTE:
1260 case INSN_DEVICE_CONFIG_CONNECT_ROUTE:
1261 case INSN_DEVICE_CONFIG_DISCONNECT_ROUTE:
1262 if (insn->n == 3)
1263 return 0;
1264 break;
1265 case INSN_DEVICE_CONFIG_GET_ROUTES:
1266 /*
1267 * Big enough for config_id and the length of the userland
1268 * memory buffer. Additional length should be in factors of 2
1269 * to communicate any returned route pairs (source,destination).
1270 */
1271 if (insn->n >= 2)
1272 return 0;
1273 break;
1274 }
1275 return -EINVAL;
1276}
1277
1278/**
1279 * get_valid_routes() - Calls low-level driver get_valid_routes function to
1280 * either return a count of valid routes to user, or copy
1281 * of list of all valid device routes to buffer in
1282 * userspace.
1283 * @dev: comedi device pointer
1284 * @data: data from user insn call. The length of the data must be >= 2.
1285 * data[0] must contain the INSN_DEVICE_CONFIG config_id.
1286 * data[1](input) contains the number of _pairs_ for which memory is
1287 * allotted from the user. If the user specifies '0', then only
1288 * the number of pairs available is returned.
1289 * data[1](output) returns either the number of pairs available (if none
1290 * where requested) or the number of _pairs_ that are copied back
1291 * to the user.
1292 * data[2::2] returns each (source, destination) pair.
1293 *
1294 * Return: -EINVAL if low-level driver does not allocate and return routes as
1295 * expected. Returns 0 otherwise.
1296 */
1297static int get_valid_routes(struct comedi_device *dev, unsigned int *data)
1298{
77c21b62 1299 lockdep_assert_held(&dev->mutex);
d7569ad7
SO
1300 data[1] = dev->get_valid_routes(dev, data[1], data + 2);
1301 return 0;
1302}
1303
0a85b6f0
MT
1304static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1305 unsigned int *data, void *file)
ed9eccbe 1306{
34c43922 1307 struct comedi_subdevice *s;
ed9eccbe
DS
1308 int ret = 0;
1309 int i;
1310
77c21b62 1311 lockdep_assert_held(&dev->mutex);
ed9eccbe
DS
1312 if (insn->insn & INSN_MASK_SPECIAL) {
1313 /* a non-subdevice instruction */
1314
1315 switch (insn->insn) {
1316 case INSN_GTOD:
1317 {
70db384c 1318 struct timespec64 tv;
ed9eccbe
DS
1319
1320 if (insn->n != 2) {
1321 ret = -EINVAL;
1322 break;
1323 }
1324
70db384c
AB
1325 ktime_get_real_ts64(&tv);
1326 /* unsigned data safe until 2106 */
1327 data[0] = (unsigned int)tv.tv_sec;
1328 data[1] = tv.tv_nsec / NSEC_PER_USEC;
ed9eccbe
DS
1329 ret = 2;
1330
1331 break;
1332 }
1333 case INSN_WAIT:
1334 if (insn->n != 1 || data[0] >= 100000) {
1335 ret = -EINVAL;
1336 break;
1337 }
1338 udelay(data[0] / 1000);
1339 ret = 1;
1340 break;
1341 case INSN_INTTRIG:
1342 if (insn->n != 1) {
1343 ret = -EINVAL;
1344 break;
1345 }
1346 if (insn->subdev >= dev->n_subdevices) {
272850f0
HS
1347 dev_dbg(dev->class_dev,
1348 "%d not usable subdevice\n",
ed9eccbe
DS
1349 insn->subdev);
1350 ret = -EINVAL;
1351 break;
1352 }
b077f2cd 1353 s = &dev->subdevices[insn->subdev];
ed9eccbe 1354 if (!s->async) {
272850f0 1355 dev_dbg(dev->class_dev, "no async\n");
ed9eccbe
DS
1356 ret = -EINVAL;
1357 break;
1358 }
1359 if (!s->async->inttrig) {
272850f0 1360 dev_dbg(dev->class_dev, "no inttrig\n");
ed9eccbe
DS
1361 ret = -EAGAIN;
1362 break;
1363 }
5d06e3df 1364 ret = s->async->inttrig(dev, s, data[0]);
ed9eccbe
DS
1365 if (ret >= 0)
1366 ret = 1;
1367 break;
d7569ad7
SO
1368 case INSN_DEVICE_CONFIG:
1369 ret = check_insn_device_config_length(insn, data);
1370 if (ret)
1371 break;
1372
1373 if (data[0] == INSN_DEVICE_CONFIG_GET_ROUTES) {
1374 /*
1375 * data[1] should be the number of _pairs_ that
1376 * the memory can hold.
1377 */
1378 data[1] = (insn->n - 2) / 2;
1379 ret = get_valid_routes(dev, data);
1380 break;
1381 }
1382
1383 /* other global device config instructions. */
1384 ret = dev->insn_device_config(dev, insn, data);
1385 break;
ed9eccbe 1386 default:
272850f0 1387 dev_dbg(dev->class_dev, "invalid insn\n");
ed9eccbe
DS
1388 ret = -EINVAL;
1389 break;
1390 }
1391 } else {
1392 /* a subdevice instruction */
790c5541 1393 unsigned int maxdata;
ed9eccbe
DS
1394
1395 if (insn->subdev >= dev->n_subdevices) {
272850f0
HS
1396 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1397 insn->subdev);
ed9eccbe
DS
1398 ret = -EINVAL;
1399 goto out;
1400 }
b077f2cd 1401 s = &dev->subdevices[insn->subdev];
ed9eccbe
DS
1402
1403 if (s->type == COMEDI_SUBD_UNUSED) {
272850f0
HS
1404 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1405 insn->subdev);
ed9eccbe
DS
1406 ret = -EIO;
1407 goto out;
1408 }
1409
1410 /* are we locked? (ioctl lock) */
1411 if (s->lock && s->lock != file) {
272850f0 1412 dev_dbg(dev->class_dev, "device locked\n");
ed9eccbe
DS
1413 ret = -EACCES;
1414 goto out;
1415 }
1416
0fd0ca75 1417 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
476b8477 1418 if (ret < 0) {
ed9eccbe 1419 ret = -EINVAL;
272850f0 1420 dev_dbg(dev->class_dev, "bad chanspec\n");
ed9eccbe
DS
1421 goto out;
1422 }
1423
1424 if (s->busy) {
1425 ret = -EBUSY;
1426 goto out;
1427 }
1428 /* This looks arbitrary. It is. */
30cc9bd6 1429 s->busy = parse_insn;
ed9eccbe
DS
1430 switch (insn->insn) {
1431 case INSN_READ:
1432 ret = s->insn_read(dev, s, insn, data);
22ca19d9
HS
1433 if (ret == -ETIMEDOUT) {
1434 dev_dbg(dev->class_dev,
1435 "subdevice %d read instruction timed out\n",
1436 s->index);
1437 }
ed9eccbe
DS
1438 break;
1439 case INSN_WRITE:
1440 maxdata = s->maxdata_list
476b8477
GKH
1441 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1442 : s->maxdata;
ed9eccbe
DS
1443 for (i = 0; i < insn->n; ++i) {
1444 if (data[i] > maxdata) {
1445 ret = -EINVAL;
272850f0
HS
1446 dev_dbg(dev->class_dev,
1447 "bad data value(s)\n");
ed9eccbe
DS
1448 break;
1449 }
1450 }
22ca19d9 1451 if (ret == 0) {
ed9eccbe 1452 ret = s->insn_write(dev, s, insn, data);
22ca19d9
HS
1453 if (ret == -ETIMEDOUT) {
1454 dev_dbg(dev->class_dev,
1455 "subdevice %d write instruction timed out\n",
1456 s->index);
1457 }
1458 }
ed9eccbe
DS
1459 break;
1460 case INSN_BITS:
1461 if (insn->n != 2) {
1462 ret = -EINVAL;
2f644ccf 1463 } else {
a2aab8b4
IA
1464 /*
1465 * Most drivers ignore the base channel in
2f644ccf 1466 * insn->chanspec. Fix this here if
a2aab8b4
IA
1467 * the subdevice has <= 32 channels.
1468 */
36efbacd
HS
1469 unsigned int orig_mask = data[0];
1470 unsigned int shift = 0;
2f644ccf 1471
2f644ccf
IA
1472 if (s->n_chan <= 32) {
1473 shift = CR_CHAN(insn->chanspec);
1474 if (shift > 0) {
1475 insn->chanspec = 0;
1476 data[0] <<= shift;
1477 data[1] <<= shift;
1478 }
36efbacd 1479 }
2f644ccf
IA
1480 ret = s->insn_bits(dev, s, insn, data);
1481 data[0] = orig_mask;
1482 if (shift > 0)
1483 data[1] >>= shift;
ed9eccbe 1484 }
ed9eccbe
DS
1485 break;
1486 case INSN_CONFIG:
1487 ret = check_insn_config_length(insn, data);
1488 if (ret)
1489 break;
1490 ret = s->insn_config(dev, s, insn, data);
1491 break;
1492 default:
1493 ret = -EINVAL;
1494 break;
1495 }
1496
1497 s->busy = NULL;
1498 }
1499
476b8477 1500out:
ed9eccbe
DS
1501 return ret;
1502}
1503
5b6cbd87 1504/*
18e01b24
IA
1505 * COMEDI_INSNLIST ioctl
1506 * synchronous instruction list
5b6cbd87 1507 *
18e01b24
IA
1508 * arg:
1509 * pointer to comedi_insnlist structure
5b6cbd87 1510 *
18e01b24
IA
1511 * reads:
1512 * comedi_insnlist structure
1513 * array of comedi_insn structures from insnlist->insns pointer
1514 * data (for writes) from insns[].data pointers
5b6cbd87 1515 *
18e01b24
IA
1516 * writes:
1517 * data (for reads) to insns[].data pointers
5b6cbd87
HS
1518 */
1519/* arbitrary limits */
f8bc1b2e
SO
1520#define MIN_SAMPLES 16
1521#define MAX_SAMPLES 65536
5b6cbd87
HS
1522static int do_insnlist_ioctl(struct comedi_device *dev,
1523 struct comedi_insnlist __user *arg, void *file)
1524{
1525 struct comedi_insnlist insnlist;
1526 struct comedi_insn *insns = NULL;
1527 unsigned int *data = NULL;
f8bc1b2e 1528 unsigned int max_n_data_required = MIN_SAMPLES;
5b6cbd87
HS
1529 int i = 0;
1530 int ret = 0;
1531
77c21b62 1532 lockdep_assert_held(&dev->mutex);
5b6cbd87
HS
1533 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1534 return -EFAULT;
1535
5b6cbd87
HS
1536 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1537 if (!insns) {
5b6cbd87
HS
1538 ret = -ENOMEM;
1539 goto error;
1540 }
1541
1542 if (copy_from_user(insns, insnlist.insns,
1543 sizeof(*insns) * insnlist.n_insns)) {
272850f0 1544 dev_dbg(dev->class_dev, "copy_from_user failed\n");
5b6cbd87
HS
1545 ret = -EFAULT;
1546 goto error;
1547 }
1548
f8bc1b2e
SO
1549 /* Determine maximum memory needed for all instructions. */
1550 for (i = 0; i < insnlist.n_insns; ++i) {
5b6cbd87 1551 if (insns[i].n > MAX_SAMPLES) {
272850f0
HS
1552 dev_dbg(dev->class_dev,
1553 "number of samples too large\n");
5b6cbd87
HS
1554 ret = -EINVAL;
1555 goto error;
1556 }
f8bc1b2e
SO
1557 max_n_data_required = max(max_n_data_required, insns[i].n);
1558 }
1559
1560 /* Allocate scratch space for all instruction data. */
1561 data = kmalloc_array(max_n_data_required, sizeof(unsigned int),
1562 GFP_KERNEL);
1563 if (!data) {
1564 ret = -ENOMEM;
1565 goto error;
1566 }
1567
1568 for (i = 0; i < insnlist.n_insns; ++i) {
5b6cbd87
HS
1569 if (insns[i].insn & INSN_MASK_WRITE) {
1570 if (copy_from_user(data, insns[i].data,
1571 insns[i].n * sizeof(unsigned int))) {
272850f0
HS
1572 dev_dbg(dev->class_dev,
1573 "copy_from_user failed\n");
5b6cbd87
HS
1574 ret = -EFAULT;
1575 goto error;
1576 }
1577 }
1578 ret = parse_insn(dev, insns + i, data, file);
1579 if (ret < 0)
1580 goto error;
1581 if (insns[i].insn & INSN_MASK_READ) {
1582 if (copy_to_user(insns[i].data, data,
1583 insns[i].n * sizeof(unsigned int))) {
272850f0
HS
1584 dev_dbg(dev->class_dev,
1585 "copy_to_user failed\n");
5b6cbd87
HS
1586 ret = -EFAULT;
1587 goto error;
1588 }
1589 }
1590 if (need_resched())
1591 schedule();
1592 }
1593
1594error:
1595 kfree(insns);
1596 kfree(data);
1597
1598 if (ret < 0)
1599 return ret;
1600 return i;
1601}
1602
ed9eccbe 1603/*
18e01b24
IA
1604 * COMEDI_INSN ioctl
1605 * synchronous instruction
ed9eccbe 1606 *
18e01b24
IA
1607 * arg:
1608 * pointer to comedi_insn structure
ed9eccbe 1609 *
18e01b24
IA
1610 * reads:
1611 * comedi_insn structure
1612 * data (for writes) from insn->data pointer
ed9eccbe 1613 *
18e01b24
IA
1614 * writes:
1615 * data (for reads) to insn->data pointer
ed9eccbe 1616 */
92d0127c
GKH
1617static int do_insn_ioctl(struct comedi_device *dev,
1618 struct comedi_insn __user *arg, void *file)
ed9eccbe 1619{
90035c08 1620 struct comedi_insn insn;
790c5541 1621 unsigned int *data = NULL;
f8bc1b2e 1622 unsigned int n_data = MIN_SAMPLES;
ed9eccbe
DS
1623 int ret = 0;
1624
77c21b62 1625 lockdep_assert_held(&dev->mutex);
56eec180 1626 if (copy_from_user(&insn, arg, sizeof(insn)))
f8bc1b2e 1627 return -EFAULT;
56eec180 1628
f8bc1b2e
SO
1629 n_data = max(n_data, insn.n);
1630
ed9eccbe 1631 /* This is where the behavior of insn and insnlist deviate. */
f8bc1b2e 1632 if (insn.n > MAX_SAMPLES) {
ed9eccbe 1633 insn.n = MAX_SAMPLES;
f8bc1b2e
SO
1634 n_data = MAX_SAMPLES;
1635 }
1636
1637 data = kmalloc_array(n_data, sizeof(unsigned int), GFP_KERNEL);
1638 if (!data) {
1639 ret = -ENOMEM;
1640 goto error;
1641 }
1642
ed9eccbe 1643 if (insn.insn & INSN_MASK_WRITE) {
21fe2eea
M
1644 if (copy_from_user(data,
1645 insn.data,
1646 insn.n * sizeof(unsigned int))) {
ed9eccbe
DS
1647 ret = -EFAULT;
1648 goto error;
1649 }
1650 }
1651 ret = parse_insn(dev, &insn, data, file);
1652 if (ret < 0)
1653 goto error;
1654 if (insn.insn & INSN_MASK_READ) {
21fe2eea
M
1655 if (copy_to_user(insn.data,
1656 data,
1657 insn.n * sizeof(unsigned int))) {
ed9eccbe
DS
1658 ret = -EFAULT;
1659 goto error;
1660 }
1661 }
1662 ret = insn.n;
1663
476b8477
GKH
1664error:
1665 kfree(data);
ed9eccbe
DS
1666
1667 return ret;
1668}
1669
87ece583
HS
1670static int __comedi_get_user_cmd(struct comedi_device *dev,
1671 struct comedi_cmd __user *arg,
1672 struct comedi_cmd *cmd)
ed9eccbe 1673{
34c43922 1674 struct comedi_subdevice *s;
ed9eccbe 1675
77c21b62 1676 lockdep_assert_held(&dev->mutex);
87ece583 1677 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
272850f0 1678 dev_dbg(dev->class_dev, "bad cmd address\n");
ed9eccbe
DS
1679 return -EFAULT;
1680 }
ed9eccbe 1681
87ece583
HS
1682 if (cmd->subdev >= dev->n_subdevices) {
1683 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
ed9eccbe
DS
1684 return -ENODEV;
1685 }
1686
87ece583 1687 s = &dev->subdevices[cmd->subdev];
ed9eccbe
DS
1688
1689 if (s->type == COMEDI_SUBD_UNUSED) {
b2f48741
YD
1690 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1691 cmd->subdev);
ed9eccbe
DS
1692 return -EIO;
1693 }
1694
1695 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
272850f0 1696 dev_dbg(dev->class_dev,
b2f48741
YD
1697 "subdevice %d does not support commands\n",
1698 cmd->subdev);
ed9eccbe
DS
1699 return -EIO;
1700 }
1701
87ece583
HS
1702 /* make sure channel/gain list isn't too long */
1703 if (cmd->chanlist_len > s->len_chanlist) {
1704 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1705 cmd->chanlist_len, s->len_chanlist);
1706 return -EINVAL;
1707 }
1708
5d070cf2
IA
1709 /*
1710 * Set the CMDF_WRITE flag to the correct state if the subdevice
1711 * supports only "read" commands or only "write" commands.
1712 */
1713 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1714 case SDF_CMD_READ:
1715 cmd->flags &= ~CMDF_WRITE;
1716 break;
1717 case SDF_CMD_WRITE:
1718 cmd->flags |= CMDF_WRITE;
1719 break;
1720 default:
1721 break;
1722 }
1723
87ece583
HS
1724 return 0;
1725}
1726
c6cd0eef
HS
1727static int __comedi_get_user_chanlist(struct comedi_device *dev,
1728 struct comedi_subdevice *s,
1729 unsigned int __user *user_chanlist,
1730 struct comedi_cmd *cmd)
1731{
1732 unsigned int *chanlist;
1733 int ret;
1734
77c21b62 1735 lockdep_assert_held(&dev->mutex);
238b5ad8 1736 cmd->chanlist = NULL;
c6cd0eef
HS
1737 chanlist = memdup_user(user_chanlist,
1738 cmd->chanlist_len * sizeof(unsigned int));
1739 if (IS_ERR(chanlist))
1740 return PTR_ERR(chanlist);
1741
1742 /* make sure each element in channel/gain list is valid */
1743 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1744 if (ret < 0) {
1745 kfree(chanlist);
1746 return ret;
1747 }
1748
1749 cmd->chanlist = chanlist;
1750
1751 return 0;
1752}
1753
18e01b24
IA
1754/*
1755 * COMEDI_CMD ioctl
1756 * asynchronous acquisition command set-up
1757 *
1758 * arg:
1759 * pointer to comedi_cmd structure
1760 *
1761 * reads:
1762 * comedi_cmd structure
1763 * channel/range list from cmd->chanlist pointer
1764 *
1765 * writes:
1766 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1767 */
87ece583
HS
1768static int do_cmd_ioctl(struct comedi_device *dev,
1769 struct comedi_cmd __user *arg, void *file)
1770{
1771 struct comedi_cmd cmd;
1772 struct comedi_subdevice *s;
1773 struct comedi_async *async;
1774 unsigned int __user *user_chanlist;
1775 int ret;
1776
77c21b62
IA
1777 lockdep_assert_held(&dev->mutex);
1778
87ece583
HS
1779 /* get the user's cmd and do some simple validation */
1780 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1781 if (ret)
1782 return ret;
1783
1784 /* save user's chanlist pointer so it can be restored later */
1785 user_chanlist = (unsigned int __user *)cmd.chanlist;
1786
1787 s = &dev->subdevices[cmd.subdev];
1788 async = s->async;
1789
ed9eccbe
DS
1790 /* are we locked? (ioctl lock) */
1791 if (s->lock && s->lock != file) {
272850f0 1792 dev_dbg(dev->class_dev, "subdevice locked\n");
ed9eccbe
DS
1793 return -EACCES;
1794 }
1795
1796 /* are we busy? */
1797 if (s->busy) {
272850f0 1798 dev_dbg(dev->class_dev, "subdevice busy\n");
ed9eccbe
DS
1799 return -EBUSY;
1800 }
ed9eccbe 1801
ed9eccbe 1802 /* make sure channel/gain list isn't too short */
88bc0574 1803 if (cmd.chanlist_len < 1) {
272850f0 1804 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
88bc0574 1805 cmd.chanlist_len);
4b18f08b 1806 return -EINVAL;
ed9eccbe
DS
1807 }
1808
88bc0574 1809 async->cmd = cmd;
ed9eccbe 1810 async->cmd.data = NULL;
ed9eccbe 1811
c6cd0eef
HS
1812 /* load channel/gain list */
1813 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1814 if (ret)
ed9eccbe 1815 goto cleanup;
ed9eccbe
DS
1816
1817 ret = s->do_cmdtest(dev, s, &async->cmd);
1818
b0446a21 1819 if (async->cmd.flags & CMDF_BOGUS || ret) {
272850f0 1820 dev_dbg(dev->class_dev, "test returned %d\n", ret);
88bc0574 1821 cmd = async->cmd;
476b8477 1822 /* restore chanlist pointer before copying back */
95bc359f 1823 cmd.chanlist = (unsigned int __force *)user_chanlist;
88bc0574 1824 cmd.data = NULL;
bc252fd1 1825 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
272850f0 1826 dev_dbg(dev->class_dev, "fault writing cmd\n");
ed9eccbe
DS
1827 ret = -EFAULT;
1828 goto cleanup;
1829 }
1830 ret = -EAGAIN;
1831 goto cleanup;
1832 }
1833
1834 if (!async->prealloc_bufsz) {
1835 ret = -ENOMEM;
272850f0 1836 dev_dbg(dev->class_dev, "no buffer (?)\n");
ed9eccbe
DS
1837 goto cleanup;
1838 }
1839
fcc18a9a 1840 comedi_buf_reset(s);
ed9eccbe 1841
781f933c 1842 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
d8bff6e3 1843 if (async->cmd.flags & CMDF_WAKE_EOS)
ed9eccbe 1844 async->cb_mask |= COMEDI_CB_EOS;
ed9eccbe 1845
cc64ea42
IA
1846 comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1847 COMEDI_SRF_RUNNING);
ed9eccbe 1848
84bb0bcc
HS
1849 /*
1850 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1851 * race with comedi_read() or comedi_write().
1852 */
4b18f08b 1853 s->busy = file;
ed9eccbe
DS
1854 ret = s->do_cmd(dev, s);
1855 if (ret == 0)
1856 return 0;
1857
476b8477 1858cleanup:
ed9eccbe
DS
1859 do_become_nonbusy(dev, s);
1860
1861 return ret;
1862}
1863
1864/*
18e01b24 1865 * COMEDI_CMDTEST ioctl
69e98df7 1866 * asynchronous acquisition command testing
18e01b24
IA
1867 *
1868 * arg:
1869 * pointer to comedi_cmd structure
1870 *
1871 * reads:
1872 * comedi_cmd structure
1873 * channel/range list from cmd->chanlist pointer
1874 *
1875 * writes:
1876 * possibly modified comedi_cmd structure
1877 */
92d0127c
GKH
1878static int do_cmdtest_ioctl(struct comedi_device *dev,
1879 struct comedi_cmd __user *arg, void *file)
ed9eccbe 1880{
f8348677 1881 struct comedi_cmd cmd;
34c43922 1882 struct comedi_subdevice *s;
95bc359f 1883 unsigned int __user *user_chanlist;
87ece583
HS
1884 int ret;
1885
77c21b62
IA
1886 lockdep_assert_held(&dev->mutex);
1887
87ece583
HS
1888 /* get the user's cmd and do some simple validation */
1889 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1890 if (ret)
1891 return ret;
ed9eccbe 1892
476b8477 1893 /* save user's chanlist pointer so it can be restored later */
95bc359f 1894 user_chanlist = (unsigned int __user *)cmd.chanlist;
ed9eccbe 1895
f8348677 1896 s = &dev->subdevices[cmd.subdev];
ed9eccbe 1897
6cab7a37
IA
1898 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1899 if (user_chanlist) {
1900 /* load channel/gain list */
1901 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1902 if (ret)
1903 return ret;
1904 }
ed9eccbe 1905
f8348677 1906 ret = s->do_cmdtest(dev, s, &cmd);
ed9eccbe 1907
238b5ad8
IA
1908 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1909
476b8477 1910 /* restore chanlist pointer before copying back */
95bc359f 1911 cmd.chanlist = (unsigned int __force *)user_chanlist;
ed9eccbe 1912
bc252fd1 1913 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
272850f0 1914 dev_dbg(dev->class_dev, "bad cmd address\n");
ed9eccbe 1915 ret = -EFAULT;
ed9eccbe 1916 }
c6cd0eef 1917
ed9eccbe
DS
1918 return ret;
1919}
1920
1921/*
18e01b24
IA
1922 * COMEDI_LOCK ioctl
1923 * lock subdevice
1924 *
1925 * arg:
1926 * subdevice number
1927 *
1928 * reads:
1929 * nothing
1930 *
1931 * writes:
1932 * nothing
1933 */
c1a6eac1 1934static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
0a85b6f0 1935 void *file)
ed9eccbe
DS
1936{
1937 int ret = 0;
1938 unsigned long flags;
34c43922 1939 struct comedi_subdevice *s;
ed9eccbe 1940
77c21b62 1941 lockdep_assert_held(&dev->mutex);
ed9eccbe
DS
1942 if (arg >= dev->n_subdevices)
1943 return -EINVAL;
b077f2cd 1944 s = &dev->subdevices[arg];
ed9eccbe 1945
5f74ea14 1946 spin_lock_irqsave(&s->spin_lock, flags);
476b8477 1947 if (s->busy || s->lock)
ed9eccbe 1948 ret = -EBUSY;
476b8477 1949 else
ed9eccbe 1950 s->lock = file;
5f74ea14 1951 spin_unlock_irqrestore(&s->spin_lock, flags);
ed9eccbe 1952
ed9eccbe
DS
1953 return ret;
1954}
1955
1956/*
18e01b24
IA
1957 * COMEDI_UNLOCK ioctl
1958 * unlock subdevice
1959 *
1960 * arg:
1961 * subdevice number
1962 *
1963 * reads:
1964 * nothing
1965 *
1966 * writes:
1967 * nothing
1968 */
c1a6eac1 1969static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
0a85b6f0 1970 void *file)
ed9eccbe 1971{
34c43922 1972 struct comedi_subdevice *s;
ed9eccbe 1973
77c21b62 1974 lockdep_assert_held(&dev->mutex);
ed9eccbe
DS
1975 if (arg >= dev->n_subdevices)
1976 return -EINVAL;
b077f2cd 1977 s = &dev->subdevices[arg];
ed9eccbe
DS
1978
1979 if (s->busy)
1980 return -EBUSY;
1981
1982 if (s->lock && s->lock != file)
1983 return -EACCES;
1984
90ac0764 1985 if (s->lock == file)
ed9eccbe 1986 s->lock = NULL;
ed9eccbe
DS
1987
1988 return 0;
1989}
1990
1991/*
18e01b24
IA
1992 * COMEDI_CANCEL ioctl
1993 * cancel asynchronous acquisition
1994 *
1995 * arg:
1996 * subdevice number
1997 *
1998 * reads:
1999 * nothing
2000 *
2001 * writes:
2002 * nothing
2003 */
c1a6eac1 2004static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
0a85b6f0 2005 void *file)
ed9eccbe 2006{
34c43922 2007 struct comedi_subdevice *s;
ed9eccbe 2008
77c21b62 2009 lockdep_assert_held(&dev->mutex);
ed9eccbe
DS
2010 if (arg >= dev->n_subdevices)
2011 return -EINVAL;
b077f2cd 2012 s = &dev->subdevices[arg];
88cc30cf 2013 if (!s->async)
ed9eccbe
DS
2014 return -EINVAL;
2015
ed9eccbe
DS
2016 if (!s->busy)
2017 return 0;
2018
2019 if (s->busy != file)
2020 return -EBUSY;
2021
fe43ec53 2022 return do_cancel(dev, s);
ed9eccbe
DS
2023}
2024
2025/*
18e01b24
IA
2026 * COMEDI_POLL ioctl
2027 * instructs driver to synchronize buffers
2028 *
2029 * arg:
2030 * subdevice number
2031 *
2032 * reads:
2033 * nothing
2034 *
2035 * writes:
2036 * nothing
2037 */
c1a6eac1 2038static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
0a85b6f0 2039 void *file)
ed9eccbe 2040{
34c43922 2041 struct comedi_subdevice *s;
ed9eccbe 2042
77c21b62 2043 lockdep_assert_held(&dev->mutex);
ed9eccbe
DS
2044 if (arg >= dev->n_subdevices)
2045 return -EINVAL;
b077f2cd 2046 s = &dev->subdevices[arg];
ed9eccbe 2047
ed9eccbe
DS
2048 if (!s->busy)
2049 return 0;
2050
2051 if (s->busy != file)
2052 return -EBUSY;
2053
2054 if (s->poll)
2055 return s->poll(dev, s);
2056
2057 return -EINVAL;
2058}
2059
c299a678
IA
2060/*
2061 * COMEDI_SETRSUBD ioctl
2062 * sets the current "read" subdevice on a per-file basis
2063 *
2064 * arg:
2065 * subdevice number
2066 *
2067 * reads:
2068 * nothing
2069 *
2070 * writes:
2071 * nothing
2072 */
2073static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
2074 struct file *file)
2075{
2076 struct comedi_file *cfp = file->private_data;
2077 struct comedi_subdevice *s_old, *s_new;
2078
77c21b62 2079 lockdep_assert_held(&dev->mutex);
c299a678
IA
2080 if (arg >= dev->n_subdevices)
2081 return -EINVAL;
2082
2083 s_new = &dev->subdevices[arg];
2084 s_old = comedi_file_read_subdevice(file);
2085 if (s_old == s_new)
2086 return 0; /* no change */
2087
2088 if (!(s_new->subdev_flags & SDF_CMD_READ))
2089 return -EINVAL;
2090
2091 /*
2092 * Check the file isn't still busy handling a "read" command on the
2093 * old subdevice (if any).
2094 */
2095 if (s_old && s_old->busy == file && s_old->async &&
2096 !(s_old->async->cmd.flags & CMDF_WRITE))
2097 return -EBUSY;
2098
34d34732 2099 WRITE_ONCE(cfp->read_subdev, s_new);
c299a678
IA
2100 return 0;
2101}
2102
2103/*
2104 * COMEDI_SETWSUBD ioctl
2105 * sets the current "write" subdevice on a per-file basis
2106 *
2107 * arg:
2108 * subdevice number
2109 *
2110 * reads:
2111 * nothing
2112 *
2113 * writes:
2114 * nothing
2115 */
2116static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
2117 struct file *file)
2118{
2119 struct comedi_file *cfp = file->private_data;
2120 struct comedi_subdevice *s_old, *s_new;
2121
77c21b62 2122 lockdep_assert_held(&dev->mutex);
c299a678
IA
2123 if (arg >= dev->n_subdevices)
2124 return -EINVAL;
2125
2126 s_new = &dev->subdevices[arg];
2127 s_old = comedi_file_write_subdevice(file);
2128 if (s_old == s_new)
2129 return 0; /* no change */
2130
2131 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
2132 return -EINVAL;
2133
2134 /*
2135 * Check the file isn't still busy handling a "write" command on the
2136 * old subdevice (if any).
2137 */
2138 if (s_old && s_old->busy == file && s_old->async &&
2139 (s_old->async->cmd.flags & CMDF_WRITE))
2140 return -EBUSY;
2141
34d34732 2142 WRITE_ONCE(cfp->write_subdev, s_new);
c299a678
IA
2143 return 0;
2144}
2145
47db6d58
HS
2146static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2147 unsigned long arg)
2148{
d4d47895 2149 unsigned int minor = iminor(file_inode(file));
20f083c0
IA
2150 struct comedi_file *cfp = file->private_data;
2151 struct comedi_device *dev = cfp->dev;
47db6d58
HS
2152 int rc;
2153
47db6d58
HS
2154 mutex_lock(&dev->mutex);
2155
a2aab8b4
IA
2156 /*
2157 * Device config is special, because it must work on
2158 * an unconfigured device.
2159 */
47db6d58 2160 if (cmd == COMEDI_DEVCONFIG) {
754ab5c0
IA
2161 if (minor >= COMEDI_NUM_BOARD_MINORS) {
2162 /* Device config not appropriate on non-board minors. */
2163 rc = -ENOTTY;
2164 goto done;
2165 }
47db6d58
HS
2166 rc = do_devconfig_ioctl(dev,
2167 (struct comedi_devconfig __user *)arg);
8ab4ed6e
IA
2168 if (rc == 0) {
2169 if (arg == 0 &&
2170 dev->minor >= comedi_num_legacy_minors) {
a2aab8b4
IA
2171 /*
2172 * Successfully unconfigured a dynamically
2173 * allocated device. Try and remove it.
2174 */
db210da2 2175 if (comedi_clear_board_dev(dev)) {
8ab4ed6e 2176 mutex_unlock(&dev->mutex);
cb6b79de 2177 comedi_free_board_dev(dev);
8ab4ed6e
IA
2178 return rc;
2179 }
2180 }
2181 }
47db6d58
HS
2182 goto done;
2183 }
2184
2185 if (!dev->attached) {
272850f0 2186 dev_dbg(dev->class_dev, "no driver attached\n");
47db6d58
HS
2187 rc = -ENODEV;
2188 goto done;
2189 }
2190
2191 switch (cmd) {
2192 case COMEDI_BUFCONFIG:
2193 rc = do_bufconfig_ioctl(dev,
2194 (struct comedi_bufconfig __user *)arg);
2195 break;
2196 case COMEDI_DEVINFO:
2197 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2198 file);
2199 break;
2200 case COMEDI_SUBDINFO:
2201 rc = do_subdinfo_ioctl(dev,
2202 (struct comedi_subdinfo __user *)arg,
2203 file);
2204 break;
3fbfd222
AV
2205 case COMEDI_CHANINFO: {
2206 struct comedi_chaninfo it;
2207 if (copy_from_user(&it, (void __user *)arg, sizeof(it)))
2208 rc = -EFAULT;
2209 else
2210 rc = do_chaninfo_ioctl(dev, &it);
47db6d58 2211 break;
3fbfd222 2212 }
47db6d58
HS
2213 case COMEDI_RANGEINFO:
2214 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2215 break;
2216 case COMEDI_BUFINFO:
2217 rc = do_bufinfo_ioctl(dev,
2218 (struct comedi_bufinfo __user *)arg,
2219 file);
2220 break;
2221 case COMEDI_LOCK:
2222 rc = do_lock_ioctl(dev, arg, file);
2223 break;
2224 case COMEDI_UNLOCK:
2225 rc = do_unlock_ioctl(dev, arg, file);
2226 break;
2227 case COMEDI_CANCEL:
2228 rc = do_cancel_ioctl(dev, arg, file);
2229 break;
2230 case COMEDI_CMD:
2231 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2232 break;
2233 case COMEDI_CMDTEST:
2234 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2235 file);
2236 break;
2237 case COMEDI_INSNLIST:
2238 rc = do_insnlist_ioctl(dev,
2239 (struct comedi_insnlist __user *)arg,
2240 file);
2241 break;
2242 case COMEDI_INSN:
2243 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2244 file);
2245 break;
2246 case COMEDI_POLL:
2247 rc = do_poll_ioctl(dev, arg, file);
2248 break;
c299a678
IA
2249 case COMEDI_SETRSUBD:
2250 rc = do_setrsubd_ioctl(dev, arg, file);
2251 break;
2252 case COMEDI_SETWSUBD:
2253 rc = do_setwsubd_ioctl(dev, arg, file);
2254 break;
47db6d58
HS
2255 default:
2256 rc = -ENOTTY;
2257 break;
2258 }
2259
2260done:
2261 mutex_unlock(&dev->mutex);
2262 return rc;
2263}
2264
df30b21c
FV
2265static void comedi_vm_open(struct vm_area_struct *area)
2266{
af93da31 2267 struct comedi_buf_map *bm;
df30b21c 2268
af93da31
IA
2269 bm = area->vm_private_data;
2270 comedi_buf_map_get(bm);
df30b21c
FV
2271}
2272
2273static void comedi_vm_close(struct vm_area_struct *area)
ed9eccbe 2274{
af93da31 2275 struct comedi_buf_map *bm;
ed9eccbe 2276
af93da31
IA
2277 bm = area->vm_private_data;
2278 comedi_buf_map_put(bm);
ed9eccbe
DS
2279}
2280
255364f7
IA
2281static int comedi_vm_access(struct vm_area_struct *vma, unsigned long addr,
2282 void *buf, int len, int write)
2283{
2284 struct comedi_buf_map *bm = vma->vm_private_data;
2285 unsigned long offset =
2286 addr - vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT);
2287
2288 if (len < 0)
2289 return -EINVAL;
2290 if (len > vma->vm_end - addr)
2291 len = vma->vm_end - addr;
2292 return comedi_buf_map_access(bm, offset, buf, len, write);
2293}
2294
7cbea8dc 2295static const struct vm_operations_struct comedi_vm_ops = {
df30b21c
FV
2296 .open = comedi_vm_open,
2297 .close = comedi_vm_close,
255364f7 2298 .access = comedi_vm_access,
ed9eccbe
DS
2299};
2300
2301static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2302{
20f083c0
IA
2303 struct comedi_file *cfp = file->private_data;
2304 struct comedi_device *dev = cfp->dev;
a52840a9
HS
2305 struct comedi_subdevice *s;
2306 struct comedi_async *async;
b34aa86f 2307 struct comedi_buf_map *bm = NULL;
e3647214 2308 struct comedi_buf_page *buf;
ed9eccbe
DS
2309 unsigned long start = vma->vm_start;
2310 unsigned long size;
2311 int n_pages;
2312 int i;
e3647214 2313 int retval = 0;
3ffab428 2314
b34aa86f
IA
2315 /*
2316 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2317 * and down-reading &dev->attach_lock should normally succeed without
2318 * contention unless the device is in the process of being attached
2319 * or detached.
2320 */
2321 if (!down_read_trylock(&dev->attach_lock))
2322 return -EAGAIN;
a52840a9 2323
ed9eccbe 2324 if (!dev->attached) {
272850f0 2325 dev_dbg(dev->class_dev, "no driver attached\n");
ed9eccbe
DS
2326 retval = -ENODEV;
2327 goto done;
2328 }
a52840a9 2329
476b8477 2330 if (vma->vm_flags & VM_WRITE)
20f083c0 2331 s = comedi_file_write_subdevice(file);
476b8477 2332 else
20f083c0 2333 s = comedi_file_read_subdevice(file);
a52840a9 2334 if (!s) {
ed9eccbe
DS
2335 retval = -EINVAL;
2336 goto done;
2337 }
a52840a9 2338
ed9eccbe 2339 async = s->async;
a52840a9 2340 if (!async) {
ed9eccbe
DS
2341 retval = -EINVAL;
2342 goto done;
2343 }
2344
2345 if (vma->vm_pgoff != 0) {
272850f0 2346 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
ed9eccbe
DS
2347 retval = -EINVAL;
2348 goto done;
2349 }
2350
2351 size = vma->vm_end - vma->vm_start;
2352 if (size > async->prealloc_bufsz) {
2353 retval = -EFAULT;
2354 goto done;
2355 }
44b8c793 2356 if (offset_in_page(size)) {
ed9eccbe
DS
2357 retval = -EFAULT;
2358 goto done;
2359 }
2360
ec9d0754 2361 n_pages = vma_pages(vma);
b34aa86f
IA
2362
2363 /* get reference to current buf map (if any) */
2364 bm = comedi_buf_map_from_subdev_get(s);
af93da31
IA
2365 if (!bm || n_pages > bm->n_pages) {
2366 retval = -EINVAL;
2367 goto done;
2368 }
e3647214
IA
2369 if (bm->dma_dir != DMA_NONE) {
2370 /*
2371 * DMA buffer was allocated as a single block.
2372 * Address is in page_list[0].
2373 */
2374 buf = &bm->page_list[0];
2375 retval = dma_mmap_coherent(bm->dma_hw_dev, vma, buf->virt_addr,
2376 buf->dma_addr, n_pages * PAGE_SIZE);
2377 } else {
2378 for (i = 0; i < n_pages; ++i) {
2379 unsigned long pfn;
2380
2381 buf = &bm->page_list[i];
2382 pfn = page_to_pfn(virt_to_page(buf->virt_addr));
2383 retval = remap_pfn_range(vma, start, pfn, PAGE_SIZE,
2384 PAGE_SHARED);
2385 if (retval)
2386 break;
a52840a9 2387
e3647214 2388 start += PAGE_SIZE;
ed9eccbe 2389 }
ed9eccbe
DS
2390 }
2391
e3647214
IA
2392 if (retval == 0) {
2393 vma->vm_ops = &comedi_vm_ops;
2394 vma->vm_private_data = bm;
ed9eccbe 2395
e3647214
IA
2396 vma->vm_ops->open(vma);
2397 }
ed9eccbe 2398
476b8477 2399done:
b34aa86f
IA
2400 up_read(&dev->attach_lock);
2401 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
ed9eccbe
DS
2402 return retval;
2403}
2404
afc9a42b 2405static __poll_t comedi_poll(struct file *file, poll_table *wait)
ed9eccbe 2406{
afc9a42b 2407 __poll_t mask = 0;
20f083c0
IA
2408 struct comedi_file *cfp = file->private_data;
2409 struct comedi_device *dev = cfp->dev;
322146d5 2410 struct comedi_subdevice *s, *s_read;
3ffab428 2411
d5eb3a74 2412 down_read(&dev->attach_lock);
ca081b1d 2413
ed9eccbe 2414 if (!dev->attached) {
272850f0 2415 dev_dbg(dev->class_dev, "no driver attached\n");
ca081b1d 2416 goto done;
ed9eccbe
DS
2417 }
2418
20f083c0 2419 s = comedi_file_read_subdevice(file);
322146d5 2420 s_read = s;
cc400e18 2421 if (s && s->async) {
ca081b1d 2422 poll_wait(file, &s->async->wait_head, wait);
3834234f 2423 if (s->busy != file || !comedi_is_subdevice_running(s) ||
662c722b 2424 (s->async->cmd.flags & CMDF_WRITE) ||
e9edef3a 2425 comedi_buf_read_n_available(s) > 0)
a9a08845 2426 mask |= EPOLLIN | EPOLLRDNORM;
ed9eccbe 2427 }
ca081b1d 2428
20f083c0 2429 s = comedi_file_write_subdevice(file);
cc400e18 2430 if (s && s->async) {
c39e050d 2431 unsigned int bps = comedi_bytes_per_sample(s);
ca081b1d 2432
322146d5
IA
2433 if (s != s_read)
2434 poll_wait(file, &s->async->wait_head, wait);
3834234f 2435 if (s->busy != file || !comedi_is_subdevice_running(s) ||
662c722b 2436 !(s->async->cmd.flags & CMDF_WRITE) ||
ecf04ed3 2437 comedi_buf_write_n_available(s) >= bps)
a9a08845 2438 mask |= EPOLLOUT | EPOLLWRNORM;
ed9eccbe
DS
2439 }
2440
ca081b1d 2441done:
d5eb3a74 2442 up_read(&dev->attach_lock);
ed9eccbe
DS
2443 return mask;
2444}
2445
92d0127c
GKH
2446static ssize_t comedi_write(struct file *file, const char __user *buf,
2447 size_t nbytes, loff_t *offset)
ed9eccbe 2448{
34c43922 2449 struct comedi_subdevice *s;
d163679c 2450 struct comedi_async *async;
84a185ec
IA
2451 unsigned int n, m;
2452 ssize_t count = 0;
2453 int retval = 0;
ed9eccbe 2454 DECLARE_WAITQUEUE(wait, current);
20f083c0
IA
2455 struct comedi_file *cfp = file->private_data;
2456 struct comedi_device *dev = cfp->dev;
06181de1 2457 bool become_nonbusy = false;
9329f139
IA
2458 bool attach_locked;
2459 unsigned int old_detach_count;
3ffab428 2460
9329f139
IA
2461 /* Protect against device detachment during operation. */
2462 down_read(&dev->attach_lock);
2463 attach_locked = true;
2464 old_detach_count = dev->detach_count;
2465
ed9eccbe 2466 if (!dev->attached) {
272850f0 2467 dev_dbg(dev->class_dev, "no driver attached\n");
9329f139
IA
2468 retval = -ENODEV;
2469 goto out;
ed9eccbe
DS
2470 }
2471
20f083c0 2472 s = comedi_file_write_subdevice(file);
9329f139
IA
2473 if (!s || !s->async) {
2474 retval = -EIO;
2475 goto out;
2476 }
2714b019 2477
ed9eccbe 2478 async = s->async;
3318c7ad 2479 if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) {
f7398509
IA
2480 retval = -EINVAL;
2481 goto out;
2482 }
2714b019 2483
ed9eccbe 2484 add_wait_queue(&async->wait_head, &wait);
06181de1 2485 while (count == 0 && !retval) {
d4d47895 2486 unsigned int runflags;
35a7475d 2487 unsigned int wp, n1, n2;
b183a836 2488
ed9eccbe
DS
2489 set_current_state(TASK_INTERRUPTIBLE);
2490
b183a836
IA
2491 runflags = comedi_get_subdevice_runflags(s);
2492 if (!comedi_is_runflags_running(runflags)) {
06181de1
IA
2493 if (comedi_is_runflags_in_error(runflags))
2494 retval = -EPIPE;
28a60c45
IA
2495 if (retval || nbytes)
2496 become_nonbusy = true;
d2611540
IA
2497 break;
2498 }
28a60c45
IA
2499 if (nbytes == 0)
2500 break;
d2611540 2501
591c5f8a
IA
2502 /* Allocate all free buffer space. */
2503 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2504 m = comedi_buf_write_n_allocated(s);
591c5f8a 2505 n = min_t(size_t, m, nbytes);
ed9eccbe
DS
2506
2507 if (n == 0) {
ed9eccbe
DS
2508 if (file->f_flags & O_NONBLOCK) {
2509 retval = -EAGAIN;
2510 break;
2511 }
6a9ce6b6 2512 schedule();
ed9eccbe
DS
2513 if (signal_pending(current)) {
2514 retval = -ERESTARTSYS;
2515 break;
2516 }
3318c7ad
IA
2517 if (s->busy != file ||
2518 !(async->cmd.flags & CMDF_WRITE)) {
f7398509
IA
2519 retval = -EINVAL;
2520 break;
2521 }
ed9eccbe
DS
2522 continue;
2523 }
2524
cef98864 2525 set_current_state(TASK_RUNNING);
35a7475d
IA
2526 wp = async->buf_write_ptr;
2527 n1 = min(n, async->prealloc_bufsz - wp);
2528 n2 = n - n1;
2529 m = copy_from_user(async->prealloc_buf + wp, buf, n1);
2530 if (m)
2531 m += n2;
2532 else if (n2)
2533 m = copy_from_user(async->prealloc_buf, buf + n1, n2);
ed9eccbe
DS
2534 if (m) {
2535 n -= m;
2536 retval = -EFAULT;
2537 }
940dd35d 2538 comedi_buf_write_free(s, n);
ed9eccbe
DS
2539
2540 count += n;
2541 nbytes -= n;
2542
2543 buf += n;
ed9eccbe 2544 }
06181de1 2545 remove_wait_queue(&async->wait_head, &wait);
ed9eccbe 2546 set_current_state(TASK_RUNNING);
06181de1
IA
2547 if (become_nonbusy && count == 0) {
2548 struct comedi_subdevice *new_s;
2549
2550 /*
2551 * To avoid deadlock, cannot acquire dev->mutex
2552 * while dev->attach_lock is held.
2553 */
2554 up_read(&dev->attach_lock);
2555 attach_locked = false;
2556 mutex_lock(&dev->mutex);
2557 /*
2558 * Check device hasn't become detached behind our back.
2559 * Checking dev->detach_count is unchanged ought to be
2560 * sufficient (unless there have been 2**32 detaches in the
2561 * meantime!), but check the subdevice pointer as well just in
2562 * case.
ed65bba3
IA
2563 *
2564 * Also check the subdevice is still in a suitable state to
2565 * become non-busy in case it changed behind our back.
06181de1
IA
2566 */
2567 new_s = comedi_file_write_subdevice(file);
2568 if (dev->attached && old_detach_count == dev->detach_count &&
ed65bba3
IA
2569 s == new_s && new_s->async == async && s->busy == file &&
2570 (async->cmd.flags & CMDF_WRITE) &&
2571 !comedi_is_subdevice_running(s))
06181de1
IA
2572 do_become_nonbusy(dev, s);
2573 mutex_unlock(&dev->mutex);
2574 }
2575out:
9329f139
IA
2576 if (attach_locked)
2577 up_read(&dev->attach_lock);
ed9eccbe 2578
476b8477 2579 return count ? count : retval;
ed9eccbe
DS
2580}
2581
92d0127c 2582static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
36efbacd 2583 loff_t *offset)
ed9eccbe 2584{
34c43922 2585 struct comedi_subdevice *s;
d163679c 2586 struct comedi_async *async;
76e8e7d4
IA
2587 unsigned int n, m;
2588 ssize_t count = 0;
2589 int retval = 0;
ed9eccbe 2590 DECLARE_WAITQUEUE(wait, current);
20f083c0
IA
2591 struct comedi_file *cfp = file->private_data;
2592 struct comedi_device *dev = cfp->dev;
45c2bc55
IA
2593 unsigned int old_detach_count;
2594 bool become_nonbusy = false;
2595 bool attach_locked;
3ffab428 2596
45c2bc55
IA
2597 /* Protect against device detachment during operation. */
2598 down_read(&dev->attach_lock);
2599 attach_locked = true;
2600 old_detach_count = dev->detach_count;
2601
ed9eccbe 2602 if (!dev->attached) {
272850f0 2603 dev_dbg(dev->class_dev, "no driver attached\n");
45c2bc55
IA
2604 retval = -ENODEV;
2605 goto out;
ed9eccbe
DS
2606 }
2607
20f083c0 2608 s = comedi_file_read_subdevice(file);
45c2bc55
IA
2609 if (!s || !s->async) {
2610 retval = -EIO;
2611 goto out;
2612 }
5c87fef5 2613
ed9eccbe 2614 async = s->async;
39582847 2615 if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
f025ab9e
IA
2616 retval = -EINVAL;
2617 goto out;
2618 }
ed9eccbe
DS
2619
2620 add_wait_queue(&async->wait_head, &wait);
3c3bea26 2621 while (count == 0 && !retval) {
42ea907d
IA
2622 unsigned int rp, n1, n2;
2623
ed9eccbe
DS
2624 set_current_state(TASK_INTERRUPTIBLE);
2625
e9edef3a 2626 m = comedi_buf_read_n_available(s);
8ea93928 2627 n = min_t(size_t, m, nbytes);
ed9eccbe
DS
2628
2629 if (n == 0) {
b2073dcb
IA
2630 unsigned int runflags =
2631 comedi_get_subdevice_runflags(s);
b183a836
IA
2632
2633 if (!comedi_is_runflags_running(runflags)) {
2634 if (comedi_is_runflags_in_error(runflags))
ed9eccbe 2635 retval = -EPIPE;
3c3bea26
IA
2636 if (retval || nbytes)
2637 become_nonbusy = true;
ed9eccbe
DS
2638 break;
2639 }
3c3bea26
IA
2640 if (nbytes == 0)
2641 break;
ed9eccbe
DS
2642 if (file->f_flags & O_NONBLOCK) {
2643 retval = -EAGAIN;
2644 break;
2645 }
6a9ce6b6 2646 schedule();
ed9eccbe
DS
2647 if (signal_pending(current)) {
2648 retval = -ERESTARTSYS;
2649 break;
2650 }
39582847
IA
2651 if (s->busy != file ||
2652 (async->cmd.flags & CMDF_WRITE)) {
f025ab9e
IA
2653 retval = -EINVAL;
2654 break;
2655 }
ed9eccbe
DS
2656 continue;
2657 }
cef98864
IA
2658
2659 set_current_state(TASK_RUNNING);
42ea907d
IA
2660 rp = async->buf_read_ptr;
2661 n1 = min(n, async->prealloc_bufsz - rp);
2662 n2 = n - n1;
2663 m = copy_to_user(buf, async->prealloc_buf + rp, n1);
2664 if (m)
2665 m += n2;
2666 else if (n2)
2667 m = copy_to_user(buf + n1, async->prealloc_buf, n2);
ed9eccbe
DS
2668 if (m) {
2669 n -= m;
2670 retval = -EFAULT;
2671 }
2672
d13be55a 2673 comedi_buf_read_alloc(s, n);
f1df8662 2674 comedi_buf_read_free(s, n);
ed9eccbe
DS
2675
2676 count += n;
2677 nbytes -= n;
2678
2679 buf += n;
ed9eccbe 2680 }
45c2bc55
IA
2681 remove_wait_queue(&async->wait_head, &wait);
2682 set_current_state(TASK_RUNNING);
970679b0 2683 if (become_nonbusy && count == 0) {
45c2bc55
IA
2684 struct comedi_subdevice *new_s;
2685
2686 /*
2687 * To avoid deadlock, cannot acquire dev->mutex
2688 * while dev->attach_lock is held.
2689 */
2690 up_read(&dev->attach_lock);
2691 attach_locked = false;
4b18f08b 2692 mutex_lock(&dev->mutex);
45c2bc55
IA
2693 /*
2694 * Check device hasn't become detached behind our back.
2695 * Checking dev->detach_count is unchanged ought to be
2696 * sufficient (unless there have been 2**32 detaches in the
2697 * meantime!), but check the subdevice pointer as well just in
2698 * case.
fd060c8f
IA
2699 *
2700 * Also check the subdevice is still in a suitable state to
2701 * become non-busy in case it changed behind our back.
45c2bc55 2702 */
20f083c0 2703 new_s = comedi_file_read_subdevice(file);
45c2bc55 2704 if (dev->attached && old_detach_count == dev->detach_count &&
fd060c8f
IA
2705 s == new_s && new_s->async == async && s->busy == file &&
2706 !(async->cmd.flags & CMDF_WRITE) &&
2707 !comedi_is_subdevice_running(s) &&
2708 comedi_buf_read_n_available(s) == 0)
2709 do_become_nonbusy(dev, s);
4b18f08b 2710 mutex_unlock(&dev->mutex);
ed9eccbe 2711 }
45c2bc55
IA
2712out:
2713 if (attach_locked)
2714 up_read(&dev->attach_lock);
ed9eccbe 2715
476b8477 2716 return count ? count : retval;
ed9eccbe
DS
2717}
2718
ed9eccbe
DS
2719static int comedi_open(struct inode *inode, struct file *file)
2720{
d4d47895 2721 const unsigned int minor = iminor(inode);
20f083c0 2722 struct comedi_file *cfp;
fc406986
IA
2723 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2724 int rc;
97920071 2725
4da5fa9a 2726 if (!dev) {
272850f0 2727 pr_debug("invalid minor number\n");
ed9eccbe
DS
2728 return -ENODEV;
2729 }
2730
20f083c0
IA
2731 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2732 if (!cfp)
2733 return -ENOMEM;
2734
2735 cfp->dev = dev;
2736
ed9eccbe 2737 mutex_lock(&dev->mutex);
0e0d311e
IA
2738 if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
2739 dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
fc406986
IA
2740 rc = -ENODEV;
2741 goto out;
ed9eccbe 2742 }
1363e4fb 2743 if (dev->attached && dev->use_count == 0) {
ed9eccbe 2744 if (!try_module_get(dev->driver->module)) {
90e6f51d 2745 rc = -ENXIO;
fc406986 2746 goto out;
ed9eccbe 2747 }
1363e4fb
IA
2748 if (dev->open) {
2749 rc = dev->open(dev);
2750 if (rc < 0) {
2751 module_put(dev->driver->module);
2752 goto out;
2753 }
3c17ba07
IA
2754 }
2755 }
ed9eccbe
DS
2756
2757 dev->use_count++;
20f083c0
IA
2758 file->private_data = cfp;
2759 comedi_file_reset(file);
fc406986 2760 rc = 0;
ed9eccbe 2761
fc406986 2762out:
a5011a26 2763 mutex_unlock(&dev->mutex);
20f083c0 2764 if (rc) {
fc406986 2765 comedi_dev_put(dev);
20f083c0
IA
2766 kfree(cfp);
2767 }
fc406986 2768 return rc;
ed9eccbe
DS
2769}
2770
2aae0076
HS
2771static int comedi_fasync(int fd, struct file *file, int on)
2772{
20f083c0
IA
2773 struct comedi_file *cfp = file->private_data;
2774 struct comedi_device *dev = cfp->dev;
2aae0076
HS
2775
2776 return fasync_helper(fd, file, on, &dev->async_queue);
2777}
2778
a5011a26 2779static int comedi_close(struct inode *inode, struct file *file)
ed9eccbe 2780{
20f083c0
IA
2781 struct comedi_file *cfp = file->private_data;
2782 struct comedi_device *dev = cfp->dev;
a5011a26 2783 struct comedi_subdevice *s = NULL;
ed9eccbe
DS
2784 int i;
2785
a5011a26
HS
2786 mutex_lock(&dev->mutex);
2787
2788 if (dev->subdevices) {
2789 for (i = 0; i < dev->n_subdevices; i++) {
b077f2cd 2790 s = &dev->subdevices[i];
a5011a26
HS
2791
2792 if (s->busy == file)
2793 do_cancel(dev, s);
2794 if (s->lock == file)
2795 s->lock = NULL;
2796 }
ed9eccbe 2797 }
1363e4fb
IA
2798 if (dev->attached && dev->use_count == 1) {
2799 if (dev->close)
2800 dev->close(dev);
a5011a26 2801 module_put(dev->driver->module);
1363e4fb 2802 }
a5011a26
HS
2803
2804 dev->use_count--;
2805
2806 mutex_unlock(&dev->mutex);
fc406986 2807 comedi_dev_put(dev);
20f083c0 2808 kfree(cfp);
a5011a26 2809
ed9eccbe
DS
2810 return 0;
2811}
2812
e0d0bf8a
AV
2813#ifdef CONFIG_COMPAT
2814
2815#define COMEDI32_CHANINFO _IOR(CIO, 3, struct comedi32_chaninfo_struct)
2816#define COMEDI32_RANGEINFO _IOR(CIO, 8, struct comedi32_rangeinfo_struct)
2817/*
2818 * N.B. COMEDI32_CMD and COMEDI_CMD ought to use _IOWR, not _IOR.
2819 * It's too late to change it now, but it only affects the command number.
2820 */
2821#define COMEDI32_CMD _IOR(CIO, 9, struct comedi32_cmd_struct)
2822/*
2823 * N.B. COMEDI32_CMDTEST and COMEDI_CMDTEST ought to use _IOWR, not _IOR.
2824 * It's too late to change it now, but it only affects the command number.
2825 */
2826#define COMEDI32_CMDTEST _IOR(CIO, 10, struct comedi32_cmd_struct)
2827#define COMEDI32_INSNLIST _IOR(CIO, 11, struct comedi32_insnlist_struct)
2828#define COMEDI32_INSN _IOR(CIO, 12, struct comedi32_insn_struct)
2829
2830struct comedi32_chaninfo_struct {
2831 unsigned int subdev;
2832 compat_uptr_t maxdata_list; /* 32-bit 'unsigned int *' */
2833 compat_uptr_t flaglist; /* 32-bit 'unsigned int *' */
2834 compat_uptr_t rangelist; /* 32-bit 'unsigned int *' */
2835 unsigned int unused[4];
2836};
2837
2838struct comedi32_rangeinfo_struct {
2839 unsigned int range_type;
2840 compat_uptr_t range_ptr; /* 32-bit 'void *' */
2841};
2842
2843struct comedi32_cmd_struct {
2844 unsigned int subdev;
2845 unsigned int flags;
2846 unsigned int start_src;
2847 unsigned int start_arg;
2848 unsigned int scan_begin_src;
2849 unsigned int scan_begin_arg;
2850 unsigned int convert_src;
2851 unsigned int convert_arg;
2852 unsigned int scan_end_src;
2853 unsigned int scan_end_arg;
2854 unsigned int stop_src;
2855 unsigned int stop_arg;
2856 compat_uptr_t chanlist; /* 32-bit 'unsigned int *' */
2857 unsigned int chanlist_len;
2858 compat_uptr_t data; /* 32-bit 'short *' */
2859 unsigned int data_len;
2860};
2861
2862struct comedi32_insn_struct {
2863 unsigned int insn;
2864 unsigned int n;
2865 compat_uptr_t data; /* 32-bit 'unsigned int *' */
2866 unsigned int subdev;
2867 unsigned int chanspec;
2868 unsigned int unused[3];
2869};
2870
2871struct comedi32_insnlist_struct {
2872 unsigned int n_insns;
2873 compat_uptr_t insns; /* 32-bit 'struct comedi_insn *' */
2874};
2875
e0d0bf8a
AV
2876/* Handle 32-bit COMEDI_CHANINFO ioctl. */
2877static int compat_chaninfo(struct file *file, unsigned long arg)
2878{
3fbfd222
AV
2879 struct comedi_file *cfp = file->private_data;
2880 struct comedi_device *dev = cfp->dev;
2881 struct comedi32_chaninfo_struct chaninfo32;
2882 struct comedi_chaninfo chaninfo;
e0d0bf8a 2883 int err;
e0d0bf8a 2884
3fbfd222 2885 if (copy_from_user(&chaninfo32, compat_ptr(arg), sizeof(chaninfo32)))
e0d0bf8a
AV
2886 return -EFAULT;
2887
3fbfd222
AV
2888 memset(&chaninfo, 0, sizeof(chaninfo));
2889 chaninfo.subdev = chaninfo32.subdev;
2890 chaninfo.maxdata_list = compat_ptr(chaninfo32.maxdata_list);
2891 chaninfo.flaglist = compat_ptr(chaninfo32.flaglist);
2892 chaninfo.rangelist = compat_ptr(chaninfo32.rangelist);
e0d0bf8a 2893
3fbfd222
AV
2894 mutex_lock(&dev->mutex);
2895 err = do_chaninfo_ioctl(dev, &chaninfo);
2896 mutex_unlock(&dev->mutex);
2897 return err;
e0d0bf8a
AV
2898}
2899
2900/* Handle 32-bit COMEDI_RANGEINFO ioctl. */
2901static int compat_rangeinfo(struct file *file, unsigned long arg)
2902{
2903 struct comedi_rangeinfo __user *rangeinfo;
2904 struct comedi32_rangeinfo_struct __user *rangeinfo32;
2905 int err;
2906 union {
2907 unsigned int uint;
2908 compat_uptr_t uptr;
2909 } temp;
2910
2911 rangeinfo32 = compat_ptr(arg);
2912 rangeinfo = compat_alloc_user_space(sizeof(*rangeinfo));
2913
2914 /* Copy rangeinfo structure. */
2915 if (!access_ok(rangeinfo32, sizeof(*rangeinfo32)) ||
2916 !access_ok(rangeinfo, sizeof(*rangeinfo)))
2917 return -EFAULT;
2918
2919 err = 0;
2920 err |= __get_user(temp.uint, &rangeinfo32->range_type);
2921 err |= __put_user(temp.uint, &rangeinfo->range_type);
2922 err |= __get_user(temp.uptr, &rangeinfo32->range_ptr);
2923 err |= __put_user(compat_ptr(temp.uptr), &rangeinfo->range_ptr);
2924 if (err)
2925 return -EFAULT;
2926
5c6a8747 2927 return comedi_unlocked_ioctl(file, COMEDI_RANGEINFO,
e0d0bf8a
AV
2928 (unsigned long)rangeinfo);
2929}
2930
2931/* Copy 32-bit cmd structure to native cmd structure. */
2932static int get_compat_cmd(struct comedi_cmd __user *cmd,
2933 struct comedi32_cmd_struct __user *cmd32)
2934{
2935 int err;
2936 union {
2937 unsigned int uint;
2938 compat_uptr_t uptr;
2939 } temp;
2940
2941 /* Copy cmd structure. */
2942 if (!access_ok(cmd32, sizeof(*cmd32)) ||
2943 !access_ok(cmd, sizeof(*cmd)))
2944 return -EFAULT;
2945
2946 err = 0;
2947 err |= __get_user(temp.uint, &cmd32->subdev);
2948 err |= __put_user(temp.uint, &cmd->subdev);
2949 err |= __get_user(temp.uint, &cmd32->flags);
2950 err |= __put_user(temp.uint, &cmd->flags);
2951 err |= __get_user(temp.uint, &cmd32->start_src);
2952 err |= __put_user(temp.uint, &cmd->start_src);
2953 err |= __get_user(temp.uint, &cmd32->start_arg);
2954 err |= __put_user(temp.uint, &cmd->start_arg);
2955 err |= __get_user(temp.uint, &cmd32->scan_begin_src);
2956 err |= __put_user(temp.uint, &cmd->scan_begin_src);
2957 err |= __get_user(temp.uint, &cmd32->scan_begin_arg);
2958 err |= __put_user(temp.uint, &cmd->scan_begin_arg);
2959 err |= __get_user(temp.uint, &cmd32->convert_src);
2960 err |= __put_user(temp.uint, &cmd->convert_src);
2961 err |= __get_user(temp.uint, &cmd32->convert_arg);
2962 err |= __put_user(temp.uint, &cmd->convert_arg);
2963 err |= __get_user(temp.uint, &cmd32->scan_end_src);
2964 err |= __put_user(temp.uint, &cmd->scan_end_src);
2965 err |= __get_user(temp.uint, &cmd32->scan_end_arg);
2966 err |= __put_user(temp.uint, &cmd->scan_end_arg);
2967 err |= __get_user(temp.uint, &cmd32->stop_src);
2968 err |= __put_user(temp.uint, &cmd->stop_src);
2969 err |= __get_user(temp.uint, &cmd32->stop_arg);
2970 err |= __put_user(temp.uint, &cmd->stop_arg);
2971 err |= __get_user(temp.uptr, &cmd32->chanlist);
2972 err |= __put_user((unsigned int __force *)compat_ptr(temp.uptr),
2973 &cmd->chanlist);
2974 err |= __get_user(temp.uint, &cmd32->chanlist_len);
2975 err |= __put_user(temp.uint, &cmd->chanlist_len);
2976 err |= __get_user(temp.uptr, &cmd32->data);
2977 err |= __put_user(compat_ptr(temp.uptr), &cmd->data);
2978 err |= __get_user(temp.uint, &cmd32->data_len);
2979 err |= __put_user(temp.uint, &cmd->data_len);
2980 return err ? -EFAULT : 0;
2981}
2982
2983/* Copy native cmd structure to 32-bit cmd structure. */
2984static int put_compat_cmd(struct comedi32_cmd_struct __user *cmd32,
2985 struct comedi_cmd __user *cmd)
2986{
2987 int err;
2988 unsigned int temp;
2989
2990 /*
2991 * Copy back most of cmd structure.
2992 *
2993 * Assume the pointer values are already valid.
2994 * (Could use ptr_to_compat() to set them.)
2995 */
2996 if (!access_ok(cmd, sizeof(*cmd)) ||
2997 !access_ok(cmd32, sizeof(*cmd32)))
2998 return -EFAULT;
2999
3000 err = 0;
3001 err |= __get_user(temp, &cmd->subdev);
3002 err |= __put_user(temp, &cmd32->subdev);
3003 err |= __get_user(temp, &cmd->flags);
3004 err |= __put_user(temp, &cmd32->flags);
3005 err |= __get_user(temp, &cmd->start_src);
3006 err |= __put_user(temp, &cmd32->start_src);
3007 err |= __get_user(temp, &cmd->start_arg);
3008 err |= __put_user(temp, &cmd32->start_arg);
3009 err |= __get_user(temp, &cmd->scan_begin_src);
3010 err |= __put_user(temp, &cmd32->scan_begin_src);
3011 err |= __get_user(temp, &cmd->scan_begin_arg);
3012 err |= __put_user(temp, &cmd32->scan_begin_arg);
3013 err |= __get_user(temp, &cmd->convert_src);
3014 err |= __put_user(temp, &cmd32->convert_src);
3015 err |= __get_user(temp, &cmd->convert_arg);
3016 err |= __put_user(temp, &cmd32->convert_arg);
3017 err |= __get_user(temp, &cmd->scan_end_src);
3018 err |= __put_user(temp, &cmd32->scan_end_src);
3019 err |= __get_user(temp, &cmd->scan_end_arg);
3020 err |= __put_user(temp, &cmd32->scan_end_arg);
3021 err |= __get_user(temp, &cmd->stop_src);
3022 err |= __put_user(temp, &cmd32->stop_src);
3023 err |= __get_user(temp, &cmd->stop_arg);
3024 err |= __put_user(temp, &cmd32->stop_arg);
3025 /* Assume chanlist pointer is unchanged. */
3026 err |= __get_user(temp, &cmd->chanlist_len);
3027 err |= __put_user(temp, &cmd32->chanlist_len);
3028 /* Assume data pointer is unchanged. */
3029 err |= __get_user(temp, &cmd->data_len);
3030 err |= __put_user(temp, &cmd32->data_len);
3031 return err ? -EFAULT : 0;
3032}
3033
3034/* Handle 32-bit COMEDI_CMD ioctl. */
3035static int compat_cmd(struct file *file, unsigned long arg)
3036{
3037 struct comedi_cmd __user *cmd;
3038 struct comedi32_cmd_struct __user *cmd32;
3039 int rc, err;
3040
3041 cmd32 = compat_ptr(arg);
3042 cmd = compat_alloc_user_space(sizeof(*cmd));
3043
3044 rc = get_compat_cmd(cmd, cmd32);
3045 if (rc)
3046 return rc;
3047
5c6a8747 3048 rc = comedi_unlocked_ioctl(file, COMEDI_CMD, (unsigned long)cmd);
e0d0bf8a
AV
3049 if (rc == -EAGAIN) {
3050 /* Special case: copy cmd back to user. */
3051 err = put_compat_cmd(cmd32, cmd);
3052 if (err)
3053 rc = err;
3054 }
3055
3056 return rc;
3057}
3058
3059/* Handle 32-bit COMEDI_CMDTEST ioctl. */
3060static int compat_cmdtest(struct file *file, unsigned long arg)
3061{
3062 struct comedi_cmd __user *cmd;
3063 struct comedi32_cmd_struct __user *cmd32;
3064 int rc, err;
3065
3066 cmd32 = compat_ptr(arg);
3067 cmd = compat_alloc_user_space(sizeof(*cmd));
3068
3069 rc = get_compat_cmd(cmd, cmd32);
3070 if (rc)
3071 return rc;
3072
5c6a8747 3073 rc = comedi_unlocked_ioctl(file, COMEDI_CMDTEST, (unsigned long)cmd);
e0d0bf8a
AV
3074 if (rc < 0)
3075 return rc;
3076
3077 err = put_compat_cmd(cmd32, cmd);
3078 if (err)
3079 rc = err;
3080
3081 return rc;
3082}
3083
3084/* Copy 32-bit insn structure to native insn structure. */
3085static int get_compat_insn(struct comedi_insn __user *insn,
3086 struct comedi32_insn_struct __user *insn32)
3087{
3088 int err;
3089 union {
3090 unsigned int uint;
3091 compat_uptr_t uptr;
3092 } temp;
3093
3094 /* Copy insn structure. Ignore the unused members. */
3095 err = 0;
3096 if (!access_ok(insn32, sizeof(*insn32)) ||
3097 !access_ok(insn, sizeof(*insn)))
3098 return -EFAULT;
3099
3100 err |= __get_user(temp.uint, &insn32->insn);
3101 err |= __put_user(temp.uint, &insn->insn);
3102 err |= __get_user(temp.uint, &insn32->n);
3103 err |= __put_user(temp.uint, &insn->n);
3104 err |= __get_user(temp.uptr, &insn32->data);
3105 err |= __put_user(compat_ptr(temp.uptr), &insn->data);
3106 err |= __get_user(temp.uint, &insn32->subdev);
3107 err |= __put_user(temp.uint, &insn->subdev);
3108 err |= __get_user(temp.uint, &insn32->chanspec);
3109 err |= __put_user(temp.uint, &insn->chanspec);
3110 return err ? -EFAULT : 0;
3111}
3112
3113/* Handle 32-bit COMEDI_INSNLIST ioctl. */
3114static int compat_insnlist(struct file *file, unsigned long arg)
3115{
3116 struct combined_insnlist {
3117 struct comedi_insnlist insnlist;
3118 struct comedi_insn insn[1];
3119 } __user *s;
3120 struct comedi32_insnlist_struct __user *insnlist32;
3121 struct comedi32_insn_struct __user *insn32;
3122 compat_uptr_t uptr;
3123 unsigned int n_insns, n;
3124 int err, rc;
3125
3126 insnlist32 = compat_ptr(arg);
3127
3128 /* Get 32-bit insnlist structure. */
3129 if (!access_ok(insnlist32, sizeof(*insnlist32)))
3130 return -EFAULT;
3131
3132 err = 0;
3133 err |= __get_user(n_insns, &insnlist32->n_insns);
3134 err |= __get_user(uptr, &insnlist32->insns);
3135 insn32 = compat_ptr(uptr);
3136 if (err)
3137 return -EFAULT;
3138
3139 /* Allocate user memory to copy insnlist and insns into. */
3140 s = compat_alloc_user_space(offsetof(struct combined_insnlist,
3141 insn[n_insns]));
3142
3143 /* Set native insnlist structure. */
3144 if (!access_ok(&s->insnlist, sizeof(s->insnlist)))
3145 return -EFAULT;
3146
3147 err |= __put_user(n_insns, &s->insnlist.n_insns);
3148 err |= __put_user(&s->insn[0], &s->insnlist.insns);
3149 if (err)
3150 return -EFAULT;
3151
3152 /* Copy insn structures. */
3153 for (n = 0; n < n_insns; n++) {
3154 rc = get_compat_insn(&s->insn[n], &insn32[n]);
3155 if (rc)
3156 return rc;
3157 }
3158
5c6a8747 3159 return comedi_unlocked_ioctl(file, COMEDI_INSNLIST,
e0d0bf8a
AV
3160 (unsigned long)&s->insnlist);
3161}
3162
3163/* Handle 32-bit COMEDI_INSN ioctl. */
3164static int compat_insn(struct file *file, unsigned long arg)
3165{
3166 struct comedi_insn __user *insn;
3167 struct comedi32_insn_struct __user *insn32;
3168 int rc;
3169
3170 insn32 = compat_ptr(arg);
3171 insn = compat_alloc_user_space(sizeof(*insn));
3172
3173 rc = get_compat_insn(insn, insn32);
3174 if (rc)
3175 return rc;
3176
5c6a8747 3177 return comedi_unlocked_ioctl(file, COMEDI_INSN, (unsigned long)insn);
e0d0bf8a
AV
3178}
3179
3180/*
3181 * compat_ioctl file operation.
3182 *
3183 * Returns -ENOIOCTLCMD for unrecognised ioctl codes.
3184 */
3185static long comedi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3186{
3187 int rc;
3188
3189 switch (cmd) {
3190 case COMEDI_DEVCONFIG:
3191 case COMEDI_DEVINFO:
3192 case COMEDI_SUBDINFO:
3193 case COMEDI_BUFCONFIG:
3194 case COMEDI_BUFINFO:
3195 /* Just need to translate the pointer argument. */
3196 arg = (unsigned long)compat_ptr(arg);
5c6a8747 3197 rc = comedi_unlocked_ioctl(file, cmd, arg);
e0d0bf8a
AV
3198 break;
3199 case COMEDI_LOCK:
3200 case COMEDI_UNLOCK:
3201 case COMEDI_CANCEL:
3202 case COMEDI_POLL:
3203 case COMEDI_SETRSUBD:
3204 case COMEDI_SETWSUBD:
3205 /* No translation needed. */
5c6a8747 3206 rc = comedi_unlocked_ioctl(file, cmd, arg);
e0d0bf8a
AV
3207 break;
3208 case COMEDI32_CHANINFO:
3209 rc = compat_chaninfo(file, arg);
3210 break;
3211 case COMEDI32_RANGEINFO:
3212 rc = compat_rangeinfo(file, arg);
3213 break;
3214 case COMEDI32_CMD:
3215 rc = compat_cmd(file, arg);
3216 break;
3217 case COMEDI32_CMDTEST:
3218 rc = compat_cmdtest(file, arg);
3219 break;
3220 case COMEDI32_INSNLIST:
3221 rc = compat_insnlist(file, arg);
3222 break;
3223 case COMEDI32_INSN:
3224 rc = compat_insn(file, arg);
3225 break;
3226 default:
3227 rc = -ENOIOCTLCMD;
3228 break;
3229 }
3230 return rc;
3231}
3232#else
3233#define comedi_compat_ioctl NULL
3234#endif
3235
8cb8aad7 3236static const struct file_operations comedi_fops = {
a5011a26
HS
3237 .owner = THIS_MODULE,
3238 .unlocked_ioctl = comedi_unlocked_ioctl,
3239 .compat_ioctl = comedi_compat_ioctl,
3240 .open = comedi_open,
3241 .release = comedi_close,
3242 .read = comedi_read,
3243 .write = comedi_write,
3244 .mmap = comedi_mmap,
3245 .poll = comedi_poll,
3246 .fasync = comedi_fasync,
3247 .llseek = noop_llseek,
3248};
3249
dd630cde 3250/**
a3e39942
IA
3251 * comedi_event() - Handle events for asynchronous COMEDI command
3252 * @dev: COMEDI device.
3253 * @s: COMEDI subdevice.
3254 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
dd630cde 3255 *
a3e39942
IA
3256 * If an asynchronous COMEDI command is active on the subdevice, process
3257 * any %COMEDI_CB_... event flags that have been set, usually by an
dd630cde 3258 * interrupt handler. These may change the run state of the asynchronous
a3e39942 3259 * command, wake a task, and/or send a %SIGIO signal.
dd630cde 3260 */
a5011a26 3261void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
883db3d9 3262{
a5011a26 3263 struct comedi_async *async = s->async;
ef4b4b27
IA
3264 unsigned int events;
3265 int si_code = 0;
3266 unsigned long flags;
3267
3268 spin_lock_irqsave(&s->spin_lock, flags);
883db3d9 3269
ef4b4b27 3270 events = async->events;
922d9ced 3271 async->events = 0;
ef4b4b27
IA
3272 if (!__comedi_is_subdevice_running(s)) {
3273 spin_unlock_irqrestore(&s->spin_lock, flags);
a5011a26 3274 return;
ef4b4b27 3275 }
a5011a26 3276
922d9ced 3277 if (events & COMEDI_CB_CANCEL_MASK)
ef4b4b27 3278 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
781f933c
HS
3279
3280 /*
ef4b4b27
IA
3281 * Remember if an error event has occurred, so an error can be
3282 * returned the next time the user does a read() or write().
781f933c 3283 */
ef4b4b27
IA
3284 if (events & COMEDI_CB_ERROR_MASK)
3285 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
883db3d9 3286
922d9ced 3287 if (async->cb_mask & events) {
c265be01 3288 wake_up_interruptible(&async->wait_head);
aa33122f 3289 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
a5011a26 3290 }
ef4b4b27
IA
3291
3292 spin_unlock_irqrestore(&s->spin_lock, flags);
3293
3294 if (si_code)
3295 kill_fasync(&dev->async_queue, SIGIO, si_code);
883db3d9 3296}
5660e742 3297EXPORT_SYMBOL_GPL(comedi_event);
883db3d9 3298
07778393 3299/* Note: the ->mutex is pre-locked on successful return */
7638ffcb 3300struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
a5011a26 3301{
7638ffcb 3302 struct comedi_device *dev;
a5011a26 3303 struct device *csdev;
d4d47895 3304 unsigned int i;
a5011a26 3305
36efbacd 3306 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
88cc30cf 3307 if (!dev)
7638ffcb 3308 return ERR_PTR(-ENOMEM);
7638ffcb 3309 comedi_device_init(dev);
db2e3487 3310 comedi_set_hw_dev(dev, hardware_device);
07778393 3311 mutex_lock(&dev->mutex);
5b7dba1b 3312 mutex_lock(&comedi_board_minor_table_lock);
38b9722a
IA
3313 for (i = hardware_device ? comedi_num_legacy_minors : 0;
3314 i < COMEDI_NUM_BOARD_MINORS; ++i) {
88cc30cf 3315 if (!comedi_board_minor_table[i]) {
cb6b79de 3316 comedi_board_minor_table[i] = dev;
a5011a26
HS
3317 break;
3318 }
3319 }
5b7dba1b 3320 mutex_unlock(&comedi_board_minor_table_lock);
a5011a26 3321 if (i == COMEDI_NUM_BOARD_MINORS) {
07778393 3322 mutex_unlock(&dev->mutex);
7638ffcb 3323 comedi_device_cleanup(dev);
5b13ed94 3324 comedi_dev_put(dev);
a112eab4
HM
3325 dev_err(hardware_device,
3326 "ran out of minor numbers for board device files\n");
7638ffcb 3327 return ERR_PTR(-EBUSY);
a5011a26 3328 }
7638ffcb 3329 dev->minor = i;
a5011a26
HS
3330 csdev = device_create(comedi_class, hardware_device,
3331 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
3332 if (!IS_ERR(csdev))
8f988d87 3333 dev->class_dev = get_device(csdev);
883db3d9 3334
07778393 3335 /* Note: dev->mutex needs to be unlocked by the caller. */
7638ffcb 3336 return dev;
883db3d9
FMH
3337}
3338
3346b798 3339void comedi_release_hardware_device(struct device *hardware_device)
883db3d9 3340{
a5011a26 3341 int minor;
cb6b79de 3342 struct comedi_device *dev;
883db3d9 3343
38b9722a
IA
3344 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
3345 minor++) {
5b7dba1b 3346 mutex_lock(&comedi_board_minor_table_lock);
cb6b79de
IA
3347 dev = comedi_board_minor_table[minor];
3348 if (dev && dev->hw_dev == hardware_device) {
5b7dba1b
IA
3349 comedi_board_minor_table[minor] = NULL;
3350 mutex_unlock(&comedi_board_minor_table_lock);
cb6b79de 3351 comedi_free_board_dev(dev);
3346b798 3352 break;
a5011a26 3353 }
5b7dba1b 3354 mutex_unlock(&comedi_board_minor_table_lock);
883db3d9 3355 }
883db3d9
FMH
3356}
3357
f65cc544 3358int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
883db3d9 3359{
f65cc544 3360 struct comedi_device *dev = s->device;
a5011a26 3361 struct device *csdev;
d4d47895 3362 unsigned int i;
883db3d9 3363
5b7dba1b
IA
3364 mutex_lock(&comedi_subdevice_minor_table_lock);
3365 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
88cc30cf 3366 if (!comedi_subdevice_minor_table[i]) {
bd5b4173 3367 comedi_subdevice_minor_table[i] = s;
a5011a26
HS
3368 break;
3369 }
3370 }
5b7dba1b
IA
3371 mutex_unlock(&comedi_subdevice_minor_table_lock);
3372 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
a112eab4
HM
3373 dev_err(dev->class_dev,
3374 "ran out of minor numbers for subdevice files\n");
a5011a26
HS
3375 return -EBUSY;
3376 }
5b7dba1b 3377 i += COMEDI_NUM_BOARD_MINORS;
a5011a26
HS
3378 s->minor = i;
3379 csdev = device_create(comedi_class, dev->class_dev,
3380 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
90a35c15 3381 dev->minor, s->index);
a5011a26
HS
3382 if (!IS_ERR(csdev))
3383 s->class_dev = csdev;
883db3d9 3384
da718546 3385 return 0;
883db3d9
FMH
3386}
3387
a5011a26 3388void comedi_free_subdevice_minor(struct comedi_subdevice *s)
883db3d9 3389{
0fcc9d48 3390 unsigned int i;
883db3d9 3391
88cc30cf 3392 if (!s)
a5011a26 3393 return;
5104a898
HS
3394 if (s->minor < COMEDI_NUM_BOARD_MINORS ||
3395 s->minor >= COMEDI_NUM_MINORS)
a5011a26 3396 return;
883db3d9 3397
0fcc9d48
IA
3398 i = s->minor - COMEDI_NUM_BOARD_MINORS;
3399 mutex_lock(&comedi_subdevice_minor_table_lock);
bd5b4173
IA
3400 if (s == comedi_subdevice_minor_table[i])
3401 comedi_subdevice_minor_table[i] = NULL;
0fcc9d48 3402 mutex_unlock(&comedi_subdevice_minor_table_lock);
a5011a26
HS
3403 if (s->class_dev) {
3404 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
3405 s->class_dev = NULL;
883db3d9 3406 }
883db3d9 3407}
a5787824 3408
682b9119 3409static void comedi_cleanup_board_minors(void)
76cca89f 3410{
2be8ae58 3411 struct comedi_device *dev;
d4d47895 3412 unsigned int i;
76cca89f 3413
2be8ae58
HS
3414 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
3415 dev = comedi_clear_board_minor(i);
3416 comedi_free_board_dev(dev);
3417 }
76cca89f
HS
3418}
3419
91fa0b0c
HS
3420static int __init comedi_init(void)
3421{
3422 int i;
3423 int retval;
3424
c2ad078b 3425 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
91fa0b0c 3426
d1d78d20 3427 if (comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
c2ad078b 3428 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
91fa0b0c
HS
3429 COMEDI_NUM_BOARD_MINORS);
3430 return -EINVAL;
3431 }
3432
91fa0b0c
HS
3433 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
3434 COMEDI_NUM_MINORS, "comedi");
3435 if (retval)
125178d1
IA
3436 return retval;
3437
91fa0b0c
HS
3438 cdev_init(&comedi_cdev, &comedi_fops);
3439 comedi_cdev.owner = THIS_MODULE;
a5bde3a1
AP
3440
3441 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
125178d1
IA
3442 if (retval)
3443 goto out_unregister_chrdev_region;
3444
3445 retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
3446 COMEDI_NUM_MINORS);
3447 if (retval)
3448 goto out_unregister_chrdev_region;
a5bde3a1 3449
91fa0b0c
HS
3450 comedi_class = class_create(THIS_MODULE, "comedi");
3451 if (IS_ERR(comedi_class)) {
125178d1 3452 retval = PTR_ERR(comedi_class);
c2ad078b 3453 pr_err("failed to create class\n");
125178d1 3454 goto out_cdev_del;
91fa0b0c
HS
3455 }
3456
e56341ad 3457 comedi_class->dev_groups = comedi_dev_groups;
91fa0b0c 3458
91fa0b0c
HS
3459 /* create devices files for legacy/manual use */
3460 for (i = 0; i < comedi_num_legacy_minors; i++) {
7638ffcb 3461 struct comedi_device *dev;
4bac39f6 3462
7638ffcb
IA
3463 dev = comedi_alloc_board_minor(NULL);
3464 if (IS_ERR(dev)) {
125178d1
IA
3465 retval = PTR_ERR(dev);
3466 goto out_cleanup_board_minors;
91fa0b0c 3467 }
cb3aadae 3468 /* comedi_alloc_board_minor() locked the mutex */
77c21b62 3469 lockdep_assert_held(&dev->mutex);
cb3aadae 3470 mutex_unlock(&dev->mutex);
91fa0b0c
HS
3471 }
3472
bf279ece
CKC
3473 /* XXX requires /proc interface */
3474 comedi_proc_init();
3475
91fa0b0c 3476 return 0;
125178d1
IA
3477
3478out_cleanup_board_minors:
3479 comedi_cleanup_board_minors();
3480 class_destroy(comedi_class);
3481out_cdev_del:
3482 cdev_del(&comedi_cdev);
3483out_unregister_chrdev_region:
3484 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
3485 return retval;
91fa0b0c
HS
3486}
3487module_init(comedi_init);
3488
3489static void __exit comedi_cleanup(void)
3490{
682b9119 3491 comedi_cleanup_board_minors();
91fa0b0c
HS
3492 class_destroy(comedi_class);
3493 cdev_del(&comedi_cdev);
3494 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
3495
3496 comedi_proc_cleanup();
3497}
3498module_exit(comedi_cleanup);
3499
a5787824
HS
3500MODULE_AUTHOR("http://www.comedi.org");
3501MODULE_DESCRIPTION("Comedi core module");
3502MODULE_LICENSE("GPL");