blob: 98fa255d862ef9f7e3257ad3eaadfd4fb1053374 [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-Hartman242e7ad2010-05-03 15:20:29 -070052#include "internal.h"
David Schleefed9eccb2008-11-04 20:29:31 -080053
54MODULE_AUTHOR("http://www.comedi.org");
55MODULE_DESCRIPTION("Comedi core module");
56MODULE_LICENSE("GPL");
57
58#ifdef CONFIG_COMEDI_DEBUG
59int comedi_debug;
Greg Kroah-Hartman18736432010-05-01 12:02:23 -070060EXPORT_SYMBOL(comedi_debug);
David Schleefed9eccb2008-11-04 20:29:31 -080061module_param(comedi_debug, int, 0644);
62#endif
63
Ian Abbott6a9d7a22008-12-08 17:05:50 +000064int comedi_autoconfig = 1;
65module_param(comedi_autoconfig, bool, 0444);
66
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070067static int comedi_num_legacy_minors;
Bernd Porr1dd33ab2008-12-08 23:30:13 +000068module_param(comedi_num_legacy_minors, int, 0444);
69
David Schleefed9eccb2008-11-04 20:29:31 -080070static DEFINE_SPINLOCK(comedi_file_info_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080071static struct comedi_device_file_info
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053072*comedi_file_info_table[COMEDI_NUM_MINORS];
David Schleefed9eccb2008-11-04 20:29:31 -080073
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053074static int do_devconfig_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070075 struct comedi_devconfig __user *arg);
76static int do_bufconfig_ioctl(struct comedi_device *dev,
77 struct comedi_bufconfig __user *arg);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053078static int do_devinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070079 struct comedi_devinfo __user *arg,
80 struct file *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053081static int do_subdinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070082 struct comedi_subdinfo __user *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053083static int do_chaninfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070084 struct comedi_chaninfo __user *arg);
85static int do_bufinfo_ioctl(struct comedi_device *dev,
Ian Abbott53fa8272010-05-19 18:09:50 +010086 struct comedi_bufinfo __user *arg, void *file);
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070087static int do_cmd_ioctl(struct comedi_device *dev,
88 struct comedi_cmd __user *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053089static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
90 void *file);
91static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
92 void *file);
93static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
94 void *file);
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070095static int do_cmdtest_ioctl(struct comedi_device *dev,
96 struct comedi_cmd __user *arg, void *file);
97static int do_insnlist_ioctl(struct comedi_device *dev,
98 struct comedi_insnlist __user *arg, void *file);
99static int do_insn_ioctl(struct comedi_device *dev,
100 struct comedi_insn __user *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530101static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
102 void *file);
David Schleefed9eccb2008-11-04 20:29:31 -0800103
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530104extern void do_become_nonbusy(struct comedi_device *dev,
105 struct comedi_subdevice *s);
Bill Pemberton34c43922009-03-16 22:05:14 -0400106static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
David Schleefed9eccb2008-11-04 20:29:31 -0800107
108static int comedi_fasync(int fd, struct file *file, int on);
109
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400110static int is_device_busy(struct comedi_device *dev);
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400111static int resize_async_buffer(struct comedi_device *dev,
112 struct comedi_subdevice *s,
113 struct comedi_async *async, unsigned new_size);
114
115/* declarations for sysfs attribute files */
116static struct device_attribute dev_attr_max_read_buffer_kb;
117static struct device_attribute dev_attr_read_buffer_kb;
118static struct device_attribute dev_attr_max_write_buffer_kb;
119static struct device_attribute dev_attr_write_buffer_kb;
David Schleefed9eccb2008-11-04 20:29:31 -0800120
David Schleefed9eccb2008-11-04 20:29:31 -0800121static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800122 unsigned long arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800123{
124 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800125 struct comedi_device_file_info *dev_file_info =
126 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400127 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800128 int rc;
129
Frank Mori Hess53b670a2008-12-15 13:48:47 +0000130 if (dev_file_info == NULL || dev_file_info->device == NULL)
131 return -ENODEV;
132 dev = dev_file_info->device;
133
David Schleefed9eccb2008-11-04 20:29:31 -0800134 mutex_lock(&dev->mutex);
135
136 /* Device config is special, because it must work on
137 * an unconfigured device. */
138 if (cmd == COMEDI_DEVCONFIG) {
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700139 rc = do_devconfig_ioctl(dev,
140 (struct comedi_devconfig __user *)arg);
David Schleefed9eccb2008-11-04 20:29:31 -0800141 goto done;
142 }
143
144 if (!dev->attached) {
145 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
146 rc = -ENODEV;
147 goto done;
148 }
149
150 switch (cmd) {
151 case COMEDI_BUFCONFIG:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700152 rc = do_bufconfig_ioctl(dev,
153 (struct comedi_bufconfig __user *)arg);
David Schleefed9eccb2008-11-04 20:29:31 -0800154 break;
155 case COMEDI_DEVINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700156 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
157 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800158 break;
159 case COMEDI_SUBDINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700160 rc = do_subdinfo_ioctl(dev,
161 (struct comedi_subdinfo __user *)arg,
162 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800163 break;
164 case COMEDI_CHANINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700165 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
David Schleefed9eccb2008-11-04 20:29:31 -0800166 break;
167 case COMEDI_RANGEINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700168 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
David Schleefed9eccb2008-11-04 20:29:31 -0800169 break;
170 case COMEDI_BUFINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700171 rc = do_bufinfo_ioctl(dev,
Ian Abbott53fa8272010-05-19 18:09:50 +0100172 (struct comedi_bufinfo __user *)arg,
173 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800174 break;
175 case COMEDI_LOCK:
176 rc = do_lock_ioctl(dev, arg, file);
177 break;
178 case COMEDI_UNLOCK:
179 rc = do_unlock_ioctl(dev, arg, file);
180 break;
181 case COMEDI_CANCEL:
182 rc = do_cancel_ioctl(dev, arg, file);
183 break;
184 case COMEDI_CMD:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700185 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
David Schleefed9eccb2008-11-04 20:29:31 -0800186 break;
187 case COMEDI_CMDTEST:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700188 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
189 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800190 break;
191 case COMEDI_INSNLIST:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700192 rc = do_insnlist_ioctl(dev,
193 (struct comedi_insnlist __user *)arg,
194 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800195 break;
196 case COMEDI_INSN:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700197 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
198 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800199 break;
200 case COMEDI_POLL:
201 rc = do_poll_ioctl(dev, arg, file);
202 break;
203 default:
204 rc = -ENOTTY;
205 break;
206 }
207
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800208done:
David Schleefed9eccb2008-11-04 20:29:31 -0800209 mutex_unlock(&dev->mutex);
210 return rc;
211}
212
213/*
214 COMEDI_DEVCONFIG
215 device config ioctl
216
217 arg:
218 pointer to devconfig structure
219
220 reads:
221 devconfig structure at arg
222
223 writes:
224 none
225*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530226static int do_devconfig_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700227 struct comedi_devconfig __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800228{
Bill Pemberton0707bb02009-03-16 22:06:20 -0400229 struct comedi_devconfig it;
David Schleefed9eccb2008-11-04 20:29:31 -0800230 int ret;
231 unsigned char *aux_data = NULL;
232 int aux_len;
233
234 if (!capable(CAP_SYS_ADMIN))
235 return -EPERM;
236
237 if (arg == NULL) {
238 if (is_device_busy(dev))
239 return -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800240 if (dev->attached) {
David Schleefed9eccb2008-11-04 20:29:31 -0800241 struct module *driver_module = dev->driver->module;
242 comedi_device_detach(dev);
243 module_put(driver_module);
244 }
245 return 0;
246 }
247
Bill Pemberton0707bb02009-03-16 22:06:20 -0400248 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800249 return -EFAULT;
250
251 it.board_name[COMEDI_NAMELEN - 1] = 0;
252
253 if (comedi_aux_data(it.options, 0) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800254 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
David Schleefed9eccb2008-11-04 20:29:31 -0800255 int bit_shift;
256 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
257 if (aux_len < 0)
258 return -EFAULT;
259
260 aux_data = vmalloc(aux_len);
261 if (!aux_data)
262 return -ENOMEM;
263
264 if (copy_from_user(aux_data,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800265 comedi_aux_data(it.options, 0), aux_len)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800266 vfree(aux_data);
267 return -EFAULT;
268 }
269 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800270 (unsigned long)aux_data;
David Schleefed9eccb2008-11-04 20:29:31 -0800271 if (sizeof(void *) > sizeof(int)) {
272 bit_shift = sizeof(int) * 8;
273 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800274 ((unsigned long)aux_data) >> bit_shift;
David Schleefed9eccb2008-11-04 20:29:31 -0800275 } else
276 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
277 }
278
279 ret = comedi_device_attach(dev, &it);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800280 if (ret == 0) {
281 if (!try_module_get(dev->driver->module)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800282 comedi_device_detach(dev);
283 return -ENOSYS;
284 }
285 }
286
287 if (aux_data)
288 vfree(aux_data);
289
290 return ret;
291}
292
293/*
294 COMEDI_BUFCONFIG
295 buffer configuration ioctl
296
297 arg:
298 pointer to bufconfig structure
299
300 reads:
301 bufconfig at arg
302
303 writes:
304 modified bufconfig at arg
305
306*/
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700307static int do_bufconfig_ioctl(struct comedi_device *dev,
308 struct comedi_bufconfig __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800309{
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400310 struct comedi_bufconfig bc;
Bill Pembertond1636792009-03-16 22:05:20 -0400311 struct comedi_async *async;
Bill Pemberton34c43922009-03-16 22:05:14 -0400312 struct comedi_subdevice *s;
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400313 int retval = 0;
David Schleefed9eccb2008-11-04 20:29:31 -0800314
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400315 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800316 return -EFAULT;
317
318 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
319 return -EINVAL;
320
321 s = dev->subdevices + bc.subdevice;
322 async = s->async;
323
324 if (!async) {
325 DPRINTK("subdevice does not have async capability\n");
326 bc.size = 0;
327 bc.maximum_size = 0;
328 goto copyback;
329 }
330
331 if (bc.maximum_size) {
332 if (!capable(CAP_SYS_ADMIN))
333 return -EPERM;
334
335 async->max_bufsize = bc.maximum_size;
336 }
337
338 if (bc.size) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400339 retval = resize_async_buffer(dev, s, async, bc.size);
340 if (retval < 0)
341 return retval;
David Schleefed9eccb2008-11-04 20:29:31 -0800342 }
343
344 bc.size = async->prealloc_bufsz;
345 bc.maximum_size = async->max_bufsize;
346
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800347copyback:
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400348 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800349 return -EFAULT;
350
351 return 0;
352}
353
354/*
355 COMEDI_DEVINFO
356 device info ioctl
357
358 arg:
359 pointer to devinfo structure
360
361 reads:
362 none
363
364 writes:
365 devinfo structure
366
367*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530368static int do_devinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700369 struct comedi_devinfo __user *arg,
370 struct file *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800371{
Bill Pemberton063db042009-03-16 22:06:15 -0400372 struct comedi_devinfo devinfo;
David Schleefed9eccb2008-11-04 20:29:31 -0800373 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800374 struct comedi_device_file_info *dev_file_info =
375 comedi_get_device_file_info(minor);
Bill Pemberton34c43922009-03-16 22:05:14 -0400376 struct comedi_subdevice *read_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800377 comedi_get_read_subdevice(dev_file_info);
Bill Pemberton34c43922009-03-16 22:05:14 -0400378 struct comedi_subdevice *write_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800379 comedi_get_write_subdevice(dev_file_info);
David Schleefed9eccb2008-11-04 20:29:31 -0800380
381 memset(&devinfo, 0, sizeof(devinfo));
382
383 /* fill devinfo structure */
384 devinfo.version_code = COMEDI_VERSION_CODE;
385 devinfo.n_subdevs = dev->n_subdevices;
386 memcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
387 memcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
388
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800389 if (read_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800390 devinfo.read_subdevice = read_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800391 else
David Schleefed9eccb2008-11-04 20:29:31 -0800392 devinfo.read_subdevice = -1;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800393
394 if (write_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800395 devinfo.write_subdevice = write_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800396 else
David Schleefed9eccb2008-11-04 20:29:31 -0800397 devinfo.write_subdevice = -1;
David Schleefed9eccb2008-11-04 20:29:31 -0800398
Bill Pemberton063db042009-03-16 22:06:15 -0400399 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800400 return -EFAULT;
401
402 return 0;
403}
404
405/*
406 COMEDI_SUBDINFO
407 subdevice info ioctl
408
409 arg:
410 pointer to array of subdevice info structures
411
412 reads:
413 none
414
415 writes:
416 array of subdevice info structures at arg
417
418*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530419static int do_subdinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700420 struct comedi_subdinfo __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800421{
422 int ret, i;
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400423 struct comedi_subdinfo *tmp, *us;
Bill Pemberton34c43922009-03-16 22:05:14 -0400424 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800425
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530426 tmp =
427 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
428 GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800429 if (!tmp)
430 return -ENOMEM;
431
432 /* fill subdinfo structs */
433 for (i = 0; i < dev->n_subdevices; i++) {
434 s = dev->subdevices + i;
435 us = tmp + i;
436
437 us->type = s->type;
438 us->n_chan = s->n_chan;
439 us->subd_flags = s->subdev_flags;
440 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
441 us->subd_flags |= SDF_RUNNING;
442#define TIMER_nanosec 5 /* backwards compatibility */
443 us->timer_type = TIMER_nanosec;
444 us->len_chanlist = s->len_chanlist;
445 us->maxdata = s->maxdata;
446 if (s->range_table) {
447 us->range_type =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800448 (i << 24) | (0 << 16) | (s->range_table->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800449 } else {
450 us->range_type = 0; /* XXX */
451 }
452 us->flags = s->flags;
453
454 if (s->busy)
455 us->subd_flags |= SDF_BUSY;
456 if (s->busy == file)
457 us->subd_flags |= SDF_BUSY_OWNER;
458 if (s->lock)
459 us->subd_flags |= SDF_LOCKED;
460 if (s->lock == file)
461 us->subd_flags |= SDF_LOCK_OWNER;
462 if (!s->maxdata && s->maxdata_list)
463 us->subd_flags |= SDF_MAXDATA;
464 if (s->flaglist)
465 us->subd_flags |= SDF_FLAGS;
466 if (s->range_table_list)
467 us->subd_flags |= SDF_RANGETYPE;
468 if (s->do_cmd)
469 us->subd_flags |= SDF_CMD;
470
471 if (s->insn_bits != &insn_inval)
472 us->insn_bits_support = COMEDI_SUPPORTED;
473 else
474 us->insn_bits_support = COMEDI_UNSUPPORTED;
475
476 us->settling_time_0 = s->settling_time_0;
477 }
478
479 ret = copy_to_user(arg, tmp,
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400480 dev->n_subdevices * sizeof(struct comedi_subdinfo));
David Schleefed9eccb2008-11-04 20:29:31 -0800481
482 kfree(tmp);
483
484 return ret ? -EFAULT : 0;
485}
486
487/*
488 COMEDI_CHANINFO
489 subdevice info ioctl
490
491 arg:
492 pointer to chaninfo structure
493
494 reads:
495 chaninfo structure at arg
496
497 writes:
498 arrays at elements of chaninfo structure
499
500*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530501static int do_chaninfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700502 struct comedi_chaninfo __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800503{
Bill Pemberton34c43922009-03-16 22:05:14 -0400504 struct comedi_subdevice *s;
Bill Pembertona18b4162009-03-16 22:06:04 -0400505 struct comedi_chaninfo it;
David Schleefed9eccb2008-11-04 20:29:31 -0800506
Bill Pembertona18b4162009-03-16 22:06:04 -0400507 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800508 return -EFAULT;
509
510 if (it.subdev >= dev->n_subdevices)
511 return -EINVAL;
512 s = dev->subdevices + it.subdev;
513
514 if (it.maxdata_list) {
515 if (s->maxdata || !s->maxdata_list)
516 return -EINVAL;
517 if (copy_to_user(it.maxdata_list, s->maxdata_list,
Bill Pemberton790c5542009-03-16 22:05:02 -0400518 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800519 return -EFAULT;
520 }
521
522 if (it.flaglist) {
523 if (!s->flaglist)
524 return -EINVAL;
525 if (copy_to_user(it.flaglist, s->flaglist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800526 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800527 return -EFAULT;
528 }
529
530 if (it.rangelist) {
531 int i;
532
533 if (!s->range_table_list)
534 return -EINVAL;
535 for (i = 0; i < s->n_chan; i++) {
536 int x;
537
538 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800539 (s->range_table_list[i]->length);
Vasiliy Kulikov81604d42010-09-05 22:32:33 +0400540 if (put_user(x, it.rangelist + i))
541 return -EFAULT;
David Schleefed9eccb2008-11-04 20:29:31 -0800542 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800543#if 0
544 if (copy_to_user(it.rangelist, s->range_type_list,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530545 s->n_chan * sizeof(unsigned int)))
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800546 return -EFAULT;
547#endif
David Schleefed9eccb2008-11-04 20:29:31 -0800548 }
549
550 return 0;
551}
552
553 /*
554 COMEDI_BUFINFO
555 buffer information ioctl
556
557 arg:
558 pointer to bufinfo structure
559
560 reads:
561 bufinfo at arg
562
563 writes:
564 modified bufinfo at arg
565
566 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700567static int do_bufinfo_ioctl(struct comedi_device *dev,
Ian Abbott53fa8272010-05-19 18:09:50 +0100568 struct comedi_bufinfo __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800569{
Bill Pemberton9aa53392009-03-16 22:06:42 -0400570 struct comedi_bufinfo bi;
Bill Pemberton34c43922009-03-16 22:05:14 -0400571 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400572 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800573
Bill Pemberton9aa53392009-03-16 22:06:42 -0400574 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800575 return -EFAULT;
576
577 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
578 return -EINVAL;
579
580 s = dev->subdevices + bi.subdevice;
Ian Abbott53fa8272010-05-19 18:09:50 +0100581
582 if (s->lock && s->lock != file)
583 return -EACCES;
584
David Schleefed9eccb2008-11-04 20:29:31 -0800585 async = s->async;
586
587 if (!async) {
588 DPRINTK("subdevice does not have async capability\n");
589 bi.buf_write_ptr = 0;
590 bi.buf_read_ptr = 0;
591 bi.buf_write_count = 0;
592 bi.buf_read_count = 0;
Ian Abbott4772c012010-05-19 18:09:49 +0100593 bi.bytes_read = 0;
594 bi.bytes_written = 0;
David Schleefed9eccb2008-11-04 20:29:31 -0800595 goto copyback;
596 }
Ian Abbott53fa8272010-05-19 18:09:50 +0100597 if (!s->busy) {
598 bi.bytes_read = 0;
599 bi.bytes_written = 0;
600 goto copyback_position;
601 }
602 if (s->busy != file)
603 return -EACCES;
David Schleefed9eccb2008-11-04 20:29:31 -0800604
605 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
606 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
607 comedi_buf_read_free(async, bi.bytes_read);
608
609 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800610 SRF_RUNNING))
611 && async->buf_write_count == async->buf_read_count) {
David Schleefed9eccb2008-11-04 20:29:31 -0800612 do_become_nonbusy(dev, s);
613 }
614 }
615
616 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
617 bi.bytes_written =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800618 comedi_buf_write_alloc(async, bi.bytes_written);
David Schleefed9eccb2008-11-04 20:29:31 -0800619 comedi_buf_write_free(async, bi.bytes_written);
620 }
621
Ian Abbott53fa8272010-05-19 18:09:50 +0100622copyback_position:
David Schleefed9eccb2008-11-04 20:29:31 -0800623 bi.buf_write_count = async->buf_write_count;
624 bi.buf_write_ptr = async->buf_write_ptr;
625 bi.buf_read_count = async->buf_read_count;
626 bi.buf_read_ptr = async->buf_read_ptr;
627
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800628copyback:
Bill Pemberton9aa53392009-03-16 22:06:42 -0400629 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800630 return -EFAULT;
631
632 return 0;
633}
634
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530635static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
636 unsigned int *data, void *file);
David Schleefed9eccb2008-11-04 20:29:31 -0800637/*
Pieter De Praetere20617f22010-03-10 09:47:44 +0100638 * COMEDI_INSNLIST
639 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -0800640 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100641 * arg:
642 * pointer to sync cmd structure
David Schleefed9eccb2008-11-04 20:29:31 -0800643 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100644 * reads:
645 * sync cmd struct at arg
646 * instruction list
647 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -0800648 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100649 * writes:
650 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -0800651 */
652/* arbitrary limits */
653#define MAX_SAMPLES 256
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700654static int do_insnlist_ioctl(struct comedi_device *dev,
655 struct comedi_insnlist __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800656{
Bill Pembertonda613f42009-03-16 22:05:59 -0400657 struct comedi_insnlist insnlist;
Bill Pemberton90035c02009-03-16 22:05:53 -0400658 struct comedi_insn *insns = NULL;
Bill Pemberton790c5542009-03-16 22:05:02 -0400659 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800660 int i = 0;
661 int ret = 0;
662
Bill Pembertonda613f42009-03-16 22:05:59 -0400663 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
David Schleefed9eccb2008-11-04 20:29:31 -0800664 return -EFAULT;
665
Bill Pemberton790c5542009-03-16 22:05:02 -0400666 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800667 if (!data) {
668 DPRINTK("kmalloc failed\n");
669 ret = -ENOMEM;
670 goto error;
671 }
672
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530673 insns =
674 kmalloc(sizeof(struct comedi_insn) * insnlist.n_insns, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800675 if (!insns) {
676 DPRINTK("kmalloc failed\n");
677 ret = -ENOMEM;
678 goto error;
679 }
680
681 if (copy_from_user(insns, insnlist.insns,
Bill Pemberton90035c02009-03-16 22:05:53 -0400682 sizeof(struct comedi_insn) * insnlist.n_insns)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800683 DPRINTK("copy_from_user failed\n");
684 ret = -EFAULT;
685 goto error;
686 }
687
688 for (i = 0; i < insnlist.n_insns; i++) {
689 if (insns[i].n > MAX_SAMPLES) {
690 DPRINTK("number of samples too large\n");
691 ret = -EINVAL;
692 goto error;
693 }
694 if (insns[i].insn & INSN_MASK_WRITE) {
695 if (copy_from_user(data, insns[i].data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400696 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800697 DPRINTK("copy_from_user failed\n");
698 ret = -EFAULT;
699 goto error;
700 }
701 }
702 ret = parse_insn(dev, insns + i, data, file);
703 if (ret < 0)
704 goto error;
705 if (insns[i].insn & INSN_MASK_READ) {
706 if (copy_to_user(insns[i].data, data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400707 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800708 DPRINTK("copy_to_user failed\n");
709 ret = -EFAULT;
710 goto error;
711 }
712 }
713 if (need_resched())
714 schedule();
715 }
716
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800717error:
718 kfree(insns);
719 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800720
721 if (ret < 0)
722 return ret;
723 return i;
724}
725
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530726static int check_insn_config_length(struct comedi_insn *insn,
727 unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -0800728{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800729 if (insn->n < 1)
730 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -0800731
732 switch (data[0]) {
733 case INSN_CONFIG_DIO_OUTPUT:
734 case INSN_CONFIG_DIO_INPUT:
735 case INSN_CONFIG_DISARM:
736 case INSN_CONFIG_RESET:
737 if (insn->n == 1)
738 return 0;
739 break;
740 case INSN_CONFIG_ARM:
741 case INSN_CONFIG_DIO_QUERY:
742 case INSN_CONFIG_BLOCK_SIZE:
743 case INSN_CONFIG_FILTER:
744 case INSN_CONFIG_SERIAL_CLOCK:
745 case INSN_CONFIG_BIDIRECTIONAL_DATA:
746 case INSN_CONFIG_ALT_SOURCE:
747 case INSN_CONFIG_SET_COUNTER_MODE:
748 case INSN_CONFIG_8254_READ_STATUS:
749 case INSN_CONFIG_SET_ROUTING:
750 case INSN_CONFIG_GET_ROUTING:
751 case INSN_CONFIG_GET_PWM_STATUS:
752 case INSN_CONFIG_PWM_SET_PERIOD:
753 case INSN_CONFIG_PWM_GET_PERIOD:
754 if (insn->n == 2)
755 return 0;
756 break;
757 case INSN_CONFIG_SET_GATE_SRC:
758 case INSN_CONFIG_GET_GATE_SRC:
759 case INSN_CONFIG_SET_CLOCK_SRC:
760 case INSN_CONFIG_GET_CLOCK_SRC:
761 case INSN_CONFIG_SET_OTHER_SRC:
762 case INSN_CONFIG_GET_COUNTER_STATUS:
763 case INSN_CONFIG_PWM_SET_H_BRIDGE:
764 case INSN_CONFIG_PWM_GET_H_BRIDGE:
765 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
766 if (insn->n == 3)
767 return 0;
768 break;
769 case INSN_CONFIG_PWM_OUTPUT:
770 case INSN_CONFIG_ANALOG_TRIG:
771 if (insn->n == 5)
772 return 0;
773 break;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530774 /* by default we allow the insn since we don't have checks for
775 * all possible cases yet */
David Schleefed9eccb2008-11-04 20:29:31 -0800776 default:
Mark Rankilor3fffdf22010-04-29 18:17:16 +0800777 printk(KERN_WARNING
778 "comedi: no check for data length of config insn id "
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530779 "%i is implemented.\n"
780 " Add a check to %s in %s.\n"
781 " Assuming n=%i is correct.\n", data[0], __func__,
782 __FILE__, insn->n);
David Schleefed9eccb2008-11-04 20:29:31 -0800783 return 0;
784 break;
785 }
786 return -EINVAL;
787}
788
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530789static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
790 unsigned int *data, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800791{
Bill Pemberton34c43922009-03-16 22:05:14 -0400792 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800793 int ret = 0;
794 int i;
795
796 if (insn->insn & INSN_MASK_SPECIAL) {
797 /* a non-subdevice instruction */
798
799 switch (insn->insn) {
800 case INSN_GTOD:
801 {
802 struct timeval tv;
803
804 if (insn->n != 2) {
805 ret = -EINVAL;
806 break;
807 }
808
809 do_gettimeofday(&tv);
810 data[0] = tv.tv_sec;
811 data[1] = tv.tv_usec;
812 ret = 2;
813
814 break;
815 }
816 case INSN_WAIT:
817 if (insn->n != 1 || data[0] >= 100000) {
818 ret = -EINVAL;
819 break;
820 }
821 udelay(data[0] / 1000);
822 ret = 1;
823 break;
824 case INSN_INTTRIG:
825 if (insn->n != 1) {
826 ret = -EINVAL;
827 break;
828 }
829 if (insn->subdev >= dev->n_subdevices) {
830 DPRINTK("%d not usable subdevice\n",
831 insn->subdev);
832 ret = -EINVAL;
833 break;
834 }
835 s = dev->subdevices + insn->subdev;
836 if (!s->async) {
837 DPRINTK("no async\n");
838 ret = -EINVAL;
839 break;
840 }
841 if (!s->async->inttrig) {
842 DPRINTK("no inttrig\n");
843 ret = -EAGAIN;
844 break;
845 }
846 ret = s->async->inttrig(dev, s, insn->data[0]);
847 if (ret >= 0)
848 ret = 1;
849 break;
850 default:
851 DPRINTK("invalid insn\n");
852 ret = -EINVAL;
853 break;
854 }
855 } else {
856 /* a subdevice instruction */
Bill Pemberton790c5542009-03-16 22:05:02 -0400857 unsigned int maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800858
859 if (insn->subdev >= dev->n_subdevices) {
860 DPRINTK("subdevice %d out of range\n", insn->subdev);
861 ret = -EINVAL;
862 goto out;
863 }
864 s = dev->subdevices + insn->subdev;
865
866 if (s->type == COMEDI_SUBD_UNUSED) {
867 DPRINTK("%d not usable subdevice\n", insn->subdev);
868 ret = -EIO;
869 goto out;
870 }
871
872 /* are we locked? (ioctl lock) */
873 if (s->lock && s->lock != file) {
874 DPRINTK("device locked\n");
875 ret = -EACCES;
876 goto out;
877 }
878
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -0700879 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800880 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -0800881 ret = -EINVAL;
882 DPRINTK("bad chanspec\n");
883 goto out;
884 }
885
886 if (s->busy) {
887 ret = -EBUSY;
888 goto out;
889 }
890 /* This looks arbitrary. It is. */
891 s->busy = &parse_insn;
892 switch (insn->insn) {
893 case INSN_READ:
894 ret = s->insn_read(dev, s, insn, data);
895 break;
896 case INSN_WRITE:
897 maxdata = s->maxdata_list
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800898 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
899 : s->maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -0800900 for (i = 0; i < insn->n; ++i) {
901 if (data[i] > maxdata) {
902 ret = -EINVAL;
903 DPRINTK("bad data value(s)\n");
904 break;
905 }
906 }
907 if (ret == 0)
908 ret = s->insn_write(dev, s, insn, data);
909 break;
910 case INSN_BITS:
911 if (insn->n != 2) {
912 ret = -EINVAL;
913 break;
914 }
915 ret = s->insn_bits(dev, s, insn, data);
916 break;
917 case INSN_CONFIG:
918 ret = check_insn_config_length(insn, data);
919 if (ret)
920 break;
921 ret = s->insn_config(dev, s, insn, data);
922 break;
923 default:
924 ret = -EINVAL;
925 break;
926 }
927
928 s->busy = NULL;
929 }
930
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800931out:
David Schleefed9eccb2008-11-04 20:29:31 -0800932 return ret;
933}
934
935/*
Pieter De Praetere20617f22010-03-10 09:47:44 +0100936 * COMEDI_INSN
937 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -0800938 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100939 * arg:
940 * pointer to insn
David Schleefed9eccb2008-11-04 20:29:31 -0800941 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100942 * reads:
943 * struct comedi_insn struct at arg
944 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -0800945 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100946 * writes:
947 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -0800948 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700949static int do_insn_ioctl(struct comedi_device *dev,
950 struct comedi_insn __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800951{
Bill Pemberton90035c02009-03-16 22:05:53 -0400952 struct comedi_insn insn;
Bill Pemberton790c5542009-03-16 22:05:02 -0400953 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800954 int ret = 0;
955
Bill Pemberton790c5542009-03-16 22:05:02 -0400956 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800957 if (!data) {
958 ret = -ENOMEM;
959 goto error;
960 }
961
Bill Pemberton90035c02009-03-16 22:05:53 -0400962 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800963 ret = -EFAULT;
964 goto error;
965 }
966
967 /* This is where the behavior of insn and insnlist deviate. */
968 if (insn.n > MAX_SAMPLES)
969 insn.n = MAX_SAMPLES;
970 if (insn.insn & INSN_MASK_WRITE) {
Mark21fe2ee2010-05-13 17:44:39 +0800971 if (copy_from_user(data,
972 insn.data,
973 insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800974 ret = -EFAULT;
975 goto error;
976 }
977 }
978 ret = parse_insn(dev, &insn, data, file);
979 if (ret < 0)
980 goto error;
981 if (insn.insn & INSN_MASK_READ) {
Mark21fe2ee2010-05-13 17:44:39 +0800982 if (copy_to_user(insn.data,
983 data,
984 insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800985 ret = -EFAULT;
986 goto error;
987 }
988 }
989 ret = insn.n;
990
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800991error:
992 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -0800993
994 return ret;
995}
996
Greg Kroah-Hartman181bd672010-05-03 15:15:06 -0700997static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
998 unsigned mask, unsigned bits)
999{
1000 unsigned long flags;
1001
1002 spin_lock_irqsave(&s->spin_lock, flags);
1003 s->runflags &= ~mask;
1004 s->runflags |= (bits & mask);
1005 spin_unlock_irqrestore(&s->spin_lock, flags);
1006}
1007
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001008static int do_cmd_ioctl(struct comedi_device *dev,
1009 struct comedi_cmd __user *cmd, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001010{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001011 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001012 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001013 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001014 int ret = 0;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001015 unsigned int __user *chanlist_saver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001016
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001017 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001018 DPRINTK("bad cmd address\n");
1019 return -EFAULT;
1020 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001021 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001022 chanlist_saver = user_cmd.chanlist;
1023
1024 if (user_cmd.subdev >= dev->n_subdevices) {
1025 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1026 return -ENODEV;
1027 }
1028
1029 s = dev->subdevices + user_cmd.subdev;
1030 async = s->async;
1031
1032 if (s->type == COMEDI_SUBD_UNUSED) {
1033 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1034 return -EIO;
1035 }
1036
1037 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1038 DPRINTK("subdevice %i does not support commands\n",
1039 user_cmd.subdev);
1040 return -EIO;
1041 }
1042
1043 /* are we locked? (ioctl lock) */
1044 if (s->lock && s->lock != file) {
1045 DPRINTK("subdevice locked\n");
1046 return -EACCES;
1047 }
1048
1049 /* are we busy? */
1050 if (s->busy) {
1051 DPRINTK("subdevice busy\n");
1052 return -EBUSY;
1053 }
1054 s->busy = file;
1055
1056 /* make sure channel/gain list isn't too long */
1057 if (user_cmd.chanlist_len > s->len_chanlist) {
1058 DPRINTK("channel/gain list too long %u > %d\n",
1059 user_cmd.chanlist_len, s->len_chanlist);
1060 ret = -EINVAL;
1061 goto cleanup;
1062 }
1063
1064 /* make sure channel/gain list isn't too short */
1065 if (user_cmd.chanlist_len < 1) {
1066 DPRINTK("channel/gain list too short %u < 1\n",
1067 user_cmd.chanlist_len);
1068 ret = -EINVAL;
1069 goto cleanup;
1070 }
1071
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001072 kfree(async->cmd.chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001073 async->cmd = user_cmd;
1074 async->cmd.data = NULL;
1075 /* load channel/gain list */
1076 async->cmd.chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001077 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001078 if (!async->cmd.chanlist) {
1079 DPRINTK("allocation failed\n");
1080 ret = -ENOMEM;
1081 goto cleanup;
1082 }
1083
1084 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001085 async->cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001086 DPRINTK("fault reading chanlist\n");
1087 ret = -EFAULT;
1088 goto cleanup;
1089 }
1090
1091 /* make sure each element in channel/gain list is valid */
Mark21fe2ee2010-05-13 17:44:39 +08001092 ret = comedi_check_chanlist(s,
1093 async->cmd.chanlist_len,
1094 async->cmd.chanlist);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001095 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001096 DPRINTK("bad chanlist\n");
1097 goto cleanup;
1098 }
1099
1100 ret = s->do_cmdtest(dev, s, &async->cmd);
1101
1102 if (async->cmd.flags & TRIG_BOGUS || ret) {
1103 DPRINTK("test returned %d\n", ret);
1104 user_cmd = async->cmd;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001105 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001106 user_cmd.chanlist = chanlist_saver;
1107 user_cmd.data = NULL;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001108 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001109 DPRINTK("fault writing cmd\n");
1110 ret = -EFAULT;
1111 goto cleanup;
1112 }
1113 ret = -EAGAIN;
1114 goto cleanup;
1115 }
1116
1117 if (!async->prealloc_bufsz) {
1118 ret = -ENOMEM;
1119 DPRINTK("no buffer (?)\n");
1120 goto cleanup;
1121 }
1122
1123 comedi_reset_async_buf(async);
1124
1125 async->cb_mask =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001126 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1127 COMEDI_CB_OVERFLOW;
1128 if (async->cmd.flags & TRIG_WAKE_EOS)
David Schleefed9eccb2008-11-04 20:29:31 -08001129 async->cb_mask |= COMEDI_CB_EOS;
David Schleefed9eccb2008-11-04 20:29:31 -08001130
1131 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1132
David Schleefed9eccb2008-11-04 20:29:31 -08001133 ret = s->do_cmd(dev, s);
1134 if (ret == 0)
1135 return 0;
1136
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001137cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -08001138 do_become_nonbusy(dev, s);
1139
1140 return ret;
1141}
1142
1143/*
1144 COMEDI_CMDTEST
1145 command testing ioctl
1146
1147 arg:
1148 pointer to cmd structure
1149
1150 reads:
1151 cmd structure at arg
1152 channel/range list
1153
1154 writes:
1155 modified cmd structure at arg
1156
1157*/
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001158static int do_cmdtest_ioctl(struct comedi_device *dev,
1159 struct comedi_cmd __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001160{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001161 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001162 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001163 int ret = 0;
1164 unsigned int *chanlist = NULL;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001165 unsigned int __user *chanlist_saver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001166
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001167 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001168 DPRINTK("bad cmd address\n");
1169 return -EFAULT;
1170 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001171 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001172 chanlist_saver = user_cmd.chanlist;
1173
1174 if (user_cmd.subdev >= dev->n_subdevices) {
1175 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1176 return -ENODEV;
1177 }
1178
1179 s = dev->subdevices + user_cmd.subdev;
1180 if (s->type == COMEDI_SUBD_UNUSED) {
1181 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1182 return -EIO;
1183 }
1184
1185 if (!s->do_cmd || !s->do_cmdtest) {
1186 DPRINTK("subdevice %i does not support commands\n",
1187 user_cmd.subdev);
1188 return -EIO;
1189 }
1190
1191 /* make sure channel/gain list isn't too long */
1192 if (user_cmd.chanlist_len > s->len_chanlist) {
1193 DPRINTK("channel/gain list too long %d > %d\n",
1194 user_cmd.chanlist_len, s->len_chanlist);
1195 ret = -EINVAL;
1196 goto cleanup;
1197 }
1198
1199 /* load channel/gain list */
1200 if (user_cmd.chanlist) {
1201 chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001202 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001203 if (!chanlist) {
1204 DPRINTK("allocation failed\n");
1205 ret = -ENOMEM;
1206 goto cleanup;
1207 }
1208
1209 if (copy_from_user(chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001210 user_cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001211 DPRINTK("fault reading chanlist\n");
1212 ret = -EFAULT;
1213 goto cleanup;
1214 }
1215
1216 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -07001217 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001218 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001219 DPRINTK("bad chanlist\n");
1220 goto cleanup;
1221 }
1222
1223 user_cmd.chanlist = chanlist;
1224 }
1225
1226 ret = s->do_cmdtest(dev, s, &user_cmd);
1227
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001228 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001229 user_cmd.chanlist = chanlist_saver;
1230
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001231 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001232 DPRINTK("bad cmd address\n");
1233 ret = -EFAULT;
1234 goto cleanup;
1235 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001236cleanup:
1237 kfree(chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001238
1239 return ret;
1240}
1241
1242/*
1243 COMEDI_LOCK
1244 lock subdevice
1245
1246 arg:
1247 subdevice number
1248
1249 reads:
1250 none
1251
1252 writes:
1253 none
1254
1255*/
1256
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301257static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1258 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001259{
1260 int ret = 0;
1261 unsigned long flags;
Bill Pemberton34c43922009-03-16 22:05:14 -04001262 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001263
1264 if (arg >= dev->n_subdevices)
1265 return -EINVAL;
1266 s = dev->subdevices + arg;
1267
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001268 spin_lock_irqsave(&s->spin_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001269 if (s->busy || s->lock)
David Schleefed9eccb2008-11-04 20:29:31 -08001270 ret = -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001271 else
David Schleefed9eccb2008-11-04 20:29:31 -08001272 s->lock = file;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001273 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08001274
1275 if (ret < 0)
1276 return ret;
1277
1278#if 0
1279 if (s->lock_f)
1280 ret = s->lock_f(dev, s);
1281#endif
1282
1283 return ret;
1284}
1285
1286/*
1287 COMEDI_UNLOCK
1288 unlock subdevice
1289
1290 arg:
1291 subdevice number
1292
1293 reads:
1294 none
1295
1296 writes:
1297 none
1298
1299 This function isn't protected by the semaphore, since
1300 we already own the lock.
1301*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301302static int do_unlock_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
1311 if (s->busy)
1312 return -EBUSY;
1313
1314 if (s->lock && s->lock != file)
1315 return -EACCES;
1316
1317 if (s->lock == file) {
1318#if 0
1319 if (s->unlock)
1320 s->unlock(dev, s);
1321#endif
1322
1323 s->lock = NULL;
1324 }
1325
1326 return 0;
1327}
1328
1329/*
1330 COMEDI_CANCEL
1331 cancel acquisition ioctl
1332
1333 arg:
1334 subdevice number
1335
1336 reads:
1337 nothing
1338
1339 writes:
1340 nothing
1341
1342*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301343static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1344 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001345{
Bill Pemberton34c43922009-03-16 22:05:14 -04001346 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001347
1348 if (arg >= dev->n_subdevices)
1349 return -EINVAL;
1350 s = dev->subdevices + arg;
1351 if (s->async == NULL)
1352 return -EINVAL;
1353
1354 if (s->lock && s->lock != file)
1355 return -EACCES;
1356
1357 if (!s->busy)
1358 return 0;
1359
1360 if (s->busy != file)
1361 return -EBUSY;
1362
1363 return do_cancel(dev, s);
1364}
1365
1366/*
1367 COMEDI_POLL ioctl
1368 instructs driver to synchronize buffers
1369
1370 arg:
1371 subdevice number
1372
1373 reads:
1374 nothing
1375
1376 writes:
1377 nothing
1378
1379*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301380static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1381 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001382{
Bill Pemberton34c43922009-03-16 22:05:14 -04001383 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001384
1385 if (arg >= dev->n_subdevices)
1386 return -EINVAL;
1387 s = dev->subdevices + arg;
1388
1389 if (s->lock && s->lock != file)
1390 return -EACCES;
1391
1392 if (!s->busy)
1393 return 0;
1394
1395 if (s->busy != file)
1396 return -EBUSY;
1397
1398 if (s->poll)
1399 return s->poll(dev, s);
1400
1401 return -EINVAL;
1402}
1403
Bill Pemberton34c43922009-03-16 22:05:14 -04001404static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001405{
1406 int ret = 0;
1407
1408 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1409 ret = s->cancel(dev, s);
1410
1411 do_become_nonbusy(dev, s);
1412
1413 return ret;
1414}
1415
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001416static void comedi_unmap(struct vm_area_struct *area)
David Schleefed9eccb2008-11-04 20:29:31 -08001417{
Bill Pembertond1636792009-03-16 22:05:20 -04001418 struct comedi_async *async;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001419 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -08001420
1421 async = area->vm_private_data;
1422 dev = async->subdevice->device;
1423
1424 mutex_lock(&dev->mutex);
1425 async->mmap_count--;
1426 mutex_unlock(&dev->mutex);
1427}
1428
1429static struct vm_operations_struct comedi_vm_ops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301430 .close = comedi_unmap,
David Schleefed9eccb2008-11-04 20:29:31 -08001431};
1432
1433static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1434{
1435 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001436 struct comedi_device_file_info *dev_file_info =
1437 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001438 struct comedi_device *dev = dev_file_info->device;
Bill Pembertond1636792009-03-16 22:05:20 -04001439 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001440 unsigned long start = vma->vm_start;
1441 unsigned long size;
1442 int n_pages;
1443 int i;
1444 int retval;
Bill Pemberton34c43922009-03-16 22:05:14 -04001445 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001446
1447 mutex_lock(&dev->mutex);
1448 if (!dev->attached) {
1449 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1450 retval = -ENODEV;
1451 goto done;
1452 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001453 if (vma->vm_flags & VM_WRITE)
David Schleefed9eccb2008-11-04 20:29:31 -08001454 s = comedi_get_write_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001455 else
David Schleefed9eccb2008-11-04 20:29:31 -08001456 s = comedi_get_read_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001457
David Schleefed9eccb2008-11-04 20:29:31 -08001458 if (s == NULL) {
1459 retval = -EINVAL;
1460 goto done;
1461 }
1462 async = s->async;
1463 if (async == NULL) {
1464 retval = -EINVAL;
1465 goto done;
1466 }
1467
1468 if (vma->vm_pgoff != 0) {
1469 DPRINTK("comedi: mmap() offset must be 0.\n");
1470 retval = -EINVAL;
1471 goto done;
1472 }
1473
1474 size = vma->vm_end - vma->vm_start;
1475 if (size > async->prealloc_bufsz) {
1476 retval = -EFAULT;
1477 goto done;
1478 }
1479 if (size & (~PAGE_MASK)) {
1480 retval = -EFAULT;
1481 goto done;
1482 }
1483
1484 n_pages = size >> PAGE_SHIFT;
1485 for (i = 0; i < n_pages; ++i) {
1486 if (remap_pfn_range(vma, start,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301487 page_to_pfn(virt_to_page
1488 (async->buf_page_list
1489 [i].virt_addr)), PAGE_SIZE,
1490 PAGE_SHARED)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001491 retval = -EAGAIN;
1492 goto done;
1493 }
1494 start += PAGE_SIZE;
1495 }
1496
1497 vma->vm_ops = &comedi_vm_ops;
1498 vma->vm_private_data = async;
1499
1500 async->mmap_count++;
1501
1502 retval = 0;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001503done:
David Schleefed9eccb2008-11-04 20:29:31 -08001504 mutex_unlock(&dev->mutex);
1505 return retval;
1506}
1507
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301508static unsigned int comedi_poll(struct file *file, poll_table * wait)
David Schleefed9eccb2008-11-04 20:29:31 -08001509{
1510 unsigned int mask = 0;
1511 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001512 struct comedi_device_file_info *dev_file_info =
1513 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001514 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001515 struct comedi_subdevice *read_subdev;
1516 struct comedi_subdevice *write_subdev;
David Schleefed9eccb2008-11-04 20:29:31 -08001517
1518 mutex_lock(&dev->mutex);
1519 if (!dev->attached) {
1520 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1521 mutex_unlock(&dev->mutex);
1522 return 0;
1523 }
1524
1525 mask = 0;
1526 read_subdev = comedi_get_read_subdevice(dev_file_info);
1527 if (read_subdev) {
1528 poll_wait(file, &read_subdev->async->wait_head, wait);
1529 if (!read_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001530 || comedi_buf_read_n_available(read_subdev->async) > 0
1531 || !(comedi_get_subdevice_runflags(read_subdev) &
1532 SRF_RUNNING)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001533 mask |= POLLIN | POLLRDNORM;
1534 }
1535 }
1536 write_subdev = comedi_get_write_subdevice(dev_file_info);
1537 if (write_subdev) {
1538 poll_wait(file, &write_subdev->async->wait_head, wait);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001539 comedi_buf_write_alloc(write_subdev->async,
1540 write_subdev->async->prealloc_bufsz);
David Schleefed9eccb2008-11-04 20:29:31 -08001541 if (!write_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001542 || !(comedi_get_subdevice_runflags(write_subdev) &
1543 SRF_RUNNING)
1544 || comedi_buf_write_n_allocated(write_subdev->async) >=
1545 bytes_per_sample(write_subdev->async->subdevice)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001546 mask |= POLLOUT | POLLWRNORM;
1547 }
1548 }
1549
1550 mutex_unlock(&dev->mutex);
1551 return mask;
1552}
1553
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001554static ssize_t comedi_write(struct file *file, const char __user *buf,
1555 size_t nbytes, loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001556{
Bill Pemberton34c43922009-03-16 22:05:14 -04001557 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001558 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001559 int n, m, count = 0, retval = 0;
1560 DECLARE_WAITQUEUE(wait, current);
1561 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001562 struct comedi_device_file_info *dev_file_info =
1563 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001564 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001565
1566 if (!dev->attached) {
1567 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1568 retval = -ENODEV;
1569 goto done;
1570 }
1571
1572 s = comedi_get_write_subdevice(dev_file_info);
1573 if (s == NULL) {
1574 retval = -EIO;
1575 goto done;
1576 }
1577 async = s->async;
1578
1579 if (!nbytes) {
1580 retval = 0;
1581 goto done;
1582 }
1583 if (!s->busy) {
1584 retval = 0;
1585 goto done;
1586 }
1587 if (s->busy != file) {
1588 retval = -EACCES;
1589 goto done;
1590 }
1591 add_wait_queue(&async->wait_head, &wait);
1592 while (nbytes > 0 && !retval) {
1593 set_current_state(TASK_INTERRUPTIBLE);
1594
Ian Abbottd2611542010-05-19 17:22:41 +01001595 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1596 if (count == 0) {
1597 if (comedi_get_subdevice_runflags(s) &
1598 SRF_ERROR) {
1599 retval = -EPIPE;
1600 } else {
1601 retval = 0;
1602 }
1603 do_become_nonbusy(dev, s);
1604 }
1605 break;
1606 }
1607
David Schleefed9eccb2008-11-04 20:29:31 -08001608 n = nbytes;
1609
1610 m = n;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001611 if (async->buf_write_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001612 m = async->prealloc_bufsz - async->buf_write_ptr;
David Schleefed9eccb2008-11-04 20:29:31 -08001613 comedi_buf_write_alloc(async, async->prealloc_bufsz);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001614 if (m > comedi_buf_write_n_allocated(async))
David Schleefed9eccb2008-11-04 20:29:31 -08001615 m = comedi_buf_write_n_allocated(async);
David Schleefed9eccb2008-11-04 20:29:31 -08001616 if (m < n)
1617 n = m;
1618
1619 if (n == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001620 if (file->f_flags & O_NONBLOCK) {
1621 retval = -EAGAIN;
1622 break;
1623 }
1624 if (signal_pending(current)) {
1625 retval = -ERESTARTSYS;
1626 break;
1627 }
1628 schedule();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001629 if (!s->busy)
David Schleefed9eccb2008-11-04 20:29:31 -08001630 break;
David Schleefed9eccb2008-11-04 20:29:31 -08001631 if (s->busy != file) {
1632 retval = -EACCES;
1633 break;
1634 }
1635 continue;
1636 }
1637
1638 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001639 buf, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001640 if (m) {
1641 n -= m;
1642 retval = -EFAULT;
1643 }
1644 comedi_buf_write_free(async, n);
1645
1646 count += n;
1647 nbytes -= n;
1648
1649 buf += n;
1650 break; /* makes device work like a pipe */
1651 }
1652 set_current_state(TASK_RUNNING);
1653 remove_wait_queue(&async->wait_head, &wait);
1654
1655done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001656 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001657}
1658
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001659static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01001660 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001661{
Bill Pemberton34c43922009-03-16 22:05:14 -04001662 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001663 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001664 int n, m, count = 0, retval = 0;
1665 DECLARE_WAITQUEUE(wait, current);
1666 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001667 struct comedi_device_file_info *dev_file_info =
1668 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001669 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001670
1671 if (!dev->attached) {
1672 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1673 retval = -ENODEV;
1674 goto done;
1675 }
1676
1677 s = comedi_get_read_subdevice(dev_file_info);
1678 if (s == NULL) {
1679 retval = -EIO;
1680 goto done;
1681 }
1682 async = s->async;
1683 if (!nbytes) {
1684 retval = 0;
1685 goto done;
1686 }
1687 if (!s->busy) {
1688 retval = 0;
1689 goto done;
1690 }
1691 if (s->busy != file) {
1692 retval = -EACCES;
1693 goto done;
1694 }
1695
1696 add_wait_queue(&async->wait_head, &wait);
1697 while (nbytes > 0 && !retval) {
1698 set_current_state(TASK_INTERRUPTIBLE);
1699
1700 n = nbytes;
1701
1702 m = comedi_buf_read_n_available(async);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001703 /* printk("%d available\n",m); */
1704 if (async->buf_read_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001705 m = async->prealloc_bufsz - async->buf_read_ptr;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001706 /* printk("%d contiguous\n",m); */
David Schleefed9eccb2008-11-04 20:29:31 -08001707 if (m < n)
1708 n = m;
1709
1710 if (n == 0) {
1711 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1712 do_become_nonbusy(dev, s);
1713 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001714 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001715 retval = -EPIPE;
1716 } else {
1717 retval = 0;
1718 }
1719 break;
1720 }
1721 if (file->f_flags & O_NONBLOCK) {
1722 retval = -EAGAIN;
1723 break;
1724 }
1725 if (signal_pending(current)) {
1726 retval = -ERESTARTSYS;
1727 break;
1728 }
1729 schedule();
1730 if (!s->busy) {
1731 retval = 0;
1732 break;
1733 }
1734 if (s->busy != file) {
1735 retval = -EACCES;
1736 break;
1737 }
1738 continue;
1739 }
1740 m = copy_to_user(buf, async->prealloc_buf +
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001741 async->buf_read_ptr, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001742 if (m) {
1743 n -= m;
1744 retval = -EFAULT;
1745 }
1746
1747 comedi_buf_read_alloc(async, n);
1748 comedi_buf_read_free(async, n);
1749
1750 count += n;
1751 nbytes -= n;
1752
1753 buf += n;
1754 break; /* makes device work like a pipe */
1755 }
1756 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001757 async->buf_read_count - async->buf_write_count == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001758 do_become_nonbusy(dev, s);
1759 }
1760 set_current_state(TASK_RUNNING);
1761 remove_wait_queue(&async->wait_head, &wait);
1762
1763done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001764 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001765}
1766
1767/*
1768 This function restores a subdevice to an idle state.
1769 */
Bill Pemberton34c43922009-03-16 22:05:14 -04001770void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001771{
Bill Pembertond1636792009-03-16 22:05:20 -04001772 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001773
1774 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001775 if (async) {
1776 comedi_reset_async_buf(async);
1777 async->inttrig = NULL;
1778 } else {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001779 printk(KERN_ERR
1780 "BUG: (?) do_become_nonbusy called with async=0\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001781 }
1782
1783 s->busy = NULL;
1784}
1785
1786static int comedi_open(struct inode *inode, struct file *file)
1787{
David Schleefed9eccb2008-11-04 20:29:31 -08001788 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001789 struct comedi_device_file_info *dev_file_info =
1790 comedi_get_device_file_info(minor);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301791 struct comedi_device *dev =
1792 dev_file_info ? dev_file_info->device : NULL;
Ian Abbott97920072009-02-09 16:51:38 +00001793
David Schleefed9eccb2008-11-04 20:29:31 -08001794 if (dev == NULL) {
1795 DPRINTK("invalid minor number\n");
1796 return -ENODEV;
1797 }
1798
1799 /* This is slightly hacky, but we want module autoloading
1800 * to work for root.
1801 * case: user opens device, attached -> ok
1802 * case: user opens device, unattached, in_request_module=0 -> autoload
1803 * case: user opens device, unattached, in_request_module=1 -> fail
1804 * case: root opens device, attached -> ok
1805 * case: root opens device, unattached, in_request_module=1 -> ok
1806 * (typically called from modprobe)
1807 * case: root opens device, unattached, in_request_module=0 -> autoload
1808 *
1809 * The last could be changed to "-> ok", which would deny root
1810 * autoloading.
1811 */
1812 mutex_lock(&dev->mutex);
1813 if (dev->attached)
1814 goto ok;
Eric Parisa8f80e82009-08-13 09:44:51 -04001815 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
David Schleefed9eccb2008-11-04 20:29:31 -08001816 DPRINTK("in request module\n");
1817 mutex_unlock(&dev->mutex);
1818 return -ENODEV;
1819 }
Eric Parisa8f80e82009-08-13 09:44:51 -04001820 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
David Schleefed9eccb2008-11-04 20:29:31 -08001821 goto ok;
1822
1823 dev->in_request_module = 1;
1824
David Schleefed9eccb2008-11-04 20:29:31 -08001825#ifdef CONFIG_KMOD
1826 mutex_unlock(&dev->mutex);
Ian Abbott56d92c62009-02-09 16:32:12 +00001827 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
David Schleefed9eccb2008-11-04 20:29:31 -08001828 mutex_lock(&dev->mutex);
1829#endif
1830
1831 dev->in_request_module = 0;
1832
Eric Parisa8f80e82009-08-13 09:44:51 -04001833 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1834 DPRINTK("not attached and not CAP_NET_ADMIN\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001835 mutex_unlock(&dev->mutex);
1836 return -ENODEV;
1837 }
1838ok:
1839 __module_get(THIS_MODULE);
1840
1841 if (dev->attached) {
1842 if (!try_module_get(dev->driver->module)) {
1843 module_put(THIS_MODULE);
1844 mutex_unlock(&dev->mutex);
1845 return -ENOSYS;
1846 }
1847 }
1848
Ian Abbott3c17ba072010-05-19 14:10:00 +01001849 if (dev->attached && dev->use_count == 0 && dev->open) {
1850 int rc = dev->open(dev);
1851 if (rc < 0) {
1852 module_put(dev->driver->module);
1853 module_put(THIS_MODULE);
1854 mutex_unlock(&dev->mutex);
1855 return rc;
1856 }
1857 }
David Schleefed9eccb2008-11-04 20:29:31 -08001858
1859 dev->use_count++;
1860
1861 mutex_unlock(&dev->mutex);
1862
1863 return 0;
1864}
1865
1866static int comedi_close(struct inode *inode, struct file *file)
1867{
1868 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001869 struct comedi_device_file_info *dev_file_info =
1870 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001871 struct comedi_device *dev = dev_file_info->device;
Bill Pemberton34c43922009-03-16 22:05:14 -04001872 struct comedi_subdevice *s = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001873 int i;
1874
1875 mutex_lock(&dev->mutex);
1876
1877 if (dev->subdevices) {
1878 for (i = 0; i < dev->n_subdevices; i++) {
1879 s = dev->subdevices + i;
1880
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001881 if (s->busy == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001882 do_cancel(dev, s);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001883 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001884 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001885 }
1886 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001887 if (dev->attached && dev->use_count == 1 && dev->close)
David Schleefed9eccb2008-11-04 20:29:31 -08001888 dev->close(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001889
1890 module_put(THIS_MODULE);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001891 if (dev->attached)
David Schleefed9eccb2008-11-04 20:29:31 -08001892 module_put(dev->driver->module);
David Schleefed9eccb2008-11-04 20:29:31 -08001893
1894 dev->use_count--;
1895
1896 mutex_unlock(&dev->mutex);
1897
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001898 if (file->f_flags & FASYNC)
David Schleefed9eccb2008-11-04 20:29:31 -08001899 comedi_fasync(-1, file, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001900
1901 return 0;
1902}
1903
1904static int comedi_fasync(int fd, struct file *file, int on)
1905{
1906 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001907 struct comedi_device_file_info *dev_file_info =
1908 comedi_get_device_file_info(minor);
1909
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001910 struct comedi_device *dev = dev_file_info->device;
David Schleefed9eccb2008-11-04 20:29:31 -08001911
1912 return fasync_helper(fd, file, on, &dev->async_queue);
1913}
1914
1915const struct file_operations comedi_fops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301916 .owner = THIS_MODULE,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301917 .unlocked_ioctl = comedi_unlocked_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301918 .compat_ioctl = comedi_compat_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301919 .open = comedi_open,
1920 .release = comedi_close,
1921 .read = comedi_read,
1922 .write = comedi_write,
1923 .mmap = comedi_mmap,
1924 .poll = comedi_poll,
1925 .fasync = comedi_fasync,
David Schleefed9eccb2008-11-04 20:29:31 -08001926};
1927
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001928struct class *comedi_class;
David Schleefed9eccb2008-11-04 20:29:31 -08001929static struct cdev comedi_cdev;
1930
1931static void comedi_cleanup_legacy_minors(void)
1932{
1933 unsigned i;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001934
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001935 for (i = 0; i < comedi_num_legacy_minors; i++)
David Schleefed9eccb2008-11-04 20:29:31 -08001936 comedi_free_board_minor(i);
David Schleefed9eccb2008-11-04 20:29:31 -08001937}
1938
1939static int __init comedi_init(void)
1940{
1941 int i;
1942 int retval;
1943
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001944 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1945 " - http://www.comedi.org\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001946
Frank Mori Hessa3cb7292008-12-15 13:44:45 +00001947 if (comedi_num_legacy_minors < 0 ||
1948 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
1949 printk(KERN_ERR "comedi: error: invalid value for module "
1950 "parameter \"comedi_num_legacy_minors\". Valid values "
1951 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
1952 return -EINVAL;
1953 }
1954
1955 /*
1956 * comedi is unusable if both comedi_autoconfig and
1957 * comedi_num_legacy_minors are zero, so we might as well adjust the
1958 * defaults in that case
1959 */
1960 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
1961 comedi_num_legacy_minors = 16;
1962
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001963 memset(comedi_file_info_table, 0,
1964 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001965
1966 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001967 COMEDI_NUM_MINORS, "comedi");
David Schleefed9eccb2008-11-04 20:29:31 -08001968 if (retval)
1969 return -EIO;
1970 cdev_init(&comedi_cdev, &comedi_fops);
1971 comedi_cdev.owner = THIS_MODULE;
1972 kobject_set_name(&comedi_cdev.kobj, "comedi");
1973 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1974 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001975 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001976 return -EIO;
1977 }
1978 comedi_class = class_create(THIS_MODULE, "comedi");
1979 if (IS_ERR(comedi_class)) {
Mark Rankilor3fffdf22010-04-29 18:17:16 +08001980 printk(KERN_ERR "comedi: failed to create class");
David Schleefed9eccb2008-11-04 20:29:31 -08001981 cdev_del(&comedi_cdev);
1982 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001983 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001984 return PTR_ERR(comedi_class);
1985 }
1986
1987 /* XXX requires /proc interface */
1988 comedi_proc_init();
1989
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001990 /* create devices files for legacy/manual use */
Bernd Porr1dd33ab2008-12-08 23:30:13 +00001991 for (i = 0; i < comedi_num_legacy_minors; i++) {
David Schleefed9eccb2008-11-04 20:29:31 -08001992 int minor;
1993 minor = comedi_alloc_board_minor(NULL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001994 if (minor < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001995 comedi_cleanup_legacy_minors();
1996 cdev_del(&comedi_cdev);
1997 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001998 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08001999 return minor;
2000 }
2001 }
2002
David Schleefed9eccb2008-11-04 20:29:31 -08002003 return 0;
2004}
2005
2006static void __exit comedi_cleanup(void)
2007{
2008 int i;
2009
2010 comedi_cleanup_legacy_minors();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002011 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
David Schleefed9eccb2008-11-04 20:29:31 -08002012 BUG_ON(comedi_file_info_table[i]);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002013
David Schleefed9eccb2008-11-04 20:29:31 -08002014 class_destroy(comedi_class);
2015 cdev_del(&comedi_cdev);
2016 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2017
2018 comedi_proc_cleanup();
David Schleefed9eccb2008-11-04 20:29:31 -08002019}
2020
2021module_init(comedi_init);
2022module_exit(comedi_cleanup);
2023
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002024void comedi_error(const struct comedi_device *dev, const char *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002025{
Mark Rankilor3fffdf22010-04-29 18:17:16 +08002026 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2027 dev->driver->driver_name, s);
David Schleefed9eccb2008-11-04 20:29:31 -08002028}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002029EXPORT_SYMBOL(comedi_error);
David Schleefed9eccb2008-11-04 20:29:31 -08002030
Bill Pemberton34c43922009-03-16 22:05:14 -04002031void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002032{
Bill Pembertond1636792009-03-16 22:05:20 -04002033 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08002034 unsigned runflags = 0;
2035 unsigned runflags_mask = 0;
2036
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002037 /* DPRINTK("comedi_event 0x%x\n",mask); */
David Schleefed9eccb2008-11-04 20:29:31 -08002038
2039 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2040 return;
2041
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302042 if (s->
2043 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2044 COMEDI_CB_OVERFLOW)) {
David Schleefed9eccb2008-11-04 20:29:31 -08002045 runflags_mask |= SRF_RUNNING;
2046 }
2047 /* remember if an error event has occured, so an error
2048 * can be returned the next time the user does a read() */
2049 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2050 runflags_mask |= SRF_ERROR;
2051 runflags |= SRF_ERROR;
2052 }
2053 if (runflags_mask) {
2054 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2055 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2056 }
2057
2058 if (async->cb_mask & s->async->events) {
2059 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
Greg Kroah-Hartmanfcea1152009-04-27 15:15:30 -07002060 wake_up_interruptible(&async->wait_head);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002061 if (s->subdev_flags & SDF_CMD_READ)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302062 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002063 if (s->subdev_flags & SDF_CMD_WRITE)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302064 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
David Schleefed9eccb2008-11-04 20:29:31 -08002065 } else {
2066 if (async->cb_func)
2067 async->cb_func(s->async->events, async->cb_arg);
David Schleefed9eccb2008-11-04 20:29:31 -08002068 }
2069 }
2070 s->async->events = 0;
2071}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002072EXPORT_SYMBOL(comedi_event);
David Schleefed9eccb2008-11-04 20:29:31 -08002073
Bill Pemberton34c43922009-03-16 22:05:14 -04002074unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002075{
2076 unsigned long flags;
2077 unsigned runflags;
2078
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002079 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002080 runflags = s->runflags;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002081 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002082 return runflags;
2083}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002084EXPORT_SYMBOL(comedi_get_subdevice_runflags);
David Schleefed9eccb2008-11-04 20:29:31 -08002085
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002086static int is_device_busy(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002087{
Bill Pemberton34c43922009-03-16 22:05:14 -04002088 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08002089 int i;
2090
2091 if (!dev->attached)
2092 return 0;
2093
2094 for (i = 0; i < dev->n_subdevices; i++) {
2095 s = dev->subdevices + i;
2096 if (s->busy)
2097 return 1;
2098 if (s->async && s->async->mmap_count)
2099 return 1;
2100 }
2101
2102 return 0;
2103}
2104
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002105static void comedi_device_init(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002106{
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002107 memset(dev, 0, sizeof(struct comedi_device));
David Schleefed9eccb2008-11-04 20:29:31 -08002108 spin_lock_init(&dev->spinlock);
2109 mutex_init(&dev->mutex);
2110 dev->minor = -1;
2111}
2112
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002113static void comedi_device_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002114{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002115 if (dev == NULL)
2116 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002117 mutex_lock(&dev->mutex);
2118 comedi_device_detach(dev);
2119 mutex_unlock(&dev->mutex);
2120 mutex_destroy(&dev->mutex);
2121}
2122
2123int comedi_alloc_board_minor(struct device *hardware_device)
2124{
2125 unsigned long flags;
2126 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002127 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002128 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002129 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002130
2131 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002132 if (info == NULL)
2133 return -ENOMEM;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002134 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002135 if (info->device == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002136 kfree(info);
2137 return -ENOMEM;
2138 }
2139 comedi_device_init(info->device);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002140 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002141 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2142 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002143 comedi_file_info_table[i] = info;
2144 break;
2145 }
2146 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002147 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002148 if (i == COMEDI_NUM_BOARD_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002149 comedi_device_cleanup(info->device);
2150 kfree(info->device);
2151 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302152 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002153 "comedi: error: "
2154 "ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002155 return -EBUSY;
2156 }
2157 info->device->minor = i;
2158 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002159 MKDEV(COMEDI_MAJOR, i), NULL,
2160 hardware_device, "comedi%i", i);
2161 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002162 info->device->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002163 dev_set_drvdata(csdev, info);
2164 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2165 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302166 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002167 "comedi: "
2168 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302169 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002170 comedi_free_board_minor(i);
2171 return retval;
2172 }
2173 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2174 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302175 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002176 "comedi: "
2177 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302178 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002179 comedi_free_board_minor(i);
2180 return retval;
2181 }
2182 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2183 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302184 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002185 "comedi: "
2186 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302187 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002188 comedi_free_board_minor(i);
2189 return retval;
2190 }
2191 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2192 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302193 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002194 "comedi: "
2195 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302196 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002197 comedi_free_board_minor(i);
2198 return retval;
2199 }
David Schleefed9eccb2008-11-04 20:29:31 -08002200 return i;
2201}
2202
2203void comedi_free_board_minor(unsigned minor)
2204{
2205 unsigned long flags;
2206 struct comedi_device_file_info *info;
2207
2208 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002209 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002210 info = comedi_file_info_table[minor];
2211 comedi_file_info_table[minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002212 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002213
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002214 if (info) {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002215 struct comedi_device *dev = info->device;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002216 if (dev) {
2217 if (dev->class_dev) {
2218 device_destroy(comedi_class,
2219 MKDEV(COMEDI_MAJOR, dev->minor));
David Schleefed9eccb2008-11-04 20:29:31 -08002220 }
2221 comedi_device_cleanup(dev);
2222 kfree(dev);
2223 }
2224 kfree(info);
2225 }
2226}
2227
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002228int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2229 struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002230{
2231 unsigned long flags;
2232 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002233 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002234 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002235 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002236
2237 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002238 if (info == NULL)
2239 return -ENOMEM;
David Schleefed9eccb2008-11-04 20:29:31 -08002240 info->device = dev;
2241 info->read_subdevice = s;
2242 info->write_subdevice = s;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002243 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Frank Mori Hess4c41f3a2008-12-09 14:47:22 +00002244 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002245 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002246 comedi_file_info_table[i] = info;
2247 break;
2248 }
2249 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002250 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002251 if (i == COMEDI_NUM_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002252 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302253 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002254 "comedi: error: "
2255 "ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002256 return -EBUSY;
2257 }
2258 s->minor = i;
2259 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002260 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2261 "comedi%i_subd%i", dev->minor,
2262 (int)(s - dev->subdevices));
2263 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002264 s->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002265 dev_set_drvdata(csdev, info);
2266 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2267 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302268 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002269 "comedi: "
2270 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302271 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002272 comedi_free_subdevice_minor(s);
2273 return retval;
2274 }
2275 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2276 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302277 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002278 "comedi: "
2279 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302280 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002281 comedi_free_subdevice_minor(s);
2282 return retval;
2283 }
2284 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2285 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302286 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002287 "comedi: "
2288 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302289 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002290 comedi_free_subdevice_minor(s);
2291 return retval;
2292 }
2293 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2294 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302295 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002296 "comedi: "
2297 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302298 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002299 comedi_free_subdevice_minor(s);
2300 return retval;
2301 }
David Schleefed9eccb2008-11-04 20:29:31 -08002302 return i;
2303}
2304
Bill Pemberton34c43922009-03-16 22:05:14 -04002305void comedi_free_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002306{
2307 unsigned long flags;
2308 struct comedi_device_file_info *info;
2309
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002310 if (s == NULL)
2311 return;
2312 if (s->minor < 0)
2313 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002314
2315 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2316 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2317
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002318 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002319 info = comedi_file_info_table[s->minor];
2320 comedi_file_info_table[s->minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002321 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002322
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002323 if (s->class_dev) {
David Schleefed9eccb2008-11-04 20:29:31 -08002324 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2325 s->class_dev = NULL;
2326 }
2327 kfree(info);
2328}
2329
2330struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2331{
2332 unsigned long flags;
2333 struct comedi_device_file_info *info;
2334
2335 BUG_ON(minor >= COMEDI_NUM_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002336 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002337 info = comedi_file_info_table[minor];
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002338 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002339 return info;
2340}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002341EXPORT_SYMBOL_GPL(comedi_get_device_file_info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002342
2343static int resize_async_buffer(struct comedi_device *dev,
2344 struct comedi_subdevice *s,
2345 struct comedi_async *async, unsigned new_size)
2346{
2347 int retval;
2348
2349 if (new_size > async->max_bufsize)
2350 return -EPERM;
2351
2352 if (s->busy) {
2353 DPRINTK("subdevice is busy, cannot resize buffer\n");
2354 return -EBUSY;
2355 }
2356 if (async->mmap_count) {
2357 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2358 return -EBUSY;
2359 }
2360
2361 if (!async->prealloc_buf)
2362 return -EINVAL;
2363
2364 /* make sure buffer is an integral number of pages
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302365 * (we round up) */
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002366 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2367
2368 retval = comedi_buf_alloc(dev, s, new_size);
2369 if (retval < 0)
2370 return retval;
2371
2372 if (s->buf_change) {
2373 retval = s->buf_change(dev, s, new_size);
2374 if (retval < 0)
2375 return retval;
2376 }
2377
2378 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
Ian Abbottb8b5cd92009-09-21 14:55:23 -04002379 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002380 return 0;
2381}
2382
2383/* sysfs attribute files */
2384
2385static const unsigned bytes_per_kibi = 1024;
2386
2387static ssize_t show_max_read_buffer_kb(struct device *dev,
2388 struct device_attribute *attr, char *buf)
2389{
2390 ssize_t retval;
2391 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2392 unsigned max_buffer_size_kb = 0;
2393 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302394 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002395
2396 mutex_lock(&info->device->mutex);
2397 if (read_subdevice &&
2398 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2399 read_subdevice->async) {
2400 max_buffer_size_kb = read_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302401 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002402 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302403 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002404 mutex_unlock(&info->device->mutex);
2405
2406 return retval;
2407}
2408
2409static ssize_t store_max_read_buffer_kb(struct device *dev,
2410 struct device_attribute *attr,
2411 const char *buf, size_t count)
2412{
2413 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2414 unsigned long new_max_size_kb;
2415 uint64_t new_max_size;
2416 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302417 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002418
2419 if (strict_strtoul(buf, 10, &new_max_size_kb))
2420 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302421 if (new_max_size_kb != (uint32_t) new_max_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002422 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302423 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2424 if (new_max_size != (uint32_t) new_max_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002425 return -EINVAL;
2426
2427 mutex_lock(&info->device->mutex);
2428 if (read_subdevice == NULL ||
2429 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2430 read_subdevice->async == NULL) {
2431 mutex_unlock(&info->device->mutex);
2432 return -EINVAL;
2433 }
2434 read_subdevice->async->max_bufsize = new_max_size;
2435 mutex_unlock(&info->device->mutex);
2436
2437 return count;
2438}
2439
2440static struct device_attribute dev_attr_max_read_buffer_kb = {
2441 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302442 .name = "max_read_buffer_kb",
2443 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002444 .show = &show_max_read_buffer_kb,
2445 .store = &store_max_read_buffer_kb
2446};
2447
2448static ssize_t show_read_buffer_kb(struct device *dev,
2449 struct device_attribute *attr, char *buf)
2450{
2451 ssize_t retval;
2452 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2453 unsigned buffer_size_kb = 0;
2454 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302455 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002456
2457 mutex_lock(&info->device->mutex);
2458 if (read_subdevice &&
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302459 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2460 read_subdevice->async) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002461 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302462 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002463 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302464 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002465 mutex_unlock(&info->device->mutex);
2466
2467 return retval;
2468}
2469
2470static ssize_t store_read_buffer_kb(struct device *dev,
2471 struct device_attribute *attr,
2472 const char *buf, size_t count)
2473{
2474 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2475 unsigned long new_size_kb;
2476 uint64_t new_size;
2477 int retval;
2478 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302479 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002480
2481 if (strict_strtoul(buf, 10, &new_size_kb))
2482 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302483 if (new_size_kb != (uint32_t) new_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002484 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302485 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2486 if (new_size != (uint32_t) new_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002487 return -EINVAL;
2488
2489 mutex_lock(&info->device->mutex);
2490 if (read_subdevice == NULL ||
2491 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2492 read_subdevice->async == NULL) {
2493 mutex_unlock(&info->device->mutex);
2494 return -EINVAL;
2495 }
2496 retval = resize_async_buffer(info->device, read_subdevice,
2497 read_subdevice->async, new_size);
2498 mutex_unlock(&info->device->mutex);
2499
2500 if (retval < 0)
2501 return retval;
2502 return count;
2503}
2504
2505static struct device_attribute dev_attr_read_buffer_kb = {
2506 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302507 .name = "read_buffer_kb",
2508 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002509 .show = &show_read_buffer_kb,
2510 .store = &store_read_buffer_kb
2511};
2512
2513static ssize_t show_max_write_buffer_kb(struct device *dev,
2514 struct device_attribute *attr,
2515 char *buf)
2516{
2517 ssize_t retval;
2518 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2519 unsigned max_buffer_size_kb = 0;
2520 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302521 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002522
2523 mutex_lock(&info->device->mutex);
2524 if (write_subdevice &&
2525 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2526 write_subdevice->async) {
2527 max_buffer_size_kb = write_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302528 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002529 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302530 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002531 mutex_unlock(&info->device->mutex);
2532
2533 return retval;
2534}
2535
2536static ssize_t store_max_write_buffer_kb(struct device *dev,
2537 struct device_attribute *attr,
2538 const char *buf, size_t count)
2539{
2540 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2541 unsigned long new_max_size_kb;
2542 uint64_t new_max_size;
2543 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302544 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002545
2546 if (strict_strtoul(buf, 10, &new_max_size_kb))
2547 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302548 if (new_max_size_kb != (uint32_t) new_max_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002549 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302550 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2551 if (new_max_size != (uint32_t) new_max_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002552 return -EINVAL;
2553
2554 mutex_lock(&info->device->mutex);
2555 if (write_subdevice == NULL ||
2556 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2557 write_subdevice->async == NULL) {
2558 mutex_unlock(&info->device->mutex);
2559 return -EINVAL;
2560 }
2561 write_subdevice->async->max_bufsize = new_max_size;
2562 mutex_unlock(&info->device->mutex);
2563
2564 return count;
2565}
2566
2567static struct device_attribute dev_attr_max_write_buffer_kb = {
2568 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302569 .name = "max_write_buffer_kb",
2570 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002571 .show = &show_max_write_buffer_kb,
2572 .store = &store_max_write_buffer_kb
2573};
2574
2575static ssize_t show_write_buffer_kb(struct device *dev,
2576 struct device_attribute *attr, char *buf)
2577{
2578 ssize_t retval;
2579 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2580 unsigned buffer_size_kb = 0;
2581 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302582 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002583
2584 mutex_lock(&info->device->mutex);
2585 if (write_subdevice &&
2586 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2587 write_subdevice->async) {
2588 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302589 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002590 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302591 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002592 mutex_unlock(&info->device->mutex);
2593
2594 return retval;
2595}
2596
2597static ssize_t store_write_buffer_kb(struct device *dev,
2598 struct device_attribute *attr,
2599 const char *buf, size_t count)
2600{
2601 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2602 unsigned long new_size_kb;
2603 uint64_t new_size;
2604 int retval;
2605 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302606 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002607
2608 if (strict_strtoul(buf, 10, &new_size_kb))
2609 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302610 if (new_size_kb != (uint32_t) new_size_kb)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002611 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302612 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2613 if (new_size != (uint32_t) new_size)
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002614 return -EINVAL;
2615
2616 mutex_lock(&info->device->mutex);
2617 if (write_subdevice == NULL ||
2618 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2619 write_subdevice->async == NULL) {
2620 mutex_unlock(&info->device->mutex);
2621 return -EINVAL;
2622 }
2623 retval = resize_async_buffer(info->device, write_subdevice,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302624 write_subdevice->async, new_size);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002625 mutex_unlock(&info->device->mutex);
2626
2627 if (retval < 0)
2628 return retval;
2629 return count;
2630}
2631
2632static struct device_attribute dev_attr_write_buffer_kb = {
2633 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302634 .name = "write_buffer_kb",
2635 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002636 .show = &show_write_buffer_kb,
2637 .store = &store_write_buffer_kb
2638};