Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / drivers / staging / comedi / drivers / comedi_fc.c
blobb3d89c82d08798697a654980686e085e7770038f
1 /*
2 comedi/drivers/comedi_fc.c
4 This is a place for code driver writers wish to share between
5 two or more drivers. fc is short
6 for frank-common.
8 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
9 Copyright (C) 2002 Frank Mori Hess
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
22 #include "../comedidev.h"
24 #include "comedi_fc.h"
26 static void increment_scan_progress(struct comedi_subdevice *subd,
27 unsigned int num_bytes)
29 struct comedi_async *async = subd->async;
30 unsigned int scan_length = cfc_bytes_per_scan(subd);
32 async->scan_progress += num_bytes;
33 if (async->scan_progress >= scan_length) {
34 async->scan_progress %= scan_length;
35 async->events |= COMEDI_CB_EOS;
39 /* Writes an array of data points to comedi's buffer */
40 unsigned int cfc_write_array_to_buffer(struct comedi_subdevice *subd,
41 void *data, unsigned int num_bytes)
43 struct comedi_async *async = subd->async;
44 unsigned int retval;
46 if (num_bytes == 0)
47 return 0;
49 retval = comedi_buf_write_alloc(async, num_bytes);
50 if (retval != num_bytes) {
51 dev_warn(subd->device->class_dev, "comedi: buffer overrun\n");
52 async->events |= COMEDI_CB_OVERFLOW;
53 return 0;
56 comedi_buf_memcpy_to(async, 0, data, num_bytes);
57 comedi_buf_write_free(async, num_bytes);
58 increment_scan_progress(subd, num_bytes);
59 async->events |= COMEDI_CB_BLOCK;
61 return num_bytes;
63 EXPORT_SYMBOL_GPL(cfc_write_array_to_buffer);
65 unsigned int cfc_read_array_from_buffer(struct comedi_subdevice *subd,
66 void *data, unsigned int num_bytes)
68 struct comedi_async *async = subd->async;
70 if (num_bytes == 0)
71 return 0;
73 num_bytes = comedi_buf_read_alloc(async, num_bytes);
74 comedi_buf_memcpy_from(async, 0, data, num_bytes);
75 comedi_buf_read_free(async, num_bytes);
76 increment_scan_progress(subd, num_bytes);
77 async->events |= COMEDI_CB_BLOCK;
79 return num_bytes;
81 EXPORT_SYMBOL_GPL(cfc_read_array_from_buffer);
83 unsigned int cfc_handle_events(struct comedi_device *dev,
84 struct comedi_subdevice *subd)
86 unsigned int events = subd->async->events;
88 if (events == 0)
89 return events;
91 if (events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
92 subd->cancel(dev, subd);
94 comedi_event(dev, subd);
96 return events;
98 EXPORT_SYMBOL_GPL(cfc_handle_events);
100 MODULE_AUTHOR("Frank Mori Hess <fmhess@users.sourceforge.net>");
101 MODULE_DESCRIPTION("Shared functions for Comedi low-level drivers");
102 MODULE_LICENSE("GPL");
104 static int __init comedi_fc_init_module(void)
106 return 0;
109 static void __exit comedi_fc_cleanup_module(void)
113 module_init(comedi_fc_init_module);
114 module_exit(comedi_fc_cleanup_module);