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