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