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