blob: 4fe3d0c2e6bf7ff8a5a7e5a4910aecde429e0160 [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;
Greg Kroah-Hartman18736432010-05-01 12:02:23 -070060EXPORT_SYMBOL(comedi_debug);
David Schleefed9eccb2008-11-04 20:29:31 -080061module_param(comedi_debug, int, 0644);
62#endif
63
Ian Abbott6a9d7a22008-12-08 17:05:50 +000064int comedi_autoconfig = 1;
65module_param(comedi_autoconfig, bool, 0444);
66
Andrea Gelmini6705b682010-02-26 10:14:55 +010067int comedi_num_legacy_minors;
Bernd Porr1dd33ab2008-12-08 23:30:13 +000068module_param(comedi_num_legacy_minors, int, 0444);
69
David Schleefed9eccb2008-11-04 20:29:31 -080070static DEFINE_SPINLOCK(comedi_file_info_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080071static struct comedi_device_file_info
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053072*comedi_file_info_table[COMEDI_NUM_MINORS];
David Schleefed9eccb2008-11-04 20:29:31 -080073
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053074static int do_devconfig_ioctl(struct comedi_device *dev,
75 struct comedi_devconfig *arg);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040076static int do_bufconfig_ioctl(struct comedi_device *dev, void *arg);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053077static int do_devinfo_ioctl(struct comedi_device *dev,
78 struct comedi_devinfo *arg, struct file *file);
79static int do_subdinfo_ioctl(struct comedi_device *dev,
80 struct comedi_subdinfo *arg, void *file);
81static int do_chaninfo_ioctl(struct comedi_device *dev,
82 struct comedi_chaninfo *arg);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040083static int do_bufinfo_ioctl(struct comedi_device *dev, void *arg);
84static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053085static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
86 void *file);
87static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
88 void *file);
89static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
90 void *file);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040091static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file);
92static int do_insnlist_ioctl(struct comedi_device *dev, void *arg, void *file);
93static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053094static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
95 void *file);
David Schleefed9eccb2008-11-04 20:29:31 -080096
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053097extern void do_become_nonbusy(struct comedi_device *dev,
98 struct comedi_subdevice *s);
Bill Pemberton34c43922009-03-16 22:05:14 -040099static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
David Schleefed9eccb2008-11-04 20:29:31 -0800100
101static int comedi_fasync(int fd, struct file *file, int on);
102
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400103static int is_device_busy(struct comedi_device *dev);
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400104static int resize_async_buffer(struct comedi_device *dev,
105 struct comedi_subdevice *s,
106 struct comedi_async *async, unsigned new_size);
107
108/* declarations for sysfs attribute files */
109static struct device_attribute dev_attr_max_read_buffer_kb;
110static struct device_attribute dev_attr_read_buffer_kb;
111static struct device_attribute dev_attr_max_write_buffer_kb;
112static struct device_attribute dev_attr_write_buffer_kb;
David Schleefed9eccb2008-11-04 20:29:31 -0800113
David Schleefed9eccb2008-11-04 20:29:31 -0800114static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800115 unsigned long arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800116{
117 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800118 struct comedi_device_file_info *dev_file_info =
119 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400120 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800121 int rc;
122
Frank Mori Hess53b670a2008-12-15 13:48:47 +0000123 if (dev_file_info == NULL || dev_file_info->device == NULL)
124 return -ENODEV;
125 dev = dev_file_info->device;
126
David Schleefed9eccb2008-11-04 20:29:31 -0800127 mutex_lock(&dev->mutex);
128
129 /* Device config is special, because it must work on
130 * an unconfigured device. */
131 if (cmd == COMEDI_DEVCONFIG) {
132 rc = do_devconfig_ioctl(dev, (void *)arg);
133 goto done;
134 }
135
136 if (!dev->attached) {
137 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
138 rc = -ENODEV;
139 goto done;
140 }
141
142 switch (cmd) {
143 case COMEDI_BUFCONFIG:
144 rc = do_bufconfig_ioctl(dev, (void *)arg);
145 break;
146 case COMEDI_DEVINFO:
147 rc = do_devinfo_ioctl(dev, (void *)arg, file);
148 break;
149 case COMEDI_SUBDINFO:
150 rc = do_subdinfo_ioctl(dev, (void *)arg, file);
151 break;
152 case COMEDI_CHANINFO:
153 rc = do_chaninfo_ioctl(dev, (void *)arg);
154 break;
155 case COMEDI_RANGEINFO:
156 rc = do_rangeinfo_ioctl(dev, (void *)arg);
157 break;
158 case COMEDI_BUFINFO:
159 rc = do_bufinfo_ioctl(dev, (void *)arg);
160 break;
161 case COMEDI_LOCK:
162 rc = do_lock_ioctl(dev, arg, file);
163 break;
164 case COMEDI_UNLOCK:
165 rc = do_unlock_ioctl(dev, arg, file);
166 break;
167 case COMEDI_CANCEL:
168 rc = do_cancel_ioctl(dev, arg, file);
169 break;
170 case COMEDI_CMD:
171 rc = do_cmd_ioctl(dev, (void *)arg, file);
172 break;
173 case COMEDI_CMDTEST:
174 rc = do_cmdtest_ioctl(dev, (void *)arg, file);
175 break;
176 case COMEDI_INSNLIST:
177 rc = do_insnlist_ioctl(dev, (void *)arg, file);
178 break;
179 case COMEDI_INSN:
180 rc = do_insn_ioctl(dev, (void *)arg, file);
181 break;
182 case COMEDI_POLL:
183 rc = do_poll_ioctl(dev, arg, file);
184 break;
185 default:
186 rc = -ENOTTY;
187 break;
188 }
189
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800190done:
David Schleefed9eccb2008-11-04 20:29:31 -0800191 mutex_unlock(&dev->mutex);
192 return rc;
193}
194
195/*
196 COMEDI_DEVCONFIG
197 device config ioctl
198
199 arg:
200 pointer to devconfig structure
201
202 reads:
203 devconfig structure at arg
204
205 writes:
206 none
207*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530208static int do_devconfig_ioctl(struct comedi_device *dev,
209 struct comedi_devconfig *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800210{
Bill Pemberton0707bb02009-03-16 22:06:20 -0400211 struct comedi_devconfig it;
David Schleefed9eccb2008-11-04 20:29:31 -0800212 int ret;
213 unsigned char *aux_data = NULL;
214 int aux_len;
215
216 if (!capable(CAP_SYS_ADMIN))
217 return -EPERM;
218
219 if (arg == NULL) {
220 if (is_device_busy(dev))
221 return -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800222 if (dev->attached) {
David Schleefed9eccb2008-11-04 20:29:31 -0800223 struct module *driver_module = dev->driver->module;
224 comedi_device_detach(dev);
225 module_put(driver_module);
226 }
227 return 0;
228 }
229
Bill Pemberton0707bb02009-03-16 22:06:20 -0400230 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800231 return -EFAULT;
232
233 it.board_name[COMEDI_NAMELEN - 1] = 0;
234
235 if (comedi_aux_data(it.options, 0) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800236 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
David Schleefed9eccb2008-11-04 20:29:31 -0800237 int bit_shift;
238 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
239 if (aux_len < 0)
240 return -EFAULT;
241
242 aux_data = vmalloc(aux_len);
243 if (!aux_data)
244 return -ENOMEM;
245
246 if (copy_from_user(aux_data,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800247 comedi_aux_data(it.options, 0), aux_len)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800248 vfree(aux_data);
249 return -EFAULT;
250 }
251 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800252 (unsigned long)aux_data;
David Schleefed9eccb2008-11-04 20:29:31 -0800253 if (sizeof(void *) > sizeof(int)) {
254 bit_shift = sizeof(int) * 8;
255 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800256 ((unsigned long)aux_data) >> bit_shift;
David Schleefed9eccb2008-11-04 20:29:31 -0800257 } else
258 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
259 }
260
261 ret = comedi_device_attach(dev, &it);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800262 if (ret == 0) {
263 if (!try_module_get(dev->driver->module)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800264 comedi_device_detach(dev);
265 return -ENOSYS;
266 }
267 }
268
269 if (aux_data)
270 vfree(aux_data);
271
272 return ret;
273}
274
275/*
276 COMEDI_BUFCONFIG
277 buffer configuration ioctl
278
279 arg:
280 pointer to bufconfig structure
281
282 reads:
283 bufconfig at arg
284
285 writes:
286 modified bufconfig at arg
287
288*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400289static int do_bufconfig_ioctl(struct comedi_device *dev, void *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800290{
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400291 struct comedi_bufconfig bc;
Bill Pembertond1636792009-03-16 22:05:20 -0400292 struct comedi_async *async;
Bill Pemberton34c43922009-03-16 22:05:14 -0400293 struct comedi_subdevice *s;
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400294 int retval = 0;
David Schleefed9eccb2008-11-04 20:29:31 -0800295
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400296 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800297 return -EFAULT;
298
299 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
300 return -EINVAL;
301
302 s = dev->subdevices + bc.subdevice;
303 async = s->async;
304
305 if (!async) {
306 DPRINTK("subdevice does not have async capability\n");
307 bc.size = 0;
308 bc.maximum_size = 0;
309 goto copyback;
310 }
311
312 if (bc.maximum_size) {
313 if (!capable(CAP_SYS_ADMIN))
314 return -EPERM;
315
316 async->max_bufsize = bc.maximum_size;
317 }
318
319 if (bc.size) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400320 retval = resize_async_buffer(dev, s, async, bc.size);
321 if (retval < 0)
322 return retval;
David Schleefed9eccb2008-11-04 20:29:31 -0800323 }
324
325 bc.size = async->prealloc_bufsz;
326 bc.maximum_size = async->max_bufsize;
327
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800328copyback:
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400329 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800330 return -EFAULT;
331
332 return 0;
333}
334
335/*
336 COMEDI_DEVINFO
337 device info ioctl
338
339 arg:
340 pointer to devinfo structure
341
342 reads:
343 none
344
345 writes:
346 devinfo structure
347
348*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530349static int do_devinfo_ioctl(struct comedi_device *dev,
350 struct comedi_devinfo *arg, struct file *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800351{
Bill Pemberton063db042009-03-16 22:06:15 -0400352 struct comedi_devinfo devinfo;
David Schleefed9eccb2008-11-04 20:29:31 -0800353 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800354 struct comedi_device_file_info *dev_file_info =
355 comedi_get_device_file_info(minor);
Bill Pemberton34c43922009-03-16 22:05:14 -0400356 struct comedi_subdevice *read_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800357 comedi_get_read_subdevice(dev_file_info);
Bill Pemberton34c43922009-03-16 22:05:14 -0400358 struct comedi_subdevice *write_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800359 comedi_get_write_subdevice(dev_file_info);
David Schleefed9eccb2008-11-04 20:29:31 -0800360
361 memset(&devinfo, 0, sizeof(devinfo));
362
363 /* fill devinfo structure */
364 devinfo.version_code = COMEDI_VERSION_CODE;
365 devinfo.n_subdevs = dev->n_subdevices;
366 memcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
367 memcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
368
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800369 if (read_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800370 devinfo.read_subdevice = read_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800371 else
David Schleefed9eccb2008-11-04 20:29:31 -0800372 devinfo.read_subdevice = -1;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800373
374 if (write_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800375 devinfo.write_subdevice = write_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800376 else
David Schleefed9eccb2008-11-04 20:29:31 -0800377 devinfo.write_subdevice = -1;
David Schleefed9eccb2008-11-04 20:29:31 -0800378
Bill Pemberton063db042009-03-16 22:06:15 -0400379 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800380 return -EFAULT;
381
382 return 0;
383}
384
385/*
386 COMEDI_SUBDINFO
387 subdevice info ioctl
388
389 arg:
390 pointer to array of subdevice info structures
391
392 reads:
393 none
394
395 writes:
396 array of subdevice info structures at arg
397
398*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530399static int do_subdinfo_ioctl(struct comedi_device *dev,
400 struct comedi_subdinfo *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800401{
402 int ret, i;
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400403 struct comedi_subdinfo *tmp, *us;
Bill Pemberton34c43922009-03-16 22:05:14 -0400404 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800405
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530406 tmp =
407 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
408 GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800409 if (!tmp)
410 return -ENOMEM;
411
412 /* fill subdinfo structs */
413 for (i = 0; i < dev->n_subdevices; i++) {
414 s = dev->subdevices + i;
415 us = tmp + i;
416
417 us->type = s->type;
418 us->n_chan = s->n_chan;
419 us->subd_flags = s->subdev_flags;
420 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
421 us->subd_flags |= SDF_RUNNING;
422#define TIMER_nanosec 5 /* backwards compatibility */
423 us->timer_type = TIMER_nanosec;
424 us->len_chanlist = s->len_chanlist;
425 us->maxdata = s->maxdata;
426 if (s->range_table) {
427 us->range_type =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800428 (i << 24) | (0 << 16) | (s->range_table->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800429 } else {
430 us->range_type = 0; /* XXX */
431 }
432 us->flags = s->flags;
433
434 if (s->busy)
435 us->subd_flags |= SDF_BUSY;
436 if (s->busy == file)
437 us->subd_flags |= SDF_BUSY_OWNER;
438 if (s->lock)
439 us->subd_flags |= SDF_LOCKED;
440 if (s->lock == file)
441 us->subd_flags |= SDF_LOCK_OWNER;
442 if (!s->maxdata && s->maxdata_list)
443 us->subd_flags |= SDF_MAXDATA;
444 if (s->flaglist)
445 us->subd_flags |= SDF_FLAGS;
446 if (s->range_table_list)
447 us->subd_flags |= SDF_RANGETYPE;
448 if (s->do_cmd)
449 us->subd_flags |= SDF_CMD;
450
451 if (s->insn_bits != &insn_inval)
452 us->insn_bits_support = COMEDI_SUPPORTED;
453 else
454 us->insn_bits_support = COMEDI_UNSUPPORTED;
455
456 us->settling_time_0 = s->settling_time_0;
457 }
458
459 ret = copy_to_user(arg, tmp,
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400460 dev->n_subdevices * sizeof(struct comedi_subdinfo));
David Schleefed9eccb2008-11-04 20:29:31 -0800461
462 kfree(tmp);
463
464 return ret ? -EFAULT : 0;
465}
466
467/*
468 COMEDI_CHANINFO
469 subdevice info ioctl
470
471 arg:
472 pointer to chaninfo structure
473
474 reads:
475 chaninfo structure at arg
476
477 writes:
478 arrays at elements of chaninfo structure
479
480*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530481static int do_chaninfo_ioctl(struct comedi_device *dev,
482 struct comedi_chaninfo *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800483{
Bill Pemberton34c43922009-03-16 22:05:14 -0400484 struct comedi_subdevice *s;
Bill Pembertona18b4162009-03-16 22:06:04 -0400485 struct comedi_chaninfo it;
David Schleefed9eccb2008-11-04 20:29:31 -0800486
Bill Pembertona18b4162009-03-16 22:06:04 -0400487 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800488 return -EFAULT;
489
490 if (it.subdev >= dev->n_subdevices)
491 return -EINVAL;
492 s = dev->subdevices + it.subdev;
493
494 if (it.maxdata_list) {
495 if (s->maxdata || !s->maxdata_list)
496 return -EINVAL;
497 if (copy_to_user(it.maxdata_list, s->maxdata_list,
Bill Pemberton790c5542009-03-16 22:05:02 -0400498 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800499 return -EFAULT;
500 }
501
502 if (it.flaglist) {
503 if (!s->flaglist)
504 return -EINVAL;
505 if (copy_to_user(it.flaglist, s->flaglist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800506 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800507 return -EFAULT;
508 }
509
510 if (it.rangelist) {
511 int i;
512
513 if (!s->range_table_list)
514 return -EINVAL;
515 for (i = 0; i < s->n_chan; i++) {
516 int x;
517
518 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800519 (s->range_table_list[i]->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800520 put_user(x, it.rangelist + i);
521 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800522#if 0
523 if (copy_to_user(it.rangelist, s->range_type_list,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530524 s->n_chan * sizeof(unsigned int)))
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800525 return -EFAULT;
526#endif
David Schleefed9eccb2008-11-04 20:29:31 -0800527 }
528
529 return 0;
530}
531
532 /*
533 COMEDI_BUFINFO
534 buffer information ioctl
535
536 arg:
537 pointer to bufinfo structure
538
539 reads:
540 bufinfo at arg
541
542 writes:
543 modified bufinfo at arg
544
545 */
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400546static int do_bufinfo_ioctl(struct comedi_device *dev, void *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800547{
Bill Pemberton9aa53392009-03-16 22:06:42 -0400548 struct comedi_bufinfo bi;
Bill Pemberton34c43922009-03-16 22:05:14 -0400549 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400550 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800551
Bill Pemberton9aa53392009-03-16 22:06:42 -0400552 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800553 return -EFAULT;
554
555 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
556 return -EINVAL;
557
558 s = dev->subdevices + bi.subdevice;
559 async = s->async;
560
561 if (!async) {
562 DPRINTK("subdevice does not have async capability\n");
563 bi.buf_write_ptr = 0;
564 bi.buf_read_ptr = 0;
565 bi.buf_write_count = 0;
566 bi.buf_read_count = 0;
567 goto copyback;
568 }
569
570 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
571 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
572 comedi_buf_read_free(async, bi.bytes_read);
573
574 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800575 SRF_RUNNING))
576 && async->buf_write_count == async->buf_read_count) {
David Schleefed9eccb2008-11-04 20:29:31 -0800577 do_become_nonbusy(dev, s);
578 }
579 }
580
581 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
582 bi.bytes_written =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800583 comedi_buf_write_alloc(async, bi.bytes_written);
David Schleefed9eccb2008-11-04 20:29:31 -0800584 comedi_buf_write_free(async, bi.bytes_written);
585 }
586
587 bi.buf_write_count = async->buf_write_count;
588 bi.buf_write_ptr = async->buf_write_ptr;
589 bi.buf_read_count = async->buf_read_count;
590 bi.buf_read_ptr = async->buf_read_ptr;
591
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800592copyback:
Bill Pemberton9aa53392009-03-16 22:06:42 -0400593 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800594 return -EFAULT;
595
596 return 0;
597}
598
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530599static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
600 unsigned int *data, void *file);
David Schleefed9eccb2008-11-04 20:29:31 -0800601/*
Pieter De Praetere20617f22010-03-10 09:47:44 +0100602 * COMEDI_INSNLIST
603 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -0800604 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100605 * arg:
606 * pointer to sync cmd structure
David Schleefed9eccb2008-11-04 20:29:31 -0800607 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100608 * reads:
609 * sync cmd struct at arg
610 * instruction list
611 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -0800612 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100613 * writes:
614 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -0800615 */
616/* arbitrary limits */
617#define MAX_SAMPLES 256
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400618static int do_insnlist_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800619{
Bill Pembertonda613f42009-03-16 22:05:59 -0400620 struct comedi_insnlist insnlist;
Bill Pemberton90035c02009-03-16 22:05:53 -0400621 struct comedi_insn *insns = NULL;
Bill Pemberton790c5542009-03-16 22:05:02 -0400622 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800623 int i = 0;
624 int ret = 0;
625
Bill Pembertonda613f42009-03-16 22:05:59 -0400626 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
David Schleefed9eccb2008-11-04 20:29:31 -0800627 return -EFAULT;
628
Bill Pemberton790c5542009-03-16 22:05:02 -0400629 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800630 if (!data) {
631 DPRINTK("kmalloc failed\n");
632 ret = -ENOMEM;
633 goto error;
634 }
635
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530636 insns =
637 kmalloc(sizeof(struct comedi_insn) * insnlist.n_insns, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800638 if (!insns) {
639 DPRINTK("kmalloc failed\n");
640 ret = -ENOMEM;
641 goto error;
642 }
643
644 if (copy_from_user(insns, insnlist.insns,
Bill Pemberton90035c02009-03-16 22:05:53 -0400645 sizeof(struct comedi_insn) * insnlist.n_insns)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800646 DPRINTK("copy_from_user failed\n");
647 ret = -EFAULT;
648 goto error;
649 }
650
651 for (i = 0; i < insnlist.n_insns; i++) {
652 if (insns[i].n > MAX_SAMPLES) {
653 DPRINTK("number of samples too large\n");
654 ret = -EINVAL;
655 goto error;
656 }
657 if (insns[i].insn & INSN_MASK_WRITE) {
658 if (copy_from_user(data, insns[i].data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400659 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800660 DPRINTK("copy_from_user failed\n");
661 ret = -EFAULT;
662 goto error;
663 }
664 }
665 ret = parse_insn(dev, insns + i, data, file);
666 if (ret < 0)
667 goto error;
668 if (insns[i].insn & INSN_MASK_READ) {
669 if (copy_to_user(insns[i].data, data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400670 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800671 DPRINTK("copy_to_user failed\n");
672 ret = -EFAULT;
673 goto error;
674 }
675 }
676 if (need_resched())
677 schedule();
678 }
679
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800680error:
681 kfree(insns);
682 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800683
684 if (ret < 0)
685 return ret;
686 return i;
687}
688
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530689static int check_insn_config_length(struct comedi_insn *insn,
690 unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -0800691{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800692 if (insn->n < 1)
693 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -0800694
695 switch (data[0]) {
696 case INSN_CONFIG_DIO_OUTPUT:
697 case INSN_CONFIG_DIO_INPUT:
698 case INSN_CONFIG_DISARM:
699 case INSN_CONFIG_RESET:
700 if (insn->n == 1)
701 return 0;
702 break;
703 case INSN_CONFIG_ARM:
704 case INSN_CONFIG_DIO_QUERY:
705 case INSN_CONFIG_BLOCK_SIZE:
706 case INSN_CONFIG_FILTER:
707 case INSN_CONFIG_SERIAL_CLOCK:
708 case INSN_CONFIG_BIDIRECTIONAL_DATA:
709 case INSN_CONFIG_ALT_SOURCE:
710 case INSN_CONFIG_SET_COUNTER_MODE:
711 case INSN_CONFIG_8254_READ_STATUS:
712 case INSN_CONFIG_SET_ROUTING:
713 case INSN_CONFIG_GET_ROUTING:
714 case INSN_CONFIG_GET_PWM_STATUS:
715 case INSN_CONFIG_PWM_SET_PERIOD:
716 case INSN_CONFIG_PWM_GET_PERIOD:
717 if (insn->n == 2)
718 return 0;
719 break;
720 case INSN_CONFIG_SET_GATE_SRC:
721 case INSN_CONFIG_GET_GATE_SRC:
722 case INSN_CONFIG_SET_CLOCK_SRC:
723 case INSN_CONFIG_GET_CLOCK_SRC:
724 case INSN_CONFIG_SET_OTHER_SRC:
725 case INSN_CONFIG_GET_COUNTER_STATUS:
726 case INSN_CONFIG_PWM_SET_H_BRIDGE:
727 case INSN_CONFIG_PWM_GET_H_BRIDGE:
728 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
729 if (insn->n == 3)
730 return 0;
731 break;
732 case INSN_CONFIG_PWM_OUTPUT:
733 case INSN_CONFIG_ANALOG_TRIG:
734 if (insn->n == 5)
735 return 0;
736 break;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530737 /* by default we allow the insn since we don't have checks for
738 * all possible cases yet */
David Schleefed9eccb2008-11-04 20:29:31 -0800739 default:
Mark Rankilor3fffdf22010-04-29 18:17:16 +0800740 printk(KERN_WARNING
741 "comedi: no check for data length of config insn id "
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530742 "%i is implemented.\n"
743 " Add a check to %s in %s.\n"
744 " Assuming n=%i is correct.\n", data[0], __func__,
745 __FILE__, insn->n);
David Schleefed9eccb2008-11-04 20:29:31 -0800746 return 0;
747 break;
748 }
749 return -EINVAL;
750}
751
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530752static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
753 unsigned int *data, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800754{
Bill Pemberton34c43922009-03-16 22:05:14 -0400755 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800756 int ret = 0;
757 int i;
758
759 if (insn->insn & INSN_MASK_SPECIAL) {
760 /* a non-subdevice instruction */
761
762 switch (insn->insn) {
763 case INSN_GTOD:
764 {
765 struct timeval tv;
766
767 if (insn->n != 2) {
768 ret = -EINVAL;
769 break;
770 }
771
772 do_gettimeofday(&tv);
773 data[0] = tv.tv_sec;
774 data[1] = tv.tv_usec;
775 ret = 2;
776
777 break;
778 }
779 case INSN_WAIT:
780 if (insn->n != 1 || data[0] >= 100000) {
781 ret = -EINVAL;
782 break;
783 }
784 udelay(data[0] / 1000);
785 ret = 1;
786 break;
787 case INSN_INTTRIG:
788 if (insn->n != 1) {
789 ret = -EINVAL;
790 break;
791 }
792 if (insn->subdev >= dev->n_subdevices) {
793 DPRINTK("%d not usable subdevice\n",
794 insn->subdev);
795 ret = -EINVAL;
796 break;
797 }
798 s = dev->subdevices + insn->subdev;
799 if (!s->async) {
800 DPRINTK("no async\n");
801 ret = -EINVAL;
802 break;
803 }
804 if (!s->async->inttrig) {
805 DPRINTK("no inttrig\n");
806 ret = -EAGAIN;
807 break;
808 }
809 ret = s->async->inttrig(dev, s, insn->data[0]);
810 if (ret >= 0)
811 ret = 1;
812 break;
813 default:
814 DPRINTK("invalid insn\n");
815 ret = -EINVAL;
816 break;
817 }
818 } else {
819 /* a subdevice instruction */
Bill Pemberton790c5542009-03-16 22:05:02 -0400820 unsigned int maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800821
822 if (insn->subdev >= dev->n_subdevices) {
823 DPRINTK("subdevice %d out of range\n", insn->subdev);
824 ret = -EINVAL;
825 goto out;
826 }
827 s = dev->subdevices + insn->subdev;
828
829 if (s->type == COMEDI_SUBD_UNUSED) {
830 DPRINTK("%d not usable subdevice\n", insn->subdev);
831 ret = -EIO;
832 goto out;
833 }
834
835 /* are we locked? (ioctl lock) */
836 if (s->lock && s->lock != file) {
837 DPRINTK("device locked\n");
838 ret = -EACCES;
839 goto out;
840 }
841
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800842 ret = check_chanlist(s, 1, &insn->chanspec);
843 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -0800844 ret = -EINVAL;
845 DPRINTK("bad chanspec\n");
846 goto out;
847 }
848
849 if (s->busy) {
850 ret = -EBUSY;
851 goto out;
852 }
853 /* This looks arbitrary. It is. */
854 s->busy = &parse_insn;
855 switch (insn->insn) {
856 case INSN_READ:
857 ret = s->insn_read(dev, s, insn, data);
858 break;
859 case INSN_WRITE:
860 maxdata = s->maxdata_list
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800861 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
862 : s->maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800863 for (i = 0; i < insn->n; ++i) {
864 if (data[i] > maxdata) {
865 ret = -EINVAL;
866 DPRINTK("bad data value(s)\n");
867 break;
868 }
869 }
870 if (ret == 0)
871 ret = s->insn_write(dev, s, insn, data);
872 break;
873 case INSN_BITS:
874 if (insn->n != 2) {
875 ret = -EINVAL;
876 break;
877 }
878 ret = s->insn_bits(dev, s, insn, data);
879 break;
880 case INSN_CONFIG:
881 ret = check_insn_config_length(insn, data);
882 if (ret)
883 break;
884 ret = s->insn_config(dev, s, insn, data);
885 break;
886 default:
887 ret = -EINVAL;
888 break;
889 }
890
891 s->busy = NULL;
892 }
893
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800894out:
David Schleefed9eccb2008-11-04 20:29:31 -0800895 return ret;
896}
897
898/*
Pieter De Praetere20617f22010-03-10 09:47:44 +0100899 * COMEDI_INSN
900 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -0800901 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100902 * arg:
903 * pointer to insn
David Schleefed9eccb2008-11-04 20:29:31 -0800904 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100905 * reads:
906 * struct comedi_insn struct at arg
907 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -0800908 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100909 * writes:
910 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -0800911 */
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400912static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800913{
Bill Pemberton90035c02009-03-16 22:05:53 -0400914 struct comedi_insn insn;
Bill Pemberton790c5542009-03-16 22:05:02 -0400915 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800916 int ret = 0;
917
Bill Pemberton790c5542009-03-16 22:05:02 -0400918 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800919 if (!data) {
920 ret = -ENOMEM;
921 goto error;
922 }
923
Bill Pemberton90035c02009-03-16 22:05:53 -0400924 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800925 ret = -EFAULT;
926 goto error;
927 }
928
929 /* This is where the behavior of insn and insnlist deviate. */
930 if (insn.n > MAX_SAMPLES)
931 insn.n = MAX_SAMPLES;
932 if (insn.insn & INSN_MASK_WRITE) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530933 if (copy_from_user
934 (data, insn.data, insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800935 ret = -EFAULT;
936 goto error;
937 }
938 }
939 ret = parse_insn(dev, &insn, data, file);
940 if (ret < 0)
941 goto error;
942 if (insn.insn & INSN_MASK_READ) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530943 if (copy_to_user
944 (insn.data, data, insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800945 ret = -EFAULT;
946 goto error;
947 }
948 }
949 ret = insn.n;
950
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800951error:
952 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800953
954 return ret;
955}
956
957/*
958 COMEDI_CMD
959 command ioctl
960
961 arg:
962 pointer to cmd structure
963
964 reads:
965 cmd structure at arg
966 channel/range list
967
968 writes:
969 modified cmd structure at arg
970
971*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400972static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800973{
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400974 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -0400975 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400976 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800977 int ret = 0;
978 unsigned int *chanlist_saver = NULL;
979
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400980 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800981 DPRINTK("bad cmd address\n");
982 return -EFAULT;
983 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800984 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -0800985 chanlist_saver = user_cmd.chanlist;
986
987 if (user_cmd.subdev >= dev->n_subdevices) {
988 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
989 return -ENODEV;
990 }
991
992 s = dev->subdevices + user_cmd.subdev;
993 async = s->async;
994
995 if (s->type == COMEDI_SUBD_UNUSED) {
996 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
997 return -EIO;
998 }
999
1000 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1001 DPRINTK("subdevice %i does not support commands\n",
1002 user_cmd.subdev);
1003 return -EIO;
1004 }
1005
1006 /* are we locked? (ioctl lock) */
1007 if (s->lock && s->lock != file) {
1008 DPRINTK("subdevice locked\n");
1009 return -EACCES;
1010 }
1011
1012 /* are we busy? */
1013 if (s->busy) {
1014 DPRINTK("subdevice busy\n");
1015 return -EBUSY;
1016 }
1017 s->busy = file;
1018
1019 /* make sure channel/gain list isn't too long */
1020 if (user_cmd.chanlist_len > s->len_chanlist) {
1021 DPRINTK("channel/gain list too long %u > %d\n",
1022 user_cmd.chanlist_len, s->len_chanlist);
1023 ret = -EINVAL;
1024 goto cleanup;
1025 }
1026
1027 /* make sure channel/gain list isn't too short */
1028 if (user_cmd.chanlist_len < 1) {
1029 DPRINTK("channel/gain list too short %u < 1\n",
1030 user_cmd.chanlist_len);
1031 ret = -EINVAL;
1032 goto cleanup;
1033 }
1034
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001035 kfree(async->cmd.chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001036 async->cmd = user_cmd;
1037 async->cmd.data = NULL;
1038 /* load channel/gain list */
1039 async->cmd.chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001040 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001041 if (!async->cmd.chanlist) {
1042 DPRINTK("allocation failed\n");
1043 ret = -ENOMEM;
1044 goto cleanup;
1045 }
1046
1047 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001048 async->cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001049 DPRINTK("fault reading chanlist\n");
1050 ret = -EFAULT;
1051 goto cleanup;
1052 }
1053
1054 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001055 ret = check_chanlist(s, async->cmd.chanlist_len, async->cmd.chanlist);
1056 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001057 DPRINTK("bad chanlist\n");
1058 goto cleanup;
1059 }
1060
1061 ret = s->do_cmdtest(dev, s, &async->cmd);
1062
1063 if (async->cmd.flags & TRIG_BOGUS || ret) {
1064 DPRINTK("test returned %d\n", ret);
1065 user_cmd = async->cmd;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001066 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001067 user_cmd.chanlist = chanlist_saver;
1068 user_cmd.data = NULL;
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001069 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001070 DPRINTK("fault writing cmd\n");
1071 ret = -EFAULT;
1072 goto cleanup;
1073 }
1074 ret = -EAGAIN;
1075 goto cleanup;
1076 }
1077
1078 if (!async->prealloc_bufsz) {
1079 ret = -ENOMEM;
1080 DPRINTK("no buffer (?)\n");
1081 goto cleanup;
1082 }
1083
1084 comedi_reset_async_buf(async);
1085
1086 async->cb_mask =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001087 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1088 COMEDI_CB_OVERFLOW;
1089 if (async->cmd.flags & TRIG_WAKE_EOS)
David Schleefed9eccb2008-11-04 20:29:31 -08001090 async->cb_mask |= COMEDI_CB_EOS;
David Schleefed9eccb2008-11-04 20:29:31 -08001091
1092 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1093
David Schleefed9eccb2008-11-04 20:29:31 -08001094 ret = s->do_cmd(dev, s);
1095 if (ret == 0)
1096 return 0;
1097
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001098cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -08001099 do_become_nonbusy(dev, s);
1100
1101 return ret;
1102}
1103
1104/*
1105 COMEDI_CMDTEST
1106 command testing ioctl
1107
1108 arg:
1109 pointer to cmd structure
1110
1111 reads:
1112 cmd structure at arg
1113 channel/range list
1114
1115 writes:
1116 modified cmd structure at arg
1117
1118*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001119static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001120{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001121 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001122 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001123 int ret = 0;
1124 unsigned int *chanlist = NULL;
1125 unsigned int *chanlist_saver = NULL;
1126
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001127 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001128 DPRINTK("bad cmd address\n");
1129 return -EFAULT;
1130 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001131 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001132 chanlist_saver = user_cmd.chanlist;
1133
1134 if (user_cmd.subdev >= dev->n_subdevices) {
1135 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1136 return -ENODEV;
1137 }
1138
1139 s = dev->subdevices + user_cmd.subdev;
1140 if (s->type == COMEDI_SUBD_UNUSED) {
1141 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1142 return -EIO;
1143 }
1144
1145 if (!s->do_cmd || !s->do_cmdtest) {
1146 DPRINTK("subdevice %i does not support commands\n",
1147 user_cmd.subdev);
1148 return -EIO;
1149 }
1150
1151 /* make sure channel/gain list isn't too long */
1152 if (user_cmd.chanlist_len > s->len_chanlist) {
1153 DPRINTK("channel/gain list too long %d > %d\n",
1154 user_cmd.chanlist_len, s->len_chanlist);
1155 ret = -EINVAL;
1156 goto cleanup;
1157 }
1158
1159 /* load channel/gain list */
1160 if (user_cmd.chanlist) {
1161 chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001162 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001163 if (!chanlist) {
1164 DPRINTK("allocation failed\n");
1165 ret = -ENOMEM;
1166 goto cleanup;
1167 }
1168
1169 if (copy_from_user(chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001170 user_cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001171 DPRINTK("fault reading chanlist\n");
1172 ret = -EFAULT;
1173 goto cleanup;
1174 }
1175
1176 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001177 ret = check_chanlist(s, user_cmd.chanlist_len, chanlist);
1178 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001179 DPRINTK("bad chanlist\n");
1180 goto cleanup;
1181 }
1182
1183 user_cmd.chanlist = chanlist;
1184 }
1185
1186 ret = s->do_cmdtest(dev, s, &user_cmd);
1187
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001188 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001189 user_cmd.chanlist = chanlist_saver;
1190
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001191 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001192 DPRINTK("bad cmd address\n");
1193 ret = -EFAULT;
1194 goto cleanup;
1195 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001196cleanup:
1197 kfree(chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001198
1199 return ret;
1200}
1201
1202/*
1203 COMEDI_LOCK
1204 lock subdevice
1205
1206 arg:
1207 subdevice number
1208
1209 reads:
1210 none
1211
1212 writes:
1213 none
1214
1215*/
1216
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301217static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1218 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001219{
1220 int ret = 0;
1221 unsigned long flags;
Bill Pemberton34c43922009-03-16 22:05:14 -04001222 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001223
1224 if (arg >= dev->n_subdevices)
1225 return -EINVAL;
1226 s = dev->subdevices + arg;
1227
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001228 spin_lock_irqsave(&s->spin_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001229 if (s->busy || s->lock)
David Schleefed9eccb2008-11-04 20:29:31 -08001230 ret = -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001231 else
David Schleefed9eccb2008-11-04 20:29:31 -08001232 s->lock = file;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001233 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08001234
1235 if (ret < 0)
1236 return ret;
1237
1238#if 0
1239 if (s->lock_f)
1240 ret = s->lock_f(dev, s);
1241#endif
1242
1243 return ret;
1244}
1245
1246/*
1247 COMEDI_UNLOCK
1248 unlock subdevice
1249
1250 arg:
1251 subdevice number
1252
1253 reads:
1254 none
1255
1256 writes:
1257 none
1258
1259 This function isn't protected by the semaphore, since
1260 we already own the lock.
1261*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301262static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1263 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001264{
Bill Pemberton34c43922009-03-16 22:05:14 -04001265 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001266
1267 if (arg >= dev->n_subdevices)
1268 return -EINVAL;
1269 s = dev->subdevices + arg;
1270
1271 if (s->busy)
1272 return -EBUSY;
1273
1274 if (s->lock && s->lock != file)
1275 return -EACCES;
1276
1277 if (s->lock == file) {
1278#if 0
1279 if (s->unlock)
1280 s->unlock(dev, s);
1281#endif
1282
1283 s->lock = NULL;
1284 }
1285
1286 return 0;
1287}
1288
1289/*
1290 COMEDI_CANCEL
1291 cancel acquisition ioctl
1292
1293 arg:
1294 subdevice number
1295
1296 reads:
1297 nothing
1298
1299 writes:
1300 nothing
1301
1302*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301303static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1304 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001305{
Bill Pemberton34c43922009-03-16 22:05:14 -04001306 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001307
1308 if (arg >= dev->n_subdevices)
1309 return -EINVAL;
1310 s = dev->subdevices + arg;
1311 if (s->async == NULL)
1312 return -EINVAL;
1313
1314 if (s->lock && s->lock != file)
1315 return -EACCES;
1316
1317 if (!s->busy)
1318 return 0;
1319
1320 if (s->busy != file)
1321 return -EBUSY;
1322
1323 return do_cancel(dev, s);
1324}
1325
1326/*
1327 COMEDI_POLL ioctl
1328 instructs driver to synchronize buffers
1329
1330 arg:
1331 subdevice number
1332
1333 reads:
1334 nothing
1335
1336 writes:
1337 nothing
1338
1339*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301340static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1341 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001342{
Bill Pemberton34c43922009-03-16 22:05:14 -04001343 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001344
1345 if (arg >= dev->n_subdevices)
1346 return -EINVAL;
1347 s = dev->subdevices + arg;
1348
1349 if (s->lock && s->lock != file)
1350 return -EACCES;
1351
1352 if (!s->busy)
1353 return 0;
1354
1355 if (s->busy != file)
1356 return -EBUSY;
1357
1358 if (s->poll)
1359 return s->poll(dev, s);
1360
1361 return -EINVAL;
1362}
1363
Bill Pemberton34c43922009-03-16 22:05:14 -04001364static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001365{
1366 int ret = 0;
1367
1368 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1369 ret = s->cancel(dev, s);
1370
1371 do_become_nonbusy(dev, s);
1372
1373 return ret;
1374}
1375
1376void comedi_unmap(struct vm_area_struct *area)
1377{
Bill Pembertond1636792009-03-16 22:05:20 -04001378 struct comedi_async *async;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001379 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -08001380
1381 async = area->vm_private_data;
1382 dev = async->subdevice->device;
1383
1384 mutex_lock(&dev->mutex);
1385 async->mmap_count--;
1386 mutex_unlock(&dev->mutex);
1387}
1388
1389static struct vm_operations_struct comedi_vm_ops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301390 .close = comedi_unmap,
David Schleefed9eccb2008-11-04 20:29:31 -08001391};
1392
1393static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1394{
1395 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001396 struct comedi_device_file_info *dev_file_info =
1397 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001398 struct comedi_device *dev = dev_file_info->device;
Bill Pembertond1636792009-03-16 22:05:20 -04001399 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001400 unsigned long start = vma->vm_start;
1401 unsigned long size;
1402 int n_pages;
1403 int i;
1404 int retval;
Bill Pemberton34c43922009-03-16 22:05:14 -04001405 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001406
1407 mutex_lock(&dev->mutex);
1408 if (!dev->attached) {
1409 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1410 retval = -ENODEV;
1411 goto done;
1412 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001413 if (vma->vm_flags & VM_WRITE)
David Schleefed9eccb2008-11-04 20:29:31 -08001414 s = comedi_get_write_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001415 else
David Schleefed9eccb2008-11-04 20:29:31 -08001416 s = comedi_get_read_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001417
David Schleefed9eccb2008-11-04 20:29:31 -08001418 if (s == NULL) {
1419 retval = -EINVAL;
1420 goto done;
1421 }
1422 async = s->async;
1423 if (async == NULL) {
1424 retval = -EINVAL;
1425 goto done;
1426 }
1427
1428 if (vma->vm_pgoff != 0) {
1429 DPRINTK("comedi: mmap() offset must be 0.\n");
1430 retval = -EINVAL;
1431 goto done;
1432 }
1433
1434 size = vma->vm_end - vma->vm_start;
1435 if (size > async->prealloc_bufsz) {
1436 retval = -EFAULT;
1437 goto done;
1438 }
1439 if (size & (~PAGE_MASK)) {
1440 retval = -EFAULT;
1441 goto done;
1442 }
1443
1444 n_pages = size >> PAGE_SHIFT;
1445 for (i = 0; i < n_pages; ++i) {
1446 if (remap_pfn_range(vma, start,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301447 page_to_pfn(virt_to_page
1448 (async->buf_page_list
1449 [i].virt_addr)), PAGE_SIZE,
1450 PAGE_SHARED)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001451 retval = -EAGAIN;
1452 goto done;
1453 }
1454 start += PAGE_SIZE;
1455 }
1456
1457 vma->vm_ops = &comedi_vm_ops;
1458 vma->vm_private_data = async;
1459
1460 async->mmap_count++;
1461
1462 retval = 0;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001463done:
David Schleefed9eccb2008-11-04 20:29:31 -08001464 mutex_unlock(&dev->mutex);
1465 return retval;
1466}
1467
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301468static unsigned int comedi_poll(struct file *file, poll_table * wait)
David Schleefed9eccb2008-11-04 20:29:31 -08001469{
1470 unsigned int mask = 0;
1471 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001472 struct comedi_device_file_info *dev_file_info =
1473 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001474 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001475 struct comedi_subdevice *read_subdev;
1476 struct comedi_subdevice *write_subdev;
David Schleefed9eccb2008-11-04 20:29:31 -08001477
1478 mutex_lock(&dev->mutex);
1479 if (!dev->attached) {
1480 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1481 mutex_unlock(&dev->mutex);
1482 return 0;
1483 }
1484
1485 mask = 0;
1486 read_subdev = comedi_get_read_subdevice(dev_file_info);
1487 if (read_subdev) {
1488 poll_wait(file, &read_subdev->async->wait_head, wait);
1489 if (!read_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001490 || comedi_buf_read_n_available(read_subdev->async) > 0
1491 || !(comedi_get_subdevice_runflags(read_subdev) &
1492 SRF_RUNNING)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001493 mask |= POLLIN | POLLRDNORM;
1494 }
1495 }
1496 write_subdev = comedi_get_write_subdevice(dev_file_info);
1497 if (write_subdev) {
1498 poll_wait(file, &write_subdev->async->wait_head, wait);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001499 comedi_buf_write_alloc(write_subdev->async,
1500 write_subdev->async->prealloc_bufsz);
David Schleefed9eccb2008-11-04 20:29:31 -08001501 if (!write_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001502 || !(comedi_get_subdevice_runflags(write_subdev) &
1503 SRF_RUNNING)
1504 || comedi_buf_write_n_allocated(write_subdev->async) >=
1505 bytes_per_sample(write_subdev->async->subdevice)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001506 mask |= POLLOUT | POLLWRNORM;
1507 }
1508 }
1509
1510 mutex_unlock(&dev->mutex);
1511 return mask;
1512}
1513
1514static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01001515 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001516{
Bill Pemberton34c43922009-03-16 22:05:14 -04001517 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001518 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001519 int n, m, count = 0, retval = 0;
1520 DECLARE_WAITQUEUE(wait, current);
1521 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001522 struct comedi_device_file_info *dev_file_info =
1523 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001524 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001525
1526 if (!dev->attached) {
1527 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1528 retval = -ENODEV;
1529 goto done;
1530 }
1531
1532 s = comedi_get_write_subdevice(dev_file_info);
1533 if (s == NULL) {
1534 retval = -EIO;
1535 goto done;
1536 }
1537 async = s->async;
1538
1539 if (!nbytes) {
1540 retval = 0;
1541 goto done;
1542 }
1543 if (!s->busy) {
1544 retval = 0;
1545 goto done;
1546 }
1547 if (s->busy != file) {
1548 retval = -EACCES;
1549 goto done;
1550 }
1551 add_wait_queue(&async->wait_head, &wait);
1552 while (nbytes > 0 && !retval) {
1553 set_current_state(TASK_INTERRUPTIBLE);
1554
1555 n = nbytes;
1556
1557 m = n;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001558 if (async->buf_write_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001559 m = async->prealloc_bufsz - async->buf_write_ptr;
David Schleefed9eccb2008-11-04 20:29:31 -08001560 comedi_buf_write_alloc(async, async->prealloc_bufsz);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001561 if (m > comedi_buf_write_n_allocated(async))
David Schleefed9eccb2008-11-04 20:29:31 -08001562 m = comedi_buf_write_n_allocated(async);
David Schleefed9eccb2008-11-04 20:29:31 -08001563 if (m < n)
1564 n = m;
1565
1566 if (n == 0) {
1567 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1568 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001569 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001570 retval = -EPIPE;
1571 } else {
1572 retval = 0;
1573 }
1574 do_become_nonbusy(dev, s);
1575 break;
1576 }
1577 if (file->f_flags & O_NONBLOCK) {
1578 retval = -EAGAIN;
1579 break;
1580 }
1581 if (signal_pending(current)) {
1582 retval = -ERESTARTSYS;
1583 break;
1584 }
1585 schedule();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001586 if (!s->busy)
David Schleefed9eccb2008-11-04 20:29:31 -08001587 break;
David Schleefed9eccb2008-11-04 20:29:31 -08001588 if (s->busy != file) {
1589 retval = -EACCES;
1590 break;
1591 }
1592 continue;
1593 }
1594
1595 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001596 buf, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001597 if (m) {
1598 n -= m;
1599 retval = -EFAULT;
1600 }
1601 comedi_buf_write_free(async, n);
1602
1603 count += n;
1604 nbytes -= n;
1605
1606 buf += n;
1607 break; /* makes device work like a pipe */
1608 }
1609 set_current_state(TASK_RUNNING);
1610 remove_wait_queue(&async->wait_head, &wait);
1611
1612done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001613 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001614}
1615
1616static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01001617 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001618{
Bill Pemberton34c43922009-03-16 22:05:14 -04001619 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001620 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001621 int n, m, count = 0, retval = 0;
1622 DECLARE_WAITQUEUE(wait, current);
1623 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001624 struct comedi_device_file_info *dev_file_info =
1625 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001626 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001627
1628 if (!dev->attached) {
1629 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1630 retval = -ENODEV;
1631 goto done;
1632 }
1633
1634 s = comedi_get_read_subdevice(dev_file_info);
1635 if (s == NULL) {
1636 retval = -EIO;
1637 goto done;
1638 }
1639 async = s->async;
1640 if (!nbytes) {
1641 retval = 0;
1642 goto done;
1643 }
1644 if (!s->busy) {
1645 retval = 0;
1646 goto done;
1647 }
1648 if (s->busy != file) {
1649 retval = -EACCES;
1650 goto done;
1651 }
1652
1653 add_wait_queue(&async->wait_head, &wait);
1654 while (nbytes > 0 && !retval) {
1655 set_current_state(TASK_INTERRUPTIBLE);
1656
1657 n = nbytes;
1658
1659 m = comedi_buf_read_n_available(async);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001660 /* printk("%d available\n",m); */
1661 if (async->buf_read_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001662 m = async->prealloc_bufsz - async->buf_read_ptr;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001663 /* printk("%d contiguous\n",m); */
David Schleefed9eccb2008-11-04 20:29:31 -08001664 if (m < n)
1665 n = m;
1666
1667 if (n == 0) {
1668 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1669 do_become_nonbusy(dev, s);
1670 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001671 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001672 retval = -EPIPE;
1673 } else {
1674 retval = 0;
1675 }
1676 break;
1677 }
1678 if (file->f_flags & O_NONBLOCK) {
1679 retval = -EAGAIN;
1680 break;
1681 }
1682 if (signal_pending(current)) {
1683 retval = -ERESTARTSYS;
1684 break;
1685 }
1686 schedule();
1687 if (!s->busy) {
1688 retval = 0;
1689 break;
1690 }
1691 if (s->busy != file) {
1692 retval = -EACCES;
1693 break;
1694 }
1695 continue;
1696 }
1697 m = copy_to_user(buf, async->prealloc_buf +
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001698 async->buf_read_ptr, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001699 if (m) {
1700 n -= m;
1701 retval = -EFAULT;
1702 }
1703
1704 comedi_buf_read_alloc(async, n);
1705 comedi_buf_read_free(async, n);
1706
1707 count += n;
1708 nbytes -= n;
1709
1710 buf += n;
1711 break; /* makes device work like a pipe */
1712 }
1713 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001714 async->buf_read_count - async->buf_write_count == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001715 do_become_nonbusy(dev, s);
1716 }
1717 set_current_state(TASK_RUNNING);
1718 remove_wait_queue(&async->wait_head, &wait);
1719
1720done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001721 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001722}
1723
1724/*
1725 This function restores a subdevice to an idle state.
1726 */
Bill Pemberton34c43922009-03-16 22:05:14 -04001727void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001728{
Bill Pembertond1636792009-03-16 22:05:20 -04001729 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001730
1731 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001732 if (async) {
1733 comedi_reset_async_buf(async);
1734 async->inttrig = NULL;
1735 } else {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001736 printk(KERN_ERR
1737 "BUG: (?) do_become_nonbusy called with async=0\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001738 }
1739
1740 s->busy = NULL;
1741}
1742
1743static int comedi_open(struct inode *inode, struct file *file)
1744{
David Schleefed9eccb2008-11-04 20:29:31 -08001745 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001746 struct comedi_device_file_info *dev_file_info =
1747 comedi_get_device_file_info(minor);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301748 struct comedi_device *dev =
1749 dev_file_info ? dev_file_info->device : NULL;
Ian Abbott97920072009-02-09 16:51:38 +00001750
David Schleefed9eccb2008-11-04 20:29:31 -08001751 if (dev == NULL) {
1752 DPRINTK("invalid minor number\n");
1753 return -ENODEV;
1754 }
1755
1756 /* This is slightly hacky, but we want module autoloading
1757 * to work for root.
1758 * case: user opens device, attached -> ok
1759 * case: user opens device, unattached, in_request_module=0 -> autoload
1760 * case: user opens device, unattached, in_request_module=1 -> fail
1761 * case: root opens device, attached -> ok
1762 * case: root opens device, unattached, in_request_module=1 -> ok
1763 * (typically called from modprobe)
1764 * case: root opens device, unattached, in_request_module=0 -> autoload
1765 *
1766 * The last could be changed to "-> ok", which would deny root
1767 * autoloading.
1768 */
1769 mutex_lock(&dev->mutex);
1770 if (dev->attached)
1771 goto ok;
Eric Parisa8f80e82009-08-13 09:44:51 -04001772 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
David Schleefed9eccb2008-11-04 20:29:31 -08001773 DPRINTK("in request module\n");
1774 mutex_unlock(&dev->mutex);
1775 return -ENODEV;
1776 }
Eric Parisa8f80e82009-08-13 09:44:51 -04001777 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
David Schleefed9eccb2008-11-04 20:29:31 -08001778 goto ok;
1779
1780 dev->in_request_module = 1;
1781
David Schleefed9eccb2008-11-04 20:29:31 -08001782#ifdef CONFIG_KMOD
1783 mutex_unlock(&dev->mutex);
Ian Abbott56d92c62009-02-09 16:32:12 +00001784 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
David Schleefed9eccb2008-11-04 20:29:31 -08001785 mutex_lock(&dev->mutex);
1786#endif
1787
1788 dev->in_request_module = 0;
1789
Eric Parisa8f80e82009-08-13 09:44:51 -04001790 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1791 DPRINTK("not attached and not CAP_NET_ADMIN\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001792 mutex_unlock(&dev->mutex);
1793 return -ENODEV;
1794 }
1795ok:
1796 __module_get(THIS_MODULE);
1797
1798 if (dev->attached) {
1799 if (!try_module_get(dev->driver->module)) {
1800 module_put(THIS_MODULE);
1801 mutex_unlock(&dev->mutex);
1802 return -ENOSYS;
1803 }
1804 }
1805
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001806 if (dev->attached && dev->use_count == 0 && dev->open)
David Schleefed9eccb2008-11-04 20:29:31 -08001807 dev->open(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001808
1809 dev->use_count++;
1810
1811 mutex_unlock(&dev->mutex);
1812
1813 return 0;
1814}
1815
1816static int comedi_close(struct inode *inode, struct file *file)
1817{
1818 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001819 struct comedi_device_file_info *dev_file_info =
1820 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001821 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001822 struct comedi_subdevice *s = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001823 int i;
1824
1825 mutex_lock(&dev->mutex);
1826
1827 if (dev->subdevices) {
1828 for (i = 0; i < dev->n_subdevices; i++) {
1829 s = dev->subdevices + i;
1830
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001831 if (s->busy == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001832 do_cancel(dev, s);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001833 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001834 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001835 }
1836 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001837 if (dev->attached && dev->use_count == 1 && dev->close)
David Schleefed9eccb2008-11-04 20:29:31 -08001838 dev->close(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001839
1840 module_put(THIS_MODULE);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001841 if (dev->attached)
David Schleefed9eccb2008-11-04 20:29:31 -08001842 module_put(dev->driver->module);
David Schleefed9eccb2008-11-04 20:29:31 -08001843
1844 dev->use_count--;
1845
1846 mutex_unlock(&dev->mutex);
1847
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001848 if (file->f_flags & FASYNC)
David Schleefed9eccb2008-11-04 20:29:31 -08001849 comedi_fasync(-1, file, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001850
1851 return 0;
1852}
1853
1854static int comedi_fasync(int fd, struct file *file, int on)
1855{
1856 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001857 struct comedi_device_file_info *dev_file_info =
1858 comedi_get_device_file_info(minor);
1859
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001860 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001861
1862 return fasync_helper(fd, file, on, &dev->async_queue);
1863}
1864
1865const struct file_operations comedi_fops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301866 .owner = THIS_MODULE,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301867 .unlocked_ioctl = comedi_unlocked_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301868 .compat_ioctl = comedi_compat_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301869 .open = comedi_open,
1870 .release = comedi_close,
1871 .read = comedi_read,
1872 .write = comedi_write,
1873 .mmap = comedi_mmap,
1874 .poll = comedi_poll,
1875 .fasync = comedi_fasync,
David Schleefed9eccb2008-11-04 20:29:31 -08001876};
1877
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001878struct class *comedi_class;
David Schleefed9eccb2008-11-04 20:29:31 -08001879static struct cdev comedi_cdev;
1880
1881static void comedi_cleanup_legacy_minors(void)
1882{
1883 unsigned i;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001884
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001885 for (i = 0; i < comedi_num_legacy_minors; i++)
David Schleefed9eccb2008-11-04 20:29:31 -08001886 comedi_free_board_minor(i);
David Schleefed9eccb2008-11-04 20:29:31 -08001887}
1888
1889static int __init comedi_init(void)
1890{
1891 int i;
1892 int retval;
1893
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001894 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1895 " - http://www.comedi.org\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001896
Frank Mori Hessa3cb7292008-12-15 13:44:45 +00001897 if (comedi_num_legacy_minors < 0 ||
1898 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
1899 printk(KERN_ERR "comedi: error: invalid value for module "
1900 "parameter \"comedi_num_legacy_minors\". Valid values "
1901 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
1902 return -EINVAL;
1903 }
1904
1905 /*
1906 * comedi is unusable if both comedi_autoconfig and
1907 * comedi_num_legacy_minors are zero, so we might as well adjust the
1908 * defaults in that case
1909 */
1910 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
1911 comedi_num_legacy_minors = 16;
1912
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001913 memset(comedi_file_info_table, 0,
1914 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001915
1916 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001917 COMEDI_NUM_MINORS, "comedi");
David Schleefed9eccb2008-11-04 20:29:31 -08001918 if (retval)
1919 return -EIO;
1920 cdev_init(&comedi_cdev, &comedi_fops);
1921 comedi_cdev.owner = THIS_MODULE;
1922 kobject_set_name(&comedi_cdev.kobj, "comedi");
1923 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1924 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001925 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001926 return -EIO;
1927 }
1928 comedi_class = class_create(THIS_MODULE, "comedi");
1929 if (IS_ERR(comedi_class)) {
Mark Rankilor3fffdf22010-04-29 18:17:16 +08001930 printk(KERN_ERR "comedi: failed to create class");
David Schleefed9eccb2008-11-04 20:29:31 -08001931 cdev_del(&comedi_cdev);
1932 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001933 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001934 return PTR_ERR(comedi_class);
1935 }
1936
1937 /* XXX requires /proc interface */
1938 comedi_proc_init();
1939
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001940 /* create devices files for legacy/manual use */
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001941 for (i = 0; i < comedi_num_legacy_minors; i++) {
David Schleefed9eccb2008-11-04 20:29:31 -08001942 int minor;
1943 minor = comedi_alloc_board_minor(NULL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001944 if (minor < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001945 comedi_cleanup_legacy_minors();
1946 cdev_del(&comedi_cdev);
1947 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001948 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001949 return minor;
1950 }
1951 }
1952
David Schleefed9eccb2008-11-04 20:29:31 -08001953 return 0;
1954}
1955
1956static void __exit comedi_cleanup(void)
1957{
1958 int i;
1959
1960 comedi_cleanup_legacy_minors();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001961 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
David Schleefed9eccb2008-11-04 20:29:31 -08001962 BUG_ON(comedi_file_info_table[i]);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001963
David Schleefed9eccb2008-11-04 20:29:31 -08001964 class_destroy(comedi_class);
1965 cdev_del(&comedi_cdev);
1966 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
1967
1968 comedi_proc_cleanup();
David Schleefed9eccb2008-11-04 20:29:31 -08001969}
1970
1971module_init(comedi_init);
1972module_exit(comedi_cleanup);
1973
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001974void comedi_error(const struct comedi_device *dev, const char *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001975{
Mark Rankilor3fffdf22010-04-29 18:17:16 +08001976 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
1977 dev->driver->driver_name, s);
David Schleefed9eccb2008-11-04 20:29:31 -08001978}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07001979EXPORT_SYMBOL(comedi_error);
David Schleefed9eccb2008-11-04 20:29:31 -08001980
Bill Pemberton34c43922009-03-16 22:05:14 -04001981void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001982{
Bill Pembertond1636792009-03-16 22:05:20 -04001983 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001984 unsigned runflags = 0;
1985 unsigned runflags_mask = 0;
1986
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001987 /* DPRINTK("comedi_event 0x%x\n",mask); */
David Schleefed9eccb2008-11-04 20:29:31 -08001988
1989 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
1990 return;
1991
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301992 if (s->
1993 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
1994 COMEDI_CB_OVERFLOW)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001995 runflags_mask |= SRF_RUNNING;
1996 }
1997 /* remember if an error event has occured, so an error
1998 * can be returned the next time the user does a read() */
1999 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2000 runflags_mask |= SRF_ERROR;
2001 runflags |= SRF_ERROR;
2002 }
2003 if (runflags_mask) {
2004 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2005 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2006 }
2007
2008 if (async->cb_mask & s->async->events) {
2009 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
Greg Kroah-Hartmanfcea1152009-04-27 15:15:30 -07002010 wake_up_interruptible(&async->wait_head);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002011 if (s->subdev_flags & SDF_CMD_READ)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302012 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002013 if (s->subdev_flags & SDF_CMD_WRITE)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302014 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
David Schleefed9eccb2008-11-04 20:29:31 -08002015 } else {
2016 if (async->cb_func)
2017 async->cb_func(s->async->events, async->cb_arg);
David Schleefed9eccb2008-11-04 20:29:31 -08002018 }
2019 }
2020 s->async->events = 0;
2021}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002022EXPORT_SYMBOL(comedi_event);
David Schleefed9eccb2008-11-04 20:29:31 -08002023
Bill Pemberton34c43922009-03-16 22:05:14 -04002024void comedi_set_subdevice_runflags(struct comedi_subdevice *s, unsigned mask,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002025 unsigned bits)
David Schleefed9eccb2008-11-04 20:29:31 -08002026{
2027 unsigned long flags;
2028
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002029 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002030 s->runflags &= ~mask;
2031 s->runflags |= (bits & mask);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002032 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002033}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002034EXPORT_SYMBOL(comedi_set_subdevice_runflags);
David Schleefed9eccb2008-11-04 20:29:31 -08002035
Bill Pemberton34c43922009-03-16 22:05:14 -04002036unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002037{
2038 unsigned long flags;
2039 unsigned runflags;
2040
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002041 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002042 runflags = s->runflags;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002043 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002044 return runflags;
2045}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002046EXPORT_SYMBOL(comedi_get_subdevice_runflags);
David Schleefed9eccb2008-11-04 20:29:31 -08002047
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002048static int is_device_busy(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002049{
Bill Pemberton34c43922009-03-16 22:05:14 -04002050 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08002051 int i;
2052
2053 if (!dev->attached)
2054 return 0;
2055
2056 for (i = 0; i < dev->n_subdevices; i++) {
2057 s = dev->subdevices + i;
2058 if (s->busy)
2059 return 1;
2060 if (s->async && s->async->mmap_count)
2061 return 1;
2062 }
2063
2064 return 0;
2065}
2066
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002067void comedi_device_init(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002068{
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002069 memset(dev, 0, sizeof(struct comedi_device));
David Schleefed9eccb2008-11-04 20:29:31 -08002070 spin_lock_init(&dev->spinlock);
2071 mutex_init(&dev->mutex);
2072 dev->minor = -1;
2073}
2074
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002075void comedi_device_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002076{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002077 if (dev == NULL)
2078 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002079 mutex_lock(&dev->mutex);
2080 comedi_device_detach(dev);
2081 mutex_unlock(&dev->mutex);
2082 mutex_destroy(&dev->mutex);
2083}
2084
2085int comedi_alloc_board_minor(struct device *hardware_device)
2086{
2087 unsigned long flags;
2088 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002089 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002090 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002091 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002092
2093 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002094 if (info == NULL)
2095 return -ENOMEM;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002096 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002097 if (info->device == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002098 kfree(info);
2099 return -ENOMEM;
2100 }
2101 comedi_device_init(info->device);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002102 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002103 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2104 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002105 comedi_file_info_table[i] = info;
2106 break;
2107 }
2108 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002109 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002110 if (i == COMEDI_NUM_BOARD_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002111 comedi_device_cleanup(info->device);
2112 kfree(info->device);
2113 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302114 printk(KERN_ERR
Pieter De Praetere20617f22010-03-10 09:47:44 +01002115
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302116 "comedi: error: ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002117 return -EBUSY;
2118 }
2119 info->device->minor = i;
2120 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002121 MKDEV(COMEDI_MAJOR, i), NULL,
2122 hardware_device, "comedi%i", i);
2123 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002124 info->device->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002125 dev_set_drvdata(csdev, info);
2126 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2127 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302128 printk(KERN_ERR
2129 "comedi: failed to create sysfs attribute file \"%s\".\n",
2130 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002131 comedi_free_board_minor(i);
2132 return retval;
2133 }
2134 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2135 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302136 printk(KERN_ERR
2137 "comedi: failed to create sysfs attribute file \"%s\".\n",
2138 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002139 comedi_free_board_minor(i);
2140 return retval;
2141 }
2142 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2143 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302144 printk(KERN_ERR
2145 "comedi: failed to create sysfs attribute file \"%s\".\n",
2146 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002147 comedi_free_board_minor(i);
2148 return retval;
2149 }
2150 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2151 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302152 printk(KERN_ERR
2153 "comedi: failed to create sysfs attribute file \"%s\".\n",
2154 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002155 comedi_free_board_minor(i);
2156 return retval;
2157 }
David Schleefed9eccb2008-11-04 20:29:31 -08002158 return i;
2159}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002160EXPORT_SYMBOL_GPL(comedi_alloc_board_minor);
David Schleefed9eccb2008-11-04 20:29:31 -08002161
2162void comedi_free_board_minor(unsigned minor)
2163{
2164 unsigned long flags;
2165 struct comedi_device_file_info *info;
2166
2167 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002168 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002169 info = comedi_file_info_table[minor];
2170 comedi_file_info_table[minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002171 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002172
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002173 if (info) {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002174 struct comedi_device *dev = info->device;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002175 if (dev) {
2176 if (dev->class_dev) {
2177 device_destroy(comedi_class,
2178 MKDEV(COMEDI_MAJOR, dev->minor));
David Schleefed9eccb2008-11-04 20:29:31 -08002179 }
2180 comedi_device_cleanup(dev);
2181 kfree(dev);
2182 }
2183 kfree(info);
2184 }
2185}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002186EXPORT_SYMBOL_GPL(comedi_free_board_minor);
David Schleefed9eccb2008-11-04 20:29:31 -08002187
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002188int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2189 struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002190{
2191 unsigned long flags;
2192 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002193 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002194 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002195 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002196
2197 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002198 if (info == NULL)
2199 return -ENOMEM;
David Schleefed9eccb2008-11-04 20:29:31 -08002200 info->device = dev;
2201 info->read_subdevice = s;
2202 info->write_subdevice = s;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002203 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Frank Mori Hess4c41f3a2008-12-09 14:47:22 +00002204 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002205 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002206 comedi_file_info_table[i] = info;
2207 break;
2208 }
2209 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002210 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002211 if (i == COMEDI_NUM_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002212 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302213 printk(KERN_ERR
2214 "comedi: error: ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002215 return -EBUSY;
2216 }
2217 s->minor = i;
2218 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002219 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2220 "comedi%i_subd%i", dev->minor,
2221 (int)(s - dev->subdevices));
2222 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002223 s->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002224 dev_set_drvdata(csdev, info);
2225 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2226 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302227 printk(KERN_ERR
2228 "comedi: failed to create sysfs attribute file \"%s\".\n",
2229 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002230 comedi_free_subdevice_minor(s);
2231 return retval;
2232 }
2233 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2234 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302235 printk(KERN_ERR
2236 "comedi: failed to create sysfs attribute file \"%s\".\n",
2237 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002238 comedi_free_subdevice_minor(s);
2239 return retval;
2240 }
2241 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2242 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302243 printk(KERN_ERR
2244 "comedi: failed to create sysfs attribute file \"%s\".\n",
2245 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002246 comedi_free_subdevice_minor(s);
2247 return retval;
2248 }
2249 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2250 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302251 printk(KERN_ERR
2252 "comedi: failed to create sysfs attribute file \"%s\".\n",
2253 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002254 comedi_free_subdevice_minor(s);
2255 return retval;
2256 }
David Schleefed9eccb2008-11-04 20:29:31 -08002257 return i;
2258}
2259
Bill Pemberton34c43922009-03-16 22:05:14 -04002260void comedi_free_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002261{
2262 unsigned long flags;
2263 struct comedi_device_file_info *info;
2264
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002265 if (s == NULL)
2266 return;
2267 if (s->minor < 0)
2268 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002269
2270 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2271 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2272
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002273 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002274 info = comedi_file_info_table[s->minor];
2275 comedi_file_info_table[s->minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002276 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002277
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002278 if (s->class_dev) {
David Schleefed9eccb2008-11-04 20:29:31 -08002279 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2280 s->class_dev = NULL;
2281 }
2282 kfree(info);
2283}
2284
2285struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2286{
2287 unsigned long flags;
2288 struct comedi_device_file_info *info;
2289
2290 BUG_ON(minor >= COMEDI_NUM_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002291 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002292 info = comedi_file_info_table[minor];
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002293 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002294 return info;
2295}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002296EXPORT_SYMBOL_GPL(comedi_get_device_file_info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002297
2298static int resize_async_buffer(struct comedi_device *dev,
2299 struct comedi_subdevice *s,
2300 struct comedi_async *async, unsigned new_size)
2301{
2302 int retval;
2303
2304 if (new_size > async->max_bufsize)
2305 return -EPERM;
2306
2307 if (s->busy) {
2308 DPRINTK("subdevice is busy, cannot resize buffer\n");
2309 return -EBUSY;
2310 }
2311 if (async->mmap_count) {
2312 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2313 return -EBUSY;
2314 }
2315
2316 if (!async->prealloc_buf)
2317 return -EINVAL;
2318
2319 /* make sure buffer is an integral number of pages
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302320 * (we round up) */
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002321 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2322
2323 retval = comedi_buf_alloc(dev, s, new_size);
2324 if (retval < 0)
2325 return retval;
2326
2327 if (s->buf_change) {
2328 retval = s->buf_change(dev, s, new_size);
2329 if (retval < 0)
2330 return retval;
2331 }
2332
2333 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
Ian Abbottb8b5cd92009-09-21 14:55:23 -04002334 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002335 return 0;
2336}
2337
2338/* sysfs attribute files */
2339
2340static const unsigned bytes_per_kibi = 1024;
2341
2342static ssize_t show_max_read_buffer_kb(struct device *dev,
2343 struct device_attribute *attr, char *buf)
2344{
2345 ssize_t retval;
2346 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2347 unsigned max_buffer_size_kb = 0;
2348 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302349 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002350
2351 mutex_lock(&info->device->mutex);
2352 if (read_subdevice &&
2353 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2354 read_subdevice->async) {
2355 max_buffer_size_kb = read_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302356 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002357 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302358 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002359 mutex_unlock(&info->device->mutex);
2360
2361 return retval;
2362}
2363
2364static ssize_t store_max_read_buffer_kb(struct device *dev,
2365 struct device_attribute *attr,
2366 const char *buf, size_t count)
2367{
2368 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2369 unsigned long new_max_size_kb;
2370 uint64_t new_max_size;
2371 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302372 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002373
2374 if (strict_strtoul(buf, 10, &new_max_size_kb))
2375 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302376 if (new_max_size_kb != (uint32_t) new_max_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002377 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302378 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2379 if (new_max_size != (uint32_t) new_max_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002380 return -EINVAL;
2381
2382 mutex_lock(&info->device->mutex);
2383 if (read_subdevice == NULL ||
2384 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2385 read_subdevice->async == NULL) {
2386 mutex_unlock(&info->device->mutex);
2387 return -EINVAL;
2388 }
2389 read_subdevice->async->max_bufsize = new_max_size;
2390 mutex_unlock(&info->device->mutex);
2391
2392 return count;
2393}
2394
2395static struct device_attribute dev_attr_max_read_buffer_kb = {
2396 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302397 .name = "max_read_buffer_kb",
2398 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002399 .show = &show_max_read_buffer_kb,
2400 .store = &store_max_read_buffer_kb
2401};
2402
2403static ssize_t show_read_buffer_kb(struct device *dev,
2404 struct device_attribute *attr, char *buf)
2405{
2406 ssize_t retval;
2407 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2408 unsigned buffer_size_kb = 0;
2409 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302410 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002411
2412 mutex_lock(&info->device->mutex);
2413 if (read_subdevice &&
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302414 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2415 read_subdevice->async) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002416 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302417 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002418 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302419 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002420 mutex_unlock(&info->device->mutex);
2421
2422 return retval;
2423}
2424
2425static ssize_t store_read_buffer_kb(struct device *dev,
2426 struct device_attribute *attr,
2427 const char *buf, size_t count)
2428{
2429 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2430 unsigned long new_size_kb;
2431 uint64_t new_size;
2432 int retval;
2433 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302434 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002435
2436 if (strict_strtoul(buf, 10, &new_size_kb))
2437 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302438 if (new_size_kb != (uint32_t) new_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002439 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302440 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2441 if (new_size != (uint32_t) new_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002442 return -EINVAL;
2443
2444 mutex_lock(&info->device->mutex);
2445 if (read_subdevice == NULL ||
2446 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2447 read_subdevice->async == NULL) {
2448 mutex_unlock(&info->device->mutex);
2449 return -EINVAL;
2450 }
2451 retval = resize_async_buffer(info->device, read_subdevice,
2452 read_subdevice->async, new_size);
2453 mutex_unlock(&info->device->mutex);
2454
2455 if (retval < 0)
2456 return retval;
2457 return count;
2458}
2459
2460static struct device_attribute dev_attr_read_buffer_kb = {
2461 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302462 .name = "read_buffer_kb",
2463 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002464 .show = &show_read_buffer_kb,
2465 .store = &store_read_buffer_kb
2466};
2467
2468static ssize_t show_max_write_buffer_kb(struct device *dev,
2469 struct device_attribute *attr,
2470 char *buf)
2471{
2472 ssize_t retval;
2473 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2474 unsigned max_buffer_size_kb = 0;
2475 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302476 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002477
2478 mutex_lock(&info->device->mutex);
2479 if (write_subdevice &&
2480 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2481 write_subdevice->async) {
2482 max_buffer_size_kb = write_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302483 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002484 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302485 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002486 mutex_unlock(&info->device->mutex);
2487
2488 return retval;
2489}
2490
2491static ssize_t store_max_write_buffer_kb(struct device *dev,
2492 struct device_attribute *attr,
2493 const char *buf, size_t count)
2494{
2495 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2496 unsigned long new_max_size_kb;
2497 uint64_t new_max_size;
2498 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302499 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002500
2501 if (strict_strtoul(buf, 10, &new_max_size_kb))
2502 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302503 if (new_max_size_kb != (uint32_t) new_max_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002504 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302505 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2506 if (new_max_size != (uint32_t) new_max_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002507 return -EINVAL;
2508
2509 mutex_lock(&info->device->mutex);
2510 if (write_subdevice == NULL ||
2511 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2512 write_subdevice->async == NULL) {
2513 mutex_unlock(&info->device->mutex);
2514 return -EINVAL;
2515 }
2516 write_subdevice->async->max_bufsize = new_max_size;
2517 mutex_unlock(&info->device->mutex);
2518
2519 return count;
2520}
2521
2522static struct device_attribute dev_attr_max_write_buffer_kb = {
2523 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302524 .name = "max_write_buffer_kb",
2525 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002526 .show = &show_max_write_buffer_kb,
2527 .store = &store_max_write_buffer_kb
2528};
2529
2530static ssize_t show_write_buffer_kb(struct device *dev,
2531 struct device_attribute *attr, char *buf)
2532{
2533 ssize_t retval;
2534 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2535 unsigned buffer_size_kb = 0;
2536 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302537 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002538
2539 mutex_lock(&info->device->mutex);
2540 if (write_subdevice &&
2541 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2542 write_subdevice->async) {
2543 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302544 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002545 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302546 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002547 mutex_unlock(&info->device->mutex);
2548
2549 return retval;
2550}
2551
2552static ssize_t store_write_buffer_kb(struct device *dev,
2553 struct device_attribute *attr,
2554 const char *buf, size_t count)
2555{
2556 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2557 unsigned long new_size_kb;
2558 uint64_t new_size;
2559 int retval;
2560 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302561 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002562
2563 if (strict_strtoul(buf, 10, &new_size_kb))
2564 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302565 if (new_size_kb != (uint32_t) new_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002566 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302567 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2568 if (new_size != (uint32_t) new_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002569 return -EINVAL;
2570
2571 mutex_lock(&info->device->mutex);
2572 if (write_subdevice == NULL ||
2573 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2574 write_subdevice->async == NULL) {
2575 mutex_unlock(&info->device->mutex);
2576 return -EINVAL;
2577 }
2578 retval = resize_async_buffer(info->device, write_subdevice,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302579 write_subdevice->async, new_size);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002580 mutex_unlock(&info->device->mutex);
2581
2582 if (retval < 0)
2583 return retval;
2584 return count;
2585}
2586
2587static struct device_attribute dev_attr_write_buffer_kb = {
2588 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302589 .name = "write_buffer_kb",
2590 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002591 .show = &show_write_buffer_kb,
2592 .store = &store_write_buffer_kb
2593};