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