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