blob: b719460c4ecfe74fd126ae66c730d42d919e4bb0 [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
Rusty Russell90ab5ee2012-01-13 09:32:20 +103064bool comedi_autoconfig = 1;
Ian Abbott6a9d7a22008-12-08 17:05:50 +000065module_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
Mark Pearsonf5283a42011-07-10 21:16:38 +0200104static void do_become_nonbusy(struct comedi_device *dev,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530105 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);
Julia Lawall84d0e612012-04-22 13:37:09 +0200283 ret = -ENOSYS;
David Schleefed9eccb2008-11-04 20:29:31 -0800284 }
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;
Vasiliy Kulikov819cbb12011-06-26 12:56:22 +0400386 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
387 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
David Schleefed9eccb2008-11-04 20:29:31 -0800388
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 =
Xi Wangdfd8ee92011-11-25 16:46:51 -0500674 kcalloc(insnlist.n_insns, sizeof(struct comedi_insn), 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 }
Ian Abbott2b7f2092012-09-18 19:46:58 +0100846 ret = s->async->inttrig(dev, s, data[0]);
David Schleefed9eccb2008-11-04 20:29:31 -0800847 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;
Ian Abbott2f644cc2011-01-18 17:44:33 +0000913 } else {
914 /* Most drivers ignore the base channel in
915 * insn->chanspec. Fix this here if
916 * the subdevice has <= 32 channels. */
917 unsigned int shift;
918 unsigned int orig_mask;
919
920 orig_mask = data[0];
921 if (s->n_chan <= 32) {
922 shift = CR_CHAN(insn->chanspec);
923 if (shift > 0) {
924 insn->chanspec = 0;
925 data[0] <<= shift;
926 data[1] <<= shift;
927 }
928 } else
929 shift = 0;
930 ret = s->insn_bits(dev, s, insn, data);
931 data[0] = orig_mask;
932 if (shift > 0)
933 data[1] >>= shift;
David Schleefed9eccb2008-11-04 20:29:31 -0800934 }
David Schleefed9eccb2008-11-04 20:29:31 -0800935 break;
936 case INSN_CONFIG:
937 ret = check_insn_config_length(insn, data);
938 if (ret)
939 break;
940 ret = s->insn_config(dev, s, insn, data);
941 break;
942 default:
943 ret = -EINVAL;
944 break;
945 }
946
947 s->busy = NULL;
948 }
949
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800950out:
David Schleefed9eccb2008-11-04 20:29:31 -0800951 return ret;
952}
953
954/*
Pieter De Praetere20617f22010-03-10 09:47:44 +0100955 * COMEDI_INSN
956 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -0800957 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100958 * arg:
959 * pointer to insn
David Schleefed9eccb2008-11-04 20:29:31 -0800960 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100961 * reads:
962 * struct comedi_insn struct at arg
963 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -0800964 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100965 * writes:
966 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -0800967 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700968static int do_insn_ioctl(struct comedi_device *dev,
969 struct comedi_insn __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800970{
Bill Pemberton90035c02009-03-16 22:05:53 -0400971 struct comedi_insn insn;
Bill Pemberton790c5542009-03-16 22:05:02 -0400972 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800973 int ret = 0;
974
Bill Pemberton790c5542009-03-16 22:05:02 -0400975 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800976 if (!data) {
977 ret = -ENOMEM;
978 goto error;
979 }
980
Bill Pemberton90035c02009-03-16 22:05:53 -0400981 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800982 ret = -EFAULT;
983 goto error;
984 }
985
986 /* This is where the behavior of insn and insnlist deviate. */
987 if (insn.n > MAX_SAMPLES)
988 insn.n = MAX_SAMPLES;
989 if (insn.insn & INSN_MASK_WRITE) {
Mark21fe2ee2010-05-13 17:44:39 +0800990 if (copy_from_user(data,
991 insn.data,
992 insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800993 ret = -EFAULT;
994 goto error;
995 }
996 }
997 ret = parse_insn(dev, &insn, data, file);
998 if (ret < 0)
999 goto error;
1000 if (insn.insn & INSN_MASK_READ) {
Mark21fe2ee2010-05-13 17:44:39 +08001001 if (copy_to_user(insn.data,
1002 data,
1003 insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001004 ret = -EFAULT;
1005 goto error;
1006 }
1007 }
1008 ret = insn.n;
1009
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001010error:
1011 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -08001012
1013 return ret;
1014}
1015
Greg Kroah-Hartman181bd672010-05-03 15:15:06 -07001016static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
1017 unsigned mask, unsigned bits)
1018{
1019 unsigned long flags;
1020
1021 spin_lock_irqsave(&s->spin_lock, flags);
1022 s->runflags &= ~mask;
1023 s->runflags |= (bits & mask);
1024 spin_unlock_irqrestore(&s->spin_lock, flags);
1025}
1026
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001027static int do_cmd_ioctl(struct comedi_device *dev,
1028 struct comedi_cmd __user *cmd, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001029{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001030 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001031 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001032 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001033 int ret = 0;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001034 unsigned int __user *chanlist_saver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001035
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001036 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001037 DPRINTK("bad cmd address\n");
1038 return -EFAULT;
1039 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001040 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001041 chanlist_saver = user_cmd.chanlist;
1042
1043 if (user_cmd.subdev >= dev->n_subdevices) {
1044 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1045 return -ENODEV;
1046 }
1047
1048 s = dev->subdevices + user_cmd.subdev;
1049 async = s->async;
1050
1051 if (s->type == COMEDI_SUBD_UNUSED) {
1052 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1053 return -EIO;
1054 }
1055
1056 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1057 DPRINTK("subdevice %i does not support commands\n",
1058 user_cmd.subdev);
1059 return -EIO;
1060 }
1061
1062 /* are we locked? (ioctl lock) */
1063 if (s->lock && s->lock != file) {
1064 DPRINTK("subdevice locked\n");
1065 return -EACCES;
1066 }
1067
1068 /* are we busy? */
1069 if (s->busy) {
1070 DPRINTK("subdevice busy\n");
1071 return -EBUSY;
1072 }
1073 s->busy = file;
1074
1075 /* make sure channel/gain list isn't too long */
1076 if (user_cmd.chanlist_len > s->len_chanlist) {
1077 DPRINTK("channel/gain list too long %u > %d\n",
1078 user_cmd.chanlist_len, s->len_chanlist);
1079 ret = -EINVAL;
1080 goto cleanup;
1081 }
1082
1083 /* make sure channel/gain list isn't too short */
1084 if (user_cmd.chanlist_len < 1) {
1085 DPRINTK("channel/gain list too short %u < 1\n",
1086 user_cmd.chanlist_len);
1087 ret = -EINVAL;
1088 goto cleanup;
1089 }
1090
David Schleefed9eccb2008-11-04 20:29:31 -08001091 async->cmd = user_cmd;
1092 async->cmd.data = NULL;
1093 /* load channel/gain list */
1094 async->cmd.chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001095 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001096 if (!async->cmd.chanlist) {
1097 DPRINTK("allocation failed\n");
1098 ret = -ENOMEM;
1099 goto cleanup;
1100 }
1101
1102 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001103 async->cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001104 DPRINTK("fault reading chanlist\n");
1105 ret = -EFAULT;
1106 goto cleanup;
1107 }
1108
1109 /* make sure each element in channel/gain list is valid */
Mark21fe2ee2010-05-13 17:44:39 +08001110 ret = comedi_check_chanlist(s,
1111 async->cmd.chanlist_len,
1112 async->cmd.chanlist);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001113 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001114 DPRINTK("bad chanlist\n");
1115 goto cleanup;
1116 }
1117
1118 ret = s->do_cmdtest(dev, s, &async->cmd);
1119
1120 if (async->cmd.flags & TRIG_BOGUS || ret) {
1121 DPRINTK("test returned %d\n", ret);
1122 user_cmd = async->cmd;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001123 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001124 user_cmd.chanlist = chanlist_saver;
1125 user_cmd.data = NULL;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001126 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001127 DPRINTK("fault writing cmd\n");
1128 ret = -EFAULT;
1129 goto cleanup;
1130 }
1131 ret = -EAGAIN;
1132 goto cleanup;
1133 }
1134
1135 if (!async->prealloc_bufsz) {
1136 ret = -ENOMEM;
1137 DPRINTK("no buffer (?)\n");
1138 goto cleanup;
1139 }
1140
1141 comedi_reset_async_buf(async);
1142
1143 async->cb_mask =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001144 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1145 COMEDI_CB_OVERFLOW;
1146 if (async->cmd.flags & TRIG_WAKE_EOS)
David Schleefed9eccb2008-11-04 20:29:31 -08001147 async->cb_mask |= COMEDI_CB_EOS;
David Schleefed9eccb2008-11-04 20:29:31 -08001148
1149 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1150
David Schleefed9eccb2008-11-04 20:29:31 -08001151 ret = s->do_cmd(dev, s);
1152 if (ret == 0)
1153 return 0;
1154
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001155cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -08001156 do_become_nonbusy(dev, s);
1157
1158 return ret;
1159}
1160
1161/*
1162 COMEDI_CMDTEST
1163 command testing ioctl
1164
1165 arg:
1166 pointer to cmd structure
1167
1168 reads:
1169 cmd structure at arg
1170 channel/range list
1171
1172 writes:
1173 modified cmd structure at arg
1174
1175*/
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001176static int do_cmdtest_ioctl(struct comedi_device *dev,
1177 struct comedi_cmd __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001178{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001179 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001180 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001181 int ret = 0;
1182 unsigned int *chanlist = NULL;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001183 unsigned int __user *chanlist_saver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001184
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001185 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001186 DPRINTK("bad cmd address\n");
1187 return -EFAULT;
1188 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001189 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001190 chanlist_saver = user_cmd.chanlist;
1191
1192 if (user_cmd.subdev >= dev->n_subdevices) {
1193 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1194 return -ENODEV;
1195 }
1196
1197 s = dev->subdevices + user_cmd.subdev;
1198 if (s->type == COMEDI_SUBD_UNUSED) {
1199 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1200 return -EIO;
1201 }
1202
1203 if (!s->do_cmd || !s->do_cmdtest) {
1204 DPRINTK("subdevice %i does not support commands\n",
1205 user_cmd.subdev);
1206 return -EIO;
1207 }
1208
1209 /* make sure channel/gain list isn't too long */
1210 if (user_cmd.chanlist_len > s->len_chanlist) {
1211 DPRINTK("channel/gain list too long %d > %d\n",
1212 user_cmd.chanlist_len, s->len_chanlist);
1213 ret = -EINVAL;
1214 goto cleanup;
1215 }
1216
1217 /* load channel/gain list */
1218 if (user_cmd.chanlist) {
1219 chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001220 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001221 if (!chanlist) {
1222 DPRINTK("allocation failed\n");
1223 ret = -ENOMEM;
1224 goto cleanup;
1225 }
1226
1227 if (copy_from_user(chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001228 user_cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001229 DPRINTK("fault reading chanlist\n");
1230 ret = -EFAULT;
1231 goto cleanup;
1232 }
1233
1234 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -07001235 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001236 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001237 DPRINTK("bad chanlist\n");
1238 goto cleanup;
1239 }
1240
1241 user_cmd.chanlist = chanlist;
1242 }
1243
1244 ret = s->do_cmdtest(dev, s, &user_cmd);
1245
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001246 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001247 user_cmd.chanlist = chanlist_saver;
1248
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001249 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001250 DPRINTK("bad cmd address\n");
1251 ret = -EFAULT;
1252 goto cleanup;
1253 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001254cleanup:
1255 kfree(chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001256
1257 return ret;
1258}
1259
1260/*
1261 COMEDI_LOCK
1262 lock subdevice
1263
1264 arg:
1265 subdevice number
1266
1267 reads:
1268 none
1269
1270 writes:
1271 none
1272
1273*/
1274
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301275static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1276 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001277{
1278 int ret = 0;
1279 unsigned long flags;
Bill Pemberton34c43922009-03-16 22:05:14 -04001280 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001281
1282 if (arg >= dev->n_subdevices)
1283 return -EINVAL;
1284 s = dev->subdevices + arg;
1285
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001286 spin_lock_irqsave(&s->spin_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001287 if (s->busy || s->lock)
David Schleefed9eccb2008-11-04 20:29:31 -08001288 ret = -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001289 else
David Schleefed9eccb2008-11-04 20:29:31 -08001290 s->lock = file;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001291 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08001292
Greg Dietschec5274ab2011-06-13 13:11:47 -05001293#if 0
David Schleefed9eccb2008-11-04 20:29:31 -08001294 if (ret < 0)
1295 return ret;
1296
David Schleefed9eccb2008-11-04 20:29:31 -08001297 if (s->lock_f)
1298 ret = s->lock_f(dev, s);
1299#endif
1300
1301 return ret;
1302}
1303
1304/*
1305 COMEDI_UNLOCK
1306 unlock subdevice
1307
1308 arg:
1309 subdevice number
1310
1311 reads:
1312 none
1313
1314 writes:
1315 none
1316
1317 This function isn't protected by the semaphore, since
1318 we already own the lock.
1319*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301320static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1321 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001322{
Bill Pemberton34c43922009-03-16 22:05:14 -04001323 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001324
1325 if (arg >= dev->n_subdevices)
1326 return -EINVAL;
1327 s = dev->subdevices + arg;
1328
1329 if (s->busy)
1330 return -EBUSY;
1331
1332 if (s->lock && s->lock != file)
1333 return -EACCES;
1334
1335 if (s->lock == file) {
1336#if 0
1337 if (s->unlock)
1338 s->unlock(dev, s);
1339#endif
1340
1341 s->lock = NULL;
1342 }
1343
1344 return 0;
1345}
1346
1347/*
1348 COMEDI_CANCEL
1349 cancel acquisition ioctl
1350
1351 arg:
1352 subdevice number
1353
1354 reads:
1355 nothing
1356
1357 writes:
1358 nothing
1359
1360*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301361static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1362 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001363{
Bill Pemberton34c43922009-03-16 22:05:14 -04001364 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001365
1366 if (arg >= dev->n_subdevices)
1367 return -EINVAL;
1368 s = dev->subdevices + arg;
1369 if (s->async == NULL)
1370 return -EINVAL;
1371
1372 if (s->lock && s->lock != file)
1373 return -EACCES;
1374
1375 if (!s->busy)
1376 return 0;
1377
1378 if (s->busy != file)
1379 return -EBUSY;
1380
1381 return do_cancel(dev, s);
1382}
1383
1384/*
1385 COMEDI_POLL ioctl
1386 instructs driver to synchronize buffers
1387
1388 arg:
1389 subdevice number
1390
1391 reads:
1392 nothing
1393
1394 writes:
1395 nothing
1396
1397*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301398static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1399 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001400{
Bill Pemberton34c43922009-03-16 22:05:14 -04001401 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001402
1403 if (arg >= dev->n_subdevices)
1404 return -EINVAL;
1405 s = dev->subdevices + arg;
1406
1407 if (s->lock && s->lock != file)
1408 return -EACCES;
1409
1410 if (!s->busy)
1411 return 0;
1412
1413 if (s->busy != file)
1414 return -EBUSY;
1415
1416 if (s->poll)
1417 return s->poll(dev, s);
1418
1419 return -EINVAL;
1420}
1421
Bill Pemberton34c43922009-03-16 22:05:14 -04001422static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001423{
1424 int ret = 0;
1425
1426 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1427 ret = s->cancel(dev, s);
1428
1429 do_become_nonbusy(dev, s);
1430
1431 return ret;
1432}
1433
Federico Vagadf30b212011-10-29 09:45:39 +02001434
1435static void comedi_vm_open(struct vm_area_struct *area)
1436{
1437 struct comedi_async *async;
1438 struct comedi_device *dev;
1439
1440 async = area->vm_private_data;
1441 dev = async->subdevice->device;
1442
1443 mutex_lock(&dev->mutex);
1444 async->mmap_count++;
1445 mutex_unlock(&dev->mutex);
1446}
1447
1448static void comedi_vm_close(struct vm_area_struct *area)
David Schleefed9eccb2008-11-04 20:29:31 -08001449{
Bill Pembertond1636792009-03-16 22:05:20 -04001450 struct comedi_async *async;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001451 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -08001452
1453 async = area->vm_private_data;
1454 dev = async->subdevice->device;
1455
1456 mutex_lock(&dev->mutex);
1457 async->mmap_count--;
1458 mutex_unlock(&dev->mutex);
1459}
1460
1461static struct vm_operations_struct comedi_vm_ops = {
Federico Vagadf30b212011-10-29 09:45:39 +02001462 .open = comedi_vm_open,
1463 .close = comedi_vm_close,
David Schleefed9eccb2008-11-04 20:29:31 -08001464};
1465
1466static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1467{
1468 const unsigned minor = iminor(file->f_dentry->d_inode);
Bill Pembertond1636792009-03-16 22:05:20 -04001469 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001470 unsigned long start = vma->vm_start;
1471 unsigned long size;
1472 int n_pages;
1473 int i;
1474 int retval;
Bill Pemberton34c43922009-03-16 22:05:14 -04001475 struct comedi_subdevice *s;
Bernd Porr3ffab422011-11-08 21:23:03 +00001476 struct comedi_device_file_info *dev_file_info;
1477 struct comedi_device *dev;
1478
1479 dev_file_info = comedi_get_device_file_info(minor);
1480 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001481 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001482 dev = dev_file_info->device;
1483 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001484 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001485
1486 mutex_lock(&dev->mutex);
1487 if (!dev->attached) {
1488 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1489 retval = -ENODEV;
1490 goto done;
1491 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001492 if (vma->vm_flags & VM_WRITE)
David Schleefed9eccb2008-11-04 20:29:31 -08001493 s = comedi_get_write_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001494 else
David Schleefed9eccb2008-11-04 20:29:31 -08001495 s = comedi_get_read_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001496
David Schleefed9eccb2008-11-04 20:29:31 -08001497 if (s == NULL) {
1498 retval = -EINVAL;
1499 goto done;
1500 }
1501 async = s->async;
1502 if (async == NULL) {
1503 retval = -EINVAL;
1504 goto done;
1505 }
1506
1507 if (vma->vm_pgoff != 0) {
1508 DPRINTK("comedi: mmap() offset must be 0.\n");
1509 retval = -EINVAL;
1510 goto done;
1511 }
1512
1513 size = vma->vm_end - vma->vm_start;
1514 if (size > async->prealloc_bufsz) {
1515 retval = -EFAULT;
1516 goto done;
1517 }
1518 if (size & (~PAGE_MASK)) {
1519 retval = -EFAULT;
1520 goto done;
1521 }
1522
1523 n_pages = size >> PAGE_SHIFT;
1524 for (i = 0; i < n_pages; ++i) {
1525 if (remap_pfn_range(vma, start,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301526 page_to_pfn(virt_to_page
1527 (async->buf_page_list
1528 [i].virt_addr)), PAGE_SIZE,
1529 PAGE_SHARED)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001530 retval = -EAGAIN;
1531 goto done;
1532 }
1533 start += PAGE_SIZE;
1534 }
1535
1536 vma->vm_ops = &comedi_vm_ops;
1537 vma->vm_private_data = async;
1538
1539 async->mmap_count++;
1540
1541 retval = 0;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001542done:
David Schleefed9eccb2008-11-04 20:29:31 -08001543 mutex_unlock(&dev->mutex);
1544 return retval;
1545}
1546
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301547static unsigned int comedi_poll(struct file *file, poll_table * wait)
David Schleefed9eccb2008-11-04 20:29:31 -08001548{
1549 unsigned int mask = 0;
1550 const unsigned minor = iminor(file->f_dentry->d_inode);
Bill Pemberton34c43922009-03-16 22:05:14 -04001551 struct comedi_subdevice *read_subdev;
1552 struct comedi_subdevice *write_subdev;
Bernd Porr3ffab422011-11-08 21:23:03 +00001553 struct comedi_device_file_info *dev_file_info;
1554 struct comedi_device *dev;
1555 dev_file_info = comedi_get_device_file_info(minor);
1556
1557 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001558 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001559 dev = dev_file_info->device;
1560 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001561 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001562
1563 mutex_lock(&dev->mutex);
1564 if (!dev->attached) {
1565 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1566 mutex_unlock(&dev->mutex);
1567 return 0;
1568 }
1569
1570 mask = 0;
1571 read_subdev = comedi_get_read_subdevice(dev_file_info);
1572 if (read_subdev) {
1573 poll_wait(file, &read_subdev->async->wait_head, wait);
1574 if (!read_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001575 || comedi_buf_read_n_available(read_subdev->async) > 0
1576 || !(comedi_get_subdevice_runflags(read_subdev) &
1577 SRF_RUNNING)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001578 mask |= POLLIN | POLLRDNORM;
1579 }
1580 }
1581 write_subdev = comedi_get_write_subdevice(dev_file_info);
1582 if (write_subdev) {
1583 poll_wait(file, &write_subdev->async->wait_head, wait);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001584 comedi_buf_write_alloc(write_subdev->async,
1585 write_subdev->async->prealloc_bufsz);
David Schleefed9eccb2008-11-04 20:29:31 -08001586 if (!write_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001587 || !(comedi_get_subdevice_runflags(write_subdev) &
1588 SRF_RUNNING)
1589 || comedi_buf_write_n_allocated(write_subdev->async) >=
1590 bytes_per_sample(write_subdev->async->subdevice)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001591 mask |= POLLOUT | POLLWRNORM;
1592 }
1593 }
1594
1595 mutex_unlock(&dev->mutex);
1596 return mask;
1597}
1598
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001599static ssize_t comedi_write(struct file *file, const char __user *buf,
1600 size_t nbytes, loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001601{
Bill Pemberton34c43922009-03-16 22:05:14 -04001602 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001603 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001604 int n, m, count = 0, retval = 0;
1605 DECLARE_WAITQUEUE(wait, current);
1606 const unsigned minor = iminor(file->f_dentry->d_inode);
Bernd Porr3ffab422011-11-08 21:23:03 +00001607 struct comedi_device_file_info *dev_file_info;
1608 struct comedi_device *dev;
1609 dev_file_info = comedi_get_device_file_info(minor);
1610
1611 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001612 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001613 dev = dev_file_info->device;
1614 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001615 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001616
1617 if (!dev->attached) {
1618 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1619 retval = -ENODEV;
1620 goto done;
1621 }
1622
1623 s = comedi_get_write_subdevice(dev_file_info);
1624 if (s == NULL) {
1625 retval = -EIO;
1626 goto done;
1627 }
1628 async = s->async;
1629
1630 if (!nbytes) {
1631 retval = 0;
1632 goto done;
1633 }
1634 if (!s->busy) {
1635 retval = 0;
1636 goto done;
1637 }
1638 if (s->busy != file) {
1639 retval = -EACCES;
1640 goto done;
1641 }
1642 add_wait_queue(&async->wait_head, &wait);
1643 while (nbytes > 0 && !retval) {
1644 set_current_state(TASK_INTERRUPTIBLE);
1645
Ian Abbottd2611542010-05-19 17:22:41 +01001646 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1647 if (count == 0) {
1648 if (comedi_get_subdevice_runflags(s) &
1649 SRF_ERROR) {
1650 retval = -EPIPE;
1651 } else {
1652 retval = 0;
1653 }
1654 do_become_nonbusy(dev, s);
1655 }
1656 break;
1657 }
1658
David Schleefed9eccb2008-11-04 20:29:31 -08001659 n = nbytes;
1660
1661 m = n;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001662 if (async->buf_write_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001663 m = async->prealloc_bufsz - async->buf_write_ptr;
David Schleefed9eccb2008-11-04 20:29:31 -08001664 comedi_buf_write_alloc(async, async->prealloc_bufsz);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001665 if (m > comedi_buf_write_n_allocated(async))
David Schleefed9eccb2008-11-04 20:29:31 -08001666 m = comedi_buf_write_n_allocated(async);
David Schleefed9eccb2008-11-04 20:29:31 -08001667 if (m < n)
1668 n = m;
1669
1670 if (n == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001671 if (file->f_flags & O_NONBLOCK) {
1672 retval = -EAGAIN;
1673 break;
1674 }
Federico Vaga6a9ce6b2011-10-29 09:47:39 +02001675 schedule();
David Schleefed9eccb2008-11-04 20:29:31 -08001676 if (signal_pending(current)) {
1677 retval = -ERESTARTSYS;
1678 break;
1679 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001680 if (!s->busy)
David Schleefed9eccb2008-11-04 20:29:31 -08001681 break;
David Schleefed9eccb2008-11-04 20:29:31 -08001682 if (s->busy != file) {
1683 retval = -EACCES;
1684 break;
1685 }
1686 continue;
1687 }
1688
1689 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001690 buf, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001691 if (m) {
1692 n -= m;
1693 retval = -EFAULT;
1694 }
1695 comedi_buf_write_free(async, n);
1696
1697 count += n;
1698 nbytes -= n;
1699
1700 buf += n;
1701 break; /* makes device work like a pipe */
1702 }
1703 set_current_state(TASK_RUNNING);
1704 remove_wait_queue(&async->wait_head, &wait);
1705
1706done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001707 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001708}
1709
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001710static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01001711 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001712{
Bill Pemberton34c43922009-03-16 22:05:14 -04001713 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001714 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001715 int n, m, count = 0, retval = 0;
1716 DECLARE_WAITQUEUE(wait, current);
1717 const unsigned minor = iminor(file->f_dentry->d_inode);
Bernd Porr3ffab422011-11-08 21:23:03 +00001718 struct comedi_device_file_info *dev_file_info;
1719 struct comedi_device *dev;
1720 dev_file_info = comedi_get_device_file_info(minor);
1721
1722 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001723 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001724 dev = dev_file_info->device;
1725 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001726 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001727
1728 if (!dev->attached) {
1729 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1730 retval = -ENODEV;
1731 goto done;
1732 }
1733
1734 s = comedi_get_read_subdevice(dev_file_info);
1735 if (s == NULL) {
1736 retval = -EIO;
1737 goto done;
1738 }
1739 async = s->async;
1740 if (!nbytes) {
1741 retval = 0;
1742 goto done;
1743 }
1744 if (!s->busy) {
1745 retval = 0;
1746 goto done;
1747 }
1748 if (s->busy != file) {
1749 retval = -EACCES;
1750 goto done;
1751 }
1752
1753 add_wait_queue(&async->wait_head, &wait);
1754 while (nbytes > 0 && !retval) {
1755 set_current_state(TASK_INTERRUPTIBLE);
1756
1757 n = nbytes;
1758
1759 m = comedi_buf_read_n_available(async);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001760 /* printk("%d available\n",m); */
1761 if (async->buf_read_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001762 m = async->prealloc_bufsz - async->buf_read_ptr;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001763 /* printk("%d contiguous\n",m); */
David Schleefed9eccb2008-11-04 20:29:31 -08001764 if (m < n)
1765 n = m;
1766
1767 if (n == 0) {
1768 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1769 do_become_nonbusy(dev, s);
1770 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001771 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08001772 retval = -EPIPE;
1773 } else {
1774 retval = 0;
1775 }
1776 break;
1777 }
1778 if (file->f_flags & O_NONBLOCK) {
1779 retval = -EAGAIN;
1780 break;
1781 }
Federico Vaga6a9ce6b2011-10-29 09:47:39 +02001782 schedule();
David Schleefed9eccb2008-11-04 20:29:31 -08001783 if (signal_pending(current)) {
1784 retval = -ERESTARTSYS;
1785 break;
1786 }
David Schleefed9eccb2008-11-04 20:29:31 -08001787 if (!s->busy) {
1788 retval = 0;
1789 break;
1790 }
1791 if (s->busy != file) {
1792 retval = -EACCES;
1793 break;
1794 }
1795 continue;
1796 }
1797 m = copy_to_user(buf, async->prealloc_buf +
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001798 async->buf_read_ptr, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001799 if (m) {
1800 n -= m;
1801 retval = -EFAULT;
1802 }
1803
1804 comedi_buf_read_alloc(async, n);
1805 comedi_buf_read_free(async, n);
1806
1807 count += n;
1808 nbytes -= n;
1809
1810 buf += n;
1811 break; /* makes device work like a pipe */
1812 }
1813 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001814 async->buf_read_count - async->buf_write_count == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001815 do_become_nonbusy(dev, s);
1816 }
1817 set_current_state(TASK_RUNNING);
1818 remove_wait_queue(&async->wait_head, &wait);
1819
1820done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001821 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001822}
1823
1824/*
1825 This function restores a subdevice to an idle state.
1826 */
Bill Pemberton34c43922009-03-16 22:05:14 -04001827void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001828{
Bill Pembertond1636792009-03-16 22:05:20 -04001829 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08001830
1831 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001832 if (async) {
1833 comedi_reset_async_buf(async);
1834 async->inttrig = NULL;
Ian Abbottcc088792012-09-19 19:37:39 +01001835 kfree(async->cmd.chanlist);
1836 async->cmd.chanlist = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001837 } else {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001838 printk(KERN_ERR
1839 "BUG: (?) do_become_nonbusy called with async=0\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001840 }
1841
1842 s->busy = NULL;
1843}
1844
1845static int comedi_open(struct inode *inode, struct file *file)
1846{
David Schleefed9eccb2008-11-04 20:29:31 -08001847 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001848 struct comedi_device_file_info *dev_file_info =
1849 comedi_get_device_file_info(minor);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301850 struct comedi_device *dev =
1851 dev_file_info ? dev_file_info->device : NULL;
Ian Abbott97920072009-02-09 16:51:38 +00001852
David Schleefed9eccb2008-11-04 20:29:31 -08001853 if (dev == NULL) {
1854 DPRINTK("invalid minor number\n");
1855 return -ENODEV;
1856 }
1857
1858 /* This is slightly hacky, but we want module autoloading
1859 * to work for root.
1860 * case: user opens device, attached -> ok
1861 * case: user opens device, unattached, in_request_module=0 -> autoload
1862 * case: user opens device, unattached, in_request_module=1 -> fail
1863 * case: root opens device, attached -> ok
1864 * case: root opens device, unattached, in_request_module=1 -> ok
1865 * (typically called from modprobe)
1866 * case: root opens device, unattached, in_request_module=0 -> autoload
1867 *
1868 * The last could be changed to "-> ok", which would deny root
1869 * autoloading.
1870 */
1871 mutex_lock(&dev->mutex);
1872 if (dev->attached)
1873 goto ok;
Eric Parisa8f80e82009-08-13 09:44:51 -04001874 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
David Schleefed9eccb2008-11-04 20:29:31 -08001875 DPRINTK("in request module\n");
1876 mutex_unlock(&dev->mutex);
1877 return -ENODEV;
1878 }
Eric Parisa8f80e82009-08-13 09:44:51 -04001879 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
David Schleefed9eccb2008-11-04 20:29:31 -08001880 goto ok;
1881
1882 dev->in_request_module = 1;
1883
David Schleefed9eccb2008-11-04 20:29:31 -08001884#ifdef CONFIG_KMOD
1885 mutex_unlock(&dev->mutex);
Ian Abbott56d92c62009-02-09 16:32:12 +00001886 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
David Schleefed9eccb2008-11-04 20:29:31 -08001887 mutex_lock(&dev->mutex);
1888#endif
1889
1890 dev->in_request_module = 0;
1891
Eric Parisa8f80e82009-08-13 09:44:51 -04001892 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1893 DPRINTK("not attached and not CAP_NET_ADMIN\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001894 mutex_unlock(&dev->mutex);
1895 return -ENODEV;
1896 }
1897ok:
1898 __module_get(THIS_MODULE);
1899
1900 if (dev->attached) {
1901 if (!try_module_get(dev->driver->module)) {
1902 module_put(THIS_MODULE);
1903 mutex_unlock(&dev->mutex);
1904 return -ENOSYS;
1905 }
1906 }
1907
Ian Abbott3c17ba072010-05-19 14:10:00 +01001908 if (dev->attached && dev->use_count == 0 && dev->open) {
1909 int rc = dev->open(dev);
1910 if (rc < 0) {
1911 module_put(dev->driver->module);
1912 module_put(THIS_MODULE);
1913 mutex_unlock(&dev->mutex);
1914 return rc;
1915 }
1916 }
David Schleefed9eccb2008-11-04 20:29:31 -08001917
1918 dev->use_count++;
1919
1920 mutex_unlock(&dev->mutex);
1921
1922 return 0;
1923}
1924
1925static int comedi_close(struct inode *inode, struct file *file)
1926{
1927 const unsigned minor = iminor(inode);
Bill Pemberton34c43922009-03-16 22:05:14 -04001928 struct comedi_subdevice *s = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001929 int i;
Bernd Porr3ffab422011-11-08 21:23:03 +00001930 struct comedi_device_file_info *dev_file_info;
1931 struct comedi_device *dev;
1932 dev_file_info = comedi_get_device_file_info(minor);
1933
1934 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001935 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001936 dev = dev_file_info->device;
1937 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001938 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001939
1940 mutex_lock(&dev->mutex);
1941
1942 if (dev->subdevices) {
1943 for (i = 0; i < dev->n_subdevices; i++) {
1944 s = dev->subdevices + i;
1945
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001946 if (s->busy == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001947 do_cancel(dev, s);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001948 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001949 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001950 }
1951 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001952 if (dev->attached && dev->use_count == 1 && dev->close)
David Schleefed9eccb2008-11-04 20:29:31 -08001953 dev->close(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08001954
1955 module_put(THIS_MODULE);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001956 if (dev->attached)
David Schleefed9eccb2008-11-04 20:29:31 -08001957 module_put(dev->driver->module);
David Schleefed9eccb2008-11-04 20:29:31 -08001958
1959 dev->use_count--;
1960
1961 mutex_unlock(&dev->mutex);
1962
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001963 if (file->f_flags & FASYNC)
David Schleefed9eccb2008-11-04 20:29:31 -08001964 comedi_fasync(-1, file, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08001965
1966 return 0;
1967}
1968
1969static int comedi_fasync(int fd, struct file *file, int on)
1970{
1971 const unsigned minor = iminor(file->f_dentry->d_inode);
Bernd Porr3ffab422011-11-08 21:23:03 +00001972 struct comedi_device_file_info *dev_file_info;
1973 struct comedi_device *dev;
1974 dev_file_info = comedi_get_device_file_info(minor);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001975
Bernd Porr3ffab422011-11-08 21:23:03 +00001976 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001977 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001978 dev = dev_file_info->device;
1979 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001980 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001981
1982 return fasync_helper(fd, file, on, &dev->async_queue);
1983}
1984
1985const struct file_operations comedi_fops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301986 .owner = THIS_MODULE,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301987 .unlocked_ioctl = comedi_unlocked_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301988 .compat_ioctl = comedi_compat_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301989 .open = comedi_open,
1990 .release = comedi_close,
1991 .read = comedi_read,
1992 .write = comedi_write,
1993 .mmap = comedi_mmap,
1994 .poll = comedi_poll,
1995 .fasync = comedi_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001996 .llseek = noop_llseek,
David Schleefed9eccb2008-11-04 20:29:31 -08001997};
1998
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001999struct class *comedi_class;
David Schleefed9eccb2008-11-04 20:29:31 -08002000static struct cdev comedi_cdev;
2001
2002static void comedi_cleanup_legacy_minors(void)
2003{
2004 unsigned i;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002005
Bernd Porr1dd33ab2008-12-08 23:30:13 +00002006 for (i = 0; i < comedi_num_legacy_minors; i++)
David Schleefed9eccb2008-11-04 20:29:31 -08002007 comedi_free_board_minor(i);
David Schleefed9eccb2008-11-04 20:29:31 -08002008}
2009
2010static int __init comedi_init(void)
2011{
2012 int i;
2013 int retval;
2014
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002015 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
2016 " - http://www.comedi.org\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002017
Frank Mori Hessa3cb7292008-12-15 13:44:45 +00002018 if (comedi_num_legacy_minors < 0 ||
2019 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2020 printk(KERN_ERR "comedi: error: invalid value for module "
2021 "parameter \"comedi_num_legacy_minors\". Valid values "
2022 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2023 return -EINVAL;
2024 }
2025
2026 /*
2027 * comedi is unusable if both comedi_autoconfig and
2028 * comedi_num_legacy_minors are zero, so we might as well adjust the
2029 * defaults in that case
2030 */
2031 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2032 comedi_num_legacy_minors = 16;
2033
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002034 memset(comedi_file_info_table, 0,
2035 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08002036
2037 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002038 COMEDI_NUM_MINORS, "comedi");
David Schleefed9eccb2008-11-04 20:29:31 -08002039 if (retval)
2040 return -EIO;
2041 cdev_init(&comedi_cdev, &comedi_fops);
2042 comedi_cdev.owner = THIS_MODULE;
2043 kobject_set_name(&comedi_cdev.kobj, "comedi");
2044 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2045 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002046 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08002047 return -EIO;
2048 }
2049 comedi_class = class_create(THIS_MODULE, "comedi");
2050 if (IS_ERR(comedi_class)) {
Mark Rankilor3fffdf22010-04-29 18:17:16 +08002051 printk(KERN_ERR "comedi: failed to create class");
David Schleefed9eccb2008-11-04 20:29:31 -08002052 cdev_del(&comedi_cdev);
2053 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002054 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08002055 return PTR_ERR(comedi_class);
2056 }
2057
2058 /* XXX requires /proc interface */
2059 comedi_proc_init();
2060
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002061 /* create devices files for legacy/manual use */
Bernd Porr1dd33ab2008-12-08 23:30:13 +00002062 for (i = 0; i < comedi_num_legacy_minors; i++) {
David Schleefed9eccb2008-11-04 20:29:31 -08002063 int minor;
2064 minor = comedi_alloc_board_minor(NULL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002065 if (minor < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08002066 comedi_cleanup_legacy_minors();
2067 cdev_del(&comedi_cdev);
2068 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002069 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08002070 return minor;
2071 }
2072 }
2073
David Schleefed9eccb2008-11-04 20:29:31 -08002074 return 0;
2075}
2076
2077static void __exit comedi_cleanup(void)
2078{
2079 int i;
2080
2081 comedi_cleanup_legacy_minors();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002082 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
David Schleefed9eccb2008-11-04 20:29:31 -08002083 BUG_ON(comedi_file_info_table[i]);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002084
David Schleefed9eccb2008-11-04 20:29:31 -08002085 class_destroy(comedi_class);
2086 cdev_del(&comedi_cdev);
2087 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2088
2089 comedi_proc_cleanup();
David Schleefed9eccb2008-11-04 20:29:31 -08002090}
2091
2092module_init(comedi_init);
2093module_exit(comedi_cleanup);
2094
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002095void comedi_error(const struct comedi_device *dev, const char *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002096{
Mark Rankilor3fffdf22010-04-29 18:17:16 +08002097 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2098 dev->driver->driver_name, s);
David Schleefed9eccb2008-11-04 20:29:31 -08002099}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002100EXPORT_SYMBOL(comedi_error);
David Schleefed9eccb2008-11-04 20:29:31 -08002101
Bill Pemberton34c43922009-03-16 22:05:14 -04002102void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002103{
Bill Pembertond1636792009-03-16 22:05:20 -04002104 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08002105 unsigned runflags = 0;
2106 unsigned runflags_mask = 0;
2107
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002108 /* DPRINTK("comedi_event 0x%x\n",mask); */
David Schleefed9eccb2008-11-04 20:29:31 -08002109
2110 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2111 return;
2112
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302113 if (s->
2114 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2115 COMEDI_CB_OVERFLOW)) {
David Schleefed9eccb2008-11-04 20:29:31 -08002116 runflags_mask |= SRF_RUNNING;
2117 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002118 /* remember if an error event has occurred, so an error
David Schleefed9eccb2008-11-04 20:29:31 -08002119 * can be returned the next time the user does a read() */
2120 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2121 runflags_mask |= SRF_ERROR;
2122 runflags |= SRF_ERROR;
2123 }
2124 if (runflags_mask) {
2125 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2126 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2127 }
2128
2129 if (async->cb_mask & s->async->events) {
2130 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
Greg Kroah-Hartmanfcea1152009-04-27 15:15:30 -07002131 wake_up_interruptible(&async->wait_head);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002132 if (s->subdev_flags & SDF_CMD_READ)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302133 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002134 if (s->subdev_flags & SDF_CMD_WRITE)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302135 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
David Schleefed9eccb2008-11-04 20:29:31 -08002136 } else {
2137 if (async->cb_func)
2138 async->cb_func(s->async->events, async->cb_arg);
David Schleefed9eccb2008-11-04 20:29:31 -08002139 }
2140 }
2141 s->async->events = 0;
2142}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002143EXPORT_SYMBOL(comedi_event);
David Schleefed9eccb2008-11-04 20:29:31 -08002144
Bill Pemberton34c43922009-03-16 22:05:14 -04002145unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002146{
2147 unsigned long flags;
2148 unsigned runflags;
2149
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002150 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002151 runflags = s->runflags;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002152 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002153 return runflags;
2154}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002155EXPORT_SYMBOL(comedi_get_subdevice_runflags);
David Schleefed9eccb2008-11-04 20:29:31 -08002156
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002157static int is_device_busy(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002158{
Bill Pemberton34c43922009-03-16 22:05:14 -04002159 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08002160 int i;
2161
2162 if (!dev->attached)
2163 return 0;
2164
2165 for (i = 0; i < dev->n_subdevices; i++) {
2166 s = dev->subdevices + i;
2167 if (s->busy)
2168 return 1;
2169 if (s->async && s->async->mmap_count)
2170 return 1;
2171 }
2172
2173 return 0;
2174}
2175
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002176static void comedi_device_init(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002177{
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002178 memset(dev, 0, sizeof(struct comedi_device));
David Schleefed9eccb2008-11-04 20:29:31 -08002179 spin_lock_init(&dev->spinlock);
2180 mutex_init(&dev->mutex);
2181 dev->minor = -1;
2182}
2183
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002184static void comedi_device_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002185{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002186 if (dev == NULL)
2187 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002188 mutex_lock(&dev->mutex);
2189 comedi_device_detach(dev);
2190 mutex_unlock(&dev->mutex);
2191 mutex_destroy(&dev->mutex);
2192}
2193
2194int comedi_alloc_board_minor(struct device *hardware_device)
2195{
2196 unsigned long flags;
2197 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002198 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002199 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002200 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002201
2202 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002203 if (info == NULL)
2204 return -ENOMEM;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002205 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002206 if (info->device == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002207 kfree(info);
2208 return -ENOMEM;
2209 }
2210 comedi_device_init(info->device);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002211 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002212 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2213 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002214 comedi_file_info_table[i] = info;
2215 break;
2216 }
2217 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002218 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002219 if (i == COMEDI_NUM_BOARD_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002220 comedi_device_cleanup(info->device);
2221 kfree(info->device);
2222 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302223 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002224 "comedi: error: "
2225 "ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002226 return -EBUSY;
2227 }
2228 info->device->minor = i;
Pavel Roskin0435f932011-07-06 10:15:44 -04002229 csdev = device_create(comedi_class, hardware_device,
2230 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002231 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002232 info->device->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002233 dev_set_drvdata(csdev, info);
2234 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2235 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302236 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002237 "comedi: "
2238 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302239 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002240 comedi_free_board_minor(i);
2241 return retval;
2242 }
2243 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2244 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302245 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002246 "comedi: "
2247 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302248 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002249 comedi_free_board_minor(i);
2250 return retval;
2251 }
2252 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2253 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302254 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002255 "comedi: "
2256 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302257 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002258 comedi_free_board_minor(i);
2259 return retval;
2260 }
2261 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2262 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302263 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002264 "comedi: "
2265 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302266 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002267 comedi_free_board_minor(i);
2268 return retval;
2269 }
David Schleefed9eccb2008-11-04 20:29:31 -08002270 return i;
2271}
2272
2273void comedi_free_board_minor(unsigned minor)
2274{
2275 unsigned long flags;
2276 struct comedi_device_file_info *info;
2277
2278 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002279 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002280 info = comedi_file_info_table[minor];
2281 comedi_file_info_table[minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002282 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002283
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002284 if (info) {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002285 struct comedi_device *dev = info->device;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002286 if (dev) {
2287 if (dev->class_dev) {
2288 device_destroy(comedi_class,
2289 MKDEV(COMEDI_MAJOR, dev->minor));
David Schleefed9eccb2008-11-04 20:29:31 -08002290 }
2291 comedi_device_cleanup(dev);
2292 kfree(dev);
2293 }
2294 kfree(info);
2295 }
2296}
2297
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002298int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2299 struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002300{
2301 unsigned long flags;
2302 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002303 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002304 unsigned i;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002305 int retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002306
2307 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002308 if (info == NULL)
2309 return -ENOMEM;
David Schleefed9eccb2008-11-04 20:29:31 -08002310 info->device = dev;
2311 info->read_subdevice = s;
2312 info->write_subdevice = s;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002313 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
Frank Mori Hess4c41f3a2008-12-09 14:47:22 +00002314 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002315 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002316 comedi_file_info_table[i] = info;
2317 break;
2318 }
2319 }
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002320 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002321 if (i == COMEDI_NUM_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002322 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302323 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002324 "comedi: error: "
2325 "ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002326 return -EBUSY;
2327 }
2328 s->minor = i;
Pavel Roskin0435f932011-07-06 10:15:44 -04002329 csdev = device_create(comedi_class, dev->class_dev,
2330 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2331 dev->minor, (int)(s - dev->subdevices));
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002332 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002333 s->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002334 dev_set_drvdata(csdev, info);
2335 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2336 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302337 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002338 "comedi: "
2339 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302340 dev_attr_max_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002341 comedi_free_subdevice_minor(s);
2342 return retval;
2343 }
2344 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2345 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302346 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002347 "comedi: "
2348 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302349 dev_attr_read_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002350 comedi_free_subdevice_minor(s);
2351 return retval;
2352 }
2353 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2354 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302355 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002356 "comedi: "
2357 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302358 dev_attr_max_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002359 comedi_free_subdevice_minor(s);
2360 return retval;
2361 }
2362 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2363 if (retval) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302364 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002365 "comedi: "
2366 "failed to create sysfs attribute file \"%s\".\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302367 dev_attr_write_buffer_kb.attr.name);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002368 comedi_free_subdevice_minor(s);
2369 return retval;
2370 }
David Schleefed9eccb2008-11-04 20:29:31 -08002371 return i;
2372}
2373
Bill Pemberton34c43922009-03-16 22:05:14 -04002374void comedi_free_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002375{
2376 unsigned long flags;
2377 struct comedi_device_file_info *info;
2378
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002379 if (s == NULL)
2380 return;
2381 if (s->minor < 0)
2382 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002383
2384 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2385 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2386
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002387 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002388 info = comedi_file_info_table[s->minor];
2389 comedi_file_info_table[s->minor] = NULL;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002390 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002391
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002392 if (s->class_dev) {
David Schleefed9eccb2008-11-04 20:29:31 -08002393 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2394 s->class_dev = NULL;
2395 }
2396 kfree(info);
2397}
2398
2399struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2400{
2401 unsigned long flags;
2402 struct comedi_device_file_info *info;
2403
2404 BUG_ON(minor >= COMEDI_NUM_MINORS);
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002405 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002406 info = comedi_file_info_table[minor];
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002407 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002408 return info;
2409}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002410EXPORT_SYMBOL_GPL(comedi_get_device_file_info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002411
2412static int resize_async_buffer(struct comedi_device *dev,
2413 struct comedi_subdevice *s,
2414 struct comedi_async *async, unsigned new_size)
2415{
2416 int retval;
2417
2418 if (new_size > async->max_bufsize)
2419 return -EPERM;
2420
2421 if (s->busy) {
2422 DPRINTK("subdevice is busy, cannot resize buffer\n");
2423 return -EBUSY;
2424 }
2425 if (async->mmap_count) {
2426 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2427 return -EBUSY;
2428 }
2429
2430 if (!async->prealloc_buf)
2431 return -EINVAL;
2432
2433 /* make sure buffer is an integral number of pages
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302434 * (we round up) */
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002435 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2436
2437 retval = comedi_buf_alloc(dev, s, new_size);
2438 if (retval < 0)
2439 return retval;
2440
2441 if (s->buf_change) {
2442 retval = s->buf_change(dev, s, new_size);
2443 if (retval < 0)
2444 return retval;
2445 }
2446
2447 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
Ian Abbottb8b5cd92009-09-21 14:55:23 -04002448 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002449 return 0;
2450}
2451
2452/* sysfs attribute files */
2453
2454static const unsigned bytes_per_kibi = 1024;
2455
2456static ssize_t show_max_read_buffer_kb(struct device *dev,
2457 struct device_attribute *attr, char *buf)
2458{
2459 ssize_t retval;
2460 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2461 unsigned max_buffer_size_kb = 0;
2462 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302463 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002464
2465 mutex_lock(&info->device->mutex);
2466 if (read_subdevice &&
2467 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2468 read_subdevice->async) {
2469 max_buffer_size_kb = read_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302470 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002471 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302472 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002473 mutex_unlock(&info->device->mutex);
2474
2475 return retval;
2476}
2477
2478static ssize_t store_max_read_buffer_kb(struct device *dev,
2479 struct device_attribute *attr,
2480 const char *buf, size_t count)
2481{
2482 struct comedi_device_file_info *info = dev_get_drvdata(dev);
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002483 unsigned int new_max_size_kb;
Florian Schmausc5018162011-12-08 12:12:45 +01002484 unsigned int new_max_size;
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002485 int ret;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002486 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302487 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002488
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002489 ret = kstrtouint(buf, 10, &new_max_size_kb);
2490 if (ret)
2491 return ret;
Florian Schmausc5018162011-12-08 12:12:45 +01002492 if (new_max_size_kb > (UINT_MAX / bytes_per_kibi))
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002493 return -EINVAL;
Florian Schmausc5018162011-12-08 12:12:45 +01002494 new_max_size = new_max_size_kb * bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002495
2496 mutex_lock(&info->device->mutex);
2497 if (read_subdevice == NULL ||
2498 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2499 read_subdevice->async == NULL) {
2500 mutex_unlock(&info->device->mutex);
2501 return -EINVAL;
2502 }
2503 read_subdevice->async->max_bufsize = new_max_size;
2504 mutex_unlock(&info->device->mutex);
2505
2506 return count;
2507}
2508
2509static struct device_attribute dev_attr_max_read_buffer_kb = {
2510 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302511 .name = "max_read_buffer_kb",
2512 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002513 .show = &show_max_read_buffer_kb,
2514 .store = &store_max_read_buffer_kb
2515};
2516
2517static ssize_t show_read_buffer_kb(struct device *dev,
2518 struct device_attribute *attr, char *buf)
2519{
2520 ssize_t retval;
2521 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2522 unsigned buffer_size_kb = 0;
2523 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302524 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002525
2526 mutex_lock(&info->device->mutex);
2527 if (read_subdevice &&
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302528 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2529 read_subdevice->async) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002530 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302531 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002532 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302533 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002534 mutex_unlock(&info->device->mutex);
2535
2536 return retval;
2537}
2538
2539static ssize_t store_read_buffer_kb(struct device *dev,
2540 struct device_attribute *attr,
2541 const char *buf, size_t count)
2542{
2543 struct comedi_device_file_info *info = dev_get_drvdata(dev);
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002544 unsigned int new_size_kb;
Florian Schmausc5018162011-12-08 12:12:45 +01002545 unsigned int new_size;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002546 int retval;
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002547 int ret;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002548 struct comedi_subdevice *const read_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302549 comedi_get_read_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002550
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002551 ret = kstrtouint(buf, 10, &new_size_kb);
2552 if (ret)
2553 return ret;
Florian Schmausc5018162011-12-08 12:12:45 +01002554 if (new_size_kb > (UINT_MAX / bytes_per_kibi))
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002555 return -EINVAL;
Florian Schmausc5018162011-12-08 12:12:45 +01002556 new_size = new_size_kb * bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002557
2558 mutex_lock(&info->device->mutex);
2559 if (read_subdevice == NULL ||
2560 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2561 read_subdevice->async == NULL) {
2562 mutex_unlock(&info->device->mutex);
2563 return -EINVAL;
2564 }
2565 retval = resize_async_buffer(info->device, read_subdevice,
2566 read_subdevice->async, new_size);
2567 mutex_unlock(&info->device->mutex);
2568
2569 if (retval < 0)
2570 return retval;
2571 return count;
2572}
2573
2574static struct device_attribute dev_attr_read_buffer_kb = {
2575 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302576 .name = "read_buffer_kb",
2577 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002578 .show = &show_read_buffer_kb,
2579 .store = &store_read_buffer_kb
2580};
2581
2582static ssize_t show_max_write_buffer_kb(struct device *dev,
2583 struct device_attribute *attr,
2584 char *buf)
2585{
2586 ssize_t retval;
2587 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2588 unsigned max_buffer_size_kb = 0;
2589 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302590 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002591
2592 mutex_lock(&info->device->mutex);
2593 if (write_subdevice &&
2594 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2595 write_subdevice->async) {
2596 max_buffer_size_kb = write_subdevice->async->max_bufsize /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302597 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002598 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302599 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002600 mutex_unlock(&info->device->mutex);
2601
2602 return retval;
2603}
2604
2605static ssize_t store_max_write_buffer_kb(struct device *dev,
2606 struct device_attribute *attr,
2607 const char *buf, size_t count)
2608{
2609 struct comedi_device_file_info *info = dev_get_drvdata(dev);
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002610 unsigned int new_max_size_kb;
Florian Schmausc5018162011-12-08 12:12:45 +01002611 unsigned int new_max_size;
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002612 int ret;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002613 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302614 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002615
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002616 ret = kstrtouint(buf, 10, &new_max_size_kb);
2617 if (ret)
2618 return ret;
Florian Schmausc5018162011-12-08 12:12:45 +01002619 if (new_max_size_kb > (UINT_MAX / bytes_per_kibi))
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002620 return -EINVAL;
Florian Schmausc5018162011-12-08 12:12:45 +01002621 new_max_size = new_max_size_kb * bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002622
2623 mutex_lock(&info->device->mutex);
2624 if (write_subdevice == NULL ||
2625 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2626 write_subdevice->async == NULL) {
2627 mutex_unlock(&info->device->mutex);
2628 return -EINVAL;
2629 }
2630 write_subdevice->async->max_bufsize = new_max_size;
2631 mutex_unlock(&info->device->mutex);
2632
2633 return count;
2634}
2635
2636static struct device_attribute dev_attr_max_write_buffer_kb = {
2637 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302638 .name = "max_write_buffer_kb",
2639 .mode = S_IRUGO | S_IWUSR},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002640 .show = &show_max_write_buffer_kb,
2641 .store = &store_max_write_buffer_kb
2642};
2643
2644static ssize_t show_write_buffer_kb(struct device *dev,
2645 struct device_attribute *attr, char *buf)
2646{
2647 ssize_t retval;
2648 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2649 unsigned buffer_size_kb = 0;
2650 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302651 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002652
2653 mutex_lock(&info->device->mutex);
2654 if (write_subdevice &&
2655 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2656 write_subdevice->async) {
2657 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302658 bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002659 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302660 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002661 mutex_unlock(&info->device->mutex);
2662
2663 return retval;
2664}
2665
2666static ssize_t store_write_buffer_kb(struct device *dev,
2667 struct device_attribute *attr,
2668 const char *buf, size_t count)
2669{
2670 struct comedi_device_file_info *info = dev_get_drvdata(dev);
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002671 unsigned int new_size_kb;
Florian Schmausc5018162011-12-08 12:12:45 +01002672 unsigned int new_size;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002673 int retval;
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002674 int ret;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002675 struct comedi_subdevice *const write_subdevice =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302676 comedi_get_write_subdevice(info);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002677
Florian Schmaus2e1c3942011-12-08 12:12:44 +01002678 ret = kstrtouint(buf, 10, &new_size_kb);
2679 if (ret)
2680 return ret;
Florian Schmausc5018162011-12-08 12:12:45 +01002681 if (new_size_kb > (UINT_MAX / bytes_per_kibi))
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002682 return -EINVAL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302683 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002684
2685 mutex_lock(&info->device->mutex);
2686 if (write_subdevice == NULL ||
2687 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2688 write_subdevice->async == NULL) {
2689 mutex_unlock(&info->device->mutex);
2690 return -EINVAL;
2691 }
2692 retval = resize_async_buffer(info->device, write_subdevice,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302693 write_subdevice->async, new_size);
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002694 mutex_unlock(&info->device->mutex);
2695
2696 if (retval < 0)
2697 return retval;
2698 return count;
2699}
2700
2701static struct device_attribute dev_attr_write_buffer_kb = {
2702 .attr = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302703 .name = "write_buffer_kb",
2704 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002705 .show = &show_write_buffer_kb,
2706 .store = &store_write_buffer_kb
2707};