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