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