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