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