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