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