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