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