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