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