Merge branch 'for-next' of git://git.o-hand.com/linux-mfd
[sfrench/cifs-2.6.git] / drivers / staging / comedi / kcomedilib / get.c
1 /*
2     kcomedilib/get.c
3     a comedlib interface for kernel modules
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 #define __NO_VERSION__
25 #include "../comedi.h"
26 #include "../comedilib.h"
27 #include "../comedidev.h"
28
29 int comedi_get_n_subdevices(void *d)
30 {
31         struct comedi_device *dev = (struct comedi_device *) d;
32
33         return dev->n_subdevices;
34 }
35
36 int comedi_get_version_code(void *d)
37 {
38         return COMEDI_VERSION_CODE;
39 }
40
41 const char *comedi_get_driver_name(void * d)
42 {
43         struct comedi_device *dev = (struct comedi_device *) d;
44
45         return dev->driver->driver_name;
46 }
47
48 const char *comedi_get_board_name(void * d)
49 {
50         struct comedi_device *dev = (struct comedi_device *) d;
51
52         return dev->board_name;
53 }
54
55 int comedi_get_subdevice_type(void *d, unsigned int subdevice)
56 {
57         struct comedi_device *dev = (struct comedi_device *) d;
58         struct comedi_subdevice *s = dev->subdevices + subdevice;
59
60         return s->type;
61 }
62
63 unsigned int comedi_get_subdevice_flags(void *d, unsigned int subdevice)
64 {
65         struct comedi_device *dev = (struct comedi_device *) d;
66         struct comedi_subdevice *s = dev->subdevices + subdevice;
67
68         return s->subdev_flags;
69 }
70
71 int comedi_find_subdevice_by_type(void *d, int type, unsigned int subd)
72 {
73         struct comedi_device *dev = (struct comedi_device *) d;
74
75         if (subd > dev->n_subdevices)
76                 return -ENODEV;
77
78         for (; subd < dev->n_subdevices; subd++) {
79                 if (dev->subdevices[subd].type == type)
80                         return subd;
81         }
82         return -1;
83 }
84
85 int comedi_get_n_channels(void *d, unsigned int subdevice)
86 {
87         struct comedi_device *dev = (struct comedi_device *) d;
88         struct comedi_subdevice *s = dev->subdevices + subdevice;
89
90         return s->n_chan;
91 }
92
93 int comedi_get_len_chanlist(void *d, unsigned int subdevice)
94 {
95         struct comedi_device *dev = (struct comedi_device *) d;
96         struct comedi_subdevice *s = dev->subdevices + subdevice;
97
98         return s->len_chanlist;
99 }
100
101 unsigned int comedi_get_maxdata(void *d, unsigned int subdevice,
102         unsigned int chan)
103 {
104         struct comedi_device *dev = (struct comedi_device *) d;
105         struct comedi_subdevice *s = dev->subdevices + subdevice;
106
107         if (s->maxdata_list)
108                 return s->maxdata_list[chan];
109
110         return s->maxdata;
111 }
112
113 #ifdef KCOMEDILIB_DEPRECATED
114 int comedi_get_rangetype(void *d, unsigned int subdevice,
115         unsigned int chan)
116 {
117         struct comedi_device *dev = (struct comedi_device *) d;
118         struct comedi_subdevice *s = dev->subdevices + subdevice;
119         int ret;
120
121         if (s->range_table_list) {
122                 ret = s->range_table_list[chan]->length;
123         } else {
124                 ret = s->range_table->length;
125         }
126
127         ret = ret | (dev->minor << 28) | (subdevice << 24) | (chan << 16);
128
129         return ret;
130 }
131 #endif
132
133 int comedi_get_n_ranges(void *d, unsigned int subdevice, unsigned int chan)
134 {
135         struct comedi_device *dev = (struct comedi_device *) d;
136         struct comedi_subdevice *s = dev->subdevices + subdevice;
137         int ret;
138
139         if (s->range_table_list) {
140                 ret = s->range_table_list[chan]->length;
141         } else {
142                 ret = s->range_table->length;
143         }
144
145         return ret;
146 }
147
148 /*
149  * ALPHA (non-portable)
150 */
151 int comedi_get_krange(void *d, unsigned int subdevice, unsigned int chan,
152         unsigned int range, struct comedi_krange *krange)
153 {
154         struct comedi_device *dev = (struct comedi_device *) d;
155         struct comedi_subdevice *s = dev->subdevices + subdevice;
156         const struct comedi_lrange *lr;
157
158         if (s->range_table_list) {
159                 lr = s->range_table_list[chan];
160         } else {
161                 lr = s->range_table;
162         }
163         if (range >= lr->length)
164                 return -EINVAL;
165
166         memcpy(krange, lr->range + range, sizeof(struct comedi_krange));
167
168         return 0;
169 }
170
171 /*
172  * ALPHA (may be renamed)
173 */
174 unsigned int comedi_get_buf_head_pos(void *d, unsigned int subdevice)
175 {
176         struct comedi_device *dev = (struct comedi_device *) d;
177         struct comedi_subdevice *s = dev->subdevices + subdevice;
178         struct comedi_async *async;
179
180         async = s->async;
181         if (async == NULL)
182                 return 0;
183
184         return async->buf_write_count;
185 }
186
187 int comedi_get_buffer_contents(void *d, unsigned int subdevice)
188 {
189         struct comedi_device *dev = (struct comedi_device *) d;
190         struct comedi_subdevice *s = dev->subdevices + subdevice;
191         struct comedi_async *async;
192         unsigned int num_bytes;
193
194         if (subdevice >= dev->n_subdevices)
195                 return -1;
196         async = s->async;
197         if (async == NULL)
198                 return 0;
199         num_bytes = comedi_buf_read_n_available(s->async);
200         return num_bytes;
201 }
202
203 /*
204  * ALPHA
205 */
206 int comedi_set_user_int_count(void *d, unsigned int subdevice,
207         unsigned int buf_user_count)
208 {
209         struct comedi_device *dev = (struct comedi_device *) d;
210         struct comedi_subdevice *s = dev->subdevices + subdevice;
211         struct comedi_async *async;
212         int num_bytes;
213
214         async = s->async;
215         if (async == NULL)
216                 return -1;
217
218         num_bytes = buf_user_count - async->buf_read_count;
219         if (num_bytes < 0)
220                 return -1;
221         comedi_buf_read_alloc(async, num_bytes);
222         comedi_buf_read_free(async, num_bytes);
223
224         return 0;
225 }
226
227 int comedi_mark_buffer_read(void *d, unsigned int subdevice,
228         unsigned int num_bytes)
229 {
230         struct comedi_device *dev = (struct comedi_device *) d;
231         struct comedi_subdevice *s = dev->subdevices + subdevice;
232         struct comedi_async *async;
233
234         if (subdevice >= dev->n_subdevices)
235                 return -1;
236         async = s->async;
237         if (async == NULL)
238                 return -1;
239
240         comedi_buf_read_alloc(async, num_bytes);
241         comedi_buf_read_free(async, num_bytes);
242
243         return 0;
244 }
245
246 int comedi_mark_buffer_written(void *d, unsigned int subdevice,
247         unsigned int num_bytes)
248 {
249         struct comedi_device *dev = (struct comedi_device *) d;
250         struct comedi_subdevice *s = dev->subdevices + subdevice;
251         struct comedi_async *async;
252         int bytes_written;
253
254         if (subdevice >= dev->n_subdevices)
255                 return -1;
256         async = s->async;
257         if (async == NULL)
258                 return -1;
259         bytes_written = comedi_buf_write_alloc(async, num_bytes);
260         comedi_buf_write_free(async, bytes_written);
261         if (bytes_written != num_bytes)
262                 return -1;
263         return 0;
264 }
265
266 int comedi_get_buffer_size(void *d, unsigned int subdev)
267 {
268         struct comedi_device *dev = (struct comedi_device *) d;
269         struct comedi_subdevice *s = dev->subdevices + subdev;
270         struct comedi_async *async;
271
272         if (subdev >= dev->n_subdevices)
273                 return -1;
274         async = s->async;
275         if (async == NULL)
276                 return 0;
277
278         return async->prealloc_bufsz;
279 }
280
281 int comedi_get_buffer_offset(void *d, unsigned int subdevice)
282 {
283         struct comedi_device *dev = (struct comedi_device *) d;
284         struct comedi_subdevice *s = dev->subdevices + subdevice;
285         struct comedi_async *async;
286
287         if (subdevice >= dev->n_subdevices)
288                 return -1;
289         async = s->async;
290         if (async == NULL)
291                 return 0;
292
293         return async->buf_read_ptr;
294 }