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