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