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