• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * comedi/comedi_fops.c
3  * comedi kernel module
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18 
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #include "comedi_compat32.h"
22 
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/vmalloc.h>
36 #include <linux/fs.h>
37 #include "comedidev.h"
38 #include <linux/cdev.h>
39 #include <linux/stat.h>
40 
41 #include <linux/io.h>
42 #include <linux/uaccess.h>
43 
44 #include "comedi_internal.h"
45 
46 /*
47  * comedi_subdevice "runflags"
48  * COMEDI_SRF_RT:		DEPRECATED: command is running real-time
49  * COMEDI_SRF_ERROR:		indicates an COMEDI_CB_ERROR event has occurred
50  *				since the last command was started
51  * COMEDI_SRF_RUNNING:		command is running
52  * COMEDI_SRF_FREE_SPRIV:	free s->private on detach
53  *
54  * COMEDI_SRF_BUSY_MASK:	runflags that indicate the subdevice is "busy"
55  */
56 #define COMEDI_SRF_RT		BIT(1)
57 #define COMEDI_SRF_ERROR	BIT(2)
58 #define COMEDI_SRF_RUNNING	BIT(27)
59 #define COMEDI_SRF_FREE_SPRIV	BIT(31)
60 
61 #define COMEDI_SRF_BUSY_MASK	(COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
62 
63 /**
64  * struct comedi_file - Per-file private data for COMEDI device
65  * @dev: COMEDI device.
66  * @read_subdev: Current "read" subdevice.
67  * @write_subdev: Current "write" subdevice.
68  * @last_detach_count: Last known detach count.
69  * @last_attached: Last known attached/detached state.
70  */
71 struct comedi_file {
72 	struct comedi_device *dev;
73 	struct comedi_subdevice *read_subdev;
74 	struct comedi_subdevice *write_subdev;
75 	unsigned int last_detach_count;
76 	bool last_attached:1;
77 };
78 
79 #define COMEDI_NUM_MINORS 0x100
80 #define COMEDI_NUM_SUBDEVICE_MINORS	\
81 	(COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
82 
83 static int comedi_num_legacy_minors;
84 module_param(comedi_num_legacy_minors, int, S_IRUGO);
85 MODULE_PARM_DESC(comedi_num_legacy_minors,
86 		 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
87 		);
88 
89 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
90 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
91 MODULE_PARM_DESC(comedi_default_buf_size_kb,
92 		 "default asynchronous buffer size in KiB (default "
93 		 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
94 
95 unsigned int comedi_default_buf_maxsize_kb
96 	= CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
97 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
98 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
99 		 "default maximum size of asynchronous buffer in KiB (default "
100 		 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
101 
102 static DEFINE_MUTEX(comedi_board_minor_table_lock);
103 static struct comedi_device
104 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
105 
106 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
107 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
108 static struct comedi_subdevice
109 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
110 
111 static struct class *comedi_class;
112 static struct cdev comedi_cdev;
113 
comedi_device_init(struct comedi_device * dev)114 static void comedi_device_init(struct comedi_device *dev)
115 {
116 	kref_init(&dev->refcount);
117 	spin_lock_init(&dev->spinlock);
118 	mutex_init(&dev->mutex);
119 	init_rwsem(&dev->attach_lock);
120 	dev->minor = -1;
121 }
122 
comedi_dev_kref_release(struct kref * kref)123 static void comedi_dev_kref_release(struct kref *kref)
124 {
125 	struct comedi_device *dev =
126 		container_of(kref, struct comedi_device, refcount);
127 
128 	mutex_destroy(&dev->mutex);
129 	put_device(dev->class_dev);
130 	kfree(dev);
131 }
132 
133 /**
134  * comedi_dev_put() - Release a use of a COMEDI device
135  * @dev: COMEDI device.
136  *
137  * Must be called when a user of a COMEDI device is finished with it.
138  * When the last user of the COMEDI device calls this function, the
139  * COMEDI device is destroyed.
140  *
141  * Return: 1 if the COMEDI device is destroyed by this call or @dev is
142  * NULL, otherwise return 0.  Callers must not assume the COMEDI
143  * device is still valid if this function returns 0.
144  */
comedi_dev_put(struct comedi_device * dev)145 int comedi_dev_put(struct comedi_device *dev)
146 {
147 	if (dev)
148 		return kref_put(&dev->refcount, comedi_dev_kref_release);
149 	return 1;
150 }
151 EXPORT_SYMBOL_GPL(comedi_dev_put);
152 
comedi_dev_get(struct comedi_device * dev)153 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
154 {
155 	if (dev)
156 		kref_get(&dev->refcount);
157 	return dev;
158 }
159 
comedi_device_cleanup(struct comedi_device * dev)160 static void comedi_device_cleanup(struct comedi_device *dev)
161 {
162 	struct module *driver_module = NULL;
163 
164 	if (!dev)
165 		return;
166 	mutex_lock(&dev->mutex);
167 	if (dev->attached)
168 		driver_module = dev->driver->module;
169 	comedi_device_detach(dev);
170 	if (driver_module && dev->use_count)
171 		module_put(driver_module);
172 	mutex_unlock(&dev->mutex);
173 }
174 
comedi_clear_board_dev(struct comedi_device * dev)175 static bool comedi_clear_board_dev(struct comedi_device *dev)
176 {
177 	unsigned int i = dev->minor;
178 	bool cleared = false;
179 
180 	mutex_lock(&comedi_board_minor_table_lock);
181 	if (dev == comedi_board_minor_table[i]) {
182 		comedi_board_minor_table[i] = NULL;
183 		cleared = true;
184 	}
185 	mutex_unlock(&comedi_board_minor_table_lock);
186 	return cleared;
187 }
188 
comedi_clear_board_minor(unsigned minor)189 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
190 {
191 	struct comedi_device *dev;
192 
193 	mutex_lock(&comedi_board_minor_table_lock);
194 	dev = comedi_board_minor_table[minor];
195 	comedi_board_minor_table[minor] = NULL;
196 	mutex_unlock(&comedi_board_minor_table_lock);
197 	return dev;
198 }
199 
comedi_free_board_dev(struct comedi_device * dev)200 static void comedi_free_board_dev(struct comedi_device *dev)
201 {
202 	if (dev) {
203 		comedi_device_cleanup(dev);
204 		if (dev->class_dev) {
205 			device_destroy(comedi_class,
206 				       MKDEV(COMEDI_MAJOR, dev->minor));
207 		}
208 		comedi_dev_put(dev);
209 	}
210 }
211 
212 static struct comedi_subdevice
comedi_subdevice_from_minor(const struct comedi_device * dev,unsigned minor)213 *comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned minor)
214 {
215 	struct comedi_subdevice *s;
216 	unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
217 
218 	mutex_lock(&comedi_subdevice_minor_table_lock);
219 	s = comedi_subdevice_minor_table[i];
220 	if (s && s->device != dev)
221 		s = NULL;
222 	mutex_unlock(&comedi_subdevice_minor_table_lock);
223 	return s;
224 }
225 
comedi_dev_get_from_board_minor(unsigned minor)226 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
227 {
228 	struct comedi_device *dev;
229 
230 	mutex_lock(&comedi_board_minor_table_lock);
231 	dev = comedi_dev_get(comedi_board_minor_table[minor]);
232 	mutex_unlock(&comedi_board_minor_table_lock);
233 	return dev;
234 }
235 
comedi_dev_get_from_subdevice_minor(unsigned minor)236 static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
237 {
238 	struct comedi_device *dev;
239 	struct comedi_subdevice *s;
240 	unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
241 
242 	mutex_lock(&comedi_subdevice_minor_table_lock);
243 	s = comedi_subdevice_minor_table[i];
244 	dev = comedi_dev_get(s ? s->device : NULL);
245 	mutex_unlock(&comedi_subdevice_minor_table_lock);
246 	return dev;
247 }
248 
249 /**
250  * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
251  * @minor: Minor device number.
252  *
253  * Finds the COMEDI device associated with the minor device number, if any,
254  * and increments its reference count.  The COMEDI device is prevented from
255  * being freed until a matching call is made to comedi_dev_put().
256  *
257  * Return: A pointer to the COMEDI device if it exists, with its usage
258  * reference incremented.  Return NULL if no COMEDI device exists with the
259  * specified minor device number.
260  */
comedi_dev_get_from_minor(unsigned minor)261 struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
262 {
263 	if (minor < COMEDI_NUM_BOARD_MINORS)
264 		return comedi_dev_get_from_board_minor(minor);
265 
266 	return comedi_dev_get_from_subdevice_minor(minor);
267 }
268 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
269 
270 static struct comedi_subdevice *
comedi_read_subdevice(const struct comedi_device * dev,unsigned int minor)271 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
272 {
273 	struct comedi_subdevice *s;
274 
275 	if (minor >= COMEDI_NUM_BOARD_MINORS) {
276 		s = comedi_subdevice_from_minor(dev, minor);
277 		if (!s || (s->subdev_flags & SDF_CMD_READ))
278 			return s;
279 	}
280 	return dev->read_subdev;
281 }
282 
283 static struct comedi_subdevice *
comedi_write_subdevice(const struct comedi_device * dev,unsigned int minor)284 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
285 {
286 	struct comedi_subdevice *s;
287 
288 	if (minor >= COMEDI_NUM_BOARD_MINORS) {
289 		s = comedi_subdevice_from_minor(dev, minor);
290 		if (!s || (s->subdev_flags & SDF_CMD_WRITE))
291 			return s;
292 	}
293 	return dev->write_subdev;
294 }
295 
comedi_file_reset(struct file * file)296 static void comedi_file_reset(struct file *file)
297 {
298 	struct comedi_file *cfp = file->private_data;
299 	struct comedi_device *dev = cfp->dev;
300 	struct comedi_subdevice *s, *read_s, *write_s;
301 	unsigned int minor = iminor(file_inode(file));
302 
303 	read_s = dev->read_subdev;
304 	write_s = dev->write_subdev;
305 	if (minor >= COMEDI_NUM_BOARD_MINORS) {
306 		s = comedi_subdevice_from_minor(dev, minor);
307 		if (!s || s->subdev_flags & SDF_CMD_READ)
308 			read_s = s;
309 		if (!s || s->subdev_flags & SDF_CMD_WRITE)
310 			write_s = s;
311 	}
312 	cfp->last_attached = dev->attached;
313 	cfp->last_detach_count = dev->detach_count;
314 	ACCESS_ONCE(cfp->read_subdev) = read_s;
315 	ACCESS_ONCE(cfp->write_subdev) = write_s;
316 }
317 
comedi_file_check(struct file * file)318 static void comedi_file_check(struct file *file)
319 {
320 	struct comedi_file *cfp = file->private_data;
321 	struct comedi_device *dev = cfp->dev;
322 
323 	if (cfp->last_attached != dev->attached ||
324 	    cfp->last_detach_count != dev->detach_count)
325 		comedi_file_reset(file);
326 }
327 
comedi_file_read_subdevice(struct file * file)328 static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
329 {
330 	struct comedi_file *cfp = file->private_data;
331 
332 	comedi_file_check(file);
333 	return ACCESS_ONCE(cfp->read_subdev);
334 }
335 
comedi_file_write_subdevice(struct file * file)336 static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
337 {
338 	struct comedi_file *cfp = file->private_data;
339 
340 	comedi_file_check(file);
341 	return ACCESS_ONCE(cfp->write_subdev);
342 }
343 
resize_async_buffer(struct comedi_device * dev,struct comedi_subdevice * s,unsigned new_size)344 static int resize_async_buffer(struct comedi_device *dev,
345 			       struct comedi_subdevice *s, unsigned new_size)
346 {
347 	struct comedi_async *async = s->async;
348 	int retval;
349 
350 	if (new_size > async->max_bufsize)
351 		return -EPERM;
352 
353 	if (s->busy) {
354 		dev_dbg(dev->class_dev,
355 			"subdevice is busy, cannot resize buffer\n");
356 		return -EBUSY;
357 	}
358 	if (comedi_buf_is_mmapped(s)) {
359 		dev_dbg(dev->class_dev,
360 			"subdevice is mmapped, cannot resize buffer\n");
361 		return -EBUSY;
362 	}
363 
364 	/* make sure buffer is an integral number of pages (we round up) */
365 	new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
366 
367 	retval = comedi_buf_alloc(dev, s, new_size);
368 	if (retval < 0)
369 		return retval;
370 
371 	if (s->buf_change) {
372 		retval = s->buf_change(dev, s);
373 		if (retval < 0)
374 			return retval;
375 	}
376 
377 	dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
378 		s->index, async->prealloc_bufsz);
379 	return 0;
380 }
381 
382 /* sysfs attribute files */
383 
max_read_buffer_kb_show(struct device * csdev,struct device_attribute * attr,char * buf)384 static ssize_t max_read_buffer_kb_show(struct device *csdev,
385 				       struct device_attribute *attr, char *buf)
386 {
387 	unsigned int minor = MINOR(csdev->devt);
388 	struct comedi_device *dev;
389 	struct comedi_subdevice *s;
390 	unsigned int size = 0;
391 
392 	dev = comedi_dev_get_from_minor(minor);
393 	if (!dev)
394 		return -ENODEV;
395 
396 	mutex_lock(&dev->mutex);
397 	s = comedi_read_subdevice(dev, minor);
398 	if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
399 		size = s->async->max_bufsize / 1024;
400 	mutex_unlock(&dev->mutex);
401 
402 	comedi_dev_put(dev);
403 	return snprintf(buf, PAGE_SIZE, "%u\n", size);
404 }
405 
max_read_buffer_kb_store(struct device * csdev,struct device_attribute * attr,const char * buf,size_t count)406 static ssize_t max_read_buffer_kb_store(struct device *csdev,
407 					struct device_attribute *attr,
408 					const char *buf, size_t count)
409 {
410 	unsigned int minor = MINOR(csdev->devt);
411 	struct comedi_device *dev;
412 	struct comedi_subdevice *s;
413 	unsigned int size;
414 	int err;
415 
416 	err = kstrtouint(buf, 10, &size);
417 	if (err)
418 		return err;
419 	if (size > (UINT_MAX / 1024))
420 		return -EINVAL;
421 	size *= 1024;
422 
423 	dev = comedi_dev_get_from_minor(minor);
424 	if (!dev)
425 		return -ENODEV;
426 
427 	mutex_lock(&dev->mutex);
428 	s = comedi_read_subdevice(dev, minor);
429 	if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
430 		s->async->max_bufsize = size;
431 	else
432 		err = -EINVAL;
433 	mutex_unlock(&dev->mutex);
434 
435 	comedi_dev_put(dev);
436 	return err ? err : count;
437 }
438 static DEVICE_ATTR_RW(max_read_buffer_kb);
439 
read_buffer_kb_show(struct device * csdev,struct device_attribute * attr,char * buf)440 static ssize_t read_buffer_kb_show(struct device *csdev,
441 				   struct device_attribute *attr, char *buf)
442 {
443 	unsigned int minor = MINOR(csdev->devt);
444 	struct comedi_device *dev;
445 	struct comedi_subdevice *s;
446 	unsigned int size = 0;
447 
448 	dev = comedi_dev_get_from_minor(minor);
449 	if (!dev)
450 		return -ENODEV;
451 
452 	mutex_lock(&dev->mutex);
453 	s = comedi_read_subdevice(dev, minor);
454 	if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
455 		size = s->async->prealloc_bufsz / 1024;
456 	mutex_unlock(&dev->mutex);
457 
458 	comedi_dev_put(dev);
459 	return snprintf(buf, PAGE_SIZE, "%u\n", size);
460 }
461 
read_buffer_kb_store(struct device * csdev,struct device_attribute * attr,const char * buf,size_t count)462 static ssize_t read_buffer_kb_store(struct device *csdev,
463 				    struct device_attribute *attr,
464 				    const char *buf, size_t count)
465 {
466 	unsigned int minor = MINOR(csdev->devt);
467 	struct comedi_device *dev;
468 	struct comedi_subdevice *s;
469 	unsigned int size;
470 	int err;
471 
472 	err = kstrtouint(buf, 10, &size);
473 	if (err)
474 		return err;
475 	if (size > (UINT_MAX / 1024))
476 		return -EINVAL;
477 	size *= 1024;
478 
479 	dev = comedi_dev_get_from_minor(minor);
480 	if (!dev)
481 		return -ENODEV;
482 
483 	mutex_lock(&dev->mutex);
484 	s = comedi_read_subdevice(dev, minor);
485 	if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
486 		err = resize_async_buffer(dev, s, size);
487 	else
488 		err = -EINVAL;
489 	mutex_unlock(&dev->mutex);
490 
491 	comedi_dev_put(dev);
492 	return err ? err : count;
493 }
494 static DEVICE_ATTR_RW(read_buffer_kb);
495 
max_write_buffer_kb_show(struct device * csdev,struct device_attribute * attr,char * buf)496 static ssize_t max_write_buffer_kb_show(struct device *csdev,
497 					struct device_attribute *attr,
498 					char *buf)
499 {
500 	unsigned int minor = MINOR(csdev->devt);
501 	struct comedi_device *dev;
502 	struct comedi_subdevice *s;
503 	unsigned int size = 0;
504 
505 	dev = comedi_dev_get_from_minor(minor);
506 	if (!dev)
507 		return -ENODEV;
508 
509 	mutex_lock(&dev->mutex);
510 	s = comedi_write_subdevice(dev, minor);
511 	if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
512 		size = s->async->max_bufsize / 1024;
513 	mutex_unlock(&dev->mutex);
514 
515 	comedi_dev_put(dev);
516 	return snprintf(buf, PAGE_SIZE, "%u\n", size);
517 }
518 
max_write_buffer_kb_store(struct device * csdev,struct device_attribute * attr,const char * buf,size_t count)519 static ssize_t max_write_buffer_kb_store(struct device *csdev,
520 					 struct device_attribute *attr,
521 					 const char *buf, size_t count)
522 {
523 	unsigned int minor = MINOR(csdev->devt);
524 	struct comedi_device *dev;
525 	struct comedi_subdevice *s;
526 	unsigned int size;
527 	int err;
528 
529 	err = kstrtouint(buf, 10, &size);
530 	if (err)
531 		return err;
532 	if (size > (UINT_MAX / 1024))
533 		return -EINVAL;
534 	size *= 1024;
535 
536 	dev = comedi_dev_get_from_minor(minor);
537 	if (!dev)
538 		return -ENODEV;
539 
540 	mutex_lock(&dev->mutex);
541 	s = comedi_write_subdevice(dev, minor);
542 	if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
543 		s->async->max_bufsize = size;
544 	else
545 		err = -EINVAL;
546 	mutex_unlock(&dev->mutex);
547 
548 	comedi_dev_put(dev);
549 	return err ? err : count;
550 }
551 static DEVICE_ATTR_RW(max_write_buffer_kb);
552 
write_buffer_kb_show(struct device * csdev,struct device_attribute * attr,char * buf)553 static ssize_t write_buffer_kb_show(struct device *csdev,
554 				    struct device_attribute *attr, char *buf)
555 {
556 	unsigned int minor = MINOR(csdev->devt);
557 	struct comedi_device *dev;
558 	struct comedi_subdevice *s;
559 	unsigned int size = 0;
560 
561 	dev = comedi_dev_get_from_minor(minor);
562 	if (!dev)
563 		return -ENODEV;
564 
565 	mutex_lock(&dev->mutex);
566 	s = comedi_write_subdevice(dev, minor);
567 	if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
568 		size = s->async->prealloc_bufsz / 1024;
569 	mutex_unlock(&dev->mutex);
570 
571 	comedi_dev_put(dev);
572 	return snprintf(buf, PAGE_SIZE, "%u\n", size);
573 }
574 
write_buffer_kb_store(struct device * csdev,struct device_attribute * attr,const char * buf,size_t count)575 static ssize_t write_buffer_kb_store(struct device *csdev,
576 				     struct device_attribute *attr,
577 				     const char *buf, size_t count)
578 {
579 	unsigned int minor = MINOR(csdev->devt);
580 	struct comedi_device *dev;
581 	struct comedi_subdevice *s;
582 	unsigned int size;
583 	int err;
584 
585 	err = kstrtouint(buf, 10, &size);
586 	if (err)
587 		return err;
588 	if (size > (UINT_MAX / 1024))
589 		return -EINVAL;
590 	size *= 1024;
591 
592 	dev = comedi_dev_get_from_minor(minor);
593 	if (!dev)
594 		return -ENODEV;
595 
596 	mutex_lock(&dev->mutex);
597 	s = comedi_write_subdevice(dev, minor);
598 	if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
599 		err = resize_async_buffer(dev, s, size);
600 	else
601 		err = -EINVAL;
602 	mutex_unlock(&dev->mutex);
603 
604 	comedi_dev_put(dev);
605 	return err ? err : count;
606 }
607 static DEVICE_ATTR_RW(write_buffer_kb);
608 
609 static struct attribute *comedi_dev_attrs[] = {
610 	&dev_attr_max_read_buffer_kb.attr,
611 	&dev_attr_read_buffer_kb.attr,
612 	&dev_attr_max_write_buffer_kb.attr,
613 	&dev_attr_write_buffer_kb.attr,
614 	NULL,
615 };
616 ATTRIBUTE_GROUPS(comedi_dev);
617 
__comedi_clear_subdevice_runflags(struct comedi_subdevice * s,unsigned bits)618 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
619 					      unsigned bits)
620 {
621 	s->runflags &= ~bits;
622 }
623 
__comedi_set_subdevice_runflags(struct comedi_subdevice * s,unsigned bits)624 static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
625 					    unsigned bits)
626 {
627 	s->runflags |= bits;
628 }
629 
comedi_update_subdevice_runflags(struct comedi_subdevice * s,unsigned mask,unsigned bits)630 static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
631 					     unsigned mask, unsigned bits)
632 {
633 	unsigned long flags;
634 
635 	spin_lock_irqsave(&s->spin_lock, flags);
636 	__comedi_clear_subdevice_runflags(s, mask);
637 	__comedi_set_subdevice_runflags(s, bits & mask);
638 	spin_unlock_irqrestore(&s->spin_lock, flags);
639 }
640 
__comedi_get_subdevice_runflags(struct comedi_subdevice * s)641 static unsigned __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
642 {
643 	return s->runflags;
644 }
645 
comedi_get_subdevice_runflags(struct comedi_subdevice * s)646 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
647 {
648 	unsigned long flags;
649 	unsigned runflags;
650 
651 	spin_lock_irqsave(&s->spin_lock, flags);
652 	runflags = __comedi_get_subdevice_runflags(s);
653 	spin_unlock_irqrestore(&s->spin_lock, flags);
654 	return runflags;
655 }
656 
comedi_is_runflags_running(unsigned runflags)657 static bool comedi_is_runflags_running(unsigned runflags)
658 {
659 	return runflags & COMEDI_SRF_RUNNING;
660 }
661 
comedi_is_runflags_in_error(unsigned runflags)662 static bool comedi_is_runflags_in_error(unsigned runflags)
663 {
664 	return runflags & COMEDI_SRF_ERROR;
665 }
666 
667 /**
668  * comedi_is_subdevice_running() - Check if async command running on subdevice
669  * @s: COMEDI subdevice.
670  *
671  * Return: %true if an asynchronous COMEDI command is active on the
672  * subdevice, else %false.
673  */
comedi_is_subdevice_running(struct comedi_subdevice * s)674 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
675 {
676 	unsigned runflags = comedi_get_subdevice_runflags(s);
677 
678 	return comedi_is_runflags_running(runflags);
679 }
680 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
681 
__comedi_is_subdevice_running(struct comedi_subdevice * s)682 static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
683 {
684 	unsigned runflags = __comedi_get_subdevice_runflags(s);
685 
686 	return comedi_is_runflags_running(runflags);
687 }
688 
comedi_is_subdevice_idle(struct comedi_subdevice * s)689 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
690 {
691 	unsigned runflags = comedi_get_subdevice_runflags(s);
692 
693 	return !(runflags & COMEDI_SRF_BUSY_MASK);
694 }
695 
comedi_can_auto_free_spriv(struct comedi_subdevice * s)696 bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
697 {
698 	unsigned runflags = __comedi_get_subdevice_runflags(s);
699 
700 	return runflags & COMEDI_SRF_FREE_SPRIV;
701 }
702 
703 /**
704  * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
705  * @s: COMEDI subdevice.
706  *
707  * Mark the subdevice as having a pointer to private data that can be
708  * automatically freed when the COMEDI device is detached from the low-level
709  * driver.
710  */
comedi_set_spriv_auto_free(struct comedi_subdevice * s)711 void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
712 {
713 	__comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
714 }
715 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
716 
717 /**
718  * comedi_alloc_spriv - Allocate memory for the subdevice private data
719  * @s: COMEDI subdevice.
720  * @size: Size of the memory to allocate.
721  *
722  * Allocate memory for the subdevice private data and point @s->private
723  * to it.  The memory will be freed automatically when the COMEDI device
724  * is detached from the low-level driver.
725  *
726  * Return: A pointer to the allocated memory @s->private on success.
727  * Return NULL on failure.
728  */
comedi_alloc_spriv(struct comedi_subdevice * s,size_t size)729 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
730 {
731 	s->private = kzalloc(size, GFP_KERNEL);
732 	if (s->private)
733 		comedi_set_spriv_auto_free(s);
734 	return s->private;
735 }
736 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
737 
738 /*
739  * This function restores a subdevice to an idle state.
740  */
do_become_nonbusy(struct comedi_device * dev,struct comedi_subdevice * s)741 static void do_become_nonbusy(struct comedi_device *dev,
742 			      struct comedi_subdevice *s)
743 {
744 	struct comedi_async *async = s->async;
745 
746 	comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
747 	if (async) {
748 		comedi_buf_reset(s);
749 		async->inttrig = NULL;
750 		kfree(async->cmd.chanlist);
751 		async->cmd.chanlist = NULL;
752 		s->busy = NULL;
753 		wake_up_interruptible_all(&async->wait_head);
754 	} else {
755 		dev_err(dev->class_dev,
756 			"BUG: (?) do_become_nonbusy called with async=NULL\n");
757 		s->busy = NULL;
758 	}
759 }
760 
do_cancel(struct comedi_device * dev,struct comedi_subdevice * s)761 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
762 {
763 	int ret = 0;
764 
765 	if (comedi_is_subdevice_running(s) && s->cancel)
766 		ret = s->cancel(dev, s);
767 
768 	do_become_nonbusy(dev, s);
769 
770 	return ret;
771 }
772 
comedi_device_cancel_all(struct comedi_device * dev)773 void comedi_device_cancel_all(struct comedi_device *dev)
774 {
775 	struct comedi_subdevice *s;
776 	int i;
777 
778 	if (!dev->attached)
779 		return;
780 
781 	for (i = 0; i < dev->n_subdevices; i++) {
782 		s = &dev->subdevices[i];
783 		if (s->async)
784 			do_cancel(dev, s);
785 	}
786 }
787 
is_device_busy(struct comedi_device * dev)788 static int is_device_busy(struct comedi_device *dev)
789 {
790 	struct comedi_subdevice *s;
791 	int i;
792 
793 	if (!dev->attached)
794 		return 0;
795 
796 	for (i = 0; i < dev->n_subdevices; i++) {
797 		s = &dev->subdevices[i];
798 		if (s->busy)
799 			return 1;
800 		if (s->async && comedi_buf_is_mmapped(s))
801 			return 1;
802 	}
803 
804 	return 0;
805 }
806 
807 /*
808  * COMEDI_DEVCONFIG ioctl
809  * attaches (and configures) or detaches a legacy device
810  *
811  * arg:
812  *	pointer to comedi_devconfig structure (NULL if detaching)
813  *
814  * reads:
815  *	comedi_devconfig structure (if attaching)
816  *
817  * writes:
818  *	nothing
819  */
do_devconfig_ioctl(struct comedi_device * dev,struct comedi_devconfig __user * arg)820 static int do_devconfig_ioctl(struct comedi_device *dev,
821 			      struct comedi_devconfig __user *arg)
822 {
823 	struct comedi_devconfig it;
824 
825 	if (!capable(CAP_SYS_ADMIN))
826 		return -EPERM;
827 
828 	if (!arg) {
829 		if (is_device_busy(dev))
830 			return -EBUSY;
831 		if (dev->attached) {
832 			struct module *driver_module = dev->driver->module;
833 
834 			comedi_device_detach(dev);
835 			module_put(driver_module);
836 		}
837 		return 0;
838 	}
839 
840 	if (copy_from_user(&it, arg, sizeof(it)))
841 		return -EFAULT;
842 
843 	it.board_name[COMEDI_NAMELEN - 1] = 0;
844 
845 	if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
846 		dev_warn(dev->class_dev,
847 			 "comedi_config --init_data is deprecated\n");
848 		return -EINVAL;
849 	}
850 
851 	if (dev->minor >= comedi_num_legacy_minors)
852 		/* don't re-use dynamically allocated comedi devices */
853 		return -EBUSY;
854 
855 	/* This increments the driver module count on success. */
856 	return comedi_device_attach(dev, &it);
857 }
858 
859 /*
860  * COMEDI_BUFCONFIG ioctl
861  * buffer configuration
862  *
863  * arg:
864  *	pointer to comedi_bufconfig structure
865  *
866  * reads:
867  *	comedi_bufconfig structure
868  *
869  * writes:
870  *	modified comedi_bufconfig structure
871  */
do_bufconfig_ioctl(struct comedi_device * dev,struct comedi_bufconfig __user * arg)872 static int do_bufconfig_ioctl(struct comedi_device *dev,
873 			      struct comedi_bufconfig __user *arg)
874 {
875 	struct comedi_bufconfig bc;
876 	struct comedi_async *async;
877 	struct comedi_subdevice *s;
878 	int retval = 0;
879 
880 	if (copy_from_user(&bc, arg, sizeof(bc)))
881 		return -EFAULT;
882 
883 	if (bc.subdevice >= dev->n_subdevices)
884 		return -EINVAL;
885 
886 	s = &dev->subdevices[bc.subdevice];
887 	async = s->async;
888 
889 	if (!async) {
890 		dev_dbg(dev->class_dev,
891 			"subdevice does not have async capability\n");
892 		bc.size = 0;
893 		bc.maximum_size = 0;
894 		goto copyback;
895 	}
896 
897 	if (bc.maximum_size) {
898 		if (!capable(CAP_SYS_ADMIN))
899 			return -EPERM;
900 
901 		async->max_bufsize = bc.maximum_size;
902 	}
903 
904 	if (bc.size) {
905 		retval = resize_async_buffer(dev, s, bc.size);
906 		if (retval < 0)
907 			return retval;
908 	}
909 
910 	bc.size = async->prealloc_bufsz;
911 	bc.maximum_size = async->max_bufsize;
912 
913 copyback:
914 	if (copy_to_user(arg, &bc, sizeof(bc)))
915 		return -EFAULT;
916 
917 	return 0;
918 }
919 
920 /*
921  * COMEDI_DEVINFO ioctl
922  * device info
923  *
924  * arg:
925  *	pointer to comedi_devinfo structure
926  *
927  * reads:
928  *	nothing
929  *
930  * writes:
931  *	comedi_devinfo structure
932  */
do_devinfo_ioctl(struct comedi_device * dev,struct comedi_devinfo __user * arg,struct file * file)933 static int do_devinfo_ioctl(struct comedi_device *dev,
934 			    struct comedi_devinfo __user *arg,
935 			    struct file *file)
936 {
937 	struct comedi_subdevice *s;
938 	struct comedi_devinfo devinfo;
939 
940 	memset(&devinfo, 0, sizeof(devinfo));
941 
942 	/* fill devinfo structure */
943 	devinfo.version_code = COMEDI_VERSION_CODE;
944 	devinfo.n_subdevs = dev->n_subdevices;
945 	strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
946 	strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
947 
948 	s = comedi_file_read_subdevice(file);
949 	if (s)
950 		devinfo.read_subdevice = s->index;
951 	else
952 		devinfo.read_subdevice = -1;
953 
954 	s = comedi_file_write_subdevice(file);
955 	if (s)
956 		devinfo.write_subdevice = s->index;
957 	else
958 		devinfo.write_subdevice = -1;
959 
960 	if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
961 		return -EFAULT;
962 
963 	return 0;
964 }
965 
966 /*
967  * COMEDI_SUBDINFO ioctl
968  * subdevices info
969  *
970  * arg:
971  *	pointer to array of comedi_subdinfo structures
972  *
973  * reads:
974  *	nothing
975  *
976  * writes:
977  *	array of comedi_subdinfo structures
978  */
do_subdinfo_ioctl(struct comedi_device * dev,struct comedi_subdinfo __user * arg,void * file)979 static int do_subdinfo_ioctl(struct comedi_device *dev,
980 			     struct comedi_subdinfo __user *arg, void *file)
981 {
982 	int ret, i;
983 	struct comedi_subdinfo *tmp, *us;
984 	struct comedi_subdevice *s;
985 
986 	tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
987 	if (!tmp)
988 		return -ENOMEM;
989 
990 	/* fill subdinfo structs */
991 	for (i = 0; i < dev->n_subdevices; i++) {
992 		s = &dev->subdevices[i];
993 		us = tmp + i;
994 
995 		us->type = s->type;
996 		us->n_chan = s->n_chan;
997 		us->subd_flags = s->subdev_flags;
998 		if (comedi_is_subdevice_running(s))
999 			us->subd_flags |= SDF_RUNNING;
1000 #define TIMER_nanosec 5		/* backwards compatibility */
1001 		us->timer_type = TIMER_nanosec;
1002 		us->len_chanlist = s->len_chanlist;
1003 		us->maxdata = s->maxdata;
1004 		if (s->range_table) {
1005 			us->range_type =
1006 			    (i << 24) | (0 << 16) | (s->range_table->length);
1007 		} else {
1008 			us->range_type = 0;	/* XXX */
1009 		}
1010 
1011 		if (s->busy)
1012 			us->subd_flags |= SDF_BUSY;
1013 		if (s->busy == file)
1014 			us->subd_flags |= SDF_BUSY_OWNER;
1015 		if (s->lock)
1016 			us->subd_flags |= SDF_LOCKED;
1017 		if (s->lock == file)
1018 			us->subd_flags |= SDF_LOCK_OWNER;
1019 		if (!s->maxdata && s->maxdata_list)
1020 			us->subd_flags |= SDF_MAXDATA;
1021 		if (s->range_table_list)
1022 			us->subd_flags |= SDF_RANGETYPE;
1023 		if (s->do_cmd)
1024 			us->subd_flags |= SDF_CMD;
1025 
1026 		if (s->insn_bits != &insn_inval)
1027 			us->insn_bits_support = COMEDI_SUPPORTED;
1028 		else
1029 			us->insn_bits_support = COMEDI_UNSUPPORTED;
1030 	}
1031 
1032 	ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
1033 
1034 	kfree(tmp);
1035 
1036 	return ret ? -EFAULT : 0;
1037 }
1038 
1039 /*
1040  * COMEDI_CHANINFO ioctl
1041  * subdevice channel info
1042  *
1043  * arg:
1044  *	pointer to comedi_chaninfo structure
1045  *
1046  * reads:
1047  *	comedi_chaninfo structure
1048  *
1049  * writes:
1050  *	array of maxdata values to chaninfo->maxdata_list if requested
1051  *	array of range table lengths to chaninfo->range_table_list if requested
1052  */
do_chaninfo_ioctl(struct comedi_device * dev,struct comedi_chaninfo __user * arg)1053 static int do_chaninfo_ioctl(struct comedi_device *dev,
1054 			     struct comedi_chaninfo __user *arg)
1055 {
1056 	struct comedi_subdevice *s;
1057 	struct comedi_chaninfo it;
1058 
1059 	if (copy_from_user(&it, arg, sizeof(it)))
1060 		return -EFAULT;
1061 
1062 	if (it.subdev >= dev->n_subdevices)
1063 		return -EINVAL;
1064 	s = &dev->subdevices[it.subdev];
1065 
1066 	if (it.maxdata_list) {
1067 		if (s->maxdata || !s->maxdata_list)
1068 			return -EINVAL;
1069 		if (copy_to_user(it.maxdata_list, s->maxdata_list,
1070 				 s->n_chan * sizeof(unsigned int)))
1071 			return -EFAULT;
1072 	}
1073 
1074 	if (it.flaglist)
1075 		return -EINVAL;	/* flaglist not supported */
1076 
1077 	if (it.rangelist) {
1078 		int i;
1079 
1080 		if (!s->range_table_list)
1081 			return -EINVAL;
1082 		for (i = 0; i < s->n_chan; i++) {
1083 			int x;
1084 
1085 			x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
1086 			    (s->range_table_list[i]->length);
1087 			if (put_user(x, it.rangelist + i))
1088 				return -EFAULT;
1089 		}
1090 	}
1091 
1092 	return 0;
1093 }
1094 
1095 /*
1096  * COMEDI_BUFINFO ioctl
1097  * buffer information
1098  *
1099  * arg:
1100  *	pointer to comedi_bufinfo structure
1101  *
1102  * reads:
1103  *	comedi_bufinfo structure
1104  *
1105  * writes:
1106  *	modified comedi_bufinfo structure
1107  */
do_bufinfo_ioctl(struct comedi_device * dev,struct comedi_bufinfo __user * arg,void * file)1108 static int do_bufinfo_ioctl(struct comedi_device *dev,
1109 			    struct comedi_bufinfo __user *arg, void *file)
1110 {
1111 	struct comedi_bufinfo bi;
1112 	struct comedi_subdevice *s;
1113 	struct comedi_async *async;
1114 
1115 	if (copy_from_user(&bi, arg, sizeof(bi)))
1116 		return -EFAULT;
1117 
1118 	if (bi.subdevice >= dev->n_subdevices)
1119 		return -EINVAL;
1120 
1121 	s = &dev->subdevices[bi.subdevice];
1122 
1123 	async = s->async;
1124 
1125 	if (!async) {
1126 		dev_dbg(dev->class_dev,
1127 			"subdevice does not have async capability\n");
1128 		bi.buf_write_ptr = 0;
1129 		bi.buf_read_ptr = 0;
1130 		bi.buf_write_count = 0;
1131 		bi.buf_read_count = 0;
1132 		bi.bytes_read = 0;
1133 		bi.bytes_written = 0;
1134 		goto copyback;
1135 	}
1136 	if (!s->busy) {
1137 		bi.bytes_read = 0;
1138 		bi.bytes_written = 0;
1139 		goto copyback_position;
1140 	}
1141 	if (s->busy != file)
1142 		return -EACCES;
1143 
1144 	if (bi.bytes_read && !(async->cmd.flags & CMDF_WRITE)) {
1145 		bi.bytes_read = comedi_buf_read_alloc(s, bi.bytes_read);
1146 		comedi_buf_read_free(s, bi.bytes_read);
1147 
1148 		if (comedi_is_subdevice_idle(s) &&
1149 		    comedi_buf_read_n_available(s) == 0) {
1150 			do_become_nonbusy(dev, s);
1151 		}
1152 	}
1153 
1154 	if (bi.bytes_written && (async->cmd.flags & CMDF_WRITE)) {
1155 		bi.bytes_written =
1156 		    comedi_buf_write_alloc(s, bi.bytes_written);
1157 		comedi_buf_write_free(s, bi.bytes_written);
1158 	}
1159 
1160 copyback_position:
1161 	bi.buf_write_count = async->buf_write_count;
1162 	bi.buf_write_ptr = async->buf_write_ptr;
1163 	bi.buf_read_count = async->buf_read_count;
1164 	bi.buf_read_ptr = async->buf_read_ptr;
1165 
1166 copyback:
1167 	if (copy_to_user(arg, &bi, sizeof(bi)))
1168 		return -EFAULT;
1169 
1170 	return 0;
1171 }
1172 
check_insn_config_length(struct comedi_insn * insn,unsigned int * data)1173 static int check_insn_config_length(struct comedi_insn *insn,
1174 				    unsigned int *data)
1175 {
1176 	if (insn->n < 1)
1177 		return -EINVAL;
1178 
1179 	switch (data[0]) {
1180 	case INSN_CONFIG_DIO_OUTPUT:
1181 	case INSN_CONFIG_DIO_INPUT:
1182 	case INSN_CONFIG_DISARM:
1183 	case INSN_CONFIG_RESET:
1184 		if (insn->n == 1)
1185 			return 0;
1186 		break;
1187 	case INSN_CONFIG_ARM:
1188 	case INSN_CONFIG_DIO_QUERY:
1189 	case INSN_CONFIG_BLOCK_SIZE:
1190 	case INSN_CONFIG_FILTER:
1191 	case INSN_CONFIG_SERIAL_CLOCK:
1192 	case INSN_CONFIG_BIDIRECTIONAL_DATA:
1193 	case INSN_CONFIG_ALT_SOURCE:
1194 	case INSN_CONFIG_SET_COUNTER_MODE:
1195 	case INSN_CONFIG_8254_READ_STATUS:
1196 	case INSN_CONFIG_SET_ROUTING:
1197 	case INSN_CONFIG_GET_ROUTING:
1198 	case INSN_CONFIG_GET_PWM_STATUS:
1199 	case INSN_CONFIG_PWM_SET_PERIOD:
1200 	case INSN_CONFIG_PWM_GET_PERIOD:
1201 		if (insn->n == 2)
1202 			return 0;
1203 		break;
1204 	case INSN_CONFIG_SET_GATE_SRC:
1205 	case INSN_CONFIG_GET_GATE_SRC:
1206 	case INSN_CONFIG_SET_CLOCK_SRC:
1207 	case INSN_CONFIG_GET_CLOCK_SRC:
1208 	case INSN_CONFIG_SET_OTHER_SRC:
1209 	case INSN_CONFIG_GET_COUNTER_STATUS:
1210 	case INSN_CONFIG_PWM_SET_H_BRIDGE:
1211 	case INSN_CONFIG_PWM_GET_H_BRIDGE:
1212 	case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1213 		if (insn->n == 3)
1214 			return 0;
1215 		break;
1216 	case INSN_CONFIG_PWM_OUTPUT:
1217 	case INSN_CONFIG_ANALOG_TRIG:
1218 		if (insn->n == 5)
1219 			return 0;
1220 		break;
1221 	case INSN_CONFIG_DIGITAL_TRIG:
1222 		if (insn->n == 6)
1223 			return 0;
1224 		break;
1225 		/*
1226 		 * by default we allow the insn since we don't have checks for
1227 		 * all possible cases yet
1228 		 */
1229 	default:
1230 		pr_warn("No check for data length of config insn id %i is implemented\n",
1231 			data[0]);
1232 		pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1233 		pr_warn("Assuming n=%i is correct\n", insn->n);
1234 		return 0;
1235 	}
1236 	return -EINVAL;
1237 }
1238 
parse_insn(struct comedi_device * dev,struct comedi_insn * insn,unsigned int * data,void * file)1239 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1240 		      unsigned int *data, void *file)
1241 {
1242 	struct comedi_subdevice *s;
1243 	int ret = 0;
1244 	int i;
1245 
1246 	if (insn->insn & INSN_MASK_SPECIAL) {
1247 		/* a non-subdevice instruction */
1248 
1249 		switch (insn->insn) {
1250 		case INSN_GTOD:
1251 			{
1252 				struct timeval tv;
1253 
1254 				if (insn->n != 2) {
1255 					ret = -EINVAL;
1256 					break;
1257 				}
1258 
1259 				do_gettimeofday(&tv);
1260 				data[0] = tv.tv_sec;
1261 				data[1] = tv.tv_usec;
1262 				ret = 2;
1263 
1264 				break;
1265 			}
1266 		case INSN_WAIT:
1267 			if (insn->n != 1 || data[0] >= 100000) {
1268 				ret = -EINVAL;
1269 				break;
1270 			}
1271 			udelay(data[0] / 1000);
1272 			ret = 1;
1273 			break;
1274 		case INSN_INTTRIG:
1275 			if (insn->n != 1) {
1276 				ret = -EINVAL;
1277 				break;
1278 			}
1279 			if (insn->subdev >= dev->n_subdevices) {
1280 				dev_dbg(dev->class_dev,
1281 					"%d not usable subdevice\n",
1282 					insn->subdev);
1283 				ret = -EINVAL;
1284 				break;
1285 			}
1286 			s = &dev->subdevices[insn->subdev];
1287 			if (!s->async) {
1288 				dev_dbg(dev->class_dev, "no async\n");
1289 				ret = -EINVAL;
1290 				break;
1291 			}
1292 			if (!s->async->inttrig) {
1293 				dev_dbg(dev->class_dev, "no inttrig\n");
1294 				ret = -EAGAIN;
1295 				break;
1296 			}
1297 			ret = s->async->inttrig(dev, s, data[0]);
1298 			if (ret >= 0)
1299 				ret = 1;
1300 			break;
1301 		default:
1302 			dev_dbg(dev->class_dev, "invalid insn\n");
1303 			ret = -EINVAL;
1304 			break;
1305 		}
1306 	} else {
1307 		/* a subdevice instruction */
1308 		unsigned int maxdata;
1309 
1310 		if (insn->subdev >= dev->n_subdevices) {
1311 			dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1312 				insn->subdev);
1313 			ret = -EINVAL;
1314 			goto out;
1315 		}
1316 		s = &dev->subdevices[insn->subdev];
1317 
1318 		if (s->type == COMEDI_SUBD_UNUSED) {
1319 			dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1320 				insn->subdev);
1321 			ret = -EIO;
1322 			goto out;
1323 		}
1324 
1325 		/* are we locked? (ioctl lock) */
1326 		if (s->lock && s->lock != file) {
1327 			dev_dbg(dev->class_dev, "device locked\n");
1328 			ret = -EACCES;
1329 			goto out;
1330 		}
1331 
1332 		ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1333 		if (ret < 0) {
1334 			ret = -EINVAL;
1335 			dev_dbg(dev->class_dev, "bad chanspec\n");
1336 			goto out;
1337 		}
1338 
1339 		if (s->busy) {
1340 			ret = -EBUSY;
1341 			goto out;
1342 		}
1343 		/* This looks arbitrary.  It is. */
1344 		s->busy = parse_insn;
1345 		switch (insn->insn) {
1346 		case INSN_READ:
1347 			ret = s->insn_read(dev, s, insn, data);
1348 			if (ret == -ETIMEDOUT) {
1349 				dev_dbg(dev->class_dev,
1350 					"subdevice %d read instruction timed out\n",
1351 					s->index);
1352 			}
1353 			break;
1354 		case INSN_WRITE:
1355 			maxdata = s->maxdata_list
1356 			    ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1357 			    : s->maxdata;
1358 			for (i = 0; i < insn->n; ++i) {
1359 				if (data[i] > maxdata) {
1360 					ret = -EINVAL;
1361 					dev_dbg(dev->class_dev,
1362 						"bad data value(s)\n");
1363 					break;
1364 				}
1365 			}
1366 			if (ret == 0) {
1367 				ret = s->insn_write(dev, s, insn, data);
1368 				if (ret == -ETIMEDOUT) {
1369 					dev_dbg(dev->class_dev,
1370 						"subdevice %d write instruction timed out\n",
1371 						s->index);
1372 				}
1373 			}
1374 			break;
1375 		case INSN_BITS:
1376 			if (insn->n != 2) {
1377 				ret = -EINVAL;
1378 			} else {
1379 				/*
1380 				 * Most drivers ignore the base channel in
1381 				 * insn->chanspec.  Fix this here if
1382 				 * the subdevice has <= 32 channels.
1383 				 */
1384 				unsigned int orig_mask = data[0];
1385 				unsigned int shift = 0;
1386 
1387 				if (s->n_chan <= 32) {
1388 					shift = CR_CHAN(insn->chanspec);
1389 					if (shift > 0) {
1390 						insn->chanspec = 0;
1391 						data[0] <<= shift;
1392 						data[1] <<= shift;
1393 					}
1394 				}
1395 				ret = s->insn_bits(dev, s, insn, data);
1396 				data[0] = orig_mask;
1397 				if (shift > 0)
1398 					data[1] >>= shift;
1399 			}
1400 			break;
1401 		case INSN_CONFIG:
1402 			ret = check_insn_config_length(insn, data);
1403 			if (ret)
1404 				break;
1405 			ret = s->insn_config(dev, s, insn, data);
1406 			break;
1407 		default:
1408 			ret = -EINVAL;
1409 			break;
1410 		}
1411 
1412 		s->busy = NULL;
1413 	}
1414 
1415 out:
1416 	return ret;
1417 }
1418 
1419 /*
1420  * COMEDI_INSNLIST ioctl
1421  * synchronous instruction list
1422  *
1423  * arg:
1424  *	pointer to comedi_insnlist structure
1425  *
1426  * reads:
1427  *	comedi_insnlist structure
1428  *	array of comedi_insn structures from insnlist->insns pointer
1429  *	data (for writes) from insns[].data pointers
1430  *
1431  * writes:
1432  *	data (for reads) to insns[].data pointers
1433  */
1434 /* arbitrary limits */
1435 #define MAX_SAMPLES 256
do_insnlist_ioctl(struct comedi_device * dev,struct comedi_insnlist __user * arg,void * file)1436 static int do_insnlist_ioctl(struct comedi_device *dev,
1437 			     struct comedi_insnlist __user *arg, void *file)
1438 {
1439 	struct comedi_insnlist insnlist;
1440 	struct comedi_insn *insns = NULL;
1441 	unsigned int *data = NULL;
1442 	int i = 0;
1443 	int ret = 0;
1444 
1445 	if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1446 		return -EFAULT;
1447 
1448 	data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1449 	if (!data) {
1450 		ret = -ENOMEM;
1451 		goto error;
1452 	}
1453 
1454 	insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1455 	if (!insns) {
1456 		ret = -ENOMEM;
1457 		goto error;
1458 	}
1459 
1460 	if (copy_from_user(insns, insnlist.insns,
1461 			   sizeof(*insns) * insnlist.n_insns)) {
1462 		dev_dbg(dev->class_dev, "copy_from_user failed\n");
1463 		ret = -EFAULT;
1464 		goto error;
1465 	}
1466 
1467 	for (i = 0; i < insnlist.n_insns; i++) {
1468 		if (insns[i].n > MAX_SAMPLES) {
1469 			dev_dbg(dev->class_dev,
1470 				"number of samples too large\n");
1471 			ret = -EINVAL;
1472 			goto error;
1473 		}
1474 		if (insns[i].insn & INSN_MASK_WRITE) {
1475 			if (copy_from_user(data, insns[i].data,
1476 					   insns[i].n * sizeof(unsigned int))) {
1477 				dev_dbg(dev->class_dev,
1478 					"copy_from_user failed\n");
1479 				ret = -EFAULT;
1480 				goto error;
1481 			}
1482 		}
1483 		ret = parse_insn(dev, insns + i, data, file);
1484 		if (ret < 0)
1485 			goto error;
1486 		if (insns[i].insn & INSN_MASK_READ) {
1487 			if (copy_to_user(insns[i].data, data,
1488 					 insns[i].n * sizeof(unsigned int))) {
1489 				dev_dbg(dev->class_dev,
1490 					"copy_to_user failed\n");
1491 				ret = -EFAULT;
1492 				goto error;
1493 			}
1494 		}
1495 		if (need_resched())
1496 			schedule();
1497 	}
1498 
1499 error:
1500 	kfree(insns);
1501 	kfree(data);
1502 
1503 	if (ret < 0)
1504 		return ret;
1505 	return i;
1506 }
1507 
1508 /*
1509  * COMEDI_INSN ioctl
1510  * synchronous instruction
1511  *
1512  * arg:
1513  *	pointer to comedi_insn structure
1514  *
1515  * reads:
1516  *	comedi_insn structure
1517  *	data (for writes) from insn->data pointer
1518  *
1519  * writes:
1520  *	data (for reads) to insn->data pointer
1521  */
do_insn_ioctl(struct comedi_device * dev,struct comedi_insn __user * arg,void * file)1522 static int do_insn_ioctl(struct comedi_device *dev,
1523 			 struct comedi_insn __user *arg, void *file)
1524 {
1525 	struct comedi_insn insn;
1526 	unsigned int *data = NULL;
1527 	int ret = 0;
1528 
1529 	data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1530 	if (!data) {
1531 		ret = -ENOMEM;
1532 		goto error;
1533 	}
1534 
1535 	if (copy_from_user(&insn, arg, sizeof(insn))) {
1536 		ret = -EFAULT;
1537 		goto error;
1538 	}
1539 
1540 	/* This is where the behavior of insn and insnlist deviate. */
1541 	if (insn.n > MAX_SAMPLES)
1542 		insn.n = MAX_SAMPLES;
1543 	if (insn.insn & INSN_MASK_WRITE) {
1544 		if (copy_from_user(data,
1545 				   insn.data,
1546 				   insn.n * sizeof(unsigned int))) {
1547 			ret = -EFAULT;
1548 			goto error;
1549 		}
1550 	}
1551 	ret = parse_insn(dev, &insn, data, file);
1552 	if (ret < 0)
1553 		goto error;
1554 	if (insn.insn & INSN_MASK_READ) {
1555 		if (copy_to_user(insn.data,
1556 				 data,
1557 				 insn.n * sizeof(unsigned int))) {
1558 			ret = -EFAULT;
1559 			goto error;
1560 		}
1561 	}
1562 	ret = insn.n;
1563 
1564 error:
1565 	kfree(data);
1566 
1567 	return ret;
1568 }
1569 
__comedi_get_user_cmd(struct comedi_device * dev,struct comedi_cmd __user * arg,struct comedi_cmd * cmd)1570 static int __comedi_get_user_cmd(struct comedi_device *dev,
1571 				 struct comedi_cmd __user *arg,
1572 				 struct comedi_cmd *cmd)
1573 {
1574 	struct comedi_subdevice *s;
1575 
1576 	if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1577 		dev_dbg(dev->class_dev, "bad cmd address\n");
1578 		return -EFAULT;
1579 	}
1580 
1581 	if (cmd->subdev >= dev->n_subdevices) {
1582 		dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1583 		return -ENODEV;
1584 	}
1585 
1586 	s = &dev->subdevices[cmd->subdev];
1587 
1588 	if (s->type == COMEDI_SUBD_UNUSED) {
1589 		dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1590 			cmd->subdev);
1591 		return -EIO;
1592 	}
1593 
1594 	if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1595 		dev_dbg(dev->class_dev,
1596 			"subdevice %d does not support commands\n",
1597 			cmd->subdev);
1598 		return -EIO;
1599 	}
1600 
1601 	/* make sure channel/gain list isn't too long */
1602 	if (cmd->chanlist_len > s->len_chanlist) {
1603 		dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1604 			cmd->chanlist_len, s->len_chanlist);
1605 		return -EINVAL;
1606 	}
1607 
1608 	/*
1609 	 * Set the CMDF_WRITE flag to the correct state if the subdevice
1610 	 * supports only "read" commands or only "write" commands.
1611 	 */
1612 	switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1613 	case SDF_CMD_READ:
1614 		cmd->flags &= ~CMDF_WRITE;
1615 		break;
1616 	case SDF_CMD_WRITE:
1617 		cmd->flags |= CMDF_WRITE;
1618 		break;
1619 	default:
1620 		break;
1621 	}
1622 
1623 	return 0;
1624 }
1625 
__comedi_get_user_chanlist(struct comedi_device * dev,struct comedi_subdevice * s,unsigned int __user * user_chanlist,struct comedi_cmd * cmd)1626 static int __comedi_get_user_chanlist(struct comedi_device *dev,
1627 				      struct comedi_subdevice *s,
1628 				      unsigned int __user *user_chanlist,
1629 				      struct comedi_cmd *cmd)
1630 {
1631 	unsigned int *chanlist;
1632 	int ret;
1633 
1634 	cmd->chanlist = NULL;
1635 	chanlist = memdup_user(user_chanlist,
1636 			       cmd->chanlist_len * sizeof(unsigned int));
1637 	if (IS_ERR(chanlist))
1638 		return PTR_ERR(chanlist);
1639 
1640 	/* make sure each element in channel/gain list is valid */
1641 	ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1642 	if (ret < 0) {
1643 		kfree(chanlist);
1644 		return ret;
1645 	}
1646 
1647 	cmd->chanlist = chanlist;
1648 
1649 	return 0;
1650 }
1651 
1652 /*
1653  * COMEDI_CMD ioctl
1654  * asynchronous acquisition command set-up
1655  *
1656  * arg:
1657  *	pointer to comedi_cmd structure
1658  *
1659  * reads:
1660  *	comedi_cmd structure
1661  *	channel/range list from cmd->chanlist pointer
1662  *
1663  * writes:
1664  *	possibly modified comedi_cmd structure (when -EAGAIN returned)
1665  */
do_cmd_ioctl(struct comedi_device * dev,struct comedi_cmd __user * arg,void * file)1666 static int do_cmd_ioctl(struct comedi_device *dev,
1667 			struct comedi_cmd __user *arg, void *file)
1668 {
1669 	struct comedi_cmd cmd;
1670 	struct comedi_subdevice *s;
1671 	struct comedi_async *async;
1672 	unsigned int __user *user_chanlist;
1673 	int ret;
1674 
1675 	/* get the user's cmd and do some simple validation */
1676 	ret = __comedi_get_user_cmd(dev, arg, &cmd);
1677 	if (ret)
1678 		return ret;
1679 
1680 	/* save user's chanlist pointer so it can be restored later */
1681 	user_chanlist = (unsigned int __user *)cmd.chanlist;
1682 
1683 	s = &dev->subdevices[cmd.subdev];
1684 	async = s->async;
1685 
1686 	/* are we locked? (ioctl lock) */
1687 	if (s->lock && s->lock != file) {
1688 		dev_dbg(dev->class_dev, "subdevice locked\n");
1689 		return -EACCES;
1690 	}
1691 
1692 	/* are we busy? */
1693 	if (s->busy) {
1694 		dev_dbg(dev->class_dev, "subdevice busy\n");
1695 		return -EBUSY;
1696 	}
1697 
1698 	/* make sure channel/gain list isn't too short */
1699 	if (cmd.chanlist_len < 1) {
1700 		dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
1701 			cmd.chanlist_len);
1702 		return -EINVAL;
1703 	}
1704 
1705 	async->cmd = cmd;
1706 	async->cmd.data = NULL;
1707 
1708 	/* load channel/gain list */
1709 	ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1710 	if (ret)
1711 		goto cleanup;
1712 
1713 	ret = s->do_cmdtest(dev, s, &async->cmd);
1714 
1715 	if (async->cmd.flags & CMDF_BOGUS || ret) {
1716 		dev_dbg(dev->class_dev, "test returned %d\n", ret);
1717 		cmd = async->cmd;
1718 		/* restore chanlist pointer before copying back */
1719 		cmd.chanlist = (unsigned int __force *)user_chanlist;
1720 		cmd.data = NULL;
1721 		if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1722 			dev_dbg(dev->class_dev, "fault writing cmd\n");
1723 			ret = -EFAULT;
1724 			goto cleanup;
1725 		}
1726 		ret = -EAGAIN;
1727 		goto cleanup;
1728 	}
1729 
1730 	if (!async->prealloc_bufsz) {
1731 		ret = -ENOMEM;
1732 		dev_dbg(dev->class_dev, "no buffer (?)\n");
1733 		goto cleanup;
1734 	}
1735 
1736 	comedi_buf_reset(s);
1737 
1738 	async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
1739 	if (async->cmd.flags & CMDF_WAKE_EOS)
1740 		async->cb_mask |= COMEDI_CB_EOS;
1741 
1742 	comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1743 					 COMEDI_SRF_RUNNING);
1744 
1745 	/*
1746 	 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1747 	 * race with comedi_read() or comedi_write().
1748 	 */
1749 	s->busy = file;
1750 	ret = s->do_cmd(dev, s);
1751 	if (ret == 0)
1752 		return 0;
1753 
1754 cleanup:
1755 	do_become_nonbusy(dev, s);
1756 
1757 	return ret;
1758 }
1759 
1760 /*
1761  * COMEDI_CMDTEST ioctl
1762  * asynchronous acquisition command testing
1763  *
1764  * arg:
1765  *	pointer to comedi_cmd structure
1766  *
1767  * reads:
1768  *	comedi_cmd structure
1769  *	channel/range list from cmd->chanlist pointer
1770  *
1771  * writes:
1772  *	possibly modified comedi_cmd structure
1773  */
do_cmdtest_ioctl(struct comedi_device * dev,struct comedi_cmd __user * arg,void * file)1774 static int do_cmdtest_ioctl(struct comedi_device *dev,
1775 			    struct comedi_cmd __user *arg, void *file)
1776 {
1777 	struct comedi_cmd cmd;
1778 	struct comedi_subdevice *s;
1779 	unsigned int __user *user_chanlist;
1780 	int ret;
1781 
1782 	/* get the user's cmd and do some simple validation */
1783 	ret = __comedi_get_user_cmd(dev, arg, &cmd);
1784 	if (ret)
1785 		return ret;
1786 
1787 	/* save user's chanlist pointer so it can be restored later */
1788 	user_chanlist = (unsigned int __user *)cmd.chanlist;
1789 
1790 	s = &dev->subdevices[cmd.subdev];
1791 
1792 	/* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1793 	if (user_chanlist) {
1794 		/* load channel/gain list */
1795 		ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1796 		if (ret)
1797 			return ret;
1798 	}
1799 
1800 	ret = s->do_cmdtest(dev, s, &cmd);
1801 
1802 	kfree(cmd.chanlist);	/* free kernel copy of user chanlist */
1803 
1804 	/* restore chanlist pointer before copying back */
1805 	cmd.chanlist = (unsigned int __force *)user_chanlist;
1806 
1807 	if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1808 		dev_dbg(dev->class_dev, "bad cmd address\n");
1809 		ret = -EFAULT;
1810 	}
1811 
1812 	return ret;
1813 }
1814 
1815 /*
1816  * COMEDI_LOCK ioctl
1817  * lock subdevice
1818  *
1819  * arg:
1820  *	subdevice number
1821  *
1822  * reads:
1823  *	nothing
1824  *
1825  * writes:
1826  *	nothing
1827  */
do_lock_ioctl(struct comedi_device * dev,unsigned long arg,void * file)1828 static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
1829 			 void *file)
1830 {
1831 	int ret = 0;
1832 	unsigned long flags;
1833 	struct comedi_subdevice *s;
1834 
1835 	if (arg >= dev->n_subdevices)
1836 		return -EINVAL;
1837 	s = &dev->subdevices[arg];
1838 
1839 	spin_lock_irqsave(&s->spin_lock, flags);
1840 	if (s->busy || s->lock)
1841 		ret = -EBUSY;
1842 	else
1843 		s->lock = file;
1844 	spin_unlock_irqrestore(&s->spin_lock, flags);
1845 
1846 	return ret;
1847 }
1848 
1849 /*
1850  * COMEDI_UNLOCK ioctl
1851  * unlock subdevice
1852  *
1853  * arg:
1854  *	subdevice number
1855  *
1856  * reads:
1857  *	nothing
1858  *
1859  * writes:
1860  *	nothing
1861  */
do_unlock_ioctl(struct comedi_device * dev,unsigned long arg,void * file)1862 static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
1863 			   void *file)
1864 {
1865 	struct comedi_subdevice *s;
1866 
1867 	if (arg >= dev->n_subdevices)
1868 		return -EINVAL;
1869 	s = &dev->subdevices[arg];
1870 
1871 	if (s->busy)
1872 		return -EBUSY;
1873 
1874 	if (s->lock && s->lock != file)
1875 		return -EACCES;
1876 
1877 	if (s->lock == file)
1878 		s->lock = NULL;
1879 
1880 	return 0;
1881 }
1882 
1883 /*
1884  * COMEDI_CANCEL ioctl
1885  * cancel asynchronous acquisition
1886  *
1887  * arg:
1888  *	subdevice number
1889  *
1890  * reads:
1891  *	nothing
1892  *
1893  * writes:
1894  *	nothing
1895  */
do_cancel_ioctl(struct comedi_device * dev,unsigned long arg,void * file)1896 static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
1897 			   void *file)
1898 {
1899 	struct comedi_subdevice *s;
1900 
1901 	if (arg >= dev->n_subdevices)
1902 		return -EINVAL;
1903 	s = &dev->subdevices[arg];
1904 	if (!s->async)
1905 		return -EINVAL;
1906 
1907 	if (!s->busy)
1908 		return 0;
1909 
1910 	if (s->busy != file)
1911 		return -EBUSY;
1912 
1913 	return do_cancel(dev, s);
1914 }
1915 
1916 /*
1917  * COMEDI_POLL ioctl
1918  * instructs driver to synchronize buffers
1919  *
1920  * arg:
1921  *	subdevice number
1922  *
1923  * reads:
1924  *	nothing
1925  *
1926  * writes:
1927  *	nothing
1928  */
do_poll_ioctl(struct comedi_device * dev,unsigned long arg,void * file)1929 static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
1930 			 void *file)
1931 {
1932 	struct comedi_subdevice *s;
1933 
1934 	if (arg >= dev->n_subdevices)
1935 		return -EINVAL;
1936 	s = &dev->subdevices[arg];
1937 
1938 	if (!s->busy)
1939 		return 0;
1940 
1941 	if (s->busy != file)
1942 		return -EBUSY;
1943 
1944 	if (s->poll)
1945 		return s->poll(dev, s);
1946 
1947 	return -EINVAL;
1948 }
1949 
1950 /*
1951  * COMEDI_SETRSUBD ioctl
1952  * sets the current "read" subdevice on a per-file basis
1953  *
1954  * arg:
1955  *	subdevice number
1956  *
1957  * reads:
1958  *	nothing
1959  *
1960  * writes:
1961  *	nothing
1962  */
do_setrsubd_ioctl(struct comedi_device * dev,unsigned long arg,struct file * file)1963 static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1964 			     struct file *file)
1965 {
1966 	struct comedi_file *cfp = file->private_data;
1967 	struct comedi_subdevice *s_old, *s_new;
1968 
1969 	if (arg >= dev->n_subdevices)
1970 		return -EINVAL;
1971 
1972 	s_new = &dev->subdevices[arg];
1973 	s_old = comedi_file_read_subdevice(file);
1974 	if (s_old == s_new)
1975 		return 0;	/* no change */
1976 
1977 	if (!(s_new->subdev_flags & SDF_CMD_READ))
1978 		return -EINVAL;
1979 
1980 	/*
1981 	 * Check the file isn't still busy handling a "read" command on the
1982 	 * old subdevice (if any).
1983 	 */
1984 	if (s_old && s_old->busy == file && s_old->async &&
1985 	    !(s_old->async->cmd.flags & CMDF_WRITE))
1986 		return -EBUSY;
1987 
1988 	ACCESS_ONCE(cfp->read_subdev) = s_new;
1989 	return 0;
1990 }
1991 
1992 /*
1993  * COMEDI_SETWSUBD ioctl
1994  * sets the current "write" subdevice on a per-file basis
1995  *
1996  * arg:
1997  *	subdevice number
1998  *
1999  * reads:
2000  *	nothing
2001  *
2002  * writes:
2003  *	nothing
2004  */
do_setwsubd_ioctl(struct comedi_device * dev,unsigned long arg,struct file * file)2005 static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
2006 			     struct file *file)
2007 {
2008 	struct comedi_file *cfp = file->private_data;
2009 	struct comedi_subdevice *s_old, *s_new;
2010 
2011 	if (arg >= dev->n_subdevices)
2012 		return -EINVAL;
2013 
2014 	s_new = &dev->subdevices[arg];
2015 	s_old = comedi_file_write_subdevice(file);
2016 	if (s_old == s_new)
2017 		return 0;	/* no change */
2018 
2019 	if (!(s_new->subdev_flags & SDF_CMD_WRITE))
2020 		return -EINVAL;
2021 
2022 	/*
2023 	 * Check the file isn't still busy handling a "write" command on the
2024 	 * old subdevice (if any).
2025 	 */
2026 	if (s_old && s_old->busy == file && s_old->async &&
2027 	    (s_old->async->cmd.flags & CMDF_WRITE))
2028 		return -EBUSY;
2029 
2030 	ACCESS_ONCE(cfp->write_subdev) = s_new;
2031 	return 0;
2032 }
2033 
comedi_unlocked_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2034 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2035 				  unsigned long arg)
2036 {
2037 	unsigned minor = iminor(file_inode(file));
2038 	struct comedi_file *cfp = file->private_data;
2039 	struct comedi_device *dev = cfp->dev;
2040 	int rc;
2041 
2042 	mutex_lock(&dev->mutex);
2043 
2044 	/*
2045 	 * Device config is special, because it must work on
2046 	 * an unconfigured device.
2047 	 */
2048 	if (cmd == COMEDI_DEVCONFIG) {
2049 		if (minor >= COMEDI_NUM_BOARD_MINORS) {
2050 			/* Device config not appropriate on non-board minors. */
2051 			rc = -ENOTTY;
2052 			goto done;
2053 		}
2054 		rc = do_devconfig_ioctl(dev,
2055 					(struct comedi_devconfig __user *)arg);
2056 		if (rc == 0) {
2057 			if (arg == 0 &&
2058 			    dev->minor >= comedi_num_legacy_minors) {
2059 				/*
2060 				 * Successfully unconfigured a dynamically
2061 				 * allocated device.  Try and remove it.
2062 				 */
2063 				if (comedi_clear_board_dev(dev)) {
2064 					mutex_unlock(&dev->mutex);
2065 					comedi_free_board_dev(dev);
2066 					return rc;
2067 				}
2068 			}
2069 		}
2070 		goto done;
2071 	}
2072 
2073 	if (!dev->attached) {
2074 		dev_dbg(dev->class_dev, "no driver attached\n");
2075 		rc = -ENODEV;
2076 		goto done;
2077 	}
2078 
2079 	switch (cmd) {
2080 	case COMEDI_BUFCONFIG:
2081 		rc = do_bufconfig_ioctl(dev,
2082 					(struct comedi_bufconfig __user *)arg);
2083 		break;
2084 	case COMEDI_DEVINFO:
2085 		rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2086 				      file);
2087 		break;
2088 	case COMEDI_SUBDINFO:
2089 		rc = do_subdinfo_ioctl(dev,
2090 				       (struct comedi_subdinfo __user *)arg,
2091 				       file);
2092 		break;
2093 	case COMEDI_CHANINFO:
2094 		rc = do_chaninfo_ioctl(dev, (void __user *)arg);
2095 		break;
2096 	case COMEDI_RANGEINFO:
2097 		rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2098 		break;
2099 	case COMEDI_BUFINFO:
2100 		rc = do_bufinfo_ioctl(dev,
2101 				      (struct comedi_bufinfo __user *)arg,
2102 				      file);
2103 		break;
2104 	case COMEDI_LOCK:
2105 		rc = do_lock_ioctl(dev, arg, file);
2106 		break;
2107 	case COMEDI_UNLOCK:
2108 		rc = do_unlock_ioctl(dev, arg, file);
2109 		break;
2110 	case COMEDI_CANCEL:
2111 		rc = do_cancel_ioctl(dev, arg, file);
2112 		break;
2113 	case COMEDI_CMD:
2114 		rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2115 		break;
2116 	case COMEDI_CMDTEST:
2117 		rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2118 				      file);
2119 		break;
2120 	case COMEDI_INSNLIST:
2121 		rc = do_insnlist_ioctl(dev,
2122 				       (struct comedi_insnlist __user *)arg,
2123 				       file);
2124 		break;
2125 	case COMEDI_INSN:
2126 		rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2127 				   file);
2128 		break;
2129 	case COMEDI_POLL:
2130 		rc = do_poll_ioctl(dev, arg, file);
2131 		break;
2132 	case COMEDI_SETRSUBD:
2133 		rc = do_setrsubd_ioctl(dev, arg, file);
2134 		break;
2135 	case COMEDI_SETWSUBD:
2136 		rc = do_setwsubd_ioctl(dev, arg, file);
2137 		break;
2138 	default:
2139 		rc = -ENOTTY;
2140 		break;
2141 	}
2142 
2143 done:
2144 	mutex_unlock(&dev->mutex);
2145 	return rc;
2146 }
2147 
comedi_vm_open(struct vm_area_struct * area)2148 static void comedi_vm_open(struct vm_area_struct *area)
2149 {
2150 	struct comedi_buf_map *bm;
2151 
2152 	bm = area->vm_private_data;
2153 	comedi_buf_map_get(bm);
2154 }
2155 
comedi_vm_close(struct vm_area_struct * area)2156 static void comedi_vm_close(struct vm_area_struct *area)
2157 {
2158 	struct comedi_buf_map *bm;
2159 
2160 	bm = area->vm_private_data;
2161 	comedi_buf_map_put(bm);
2162 }
2163 
2164 static const struct vm_operations_struct comedi_vm_ops = {
2165 	.open = comedi_vm_open,
2166 	.close = comedi_vm_close,
2167 };
2168 
comedi_mmap(struct file * file,struct vm_area_struct * vma)2169 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2170 {
2171 	struct comedi_file *cfp = file->private_data;
2172 	struct comedi_device *dev = cfp->dev;
2173 	struct comedi_subdevice *s;
2174 	struct comedi_async *async;
2175 	struct comedi_buf_map *bm = NULL;
2176 	unsigned long start = vma->vm_start;
2177 	unsigned long size;
2178 	int n_pages;
2179 	int i;
2180 	int retval;
2181 
2182 	/*
2183 	 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2184 	 * and down-reading &dev->attach_lock should normally succeed without
2185 	 * contention unless the device is in the process of being attached
2186 	 * or detached.
2187 	 */
2188 	if (!down_read_trylock(&dev->attach_lock))
2189 		return -EAGAIN;
2190 
2191 	if (!dev->attached) {
2192 		dev_dbg(dev->class_dev, "no driver attached\n");
2193 		retval = -ENODEV;
2194 		goto done;
2195 	}
2196 
2197 	if (vma->vm_flags & VM_WRITE)
2198 		s = comedi_file_write_subdevice(file);
2199 	else
2200 		s = comedi_file_read_subdevice(file);
2201 	if (!s) {
2202 		retval = -EINVAL;
2203 		goto done;
2204 	}
2205 
2206 	async = s->async;
2207 	if (!async) {
2208 		retval = -EINVAL;
2209 		goto done;
2210 	}
2211 
2212 	if (vma->vm_pgoff != 0) {
2213 		dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
2214 		retval = -EINVAL;
2215 		goto done;
2216 	}
2217 
2218 	size = vma->vm_end - vma->vm_start;
2219 	if (size > async->prealloc_bufsz) {
2220 		retval = -EFAULT;
2221 		goto done;
2222 	}
2223 	if (size & (~PAGE_MASK)) {
2224 		retval = -EFAULT;
2225 		goto done;
2226 	}
2227 
2228 	n_pages = size >> PAGE_SHIFT;
2229 
2230 	/* get reference to current buf map (if any) */
2231 	bm = comedi_buf_map_from_subdev_get(s);
2232 	if (!bm || n_pages > bm->n_pages) {
2233 		retval = -EINVAL;
2234 		goto done;
2235 	}
2236 	for (i = 0; i < n_pages; ++i) {
2237 		struct comedi_buf_page *buf = &bm->page_list[i];
2238 
2239 		if (remap_pfn_range(vma, start,
2240 				    page_to_pfn(virt_to_page(buf->virt_addr)),
2241 				    PAGE_SIZE, PAGE_SHARED)) {
2242 			retval = -EAGAIN;
2243 			goto done;
2244 		}
2245 		start += PAGE_SIZE;
2246 	}
2247 
2248 	vma->vm_ops = &comedi_vm_ops;
2249 	vma->vm_private_data = bm;
2250 
2251 	vma->vm_ops->open(vma);
2252 
2253 	retval = 0;
2254 done:
2255 	up_read(&dev->attach_lock);
2256 	comedi_buf_map_put(bm);	/* put reference to buf map - okay if NULL */
2257 	return retval;
2258 }
2259 
comedi_poll(struct file * file,poll_table * wait)2260 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2261 {
2262 	unsigned int mask = 0;
2263 	struct comedi_file *cfp = file->private_data;
2264 	struct comedi_device *dev = cfp->dev;
2265 	struct comedi_subdevice *s, *s_read;
2266 
2267 	down_read(&dev->attach_lock);
2268 
2269 	if (!dev->attached) {
2270 		dev_dbg(dev->class_dev, "no driver attached\n");
2271 		goto done;
2272 	}
2273 
2274 	s = comedi_file_read_subdevice(file);
2275 	s_read = s;
2276 	if (s && s->async) {
2277 		poll_wait(file, &s->async->wait_head, wait);
2278 		if (s->busy != file || !comedi_is_subdevice_running(s) ||
2279 		    (s->async->cmd.flags & CMDF_WRITE) ||
2280 		    comedi_buf_read_n_available(s) > 0)
2281 			mask |= POLLIN | POLLRDNORM;
2282 	}
2283 
2284 	s = comedi_file_write_subdevice(file);
2285 	if (s && s->async) {
2286 		unsigned int bps = comedi_bytes_per_sample(s);
2287 
2288 		if (s != s_read)
2289 			poll_wait(file, &s->async->wait_head, wait);
2290 		if (s->busy != file || !comedi_is_subdevice_running(s) ||
2291 		    !(s->async->cmd.flags & CMDF_WRITE) ||
2292 		    comedi_buf_write_n_available(s) >= bps)
2293 			mask |= POLLOUT | POLLWRNORM;
2294 	}
2295 
2296 done:
2297 	up_read(&dev->attach_lock);
2298 	return mask;
2299 }
2300 
comedi_write(struct file * file,const char __user * buf,size_t nbytes,loff_t * offset)2301 static ssize_t comedi_write(struct file *file, const char __user *buf,
2302 			    size_t nbytes, loff_t *offset)
2303 {
2304 	struct comedi_subdevice *s;
2305 	struct comedi_async *async;
2306 	int n, m, count = 0, retval = 0;
2307 	DECLARE_WAITQUEUE(wait, current);
2308 	struct comedi_file *cfp = file->private_data;
2309 	struct comedi_device *dev = cfp->dev;
2310 	bool on_wait_queue = false;
2311 	bool attach_locked;
2312 	unsigned int old_detach_count;
2313 
2314 	/* Protect against device detachment during operation. */
2315 	down_read(&dev->attach_lock);
2316 	attach_locked = true;
2317 	old_detach_count = dev->detach_count;
2318 
2319 	if (!dev->attached) {
2320 		dev_dbg(dev->class_dev, "no driver attached\n");
2321 		retval = -ENODEV;
2322 		goto out;
2323 	}
2324 
2325 	s = comedi_file_write_subdevice(file);
2326 	if (!s || !s->async) {
2327 		retval = -EIO;
2328 		goto out;
2329 	}
2330 
2331 	async = s->async;
2332 
2333 	if (!s->busy || !nbytes)
2334 		goto out;
2335 	if (s->busy != file) {
2336 		retval = -EACCES;
2337 		goto out;
2338 	}
2339 	if (!(async->cmd.flags & CMDF_WRITE)) {
2340 		retval = -EINVAL;
2341 		goto out;
2342 	}
2343 
2344 	add_wait_queue(&async->wait_head, &wait);
2345 	on_wait_queue = true;
2346 	while (nbytes > 0 && !retval) {
2347 		unsigned runflags;
2348 
2349 		set_current_state(TASK_INTERRUPTIBLE);
2350 
2351 		runflags = comedi_get_subdevice_runflags(s);
2352 		if (!comedi_is_runflags_running(runflags)) {
2353 			if (count == 0) {
2354 				struct comedi_subdevice *new_s;
2355 
2356 				if (comedi_is_runflags_in_error(runflags))
2357 					retval = -EPIPE;
2358 				else
2359 					retval = 0;
2360 				/*
2361 				 * To avoid deadlock, cannot acquire dev->mutex
2362 				 * while dev->attach_lock is held.  Need to
2363 				 * remove task from the async wait queue before
2364 				 * releasing dev->attach_lock, as it might not
2365 				 * be valid afterwards.
2366 				 */
2367 				remove_wait_queue(&async->wait_head, &wait);
2368 				on_wait_queue = false;
2369 				up_read(&dev->attach_lock);
2370 				attach_locked = false;
2371 				mutex_lock(&dev->mutex);
2372 				/*
2373 				 * Become non-busy unless things have changed
2374 				 * behind our back.  Checking dev->detach_count
2375 				 * is unchanged ought to be sufficient (unless
2376 				 * there have been 2**32 detaches in the
2377 				 * meantime!), but check the subdevice pointer
2378 				 * as well just in case.
2379 				 */
2380 				new_s = comedi_file_write_subdevice(file);
2381 				if (dev->attached &&
2382 				    old_detach_count == dev->detach_count &&
2383 				    s == new_s && new_s->async == async)
2384 					do_become_nonbusy(dev, s);
2385 				mutex_unlock(&dev->mutex);
2386 			}
2387 			break;
2388 		}
2389 
2390 		n = nbytes;
2391 
2392 		m = n;
2393 		if (async->buf_write_ptr + m > async->prealloc_bufsz)
2394 			m = async->prealloc_bufsz - async->buf_write_ptr;
2395 		comedi_buf_write_alloc(s, async->prealloc_bufsz);
2396 		if (m > comedi_buf_write_n_allocated(s))
2397 			m = comedi_buf_write_n_allocated(s);
2398 		if (m < n)
2399 			n = m;
2400 
2401 		if (n == 0) {
2402 			if (file->f_flags & O_NONBLOCK) {
2403 				retval = -EAGAIN;
2404 				break;
2405 			}
2406 			schedule();
2407 			if (signal_pending(current)) {
2408 				retval = -ERESTARTSYS;
2409 				break;
2410 			}
2411 			if (!s->busy)
2412 				break;
2413 			if (s->busy != file) {
2414 				retval = -EACCES;
2415 				break;
2416 			}
2417 			if (!(async->cmd.flags & CMDF_WRITE)) {
2418 				retval = -EINVAL;
2419 				break;
2420 			}
2421 			continue;
2422 		}
2423 
2424 		m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2425 				   buf, n);
2426 		if (m) {
2427 			n -= m;
2428 			retval = -EFAULT;
2429 		}
2430 		comedi_buf_write_free(s, n);
2431 
2432 		count += n;
2433 		nbytes -= n;
2434 
2435 		buf += n;
2436 		break;		/* makes device work like a pipe */
2437 	}
2438 out:
2439 	if (on_wait_queue)
2440 		remove_wait_queue(&async->wait_head, &wait);
2441 	set_current_state(TASK_RUNNING);
2442 	if (attach_locked)
2443 		up_read(&dev->attach_lock);
2444 
2445 	return count ? count : retval;
2446 }
2447 
comedi_read(struct file * file,char __user * buf,size_t nbytes,loff_t * offset)2448 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2449 			   loff_t *offset)
2450 {
2451 	struct comedi_subdevice *s;
2452 	struct comedi_async *async;
2453 	unsigned int n, m;
2454 	ssize_t count = 0;
2455 	int retval = 0;
2456 	DECLARE_WAITQUEUE(wait, current);
2457 	struct comedi_file *cfp = file->private_data;
2458 	struct comedi_device *dev = cfp->dev;
2459 	unsigned int old_detach_count;
2460 	bool become_nonbusy = false;
2461 	bool attach_locked;
2462 
2463 	/* Protect against device detachment during operation. */
2464 	down_read(&dev->attach_lock);
2465 	attach_locked = true;
2466 	old_detach_count = dev->detach_count;
2467 
2468 	if (!dev->attached) {
2469 		dev_dbg(dev->class_dev, "no driver attached\n");
2470 		retval = -ENODEV;
2471 		goto out;
2472 	}
2473 
2474 	s = comedi_file_read_subdevice(file);
2475 	if (!s || !s->async) {
2476 		retval = -EIO;
2477 		goto out;
2478 	}
2479 
2480 	async = s->async;
2481 	if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
2482 		retval = -EINVAL;
2483 		goto out;
2484 	}
2485 
2486 	add_wait_queue(&async->wait_head, &wait);
2487 	while (count == 0 && !retval) {
2488 		unsigned int rp, n1, n2;
2489 
2490 		set_current_state(TASK_INTERRUPTIBLE);
2491 
2492 		m = comedi_buf_read_n_available(s);
2493 		n = min_t(size_t, m, nbytes);
2494 
2495 		if (n == 0) {
2496 			unsigned runflags = comedi_get_subdevice_runflags(s);
2497 
2498 			if (!comedi_is_runflags_running(runflags)) {
2499 				if (comedi_is_runflags_in_error(runflags))
2500 					retval = -EPIPE;
2501 				if (retval || nbytes)
2502 					become_nonbusy = true;
2503 				break;
2504 			}
2505 			if (nbytes == 0)
2506 				break;
2507 			if (file->f_flags & O_NONBLOCK) {
2508 				retval = -EAGAIN;
2509 				break;
2510 			}
2511 			schedule();
2512 			if (signal_pending(current)) {
2513 				retval = -ERESTARTSYS;
2514 				break;
2515 			}
2516 			if (s->busy != file ||
2517 			    (async->cmd.flags & CMDF_WRITE)) {
2518 				retval = -EINVAL;
2519 				break;
2520 			}
2521 			continue;
2522 		}
2523 		rp = async->buf_read_ptr;
2524 		n1 = min(n, async->prealloc_bufsz - rp);
2525 		n2 = n - n1;
2526 		m = copy_to_user(buf, async->prealloc_buf + rp, n1);
2527 		if (m)
2528 			m += n2;
2529 		else if (n2)
2530 			m = copy_to_user(buf + n1, async->prealloc_buf, n2);
2531 		if (m) {
2532 			n -= m;
2533 			retval = -EFAULT;
2534 		}
2535 
2536 		comedi_buf_read_alloc(s, n);
2537 		comedi_buf_read_free(s, n);
2538 
2539 		count += n;
2540 		nbytes -= n;
2541 
2542 		buf += n;
2543 	}
2544 	remove_wait_queue(&async->wait_head, &wait);
2545 	set_current_state(TASK_RUNNING);
2546 	if (become_nonbusy && count == 0) {
2547 		struct comedi_subdevice *new_s;
2548 
2549 		/*
2550 		 * To avoid deadlock, cannot acquire dev->mutex
2551 		 * while dev->attach_lock is held.
2552 		 */
2553 		up_read(&dev->attach_lock);
2554 		attach_locked = false;
2555 		mutex_lock(&dev->mutex);
2556 		/*
2557 		 * Check device hasn't become detached behind our back.
2558 		 * Checking dev->detach_count is unchanged ought to be
2559 		 * sufficient (unless there have been 2**32 detaches in the
2560 		 * meantime!), but check the subdevice pointer as well just in
2561 		 * case.
2562 		 *
2563 		 * Also check the subdevice is still in a suitable state to
2564 		 * become non-busy in case it changed behind our back.
2565 		 */
2566 		new_s = comedi_file_read_subdevice(file);
2567 		if (dev->attached && old_detach_count == dev->detach_count &&
2568 		    s == new_s && new_s->async == async && s->busy == file &&
2569 		    !(async->cmd.flags & CMDF_WRITE) &&
2570 		    !comedi_is_subdevice_running(s) &&
2571 		    comedi_buf_read_n_available(s) == 0)
2572 			do_become_nonbusy(dev, s);
2573 		mutex_unlock(&dev->mutex);
2574 	}
2575 out:
2576 	if (attach_locked)
2577 		up_read(&dev->attach_lock);
2578 
2579 	return count ? count : retval;
2580 }
2581 
comedi_open(struct inode * inode,struct file * file)2582 static int comedi_open(struct inode *inode, struct file *file)
2583 {
2584 	const unsigned minor = iminor(inode);
2585 	struct comedi_file *cfp;
2586 	struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2587 	int rc;
2588 
2589 	if (!dev) {
2590 		pr_debug("invalid minor number\n");
2591 		return -ENODEV;
2592 	}
2593 
2594 	cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2595 	if (!cfp) {
2596 		comedi_dev_put(dev);
2597 		return -ENOMEM;
2598 	}
2599 
2600 	cfp->dev = dev;
2601 
2602 	mutex_lock(&dev->mutex);
2603 	if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
2604 		dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
2605 		rc = -ENODEV;
2606 		goto out;
2607 	}
2608 	if (dev->attached && dev->use_count == 0) {
2609 		if (!try_module_get(dev->driver->module)) {
2610 			rc = -ENXIO;
2611 			goto out;
2612 		}
2613 		if (dev->open) {
2614 			rc = dev->open(dev);
2615 			if (rc < 0) {
2616 				module_put(dev->driver->module);
2617 				goto out;
2618 			}
2619 		}
2620 	}
2621 
2622 	dev->use_count++;
2623 	file->private_data = cfp;
2624 	comedi_file_reset(file);
2625 	rc = 0;
2626 
2627 out:
2628 	mutex_unlock(&dev->mutex);
2629 	if (rc) {
2630 		comedi_dev_put(dev);
2631 		kfree(cfp);
2632 	}
2633 	return rc;
2634 }
2635 
comedi_fasync(int fd,struct file * file,int on)2636 static int comedi_fasync(int fd, struct file *file, int on)
2637 {
2638 	struct comedi_file *cfp = file->private_data;
2639 	struct comedi_device *dev = cfp->dev;
2640 
2641 	return fasync_helper(fd, file, on, &dev->async_queue);
2642 }
2643 
comedi_close(struct inode * inode,struct file * file)2644 static int comedi_close(struct inode *inode, struct file *file)
2645 {
2646 	struct comedi_file *cfp = file->private_data;
2647 	struct comedi_device *dev = cfp->dev;
2648 	struct comedi_subdevice *s = NULL;
2649 	int i;
2650 
2651 	mutex_lock(&dev->mutex);
2652 
2653 	if (dev->subdevices) {
2654 		for (i = 0; i < dev->n_subdevices; i++) {
2655 			s = &dev->subdevices[i];
2656 
2657 			if (s->busy == file)
2658 				do_cancel(dev, s);
2659 			if (s->lock == file)
2660 				s->lock = NULL;
2661 		}
2662 	}
2663 	if (dev->attached && dev->use_count == 1) {
2664 		if (dev->close)
2665 			dev->close(dev);
2666 		module_put(dev->driver->module);
2667 	}
2668 
2669 	dev->use_count--;
2670 
2671 	mutex_unlock(&dev->mutex);
2672 	comedi_dev_put(dev);
2673 	kfree(cfp);
2674 
2675 	return 0;
2676 }
2677 
2678 static const struct file_operations comedi_fops = {
2679 	.owner = THIS_MODULE,
2680 	.unlocked_ioctl = comedi_unlocked_ioctl,
2681 	.compat_ioctl = comedi_compat_ioctl,
2682 	.open = comedi_open,
2683 	.release = comedi_close,
2684 	.read = comedi_read,
2685 	.write = comedi_write,
2686 	.mmap = comedi_mmap,
2687 	.poll = comedi_poll,
2688 	.fasync = comedi_fasync,
2689 	.llseek = noop_llseek,
2690 };
2691 
2692 /**
2693  * comedi_event() - Handle events for asynchronous COMEDI command
2694  * @dev: COMEDI device.
2695  * @s: COMEDI subdevice.
2696  * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
2697  *
2698  * If an asynchronous COMEDI command is active on the subdevice, process
2699  * any %COMEDI_CB_... event flags that have been set, usually by an
2700  * interrupt handler.  These may change the run state of the asynchronous
2701  * command, wake a task, and/or send a %SIGIO signal.
2702  */
comedi_event(struct comedi_device * dev,struct comedi_subdevice * s)2703 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2704 {
2705 	struct comedi_async *async = s->async;
2706 	unsigned int events;
2707 	int si_code = 0;
2708 	unsigned long flags;
2709 
2710 	spin_lock_irqsave(&s->spin_lock, flags);
2711 
2712 	events = async->events;
2713 	async->events = 0;
2714 	if (!__comedi_is_subdevice_running(s)) {
2715 		spin_unlock_irqrestore(&s->spin_lock, flags);
2716 		return;
2717 	}
2718 
2719 	if (events & COMEDI_CB_CANCEL_MASK)
2720 		__comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
2721 
2722 	/*
2723 	 * Remember if an error event has occurred, so an error can be
2724 	 * returned the next time the user does a read() or write().
2725 	 */
2726 	if (events & COMEDI_CB_ERROR_MASK)
2727 		__comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
2728 
2729 	if (async->cb_mask & events) {
2730 		wake_up_interruptible(&async->wait_head);
2731 		si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
2732 	}
2733 
2734 	spin_unlock_irqrestore(&s->spin_lock, flags);
2735 
2736 	if (si_code)
2737 		kill_fasync(&dev->async_queue, SIGIO, si_code);
2738 }
2739 EXPORT_SYMBOL_GPL(comedi_event);
2740 
2741 /* Note: the ->mutex is pre-locked on successful return */
comedi_alloc_board_minor(struct device * hardware_device)2742 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2743 {
2744 	struct comedi_device *dev;
2745 	struct device *csdev;
2746 	unsigned i;
2747 
2748 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2749 	if (!dev)
2750 		return ERR_PTR(-ENOMEM);
2751 	comedi_device_init(dev);
2752 	comedi_set_hw_dev(dev, hardware_device);
2753 	mutex_lock(&dev->mutex);
2754 	mutex_lock(&comedi_board_minor_table_lock);
2755 	for (i = hardware_device ? comedi_num_legacy_minors : 0;
2756 	     i < COMEDI_NUM_BOARD_MINORS; ++i) {
2757 		if (!comedi_board_minor_table[i]) {
2758 			comedi_board_minor_table[i] = dev;
2759 			break;
2760 		}
2761 	}
2762 	mutex_unlock(&comedi_board_minor_table_lock);
2763 	if (i == COMEDI_NUM_BOARD_MINORS) {
2764 		mutex_unlock(&dev->mutex);
2765 		comedi_device_cleanup(dev);
2766 		comedi_dev_put(dev);
2767 		dev_err(hardware_device,
2768 			"ran out of minor numbers for board device files\n");
2769 		return ERR_PTR(-EBUSY);
2770 	}
2771 	dev->minor = i;
2772 	csdev = device_create(comedi_class, hardware_device,
2773 			      MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2774 	if (!IS_ERR(csdev))
2775 		dev->class_dev = get_device(csdev);
2776 
2777 	/* Note: dev->mutex needs to be unlocked by the caller. */
2778 	return dev;
2779 }
2780 
comedi_release_hardware_device(struct device * hardware_device)2781 void comedi_release_hardware_device(struct device *hardware_device)
2782 {
2783 	int minor;
2784 	struct comedi_device *dev;
2785 
2786 	for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2787 	     minor++) {
2788 		mutex_lock(&comedi_board_minor_table_lock);
2789 		dev = comedi_board_minor_table[minor];
2790 		if (dev && dev->hw_dev == hardware_device) {
2791 			comedi_board_minor_table[minor] = NULL;
2792 			mutex_unlock(&comedi_board_minor_table_lock);
2793 			comedi_free_board_dev(dev);
2794 			break;
2795 		}
2796 		mutex_unlock(&comedi_board_minor_table_lock);
2797 	}
2798 }
2799 
comedi_alloc_subdevice_minor(struct comedi_subdevice * s)2800 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2801 {
2802 	struct comedi_device *dev = s->device;
2803 	struct device *csdev;
2804 	unsigned i;
2805 
2806 	mutex_lock(&comedi_subdevice_minor_table_lock);
2807 	for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2808 		if (!comedi_subdevice_minor_table[i]) {
2809 			comedi_subdevice_minor_table[i] = s;
2810 			break;
2811 		}
2812 	}
2813 	mutex_unlock(&comedi_subdevice_minor_table_lock);
2814 	if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2815 		dev_err(dev->class_dev,
2816 			"ran out of minor numbers for subdevice files\n");
2817 		return -EBUSY;
2818 	}
2819 	i += COMEDI_NUM_BOARD_MINORS;
2820 	s->minor = i;
2821 	csdev = device_create(comedi_class, dev->class_dev,
2822 			      MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2823 			      dev->minor, s->index);
2824 	if (!IS_ERR(csdev))
2825 		s->class_dev = csdev;
2826 
2827 	return 0;
2828 }
2829 
comedi_free_subdevice_minor(struct comedi_subdevice * s)2830 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2831 {
2832 	unsigned int i;
2833 
2834 	if (!s)
2835 		return;
2836 	if (s->minor < COMEDI_NUM_BOARD_MINORS ||
2837 	    s->minor >= COMEDI_NUM_MINORS)
2838 		return;
2839 
2840 	i = s->minor - COMEDI_NUM_BOARD_MINORS;
2841 	mutex_lock(&comedi_subdevice_minor_table_lock);
2842 	if (s == comedi_subdevice_minor_table[i])
2843 		comedi_subdevice_minor_table[i] = NULL;
2844 	mutex_unlock(&comedi_subdevice_minor_table_lock);
2845 	if (s->class_dev) {
2846 		device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2847 		s->class_dev = NULL;
2848 	}
2849 }
2850 
comedi_cleanup_board_minors(void)2851 static void comedi_cleanup_board_minors(void)
2852 {
2853 	struct comedi_device *dev;
2854 	unsigned i;
2855 
2856 	for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
2857 		dev = comedi_clear_board_minor(i);
2858 		comedi_free_board_dev(dev);
2859 	}
2860 }
2861 
comedi_init(void)2862 static int __init comedi_init(void)
2863 {
2864 	int i;
2865 	int retval;
2866 
2867 	pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
2868 
2869 	if (comedi_num_legacy_minors < 0 ||
2870 	    comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2871 		pr_err("invalid value for module parameter \"comedi_num_legacy_minors\".  Valid values are 0 through %i.\n",
2872 		       COMEDI_NUM_BOARD_MINORS);
2873 		return -EINVAL;
2874 	}
2875 
2876 	retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2877 					COMEDI_NUM_MINORS, "comedi");
2878 	if (retval)
2879 		return -EIO;
2880 	cdev_init(&comedi_cdev, &comedi_fops);
2881 	comedi_cdev.owner = THIS_MODULE;
2882 
2883 	retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2884 	if (retval) {
2885 		unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2886 					 COMEDI_NUM_MINORS);
2887 		return retval;
2888 	}
2889 
2890 	if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2891 		unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2892 					 COMEDI_NUM_MINORS);
2893 		return -EIO;
2894 	}
2895 	comedi_class = class_create(THIS_MODULE, "comedi");
2896 	if (IS_ERR(comedi_class)) {
2897 		pr_err("failed to create class\n");
2898 		cdev_del(&comedi_cdev);
2899 		unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2900 					 COMEDI_NUM_MINORS);
2901 		return PTR_ERR(comedi_class);
2902 	}
2903 
2904 	comedi_class->dev_groups = comedi_dev_groups;
2905 
2906 	/* create devices files for legacy/manual use */
2907 	for (i = 0; i < comedi_num_legacy_minors; i++) {
2908 		struct comedi_device *dev;
2909 
2910 		dev = comedi_alloc_board_minor(NULL);
2911 		if (IS_ERR(dev)) {
2912 			comedi_cleanup_board_minors();
2913 			class_destroy(comedi_class);
2914 			cdev_del(&comedi_cdev);
2915 			unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2916 						 COMEDI_NUM_MINORS);
2917 			return PTR_ERR(dev);
2918 		}
2919 		/* comedi_alloc_board_minor() locked the mutex */
2920 		mutex_unlock(&dev->mutex);
2921 	}
2922 
2923 	/* XXX requires /proc interface */
2924 	comedi_proc_init();
2925 
2926 	return 0;
2927 }
2928 module_init(comedi_init);
2929 
comedi_cleanup(void)2930 static void __exit comedi_cleanup(void)
2931 {
2932 	comedi_cleanup_board_minors();
2933 	class_destroy(comedi_class);
2934 	cdev_del(&comedi_cdev);
2935 	unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2936 
2937 	comedi_proc_cleanup();
2938 }
2939 module_exit(comedi_cleanup);
2940 
2941 MODULE_AUTHOR("http://www.comedi.org");
2942 MODULE_DESCRIPTION("Comedi core module");
2943 MODULE_LICENSE("GPL");
2944