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