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