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