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