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