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