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