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