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