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