Staging: et131x: Correct WRAP bit handling
[safe/jmp/linux-2.6] / drivers / staging / comedi / drivers / comedi_fc.c
1 /*
2     comedi/drivers/comedi_fc.c
3
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.
7
8     Author:  Frank Mori Hess <fmhess@users.sourceforge.net>
9     Copyright (C) 2002 Frank Mori Hess
10
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.
15
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.
20
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ************************************************************************/
26
27 #include "../comedidev.h"
28
29 #include "comedi_fc.h"
30
31 static void increment_scan_progress(struct comedi_subdevice *subd,
32                                     unsigned int num_bytes)
33 {
34         struct comedi_async *async = subd->async;
35         unsigned int scan_length = cfc_bytes_per_scan(subd);
36
37         async->scan_progress += num_bytes;
38         if (async->scan_progress >= scan_length) {
39                 async->scan_progress %= scan_length;
40                 async->events |= COMEDI_CB_EOS;
41         }
42 }
43
44 /* Writes an array of data points to comedi's buffer */
45 unsigned int cfc_write_array_to_buffer(struct comedi_subdevice *subd,
46                                        void *data, unsigned int num_bytes)
47 {
48         struct comedi_async *async = subd->async;
49         unsigned int retval;
50
51         if (num_bytes == 0)
52                 return 0;
53
54         retval = comedi_buf_write_alloc(async, num_bytes);
55         if (retval != num_bytes) {
56                 printk("comedi: buffer overrun\n");
57                 async->events |= COMEDI_CB_OVERFLOW;
58                 return 0;
59         }
60
61         comedi_buf_memcpy_to(async, 0, data, num_bytes);
62         comedi_buf_write_free(async, num_bytes);
63         increment_scan_progress(subd, num_bytes);
64         async->events |= COMEDI_CB_BLOCK;
65
66         return num_bytes;
67 }
68
69 EXPORT_SYMBOL(cfc_write_array_to_buffer);
70
71 unsigned int cfc_read_array_from_buffer(struct comedi_subdevice *subd,
72                                         void *data, unsigned int num_bytes)
73 {
74         struct comedi_async *async = subd->async;
75
76         if (num_bytes == 0)
77                 return 0;
78
79         num_bytes = comedi_buf_read_alloc(async, num_bytes);
80         comedi_buf_memcpy_from(async, 0, data, num_bytes);
81         comedi_buf_read_free(async, num_bytes);
82         increment_scan_progress(subd, num_bytes);
83         async->events |= COMEDI_CB_BLOCK;
84
85         return num_bytes;
86 }
87
88 EXPORT_SYMBOL(cfc_read_array_from_buffer);
89
90 unsigned int cfc_handle_events(struct comedi_device *dev,
91                                struct comedi_subdevice *subd)
92 {
93         unsigned int events = subd->async->events;
94
95         if (events == 0)
96                 return events;
97
98         if (events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
99                 subd->cancel(dev, subd);
100
101         comedi_event(dev, subd);
102
103         return events;
104 }
105
106 EXPORT_SYMBOL(cfc_handle_events);
107
108 MODULE_AUTHOR("Frank Mori Hess <fmhess@users.sourceforge.net>");
109 MODULE_DESCRIPTION("Shared functions for Comedi low-level drivers");
110 MODULE_LICENSE("GPL");
111
112 static int __init comedi_fc_init_module(void)
113 {
114         return 0;
115 }
116
117 static void __exit comedi_fc_cleanup_module(void)
118 {
119 }
120
121 module_init(comedi_fc_init_module);
122 module_exit(comedi_fc_cleanup_module);