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