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