blob: aca96747e5e2da1a24964f6bb5789aec1ad9b47b [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
2 comedi/comedi_fops.c
3 comedi kernel module
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23
24#undef DEBUG
25
26#define __NO_VERSION__
27#include "comedi_fops.h"
28#include "comedi_compat32.h"
29
30#include <linux/module.h>
31#include <linux/errno.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/fcntl.h>
35#include <linux/delay.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
38#include <linux/slab.h>
39#include <linux/kmod.h>
40#include <linux/poll.h>
41#include <linux/init.h>
42#include <linux/device.h>
43#include <linux/vmalloc.h>
44#include <linux/fs.h>
45#include "comedidev.h"
46#include <linux/cdev.h>
Frank Mori Hess883db3d2009-04-14 11:21:41 -040047#include <linux/stat.h>
David Schleefed9eccb2008-11-04 20:29:31 -080048
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080049#include <linux/io.h>
50#include <linux/uaccess.h>
David Schleefed9eccb2008-11-04 20:29:31 -080051
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080052/* #include "kvmem.h" */
David Schleefed9eccb2008-11-04 20:29:31 -080053
54MODULE_AUTHOR("http://www.comedi.org");
55MODULE_DESCRIPTION("Comedi core module");
56MODULE_LICENSE("GPL");
57
58#ifdef CONFIG_COMEDI_DEBUG
59int comedi_debug;
60module_param(comedi_debug, int, 0644);
61#endif
62
Ian Abbott6a9d7a22008-12-08 17:05:50 +000063int comedi_autoconfig = 1;
64module_param(comedi_autoconfig, bool, 0444);
65
Andrea Gelmini6705b682010-02-26 10:14:55 +010066int comedi_num_legacy_minors;
Bernd Porr1dd33ab2008-12-08 23:30:13 +000067module_param(comedi_num_legacy_minors, int, 0444);
68
David Schleefed9eccb2008-11-04 20:29:31 -080069static DEFINE_SPINLOCK(comedi_file_info_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080070static struct comedi_device_file_info
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053071*comedi_file_info_table[COMEDI_NUM_MINORS];
David Schleefed9eccb2008-11-04 20:29:31 -080072
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053073static int do_devconfig_ioctl(struct comedi_device *dev,
74 struct comedi_devconfig *arg);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040075static int do_bufconfig_ioctl(struct comedi_device *dev, void *arg);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053076static int do_devinfo_ioctl(struct comedi_device *dev,
77 struct comedi_devinfo *arg, struct file *file);
78static int do_subdinfo_ioctl(struct comedi_device *dev,
79 struct comedi_subdinfo *arg, void *file);
80static int do_chaninfo_ioctl(struct comedi_device *dev,
81 struct comedi_chaninfo *arg);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040082static int do_bufinfo_ioctl(struct comedi_device *dev, void *arg);
83static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053084static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
85 void *file);
86static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
87 void *file);
88static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
89 void *file);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040090static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file);
91static int do_insnlist_ioctl(struct comedi_device *dev, void *arg, void *file);
92static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053093static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
94 void *file);
David Schleefed9eccb2008-11-04 20:29:31 -080095
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053096extern void do_become_nonbusy(struct comedi_device *dev,
97 struct comedi_subdevice *s);
Bill Pemberton34c43922009-03-16 22:05:14 -040098static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
David Schleefed9eccb2008-11-04 20:29:31 -080099
100static int comedi_fasync(int fd, struct file *file, int on);
101
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400102static int is_device_busy(struct comedi_device *dev);
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400103static int resize_async_buffer(struct comedi_device *dev,
104 struct comedi_subdevice *s,
105 struct comedi_async *async, unsigned new_size);
106
107/* declarations for sysfs attribute files */
108static struct device_attribute dev_attr_max_read_buffer_kb;
109static struct device_attribute dev_attr_read_buffer_kb;
110static struct device_attribute dev_attr_max_write_buffer_kb;
111static struct device_attribute dev_attr_write_buffer_kb;
David Schleefed9eccb2008-11-04 20:29:31 -0800112
David Schleefed9eccb2008-11-04 20:29:31 -0800113static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800114 unsigned long arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800115{
116 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800117 struct comedi_device_file_info *dev_file_info =
118 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400119 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800120 int rc;
121
Frank Mori Hess53b670a2008-12-15 13:48:47 +0000122 if (dev_file_info == NULL || dev_file_info->device == NULL)
123 return -ENODEV;
124 dev = dev_file_info->device;
125
David Schleefed9eccb2008-11-04 20:29:31 -0800126 mutex_lock(&dev->mutex);
127
128 /* Device config is special, because it must work on
129 * an unconfigured device. */
130 if (cmd == COMEDI_DEVCONFIG) {
131 rc = do_devconfig_ioctl(dev, (void *)arg);
132 goto done;
133 }
134
135 if (!dev->attached) {
136 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
137 rc = -ENODEV;
138 goto done;
139 }
140
141 switch (cmd) {
142 case COMEDI_BUFCONFIG:
143 rc = do_bufconfig_ioctl(dev, (void *)arg);
144 break;
145 case COMEDI_DEVINFO:
146 rc = do_devinfo_ioctl(dev, (void *)arg, file);
147 break;
148 case COMEDI_SUBDINFO:
149 rc = do_subdinfo_ioctl(dev, (void *)arg, file);
150 break;
151 case COMEDI_CHANINFO:
152 rc = do_chaninfo_ioctl(dev, (void *)arg);
153 break;
154 case COMEDI_RANGEINFO:
155 rc = do_rangeinfo_ioctl(dev, (void *)arg);
156 break;
157 case COMEDI_BUFINFO:
158 rc = do_bufinfo_ioctl(dev, (void *)arg);
159 break;
160 case COMEDI_LOCK:
161 rc = do_lock_ioctl(dev, arg, file);
162 break;
163 case COMEDI_UNLOCK:
164 rc = do_unlock_ioctl(dev, arg, file);
165 break;
166 case COMEDI_CANCEL:
167 rc = do_cancel_ioctl(dev, arg, file);
168 break;
169 case COMEDI_CMD:
170 rc = do_cmd_ioctl(dev, (void *)arg, file);
171 break;
172 case COMEDI_CMDTEST:
173 rc = do_cmdtest_ioctl(dev, (void *)arg, file);
174 break;
175 case COMEDI_INSNLIST:
176 rc = do_insnlist_ioctl(dev, (void *)arg, file);
177 break;
178 case COMEDI_INSN:
179 rc = do_insn_ioctl(dev, (void *)arg, file);
180 break;
181 case COMEDI_POLL:
182 rc = do_poll_ioctl(dev, arg, file);
183 break;
184 default:
185 rc = -ENOTTY;
186 break;
187 }
188
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800189done:
David Schleefed9eccb2008-11-04 20:29:31 -0800190 mutex_unlock(&dev->mutex);
191 return rc;
192}
193
194/*
195 COMEDI_DEVCONFIG
196 device config ioctl
197
198 arg:
199 pointer to devconfig structure
200
201 reads:
202 devconfig structure at arg
203
204 writes:
205 none
206*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530207static int do_devconfig_ioctl(struct comedi_device *dev,
208 struct comedi_devconfig *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800209{
Bill Pemberton0707bb02009-03-16 22:06:20 -0400210 struct comedi_devconfig it;
David Schleefed9eccb2008-11-04 20:29:31 -0800211 int ret;
212 unsigned char *aux_data = NULL;
213 int aux_len;
214
215 if (!capable(CAP_SYS_ADMIN))
216 return -EPERM;
217
218 if (arg == NULL) {
219 if (is_device_busy(dev))
220 return -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800221 if (dev->attached) {
David Schleefed9eccb2008-11-04 20:29:31 -0800222 struct module *driver_module = dev->driver->module;
223 comedi_device_detach(dev);
224 module_put(driver_module);
225 }
226 return 0;
227 }
228
Bill Pemberton0707bb02009-03-16 22:06:20 -0400229 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800230 return -EFAULT;
231
232 it.board_name[COMEDI_NAMELEN - 1] = 0;
233
234 if (comedi_aux_data(it.options, 0) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800235 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
David Schleefed9eccb2008-11-04 20:29:31 -0800236 int bit_shift;
237 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
238 if (aux_len < 0)
239 return -EFAULT;
240
241 aux_data = vmalloc(aux_len);
242 if (!aux_data)
243 return -ENOMEM;
244
245 if (copy_from_user(aux_data,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800246 comedi_aux_data(it.options, 0), aux_len)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800247 vfree(aux_data);
248 return -EFAULT;
249 }
250 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800251 (unsigned long)aux_data;
David Schleefed9eccb2008-11-04 20:29:31 -0800252 if (sizeof(void *) > sizeof(int)) {
253 bit_shift = sizeof(int) * 8;
254 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800255 ((unsigned long)aux_data) >> bit_shift;
David Schleefed9eccb2008-11-04 20:29:31 -0800256 } else
257 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
258 }
259
260 ret = comedi_device_attach(dev, &it);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800261 if (ret == 0) {
262 if (!try_module_get(dev->driver->module)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800263 comedi_device_detach(dev);
264 return -ENOSYS;
265 }
266 }
267
268 if (aux_data)
269 vfree(aux_data);
270
271 return ret;
272}
273
274/*
275 COMEDI_BUFCONFIG
276 buffer configuration ioctl
277
278 arg:
279 pointer to bufconfig structure
280
281 reads:
282 bufconfig at arg
283
284 writes:
285 modified bufconfig at arg
286
287*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400288static int do_bufconfig_ioctl(struct comedi_device *dev, void *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800289{
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400290 struct comedi_bufconfig bc;
Bill Pembertond1636792009-03-16 22:05:20 -0400291 struct comedi_async *async;
Bill Pemberton34c43922009-03-16 22:05:14 -0400292 struct comedi_subdevice *s;
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400293 int retval = 0;
David Schleefed9eccb2008-11-04 20:29:31 -0800294
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400295 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800296 return -EFAULT;
297
298 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
299 return -EINVAL;
300
301 s = dev->subdevices + bc.subdevice;
302 async = s->async;
303
304 if (!async) {
305 DPRINTK("subdevice does not have async capability\n");
306 bc.size = 0;
307 bc.maximum_size = 0;
308 goto copyback;
309 }
310
311 if (bc.maximum_size) {
312 if (!capable(CAP_SYS_ADMIN))
313 return -EPERM;
314
315 async->max_bufsize = bc.maximum_size;
316 }
317
318 if (bc.size) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400319 retval = resize_async_buffer(dev, s, async, bc.size);
320 if (retval < 0)
321 return retval;
David Schleefed9eccb2008-11-04 20:29:31 -0800322 }
323
324 bc.size = async->prealloc_bufsz;
325 bc.maximum_size = async->max_bufsize;
326
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800327copyback:
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400328 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800329 return -EFAULT;
330
331 return 0;
332}
333
334/*
335 COMEDI_DEVINFO
336 device info ioctl
337
338 arg:
339 pointer to devinfo structure
340
341 reads:
342 none
343
344 writes:
345 devinfo structure
346
347*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530348static int do_devinfo_ioctl(struct comedi_device *dev,
349 struct comedi_devinfo *arg, struct file *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800350{
Bill Pemberton063db042009-03-16 22:06:15 -0400351 struct comedi_devinfo devinfo;
David Schleefed9eccb2008-11-04 20:29:31 -0800352 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800353 struct comedi_device_file_info *dev_file_info =
354 comedi_get_device_file_info(minor);
Bill Pemberton34c43922009-03-16 22:05:14 -0400355 struct comedi_subdevice *read_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800356 comedi_get_read_subdevice(dev_file_info);
Bill Pemberton34c43922009-03-16 22:05:14 -0400357 struct comedi_subdevice *write_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800358 comedi_get_write_subdevice(dev_file_info);
David Schleefed9eccb2008-11-04 20:29:31 -0800359
360 memset(&devinfo, 0, sizeof(devinfo));
361
362 /* fill devinfo structure */
363 devinfo.version_code = COMEDI_VERSION_CODE;
364 devinfo.n_subdevs = dev->n_subdevices;
365 memcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
366 memcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
367
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800368 if (read_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800369 devinfo.read_subdevice = read_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800370 else
David Schleefed9eccb2008-11-04 20:29:31 -0800371 devinfo.read_subdevice = -1;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800372
373 if (write_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800374 devinfo.write_subdevice = write_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800375 else
David Schleefed9eccb2008-11-04 20:29:31 -0800376 devinfo.write_subdevice = -1;
David Schleefed9eccb2008-11-04 20:29:31 -0800377
Bill Pemberton063db042009-03-16 22:06:15 -0400378 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800379 return -EFAULT;
380
381 return 0;
382}
383
384/*
385 COMEDI_SUBDINFO
386 subdevice info ioctl
387
388 arg:
389 pointer to array of subdevice info structures
390
391 reads:
392 none
393
394 writes:
395 array of subdevice info structures at arg
396
397*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530398static int do_subdinfo_ioctl(struct comedi_device *dev,
399 struct comedi_subdinfo *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800400{
401 int ret, i;
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400402 struct comedi_subdinfo *tmp, *us;
Bill Pemberton34c43922009-03-16 22:05:14 -0400403 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800404
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530405 tmp =
406 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
407 GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800408 if (!tmp)
409 return -ENOMEM;
410
411 /* fill subdinfo structs */
412 for (i = 0; i < dev->n_subdevices; i++) {
413 s = dev->subdevices + i;
414 us = tmp + i;
415
416 us->type = s->type;
417 us->n_chan = s->n_chan;
418 us->subd_flags = s->subdev_flags;
419 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
420 us->subd_flags |= SDF_RUNNING;
421#define TIMER_nanosec 5 /* backwards compatibility */
422 us->timer_type = TIMER_nanosec;
423 us->len_chanlist = s->len_chanlist;
424 us->maxdata = s->maxdata;
425 if (s->range_table) {
426 us->range_type =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800427 (i << 24) | (0 << 16) | (s->range_table->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800428 } else {
429 us->range_type = 0; /* XXX */
430 }
431 us->flags = s->flags;
432
433 if (s->busy)
434 us->subd_flags |= SDF_BUSY;
435 if (s->busy == file)
436 us->subd_flags |= SDF_BUSY_OWNER;
437 if (s->lock)
438 us->subd_flags |= SDF_LOCKED;
439 if (s->lock == file)
440 us->subd_flags |= SDF_LOCK_OWNER;
441 if (!s->maxdata && s->maxdata_list)
442 us->subd_flags |= SDF_MAXDATA;
443 if (s->flaglist)
444 us->subd_flags |= SDF_FLAGS;
445 if (s->range_table_list)
446 us->subd_flags |= SDF_RANGETYPE;
447 if (s->do_cmd)
448 us->subd_flags |= SDF_CMD;
449
450 if (s->insn_bits != &insn_inval)
451 us->insn_bits_support = COMEDI_SUPPORTED;
452 else
453 us->insn_bits_support = COMEDI_UNSUPPORTED;
454
455 us->settling_time_0 = s->settling_time_0;
456 }
457
458 ret = copy_to_user(arg, tmp,
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400459 dev->n_subdevices * sizeof(struct comedi_subdinfo));
David Schleefed9eccb2008-11-04 20:29:31 -0800460
461 kfree(tmp);
462
463 return ret ? -EFAULT : 0;
464}
465
466/*
467 COMEDI_CHANINFO
468 subdevice info ioctl
469
470 arg:
471 pointer to chaninfo structure
472
473 reads:
474 chaninfo structure at arg
475
476 writes:
477 arrays at elements of chaninfo structure
478
479*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530480static int do_chaninfo_ioctl(struct comedi_device *dev,
481 struct comedi_chaninfo *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800482{
Bill Pemberton34c43922009-03-16 22:05:14 -0400483 struct comedi_subdevice *s;
Bill Pembertona18b4162009-03-16 22:06:04 -0400484 struct comedi_chaninfo it;
David Schleefed9eccb2008-11-04 20:29:31 -0800485
Bill Pembertona18b4162009-03-16 22:06:04 -0400486 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800487 return -EFAULT;
488
489 if (it.subdev >= dev->n_subdevices)
490 return -EINVAL;
491 s = dev->subdevices + it.subdev;
492
493 if (it.maxdata_list) {
494 if (s->maxdata || !s->maxdata_list)
495 return -EINVAL;
496 if (copy_to_user(it.maxdata_list, s->maxdata_list,
Bill Pemberton790c5542009-03-16 22:05:02 -0400497 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800498 return -EFAULT;
499 }
500
501 if (it.flaglist) {
502 if (!s->flaglist)
503 return -EINVAL;
504 if (copy_to_user(it.flaglist, s->flaglist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800505 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800506 return -EFAULT;
507 }
508
509 if (it.rangelist) {
510 int i;
511
512 if (!s->range_table_list)
513 return -EINVAL;
514 for (i = 0; i < s->n_chan; i++) {
515 int x;
516
517 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800518 (s->range_table_list[i]->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800519 put_user(x, it.rangelist + i);
520 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800521#if 0
522 if (copy_to_user(it.rangelist, s->range_type_list,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530523 s->n_chan * sizeof(unsigned int)))
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800524 return -EFAULT;
525#endif
David Schleefed9eccb2008-11-04 20:29:31 -0800526 }
527
528 return 0;
529}
530
531 /*
532 COMEDI_BUFINFO
533 buffer information ioctl
534
535 arg:
536 pointer to bufinfo structure
537
538 reads:
539 bufinfo at arg
540
541 writes:
542 modified bufinfo at arg
543
544 */
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400545static int do_bufinfo_ioctl(struct comedi_device *dev, void *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800546{
Bill Pemberton9aa53392009-03-16 22:06:42 -0400547 struct comedi_bufinfo bi;
Bill Pemberton34c43922009-03-16 22:05:14 -0400548 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400549 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800550
Bill Pemberton9aa53392009-03-16 22:06:42 -0400551 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800552 return -EFAULT;
553
554 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
555 return -EINVAL;
556
557 s = dev->subdevices + bi.subdevice;
558 async = s->async;
559
560 if (!async) {
561 DPRINTK("subdevice does not have async capability\n");
562 bi.buf_write_ptr = 0;
563 bi.buf_read_ptr = 0;
564 bi.buf_write_count = 0;
565 bi.buf_read_count = 0;
566 goto copyback;
567 }
568
569 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
570 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
571 comedi_buf_read_free(async, bi.bytes_read);
572
573 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800574 SRF_RUNNING))
575 && async->buf_write_count == async->buf_read_count) {
David Schleefed9eccb2008-11-04 20:29:31 -0800576 do_become_nonbusy(dev, s);
577 }
578 }
579
580 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
581 bi.bytes_written =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800582 comedi_buf_write_alloc(async, bi.bytes_written);
David Schleefed9eccb2008-11-04 20:29:31 -0800583 comedi_buf_write_free(async, bi.bytes_written);
584 }
585
586 bi.buf_write_count = async->buf_write_count;
587 bi.buf_write_ptr = async->buf_write_ptr;
588 bi.buf_read_count = async->buf_read_count;
589 bi.buf_read_ptr = async->buf_read_ptr;
590
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800591copyback:
Bill Pemberton9aa53392009-03-16 22:06:42 -0400592 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800593 return -EFAULT;
594
595 return 0;
596}
597
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530598static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
599 unsigned int *data, void *file);
David Schleefed9eccb2008-11-04 20:29:31 -0800600/*
601 * COMEDI_INSNLIST
602 * synchronous instructions
603 *
604 * arg:
605 * pointer to sync cmd structure
606 *
607 * reads:
608 * sync cmd struct at arg
609 * instruction list
610 * data (for writes)
611 *
612 * writes:
613 * data (for reads)
614 */
615/* arbitrary limits */
616#define MAX_SAMPLES 256
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400617static int do_insnlist_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800618{
Bill Pembertonda613f42009-03-16 22:05:59 -0400619 struct comedi_insnlist insnlist;
Bill Pemberton90035c02009-03-16 22:05:53 -0400620 struct comedi_insn *insns = NULL;
Bill Pemberton790c5542009-03-16 22:05:02 -0400621 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800622 int i = 0;
623 int ret = 0;
624
Bill Pembertonda613f42009-03-16 22:05:59 -0400625 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
David Schleefed9eccb2008-11-04 20:29:31 -0800626 return -EFAULT;
627
Bill Pemberton790c5542009-03-16 22:05:02 -0400628 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800629 if (!data) {
630 DPRINTK("kmalloc failed\n");
631 ret = -ENOMEM;
632 goto error;
633 }
634
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530635 insns =
636 kmalloc(sizeof(struct comedi_insn) * insnlist.n_insns, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800637 if (!insns) {
638 DPRINTK("kmalloc failed\n");
639 ret = -ENOMEM;
640 goto error;
641 }
642
643 if (copy_from_user(insns, insnlist.insns,
Bill Pemberton90035c02009-03-16 22:05:53 -0400644 sizeof(struct comedi_insn) * insnlist.n_insns)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800645 DPRINTK("copy_from_user failed\n");
646 ret = -EFAULT;
647 goto error;
648 }
649
650 for (i = 0; i < insnlist.n_insns; i++) {
651 if (insns[i].n > MAX_SAMPLES) {
652 DPRINTK("number of samples too large\n");
653 ret = -EINVAL;
654 goto error;
655 }
656 if (insns[i].insn & INSN_MASK_WRITE) {
657 if (copy_from_user(data, insns[i].data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400658 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800659 DPRINTK("copy_from_user failed\n");
660 ret = -EFAULT;
661 goto error;
662 }
663 }
664 ret = parse_insn(dev, insns + i, data, file);
665 if (ret < 0)
666 goto error;
667 if (insns[i].insn & INSN_MASK_READ) {
668 if (copy_to_user(insns[i].data, data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400669 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800670 DPRINTK("copy_to_user failed\n");
671 ret = -EFAULT;
672 goto error;
673 }
674 }
675 if (need_resched())
676 schedule();
677 }
678
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800679error:
680 kfree(insns);
681 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800682
683 if (ret < 0)
684 return ret;
685 return i;
686}
687
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530688static int check_insn_config_length(struct comedi_insn *insn,
689 unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -0800690{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800691 if (insn->n < 1)
692 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -0800693
694 switch (data[0]) {
695 case INSN_CONFIG_DIO_OUTPUT:
696 case INSN_CONFIG_DIO_INPUT:
697 case INSN_CONFIG_DISARM:
698 case INSN_CONFIG_RESET:
699 if (insn->n == 1)
700 return 0;
701 break;
702 case INSN_CONFIG_ARM:
703 case INSN_CONFIG_DIO_QUERY:
704 case INSN_CONFIG_BLOCK_SIZE:
705 case INSN_CONFIG_FILTER:
706 case INSN_CONFIG_SERIAL_CLOCK:
707 case INSN_CONFIG_BIDIRECTIONAL_DATA:
708 case INSN_CONFIG_ALT_SOURCE:
709 case INSN_CONFIG_SET_COUNTER_MODE:
710 case INSN_CONFIG_8254_READ_STATUS:
711 case INSN_CONFIG_SET_ROUTING:
712 case INSN_CONFIG_GET_ROUTING:
713 case INSN_CONFIG_GET_PWM_STATUS:
714 case INSN_CONFIG_PWM_SET_PERIOD:
715 case INSN_CONFIG_PWM_GET_PERIOD:
716 if (insn->n == 2)
717 return 0;
718 break;
719 case INSN_CONFIG_SET_GATE_SRC:
720 case INSN_CONFIG_GET_GATE_SRC:
721 case INSN_CONFIG_SET_CLOCK_SRC:
722 case INSN_CONFIG_GET_CLOCK_SRC:
723 case INSN_CONFIG_SET_OTHER_SRC:
724 case INSN_CONFIG_GET_COUNTER_STATUS:
725 case INSN_CONFIG_PWM_SET_H_BRIDGE:
726 case INSN_CONFIG_PWM_GET_H_BRIDGE:
727 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
728 if (insn->n == 3)
729 return 0;
730 break;
731 case INSN_CONFIG_PWM_OUTPUT:
732 case INSN_CONFIG_ANALOG_TRIG:
733 if (insn->n == 5)
734 return 0;
735 break;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530736 /* by default we allow the insn since we don't have checks for
737 * all possible cases yet */
David Schleefed9eccb2008-11-04 20:29:31 -0800738 default:
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -0700739 printk("comedi: no check for data length of config insn id "
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530740 "%i is implemented.\n"
741 " Add a check to %s in %s.\n"
742 " Assuming n=%i is correct.\n", data[0], __func__,
743 __FILE__, insn->n);
David Schleefed9eccb2008-11-04 20:29:31 -0800744 return 0;
745 break;
746 }
747 return -EINVAL;
748}
749
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530750static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
751 unsigned int *data, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800752{
Bill Pemberton34c43922009-03-16 22:05:14 -0400753 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800754 int ret = 0;
755 int i;
756
757 if (insn->insn & INSN_MASK_SPECIAL) {
758 /* a non-subdevice instruction */
759
760 switch (insn->insn) {
761 case INSN_GTOD:
762 {
763 struct timeval tv;
764
765 if (insn->n != 2) {
766 ret = -EINVAL;
767 break;
768 }
769
770 do_gettimeofday(&tv);
771 data[0] = tv.tv_sec;
772 data[1] = tv.tv_usec;
773 ret = 2;
774
775 break;
776 }
777 case INSN_WAIT:
778 if (insn->n != 1 || data[0] >= 100000) {
779 ret = -EINVAL;
780 break;
781 }
782 udelay(data[0] / 1000);
783 ret = 1;
784 break;
785 case INSN_INTTRIG:
786 if (insn->n != 1) {
787 ret = -EINVAL;
788 break;
789 }
790 if (insn->subdev >= dev->n_subdevices) {
791 DPRINTK("%d not usable subdevice\n",
792 insn->subdev);
793 ret = -EINVAL;
794 break;
795 }
796 s = dev->subdevices + insn->subdev;
797 if (!s->async) {
798 DPRINTK("no async\n");
799 ret = -EINVAL;
800 break;
801 }
802 if (!s->async->inttrig) {
803 DPRINTK("no inttrig\n");
804 ret = -EAGAIN;
805 break;
806 }
807 ret = s->async->inttrig(dev, s, insn->data[0]);
808 if (ret >= 0)
809 ret = 1;
810 break;
811 default:
812 DPRINTK("invalid insn\n");
813 ret = -EINVAL;
814 break;
815 }
816 } else {
817 /* a subdevice instruction */
Bill Pemberton790c5542009-03-16 22:05:02 -0400818 unsigned int maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800819
820 if (insn->subdev >= dev->n_subdevices) {
821 DPRINTK("subdevice %d out of range\n", insn->subdev);
822 ret = -EINVAL;
823 goto out;
824 }
825 s = dev->subdevices + insn->subdev;
826
827 if (s->type == COMEDI_SUBD_UNUSED) {
828 DPRINTK("%d not usable subdevice\n", insn->subdev);
829 ret = -EIO;
830 goto out;
831 }
832
833 /* are we locked? (ioctl lock) */
834 if (s->lock && s->lock != file) {
835 DPRINTK("device locked\n");
836 ret = -EACCES;
837 goto out;
838 }
839
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800840 ret = check_chanlist(s, 1, &insn->chanspec);
841 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -0800842 ret = -EINVAL;
843 DPRINTK("bad chanspec\n");
844 goto out;
845 }
846
847 if (s->busy) {
848 ret = -EBUSY;
849 goto out;
850 }
851 /* This looks arbitrary. It is. */
852 s->busy = &parse_insn;
853 switch (insn->insn) {
854 case INSN_READ:
855 ret = s->insn_read(dev, s, insn, data);
856 break;
857 case INSN_WRITE:
858 maxdata = s->maxdata_list
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800859 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
860 : s->maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800861 for (i = 0; i < insn->n; ++i) {
862 if (data[i] > maxdata) {
863 ret = -EINVAL;
864 DPRINTK("bad data value(s)\n");
865 break;
866 }
867 }
868 if (ret == 0)
869 ret = s->insn_write(dev, s, insn, data);
870 break;
871 case INSN_BITS:
872 if (insn->n != 2) {
873 ret = -EINVAL;
874 break;
875 }
876 ret = s->insn_bits(dev, s, insn, data);
877 break;
878 case INSN_CONFIG:
879 ret = check_insn_config_length(insn, data);
880 if (ret)
881 break;
882 ret = s->insn_config(dev, s, insn, data);
883 break;
884 default:
885 ret = -EINVAL;
886 break;
887 }
888
889 s->busy = NULL;
890 }
891
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800892out:
David Schleefed9eccb2008-11-04 20:29:31 -0800893 return ret;
894}
895
896/*
897 * COMEDI_INSN
898 * synchronous instructions
899 *
900 * arg:
901 * pointer to insn
902 *
903 * reads:
Bill Pemberton90035c02009-03-16 22:05:53 -0400904 * struct comedi_insn struct at arg
David Schleefed9eccb2008-11-04 20:29:31 -0800905 * data (for writes)
906 *
907 * writes:
908 * data (for reads)
909 */
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400910static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800911{
Bill Pemberton90035c02009-03-16 22:05:53 -0400912 struct comedi_insn insn;
Bill Pemberton790c5542009-03-16 22:05:02 -0400913 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800914 int ret = 0;
915
Bill Pemberton790c5542009-03-16 22:05:02 -0400916 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800917 if (!data) {
918 ret = -ENOMEM;
919 goto error;
920 }
921
Bill Pemberton90035c02009-03-16 22:05:53 -0400922 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800923 ret = -EFAULT;
924 goto error;
925 }
926
927 /* This is where the behavior of insn and insnlist deviate. */
928 if (insn.n > MAX_SAMPLES)
929 insn.n = MAX_SAMPLES;
930 if (insn.insn & INSN_MASK_WRITE) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530931 if (copy_from_user
932 (data, insn.data, insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800933 ret = -EFAULT;
934 goto error;
935 }
936 }
937 ret = parse_insn(dev, &insn, data, file);
938 if (ret < 0)
939 goto error;
940 if (insn.insn & INSN_MASK_READ) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530941 if (copy_to_user
942 (insn.data, data, insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800943 ret = -EFAULT;
944 goto error;
945 }
946 }
947 ret = insn.n;
948
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800949error:
950 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800951
952 return ret;
953}
954
955/*
956 COMEDI_CMD
957 command ioctl
958
959 arg:
960 pointer to cmd structure
961
962 reads:
963 cmd structure at arg
964 channel/range list
965
966 writes:
967 modified cmd structure at arg
968
969*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400970static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800971{
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400972 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -0400973 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400974 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800975 int ret = 0;
976 unsigned int *chanlist_saver = NULL;
977
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400978 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800979 DPRINTK("bad cmd address\n");
980 return -EFAULT;
981 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800982 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -0800983 chanlist_saver = user_cmd.chanlist;
984
985 if (user_cmd.subdev >= dev->n_subdevices) {
986 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
987 return -ENODEV;
988 }
989
990 s = dev->subdevices + user_cmd.subdev;
991 async = s->async;
992
993 if (s->type == COMEDI_SUBD_UNUSED) {
994 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
995 return -EIO;
996 }
997
998 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
999 DPRINTK("subdevice %i does not support commands\n",
1000 user_cmd.subdev);
1001 return -EIO;
1002 }
1003
1004 /* are we locked? (ioctl lock) */
1005 if (s->lock && s->lock != file) {
1006 DPRINTK("subdevice locked\n");
1007 return -EACCES;
1008 }
1009
1010 /* are we busy? */
1011 if (s->busy) {
1012 DPRINTK("subdevice busy\n");
1013 return -EBUSY;
1014 }
1015 s->busy = file;
1016
1017 /* make sure channel/gain list isn't too long */
1018 if (user_cmd.chanlist_len > s->len_chanlist) {
1019 DPRINTK("channel/gain list too long %u > %d\n",
1020 user_cmd.chanlist_len, s->len_chanlist);
1021 ret = -EINVAL;
1022 goto cleanup;
1023 }
1024
1025 /* make sure channel/gain list isn't too short */
1026 if (user_cmd.chanlist_len < 1) {
1027 DPRINTK("channel/gain list too short %u < 1\n",
1028 user_cmd.chanlist_len);
1029 ret = -EINVAL;
1030 goto cleanup;
1031 }
1032
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001033 kfree(async->cmd.chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001034 async->cmd = user_cmd;
1035 async->cmd.data = NULL;
1036 /* load channel/gain list */
1037 async->cmd.chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001038 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001039 if (!async->cmd.chanlist) {
1040 DPRINTK("allocation failed\n");
1041 ret = -ENOMEM;
1042 goto cleanup;
1043 }
1044
1045 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001046 async->cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001047 DPRINTK("fault reading chanlist\n");
1048 ret = -EFAULT;
1049 goto cleanup;
1050 }
1051
1052 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001053 ret = check_chanlist(s, async->cmd.chanlist_len, async->cmd.chanlist);
1054 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001055 DPRINTK("bad chanlist\n");
1056 goto cleanup;
1057 }
1058
1059 ret = s->do_cmdtest(dev, s, &async->cmd);
1060
1061 if (async->cmd.flags & TRIG_BOGUS || ret) {
1062 DPRINTK("test returned %d\n", ret);
1063 user_cmd = async->cmd;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001064 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001065 user_cmd.chanlist = chanlist_saver;
1066 user_cmd.data = NULL;
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001067 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001068 DPRINTK("fault writing cmd\n");
1069 ret = -EFAULT;
1070 goto cleanup;
1071 }
1072 ret = -EAGAIN;
1073 goto cleanup;
1074 }
1075
1076 if (!async->prealloc_bufsz) {
1077 ret = -ENOMEM;
1078 DPRINTK("no buffer (?)\n");
1079 goto cleanup;
1080 }
1081
1082 comedi_reset_async_buf(async);
1083
1084 async->cb_mask =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001085 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1086 COMEDI_CB_OVERFLOW;
1087 if (async->cmd.flags & TRIG_WAKE_EOS)
David Schleefed9eccb2008-11-04 20:29:31 -08001088 async->cb_mask |= COMEDI_CB_EOS;
David Schleefed9eccb2008-11-04 20:29:31 -08001089
1090 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1091
David Schleefed9eccb2008-11-04 20:29:31 -08001092 ret = s->do_cmd(dev, s);
1093 if (ret == 0)
1094 return 0;
1095
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001096cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -08001097 do_become_nonbusy(dev, s);
1098
1099 return ret;
1100}
1101
1102/*
1103 COMEDI_CMDTEST
1104 command testing ioctl
1105
1106 arg:
1107 pointer to cmd structure
1108
1109 reads:
1110 cmd structure at arg
1111 channel/range list
1112
1113 writes:
1114 modified cmd structure at arg
1115
1116*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001117static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001118{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001119 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001120 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001121 int ret = 0;
1122 unsigned int *chanlist = NULL;
1123 unsigned int *chanlist_saver = NULL;
1124
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001125 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001126 DPRINTK("bad cmd address\n");
1127 return -EFAULT;
1128 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001129 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001130 chanlist_saver = user_cmd.chanlist;
1131
1132 if (user_cmd.subdev >= dev->n_subdevices) {
1133 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1134 return -ENODEV;
1135 }
1136
1137 s = dev->subdevices + user_cmd.subdev;
1138 if (s->type == COMEDI_SUBD_UNUSED) {
1139 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1140 return -EIO;
1141 }
1142
1143 if (!s->do_cmd || !s->do_cmdtest) {
1144 DPRINTK("subdevice %i does not support commands\n",
1145 user_cmd.subdev);
1146 return -EIO;
1147 }
1148
1149 /* make sure channel/gain list isn't too long */
1150 if (user_cmd.chanlist_len > s->len_chanlist) {
1151 DPRINTK("channel/gain list too long %d > %d\n",
1152 user_cmd.chanlist_len, s->len_chanlist);
1153 ret = -EINVAL;
1154 goto cleanup;
1155 }
1156
1157 /* load channel/gain list */
1158 if (user_cmd.chanlist) {
1159 chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001160 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001161 if (!chanlist) {
1162 DPRINTK("allocation failed\n");
1163 ret = -ENOMEM;
1164 goto cleanup;
1165 }
1166
1167 if (copy_from_user(chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001168 user_cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001169 DPRINTK("fault reading chanlist\n");
1170 ret = -EFAULT;
1171 goto cleanup;
1172 }
1173
1174 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001175 ret = check_chanlist(s, user_cmd.chanlist_len, chanlist);
1176 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001177 DPRINTK("bad chanlist\n");
1178 goto cleanup;
1179 }
1180
1181 user_cmd.chanlist = chanlist;
1182 }
1183
1184 ret = s->do_cmdtest(dev, s, &user_cmd);
1185
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001186 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001187 user_cmd.chanlist = chanlist_saver;
1188
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001189 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001190 DPRINTK("bad cmd address\n");
1191 ret = -EFAULT;
1192 goto cleanup;
1193 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001194cleanup:
1195 kfree(chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001196
1197 return ret;
1198}
1199
1200/*
1201 COMEDI_LOCK
1202 lock subdevice
1203
1204 arg:
1205 subdevice number
1206
1207 reads:
1208 none
1209
1210 writes:
1211 none
1212
1213*/
1214
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301215static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1216 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001217{
1218 int ret = 0;
1219 unsigned long flags;
Bill Pemberton34c43922009-03-16 22:05:14 -04001220 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001221
1222 if (arg >= dev->n_subdevices)
1223 return -EINVAL;
1224 s = dev->subdevices + arg;
1225
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001226 spin_lock_irqsave(&s->spin_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001227 if (s->busy || s->lock)
David Schleefed9eccb2008-11-04 20:29:31 -08001228 ret = -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001229 else
David Schleefed9eccb2008-11-04 20:29:31 -08001230 s->lock = file;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001231 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08001232
1233 if (ret < 0)
1234 return ret;
1235
1236#if 0
1237 if (s->lock_f)
1238 ret = s->lock_f(dev, s);
1239#endif
1240
1241 return ret;
1242}
1243
1244/*
1245 COMEDI_UNLOCK
1246 unlock subdevice
1247
1248 arg:
1249 subdevice number
1250
1251 reads:
1252 none
1253
1254 writes:
1255 none
1256
1257 This function isn't protected by the semaphore, since
1258 we already own the lock.
1259*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301260static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1261 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001262{
Bill Pemberton34c43922009-03-16 22:05:14 -04001263 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001264
1265 if (arg >= dev->n_subdevices)
1266 return -EINVAL;
1267 s = dev->subdevices + arg;
1268
1269 if (s->busy)
1270 return -EBUSY;
1271
1272 if (s->lock && s->lock != file)
1273 return -EACCES;
1274
1275 if (s->lock == file) {
1276#if 0
1277 if (s->unlock)
1278 s->unlock(dev, s);
1279#endif
1280
1281 s->lock = NULL;
1282 }
1283
1284 return 0;
1285}
1286
1287/*
1288 COMEDI_CANCEL
1289 cancel acquisition ioctl
1290
1291 arg:
1292 subdevice number
1293
1294 reads:
1295 nothing
1296
1297 writes:
1298 nothing
1299
1300*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301301static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1302 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001303{
Bill Pemberton34c43922009-03-16 22:05:14 -04001304 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001305
1306 if (arg >= dev->n_subdevices)
1307 return -EINVAL;
1308 s = dev->subdevices + arg;
1309 if (s->async == NULL)
1310 return -EINVAL;
1311
1312 if (s->lock && s->lock != file)
1313 return -EACCES;
1314
1315 if (!s->busy)
1316 return 0;
1317
1318 if (s->busy != file)
1319 return -EBUSY;
1320
1321 return do_cancel(dev, s);
1322}
1323
1324/*
1325 COMEDI_POLL ioctl
1326 instructs driver to synchronize buffers
1327
1328 arg:
1329 subdevice number
1330
1331 reads:
1332 nothing
1333
1334 writes:
1335 nothing
1336
1337*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301338static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1339 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001340{
Bill Pemberton34c43922009-03-16 22:05:14 -04001341 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001342
1343 if (arg >= dev->n_subdevices)
1344 return -EINVAL;
1345 s = dev->subdevices + arg;
1346
1347 if (s->lock && s->lock != file)
1348 return -EACCES;
1349
1350 if (!s->busy)
1351 return 0;
1352
1353 if (s->busy != file)
1354 return -EBUSY;
1355
1356 if (s->poll)
1357 return s->poll(dev, s);
1358
1359 return -EINVAL;
1360}
1361
Bill Pemberton34c43922009-03-16 22:05:14 -04001362static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001363{
1364 int ret = 0;
1365
1366 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1367 ret = s->cancel(dev, s);
1368
1369 do_become_nonbusy(dev, s);
1370
1371 return ret;
1372}
1373
1374void comedi_unmap(struct vm_area_struct *area)
1375{
Bill Pembertond1636792009-03-16 22:05:20 -04001376 struct comedi_async *async;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001377 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -08001378
1379 async = area->vm_private_data;
1380 dev = async->subdevice->device;
1381
1382 mutex_lock(&dev->mutex);
1383 async->mmap_count--;
1384 mutex_unlock(&dev->mutex);
1385}
1386
1387static struct vm_operations_struct comedi_vm_ops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301388 .close = comedi_unmap,
David Schleefed9eccb2008-11-04 20:29:31 -08001389};
1390
1391static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1392{
1393 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001394 struct comedi_device_file_info *dev_file_info =
1395 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001396 struct comedi_device *dev = dev_file_info->device;
Bill Pembertond1636792009-03-16 22:05:20 -04001397 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001398 unsigned long start = vma->vm_start;
1399 unsigned long size;
1400 int n_pages;
1401 int i;
1402 int retval;
Bill Pemberton34c43922009-03-16 22:05:14 -04001403 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001404
1405 mutex_lock(&dev->mutex);
1406 if (!dev->attached) {
1407 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1408 retval = -ENODEV;
1409 goto done;
1410 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001411 if (vma->vm_flags & VM_WRITE)
David Schleefed9eccb2008-11-04 20:29:31 -08001412 s = comedi_get_write_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001413 else
David Schleefed9eccb2008-11-04 20:29:31 -08001414 s = comedi_get_read_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001415
David Schleefed9eccb2008-11-04 20:29:31 -08001416 if (s == NULL) {
1417 retval = -EINVAL;
1418 goto done;
1419 }
1420 async = s->async;
1421 if (async == NULL) {
1422 retval = -EINVAL;
1423 goto done;
1424 }
1425
1426 if (vma->vm_pgoff != 0) {
1427 DPRINTK("comedi: mmap() offset must be 0.\n");
1428 retval = -EINVAL;
1429 goto done;
1430 }
1431
1432 size = vma->vm_end - vma->vm_start;
1433 if (size > async->prealloc_bufsz) {
1434 retval = -EFAULT;
1435 goto done;
1436 }
1437 if (size & (~PAGE_MASK)) {
1438 retval = -EFAULT;
1439 goto done;
1440 }
1441
1442 n_pages = size >> PAGE_SHIFT;
1443 for (i = 0; i < n_pages; ++i) {
1444 if (remap_pfn_range(vma, start,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301445 page_to_pfn(virt_to_page
1446 (async->buf_page_list
1447 [i].virt_addr)), PAGE_SIZE,
1448 PAGE_SHARED)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001449 retval = -EAGAIN;
1450 goto done;
1451 }
1452 start += PAGE_SIZE;
1453 }
1454
1455 vma->vm_ops = &comedi_vm_ops;
1456 vma->vm_private_data = async;
1457
1458 async->mmap_count++;
1459
1460 retval = 0;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001461done:
David Schleefed9eccb2008-11-04 20:29:31 -08001462 mutex_unlock(&dev->mutex);
1463 return retval;
1464}
1465
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301466static unsigned int comedi_poll(struct file *file, poll_table * wait)
David Schleefed9eccb2008-11-04 20:29:31 -08001467{
1468 unsigned int mask = 0;
1469 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001470 struct comedi_device_file_info *dev_file_info =
1471 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001472 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001473 struct comedi_subdevice *read_subdev;
1474 struct comedi_subdevice *write_subdev;
David Schleefed9eccb2008-11-04 20:29:31 -08001475
1476 mutex_lock(&dev->mutex);
1477 if (!dev->attached) {
1478 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1479 mutex_unlock(&dev->mutex);
1480 return 0;
1481 }
1482
1483 mask = 0;
1484 read_subdev = comedi_get_read_subdevice(dev_file_info);
1485 if (read_subdev) {
1486 poll_wait(file, &read_subdev->async->wait_head, wait);
1487 if (!read_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001488 || comedi_buf_read_n_available(read_subdev->async) > 0
1489 || !(comedi_get_subdevice_runflags(read_subdev) &
1490 SRF_RUNNING)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001491 mask |= POLLIN | POLLRDNORM;
1492 }
1493 }
1494 write_subdev = comedi_get_write_subdevice(dev_file_info);
1495 if (write_subdev) {
1496 poll_wait(file, &write_subdev->async->wait_head, wait);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001497 comedi_buf_write_alloc(write_subdev->async,
1498 write_subdev->async->prealloc_bufsz);
David Schleefed9eccb2008-11-04 20:29:31 -08001499 if (!write_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001500 || !(comedi_get_subdevice_runflags(write_subdev) &
1501 SRF_RUNNING)
1502 || comedi_buf_write_n_allocated(write_subdev->async) >=
1503 bytes_per_sample(write_subdev->async->subdevice)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001504 mask |= POLLOUT | POLLWRNORM;
1505 }
1506 }
1507
1508 mutex_unlock(&dev->mutex);
1509 return mask;
1510}
1511
1512static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01001513 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001514{
Bill Pemberton34c43922009-03-16 22:05:14 -04001515 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001516 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001517 int n, m, count = 0, retval = 0;
1518 DECLARE_WAITQUEUE(wait, current);
1519 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001520 struct comedi_device_file_info *dev_file_info =
1521 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001522 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001523
1524 if (!dev->attached) {
1525 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1526 retval = -ENODEV;
1527 goto done;
1528 }
1529
1530 s = comedi_get_write_subdevice(dev_file_info);
1531 if (s == NULL) {
1532 retval = -EIO;
1533 goto done;
1534 }
1535 async = s->async;
1536
1537 if (!nbytes) {
1538 retval = 0;
1539 goto done;
1540 }
1541 if (!s->busy) {
1542 retval = 0;
1543 goto done;
1544 }
1545 if (s->busy != file) {
1546 retval = -EACCES;
1547 goto done;
1548 }
1549 add_wait_queue(&async->wait_head, &wait);
1550 while (nbytes > 0 && !retval) {
1551 set_current_state(TASK_INTERRUPTIBLE);
1552
1553 n = nbytes;
1554
1555 m = n;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001556 if (async->buf_write_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001557 m = async->prealloc_bufsz - async->buf_write_ptr;
David Schleefed9eccb2008-11-04 20:29:31 -08001558 comedi_buf_write_alloc(async, async->prealloc_bufsz);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001559 if (m > comedi_buf_write_n_allocated(async))
David Schleefed9eccb2008-11-04 20:29:31 -08001560 m = comedi_buf_write_n_allocated(async);
David Schleefed9eccb2008-11-04 20:29:31 -08001561 if (m < n)
1562 n = m;
1563
1564 if (n == 0) {
1565 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1566 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001567 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001568 retval = -EPIPE;
1569 } else {
1570 retval = 0;
1571 }
1572 do_become_nonbusy(dev, s);
1573 break;
1574 }
1575 if (file->f_flags & O_NONBLOCK) {
1576 retval = -EAGAIN;
1577 break;
1578 }
1579 if (signal_pending(current)) {
1580 retval = -ERESTARTSYS;
1581 break;
1582 }
1583 schedule();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001584 if (!s->busy)
David Schleefed9eccb2008-11-04 20:29:31 -08001585 break;
David Schleefed9eccb2008-11-04 20:29:31 -08001586 if (s->busy != file) {
1587 retval = -EACCES;
1588 break;
1589 }
1590 continue;
1591 }
1592
1593 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001594 buf, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001595 if (m) {
1596 n -= m;
1597 retval = -EFAULT;
1598 }
1599 comedi_buf_write_free(async, n);
1600
1601 count += n;
1602 nbytes -= n;
1603
1604 buf += n;
1605 break; /* makes device work like a pipe */
1606 }
1607 set_current_state(TASK_RUNNING);
1608 remove_wait_queue(&async->wait_head, &wait);
1609
1610done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001611 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001612}
1613
1614static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01001615 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001616{
Bill Pemberton34c43922009-03-16 22:05:14 -04001617 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001618 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001619 int n, m, count = 0, retval = 0;
1620 DECLARE_WAITQUEUE(wait, current);
1621 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001622 struct comedi_device_file_info *dev_file_info =
1623 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001624 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001625
1626 if (!dev->attached) {
1627 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1628 retval = -ENODEV;
1629 goto done;
1630 }
1631
1632 s = comedi_get_read_subdevice(dev_file_info);
1633 if (s == NULL) {
1634 retval = -EIO;
1635 goto done;
1636 }
1637 async = s->async;
1638 if (!nbytes) {
1639 retval = 0;
1640 goto done;
1641 }
1642 if (!s->busy) {
1643 retval = 0;
1644 goto done;
1645 }
1646 if (s->busy != file) {
1647 retval = -EACCES;
1648 goto done;
1649 }
1650
1651 add_wait_queue(&async->wait_head, &wait);
1652 while (nbytes > 0 && !retval) {
1653 set_current_state(TASK_INTERRUPTIBLE);
1654
1655 n = nbytes;
1656
1657 m = comedi_buf_read_n_available(async);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001658 /* printk("%d available\n",m); */
1659 if (async->buf_read_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001660 m = async->prealloc_bufsz - async->buf_read_ptr;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001661 /* printk("%d contiguous\n",m); */
David Schleefed9eccb2008-11-04 20:29:31 -08001662 if (m < n)
1663 n = m;
1664
1665 if (n == 0) {
1666 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1667 do_become_nonbusy(dev, s);
1668 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001669 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001670 retval = -EPIPE;
1671 } else {
1672 retval = 0;
1673 }
1674 break;
1675 }
1676 if (file->f_flags & O_NONBLOCK) {
1677 retval = -EAGAIN;
1678 break;
1679 }
1680 if (signal_pending(current)) {
1681 retval = -ERESTARTSYS;
1682 break;
1683 }
1684 schedule();
1685 if (!s->busy) {
1686 retval = 0;
1687 break;
1688 }
1689 if (s->busy != file) {
1690 retval = -EACCES;
1691 break;
1692 }
1693 continue;
1694 }
1695 m = copy_to_user(buf, async->prealloc_buf +
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001696 async->buf_read_ptr, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001697 if (m) {
1698 n -= m;
1699 retval = -EFAULT;
1700 }
1701
1702 comedi_buf_read_alloc(async, n);
1703 comedi_buf_read_free(async, n);
1704
1705 count += n;
1706 nbytes -= n;
1707
1708 buf += n;
1709 break; /* makes device work like a pipe */
1710 }
1711 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001712 async->buf_read_count - async->buf_write_count == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001713 do_become_nonbusy(dev, s);
1714 }
1715 set_current_state(TASK_RUNNING);
1716 remove_wait_queue(&async->wait_head, &wait);
1717
1718done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001719 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001720}
1721
1722/*
1723 This function restores a subdevice to an idle state.
1724 */
Bill Pemberton34c43922009-03-16 22:05:14 -04001725void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001726{
Bill Pembertond1636792009-03-16 22:05:20 -04001727 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001728
1729 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001730 if (async) {
1731 comedi_reset_async_buf(async);
1732 async->inttrig = NULL;
1733 } else {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001734 printk(KERN_ERR
1735 "BUG: (?) do_become_nonbusy called with async=0\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001736 }
1737
1738 s->busy = NULL;
1739}
1740
1741static int comedi_open(struct inode *inode, struct file *file)
1742{
David Schleefed9eccb2008-11-04 20:29:31 -08001743 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001744 struct comedi_device_file_info *dev_file_info =
1745 comedi_get_device_file_info(minor);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301746 struct comedi_device *dev =
1747 dev_file_info ? dev_file_info->device : NULL;
Ian Abbott97920072009-02-09 16:51:38 +00001748
David Schleefed9eccb2008-11-04 20:29:31 -08001749 if (dev == NULL) {
1750 DPRINTK("invalid minor number\n");
1751 return -ENODEV;
1752 }
1753
1754 /* This is slightly hacky, but we want module autoloading
1755 * to work for root.
1756 * case: user opens device, attached -> ok
1757 * case: user opens device, unattached, in_request_module=0 -> autoload
1758 * case: user opens device, unattached, in_request_module=1 -> fail
1759 * case: root opens device, attached -> ok
1760 * case: root opens device, unattached, in_request_module=1 -> ok
1761 * (typically called from modprobe)
1762 * case: root opens device, unattached, in_request_module=0 -> autoload
1763 *
1764 * The last could be changed to "-> ok", which would deny root
1765 * autoloading.
1766 */
1767 mutex_lock(&dev->mutex);
1768 if (dev->attached)
1769 goto ok;
Eric Parisa8f80e82009-08-13 09:44:51 -04001770 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
David Schleefed9eccb2008-11-04 20:29:31 -08001771 DPRINTK("in request module\n");
1772 mutex_unlock(&dev->mutex);
1773 return -ENODEV;
1774 }
Eric Parisa8f80e82009-08-13 09:44:51 -04001775 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
David Schleefed9eccb2008-11-04 20:29:31 -08001776 goto ok;
1777
1778 dev->in_request_module = 1;
1779
David Schleefed9eccb2008-11-04 20:29:31 -08001780#ifdef CONFIG_KMOD
1781 mutex_unlock(&dev->mutex);
Ian Abbott56d92c62009-02-09 16:32:12 +00001782 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
David Schleefed9eccb2008-11-04 20:29:31 -08001783 mutex_lock(&dev->mutex);
1784#endif
1785
1786 dev->in_request_module = 0;
1787
Eric Parisa8f80e82009-08-13 09:44:51 -04001788 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1789 DPRINTK("not attached and not CAP_NET_ADMIN\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001790 mutex_unlock(&dev->mutex);
1791 return -ENODEV;
1792 }
1793ok:
1794 __module_get(THIS_MODULE);
1795
1796 if (dev->attached) {
1797 if (!try_module_get(dev->driver->module)) {
1798 module_put(THIS_MODULE);
1799 mutex_unlock(&dev->mutex);
1800 return -ENOSYS;
1801 }
1802 }
1803
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001804 if (dev->attached && dev->use_count == 0 && dev->open)
David Schleefed9eccb2008-11-04 20:29:31 -08001805 dev->open(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001806
1807 dev->use_count++;
1808
1809 mutex_unlock(&dev->mutex);
1810
1811 return 0;
1812}
1813
1814static int comedi_close(struct inode *inode, struct file *file)
1815{
1816 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001817 struct comedi_device_file_info *dev_file_info =
1818 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001819 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001820 struct comedi_subdevice *s = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001821 int i;
1822
1823 mutex_lock(&dev->mutex);
1824
1825 if (dev->subdevices) {
1826 for (i = 0; i < dev->n_subdevices; i++) {
1827 s = dev->subdevices + i;
1828
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001829 if (s->busy == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001830 do_cancel(dev, s);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001831 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001832 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001833 }
1834 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001835 if (dev->attached && dev->use_count == 1 && dev->close)
David Schleefed9eccb2008-11-04 20:29:31 -08001836 dev->close(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001837
1838 module_put(THIS_MODULE);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001839 if (dev->attached)
David Schleefed9eccb2008-11-04 20:29:31 -08001840 module_put(dev->driver->module);
David Schleefed9eccb2008-11-04 20:29:31 -08001841
1842 dev->use_count--;
1843
1844 mutex_unlock(&dev->mutex);
1845
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001846 if (file->f_flags & FASYNC)
David Schleefed9eccb2008-11-04 20:29:31 -08001847 comedi_fasync(-1, file, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001848
1849 return 0;
1850}
1851
1852static int comedi_fasync(int fd, struct file *file, int on)
1853{
1854 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001855 struct comedi_device_file_info *dev_file_info =
1856 comedi_get_device_file_info(minor);
1857
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001858 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001859
1860 return fasync_helper(fd, file, on, &dev->async_queue);
1861}
1862
1863const struct file_operations comedi_fops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301864 .owner = THIS_MODULE,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301865 .unlocked_ioctl = comedi_unlocked_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301866 .compat_ioctl = comedi_compat_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301867 .open = comedi_open,
1868 .release = comedi_close,
1869 .read = comedi_read,
1870 .write = comedi_write,
1871 .mmap = comedi_mmap,
1872 .poll = comedi_poll,
1873 .fasync = comedi_fasync,
David Schleefed9eccb2008-11-04 20:29:31 -08001874};
1875
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001876struct class *comedi_class;
David Schleefed9eccb2008-11-04 20:29:31 -08001877static struct cdev comedi_cdev;
1878
1879static void comedi_cleanup_legacy_minors(void)
1880{
1881 unsigned i;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001882
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001883 for (i = 0; i < comedi_num_legacy_minors; i++)
David Schleefed9eccb2008-11-04 20:29:31 -08001884 comedi_free_board_minor(i);
David Schleefed9eccb2008-11-04 20:29:31 -08001885}
1886
1887static int __init comedi_init(void)
1888{
1889 int i;
1890 int retval;
1891
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001892 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1893 " - http://www.comedi.org\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001894
Frank Mori Hessa3cb7292008-12-15 13:44:45 +00001895 if (comedi_num_legacy_minors < 0 ||
1896 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
1897 printk(KERN_ERR "comedi: error: invalid value for module "
1898 "parameter \"comedi_num_legacy_minors\". Valid values "
1899 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
1900 return -EINVAL;
1901 }
1902
1903 /*
1904 * comedi is unusable if both comedi_autoconfig and
1905 * comedi_num_legacy_minors are zero, so we might as well adjust the
1906 * defaults in that case
1907 */
1908 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
1909 comedi_num_legacy_minors = 16;
1910
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001911 memset(comedi_file_info_table, 0,
1912 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001913
1914 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001915 COMEDI_NUM_MINORS, "comedi");
David Schleefed9eccb2008-11-04 20:29:31 -08001916 if (retval)
1917 return -EIO;
1918 cdev_init(&comedi_cdev, &comedi_fops);
1919 comedi_cdev.owner = THIS_MODULE;
1920 kobject_set_name(&comedi_cdev.kobj, "comedi");
1921 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1922 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001923 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001924 return -EIO;
1925 }
1926 comedi_class = class_create(THIS_MODULE, "comedi");
1927 if (IS_ERR(comedi_class)) {
1928 printk("comedi: failed to create class");
1929 cdev_del(&comedi_cdev);
1930 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001931 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001932 return PTR_ERR(comedi_class);
1933 }
1934
1935 /* XXX requires /proc interface */
1936 comedi_proc_init();
1937
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001938 /* create devices files for legacy/manual use */
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001939 for (i = 0; i < comedi_num_legacy_minors; i++) {
David Schleefed9eccb2008-11-04 20:29:31 -08001940 int minor;
1941 minor = comedi_alloc_board_minor(NULL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001942 if (minor < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001943 comedi_cleanup_legacy_minors();
1944 cdev_del(&comedi_cdev);
1945 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001946 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001947 return minor;
1948 }
1949 }
1950
David Schleefed9eccb2008-11-04 20:29:31 -08001951 return 0;
1952}
1953
1954static void __exit comedi_cleanup(void)
1955{
1956 int i;
1957
1958 comedi_cleanup_legacy_minors();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001959 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
David Schleefed9eccb2008-11-04 20:29:31 -08001960 BUG_ON(comedi_file_info_table[i]);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001961
David Schleefed9eccb2008-11-04 20:29:31 -08001962 class_destroy(comedi_class);
1963 cdev_del(&comedi_cdev);
1964 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
1965
1966 comedi_proc_cleanup();
David Schleefed9eccb2008-11-04 20:29:31 -08001967}
1968
1969module_init(comedi_init);
1970module_exit(comedi_cleanup);
1971
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001972void comedi_error(const struct comedi_device *dev, const char *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001973{
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001974 printk("comedi%d: %s: %s\n", dev->minor, dev->driver->driver_name, s);
David Schleefed9eccb2008-11-04 20:29:31 -08001975}
1976
Bill Pemberton34c43922009-03-16 22:05:14 -04001977void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001978{
Bill Pembertond1636792009-03-16 22:05:20 -04001979 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001980 unsigned runflags = 0;
1981 unsigned runflags_mask = 0;
1982
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001983 /* DPRINTK("comedi_event 0x%x\n",mask); */
David Schleefed9eccb2008-11-04 20:29:31 -08001984
1985 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
1986 return;
1987
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301988 if (s->
1989 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
1990 COMEDI_CB_OVERFLOW)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001991 runflags_mask |= SRF_RUNNING;
1992 }
1993 /* remember if an error event has occured, so an error
1994 * can be returned the next time the user does a read() */
1995 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
1996 runflags_mask |= SRF_ERROR;
1997 runflags |= SRF_ERROR;
1998 }
1999 if (runflags_mask) {
2000 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2001 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2002 }
2003
2004 if (async->cb_mask & s->async->events) {
2005 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
Greg Kroah-Hartmanfcea1152009-04-27 15:15:30 -07002006 wake_up_interruptible(&async->wait_head);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002007 if (s->subdev_flags & SDF_CMD_READ)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302008 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002009 if (s->subdev_flags & SDF_CMD_WRITE)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302010 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
David Schleefed9eccb2008-11-04 20:29:31 -08002011 } else {
2012 if (async->cb_func)
2013 async->cb_func(s->async->events, async->cb_arg);
David Schleefed9eccb2008-11-04 20:29:31 -08002014 }
2015 }
2016 s->async->events = 0;
2017}
2018
Bill Pemberton34c43922009-03-16 22:05:14 -04002019void comedi_set_subdevice_runflags(struct comedi_subdevice *s, unsigned mask,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002020 unsigned bits)
David Schleefed9eccb2008-11-04 20:29:31 -08002021{
2022 unsigned long flags;
2023
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002024 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002025 s->runflags &= ~mask;
2026 s->runflags |= (bits & mask);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002027 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002028}
2029
Bill Pemberton34c43922009-03-16 22:05:14 -04002030unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002031{
2032 unsigned long flags;
2033 unsigned runflags;
2034
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002035 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002036 runflags = s->runflags;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002037 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002038 return runflags;
2039}
2040
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002041static int is_device_busy(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002042{
Bill Pemberton34c43922009-03-16 22:05:14 -04002043 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08002044 int i;
2045
2046 if (!dev->attached)
2047 return 0;
2048
2049 for (i = 0; i < dev->n_subdevices; i++) {
2050 s = dev->subdevices + i;
2051 if (s->busy)
2052 return 1;
2053 if (s->async && s->async->mmap_count)
2054 return 1;
2055 }
2056
2057 return 0;
2058}
2059
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002060void comedi_device_init(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002061{
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002062 memset(dev, 0, sizeof(struct comedi_device));
David Schleefed9eccb2008-11-04 20:29:31 -08002063 spin_lock_init(&dev->spinlock);
2064 mutex_init(&dev->mutex);
2065 dev->minor = -1;
2066}
2067
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002068void comedi_device_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002069{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002070 if (dev == NULL)
2071 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002072 mutex_lock(&dev->mutex);
2073 comedi_device_detach(dev);
2074 mutex_unlock(&dev->mutex);
2075 mutex_destroy(&dev->mutex);
2076}
2077
2078int comedi_alloc_board_minor(struct device *hardware_device)
2079{
2080 unsigned long flags;
2081 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002082 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002083 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002084 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002085
2086 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002087 if (info == NULL)
2088 return -ENOMEM;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002089 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002090 if (info->device == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002091 kfree(info);
2092 return -ENOMEM;
2093 }
2094 comedi_device_init(info->device);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002095 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002096 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2097 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002098 comedi_file_info_table[i] = info;
2099 break;
2100 }
2101 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002102 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002103 if (i == COMEDI_NUM_BOARD_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002104 comedi_device_cleanup(info->device);
2105 kfree(info->device);
2106 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302107 printk(KERN_ERR
2108 "comedi: error: ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002109 return -EBUSY;
2110 }
2111 info->device->minor = i;
2112 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002113 MKDEV(COMEDI_MAJOR, i), NULL,
2114 hardware_device, "comedi%i", i);
2115 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002116 info->device->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002117 dev_set_drvdata(csdev, info);
2118 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2119 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302120 printk(KERN_ERR
2121 "comedi: failed to create sysfs attribute file \"%s\".\n",
2122 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002123 comedi_free_board_minor(i);
2124 return retval;
2125 }
2126 retval = device_create_file(csdev, &dev_attr_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_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_max_write_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_max_write_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_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_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002147 comedi_free_board_minor(i);
2148 return retval;
2149 }
David Schleefed9eccb2008-11-04 20:29:31 -08002150 return i;
2151}
2152
2153void comedi_free_board_minor(unsigned minor)
2154{
2155 unsigned long flags;
2156 struct comedi_device_file_info *info;
2157
2158 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002159 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002160 info = comedi_file_info_table[minor];
2161 comedi_file_info_table[minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002162 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002163
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002164 if (info) {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002165 struct comedi_device *dev = info->device;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002166 if (dev) {
2167 if (dev->class_dev) {
2168 device_destroy(comedi_class,
2169 MKDEV(COMEDI_MAJOR, dev->minor));
David Schleefed9eccb2008-11-04 20:29:31 -08002170 }
2171 comedi_device_cleanup(dev);
2172 kfree(dev);
2173 }
2174 kfree(info);
2175 }
2176}
2177
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002178int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2179 struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002180{
2181 unsigned long flags;
2182 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002183 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002184 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002185 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002186
2187 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002188 if (info == NULL)
2189 return -ENOMEM;
David Schleefed9eccb2008-11-04 20:29:31 -08002190 info->device = dev;
2191 info->read_subdevice = s;
2192 info->write_subdevice = s;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002193 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Frank Mori Hess4c41f3a2008-12-09 14:47:22 +00002194 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002195 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002196 comedi_file_info_table[i] = info;
2197 break;
2198 }
2199 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002200 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002201 if (i == COMEDI_NUM_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002202 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302203 printk(KERN_ERR
2204 "comedi: error: ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002205 return -EBUSY;
2206 }
2207 s->minor = i;
2208 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002209 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2210 "comedi%i_subd%i", dev->minor,
2211 (int)(s - dev->subdevices));
2212 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002213 s->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002214 dev_set_drvdata(csdev, info);
2215 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2216 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302217 printk(KERN_ERR
2218 "comedi: failed to create sysfs attribute file \"%s\".\n",
2219 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002220 comedi_free_subdevice_minor(s);
2221 return retval;
2222 }
2223 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2224 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302225 printk(KERN_ERR
2226 "comedi: failed to create sysfs attribute file \"%s\".\n",
2227 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002228 comedi_free_subdevice_minor(s);
2229 return retval;
2230 }
2231 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2232 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302233 printk(KERN_ERR
2234 "comedi: failed to create sysfs attribute file \"%s\".\n",
2235 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002236 comedi_free_subdevice_minor(s);
2237 return retval;
2238 }
2239 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2240 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302241 printk(KERN_ERR
2242 "comedi: failed to create sysfs attribute file \"%s\".\n",
2243 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002244 comedi_free_subdevice_minor(s);
2245 return retval;
2246 }
David Schleefed9eccb2008-11-04 20:29:31 -08002247 return i;
2248}
2249
Bill Pemberton34c43922009-03-16 22:05:14 -04002250void comedi_free_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002251{
2252 unsigned long flags;
2253 struct comedi_device_file_info *info;
2254
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002255 if (s == NULL)
2256 return;
2257 if (s->minor < 0)
2258 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002259
2260 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2261 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2262
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002263 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002264 info = comedi_file_info_table[s->minor];
2265 comedi_file_info_table[s->minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002266 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002267
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002268 if (s->class_dev) {
David Schleefed9eccb2008-11-04 20:29:31 -08002269 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2270 s->class_dev = NULL;
2271 }
2272 kfree(info);
2273}
2274
2275struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2276{
2277 unsigned long flags;
2278 struct comedi_device_file_info *info;
2279
2280 BUG_ON(minor >= COMEDI_NUM_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002281 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002282 info = comedi_file_info_table[minor];
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002283 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002284 return info;
2285}
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002286
2287static int resize_async_buffer(struct comedi_device *dev,
2288 struct comedi_subdevice *s,
2289 struct comedi_async *async, unsigned new_size)
2290{
2291 int retval;
2292
2293 if (new_size > async->max_bufsize)
2294 return -EPERM;
2295
2296 if (s->busy) {
2297 DPRINTK("subdevice is busy, cannot resize buffer\n");
2298 return -EBUSY;
2299 }
2300 if (async->mmap_count) {
2301 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2302 return -EBUSY;
2303 }
2304
2305 if (!async->prealloc_buf)
2306 return -EINVAL;
2307
2308 /* make sure buffer is an integral number of pages
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302309 * (we round up) */
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002310 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2311
2312 retval = comedi_buf_alloc(dev, s, new_size);
2313 if (retval < 0)
2314 return retval;
2315
2316 if (s->buf_change) {
2317 retval = s->buf_change(dev, s, new_size);
2318 if (retval < 0)
2319 return retval;
2320 }
2321
2322 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
Ian Abbottb8b5cd92009-09-21 14:55:23 -04002323 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002324 return 0;
2325}
2326
2327/* sysfs attribute files */
2328
2329static const unsigned bytes_per_kibi = 1024;
2330
2331static ssize_t show_max_read_buffer_kb(struct device *dev,
2332 struct device_attribute *attr, char *buf)
2333{
2334 ssize_t retval;
2335 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2336 unsigned max_buffer_size_kb = 0;
2337 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302338 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002339
2340 mutex_lock(&info->device->mutex);
2341 if (read_subdevice &&
2342 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2343 read_subdevice->async) {
2344 max_buffer_size_kb = read_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302345 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002346 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302347 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002348 mutex_unlock(&info->device->mutex);
2349
2350 return retval;
2351}
2352
2353static ssize_t store_max_read_buffer_kb(struct device *dev,
2354 struct device_attribute *attr,
2355 const char *buf, size_t count)
2356{
2357 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2358 unsigned long new_max_size_kb;
2359 uint64_t new_max_size;
2360 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302361 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002362
2363 if (strict_strtoul(buf, 10, &new_max_size_kb))
2364 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302365 if (new_max_size_kb != (uint32_t) new_max_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002366 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302367 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2368 if (new_max_size != (uint32_t) new_max_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002369 return -EINVAL;
2370
2371 mutex_lock(&info->device->mutex);
2372 if (read_subdevice == NULL ||
2373 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2374 read_subdevice->async == NULL) {
2375 mutex_unlock(&info->device->mutex);
2376 return -EINVAL;
2377 }
2378 read_subdevice->async->max_bufsize = new_max_size;
2379 mutex_unlock(&info->device->mutex);
2380
2381 return count;
2382}
2383
2384static struct device_attribute dev_attr_max_read_buffer_kb = {
2385 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302386 .name = "max_read_buffer_kb",
2387 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002388 .show = &show_max_read_buffer_kb,
2389 .store = &store_max_read_buffer_kb
2390};
2391
2392static ssize_t show_read_buffer_kb(struct device *dev,
2393 struct device_attribute *attr, char *buf)
2394{
2395 ssize_t retval;
2396 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2397 unsigned buffer_size_kb = 0;
2398 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302399 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002400
2401 mutex_lock(&info->device->mutex);
2402 if (read_subdevice &&
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302403 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2404 read_subdevice->async) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002405 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302406 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002407 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302408 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002409 mutex_unlock(&info->device->mutex);
2410
2411 return retval;
2412}
2413
2414static ssize_t store_read_buffer_kb(struct device *dev,
2415 struct device_attribute *attr,
2416 const char *buf, size_t count)
2417{
2418 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2419 unsigned long new_size_kb;
2420 uint64_t new_size;
2421 int retval;
2422 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302423 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002424
2425 if (strict_strtoul(buf, 10, &new_size_kb))
2426 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302427 if (new_size_kb != (uint32_t) new_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002428 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302429 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2430 if (new_size != (uint32_t) new_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002431 return -EINVAL;
2432
2433 mutex_lock(&info->device->mutex);
2434 if (read_subdevice == NULL ||
2435 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2436 read_subdevice->async == NULL) {
2437 mutex_unlock(&info->device->mutex);
2438 return -EINVAL;
2439 }
2440 retval = resize_async_buffer(info->device, read_subdevice,
2441 read_subdevice->async, new_size);
2442 mutex_unlock(&info->device->mutex);
2443
2444 if (retval < 0)
2445 return retval;
2446 return count;
2447}
2448
2449static struct device_attribute dev_attr_read_buffer_kb = {
2450 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302451 .name = "read_buffer_kb",
2452 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002453 .show = &show_read_buffer_kb,
2454 .store = &store_read_buffer_kb
2455};
2456
2457static ssize_t show_max_write_buffer_kb(struct device *dev,
2458 struct device_attribute *attr,
2459 char *buf)
2460{
2461 ssize_t retval;
2462 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2463 unsigned max_buffer_size_kb = 0;
2464 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302465 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002466
2467 mutex_lock(&info->device->mutex);
2468 if (write_subdevice &&
2469 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2470 write_subdevice->async) {
2471 max_buffer_size_kb = write_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302472 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002473 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302474 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002475 mutex_unlock(&info->device->mutex);
2476
2477 return retval;
2478}
2479
2480static ssize_t store_max_write_buffer_kb(struct device *dev,
2481 struct device_attribute *attr,
2482 const char *buf, size_t count)
2483{
2484 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2485 unsigned long new_max_size_kb;
2486 uint64_t new_max_size;
2487 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302488 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002489
2490 if (strict_strtoul(buf, 10, &new_max_size_kb))
2491 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302492 if (new_max_size_kb != (uint32_t) new_max_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002493 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302494 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2495 if (new_max_size != (uint32_t) new_max_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002496 return -EINVAL;
2497
2498 mutex_lock(&info->device->mutex);
2499 if (write_subdevice == NULL ||
2500 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2501 write_subdevice->async == NULL) {
2502 mutex_unlock(&info->device->mutex);
2503 return -EINVAL;
2504 }
2505 write_subdevice->async->max_bufsize = new_max_size;
2506 mutex_unlock(&info->device->mutex);
2507
2508 return count;
2509}
2510
2511static struct device_attribute dev_attr_max_write_buffer_kb = {
2512 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302513 .name = "max_write_buffer_kb",
2514 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002515 .show = &show_max_write_buffer_kb,
2516 .store = &store_max_write_buffer_kb
2517};
2518
2519static ssize_t show_write_buffer_kb(struct device *dev,
2520 struct device_attribute *attr, char *buf)
2521{
2522 ssize_t retval;
2523 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2524 unsigned buffer_size_kb = 0;
2525 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302526 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002527
2528 mutex_lock(&info->device->mutex);
2529 if (write_subdevice &&
2530 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2531 write_subdevice->async) {
2532 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302533 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002534 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302535 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002536 mutex_unlock(&info->device->mutex);
2537
2538 return retval;
2539}
2540
2541static ssize_t store_write_buffer_kb(struct device *dev,
2542 struct device_attribute *attr,
2543 const char *buf, size_t count)
2544{
2545 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2546 unsigned long new_size_kb;
2547 uint64_t new_size;
2548 int retval;
2549 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302550 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002551
2552 if (strict_strtoul(buf, 10, &new_size_kb))
2553 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302554 if (new_size_kb != (uint32_t) new_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002555 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302556 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2557 if (new_size != (uint32_t) new_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002558 return -EINVAL;
2559
2560 mutex_lock(&info->device->mutex);
2561 if (write_subdevice == NULL ||
2562 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2563 write_subdevice->async == NULL) {
2564 mutex_unlock(&info->device->mutex);
2565 return -EINVAL;
2566 }
2567 retval = resize_async_buffer(info->device, write_subdevice,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302568 write_subdevice->async, new_size);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002569 mutex_unlock(&info->device->mutex);
2570
2571 if (retval < 0)
2572 return retval;
2573 return count;
2574}
2575
2576static struct device_attribute dev_attr_write_buffer_kb = {
2577 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302578 .name = "write_buffer_kb",
2579 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002580 .show = &show_write_buffer_kb,
2581 .store = &store_write_buffer_kb
2582};