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