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