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