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