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