Commit | Line | Data |
---|---|---|
ed9eccbe DS |
1 | /* |
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 | ||
ed9eccbe DS |
26 | #include "comedi_compat32.h" |
27 | ||
28 | #include <linux/module.h> | |
29 | #include <linux/errno.h> | |
30 | #include <linux/kernel.h> | |
31 | #include <linux/sched.h> | |
32 | #include <linux/fcntl.h> | |
33 | #include <linux/delay.h> | |
34 | #include <linux/ioport.h> | |
35 | #include <linux/mm.h> | |
36 | #include <linux/slab.h> | |
37 | #include <linux/kmod.h> | |
38 | #include <linux/poll.h> | |
39 | #include <linux/init.h> | |
40 | #include <linux/device.h> | |
41 | #include <linux/vmalloc.h> | |
42 | #include <linux/fs.h> | |
43 | #include "comedidev.h" | |
44 | #include <linux/cdev.h> | |
883db3d9 | 45 | #include <linux/stat.h> |
ed9eccbe | 46 | |
476b8477 GKH |
47 | #include <linux/io.h> |
48 | #include <linux/uaccess.h> | |
ed9eccbe | 49 | |
3a5fa275 | 50 | #include "comedi_internal.h" |
ed9eccbe DS |
51 | |
52 | MODULE_AUTHOR("http://www.comedi.org"); | |
53 | MODULE_DESCRIPTION("Comedi core module"); | |
54 | MODULE_LICENSE("GPL"); | |
55 | ||
56 | #ifdef CONFIG_COMEDI_DEBUG | |
57 | int comedi_debug; | |
18736438 | 58 | EXPORT_SYMBOL(comedi_debug); |
4d7df821 IA |
59 | module_param(comedi_debug, int, S_IRUGO | S_IWUSR); |
60 | MODULE_PARM_DESC(comedi_debug, | |
61 | "enable comedi core and driver debugging if non-zero (default 0)" | |
62 | ); | |
ed9eccbe DS |
63 | #endif |
64 | ||
90ab5ee9 | 65 | bool comedi_autoconfig = 1; |
4d7df821 IA |
66 | module_param(comedi_autoconfig, bool, S_IRUGO); |
67 | MODULE_PARM_DESC(comedi_autoconfig, | |
68 | "enable drivers to auto-configure comedi devices (default 1)"); | |
6a9d7a21 | 69 | |
92d0127c | 70 | static int comedi_num_legacy_minors; |
4d7df821 IA |
71 | module_param(comedi_num_legacy_minors, int, S_IRUGO); |
72 | MODULE_PARM_DESC(comedi_num_legacy_minors, | |
73 | "number of comedi minor devices to reserve for non-auto-configured devices (default 0)" | |
74 | ); | |
75 | ||
234bb3c6 | 76 | unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB; |
4d7df821 IA |
77 | module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR); |
78 | MODULE_PARM_DESC(comedi_default_buf_size_kb, | |
79 | "default asynchronous buffer size in KiB (default " | |
234bb3c6 | 80 | __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")"); |
4d7df821 | 81 | |
234bb3c6 IA |
82 | unsigned int comedi_default_buf_maxsize_kb |
83 | = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB; | |
4d7df821 IA |
84 | module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR); |
85 | MODULE_PARM_DESC(comedi_default_buf_maxsize_kb, | |
86 | "default maximum size of asynchronous buffer in KiB (default " | |
234bb3c6 | 87 | __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")"); |
1dd33ab8 | 88 | |
e79c8d21 HS |
89 | struct comedi_device_file_info { |
90 | struct comedi_device *device; | |
91 | struct comedi_subdevice *read_subdevice; | |
92 | struct comedi_subdevice *write_subdevice; | |
93 | struct device *hardware_device; | |
94 | }; | |
95 | ||
ed9eccbe | 96 | static DEFINE_SPINLOCK(comedi_file_info_table_lock); |
476b8477 | 97 | static struct comedi_device_file_info |
0a85b6f0 | 98 | *comedi_file_info_table[COMEDI_NUM_MINORS]; |
476b8477 | 99 | |
87b1ad7a HS |
100 | static struct comedi_device_file_info * |
101 | comedi_get_device_file_info(unsigned minor) | |
102 | { | |
103 | struct comedi_device_file_info *info; | |
104 | ||
105 | BUG_ON(minor >= COMEDI_NUM_MINORS); | |
106 | spin_lock(&comedi_file_info_table_lock); | |
107 | info = comedi_file_info_table[minor]; | |
108 | spin_unlock(&comedi_file_info_table_lock); | |
109 | return info; | |
110 | } | |
111 | ||
85104e9b HS |
112 | struct comedi_device *comedi_dev_from_minor(unsigned minor) |
113 | { | |
114 | struct comedi_device_file_info *info; | |
115 | ||
116 | info = comedi_get_device_file_info(minor); | |
117 | ||
118 | return info ? info->device : NULL; | |
119 | } | |
120 | EXPORT_SYMBOL_GPL(comedi_dev_from_minor); | |
121 | ||
43bd33f2 HS |
122 | static struct comedi_subdevice * |
123 | comedi_get_read_subdevice(const struct comedi_device_file_info *info) | |
124 | { | |
125 | if (info->read_subdevice) | |
126 | return info->read_subdevice; | |
127 | if (info->device == NULL) | |
128 | return NULL; | |
129 | return info->device->read_subdev; | |
130 | } | |
131 | ||
132 | static struct comedi_subdevice * | |
133 | comedi_get_write_subdevice(const struct comedi_device_file_info *info) | |
134 | { | |
135 | if (info->write_subdevice) | |
136 | return info->write_subdevice; | |
137 | if (info->device == NULL) | |
138 | return NULL; | |
139 | return info->device->write_subdev; | |
140 | } | |
141 | ||
883db3d9 FMH |
142 | static int resize_async_buffer(struct comedi_device *dev, |
143 | struct comedi_subdevice *s, | |
a5011a26 HS |
144 | struct comedi_async *async, unsigned new_size) |
145 | { | |
146 | int retval; | |
147 | ||
148 | if (new_size > async->max_bufsize) | |
149 | return -EPERM; | |
150 | ||
151 | if (s->busy) { | |
152 | DPRINTK("subdevice is busy, cannot resize buffer\n"); | |
153 | return -EBUSY; | |
154 | } | |
155 | if (async->mmap_count) { | |
156 | DPRINTK("subdevice is mmapped, cannot resize buffer\n"); | |
157 | return -EBUSY; | |
158 | } | |
159 | ||
160 | if (!async->prealloc_buf) | |
161 | return -EINVAL; | |
162 | ||
163 | /* make sure buffer is an integral number of pages | |
164 | * (we round up) */ | |
165 | new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK; | |
166 | ||
167 | retval = comedi_buf_alloc(dev, s, new_size); | |
168 | if (retval < 0) | |
169 | return retval; | |
170 | ||
171 | if (s->buf_change) { | |
172 | retval = s->buf_change(dev, s, new_size); | |
173 | if (retval < 0) | |
174 | return retval; | |
175 | } | |
176 | ||
177 | DPRINTK("comedi%i subd %d buffer resized to %i bytes\n", | |
178 | dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz); | |
179 | return 0; | |
180 | } | |
181 | ||
182 | /* sysfs attribute files */ | |
183 | ||
a5011a26 HS |
184 | static ssize_t show_max_read_buffer_kb(struct device *dev, |
185 | struct device_attribute *attr, char *buf) | |
186 | { | |
a5011a26 | 187 | struct comedi_device_file_info *info = dev_get_drvdata(dev); |
72fd9fac HS |
188 | struct comedi_subdevice *s = comedi_get_read_subdevice(info); |
189 | unsigned int size = 0; | |
a5011a26 HS |
190 | |
191 | mutex_lock(&info->device->mutex); | |
72fd9fac HS |
192 | if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) |
193 | size = s->async->max_bufsize / 1024; | |
a5011a26 HS |
194 | mutex_unlock(&info->device->mutex); |
195 | ||
72fd9fac | 196 | return snprintf(buf, PAGE_SIZE, "%i\n", size); |
a5011a26 HS |
197 | } |
198 | ||
199 | static ssize_t store_max_read_buffer_kb(struct device *dev, | |
200 | struct device_attribute *attr, | |
201 | const char *buf, size_t count) | |
202 | { | |
203 | struct comedi_device_file_info *info = dev_get_drvdata(dev); | |
72fd9fac HS |
204 | struct comedi_subdevice *s = comedi_get_read_subdevice(info); |
205 | unsigned int size; | |
206 | int err; | |
207 | ||
208 | err = kstrtouint(buf, 10, &size); | |
209 | if (err) | |
210 | return err; | |
211 | if (size > (UINT_MAX / 1024)) | |
a5011a26 | 212 | return -EINVAL; |
72fd9fac | 213 | size *= 1024; |
a5011a26 HS |
214 | |
215 | mutex_lock(&info->device->mutex); | |
72fd9fac HS |
216 | if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) |
217 | s->async->max_bufsize = size; | |
218 | else | |
219 | err = -EINVAL; | |
a5011a26 HS |
220 | mutex_unlock(&info->device->mutex); |
221 | ||
72fd9fac | 222 | return err ? err : count; |
a5011a26 HS |
223 | } |
224 | ||
a5011a26 HS |
225 | static ssize_t show_read_buffer_kb(struct device *dev, |
226 | struct device_attribute *attr, char *buf) | |
227 | { | |
a5011a26 | 228 | struct comedi_device_file_info *info = dev_get_drvdata(dev); |
72fd9fac HS |
229 | struct comedi_subdevice *s = comedi_get_read_subdevice(info); |
230 | unsigned int size = 0; | |
a5011a26 HS |
231 | |
232 | mutex_lock(&info->device->mutex); | |
72fd9fac HS |
233 | if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) |
234 | size = s->async->prealloc_bufsz / 1024; | |
a5011a26 HS |
235 | mutex_unlock(&info->device->mutex); |
236 | ||
72fd9fac | 237 | return snprintf(buf, PAGE_SIZE, "%i\n", size); |
a5011a26 HS |
238 | } |
239 | ||
240 | static ssize_t store_read_buffer_kb(struct device *dev, | |
241 | struct device_attribute *attr, | |
242 | const char *buf, size_t count) | |
243 | { | |
244 | struct comedi_device_file_info *info = dev_get_drvdata(dev); | |
72fd9fac HS |
245 | struct comedi_subdevice *s = comedi_get_read_subdevice(info); |
246 | unsigned int size; | |
247 | int err; | |
248 | ||
249 | err = kstrtouint(buf, 10, &size); | |
250 | if (err) | |
251 | return err; | |
252 | if (size > (UINT_MAX / 1024)) | |
a5011a26 | 253 | return -EINVAL; |
72fd9fac | 254 | size *= 1024; |
a5011a26 HS |
255 | |
256 | mutex_lock(&info->device->mutex); | |
72fd9fac HS |
257 | if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) |
258 | err = resize_async_buffer(info->device, s, s->async, size); | |
259 | else | |
260 | err = -EINVAL; | |
a5011a26 HS |
261 | mutex_unlock(&info->device->mutex); |
262 | ||
72fd9fac | 263 | return err ? err : count; |
a5011a26 | 264 | } |
883db3d9 | 265 | |
a5011a26 HS |
266 | static ssize_t show_max_write_buffer_kb(struct device *dev, |
267 | struct device_attribute *attr, | |
268 | char *buf) | |
269 | { | |
a5011a26 | 270 | struct comedi_device_file_info *info = dev_get_drvdata(dev); |
72fd9fac HS |
271 | struct comedi_subdevice *s = comedi_get_write_subdevice(info); |
272 | unsigned int size = 0; | |
a5011a26 HS |
273 | |
274 | mutex_lock(&info->device->mutex); | |
72fd9fac HS |
275 | if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) |
276 | size = s->async->max_bufsize / 1024; | |
a5011a26 HS |
277 | mutex_unlock(&info->device->mutex); |
278 | ||
72fd9fac | 279 | return snprintf(buf, PAGE_SIZE, "%i\n", size); |
a5011a26 HS |
280 | } |
281 | ||
282 | static ssize_t store_max_write_buffer_kb(struct device *dev, | |
283 | struct device_attribute *attr, | |
284 | const char *buf, size_t count) | |
285 | { | |
286 | struct comedi_device_file_info *info = dev_get_drvdata(dev); | |
72fd9fac HS |
287 | struct comedi_subdevice *s = comedi_get_write_subdevice(info); |
288 | unsigned int size; | |
289 | int err; | |
290 | ||
291 | err = kstrtouint(buf, 10, &size); | |
292 | if (err) | |
293 | return err; | |
294 | if (size > (UINT_MAX / 1024)) | |
a5011a26 | 295 | return -EINVAL; |
72fd9fac | 296 | size *= 1024; |
a5011a26 HS |
297 | |
298 | mutex_lock(&info->device->mutex); | |
72fd9fac HS |
299 | if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) |
300 | s->async->max_bufsize = size; | |
301 | else | |
302 | err = -EINVAL; | |
a5011a26 HS |
303 | mutex_unlock(&info->device->mutex); |
304 | ||
72fd9fac | 305 | return err ? err : count; |
a5011a26 HS |
306 | } |
307 | ||
a5011a26 HS |
308 | static ssize_t show_write_buffer_kb(struct device *dev, |
309 | struct device_attribute *attr, char *buf) | |
310 | { | |
a5011a26 | 311 | struct comedi_device_file_info *info = dev_get_drvdata(dev); |
72fd9fac HS |
312 | struct comedi_subdevice *s = comedi_get_write_subdevice(info); |
313 | unsigned int size = 0; | |
a5011a26 HS |
314 | |
315 | mutex_lock(&info->device->mutex); | |
72fd9fac HS |
316 | if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) |
317 | size = s->async->prealloc_bufsz / 1024; | |
a5011a26 HS |
318 | mutex_unlock(&info->device->mutex); |
319 | ||
72fd9fac | 320 | return snprintf(buf, PAGE_SIZE, "%i\n", size); |
a5011a26 HS |
321 | } |
322 | ||
323 | static ssize_t store_write_buffer_kb(struct device *dev, | |
324 | struct device_attribute *attr, | |
325 | const char *buf, size_t count) | |
326 | { | |
327 | struct comedi_device_file_info *info = dev_get_drvdata(dev); | |
72fd9fac HS |
328 | struct comedi_subdevice *s = comedi_get_write_subdevice(info); |
329 | unsigned int size; | |
330 | int err; | |
331 | ||
332 | err = kstrtouint(buf, 10, &size); | |
333 | if (err) | |
334 | return err; | |
335 | if (size > (UINT_MAX / 1024)) | |
a5011a26 | 336 | return -EINVAL; |
72fd9fac | 337 | size *= 1024; |
a5011a26 HS |
338 | |
339 | mutex_lock(&info->device->mutex); | |
72fd9fac HS |
340 | if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) |
341 | err = resize_async_buffer(info->device, s, s->async, size); | |
342 | else | |
343 | err = -EINVAL; | |
a5011a26 HS |
344 | mutex_unlock(&info->device->mutex); |
345 | ||
72fd9fac | 346 | return err ? err : count; |
a5011a26 HS |
347 | } |
348 | ||
fb60367d HS |
349 | static struct device_attribute comedi_dev_attrs[] = { |
350 | __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR, | |
351 | show_max_read_buffer_kb, store_max_read_buffer_kb), | |
352 | __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP, | |
353 | show_read_buffer_kb, store_read_buffer_kb), | |
354 | __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR, | |
355 | show_max_write_buffer_kb, store_max_write_buffer_kb), | |
356 | __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP, | |
357 | show_write_buffer_kb, store_write_buffer_kb), | |
358 | __ATTR_NULL | |
a5011a26 | 359 | }; |
ed9eccbe | 360 | |
2aae0076 HS |
361 | static void comedi_set_subdevice_runflags(struct comedi_subdevice *s, |
362 | unsigned mask, unsigned bits) | |
363 | { | |
364 | unsigned long flags; | |
365 | ||
366 | spin_lock_irqsave(&s->spin_lock, flags); | |
367 | s->runflags &= ~mask; | |
368 | s->runflags |= (bits & mask); | |
369 | spin_unlock_irqrestore(&s->spin_lock, flags); | |
370 | } | |
371 | ||
372 | /* | |
373 | This function restores a subdevice to an idle state. | |
374 | */ | |
375 | static void do_become_nonbusy(struct comedi_device *dev, | |
376 | struct comedi_subdevice *s) | |
377 | { | |
378 | struct comedi_async *async = s->async; | |
379 | ||
380 | comedi_set_subdevice_runflags(s, SRF_RUNNING, 0); | |
381 | if (async) { | |
382 | comedi_reset_async_buf(async); | |
383 | async->inttrig = NULL; | |
384 | kfree(async->cmd.chanlist); | |
385 | async->cmd.chanlist = NULL; | |
386 | } else { | |
387 | dev_err(dev->class_dev, | |
388 | "BUG: (?) do_become_nonbusy called with async=NULL\n"); | |
389 | } | |
390 | ||
391 | s->busy = NULL; | |
392 | } | |
393 | ||
394 | static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s) | |
395 | { | |
396 | int ret = 0; | |
397 | ||
398 | if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel) | |
399 | ret = s->cancel(dev, s); | |
400 | ||
401 | do_become_nonbusy(dev, s); | |
402 | ||
403 | return ret; | |
404 | } | |
405 | ||
406 | static int is_device_busy(struct comedi_device *dev) | |
407 | { | |
408 | struct comedi_subdevice *s; | |
409 | int i; | |
410 | ||
411 | if (!dev->attached) | |
412 | return 0; | |
413 | ||
414 | for (i = 0; i < dev->n_subdevices; i++) { | |
415 | s = &dev->subdevices[i]; | |
416 | if (s->busy) | |
417 | return 1; | |
418 | if (s->async && s->async->mmap_count) | |
419 | return 1; | |
420 | } | |
421 | ||
422 | return 0; | |
423 | } | |
424 | ||
ed9eccbe DS |
425 | /* |
426 | COMEDI_DEVCONFIG | |
427 | device config ioctl | |
428 | ||
429 | arg: | |
430 | pointer to devconfig structure | |
431 | ||
432 | reads: | |
433 | devconfig structure at arg | |
434 | ||
435 | writes: | |
436 | none | |
437 | */ | |
0a85b6f0 | 438 | static int do_devconfig_ioctl(struct comedi_device *dev, |
92d0127c | 439 | struct comedi_devconfig __user *arg) |
ed9eccbe | 440 | { |
0707bb04 | 441 | struct comedi_devconfig it; |
ed9eccbe DS |
442 | int ret; |
443 | unsigned char *aux_data = NULL; | |
444 | int aux_len; | |
445 | ||
446 | if (!capable(CAP_SYS_ADMIN)) | |
447 | return -EPERM; | |
448 | ||
449 | if (arg == NULL) { | |
450 | if (is_device_busy(dev)) | |
451 | return -EBUSY; | |
476b8477 | 452 | if (dev->attached) { |
ed9eccbe DS |
453 | struct module *driver_module = dev->driver->module; |
454 | comedi_device_detach(dev); | |
455 | module_put(driver_module); | |
456 | } | |
457 | return 0; | |
458 | } | |
459 | ||
0707bb04 | 460 | if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig))) |
ed9eccbe DS |
461 | return -EFAULT; |
462 | ||
463 | it.board_name[COMEDI_NAMELEN - 1] = 0; | |
464 | ||
465 | if (comedi_aux_data(it.options, 0) && | |
476b8477 | 466 | it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) { |
ed9eccbe DS |
467 | int bit_shift; |
468 | aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]; | |
469 | if (aux_len < 0) | |
470 | return -EFAULT; | |
471 | ||
472 | aux_data = vmalloc(aux_len); | |
473 | if (!aux_data) | |
474 | return -ENOMEM; | |
475 | ||
476 | if (copy_from_user(aux_data, | |
dc881f29 IA |
477 | (unsigned char __user * |
478 | )comedi_aux_data(it.options, 0), aux_len)) { | |
ed9eccbe DS |
479 | vfree(aux_data); |
480 | return -EFAULT; | |
481 | } | |
482 | it.options[COMEDI_DEVCONF_AUX_DATA_LO] = | |
476b8477 | 483 | (unsigned long)aux_data; |
ed9eccbe DS |
484 | if (sizeof(void *) > sizeof(int)) { |
485 | bit_shift = sizeof(int) * 8; | |
486 | it.options[COMEDI_DEVCONF_AUX_DATA_HI] = | |
476b8477 | 487 | ((unsigned long)aux_data) >> bit_shift; |
ed9eccbe DS |
488 | } else |
489 | it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0; | |
490 | } | |
491 | ||
492 | ret = comedi_device_attach(dev, &it); | |
476b8477 GKH |
493 | if (ret == 0) { |
494 | if (!try_module_get(dev->driver->module)) { | |
ed9eccbe | 495 | comedi_device_detach(dev); |
abae41e6 | 496 | ret = -ENOSYS; |
ed9eccbe DS |
497 | } |
498 | } | |
499 | ||
500 | if (aux_data) | |
501 | vfree(aux_data); | |
502 | ||
503 | return ret; | |
504 | } | |
505 | ||
506 | /* | |
507 | COMEDI_BUFCONFIG | |
508 | buffer configuration ioctl | |
509 | ||
510 | arg: | |
511 | pointer to bufconfig structure | |
512 | ||
513 | reads: | |
514 | bufconfig at arg | |
515 | ||
516 | writes: | |
517 | modified bufconfig at arg | |
518 | ||
519 | */ | |
92d0127c GKH |
520 | static int do_bufconfig_ioctl(struct comedi_device *dev, |
521 | struct comedi_bufconfig __user *arg) | |
ed9eccbe | 522 | { |
be6aba4a | 523 | struct comedi_bufconfig bc; |
d163679c | 524 | struct comedi_async *async; |
34c43922 | 525 | struct comedi_subdevice *s; |
883db3d9 | 526 | int retval = 0; |
ed9eccbe | 527 | |
be6aba4a | 528 | if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig))) |
ed9eccbe DS |
529 | return -EFAULT; |
530 | ||
531 | if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0) | |
532 | return -EINVAL; | |
533 | ||
b077f2cd | 534 | s = &dev->subdevices[bc.subdevice]; |
ed9eccbe DS |
535 | async = s->async; |
536 | ||
537 | if (!async) { | |
538 | DPRINTK("subdevice does not have async capability\n"); | |
539 | bc.size = 0; | |
540 | bc.maximum_size = 0; | |
541 | goto copyback; | |
542 | } | |
543 | ||
544 | if (bc.maximum_size) { | |
545 | if (!capable(CAP_SYS_ADMIN)) | |
546 | return -EPERM; | |
547 | ||
548 | async->max_bufsize = bc.maximum_size; | |
549 | } | |
550 | ||
551 | if (bc.size) { | |
883db3d9 FMH |
552 | retval = resize_async_buffer(dev, s, async, bc.size); |
553 | if (retval < 0) | |
554 | return retval; | |
ed9eccbe DS |
555 | } |
556 | ||
557 | bc.size = async->prealloc_bufsz; | |
558 | bc.maximum_size = async->max_bufsize; | |
559 | ||
476b8477 | 560 | copyback: |
be6aba4a | 561 | if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig))) |
ed9eccbe DS |
562 | return -EFAULT; |
563 | ||
564 | return 0; | |
565 | } | |
566 | ||
567 | /* | |
568 | COMEDI_DEVINFO | |
569 | device info ioctl | |
570 | ||
571 | arg: | |
572 | pointer to devinfo structure | |
573 | ||
574 | reads: | |
575 | none | |
576 | ||
577 | writes: | |
578 | devinfo structure | |
579 | ||
580 | */ | |
0a85b6f0 | 581 | static int do_devinfo_ioctl(struct comedi_device *dev, |
92d0127c GKH |
582 | struct comedi_devinfo __user *arg, |
583 | struct file *file) | |
ed9eccbe | 584 | { |
063db04b | 585 | struct comedi_devinfo devinfo; |
ed9eccbe | 586 | const unsigned minor = iminor(file->f_dentry->d_inode); |
476b8477 GKH |
587 | struct comedi_device_file_info *dev_file_info = |
588 | comedi_get_device_file_info(minor); | |
34c43922 | 589 | struct comedi_subdevice *read_subdev = |
476b8477 | 590 | comedi_get_read_subdevice(dev_file_info); |
34c43922 | 591 | struct comedi_subdevice *write_subdev = |
476b8477 | 592 | comedi_get_write_subdevice(dev_file_info); |
ed9eccbe DS |
593 | |
594 | memset(&devinfo, 0, sizeof(devinfo)); | |
595 | ||
596 | /* fill devinfo structure */ | |
597 | devinfo.version_code = COMEDI_VERSION_CODE; | |
598 | devinfo.n_subdevs = dev->n_subdevices; | |
819cbb12 VK |
599 | strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN); |
600 | strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN); | |
ed9eccbe | 601 | |
476b8477 | 602 | if (read_subdev) |
ed9eccbe | 603 | devinfo.read_subdevice = read_subdev - dev->subdevices; |
476b8477 | 604 | else |
ed9eccbe | 605 | devinfo.read_subdevice = -1; |
476b8477 GKH |
606 | |
607 | if (write_subdev) | |
ed9eccbe | 608 | devinfo.write_subdevice = write_subdev - dev->subdevices; |
476b8477 | 609 | else |
ed9eccbe | 610 | devinfo.write_subdevice = -1; |
ed9eccbe | 611 | |
063db04b | 612 | if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo))) |
ed9eccbe DS |
613 | return -EFAULT; |
614 | ||
615 | return 0; | |
616 | } | |
617 | ||
618 | /* | |
619 | COMEDI_SUBDINFO | |
620 | subdevice info ioctl | |
621 | ||
622 | arg: | |
623 | pointer to array of subdevice info structures | |
624 | ||
625 | reads: | |
626 | none | |
627 | ||
628 | writes: | |
629 | array of subdevice info structures at arg | |
630 | ||
631 | */ | |
0a85b6f0 | 632 | static int do_subdinfo_ioctl(struct comedi_device *dev, |
92d0127c | 633 | struct comedi_subdinfo __user *arg, void *file) |
ed9eccbe DS |
634 | { |
635 | int ret, i; | |
bd52efbb | 636 | struct comedi_subdinfo *tmp, *us; |
34c43922 | 637 | struct comedi_subdevice *s; |
ed9eccbe | 638 | |
0a85b6f0 MT |
639 | tmp = |
640 | kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo), | |
641 | GFP_KERNEL); | |
ed9eccbe DS |
642 | if (!tmp) |
643 | return -ENOMEM; | |
644 | ||
645 | /* fill subdinfo structs */ | |
646 | for (i = 0; i < dev->n_subdevices; i++) { | |
b077f2cd | 647 | s = &dev->subdevices[i]; |
ed9eccbe DS |
648 | us = tmp + i; |
649 | ||
650 | us->type = s->type; | |
651 | us->n_chan = s->n_chan; | |
652 | us->subd_flags = s->subdev_flags; | |
653 | if (comedi_get_subdevice_runflags(s) & SRF_RUNNING) | |
654 | us->subd_flags |= SDF_RUNNING; | |
655 | #define TIMER_nanosec 5 /* backwards compatibility */ | |
656 | us->timer_type = TIMER_nanosec; | |
657 | us->len_chanlist = s->len_chanlist; | |
658 | us->maxdata = s->maxdata; | |
659 | if (s->range_table) { | |
660 | us->range_type = | |
476b8477 | 661 | (i << 24) | (0 << 16) | (s->range_table->length); |
ed9eccbe DS |
662 | } else { |
663 | us->range_type = 0; /* XXX */ | |
664 | } | |
665 | us->flags = s->flags; | |
666 | ||
667 | if (s->busy) | |
668 | us->subd_flags |= SDF_BUSY; | |
669 | if (s->busy == file) | |
670 | us->subd_flags |= SDF_BUSY_OWNER; | |
671 | if (s->lock) | |
672 | us->subd_flags |= SDF_LOCKED; | |
673 | if (s->lock == file) | |
674 | us->subd_flags |= SDF_LOCK_OWNER; | |
675 | if (!s->maxdata && s->maxdata_list) | |
676 | us->subd_flags |= SDF_MAXDATA; | |
677 | if (s->flaglist) | |
678 | us->subd_flags |= SDF_FLAGS; | |
679 | if (s->range_table_list) | |
680 | us->subd_flags |= SDF_RANGETYPE; | |
681 | if (s->do_cmd) | |
682 | us->subd_flags |= SDF_CMD; | |
683 | ||
684 | if (s->insn_bits != &insn_inval) | |
685 | us->insn_bits_support = COMEDI_SUPPORTED; | |
686 | else | |
687 | us->insn_bits_support = COMEDI_UNSUPPORTED; | |
688 | ||
689 | us->settling_time_0 = s->settling_time_0; | |
690 | } | |
691 | ||
692 | ret = copy_to_user(arg, tmp, | |
bd52efbb | 693 | dev->n_subdevices * sizeof(struct comedi_subdinfo)); |
ed9eccbe DS |
694 | |
695 | kfree(tmp); | |
696 | ||
697 | return ret ? -EFAULT : 0; | |
698 | } | |
699 | ||
700 | /* | |
701 | COMEDI_CHANINFO | |
702 | subdevice info ioctl | |
703 | ||
704 | arg: | |
705 | pointer to chaninfo structure | |
706 | ||
707 | reads: | |
708 | chaninfo structure at arg | |
709 | ||
710 | writes: | |
711 | arrays at elements of chaninfo structure | |
712 | ||
713 | */ | |
0a85b6f0 | 714 | static int do_chaninfo_ioctl(struct comedi_device *dev, |
92d0127c | 715 | struct comedi_chaninfo __user *arg) |
ed9eccbe | 716 | { |
34c43922 | 717 | struct comedi_subdevice *s; |
a18b416d | 718 | struct comedi_chaninfo it; |
ed9eccbe | 719 | |
a18b416d | 720 | if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo))) |
ed9eccbe DS |
721 | return -EFAULT; |
722 | ||
723 | if (it.subdev >= dev->n_subdevices) | |
724 | return -EINVAL; | |
b077f2cd | 725 | s = &dev->subdevices[it.subdev]; |
ed9eccbe DS |
726 | |
727 | if (it.maxdata_list) { | |
728 | if (s->maxdata || !s->maxdata_list) | |
729 | return -EINVAL; | |
730 | if (copy_to_user(it.maxdata_list, s->maxdata_list, | |
790c5541 | 731 | s->n_chan * sizeof(unsigned int))) |
ed9eccbe DS |
732 | return -EFAULT; |
733 | } | |
734 | ||
735 | if (it.flaglist) { | |
736 | if (!s->flaglist) | |
737 | return -EINVAL; | |
738 | if (copy_to_user(it.flaglist, s->flaglist, | |
476b8477 | 739 | s->n_chan * sizeof(unsigned int))) |
ed9eccbe DS |
740 | return -EFAULT; |
741 | } | |
742 | ||
743 | if (it.rangelist) { | |
744 | int i; | |
745 | ||
746 | if (!s->range_table_list) | |
747 | return -EINVAL; | |
748 | for (i = 0; i < s->n_chan; i++) { | |
749 | int x; | |
750 | ||
751 | x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) | | |
476b8477 | 752 | (s->range_table_list[i]->length); |
81604d43 VK |
753 | if (put_user(x, it.rangelist + i)) |
754 | return -EFAULT; | |
ed9eccbe | 755 | } |
476b8477 GKH |
756 | #if 0 |
757 | if (copy_to_user(it.rangelist, s->range_type_list, | |
0a85b6f0 | 758 | s->n_chan * sizeof(unsigned int))) |
476b8477 GKH |
759 | return -EFAULT; |
760 | #endif | |
ed9eccbe DS |
761 | } |
762 | ||
763 | return 0; | |
764 | } | |
765 | ||
766 | /* | |
767 | COMEDI_BUFINFO | |
768 | buffer information ioctl | |
769 | ||
770 | arg: | |
771 | pointer to bufinfo structure | |
772 | ||
773 | reads: | |
774 | bufinfo at arg | |
775 | ||
776 | writes: | |
777 | modified bufinfo at arg | |
778 | ||
779 | */ | |
92d0127c | 780 | static int do_bufinfo_ioctl(struct comedi_device *dev, |
53fa827e | 781 | struct comedi_bufinfo __user *arg, void *file) |
ed9eccbe | 782 | { |
9aa5339a | 783 | struct comedi_bufinfo bi; |
34c43922 | 784 | struct comedi_subdevice *s; |
d163679c | 785 | struct comedi_async *async; |
ed9eccbe | 786 | |
9aa5339a | 787 | if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo))) |
ed9eccbe DS |
788 | return -EFAULT; |
789 | ||
790 | if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0) | |
791 | return -EINVAL; | |
792 | ||
b077f2cd | 793 | s = &dev->subdevices[bi.subdevice]; |
53fa827e IA |
794 | |
795 | if (s->lock && s->lock != file) | |
796 | return -EACCES; | |
797 | ||
ed9eccbe DS |
798 | async = s->async; |
799 | ||
800 | if (!async) { | |
801 | DPRINTK("subdevice does not have async capability\n"); | |
802 | bi.buf_write_ptr = 0; | |
803 | bi.buf_read_ptr = 0; | |
804 | bi.buf_write_count = 0; | |
805 | bi.buf_read_count = 0; | |
4772c018 IA |
806 | bi.bytes_read = 0; |
807 | bi.bytes_written = 0; | |
ed9eccbe DS |
808 | goto copyback; |
809 | } | |
53fa827e IA |
810 | if (!s->busy) { |
811 | bi.bytes_read = 0; | |
812 | bi.bytes_written = 0; | |
813 | goto copyback_position; | |
814 | } | |
815 | if (s->busy != file) | |
816 | return -EACCES; | |
ed9eccbe DS |
817 | |
818 | if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) { | |
819 | bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read); | |
820 | comedi_buf_read_free(async, bi.bytes_read); | |
821 | ||
822 | if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | | |
476b8477 GKH |
823 | SRF_RUNNING)) |
824 | && async->buf_write_count == async->buf_read_count) { | |
ed9eccbe DS |
825 | do_become_nonbusy(dev, s); |
826 | } | |
827 | } | |
828 | ||
829 | if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) { | |
830 | bi.bytes_written = | |
476b8477 | 831 | comedi_buf_write_alloc(async, bi.bytes_written); |
ed9eccbe DS |
832 | comedi_buf_write_free(async, bi.bytes_written); |
833 | } | |
834 | ||
53fa827e | 835 | copyback_position: |
ed9eccbe DS |
836 | bi.buf_write_count = async->buf_write_count; |
837 | bi.buf_write_ptr = async->buf_write_ptr; | |
838 | bi.buf_read_count = async->buf_read_count; | |
839 | bi.buf_read_ptr = async->buf_read_ptr; | |
840 | ||
476b8477 | 841 | copyback: |
9aa5339a | 842 | if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo))) |
ed9eccbe DS |
843 | return -EFAULT; |
844 | ||
845 | return 0; | |
846 | } | |
847 | ||
0a85b6f0 MT |
848 | static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn, |
849 | unsigned int *data, void *file); | |
ed9eccbe | 850 | /* |
20617f22 PDP |
851 | * COMEDI_INSNLIST |
852 | * synchronous instructions | |
ed9eccbe | 853 | * |
20617f22 PDP |
854 | * arg: |
855 | * pointer to sync cmd structure | |
ed9eccbe | 856 | * |
20617f22 PDP |
857 | * reads: |
858 | * sync cmd struct at arg | |
859 | * instruction list | |
860 | * data (for writes) | |
ed9eccbe | 861 | * |
20617f22 PDP |
862 | * writes: |
863 | * data (for reads) | |
ed9eccbe DS |
864 | */ |
865 | /* arbitrary limits */ | |
866 | #define MAX_SAMPLES 256 | |
92d0127c GKH |
867 | static int do_insnlist_ioctl(struct comedi_device *dev, |
868 | struct comedi_insnlist __user *arg, void *file) | |
ed9eccbe | 869 | { |
da613f4f | 870 | struct comedi_insnlist insnlist; |
90035c08 | 871 | struct comedi_insn *insns = NULL; |
790c5541 | 872 | unsigned int *data = NULL; |
ed9eccbe DS |
873 | int i = 0; |
874 | int ret = 0; | |
875 | ||
da613f4f | 876 | if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist))) |
ed9eccbe DS |
877 | return -EFAULT; |
878 | ||
790c5541 | 879 | data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL); |
ed9eccbe DS |
880 | if (!data) { |
881 | DPRINTK("kmalloc failed\n"); | |
882 | ret = -ENOMEM; | |
883 | goto error; | |
884 | } | |
885 | ||
0a85b6f0 | 886 | insns = |
dfd8ee92 | 887 | kcalloc(insnlist.n_insns, sizeof(struct comedi_insn), GFP_KERNEL); |
ed9eccbe DS |
888 | if (!insns) { |
889 | DPRINTK("kmalloc failed\n"); | |
890 | ret = -ENOMEM; | |
891 | goto error; | |
892 | } | |
893 | ||
894 | if (copy_from_user(insns, insnlist.insns, | |
90035c08 | 895 | sizeof(struct comedi_insn) * insnlist.n_insns)) { |
ed9eccbe DS |
896 | DPRINTK("copy_from_user failed\n"); |
897 | ret = -EFAULT; | |
898 | goto error; | |
899 | } | |
900 | ||
901 | for (i = 0; i < insnlist.n_insns; i++) { | |
902 | if (insns[i].n > MAX_SAMPLES) { | |
903 | DPRINTK("number of samples too large\n"); | |
904 | ret = -EINVAL; | |
905 | goto error; | |
906 | } | |
907 | if (insns[i].insn & INSN_MASK_WRITE) { | |
908 | if (copy_from_user(data, insns[i].data, | |
790c5541 | 909 | insns[i].n * sizeof(unsigned int))) { |
ed9eccbe DS |
910 | DPRINTK("copy_from_user failed\n"); |
911 | ret = -EFAULT; | |
912 | goto error; | |
913 | } | |
914 | } | |
915 | ret = parse_insn(dev, insns + i, data, file); | |
916 | if (ret < 0) | |
917 | goto error; | |
918 | if (insns[i].insn & INSN_MASK_READ) { | |
919 | if (copy_to_user(insns[i].data, data, | |
790c5541 | 920 | insns[i].n * sizeof(unsigned int))) { |
ed9eccbe DS |
921 | DPRINTK("copy_to_user failed\n"); |
922 | ret = -EFAULT; | |
923 | goto error; | |
924 | } | |
925 | } | |
926 | if (need_resched()) | |
927 | schedule(); | |
928 | } | |
929 | ||
476b8477 GKH |
930 | error: |
931 | kfree(insns); | |
932 | kfree(data); | |
ed9eccbe DS |
933 | |
934 | if (ret < 0) | |
935 | return ret; | |
936 | return i; | |
937 | } | |
938 | ||
0a85b6f0 MT |
939 | static int check_insn_config_length(struct comedi_insn *insn, |
940 | unsigned int *data) | |
ed9eccbe | 941 | { |
476b8477 GKH |
942 | if (insn->n < 1) |
943 | return -EINVAL; | |
ed9eccbe DS |
944 | |
945 | switch (data[0]) { | |
946 | case INSN_CONFIG_DIO_OUTPUT: | |
947 | case INSN_CONFIG_DIO_INPUT: | |
948 | case INSN_CONFIG_DISARM: | |
949 | case INSN_CONFIG_RESET: | |
950 | if (insn->n == 1) | |
951 | return 0; | |
952 | break; | |
953 | case INSN_CONFIG_ARM: | |
954 | case INSN_CONFIG_DIO_QUERY: | |
955 | case INSN_CONFIG_BLOCK_SIZE: | |
956 | case INSN_CONFIG_FILTER: | |
957 | case INSN_CONFIG_SERIAL_CLOCK: | |
958 | case INSN_CONFIG_BIDIRECTIONAL_DATA: | |
959 | case INSN_CONFIG_ALT_SOURCE: | |
960 | case INSN_CONFIG_SET_COUNTER_MODE: | |
961 | case INSN_CONFIG_8254_READ_STATUS: | |
962 | case INSN_CONFIG_SET_ROUTING: | |
963 | case INSN_CONFIG_GET_ROUTING: | |
964 | case INSN_CONFIG_GET_PWM_STATUS: | |
965 | case INSN_CONFIG_PWM_SET_PERIOD: | |
966 | case INSN_CONFIG_PWM_GET_PERIOD: | |
967 | if (insn->n == 2) | |
968 | return 0; | |
969 | break; | |
970 | case INSN_CONFIG_SET_GATE_SRC: | |
971 | case INSN_CONFIG_GET_GATE_SRC: | |
972 | case INSN_CONFIG_SET_CLOCK_SRC: | |
973 | case INSN_CONFIG_GET_CLOCK_SRC: | |
974 | case INSN_CONFIG_SET_OTHER_SRC: | |
975 | case INSN_CONFIG_GET_COUNTER_STATUS: | |
976 | case INSN_CONFIG_PWM_SET_H_BRIDGE: | |
977 | case INSN_CONFIG_PWM_GET_H_BRIDGE: | |
978 | case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE: | |
979 | if (insn->n == 3) | |
980 | return 0; | |
981 | break; | |
982 | case INSN_CONFIG_PWM_OUTPUT: | |
983 | case INSN_CONFIG_ANALOG_TRIG: | |
984 | if (insn->n == 5) | |
985 | return 0; | |
986 | break; | |
b0a2b6d8 IA |
987 | case INSN_CONFIG_DIGITAL_TRIG: |
988 | if (insn->n == 6) | |
989 | return 0; | |
990 | break; | |
0a85b6f0 MT |
991 | /* by default we allow the insn since we don't have checks for |
992 | * all possible cases yet */ | |
ed9eccbe | 993 | default: |
4f870fe6 IA |
994 | pr_warn("comedi: No check for data length of config insn id %i is implemented.\n", |
995 | data[0]); | |
996 | pr_warn("comedi: Add a check to %s in %s.\n", | |
997 | __func__, __FILE__); | |
998 | pr_warn("comedi: Assuming n=%i is correct.\n", insn->n); | |
ed9eccbe | 999 | return 0; |
ed9eccbe DS |
1000 | } |
1001 | return -EINVAL; | |
1002 | } | |
1003 | ||
0a85b6f0 MT |
1004 | static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn, |
1005 | unsigned int *data, void *file) | |
ed9eccbe | 1006 | { |
34c43922 | 1007 | struct comedi_subdevice *s; |
ed9eccbe DS |
1008 | int ret = 0; |
1009 | int i; | |
1010 | ||
1011 | if (insn->insn & INSN_MASK_SPECIAL) { | |
1012 | /* a non-subdevice instruction */ | |
1013 | ||
1014 | switch (insn->insn) { | |
1015 | case INSN_GTOD: | |
1016 | { | |
1017 | struct timeval tv; | |
1018 | ||
1019 | if (insn->n != 2) { | |
1020 | ret = -EINVAL; | |
1021 | break; | |
1022 | } | |
1023 | ||
1024 | do_gettimeofday(&tv); | |
1025 | data[0] = tv.tv_sec; | |
1026 | data[1] = tv.tv_usec; | |
1027 | ret = 2; | |
1028 | ||
1029 | break; | |
1030 | } | |
1031 | case INSN_WAIT: | |
1032 | if (insn->n != 1 || data[0] >= 100000) { | |
1033 | ret = -EINVAL; | |
1034 | break; | |
1035 | } | |
1036 | udelay(data[0] / 1000); | |
1037 | ret = 1; | |
1038 | break; | |
1039 | case INSN_INTTRIG: | |
1040 | if (insn->n != 1) { | |
1041 | ret = -EINVAL; | |
1042 | break; | |
1043 | } | |
1044 | if (insn->subdev >= dev->n_subdevices) { | |
1045 | DPRINTK("%d not usable subdevice\n", | |
1046 | insn->subdev); | |
1047 | ret = -EINVAL; | |
1048 | break; | |
1049 | } | |
b077f2cd | 1050 | s = &dev->subdevices[insn->subdev]; |
ed9eccbe DS |
1051 | if (!s->async) { |
1052 | DPRINTK("no async\n"); | |
1053 | ret = -EINVAL; | |
1054 | break; | |
1055 | } | |
1056 | if (!s->async->inttrig) { | |
1057 | DPRINTK("no inttrig\n"); | |
1058 | ret = -EAGAIN; | |
1059 | break; | |
1060 | } | |
5d06e3df | 1061 | ret = s->async->inttrig(dev, s, data[0]); |
ed9eccbe DS |
1062 | if (ret >= 0) |
1063 | ret = 1; | |
1064 | break; | |
1065 | default: | |
1066 | DPRINTK("invalid insn\n"); | |
1067 | ret = -EINVAL; | |
1068 | break; | |
1069 | } | |
1070 | } else { | |
1071 | /* a subdevice instruction */ | |
790c5541 | 1072 | unsigned int maxdata; |
ed9eccbe DS |
1073 | |
1074 | if (insn->subdev >= dev->n_subdevices) { | |
1075 | DPRINTK("subdevice %d out of range\n", insn->subdev); | |
1076 | ret = -EINVAL; | |
1077 | goto out; | |
1078 | } | |
b077f2cd | 1079 | s = &dev->subdevices[insn->subdev]; |
ed9eccbe DS |
1080 | |
1081 | if (s->type == COMEDI_SUBD_UNUSED) { | |
1082 | DPRINTK("%d not usable subdevice\n", insn->subdev); | |
1083 | ret = -EIO; | |
1084 | goto out; | |
1085 | } | |
1086 | ||
1087 | /* are we locked? (ioctl lock) */ | |
1088 | if (s->lock && s->lock != file) { | |
1089 | DPRINTK("device locked\n"); | |
1090 | ret = -EACCES; | |
1091 | goto out; | |
1092 | } | |
1093 | ||
0fd0ca75 | 1094 | ret = comedi_check_chanlist(s, 1, &insn->chanspec); |
476b8477 | 1095 | if (ret < 0) { |
ed9eccbe DS |
1096 | ret = -EINVAL; |
1097 | DPRINTK("bad chanspec\n"); | |
1098 | goto out; | |
1099 | } | |
1100 | ||
1101 | if (s->busy) { | |
1102 | ret = -EBUSY; | |
1103 | goto out; | |
1104 | } | |
1105 | /* This looks arbitrary. It is. */ | |
1106 | s->busy = &parse_insn; | |
1107 | switch (insn->insn) { | |
1108 | case INSN_READ: | |
1109 | ret = s->insn_read(dev, s, insn, data); | |
1110 | break; | |
1111 | case INSN_WRITE: | |
1112 | maxdata = s->maxdata_list | |
476b8477 GKH |
1113 | ? s->maxdata_list[CR_CHAN(insn->chanspec)] |
1114 | : s->maxdata; | |
ed9eccbe DS |
1115 | for (i = 0; i < insn->n; ++i) { |
1116 | if (data[i] > maxdata) { | |
1117 | ret = -EINVAL; | |
1118 | DPRINTK("bad data value(s)\n"); | |
1119 | break; | |
1120 | } | |
1121 | } | |
1122 | if (ret == 0) | |
1123 | ret = s->insn_write(dev, s, insn, data); | |
1124 | break; | |
1125 | case INSN_BITS: | |
1126 | if (insn->n != 2) { | |
1127 | ret = -EINVAL; | |
2f644ccf IA |
1128 | } else { |
1129 | /* Most drivers ignore the base channel in | |
1130 | * insn->chanspec. Fix this here if | |
1131 | * the subdevice has <= 32 channels. */ | |
1132 | unsigned int shift; | |
1133 | unsigned int orig_mask; | |
1134 | ||
1135 | orig_mask = data[0]; | |
1136 | if (s->n_chan <= 32) { | |
1137 | shift = CR_CHAN(insn->chanspec); | |
1138 | if (shift > 0) { | |
1139 | insn->chanspec = 0; | |
1140 | data[0] <<= shift; | |
1141 | data[1] <<= shift; | |
1142 | } | |
1143 | } else | |
1144 | shift = 0; | |
1145 | ret = s->insn_bits(dev, s, insn, data); | |
1146 | data[0] = orig_mask; | |
1147 | if (shift > 0) | |
1148 | data[1] >>= shift; | |
ed9eccbe | 1149 | } |
ed9eccbe DS |
1150 | break; |
1151 | case INSN_CONFIG: | |
1152 | ret = check_insn_config_length(insn, data); | |
1153 | if (ret) | |
1154 | break; | |
1155 | ret = s->insn_config(dev, s, insn, data); | |
1156 | break; | |
1157 | default: | |
1158 | ret = -EINVAL; | |
1159 | break; | |
1160 | } | |
1161 | ||
1162 | s->busy = NULL; | |
1163 | } | |
1164 | ||
476b8477 | 1165 | out: |
ed9eccbe DS |
1166 | return ret; |
1167 | } | |
1168 | ||
1169 | /* | |
20617f22 PDP |
1170 | * COMEDI_INSN |
1171 | * synchronous instructions | |
ed9eccbe | 1172 | * |
20617f22 PDP |
1173 | * arg: |
1174 | * pointer to insn | |
ed9eccbe | 1175 | * |
20617f22 PDP |
1176 | * reads: |
1177 | * struct comedi_insn struct at arg | |
1178 | * data (for writes) | |
ed9eccbe | 1179 | * |
20617f22 PDP |
1180 | * writes: |
1181 | * data (for reads) | |
ed9eccbe | 1182 | */ |
92d0127c GKH |
1183 | static int do_insn_ioctl(struct comedi_device *dev, |
1184 | struct comedi_insn __user *arg, void *file) | |
ed9eccbe | 1185 | { |
90035c08 | 1186 | struct comedi_insn insn; |
790c5541 | 1187 | unsigned int *data = NULL; |
ed9eccbe DS |
1188 | int ret = 0; |
1189 | ||
790c5541 | 1190 | data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL); |
ed9eccbe DS |
1191 | if (!data) { |
1192 | ret = -ENOMEM; | |
1193 | goto error; | |
1194 | } | |
1195 | ||
90035c08 | 1196 | if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) { |
ed9eccbe DS |
1197 | ret = -EFAULT; |
1198 | goto error; | |
1199 | } | |
1200 | ||
1201 | /* This is where the behavior of insn and insnlist deviate. */ | |
1202 | if (insn.n > MAX_SAMPLES) | |
1203 | insn.n = MAX_SAMPLES; | |
1204 | if (insn.insn & INSN_MASK_WRITE) { | |
21fe2eea M |
1205 | if (copy_from_user(data, |
1206 | insn.data, | |
1207 | insn.n * sizeof(unsigned int))) { | |
ed9eccbe DS |
1208 | ret = -EFAULT; |
1209 | goto error; | |
1210 | } | |
1211 | } | |
1212 | ret = parse_insn(dev, &insn, data, file); | |
1213 | if (ret < 0) | |
1214 | goto error; | |
1215 | if (insn.insn & INSN_MASK_READ) { | |
21fe2eea M |
1216 | if (copy_to_user(insn.data, |
1217 | data, | |
1218 | insn.n * sizeof(unsigned int))) { | |
ed9eccbe DS |
1219 | ret = -EFAULT; |
1220 | goto error; | |
1221 | } | |
1222 | } | |
1223 | ret = insn.n; | |
1224 | ||
476b8477 GKH |
1225 | error: |
1226 | kfree(data); | |
ed9eccbe DS |
1227 | |
1228 | return ret; | |
1229 | } | |
1230 | ||
92d0127c | 1231 | static int do_cmd_ioctl(struct comedi_device *dev, |
cbe01f72 | 1232 | struct comedi_cmd __user *arg, void *file) |
ed9eccbe | 1233 | { |
88bc0574 | 1234 | struct comedi_cmd cmd; |
34c43922 | 1235 | struct comedi_subdevice *s; |
d163679c | 1236 | struct comedi_async *async; |
ed9eccbe | 1237 | int ret = 0; |
95bc359f | 1238 | unsigned int __user *user_chanlist; |
ed9eccbe | 1239 | |
88bc0574 | 1240 | if (copy_from_user(&cmd, arg, sizeof(struct comedi_cmd))) { |
ed9eccbe DS |
1241 | DPRINTK("bad cmd address\n"); |
1242 | return -EFAULT; | |
1243 | } | |
476b8477 | 1244 | /* save user's chanlist pointer so it can be restored later */ |
95bc359f | 1245 | user_chanlist = (unsigned int __user *)cmd.chanlist; |
ed9eccbe | 1246 | |
88bc0574 HS |
1247 | if (cmd.subdev >= dev->n_subdevices) { |
1248 | DPRINTK("%d no such subdevice\n", cmd.subdev); | |
ed9eccbe DS |
1249 | return -ENODEV; |
1250 | } | |
1251 | ||
88bc0574 | 1252 | s = &dev->subdevices[cmd.subdev]; |
ed9eccbe DS |
1253 | async = s->async; |
1254 | ||
1255 | if (s->type == COMEDI_SUBD_UNUSED) { | |
88bc0574 | 1256 | DPRINTK("%d not valid subdevice\n", cmd.subdev); |
ed9eccbe DS |
1257 | return -EIO; |
1258 | } | |
1259 | ||
1260 | if (!s->do_cmd || !s->do_cmdtest || !s->async) { | |
1261 | DPRINTK("subdevice %i does not support commands\n", | |
88bc0574 | 1262 | cmd.subdev); |
ed9eccbe DS |
1263 | return -EIO; |
1264 | } | |
1265 | ||
1266 | /* are we locked? (ioctl lock) */ | |
1267 | if (s->lock && s->lock != file) { | |
1268 | DPRINTK("subdevice locked\n"); | |
1269 | return -EACCES; | |
1270 | } | |
1271 | ||
1272 | /* are we busy? */ | |
1273 | if (s->busy) { | |
1274 | DPRINTK("subdevice busy\n"); | |
1275 | return -EBUSY; | |
1276 | } | |
1277 | s->busy = file; | |
1278 | ||
1279 | /* make sure channel/gain list isn't too long */ | |
88bc0574 | 1280 | if (cmd.chanlist_len > s->len_chanlist) { |
ed9eccbe | 1281 | DPRINTK("channel/gain list too long %u > %d\n", |
88bc0574 | 1282 | cmd.chanlist_len, s->len_chanlist); |
ed9eccbe DS |
1283 | ret = -EINVAL; |
1284 | goto cleanup; | |
1285 | } | |
1286 | ||
1287 | /* make sure channel/gain list isn't too short */ | |
88bc0574 | 1288 | if (cmd.chanlist_len < 1) { |
ed9eccbe | 1289 | DPRINTK("channel/gain list too short %u < 1\n", |
88bc0574 | 1290 | cmd.chanlist_len); |
ed9eccbe DS |
1291 | ret = -EINVAL; |
1292 | goto cleanup; | |
1293 | } | |
1294 | ||
88bc0574 | 1295 | async->cmd = cmd; |
ed9eccbe DS |
1296 | async->cmd.data = NULL; |
1297 | /* load channel/gain list */ | |
1298 | async->cmd.chanlist = | |
476b8477 | 1299 | kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL); |
ed9eccbe DS |
1300 | if (!async->cmd.chanlist) { |
1301 | DPRINTK("allocation failed\n"); | |
1302 | ret = -ENOMEM; | |
1303 | goto cleanup; | |
1304 | } | |
1305 | ||
95bc359f | 1306 | if (copy_from_user(async->cmd.chanlist, user_chanlist, |
476b8477 | 1307 | async->cmd.chanlist_len * sizeof(int))) { |
ed9eccbe DS |
1308 | DPRINTK("fault reading chanlist\n"); |
1309 | ret = -EFAULT; | |
1310 | goto cleanup; | |
1311 | } | |
1312 | ||
1313 | /* make sure each element in channel/gain list is valid */ | |
21fe2eea M |
1314 | ret = comedi_check_chanlist(s, |
1315 | async->cmd.chanlist_len, | |
1316 | async->cmd.chanlist); | |
476b8477 | 1317 | if (ret < 0) { |
ed9eccbe DS |
1318 | DPRINTK("bad chanlist\n"); |
1319 | goto cleanup; | |
1320 | } | |
1321 | ||
1322 | ret = s->do_cmdtest(dev, s, &async->cmd); | |
1323 | ||
1324 | if (async->cmd.flags & TRIG_BOGUS || ret) { | |
1325 | DPRINTK("test returned %d\n", ret); | |
88bc0574 | 1326 | cmd = async->cmd; |
476b8477 | 1327 | /* restore chanlist pointer before copying back */ |
95bc359f | 1328 | cmd.chanlist = (unsigned int __force *)user_chanlist; |
88bc0574 HS |
1329 | cmd.data = NULL; |
1330 | if (copy_to_user(arg, &cmd, sizeof(struct comedi_cmd))) { | |
ed9eccbe DS |
1331 | DPRINTK("fault writing cmd\n"); |
1332 | ret = -EFAULT; | |
1333 | goto cleanup; | |
1334 | } | |
1335 | ret = -EAGAIN; | |
1336 | goto cleanup; | |
1337 | } | |
1338 | ||
1339 | if (!async->prealloc_bufsz) { | |
1340 | ret = -ENOMEM; | |
1341 | DPRINTK("no buffer (?)\n"); | |
1342 | goto cleanup; | |
1343 | } | |
1344 | ||
1345 | comedi_reset_async_buf(async); | |
1346 | ||
1347 | async->cb_mask = | |
476b8477 GKH |
1348 | COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR | |
1349 | COMEDI_CB_OVERFLOW; | |
1350 | if (async->cmd.flags & TRIG_WAKE_EOS) | |
ed9eccbe | 1351 | async->cb_mask |= COMEDI_CB_EOS; |
ed9eccbe DS |
1352 | |
1353 | comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING); | |
1354 | ||
ed9eccbe DS |
1355 | ret = s->do_cmd(dev, s); |
1356 | if (ret == 0) | |
1357 | return 0; | |
1358 | ||
476b8477 | 1359 | cleanup: |
ed9eccbe DS |
1360 | do_become_nonbusy(dev, s); |
1361 | ||
1362 | return ret; | |
1363 | } | |
1364 | ||
1365 | /* | |
1366 | COMEDI_CMDTEST | |
1367 | command testing ioctl | |
1368 | ||
1369 | arg: | |
1370 | pointer to cmd structure | |
1371 | ||
1372 | reads: | |
1373 | cmd structure at arg | |
1374 | channel/range list | |
1375 | ||
1376 | writes: | |
1377 | modified cmd structure at arg | |
1378 | ||
1379 | */ | |
92d0127c GKH |
1380 | static int do_cmdtest_ioctl(struct comedi_device *dev, |
1381 | struct comedi_cmd __user *arg, void *file) | |
ed9eccbe | 1382 | { |
f8348677 | 1383 | struct comedi_cmd cmd; |
34c43922 | 1384 | struct comedi_subdevice *s; |
ed9eccbe DS |
1385 | int ret = 0; |
1386 | unsigned int *chanlist = NULL; | |
95bc359f | 1387 | unsigned int __user *user_chanlist; |
ed9eccbe | 1388 | |
f8348677 | 1389 | if (copy_from_user(&cmd, arg, sizeof(struct comedi_cmd))) { |
ed9eccbe DS |
1390 | DPRINTK("bad cmd address\n"); |
1391 | return -EFAULT; | |
1392 | } | |
476b8477 | 1393 | /* save user's chanlist pointer so it can be restored later */ |
95bc359f | 1394 | user_chanlist = (unsigned int __user *)cmd.chanlist; |
ed9eccbe | 1395 | |
f8348677 HS |
1396 | if (cmd.subdev >= dev->n_subdevices) { |
1397 | DPRINTK("%d no such subdevice\n", cmd.subdev); | |
ed9eccbe DS |
1398 | return -ENODEV; |
1399 | } | |
1400 | ||
f8348677 | 1401 | s = &dev->subdevices[cmd.subdev]; |
ed9eccbe | 1402 | if (s->type == COMEDI_SUBD_UNUSED) { |
f8348677 | 1403 | DPRINTK("%d not valid subdevice\n", cmd.subdev); |
ed9eccbe DS |
1404 | return -EIO; |
1405 | } | |
1406 | ||
1407 | if (!s->do_cmd || !s->do_cmdtest) { | |
1408 | DPRINTK("subdevice %i does not support commands\n", | |
f8348677 | 1409 | cmd.subdev); |
ed9eccbe DS |
1410 | return -EIO; |
1411 | } | |
1412 | ||
1413 | /* make sure channel/gain list isn't too long */ | |
f8348677 | 1414 | if (cmd.chanlist_len > s->len_chanlist) { |
ed9eccbe | 1415 | DPRINTK("channel/gain list too long %d > %d\n", |
f8348677 | 1416 | cmd.chanlist_len, s->len_chanlist); |
ed9eccbe DS |
1417 | ret = -EINVAL; |
1418 | goto cleanup; | |
1419 | } | |
1420 | ||
1421 | /* load channel/gain list */ | |
f8348677 | 1422 | if (cmd.chanlist) { |
ed9eccbe | 1423 | chanlist = |
f8348677 | 1424 | kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL); |
ed9eccbe DS |
1425 | if (!chanlist) { |
1426 | DPRINTK("allocation failed\n"); | |
1427 | ret = -ENOMEM; | |
1428 | goto cleanup; | |
1429 | } | |
1430 | ||
95bc359f | 1431 | if (copy_from_user(chanlist, user_chanlist, |
f8348677 | 1432 | cmd.chanlist_len * sizeof(int))) { |
ed9eccbe DS |
1433 | DPRINTK("fault reading chanlist\n"); |
1434 | ret = -EFAULT; | |
1435 | goto cleanup; | |
1436 | } | |
1437 | ||
1438 | /* make sure each element in channel/gain list is valid */ | |
f8348677 | 1439 | ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist); |
476b8477 | 1440 | if (ret < 0) { |
ed9eccbe DS |
1441 | DPRINTK("bad chanlist\n"); |
1442 | goto cleanup; | |
1443 | } | |
1444 | ||
f8348677 | 1445 | cmd.chanlist = chanlist; |
ed9eccbe DS |
1446 | } |
1447 | ||
f8348677 | 1448 | ret = s->do_cmdtest(dev, s, &cmd); |
ed9eccbe | 1449 | |
476b8477 | 1450 | /* restore chanlist pointer before copying back */ |
95bc359f | 1451 | cmd.chanlist = (unsigned int __force *)user_chanlist; |
ed9eccbe | 1452 | |
f8348677 | 1453 | if (copy_to_user(arg, &cmd, sizeof(struct comedi_cmd))) { |
ed9eccbe DS |
1454 | DPRINTK("bad cmd address\n"); |
1455 | ret = -EFAULT; | |
1456 | goto cleanup; | |
1457 | } | |
476b8477 GKH |
1458 | cleanup: |
1459 | kfree(chanlist); | |
ed9eccbe DS |
1460 | |
1461 | return ret; | |
1462 | } | |
1463 | ||
1464 | /* | |
1465 | COMEDI_LOCK | |
1466 | lock subdevice | |
1467 | ||
1468 | arg: | |
1469 | subdevice number | |
1470 | ||
1471 | reads: | |
1472 | none | |
1473 | ||
1474 | writes: | |
1475 | none | |
1476 | ||
1477 | */ | |
1478 | ||
0a85b6f0 MT |
1479 | static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg, |
1480 | void *file) | |
ed9eccbe DS |
1481 | { |
1482 | int ret = 0; | |
1483 | unsigned long flags; | |
34c43922 | 1484 | struct comedi_subdevice *s; |
ed9eccbe DS |
1485 | |
1486 | if (arg >= dev->n_subdevices) | |
1487 | return -EINVAL; | |
b077f2cd | 1488 | s = &dev->subdevices[arg]; |
ed9eccbe | 1489 | |
5f74ea14 | 1490 | spin_lock_irqsave(&s->spin_lock, flags); |
476b8477 | 1491 | if (s->busy || s->lock) |
ed9eccbe | 1492 | ret = -EBUSY; |
476b8477 | 1493 | else |
ed9eccbe | 1494 | s->lock = file; |
5f74ea14 | 1495 | spin_unlock_irqrestore(&s->spin_lock, flags); |
ed9eccbe | 1496 | |
c5274ab0 | 1497 | #if 0 |
ed9eccbe DS |
1498 | if (ret < 0) |
1499 | return ret; | |
1500 | ||
ed9eccbe DS |
1501 | if (s->lock_f) |
1502 | ret = s->lock_f(dev, s); | |
1503 | #endif | |
1504 | ||
1505 | return ret; | |
1506 | } | |
1507 | ||
1508 | /* | |
1509 | COMEDI_UNLOCK | |
1510 | unlock subdevice | |
1511 | ||
1512 | arg: | |
1513 | subdevice number | |
1514 | ||
1515 | reads: | |
1516 | none | |
1517 | ||
1518 | writes: | |
1519 | none | |
1520 | ||
1521 | This function isn't protected by the semaphore, since | |
1522 | we already own the lock. | |
1523 | */ | |
0a85b6f0 MT |
1524 | static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg, |
1525 | void *file) | |
ed9eccbe | 1526 | { |
34c43922 | 1527 | struct comedi_subdevice *s; |
ed9eccbe DS |
1528 | |
1529 | if (arg >= dev->n_subdevices) | |
1530 | return -EINVAL; | |
b077f2cd | 1531 | s = &dev->subdevices[arg]; |
ed9eccbe DS |
1532 | |
1533 | if (s->busy) | |
1534 | return -EBUSY; | |
1535 | ||
1536 | if (s->lock && s->lock != file) | |
1537 | return -EACCES; | |
1538 | ||
1539 | if (s->lock == file) { | |
1540 | #if 0 | |
1541 | if (s->unlock) | |
1542 | s->unlock(dev, s); | |
1543 | #endif | |
1544 | ||
1545 | s->lock = NULL; | |
1546 | } | |
1547 | ||
1548 | return 0; | |
1549 | } | |
1550 | ||
1551 | /* | |
1552 | COMEDI_CANCEL | |
1553 | cancel acquisition ioctl | |
1554 | ||
1555 | arg: | |
1556 | subdevice number | |
1557 | ||
1558 | reads: | |
1559 | nothing | |
1560 | ||
1561 | writes: | |
1562 | nothing | |
1563 | ||
1564 | */ | |
0a85b6f0 MT |
1565 | static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg, |
1566 | void *file) | |
ed9eccbe | 1567 | { |
34c43922 | 1568 | struct comedi_subdevice *s; |
ed9eccbe DS |
1569 | |
1570 | if (arg >= dev->n_subdevices) | |
1571 | return -EINVAL; | |
b077f2cd | 1572 | s = &dev->subdevices[arg]; |
ed9eccbe DS |
1573 | if (s->async == NULL) |
1574 | return -EINVAL; | |
1575 | ||
1576 | if (s->lock && s->lock != file) | |
1577 | return -EACCES; | |
1578 | ||
1579 | if (!s->busy) | |
1580 | return 0; | |
1581 | ||
1582 | if (s->busy != file) | |
1583 | return -EBUSY; | |
1584 | ||
1585 | return do_cancel(dev, s); | |
1586 | } | |
1587 | ||
1588 | /* | |
1589 | COMEDI_POLL ioctl | |
1590 | instructs driver to synchronize buffers | |
1591 | ||
1592 | arg: | |
1593 | subdevice number | |
1594 | ||
1595 | reads: | |
1596 | nothing | |
1597 | ||
1598 | writes: | |
1599 | nothing | |
1600 | ||
1601 | */ | |
0a85b6f0 MT |
1602 | static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg, |
1603 | void *file) | |
ed9eccbe | 1604 | { |
34c43922 | 1605 | struct comedi_subdevice *s; |
ed9eccbe DS |
1606 | |
1607 | if (arg >= dev->n_subdevices) | |
1608 | return -EINVAL; | |
b077f2cd | 1609 | s = &dev->subdevices[arg]; |
ed9eccbe DS |
1610 | |
1611 | if (s->lock && s->lock != file) | |
1612 | return -EACCES; | |
1613 | ||
1614 | if (!s->busy) | |
1615 | return 0; | |
1616 | ||
1617 | if (s->busy != file) | |
1618 | return -EBUSY; | |
1619 | ||
1620 | if (s->poll) | |
1621 | return s->poll(dev, s); | |
1622 | ||
1623 | return -EINVAL; | |
1624 | } | |
1625 | ||
47db6d58 HS |
1626 | static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd, |
1627 | unsigned long arg) | |
1628 | { | |
1629 | const unsigned minor = iminor(file->f_dentry->d_inode); | |
4da5fa9a | 1630 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
47db6d58 HS |
1631 | int rc; |
1632 | ||
4da5fa9a | 1633 | if (!dev) |
47db6d58 | 1634 | return -ENODEV; |
47db6d58 HS |
1635 | |
1636 | mutex_lock(&dev->mutex); | |
1637 | ||
1638 | /* Device config is special, because it must work on | |
1639 | * an unconfigured device. */ | |
1640 | if (cmd == COMEDI_DEVCONFIG) { | |
1641 | rc = do_devconfig_ioctl(dev, | |
1642 | (struct comedi_devconfig __user *)arg); | |
1643 | goto done; | |
1644 | } | |
1645 | ||
1646 | if (!dev->attached) { | |
1647 | DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor); | |
1648 | rc = -ENODEV; | |
1649 | goto done; | |
1650 | } | |
1651 | ||
1652 | switch (cmd) { | |
1653 | case COMEDI_BUFCONFIG: | |
1654 | rc = do_bufconfig_ioctl(dev, | |
1655 | (struct comedi_bufconfig __user *)arg); | |
1656 | break; | |
1657 | case COMEDI_DEVINFO: | |
1658 | rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg, | |
1659 | file); | |
1660 | break; | |
1661 | case COMEDI_SUBDINFO: | |
1662 | rc = do_subdinfo_ioctl(dev, | |
1663 | (struct comedi_subdinfo __user *)arg, | |
1664 | file); | |
1665 | break; | |
1666 | case COMEDI_CHANINFO: | |
1667 | rc = do_chaninfo_ioctl(dev, (void __user *)arg); | |
1668 | break; | |
1669 | case COMEDI_RANGEINFO: | |
1670 | rc = do_rangeinfo_ioctl(dev, (void __user *)arg); | |
1671 | break; | |
1672 | case COMEDI_BUFINFO: | |
1673 | rc = do_bufinfo_ioctl(dev, | |
1674 | (struct comedi_bufinfo __user *)arg, | |
1675 | file); | |
1676 | break; | |
1677 | case COMEDI_LOCK: | |
1678 | rc = do_lock_ioctl(dev, arg, file); | |
1679 | break; | |
1680 | case COMEDI_UNLOCK: | |
1681 | rc = do_unlock_ioctl(dev, arg, file); | |
1682 | break; | |
1683 | case COMEDI_CANCEL: | |
1684 | rc = do_cancel_ioctl(dev, arg, file); | |
1685 | break; | |
1686 | case COMEDI_CMD: | |
1687 | rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file); | |
1688 | break; | |
1689 | case COMEDI_CMDTEST: | |
1690 | rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg, | |
1691 | file); | |
1692 | break; | |
1693 | case COMEDI_INSNLIST: | |
1694 | rc = do_insnlist_ioctl(dev, | |
1695 | (struct comedi_insnlist __user *)arg, | |
1696 | file); | |
1697 | break; | |
1698 | case COMEDI_INSN: | |
1699 | rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg, | |
1700 | file); | |
1701 | break; | |
1702 | case COMEDI_POLL: | |
1703 | rc = do_poll_ioctl(dev, arg, file); | |
1704 | break; | |
1705 | default: | |
1706 | rc = -ENOTTY; | |
1707 | break; | |
1708 | } | |
1709 | ||
1710 | done: | |
1711 | mutex_unlock(&dev->mutex); | |
1712 | return rc; | |
1713 | } | |
1714 | ||
df30b21c FV |
1715 | static void comedi_vm_open(struct vm_area_struct *area) |
1716 | { | |
1717 | struct comedi_async *async; | |
1718 | struct comedi_device *dev; | |
1719 | ||
1720 | async = area->vm_private_data; | |
1721 | dev = async->subdevice->device; | |
1722 | ||
1723 | mutex_lock(&dev->mutex); | |
1724 | async->mmap_count++; | |
1725 | mutex_unlock(&dev->mutex); | |
1726 | } | |
1727 | ||
1728 | static void comedi_vm_close(struct vm_area_struct *area) | |
ed9eccbe | 1729 | { |
d163679c | 1730 | struct comedi_async *async; |
71b5f4f1 | 1731 | struct comedi_device *dev; |
ed9eccbe DS |
1732 | |
1733 | async = area->vm_private_data; | |
1734 | dev = async->subdevice->device; | |
1735 | ||
1736 | mutex_lock(&dev->mutex); | |
1737 | async->mmap_count--; | |
1738 | mutex_unlock(&dev->mutex); | |
1739 | } | |
1740 | ||
1741 | static struct vm_operations_struct comedi_vm_ops = { | |
df30b21c FV |
1742 | .open = comedi_vm_open, |
1743 | .close = comedi_vm_close, | |
ed9eccbe DS |
1744 | }; |
1745 | ||
1746 | static int comedi_mmap(struct file *file, struct vm_area_struct *vma) | |
1747 | { | |
1748 | const unsigned minor = iminor(file->f_dentry->d_inode); | |
d163679c | 1749 | struct comedi_async *async = NULL; |
ed9eccbe DS |
1750 | unsigned long start = vma->vm_start; |
1751 | unsigned long size; | |
1752 | int n_pages; | |
1753 | int i; | |
1754 | int retval; | |
34c43922 | 1755 | struct comedi_subdevice *s; |
3ffab428 BP |
1756 | struct comedi_device_file_info *dev_file_info; |
1757 | struct comedi_device *dev; | |
1758 | ||
1759 | dev_file_info = comedi_get_device_file_info(minor); | |
1760 | if (dev_file_info == NULL) | |
70fe742c | 1761 | return -ENODEV; |
3ffab428 BP |
1762 | dev = dev_file_info->device; |
1763 | if (dev == NULL) | |
70fe742c | 1764 | return -ENODEV; |
ed9eccbe DS |
1765 | |
1766 | mutex_lock(&dev->mutex); | |
1767 | if (!dev->attached) { | |
1768 | DPRINTK("no driver configured on comedi%i\n", dev->minor); | |
1769 | retval = -ENODEV; | |
1770 | goto done; | |
1771 | } | |
476b8477 | 1772 | if (vma->vm_flags & VM_WRITE) |
ed9eccbe | 1773 | s = comedi_get_write_subdevice(dev_file_info); |
476b8477 | 1774 | else |
ed9eccbe | 1775 | s = comedi_get_read_subdevice(dev_file_info); |
476b8477 | 1776 | |
ed9eccbe DS |
1777 | if (s == NULL) { |
1778 | retval = -EINVAL; | |
1779 | goto done; | |
1780 | } | |
1781 | async = s->async; | |
1782 | if (async == NULL) { | |
1783 | retval = -EINVAL; | |
1784 | goto done; | |
1785 | } | |
1786 | ||
1787 | if (vma->vm_pgoff != 0) { | |
1788 | DPRINTK("comedi: mmap() offset must be 0.\n"); | |
1789 | retval = -EINVAL; | |
1790 | goto done; | |
1791 | } | |
1792 | ||
1793 | size = vma->vm_end - vma->vm_start; | |
1794 | if (size > async->prealloc_bufsz) { | |
1795 | retval = -EFAULT; | |
1796 | goto done; | |
1797 | } | |
1798 | if (size & (~PAGE_MASK)) { | |
1799 | retval = -EFAULT; | |
1800 | goto done; | |
1801 | } | |
1802 | ||
1803 | n_pages = size >> PAGE_SHIFT; | |
1804 | for (i = 0; i < n_pages; ++i) { | |
1805 | if (remap_pfn_range(vma, start, | |
0a85b6f0 MT |
1806 | page_to_pfn(virt_to_page |
1807 | (async->buf_page_list | |
1808 | [i].virt_addr)), PAGE_SIZE, | |
1809 | PAGE_SHARED)) { | |
ed9eccbe DS |
1810 | retval = -EAGAIN; |
1811 | goto done; | |
1812 | } | |
1813 | start += PAGE_SIZE; | |
1814 | } | |
1815 | ||
1816 | vma->vm_ops = &comedi_vm_ops; | |
1817 | vma->vm_private_data = async; | |
1818 | ||
1819 | async->mmap_count++; | |
1820 | ||
1821 | retval = 0; | |
476b8477 | 1822 | done: |
ed9eccbe DS |
1823 | mutex_unlock(&dev->mutex); |
1824 | return retval; | |
1825 | } | |
1826 | ||
1ae5062a | 1827 | static unsigned int comedi_poll(struct file *file, poll_table *wait) |
ed9eccbe DS |
1828 | { |
1829 | unsigned int mask = 0; | |
1830 | const unsigned minor = iminor(file->f_dentry->d_inode); | |
34c43922 BP |
1831 | struct comedi_subdevice *read_subdev; |
1832 | struct comedi_subdevice *write_subdev; | |
3ffab428 BP |
1833 | struct comedi_device_file_info *dev_file_info; |
1834 | struct comedi_device *dev; | |
1835 | dev_file_info = comedi_get_device_file_info(minor); | |
1836 | ||
1837 | if (dev_file_info == NULL) | |
70fe742c | 1838 | return -ENODEV; |
3ffab428 BP |
1839 | dev = dev_file_info->device; |
1840 | if (dev == NULL) | |
70fe742c | 1841 | return -ENODEV; |
ed9eccbe DS |
1842 | |
1843 | mutex_lock(&dev->mutex); | |
1844 | if (!dev->attached) { | |
1845 | DPRINTK("no driver configured on comedi%i\n", dev->minor); | |
1846 | mutex_unlock(&dev->mutex); | |
1847 | return 0; | |
1848 | } | |
1849 | ||
1850 | mask = 0; | |
1851 | read_subdev = comedi_get_read_subdevice(dev_file_info); | |
1852 | if (read_subdev) { | |
1853 | poll_wait(file, &read_subdev->async->wait_head, wait); | |
1854 | if (!read_subdev->busy | |
476b8477 GKH |
1855 | || comedi_buf_read_n_available(read_subdev->async) > 0 |
1856 | || !(comedi_get_subdevice_runflags(read_subdev) & | |
1857 | SRF_RUNNING)) { | |
ed9eccbe DS |
1858 | mask |= POLLIN | POLLRDNORM; |
1859 | } | |
1860 | } | |
1861 | write_subdev = comedi_get_write_subdevice(dev_file_info); | |
1862 | if (write_subdev) { | |
1863 | poll_wait(file, &write_subdev->async->wait_head, wait); | |
476b8477 GKH |
1864 | comedi_buf_write_alloc(write_subdev->async, |
1865 | write_subdev->async->prealloc_bufsz); | |
ed9eccbe | 1866 | if (!write_subdev->busy |
476b8477 GKH |
1867 | || !(comedi_get_subdevice_runflags(write_subdev) & |
1868 | SRF_RUNNING) | |
1869 | || comedi_buf_write_n_allocated(write_subdev->async) >= | |
1870 | bytes_per_sample(write_subdev->async->subdevice)) { | |
ed9eccbe DS |
1871 | mask |= POLLOUT | POLLWRNORM; |
1872 | } | |
1873 | } | |
1874 | ||
1875 | mutex_unlock(&dev->mutex); | |
1876 | return mask; | |
1877 | } | |
1878 | ||
92d0127c GKH |
1879 | static ssize_t comedi_write(struct file *file, const char __user *buf, |
1880 | size_t nbytes, loff_t *offset) | |
ed9eccbe | 1881 | { |
34c43922 | 1882 | struct comedi_subdevice *s; |
d163679c | 1883 | struct comedi_async *async; |
ed9eccbe DS |
1884 | int n, m, count = 0, retval = 0; |
1885 | DECLARE_WAITQUEUE(wait, current); | |
1886 | const unsigned minor = iminor(file->f_dentry->d_inode); | |
3ffab428 BP |
1887 | struct comedi_device_file_info *dev_file_info; |
1888 | struct comedi_device *dev; | |
1889 | dev_file_info = comedi_get_device_file_info(minor); | |
1890 | ||
1891 | if (dev_file_info == NULL) | |
70fe742c | 1892 | return -ENODEV; |
3ffab428 BP |
1893 | dev = dev_file_info->device; |
1894 | if (dev == NULL) | |
70fe742c | 1895 | return -ENODEV; |
ed9eccbe DS |
1896 | |
1897 | if (!dev->attached) { | |
1898 | DPRINTK("no driver configured on comedi%i\n", dev->minor); | |
1899 | retval = -ENODEV; | |
1900 | goto done; | |
1901 | } | |
1902 | ||
1903 | s = comedi_get_write_subdevice(dev_file_info); | |
1904 | if (s == NULL) { | |
1905 | retval = -EIO; | |
1906 | goto done; | |
1907 | } | |
1908 | async = s->async; | |
1909 | ||
1910 | if (!nbytes) { | |
1911 | retval = 0; | |
1912 | goto done; | |
1913 | } | |
1914 | if (!s->busy) { | |
1915 | retval = 0; | |
1916 | goto done; | |
1917 | } | |
1918 | if (s->busy != file) { | |
1919 | retval = -EACCES; | |
1920 | goto done; | |
1921 | } | |
1922 | add_wait_queue(&async->wait_head, &wait); | |
1923 | while (nbytes > 0 && !retval) { | |
1924 | set_current_state(TASK_INTERRUPTIBLE); | |
1925 | ||
d2611540 IA |
1926 | if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) { |
1927 | if (count == 0) { | |
1928 | if (comedi_get_subdevice_runflags(s) & | |
1929 | SRF_ERROR) { | |
1930 | retval = -EPIPE; | |
1931 | } else { | |
1932 | retval = 0; | |
1933 | } | |
1934 | do_become_nonbusy(dev, s); | |
1935 | } | |
1936 | break; | |
1937 | } | |
1938 | ||
ed9eccbe DS |
1939 | n = nbytes; |
1940 | ||
1941 | m = n; | |
476b8477 | 1942 | if (async->buf_write_ptr + m > async->prealloc_bufsz) |
ed9eccbe | 1943 | m = async->prealloc_bufsz - async->buf_write_ptr; |
ed9eccbe | 1944 | comedi_buf_write_alloc(async, async->prealloc_bufsz); |
476b8477 | 1945 | if (m > comedi_buf_write_n_allocated(async)) |
ed9eccbe | 1946 | m = comedi_buf_write_n_allocated(async); |
ed9eccbe DS |
1947 | if (m < n) |
1948 | n = m; | |
1949 | ||
1950 | if (n == 0) { | |
ed9eccbe DS |
1951 | if (file->f_flags & O_NONBLOCK) { |
1952 | retval = -EAGAIN; | |
1953 | break; | |
1954 | } | |
6a9ce6b6 | 1955 | schedule(); |
ed9eccbe DS |
1956 | if (signal_pending(current)) { |
1957 | retval = -ERESTARTSYS; | |
1958 | break; | |
1959 | } | |
476b8477 | 1960 | if (!s->busy) |
ed9eccbe | 1961 | break; |
ed9eccbe DS |
1962 | if (s->busy != file) { |
1963 | retval = -EACCES; | |
1964 | break; | |
1965 | } | |
1966 | continue; | |
1967 | } | |
1968 | ||
1969 | m = copy_from_user(async->prealloc_buf + async->buf_write_ptr, | |
476b8477 | 1970 | buf, n); |
ed9eccbe DS |
1971 | if (m) { |
1972 | n -= m; | |
1973 | retval = -EFAULT; | |
1974 | } | |
1975 | comedi_buf_write_free(async, n); | |
1976 | ||
1977 | count += n; | |
1978 | nbytes -= n; | |
1979 | ||
1980 | buf += n; | |
1981 | break; /* makes device work like a pipe */ | |
1982 | } | |
1983 | set_current_state(TASK_RUNNING); | |
1984 | remove_wait_queue(&async->wait_head, &wait); | |
1985 | ||
1986 | done: | |
476b8477 | 1987 | return count ? count : retval; |
ed9eccbe DS |
1988 | } |
1989 | ||
92d0127c | 1990 | static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes, |
6705b68d | 1991 | loff_t *offset) |
ed9eccbe | 1992 | { |
34c43922 | 1993 | struct comedi_subdevice *s; |
d163679c | 1994 | struct comedi_async *async; |
ed9eccbe DS |
1995 | int n, m, count = 0, retval = 0; |
1996 | DECLARE_WAITQUEUE(wait, current); | |
1997 | const unsigned minor = iminor(file->f_dentry->d_inode); | |
3ffab428 BP |
1998 | struct comedi_device_file_info *dev_file_info; |
1999 | struct comedi_device *dev; | |
2000 | dev_file_info = comedi_get_device_file_info(minor); | |
2001 | ||
2002 | if (dev_file_info == NULL) | |
70fe742c | 2003 | return -ENODEV; |
3ffab428 BP |
2004 | dev = dev_file_info->device; |
2005 | if (dev == NULL) | |
70fe742c | 2006 | return -ENODEV; |
ed9eccbe DS |
2007 | |
2008 | if (!dev->attached) { | |
2009 | DPRINTK("no driver configured on comedi%i\n", dev->minor); | |
2010 | retval = -ENODEV; | |
2011 | goto done; | |
2012 | } | |
2013 | ||
2014 | s = comedi_get_read_subdevice(dev_file_info); | |
2015 | if (s == NULL) { | |
2016 | retval = -EIO; | |
2017 | goto done; | |
2018 | } | |
2019 | async = s->async; | |
2020 | if (!nbytes) { | |
2021 | retval = 0; | |
2022 | goto done; | |
2023 | } | |
2024 | if (!s->busy) { | |
2025 | retval = 0; | |
2026 | goto done; | |
2027 | } | |
2028 | if (s->busy != file) { | |
2029 | retval = -EACCES; | |
2030 | goto done; | |
2031 | } | |
2032 | ||
2033 | add_wait_queue(&async->wait_head, &wait); | |
2034 | while (nbytes > 0 && !retval) { | |
2035 | set_current_state(TASK_INTERRUPTIBLE); | |
2036 | ||
2037 | n = nbytes; | |
2038 | ||
2039 | m = comedi_buf_read_n_available(async); | |
476b8477 GKH |
2040 | /* printk("%d available\n",m); */ |
2041 | if (async->buf_read_ptr + m > async->prealloc_bufsz) | |
ed9eccbe | 2042 | m = async->prealloc_bufsz - async->buf_read_ptr; |
476b8477 | 2043 | /* printk("%d contiguous\n",m); */ |
ed9eccbe DS |
2044 | if (m < n) |
2045 | n = m; | |
2046 | ||
2047 | if (n == 0) { | |
2048 | if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) { | |
2049 | do_become_nonbusy(dev, s); | |
2050 | if (comedi_get_subdevice_runflags(s) & | |
476b8477 | 2051 | SRF_ERROR) { |
ed9eccbe DS |
2052 | retval = -EPIPE; |
2053 | } else { | |
2054 | retval = 0; | |
2055 | } | |
2056 | break; | |
2057 | } | |
2058 | if (file->f_flags & O_NONBLOCK) { | |
2059 | retval = -EAGAIN; | |
2060 | break; | |
2061 | } | |
6a9ce6b6 | 2062 | schedule(); |
ed9eccbe DS |
2063 | if (signal_pending(current)) { |
2064 | retval = -ERESTARTSYS; | |
2065 | break; | |
2066 | } | |
ed9eccbe DS |
2067 | if (!s->busy) { |
2068 | retval = 0; | |
2069 | break; | |
2070 | } | |
2071 | if (s->busy != file) { | |
2072 | retval = -EACCES; | |
2073 | break; | |
2074 | } | |
2075 | continue; | |
2076 | } | |
2077 | m = copy_to_user(buf, async->prealloc_buf + | |
476b8477 | 2078 | async->buf_read_ptr, n); |
ed9eccbe DS |
2079 | if (m) { |
2080 | n -= m; | |
2081 | retval = -EFAULT; | |
2082 | } | |
2083 | ||
2084 | comedi_buf_read_alloc(async, n); | |
2085 | comedi_buf_read_free(async, n); | |
2086 | ||
2087 | count += n; | |
2088 | nbytes -= n; | |
2089 | ||
2090 | buf += n; | |
2091 | break; /* makes device work like a pipe */ | |
2092 | } | |
2093 | if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) && | |
476b8477 | 2094 | async->buf_read_count - async->buf_write_count == 0) { |
ed9eccbe DS |
2095 | do_become_nonbusy(dev, s); |
2096 | } | |
2097 | set_current_state(TASK_RUNNING); | |
2098 | remove_wait_queue(&async->wait_head, &wait); | |
2099 | ||
2100 | done: | |
476b8477 | 2101 | return count ? count : retval; |
ed9eccbe DS |
2102 | } |
2103 | ||
ed9eccbe DS |
2104 | static int comedi_open(struct inode *inode, struct file *file) |
2105 | { | |
ed9eccbe | 2106 | const unsigned minor = iminor(inode); |
4da5fa9a | 2107 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
97920071 | 2108 | |
4da5fa9a | 2109 | if (!dev) { |
ed9eccbe DS |
2110 | DPRINTK("invalid minor number\n"); |
2111 | return -ENODEV; | |
2112 | } | |
2113 | ||
2114 | /* This is slightly hacky, but we want module autoloading | |
2115 | * to work for root. | |
2116 | * case: user opens device, attached -> ok | |
2117 | * case: user opens device, unattached, in_request_module=0 -> autoload | |
2118 | * case: user opens device, unattached, in_request_module=1 -> fail | |
2119 | * case: root opens device, attached -> ok | |
2120 | * case: root opens device, unattached, in_request_module=1 -> ok | |
2121 | * (typically called from modprobe) | |
2122 | * case: root opens device, unattached, in_request_module=0 -> autoload | |
2123 | * | |
2124 | * The last could be changed to "-> ok", which would deny root | |
2125 | * autoloading. | |
2126 | */ | |
2127 | mutex_lock(&dev->mutex); | |
2128 | if (dev->attached) | |
2129 | goto ok; | |
a8f80e8f | 2130 | if (!capable(CAP_NET_ADMIN) && dev->in_request_module) { |
ed9eccbe DS |
2131 | DPRINTK("in request module\n"); |
2132 | mutex_unlock(&dev->mutex); | |
2133 | return -ENODEV; | |
2134 | } | |
a8f80e8f | 2135 | if (capable(CAP_NET_ADMIN) && dev->in_request_module) |
ed9eccbe DS |
2136 | goto ok; |
2137 | ||
2138 | dev->in_request_module = 1; | |
2139 | ||
ed9eccbe DS |
2140 | #ifdef CONFIG_KMOD |
2141 | mutex_unlock(&dev->mutex); | |
56d92c60 | 2142 | request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor); |
ed9eccbe DS |
2143 | mutex_lock(&dev->mutex); |
2144 | #endif | |
2145 | ||
2146 | dev->in_request_module = 0; | |
2147 | ||
a8f80e8f EP |
2148 | if (!dev->attached && !capable(CAP_NET_ADMIN)) { |
2149 | DPRINTK("not attached and not CAP_NET_ADMIN\n"); | |
ed9eccbe DS |
2150 | mutex_unlock(&dev->mutex); |
2151 | return -ENODEV; | |
2152 | } | |
2153 | ok: | |
2154 | __module_get(THIS_MODULE); | |
2155 | ||
2156 | if (dev->attached) { | |
2157 | if (!try_module_get(dev->driver->module)) { | |
2158 | module_put(THIS_MODULE); | |
2159 | mutex_unlock(&dev->mutex); | |
2160 | return -ENOSYS; | |
2161 | } | |
2162 | } | |
2163 | ||
3c17ba07 IA |
2164 | if (dev->attached && dev->use_count == 0 && dev->open) { |
2165 | int rc = dev->open(dev); | |
2166 | if (rc < 0) { | |
2167 | module_put(dev->driver->module); | |
2168 | module_put(THIS_MODULE); | |
2169 | mutex_unlock(&dev->mutex); | |
2170 | return rc; | |
2171 | } | |
2172 | } | |
ed9eccbe DS |
2173 | |
2174 | dev->use_count++; | |
2175 | ||
a5011a26 | 2176 | mutex_unlock(&dev->mutex); |
ed9eccbe | 2177 | |
a5011a26 | 2178 | return 0; |
ed9eccbe DS |
2179 | } |
2180 | ||
2aae0076 HS |
2181 | static int comedi_fasync(int fd, struct file *file, int on) |
2182 | { | |
2183 | const unsigned minor = iminor(file->f_dentry->d_inode); | |
4da5fa9a | 2184 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
2aae0076 | 2185 | |
4da5fa9a | 2186 | if (!dev) |
2aae0076 HS |
2187 | return -ENODEV; |
2188 | ||
2189 | return fasync_helper(fd, file, on, &dev->async_queue); | |
2190 | } | |
2191 | ||
a5011a26 | 2192 | static int comedi_close(struct inode *inode, struct file *file) |
ed9eccbe | 2193 | { |
a5011a26 | 2194 | const unsigned minor = iminor(inode); |
4da5fa9a | 2195 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
a5011a26 | 2196 | struct comedi_subdevice *s = NULL; |
ed9eccbe DS |
2197 | int i; |
2198 | ||
4da5fa9a | 2199 | if (!dev) |
a5011a26 | 2200 | return -ENODEV; |
ed9eccbe | 2201 | |
a5011a26 HS |
2202 | mutex_lock(&dev->mutex); |
2203 | ||
2204 | if (dev->subdevices) { | |
2205 | for (i = 0; i < dev->n_subdevices; i++) { | |
b077f2cd | 2206 | s = &dev->subdevices[i]; |
a5011a26 HS |
2207 | |
2208 | if (s->busy == file) | |
2209 | do_cancel(dev, s); | |
2210 | if (s->lock == file) | |
2211 | s->lock = NULL; | |
2212 | } | |
ed9eccbe | 2213 | } |
a5011a26 HS |
2214 | if (dev->attached && dev->use_count == 1 && dev->close) |
2215 | dev->close(dev); | |
2216 | ||
2217 | module_put(THIS_MODULE); | |
2218 | if (dev->attached) | |
2219 | module_put(dev->driver->module); | |
2220 | ||
2221 | dev->use_count--; | |
2222 | ||
2223 | mutex_unlock(&dev->mutex); | |
2224 | ||
2225 | if (file->f_flags & FASYNC) | |
2226 | comedi_fasync(-1, file, 0); | |
ed9eccbe DS |
2227 | |
2228 | return 0; | |
2229 | } | |
2230 | ||
8cb8aad7 | 2231 | static const struct file_operations comedi_fops = { |
a5011a26 HS |
2232 | .owner = THIS_MODULE, |
2233 | .unlocked_ioctl = comedi_unlocked_ioctl, | |
2234 | .compat_ioctl = comedi_compat_ioctl, | |
2235 | .open = comedi_open, | |
2236 | .release = comedi_close, | |
2237 | .read = comedi_read, | |
2238 | .write = comedi_write, | |
2239 | .mmap = comedi_mmap, | |
2240 | .poll = comedi_poll, | |
2241 | .fasync = comedi_fasync, | |
2242 | .llseek = noop_llseek, | |
2243 | }; | |
2244 | ||
8cb8aad7 | 2245 | static struct class *comedi_class; |
a5011a26 HS |
2246 | static struct cdev comedi_cdev; |
2247 | ||
2248 | static void comedi_cleanup_legacy_minors(void) | |
ed9eccbe | 2249 | { |
ed9eccbe DS |
2250 | unsigned i; |
2251 | ||
a5011a26 | 2252 | for (i = 0; i < comedi_num_legacy_minors; i++) |
883db3d9 | 2253 | comedi_free_board_minor(i); |
ed9eccbe DS |
2254 | } |
2255 | ||
a5011a26 | 2256 | static int __init comedi_init(void) |
ed9eccbe | 2257 | { |
a5011a26 HS |
2258 | int i; |
2259 | int retval; | |
ed9eccbe | 2260 | |
4f870fe6 | 2261 | pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n"); |
ed9eccbe | 2262 | |
a5011a26 HS |
2263 | if (comedi_num_legacy_minors < 0 || |
2264 | comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) { | |
4f870fe6 IA |
2265 | pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n", |
2266 | COMEDI_NUM_BOARD_MINORS); | |
a5011a26 | 2267 | return -EINVAL; |
ed9eccbe | 2268 | } |
c43435d7 | 2269 | |
a5011a26 HS |
2270 | /* |
2271 | * comedi is unusable if both comedi_autoconfig and | |
2272 | * comedi_num_legacy_minors are zero, so we might as well adjust the | |
2273 | * defaults in that case | |
2274 | */ | |
2275 | if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0) | |
2276 | comedi_num_legacy_minors = 16; | |
c43435d7 | 2277 | |
a5011a26 HS |
2278 | memset(comedi_file_info_table, 0, |
2279 | sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS); | |
ed9eccbe | 2280 | |
a5011a26 HS |
2281 | retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0), |
2282 | COMEDI_NUM_MINORS, "comedi"); | |
2283 | if (retval) | |
2284 | return -EIO; | |
2285 | cdev_init(&comedi_cdev, &comedi_fops); | |
2286 | comedi_cdev.owner = THIS_MODULE; | |
2287 | kobject_set_name(&comedi_cdev.kobj, "comedi"); | |
2288 | if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) { | |
2289 | unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), | |
2290 | COMEDI_NUM_MINORS); | |
2291 | return -EIO; | |
883db3d9 | 2292 | } |
a5011a26 HS |
2293 | comedi_class = class_create(THIS_MODULE, "comedi"); |
2294 | if (IS_ERR(comedi_class)) { | |
4f870fe6 | 2295 | pr_err("comedi: failed to create class\n"); |
a5011a26 HS |
2296 | cdev_del(&comedi_cdev); |
2297 | unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), | |
2298 | COMEDI_NUM_MINORS); | |
2299 | return PTR_ERR(comedi_class); | |
883db3d9 | 2300 | } |
a5011a26 | 2301 | |
fb60367d HS |
2302 | comedi_class->dev_attrs = comedi_dev_attrs; |
2303 | ||
a5011a26 HS |
2304 | /* XXX requires /proc interface */ |
2305 | comedi_proc_init(); | |
2306 | ||
2307 | /* create devices files for legacy/manual use */ | |
2308 | for (i = 0; i < comedi_num_legacy_minors; i++) { | |
2309 | int minor; | |
2310 | minor = comedi_alloc_board_minor(NULL); | |
2311 | if (minor < 0) { | |
2312 | comedi_cleanup_legacy_minors(); | |
2313 | cdev_del(&comedi_cdev); | |
2314 | unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), | |
2315 | COMEDI_NUM_MINORS); | |
2316 | return minor; | |
2317 | } | |
883db3d9 | 2318 | } |
a5011a26 HS |
2319 | |
2320 | return 0; | |
ed9eccbe DS |
2321 | } |
2322 | ||
a5011a26 | 2323 | static void __exit comedi_cleanup(void) |
ed9eccbe | 2324 | { |
a5011a26 | 2325 | int i; |
ed9eccbe | 2326 | |
a5011a26 HS |
2327 | comedi_cleanup_legacy_minors(); |
2328 | for (i = 0; i < COMEDI_NUM_MINORS; ++i) | |
2329 | BUG_ON(comedi_file_info_table[i]); | |
ed9eccbe | 2330 | |
a5011a26 HS |
2331 | class_destroy(comedi_class); |
2332 | cdev_del(&comedi_cdev); | |
2333 | unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS); | |
ed9eccbe | 2334 | |
a5011a26 | 2335 | comedi_proc_cleanup(); |
ed9eccbe DS |
2336 | } |
2337 | ||
a5011a26 HS |
2338 | module_init(comedi_init); |
2339 | module_exit(comedi_cleanup); | |
ed9eccbe | 2340 | |
a5011a26 HS |
2341 | void comedi_error(const struct comedi_device *dev, const char *s) |
2342 | { | |
4f870fe6 | 2343 | dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s); |
ed9eccbe | 2344 | } |
a5011a26 | 2345 | EXPORT_SYMBOL(comedi_error); |
883db3d9 | 2346 | |
a5011a26 | 2347 | void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s) |
883db3d9 | 2348 | { |
a5011a26 HS |
2349 | struct comedi_async *async = s->async; |
2350 | unsigned runflags = 0; | |
2351 | unsigned runflags_mask = 0; | |
883db3d9 | 2352 | |
a5011a26 | 2353 | /* DPRINTK("comedi_event 0x%x\n",mask); */ |
883db3d9 | 2354 | |
a5011a26 HS |
2355 | if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0) |
2356 | return; | |
2357 | ||
2358 | if (s-> | |
2359 | async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | | |
2360 | COMEDI_CB_OVERFLOW)) { | |
2361 | runflags_mask |= SRF_RUNNING; | |
883db3d9 | 2362 | } |
a5011a26 HS |
2363 | /* remember if an error event has occurred, so an error |
2364 | * can be returned the next time the user does a read() */ | |
2365 | if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) { | |
2366 | runflags_mask |= SRF_ERROR; | |
2367 | runflags |= SRF_ERROR; | |
883db3d9 | 2368 | } |
a5011a26 HS |
2369 | if (runflags_mask) { |
2370 | /*sets SRF_ERROR and SRF_RUNNING together atomically */ | |
2371 | comedi_set_subdevice_runflags(s, runflags_mask, runflags); | |
883db3d9 FMH |
2372 | } |
2373 | ||
a5011a26 HS |
2374 | if (async->cb_mask & s->async->events) { |
2375 | if (comedi_get_subdevice_runflags(s) & SRF_USER) { | |
2376 | wake_up_interruptible(&async->wait_head); | |
2377 | if (s->subdev_flags & SDF_CMD_READ) | |
2378 | kill_fasync(&dev->async_queue, SIGIO, POLL_IN); | |
2379 | if (s->subdev_flags & SDF_CMD_WRITE) | |
2380 | kill_fasync(&dev->async_queue, SIGIO, POLL_OUT); | |
2381 | } else { | |
2382 | if (async->cb_func) | |
2383 | async->cb_func(s->async->events, async->cb_arg); | |
2384 | } | |
2385 | } | |
2386 | s->async->events = 0; | |
883db3d9 | 2387 | } |
a5011a26 | 2388 | EXPORT_SYMBOL(comedi_event); |
883db3d9 | 2389 | |
a5011a26 | 2390 | unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s) |
883db3d9 | 2391 | { |
a5011a26 HS |
2392 | unsigned long flags; |
2393 | unsigned runflags; | |
883db3d9 | 2394 | |
a5011a26 HS |
2395 | spin_lock_irqsave(&s->spin_lock, flags); |
2396 | runflags = s->runflags; | |
2397 | spin_unlock_irqrestore(&s->spin_lock, flags); | |
2398 | return runflags; | |
883db3d9 | 2399 | } |
a5011a26 | 2400 | EXPORT_SYMBOL(comedi_get_subdevice_runflags); |
883db3d9 | 2401 | |
a5011a26 HS |
2402 | static void comedi_device_init(struct comedi_device *dev) |
2403 | { | |
2404 | memset(dev, 0, sizeof(struct comedi_device)); | |
2405 | spin_lock_init(&dev->spinlock); | |
2406 | mutex_init(&dev->mutex); | |
2407 | dev->minor = -1; | |
2408 | } | |
883db3d9 | 2409 | |
a5011a26 | 2410 | static void comedi_device_cleanup(struct comedi_device *dev) |
883db3d9 | 2411 | { |
a5011a26 HS |
2412 | if (dev == NULL) |
2413 | return; | |
2414 | mutex_lock(&dev->mutex); | |
2415 | comedi_device_detach(dev); | |
2416 | mutex_unlock(&dev->mutex); | |
2417 | mutex_destroy(&dev->mutex); | |
2418 | } | |
2419 | ||
2420 | int comedi_alloc_board_minor(struct device *hardware_device) | |
2421 | { | |
2422 | struct comedi_device_file_info *info; | |
2423 | struct device *csdev; | |
2424 | unsigned i; | |
a5011a26 HS |
2425 | |
2426 | info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL); | |
2427 | if (info == NULL) | |
2428 | return -ENOMEM; | |
2429 | info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL); | |
2430 | if (info->device == NULL) { | |
2431 | kfree(info); | |
2432 | return -ENOMEM; | |
2433 | } | |
2434 | info->hardware_device = hardware_device; | |
2435 | comedi_device_init(info->device); | |
2436 | spin_lock(&comedi_file_info_table_lock); | |
2437 | for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) { | |
2438 | if (comedi_file_info_table[i] == NULL) { | |
2439 | comedi_file_info_table[i] = info; | |
2440 | break; | |
2441 | } | |
2442 | } | |
2443 | spin_unlock(&comedi_file_info_table_lock); | |
2444 | if (i == COMEDI_NUM_BOARD_MINORS) { | |
2445 | comedi_device_cleanup(info->device); | |
2446 | kfree(info->device); | |
2447 | kfree(info); | |
4f870fe6 | 2448 | pr_err("comedi: error: ran out of minor numbers for board device files.\n"); |
a5011a26 HS |
2449 | return -EBUSY; |
2450 | } | |
2451 | info->device->minor = i; | |
2452 | csdev = device_create(comedi_class, hardware_device, | |
2453 | MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i); | |
2454 | if (!IS_ERR(csdev)) | |
2455 | info->device->class_dev = csdev; | |
2456 | dev_set_drvdata(csdev, info); | |
883db3d9 | 2457 | |
a5011a26 | 2458 | return i; |
883db3d9 FMH |
2459 | } |
2460 | ||
a5011a26 | 2461 | void comedi_free_board_minor(unsigned minor) |
883db3d9 | 2462 | { |
a5011a26 | 2463 | struct comedi_device_file_info *info; |
883db3d9 | 2464 | |
a5011a26 HS |
2465 | BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS); |
2466 | spin_lock(&comedi_file_info_table_lock); | |
2467 | info = comedi_file_info_table[minor]; | |
2468 | comedi_file_info_table[minor] = NULL; | |
2469 | spin_unlock(&comedi_file_info_table_lock); | |
883db3d9 | 2470 | |
a5011a26 HS |
2471 | if (info) { |
2472 | struct comedi_device *dev = info->device; | |
2473 | if (dev) { | |
2474 | if (dev->class_dev) { | |
2475 | device_destroy(comedi_class, | |
2476 | MKDEV(COMEDI_MAJOR, dev->minor)); | |
2477 | } | |
2478 | comedi_device_cleanup(dev); | |
2479 | kfree(dev); | |
2480 | } | |
2481 | kfree(info); | |
883db3d9 | 2482 | } |
883db3d9 FMH |
2483 | } |
2484 | ||
a5011a26 | 2485 | int comedi_find_board_minor(struct device *hardware_device) |
883db3d9 | 2486 | { |
a5011a26 HS |
2487 | int minor; |
2488 | struct comedi_device_file_info *info; | |
883db3d9 | 2489 | |
a5011a26 HS |
2490 | for (minor = 0; minor < COMEDI_NUM_BOARD_MINORS; minor++) { |
2491 | spin_lock(&comedi_file_info_table_lock); | |
2492 | info = comedi_file_info_table[minor]; | |
2493 | if (info && info->hardware_device == hardware_device) { | |
2494 | spin_unlock(&comedi_file_info_table_lock); | |
2495 | return minor; | |
2496 | } | |
2497 | spin_unlock(&comedi_file_info_table_lock); | |
883db3d9 | 2498 | } |
a5011a26 | 2499 | return -ENODEV; |
883db3d9 FMH |
2500 | } |
2501 | ||
a5011a26 HS |
2502 | int comedi_alloc_subdevice_minor(struct comedi_device *dev, |
2503 | struct comedi_subdevice *s) | |
883db3d9 | 2504 | { |
a5011a26 HS |
2505 | struct comedi_device_file_info *info; |
2506 | struct device *csdev; | |
2507 | unsigned i; | |
883db3d9 | 2508 | |
a5011a26 HS |
2509 | info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL); |
2510 | if (info == NULL) | |
2511 | return -ENOMEM; | |
2512 | info->device = dev; | |
2513 | info->read_subdevice = s; | |
2514 | info->write_subdevice = s; | |
2515 | spin_lock(&comedi_file_info_table_lock); | |
2516 | for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) { | |
2517 | if (comedi_file_info_table[i] == NULL) { | |
2518 | comedi_file_info_table[i] = info; | |
2519 | break; | |
2520 | } | |
2521 | } | |
2522 | spin_unlock(&comedi_file_info_table_lock); | |
2523 | if (i == COMEDI_NUM_MINORS) { | |
2524 | kfree(info); | |
4f870fe6 | 2525 | pr_err("comedi: error: ran out of minor numbers for board device files.\n"); |
a5011a26 HS |
2526 | return -EBUSY; |
2527 | } | |
2528 | s->minor = i; | |
2529 | csdev = device_create(comedi_class, dev->class_dev, | |
2530 | MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i", | |
2531 | dev->minor, (int)(s - dev->subdevices)); | |
2532 | if (!IS_ERR(csdev)) | |
2533 | s->class_dev = csdev; | |
2534 | dev_set_drvdata(csdev, info); | |
883db3d9 | 2535 | |
a5011a26 | 2536 | return i; |
883db3d9 FMH |
2537 | } |
2538 | ||
a5011a26 | 2539 | void comedi_free_subdevice_minor(struct comedi_subdevice *s) |
883db3d9 | 2540 | { |
a5011a26 | 2541 | struct comedi_device_file_info *info; |
883db3d9 | 2542 | |
a5011a26 HS |
2543 | if (s == NULL) |
2544 | return; | |
2545 | if (s->minor < 0) | |
2546 | return; | |
883db3d9 | 2547 | |
a5011a26 HS |
2548 | BUG_ON(s->minor >= COMEDI_NUM_MINORS); |
2549 | BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR); | |
883db3d9 | 2550 | |
a5011a26 HS |
2551 | spin_lock(&comedi_file_info_table_lock); |
2552 | info = comedi_file_info_table[s->minor]; | |
2553 | comedi_file_info_table[s->minor] = NULL; | |
2554 | spin_unlock(&comedi_file_info_table_lock); | |
883db3d9 | 2555 | |
a5011a26 HS |
2556 | if (s->class_dev) { |
2557 | device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor)); | |
2558 | s->class_dev = NULL; | |
883db3d9 | 2559 | } |
a5011a26 | 2560 | kfree(info); |
883db3d9 | 2561 | } |