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