blob: 64b3966c5f1f0d4db3e8359e78b914e02a62a3ce [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
Ian Abbottf6fef5d2015-01-28 18:41:45 +00002 * 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 */
David Schleefed9eccb2008-11-04 20:29:31 -080018
H Hartley Sweetenc2ad0782014-07-17 12:27:32 -070019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
David Schleefed9eccb2008-11-04 20:29:31 -080021#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>
David Schleefed9eccb2008-11-04 20:29:31 -080029#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>
Frank Mori Hess883db3d2009-04-14 11:21:41 -040039#include <linux/stat.h>
David Schleefed9eccb2008-11-04 20:29:31 -080040
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080041#include <linux/io.h>
42#include <linux/uaccess.h>
David Schleefed9eccb2008-11-04 20:29:31 -080043
Ian Abbott3a5fa272012-06-19 10:17:44 +010044#include "comedi_internal.h"
David Schleefed9eccb2008-11-04 20:29:31 -080045
Ian Abbott63107ce2015-09-18 15:46:23 +010046/*
Ian Abbotteb340ac2015-04-21 13:18:11 +010047 * comedi_subdevice "runflags"
Ian Abbott63107ce2015-09-18 15:46:23 +010048 * COMEDI_SRF_RT: DEPRECATED: command is running real-time
49 * COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
Ian Abbotteb340ac2015-04-21 13:18:11 +010050 * since the last command was started
Ian Abbott63107ce2015-09-18 15:46:23 +010051 * COMEDI_SRF_RUNNING: command is running
52 * COMEDI_SRF_FREE_SPRIV: free s->private on detach
Ian Abbotteb340ac2015-04-21 13:18:11 +010053 *
Ian Abbott63107ce2015-09-18 15:46:23 +010054 * COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
Ian Abbotteb340ac2015-04-21 13:18:11 +010055 */
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
63/**
Ian Abbotta3e39942015-09-23 16:33:27 +010064 * 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.
Ian Abbott20f083c2014-11-04 18:09:00 +000070 */
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
Ian Abbotteda56822013-04-04 14:59:02 +010079#define COMEDI_NUM_MINORS 0x100
Ian Abbott5b7dba12013-04-04 14:59:04 +010080#define COMEDI_NUM_SUBDEVICE_MINORS \
81 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
Ian Abbotteda56822013-04-04 14:59:02 +010082
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070083static int comedi_num_legacy_minors;
Matias Mucciolo6e302932016-09-12 10:18:59 -030084module_param(comedi_num_legacy_minors, int, 0444);
Ian Abbott4d7df822012-04-13 14:12:53 +010085MODULE_PARM_DESC(comedi_num_legacy_minors,
86 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
87 );
88
Ian Abbott234bb3c2012-04-13 14:12:54 +010089unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
Matias Mucciolo6e302932016-09-12 10:18:59 -030090module_param(comedi_default_buf_size_kb, uint, 0644);
Ian Abbott4d7df822012-04-13 14:12:53 +010091MODULE_PARM_DESC(comedi_default_buf_size_kb,
92 "default asynchronous buffer size in KiB (default "
Ian Abbott234bb3c2012-04-13 14:12:54 +010093 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
Ian Abbott4d7df822012-04-13 14:12:53 +010094
Ian Abbott234bb3c2012-04-13 14:12:54 +010095unsigned int comedi_default_buf_maxsize_kb
96 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
Matias Mucciolo6e302932016-09-12 10:18:59 -030097module_param(comedi_default_buf_maxsize_kb, uint, 0644);
Ian Abbott4d7df822012-04-13 14:12:53 +010098MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
99 "default maximum size of asynchronous buffer in KiB (default "
Ian Abbott234bb3c2012-04-13 14:12:54 +0100100 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
Bernd Porr1dd33ab2008-12-08 23:30:13 +0000101
Ian Abbott5b7dba12013-04-04 14:59:04 +0100102static DEFINE_MUTEX(comedi_board_minor_table_lock);
Ian Abbottcb6b79d2013-04-04 14:59:16 +0100103static struct comedi_device
Ian Abbott5b7dba12013-04-04 14:59:04 +0100104*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. */
Ian Abbottbd5b4172013-04-04 14:59:15 +0100108static struct comedi_subdevice
Ian Abbott5b7dba12013-04-04 14:59:04 +0100109*comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
David Schleefed9eccb2008-11-04 20:29:31 -0800110
Ian Abbottd9740a02013-04-04 14:58:55 +0100111static struct class *comedi_class;
112static struct cdev comedi_cdev;
113
114static void comedi_device_init(struct comedi_device *dev)
115{
Ian Abbott5b13ed92013-11-08 15:03:32 +0000116 kref_init(&dev->refcount);
Ian Abbottd9740a02013-04-04 14:58:55 +0100117 spin_lock_init(&dev->spinlock);
118 mutex_init(&dev->mutex);
Ian Abbott2f3fdcd2013-11-08 15:03:24 +0000119 init_rwsem(&dev->attach_lock);
Ian Abbottd9740a02013-04-04 14:58:55 +0100120 dev->minor = -1;
121}
122
Ian Abbott5b13ed92013-11-08 15:03:32 +0000123static 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);
Ian Abbott8f988d82013-12-11 14:51:02 +0000129 put_device(dev->class_dev);
Ian Abbott5b13ed92013-11-08 15:03:32 +0000130 kfree(dev);
131}
132
Ian Abbottdd630cd2015-01-28 18:41:46 +0000133/**
Ian Abbotta3e39942015-09-23 16:33:27 +0100134 * comedi_dev_put() - Release a use of a COMEDI device
135 * @dev: COMEDI device.
Ian Abbottdd630cd2015-01-28 18:41:46 +0000136 *
Ian Abbotta3e39942015-09-23 16:33:27 +0100137 * 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.
Ian Abbottdd630cd2015-01-28 18:41:46 +0000140 *
Ian Abbotta3e39942015-09-23 16:33:27 +0100141 * 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
Ian Abbottdd630cd2015-01-28 18:41:46 +0000143 * device is still valid if this function returns 0.
144 */
Ian Abbott5b13ed92013-11-08 15:03:32 +0000145int 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}
Ian Abbottb449c1c2013-11-08 15:03:33 +0000151EXPORT_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}
Ian Abbott5b13ed92013-11-08 15:03:32 +0000159
Ian Abbottd9740a02013-04-04 14:58:55 +0100160static void comedi_device_cleanup(struct comedi_device *dev)
161{
162 struct module *driver_module = NULL;
163
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -0700164 if (!dev)
Ian Abbottd9740a02013-04-04 14:58:55 +0100165 return;
166 mutex_lock(&dev->mutex);
167 if (dev->attached)
168 driver_module = dev->driver->module;
169 comedi_device_detach(dev);
Ian Abbott1363e4f2013-12-11 14:51:03 +0000170 if (driver_module && dev->use_count)
171 module_put(driver_module);
Ian Abbottd9740a02013-04-04 14:58:55 +0100172 mutex_unlock(&dev->mutex);
Ian Abbottd9740a02013-04-04 14:58:55 +0100173}
174
Ian Abbottdb210da2013-04-04 14:59:18 +0100175static 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
Leslie Kleind4d47892016-03-21 09:18:35 -0400189static struct comedi_device *comedi_clear_board_minor(unsigned int minor)
Ian Abbottd9740a02013-04-04 14:58:55 +0100190{
Ian Abbottcb6b79d2013-04-04 14:59:16 +0100191 struct comedi_device *dev;
Ian Abbottd9740a02013-04-04 14:58:55 +0100192
Ian Abbott5b7dba12013-04-04 14:59:04 +0100193 mutex_lock(&comedi_board_minor_table_lock);
Ian Abbottcb6b79d2013-04-04 14:59:16 +0100194 dev = comedi_board_minor_table[minor];
Ian Abbott5b7dba12013-04-04 14:59:04 +0100195 comedi_board_minor_table[minor] = NULL;
196 mutex_unlock(&comedi_board_minor_table_lock);
Ian Abbottcb6b79d2013-04-04 14:59:16 +0100197 return dev;
Ian Abbottd9740a02013-04-04 14:58:55 +0100198}
199
Ian Abbottcb6b79d2013-04-04 14:59:16 +0100200static void comedi_free_board_dev(struct comedi_device *dev)
Ian Abbottd9740a02013-04-04 14:58:55 +0100201{
Ian Abbottcb6b79d2013-04-04 14:59:16 +0100202 if (dev) {
Ian Abbott52ef9e72014-01-07 12:38:32 +0000203 comedi_device_cleanup(dev);
Ian Abbottcb6b79d2013-04-04 14:59:16 +0100204 if (dev->class_dev) {
205 device_destroy(comedi_class,
206 MKDEV(COMEDI_MAJOR, dev->minor));
Ian Abbottd9740a02013-04-04 14:58:55 +0100207 }
Ian Abbott5b13ed92013-11-08 15:03:32 +0000208 comedi_dev_put(dev);
Ian Abbottd9740a02013-04-04 14:58:55 +0100209 }
210}
Ian Abbott8ab4ed62013-04-04 14:58:54 +0100211
Ian Abbottb2073dc2016-03-29 10:49:04 +0100212static struct comedi_subdevice *
213comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned int minor)
Ian Abbott5b7dba12013-04-04 14:59:04 +0100214{
Ian Abbottbd5b4172013-04-04 14:59:15 +0100215 struct comedi_subdevice *s;
Ian Abbott5b7dba12013-04-04 14:59:04 +0100216 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
217
Ian Abbott5b7dba12013-04-04 14:59:04 +0100218 mutex_lock(&comedi_subdevice_minor_table_lock);
Ian Abbottbd5b4172013-04-04 14:59:15 +0100219 s = comedi_subdevice_minor_table[i];
Ian Abbott63ab0392013-11-08 15:03:42 +0000220 if (s && s->device != dev)
221 s = NULL;
Ian Abbott5b7dba12013-04-04 14:59:04 +0100222 mutex_unlock(&comedi_subdevice_minor_table_lock);
Ian Abbottbd5b4172013-04-04 14:59:15 +0100223 return s;
Ian Abbott5b7dba12013-04-04 14:59:04 +0100224}
225
Leslie Kleind4d47892016-03-21 09:18:35 -0400226static struct comedi_device *comedi_dev_get_from_board_minor(unsigned int minor)
Ian Abbottb449c1c2013-11-08 15:03:33 +0000227{
228 struct comedi_device *dev;
229
Ian Abbottb449c1c2013-11-08 15:03:33 +0000230 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
Ian Abbottb2073dc2016-03-29 10:49:04 +0100236static struct comedi_device *
237comedi_dev_get_from_subdevice_minor(unsigned int minor)
Ian Abbottb449c1c2013-11-08 15:03:33 +0000238{
239 struct comedi_device *dev;
240 struct comedi_subdevice *s;
241 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
242
Ian Abbottb449c1c2013-11-08 15:03:33 +0000243 mutex_lock(&comedi_subdevice_minor_table_lock);
244 s = comedi_subdevice_minor_table[i];
245 dev = comedi_dev_get(s ? s->device : NULL);
246 mutex_unlock(&comedi_subdevice_minor_table_lock);
247 return dev;
248}
249
Ian Abbottdd630cd2015-01-28 18:41:46 +0000250/**
Ian Abbotta3e39942015-09-23 16:33:27 +0100251 * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
252 * @minor: Minor device number.
Ian Abbottdd630cd2015-01-28 18:41:46 +0000253 *
Ian Abbotta3e39942015-09-23 16:33:27 +0100254 * Finds the COMEDI device associated with the minor device number, if any,
255 * and increments its reference count. The COMEDI device is prevented from
Ian Abbottdd630cd2015-01-28 18:41:46 +0000256 * being freed until a matching call is made to comedi_dev_put().
257 *
Ian Abbotta3e39942015-09-23 16:33:27 +0100258 * Return: A pointer to the COMEDI device if it exists, with its usage
259 * reference incremented. Return NULL if no COMEDI device exists with the
Ian Abbottdd630cd2015-01-28 18:41:46 +0000260 * specified minor device number.
261 */
Leslie Kleind4d47892016-03-21 09:18:35 -0400262struct comedi_device *comedi_dev_get_from_minor(unsigned int minor)
Ian Abbottb449c1c2013-11-08 15:03:33 +0000263{
264 if (minor < COMEDI_NUM_BOARD_MINORS)
265 return comedi_dev_get_from_board_minor(minor);
Kinka Huangcb3aada2014-07-15 23:11:02 +0800266
267 return comedi_dev_get_from_subdevice_minor(minor);
Ian Abbottb449c1c2013-11-08 15:03:33 +0000268}
269EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
270
H Hartley Sweeten43bd33f2012-12-19 15:33:29 -0700271static struct comedi_subdevice *
Ian Abbottda56fdc2013-04-04 14:59:10 +0100272comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
H Hartley Sweeten43bd33f2012-12-19 15:33:29 -0700273{
Ian Abbottbd5b4172013-04-04 14:59:15 +0100274 struct comedi_subdevice *s;
Ian Abbottda56fdc2013-04-04 14:59:10 +0100275
276 if (minor >= COMEDI_NUM_BOARD_MINORS) {
Ian Abbott63ab0392013-11-08 15:03:42 +0000277 s = comedi_subdevice_from_minor(dev, minor);
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -0700278 if (!s || (s->subdev_flags & SDF_CMD_READ))
Ian Abbottbd5b4172013-04-04 14:59:15 +0100279 return s;
Ian Abbottda56fdc2013-04-04 14:59:10 +0100280 }
281 return dev->read_subdev;
H Hartley Sweeten43bd33f2012-12-19 15:33:29 -0700282}
283
284static struct comedi_subdevice *
Ian Abbottda56fdc2013-04-04 14:59:10 +0100285comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
H Hartley Sweeten43bd33f2012-12-19 15:33:29 -0700286{
Ian Abbottbd5b4172013-04-04 14:59:15 +0100287 struct comedi_subdevice *s;
Ian Abbottda56fdc2013-04-04 14:59:10 +0100288
289 if (minor >= COMEDI_NUM_BOARD_MINORS) {
Ian Abbott63ab0392013-11-08 15:03:42 +0000290 s = comedi_subdevice_from_minor(dev, minor);
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -0700291 if (!s || (s->subdev_flags & SDF_CMD_WRITE))
Ian Abbottbd5b4172013-04-04 14:59:15 +0100292 return s;
Ian Abbottda56fdc2013-04-04 14:59:10 +0100293 }
294 return dev->write_subdev;
H Hartley Sweeten43bd33f2012-12-19 15:33:29 -0700295}
296
Ian Abbott20f083c2014-11-04 18:09:00 +0000297static void comedi_file_reset(struct file *file)
298{
299 struct comedi_file *cfp = file->private_data;
300 struct comedi_device *dev = cfp->dev;
301 struct comedi_subdevice *s, *read_s, *write_s;
302 unsigned int minor = iminor(file_inode(file));
303
304 read_s = dev->read_subdev;
305 write_s = dev->write_subdev;
306 if (minor >= COMEDI_NUM_BOARD_MINORS) {
307 s = comedi_subdevice_from_minor(dev, minor);
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -0700308 if (!s || s->subdev_flags & SDF_CMD_READ)
Ian Abbott20f083c2014-11-04 18:09:00 +0000309 read_s = s;
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -0700310 if (!s || s->subdev_flags & SDF_CMD_WRITE)
Ian Abbott20f083c2014-11-04 18:09:00 +0000311 write_s = s;
312 }
313 cfp->last_attached = dev->attached;
314 cfp->last_detach_count = dev->detach_count;
Shyam Saini34d34732016-05-10 21:21:07 +0530315 WRITE_ONCE(cfp->read_subdev, read_s);
316 WRITE_ONCE(cfp->write_subdev, write_s);
Ian Abbott20f083c2014-11-04 18:09:00 +0000317}
318
319static void comedi_file_check(struct file *file)
320{
321 struct comedi_file *cfp = file->private_data;
322 struct comedi_device *dev = cfp->dev;
323
324 if (cfp->last_attached != dev->attached ||
325 cfp->last_detach_count != dev->detach_count)
326 comedi_file_reset(file);
327}
328
329static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
330{
331 struct comedi_file *cfp = file->private_data;
332
333 comedi_file_check(file);
Shyam Saini34d34732016-05-10 21:21:07 +0530334 return READ_ONCE(cfp->read_subdev);
Ian Abbott20f083c2014-11-04 18:09:00 +0000335}
336
337static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
338{
339 struct comedi_file *cfp = file->private_data;
340
341 comedi_file_check(file);
Shyam Saini34d34732016-05-10 21:21:07 +0530342 return READ_ONCE(cfp->write_subdev);
Ian Abbott20f083c2014-11-04 18:09:00 +0000343}
344
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400345static int resize_async_buffer(struct comedi_device *dev,
Ian Abbottb2073dc2016-03-29 10:49:04 +0100346 struct comedi_subdevice *s,
347 unsigned int new_size)
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700348{
Ian Abbott482f0a22014-05-02 13:50:14 +0100349 struct comedi_async *async = s->async;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700350 int retval;
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400351
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700352 if (new_size > async->max_bufsize)
353 return -EPERM;
354
355 if (s->busy) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -0700356 dev_dbg(dev->class_dev,
357 "subdevice is busy, cannot resize buffer\n");
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700358 return -EBUSY;
359 }
Ian Abbottd4526ab2014-05-06 13:12:11 +0100360 if (comedi_buf_is_mmapped(s)) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -0700361 dev_dbg(dev->class_dev,
362 "subdevice is mmapped, cannot resize buffer\n");
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700363 return -EBUSY;
364 }
365
Ian Abbotta2aab8b2015-01-28 18:41:50 +0000366 /* make sure buffer is an integral number of pages (we round up) */
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700367 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
368
369 retval = comedi_buf_alloc(dev, s, new_size);
370 if (retval < 0)
371 return retval;
372
373 if (s->buf_change) {
H Hartley Sweetend546b892014-07-21 11:48:32 -0700374 retval = s->buf_change(dev, s);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700375 if (retval < 0)
376 return retval;
377 }
378
H Hartley Sweeten272850f2013-11-26 10:21:11 -0700379 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
380 s->index, async->prealloc_bufsz);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700381 return 0;
382}
383
384/* sysfs attribute files */
385
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700386static ssize_t max_read_buffer_kb_show(struct device *csdev,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700387 struct device_attribute *attr, char *buf)
388{
Ian Abbottc88db462013-04-04 14:59:09 +0100389 unsigned int minor = MINOR(csdev->devt);
Ian Abbott7f4656c2013-04-04 14:59:08 +0100390 struct comedi_device *dev;
391 struct comedi_subdevice *s;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700392 unsigned int size = 0;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700393
Ian Abbottbe535c92013-11-08 15:03:37 +0000394 dev = comedi_dev_get_from_minor(minor);
Ian Abbott5e04c252013-04-04 14:59:11 +0100395 if (!dev)
Ian Abbottc88db462013-04-04 14:59:09 +0100396 return -ENODEV;
397
Ian Abbott7f4656c2013-04-04 14:59:08 +0100398 mutex_lock(&dev->mutex);
Ian Abbottda56fdc2013-04-04 14:59:10 +0100399 s = comedi_read_subdevice(dev, minor);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700400 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
401 size = s->async->max_bufsize / 1024;
Ian Abbott7f4656c2013-04-04 14:59:08 +0100402 mutex_unlock(&dev->mutex);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700403
Ian Abbottbe535c92013-11-08 15:03:37 +0000404 comedi_dev_put(dev);
Chase Southwoodecd56ff2014-02-12 02:28:35 -0600405 return snprintf(buf, PAGE_SIZE, "%u\n", size);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700406}
407
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700408static ssize_t max_read_buffer_kb_store(struct device *csdev,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700409 struct device_attribute *attr,
410 const char *buf, size_t count)
411{
Ian Abbottc88db462013-04-04 14:59:09 +0100412 unsigned int minor = MINOR(csdev->devt);
Ian Abbott7f4656c2013-04-04 14:59:08 +0100413 struct comedi_device *dev;
414 struct comedi_subdevice *s;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700415 unsigned int size;
416 int err;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700417
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700418 err = kstrtouint(buf, 10, &size);
419 if (err)
420 return err;
421 if (size > (UINT_MAX / 1024))
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700422 return -EINVAL;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700423 size *= 1024;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700424
Ian Abbottbe535c92013-11-08 15:03:37 +0000425 dev = comedi_dev_get_from_minor(minor);
Ian Abbott5e04c252013-04-04 14:59:11 +0100426 if (!dev)
Ian Abbottc88db462013-04-04 14:59:09 +0100427 return -ENODEV;
428
Ian Abbott7f4656c2013-04-04 14:59:08 +0100429 mutex_lock(&dev->mutex);
Ian Abbottda56fdc2013-04-04 14:59:10 +0100430 s = comedi_read_subdevice(dev, minor);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700431 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
432 s->async->max_bufsize = size;
433 else
434 err = -EINVAL;
Ian Abbott7f4656c2013-04-04 14:59:08 +0100435 mutex_unlock(&dev->mutex);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700436
Ian Abbottbe535c92013-11-08 15:03:37 +0000437 comedi_dev_put(dev);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700438 return err ? err : count;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700439}
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700440static DEVICE_ATTR_RW(max_read_buffer_kb);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700441
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700442static ssize_t read_buffer_kb_show(struct device *csdev,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700443 struct device_attribute *attr, char *buf)
444{
Ian Abbottc88db462013-04-04 14:59:09 +0100445 unsigned int minor = MINOR(csdev->devt);
Ian Abbott7f4656c2013-04-04 14:59:08 +0100446 struct comedi_device *dev;
447 struct comedi_subdevice *s;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700448 unsigned int size = 0;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700449
Ian Abbottbe535c92013-11-08 15:03:37 +0000450 dev = comedi_dev_get_from_minor(minor);
Ian Abbott5e04c252013-04-04 14:59:11 +0100451 if (!dev)
Ian Abbottc88db462013-04-04 14:59:09 +0100452 return -ENODEV;
453
Ian Abbott7f4656c2013-04-04 14:59:08 +0100454 mutex_lock(&dev->mutex);
Ian Abbottda56fdc2013-04-04 14:59:10 +0100455 s = comedi_read_subdevice(dev, minor);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700456 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
457 size = s->async->prealloc_bufsz / 1024;
Ian Abbott7f4656c2013-04-04 14:59:08 +0100458 mutex_unlock(&dev->mutex);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700459
Ian Abbottbe535c92013-11-08 15:03:37 +0000460 comedi_dev_put(dev);
Chase Southwoodecd56ff2014-02-12 02:28:35 -0600461 return snprintf(buf, PAGE_SIZE, "%u\n", size);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700462}
463
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700464static ssize_t read_buffer_kb_store(struct device *csdev,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700465 struct device_attribute *attr,
466 const char *buf, size_t count)
467{
Ian Abbottc88db462013-04-04 14:59:09 +0100468 unsigned int minor = MINOR(csdev->devt);
Ian Abbott7f4656c2013-04-04 14:59:08 +0100469 struct comedi_device *dev;
470 struct comedi_subdevice *s;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700471 unsigned int size;
472 int err;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700473
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700474 err = kstrtouint(buf, 10, &size);
475 if (err)
476 return err;
477 if (size > (UINT_MAX / 1024))
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700478 return -EINVAL;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700479 size *= 1024;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700480
Ian Abbottbe535c92013-11-08 15:03:37 +0000481 dev = comedi_dev_get_from_minor(minor);
Ian Abbott5e04c252013-04-04 14:59:11 +0100482 if (!dev)
Ian Abbottc88db462013-04-04 14:59:09 +0100483 return -ENODEV;
484
Ian Abbott7f4656c2013-04-04 14:59:08 +0100485 mutex_lock(&dev->mutex);
Ian Abbottda56fdc2013-04-04 14:59:10 +0100486 s = comedi_read_subdevice(dev, minor);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700487 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
Ian Abbott482f0a22014-05-02 13:50:14 +0100488 err = resize_async_buffer(dev, s, size);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700489 else
490 err = -EINVAL;
Ian Abbott7f4656c2013-04-04 14:59:08 +0100491 mutex_unlock(&dev->mutex);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700492
Ian Abbottbe535c92013-11-08 15:03:37 +0000493 comedi_dev_put(dev);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700494 return err ? err : count;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700495}
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700496static DEVICE_ATTR_RW(read_buffer_kb);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700497
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700498static ssize_t max_write_buffer_kb_show(struct device *csdev,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700499 struct device_attribute *attr,
500 char *buf)
501{
Ian Abbottc88db462013-04-04 14:59:09 +0100502 unsigned int minor = MINOR(csdev->devt);
Ian Abbott7f4656c2013-04-04 14:59:08 +0100503 struct comedi_device *dev;
504 struct comedi_subdevice *s;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700505 unsigned int size = 0;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700506
Ian Abbottbe535c92013-11-08 15:03:37 +0000507 dev = comedi_dev_get_from_minor(minor);
Ian Abbott5e04c252013-04-04 14:59:11 +0100508 if (!dev)
Ian Abbottc88db462013-04-04 14:59:09 +0100509 return -ENODEV;
510
Ian Abbott7f4656c2013-04-04 14:59:08 +0100511 mutex_lock(&dev->mutex);
Ian Abbottda56fdc2013-04-04 14:59:10 +0100512 s = comedi_write_subdevice(dev, minor);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700513 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
514 size = s->async->max_bufsize / 1024;
Ian Abbott7f4656c2013-04-04 14:59:08 +0100515 mutex_unlock(&dev->mutex);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700516
Ian Abbottbe535c92013-11-08 15:03:37 +0000517 comedi_dev_put(dev);
Chase Southwoodecd56ff2014-02-12 02:28:35 -0600518 return snprintf(buf, PAGE_SIZE, "%u\n", size);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700519}
520
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700521static ssize_t max_write_buffer_kb_store(struct device *csdev,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700522 struct device_attribute *attr,
523 const char *buf, size_t count)
524{
Ian Abbottc88db462013-04-04 14:59:09 +0100525 unsigned int minor = MINOR(csdev->devt);
Ian Abbott7f4656c2013-04-04 14:59:08 +0100526 struct comedi_device *dev;
527 struct comedi_subdevice *s;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700528 unsigned int size;
529 int err;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700530
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700531 err = kstrtouint(buf, 10, &size);
532 if (err)
533 return err;
534 if (size > (UINT_MAX / 1024))
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700535 return -EINVAL;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700536 size *= 1024;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700537
Ian Abbottbe535c92013-11-08 15:03:37 +0000538 dev = comedi_dev_get_from_minor(minor);
Ian Abbott5e04c252013-04-04 14:59:11 +0100539 if (!dev)
Ian Abbottc88db462013-04-04 14:59:09 +0100540 return -ENODEV;
541
Ian Abbott7f4656c2013-04-04 14:59:08 +0100542 mutex_lock(&dev->mutex);
Ian Abbottda56fdc2013-04-04 14:59:10 +0100543 s = comedi_write_subdevice(dev, minor);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700544 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
545 s->async->max_bufsize = size;
546 else
547 err = -EINVAL;
Ian Abbott7f4656c2013-04-04 14:59:08 +0100548 mutex_unlock(&dev->mutex);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700549
Ian Abbottbe535c92013-11-08 15:03:37 +0000550 comedi_dev_put(dev);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700551 return err ? err : count;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700552}
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700553static DEVICE_ATTR_RW(max_write_buffer_kb);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700554
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700555static ssize_t write_buffer_kb_show(struct device *csdev,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700556 struct device_attribute *attr, char *buf)
557{
Ian Abbottc88db462013-04-04 14:59:09 +0100558 unsigned int minor = MINOR(csdev->devt);
Ian Abbott7f4656c2013-04-04 14:59:08 +0100559 struct comedi_device *dev;
560 struct comedi_subdevice *s;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700561 unsigned int size = 0;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700562
Ian Abbottbe535c92013-11-08 15:03:37 +0000563 dev = comedi_dev_get_from_minor(minor);
Ian Abbott5e04c252013-04-04 14:59:11 +0100564 if (!dev)
Ian Abbottc88db462013-04-04 14:59:09 +0100565 return -ENODEV;
566
Ian Abbott7f4656c2013-04-04 14:59:08 +0100567 mutex_lock(&dev->mutex);
Ian Abbottda56fdc2013-04-04 14:59:10 +0100568 s = comedi_write_subdevice(dev, minor);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700569 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
570 size = s->async->prealloc_bufsz / 1024;
Ian Abbott7f4656c2013-04-04 14:59:08 +0100571 mutex_unlock(&dev->mutex);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700572
Ian Abbottbe535c92013-11-08 15:03:37 +0000573 comedi_dev_put(dev);
Chase Southwoodecd56ff2014-02-12 02:28:35 -0600574 return snprintf(buf, PAGE_SIZE, "%u\n", size);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700575}
576
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700577static ssize_t write_buffer_kb_store(struct device *csdev,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700578 struct device_attribute *attr,
579 const char *buf, size_t count)
580{
Ian Abbottc88db462013-04-04 14:59:09 +0100581 unsigned int minor = MINOR(csdev->devt);
Ian Abbott7f4656c2013-04-04 14:59:08 +0100582 struct comedi_device *dev;
583 struct comedi_subdevice *s;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700584 unsigned int size;
585 int err;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700586
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700587 err = kstrtouint(buf, 10, &size);
588 if (err)
589 return err;
590 if (size > (UINT_MAX / 1024))
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700591 return -EINVAL;
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700592 size *= 1024;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700593
Ian Abbottbe535c92013-11-08 15:03:37 +0000594 dev = comedi_dev_get_from_minor(minor);
Ian Abbott5e04c252013-04-04 14:59:11 +0100595 if (!dev)
Ian Abbottc88db462013-04-04 14:59:09 +0100596 return -ENODEV;
597
Ian Abbott7f4656c2013-04-04 14:59:08 +0100598 mutex_lock(&dev->mutex);
Ian Abbottda56fdc2013-04-04 14:59:10 +0100599 s = comedi_write_subdevice(dev, minor);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700600 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
Ian Abbott482f0a22014-05-02 13:50:14 +0100601 err = resize_async_buffer(dev, s, size);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700602 else
603 err = -EINVAL;
Ian Abbott7f4656c2013-04-04 14:59:08 +0100604 mutex_unlock(&dev->mutex);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700605
Ian Abbottbe535c92013-11-08 15:03:37 +0000606 comedi_dev_put(dev);
H Hartley Sweeten72fd9fa2012-06-05 10:18:02 -0700607 return err ? err : count;
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700608}
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700609static DEVICE_ATTR_RW(write_buffer_kb);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700610
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700611static struct attribute *comedi_dev_attrs[] = {
612 &dev_attr_max_read_buffer_kb.attr,
613 &dev_attr_read_buffer_kb.attr,
614 &dev_attr_max_write_buffer_kb.attr,
615 &dev_attr_write_buffer_kb.attr,
616 NULL,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700617};
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -0700618ATTRIBUTE_GROUPS(comedi_dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800619
Ian Abbottef4b4b22015-03-27 15:13:06 +0000620static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
Leslie Kleind4d47892016-03-21 09:18:35 -0400621 unsigned int bits)
Ian Abbottef4b4b22015-03-27 15:13:06 +0000622{
623 s->runflags &= ~bits;
624}
625
626static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
Leslie Kleind4d47892016-03-21 09:18:35 -0400627 unsigned int bits)
Ian Abbottef4b4b22015-03-27 15:13:06 +0000628{
629 s->runflags |= bits;
630}
631
Ian Abbottcc64ea42015-03-27 15:13:00 +0000632static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
Ian Abbottb2073dc2016-03-29 10:49:04 +0100633 unsigned int mask,
634 unsigned int bits)
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700635{
636 unsigned long flags;
637
638 spin_lock_irqsave(&s->spin_lock, flags);
Ian Abbottef4b4b22015-03-27 15:13:06 +0000639 __comedi_clear_subdevice_runflags(s, mask);
640 __comedi_set_subdevice_runflags(s, bits & mask);
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700641 spin_unlock_irqrestore(&s->spin_lock, flags);
642}
643
Leslie Kleind4d47892016-03-21 09:18:35 -0400644static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
Ian Abbottef4b4b22015-03-27 15:13:06 +0000645{
646 return s->runflags;
647}
648
Leslie Kleind4d47892016-03-21 09:18:35 -0400649static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice *s)
H Hartley Sweeten74120712012-12-19 15:42:26 -0700650{
651 unsigned long flags;
Leslie Kleind4d47892016-03-21 09:18:35 -0400652 unsigned int runflags;
H Hartley Sweeten74120712012-12-19 15:42:26 -0700653
654 spin_lock_irqsave(&s->spin_lock, flags);
Ian Abbottef4b4b22015-03-27 15:13:06 +0000655 runflags = __comedi_get_subdevice_runflags(s);
H Hartley Sweeten74120712012-12-19 15:42:26 -0700656 spin_unlock_irqrestore(&s->spin_lock, flags);
657 return runflags;
658}
H Hartley Sweeten74120712012-12-19 15:42:26 -0700659
Leslie Kleind4d47892016-03-21 09:18:35 -0400660static bool comedi_is_runflags_running(unsigned int runflags)
Ian Abbottb183a832015-03-27 15:13:01 +0000661{
662 return runflags & COMEDI_SRF_RUNNING;
663}
664
Leslie Kleind4d47892016-03-21 09:18:35 -0400665static bool comedi_is_runflags_in_error(unsigned int runflags)
Ian Abbottb183a832015-03-27 15:13:01 +0000666{
667 return runflags & COMEDI_SRF_ERROR;
668}
669
Ian Abbottdd630cd2015-01-28 18:41:46 +0000670/**
Ian Abbotta3e39942015-09-23 16:33:27 +0100671 * comedi_is_subdevice_running() - Check if async command running on subdevice
672 * @s: COMEDI subdevice.
Ian Abbottdd630cd2015-01-28 18:41:46 +0000673 *
Ian Abbotta3e39942015-09-23 16:33:27 +0100674 * Return: %true if an asynchronous COMEDI command is active on the
675 * subdevice, else %false.
Ian Abbottdd630cd2015-01-28 18:41:46 +0000676 */
H Hartley Sweetene0dac312012-12-19 15:42:47 -0700677bool comedi_is_subdevice_running(struct comedi_subdevice *s)
678{
Leslie Kleind4d47892016-03-21 09:18:35 -0400679 unsigned int runflags = comedi_get_subdevice_runflags(s);
H Hartley Sweetene0dac312012-12-19 15:42:47 -0700680
Ian Abbottb183a832015-03-27 15:13:01 +0000681 return comedi_is_runflags_running(runflags);
H Hartley Sweetene0dac312012-12-19 15:42:47 -0700682}
683EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
684
Ian Abbottef4b4b22015-03-27 15:13:06 +0000685static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
686{
Leslie Kleind4d47892016-03-21 09:18:35 -0400687 unsigned int runflags = __comedi_get_subdevice_runflags(s);
Ian Abbottef4b4b22015-03-27 15:13:06 +0000688
689 return comedi_is_runflags_running(runflags);
690}
691
Ian Abbott8fc369a2015-04-21 13:18:10 +0100692bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
693{
Leslie Kleind4d47892016-03-21 09:18:35 -0400694 unsigned int runflags = __comedi_get_subdevice_runflags(s);
Ian Abbott8fc369a2015-04-21 13:18:10 +0100695
696 return runflags & COMEDI_SRF_FREE_SPRIV;
697}
698
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700699/**
Ian Abbotta3e39942015-09-23 16:33:27 +0100700 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
701 * @s: COMEDI subdevice.
Ian Abbott8fc369a2015-04-21 13:18:10 +0100702 *
703 * Mark the subdevice as having a pointer to private data that can be
Ian Abbotta3e39942015-09-23 16:33:27 +0100704 * automatically freed when the COMEDI device is detached from the low-level
705 * driver.
Ian Abbott8fc369a2015-04-21 13:18:10 +0100706 */
707void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
708{
709 __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
710}
711EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
712
713/**
Ian Abbotta3e39942015-09-23 16:33:27 +0100714 * comedi_alloc_spriv - Allocate memory for the subdevice private data
715 * @s: COMEDI subdevice.
716 * @size: Size of the memory to allocate.
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700717 *
Ian Abbotta3e39942015-09-23 16:33:27 +0100718 * Allocate memory for the subdevice private data and point @s->private
719 * to it. The memory will be freed automatically when the COMEDI device
720 * is detached from the low-level driver.
721 *
722 * Return: A pointer to the allocated memory @s->private on success.
723 * Return NULL on failure.
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700724 */
H Hartley Sweeten0480bcb2013-06-19 15:24:36 -0700725void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700726{
H Hartley Sweeten0480bcb2013-06-19 15:24:36 -0700727 s->private = kzalloc(size, GFP_KERNEL);
728 if (s->private)
Ian Abbott8fc369a2015-04-21 13:18:10 +0100729 comedi_set_spriv_auto_free(s);
H Hartley Sweeten0480bcb2013-06-19 15:24:36 -0700730 return s->private;
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700731}
H Hartley Sweeten0480bcb2013-06-19 15:24:36 -0700732EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700733
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700734/*
Ian Abbotta2aab8b2015-01-28 18:41:50 +0000735 * This function restores a subdevice to an idle state.
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700736 */
737static void do_become_nonbusy(struct comedi_device *dev,
738 struct comedi_subdevice *s)
739{
740 struct comedi_async *async = s->async;
741
Ian Abbottcc64ea42015-03-27 15:13:00 +0000742 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700743 if (async) {
Ian Abbottfcc18a92014-05-06 13:12:10 +0100744 comedi_buf_reset(s);
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700745 async->inttrig = NULL;
746 kfree(async->cmd.chanlist);
747 async->cmd.chanlist = NULL;
Ian Abbott8da8c862013-11-08 15:03:27 +0000748 s->busy = NULL;
Ian Abbott258c1dd2015-03-27 15:13:03 +0000749 wake_up_interruptible_all(&async->wait_head);
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700750 } else {
751 dev_err(dev->class_dev,
752 "BUG: (?) do_become_nonbusy called with async=NULL\n");
Ian Abbott8da8c862013-11-08 15:03:27 +0000753 s->busy = NULL;
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700754 }
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700755}
756
757static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
758{
759 int ret = 0;
760
H Hartley Sweetenf0124632012-12-19 15:43:18 -0700761 if (comedi_is_subdevice_running(s) && s->cancel)
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700762 ret = s->cancel(dev, s);
763
764 do_become_nonbusy(dev, s);
765
766 return ret;
767}
768
Ian Abbottd19db512013-11-08 15:03:28 +0000769void comedi_device_cancel_all(struct comedi_device *dev)
770{
771 struct comedi_subdevice *s;
772 int i;
773
774 if (!dev->attached)
775 return;
776
777 for (i = 0; i < dev->n_subdevices; i++) {
778 s = &dev->subdevices[i];
779 if (s->async)
780 do_cancel(dev, s);
781 }
782}
783
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700784static int is_device_busy(struct comedi_device *dev)
785{
786 struct comedi_subdevice *s;
787 int i;
788
789 if (!dev->attached)
790 return 0;
791
792 for (i = 0; i < dev->n_subdevices; i++) {
793 s = &dev->subdevices[i];
794 if (s->busy)
795 return 1;
Ian Abbottd4526ab2014-05-06 13:12:11 +0100796 if (s->async && comedi_buf_is_mmapped(s))
H Hartley Sweeten2aae0072012-12-19 15:31:57 -0700797 return 1;
798 }
799
800 return 0;
801}
802
David Schleefed9eccb2008-11-04 20:29:31 -0800803/*
Ian Abbott18e01b22015-01-28 18:41:47 +0000804 * COMEDI_DEVCONFIG ioctl
805 * attaches (and configures) or detaches a legacy device
806 *
807 * arg:
808 * pointer to comedi_devconfig structure (NULL if detaching)
809 *
810 * reads:
811 * comedi_devconfig structure (if attaching)
812 *
813 * writes:
814 * nothing
815 */
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530816static int do_devconfig_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700817 struct comedi_devconfig __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800818{
Bill Pemberton0707bb02009-03-16 22:06:20 -0400819 struct comedi_devconfig it;
David Schleefed9eccb2008-11-04 20:29:31 -0800820
821 if (!capable(CAP_SYS_ADMIN))
822 return -EPERM;
823
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -0700824 if (!arg) {
David Schleefed9eccb2008-11-04 20:29:31 -0800825 if (is_device_busy(dev))
826 return -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800827 if (dev->attached) {
David Schleefed9eccb2008-11-04 20:29:31 -0800828 struct module *driver_module = dev->driver->module;
Raghavendra Ganiga4bac39f2014-05-01 13:53:12 +0530829
David Schleefed9eccb2008-11-04 20:29:31 -0800830 comedi_device_detach(dev);
831 module_put(driver_module);
832 }
833 return 0;
834 }
835
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -0700836 if (copy_from_user(&it, arg, sizeof(it)))
David Schleefed9eccb2008-11-04 20:29:31 -0800837 return -EFAULT;
838
839 it.board_name[COMEDI_NAMELEN - 1] = 0;
840
H Hartley Sweetend1843132013-01-09 09:46:10 -0700841 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
842 dev_warn(dev->class_dev,
843 "comedi_config --init_data is deprecated\n");
844 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -0800845 }
846
Ian Abbott8ab4ed62013-04-04 14:58:54 +0100847 if (dev->minor >= comedi_num_legacy_minors)
848 /* don't re-use dynamically allocated comedi devices */
849 return -EBUSY;
850
Ian Abbottb2a644b2013-04-04 14:58:56 +0100851 /* This increments the driver module count on success. */
852 return comedi_device_attach(dev, &it);
David Schleefed9eccb2008-11-04 20:29:31 -0800853}
854
855/*
Ian Abbott18e01b22015-01-28 18:41:47 +0000856 * COMEDI_BUFCONFIG ioctl
857 * buffer configuration
858 *
859 * arg:
860 * pointer to comedi_bufconfig structure
861 *
862 * reads:
863 * comedi_bufconfig structure
864 *
865 * writes:
866 * modified comedi_bufconfig structure
867 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700868static int do_bufconfig_ioctl(struct comedi_device *dev,
869 struct comedi_bufconfig __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800870{
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400871 struct comedi_bufconfig bc;
Bill Pembertond1636792009-03-16 22:05:20 -0400872 struct comedi_async *async;
Bill Pemberton34c43922009-03-16 22:05:14 -0400873 struct comedi_subdevice *s;
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400874 int retval = 0;
David Schleefed9eccb2008-11-04 20:29:31 -0800875
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -0700876 if (copy_from_user(&bc, arg, sizeof(bc)))
David Schleefed9eccb2008-11-04 20:29:31 -0800877 return -EFAULT;
878
Güngör Erseymen11f80dd2013-06-07 21:29:49 +0300879 if (bc.subdevice >= dev->n_subdevices)
David Schleefed9eccb2008-11-04 20:29:31 -0800880 return -EINVAL;
881
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -0700882 s = &dev->subdevices[bc.subdevice];
David Schleefed9eccb2008-11-04 20:29:31 -0800883 async = s->async;
884
885 if (!async) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -0700886 dev_dbg(dev->class_dev,
887 "subdevice does not have async capability\n");
David Schleefed9eccb2008-11-04 20:29:31 -0800888 bc.size = 0;
889 bc.maximum_size = 0;
890 goto copyback;
891 }
892
893 if (bc.maximum_size) {
894 if (!capable(CAP_SYS_ADMIN))
895 return -EPERM;
896
897 async->max_bufsize = bc.maximum_size;
898 }
899
900 if (bc.size) {
Ian Abbott482f0a22014-05-02 13:50:14 +0100901 retval = resize_async_buffer(dev, s, bc.size);
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400902 if (retval < 0)
903 return retval;
David Schleefed9eccb2008-11-04 20:29:31 -0800904 }
905
906 bc.size = async->prealloc_bufsz;
907 bc.maximum_size = async->max_bufsize;
908
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800909copyback:
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -0700910 if (copy_to_user(arg, &bc, sizeof(bc)))
David Schleefed9eccb2008-11-04 20:29:31 -0800911 return -EFAULT;
912
913 return 0;
914}
915
916/*
Ian Abbott18e01b22015-01-28 18:41:47 +0000917 * COMEDI_DEVINFO ioctl
918 * device info
919 *
920 * arg:
921 * pointer to comedi_devinfo structure
922 *
923 * reads:
924 * nothing
925 *
926 * writes:
927 * comedi_devinfo structure
928 */
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530929static int do_devinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700930 struct comedi_devinfo __user *arg,
931 struct file *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800932{
H Hartley Sweeten0e700922012-12-19 15:39:18 -0700933 struct comedi_subdevice *s;
934 struct comedi_devinfo devinfo;
David Schleefed9eccb2008-11-04 20:29:31 -0800935
936 memset(&devinfo, 0, sizeof(devinfo));
937
938 /* fill devinfo structure */
939 devinfo.version_code = COMEDI_VERSION_CODE;
940 devinfo.n_subdevs = dev->n_subdevices;
Vasiliy Kulikov819cbb12011-06-26 12:56:22 +0400941 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
942 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
David Schleefed9eccb2008-11-04 20:29:31 -0800943
Ian Abbott20f083c2014-11-04 18:09:00 +0000944 s = comedi_file_read_subdevice(file);
H Hartley Sweeten0e700922012-12-19 15:39:18 -0700945 if (s)
H Hartley Sweeten90a35c12012-12-19 17:27:02 -0700946 devinfo.read_subdevice = s->index;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800947 else
David Schleefed9eccb2008-11-04 20:29:31 -0800948 devinfo.read_subdevice = -1;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800949
Ian Abbott20f083c2014-11-04 18:09:00 +0000950 s = comedi_file_write_subdevice(file);
H Hartley Sweeten0e700922012-12-19 15:39:18 -0700951 if (s)
H Hartley Sweeten90a35c12012-12-19 17:27:02 -0700952 devinfo.write_subdevice = s->index;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800953 else
David Schleefed9eccb2008-11-04 20:29:31 -0800954 devinfo.write_subdevice = -1;
David Schleefed9eccb2008-11-04 20:29:31 -0800955
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -0700956 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800957 return -EFAULT;
958
959 return 0;
960}
961
962/*
Ian Abbott18e01b22015-01-28 18:41:47 +0000963 * COMEDI_SUBDINFO ioctl
964 * subdevices info
965 *
966 * arg:
967 * pointer to array of comedi_subdinfo structures
968 *
969 * reads:
970 * nothing
971 *
972 * writes:
973 * array of comedi_subdinfo structures
974 */
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530975static int do_subdinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700976 struct comedi_subdinfo __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800977{
978 int ret, i;
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400979 struct comedi_subdinfo *tmp, *us;
Bill Pemberton34c43922009-03-16 22:05:14 -0400980 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800981
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -0700982 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800983 if (!tmp)
984 return -ENOMEM;
985
986 /* fill subdinfo structs */
987 for (i = 0; i < dev->n_subdevices; i++) {
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -0700988 s = &dev->subdevices[i];
David Schleefed9eccb2008-11-04 20:29:31 -0800989 us = tmp + i;
990
991 us->type = s->type;
992 us->n_chan = s->n_chan;
993 us->subd_flags = s->subdev_flags;
H Hartley Sweetenf0124632012-12-19 15:43:18 -0700994 if (comedi_is_subdevice_running(s))
David Schleefed9eccb2008-11-04 20:29:31 -0800995 us->subd_flags |= SDF_RUNNING;
996#define TIMER_nanosec 5 /* backwards compatibility */
997 us->timer_type = TIMER_nanosec;
998 us->len_chanlist = s->len_chanlist;
999 us->maxdata = s->maxdata;
1000 if (s->range_table) {
1001 us->range_type =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001002 (i << 24) | (0 << 16) | (s->range_table->length);
David Schleefed9eccb2008-11-04 20:29:31 -08001003 } else {
1004 us->range_type = 0; /* XXX */
1005 }
David Schleefed9eccb2008-11-04 20:29:31 -08001006
1007 if (s->busy)
1008 us->subd_flags |= SDF_BUSY;
1009 if (s->busy == file)
1010 us->subd_flags |= SDF_BUSY_OWNER;
1011 if (s->lock)
1012 us->subd_flags |= SDF_LOCKED;
1013 if (s->lock == file)
1014 us->subd_flags |= SDF_LOCK_OWNER;
1015 if (!s->maxdata && s->maxdata_list)
1016 us->subd_flags |= SDF_MAXDATA;
David Schleefed9eccb2008-11-04 20:29:31 -08001017 if (s->range_table_list)
1018 us->subd_flags |= SDF_RANGETYPE;
1019 if (s->do_cmd)
1020 us->subd_flags |= SDF_CMD;
1021
1022 if (s->insn_bits != &insn_inval)
1023 us->insn_bits_support = COMEDI_SUPPORTED;
1024 else
1025 us->insn_bits_support = COMEDI_UNSUPPORTED;
David Schleefed9eccb2008-11-04 20:29:31 -08001026 }
1027
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -07001028 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
David Schleefed9eccb2008-11-04 20:29:31 -08001029
1030 kfree(tmp);
1031
1032 return ret ? -EFAULT : 0;
1033}
1034
1035/*
Ian Abbott18e01b22015-01-28 18:41:47 +00001036 * COMEDI_CHANINFO ioctl
1037 * subdevice channel info
1038 *
1039 * arg:
1040 * pointer to comedi_chaninfo structure
1041 *
1042 * reads:
1043 * comedi_chaninfo structure
1044 *
1045 * writes:
1046 * array of maxdata values to chaninfo->maxdata_list if requested
1047 * array of range table lengths to chaninfo->range_table_list if requested
1048 */
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301049static int do_chaninfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001050 struct comedi_chaninfo __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -08001051{
Bill Pemberton34c43922009-03-16 22:05:14 -04001052 struct comedi_subdevice *s;
Bill Pembertona18b4162009-03-16 22:06:04 -04001053 struct comedi_chaninfo it;
David Schleefed9eccb2008-11-04 20:29:31 -08001054
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -07001055 if (copy_from_user(&it, arg, sizeof(it)))
David Schleefed9eccb2008-11-04 20:29:31 -08001056 return -EFAULT;
1057
1058 if (it.subdev >= dev->n_subdevices)
1059 return -EINVAL;
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07001060 s = &dev->subdevices[it.subdev];
David Schleefed9eccb2008-11-04 20:29:31 -08001061
1062 if (it.maxdata_list) {
1063 if (s->maxdata || !s->maxdata_list)
1064 return -EINVAL;
1065 if (copy_to_user(it.maxdata_list, s->maxdata_list,
Bill Pemberton790c5542009-03-16 22:05:02 -04001066 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -08001067 return -EFAULT;
1068 }
1069
Ian Abbott64d9b1d2013-10-07 16:50:05 +01001070 if (it.flaglist)
1071 return -EINVAL; /* flaglist not supported */
David Schleefed9eccb2008-11-04 20:29:31 -08001072
1073 if (it.rangelist) {
1074 int i;
1075
1076 if (!s->range_table_list)
1077 return -EINVAL;
1078 for (i = 0; i < s->n_chan; i++) {
1079 int x;
1080
1081 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001082 (s->range_table_list[i]->length);
Vasiliy Kulikov81604d42010-09-05 22:32:33 +04001083 if (put_user(x, it.rangelist + i))
1084 return -EFAULT;
David Schleefed9eccb2008-11-04 20:29:31 -08001085 }
David Schleefed9eccb2008-11-04 20:29:31 -08001086 }
1087
1088 return 0;
1089}
1090
Ian Abbott18e01b22015-01-28 18:41:47 +00001091/*
1092 * COMEDI_BUFINFO ioctl
1093 * buffer information
1094 *
1095 * arg:
1096 * pointer to comedi_bufinfo structure
1097 *
1098 * reads:
1099 * comedi_bufinfo structure
1100 *
1101 * writes:
1102 * modified comedi_bufinfo structure
1103 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001104static int do_bufinfo_ioctl(struct comedi_device *dev,
Ian Abbott53fa8272010-05-19 18:09:50 +01001105 struct comedi_bufinfo __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001106{
Bill Pemberton9aa53392009-03-16 22:06:42 -04001107 struct comedi_bufinfo bi;
Bill Pemberton34c43922009-03-16 22:05:14 -04001108 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001109 struct comedi_async *async;
Ian Abbott36a51172016-02-19 16:13:56 +00001110 unsigned int runflags;
1111 int retval = 0;
Ian Abbott06578562016-02-19 16:13:52 +00001112 bool become_nonbusy = false;
David Schleefed9eccb2008-11-04 20:29:31 -08001113
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -07001114 if (copy_from_user(&bi, arg, sizeof(bi)))
David Schleefed9eccb2008-11-04 20:29:31 -08001115 return -EFAULT;
1116
Güngör Erseymen7b1a5e22013-06-07 21:29:50 +03001117 if (bi.subdevice >= dev->n_subdevices)
David Schleefed9eccb2008-11-04 20:29:31 -08001118 return -EINVAL;
1119
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07001120 s = &dev->subdevices[bi.subdevice];
Ian Abbott53fa8272010-05-19 18:09:50 +01001121
David Schleefed9eccb2008-11-04 20:29:31 -08001122 async = s->async;
1123
Ian Abbott57c563b2016-02-19 16:13:54 +00001124 if (!async || s->busy != file)
1125 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -08001126
Ian Abbottbe611a12016-02-19 16:13:57 +00001127 runflags = comedi_get_subdevice_runflags(s);
Ian Abbott66c36502016-02-19 16:13:51 +00001128 if (!(async->cmd.flags & CMDF_WRITE)) {
1129 /* command was set up in "read" direction */
1130 if (bi.bytes_read) {
1131 comedi_buf_read_alloc(s, bi.bytes_read);
1132 bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read);
David Schleefed9eccb2008-11-04 20:29:31 -08001133 }
Ian Abbott36a51172016-02-19 16:13:56 +00001134 /*
1135 * If nothing left to read, and command has stopped, and
1136 * {"read" position not updated or command stopped normally},
1137 * then become non-busy.
1138 */
Ian Abbott36a51172016-02-19 16:13:56 +00001139 if (comedi_buf_read_n_available(s) == 0 &&
1140 !comedi_is_runflags_running(runflags) &&
1141 (bi.bytes_read == 0 ||
1142 !comedi_is_runflags_in_error(runflags))) {
Ian Abbottf3aa8c02016-02-19 16:13:55 +00001143 become_nonbusy = true;
Ian Abbott36a51172016-02-19 16:13:56 +00001144 if (comedi_is_runflags_in_error(runflags))
1145 retval = -EPIPE;
1146 }
Ian Abbott66c36502016-02-19 16:13:51 +00001147 bi.bytes_written = 0;
1148 } else {
1149 /* command was set up in "write" direction */
Ian Abbottbe611a12016-02-19 16:13:57 +00001150 if (!comedi_is_runflags_running(runflags)) {
Ian Abbottbb0c6bf2016-02-19 16:13:53 +00001151 bi.bytes_written = 0;
Ian Abbottbe611a12016-02-19 16:13:57 +00001152 become_nonbusy = true;
1153 if (comedi_is_runflags_in_error(runflags))
1154 retval = -EPIPE;
1155 } else if (bi.bytes_written) {
Ian Abbott66c36502016-02-19 16:13:51 +00001156 comedi_buf_write_alloc(s, bi.bytes_written);
1157 bi.bytes_written =
1158 comedi_buf_write_free(s, bi.bytes_written);
1159 }
1160 bi.bytes_read = 0;
David Schleefed9eccb2008-11-04 20:29:31 -08001161 }
1162
1163 bi.buf_write_count = async->buf_write_count;
1164 bi.buf_write_ptr = async->buf_write_ptr;
1165 bi.buf_read_count = async->buf_read_count;
1166 bi.buf_read_ptr = async->buf_read_ptr;
1167
Ian Abbott06578562016-02-19 16:13:52 +00001168 if (become_nonbusy)
1169 do_become_nonbusy(dev, s);
1170
Ian Abbott36a51172016-02-19 16:13:56 +00001171 if (retval)
1172 return retval;
1173
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -07001174 if (copy_to_user(arg, &bi, sizeof(bi)))
David Schleefed9eccb2008-11-04 20:29:31 -08001175 return -EFAULT;
1176
1177 return 0;
1178}
1179
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301180static int check_insn_config_length(struct comedi_insn *insn,
1181 unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -08001182{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001183 if (insn->n < 1)
1184 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -08001185
1186 switch (data[0]) {
1187 case INSN_CONFIG_DIO_OUTPUT:
1188 case INSN_CONFIG_DIO_INPUT:
1189 case INSN_CONFIG_DISARM:
1190 case INSN_CONFIG_RESET:
1191 if (insn->n == 1)
1192 return 0;
1193 break;
1194 case INSN_CONFIG_ARM:
1195 case INSN_CONFIG_DIO_QUERY:
1196 case INSN_CONFIG_BLOCK_SIZE:
1197 case INSN_CONFIG_FILTER:
1198 case INSN_CONFIG_SERIAL_CLOCK:
1199 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1200 case INSN_CONFIG_ALT_SOURCE:
1201 case INSN_CONFIG_SET_COUNTER_MODE:
1202 case INSN_CONFIG_8254_READ_STATUS:
1203 case INSN_CONFIG_SET_ROUTING:
1204 case INSN_CONFIG_GET_ROUTING:
1205 case INSN_CONFIG_GET_PWM_STATUS:
1206 case INSN_CONFIG_PWM_SET_PERIOD:
1207 case INSN_CONFIG_PWM_GET_PERIOD:
1208 if (insn->n == 2)
1209 return 0;
1210 break;
1211 case INSN_CONFIG_SET_GATE_SRC:
1212 case INSN_CONFIG_GET_GATE_SRC:
1213 case INSN_CONFIG_SET_CLOCK_SRC:
1214 case INSN_CONFIG_GET_CLOCK_SRC:
1215 case INSN_CONFIG_SET_OTHER_SRC:
1216 case INSN_CONFIG_GET_COUNTER_STATUS:
1217 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1218 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1219 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1220 if (insn->n == 3)
1221 return 0;
1222 break;
1223 case INSN_CONFIG_PWM_OUTPUT:
1224 case INSN_CONFIG_ANALOG_TRIG:
1225 if (insn->n == 5)
1226 return 0;
1227 break;
Ian Abbottb0a2b6d2012-11-14 11:22:57 +00001228 case INSN_CONFIG_DIGITAL_TRIG:
1229 if (insn->n == 6)
1230 return 0;
1231 break;
Ian Abbotta2aab8b2015-01-28 18:41:50 +00001232 /*
1233 * by default we allow the insn since we don't have checks for
1234 * all possible cases yet
1235 */
David Schleefed9eccb2008-11-04 20:29:31 -08001236 default:
H Hartley Sweetenc2ad0782014-07-17 12:27:32 -07001237 pr_warn("No check for data length of config insn id %i is implemented\n",
Ian Abbott4f870fe2012-08-16 14:38:05 +01001238 data[0]);
H Hartley Sweetenc2ad0782014-07-17 12:27:32 -07001239 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1240 pr_warn("Assuming n=%i is correct\n", insn->n);
David Schleefed9eccb2008-11-04 20:29:31 -08001241 return 0;
David Schleefed9eccb2008-11-04 20:29:31 -08001242 }
1243 return -EINVAL;
1244}
1245
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301246static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1247 unsigned int *data, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001248{
Bill Pemberton34c43922009-03-16 22:05:14 -04001249 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001250 int ret = 0;
1251 int i;
1252
1253 if (insn->insn & INSN_MASK_SPECIAL) {
1254 /* a non-subdevice instruction */
1255
1256 switch (insn->insn) {
1257 case INSN_GTOD:
1258 {
Arnd Bergmann70db3842016-06-17 21:56:15 +02001259 struct timespec64 tv;
David Schleefed9eccb2008-11-04 20:29:31 -08001260
1261 if (insn->n != 2) {
1262 ret = -EINVAL;
1263 break;
1264 }
1265
Arnd Bergmann70db3842016-06-17 21:56:15 +02001266 ktime_get_real_ts64(&tv);
1267 /* unsigned data safe until 2106 */
1268 data[0] = (unsigned int)tv.tv_sec;
1269 data[1] = tv.tv_nsec / NSEC_PER_USEC;
David Schleefed9eccb2008-11-04 20:29:31 -08001270 ret = 2;
1271
1272 break;
1273 }
1274 case INSN_WAIT:
1275 if (insn->n != 1 || data[0] >= 100000) {
1276 ret = -EINVAL;
1277 break;
1278 }
1279 udelay(data[0] / 1000);
1280 ret = 1;
1281 break;
1282 case INSN_INTTRIG:
1283 if (insn->n != 1) {
1284 ret = -EINVAL;
1285 break;
1286 }
1287 if (insn->subdev >= dev->n_subdevices) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001288 dev_dbg(dev->class_dev,
1289 "%d not usable subdevice\n",
David Schleefed9eccb2008-11-04 20:29:31 -08001290 insn->subdev);
1291 ret = -EINVAL;
1292 break;
1293 }
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07001294 s = &dev->subdevices[insn->subdev];
David Schleefed9eccb2008-11-04 20:29:31 -08001295 if (!s->async) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001296 dev_dbg(dev->class_dev, "no async\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001297 ret = -EINVAL;
1298 break;
1299 }
1300 if (!s->async->inttrig) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001301 dev_dbg(dev->class_dev, "no inttrig\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001302 ret = -EAGAIN;
1303 break;
1304 }
Ian Abbott5d06e3d2012-09-18 19:46:58 +01001305 ret = s->async->inttrig(dev, s, data[0]);
David Schleefed9eccb2008-11-04 20:29:31 -08001306 if (ret >= 0)
1307 ret = 1;
1308 break;
1309 default:
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001310 dev_dbg(dev->class_dev, "invalid insn\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001311 ret = -EINVAL;
1312 break;
1313 }
1314 } else {
1315 /* a subdevice instruction */
Bill Pemberton790c5542009-03-16 22:05:02 -04001316 unsigned int maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -08001317
1318 if (insn->subdev >= dev->n_subdevices) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001319 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1320 insn->subdev);
David Schleefed9eccb2008-11-04 20:29:31 -08001321 ret = -EINVAL;
1322 goto out;
1323 }
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07001324 s = &dev->subdevices[insn->subdev];
David Schleefed9eccb2008-11-04 20:29:31 -08001325
1326 if (s->type == COMEDI_SUBD_UNUSED) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001327 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1328 insn->subdev);
David Schleefed9eccb2008-11-04 20:29:31 -08001329 ret = -EIO;
1330 goto out;
1331 }
1332
1333 /* are we locked? (ioctl lock) */
1334 if (s->lock && s->lock != file) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001335 dev_dbg(dev->class_dev, "device locked\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001336 ret = -EACCES;
1337 goto out;
1338 }
1339
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -07001340 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001341 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001342 ret = -EINVAL;
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001343 dev_dbg(dev->class_dev, "bad chanspec\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001344 goto out;
1345 }
1346
1347 if (s->busy) {
1348 ret = -EBUSY;
1349 goto out;
1350 }
1351 /* This looks arbitrary. It is. */
Shraddha Barke30cc9bd2015-10-13 21:07:47 +05301352 s->busy = parse_insn;
David Schleefed9eccb2008-11-04 20:29:31 -08001353 switch (insn->insn) {
1354 case INSN_READ:
1355 ret = s->insn_read(dev, s, insn, data);
H Hartley Sweeten22ca19d2014-02-10 11:49:45 -07001356 if (ret == -ETIMEDOUT) {
1357 dev_dbg(dev->class_dev,
1358 "subdevice %d read instruction timed out\n",
1359 s->index);
1360 }
David Schleefed9eccb2008-11-04 20:29:31 -08001361 break;
1362 case INSN_WRITE:
1363 maxdata = s->maxdata_list
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001364 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1365 : s->maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -08001366 for (i = 0; i < insn->n; ++i) {
1367 if (data[i] > maxdata) {
1368 ret = -EINVAL;
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001369 dev_dbg(dev->class_dev,
1370 "bad data value(s)\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001371 break;
1372 }
1373 }
H Hartley Sweeten22ca19d2014-02-10 11:49:45 -07001374 if (ret == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001375 ret = s->insn_write(dev, s, insn, data);
H Hartley Sweeten22ca19d2014-02-10 11:49:45 -07001376 if (ret == -ETIMEDOUT) {
1377 dev_dbg(dev->class_dev,
1378 "subdevice %d write instruction timed out\n",
1379 s->index);
1380 }
1381 }
David Schleefed9eccb2008-11-04 20:29:31 -08001382 break;
1383 case INSN_BITS:
1384 if (insn->n != 2) {
1385 ret = -EINVAL;
Ian Abbott2f644cc2011-01-18 17:44:33 +00001386 } else {
Ian Abbotta2aab8b2015-01-28 18:41:50 +00001387 /*
1388 * Most drivers ignore the base channel in
Ian Abbott2f644cc2011-01-18 17:44:33 +00001389 * insn->chanspec. Fix this here if
Ian Abbotta2aab8b2015-01-28 18:41:50 +00001390 * the subdevice has <= 32 channels.
1391 */
H Hartley Sweeten36efbac2014-07-18 14:28:14 -07001392 unsigned int orig_mask = data[0];
1393 unsigned int shift = 0;
Ian Abbott2f644cc2011-01-18 17:44:33 +00001394
Ian Abbott2f644cc2011-01-18 17:44:33 +00001395 if (s->n_chan <= 32) {
1396 shift = CR_CHAN(insn->chanspec);
1397 if (shift > 0) {
1398 insn->chanspec = 0;
1399 data[0] <<= shift;
1400 data[1] <<= shift;
1401 }
H Hartley Sweeten36efbac2014-07-18 14:28:14 -07001402 }
Ian Abbott2f644cc2011-01-18 17:44:33 +00001403 ret = s->insn_bits(dev, s, insn, data);
1404 data[0] = orig_mask;
1405 if (shift > 0)
1406 data[1] >>= shift;
David Schleefed9eccb2008-11-04 20:29:31 -08001407 }
David Schleefed9eccb2008-11-04 20:29:31 -08001408 break;
1409 case INSN_CONFIG:
1410 ret = check_insn_config_length(insn, data);
1411 if (ret)
1412 break;
1413 ret = s->insn_config(dev, s, insn, data);
1414 break;
1415 default:
1416 ret = -EINVAL;
1417 break;
1418 }
1419
1420 s->busy = NULL;
1421 }
1422
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001423out:
David Schleefed9eccb2008-11-04 20:29:31 -08001424 return ret;
1425}
1426
1427/*
Ian Abbott18e01b22015-01-28 18:41:47 +00001428 * COMEDI_INSNLIST ioctl
1429 * synchronous instruction list
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001430 *
Ian Abbott18e01b22015-01-28 18:41:47 +00001431 * arg:
1432 * pointer to comedi_insnlist structure
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001433 *
Ian Abbott18e01b22015-01-28 18:41:47 +00001434 * reads:
1435 * comedi_insnlist structure
1436 * array of comedi_insn structures from insnlist->insns pointer
1437 * data (for writes) from insns[].data pointers
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001438 *
Ian Abbott18e01b22015-01-28 18:41:47 +00001439 * writes:
1440 * data (for reads) to insns[].data pointers
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001441 */
1442/* arbitrary limits */
1443#define MAX_SAMPLES 256
1444static int do_insnlist_ioctl(struct comedi_device *dev,
1445 struct comedi_insnlist __user *arg, void *file)
1446{
1447 struct comedi_insnlist insnlist;
1448 struct comedi_insn *insns = NULL;
1449 unsigned int *data = NULL;
1450 int i = 0;
1451 int ret = 0;
1452
1453 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1454 return -EFAULT;
1455
Wolfgang Ockerf4a8f522014-06-02 22:48:06 +02001456 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001457 if (!data) {
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001458 ret = -ENOMEM;
1459 goto error;
1460 }
1461
1462 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1463 if (!insns) {
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001464 ret = -ENOMEM;
1465 goto error;
1466 }
1467
1468 if (copy_from_user(insns, insnlist.insns,
1469 sizeof(*insns) * insnlist.n_insns)) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001470 dev_dbg(dev->class_dev, "copy_from_user failed\n");
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001471 ret = -EFAULT;
1472 goto error;
1473 }
1474
1475 for (i = 0; i < insnlist.n_insns; i++) {
1476 if (insns[i].n > MAX_SAMPLES) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001477 dev_dbg(dev->class_dev,
1478 "number of samples too large\n");
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001479 ret = -EINVAL;
1480 goto error;
1481 }
1482 if (insns[i].insn & INSN_MASK_WRITE) {
1483 if (copy_from_user(data, insns[i].data,
1484 insns[i].n * sizeof(unsigned int))) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001485 dev_dbg(dev->class_dev,
1486 "copy_from_user failed\n");
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001487 ret = -EFAULT;
1488 goto error;
1489 }
1490 }
1491 ret = parse_insn(dev, insns + i, data, file);
1492 if (ret < 0)
1493 goto error;
1494 if (insns[i].insn & INSN_MASK_READ) {
1495 if (copy_to_user(insns[i].data, data,
1496 insns[i].n * sizeof(unsigned int))) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001497 dev_dbg(dev->class_dev,
1498 "copy_to_user failed\n");
H Hartley Sweeten5b6cbd82013-01-21 14:02:59 -07001499 ret = -EFAULT;
1500 goto error;
1501 }
1502 }
1503 if (need_resched())
1504 schedule();
1505 }
1506
1507error:
1508 kfree(insns);
1509 kfree(data);
1510
1511 if (ret < 0)
1512 return ret;
1513 return i;
1514}
1515
1516/*
Ian Abbott18e01b22015-01-28 18:41:47 +00001517 * COMEDI_INSN ioctl
1518 * synchronous instruction
David Schleefed9eccb2008-11-04 20:29:31 -08001519 *
Ian Abbott18e01b22015-01-28 18:41:47 +00001520 * arg:
1521 * pointer to comedi_insn structure
David Schleefed9eccb2008-11-04 20:29:31 -08001522 *
Ian Abbott18e01b22015-01-28 18:41:47 +00001523 * reads:
1524 * comedi_insn structure
1525 * data (for writes) from insn->data pointer
David Schleefed9eccb2008-11-04 20:29:31 -08001526 *
Ian Abbott18e01b22015-01-28 18:41:47 +00001527 * writes:
1528 * data (for reads) to insn->data pointer
David Schleefed9eccb2008-11-04 20:29:31 -08001529 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001530static int do_insn_ioctl(struct comedi_device *dev,
1531 struct comedi_insn __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001532{
Bill Pemberton90035c02009-03-16 22:05:53 -04001533 struct comedi_insn insn;
Bill Pemberton790c5542009-03-16 22:05:02 -04001534 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001535 int ret = 0;
1536
Wolfgang Ockerf4a8f522014-06-02 22:48:06 +02001537 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001538 if (!data) {
1539 ret = -ENOMEM;
1540 goto error;
1541 }
1542
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -07001543 if (copy_from_user(&insn, arg, sizeof(insn))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001544 ret = -EFAULT;
1545 goto error;
1546 }
1547
1548 /* This is where the behavior of insn and insnlist deviate. */
1549 if (insn.n > MAX_SAMPLES)
1550 insn.n = MAX_SAMPLES;
1551 if (insn.insn & INSN_MASK_WRITE) {
Mark21fe2ee2010-05-13 17:44:39 +08001552 if (copy_from_user(data,
1553 insn.data,
1554 insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001555 ret = -EFAULT;
1556 goto error;
1557 }
1558 }
1559 ret = parse_insn(dev, &insn, data, file);
1560 if (ret < 0)
1561 goto error;
1562 if (insn.insn & INSN_MASK_READ) {
Mark21fe2ee2010-05-13 17:44:39 +08001563 if (copy_to_user(insn.data,
1564 data,
1565 insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001566 ret = -EFAULT;
1567 goto error;
1568 }
1569 }
1570 ret = insn.n;
1571
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001572error:
1573 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -08001574
1575 return ret;
1576}
1577
H Hartley Sweeten87ece582014-03-06 12:02:56 -07001578static int __comedi_get_user_cmd(struct comedi_device *dev,
1579 struct comedi_cmd __user *arg,
1580 struct comedi_cmd *cmd)
1581{
1582 struct comedi_subdevice *s;
1583
1584 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1585 dev_dbg(dev->class_dev, "bad cmd address\n");
1586 return -EFAULT;
1587 }
1588
1589 if (cmd->subdev >= dev->n_subdevices) {
1590 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1591 return -ENODEV;
1592 }
1593
1594 s = &dev->subdevices[cmd->subdev];
1595
1596 if (s->type == COMEDI_SUBD_UNUSED) {
Yves Deweerdtb2f48742014-03-31 22:55:39 +02001597 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1598 cmd->subdev);
H Hartley Sweeten87ece582014-03-06 12:02:56 -07001599 return -EIO;
1600 }
1601
1602 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1603 dev_dbg(dev->class_dev,
Yves Deweerdtb2f48742014-03-31 22:55:39 +02001604 "subdevice %d does not support commands\n",
1605 cmd->subdev);
H Hartley Sweeten87ece582014-03-06 12:02:56 -07001606 return -EIO;
1607 }
1608
1609 /* make sure channel/gain list isn't too long */
1610 if (cmd->chanlist_len > s->len_chanlist) {
1611 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1612 cmd->chanlist_len, s->len_chanlist);
1613 return -EINVAL;
1614 }
1615
Ian Abbott5d070cf2014-10-30 12:42:26 +00001616 /*
1617 * Set the CMDF_WRITE flag to the correct state if the subdevice
1618 * supports only "read" commands or only "write" commands.
1619 */
1620 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1621 case SDF_CMD_READ:
1622 cmd->flags &= ~CMDF_WRITE;
1623 break;
1624 case SDF_CMD_WRITE:
1625 cmd->flags |= CMDF_WRITE;
1626 break;
1627 default:
1628 break;
1629 }
1630
H Hartley Sweeten87ece582014-03-06 12:02:56 -07001631 return 0;
1632}
1633
H Hartley Sweetenc6cd0ee2014-03-06 12:02:57 -07001634static int __comedi_get_user_chanlist(struct comedi_device *dev,
1635 struct comedi_subdevice *s,
1636 unsigned int __user *user_chanlist,
1637 struct comedi_cmd *cmd)
1638{
1639 unsigned int *chanlist;
1640 int ret;
1641
Ian Abbott238b5ad2014-10-20 15:10:40 +01001642 cmd->chanlist = NULL;
H Hartley Sweetenc6cd0ee2014-03-06 12:02:57 -07001643 chanlist = memdup_user(user_chanlist,
1644 cmd->chanlist_len * sizeof(unsigned int));
1645 if (IS_ERR(chanlist))
1646 return PTR_ERR(chanlist);
1647
1648 /* make sure each element in channel/gain list is valid */
1649 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1650 if (ret < 0) {
1651 kfree(chanlist);
1652 return ret;
1653 }
1654
1655 cmd->chanlist = chanlist;
1656
1657 return 0;
1658}
1659
Ian Abbott18e01b22015-01-28 18:41:47 +00001660/*
1661 * COMEDI_CMD ioctl
1662 * asynchronous acquisition command set-up
1663 *
1664 * arg:
1665 * pointer to comedi_cmd structure
1666 *
1667 * reads:
1668 * comedi_cmd structure
1669 * channel/range list from cmd->chanlist pointer
1670 *
1671 * writes:
1672 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1673 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001674static int do_cmd_ioctl(struct comedi_device *dev,
H Hartley Sweetencbe01f72012-09-18 11:41:54 -07001675 struct comedi_cmd __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001676{
H Hartley Sweeten88bc0572012-09-18 11:42:37 -07001677 struct comedi_cmd cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001678 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001679 struct comedi_async *async;
H Hartley Sweeten95bc3592012-09-18 11:43:52 -07001680 unsigned int __user *user_chanlist;
H Hartley Sweeten87ece582014-03-06 12:02:56 -07001681 int ret;
David Schleefed9eccb2008-11-04 20:29:31 -08001682
H Hartley Sweeten87ece582014-03-06 12:02:56 -07001683 /* get the user's cmd and do some simple validation */
1684 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1685 if (ret)
1686 return ret;
1687
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001688 /* save user's chanlist pointer so it can be restored later */
H Hartley Sweeten95bc3592012-09-18 11:43:52 -07001689 user_chanlist = (unsigned int __user *)cmd.chanlist;
David Schleefed9eccb2008-11-04 20:29:31 -08001690
H Hartley Sweeten88bc0572012-09-18 11:42:37 -07001691 s = &dev->subdevices[cmd.subdev];
David Schleefed9eccb2008-11-04 20:29:31 -08001692 async = s->async;
1693
David Schleefed9eccb2008-11-04 20:29:31 -08001694 /* are we locked? (ioctl lock) */
1695 if (s->lock && s->lock != file) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001696 dev_dbg(dev->class_dev, "subdevice locked\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001697 return -EACCES;
1698 }
1699
1700 /* are we busy? */
1701 if (s->busy) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001702 dev_dbg(dev->class_dev, "subdevice busy\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001703 return -EBUSY;
1704 }
David Schleefed9eccb2008-11-04 20:29:31 -08001705
David Schleefed9eccb2008-11-04 20:29:31 -08001706 /* make sure channel/gain list isn't too short */
H Hartley Sweeten88bc0572012-09-18 11:42:37 -07001707 if (cmd.chanlist_len < 1) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001708 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
H Hartley Sweeten88bc0572012-09-18 11:42:37 -07001709 cmd.chanlist_len);
Ian Abbott4b18f082013-07-05 16:49:34 +01001710 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -08001711 }
1712
H Hartley Sweeten88bc0572012-09-18 11:42:37 -07001713 async->cmd = cmd;
David Schleefed9eccb2008-11-04 20:29:31 -08001714 async->cmd.data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001715
H Hartley Sweetenc6cd0ee2014-03-06 12:02:57 -07001716 /* load channel/gain list */
1717 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1718 if (ret)
David Schleefed9eccb2008-11-04 20:29:31 -08001719 goto cleanup;
David Schleefed9eccb2008-11-04 20:29:31 -08001720
1721 ret = s->do_cmdtest(dev, s, &async->cmd);
1722
Ian Abbottb0446a22014-09-03 13:45:44 +01001723 if (async->cmd.flags & CMDF_BOGUS || ret) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001724 dev_dbg(dev->class_dev, "test returned %d\n", ret);
H Hartley Sweeten88bc0572012-09-18 11:42:37 -07001725 cmd = async->cmd;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001726 /* restore chanlist pointer before copying back */
H Hartley Sweeten95bc3592012-09-18 11:43:52 -07001727 cmd.chanlist = (unsigned int __force *)user_chanlist;
H Hartley Sweeten88bc0572012-09-18 11:42:37 -07001728 cmd.data = NULL;
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -07001729 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001730 dev_dbg(dev->class_dev, "fault writing cmd\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001731 ret = -EFAULT;
1732 goto cleanup;
1733 }
1734 ret = -EAGAIN;
1735 goto cleanup;
1736 }
1737
1738 if (!async->prealloc_bufsz) {
1739 ret = -ENOMEM;
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001740 dev_dbg(dev->class_dev, "no buffer (?)\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001741 goto cleanup;
1742 }
1743
Ian Abbottfcc18a92014-05-06 13:12:10 +01001744 comedi_buf_reset(s);
David Schleefed9eccb2008-11-04 20:29:31 -08001745
H Hartley Sweeten781f9332014-10-13 09:56:08 -07001746 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
Ian Abbottd8bff6e2014-09-03 13:45:30 +01001747 if (async->cmd.flags & CMDF_WAKE_EOS)
David Schleefed9eccb2008-11-04 20:29:31 -08001748 async->cb_mask |= COMEDI_CB_EOS;
David Schleefed9eccb2008-11-04 20:29:31 -08001749
Ian Abbottcc64ea42015-03-27 15:13:00 +00001750 comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1751 COMEDI_SRF_RUNNING);
David Schleefed9eccb2008-11-04 20:29:31 -08001752
H Hartley Sweeten84bb0bc2015-01-20 12:06:00 -07001753 /*
1754 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1755 * race with comedi_read() or comedi_write().
1756 */
Ian Abbott4b18f082013-07-05 16:49:34 +01001757 s->busy = file;
David Schleefed9eccb2008-11-04 20:29:31 -08001758 ret = s->do_cmd(dev, s);
1759 if (ret == 0)
1760 return 0;
1761
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001762cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -08001763 do_become_nonbusy(dev, s);
1764
1765 return ret;
1766}
1767
1768/*
Ian Abbott18e01b22015-01-28 18:41:47 +00001769 * COMEDI_CMDTEST ioctl
Carlos E. Garcia69e98df2015-04-24 09:40:42 -04001770 * asynchronous acquisition command testing
Ian Abbott18e01b22015-01-28 18:41:47 +00001771 *
1772 * arg:
1773 * pointer to comedi_cmd structure
1774 *
1775 * reads:
1776 * comedi_cmd structure
1777 * channel/range list from cmd->chanlist pointer
1778 *
1779 * writes:
1780 * possibly modified comedi_cmd structure
1781 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001782static int do_cmdtest_ioctl(struct comedi_device *dev,
1783 struct comedi_cmd __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001784{
H Hartley Sweetenf8348672012-09-18 11:43:13 -07001785 struct comedi_cmd cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001786 struct comedi_subdevice *s;
H Hartley Sweeten95bc3592012-09-18 11:43:52 -07001787 unsigned int __user *user_chanlist;
H Hartley Sweeten87ece582014-03-06 12:02:56 -07001788 int ret;
David Schleefed9eccb2008-11-04 20:29:31 -08001789
H Hartley Sweeten87ece582014-03-06 12:02:56 -07001790 /* get the user's cmd and do some simple validation */
1791 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1792 if (ret)
1793 return ret;
1794
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001795 /* save user's chanlist pointer so it can be restored later */
H Hartley Sweeten95bc3592012-09-18 11:43:52 -07001796 user_chanlist = (unsigned int __user *)cmd.chanlist;
David Schleefed9eccb2008-11-04 20:29:31 -08001797
H Hartley Sweetenf8348672012-09-18 11:43:13 -07001798 s = &dev->subdevices[cmd.subdev];
David Schleefed9eccb2008-11-04 20:29:31 -08001799
Ian Abbott6cab7a32014-10-08 16:09:14 +01001800 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1801 if (user_chanlist) {
1802 /* load channel/gain list */
1803 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1804 if (ret)
1805 return ret;
1806 }
David Schleefed9eccb2008-11-04 20:29:31 -08001807
H Hartley Sweetenf8348672012-09-18 11:43:13 -07001808 ret = s->do_cmdtest(dev, s, &cmd);
David Schleefed9eccb2008-11-04 20:29:31 -08001809
Ian Abbott238b5ad2014-10-20 15:10:40 +01001810 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1811
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001812 /* restore chanlist pointer before copying back */
H Hartley Sweeten95bc3592012-09-18 11:43:52 -07001813 cmd.chanlist = (unsigned int __force *)user_chanlist;
David Schleefed9eccb2008-11-04 20:29:31 -08001814
H Hartley Sweetenbc252fd2012-12-19 15:42:02 -07001815 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07001816 dev_dbg(dev->class_dev, "bad cmd address\n");
David Schleefed9eccb2008-11-04 20:29:31 -08001817 ret = -EFAULT;
David Schleefed9eccb2008-11-04 20:29:31 -08001818 }
H Hartley Sweetenc6cd0ee2014-03-06 12:02:57 -07001819
David Schleefed9eccb2008-11-04 20:29:31 -08001820 return ret;
1821}
1822
1823/*
Ian Abbott18e01b22015-01-28 18:41:47 +00001824 * COMEDI_LOCK ioctl
1825 * lock subdevice
1826 *
1827 * arg:
1828 * subdevice number
1829 *
1830 * reads:
1831 * nothing
1832 *
1833 * writes:
1834 * nothing
1835 */
Ian Abbottc1a6eac2014-10-28 17:15:47 +00001836static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301837 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001838{
1839 int ret = 0;
1840 unsigned long flags;
Bill Pemberton34c43922009-03-16 22:05:14 -04001841 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001842
1843 if (arg >= dev->n_subdevices)
1844 return -EINVAL;
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07001845 s = &dev->subdevices[arg];
David Schleefed9eccb2008-11-04 20:29:31 -08001846
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001847 spin_lock_irqsave(&s->spin_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001848 if (s->busy || s->lock)
David Schleefed9eccb2008-11-04 20:29:31 -08001849 ret = -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001850 else
David Schleefed9eccb2008-11-04 20:29:31 -08001851 s->lock = file;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001852 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08001853
David Schleefed9eccb2008-11-04 20:29:31 -08001854 return ret;
1855}
1856
1857/*
Ian Abbott18e01b22015-01-28 18:41:47 +00001858 * COMEDI_UNLOCK ioctl
1859 * unlock subdevice
1860 *
1861 * arg:
1862 * subdevice number
1863 *
1864 * reads:
1865 * nothing
1866 *
1867 * writes:
1868 * nothing
1869 */
Ian Abbottc1a6eac2014-10-28 17:15:47 +00001870static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301871 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001872{
Bill Pemberton34c43922009-03-16 22:05:14 -04001873 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001874
1875 if (arg >= dev->n_subdevices)
1876 return -EINVAL;
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07001877 s = &dev->subdevices[arg];
David Schleefed9eccb2008-11-04 20:29:31 -08001878
1879 if (s->busy)
1880 return -EBUSY;
1881
1882 if (s->lock && s->lock != file)
1883 return -EACCES;
1884
H Hartley Sweeten90ac0762014-07-21 11:01:26 -07001885 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08001886 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001887
1888 return 0;
1889}
1890
1891/*
Ian Abbott18e01b22015-01-28 18:41:47 +00001892 * COMEDI_CANCEL ioctl
1893 * cancel asynchronous acquisition
1894 *
1895 * arg:
1896 * subdevice number
1897 *
1898 * reads:
1899 * nothing
1900 *
1901 * writes:
1902 * nothing
1903 */
Ian Abbottc1a6eac2014-10-28 17:15:47 +00001904static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301905 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001906{
Bill Pemberton34c43922009-03-16 22:05:14 -04001907 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001908
1909 if (arg >= dev->n_subdevices)
1910 return -EINVAL;
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07001911 s = &dev->subdevices[arg];
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -07001912 if (!s->async)
David Schleefed9eccb2008-11-04 20:29:31 -08001913 return -EINVAL;
1914
David Schleefed9eccb2008-11-04 20:29:31 -08001915 if (!s->busy)
1916 return 0;
1917
1918 if (s->busy != file)
1919 return -EBUSY;
1920
Heena Sirwanife43ec52014-10-06 13:46:09 +05301921 return do_cancel(dev, s);
David Schleefed9eccb2008-11-04 20:29:31 -08001922}
1923
1924/*
Ian Abbott18e01b22015-01-28 18:41:47 +00001925 * COMEDI_POLL ioctl
1926 * instructs driver to synchronize buffers
1927 *
1928 * arg:
1929 * subdevice number
1930 *
1931 * reads:
1932 * nothing
1933 *
1934 * writes:
1935 * nothing
1936 */
Ian Abbottc1a6eac2014-10-28 17:15:47 +00001937static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301938 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001939{
Bill Pemberton34c43922009-03-16 22:05:14 -04001940 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001941
1942 if (arg >= dev->n_subdevices)
1943 return -EINVAL;
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07001944 s = &dev->subdevices[arg];
David Schleefed9eccb2008-11-04 20:29:31 -08001945
David Schleefed9eccb2008-11-04 20:29:31 -08001946 if (!s->busy)
1947 return 0;
1948
1949 if (s->busy != file)
1950 return -EBUSY;
1951
1952 if (s->poll)
1953 return s->poll(dev, s);
1954
1955 return -EINVAL;
1956}
1957
Ian Abbottc299a672014-11-04 18:09:01 +00001958/*
1959 * COMEDI_SETRSUBD ioctl
1960 * sets the current "read" subdevice on a per-file basis
1961 *
1962 * arg:
1963 * subdevice number
1964 *
1965 * reads:
1966 * nothing
1967 *
1968 * writes:
1969 * nothing
1970 */
1971static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1972 struct file *file)
1973{
1974 struct comedi_file *cfp = file->private_data;
1975 struct comedi_subdevice *s_old, *s_new;
1976
1977 if (arg >= dev->n_subdevices)
1978 return -EINVAL;
1979
1980 s_new = &dev->subdevices[arg];
1981 s_old = comedi_file_read_subdevice(file);
1982 if (s_old == s_new)
1983 return 0; /* no change */
1984
1985 if (!(s_new->subdev_flags & SDF_CMD_READ))
1986 return -EINVAL;
1987
1988 /*
1989 * Check the file isn't still busy handling a "read" command on the
1990 * old subdevice (if any).
1991 */
1992 if (s_old && s_old->busy == file && s_old->async &&
1993 !(s_old->async->cmd.flags & CMDF_WRITE))
1994 return -EBUSY;
1995
Shyam Saini34d34732016-05-10 21:21:07 +05301996 WRITE_ONCE(cfp->read_subdev, s_new);
Ian Abbottc299a672014-11-04 18:09:01 +00001997 return 0;
1998}
1999
2000/*
2001 * COMEDI_SETWSUBD ioctl
2002 * sets the current "write" subdevice on a per-file basis
2003 *
2004 * arg:
2005 * subdevice number
2006 *
2007 * reads:
2008 * nothing
2009 *
2010 * writes:
2011 * nothing
2012 */
2013static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
2014 struct file *file)
2015{
2016 struct comedi_file *cfp = file->private_data;
2017 struct comedi_subdevice *s_old, *s_new;
2018
2019 if (arg >= dev->n_subdevices)
2020 return -EINVAL;
2021
2022 s_new = &dev->subdevices[arg];
2023 s_old = comedi_file_write_subdevice(file);
2024 if (s_old == s_new)
2025 return 0; /* no change */
2026
2027 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
2028 return -EINVAL;
2029
2030 /*
2031 * Check the file isn't still busy handling a "write" command on the
2032 * old subdevice (if any).
2033 */
2034 if (s_old && s_old->busy == file && s_old->async &&
2035 (s_old->async->cmd.flags & CMDF_WRITE))
2036 return -EBUSY;
2037
Shyam Saini34d34732016-05-10 21:21:07 +05302038 WRITE_ONCE(cfp->write_subdev, s_new);
Ian Abbottc299a672014-11-04 18:09:01 +00002039 return 0;
2040}
2041
H Hartley Sweeten47db6d52012-06-07 17:05:27 -07002042static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2043 unsigned long arg)
2044{
Leslie Kleind4d47892016-03-21 09:18:35 -04002045 unsigned int minor = iminor(file_inode(file));
Ian Abbott20f083c2014-11-04 18:09:00 +00002046 struct comedi_file *cfp = file->private_data;
2047 struct comedi_device *dev = cfp->dev;
H Hartley Sweeten47db6d52012-06-07 17:05:27 -07002048 int rc;
2049
H Hartley Sweeten47db6d52012-06-07 17:05:27 -07002050 mutex_lock(&dev->mutex);
2051
Ian Abbotta2aab8b2015-01-28 18:41:50 +00002052 /*
2053 * Device config is special, because it must work on
2054 * an unconfigured device.
2055 */
H Hartley Sweeten47db6d52012-06-07 17:05:27 -07002056 if (cmd == COMEDI_DEVCONFIG) {
Ian Abbott754ab5c2013-01-28 16:14:31 +00002057 if (minor >= COMEDI_NUM_BOARD_MINORS) {
2058 /* Device config not appropriate on non-board minors. */
2059 rc = -ENOTTY;
2060 goto done;
2061 }
H Hartley Sweeten47db6d52012-06-07 17:05:27 -07002062 rc = do_devconfig_ioctl(dev,
2063 (struct comedi_devconfig __user *)arg);
Ian Abbott8ab4ed62013-04-04 14:58:54 +01002064 if (rc == 0) {
2065 if (arg == 0 &&
2066 dev->minor >= comedi_num_legacy_minors) {
Ian Abbotta2aab8b2015-01-28 18:41:50 +00002067 /*
2068 * Successfully unconfigured a dynamically
2069 * allocated device. Try and remove it.
2070 */
Ian Abbottdb210da2013-04-04 14:59:18 +01002071 if (comedi_clear_board_dev(dev)) {
Ian Abbott8ab4ed62013-04-04 14:58:54 +01002072 mutex_unlock(&dev->mutex);
Ian Abbottcb6b79d2013-04-04 14:59:16 +01002073 comedi_free_board_dev(dev);
Ian Abbott8ab4ed62013-04-04 14:58:54 +01002074 return rc;
2075 }
2076 }
2077 }
H Hartley Sweeten47db6d52012-06-07 17:05:27 -07002078 goto done;
2079 }
2080
2081 if (!dev->attached) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07002082 dev_dbg(dev->class_dev, "no driver attached\n");
H Hartley Sweeten47db6d52012-06-07 17:05:27 -07002083 rc = -ENODEV;
2084 goto done;
2085 }
2086
2087 switch (cmd) {
2088 case COMEDI_BUFCONFIG:
2089 rc = do_bufconfig_ioctl(dev,
2090 (struct comedi_bufconfig __user *)arg);
2091 break;
2092 case COMEDI_DEVINFO:
2093 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2094 file);
2095 break;
2096 case COMEDI_SUBDINFO:
2097 rc = do_subdinfo_ioctl(dev,
2098 (struct comedi_subdinfo __user *)arg,
2099 file);
2100 break;
2101 case COMEDI_CHANINFO:
2102 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
2103 break;
2104 case COMEDI_RANGEINFO:
2105 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2106 break;
2107 case COMEDI_BUFINFO:
2108 rc = do_bufinfo_ioctl(dev,
2109 (struct comedi_bufinfo __user *)arg,
2110 file);
2111 break;
2112 case COMEDI_LOCK:
2113 rc = do_lock_ioctl(dev, arg, file);
2114 break;
2115 case COMEDI_UNLOCK:
2116 rc = do_unlock_ioctl(dev, arg, file);
2117 break;
2118 case COMEDI_CANCEL:
2119 rc = do_cancel_ioctl(dev, arg, file);
2120 break;
2121 case COMEDI_CMD:
2122 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2123 break;
2124 case COMEDI_CMDTEST:
2125 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2126 file);
2127 break;
2128 case COMEDI_INSNLIST:
2129 rc = do_insnlist_ioctl(dev,
2130 (struct comedi_insnlist __user *)arg,
2131 file);
2132 break;
2133 case COMEDI_INSN:
2134 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2135 file);
2136 break;
2137 case COMEDI_POLL:
2138 rc = do_poll_ioctl(dev, arg, file);
2139 break;
Ian Abbottc299a672014-11-04 18:09:01 +00002140 case COMEDI_SETRSUBD:
2141 rc = do_setrsubd_ioctl(dev, arg, file);
2142 break;
2143 case COMEDI_SETWSUBD:
2144 rc = do_setwsubd_ioctl(dev, arg, file);
2145 break;
H Hartley Sweeten47db6d52012-06-07 17:05:27 -07002146 default:
2147 rc = -ENOTTY;
2148 break;
2149 }
2150
2151done:
2152 mutex_unlock(&dev->mutex);
2153 return rc;
2154}
2155
Federico Vagadf30b212011-10-29 09:45:39 +02002156static void comedi_vm_open(struct vm_area_struct *area)
2157{
Ian Abbottaf93da32013-11-08 15:03:43 +00002158 struct comedi_buf_map *bm;
Federico Vagadf30b212011-10-29 09:45:39 +02002159
Ian Abbottaf93da32013-11-08 15:03:43 +00002160 bm = area->vm_private_data;
2161 comedi_buf_map_get(bm);
Federico Vagadf30b212011-10-29 09:45:39 +02002162}
2163
2164static void comedi_vm_close(struct vm_area_struct *area)
David Schleefed9eccb2008-11-04 20:29:31 -08002165{
Ian Abbottaf93da32013-11-08 15:03:43 +00002166 struct comedi_buf_map *bm;
David Schleefed9eccb2008-11-04 20:29:31 -08002167
Ian Abbottaf93da32013-11-08 15:03:43 +00002168 bm = area->vm_private_data;
2169 comedi_buf_map_put(bm);
David Schleefed9eccb2008-11-04 20:29:31 -08002170}
2171
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07002172static const struct vm_operations_struct comedi_vm_ops = {
Federico Vagadf30b212011-10-29 09:45:39 +02002173 .open = comedi_vm_open,
2174 .close = comedi_vm_close,
David Schleefed9eccb2008-11-04 20:29:31 -08002175};
2176
2177static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2178{
Ian Abbott20f083c2014-11-04 18:09:00 +00002179 struct comedi_file *cfp = file->private_data;
2180 struct comedi_device *dev = cfp->dev;
H Hartley Sweetena52840a2012-12-19 15:45:12 -07002181 struct comedi_subdevice *s;
2182 struct comedi_async *async;
Ian Abbottb34aa862014-04-10 19:41:57 +01002183 struct comedi_buf_map *bm = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08002184 unsigned long start = vma->vm_start;
2185 unsigned long size;
2186 int n_pages;
2187 int i;
2188 int retval;
Bernd Porr3ffab422011-11-08 21:23:03 +00002189
Ian Abbottb34aa862014-04-10 19:41:57 +01002190 /*
2191 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2192 * and down-reading &dev->attach_lock should normally succeed without
2193 * contention unless the device is in the process of being attached
2194 * or detached.
2195 */
2196 if (!down_read_trylock(&dev->attach_lock))
2197 return -EAGAIN;
H Hartley Sweetena52840a2012-12-19 15:45:12 -07002198
David Schleefed9eccb2008-11-04 20:29:31 -08002199 if (!dev->attached) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07002200 dev_dbg(dev->class_dev, "no driver attached\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002201 retval = -ENODEV;
2202 goto done;
2203 }
H Hartley Sweetena52840a2012-12-19 15:45:12 -07002204
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002205 if (vma->vm_flags & VM_WRITE)
Ian Abbott20f083c2014-11-04 18:09:00 +00002206 s = comedi_file_write_subdevice(file);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002207 else
Ian Abbott20f083c2014-11-04 18:09:00 +00002208 s = comedi_file_read_subdevice(file);
H Hartley Sweetena52840a2012-12-19 15:45:12 -07002209 if (!s) {
David Schleefed9eccb2008-11-04 20:29:31 -08002210 retval = -EINVAL;
2211 goto done;
2212 }
H Hartley Sweetena52840a2012-12-19 15:45:12 -07002213
David Schleefed9eccb2008-11-04 20:29:31 -08002214 async = s->async;
H Hartley Sweetena52840a2012-12-19 15:45:12 -07002215 if (!async) {
David Schleefed9eccb2008-11-04 20:29:31 -08002216 retval = -EINVAL;
2217 goto done;
2218 }
2219
2220 if (vma->vm_pgoff != 0) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07002221 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002222 retval = -EINVAL;
2223 goto done;
2224 }
2225
2226 size = vma->vm_end - vma->vm_start;
2227 if (size > async->prealloc_bufsz) {
2228 retval = -EFAULT;
2229 goto done;
2230 }
Sandhya Bankar44b8c792016-03-06 12:35:29 +05302231 if (offset_in_page(size)) {
David Schleefed9eccb2008-11-04 20:29:31 -08002232 retval = -EFAULT;
2233 goto done;
2234 }
2235
sayli karnikec9d0752016-09-20 03:21:38 +05302236 n_pages = vma_pages(vma);
Ian Abbottb34aa862014-04-10 19:41:57 +01002237
2238 /* get reference to current buf map (if any) */
2239 bm = comedi_buf_map_from_subdev_get(s);
Ian Abbottaf93da32013-11-08 15:03:43 +00002240 if (!bm || n_pages > bm->n_pages) {
2241 retval = -EINVAL;
2242 goto done;
2243 }
David Schleefed9eccb2008-11-04 20:29:31 -08002244 for (i = 0; i < n_pages; ++i) {
Ian Abbottaf93da32013-11-08 15:03:43 +00002245 struct comedi_buf_page *buf = &bm->page_list[i];
H Hartley Sweetena52840a2012-12-19 15:45:12 -07002246
David Schleefed9eccb2008-11-04 20:29:31 -08002247 if (remap_pfn_range(vma, start,
H Hartley Sweetena52840a2012-12-19 15:45:12 -07002248 page_to_pfn(virt_to_page(buf->virt_addr)),
2249 PAGE_SIZE, PAGE_SHARED)) {
David Schleefed9eccb2008-11-04 20:29:31 -08002250 retval = -EAGAIN;
2251 goto done;
2252 }
2253 start += PAGE_SIZE;
2254 }
2255
2256 vma->vm_ops = &comedi_vm_ops;
Ian Abbottaf93da32013-11-08 15:03:43 +00002257 vma->vm_private_data = bm;
David Schleefed9eccb2008-11-04 20:29:31 -08002258
Ian Abbottaf93da32013-11-08 15:03:43 +00002259 vma->vm_ops->open(vma);
David Schleefed9eccb2008-11-04 20:29:31 -08002260
2261 retval = 0;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002262done:
Ian Abbottb34aa862014-04-10 19:41:57 +01002263 up_read(&dev->attach_lock);
2264 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
David Schleefed9eccb2008-11-04 20:29:31 -08002265 return retval;
2266}
2267
Benedikt Bergenthal1ae50622012-04-16 12:40:22 +02002268static unsigned int comedi_poll(struct file *file, poll_table *wait)
David Schleefed9eccb2008-11-04 20:29:31 -08002269{
2270 unsigned int mask = 0;
Ian Abbott20f083c2014-11-04 18:09:00 +00002271 struct comedi_file *cfp = file->private_data;
2272 struct comedi_device *dev = cfp->dev;
Ian Abbott322146d2015-10-09 12:26:47 +01002273 struct comedi_subdevice *s, *s_read;
Bernd Porr3ffab422011-11-08 21:23:03 +00002274
Ian Abbottd5eb3a72015-10-09 12:26:52 +01002275 down_read(&dev->attach_lock);
H Hartley Sweetenca081b12012-12-19 15:39:44 -07002276
David Schleefed9eccb2008-11-04 20:29:31 -08002277 if (!dev->attached) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07002278 dev_dbg(dev->class_dev, "no driver attached\n");
H Hartley Sweetenca081b12012-12-19 15:39:44 -07002279 goto done;
David Schleefed9eccb2008-11-04 20:29:31 -08002280 }
2281
Ian Abbott20f083c2014-11-04 18:09:00 +00002282 s = comedi_file_read_subdevice(file);
Ian Abbott322146d2015-10-09 12:26:47 +01002283 s_read = s;
Ian Abbottcc400e12013-02-05 12:50:40 +00002284 if (s && s->async) {
H Hartley Sweetenca081b12012-12-19 15:39:44 -07002285 poll_wait(file, &s->async->wait_head, wait);
Ian Abbott38342342015-10-09 12:26:51 +01002286 if (s->busy != file || !comedi_is_subdevice_running(s) ||
Ian Abbott662c7222014-10-30 12:42:31 +00002287 (s->async->cmd.flags & CMDF_WRITE) ||
Ian Abbotte9edef32014-05-06 13:12:09 +01002288 comedi_buf_read_n_available(s) > 0)
David Schleefed9eccb2008-11-04 20:29:31 -08002289 mask |= POLLIN | POLLRDNORM;
David Schleefed9eccb2008-11-04 20:29:31 -08002290 }
2291
Ian Abbott20f083c2014-11-04 18:09:00 +00002292 s = comedi_file_write_subdevice(file);
Ian Abbottcc400e12013-02-05 12:50:40 +00002293 if (s && s->async) {
H Hartley Sweetenc39e0502014-10-31 12:04:28 -07002294 unsigned int bps = comedi_bytes_per_sample(s);
H Hartley Sweetenca081b12012-12-19 15:39:44 -07002295
Ian Abbott322146d2015-10-09 12:26:47 +01002296 if (s != s_read)
2297 poll_wait(file, &s->async->wait_head, wait);
Ian Abbott38342342015-10-09 12:26:51 +01002298 if (s->busy != file || !comedi_is_subdevice_running(s) ||
Ian Abbott662c7222014-10-30 12:42:31 +00002299 !(s->async->cmd.flags & CMDF_WRITE) ||
Ian Abbottecf04ed2015-10-09 12:26:50 +01002300 comedi_buf_write_n_available(s) >= bps)
H Hartley Sweetenca081b12012-12-19 15:39:44 -07002301 mask |= POLLOUT | POLLWRNORM;
2302 }
2303
2304done:
Ian Abbottd5eb3a72015-10-09 12:26:52 +01002305 up_read(&dev->attach_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002306 return mask;
2307}
2308
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002309static ssize_t comedi_write(struct file *file, const char __user *buf,
2310 size_t nbytes, loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08002311{
Bill Pemberton34c43922009-03-16 22:05:14 -04002312 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04002313 struct comedi_async *async;
Ian Abbott84a185e2015-11-18 17:55:06 +00002314 unsigned int n, m;
2315 ssize_t count = 0;
2316 int retval = 0;
David Schleefed9eccb2008-11-04 20:29:31 -08002317 DECLARE_WAITQUEUE(wait, current);
Ian Abbott20f083c2014-11-04 18:09:00 +00002318 struct comedi_file *cfp = file->private_data;
2319 struct comedi_device *dev = cfp->dev;
Ian Abbott06181de2015-11-18 17:55:04 +00002320 bool become_nonbusy = false;
Ian Abbott9329f132013-11-08 15:03:30 +00002321 bool attach_locked;
2322 unsigned int old_detach_count;
Bernd Porr3ffab422011-11-08 21:23:03 +00002323
Ian Abbott9329f132013-11-08 15:03:30 +00002324 /* Protect against device detachment during operation. */
2325 down_read(&dev->attach_lock);
2326 attach_locked = true;
2327 old_detach_count = dev->detach_count;
2328
David Schleefed9eccb2008-11-04 20:29:31 -08002329 if (!dev->attached) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07002330 dev_dbg(dev->class_dev, "no driver attached\n");
Ian Abbott9329f132013-11-08 15:03:30 +00002331 retval = -ENODEV;
2332 goto out;
David Schleefed9eccb2008-11-04 20:29:31 -08002333 }
2334
Ian Abbott20f083c2014-11-04 18:09:00 +00002335 s = comedi_file_write_subdevice(file);
Ian Abbott9329f132013-11-08 15:03:30 +00002336 if (!s || !s->async) {
2337 retval = -EIO;
2338 goto out;
2339 }
H Hartley Sweeten2714b012012-12-19 15:40:34 -07002340
David Schleefed9eccb2008-11-04 20:29:31 -08002341 async = s->async;
Ian Abbott3318c7a2015-11-18 17:55:10 +00002342 if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) {
Ian Abbottf7398502014-10-30 12:42:30 +00002343 retval = -EINVAL;
2344 goto out;
2345 }
H Hartley Sweeten2714b012012-12-19 15:40:34 -07002346
David Schleefed9eccb2008-11-04 20:29:31 -08002347 add_wait_queue(&async->wait_head, &wait);
Ian Abbott06181de2015-11-18 17:55:04 +00002348 while (count == 0 && !retval) {
Leslie Kleind4d47892016-03-21 09:18:35 -04002349 unsigned int runflags;
Ian Abbott35a74752015-11-18 17:55:08 +00002350 unsigned int wp, n1, n2;
Ian Abbottb183a832015-03-27 15:13:01 +00002351
David Schleefed9eccb2008-11-04 20:29:31 -08002352 set_current_state(TASK_INTERRUPTIBLE);
2353
Ian Abbottb183a832015-03-27 15:13:01 +00002354 runflags = comedi_get_subdevice_runflags(s);
2355 if (!comedi_is_runflags_running(runflags)) {
Ian Abbott06181de2015-11-18 17:55:04 +00002356 if (comedi_is_runflags_in_error(runflags))
2357 retval = -EPIPE;
Ian Abbott28a60c42015-11-18 17:55:11 +00002358 if (retval || nbytes)
2359 become_nonbusy = true;
Ian Abbottd2611542010-05-19 17:22:41 +01002360 break;
2361 }
Ian Abbott28a60c42015-11-18 17:55:11 +00002362 if (nbytes == 0)
2363 break;
Ian Abbottd2611542010-05-19 17:22:41 +01002364
Ian Abbott591c5f82015-11-18 17:55:07 +00002365 /* Allocate all free buffer space. */
2366 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2367 m = comedi_buf_write_n_allocated(s);
Ian Abbott591c5f82015-11-18 17:55:07 +00002368 n = min_t(size_t, m, nbytes);
David Schleefed9eccb2008-11-04 20:29:31 -08002369
2370 if (n == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08002371 if (file->f_flags & O_NONBLOCK) {
2372 retval = -EAGAIN;
2373 break;
2374 }
Federico Vaga6a9ce6b2011-10-29 09:47:39 +02002375 schedule();
David Schleefed9eccb2008-11-04 20:29:31 -08002376 if (signal_pending(current)) {
2377 retval = -ERESTARTSYS;
2378 break;
2379 }
Ian Abbott3318c7a2015-11-18 17:55:10 +00002380 if (s->busy != file ||
2381 !(async->cmd.flags & CMDF_WRITE)) {
Ian Abbottf7398502014-10-30 12:42:30 +00002382 retval = -EINVAL;
2383 break;
2384 }
David Schleefed9eccb2008-11-04 20:29:31 -08002385 continue;
2386 }
2387
Ian Abbott35a74752015-11-18 17:55:08 +00002388 wp = async->buf_write_ptr;
2389 n1 = min(n, async->prealloc_bufsz - wp);
2390 n2 = n - n1;
2391 m = copy_from_user(async->prealloc_buf + wp, buf, n1);
2392 if (m)
2393 m += n2;
2394 else if (n2)
2395 m = copy_from_user(async->prealloc_buf, buf + n1, n2);
David Schleefed9eccb2008-11-04 20:29:31 -08002396 if (m) {
2397 n -= m;
2398 retval = -EFAULT;
2399 }
Ian Abbott940dd352014-05-06 13:12:05 +01002400 comedi_buf_write_free(s, n);
David Schleefed9eccb2008-11-04 20:29:31 -08002401
2402 count += n;
2403 nbytes -= n;
2404
2405 buf += n;
Ian Abbott06181de2015-11-18 17:55:04 +00002406 }
2407 remove_wait_queue(&async->wait_head, &wait);
2408 set_current_state(TASK_RUNNING);
2409 if (become_nonbusy && count == 0) {
2410 struct comedi_subdevice *new_s;
2411
2412 /*
2413 * To avoid deadlock, cannot acquire dev->mutex
2414 * while dev->attach_lock is held.
2415 */
2416 up_read(&dev->attach_lock);
2417 attach_locked = false;
2418 mutex_lock(&dev->mutex);
2419 /*
2420 * Check device hasn't become detached behind our back.
2421 * Checking dev->detach_count is unchanged ought to be
2422 * sufficient (unless there have been 2**32 detaches in the
2423 * meantime!), but check the subdevice pointer as well just in
2424 * case.
Ian Abbotted65bba2015-11-18 17:55:05 +00002425 *
2426 * Also check the subdevice is still in a suitable state to
2427 * become non-busy in case it changed behind our back.
Ian Abbott06181de2015-11-18 17:55:04 +00002428 */
2429 new_s = comedi_file_write_subdevice(file);
2430 if (dev->attached && old_detach_count == dev->detach_count &&
Ian Abbotted65bba2015-11-18 17:55:05 +00002431 s == new_s && new_s->async == async && s->busy == file &&
2432 (async->cmd.flags & CMDF_WRITE) &&
2433 !comedi_is_subdevice_running(s))
Ian Abbott06181de2015-11-18 17:55:04 +00002434 do_become_nonbusy(dev, s);
2435 mutex_unlock(&dev->mutex);
David Schleefed9eccb2008-11-04 20:29:31 -08002436 }
Ian Abbott9329f132013-11-08 15:03:30 +00002437out:
Ian Abbott9329f132013-11-08 15:03:30 +00002438 if (attach_locked)
2439 up_read(&dev->attach_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002440
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002441 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002442}
2443
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002444static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
H Hartley Sweeten36efbac2014-07-18 14:28:14 -07002445 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08002446{
Bill Pemberton34c43922009-03-16 22:05:14 -04002447 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04002448 struct comedi_async *async;
Ian Abbott76e8e7d2015-10-12 17:21:23 +01002449 unsigned int n, m;
2450 ssize_t count = 0;
2451 int retval = 0;
David Schleefed9eccb2008-11-04 20:29:31 -08002452 DECLARE_WAITQUEUE(wait, current);
Ian Abbott20f083c2014-11-04 18:09:00 +00002453 struct comedi_file *cfp = file->private_data;
2454 struct comedi_device *dev = cfp->dev;
Ian Abbott45c2bc52013-11-08 15:03:31 +00002455 unsigned int old_detach_count;
2456 bool become_nonbusy = false;
2457 bool attach_locked;
Bernd Porr3ffab422011-11-08 21:23:03 +00002458
Ian Abbott45c2bc52013-11-08 15:03:31 +00002459 /* Protect against device detachment during operation. */
2460 down_read(&dev->attach_lock);
2461 attach_locked = true;
2462 old_detach_count = dev->detach_count;
2463
David Schleefed9eccb2008-11-04 20:29:31 -08002464 if (!dev->attached) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07002465 dev_dbg(dev->class_dev, "no driver attached\n");
Ian Abbott45c2bc52013-11-08 15:03:31 +00002466 retval = -ENODEV;
2467 goto out;
David Schleefed9eccb2008-11-04 20:29:31 -08002468 }
2469
Ian Abbott20f083c2014-11-04 18:09:00 +00002470 s = comedi_file_read_subdevice(file);
Ian Abbott45c2bc52013-11-08 15:03:31 +00002471 if (!s || !s->async) {
2472 retval = -EIO;
2473 goto out;
2474 }
H Hartley Sweeten5c87fef2012-12-19 15:40:08 -07002475
David Schleefed9eccb2008-11-04 20:29:31 -08002476 async = s->async;
Ian Abbott39582842015-10-12 17:21:28 +01002477 if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
Ian Abbottf025ab92014-10-30 12:42:29 +00002478 retval = -EINVAL;
2479 goto out;
2480 }
David Schleefed9eccb2008-11-04 20:29:31 -08002481
2482 add_wait_queue(&async->wait_head, &wait);
Ian Abbott3c3bea22015-10-12 17:21:29 +01002483 while (count == 0 && !retval) {
Ian Abbott42ea9072015-10-12 17:21:25 +01002484 unsigned int rp, n1, n2;
2485
David Schleefed9eccb2008-11-04 20:29:31 -08002486 set_current_state(TASK_INTERRUPTIBLE);
2487
Ian Abbotte9edef32014-05-06 13:12:09 +01002488 m = comedi_buf_read_n_available(s);
Ian Abbott8ea93922015-10-12 17:21:24 +01002489 n = min_t(size_t, m, nbytes);
David Schleefed9eccb2008-11-04 20:29:31 -08002490
2491 if (n == 0) {
Ian Abbottb2073dc2016-03-29 10:49:04 +01002492 unsigned int runflags =
2493 comedi_get_subdevice_runflags(s);
Ian Abbottb183a832015-03-27 15:13:01 +00002494
2495 if (!comedi_is_runflags_running(runflags)) {
2496 if (comedi_is_runflags_in_error(runflags))
David Schleefed9eccb2008-11-04 20:29:31 -08002497 retval = -EPIPE;
Ian Abbott3c3bea22015-10-12 17:21:29 +01002498 if (retval || nbytes)
2499 become_nonbusy = true;
David Schleefed9eccb2008-11-04 20:29:31 -08002500 break;
2501 }
Ian Abbott3c3bea22015-10-12 17:21:29 +01002502 if (nbytes == 0)
2503 break;
David Schleefed9eccb2008-11-04 20:29:31 -08002504 if (file->f_flags & O_NONBLOCK) {
2505 retval = -EAGAIN;
2506 break;
2507 }
Federico Vaga6a9ce6b2011-10-29 09:47:39 +02002508 schedule();
David Schleefed9eccb2008-11-04 20:29:31 -08002509 if (signal_pending(current)) {
2510 retval = -ERESTARTSYS;
2511 break;
2512 }
Ian Abbott39582842015-10-12 17:21:28 +01002513 if (s->busy != file ||
2514 (async->cmd.flags & CMDF_WRITE)) {
Ian Abbottf025ab92014-10-30 12:42:29 +00002515 retval = -EINVAL;
2516 break;
2517 }
David Schleefed9eccb2008-11-04 20:29:31 -08002518 continue;
2519 }
Ian Abbott42ea9072015-10-12 17:21:25 +01002520 rp = async->buf_read_ptr;
2521 n1 = min(n, async->prealloc_bufsz - rp);
2522 n2 = n - n1;
2523 m = copy_to_user(buf, async->prealloc_buf + rp, n1);
2524 if (m)
2525 m += n2;
2526 else if (n2)
2527 m = copy_to_user(buf + n1, async->prealloc_buf, n2);
David Schleefed9eccb2008-11-04 20:29:31 -08002528 if (m) {
2529 n -= m;
2530 retval = -EFAULT;
2531 }
2532
Ian Abbottd13be552014-05-06 13:12:07 +01002533 comedi_buf_read_alloc(s, n);
Ian Abbottf1df8662014-05-06 13:12:08 +01002534 comedi_buf_read_free(s, n);
David Schleefed9eccb2008-11-04 20:29:31 -08002535
2536 count += n;
2537 nbytes -= n;
2538
2539 buf += n;
David Schleefed9eccb2008-11-04 20:29:31 -08002540 }
Ian Abbott45c2bc52013-11-08 15:03:31 +00002541 remove_wait_queue(&async->wait_head, &wait);
2542 set_current_state(TASK_RUNNING);
Ian Abbott970679b2015-10-12 17:21:20 +01002543 if (become_nonbusy && count == 0) {
Ian Abbott45c2bc52013-11-08 15:03:31 +00002544 struct comedi_subdevice *new_s;
2545
2546 /*
2547 * To avoid deadlock, cannot acquire dev->mutex
2548 * while dev->attach_lock is held.
2549 */
2550 up_read(&dev->attach_lock);
2551 attach_locked = false;
Ian Abbott4b18f082013-07-05 16:49:34 +01002552 mutex_lock(&dev->mutex);
Ian Abbott45c2bc52013-11-08 15:03:31 +00002553 /*
2554 * Check device hasn't become detached behind our back.
2555 * Checking dev->detach_count is unchanged ought to be
2556 * sufficient (unless there have been 2**32 detaches in the
2557 * meantime!), but check the subdevice pointer as well just in
2558 * case.
Ian Abbottfd060c82015-10-12 17:21:22 +01002559 *
2560 * Also check the subdevice is still in a suitable state to
2561 * become non-busy in case it changed behind our back.
Ian Abbott45c2bc52013-11-08 15:03:31 +00002562 */
Ian Abbott20f083c2014-11-04 18:09:00 +00002563 new_s = comedi_file_read_subdevice(file);
Ian Abbott45c2bc52013-11-08 15:03:31 +00002564 if (dev->attached && old_detach_count == dev->detach_count &&
Ian Abbottfd060c82015-10-12 17:21:22 +01002565 s == new_s && new_s->async == async && s->busy == file &&
2566 !(async->cmd.flags & CMDF_WRITE) &&
2567 !comedi_is_subdevice_running(s) &&
2568 comedi_buf_read_n_available(s) == 0)
2569 do_become_nonbusy(dev, s);
Ian Abbott4b18f082013-07-05 16:49:34 +01002570 mutex_unlock(&dev->mutex);
David Schleefed9eccb2008-11-04 20:29:31 -08002571 }
Ian Abbott45c2bc52013-11-08 15:03:31 +00002572out:
2573 if (attach_locked)
2574 up_read(&dev->attach_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002575
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002576 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002577}
2578
David Schleefed9eccb2008-11-04 20:29:31 -08002579static int comedi_open(struct inode *inode, struct file *file)
2580{
Leslie Kleind4d47892016-03-21 09:18:35 -04002581 const unsigned int minor = iminor(inode);
Ian Abbott20f083c2014-11-04 18:09:00 +00002582 struct comedi_file *cfp;
Ian Abbottfc406982013-11-08 15:03:34 +00002583 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2584 int rc;
Ian Abbott97920072009-02-09 16:51:38 +00002585
H Hartley Sweeten4da5fa92012-12-19 15:35:23 -07002586 if (!dev) {
H Hartley Sweeten272850f2013-11-26 10:21:11 -07002587 pr_debug("invalid minor number\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002588 return -ENODEV;
2589 }
2590
Ian Abbott20f083c2014-11-04 18:09:00 +00002591 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2592 if (!cfp)
2593 return -ENOMEM;
2594
2595 cfp->dev = dev;
2596
David Schleefed9eccb2008-11-04 20:29:31 -08002597 mutex_lock(&dev->mutex);
Ian Abbott0e0d3112015-07-07 17:06:52 +01002598 if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
2599 dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
Ian Abbottfc406982013-11-08 15:03:34 +00002600 rc = -ENODEV;
2601 goto out;
David Schleefed9eccb2008-11-04 20:29:31 -08002602 }
Ian Abbott1363e4f2013-12-11 14:51:03 +00002603 if (dev->attached && dev->use_count == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08002604 if (!try_module_get(dev->driver->module)) {
Santhosh Pai90e6f512015-06-29 10:44:03 +01002605 rc = -ENXIO;
Ian Abbottfc406982013-11-08 15:03:34 +00002606 goto out;
David Schleefed9eccb2008-11-04 20:29:31 -08002607 }
Ian Abbott1363e4f2013-12-11 14:51:03 +00002608 if (dev->open) {
2609 rc = dev->open(dev);
2610 if (rc < 0) {
2611 module_put(dev->driver->module);
2612 goto out;
2613 }
Ian Abbott3c17ba072010-05-19 14:10:00 +01002614 }
2615 }
David Schleefed9eccb2008-11-04 20:29:31 -08002616
2617 dev->use_count++;
Ian Abbott20f083c2014-11-04 18:09:00 +00002618 file->private_data = cfp;
2619 comedi_file_reset(file);
Ian Abbottfc406982013-11-08 15:03:34 +00002620 rc = 0;
David Schleefed9eccb2008-11-04 20:29:31 -08002621
Ian Abbottfc406982013-11-08 15:03:34 +00002622out:
David Schleefed9eccb2008-11-04 20:29:31 -08002623 mutex_unlock(&dev->mutex);
Ian Abbott20f083c2014-11-04 18:09:00 +00002624 if (rc) {
Ian Abbottfc406982013-11-08 15:03:34 +00002625 comedi_dev_put(dev);
Ian Abbott20f083c2014-11-04 18:09:00 +00002626 kfree(cfp);
2627 }
Ian Abbottfc406982013-11-08 15:03:34 +00002628 return rc;
David Schleefed9eccb2008-11-04 20:29:31 -08002629}
2630
H Hartley Sweeten2aae0072012-12-19 15:31:57 -07002631static int comedi_fasync(int fd, struct file *file, int on)
2632{
Ian Abbott20f083c2014-11-04 18:09:00 +00002633 struct comedi_file *cfp = file->private_data;
2634 struct comedi_device *dev = cfp->dev;
H Hartley Sweeten2aae0072012-12-19 15:31:57 -07002635
2636 return fasync_helper(fd, file, on, &dev->async_queue);
2637}
2638
David Schleefed9eccb2008-11-04 20:29:31 -08002639static int comedi_close(struct inode *inode, struct file *file)
2640{
Ian Abbott20f083c2014-11-04 18:09:00 +00002641 struct comedi_file *cfp = file->private_data;
2642 struct comedi_device *dev = cfp->dev;
Bill Pemberton34c43922009-03-16 22:05:14 -04002643 struct comedi_subdevice *s = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08002644 int i;
Bernd Porr3ffab422011-11-08 21:23:03 +00002645
David Schleefed9eccb2008-11-04 20:29:31 -08002646 mutex_lock(&dev->mutex);
2647
2648 if (dev->subdevices) {
2649 for (i = 0; i < dev->n_subdevices; i++) {
H Hartley Sweetenb077f2c2012-09-05 18:20:58 -07002650 s = &dev->subdevices[i];
David Schleefed9eccb2008-11-04 20:29:31 -08002651
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002652 if (s->busy == file)
David Schleefed9eccb2008-11-04 20:29:31 -08002653 do_cancel(dev, s);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002654 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08002655 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08002656 }
2657 }
Ian Abbott1363e4f2013-12-11 14:51:03 +00002658 if (dev->attached && dev->use_count == 1) {
2659 if (dev->close)
2660 dev->close(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08002661 module_put(dev->driver->module);
Ian Abbott1363e4f2013-12-11 14:51:03 +00002662 }
David Schleefed9eccb2008-11-04 20:29:31 -08002663
2664 dev->use_count--;
2665
2666 mutex_unlock(&dev->mutex);
Ian Abbottfc406982013-11-08 15:03:34 +00002667 comedi_dev_put(dev);
Ian Abbott20f083c2014-11-04 18:09:00 +00002668 kfree(cfp);
David Schleefed9eccb2008-11-04 20:29:31 -08002669
David Schleefed9eccb2008-11-04 20:29:31 -08002670 return 0;
2671}
2672
Ian Abbott8cb8aad2012-06-19 10:17:43 +01002673static const struct file_operations comedi_fops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302674 .owner = THIS_MODULE,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302675 .unlocked_ioctl = comedi_unlocked_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302676 .compat_ioctl = comedi_compat_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302677 .open = comedi_open,
2678 .release = comedi_close,
2679 .read = comedi_read,
2680 .write = comedi_write,
2681 .mmap = comedi_mmap,
2682 .poll = comedi_poll,
2683 .fasync = comedi_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002684 .llseek = noop_llseek,
David Schleefed9eccb2008-11-04 20:29:31 -08002685};
2686
Ian Abbottdd630cd2015-01-28 18:41:46 +00002687/**
Ian Abbotta3e39942015-09-23 16:33:27 +01002688 * comedi_event() - Handle events for asynchronous COMEDI command
2689 * @dev: COMEDI device.
2690 * @s: COMEDI subdevice.
2691 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
Ian Abbottdd630cd2015-01-28 18:41:46 +00002692 *
Ian Abbotta3e39942015-09-23 16:33:27 +01002693 * If an asynchronous COMEDI command is active on the subdevice, process
2694 * any %COMEDI_CB_... event flags that have been set, usually by an
Ian Abbottdd630cd2015-01-28 18:41:46 +00002695 * interrupt handler. These may change the run state of the asynchronous
Ian Abbotta3e39942015-09-23 16:33:27 +01002696 * command, wake a task, and/or send a %SIGIO signal.
Ian Abbottdd630cd2015-01-28 18:41:46 +00002697 */
Bill Pemberton34c43922009-03-16 22:05:14 -04002698void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002699{
Bill Pembertond1636792009-03-16 22:05:20 -04002700 struct comedi_async *async = s->async;
Ian Abbottef4b4b22015-03-27 15:13:06 +00002701 unsigned int events;
2702 int si_code = 0;
2703 unsigned long flags;
David Schleefed9eccb2008-11-04 20:29:31 -08002704
Ian Abbottef4b4b22015-03-27 15:13:06 +00002705 spin_lock_irqsave(&s->spin_lock, flags);
2706
2707 events = async->events;
Ian Abbott922d9ce2015-03-27 15:13:04 +00002708 async->events = 0;
Ian Abbottef4b4b22015-03-27 15:13:06 +00002709 if (!__comedi_is_subdevice_running(s)) {
2710 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002711 return;
Ian Abbottef4b4b22015-03-27 15:13:06 +00002712 }
David Schleefed9eccb2008-11-04 20:29:31 -08002713
Ian Abbott922d9ce2015-03-27 15:13:04 +00002714 if (events & COMEDI_CB_CANCEL_MASK)
Ian Abbottef4b4b22015-03-27 15:13:06 +00002715 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
H Hartley Sweeten781f9332014-10-13 09:56:08 -07002716
2717 /*
Ian Abbottef4b4b22015-03-27 15:13:06 +00002718 * Remember if an error event has occurred, so an error can be
2719 * returned the next time the user does a read() or write().
H Hartley Sweeten781f9332014-10-13 09:56:08 -07002720 */
Ian Abbottef4b4b22015-03-27 15:13:06 +00002721 if (events & COMEDI_CB_ERROR_MASK)
2722 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
David Schleefed9eccb2008-11-04 20:29:31 -08002723
Ian Abbott922d9ce2015-03-27 15:13:04 +00002724 if (async->cb_mask & events) {
Ian Abbottc265be02013-11-08 15:03:22 +00002725 wake_up_interruptible(&async->wait_head);
Ian Abbottaa331222015-03-27 15:13:05 +00002726 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
David Schleefed9eccb2008-11-04 20:29:31 -08002727 }
Ian Abbottef4b4b22015-03-27 15:13:06 +00002728
2729 spin_unlock_irqrestore(&s->spin_lock, flags);
2730
2731 if (si_code)
2732 kill_fasync(&dev->async_queue, SIGIO, si_code);
David Schleefed9eccb2008-11-04 20:29:31 -08002733}
H Hartley Sweeten5660e742013-04-12 10:11:54 -07002734EXPORT_SYMBOL_GPL(comedi_event);
David Schleefed9eccb2008-11-04 20:29:31 -08002735
Ian Abbott07778392013-04-04 14:58:51 +01002736/* Note: the ->mutex is pre-locked on successful return */
Ian Abbott7638ffc2013-04-04 14:58:50 +01002737struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
David Schleefed9eccb2008-11-04 20:29:31 -08002738{
Ian Abbott7638ffc2013-04-04 14:58:50 +01002739 struct comedi_device *dev;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002740 struct device *csdev;
Leslie Kleind4d47892016-03-21 09:18:35 -04002741 unsigned int i;
David Schleefed9eccb2008-11-04 20:29:31 -08002742
H Hartley Sweeten36efbac2014-07-18 14:28:14 -07002743 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -07002744 if (!dev)
Ian Abbott7638ffc2013-04-04 14:58:50 +01002745 return ERR_PTR(-ENOMEM);
Ian Abbott7638ffc2013-04-04 14:58:50 +01002746 comedi_device_init(dev);
Ian Abbottdb2e3482013-04-04 14:59:00 +01002747 comedi_set_hw_dev(dev, hardware_device);
Ian Abbott07778392013-04-04 14:58:51 +01002748 mutex_lock(&dev->mutex);
Ian Abbott5b7dba12013-04-04 14:59:04 +01002749 mutex_lock(&comedi_board_minor_table_lock);
Ian Abbott38b97222013-04-04 14:58:52 +01002750 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2751 i < COMEDI_NUM_BOARD_MINORS; ++i) {
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -07002752 if (!comedi_board_minor_table[i]) {
Ian Abbottcb6b79d2013-04-04 14:59:16 +01002753 comedi_board_minor_table[i] = dev;
David Schleefed9eccb2008-11-04 20:29:31 -08002754 break;
2755 }
2756 }
Ian Abbott5b7dba12013-04-04 14:59:04 +01002757 mutex_unlock(&comedi_board_minor_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002758 if (i == COMEDI_NUM_BOARD_MINORS) {
Ian Abbott07778392013-04-04 14:58:51 +01002759 mutex_unlock(&dev->mutex);
Ian Abbott7638ffc2013-04-04 14:58:50 +01002760 comedi_device_cleanup(dev);
Ian Abbott5b13ed92013-11-08 15:03:32 +00002761 comedi_dev_put(dev);
Haneen Mohammeda112eab2015-03-17 08:33:51 +03002762 dev_err(hardware_device,
2763 "ran out of minor numbers for board device files\n");
Ian Abbott7638ffc2013-04-04 14:58:50 +01002764 return ERR_PTR(-EBUSY);
David Schleefed9eccb2008-11-04 20:29:31 -08002765 }
Ian Abbott7638ffc2013-04-04 14:58:50 +01002766 dev->minor = i;
Pavel Roskin0435f932011-07-06 10:15:44 -04002767 csdev = device_create(comedi_class, hardware_device,
2768 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002769 if (!IS_ERR(csdev))
Ian Abbott8f988d82013-12-11 14:51:02 +00002770 dev->class_dev = get_device(csdev);
H Hartley Sweetena5011a22012-05-09 09:20:08 -07002771
Ian Abbott07778392013-04-04 14:58:51 +01002772 /* Note: dev->mutex needs to be unlocked by the caller. */
Ian Abbott7638ffc2013-04-04 14:58:50 +01002773 return dev;
David Schleefed9eccb2008-11-04 20:29:31 -08002774}
2775
Ian Abbott3346b792013-04-04 14:58:47 +01002776void comedi_release_hardware_device(struct device *hardware_device)
Ian Abbottc43435d2012-03-30 17:14:58 +01002777{
2778 int minor;
Ian Abbottcb6b79d2013-04-04 14:59:16 +01002779 struct comedi_device *dev;
Ian Abbottc43435d2012-03-30 17:14:58 +01002780
Ian Abbott38b97222013-04-04 14:58:52 +01002781 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2782 minor++) {
Ian Abbott5b7dba12013-04-04 14:59:04 +01002783 mutex_lock(&comedi_board_minor_table_lock);
Ian Abbottcb6b79d2013-04-04 14:59:16 +01002784 dev = comedi_board_minor_table[minor];
2785 if (dev && dev->hw_dev == hardware_device) {
Ian Abbott5b7dba12013-04-04 14:59:04 +01002786 comedi_board_minor_table[minor] = NULL;
2787 mutex_unlock(&comedi_board_minor_table_lock);
Ian Abbottcb6b79d2013-04-04 14:59:16 +01002788 comedi_free_board_dev(dev);
Ian Abbott3346b792013-04-04 14:58:47 +01002789 break;
Ian Abbottc43435d2012-03-30 17:14:58 +01002790 }
Ian Abbott5b7dba12013-04-04 14:59:04 +01002791 mutex_unlock(&comedi_board_minor_table_lock);
Ian Abbottc43435d2012-03-30 17:14:58 +01002792 }
Ian Abbottc43435d2012-03-30 17:14:58 +01002793}
2794
Ian Abbottf65cc542013-02-01 10:20:30 +00002795int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002796{
Ian Abbottf65cc542013-02-01 10:20:30 +00002797 struct comedi_device *dev = s->device;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002798 struct device *csdev;
Leslie Kleind4d47892016-03-21 09:18:35 -04002799 unsigned int i;
David Schleefed9eccb2008-11-04 20:29:31 -08002800
Ian Abbott5b7dba12013-04-04 14:59:04 +01002801 mutex_lock(&comedi_subdevice_minor_table_lock);
2802 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -07002803 if (!comedi_subdevice_minor_table[i]) {
Ian Abbottbd5b4172013-04-04 14:59:15 +01002804 comedi_subdevice_minor_table[i] = s;
David Schleefed9eccb2008-11-04 20:29:31 -08002805 break;
2806 }
2807 }
Ian Abbott5b7dba12013-04-04 14:59:04 +01002808 mutex_unlock(&comedi_subdevice_minor_table_lock);
2809 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
Haneen Mohammeda112eab2015-03-17 08:33:51 +03002810 dev_err(dev->class_dev,
2811 "ran out of minor numbers for subdevice files\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002812 return -EBUSY;
2813 }
Ian Abbott5b7dba12013-04-04 14:59:04 +01002814 i += COMEDI_NUM_BOARD_MINORS;
David Schleefed9eccb2008-11-04 20:29:31 -08002815 s->minor = i;
Pavel Roskin0435f932011-07-06 10:15:44 -04002816 csdev = device_create(comedi_class, dev->class_dev,
2817 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
H Hartley Sweeten90a35c12012-12-19 17:27:02 -07002818 dev->minor, s->index);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002819 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002820 s->class_dev = csdev;
H Hartley Sweetena5011a22012-05-09 09:20:08 -07002821
Ian Abbottda718542013-02-07 16:03:00 +00002822 return 0;
David Schleefed9eccb2008-11-04 20:29:31 -08002823}
2824
Bill Pemberton34c43922009-03-16 22:05:14 -04002825void comedi_free_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002826{
Ian Abbott0fcc9d42013-04-04 14:59:13 +01002827 unsigned int i;
David Schleefed9eccb2008-11-04 20:29:31 -08002828
H Hartley Sweeten88cc30c2015-03-04 12:15:28 -07002829 if (!s)
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002830 return;
H Hartley Sweeten5104a892015-08-10 13:14:01 -07002831 if (s->minor < COMEDI_NUM_BOARD_MINORS ||
2832 s->minor >= COMEDI_NUM_MINORS)
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002833 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002834
Ian Abbott0fcc9d42013-04-04 14:59:13 +01002835 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2836 mutex_lock(&comedi_subdevice_minor_table_lock);
Ian Abbottbd5b4172013-04-04 14:59:15 +01002837 if (s == comedi_subdevice_minor_table[i])
2838 comedi_subdevice_minor_table[i] = NULL;
Ian Abbott0fcc9d42013-04-04 14:59:13 +01002839 mutex_unlock(&comedi_subdevice_minor_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002840 if (s->class_dev) {
David Schleefed9eccb2008-11-04 20:29:31 -08002841 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2842 s->class_dev = NULL;
2843 }
David Schleefed9eccb2008-11-04 20:29:31 -08002844}
H Hartley Sweetena5787822012-12-19 15:40:59 -07002845
Ian Abbott682b9112013-01-28 17:07:39 +00002846static void comedi_cleanup_board_minors(void)
H Hartley Sweeten76cca892012-12-19 15:41:42 -07002847{
H Hartley Sweeten2be8ae52015-08-10 13:13:59 -07002848 struct comedi_device *dev;
Leslie Kleind4d47892016-03-21 09:18:35 -04002849 unsigned int i;
H Hartley Sweeten76cca892012-12-19 15:41:42 -07002850
H Hartley Sweeten2be8ae52015-08-10 13:13:59 -07002851 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
2852 dev = comedi_clear_board_minor(i);
2853 comedi_free_board_dev(dev);
2854 }
H Hartley Sweeten76cca892012-12-19 15:41:42 -07002855}
2856
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002857static int __init comedi_init(void)
2858{
2859 int i;
2860 int retval;
2861
H Hartley Sweetenc2ad0782014-07-17 12:27:32 -07002862 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002863
2864 if (comedi_num_legacy_minors < 0 ||
2865 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
H Hartley Sweetenc2ad0782014-07-17 12:27:32 -07002866 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002867 COMEDI_NUM_BOARD_MINORS);
2868 return -EINVAL;
2869 }
2870
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002871 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2872 COMEDI_NUM_MINORS, "comedi");
2873 if (retval)
2874 return -EIO;
2875 cdev_init(&comedi_cdev, &comedi_fops);
2876 comedi_cdev.owner = THIS_MODULE;
Anton Protopopova5bde3a2014-07-09 09:12:37 +04002877
2878 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2879 if (retval) {
2880 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2881 COMEDI_NUM_MINORS);
2882 return retval;
2883 }
2884
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002885 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2886 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2887 COMEDI_NUM_MINORS);
2888 return -EIO;
2889 }
2890 comedi_class = class_create(THIS_MODULE, "comedi");
2891 if (IS_ERR(comedi_class)) {
H Hartley Sweetenc2ad0782014-07-17 12:27:32 -07002892 pr_err("failed to create class\n");
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002893 cdev_del(&comedi_cdev);
2894 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2895 COMEDI_NUM_MINORS);
2896 return PTR_ERR(comedi_class);
2897 }
2898
Greg Kroah-Hartmane56341a2013-07-24 15:05:25 -07002899 comedi_class->dev_groups = comedi_dev_groups;
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002900
2901 /* XXX requires /proc interface */
2902 comedi_proc_init();
2903
2904 /* create devices files for legacy/manual use */
2905 for (i = 0; i < comedi_num_legacy_minors; i++) {
Ian Abbott7638ffc2013-04-04 14:58:50 +01002906 struct comedi_device *dev;
Raghavendra Ganiga4bac39f2014-05-01 13:53:12 +05302907
Ian Abbott7638ffc2013-04-04 14:58:50 +01002908 dev = comedi_alloc_board_minor(NULL);
2909 if (IS_ERR(dev)) {
Ian Abbott682b9112013-01-28 17:07:39 +00002910 comedi_cleanup_board_minors();
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002911 cdev_del(&comedi_cdev);
2912 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2913 COMEDI_NUM_MINORS);
Ian Abbott7638ffc2013-04-04 14:58:50 +01002914 return PTR_ERR(dev);
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002915 }
Kinka Huangcb3aada2014-07-15 23:11:02 +08002916 /* comedi_alloc_board_minor() locked the mutex */
2917 mutex_unlock(&dev->mutex);
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002918 }
2919
2920 return 0;
2921}
2922module_init(comedi_init);
2923
2924static void __exit comedi_cleanup(void)
2925{
Ian Abbott682b9112013-01-28 17:07:39 +00002926 comedi_cleanup_board_minors();
H Hartley Sweeten91fa0b02012-12-19 15:41:19 -07002927 class_destroy(comedi_class);
2928 cdev_del(&comedi_cdev);
2929 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2930
2931 comedi_proc_cleanup();
2932}
2933module_exit(comedi_cleanup);
2934
H Hartley Sweetena5787822012-12-19 15:40:59 -07002935MODULE_AUTHOR("http://www.comedi.org");
2936MODULE_DESCRIPTION("Comedi core module");
2937MODULE_LICENSE("GPL");