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