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