]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/staging/comedi/comedi_fops.c
staging: comedi: COMEDI_BUFINFO: return -EPIPE for abnormal read
[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
8fc369ae
IA
689bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
690{
691 unsigned runflags = __comedi_get_subdevice_runflags(s);
692
693 return runflags & COMEDI_SRF_FREE_SPRIV;
694}
695
696/**
a3e39942
IA
697 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
698 * @s: COMEDI subdevice.
8fc369ae
IA
699 *
700 * Mark the subdevice as having a pointer to private data that can be
a3e39942
IA
701 * automatically freed when the COMEDI device is detached from the low-level
702 * driver.
8fc369ae
IA
703 */
704void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
705{
706 __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
707}
708EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
709
588ba6dc 710/**
a3e39942
IA
711 * comedi_alloc_spriv - Allocate memory for the subdevice private data
712 * @s: COMEDI subdevice.
713 * @size: Size of the memory to allocate.
588ba6dc 714 *
a3e39942
IA
715 * Allocate memory for the subdevice private data and point @s->private
716 * to it. The memory will be freed automatically when the COMEDI device
717 * is detached from the low-level driver.
718 *
719 * Return: A pointer to the allocated memory @s->private on success.
720 * Return NULL on failure.
588ba6dc 721 */
0480bcb9 722void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
588ba6dc 723{
0480bcb9
HS
724 s->private = kzalloc(size, GFP_KERNEL);
725 if (s->private)
8fc369ae 726 comedi_set_spriv_auto_free(s);
0480bcb9 727 return s->private;
588ba6dc 728}
0480bcb9 729EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
588ba6dc 730
2aae0076 731/*
a2aab8b4 732 * This function restores a subdevice to an idle state.
2aae0076
HS
733 */
734static void do_become_nonbusy(struct comedi_device *dev,
735 struct comedi_subdevice *s)
736{
737 struct comedi_async *async = s->async;
738
cc64ea42 739 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
2aae0076 740 if (async) {
fcc18a9a 741 comedi_buf_reset(s);
2aae0076
HS
742 async->inttrig = NULL;
743 kfree(async->cmd.chanlist);
744 async->cmd.chanlist = NULL;
8da8c86f 745 s->busy = NULL;
258c1dd7 746 wake_up_interruptible_all(&async->wait_head);
2aae0076
HS
747 } else {
748 dev_err(dev->class_dev,
749 "BUG: (?) do_become_nonbusy called with async=NULL\n");
8da8c86f 750 s->busy = NULL;
2aae0076 751 }
2aae0076
HS
752}
753
754static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
755{
756 int ret = 0;
757
f0124630 758 if (comedi_is_subdevice_running(s) && s->cancel)
2aae0076
HS
759 ret = s->cancel(dev, s);
760
761 do_become_nonbusy(dev, s);
762
763 return ret;
764}
765
d19db51a
IA
766void comedi_device_cancel_all(struct comedi_device *dev)
767{
768 struct comedi_subdevice *s;
769 int i;
770
771 if (!dev->attached)
772 return;
773
774 for (i = 0; i < dev->n_subdevices; i++) {
775 s = &dev->subdevices[i];
776 if (s->async)
777 do_cancel(dev, s);
778 }
779}
780
2aae0076
HS
781static int is_device_busy(struct comedi_device *dev)
782{
783 struct comedi_subdevice *s;
784 int i;
785
786 if (!dev->attached)
787 return 0;
788
789 for (i = 0; i < dev->n_subdevices; i++) {
790 s = &dev->subdevices[i];
791 if (s->busy)
792 return 1;
d4526ab4 793 if (s->async && comedi_buf_is_mmapped(s))
2aae0076
HS
794 return 1;
795 }
796
797 return 0;
798}
799
ed9eccbe 800/*
18e01b24
IA
801 * COMEDI_DEVCONFIG ioctl
802 * attaches (and configures) or detaches a legacy device
803 *
804 * arg:
805 * pointer to comedi_devconfig structure (NULL if detaching)
806 *
807 * reads:
808 * comedi_devconfig structure (if attaching)
809 *
810 * writes:
811 * nothing
812 */
0a85b6f0 813static int do_devconfig_ioctl(struct comedi_device *dev,
92d0127c 814 struct comedi_devconfig __user *arg)
ed9eccbe 815{
0707bb04 816 struct comedi_devconfig it;
ed9eccbe
DS
817
818 if (!capable(CAP_SYS_ADMIN))
819 return -EPERM;
820
88cc30cf 821 if (!arg) {
ed9eccbe
DS
822 if (is_device_busy(dev))
823 return -EBUSY;
476b8477 824 if (dev->attached) {
ed9eccbe 825 struct module *driver_module = dev->driver->module;
4bac39f6 826
ed9eccbe
DS
827 comedi_device_detach(dev);
828 module_put(driver_module);
829 }
830 return 0;
831 }
832
bc252fd1 833 if (copy_from_user(&it, arg, sizeof(it)))
ed9eccbe
DS
834 return -EFAULT;
835
836 it.board_name[COMEDI_NAMELEN - 1] = 0;
837
d1843132
HS
838 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
839 dev_warn(dev->class_dev,
840 "comedi_config --init_data is deprecated\n");
841 return -EINVAL;
ed9eccbe
DS
842 }
843
8ab4ed6e
IA
844 if (dev->minor >= comedi_num_legacy_minors)
845 /* don't re-use dynamically allocated comedi devices */
846 return -EBUSY;
847
b2a644b4
IA
848 /* This increments the driver module count on success. */
849 return comedi_device_attach(dev, &it);
ed9eccbe
DS
850}
851
852/*
18e01b24
IA
853 * COMEDI_BUFCONFIG ioctl
854 * buffer configuration
855 *
856 * arg:
857 * pointer to comedi_bufconfig structure
858 *
859 * reads:
860 * comedi_bufconfig structure
861 *
862 * writes:
863 * modified comedi_bufconfig structure
864 */
92d0127c
GKH
865static int do_bufconfig_ioctl(struct comedi_device *dev,
866 struct comedi_bufconfig __user *arg)
ed9eccbe 867{
be6aba4a 868 struct comedi_bufconfig bc;
d163679c 869 struct comedi_async *async;
34c43922 870 struct comedi_subdevice *s;
883db3d9 871 int retval = 0;
ed9eccbe 872
bc252fd1 873 if (copy_from_user(&bc, arg, sizeof(bc)))
ed9eccbe
DS
874 return -EFAULT;
875
11f80ddf 876 if (bc.subdevice >= dev->n_subdevices)
ed9eccbe
DS
877 return -EINVAL;
878
b077f2cd 879 s = &dev->subdevices[bc.subdevice];
ed9eccbe
DS
880 async = s->async;
881
882 if (!async) {
272850f0
HS
883 dev_dbg(dev->class_dev,
884 "subdevice does not have async capability\n");
ed9eccbe
DS
885 bc.size = 0;
886 bc.maximum_size = 0;
887 goto copyback;
888 }
889
890 if (bc.maximum_size) {
891 if (!capable(CAP_SYS_ADMIN))
892 return -EPERM;
893
894 async->max_bufsize = bc.maximum_size;
895 }
896
897 if (bc.size) {
482f0a21 898 retval = resize_async_buffer(dev, s, bc.size);
883db3d9
FMH
899 if (retval < 0)
900 return retval;
ed9eccbe
DS
901 }
902
903 bc.size = async->prealloc_bufsz;
904 bc.maximum_size = async->max_bufsize;
905
476b8477 906copyback:
bc252fd1 907 if (copy_to_user(arg, &bc, sizeof(bc)))
ed9eccbe
DS
908 return -EFAULT;
909
910 return 0;
911}
912
913/*
18e01b24
IA
914 * COMEDI_DEVINFO ioctl
915 * device info
916 *
917 * arg:
918 * pointer to comedi_devinfo structure
919 *
920 * reads:
921 * nothing
922 *
923 * writes:
924 * comedi_devinfo structure
925 */
0a85b6f0 926static int do_devinfo_ioctl(struct comedi_device *dev,
92d0127c
GKH
927 struct comedi_devinfo __user *arg,
928 struct file *file)
ed9eccbe 929{
0e700923
HS
930 struct comedi_subdevice *s;
931 struct comedi_devinfo devinfo;
ed9eccbe
DS
932
933 memset(&devinfo, 0, sizeof(devinfo));
934
935 /* fill devinfo structure */
936 devinfo.version_code = COMEDI_VERSION_CODE;
937 devinfo.n_subdevs = dev->n_subdevices;
819cbb12
VK
938 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
939 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
ed9eccbe 940
20f083c0 941 s = comedi_file_read_subdevice(file);
0e700923 942 if (s)
90a35c15 943 devinfo.read_subdevice = s->index;
476b8477 944 else
ed9eccbe 945 devinfo.read_subdevice = -1;
476b8477 946
20f083c0 947 s = comedi_file_write_subdevice(file);
0e700923 948 if (s)
90a35c15 949 devinfo.write_subdevice = s->index;
476b8477 950 else
ed9eccbe 951 devinfo.write_subdevice = -1;
ed9eccbe 952
bc252fd1 953 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
ed9eccbe
DS
954 return -EFAULT;
955
956 return 0;
957}
958
959/*
18e01b24
IA
960 * COMEDI_SUBDINFO ioctl
961 * subdevices info
962 *
963 * arg:
964 * pointer to array of comedi_subdinfo structures
965 *
966 * reads:
967 * nothing
968 *
969 * writes:
970 * array of comedi_subdinfo structures
971 */
0a85b6f0 972static int do_subdinfo_ioctl(struct comedi_device *dev,
92d0127c 973 struct comedi_subdinfo __user *arg, void *file)
ed9eccbe
DS
974{
975 int ret, i;
bd52efbb 976 struct comedi_subdinfo *tmp, *us;
34c43922 977 struct comedi_subdevice *s;
ed9eccbe 978
bc252fd1 979 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
ed9eccbe
DS
980 if (!tmp)
981 return -ENOMEM;
982
983 /* fill subdinfo structs */
984 for (i = 0; i < dev->n_subdevices; i++) {
b077f2cd 985 s = &dev->subdevices[i];
ed9eccbe
DS
986 us = tmp + i;
987
988 us->type = s->type;
989 us->n_chan = s->n_chan;
990 us->subd_flags = s->subdev_flags;
f0124630 991 if (comedi_is_subdevice_running(s))
ed9eccbe
DS
992 us->subd_flags |= SDF_RUNNING;
993#define TIMER_nanosec 5 /* backwards compatibility */
994 us->timer_type = TIMER_nanosec;
995 us->len_chanlist = s->len_chanlist;
996 us->maxdata = s->maxdata;
997 if (s->range_table) {
998 us->range_type =
476b8477 999 (i << 24) | (0 << 16) | (s->range_table->length);
ed9eccbe
DS
1000 } else {
1001 us->range_type = 0; /* XXX */
1002 }
ed9eccbe
DS
1003
1004 if (s->busy)
1005 us->subd_flags |= SDF_BUSY;
1006 if (s->busy == file)
1007 us->subd_flags |= SDF_BUSY_OWNER;
1008 if (s->lock)
1009 us->subd_flags |= SDF_LOCKED;
1010 if (s->lock == file)
1011 us->subd_flags |= SDF_LOCK_OWNER;
1012 if (!s->maxdata && s->maxdata_list)
1013 us->subd_flags |= SDF_MAXDATA;
ed9eccbe
DS
1014 if (s->range_table_list)
1015 us->subd_flags |= SDF_RANGETYPE;
1016 if (s->do_cmd)
1017 us->subd_flags |= SDF_CMD;
1018
1019 if (s->insn_bits != &insn_inval)
1020 us->insn_bits_support = COMEDI_SUPPORTED;
1021 else
1022 us->insn_bits_support = COMEDI_UNSUPPORTED;
ed9eccbe
DS
1023 }
1024
bc252fd1 1025 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
ed9eccbe
DS
1026
1027 kfree(tmp);
1028
1029 return ret ? -EFAULT : 0;
1030}
1031
1032/*
18e01b24
IA
1033 * COMEDI_CHANINFO ioctl
1034 * subdevice channel info
1035 *
1036 * arg:
1037 * pointer to comedi_chaninfo structure
1038 *
1039 * reads:
1040 * comedi_chaninfo structure
1041 *
1042 * writes:
1043 * array of maxdata values to chaninfo->maxdata_list if requested
1044 * array of range table lengths to chaninfo->range_table_list if requested
1045 */
0a85b6f0 1046static int do_chaninfo_ioctl(struct comedi_device *dev,
92d0127c 1047 struct comedi_chaninfo __user *arg)
ed9eccbe 1048{
34c43922 1049 struct comedi_subdevice *s;
a18b416d 1050 struct comedi_chaninfo it;
ed9eccbe 1051
bc252fd1 1052 if (copy_from_user(&it, arg, sizeof(it)))
ed9eccbe
DS
1053 return -EFAULT;
1054
1055 if (it.subdev >= dev->n_subdevices)
1056 return -EINVAL;
b077f2cd 1057 s = &dev->subdevices[it.subdev];
ed9eccbe
DS
1058
1059 if (it.maxdata_list) {
1060 if (s->maxdata || !s->maxdata_list)
1061 return -EINVAL;
1062 if (copy_to_user(it.maxdata_list, s->maxdata_list,
790c5541 1063 s->n_chan * sizeof(unsigned int)))
ed9eccbe
DS
1064 return -EFAULT;
1065 }
1066
64d9b1d2
IA
1067 if (it.flaglist)
1068 return -EINVAL; /* flaglist not supported */
ed9eccbe
DS
1069
1070 if (it.rangelist) {
1071 int i;
1072
1073 if (!s->range_table_list)
1074 return -EINVAL;
1075 for (i = 0; i < s->n_chan; i++) {
1076 int x;
1077
1078 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
476b8477 1079 (s->range_table_list[i]->length);
81604d43
VK
1080 if (put_user(x, it.rangelist + i))
1081 return -EFAULT;
ed9eccbe 1082 }
ed9eccbe
DS
1083 }
1084
1085 return 0;
1086}
1087
18e01b24
IA
1088/*
1089 * COMEDI_BUFINFO ioctl
1090 * buffer information
1091 *
1092 * arg:
1093 * pointer to comedi_bufinfo structure
1094 *
1095 * reads:
1096 * comedi_bufinfo structure
1097 *
1098 * writes:
1099 * modified comedi_bufinfo structure
1100 */
92d0127c 1101static int do_bufinfo_ioctl(struct comedi_device *dev,
53fa827e 1102 struct comedi_bufinfo __user *arg, void *file)
ed9eccbe 1103{
9aa5339a 1104 struct comedi_bufinfo bi;
34c43922 1105 struct comedi_subdevice *s;
d163679c 1106 struct comedi_async *async;
36a51170
IA
1107 unsigned int runflags;
1108 int retval = 0;
06578561 1109 bool become_nonbusy = false;
ed9eccbe 1110
bc252fd1 1111 if (copy_from_user(&bi, arg, sizeof(bi)))
ed9eccbe
DS
1112 return -EFAULT;
1113
7b1a5e2f 1114 if (bi.subdevice >= dev->n_subdevices)
ed9eccbe
DS
1115 return -EINVAL;
1116
b077f2cd 1117 s = &dev->subdevices[bi.subdevice];
53fa827e 1118
ed9eccbe
DS
1119 async = s->async;
1120
57c563bf
IA
1121 if (!async || s->busy != file)
1122 return -EINVAL;
ed9eccbe 1123
66c36502
IA
1124 if (!(async->cmd.flags & CMDF_WRITE)) {
1125 /* command was set up in "read" direction */
1126 if (bi.bytes_read) {
1127 comedi_buf_read_alloc(s, bi.bytes_read);
1128 bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read);
ed9eccbe 1129 }
36a51170
IA
1130 /*
1131 * If nothing left to read, and command has stopped, and
1132 * {"read" position not updated or command stopped normally},
1133 * then become non-busy.
1134 */
1135 runflags = comedi_get_subdevice_runflags(s);
1136 if (comedi_buf_read_n_available(s) == 0 &&
1137 !comedi_is_runflags_running(runflags) &&
1138 (bi.bytes_read == 0 ||
1139 !comedi_is_runflags_in_error(runflags))) {
f3aa8c0b 1140 become_nonbusy = true;
36a51170
IA
1141 if (comedi_is_runflags_in_error(runflags))
1142 retval = -EPIPE;
1143 }
66c36502
IA
1144 bi.bytes_written = 0;
1145 } else {
1146 /* command was set up in "write" direction */
bb0c6bfa
IA
1147 if (!comedi_is_subdevice_running(s))
1148 bi.bytes_written = 0;
66c36502
IA
1149 if (bi.bytes_written) {
1150 comedi_buf_write_alloc(s, bi.bytes_written);
1151 bi.bytes_written =
1152 comedi_buf_write_free(s, bi.bytes_written);
1153 }
1154 bi.bytes_read = 0;
ed9eccbe
DS
1155 }
1156
1157 bi.buf_write_count = async->buf_write_count;
1158 bi.buf_write_ptr = async->buf_write_ptr;
1159 bi.buf_read_count = async->buf_read_count;
1160 bi.buf_read_ptr = async->buf_read_ptr;
1161
06578561
IA
1162 if (become_nonbusy)
1163 do_become_nonbusy(dev, s);
1164
36a51170
IA
1165 if (retval)
1166 return retval;
1167
bc252fd1 1168 if (copy_to_user(arg, &bi, sizeof(bi)))
ed9eccbe
DS
1169 return -EFAULT;
1170
1171 return 0;
1172}
1173
0a85b6f0
MT
1174static int check_insn_config_length(struct comedi_insn *insn,
1175 unsigned int *data)
ed9eccbe 1176{
476b8477
GKH
1177 if (insn->n < 1)
1178 return -EINVAL;
ed9eccbe
DS
1179
1180 switch (data[0]) {
1181 case INSN_CONFIG_DIO_OUTPUT:
1182 case INSN_CONFIG_DIO_INPUT:
1183 case INSN_CONFIG_DISARM:
1184 case INSN_CONFIG_RESET:
1185 if (insn->n == 1)
1186 return 0;
1187 break;
1188 case INSN_CONFIG_ARM:
1189 case INSN_CONFIG_DIO_QUERY:
1190 case INSN_CONFIG_BLOCK_SIZE:
1191 case INSN_CONFIG_FILTER:
1192 case INSN_CONFIG_SERIAL_CLOCK:
1193 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1194 case INSN_CONFIG_ALT_SOURCE:
1195 case INSN_CONFIG_SET_COUNTER_MODE:
1196 case INSN_CONFIG_8254_READ_STATUS:
1197 case INSN_CONFIG_SET_ROUTING:
1198 case INSN_CONFIG_GET_ROUTING:
1199 case INSN_CONFIG_GET_PWM_STATUS:
1200 case INSN_CONFIG_PWM_SET_PERIOD:
1201 case INSN_CONFIG_PWM_GET_PERIOD:
1202 if (insn->n == 2)
1203 return 0;
1204 break;
1205 case INSN_CONFIG_SET_GATE_SRC:
1206 case INSN_CONFIG_GET_GATE_SRC:
1207 case INSN_CONFIG_SET_CLOCK_SRC:
1208 case INSN_CONFIG_GET_CLOCK_SRC:
1209 case INSN_CONFIG_SET_OTHER_SRC:
1210 case INSN_CONFIG_GET_COUNTER_STATUS:
1211 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1212 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1213 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1214 if (insn->n == 3)
1215 return 0;
1216 break;
1217 case INSN_CONFIG_PWM_OUTPUT:
1218 case INSN_CONFIG_ANALOG_TRIG:
1219 if (insn->n == 5)
1220 return 0;
1221 break;
b0a2b6d8
IA
1222 case INSN_CONFIG_DIGITAL_TRIG:
1223 if (insn->n == 6)
1224 return 0;
1225 break;
a2aab8b4
IA
1226 /*
1227 * by default we allow the insn since we don't have checks for
1228 * all possible cases yet
1229 */
ed9eccbe 1230 default:
c2ad078b 1231 pr_warn("No check for data length of config insn id %i is implemented\n",
4f870fe6 1232 data[0]);
c2ad078b
HS
1233 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1234 pr_warn("Assuming n=%i is correct\n", insn->n);
ed9eccbe 1235 return 0;
ed9eccbe
DS
1236 }
1237 return -EINVAL;
1238}
1239
0a85b6f0
MT
1240static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1241 unsigned int *data, void *file)
ed9eccbe 1242{
34c43922 1243 struct comedi_subdevice *s;
ed9eccbe
DS
1244 int ret = 0;
1245 int i;
1246
1247 if (insn->insn & INSN_MASK_SPECIAL) {
1248 /* a non-subdevice instruction */
1249
1250 switch (insn->insn) {
1251 case INSN_GTOD:
1252 {
1253 struct timeval tv;
1254
1255 if (insn->n != 2) {
1256 ret = -EINVAL;
1257 break;
1258 }
1259
1260 do_gettimeofday(&tv);
1261 data[0] = tv.tv_sec;
1262 data[1] = tv.tv_usec;
1263 ret = 2;
1264
1265 break;
1266 }
1267 case INSN_WAIT:
1268 if (insn->n != 1 || data[0] >= 100000) {
1269 ret = -EINVAL;
1270 break;
1271 }
1272 udelay(data[0] / 1000);
1273 ret = 1;
1274 break;
1275 case INSN_INTTRIG:
1276 if (insn->n != 1) {
1277 ret = -EINVAL;
1278 break;
1279 }
1280 if (insn->subdev >= dev->n_subdevices) {
272850f0
HS
1281 dev_dbg(dev->class_dev,
1282 "%d not usable subdevice\n",
ed9eccbe
DS
1283 insn->subdev);
1284 ret = -EINVAL;
1285 break;
1286 }
b077f2cd 1287 s = &dev->subdevices[insn->subdev];
ed9eccbe 1288 if (!s->async) {
272850f0 1289 dev_dbg(dev->class_dev, "no async\n");
ed9eccbe
DS
1290 ret = -EINVAL;
1291 break;
1292 }
1293 if (!s->async->inttrig) {
272850f0 1294 dev_dbg(dev->class_dev, "no inttrig\n");
ed9eccbe
DS
1295 ret = -EAGAIN;
1296 break;
1297 }
5d06e3df 1298 ret = s->async->inttrig(dev, s, data[0]);
ed9eccbe
DS
1299 if (ret >= 0)
1300 ret = 1;
1301 break;
1302 default:
272850f0 1303 dev_dbg(dev->class_dev, "invalid insn\n");
ed9eccbe
DS
1304 ret = -EINVAL;
1305 break;
1306 }
1307 } else {
1308 /* a subdevice instruction */
790c5541 1309 unsigned int maxdata;
ed9eccbe
DS
1310
1311 if (insn->subdev >= dev->n_subdevices) {
272850f0
HS
1312 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1313 insn->subdev);
ed9eccbe
DS
1314 ret = -EINVAL;
1315 goto out;
1316 }
b077f2cd 1317 s = &dev->subdevices[insn->subdev];
ed9eccbe
DS
1318
1319 if (s->type == COMEDI_SUBD_UNUSED) {
272850f0
HS
1320 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1321 insn->subdev);
ed9eccbe
DS
1322 ret = -EIO;
1323 goto out;
1324 }
1325
1326 /* are we locked? (ioctl lock) */
1327 if (s->lock && s->lock != file) {
272850f0 1328 dev_dbg(dev->class_dev, "device locked\n");
ed9eccbe
DS
1329 ret = -EACCES;
1330 goto out;
1331 }
1332
0fd0ca75 1333 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
476b8477 1334 if (ret < 0) {
ed9eccbe 1335 ret = -EINVAL;
272850f0 1336 dev_dbg(dev->class_dev, "bad chanspec\n");
ed9eccbe
DS
1337 goto out;
1338 }
1339
1340 if (s->busy) {
1341 ret = -EBUSY;
1342 goto out;
1343 }
1344 /* This looks arbitrary. It is. */
30cc9bd6 1345 s->busy = parse_insn;
ed9eccbe
DS
1346 switch (insn->insn) {
1347 case INSN_READ:
1348 ret = s->insn_read(dev, s, insn, data);
22ca19d9
HS
1349 if (ret == -ETIMEDOUT) {
1350 dev_dbg(dev->class_dev,
1351 "subdevice %d read instruction timed out\n",
1352 s->index);
1353 }
ed9eccbe
DS
1354 break;
1355 case INSN_WRITE:
1356 maxdata = s->maxdata_list
476b8477
GKH
1357 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1358 : s->maxdata;
ed9eccbe
DS
1359 for (i = 0; i < insn->n; ++i) {
1360 if (data[i] > maxdata) {
1361 ret = -EINVAL;
272850f0
HS
1362 dev_dbg(dev->class_dev,
1363 "bad data value(s)\n");
ed9eccbe
DS
1364 break;
1365 }
1366 }
22ca19d9 1367 if (ret == 0) {
ed9eccbe 1368 ret = s->insn_write(dev, s, insn, data);
22ca19d9
HS
1369 if (ret == -ETIMEDOUT) {
1370 dev_dbg(dev->class_dev,
1371 "subdevice %d write instruction timed out\n",
1372 s->index);
1373 }
1374 }
ed9eccbe
DS
1375 break;
1376 case INSN_BITS:
1377 if (insn->n != 2) {
1378 ret = -EINVAL;
2f644ccf 1379 } else {
a2aab8b4
IA
1380 /*
1381 * Most drivers ignore the base channel in
2f644ccf 1382 * insn->chanspec. Fix this here if
a2aab8b4
IA
1383 * the subdevice has <= 32 channels.
1384 */
36efbacd
HS
1385 unsigned int orig_mask = data[0];
1386 unsigned int shift = 0;
2f644ccf 1387
2f644ccf
IA
1388 if (s->n_chan <= 32) {
1389 shift = CR_CHAN(insn->chanspec);
1390 if (shift > 0) {
1391 insn->chanspec = 0;
1392 data[0] <<= shift;
1393 data[1] <<= shift;
1394 }
36efbacd 1395 }
2f644ccf
IA
1396 ret = s->insn_bits(dev, s, insn, data);
1397 data[0] = orig_mask;
1398 if (shift > 0)
1399 data[1] >>= shift;
ed9eccbe 1400 }
ed9eccbe
DS
1401 break;
1402 case INSN_CONFIG:
1403 ret = check_insn_config_length(insn, data);
1404 if (ret)
1405 break;
1406 ret = s->insn_config(dev, s, insn, data);
1407 break;
1408 default:
1409 ret = -EINVAL;
1410 break;
1411 }
1412
1413 s->busy = NULL;
1414 }
1415
476b8477 1416out:
ed9eccbe
DS
1417 return ret;
1418}
1419
5b6cbd87 1420/*
18e01b24
IA
1421 * COMEDI_INSNLIST ioctl
1422 * synchronous instruction list
5b6cbd87 1423 *
18e01b24
IA
1424 * arg:
1425 * pointer to comedi_insnlist structure
5b6cbd87 1426 *
18e01b24
IA
1427 * reads:
1428 * comedi_insnlist structure
1429 * array of comedi_insn structures from insnlist->insns pointer
1430 * data (for writes) from insns[].data pointers
5b6cbd87 1431 *
18e01b24
IA
1432 * writes:
1433 * data (for reads) to insns[].data pointers
5b6cbd87
HS
1434 */
1435/* arbitrary limits */
1436#define MAX_SAMPLES 256
1437static int do_insnlist_ioctl(struct comedi_device *dev,
1438 struct comedi_insnlist __user *arg, void *file)
1439{
1440 struct comedi_insnlist insnlist;
1441 struct comedi_insn *insns = NULL;
1442 unsigned int *data = NULL;
1443 int i = 0;
1444 int ret = 0;
1445
1446 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1447 return -EFAULT;
1448
f4a8f528 1449 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
5b6cbd87 1450 if (!data) {
5b6cbd87
HS
1451 ret = -ENOMEM;
1452 goto error;
1453 }
1454
1455 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1456 if (!insns) {
5b6cbd87
HS
1457 ret = -ENOMEM;
1458 goto error;
1459 }
1460
1461 if (copy_from_user(insns, insnlist.insns,
1462 sizeof(*insns) * insnlist.n_insns)) {
272850f0 1463 dev_dbg(dev->class_dev, "copy_from_user failed\n");
5b6cbd87
HS
1464 ret = -EFAULT;
1465 goto error;
1466 }
1467
1468 for (i = 0; i < insnlist.n_insns; i++) {
1469 if (insns[i].n > MAX_SAMPLES) {
272850f0
HS
1470 dev_dbg(dev->class_dev,
1471 "number of samples too large\n");
5b6cbd87
HS
1472 ret = -EINVAL;
1473 goto error;
1474 }
1475 if (insns[i].insn & INSN_MASK_WRITE) {
1476 if (copy_from_user(data, insns[i].data,
1477 insns[i].n * sizeof(unsigned int))) {
272850f0
HS
1478 dev_dbg(dev->class_dev,
1479 "copy_from_user failed\n");
5b6cbd87
HS
1480 ret = -EFAULT;
1481 goto error;
1482 }
1483 }
1484 ret = parse_insn(dev, insns + i, data, file);
1485 if (ret < 0)
1486 goto error;
1487 if (insns[i].insn & INSN_MASK_READ) {
1488 if (copy_to_user(insns[i].data, data,
1489 insns[i].n * sizeof(unsigned int))) {
272850f0
HS
1490 dev_dbg(dev->class_dev,
1491 "copy_to_user failed\n");
5b6cbd87
HS
1492 ret = -EFAULT;
1493 goto error;
1494 }
1495 }
1496 if (need_resched())
1497 schedule();
1498 }
1499
1500error:
1501 kfree(insns);
1502 kfree(data);
1503
1504 if (ret < 0)
1505 return ret;
1506 return i;
1507}
1508
ed9eccbe 1509/*
18e01b24
IA
1510 * COMEDI_INSN ioctl
1511 * synchronous instruction
ed9eccbe 1512 *
18e01b24
IA
1513 * arg:
1514 * pointer to comedi_insn structure
ed9eccbe 1515 *
18e01b24
IA
1516 * reads:
1517 * comedi_insn structure
1518 * data (for writes) from insn->data pointer
ed9eccbe 1519 *
18e01b24
IA
1520 * writes:
1521 * data (for reads) to insn->data pointer
ed9eccbe 1522 */
92d0127c
GKH
1523static int do_insn_ioctl(struct comedi_device *dev,
1524 struct comedi_insn __user *arg, void *file)
ed9eccbe 1525{
90035c08 1526 struct comedi_insn insn;
790c5541 1527 unsigned int *data = NULL;
ed9eccbe
DS
1528 int ret = 0;
1529
f4a8f528 1530 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
ed9eccbe
DS
1531 if (!data) {
1532 ret = -ENOMEM;
1533 goto error;
1534 }
1535
bc252fd1 1536 if (copy_from_user(&insn, arg, sizeof(insn))) {
ed9eccbe
DS
1537 ret = -EFAULT;
1538 goto error;
1539 }
1540
1541 /* This is where the behavior of insn and insnlist deviate. */
1542 if (insn.n > MAX_SAMPLES)
1543 insn.n = MAX_SAMPLES;
1544 if (insn.insn & INSN_MASK_WRITE) {
21fe2eea
M
1545 if (copy_from_user(data,
1546 insn.data,
1547 insn.n * sizeof(unsigned int))) {
ed9eccbe
DS
1548 ret = -EFAULT;
1549 goto error;
1550 }
1551 }
1552 ret = parse_insn(dev, &insn, data, file);
1553 if (ret < 0)
1554 goto error;
1555 if (insn.insn & INSN_MASK_READ) {
21fe2eea
M
1556 if (copy_to_user(insn.data,
1557 data,
1558 insn.n * sizeof(unsigned int))) {
ed9eccbe
DS
1559 ret = -EFAULT;
1560 goto error;
1561 }
1562 }
1563 ret = insn.n;
1564
476b8477
GKH
1565error:
1566 kfree(data);
ed9eccbe
DS
1567
1568 return ret;
1569}
1570
87ece583
HS
1571static int __comedi_get_user_cmd(struct comedi_device *dev,
1572 struct comedi_cmd __user *arg,
1573 struct comedi_cmd *cmd)
ed9eccbe 1574{
34c43922 1575 struct comedi_subdevice *s;
ed9eccbe 1576
87ece583 1577 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
272850f0 1578 dev_dbg(dev->class_dev, "bad cmd address\n");
ed9eccbe
DS
1579 return -EFAULT;
1580 }
ed9eccbe 1581
87ece583
HS
1582 if (cmd->subdev >= dev->n_subdevices) {
1583 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
ed9eccbe
DS
1584 return -ENODEV;
1585 }
1586
87ece583 1587 s = &dev->subdevices[cmd->subdev];
ed9eccbe
DS
1588
1589 if (s->type == COMEDI_SUBD_UNUSED) {
b2f48741
YD
1590 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1591 cmd->subdev);
ed9eccbe
DS
1592 return -EIO;
1593 }
1594
1595 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
272850f0 1596 dev_dbg(dev->class_dev,
b2f48741
YD
1597 "subdevice %d does not support commands\n",
1598 cmd->subdev);
ed9eccbe
DS
1599 return -EIO;
1600 }
1601
87ece583
HS
1602 /* make sure channel/gain list isn't too long */
1603 if (cmd->chanlist_len > s->len_chanlist) {
1604 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1605 cmd->chanlist_len, s->len_chanlist);
1606 return -EINVAL;
1607 }
1608
5d070cf2
IA
1609 /*
1610 * Set the CMDF_WRITE flag to the correct state if the subdevice
1611 * supports only "read" commands or only "write" commands.
1612 */
1613 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1614 case SDF_CMD_READ:
1615 cmd->flags &= ~CMDF_WRITE;
1616 break;
1617 case SDF_CMD_WRITE:
1618 cmd->flags |= CMDF_WRITE;
1619 break;
1620 default:
1621 break;
1622 }
1623
87ece583
HS
1624 return 0;
1625}
1626
c6cd0eef
HS
1627static int __comedi_get_user_chanlist(struct comedi_device *dev,
1628 struct comedi_subdevice *s,
1629 unsigned int __user *user_chanlist,
1630 struct comedi_cmd *cmd)
1631{
1632 unsigned int *chanlist;
1633 int ret;
1634
238b5ad8 1635 cmd->chanlist = NULL;
c6cd0eef
HS
1636 chanlist = memdup_user(user_chanlist,
1637 cmd->chanlist_len * sizeof(unsigned int));
1638 if (IS_ERR(chanlist))
1639 return PTR_ERR(chanlist);
1640
1641 /* make sure each element in channel/gain list is valid */
1642 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1643 if (ret < 0) {
1644 kfree(chanlist);
1645 return ret;
1646 }
1647
1648 cmd->chanlist = chanlist;
1649
1650 return 0;
1651}
1652
18e01b24
IA
1653/*
1654 * COMEDI_CMD ioctl
1655 * asynchronous acquisition command set-up
1656 *
1657 * arg:
1658 * pointer to comedi_cmd structure
1659 *
1660 * reads:
1661 * comedi_cmd structure
1662 * channel/range list from cmd->chanlist pointer
1663 *
1664 * writes:
1665 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1666 */
87ece583
HS
1667static int do_cmd_ioctl(struct comedi_device *dev,
1668 struct comedi_cmd __user *arg, void *file)
1669{
1670 struct comedi_cmd cmd;
1671 struct comedi_subdevice *s;
1672 struct comedi_async *async;
1673 unsigned int __user *user_chanlist;
1674 int ret;
1675
1676 /* get the user's cmd and do some simple validation */
1677 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1678 if (ret)
1679 return ret;
1680
1681 /* save user's chanlist pointer so it can be restored later */
1682 user_chanlist = (unsigned int __user *)cmd.chanlist;
1683
1684 s = &dev->subdevices[cmd.subdev];
1685 async = s->async;
1686
ed9eccbe
DS
1687 /* are we locked? (ioctl lock) */
1688 if (s->lock && s->lock != file) {
272850f0 1689 dev_dbg(dev->class_dev, "subdevice locked\n");
ed9eccbe
DS
1690 return -EACCES;
1691 }
1692
1693 /* are we busy? */
1694 if (s->busy) {
272850f0 1695 dev_dbg(dev->class_dev, "subdevice busy\n");
ed9eccbe
DS
1696 return -EBUSY;
1697 }
ed9eccbe 1698
ed9eccbe 1699 /* make sure channel/gain list isn't too short */
88bc0574 1700 if (cmd.chanlist_len < 1) {
272850f0 1701 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
88bc0574 1702 cmd.chanlist_len);
4b18f08b 1703 return -EINVAL;
ed9eccbe
DS
1704 }
1705
88bc0574 1706 async->cmd = cmd;
ed9eccbe 1707 async->cmd.data = NULL;
ed9eccbe 1708
c6cd0eef
HS
1709 /* load channel/gain list */
1710 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1711 if (ret)
ed9eccbe 1712 goto cleanup;
ed9eccbe
DS
1713
1714 ret = s->do_cmdtest(dev, s, &async->cmd);
1715
b0446a21 1716 if (async->cmd.flags & CMDF_BOGUS || ret) {
272850f0 1717 dev_dbg(dev->class_dev, "test returned %d\n", ret);
88bc0574 1718 cmd = async->cmd;
476b8477 1719 /* restore chanlist pointer before copying back */
95bc359f 1720 cmd.chanlist = (unsigned int __force *)user_chanlist;
88bc0574 1721 cmd.data = NULL;
bc252fd1 1722 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
272850f0 1723 dev_dbg(dev->class_dev, "fault writing cmd\n");
ed9eccbe
DS
1724 ret = -EFAULT;
1725 goto cleanup;
1726 }
1727 ret = -EAGAIN;
1728 goto cleanup;
1729 }
1730
1731 if (!async->prealloc_bufsz) {
1732 ret = -ENOMEM;
272850f0 1733 dev_dbg(dev->class_dev, "no buffer (?)\n");
ed9eccbe
DS
1734 goto cleanup;
1735 }
1736
fcc18a9a 1737 comedi_buf_reset(s);
ed9eccbe 1738
781f933c 1739 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
d8bff6e3 1740 if (async->cmd.flags & CMDF_WAKE_EOS)
ed9eccbe 1741 async->cb_mask |= COMEDI_CB_EOS;
ed9eccbe 1742
cc64ea42
IA
1743 comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1744 COMEDI_SRF_RUNNING);
ed9eccbe 1745
84bb0bcc
HS
1746 /*
1747 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1748 * race with comedi_read() or comedi_write().
1749 */
4b18f08b 1750 s->busy = file;
ed9eccbe
DS
1751 ret = s->do_cmd(dev, s);
1752 if (ret == 0)
1753 return 0;
1754
476b8477 1755cleanup:
ed9eccbe
DS
1756 do_become_nonbusy(dev, s);
1757
1758 return ret;
1759}
1760
1761/*
18e01b24 1762 * COMEDI_CMDTEST ioctl
69e98df7 1763 * asynchronous acquisition command testing
18e01b24
IA
1764 *
1765 * arg:
1766 * pointer to comedi_cmd structure
1767 *
1768 * reads:
1769 * comedi_cmd structure
1770 * channel/range list from cmd->chanlist pointer
1771 *
1772 * writes:
1773 * possibly modified comedi_cmd structure
1774 */
92d0127c
GKH
1775static int do_cmdtest_ioctl(struct comedi_device *dev,
1776 struct comedi_cmd __user *arg, void *file)
ed9eccbe 1777{
f8348677 1778 struct comedi_cmd cmd;
34c43922 1779 struct comedi_subdevice *s;
95bc359f 1780 unsigned int __user *user_chanlist;
87ece583
HS
1781 int ret;
1782
1783 /* get the user's cmd and do some simple validation */
1784 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1785 if (ret)
1786 return ret;
ed9eccbe 1787
476b8477 1788 /* save user's chanlist pointer so it can be restored later */
95bc359f 1789 user_chanlist = (unsigned int __user *)cmd.chanlist;
ed9eccbe 1790
f8348677 1791 s = &dev->subdevices[cmd.subdev];
ed9eccbe 1792
6cab7a37
IA
1793 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1794 if (user_chanlist) {
1795 /* load channel/gain list */
1796 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1797 if (ret)
1798 return ret;
1799 }
ed9eccbe 1800
f8348677 1801 ret = s->do_cmdtest(dev, s, &cmd);
ed9eccbe 1802
238b5ad8
IA
1803 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1804
476b8477 1805 /* restore chanlist pointer before copying back */
95bc359f 1806 cmd.chanlist = (unsigned int __force *)user_chanlist;
ed9eccbe 1807
bc252fd1 1808 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
272850f0 1809 dev_dbg(dev->class_dev, "bad cmd address\n");
ed9eccbe 1810 ret = -EFAULT;
ed9eccbe 1811 }
c6cd0eef 1812
ed9eccbe
DS
1813 return ret;
1814}
1815
1816/*
18e01b24
IA
1817 * COMEDI_LOCK ioctl
1818 * lock subdevice
1819 *
1820 * arg:
1821 * subdevice number
1822 *
1823 * reads:
1824 * nothing
1825 *
1826 * writes:
1827 * nothing
1828 */
c1a6eac1 1829static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
0a85b6f0 1830 void *file)
ed9eccbe
DS
1831{
1832 int ret = 0;
1833 unsigned long flags;
34c43922 1834 struct comedi_subdevice *s;
ed9eccbe
DS
1835
1836 if (arg >= dev->n_subdevices)
1837 return -EINVAL;
b077f2cd 1838 s = &dev->subdevices[arg];
ed9eccbe 1839
5f74ea14 1840 spin_lock_irqsave(&s->spin_lock, flags);
476b8477 1841 if (s->busy || s->lock)
ed9eccbe 1842 ret = -EBUSY;
476b8477 1843 else
ed9eccbe 1844 s->lock = file;
5f74ea14 1845 spin_unlock_irqrestore(&s->spin_lock, flags);
ed9eccbe 1846
ed9eccbe
DS
1847 return ret;
1848}
1849
1850/*
18e01b24
IA
1851 * COMEDI_UNLOCK ioctl
1852 * unlock subdevice
1853 *
1854 * arg:
1855 * subdevice number
1856 *
1857 * reads:
1858 * nothing
1859 *
1860 * writes:
1861 * nothing
1862 */
c1a6eac1 1863static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
0a85b6f0 1864 void *file)
ed9eccbe 1865{
34c43922 1866 struct comedi_subdevice *s;
ed9eccbe
DS
1867
1868 if (arg >= dev->n_subdevices)
1869 return -EINVAL;
b077f2cd 1870 s = &dev->subdevices[arg];
ed9eccbe
DS
1871
1872 if (s->busy)
1873 return -EBUSY;
1874
1875 if (s->lock && s->lock != file)
1876 return -EACCES;
1877
90ac0764 1878 if (s->lock == file)
ed9eccbe 1879 s->lock = NULL;
ed9eccbe
DS
1880
1881 return 0;
1882}
1883
1884/*
18e01b24
IA
1885 * COMEDI_CANCEL ioctl
1886 * cancel asynchronous acquisition
1887 *
1888 * arg:
1889 * subdevice number
1890 *
1891 * reads:
1892 * nothing
1893 *
1894 * writes:
1895 * nothing
1896 */
c1a6eac1 1897static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
0a85b6f0 1898 void *file)
ed9eccbe 1899{
34c43922 1900 struct comedi_subdevice *s;
ed9eccbe
DS
1901
1902 if (arg >= dev->n_subdevices)
1903 return -EINVAL;
b077f2cd 1904 s = &dev->subdevices[arg];
88cc30cf 1905 if (!s->async)
ed9eccbe
DS
1906 return -EINVAL;
1907
ed9eccbe
DS
1908 if (!s->busy)
1909 return 0;
1910
1911 if (s->busy != file)
1912 return -EBUSY;
1913
fe43ec53 1914 return do_cancel(dev, s);
ed9eccbe
DS
1915}
1916
1917/*
18e01b24
IA
1918 * COMEDI_POLL ioctl
1919 * instructs driver to synchronize buffers
1920 *
1921 * arg:
1922 * subdevice number
1923 *
1924 * reads:
1925 * nothing
1926 *
1927 * writes:
1928 * nothing
1929 */
c1a6eac1 1930static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
0a85b6f0 1931 void *file)
ed9eccbe 1932{
34c43922 1933 struct comedi_subdevice *s;
ed9eccbe
DS
1934
1935 if (arg >= dev->n_subdevices)
1936 return -EINVAL;
b077f2cd 1937 s = &dev->subdevices[arg];
ed9eccbe 1938
ed9eccbe
DS
1939 if (!s->busy)
1940 return 0;
1941
1942 if (s->busy != file)
1943 return -EBUSY;
1944
1945 if (s->poll)
1946 return s->poll(dev, s);
1947
1948 return -EINVAL;
1949}
1950
c299a678
IA
1951/*
1952 * COMEDI_SETRSUBD ioctl
1953 * sets the current "read" subdevice on a per-file basis
1954 *
1955 * arg:
1956 * subdevice number
1957 *
1958 * reads:
1959 * nothing
1960 *
1961 * writes:
1962 * nothing
1963 */
1964static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1965 struct file *file)
1966{
1967 struct comedi_file *cfp = file->private_data;
1968 struct comedi_subdevice *s_old, *s_new;
1969
1970 if (arg >= dev->n_subdevices)
1971 return -EINVAL;
1972
1973 s_new = &dev->subdevices[arg];
1974 s_old = comedi_file_read_subdevice(file);
1975 if (s_old == s_new)
1976 return 0; /* no change */
1977
1978 if (!(s_new->subdev_flags & SDF_CMD_READ))
1979 return -EINVAL;
1980
1981 /*
1982 * Check the file isn't still busy handling a "read" command on the
1983 * old subdevice (if any).
1984 */
1985 if (s_old && s_old->busy == file && s_old->async &&
1986 !(s_old->async->cmd.flags & CMDF_WRITE))
1987 return -EBUSY;
1988
1989 ACCESS_ONCE(cfp->read_subdev) = s_new;
1990 return 0;
1991}
1992
1993/*
1994 * COMEDI_SETWSUBD ioctl
1995 * sets the current "write" subdevice on a per-file basis
1996 *
1997 * arg:
1998 * subdevice number
1999 *
2000 * reads:
2001 * nothing
2002 *
2003 * writes:
2004 * nothing
2005 */
2006static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
2007 struct file *file)
2008{
2009 struct comedi_file *cfp = file->private_data;
2010 struct comedi_subdevice *s_old, *s_new;
2011
2012 if (arg >= dev->n_subdevices)
2013 return -EINVAL;
2014
2015 s_new = &dev->subdevices[arg];
2016 s_old = comedi_file_write_subdevice(file);
2017 if (s_old == s_new)
2018 return 0; /* no change */
2019
2020 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
2021 return -EINVAL;
2022
2023 /*
2024 * Check the file isn't still busy handling a "write" command on the
2025 * old subdevice (if any).
2026 */
2027 if (s_old && s_old->busy == file && s_old->async &&
2028 (s_old->async->cmd.flags & CMDF_WRITE))
2029 return -EBUSY;
2030
2031 ACCESS_ONCE(cfp->write_subdev) = s_new;
2032 return 0;
2033}
2034
47db6d58
HS
2035static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2036 unsigned long arg)
2037{
20f083c0
IA
2038 unsigned minor = iminor(file_inode(file));
2039 struct comedi_file *cfp = file->private_data;
2040 struct comedi_device *dev = cfp->dev;
47db6d58
HS
2041 int rc;
2042
47db6d58
HS
2043 mutex_lock(&dev->mutex);
2044
a2aab8b4
IA
2045 /*
2046 * Device config is special, because it must work on
2047 * an unconfigured device.
2048 */
47db6d58 2049 if (cmd == COMEDI_DEVCONFIG) {
754ab5c0
IA
2050 if (minor >= COMEDI_NUM_BOARD_MINORS) {
2051 /* Device config not appropriate on non-board minors. */
2052 rc = -ENOTTY;
2053 goto done;
2054 }
47db6d58
HS
2055 rc = do_devconfig_ioctl(dev,
2056 (struct comedi_devconfig __user *)arg);
8ab4ed6e
IA
2057 if (rc == 0) {
2058 if (arg == 0 &&
2059 dev->minor >= comedi_num_legacy_minors) {
a2aab8b4
IA
2060 /*
2061 * Successfully unconfigured a dynamically
2062 * allocated device. Try and remove it.
2063 */
db210da2 2064 if (comedi_clear_board_dev(dev)) {
8ab4ed6e 2065 mutex_unlock(&dev->mutex);
cb6b79de 2066 comedi_free_board_dev(dev);
8ab4ed6e
IA
2067 return rc;
2068 }
2069 }
2070 }
47db6d58
HS
2071 goto done;
2072 }
2073
2074 if (!dev->attached) {
272850f0 2075 dev_dbg(dev->class_dev, "no driver attached\n");
47db6d58
HS
2076 rc = -ENODEV;
2077 goto done;
2078 }
2079
2080 switch (cmd) {
2081 case COMEDI_BUFCONFIG:
2082 rc = do_bufconfig_ioctl(dev,
2083 (struct comedi_bufconfig __user *)arg);
2084 break;
2085 case COMEDI_DEVINFO:
2086 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2087 file);
2088 break;
2089 case COMEDI_SUBDINFO:
2090 rc = do_subdinfo_ioctl(dev,
2091 (struct comedi_subdinfo __user *)arg,
2092 file);
2093 break;
2094 case COMEDI_CHANINFO:
2095 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
2096 break;
2097 case COMEDI_RANGEINFO:
2098 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2099 break;
2100 case COMEDI_BUFINFO:
2101 rc = do_bufinfo_ioctl(dev,
2102 (struct comedi_bufinfo __user *)arg,
2103 file);
2104 break;
2105 case COMEDI_LOCK:
2106 rc = do_lock_ioctl(dev, arg, file);
2107 break;
2108 case COMEDI_UNLOCK:
2109 rc = do_unlock_ioctl(dev, arg, file);
2110 break;
2111 case COMEDI_CANCEL:
2112 rc = do_cancel_ioctl(dev, arg, file);
2113 break;
2114 case COMEDI_CMD:
2115 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2116 break;
2117 case COMEDI_CMDTEST:
2118 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2119 file);
2120 break;
2121 case COMEDI_INSNLIST:
2122 rc = do_insnlist_ioctl(dev,
2123 (struct comedi_insnlist __user *)arg,
2124 file);
2125 break;
2126 case COMEDI_INSN:
2127 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2128 file);
2129 break;
2130 case COMEDI_POLL:
2131 rc = do_poll_ioctl(dev, arg, file);
2132 break;
c299a678
IA
2133 case COMEDI_SETRSUBD:
2134 rc = do_setrsubd_ioctl(dev, arg, file);
2135 break;
2136 case COMEDI_SETWSUBD:
2137 rc = do_setwsubd_ioctl(dev, arg, file);
2138 break;
47db6d58
HS
2139 default:
2140 rc = -ENOTTY;
2141 break;
2142 }
2143
2144done:
2145 mutex_unlock(&dev->mutex);
2146 return rc;
2147}
2148
df30b21c
FV
2149static void comedi_vm_open(struct vm_area_struct *area)
2150{
af93da31 2151 struct comedi_buf_map *bm;
df30b21c 2152
af93da31
IA
2153 bm = area->vm_private_data;
2154 comedi_buf_map_get(bm);
df30b21c
FV
2155}
2156
2157static void comedi_vm_close(struct vm_area_struct *area)
ed9eccbe 2158{
af93da31 2159 struct comedi_buf_map *bm;
ed9eccbe 2160
af93da31
IA
2161 bm = area->vm_private_data;
2162 comedi_buf_map_put(bm);
ed9eccbe
DS
2163}
2164
7cbea8dc 2165static const struct vm_operations_struct comedi_vm_ops = {
df30b21c
FV
2166 .open = comedi_vm_open,
2167 .close = comedi_vm_close,
ed9eccbe
DS
2168};
2169
2170static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2171{
20f083c0
IA
2172 struct comedi_file *cfp = file->private_data;
2173 struct comedi_device *dev = cfp->dev;
a52840a9
HS
2174 struct comedi_subdevice *s;
2175 struct comedi_async *async;
b34aa86f 2176 struct comedi_buf_map *bm = NULL;
ed9eccbe
DS
2177 unsigned long start = vma->vm_start;
2178 unsigned long size;
2179 int n_pages;
2180 int i;
2181 int retval;
3ffab428 2182
b34aa86f
IA
2183 /*
2184 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2185 * and down-reading &dev->attach_lock should normally succeed without
2186 * contention unless the device is in the process of being attached
2187 * or detached.
2188 */
2189 if (!down_read_trylock(&dev->attach_lock))
2190 return -EAGAIN;
a52840a9 2191
ed9eccbe 2192 if (!dev->attached) {
272850f0 2193 dev_dbg(dev->class_dev, "no driver attached\n");
ed9eccbe
DS
2194 retval = -ENODEV;
2195 goto done;
2196 }
a52840a9 2197
476b8477 2198 if (vma->vm_flags & VM_WRITE)
20f083c0 2199 s = comedi_file_write_subdevice(file);
476b8477 2200 else
20f083c0 2201 s = comedi_file_read_subdevice(file);
a52840a9 2202 if (!s) {
ed9eccbe
DS
2203 retval = -EINVAL;
2204 goto done;
2205 }
a52840a9 2206
ed9eccbe 2207 async = s->async;
a52840a9 2208 if (!async) {
ed9eccbe
DS
2209 retval = -EINVAL;
2210 goto done;
2211 }
2212
2213 if (vma->vm_pgoff != 0) {
272850f0 2214 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
ed9eccbe
DS
2215 retval = -EINVAL;
2216 goto done;
2217 }
2218
2219 size = vma->vm_end - vma->vm_start;
2220 if (size > async->prealloc_bufsz) {
2221 retval = -EFAULT;
2222 goto done;
2223 }
2224 if (size & (~PAGE_MASK)) {
2225 retval = -EFAULT;
2226 goto done;
2227 }
2228
2229 n_pages = size >> PAGE_SHIFT;
b34aa86f
IA
2230
2231 /* get reference to current buf map (if any) */
2232 bm = comedi_buf_map_from_subdev_get(s);
af93da31
IA
2233 if (!bm || n_pages > bm->n_pages) {
2234 retval = -EINVAL;
2235 goto done;
2236 }
ed9eccbe 2237 for (i = 0; i < n_pages; ++i) {
af93da31 2238 struct comedi_buf_page *buf = &bm->page_list[i];
a52840a9 2239
ed9eccbe 2240 if (remap_pfn_range(vma, start,
a52840a9
HS
2241 page_to_pfn(virt_to_page(buf->virt_addr)),
2242 PAGE_SIZE, PAGE_SHARED)) {
ed9eccbe
DS
2243 retval = -EAGAIN;
2244 goto done;
2245 }
2246 start += PAGE_SIZE;
2247 }
2248
2249 vma->vm_ops = &comedi_vm_ops;
af93da31 2250 vma->vm_private_data = bm;
ed9eccbe 2251
af93da31 2252 vma->vm_ops->open(vma);
ed9eccbe
DS
2253
2254 retval = 0;
476b8477 2255done:
b34aa86f
IA
2256 up_read(&dev->attach_lock);
2257 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
ed9eccbe
DS
2258 return retval;
2259}
2260
1ae5062a 2261static unsigned int comedi_poll(struct file *file, poll_table *wait)
ed9eccbe
DS
2262{
2263 unsigned int mask = 0;
20f083c0
IA
2264 struct comedi_file *cfp = file->private_data;
2265 struct comedi_device *dev = cfp->dev;
322146d5 2266 struct comedi_subdevice *s, *s_read;
3ffab428 2267
d5eb3a74 2268 down_read(&dev->attach_lock);
ca081b1d 2269
ed9eccbe 2270 if (!dev->attached) {
272850f0 2271 dev_dbg(dev->class_dev, "no driver attached\n");
ca081b1d 2272 goto done;
ed9eccbe
DS
2273 }
2274
20f083c0 2275 s = comedi_file_read_subdevice(file);
322146d5 2276 s_read = s;
cc400e18 2277 if (s && s->async) {
ca081b1d 2278 poll_wait(file, &s->async->wait_head, wait);
3834234f 2279 if (s->busy != file || !comedi_is_subdevice_running(s) ||
662c722b 2280 (s->async->cmd.flags & CMDF_WRITE) ||
e9edef3a 2281 comedi_buf_read_n_available(s) > 0)
ed9eccbe 2282 mask |= POLLIN | POLLRDNORM;
ed9eccbe 2283 }
ca081b1d 2284
20f083c0 2285 s = comedi_file_write_subdevice(file);
cc400e18 2286 if (s && s->async) {
c39e050d 2287 unsigned int bps = comedi_bytes_per_sample(s);
ca081b1d 2288
322146d5
IA
2289 if (s != s_read)
2290 poll_wait(file, &s->async->wait_head, wait);
3834234f 2291 if (s->busy != file || !comedi_is_subdevice_running(s) ||
662c722b 2292 !(s->async->cmd.flags & CMDF_WRITE) ||
ecf04ed3 2293 comedi_buf_write_n_available(s) >= bps)
ed9eccbe 2294 mask |= POLLOUT | POLLWRNORM;
ed9eccbe
DS
2295 }
2296
ca081b1d 2297done:
d5eb3a74 2298 up_read(&dev->attach_lock);
ed9eccbe
DS
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;
84a185ec
IA
2307 unsigned int n, m;
2308 ssize_t count = 0;
2309 int retval = 0;
ed9eccbe 2310 DECLARE_WAITQUEUE(wait, current);
20f083c0
IA
2311 struct comedi_file *cfp = file->private_data;
2312 struct comedi_device *dev = cfp->dev;
06181de1 2313 bool become_nonbusy = false;
9329f139
IA
2314 bool attach_locked;
2315 unsigned int old_detach_count;
3ffab428 2316
9329f139
IA
2317 /* Protect against device detachment during operation. */
2318 down_read(&dev->attach_lock);
2319 attach_locked = true;
2320 old_detach_count = dev->detach_count;
2321
ed9eccbe 2322 if (!dev->attached) {
272850f0 2323 dev_dbg(dev->class_dev, "no driver attached\n");
9329f139
IA
2324 retval = -ENODEV;
2325 goto out;
ed9eccbe
DS
2326 }
2327
20f083c0 2328 s = comedi_file_write_subdevice(file);
9329f139
IA
2329 if (!s || !s->async) {
2330 retval = -EIO;
2331 goto out;
2332 }
2714b019 2333
ed9eccbe 2334 async = s->async;
3318c7ad 2335 if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) {
f7398509
IA
2336 retval = -EINVAL;
2337 goto out;
2338 }
2714b019 2339
ed9eccbe 2340 add_wait_queue(&async->wait_head, &wait);
06181de1 2341 while (count == 0 && !retval) {
b183a836 2342 unsigned runflags;
35a7475d 2343 unsigned int wp, n1, n2;
b183a836 2344
ed9eccbe
DS
2345 set_current_state(TASK_INTERRUPTIBLE);
2346
b183a836
IA
2347 runflags = comedi_get_subdevice_runflags(s);
2348 if (!comedi_is_runflags_running(runflags)) {
06181de1
IA
2349 if (comedi_is_runflags_in_error(runflags))
2350 retval = -EPIPE;
28a60c45
IA
2351 if (retval || nbytes)
2352 become_nonbusy = true;
d2611540
IA
2353 break;
2354 }
28a60c45
IA
2355 if (nbytes == 0)
2356 break;
d2611540 2357
591c5f8a
IA
2358 /* Allocate all free buffer space. */
2359 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2360 m = comedi_buf_write_n_allocated(s);
591c5f8a 2361 n = min_t(size_t, m, nbytes);
ed9eccbe
DS
2362
2363 if (n == 0) {
ed9eccbe
DS
2364 if (file->f_flags & O_NONBLOCK) {
2365 retval = -EAGAIN;
2366 break;
2367 }
6a9ce6b6 2368 schedule();
ed9eccbe
DS
2369 if (signal_pending(current)) {
2370 retval = -ERESTARTSYS;
2371 break;
2372 }
3318c7ad
IA
2373 if (s->busy != file ||
2374 !(async->cmd.flags & CMDF_WRITE)) {
f7398509
IA
2375 retval = -EINVAL;
2376 break;
2377 }
ed9eccbe
DS
2378 continue;
2379 }
2380
35a7475d
IA
2381 wp = async->buf_write_ptr;
2382 n1 = min(n, async->prealloc_bufsz - wp);
2383 n2 = n - n1;
2384 m = copy_from_user(async->prealloc_buf + wp, buf, n1);
2385 if (m)
2386 m += n2;
2387 else if (n2)
2388 m = copy_from_user(async->prealloc_buf, buf + n1, n2);
ed9eccbe
DS
2389 if (m) {
2390 n -= m;
2391 retval = -EFAULT;
2392 }
940dd35d 2393 comedi_buf_write_free(s, n);
ed9eccbe
DS
2394
2395 count += n;
2396 nbytes -= n;
2397
2398 buf += n;
ed9eccbe 2399 }
06181de1 2400 remove_wait_queue(&async->wait_head, &wait);
ed9eccbe 2401 set_current_state(TASK_RUNNING);
06181de1
IA
2402 if (become_nonbusy && count == 0) {
2403 struct comedi_subdevice *new_s;
2404
2405 /*
2406 * To avoid deadlock, cannot acquire dev->mutex
2407 * while dev->attach_lock is held.
2408 */
2409 up_read(&dev->attach_lock);
2410 attach_locked = false;
2411 mutex_lock(&dev->mutex);
2412 /*
2413 * Check device hasn't become detached behind our back.
2414 * Checking dev->detach_count is unchanged ought to be
2415 * sufficient (unless there have been 2**32 detaches in the
2416 * meantime!), but check the subdevice pointer as well just in
2417 * case.
ed65bba3
IA
2418 *
2419 * Also check the subdevice is still in a suitable state to
2420 * become non-busy in case it changed behind our back.
06181de1
IA
2421 */
2422 new_s = comedi_file_write_subdevice(file);
2423 if (dev->attached && old_detach_count == dev->detach_count &&
ed65bba3
IA
2424 s == new_s && new_s->async == async && s->busy == file &&
2425 (async->cmd.flags & CMDF_WRITE) &&
2426 !comedi_is_subdevice_running(s))
06181de1
IA
2427 do_become_nonbusy(dev, s);
2428 mutex_unlock(&dev->mutex);
2429 }
2430out:
9329f139
IA
2431 if (attach_locked)
2432 up_read(&dev->attach_lock);
ed9eccbe 2433
476b8477 2434 return count ? count : retval;
ed9eccbe
DS
2435}
2436
92d0127c 2437static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
36efbacd 2438 loff_t *offset)
ed9eccbe 2439{
34c43922 2440 struct comedi_subdevice *s;
d163679c 2441 struct comedi_async *async;
76e8e7d4
IA
2442 unsigned int n, m;
2443 ssize_t count = 0;
2444 int retval = 0;
ed9eccbe 2445 DECLARE_WAITQUEUE(wait, current);
20f083c0
IA
2446 struct comedi_file *cfp = file->private_data;
2447 struct comedi_device *dev = cfp->dev;
45c2bc55
IA
2448 unsigned int old_detach_count;
2449 bool become_nonbusy = false;
2450 bool attach_locked;
3ffab428 2451
45c2bc55
IA
2452 /* Protect against device detachment during operation. */
2453 down_read(&dev->attach_lock);
2454 attach_locked = true;
2455 old_detach_count = dev->detach_count;
2456
ed9eccbe 2457 if (!dev->attached) {
272850f0 2458 dev_dbg(dev->class_dev, "no driver attached\n");
45c2bc55
IA
2459 retval = -ENODEV;
2460 goto out;
ed9eccbe
DS
2461 }
2462
20f083c0 2463 s = comedi_file_read_subdevice(file);
45c2bc55
IA
2464 if (!s || !s->async) {
2465 retval = -EIO;
2466 goto out;
2467 }
5c87fef5 2468
ed9eccbe 2469 async = s->async;
39582847 2470 if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
f025ab9e
IA
2471 retval = -EINVAL;
2472 goto out;
2473 }
ed9eccbe
DS
2474
2475 add_wait_queue(&async->wait_head, &wait);
3c3bea26 2476 while (count == 0 && !retval) {
42ea907d
IA
2477 unsigned int rp, n1, n2;
2478
ed9eccbe
DS
2479 set_current_state(TASK_INTERRUPTIBLE);
2480
e9edef3a 2481 m = comedi_buf_read_n_available(s);
8ea93928 2482 n = min_t(size_t, m, nbytes);
ed9eccbe
DS
2483
2484 if (n == 0) {
b183a836
IA
2485 unsigned runflags = comedi_get_subdevice_runflags(s);
2486
2487 if (!comedi_is_runflags_running(runflags)) {
2488 if (comedi_is_runflags_in_error(runflags))
ed9eccbe 2489 retval = -EPIPE;
3c3bea26
IA
2490 if (retval || nbytes)
2491 become_nonbusy = true;
ed9eccbe
DS
2492 break;
2493 }
3c3bea26
IA
2494 if (nbytes == 0)
2495 break;
ed9eccbe
DS
2496 if (file->f_flags & O_NONBLOCK) {
2497 retval = -EAGAIN;
2498 break;
2499 }
6a9ce6b6 2500 schedule();
ed9eccbe
DS
2501 if (signal_pending(current)) {
2502 retval = -ERESTARTSYS;
2503 break;
2504 }
39582847
IA
2505 if (s->busy != file ||
2506 (async->cmd.flags & CMDF_WRITE)) {
f025ab9e
IA
2507 retval = -EINVAL;
2508 break;
2509 }
ed9eccbe
DS
2510 continue;
2511 }
42ea907d
IA
2512 rp = async->buf_read_ptr;
2513 n1 = min(n, async->prealloc_bufsz - rp);
2514 n2 = n - n1;
2515 m = copy_to_user(buf, async->prealloc_buf + rp, n1);
2516 if (m)
2517 m += n2;
2518 else if (n2)
2519 m = copy_to_user(buf + n1, async->prealloc_buf, n2);
ed9eccbe
DS
2520 if (m) {
2521 n -= m;
2522 retval = -EFAULT;
2523 }
2524
d13be55a 2525 comedi_buf_read_alloc(s, n);
f1df8662 2526 comedi_buf_read_free(s, n);
ed9eccbe
DS
2527
2528 count += n;
2529 nbytes -= n;
2530
2531 buf += n;
ed9eccbe 2532 }
45c2bc55
IA
2533 remove_wait_queue(&async->wait_head, &wait);
2534 set_current_state(TASK_RUNNING);
970679b0 2535 if (become_nonbusy && count == 0) {
45c2bc55
IA
2536 struct comedi_subdevice *new_s;
2537
2538 /*
2539 * To avoid deadlock, cannot acquire dev->mutex
2540 * while dev->attach_lock is held.
2541 */
2542 up_read(&dev->attach_lock);
2543 attach_locked = false;
4b18f08b 2544 mutex_lock(&dev->mutex);
45c2bc55
IA
2545 /*
2546 * Check device hasn't become detached behind our back.
2547 * Checking dev->detach_count is unchanged ought to be
2548 * sufficient (unless there have been 2**32 detaches in the
2549 * meantime!), but check the subdevice pointer as well just in
2550 * case.
fd060c8f
IA
2551 *
2552 * Also check the subdevice is still in a suitable state to
2553 * become non-busy in case it changed behind our back.
45c2bc55 2554 */
20f083c0 2555 new_s = comedi_file_read_subdevice(file);
45c2bc55 2556 if (dev->attached && old_detach_count == dev->detach_count &&
fd060c8f
IA
2557 s == new_s && new_s->async == async && s->busy == file &&
2558 !(async->cmd.flags & CMDF_WRITE) &&
2559 !comedi_is_subdevice_running(s) &&
2560 comedi_buf_read_n_available(s) == 0)
2561 do_become_nonbusy(dev, s);
4b18f08b 2562 mutex_unlock(&dev->mutex);
ed9eccbe 2563 }
45c2bc55
IA
2564out:
2565 if (attach_locked)
2566 up_read(&dev->attach_lock);
ed9eccbe 2567
476b8477 2568 return count ? count : retval;
ed9eccbe
DS
2569}
2570
ed9eccbe
DS
2571static int comedi_open(struct inode *inode, struct file *file)
2572{
ed9eccbe 2573 const unsigned minor = iminor(inode);
20f083c0 2574 struct comedi_file *cfp;
fc406986
IA
2575 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2576 int rc;
97920071 2577
4da5fa9a 2578 if (!dev) {
272850f0 2579 pr_debug("invalid minor number\n");
ed9eccbe
DS
2580 return -ENODEV;
2581 }
2582
20f083c0
IA
2583 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2584 if (!cfp)
2585 return -ENOMEM;
2586
2587 cfp->dev = dev;
2588
ed9eccbe 2589 mutex_lock(&dev->mutex);
0e0d311e
IA
2590 if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
2591 dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
fc406986
IA
2592 rc = -ENODEV;
2593 goto out;
ed9eccbe 2594 }
1363e4fb 2595 if (dev->attached && dev->use_count == 0) {
ed9eccbe 2596 if (!try_module_get(dev->driver->module)) {
90e6f51d 2597 rc = -ENXIO;
fc406986 2598 goto out;
ed9eccbe 2599 }
1363e4fb
IA
2600 if (dev->open) {
2601 rc = dev->open(dev);
2602 if (rc < 0) {
2603 module_put(dev->driver->module);
2604 goto out;
2605 }
3c17ba07
IA
2606 }
2607 }
ed9eccbe
DS
2608
2609 dev->use_count++;
20f083c0
IA
2610 file->private_data = cfp;
2611 comedi_file_reset(file);
fc406986 2612 rc = 0;
ed9eccbe 2613
fc406986 2614out:
a5011a26 2615 mutex_unlock(&dev->mutex);
20f083c0 2616 if (rc) {
fc406986 2617 comedi_dev_put(dev);
20f083c0
IA
2618 kfree(cfp);
2619 }
fc406986 2620 return rc;
ed9eccbe
DS
2621}
2622
2aae0076
HS
2623static int comedi_fasync(int fd, struct file *file, int on)
2624{
20f083c0
IA
2625 struct comedi_file *cfp = file->private_data;
2626 struct comedi_device *dev = cfp->dev;
2aae0076
HS
2627
2628 return fasync_helper(fd, file, on, &dev->async_queue);
2629}
2630
a5011a26 2631static int comedi_close(struct inode *inode, struct file *file)
ed9eccbe 2632{
20f083c0
IA
2633 struct comedi_file *cfp = file->private_data;
2634 struct comedi_device *dev = cfp->dev;
a5011a26 2635 struct comedi_subdevice *s = NULL;
ed9eccbe
DS
2636 int i;
2637
a5011a26
HS
2638 mutex_lock(&dev->mutex);
2639
2640 if (dev->subdevices) {
2641 for (i = 0; i < dev->n_subdevices; i++) {
b077f2cd 2642 s = &dev->subdevices[i];
a5011a26
HS
2643
2644 if (s->busy == file)
2645 do_cancel(dev, s);
2646 if (s->lock == file)
2647 s->lock = NULL;
2648 }
ed9eccbe 2649 }
1363e4fb
IA
2650 if (dev->attached && dev->use_count == 1) {
2651 if (dev->close)
2652 dev->close(dev);
a5011a26 2653 module_put(dev->driver->module);
1363e4fb 2654 }
a5011a26
HS
2655
2656 dev->use_count--;
2657
2658 mutex_unlock(&dev->mutex);
fc406986 2659 comedi_dev_put(dev);
20f083c0 2660 kfree(cfp);
a5011a26 2661
ed9eccbe
DS
2662 return 0;
2663}
2664
8cb8aad7 2665static const struct file_operations comedi_fops = {
a5011a26
HS
2666 .owner = THIS_MODULE,
2667 .unlocked_ioctl = comedi_unlocked_ioctl,
2668 .compat_ioctl = comedi_compat_ioctl,
2669 .open = comedi_open,
2670 .release = comedi_close,
2671 .read = comedi_read,
2672 .write = comedi_write,
2673 .mmap = comedi_mmap,
2674 .poll = comedi_poll,
2675 .fasync = comedi_fasync,
2676 .llseek = noop_llseek,
2677};
2678
dd630cde 2679/**
a3e39942
IA
2680 * comedi_event() - Handle events for asynchronous COMEDI command
2681 * @dev: COMEDI device.
2682 * @s: COMEDI subdevice.
2683 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
dd630cde 2684 *
a3e39942
IA
2685 * If an asynchronous COMEDI command is active on the subdevice, process
2686 * any %COMEDI_CB_... event flags that have been set, usually by an
dd630cde 2687 * interrupt handler. These may change the run state of the asynchronous
a3e39942 2688 * command, wake a task, and/or send a %SIGIO signal.
dd630cde 2689 */
a5011a26 2690void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
883db3d9 2691{
a5011a26 2692 struct comedi_async *async = s->async;
ef4b4b27
IA
2693 unsigned int events;
2694 int si_code = 0;
2695 unsigned long flags;
2696
2697 spin_lock_irqsave(&s->spin_lock, flags);
883db3d9 2698
ef4b4b27 2699 events = async->events;
922d9ced 2700 async->events = 0;
ef4b4b27
IA
2701 if (!__comedi_is_subdevice_running(s)) {
2702 spin_unlock_irqrestore(&s->spin_lock, flags);
a5011a26 2703 return;
ef4b4b27 2704 }
a5011a26 2705
922d9ced 2706 if (events & COMEDI_CB_CANCEL_MASK)
ef4b4b27 2707 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
781f933c
HS
2708
2709 /*
ef4b4b27
IA
2710 * Remember if an error event has occurred, so an error can be
2711 * returned the next time the user does a read() or write().
781f933c 2712 */
ef4b4b27
IA
2713 if (events & COMEDI_CB_ERROR_MASK)
2714 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
883db3d9 2715
922d9ced 2716 if (async->cb_mask & events) {
c265be01 2717 wake_up_interruptible(&async->wait_head);
aa33122f 2718 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
a5011a26 2719 }
ef4b4b27
IA
2720
2721 spin_unlock_irqrestore(&s->spin_lock, flags);
2722
2723 if (si_code)
2724 kill_fasync(&dev->async_queue, SIGIO, si_code);
883db3d9 2725}
5660e742 2726EXPORT_SYMBOL_GPL(comedi_event);
883db3d9 2727
07778393 2728/* Note: the ->mutex is pre-locked on successful return */
7638ffcb 2729struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
a5011a26 2730{
7638ffcb 2731 struct comedi_device *dev;
a5011a26
HS
2732 struct device *csdev;
2733 unsigned i;
a5011a26 2734
36efbacd 2735 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
88cc30cf 2736 if (!dev)
7638ffcb 2737 return ERR_PTR(-ENOMEM);
7638ffcb 2738 comedi_device_init(dev);
db2e3487 2739 comedi_set_hw_dev(dev, hardware_device);
07778393 2740 mutex_lock(&dev->mutex);
5b7dba1b 2741 mutex_lock(&comedi_board_minor_table_lock);
38b9722a
IA
2742 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2743 i < COMEDI_NUM_BOARD_MINORS; ++i) {
88cc30cf 2744 if (!comedi_board_minor_table[i]) {
cb6b79de 2745 comedi_board_minor_table[i] = dev;
a5011a26
HS
2746 break;
2747 }
2748 }
5b7dba1b 2749 mutex_unlock(&comedi_board_minor_table_lock);
a5011a26 2750 if (i == COMEDI_NUM_BOARD_MINORS) {
07778393 2751 mutex_unlock(&dev->mutex);
7638ffcb 2752 comedi_device_cleanup(dev);
5b13ed94 2753 comedi_dev_put(dev);
a112eab4
HM
2754 dev_err(hardware_device,
2755 "ran out of minor numbers for board device files\n");
7638ffcb 2756 return ERR_PTR(-EBUSY);
a5011a26 2757 }
7638ffcb 2758 dev->minor = i;
a5011a26
HS
2759 csdev = device_create(comedi_class, hardware_device,
2760 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2761 if (!IS_ERR(csdev))
8f988d87 2762 dev->class_dev = get_device(csdev);
883db3d9 2763
07778393 2764 /* Note: dev->mutex needs to be unlocked by the caller. */
7638ffcb 2765 return dev;
883db3d9
FMH
2766}
2767
3346b798 2768void comedi_release_hardware_device(struct device *hardware_device)
883db3d9 2769{
a5011a26 2770 int minor;
cb6b79de 2771 struct comedi_device *dev;
883db3d9 2772
38b9722a
IA
2773 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2774 minor++) {
5b7dba1b 2775 mutex_lock(&comedi_board_minor_table_lock);
cb6b79de
IA
2776 dev = comedi_board_minor_table[minor];
2777 if (dev && dev->hw_dev == hardware_device) {
5b7dba1b
IA
2778 comedi_board_minor_table[minor] = NULL;
2779 mutex_unlock(&comedi_board_minor_table_lock);
cb6b79de 2780 comedi_free_board_dev(dev);
3346b798 2781 break;
a5011a26 2782 }
5b7dba1b 2783 mutex_unlock(&comedi_board_minor_table_lock);
883db3d9 2784 }
883db3d9
FMH
2785}
2786
f65cc544 2787int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
883db3d9 2788{
f65cc544 2789 struct comedi_device *dev = s->device;
a5011a26
HS
2790 struct device *csdev;
2791 unsigned i;
883db3d9 2792
5b7dba1b
IA
2793 mutex_lock(&comedi_subdevice_minor_table_lock);
2794 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
88cc30cf 2795 if (!comedi_subdevice_minor_table[i]) {
bd5b4173 2796 comedi_subdevice_minor_table[i] = s;
a5011a26
HS
2797 break;
2798 }
2799 }
5b7dba1b
IA
2800 mutex_unlock(&comedi_subdevice_minor_table_lock);
2801 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
a112eab4
HM
2802 dev_err(dev->class_dev,
2803 "ran out of minor numbers for subdevice files\n");
a5011a26
HS
2804 return -EBUSY;
2805 }
5b7dba1b 2806 i += COMEDI_NUM_BOARD_MINORS;
a5011a26
HS
2807 s->minor = i;
2808 csdev = device_create(comedi_class, dev->class_dev,
2809 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
90a35c15 2810 dev->minor, s->index);
a5011a26
HS
2811 if (!IS_ERR(csdev))
2812 s->class_dev = csdev;
883db3d9 2813
da718546 2814 return 0;
883db3d9
FMH
2815}
2816
a5011a26 2817void comedi_free_subdevice_minor(struct comedi_subdevice *s)
883db3d9 2818{
0fcc9d48 2819 unsigned int i;
883db3d9 2820
88cc30cf 2821 if (!s)
a5011a26 2822 return;
5104a898
HS
2823 if (s->minor < COMEDI_NUM_BOARD_MINORS ||
2824 s->minor >= COMEDI_NUM_MINORS)
a5011a26 2825 return;
883db3d9 2826
0fcc9d48
IA
2827 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2828 mutex_lock(&comedi_subdevice_minor_table_lock);
bd5b4173
IA
2829 if (s == comedi_subdevice_minor_table[i])
2830 comedi_subdevice_minor_table[i] = NULL;
0fcc9d48 2831 mutex_unlock(&comedi_subdevice_minor_table_lock);
a5011a26
HS
2832 if (s->class_dev) {
2833 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2834 s->class_dev = NULL;
883db3d9 2835 }
883db3d9 2836}
a5787824 2837
682b9119 2838static void comedi_cleanup_board_minors(void)
76cca89f 2839{
2be8ae58 2840 struct comedi_device *dev;
76cca89f
HS
2841 unsigned i;
2842
2be8ae58
HS
2843 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
2844 dev = comedi_clear_board_minor(i);
2845 comedi_free_board_dev(dev);
2846 }
76cca89f
HS
2847}
2848
91fa0b0c
HS
2849static int __init comedi_init(void)
2850{
2851 int i;
2852 int retval;
2853
c2ad078b 2854 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
91fa0b0c
HS
2855
2856 if (comedi_num_legacy_minors < 0 ||
2857 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
c2ad078b 2858 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
91fa0b0c
HS
2859 COMEDI_NUM_BOARD_MINORS);
2860 return -EINVAL;
2861 }
2862
91fa0b0c
HS
2863 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2864 COMEDI_NUM_MINORS, "comedi");
2865 if (retval)
2866 return -EIO;
2867 cdev_init(&comedi_cdev, &comedi_fops);
2868 comedi_cdev.owner = THIS_MODULE;
a5bde3a1
AP
2869
2870 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2871 if (retval) {
2872 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2873 COMEDI_NUM_MINORS);
2874 return retval;
2875 }
2876
91fa0b0c
HS
2877 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2878 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2879 COMEDI_NUM_MINORS);
2880 return -EIO;
2881 }
2882 comedi_class = class_create(THIS_MODULE, "comedi");
2883 if (IS_ERR(comedi_class)) {
c2ad078b 2884 pr_err("failed to create class\n");
91fa0b0c
HS
2885 cdev_del(&comedi_cdev);
2886 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2887 COMEDI_NUM_MINORS);
2888 return PTR_ERR(comedi_class);
2889 }
2890
e56341ad 2891 comedi_class->dev_groups = comedi_dev_groups;
91fa0b0c
HS
2892
2893 /* XXX requires /proc interface */
2894 comedi_proc_init();
2895
2896 /* create devices files for legacy/manual use */
2897 for (i = 0; i < comedi_num_legacy_minors; i++) {
7638ffcb 2898 struct comedi_device *dev;
4bac39f6 2899
7638ffcb
IA
2900 dev = comedi_alloc_board_minor(NULL);
2901 if (IS_ERR(dev)) {
682b9119 2902 comedi_cleanup_board_minors();
91fa0b0c
HS
2903 cdev_del(&comedi_cdev);
2904 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2905 COMEDI_NUM_MINORS);
7638ffcb 2906 return PTR_ERR(dev);
91fa0b0c 2907 }
cb3aadae
KH
2908 /* comedi_alloc_board_minor() locked the mutex */
2909 mutex_unlock(&dev->mutex);
91fa0b0c
HS
2910 }
2911
2912 return 0;
2913}
2914module_init(comedi_init);
2915
2916static void __exit comedi_cleanup(void)
2917{
682b9119 2918 comedi_cleanup_board_minors();
91fa0b0c
HS
2919 class_destroy(comedi_class);
2920 cdev_del(&comedi_cdev);
2921 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2922
2923 comedi_proc_cleanup();
2924}
2925module_exit(comedi_cleanup);
2926
a5787824
HS
2927MODULE_AUTHOR("http://www.comedi.org");
2928MODULE_DESCRIPTION("Comedi core module");
2929MODULE_LICENSE("GPL");