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