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