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