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