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