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