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