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