blob: 1b3aed4ee60a734ac9851733d4400b598685abf3 [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/*
Pieter De Praetere20617f22010-03-10 09:47:44 +0100601 * COMEDI_INSNLIST
602 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -0800603 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100604 * arg:
605 * pointer to sync cmd structure
David Schleefed9eccb2008-11-04 20:29:31 -0800606 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100607 * reads:
608 * sync cmd struct at arg
609 * instruction list
610 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -0800611 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100612 * writes:
613 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -0800614 */
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:
Mark Rankilor3fffdf22010-04-29 18:17:16 +0800739 printk(KERN_WARNING
740 "comedi: no check for data length of config insn id "
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530741 "%i is implemented.\n"
742 " Add a check to %s in %s.\n"
743 " Assuming n=%i is correct.\n", data[0], __func__,
744 __FILE__, insn->n);
David Schleefed9eccb2008-11-04 20:29:31 -0800745 return 0;
746 break;
747 }
748 return -EINVAL;
749}
750
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530751static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
752 unsigned int *data, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800753{
Bill Pemberton34c43922009-03-16 22:05:14 -0400754 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800755 int ret = 0;
756 int i;
757
758 if (insn->insn & INSN_MASK_SPECIAL) {
759 /* a non-subdevice instruction */
760
761 switch (insn->insn) {
762 case INSN_GTOD:
763 {
764 struct timeval tv;
765
766 if (insn->n != 2) {
767 ret = -EINVAL;
768 break;
769 }
770
771 do_gettimeofday(&tv);
772 data[0] = tv.tv_sec;
773 data[1] = tv.tv_usec;
774 ret = 2;
775
776 break;
777 }
778 case INSN_WAIT:
779 if (insn->n != 1 || data[0] >= 100000) {
780 ret = -EINVAL;
781 break;
782 }
783 udelay(data[0] / 1000);
784 ret = 1;
785 break;
786 case INSN_INTTRIG:
787 if (insn->n != 1) {
788 ret = -EINVAL;
789 break;
790 }
791 if (insn->subdev >= dev->n_subdevices) {
792 DPRINTK("%d not usable subdevice\n",
793 insn->subdev);
794 ret = -EINVAL;
795 break;
796 }
797 s = dev->subdevices + insn->subdev;
798 if (!s->async) {
799 DPRINTK("no async\n");
800 ret = -EINVAL;
801 break;
802 }
803 if (!s->async->inttrig) {
804 DPRINTK("no inttrig\n");
805 ret = -EAGAIN;
806 break;
807 }
808 ret = s->async->inttrig(dev, s, insn->data[0]);
809 if (ret >= 0)
810 ret = 1;
811 break;
812 default:
813 DPRINTK("invalid insn\n");
814 ret = -EINVAL;
815 break;
816 }
817 } else {
818 /* a subdevice instruction */
Bill Pemberton790c5542009-03-16 22:05:02 -0400819 unsigned int maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800820
821 if (insn->subdev >= dev->n_subdevices) {
822 DPRINTK("subdevice %d out of range\n", insn->subdev);
823 ret = -EINVAL;
824 goto out;
825 }
826 s = dev->subdevices + insn->subdev;
827
828 if (s->type == COMEDI_SUBD_UNUSED) {
829 DPRINTK("%d not usable subdevice\n", insn->subdev);
830 ret = -EIO;
831 goto out;
832 }
833
834 /* are we locked? (ioctl lock) */
835 if (s->lock && s->lock != file) {
836 DPRINTK("device locked\n");
837 ret = -EACCES;
838 goto out;
839 }
840
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800841 ret = check_chanlist(s, 1, &insn->chanspec);
842 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -0800843 ret = -EINVAL;
844 DPRINTK("bad chanspec\n");
845 goto out;
846 }
847
848 if (s->busy) {
849 ret = -EBUSY;
850 goto out;
851 }
852 /* This looks arbitrary. It is. */
853 s->busy = &parse_insn;
854 switch (insn->insn) {
855 case INSN_READ:
856 ret = s->insn_read(dev, s, insn, data);
857 break;
858 case INSN_WRITE:
859 maxdata = s->maxdata_list
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800860 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
861 : s->maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800862 for (i = 0; i < insn->n; ++i) {
863 if (data[i] > maxdata) {
864 ret = -EINVAL;
865 DPRINTK("bad data value(s)\n");
866 break;
867 }
868 }
869 if (ret == 0)
870 ret = s->insn_write(dev, s, insn, data);
871 break;
872 case INSN_BITS:
873 if (insn->n != 2) {
874 ret = -EINVAL;
875 break;
876 }
877 ret = s->insn_bits(dev, s, insn, data);
878 break;
879 case INSN_CONFIG:
880 ret = check_insn_config_length(insn, data);
881 if (ret)
882 break;
883 ret = s->insn_config(dev, s, insn, data);
884 break;
885 default:
886 ret = -EINVAL;
887 break;
888 }
889
890 s->busy = NULL;
891 }
892
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800893out:
David Schleefed9eccb2008-11-04 20:29:31 -0800894 return ret;
895}
896
897/*
Pieter De Praetere20617f22010-03-10 09:47:44 +0100898 * COMEDI_INSN
899 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -0800900 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100901 * arg:
902 * pointer to insn
David Schleefed9eccb2008-11-04 20:29:31 -0800903 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100904 * reads:
905 * struct comedi_insn struct at arg
906 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -0800907 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100908 * writes:
909 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -0800910 */
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400911static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800912{
Bill Pemberton90035c02009-03-16 22:05:53 -0400913 struct comedi_insn insn;
Bill Pemberton790c5542009-03-16 22:05:02 -0400914 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800915 int ret = 0;
916
Bill Pemberton790c5542009-03-16 22:05:02 -0400917 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800918 if (!data) {
919 ret = -ENOMEM;
920 goto error;
921 }
922
Bill Pemberton90035c02009-03-16 22:05:53 -0400923 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800924 ret = -EFAULT;
925 goto error;
926 }
927
928 /* This is where the behavior of insn and insnlist deviate. */
929 if (insn.n > MAX_SAMPLES)
930 insn.n = MAX_SAMPLES;
931 if (insn.insn & INSN_MASK_WRITE) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530932 if (copy_from_user
933 (data, insn.data, insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800934 ret = -EFAULT;
935 goto error;
936 }
937 }
938 ret = parse_insn(dev, &insn, data, file);
939 if (ret < 0)
940 goto error;
941 if (insn.insn & INSN_MASK_READ) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530942 if (copy_to_user
943 (insn.data, data, insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800944 ret = -EFAULT;
945 goto error;
946 }
947 }
948 ret = insn.n;
949
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800950error:
951 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800952
953 return ret;
954}
955
956/*
957 COMEDI_CMD
958 command ioctl
959
960 arg:
961 pointer to cmd structure
962
963 reads:
964 cmd structure at arg
965 channel/range list
966
967 writes:
968 modified cmd structure at arg
969
970*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400971static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800972{
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400973 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -0400974 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400975 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800976 int ret = 0;
977 unsigned int *chanlist_saver = NULL;
978
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400979 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800980 DPRINTK("bad cmd address\n");
981 return -EFAULT;
982 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800983 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -0800984 chanlist_saver = user_cmd.chanlist;
985
986 if (user_cmd.subdev >= dev->n_subdevices) {
987 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
988 return -ENODEV;
989 }
990
991 s = dev->subdevices + user_cmd.subdev;
992 async = s->async;
993
994 if (s->type == COMEDI_SUBD_UNUSED) {
995 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
996 return -EIO;
997 }
998
999 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1000 DPRINTK("subdevice %i does not support commands\n",
1001 user_cmd.subdev);
1002 return -EIO;
1003 }
1004
1005 /* are we locked? (ioctl lock) */
1006 if (s->lock && s->lock != file) {
1007 DPRINTK("subdevice locked\n");
1008 return -EACCES;
1009 }
1010
1011 /* are we busy? */
1012 if (s->busy) {
1013 DPRINTK("subdevice busy\n");
1014 return -EBUSY;
1015 }
1016 s->busy = file;
1017
1018 /* make sure channel/gain list isn't too long */
1019 if (user_cmd.chanlist_len > s->len_chanlist) {
1020 DPRINTK("channel/gain list too long %u > %d\n",
1021 user_cmd.chanlist_len, s->len_chanlist);
1022 ret = -EINVAL;
1023 goto cleanup;
1024 }
1025
1026 /* make sure channel/gain list isn't too short */
1027 if (user_cmd.chanlist_len < 1) {
1028 DPRINTK("channel/gain list too short %u < 1\n",
1029 user_cmd.chanlist_len);
1030 ret = -EINVAL;
1031 goto cleanup;
1032 }
1033
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001034 kfree(async->cmd.chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001035 async->cmd = user_cmd;
1036 async->cmd.data = NULL;
1037 /* load channel/gain list */
1038 async->cmd.chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001039 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001040 if (!async->cmd.chanlist) {
1041 DPRINTK("allocation failed\n");
1042 ret = -ENOMEM;
1043 goto cleanup;
1044 }
1045
1046 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001047 async->cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001048 DPRINTK("fault reading chanlist\n");
1049 ret = -EFAULT;
1050 goto cleanup;
1051 }
1052
1053 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001054 ret = check_chanlist(s, async->cmd.chanlist_len, async->cmd.chanlist);
1055 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001056 DPRINTK("bad chanlist\n");
1057 goto cleanup;
1058 }
1059
1060 ret = s->do_cmdtest(dev, s, &async->cmd);
1061
1062 if (async->cmd.flags & TRIG_BOGUS || ret) {
1063 DPRINTK("test returned %d\n", ret);
1064 user_cmd = async->cmd;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001065 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001066 user_cmd.chanlist = chanlist_saver;
1067 user_cmd.data = NULL;
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001068 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001069 DPRINTK("fault writing cmd\n");
1070 ret = -EFAULT;
1071 goto cleanup;
1072 }
1073 ret = -EAGAIN;
1074 goto cleanup;
1075 }
1076
1077 if (!async->prealloc_bufsz) {
1078 ret = -ENOMEM;
1079 DPRINTK("no buffer (?)\n");
1080 goto cleanup;
1081 }
1082
1083 comedi_reset_async_buf(async);
1084
1085 async->cb_mask =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001086 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1087 COMEDI_CB_OVERFLOW;
1088 if (async->cmd.flags & TRIG_WAKE_EOS)
David Schleefed9eccb2008-11-04 20:29:31 -08001089 async->cb_mask |= COMEDI_CB_EOS;
David Schleefed9eccb2008-11-04 20:29:31 -08001090
1091 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1092
David Schleefed9eccb2008-11-04 20:29:31 -08001093 ret = s->do_cmd(dev, s);
1094 if (ret == 0)
1095 return 0;
1096
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001097cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -08001098 do_become_nonbusy(dev, s);
1099
1100 return ret;
1101}
1102
1103/*
1104 COMEDI_CMDTEST
1105 command testing ioctl
1106
1107 arg:
1108 pointer to cmd structure
1109
1110 reads:
1111 cmd structure at arg
1112 channel/range list
1113
1114 writes:
1115 modified cmd structure at arg
1116
1117*/
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001118static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001119{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001120 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001121 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001122 int ret = 0;
1123 unsigned int *chanlist = NULL;
1124 unsigned int *chanlist_saver = NULL;
1125
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001126 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001127 DPRINTK("bad cmd address\n");
1128 return -EFAULT;
1129 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001130 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001131 chanlist_saver = user_cmd.chanlist;
1132
1133 if (user_cmd.subdev >= dev->n_subdevices) {
1134 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1135 return -ENODEV;
1136 }
1137
1138 s = dev->subdevices + user_cmd.subdev;
1139 if (s->type == COMEDI_SUBD_UNUSED) {
1140 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1141 return -EIO;
1142 }
1143
1144 if (!s->do_cmd || !s->do_cmdtest) {
1145 DPRINTK("subdevice %i does not support commands\n",
1146 user_cmd.subdev);
1147 return -EIO;
1148 }
1149
1150 /* make sure channel/gain list isn't too long */
1151 if (user_cmd.chanlist_len > s->len_chanlist) {
1152 DPRINTK("channel/gain list too long %d > %d\n",
1153 user_cmd.chanlist_len, s->len_chanlist);
1154 ret = -EINVAL;
1155 goto cleanup;
1156 }
1157
1158 /* load channel/gain list */
1159 if (user_cmd.chanlist) {
1160 chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001161 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001162 if (!chanlist) {
1163 DPRINTK("allocation failed\n");
1164 ret = -ENOMEM;
1165 goto cleanup;
1166 }
1167
1168 if (copy_from_user(chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001169 user_cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001170 DPRINTK("fault reading chanlist\n");
1171 ret = -EFAULT;
1172 goto cleanup;
1173 }
1174
1175 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001176 ret = check_chanlist(s, user_cmd.chanlist_len, chanlist);
1177 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001178 DPRINTK("bad chanlist\n");
1179 goto cleanup;
1180 }
1181
1182 user_cmd.chanlist = chanlist;
1183 }
1184
1185 ret = s->do_cmdtest(dev, s, &user_cmd);
1186
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001187 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001188 user_cmd.chanlist = chanlist_saver;
1189
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001190 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001191 DPRINTK("bad cmd address\n");
1192 ret = -EFAULT;
1193 goto cleanup;
1194 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001195cleanup:
1196 kfree(chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001197
1198 return ret;
1199}
1200
1201/*
1202 COMEDI_LOCK
1203 lock subdevice
1204
1205 arg:
1206 subdevice number
1207
1208 reads:
1209 none
1210
1211 writes:
1212 none
1213
1214*/
1215
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301216static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1217 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001218{
1219 int ret = 0;
1220 unsigned long flags;
Bill Pemberton34c43922009-03-16 22:05:14 -04001221 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001222
1223 if (arg >= dev->n_subdevices)
1224 return -EINVAL;
1225 s = dev->subdevices + arg;
1226
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001227 spin_lock_irqsave(&s->spin_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001228 if (s->busy || s->lock)
David Schleefed9eccb2008-11-04 20:29:31 -08001229 ret = -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001230 else
David Schleefed9eccb2008-11-04 20:29:31 -08001231 s->lock = file;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001232 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08001233
1234 if (ret < 0)
1235 return ret;
1236
1237#if 0
1238 if (s->lock_f)
1239 ret = s->lock_f(dev, s);
1240#endif
1241
1242 return ret;
1243}
1244
1245/*
1246 COMEDI_UNLOCK
1247 unlock subdevice
1248
1249 arg:
1250 subdevice number
1251
1252 reads:
1253 none
1254
1255 writes:
1256 none
1257
1258 This function isn't protected by the semaphore, since
1259 we already own the lock.
1260*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301261static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1262 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001263{
Bill Pemberton34c43922009-03-16 22:05:14 -04001264 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001265
1266 if (arg >= dev->n_subdevices)
1267 return -EINVAL;
1268 s = dev->subdevices + arg;
1269
1270 if (s->busy)
1271 return -EBUSY;
1272
1273 if (s->lock && s->lock != file)
1274 return -EACCES;
1275
1276 if (s->lock == file) {
1277#if 0
1278 if (s->unlock)
1279 s->unlock(dev, s);
1280#endif
1281
1282 s->lock = NULL;
1283 }
1284
1285 return 0;
1286}
1287
1288/*
1289 COMEDI_CANCEL
1290 cancel acquisition ioctl
1291
1292 arg:
1293 subdevice number
1294
1295 reads:
1296 nothing
1297
1298 writes:
1299 nothing
1300
1301*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301302static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1303 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001304{
Bill Pemberton34c43922009-03-16 22:05:14 -04001305 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001306
1307 if (arg >= dev->n_subdevices)
1308 return -EINVAL;
1309 s = dev->subdevices + arg;
1310 if (s->async == NULL)
1311 return -EINVAL;
1312
1313 if (s->lock && s->lock != file)
1314 return -EACCES;
1315
1316 if (!s->busy)
1317 return 0;
1318
1319 if (s->busy != file)
1320 return -EBUSY;
1321
1322 return do_cancel(dev, s);
1323}
1324
1325/*
1326 COMEDI_POLL ioctl
1327 instructs driver to synchronize buffers
1328
1329 arg:
1330 subdevice number
1331
1332 reads:
1333 nothing
1334
1335 writes:
1336 nothing
1337
1338*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301339static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1340 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001341{
Bill Pemberton34c43922009-03-16 22:05:14 -04001342 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001343
1344 if (arg >= dev->n_subdevices)
1345 return -EINVAL;
1346 s = dev->subdevices + arg;
1347
1348 if (s->lock && s->lock != file)
1349 return -EACCES;
1350
1351 if (!s->busy)
1352 return 0;
1353
1354 if (s->busy != file)
1355 return -EBUSY;
1356
1357 if (s->poll)
1358 return s->poll(dev, s);
1359
1360 return -EINVAL;
1361}
1362
Bill Pemberton34c43922009-03-16 22:05:14 -04001363static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001364{
1365 int ret = 0;
1366
1367 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1368 ret = s->cancel(dev, s);
1369
1370 do_become_nonbusy(dev, s);
1371
1372 return ret;
1373}
1374
1375void comedi_unmap(struct vm_area_struct *area)
1376{
Bill Pembertond1636792009-03-16 22:05:20 -04001377 struct comedi_async *async;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001378 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -08001379
1380 async = area->vm_private_data;
1381 dev = async->subdevice->device;
1382
1383 mutex_lock(&dev->mutex);
1384 async->mmap_count--;
1385 mutex_unlock(&dev->mutex);
1386}
1387
1388static struct vm_operations_struct comedi_vm_ops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301389 .close = comedi_unmap,
David Schleefed9eccb2008-11-04 20:29:31 -08001390};
1391
1392static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1393{
1394 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001395 struct comedi_device_file_info *dev_file_info =
1396 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001397 struct comedi_device *dev = dev_file_info->device;
Bill Pembertond1636792009-03-16 22:05:20 -04001398 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001399 unsigned long start = vma->vm_start;
1400 unsigned long size;
1401 int n_pages;
1402 int i;
1403 int retval;
Bill Pemberton34c43922009-03-16 22:05:14 -04001404 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001405
1406 mutex_lock(&dev->mutex);
1407 if (!dev->attached) {
1408 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1409 retval = -ENODEV;
1410 goto done;
1411 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001412 if (vma->vm_flags & VM_WRITE)
David Schleefed9eccb2008-11-04 20:29:31 -08001413 s = comedi_get_write_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001414 else
David Schleefed9eccb2008-11-04 20:29:31 -08001415 s = comedi_get_read_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001416
David Schleefed9eccb2008-11-04 20:29:31 -08001417 if (s == NULL) {
1418 retval = -EINVAL;
1419 goto done;
1420 }
1421 async = s->async;
1422 if (async == NULL) {
1423 retval = -EINVAL;
1424 goto done;
1425 }
1426
1427 if (vma->vm_pgoff != 0) {
1428 DPRINTK("comedi: mmap() offset must be 0.\n");
1429 retval = -EINVAL;
1430 goto done;
1431 }
1432
1433 size = vma->vm_end - vma->vm_start;
1434 if (size > async->prealloc_bufsz) {
1435 retval = -EFAULT;
1436 goto done;
1437 }
1438 if (size & (~PAGE_MASK)) {
1439 retval = -EFAULT;
1440 goto done;
1441 }
1442
1443 n_pages = size >> PAGE_SHIFT;
1444 for (i = 0; i < n_pages; ++i) {
1445 if (remap_pfn_range(vma, start,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301446 page_to_pfn(virt_to_page
1447 (async->buf_page_list
1448 [i].virt_addr)), PAGE_SIZE,
1449 PAGE_SHARED)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001450 retval = -EAGAIN;
1451 goto done;
1452 }
1453 start += PAGE_SIZE;
1454 }
1455
1456 vma->vm_ops = &comedi_vm_ops;
1457 vma->vm_private_data = async;
1458
1459 async->mmap_count++;
1460
1461 retval = 0;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001462done:
David Schleefed9eccb2008-11-04 20:29:31 -08001463 mutex_unlock(&dev->mutex);
1464 return retval;
1465}
1466
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301467static unsigned int comedi_poll(struct file *file, poll_table * wait)
David Schleefed9eccb2008-11-04 20:29:31 -08001468{
1469 unsigned int mask = 0;
1470 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001471 struct comedi_device_file_info *dev_file_info =
1472 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001473 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001474 struct comedi_subdevice *read_subdev;
1475 struct comedi_subdevice *write_subdev;
David Schleefed9eccb2008-11-04 20:29:31 -08001476
1477 mutex_lock(&dev->mutex);
1478 if (!dev->attached) {
1479 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1480 mutex_unlock(&dev->mutex);
1481 return 0;
1482 }
1483
1484 mask = 0;
1485 read_subdev = comedi_get_read_subdevice(dev_file_info);
1486 if (read_subdev) {
1487 poll_wait(file, &read_subdev->async->wait_head, wait);
1488 if (!read_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001489 || comedi_buf_read_n_available(read_subdev->async) > 0
1490 || !(comedi_get_subdevice_runflags(read_subdev) &
1491 SRF_RUNNING)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001492 mask |= POLLIN | POLLRDNORM;
1493 }
1494 }
1495 write_subdev = comedi_get_write_subdevice(dev_file_info);
1496 if (write_subdev) {
1497 poll_wait(file, &write_subdev->async->wait_head, wait);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001498 comedi_buf_write_alloc(write_subdev->async,
1499 write_subdev->async->prealloc_bufsz);
David Schleefed9eccb2008-11-04 20:29:31 -08001500 if (!write_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001501 || !(comedi_get_subdevice_runflags(write_subdev) &
1502 SRF_RUNNING)
1503 || comedi_buf_write_n_allocated(write_subdev->async) >=
1504 bytes_per_sample(write_subdev->async->subdevice)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001505 mask |= POLLOUT | POLLWRNORM;
1506 }
1507 }
1508
1509 mutex_unlock(&dev->mutex);
1510 return mask;
1511}
1512
1513static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01001514 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001515{
Bill Pemberton34c43922009-03-16 22:05:14 -04001516 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001517 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001518 int n, m, count = 0, retval = 0;
1519 DECLARE_WAITQUEUE(wait, current);
1520 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001521 struct comedi_device_file_info *dev_file_info =
1522 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001523 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001524
1525 if (!dev->attached) {
1526 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1527 retval = -ENODEV;
1528 goto done;
1529 }
1530
1531 s = comedi_get_write_subdevice(dev_file_info);
1532 if (s == NULL) {
1533 retval = -EIO;
1534 goto done;
1535 }
1536 async = s->async;
1537
1538 if (!nbytes) {
1539 retval = 0;
1540 goto done;
1541 }
1542 if (!s->busy) {
1543 retval = 0;
1544 goto done;
1545 }
1546 if (s->busy != file) {
1547 retval = -EACCES;
1548 goto done;
1549 }
1550 add_wait_queue(&async->wait_head, &wait);
1551 while (nbytes > 0 && !retval) {
1552 set_current_state(TASK_INTERRUPTIBLE);
1553
1554 n = nbytes;
1555
1556 m = n;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001557 if (async->buf_write_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001558 m = async->prealloc_bufsz - async->buf_write_ptr;
David Schleefed9eccb2008-11-04 20:29:31 -08001559 comedi_buf_write_alloc(async, async->prealloc_bufsz);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001560 if (m > comedi_buf_write_n_allocated(async))
David Schleefed9eccb2008-11-04 20:29:31 -08001561 m = comedi_buf_write_n_allocated(async);
David Schleefed9eccb2008-11-04 20:29:31 -08001562 if (m < n)
1563 n = m;
1564
1565 if (n == 0) {
1566 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1567 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001568 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001569 retval = -EPIPE;
1570 } else {
1571 retval = 0;
1572 }
1573 do_become_nonbusy(dev, s);
1574 break;
1575 }
1576 if (file->f_flags & O_NONBLOCK) {
1577 retval = -EAGAIN;
1578 break;
1579 }
1580 if (signal_pending(current)) {
1581 retval = -ERESTARTSYS;
1582 break;
1583 }
1584 schedule();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001585 if (!s->busy)
David Schleefed9eccb2008-11-04 20:29:31 -08001586 break;
David Schleefed9eccb2008-11-04 20:29:31 -08001587 if (s->busy != file) {
1588 retval = -EACCES;
1589 break;
1590 }
1591 continue;
1592 }
1593
1594 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001595 buf, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001596 if (m) {
1597 n -= m;
1598 retval = -EFAULT;
1599 }
1600 comedi_buf_write_free(async, n);
1601
1602 count += n;
1603 nbytes -= n;
1604
1605 buf += n;
1606 break; /* makes device work like a pipe */
1607 }
1608 set_current_state(TASK_RUNNING);
1609 remove_wait_queue(&async->wait_head, &wait);
1610
1611done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001612 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001613}
1614
1615static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01001616 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001617{
Bill Pemberton34c43922009-03-16 22:05:14 -04001618 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001619 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001620 int n, m, count = 0, retval = 0;
1621 DECLARE_WAITQUEUE(wait, current);
1622 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001623 struct comedi_device_file_info *dev_file_info =
1624 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001625 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001626
1627 if (!dev->attached) {
1628 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1629 retval = -ENODEV;
1630 goto done;
1631 }
1632
1633 s = comedi_get_read_subdevice(dev_file_info);
1634 if (s == NULL) {
1635 retval = -EIO;
1636 goto done;
1637 }
1638 async = s->async;
1639 if (!nbytes) {
1640 retval = 0;
1641 goto done;
1642 }
1643 if (!s->busy) {
1644 retval = 0;
1645 goto done;
1646 }
1647 if (s->busy != file) {
1648 retval = -EACCES;
1649 goto done;
1650 }
1651
1652 add_wait_queue(&async->wait_head, &wait);
1653 while (nbytes > 0 && !retval) {
1654 set_current_state(TASK_INTERRUPTIBLE);
1655
1656 n = nbytes;
1657
1658 m = comedi_buf_read_n_available(async);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001659 /* printk("%d available\n",m); */
1660 if (async->buf_read_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001661 m = async->prealloc_bufsz - async->buf_read_ptr;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001662 /* printk("%d contiguous\n",m); */
David Schleefed9eccb2008-11-04 20:29:31 -08001663 if (m < n)
1664 n = m;
1665
1666 if (n == 0) {
1667 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1668 do_become_nonbusy(dev, s);
1669 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001670 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001671 retval = -EPIPE;
1672 } else {
1673 retval = 0;
1674 }
1675 break;
1676 }
1677 if (file->f_flags & O_NONBLOCK) {
1678 retval = -EAGAIN;
1679 break;
1680 }
1681 if (signal_pending(current)) {
1682 retval = -ERESTARTSYS;
1683 break;
1684 }
1685 schedule();
1686 if (!s->busy) {
1687 retval = 0;
1688 break;
1689 }
1690 if (s->busy != file) {
1691 retval = -EACCES;
1692 break;
1693 }
1694 continue;
1695 }
1696 m = copy_to_user(buf, async->prealloc_buf +
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001697 async->buf_read_ptr, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001698 if (m) {
1699 n -= m;
1700 retval = -EFAULT;
1701 }
1702
1703 comedi_buf_read_alloc(async, n);
1704 comedi_buf_read_free(async, n);
1705
1706 count += n;
1707 nbytes -= n;
1708
1709 buf += n;
1710 break; /* makes device work like a pipe */
1711 }
1712 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001713 async->buf_read_count - async->buf_write_count == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001714 do_become_nonbusy(dev, s);
1715 }
1716 set_current_state(TASK_RUNNING);
1717 remove_wait_queue(&async->wait_head, &wait);
1718
1719done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001720 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001721}
1722
1723/*
1724 This function restores a subdevice to an idle state.
1725 */
Bill Pemberton34c43922009-03-16 22:05:14 -04001726void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001727{
Bill Pembertond1636792009-03-16 22:05:20 -04001728 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001729
1730 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001731 if (async) {
1732 comedi_reset_async_buf(async);
1733 async->inttrig = NULL;
1734 } else {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001735 printk(KERN_ERR
1736 "BUG: (?) do_become_nonbusy called with async=0\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001737 }
1738
1739 s->busy = NULL;
1740}
1741
1742static int comedi_open(struct inode *inode, struct file *file)
1743{
David Schleefed9eccb2008-11-04 20:29:31 -08001744 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001745 struct comedi_device_file_info *dev_file_info =
1746 comedi_get_device_file_info(minor);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301747 struct comedi_device *dev =
1748 dev_file_info ? dev_file_info->device : NULL;
Ian Abbott97920072009-02-09 16:51:38 +00001749
David Schleefed9eccb2008-11-04 20:29:31 -08001750 if (dev == NULL) {
1751 DPRINTK("invalid minor number\n");
1752 return -ENODEV;
1753 }
1754
1755 /* This is slightly hacky, but we want module autoloading
1756 * to work for root.
1757 * case: user opens device, attached -> ok
1758 * case: user opens device, unattached, in_request_module=0 -> autoload
1759 * case: user opens device, unattached, in_request_module=1 -> fail
1760 * case: root opens device, attached -> ok
1761 * case: root opens device, unattached, in_request_module=1 -> ok
1762 * (typically called from modprobe)
1763 * case: root opens device, unattached, in_request_module=0 -> autoload
1764 *
1765 * The last could be changed to "-> ok", which would deny root
1766 * autoloading.
1767 */
1768 mutex_lock(&dev->mutex);
1769 if (dev->attached)
1770 goto ok;
Eric Parisa8f80e82009-08-13 09:44:51 -04001771 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
David Schleefed9eccb2008-11-04 20:29:31 -08001772 DPRINTK("in request module\n");
1773 mutex_unlock(&dev->mutex);
1774 return -ENODEV;
1775 }
Eric Parisa8f80e82009-08-13 09:44:51 -04001776 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
David Schleefed9eccb2008-11-04 20:29:31 -08001777 goto ok;
1778
1779 dev->in_request_module = 1;
1780
David Schleefed9eccb2008-11-04 20:29:31 -08001781#ifdef CONFIG_KMOD
1782 mutex_unlock(&dev->mutex);
Ian Abbott56d92c62009-02-09 16:32:12 +00001783 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
David Schleefed9eccb2008-11-04 20:29:31 -08001784 mutex_lock(&dev->mutex);
1785#endif
1786
1787 dev->in_request_module = 0;
1788
Eric Parisa8f80e82009-08-13 09:44:51 -04001789 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1790 DPRINTK("not attached and not CAP_NET_ADMIN\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001791 mutex_unlock(&dev->mutex);
1792 return -ENODEV;
1793 }
1794ok:
1795 __module_get(THIS_MODULE);
1796
1797 if (dev->attached) {
1798 if (!try_module_get(dev->driver->module)) {
1799 module_put(THIS_MODULE);
1800 mutex_unlock(&dev->mutex);
1801 return -ENOSYS;
1802 }
1803 }
1804
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001805 if (dev->attached && dev->use_count == 0 && dev->open)
David Schleefed9eccb2008-11-04 20:29:31 -08001806 dev->open(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001807
1808 dev->use_count++;
1809
1810 mutex_unlock(&dev->mutex);
1811
1812 return 0;
1813}
1814
1815static int comedi_close(struct inode *inode, struct file *file)
1816{
1817 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001818 struct comedi_device_file_info *dev_file_info =
1819 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001820 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001821 struct comedi_subdevice *s = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001822 int i;
1823
1824 mutex_lock(&dev->mutex);
1825
1826 if (dev->subdevices) {
1827 for (i = 0; i < dev->n_subdevices; i++) {
1828 s = dev->subdevices + i;
1829
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001830 if (s->busy == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001831 do_cancel(dev, s);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001832 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001833 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001834 }
1835 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001836 if (dev->attached && dev->use_count == 1 && dev->close)
David Schleefed9eccb2008-11-04 20:29:31 -08001837 dev->close(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001838
1839 module_put(THIS_MODULE);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001840 if (dev->attached)
David Schleefed9eccb2008-11-04 20:29:31 -08001841 module_put(dev->driver->module);
David Schleefed9eccb2008-11-04 20:29:31 -08001842
1843 dev->use_count--;
1844
1845 mutex_unlock(&dev->mutex);
1846
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001847 if (file->f_flags & FASYNC)
David Schleefed9eccb2008-11-04 20:29:31 -08001848 comedi_fasync(-1, file, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001849
1850 return 0;
1851}
1852
1853static int comedi_fasync(int fd, struct file *file, int on)
1854{
1855 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001856 struct comedi_device_file_info *dev_file_info =
1857 comedi_get_device_file_info(minor);
1858
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001859 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001860
1861 return fasync_helper(fd, file, on, &dev->async_queue);
1862}
1863
1864const struct file_operations comedi_fops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301865 .owner = THIS_MODULE,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301866 .unlocked_ioctl = comedi_unlocked_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301867 .compat_ioctl = comedi_compat_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301868 .open = comedi_open,
1869 .release = comedi_close,
1870 .read = comedi_read,
1871 .write = comedi_write,
1872 .mmap = comedi_mmap,
1873 .poll = comedi_poll,
1874 .fasync = comedi_fasync,
David Schleefed9eccb2008-11-04 20:29:31 -08001875};
1876
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001877struct class *comedi_class;
David Schleefed9eccb2008-11-04 20:29:31 -08001878static struct cdev comedi_cdev;
1879
1880static void comedi_cleanup_legacy_minors(void)
1881{
1882 unsigned i;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001883
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001884 for (i = 0; i < comedi_num_legacy_minors; i++)
David Schleefed9eccb2008-11-04 20:29:31 -08001885 comedi_free_board_minor(i);
David Schleefed9eccb2008-11-04 20:29:31 -08001886}
1887
1888static int __init comedi_init(void)
1889{
1890 int i;
1891 int retval;
1892
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001893 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1894 " - http://www.comedi.org\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001895
Frank Mori Hessa3cb7292008-12-15 13:44:45 +00001896 if (comedi_num_legacy_minors < 0 ||
1897 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
1898 printk(KERN_ERR "comedi: error: invalid value for module "
1899 "parameter \"comedi_num_legacy_minors\". Valid values "
1900 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
1901 return -EINVAL;
1902 }
1903
1904 /*
1905 * comedi is unusable if both comedi_autoconfig and
1906 * comedi_num_legacy_minors are zero, so we might as well adjust the
1907 * defaults in that case
1908 */
1909 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
1910 comedi_num_legacy_minors = 16;
1911
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001912 memset(comedi_file_info_table, 0,
1913 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001914
1915 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001916 COMEDI_NUM_MINORS, "comedi");
David Schleefed9eccb2008-11-04 20:29:31 -08001917 if (retval)
1918 return -EIO;
1919 cdev_init(&comedi_cdev, &comedi_fops);
1920 comedi_cdev.owner = THIS_MODULE;
1921 kobject_set_name(&comedi_cdev.kobj, "comedi");
1922 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1923 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001924 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001925 return -EIO;
1926 }
1927 comedi_class = class_create(THIS_MODULE, "comedi");
1928 if (IS_ERR(comedi_class)) {
Mark Rankilor3fffdf22010-04-29 18:17:16 +08001929 printk(KERN_ERR "comedi: failed to create class");
David Schleefed9eccb2008-11-04 20:29:31 -08001930 cdev_del(&comedi_cdev);
1931 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001932 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001933 return PTR_ERR(comedi_class);
1934 }
1935
1936 /* XXX requires /proc interface */
1937 comedi_proc_init();
1938
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001939 /* create devices files for legacy/manual use */
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001940 for (i = 0; i < comedi_num_legacy_minors; i++) {
David Schleefed9eccb2008-11-04 20:29:31 -08001941 int minor;
1942 minor = comedi_alloc_board_minor(NULL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001943 if (minor < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001944 comedi_cleanup_legacy_minors();
1945 cdev_del(&comedi_cdev);
1946 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001947 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001948 return minor;
1949 }
1950 }
1951
David Schleefed9eccb2008-11-04 20:29:31 -08001952 return 0;
1953}
1954
1955static void __exit comedi_cleanup(void)
1956{
1957 int i;
1958
1959 comedi_cleanup_legacy_minors();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001960 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
David Schleefed9eccb2008-11-04 20:29:31 -08001961 BUG_ON(comedi_file_info_table[i]);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001962
David Schleefed9eccb2008-11-04 20:29:31 -08001963 class_destroy(comedi_class);
1964 cdev_del(&comedi_cdev);
1965 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
1966
1967 comedi_proc_cleanup();
David Schleefed9eccb2008-11-04 20:29:31 -08001968}
1969
1970module_init(comedi_init);
1971module_exit(comedi_cleanup);
1972
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001973void comedi_error(const struct comedi_device *dev, const char *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001974{
Mark Rankilor3fffdf22010-04-29 18:17:16 +08001975 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
1976 dev->driver->driver_name, s);
David Schleefed9eccb2008-11-04 20:29:31 -08001977}
1978
Bill Pemberton34c43922009-03-16 22:05:14 -04001979void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001980{
Bill Pembertond1636792009-03-16 22:05:20 -04001981 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001982 unsigned runflags = 0;
1983 unsigned runflags_mask = 0;
1984
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001985 /* DPRINTK("comedi_event 0x%x\n",mask); */
David Schleefed9eccb2008-11-04 20:29:31 -08001986
1987 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
1988 return;
1989
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301990 if (s->
1991 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
1992 COMEDI_CB_OVERFLOW)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001993 runflags_mask |= SRF_RUNNING;
1994 }
1995 /* remember if an error event has occured, so an error
1996 * can be returned the next time the user does a read() */
1997 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
1998 runflags_mask |= SRF_ERROR;
1999 runflags |= SRF_ERROR;
2000 }
2001 if (runflags_mask) {
2002 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2003 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2004 }
2005
2006 if (async->cb_mask & s->async->events) {
2007 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
Greg Kroah-Hartmanfcea1152009-04-27 15:15:30 -07002008 wake_up_interruptible(&async->wait_head);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002009 if (s->subdev_flags & SDF_CMD_READ)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302010 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002011 if (s->subdev_flags & SDF_CMD_WRITE)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302012 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
David Schleefed9eccb2008-11-04 20:29:31 -08002013 } else {
2014 if (async->cb_func)
2015 async->cb_func(s->async->events, async->cb_arg);
David Schleefed9eccb2008-11-04 20:29:31 -08002016 }
2017 }
2018 s->async->events = 0;
2019}
2020
Bill Pemberton34c43922009-03-16 22:05:14 -04002021void comedi_set_subdevice_runflags(struct comedi_subdevice *s, unsigned mask,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002022 unsigned bits)
David Schleefed9eccb2008-11-04 20:29:31 -08002023{
2024 unsigned long flags;
2025
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002026 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002027 s->runflags &= ~mask;
2028 s->runflags |= (bits & mask);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002029 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002030}
2031
Bill Pemberton34c43922009-03-16 22:05:14 -04002032unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002033{
2034 unsigned long flags;
2035 unsigned runflags;
2036
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002037 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002038 runflags = s->runflags;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002039 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002040 return runflags;
2041}
2042
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002043static int is_device_busy(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002044{
Bill Pemberton34c43922009-03-16 22:05:14 -04002045 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08002046 int i;
2047
2048 if (!dev->attached)
2049 return 0;
2050
2051 for (i = 0; i < dev->n_subdevices; i++) {
2052 s = dev->subdevices + i;
2053 if (s->busy)
2054 return 1;
2055 if (s->async && s->async->mmap_count)
2056 return 1;
2057 }
2058
2059 return 0;
2060}
2061
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002062void comedi_device_init(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002063{
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002064 memset(dev, 0, sizeof(struct comedi_device));
David Schleefed9eccb2008-11-04 20:29:31 -08002065 spin_lock_init(&dev->spinlock);
2066 mutex_init(&dev->mutex);
2067 dev->minor = -1;
2068}
2069
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002070void comedi_device_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002071{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002072 if (dev == NULL)
2073 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002074 mutex_lock(&dev->mutex);
2075 comedi_device_detach(dev);
2076 mutex_unlock(&dev->mutex);
2077 mutex_destroy(&dev->mutex);
2078}
2079
2080int comedi_alloc_board_minor(struct device *hardware_device)
2081{
2082 unsigned long flags;
2083 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002084 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002085 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002086 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002087
2088 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002089 if (info == NULL)
2090 return -ENOMEM;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002091 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002092 if (info->device == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002093 kfree(info);
2094 return -ENOMEM;
2095 }
2096 comedi_device_init(info->device);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002097 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002098 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2099 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002100 comedi_file_info_table[i] = info;
2101 break;
2102 }
2103 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002104 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002105 if (i == COMEDI_NUM_BOARD_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002106 comedi_device_cleanup(info->device);
2107 kfree(info->device);
2108 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302109 printk(KERN_ERR
Pieter De Praetere20617f22010-03-10 09:47:44 +01002110
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302111 "comedi: error: ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002112 return -EBUSY;
2113 }
2114 info->device->minor = i;
2115 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002116 MKDEV(COMEDI_MAJOR, i), NULL,
2117 hardware_device, "comedi%i", i);
2118 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002119 info->device->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002120 dev_set_drvdata(csdev, info);
2121 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2122 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302123 printk(KERN_ERR
2124 "comedi: failed to create sysfs attribute file \"%s\".\n",
2125 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002126 comedi_free_board_minor(i);
2127 return retval;
2128 }
2129 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2130 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302131 printk(KERN_ERR
2132 "comedi: failed to create sysfs attribute file \"%s\".\n",
2133 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002134 comedi_free_board_minor(i);
2135 return retval;
2136 }
2137 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2138 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302139 printk(KERN_ERR
2140 "comedi: failed to create sysfs attribute file \"%s\".\n",
2141 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002142 comedi_free_board_minor(i);
2143 return retval;
2144 }
2145 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2146 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302147 printk(KERN_ERR
2148 "comedi: failed to create sysfs attribute file \"%s\".\n",
2149 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002150 comedi_free_board_minor(i);
2151 return retval;
2152 }
David Schleefed9eccb2008-11-04 20:29:31 -08002153 return i;
2154}
2155
2156void comedi_free_board_minor(unsigned minor)
2157{
2158 unsigned long flags;
2159 struct comedi_device_file_info *info;
2160
2161 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002162 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002163 info = comedi_file_info_table[minor];
2164 comedi_file_info_table[minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002165 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002166
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002167 if (info) {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002168 struct comedi_device *dev = info->device;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002169 if (dev) {
2170 if (dev->class_dev) {
2171 device_destroy(comedi_class,
2172 MKDEV(COMEDI_MAJOR, dev->minor));
David Schleefed9eccb2008-11-04 20:29:31 -08002173 }
2174 comedi_device_cleanup(dev);
2175 kfree(dev);
2176 }
2177 kfree(info);
2178 }
2179}
2180
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002181int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2182 struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002183{
2184 unsigned long flags;
2185 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002186 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002187 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002188 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002189
2190 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002191 if (info == NULL)
2192 return -ENOMEM;
David Schleefed9eccb2008-11-04 20:29:31 -08002193 info->device = dev;
2194 info->read_subdevice = s;
2195 info->write_subdevice = s;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002196 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Frank Mori Hess4c41f3a2008-12-09 14:47:22 +00002197 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002198 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002199 comedi_file_info_table[i] = info;
2200 break;
2201 }
2202 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002203 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002204 if (i == COMEDI_NUM_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002205 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302206 printk(KERN_ERR
2207 "comedi: error: ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002208 return -EBUSY;
2209 }
2210 s->minor = i;
2211 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002212 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2213 "comedi%i_subd%i", dev->minor,
2214 (int)(s - dev->subdevices));
2215 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002216 s->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002217 dev_set_drvdata(csdev, info);
2218 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2219 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302220 printk(KERN_ERR
2221 "comedi: failed to create sysfs attribute file \"%s\".\n",
2222 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002223 comedi_free_subdevice_minor(s);
2224 return retval;
2225 }
2226 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2227 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302228 printk(KERN_ERR
2229 "comedi: failed to create sysfs attribute file \"%s\".\n",
2230 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002231 comedi_free_subdevice_minor(s);
2232 return retval;
2233 }
2234 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2235 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302236 printk(KERN_ERR
2237 "comedi: failed to create sysfs attribute file \"%s\".\n",
2238 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002239 comedi_free_subdevice_minor(s);
2240 return retval;
2241 }
2242 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2243 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302244 printk(KERN_ERR
2245 "comedi: failed to create sysfs attribute file \"%s\".\n",
2246 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002247 comedi_free_subdevice_minor(s);
2248 return retval;
2249 }
David Schleefed9eccb2008-11-04 20:29:31 -08002250 return i;
2251}
2252
Bill Pemberton34c43922009-03-16 22:05:14 -04002253void comedi_free_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002254{
2255 unsigned long flags;
2256 struct comedi_device_file_info *info;
2257
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002258 if (s == NULL)
2259 return;
2260 if (s->minor < 0)
2261 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002262
2263 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2264 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2265
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002266 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002267 info = comedi_file_info_table[s->minor];
2268 comedi_file_info_table[s->minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002269 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002270
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002271 if (s->class_dev) {
David Schleefed9eccb2008-11-04 20:29:31 -08002272 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2273 s->class_dev = NULL;
2274 }
2275 kfree(info);
2276}
2277
2278struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2279{
2280 unsigned long flags;
2281 struct comedi_device_file_info *info;
2282
2283 BUG_ON(minor >= COMEDI_NUM_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002284 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002285 info = comedi_file_info_table[minor];
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002286 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002287 return info;
2288}
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002289
2290static int resize_async_buffer(struct comedi_device *dev,
2291 struct comedi_subdevice *s,
2292 struct comedi_async *async, unsigned new_size)
2293{
2294 int retval;
2295
2296 if (new_size > async->max_bufsize)
2297 return -EPERM;
2298
2299 if (s->busy) {
2300 DPRINTK("subdevice is busy, cannot resize buffer\n");
2301 return -EBUSY;
2302 }
2303 if (async->mmap_count) {
2304 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2305 return -EBUSY;
2306 }
2307
2308 if (!async->prealloc_buf)
2309 return -EINVAL;
2310
2311 /* make sure buffer is an integral number of pages
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302312 * (we round up) */
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002313 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2314
2315 retval = comedi_buf_alloc(dev, s, new_size);
2316 if (retval < 0)
2317 return retval;
2318
2319 if (s->buf_change) {
2320 retval = s->buf_change(dev, s, new_size);
2321 if (retval < 0)
2322 return retval;
2323 }
2324
2325 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
Ian Abbottb8b5cd92009-09-21 14:55:23 -04002326 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002327 return 0;
2328}
2329
2330/* sysfs attribute files */
2331
2332static const unsigned bytes_per_kibi = 1024;
2333
2334static ssize_t show_max_read_buffer_kb(struct device *dev,
2335 struct device_attribute *attr, char *buf)
2336{
2337 ssize_t retval;
2338 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2339 unsigned max_buffer_size_kb = 0;
2340 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302341 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002342
2343 mutex_lock(&info->device->mutex);
2344 if (read_subdevice &&
2345 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2346 read_subdevice->async) {
2347 max_buffer_size_kb = read_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302348 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002349 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302350 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002351 mutex_unlock(&info->device->mutex);
2352
2353 return retval;
2354}
2355
2356static ssize_t store_max_read_buffer_kb(struct device *dev,
2357 struct device_attribute *attr,
2358 const char *buf, size_t count)
2359{
2360 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2361 unsigned long new_max_size_kb;
2362 uint64_t new_max_size;
2363 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302364 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002365
2366 if (strict_strtoul(buf, 10, &new_max_size_kb))
2367 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302368 if (new_max_size_kb != (uint32_t) new_max_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002369 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302370 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2371 if (new_max_size != (uint32_t) new_max_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002372 return -EINVAL;
2373
2374 mutex_lock(&info->device->mutex);
2375 if (read_subdevice == NULL ||
2376 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2377 read_subdevice->async == NULL) {
2378 mutex_unlock(&info->device->mutex);
2379 return -EINVAL;
2380 }
2381 read_subdevice->async->max_bufsize = new_max_size;
2382 mutex_unlock(&info->device->mutex);
2383
2384 return count;
2385}
2386
2387static struct device_attribute dev_attr_max_read_buffer_kb = {
2388 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302389 .name = "max_read_buffer_kb",
2390 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002391 .show = &show_max_read_buffer_kb,
2392 .store = &store_max_read_buffer_kb
2393};
2394
2395static ssize_t show_read_buffer_kb(struct device *dev,
2396 struct device_attribute *attr, char *buf)
2397{
2398 ssize_t retval;
2399 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2400 unsigned buffer_size_kb = 0;
2401 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302402 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002403
2404 mutex_lock(&info->device->mutex);
2405 if (read_subdevice &&
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302406 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2407 read_subdevice->async) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002408 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302409 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002410 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302411 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002412 mutex_unlock(&info->device->mutex);
2413
2414 return retval;
2415}
2416
2417static ssize_t store_read_buffer_kb(struct device *dev,
2418 struct device_attribute *attr,
2419 const char *buf, size_t count)
2420{
2421 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2422 unsigned long new_size_kb;
2423 uint64_t new_size;
2424 int retval;
2425 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302426 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002427
2428 if (strict_strtoul(buf, 10, &new_size_kb))
2429 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302430 if (new_size_kb != (uint32_t) new_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002431 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302432 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2433 if (new_size != (uint32_t) new_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002434 return -EINVAL;
2435
2436 mutex_lock(&info->device->mutex);
2437 if (read_subdevice == NULL ||
2438 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2439 read_subdevice->async == NULL) {
2440 mutex_unlock(&info->device->mutex);
2441 return -EINVAL;
2442 }
2443 retval = resize_async_buffer(info->device, read_subdevice,
2444 read_subdevice->async, new_size);
2445 mutex_unlock(&info->device->mutex);
2446
2447 if (retval < 0)
2448 return retval;
2449 return count;
2450}
2451
2452static struct device_attribute dev_attr_read_buffer_kb = {
2453 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302454 .name = "read_buffer_kb",
2455 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002456 .show = &show_read_buffer_kb,
2457 .store = &store_read_buffer_kb
2458};
2459
2460static ssize_t show_max_write_buffer_kb(struct device *dev,
2461 struct device_attribute *attr,
2462 char *buf)
2463{
2464 ssize_t retval;
2465 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2466 unsigned max_buffer_size_kb = 0;
2467 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302468 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002469
2470 mutex_lock(&info->device->mutex);
2471 if (write_subdevice &&
2472 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2473 write_subdevice->async) {
2474 max_buffer_size_kb = write_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302475 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002476 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302477 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002478 mutex_unlock(&info->device->mutex);
2479
2480 return retval;
2481}
2482
2483static ssize_t store_max_write_buffer_kb(struct device *dev,
2484 struct device_attribute *attr,
2485 const char *buf, size_t count)
2486{
2487 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2488 unsigned long new_max_size_kb;
2489 uint64_t new_max_size;
2490 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302491 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002492
2493 if (strict_strtoul(buf, 10, &new_max_size_kb))
2494 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302495 if (new_max_size_kb != (uint32_t) new_max_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002496 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302497 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2498 if (new_max_size != (uint32_t) new_max_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002499 return -EINVAL;
2500
2501 mutex_lock(&info->device->mutex);
2502 if (write_subdevice == NULL ||
2503 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2504 write_subdevice->async == NULL) {
2505 mutex_unlock(&info->device->mutex);
2506 return -EINVAL;
2507 }
2508 write_subdevice->async->max_bufsize = new_max_size;
2509 mutex_unlock(&info->device->mutex);
2510
2511 return count;
2512}
2513
2514static struct device_attribute dev_attr_max_write_buffer_kb = {
2515 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302516 .name = "max_write_buffer_kb",
2517 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002518 .show = &show_max_write_buffer_kb,
2519 .store = &store_max_write_buffer_kb
2520};
2521
2522static ssize_t show_write_buffer_kb(struct device *dev,
2523 struct device_attribute *attr, char *buf)
2524{
2525 ssize_t retval;
2526 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2527 unsigned buffer_size_kb = 0;
2528 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302529 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002530
2531 mutex_lock(&info->device->mutex);
2532 if (write_subdevice &&
2533 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2534 write_subdevice->async) {
2535 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302536 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002537 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302538 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002539 mutex_unlock(&info->device->mutex);
2540
2541 return retval;
2542}
2543
2544static ssize_t store_write_buffer_kb(struct device *dev,
2545 struct device_attribute *attr,
2546 const char *buf, size_t count)
2547{
2548 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2549 unsigned long new_size_kb;
2550 uint64_t new_size;
2551 int retval;
2552 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302553 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002554
2555 if (strict_strtoul(buf, 10, &new_size_kb))
2556 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302557 if (new_size_kb != (uint32_t) new_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002558 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302559 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2560 if (new_size != (uint32_t) new_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002561 return -EINVAL;
2562
2563 mutex_lock(&info->device->mutex);
2564 if (write_subdevice == NULL ||
2565 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2566 write_subdevice->async == NULL) {
2567 mutex_unlock(&info->device->mutex);
2568 return -EINVAL;
2569 }
2570 retval = resize_async_buffer(info->device, write_subdevice,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302571 write_subdevice->async, new_size);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002572 mutex_unlock(&info->device->mutex);
2573
2574 if (retval < 0)
2575 return retval;
2576 return count;
2577}
2578
2579static struct device_attribute dev_attr_write_buffer_kb = {
2580 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302581 .name = "write_buffer_kb",
2582 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002583 .show = &show_write_buffer_kb,
2584 .store = &store_write_buffer_kb
2585};