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