blob: 9713fc7465618728d09d8364c9e7b3eff7fc8915 [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
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>
Frank Mori Hess883db3d2009-04-14 11:21:41 -040047#include <linux/stat.h>
David Schleefed9eccb2008-11-04 20:29:31 -080048
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080049#include <linux/io.h>
50#include <linux/uaccess.h>
David Schleefed9eccb2008-11-04 20:29:31 -080051
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080052/* #include "kvmem.h" */
David Schleefed9eccb2008-11-04 20:29:31 -080053
54MODULE_AUTHOR("http://www.comedi.org");
55MODULE_DESCRIPTION("Comedi core module");
56MODULE_LICENSE("GPL");
57
58#ifdef CONFIG_COMEDI_DEBUG
59int comedi_debug;
60module_param(comedi_debug, int, 0644);
61#endif
62
Ian Abbott6a9d7a22008-12-08 17:05:50 +000063int comedi_autoconfig = 1;
64module_param(comedi_autoconfig, bool, 0444);
65
Bernd Porr1dd33ab2008-12-08 23:30:13 +000066int comedi_num_legacy_minors = 0;
67module_param(comedi_num_legacy_minors, int, 0444);
68
David Schleefed9eccb2008-11-04 20:29:31 -080069static DEFINE_SPINLOCK(comedi_file_info_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080070static struct comedi_device_file_info
71 *comedi_file_info_table[COMEDI_NUM_MINORS];
David Schleefed9eccb2008-11-04 20:29:31 -080072
Bill Pemberton0707bb02009-03-16 22:06:20 -040073static int do_devconfig_ioctl(struct comedi_device *dev, struct comedi_devconfig *arg);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040074static int do_bufconfig_ioctl(struct comedi_device *dev, void *arg);
Bill Pemberton063db042009-03-16 22:06:15 -040075static int do_devinfo_ioctl(struct comedi_device *dev, struct comedi_devinfo *arg,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080076 struct file *file);
Bill Pembertonbd52efb2009-03-16 22:06:09 -040077static int do_subdinfo_ioctl(struct comedi_device *dev, struct comedi_subdinfo *arg,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080078 void *file);
Bill Pembertona18b4162009-03-16 22:06:04 -040079static int do_chaninfo_ioctl(struct comedi_device *dev, struct comedi_chaninfo *arg);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040080static int do_bufinfo_ioctl(struct comedi_device *dev, void *arg);
81static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file);
82static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg, void *file);
83static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg, void *file);
84static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg, void *file);
85static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file);
86static int do_insnlist_ioctl(struct comedi_device *dev, void *arg, void *file);
87static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file);
88static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd, void *file);
David Schleefed9eccb2008-11-04 20:29:31 -080089
Bill Pemberton34c43922009-03-16 22:05:14 -040090extern void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s);
91static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
David Schleefed9eccb2008-11-04 20:29:31 -080092
93static int comedi_fasync(int fd, struct file *file, int on);
94
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040095static int is_device_busy(struct comedi_device *dev);
Frank Mori Hess883db3d2009-04-14 11:21:41 -040096static int resize_async_buffer(struct comedi_device *dev,
97 struct comedi_subdevice *s,
98 struct comedi_async *async, unsigned new_size);
99
100/* declarations for sysfs attribute files */
101static struct device_attribute dev_attr_max_read_buffer_kb;
102static struct device_attribute dev_attr_read_buffer_kb;
103static struct device_attribute dev_attr_max_write_buffer_kb;
104static struct device_attribute dev_attr_write_buffer_kb;
David Schleefed9eccb2008-11-04 20:29:31 -0800105
106#ifdef HAVE_UNLOCKED_IOCTL
107static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800108 unsigned long arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800109#else
110static int comedi_ioctl(struct inode *inode, struct file *file,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800111 unsigned int cmd, unsigned long arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800112#endif
113{
114 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800115 struct comedi_device_file_info *dev_file_info =
116 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400117 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800118 int rc;
119
Frank Mori Hess53b670a2008-12-15 13:48:47 +0000120 if (dev_file_info == NULL || dev_file_info->device == NULL)
121 return -ENODEV;
122 dev = dev_file_info->device;
123
David Schleefed9eccb2008-11-04 20:29:31 -0800124 mutex_lock(&dev->mutex);
125
126 /* Device config is special, because it must work on
127 * an unconfigured device. */
128 if (cmd == COMEDI_DEVCONFIG) {
129 rc = do_devconfig_ioctl(dev, (void *)arg);
130 goto done;
131 }
132
133 if (!dev->attached) {
134 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
135 rc = -ENODEV;
136 goto done;
137 }
138
139 switch (cmd) {
140 case COMEDI_BUFCONFIG:
141 rc = do_bufconfig_ioctl(dev, (void *)arg);
142 break;
143 case COMEDI_DEVINFO:
144 rc = do_devinfo_ioctl(dev, (void *)arg, file);
145 break;
146 case COMEDI_SUBDINFO:
147 rc = do_subdinfo_ioctl(dev, (void *)arg, file);
148 break;
149 case COMEDI_CHANINFO:
150 rc = do_chaninfo_ioctl(dev, (void *)arg);
151 break;
152 case COMEDI_RANGEINFO:
153 rc = do_rangeinfo_ioctl(dev, (void *)arg);
154 break;
155 case COMEDI_BUFINFO:
156 rc = do_bufinfo_ioctl(dev, (void *)arg);
157 break;
158 case COMEDI_LOCK:
159 rc = do_lock_ioctl(dev, arg, file);
160 break;
161 case COMEDI_UNLOCK:
162 rc = do_unlock_ioctl(dev, arg, file);
163 break;
164 case COMEDI_CANCEL:
165 rc = do_cancel_ioctl(dev, arg, file);
166 break;
167 case COMEDI_CMD:
168 rc = do_cmd_ioctl(dev, (void *)arg, file);
169 break;
170 case COMEDI_CMDTEST:
171 rc = do_cmdtest_ioctl(dev, (void *)arg, file);
172 break;
173 case COMEDI_INSNLIST:
174 rc = do_insnlist_ioctl(dev, (void *)arg, file);
175 break;
176 case COMEDI_INSN:
177 rc = do_insn_ioctl(dev, (void *)arg, file);
178 break;
179 case COMEDI_POLL:
180 rc = do_poll_ioctl(dev, arg, file);
181 break;
182 default:
183 rc = -ENOTTY;
184 break;
185 }
186
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800187done:
David Schleefed9eccb2008-11-04 20:29:31 -0800188 mutex_unlock(&dev->mutex);
189 return rc;
190}
191
192/*
193 COMEDI_DEVCONFIG
194 device config ioctl
195
196 arg:
197 pointer to devconfig structure
198
199 reads:
200 devconfig structure at arg
201
202 writes:
203 none
204*/
Bill Pemberton0707bb02009-03-16 22:06:20 -0400205static int do_devconfig_ioctl(struct comedi_device *dev, struct comedi_devconfig *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800206{
Bill Pemberton0707bb02009-03-16 22:06:20 -0400207 struct comedi_devconfig it;
David Schleefed9eccb2008-11-04 20:29:31 -0800208 int ret;
209 unsigned char *aux_data = NULL;
210 int aux_len;
211
212 if (!capable(CAP_SYS_ADMIN))
213 return -EPERM;
214
215 if (arg == NULL) {
216 if (is_device_busy(dev))
217 return -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800218 if (dev->attached) {
David Schleefed9eccb2008-11-04 20:29:31 -0800219 struct module *driver_module = dev->driver->module;
220 comedi_device_detach(dev);
221 module_put(driver_module);
222 }
223 return 0;
224 }
225
Bill Pemberton0707bb02009-03-16 22:06:20 -0400226 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800227 return -EFAULT;
228
229 it.board_name[COMEDI_NAMELEN - 1] = 0;
230
231 if (comedi_aux_data(it.options, 0) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800232 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
David Schleefed9eccb2008-11-04 20:29:31 -0800233 int bit_shift;
234 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
235 if (aux_len < 0)
236 return -EFAULT;
237
238 aux_data = vmalloc(aux_len);
239 if (!aux_data)
240 return -ENOMEM;
241
242 if (copy_from_user(aux_data,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800243 comedi_aux_data(it.options, 0), aux_len)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800244 vfree(aux_data);
245 return -EFAULT;
246 }
247 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800248 (unsigned long)aux_data;
David Schleefed9eccb2008-11-04 20:29:31 -0800249 if (sizeof(void *) > sizeof(int)) {
250 bit_shift = sizeof(int) * 8;
251 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800252 ((unsigned long)aux_data) >> bit_shift;
David Schleefed9eccb2008-11-04 20:29:31 -0800253 } else
254 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
255 }
256
257 ret = comedi_device_attach(dev, &it);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800258 if (ret == 0) {
259 if (!try_module_get(dev->driver->module)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800260 comedi_device_detach(dev);
261 return -ENOSYS;
262 }
263 }
264
265 if (aux_data)
266 vfree(aux_data);
267
268 return ret;
269}
270
271/*
272 COMEDI_BUFCONFIG
273 buffer configuration ioctl
274
275 arg:
276 pointer to bufconfig structure
277
278 reads:
279 bufconfig at arg
280
281 writes:
282 modified bufconfig at arg
283
284*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400285static int do_bufconfig_ioctl(struct comedi_device *dev, void *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800286{
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400287 struct comedi_bufconfig bc;
Bill Pembertond1636792009-03-16 22:05:20 -0400288 struct comedi_async *async;
Bill Pemberton34c43922009-03-16 22:05:14 -0400289 struct comedi_subdevice *s;
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400290 int retval = 0;
David Schleefed9eccb2008-11-04 20:29:31 -0800291
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400292 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800293 return -EFAULT;
294
295 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
296 return -EINVAL;
297
298 s = dev->subdevices + bc.subdevice;
299 async = s->async;
300
301 if (!async) {
302 DPRINTK("subdevice does not have async capability\n");
303 bc.size = 0;
304 bc.maximum_size = 0;
305 goto copyback;
306 }
307
308 if (bc.maximum_size) {
309 if (!capable(CAP_SYS_ADMIN))
310 return -EPERM;
311
312 async->max_bufsize = bc.maximum_size;
313 }
314
315 if (bc.size) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400316 retval = resize_async_buffer(dev, s, async, bc.size);
317 if (retval < 0)
318 return retval;
David Schleefed9eccb2008-11-04 20:29:31 -0800319 }
320
321 bc.size = async->prealloc_bufsz;
322 bc.maximum_size = async->max_bufsize;
323
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800324copyback:
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400325 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800326 return -EFAULT;
327
328 return 0;
329}
330
331/*
332 COMEDI_DEVINFO
333 device info ioctl
334
335 arg:
336 pointer to devinfo structure
337
338 reads:
339 none
340
341 writes:
342 devinfo structure
343
344*/
Bill Pemberton063db042009-03-16 22:06:15 -0400345static int do_devinfo_ioctl(struct comedi_device *dev, struct comedi_devinfo *arg,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800346 struct file *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800347{
Bill Pemberton063db042009-03-16 22:06:15 -0400348 struct comedi_devinfo devinfo;
David Schleefed9eccb2008-11-04 20:29:31 -0800349 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800350 struct comedi_device_file_info *dev_file_info =
351 comedi_get_device_file_info(minor);
Bill Pemberton34c43922009-03-16 22:05:14 -0400352 struct comedi_subdevice *read_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800353 comedi_get_read_subdevice(dev_file_info);
Bill Pemberton34c43922009-03-16 22:05:14 -0400354 struct comedi_subdevice *write_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800355 comedi_get_write_subdevice(dev_file_info);
David Schleefed9eccb2008-11-04 20:29:31 -0800356
357 memset(&devinfo, 0, sizeof(devinfo));
358
359 /* fill devinfo structure */
360 devinfo.version_code = COMEDI_VERSION_CODE;
361 devinfo.n_subdevs = dev->n_subdevices;
362 memcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
363 memcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
364
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800365 if (read_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800366 devinfo.read_subdevice = read_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800367 else
David Schleefed9eccb2008-11-04 20:29:31 -0800368 devinfo.read_subdevice = -1;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800369
370 if (write_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800371 devinfo.write_subdevice = write_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800372 else
David Schleefed9eccb2008-11-04 20:29:31 -0800373 devinfo.write_subdevice = -1;
David Schleefed9eccb2008-11-04 20:29:31 -0800374
Bill Pemberton063db042009-03-16 22:06:15 -0400375 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800376 return -EFAULT;
377
378 return 0;
379}
380
381/*
382 COMEDI_SUBDINFO
383 subdevice info ioctl
384
385 arg:
386 pointer to array of subdevice info structures
387
388 reads:
389 none
390
391 writes:
392 array of subdevice info structures at arg
393
394*/
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400395static int do_subdinfo_ioctl(struct comedi_device *dev, struct comedi_subdinfo *arg,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800396 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800397{
398 int ret, i;
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400399 struct comedi_subdinfo *tmp, *us;
Bill Pemberton34c43922009-03-16 22:05:14 -0400400 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800401
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400402 tmp = kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800403 if (!tmp)
404 return -ENOMEM;
405
406 /* fill subdinfo structs */
407 for (i = 0; i < dev->n_subdevices; i++) {
408 s = dev->subdevices + i;
409 us = tmp + i;
410
411 us->type = s->type;
412 us->n_chan = s->n_chan;
413 us->subd_flags = s->subdev_flags;
414 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
415 us->subd_flags |= SDF_RUNNING;
416#define TIMER_nanosec 5 /* backwards compatibility */
417 us->timer_type = TIMER_nanosec;
418 us->len_chanlist = s->len_chanlist;
419 us->maxdata = s->maxdata;
420 if (s->range_table) {
421 us->range_type =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800422 (i << 24) | (0 << 16) | (s->range_table->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800423 } else {
424 us->range_type = 0; /* XXX */
425 }
426 us->flags = s->flags;
427
428 if (s->busy)
429 us->subd_flags |= SDF_BUSY;
430 if (s->busy == file)
431 us->subd_flags |= SDF_BUSY_OWNER;
432 if (s->lock)
433 us->subd_flags |= SDF_LOCKED;
434 if (s->lock == file)
435 us->subd_flags |= SDF_LOCK_OWNER;
436 if (!s->maxdata && s->maxdata_list)
437 us->subd_flags |= SDF_MAXDATA;
438 if (s->flaglist)
439 us->subd_flags |= SDF_FLAGS;
440 if (s->range_table_list)
441 us->subd_flags |= SDF_RANGETYPE;
442 if (s->do_cmd)
443 us->subd_flags |= SDF_CMD;
444
445 if (s->insn_bits != &insn_inval)
446 us->insn_bits_support = COMEDI_SUPPORTED;
447 else
448 us->insn_bits_support = COMEDI_UNSUPPORTED;
449
450 us->settling_time_0 = s->settling_time_0;
451 }
452
453 ret = copy_to_user(arg, tmp,
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400454 dev->n_subdevices * sizeof(struct comedi_subdinfo));
David Schleefed9eccb2008-11-04 20:29:31 -0800455
456 kfree(tmp);
457
458 return ret ? -EFAULT : 0;
459}
460
461/*
462 COMEDI_CHANINFO
463 subdevice info ioctl
464
465 arg:
466 pointer to chaninfo structure
467
468 reads:
469 chaninfo structure at arg
470
471 writes:
472 arrays at elements of chaninfo structure
473
474*/
Bill Pembertona18b4162009-03-16 22:06:04 -0400475static int do_chaninfo_ioctl(struct comedi_device *dev, struct comedi_chaninfo *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800476{
Bill Pemberton34c43922009-03-16 22:05:14 -0400477 struct comedi_subdevice *s;
Bill Pembertona18b4162009-03-16 22:06:04 -0400478 struct comedi_chaninfo it;
David Schleefed9eccb2008-11-04 20:29:31 -0800479
Bill Pembertona18b4162009-03-16 22:06:04 -0400480 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800481 return -EFAULT;
482
483 if (it.subdev >= dev->n_subdevices)
484 return -EINVAL;
485 s = dev->subdevices + it.subdev;
486
487 if (it.maxdata_list) {
488 if (s->maxdata || !s->maxdata_list)
489 return -EINVAL;
490 if (copy_to_user(it.maxdata_list, s->maxdata_list,
Bill Pemberton790c5542009-03-16 22:05:02 -0400491 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800492 return -EFAULT;
493 }
494
495 if (it.flaglist) {
496 if (!s->flaglist)
497 return -EINVAL;
498 if (copy_to_user(it.flaglist, s->flaglist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800499 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800500 return -EFAULT;
501 }
502
503 if (it.rangelist) {
504 int i;
505
506 if (!s->range_table_list)
507 return -EINVAL;
508 for (i = 0; i < s->n_chan; i++) {
509 int x;
510
511 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800512 (s->range_table_list[i]->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800513 put_user(x, it.rangelist + i);
514 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800515#if 0
516 if (copy_to_user(it.rangelist, s->range_type_list,
517 s->n_chan*sizeof(unsigned int)))
518 return -EFAULT;
519#endif
David Schleefed9eccb2008-11-04 20:29:31 -0800520 }
521
522 return 0;
523}
524
525 /*
526 COMEDI_BUFINFO
527 buffer information ioctl
528
529 arg:
530 pointer to bufinfo structure
531
532 reads:
533 bufinfo at arg
534
535 writes:
536 modified bufinfo at arg
537
538 */
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400539static int do_bufinfo_ioctl(struct comedi_device *dev, void *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800540{
Bill Pemberton9aa53392009-03-16 22:06:42 -0400541 struct comedi_bufinfo bi;
Bill Pemberton34c43922009-03-16 22:05:14 -0400542 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400543 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800544
Bill Pemberton9aa53392009-03-16 22:06:42 -0400545 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800546 return -EFAULT;
547
548 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
549 return -EINVAL;
550
551 s = dev->subdevices + bi.subdevice;
552 async = s->async;
553
554 if (!async) {
555 DPRINTK("subdevice does not have async capability\n");
556 bi.buf_write_ptr = 0;
557 bi.buf_read_ptr = 0;
558 bi.buf_write_count = 0;
559 bi.buf_read_count = 0;
560 goto copyback;
561 }
562
563 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
564 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
565 comedi_buf_read_free(async, bi.bytes_read);
566
567 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800568 SRF_RUNNING))
569 && async->buf_write_count == async->buf_read_count) {
David Schleefed9eccb2008-11-04 20:29:31 -0800570 do_become_nonbusy(dev, s);
571 }
572 }
573
574 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
575 bi.bytes_written =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800576 comedi_buf_write_alloc(async, bi.bytes_written);
David Schleefed9eccb2008-11-04 20:29:31 -0800577 comedi_buf_write_free(async, bi.bytes_written);
578 }
579
580 bi.buf_write_count = async->buf_write_count;
581 bi.buf_write_ptr = async->buf_write_ptr;
582 bi.buf_read_count = async->buf_read_count;
583 bi.buf_read_ptr = async->buf_read_ptr;
584
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800585copyback:
Bill Pemberton9aa53392009-03-16 22:06:42 -0400586 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800587 return -EFAULT;
588
589 return 0;
590}
591
Bill Pemberton90035c02009-03-16 22:05:53 -0400592static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn, unsigned int *data,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800593 void *file);
David Schleefed9eccb2008-11-04 20:29:31 -0800594/*
595 * COMEDI_INSNLIST
596 * synchronous instructions
597 *
598 * arg:
599 * pointer to sync cmd structure
600 *
601 * reads:
602 * sync cmd struct at arg
603 * instruction list
604 * data (for writes)
605 *
606 * writes:
607 * data (for reads)
608 */
609/* arbitrary limits */
610#define MAX_SAMPLES 256
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400611static int do_insnlist_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800612{
Bill Pembertonda613f42009-03-16 22:05:59 -0400613 struct comedi_insnlist insnlist;
Bill Pemberton90035c02009-03-16 22:05:53 -0400614 struct comedi_insn *insns = NULL;
Bill Pemberton790c5542009-03-16 22:05:02 -0400615 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800616 int i = 0;
617 int ret = 0;
618
Bill Pembertonda613f42009-03-16 22:05:59 -0400619 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
David Schleefed9eccb2008-11-04 20:29:31 -0800620 return -EFAULT;
621
Bill Pemberton790c5542009-03-16 22:05:02 -0400622 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800623 if (!data) {
624 DPRINTK("kmalloc failed\n");
625 ret = -ENOMEM;
626 goto error;
627 }
628
Bill Pemberton90035c02009-03-16 22:05:53 -0400629 insns = kmalloc(sizeof(struct comedi_insn) * insnlist.n_insns, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800630 if (!insns) {
631 DPRINTK("kmalloc failed\n");
632 ret = -ENOMEM;
633 goto error;
634 }
635
636 if (copy_from_user(insns, insnlist.insns,
Bill Pemberton90035c02009-03-16 22:05:53 -0400637 sizeof(struct comedi_insn) * insnlist.n_insns)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800638 DPRINTK("copy_from_user failed\n");
639 ret = -EFAULT;
640 goto error;
641 }
642
643 for (i = 0; i < insnlist.n_insns; i++) {
644 if (insns[i].n > MAX_SAMPLES) {
645 DPRINTK("number of samples too large\n");
646 ret = -EINVAL;
647 goto error;
648 }
649 if (insns[i].insn & INSN_MASK_WRITE) {
650 if (copy_from_user(data, insns[i].data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400651 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800652 DPRINTK("copy_from_user failed\n");
653 ret = -EFAULT;
654 goto error;
655 }
656 }
657 ret = parse_insn(dev, insns + i, data, file);
658 if (ret < 0)
659 goto error;
660 if (insns[i].insn & INSN_MASK_READ) {
661 if (copy_to_user(insns[i].data, data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400662 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800663 DPRINTK("copy_to_user failed\n");
664 ret = -EFAULT;
665 goto error;
666 }
667 }
668 if (need_resched())
669 schedule();
670 }
671
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800672error:
673 kfree(insns);
674 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800675
676 if (ret < 0)
677 return ret;
678 return i;
679}
680
Bill Pemberton90035c02009-03-16 22:05:53 -0400681static int check_insn_config_length(struct comedi_insn *insn, unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -0800682{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800683 if (insn->n < 1)
684 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -0800685
686 switch (data[0]) {
687 case INSN_CONFIG_DIO_OUTPUT:
688 case INSN_CONFIG_DIO_INPUT:
689 case INSN_CONFIG_DISARM:
690 case INSN_CONFIG_RESET:
691 if (insn->n == 1)
692 return 0;
693 break;
694 case INSN_CONFIG_ARM:
695 case INSN_CONFIG_DIO_QUERY:
696 case INSN_CONFIG_BLOCK_SIZE:
697 case INSN_CONFIG_FILTER:
698 case INSN_CONFIG_SERIAL_CLOCK:
699 case INSN_CONFIG_BIDIRECTIONAL_DATA:
700 case INSN_CONFIG_ALT_SOURCE:
701 case INSN_CONFIG_SET_COUNTER_MODE:
702 case INSN_CONFIG_8254_READ_STATUS:
703 case INSN_CONFIG_SET_ROUTING:
704 case INSN_CONFIG_GET_ROUTING:
705 case INSN_CONFIG_GET_PWM_STATUS:
706 case INSN_CONFIG_PWM_SET_PERIOD:
707 case INSN_CONFIG_PWM_GET_PERIOD:
708 if (insn->n == 2)
709 return 0;
710 break;
711 case INSN_CONFIG_SET_GATE_SRC:
712 case INSN_CONFIG_GET_GATE_SRC:
713 case INSN_CONFIG_SET_CLOCK_SRC:
714 case INSN_CONFIG_GET_CLOCK_SRC:
715 case INSN_CONFIG_SET_OTHER_SRC:
716 case INSN_CONFIG_GET_COUNTER_STATUS:
717 case INSN_CONFIG_PWM_SET_H_BRIDGE:
718 case INSN_CONFIG_PWM_GET_H_BRIDGE:
719 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
720 if (insn->n == 3)
721 return 0;
722 break;
723 case INSN_CONFIG_PWM_OUTPUT:
724 case INSN_CONFIG_ANALOG_TRIG:
725 if (insn->n == 5)
726 return 0;
727 break;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800728 /* by default we allow the insn since we don't have checks for
729 * all possible cases yet */
David Schleefed9eccb2008-11-04 20:29:31 -0800730 default:
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -0700731 printk("comedi: no check for data length of config insn id "
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800732 "%i is implemented.\n"
733 " Add a check to %s in %s.\n"
734 " Assuming n=%i is correct.\n", data[0], __func__,
735 __FILE__, insn->n);
David Schleefed9eccb2008-11-04 20:29:31 -0800736 return 0;
737 break;
738 }
739 return -EINVAL;
740}
741
Bill Pemberton90035c02009-03-16 22:05:53 -0400742static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn, unsigned int *data,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800743 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800744{
Bill Pemberton34c43922009-03-16 22:05:14 -0400745 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800746 int ret = 0;
747 int i;
748
749 if (insn->insn & INSN_MASK_SPECIAL) {
750 /* a non-subdevice instruction */
751
752 switch (insn->insn) {
753 case INSN_GTOD:
754 {
755 struct timeval tv;
756
757 if (insn->n != 2) {
758 ret = -EINVAL;
759 break;
760 }
761
762 do_gettimeofday(&tv);
763 data[0] = tv.tv_sec;
764 data[1] = tv.tv_usec;
765 ret = 2;
766
767 break;
768 }
769 case INSN_WAIT:
770 if (insn->n != 1 || data[0] >= 100000) {
771 ret = -EINVAL;
772 break;
773 }
774 udelay(data[0] / 1000);
775 ret = 1;
776 break;
777 case INSN_INTTRIG:
778 if (insn->n != 1) {
779 ret = -EINVAL;
780 break;
781 }
782 if (insn->subdev >= dev->n_subdevices) {
783 DPRINTK("%d not usable subdevice\n",
784 insn->subdev);
785 ret = -EINVAL;
786 break;
787 }
788 s = dev->subdevices + insn->subdev;
789 if (!s->async) {
790 DPRINTK("no async\n");
791 ret = -EINVAL;
792 break;
793 }
794 if (!s->async->inttrig) {
795 DPRINTK("no inttrig\n");
796 ret = -EAGAIN;
797 break;
798 }
799 ret = s->async->inttrig(dev, s, insn->data[0]);
800 if (ret >= 0)
801 ret = 1;
802 break;
803 default:
804 DPRINTK("invalid insn\n");
805 ret = -EINVAL;
806 break;
807 }
808 } else {
809 /* a subdevice instruction */
Bill Pemberton790c5542009-03-16 22:05:02 -0400810 unsigned int maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800811
812 if (insn->subdev >= dev->n_subdevices) {
813 DPRINTK("subdevice %d out of range\n", insn->subdev);
814 ret = -EINVAL;
815 goto out;
816 }
817 s = dev->subdevices + insn->subdev;
818
819 if (s->type == COMEDI_SUBD_UNUSED) {
820 DPRINTK("%d not usable subdevice\n", insn->subdev);
821 ret = -EIO;
822 goto out;
823 }
824
825 /* are we locked? (ioctl lock) */
826 if (s->lock && s->lock != file) {
827 DPRINTK("device locked\n");
828 ret = -EACCES;
829 goto out;
830 }
831
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800832 ret = check_chanlist(s, 1, &insn->chanspec);
833 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -0800834 ret = -EINVAL;
835 DPRINTK("bad chanspec\n");
836 goto out;
837 }
838
839 if (s->busy) {
840 ret = -EBUSY;
841 goto out;
842 }
843 /* This looks arbitrary. It is. */
844 s->busy = &parse_insn;
845 switch (insn->insn) {
846 case INSN_READ:
847 ret = s->insn_read(dev, s, insn, data);
848 break;
849 case INSN_WRITE:
850 maxdata = s->maxdata_list
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800851 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
852 : s->maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800853 for (i = 0; i < insn->n; ++i) {
854 if (data[i] > maxdata) {
855 ret = -EINVAL;
856 DPRINTK("bad data value(s)\n");
857 break;
858 }
859 }
860 if (ret == 0)
861 ret = s->insn_write(dev, s, insn, data);
862 break;
863 case INSN_BITS:
864 if (insn->n != 2) {
865 ret = -EINVAL;
866 break;
867 }
868 ret = s->insn_bits(dev, s, insn, data);
869 break;
870 case INSN_CONFIG:
871 ret = check_insn_config_length(insn, data);
872 if (ret)
873 break;
874 ret = s->insn_config(dev, s, insn, data);
875 break;
876 default:
877 ret = -EINVAL;
878 break;
879 }
880
881 s->busy = NULL;
882 }
883
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800884out:
David Schleefed9eccb2008-11-04 20:29:31 -0800885 return ret;
886}
887
888/*
889 * COMEDI_INSN
890 * synchronous instructions
891 *
892 * arg:
893 * pointer to insn
894 *
895 * reads:
Bill Pemberton90035c02009-03-16 22:05:53 -0400896 * struct comedi_insn struct at arg
David Schleefed9eccb2008-11-04 20:29:31 -0800897 * data (for writes)
898 *
899 * writes:
900 * data (for reads)
901 */
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400902static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800903{
Bill Pemberton90035c02009-03-16 22:05:53 -0400904 struct comedi_insn insn;
Bill Pemberton790c5542009-03-16 22:05:02 -0400905 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800906 int ret = 0;
907
Bill Pemberton790c5542009-03-16 22:05:02 -0400908 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800909 if (!data) {
910 ret = -ENOMEM;
911 goto error;
912 }
913
Bill Pemberton90035c02009-03-16 22:05:53 -0400914 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800915 ret = -EFAULT;
916 goto error;
917 }
918
919 /* This is where the behavior of insn and insnlist deviate. */
920 if (insn.n > MAX_SAMPLES)
921 insn.n = MAX_SAMPLES;
922 if (insn.insn & INSN_MASK_WRITE) {
Bill Pemberton790c5542009-03-16 22:05:02 -0400923 if (copy_from_user(data, insn.data, insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800924 ret = -EFAULT;
925 goto error;
926 }
927 }
928 ret = parse_insn(dev, &insn, data, file);
929 if (ret < 0)
930 goto error;
931 if (insn.insn & INSN_MASK_READ) {
Bill Pemberton790c5542009-03-16 22:05:02 -0400932 if (copy_to_user(insn.data, data, insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800933 ret = -EFAULT;
934 goto error;
935 }
936 }
937 ret = insn.n;
938
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800939error:
940 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800941
942 return ret;
943}
944
945/*
946 COMEDI_CMD
947 command ioctl
948
949 arg:
950 pointer to cmd structure
951
952 reads:
953 cmd structure at arg
954 channel/range list
955
956 writes:
957 modified cmd structure at arg
958
959*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400960static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800961{
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400962 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -0400963 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400964 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800965 int ret = 0;
966 unsigned int *chanlist_saver = NULL;
967
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400968 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800969 DPRINTK("bad cmd address\n");
970 return -EFAULT;
971 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800972 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -0800973 chanlist_saver = user_cmd.chanlist;
974
975 if (user_cmd.subdev >= dev->n_subdevices) {
976 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
977 return -ENODEV;
978 }
979
980 s = dev->subdevices + user_cmd.subdev;
981 async = s->async;
982
983 if (s->type == COMEDI_SUBD_UNUSED) {
984 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
985 return -EIO;
986 }
987
988 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
989 DPRINTK("subdevice %i does not support commands\n",
990 user_cmd.subdev);
991 return -EIO;
992 }
993
994 /* are we locked? (ioctl lock) */
995 if (s->lock && s->lock != file) {
996 DPRINTK("subdevice locked\n");
997 return -EACCES;
998 }
999
1000 /* are we busy? */
1001 if (s->busy) {
1002 DPRINTK("subdevice busy\n");
1003 return -EBUSY;
1004 }
1005 s->busy = file;
1006
1007 /* make sure channel/gain list isn't too long */
1008 if (user_cmd.chanlist_len > s->len_chanlist) {
1009 DPRINTK("channel/gain list too long %u > %d\n",
1010 user_cmd.chanlist_len, s->len_chanlist);
1011 ret = -EINVAL;
1012 goto cleanup;
1013 }
1014
1015 /* make sure channel/gain list isn't too short */
1016 if (user_cmd.chanlist_len < 1) {
1017 DPRINTK("channel/gain list too short %u < 1\n",
1018 user_cmd.chanlist_len);
1019 ret = -EINVAL;
1020 goto cleanup;
1021 }
1022
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001023 kfree(async->cmd.chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001024 async->cmd = user_cmd;
1025 async->cmd.data = NULL;
1026 /* load channel/gain list */
1027 async->cmd.chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001028 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001029 if (!async->cmd.chanlist) {
1030 DPRINTK("allocation failed\n");
1031 ret = -ENOMEM;
1032 goto cleanup;
1033 }
1034
1035 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001036 async->cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001037 DPRINTK("fault reading chanlist\n");
1038 ret = -EFAULT;
1039 goto cleanup;
1040 }
1041
1042 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001043 ret = check_chanlist(s, async->cmd.chanlist_len, async->cmd.chanlist);
1044 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001045 DPRINTK("bad chanlist\n");
1046 goto cleanup;
1047 }
1048
1049 ret = s->do_cmdtest(dev, s, &async->cmd);
1050
1051 if (async->cmd.flags & TRIG_BOGUS || ret) {
1052 DPRINTK("test returned %d\n", ret);
1053 user_cmd = async->cmd;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001054 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001055 user_cmd.chanlist = chanlist_saver;
1056 user_cmd.data = NULL;
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001057 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001058 DPRINTK("fault writing cmd\n");
1059 ret = -EFAULT;
1060 goto cleanup;
1061 }
1062 ret = -EAGAIN;
1063 goto cleanup;
1064 }
1065
1066 if (!async->prealloc_bufsz) {
1067 ret = -ENOMEM;
1068 DPRINTK("no buffer (?)\n");
1069 goto cleanup;
1070 }
1071
1072 comedi_reset_async_buf(async);
1073
1074 async->cb_mask =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001075 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1076 COMEDI_CB_OVERFLOW;
1077 if (async->cmd.flags & TRIG_WAKE_EOS)
David Schleefed9eccb2008-11-04 20:29:31 -08001078 async->cb_mask |= COMEDI_CB_EOS;
David Schleefed9eccb2008-11-04 20:29:31 -08001079
1080 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1081
1082#ifdef CONFIG_COMEDI_RT
1083 if (async->cmd.flags & TRIG_RT) {
1084 if (comedi_switch_to_rt(dev) == 0)
1085 comedi_set_subdevice_runflags(s, SRF_RT, SRF_RT);
1086 }
1087#endif
1088
1089 ret = s->do_cmd(dev, s);
1090 if (ret == 0)
1091 return 0;
1092
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001093cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -08001094 do_become_nonbusy(dev, s);
1095
1096 return ret;
1097}
1098
1099/*
1100 COMEDI_CMDTEST
1101 command testing ioctl
1102
1103 arg:
1104 pointer to cmd structure
1105
1106 reads:
1107 cmd structure at arg
1108 channel/range list
1109
1110 writes:
1111 modified cmd structure at arg
1112
1113*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001114static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001115{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001116 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001117 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001118 int ret = 0;
1119 unsigned int *chanlist = NULL;
1120 unsigned int *chanlist_saver = NULL;
1121
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001122 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001123 DPRINTK("bad cmd address\n");
1124 return -EFAULT;
1125 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001126 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001127 chanlist_saver = user_cmd.chanlist;
1128
1129 if (user_cmd.subdev >= dev->n_subdevices) {
1130 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1131 return -ENODEV;
1132 }
1133
1134 s = dev->subdevices + user_cmd.subdev;
1135 if (s->type == COMEDI_SUBD_UNUSED) {
1136 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1137 return -EIO;
1138 }
1139
1140 if (!s->do_cmd || !s->do_cmdtest) {
1141 DPRINTK("subdevice %i does not support commands\n",
1142 user_cmd.subdev);
1143 return -EIO;
1144 }
1145
1146 /* make sure channel/gain list isn't too long */
1147 if (user_cmd.chanlist_len > s->len_chanlist) {
1148 DPRINTK("channel/gain list too long %d > %d\n",
1149 user_cmd.chanlist_len, s->len_chanlist);
1150 ret = -EINVAL;
1151 goto cleanup;
1152 }
1153
1154 /* load channel/gain list */
1155 if (user_cmd.chanlist) {
1156 chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001157 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001158 if (!chanlist) {
1159 DPRINTK("allocation failed\n");
1160 ret = -ENOMEM;
1161 goto cleanup;
1162 }
1163
1164 if (copy_from_user(chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001165 user_cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001166 DPRINTK("fault reading chanlist\n");
1167 ret = -EFAULT;
1168 goto cleanup;
1169 }
1170
1171 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001172 ret = check_chanlist(s, user_cmd.chanlist_len, chanlist);
1173 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001174 DPRINTK("bad chanlist\n");
1175 goto cleanup;
1176 }
1177
1178 user_cmd.chanlist = chanlist;
1179 }
1180
1181 ret = s->do_cmdtest(dev, s, &user_cmd);
1182
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001183 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001184 user_cmd.chanlist = chanlist_saver;
1185
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001186 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001187 DPRINTK("bad cmd address\n");
1188 ret = -EFAULT;
1189 goto cleanup;
1190 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001191cleanup:
1192 kfree(chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001193
1194 return ret;
1195}
1196
1197/*
1198 COMEDI_LOCK
1199 lock subdevice
1200
1201 arg:
1202 subdevice number
1203
1204 reads:
1205 none
1206
1207 writes:
1208 none
1209
1210*/
1211
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001212static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001213{
1214 int ret = 0;
1215 unsigned long flags;
Bill Pemberton34c43922009-03-16 22:05:14 -04001216 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001217
1218 if (arg >= dev->n_subdevices)
1219 return -EINVAL;
1220 s = dev->subdevices + arg;
1221
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001222 spin_lock_irqsave(&s->spin_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001223 if (s->busy || s->lock)
David Schleefed9eccb2008-11-04 20:29:31 -08001224 ret = -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001225 else
David Schleefed9eccb2008-11-04 20:29:31 -08001226 s->lock = file;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001227 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08001228
1229 if (ret < 0)
1230 return ret;
1231
1232#if 0
1233 if (s->lock_f)
1234 ret = s->lock_f(dev, s);
1235#endif
1236
1237 return ret;
1238}
1239
1240/*
1241 COMEDI_UNLOCK
1242 unlock subdevice
1243
1244 arg:
1245 subdevice number
1246
1247 reads:
1248 none
1249
1250 writes:
1251 none
1252
1253 This function isn't protected by the semaphore, since
1254 we already own the lock.
1255*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001256static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001257{
Bill Pemberton34c43922009-03-16 22:05:14 -04001258 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001259
1260 if (arg >= dev->n_subdevices)
1261 return -EINVAL;
1262 s = dev->subdevices + arg;
1263
1264 if (s->busy)
1265 return -EBUSY;
1266
1267 if (s->lock && s->lock != file)
1268 return -EACCES;
1269
1270 if (s->lock == file) {
1271#if 0
1272 if (s->unlock)
1273 s->unlock(dev, s);
1274#endif
1275
1276 s->lock = NULL;
1277 }
1278
1279 return 0;
1280}
1281
1282/*
1283 COMEDI_CANCEL
1284 cancel acquisition ioctl
1285
1286 arg:
1287 subdevice number
1288
1289 reads:
1290 nothing
1291
1292 writes:
1293 nothing
1294
1295*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001296static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001297{
Bill Pemberton34c43922009-03-16 22:05:14 -04001298 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001299
1300 if (arg >= dev->n_subdevices)
1301 return -EINVAL;
1302 s = dev->subdevices + arg;
1303 if (s->async == NULL)
1304 return -EINVAL;
1305
1306 if (s->lock && s->lock != file)
1307 return -EACCES;
1308
1309 if (!s->busy)
1310 return 0;
1311
1312 if (s->busy != file)
1313 return -EBUSY;
1314
1315 return do_cancel(dev, s);
1316}
1317
1318/*
1319 COMEDI_POLL ioctl
1320 instructs driver to synchronize buffers
1321
1322 arg:
1323 subdevice number
1324
1325 reads:
1326 nothing
1327
1328 writes:
1329 nothing
1330
1331*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001332static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001333{
Bill Pemberton34c43922009-03-16 22:05:14 -04001334 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001335
1336 if (arg >= dev->n_subdevices)
1337 return -EINVAL;
1338 s = dev->subdevices + arg;
1339
1340 if (s->lock && s->lock != file)
1341 return -EACCES;
1342
1343 if (!s->busy)
1344 return 0;
1345
1346 if (s->busy != file)
1347 return -EBUSY;
1348
1349 if (s->poll)
1350 return s->poll(dev, s);
1351
1352 return -EINVAL;
1353}
1354
Bill Pemberton34c43922009-03-16 22:05:14 -04001355static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001356{
1357 int ret = 0;
1358
1359 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1360 ret = s->cancel(dev, s);
1361
1362 do_become_nonbusy(dev, s);
1363
1364 return ret;
1365}
1366
1367void comedi_unmap(struct vm_area_struct *area)
1368{
Bill Pembertond1636792009-03-16 22:05:20 -04001369 struct comedi_async *async;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001370 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -08001371
1372 async = area->vm_private_data;
1373 dev = async->subdevice->device;
1374
1375 mutex_lock(&dev->mutex);
1376 async->mmap_count--;
1377 mutex_unlock(&dev->mutex);
1378}
1379
1380static struct vm_operations_struct comedi_vm_ops = {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001381 .close = comedi_unmap,
David Schleefed9eccb2008-11-04 20:29:31 -08001382};
1383
1384static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1385{
1386 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001387 struct comedi_device_file_info *dev_file_info =
1388 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001389 struct comedi_device *dev = dev_file_info->device;
Bill Pembertond1636792009-03-16 22:05:20 -04001390 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001391 unsigned long start = vma->vm_start;
1392 unsigned long size;
1393 int n_pages;
1394 int i;
1395 int retval;
Bill Pemberton34c43922009-03-16 22:05:14 -04001396 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001397
1398 mutex_lock(&dev->mutex);
1399 if (!dev->attached) {
1400 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1401 retval = -ENODEV;
1402 goto done;
1403 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001404 if (vma->vm_flags & VM_WRITE)
David Schleefed9eccb2008-11-04 20:29:31 -08001405 s = comedi_get_write_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001406 else
David Schleefed9eccb2008-11-04 20:29:31 -08001407 s = comedi_get_read_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001408
David Schleefed9eccb2008-11-04 20:29:31 -08001409 if (s == NULL) {
1410 retval = -EINVAL;
1411 goto done;
1412 }
1413 async = s->async;
1414 if (async == NULL) {
1415 retval = -EINVAL;
1416 goto done;
1417 }
1418
1419 if (vma->vm_pgoff != 0) {
1420 DPRINTK("comedi: mmap() offset must be 0.\n");
1421 retval = -EINVAL;
1422 goto done;
1423 }
1424
1425 size = vma->vm_end - vma->vm_start;
1426 if (size > async->prealloc_bufsz) {
1427 retval = -EFAULT;
1428 goto done;
1429 }
1430 if (size & (~PAGE_MASK)) {
1431 retval = -EFAULT;
1432 goto done;
1433 }
1434
1435 n_pages = size >> PAGE_SHIFT;
1436 for (i = 0; i < n_pages; ++i) {
1437 if (remap_pfn_range(vma, start,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001438 page_to_pfn(virt_to_page(async->
1439 buf_page_list[i].
1440 virt_addr)),
1441 PAGE_SIZE, PAGE_SHARED)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001442 retval = -EAGAIN;
1443 goto done;
1444 }
1445 start += PAGE_SIZE;
1446 }
1447
1448 vma->vm_ops = &comedi_vm_ops;
1449 vma->vm_private_data = async;
1450
1451 async->mmap_count++;
1452
1453 retval = 0;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001454done:
David Schleefed9eccb2008-11-04 20:29:31 -08001455 mutex_unlock(&dev->mutex);
1456 return retval;
1457}
1458
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001459static unsigned int comedi_poll(struct file *file, poll_table *wait)
David Schleefed9eccb2008-11-04 20:29:31 -08001460{
1461 unsigned int mask = 0;
1462 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001463 struct comedi_device_file_info *dev_file_info =
1464 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001465 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001466 struct comedi_subdevice *read_subdev;
1467 struct comedi_subdevice *write_subdev;
David Schleefed9eccb2008-11-04 20:29:31 -08001468
1469 mutex_lock(&dev->mutex);
1470 if (!dev->attached) {
1471 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1472 mutex_unlock(&dev->mutex);
1473 return 0;
1474 }
1475
1476 mask = 0;
1477 read_subdev = comedi_get_read_subdevice(dev_file_info);
1478 if (read_subdev) {
1479 poll_wait(file, &read_subdev->async->wait_head, wait);
1480 if (!read_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001481 || comedi_buf_read_n_available(read_subdev->async) > 0
1482 || !(comedi_get_subdevice_runflags(read_subdev) &
1483 SRF_RUNNING)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001484 mask |= POLLIN | POLLRDNORM;
1485 }
1486 }
1487 write_subdev = comedi_get_write_subdevice(dev_file_info);
1488 if (write_subdev) {
1489 poll_wait(file, &write_subdev->async->wait_head, wait);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001490 comedi_buf_write_alloc(write_subdev->async,
1491 write_subdev->async->prealloc_bufsz);
David Schleefed9eccb2008-11-04 20:29:31 -08001492 if (!write_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001493 || !(comedi_get_subdevice_runflags(write_subdev) &
1494 SRF_RUNNING)
1495 || comedi_buf_write_n_allocated(write_subdev->async) >=
1496 bytes_per_sample(write_subdev->async->subdevice)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001497 mask |= POLLOUT | POLLWRNORM;
1498 }
1499 }
1500
1501 mutex_unlock(&dev->mutex);
1502 return mask;
1503}
1504
1505static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001506 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001507{
Bill Pemberton34c43922009-03-16 22:05:14 -04001508 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001509 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001510 int n, m, count = 0, retval = 0;
1511 DECLARE_WAITQUEUE(wait, current);
1512 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001513 struct comedi_device_file_info *dev_file_info =
1514 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001515 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001516
1517 if (!dev->attached) {
1518 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1519 retval = -ENODEV;
1520 goto done;
1521 }
1522
1523 s = comedi_get_write_subdevice(dev_file_info);
1524 if (s == NULL) {
1525 retval = -EIO;
1526 goto done;
1527 }
1528 async = s->async;
1529
1530 if (!nbytes) {
1531 retval = 0;
1532 goto done;
1533 }
1534 if (!s->busy) {
1535 retval = 0;
1536 goto done;
1537 }
1538 if (s->busy != file) {
1539 retval = -EACCES;
1540 goto done;
1541 }
1542 add_wait_queue(&async->wait_head, &wait);
1543 while (nbytes > 0 && !retval) {
1544 set_current_state(TASK_INTERRUPTIBLE);
1545
1546 n = nbytes;
1547
1548 m = n;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001549 if (async->buf_write_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001550 m = async->prealloc_bufsz - async->buf_write_ptr;
David Schleefed9eccb2008-11-04 20:29:31 -08001551 comedi_buf_write_alloc(async, async->prealloc_bufsz);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001552 if (m > comedi_buf_write_n_allocated(async))
David Schleefed9eccb2008-11-04 20:29:31 -08001553 m = comedi_buf_write_n_allocated(async);
David Schleefed9eccb2008-11-04 20:29:31 -08001554 if (m < n)
1555 n = m;
1556
1557 if (n == 0) {
1558 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1559 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001560 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001561 retval = -EPIPE;
1562 } else {
1563 retval = 0;
1564 }
1565 do_become_nonbusy(dev, s);
1566 break;
1567 }
1568 if (file->f_flags & O_NONBLOCK) {
1569 retval = -EAGAIN;
1570 break;
1571 }
1572 if (signal_pending(current)) {
1573 retval = -ERESTARTSYS;
1574 break;
1575 }
1576 schedule();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001577 if (!s->busy)
David Schleefed9eccb2008-11-04 20:29:31 -08001578 break;
David Schleefed9eccb2008-11-04 20:29:31 -08001579 if (s->busy != file) {
1580 retval = -EACCES;
1581 break;
1582 }
1583 continue;
1584 }
1585
1586 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001587 buf, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001588 if (m) {
1589 n -= m;
1590 retval = -EFAULT;
1591 }
1592 comedi_buf_write_free(async, n);
1593
1594 count += n;
1595 nbytes -= n;
1596
1597 buf += n;
1598 break; /* makes device work like a pipe */
1599 }
1600 set_current_state(TASK_RUNNING);
1601 remove_wait_queue(&async->wait_head, &wait);
1602
1603done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001604 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001605}
1606
1607static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001608 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001609{
Bill Pemberton34c43922009-03-16 22:05:14 -04001610 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001611 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001612 int n, m, count = 0, retval = 0;
1613 DECLARE_WAITQUEUE(wait, current);
1614 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001615 struct comedi_device_file_info *dev_file_info =
1616 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001617 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001618
1619 if (!dev->attached) {
1620 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1621 retval = -ENODEV;
1622 goto done;
1623 }
1624
1625 s = comedi_get_read_subdevice(dev_file_info);
1626 if (s == NULL) {
1627 retval = -EIO;
1628 goto done;
1629 }
1630 async = s->async;
1631 if (!nbytes) {
1632 retval = 0;
1633 goto done;
1634 }
1635 if (!s->busy) {
1636 retval = 0;
1637 goto done;
1638 }
1639 if (s->busy != file) {
1640 retval = -EACCES;
1641 goto done;
1642 }
1643
1644 add_wait_queue(&async->wait_head, &wait);
1645 while (nbytes > 0 && !retval) {
1646 set_current_state(TASK_INTERRUPTIBLE);
1647
1648 n = nbytes;
1649
1650 m = comedi_buf_read_n_available(async);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001651 /* printk("%d available\n",m); */
1652 if (async->buf_read_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001653 m = async->prealloc_bufsz - async->buf_read_ptr;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001654 /* printk("%d contiguous\n",m); */
David Schleefed9eccb2008-11-04 20:29:31 -08001655 if (m < n)
1656 n = m;
1657
1658 if (n == 0) {
1659 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1660 do_become_nonbusy(dev, s);
1661 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001662 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001663 retval = -EPIPE;
1664 } else {
1665 retval = 0;
1666 }
1667 break;
1668 }
1669 if (file->f_flags & O_NONBLOCK) {
1670 retval = -EAGAIN;
1671 break;
1672 }
1673 if (signal_pending(current)) {
1674 retval = -ERESTARTSYS;
1675 break;
1676 }
1677 schedule();
1678 if (!s->busy) {
1679 retval = 0;
1680 break;
1681 }
1682 if (s->busy != file) {
1683 retval = -EACCES;
1684 break;
1685 }
1686 continue;
1687 }
1688 m = copy_to_user(buf, async->prealloc_buf +
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001689 async->buf_read_ptr, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001690 if (m) {
1691 n -= m;
1692 retval = -EFAULT;
1693 }
1694
1695 comedi_buf_read_alloc(async, n);
1696 comedi_buf_read_free(async, n);
1697
1698 count += n;
1699 nbytes -= n;
1700
1701 buf += n;
1702 break; /* makes device work like a pipe */
1703 }
1704 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001705 async->buf_read_count - async->buf_write_count == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001706 do_become_nonbusy(dev, s);
1707 }
1708 set_current_state(TASK_RUNNING);
1709 remove_wait_queue(&async->wait_head, &wait);
1710
1711done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001712 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001713}
1714
1715/*
1716 This function restores a subdevice to an idle state.
1717 */
Bill Pemberton34c43922009-03-16 22:05:14 -04001718void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001719{
Bill Pembertond1636792009-03-16 22:05:20 -04001720 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001721
1722 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1723#ifdef CONFIG_COMEDI_RT
1724 if (comedi_get_subdevice_runflags(s) & SRF_RT) {
1725 comedi_switch_to_non_rt(dev);
1726 comedi_set_subdevice_runflags(s, SRF_RT, 0);
1727 }
1728#endif
1729 if (async) {
1730 comedi_reset_async_buf(async);
1731 async->inttrig = NULL;
1732 } else {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001733 printk(KERN_ERR
1734 "BUG: (?) do_become_nonbusy called with async=0\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001735 }
1736
1737 s->busy = NULL;
1738}
1739
1740static int comedi_open(struct inode *inode, struct file *file)
1741{
David Schleefed9eccb2008-11-04 20:29:31 -08001742 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001743 struct comedi_device_file_info *dev_file_info =
1744 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001745 struct comedi_device *dev = dev_file_info ? dev_file_info->device : NULL;
Ian Abbott97920072009-02-09 16:51:38 +00001746
David Schleefed9eccb2008-11-04 20:29:31 -08001747 if (dev == NULL) {
1748 DPRINTK("invalid minor number\n");
1749 return -ENODEV;
1750 }
1751
1752 /* This is slightly hacky, but we want module autoloading
1753 * to work for root.
1754 * case: user opens device, attached -> ok
1755 * case: user opens device, unattached, in_request_module=0 -> autoload
1756 * case: user opens device, unattached, in_request_module=1 -> fail
1757 * case: root opens device, attached -> ok
1758 * case: root opens device, unattached, in_request_module=1 -> ok
1759 * (typically called from modprobe)
1760 * case: root opens device, unattached, in_request_module=0 -> autoload
1761 *
1762 * The last could be changed to "-> ok", which would deny root
1763 * autoloading.
1764 */
1765 mutex_lock(&dev->mutex);
1766 if (dev->attached)
1767 goto ok;
1768 if (!capable(CAP_SYS_MODULE) && dev->in_request_module) {
1769 DPRINTK("in request module\n");
1770 mutex_unlock(&dev->mutex);
1771 return -ENODEV;
1772 }
1773 if (capable(CAP_SYS_MODULE) && dev->in_request_module)
1774 goto ok;
1775
1776 dev->in_request_module = 1;
1777
David Schleefed9eccb2008-11-04 20:29:31 -08001778#ifdef CONFIG_KMOD
1779 mutex_unlock(&dev->mutex);
Ian Abbott56d92c62009-02-09 16:32:12 +00001780 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
David Schleefed9eccb2008-11-04 20:29:31 -08001781 mutex_lock(&dev->mutex);
1782#endif
1783
1784 dev->in_request_module = 0;
1785
1786 if (!dev->attached && !capable(CAP_SYS_MODULE)) {
1787 DPRINTK("not attached and not CAP_SYS_MODULE\n");
1788 mutex_unlock(&dev->mutex);
1789 return -ENODEV;
1790 }
1791ok:
1792 __module_get(THIS_MODULE);
1793
1794 if (dev->attached) {
1795 if (!try_module_get(dev->driver->module)) {
1796 module_put(THIS_MODULE);
1797 mutex_unlock(&dev->mutex);
1798 return -ENOSYS;
1799 }
1800 }
1801
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001802 if (dev->attached && dev->use_count == 0 && dev->open)
David Schleefed9eccb2008-11-04 20:29:31 -08001803 dev->open(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001804
1805 dev->use_count++;
1806
1807 mutex_unlock(&dev->mutex);
1808
1809 return 0;
1810}
1811
1812static int comedi_close(struct inode *inode, struct file *file)
1813{
1814 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001815 struct comedi_device_file_info *dev_file_info =
1816 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001817 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001818 struct comedi_subdevice *s = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001819 int i;
1820
1821 mutex_lock(&dev->mutex);
1822
1823 if (dev->subdevices) {
1824 for (i = 0; i < dev->n_subdevices; i++) {
1825 s = dev->subdevices + i;
1826
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001827 if (s->busy == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001828 do_cancel(dev, s);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001829 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001830 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001831 }
1832 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001833 if (dev->attached && dev->use_count == 1 && dev->close)
David Schleefed9eccb2008-11-04 20:29:31 -08001834 dev->close(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001835
1836 module_put(THIS_MODULE);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001837 if (dev->attached)
David Schleefed9eccb2008-11-04 20:29:31 -08001838 module_put(dev->driver->module);
David Schleefed9eccb2008-11-04 20:29:31 -08001839
1840 dev->use_count--;
1841
1842 mutex_unlock(&dev->mutex);
1843
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001844 if (file->f_flags & FASYNC)
David Schleefed9eccb2008-11-04 20:29:31 -08001845 comedi_fasync(-1, file, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001846
1847 return 0;
1848}
1849
1850static int comedi_fasync(int fd, struct file *file, int on)
1851{
1852 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001853 struct comedi_device_file_info *dev_file_info =
1854 comedi_get_device_file_info(minor);
1855
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001856 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001857
1858 return fasync_helper(fd, file, on, &dev->async_queue);
1859}
1860
1861const struct file_operations comedi_fops = {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001862 .owner = THIS_MODULE,
David Schleefed9eccb2008-11-04 20:29:31 -08001863#ifdef HAVE_UNLOCKED_IOCTL
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001864 .unlocked_ioctl = comedi_unlocked_ioctl,
David Schleefed9eccb2008-11-04 20:29:31 -08001865#else
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001866 .ioctl = comedi_ioctl,
David Schleefed9eccb2008-11-04 20:29:31 -08001867#endif
1868#ifdef HAVE_COMPAT_IOCTL
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001869 .compat_ioctl = comedi_compat_ioctl,
David Schleefed9eccb2008-11-04 20:29:31 -08001870#endif
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001871 .open = comedi_open,
1872 .release = comedi_close,
1873 .read = comedi_read,
1874 .write = comedi_write,
1875 .mmap = comedi_mmap,
1876 .poll = comedi_poll,
1877 .fasync = comedi_fasync,
David Schleefed9eccb2008-11-04 20:29:31 -08001878};
1879
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001880struct class *comedi_class;
David Schleefed9eccb2008-11-04 20:29:31 -08001881static struct cdev comedi_cdev;
1882
1883static void comedi_cleanup_legacy_minors(void)
1884{
1885 unsigned i;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001886
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001887 for (i = 0; i < comedi_num_legacy_minors; i++)
David Schleefed9eccb2008-11-04 20:29:31 -08001888 comedi_free_board_minor(i);
David Schleefed9eccb2008-11-04 20:29:31 -08001889}
1890
1891static int __init comedi_init(void)
1892{
1893 int i;
1894 int retval;
1895
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001896 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1897 " - http://www.comedi.org\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001898
Frank Mori Hessa3cb7292008-12-15 13:44:45 +00001899 if (comedi_num_legacy_minors < 0 ||
1900 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
1901 printk(KERN_ERR "comedi: error: invalid value for module "
1902 "parameter \"comedi_num_legacy_minors\". Valid values "
1903 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
1904 return -EINVAL;
1905 }
1906
1907 /*
1908 * comedi is unusable if both comedi_autoconfig and
1909 * comedi_num_legacy_minors are zero, so we might as well adjust the
1910 * defaults in that case
1911 */
1912 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
1913 comedi_num_legacy_minors = 16;
1914
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001915 memset(comedi_file_info_table, 0,
1916 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001917
1918 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001919 COMEDI_NUM_MINORS, "comedi");
David Schleefed9eccb2008-11-04 20:29:31 -08001920 if (retval)
1921 return -EIO;
1922 cdev_init(&comedi_cdev, &comedi_fops);
1923 comedi_cdev.owner = THIS_MODULE;
1924 kobject_set_name(&comedi_cdev.kobj, "comedi");
1925 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1926 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001927 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001928 return -EIO;
1929 }
1930 comedi_class = class_create(THIS_MODULE, "comedi");
1931 if (IS_ERR(comedi_class)) {
1932 printk("comedi: failed to create class");
1933 cdev_del(&comedi_cdev);
1934 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001935 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001936 return PTR_ERR(comedi_class);
1937 }
1938
1939 /* XXX requires /proc interface */
1940 comedi_proc_init();
1941
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001942 /* create devices files for legacy/manual use */
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001943 for (i = 0; i < comedi_num_legacy_minors; i++) {
David Schleefed9eccb2008-11-04 20:29:31 -08001944 int minor;
1945 minor = comedi_alloc_board_minor(NULL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001946 if (minor < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001947 comedi_cleanup_legacy_minors();
1948 cdev_del(&comedi_cdev);
1949 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001950 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001951 return minor;
1952 }
1953 }
1954
1955 comedi_rt_init();
1956
1957 comedi_register_ioctl32();
1958
1959 return 0;
1960}
1961
1962static void __exit comedi_cleanup(void)
1963{
1964 int i;
1965
1966 comedi_cleanup_legacy_minors();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001967 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
David Schleefed9eccb2008-11-04 20:29:31 -08001968 BUG_ON(comedi_file_info_table[i]);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001969
David Schleefed9eccb2008-11-04 20:29:31 -08001970
1971 class_destroy(comedi_class);
1972 cdev_del(&comedi_cdev);
1973 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
1974
1975 comedi_proc_cleanup();
1976
1977 comedi_rt_cleanup();
1978
1979 comedi_unregister_ioctl32();
1980}
1981
1982module_init(comedi_init);
1983module_exit(comedi_cleanup);
1984
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001985void comedi_error(const struct comedi_device *dev, const char *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001986{
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001987 printk("comedi%d: %s: %s\n", dev->minor, dev->driver->driver_name, s);
David Schleefed9eccb2008-11-04 20:29:31 -08001988}
1989
Bill Pemberton34c43922009-03-16 22:05:14 -04001990void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001991{
Bill Pembertond1636792009-03-16 22:05:20 -04001992 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001993 unsigned runflags = 0;
1994 unsigned runflags_mask = 0;
1995
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001996 /* DPRINTK("comedi_event 0x%x\n",mask); */
David Schleefed9eccb2008-11-04 20:29:31 -08001997
1998 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
1999 return;
2000
2001 if (s->async->
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002002 events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
David Schleefed9eccb2008-11-04 20:29:31 -08002003 runflags_mask |= SRF_RUNNING;
2004 }
2005 /* remember if an error event has occured, so an error
2006 * can be returned the next time the user does a read() */
2007 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2008 runflags_mask |= SRF_ERROR;
2009 runflags |= SRF_ERROR;
2010 }
2011 if (runflags_mask) {
2012 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2013 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2014 }
2015
2016 if (async->cb_mask & s->async->events) {
2017 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2018
2019 if (dev->rt) {
2020#ifdef CONFIG_COMEDI_RT
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002021 /* pend wake up */
David Schleefed9eccb2008-11-04 20:29:31 -08002022 comedi_rt_pend_wakeup(&async->wait_head);
2023#else
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002024 printk
2025 ("BUG: comedi_event() code unreachable\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002026#endif
2027 } else {
2028 wake_up_interruptible(&async->wait_head);
2029 if (s->subdev_flags & SDF_CMD_READ) {
2030 kill_fasync(&dev->async_queue, SIGIO,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002031 POLL_IN);
David Schleefed9eccb2008-11-04 20:29:31 -08002032 }
2033 if (s->subdev_flags & SDF_CMD_WRITE) {
2034 kill_fasync(&dev->async_queue, SIGIO,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002035 POLL_OUT);
David Schleefed9eccb2008-11-04 20:29:31 -08002036 }
2037 }
2038 } else {
2039 if (async->cb_func)
2040 async->cb_func(s->async->events, async->cb_arg);
2041 /* XXX bug here. If subdevice A is rt, and
2042 * subdevice B tries to callback to a normal
2043 * linux kernel function, it will be at the
2044 * wrong priority. Since this isn't very
2045 * common, I'm not going to worry about it. */
2046 }
2047 }
2048 s->async->events = 0;
2049}
2050
Bill Pemberton34c43922009-03-16 22:05:14 -04002051void comedi_set_subdevice_runflags(struct comedi_subdevice *s, unsigned mask,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002052 unsigned bits)
David Schleefed9eccb2008-11-04 20:29:31 -08002053{
2054 unsigned long flags;
2055
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002056 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002057 s->runflags &= ~mask;
2058 s->runflags |= (bits & mask);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002059 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002060}
2061
Bill Pemberton34c43922009-03-16 22:05:14 -04002062unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002063{
2064 unsigned long flags;
2065 unsigned runflags;
2066
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002067 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002068 runflags = s->runflags;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002069 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002070 return runflags;
2071}
2072
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002073static int is_device_busy(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002074{
Bill Pemberton34c43922009-03-16 22:05:14 -04002075 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08002076 int i;
2077
2078 if (!dev->attached)
2079 return 0;
2080
2081 for (i = 0; i < dev->n_subdevices; i++) {
2082 s = dev->subdevices + i;
2083 if (s->busy)
2084 return 1;
2085 if (s->async && s->async->mmap_count)
2086 return 1;
2087 }
2088
2089 return 0;
2090}
2091
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002092void comedi_device_init(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002093{
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002094 memset(dev, 0, sizeof(struct comedi_device));
David Schleefed9eccb2008-11-04 20:29:31 -08002095 spin_lock_init(&dev->spinlock);
2096 mutex_init(&dev->mutex);
2097 dev->minor = -1;
2098}
2099
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002100void comedi_device_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002101{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002102 if (dev == NULL)
2103 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002104 mutex_lock(&dev->mutex);
2105 comedi_device_detach(dev);
2106 mutex_unlock(&dev->mutex);
2107 mutex_destroy(&dev->mutex);
2108}
2109
2110int comedi_alloc_board_minor(struct device *hardware_device)
2111{
2112 unsigned long flags;
2113 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002114 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002115 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002116 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002117
2118 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002119 if (info == NULL)
2120 return -ENOMEM;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002121 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002122 if (info->device == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002123 kfree(info);
2124 return -ENOMEM;
2125 }
2126 comedi_device_init(info->device);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002127 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002128 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2129 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002130 comedi_file_info_table[i] = info;
2131 break;
2132 }
2133 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002134 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002135 if (i == COMEDI_NUM_BOARD_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002136 comedi_device_cleanup(info->device);
2137 kfree(info->device);
2138 kfree(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002139 printk(KERN_ERR "comedi: error: ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002140 return -EBUSY;
2141 }
2142 info->device->minor = i;
2143 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002144 MKDEV(COMEDI_MAJOR, i), NULL,
2145 hardware_device, "comedi%i", i);
2146 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002147 info->device->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002148 dev_set_drvdata(csdev, info);
2149 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2150 if (retval) {
2151 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n",
2152 dev_attr_max_read_buffer_kb.attr.name);
2153 comedi_free_board_minor(i);
2154 return retval;
2155 }
2156 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2157 if (retval) {
2158 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n",
2159 dev_attr_read_buffer_kb.attr.name);
2160 comedi_free_board_minor(i);
2161 return retval;
2162 }
2163 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2164 if (retval) {
2165 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n",
2166 dev_attr_max_write_buffer_kb.attr.name);
2167 comedi_free_board_minor(i);
2168 return retval;
2169 }
2170 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2171 if (retval) {
2172 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n",
2173 dev_attr_write_buffer_kb.attr.name);
2174 comedi_free_board_minor(i);
2175 return retval;
2176 }
David Schleefed9eccb2008-11-04 20:29:31 -08002177 return i;
2178}
2179
2180void comedi_free_board_minor(unsigned minor)
2181{
2182 unsigned long flags;
2183 struct comedi_device_file_info *info;
2184
2185 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002186 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002187 info = comedi_file_info_table[minor];
2188 comedi_file_info_table[minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002189 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002190
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002191 if (info) {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002192 struct comedi_device *dev = info->device;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002193 if (dev) {
2194 if (dev->class_dev) {
2195 device_destroy(comedi_class,
2196 MKDEV(COMEDI_MAJOR, dev->minor));
David Schleefed9eccb2008-11-04 20:29:31 -08002197 }
2198 comedi_device_cleanup(dev);
2199 kfree(dev);
2200 }
2201 kfree(info);
2202 }
2203}
2204
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002205int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2206 struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002207{
2208 unsigned long flags;
2209 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002210 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002211 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002212 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002213
2214 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002215 if (info == NULL)
2216 return -ENOMEM;
David Schleefed9eccb2008-11-04 20:29:31 -08002217 info->device = dev;
2218 info->read_subdevice = s;
2219 info->write_subdevice = s;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002220 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Frank Mori Hess4c41f3a2008-12-09 14:47:22 +00002221 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002222 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002223 comedi_file_info_table[i] = info;
2224 break;
2225 }
2226 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002227 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002228 if (i == COMEDI_NUM_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002229 kfree(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002230 printk(KERN_ERR "comedi: error: ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002231 return -EBUSY;
2232 }
2233 s->minor = i;
2234 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002235 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2236 "comedi%i_subd%i", dev->minor,
2237 (int)(s - dev->subdevices));
2238 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002239 s->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002240 dev_set_drvdata(csdev, info);
2241 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2242 if (retval) {
2243 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n",
2244 dev_attr_max_read_buffer_kb.attr.name);
2245 comedi_free_subdevice_minor(s);
2246 return retval;
2247 }
2248 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2249 if (retval) {
2250 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n",
2251 dev_attr_read_buffer_kb.attr.name);
2252 comedi_free_subdevice_minor(s);
2253 return retval;
2254 }
2255 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2256 if (retval) {
2257 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n",
2258 dev_attr_max_write_buffer_kb.attr.name);
2259 comedi_free_subdevice_minor(s);
2260 return retval;
2261 }
2262 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2263 if (retval) {
2264 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n",
2265 dev_attr_write_buffer_kb.attr.name);
2266 comedi_free_subdevice_minor(s);
2267 return retval;
2268 }
David Schleefed9eccb2008-11-04 20:29:31 -08002269 return i;
2270}
2271
Bill Pemberton34c43922009-03-16 22:05:14 -04002272void comedi_free_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002273{
2274 unsigned long flags;
2275 struct comedi_device_file_info *info;
2276
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002277 if (s == NULL)
2278 return;
2279 if (s->minor < 0)
2280 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002281
2282 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2283 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2284
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002285 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002286 info = comedi_file_info_table[s->minor];
2287 comedi_file_info_table[s->minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002288 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002289
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002290 if (s->class_dev) {
David Schleefed9eccb2008-11-04 20:29:31 -08002291 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2292 s->class_dev = NULL;
2293 }
2294 kfree(info);
2295}
2296
2297struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2298{
2299 unsigned long flags;
2300 struct comedi_device_file_info *info;
2301
2302 BUG_ON(minor >= COMEDI_NUM_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002303 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002304 info = comedi_file_info_table[minor];
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002305 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002306 return info;
2307}
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002308
2309static int resize_async_buffer(struct comedi_device *dev,
2310 struct comedi_subdevice *s,
2311 struct comedi_async *async, unsigned new_size)
2312{
2313 int retval;
2314
2315 if (new_size > async->max_bufsize)
2316 return -EPERM;
2317
2318 if (s->busy) {
2319 DPRINTK("subdevice is busy, cannot resize buffer\n");
2320 return -EBUSY;
2321 }
2322 if (async->mmap_count) {
2323 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2324 return -EBUSY;
2325 }
2326
2327 if (!async->prealloc_buf)
2328 return -EINVAL;
2329
2330 /* make sure buffer is an integral number of pages
2331 * (we round up) */
2332 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2333
2334 retval = comedi_buf_alloc(dev, s, new_size);
2335 if (retval < 0)
2336 return retval;
2337
2338 if (s->buf_change) {
2339 retval = s->buf_change(dev, s, new_size);
2340 if (retval < 0)
2341 return retval;
2342 }
2343
2344 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2345 dev->minor, s - dev->subdevices, async->prealloc_bufsz);
2346 return 0;
2347}
2348
2349/* sysfs attribute files */
2350
2351static const unsigned bytes_per_kibi = 1024;
2352
2353static ssize_t show_max_read_buffer_kb(struct device *dev,
2354 struct device_attribute *attr, char *buf)
2355{
2356 ssize_t retval;
2357 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2358 unsigned max_buffer_size_kb = 0;
2359 struct comedi_subdevice *const read_subdevice =
2360 comedi_get_read_subdevice(info);
2361
2362 mutex_lock(&info->device->mutex);
2363 if (read_subdevice &&
2364 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2365 read_subdevice->async) {
2366 max_buffer_size_kb = read_subdevice->async->max_bufsize /
2367 bytes_per_kibi;
2368 }
2369 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2370 mutex_unlock(&info->device->mutex);
2371
2372 return retval;
2373}
2374
2375static ssize_t store_max_read_buffer_kb(struct device *dev,
2376 struct device_attribute *attr,
2377 const char *buf, size_t count)
2378{
2379 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2380 unsigned long new_max_size_kb;
2381 uint64_t new_max_size;
2382 struct comedi_subdevice *const read_subdevice =
2383 comedi_get_read_subdevice(info);
2384
2385 if (strict_strtoul(buf, 10, &new_max_size_kb))
2386 return -EINVAL;
2387 if (new_max_size_kb != (uint32_t)new_max_size_kb)
2388 return -EINVAL;
2389 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2390 if (new_max_size != (uint32_t)new_max_size)
2391 return -EINVAL;
2392
2393 mutex_lock(&info->device->mutex);
2394 if (read_subdevice == NULL ||
2395 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2396 read_subdevice->async == NULL) {
2397 mutex_unlock(&info->device->mutex);
2398 return -EINVAL;
2399 }
2400 read_subdevice->async->max_bufsize = new_max_size;
2401 mutex_unlock(&info->device->mutex);
2402
2403 return count;
2404}
2405
2406static struct device_attribute dev_attr_max_read_buffer_kb = {
2407 .attr = {
2408 .name = "max_read_buffer_kb",
2409 .mode = S_IRUGO | S_IWUSR
2410 },
2411 .show = &show_max_read_buffer_kb,
2412 .store = &store_max_read_buffer_kb
2413};
2414
2415static ssize_t show_read_buffer_kb(struct device *dev,
2416 struct device_attribute *attr, char *buf)
2417{
2418 ssize_t retval;
2419 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2420 unsigned buffer_size_kb = 0;
2421 struct comedi_subdevice *const read_subdevice =
2422 comedi_get_read_subdevice(info);
2423
2424 mutex_lock(&info->device->mutex);
2425 if (read_subdevice &&
2426 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2427 read_subdevice->async) {
2428 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
2429 bytes_per_kibi;
2430 }
2431 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2432 mutex_unlock(&info->device->mutex);
2433
2434 return retval;
2435}
2436
2437static ssize_t store_read_buffer_kb(struct device *dev,
2438 struct device_attribute *attr,
2439 const char *buf, size_t count)
2440{
2441 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2442 unsigned long new_size_kb;
2443 uint64_t new_size;
2444 int retval;
2445 struct comedi_subdevice *const read_subdevice =
2446 comedi_get_read_subdevice(info);
2447
2448 if (strict_strtoul(buf, 10, &new_size_kb))
2449 return -EINVAL;
2450 if (new_size_kb != (uint32_t)new_size_kb)
2451 return -EINVAL;
2452 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2453 if (new_size != (uint32_t)new_size)
2454 return -EINVAL;
2455
2456 mutex_lock(&info->device->mutex);
2457 if (read_subdevice == NULL ||
2458 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2459 read_subdevice->async == NULL) {
2460 mutex_unlock(&info->device->mutex);
2461 return -EINVAL;
2462 }
2463 retval = resize_async_buffer(info->device, read_subdevice,
2464 read_subdevice->async, new_size);
2465 mutex_unlock(&info->device->mutex);
2466
2467 if (retval < 0)
2468 return retval;
2469 return count;
2470}
2471
2472static struct device_attribute dev_attr_read_buffer_kb = {
2473 .attr = {
2474 .name = "read_buffer_kb",
2475 .mode = S_IRUGO | S_IWUSR | S_IWGRP
2476 },
2477 .show = &show_read_buffer_kb,
2478 .store = &store_read_buffer_kb
2479};
2480
2481static ssize_t show_max_write_buffer_kb(struct device *dev,
2482 struct device_attribute *attr,
2483 char *buf)
2484{
2485 ssize_t retval;
2486 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2487 unsigned max_buffer_size_kb = 0;
2488 struct comedi_subdevice *const write_subdevice =
2489 comedi_get_write_subdevice(info);
2490
2491 mutex_lock(&info->device->mutex);
2492 if (write_subdevice &&
2493 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2494 write_subdevice->async) {
2495 max_buffer_size_kb = write_subdevice->async->max_bufsize /
2496 bytes_per_kibi;
2497 }
2498 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2499 mutex_unlock(&info->device->mutex);
2500
2501 return retval;
2502}
2503
2504static ssize_t store_max_write_buffer_kb(struct device *dev,
2505 struct device_attribute *attr,
2506 const char *buf, size_t count)
2507{
2508 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2509 unsigned long new_max_size_kb;
2510 uint64_t new_max_size;
2511 struct comedi_subdevice *const write_subdevice =
2512 comedi_get_write_subdevice(info);
2513
2514 if (strict_strtoul(buf, 10, &new_max_size_kb))
2515 return -EINVAL;
2516 if (new_max_size_kb != (uint32_t)new_max_size_kb)
2517 return -EINVAL;
2518 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2519 if (new_max_size != (uint32_t)new_max_size)
2520 return -EINVAL;
2521
2522 mutex_lock(&info->device->mutex);
2523 if (write_subdevice == NULL ||
2524 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2525 write_subdevice->async == NULL) {
2526 mutex_unlock(&info->device->mutex);
2527 return -EINVAL;
2528 }
2529 write_subdevice->async->max_bufsize = new_max_size;
2530 mutex_unlock(&info->device->mutex);
2531
2532 return count;
2533}
2534
2535static struct device_attribute dev_attr_max_write_buffer_kb = {
2536 .attr = {
2537 .name = "max_write_buffer_kb",
2538 .mode = S_IRUGO | S_IWUSR
2539 },
2540 .show = &show_max_write_buffer_kb,
2541 .store = &store_max_write_buffer_kb
2542};
2543
2544static ssize_t show_write_buffer_kb(struct device *dev,
2545 struct device_attribute *attr, char *buf)
2546{
2547 ssize_t retval;
2548 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2549 unsigned buffer_size_kb = 0;
2550 struct comedi_subdevice *const write_subdevice =
2551 comedi_get_write_subdevice(info);
2552
2553 mutex_lock(&info->device->mutex);
2554 if (write_subdevice &&
2555 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2556 write_subdevice->async) {
2557 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
2558 bytes_per_kibi;
2559 }
2560 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2561 mutex_unlock(&info->device->mutex);
2562
2563 return retval;
2564}
2565
2566static ssize_t store_write_buffer_kb(struct device *dev,
2567 struct device_attribute *attr,
2568 const char *buf, size_t count)
2569{
2570 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2571 unsigned long new_size_kb;
2572 uint64_t new_size;
2573 int retval;
2574 struct comedi_subdevice *const write_subdevice =
2575 comedi_get_write_subdevice(info);
2576
2577 if (strict_strtoul(buf, 10, &new_size_kb))
2578 return -EINVAL;
2579 if (new_size_kb != (uint32_t)new_size_kb)
2580 return -EINVAL;
2581 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2582 if (new_size != (uint32_t)new_size)
2583 return -EINVAL;
2584
2585 mutex_lock(&info->device->mutex);
2586 if (write_subdevice == NULL ||
2587 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2588 write_subdevice->async == NULL) {
2589 mutex_unlock(&info->device->mutex);
2590 return -EINVAL;
2591 }
2592 retval = resize_async_buffer(info->device, write_subdevice,
2593 write_subdevice->async, new_size);
2594 mutex_unlock(&info->device->mutex);
2595
2596 if (retval < 0)
2597 return retval;
2598 return count;
2599}
2600
2601static struct device_attribute dev_attr_write_buffer_kb = {
2602 .attr = {
2603 .name = "write_buffer_kb",
2604 .mode = S_IRUGO | S_IWUSR | S_IWGRP
2605 },
2606 .show = &show_write_buffer_kb,
2607 .store = &store_write_buffer_kb
2608};