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