Merge commit 'v2.6.37-rc1' into kbuild/kbuild
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / comedi / comedidev.h
CommitLineData
ed9eccbe
DS
1/*
2 include/linux/comedidev.h
3 header file for kernel-only structures, variables, and constants
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#ifndef _COMEDIDEV_H
25#define _COMEDIDEV_H
26
27#include <linux/kernel.h>
28#include <linux/module.h>
ed9eccbe
DS
29#include <linux/kdev_t.h>
30#include <linux/slab.h>
f9c51375 31#include <linux/delay.h>
ed9eccbe
DS
32#include <linux/errno.h>
33#include <linux/spinlock.h>
34#include <linux/mutex.h>
35#include <linux/wait.h>
36#include <linux/mm.h>
37#include <linux/init.h>
38#include <linux/vmalloc.h>
ed9eccbe 39#include <linux/dma-mapping.h>
e0dcef71
GKH
40#include <linux/uaccess.h>
41#include <linux/io.h>
c7a5d707 42#include <linux/timer.h>
ed9eccbe
DS
43
44#include "comedi.h"
45
e0dcef71
GKH
46#define DPRINTK(format, args...) do { \
47 if (comedi_debug) \
48 printk(KERN_DEBUG "comedi: " format , ## args); \
49} while (0)
ed9eccbe 50
e0dcef71 51#define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
62eeae93
SW
52#define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \
53 COMEDI_MINORVERSION, COMEDI_MICROVERSION)
ed9eccbe
DS
54#define COMEDI_RELEASE VERSION
55
ed9eccbe
DS
56#define PCI_VENDOR_ID_ADLINK 0x144a
57#define PCI_VENDOR_ID_ICP 0x104c
58#define PCI_VENDOR_ID_CONTEC 0x1221
ed9eccbe
DS
59
60#define COMEDI_NUM_MINORS 0x100
ed9eccbe
DS
61#define COMEDI_NUM_BOARD_MINORS 0x30
62#define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
63
ed9eccbe
DS
64#define COMEDI_DEVICE_CREATE(cs, parent, devt, drvdata, device, fmt...) \
65 device_create(cs, ((parent) ? (parent) : (device)), devt, drvdata, fmt)
66
34c43922 67struct comedi_subdevice {
71b5f4f1 68 struct comedi_device *device;
ed9eccbe
DS
69 int type;
70 int n_chan;
baf22b64 71 int subdev_flags;
ed9eccbe
DS
72 int len_chanlist; /* maximum length of channel/gain list */
73
74 void *private;
75
d163679c 76 struct comedi_async *async;
ed9eccbe
DS
77
78 void *lock;
79 void *busy;
80 unsigned runflags;
81 spinlock_t spin_lock;
82
83 int io_bits;
84
790c5541
BP
85 unsigned int maxdata; /* if maxdata==0, use list */
86 const unsigned int *maxdata_list; /* list is channel specific */
ed9eccbe
DS
87
88 unsigned int flags;
89 const unsigned int *flaglist;
90
91 unsigned int settling_time_0;
92
9ced1de6
BP
93 const struct comedi_lrange *range_table;
94 const struct comedi_lrange *const *range_table_list;
ed9eccbe
DS
95
96 unsigned int *chanlist; /* driver-owned chanlist (not used) */
97
0a85b6f0
MT
98 int (*insn_read) (struct comedi_device *, struct comedi_subdevice *,
99 struct comedi_insn *, unsigned int *);
100 int (*insn_write) (struct comedi_device *, struct comedi_subdevice *,
101 struct comedi_insn *, unsigned int *);
102 int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *,
103 struct comedi_insn *, unsigned int *);
104 int (*insn_config) (struct comedi_device *, struct comedi_subdevice *,
105 struct comedi_insn *, unsigned int *);
ed9eccbe 106
34c43922 107 int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
0a85b6f0
MT
108 int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *,
109 struct comedi_cmd *);
34c43922
BP
110 int (*poll) (struct comedi_device *, struct comedi_subdevice *);
111 int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
62eeae93
SW
112 /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
113 /* int (*do_unlock)(struct comedi_device *, \
114 struct comedi_subdevice *); */
ed9eccbe
DS
115
116 /* called when the buffer changes */
62eeae93
SW
117 int (*buf_change) (struct comedi_device *dev,
118 struct comedi_subdevice *s, unsigned long new_size);
ed9eccbe 119
62eeae93 120 void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s,
0a85b6f0
MT
121 void *data, unsigned int num_bytes,
122 unsigned int start_chan_index);
ed9eccbe
DS
123 enum dma_data_direction async_dma_dir;
124
125 unsigned int state;
126
0bfbbe8f 127 struct device *class_dev;
ed9eccbe
DS
128 int minor;
129};
130
131struct comedi_buf_page {
132 void *virt_addr;
133 dma_addr_t dma_addr;
134};
135
d163679c 136struct comedi_async {
34c43922 137 struct comedi_subdevice *subdevice;
ed9eccbe
DS
138
139 void *prealloc_buf; /* pre-allocated buffer */
140 unsigned int prealloc_bufsz; /* buffer size, in bytes */
62eeae93
SW
141 /* virtual and dma address of each page */
142 struct comedi_buf_page *buf_page_list;
ed9eccbe
DS
143 unsigned n_buf_pages; /* num elements in buf_page_list */
144
145 unsigned int max_bufsize; /* maximum buffer size, bytes */
62eeae93
SW
146 /* current number of mmaps of prealloc_buf */
147 unsigned int mmap_count;
148
149 /* byte count for writer (write completed) */
150 unsigned int buf_write_count;
151 /* byte count for writer (allocated for writing) */
152 unsigned int buf_write_alloc_count;
153 /* byte count for reader (read completed) */
154 unsigned int buf_read_count;
155 /* byte count for reader (allocated for reading) */
156 unsigned int buf_read_alloc_count;
ed9eccbe
DS
157
158 unsigned int buf_write_ptr; /* buffer marker for writer */
159 unsigned int buf_read_ptr; /* buffer marker for reader */
160
161 unsigned int cur_chan; /* useless channel marker for interrupt */
162 /* number of bytes that have been received for current scan */
163 unsigned int scan_progress;
164 /* keeps track of where we are in chanlist as for munging */
165 unsigned int munge_chan;
166 /* number of bytes that have been munged */
167 unsigned int munge_count;
168 /* buffer marker for munging */
169 unsigned int munge_ptr;
170
171 unsigned int events; /* events that have occurred */
172
ea6d0d4c 173 struct comedi_cmd cmd;
ed9eccbe
DS
174
175 wait_queue_head_t wait_head;
176
e0dcef71 177 /* callback stuff */
ed9eccbe
DS
178 unsigned int cb_mask;
179 int (*cb_func) (unsigned int flags, void *);
180 void *cb_arg;
181
62eeae93 182 int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
e0dcef71 183 unsigned int x);
ed9eccbe
DS
184};
185
139dfbdf
BP
186struct comedi_driver {
187 struct comedi_driver *next;
ed9eccbe
DS
188
189 const char *driver_name;
190 struct module *module;
0707bb04 191 int (*attach) (struct comedi_device *, struct comedi_devconfig *);
71b5f4f1 192 int (*detach) (struct comedi_device *);
ed9eccbe
DS
193
194 /* number of elements in board_name and board_id arrays */
195 unsigned int num_names;
196 const char *const *board_name;
197 /* offset in bytes from one board name pointer to the next */
198 int offset;
199};
200
71b5f4f1 201struct comedi_device {
ed9eccbe 202 int use_count;
139dfbdf 203 struct comedi_driver *driver;
ed9eccbe
DS
204 void *private;
205
0bfbbe8f 206 struct device *class_dev;
ed9eccbe 207 int minor;
e0dcef71
GKH
208 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
209 * for subdevices that have async_dma_dir set to something other than
210 * DMA_NONE */
ed9eccbe
DS
211 struct device *hw_dev;
212
213 const char *board_name;
214 const void *board_ptr;
215 int attached;
ed9eccbe
DS
216 spinlock_t spinlock;
217 struct mutex mutex;
218 int in_request_module;
219
220 int n_subdevices;
34c43922 221 struct comedi_subdevice *subdevices;
ed9eccbe
DS
222
223 /* dumb */
224 unsigned long iobase;
225 unsigned int irq;
226
34c43922
BP
227 struct comedi_subdevice *read_subdev;
228 struct comedi_subdevice *write_subdev;
ed9eccbe
DS
229
230 struct fasync_struct *async_queue;
231
3c17ba07 232 int (*open) (struct comedi_device *dev);
62eeae93 233 void (*close) (struct comedi_device *dev);
ed9eccbe
DS
234};
235
236struct comedi_device_file_info {
71b5f4f1 237 struct comedi_device *device;
34c43922
BP
238 struct comedi_subdevice *read_subdevice;
239 struct comedi_subdevice *write_subdevice;
ed9eccbe
DS
240};
241
242#ifdef CONFIG_COMEDI_DEBUG
243extern int comedi_debug;
244#else
e0dcef71 245static const int comedi_debug;
ed9eccbe
DS
246#endif
247
248/*
249 * function prototypes
250 */
251
34c43922 252void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
71b5f4f1 253void comedi_error(const struct comedi_device *dev, const char *s);
ed9eccbe
DS
254
255/* we can expand the number of bits used to encode devices/subdevices into
256 the minor number soon, after more distros support > 8 bit minor numbers
257 (like after Debian Etch gets released) */
258enum comedi_minor_bits {
259 COMEDI_DEVICE_MINOR_MASK = 0xf,
260 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
261};
262static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
263static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
264
e0dcef71 265struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor);
ed9eccbe 266
62eeae93
SW
267static inline struct comedi_subdevice *comedi_get_read_subdevice(
268 const struct comedi_device_file_info *info)
ed9eccbe 269{
e0dcef71
GKH
270 if (info->read_subdevice)
271 return info->read_subdevice;
272 if (info->device == NULL)
273 return NULL;
ed9eccbe
DS
274 return info->device->read_subdev;
275}
276
62eeae93
SW
277static inline struct comedi_subdevice *comedi_get_write_subdevice(
278 const struct comedi_device_file_info *info)
ed9eccbe 279{
e0dcef71
GKH
280 if (info->write_subdevice)
281 return info->write_subdevice;
282 if (info->device == NULL)
283 return NULL;
ed9eccbe
DS
284 return info->device->write_subdev;
285}
286
71b5f4f1 287void comedi_device_detach(struct comedi_device *dev);
0a85b6f0
MT
288int comedi_device_attach(struct comedi_device *dev,
289 struct comedi_devconfig *it);
139dfbdf
BP
290int comedi_driver_register(struct comedi_driver *);
291int comedi_driver_unregister(struct comedi_driver *);
ed9eccbe
DS
292
293void init_polling(void);
294void cleanup_polling(void);
71b5f4f1
BP
295void start_polling(struct comedi_device *);
296void stop_polling(struct comedi_device *);
ed9eccbe 297
ed9eccbe
DS
298#ifdef CONFIG_PROC_FS
299void comedi_proc_init(void);
300void comedi_proc_cleanup(void);
301#else
302static inline void comedi_proc_init(void)
303{
304}
0a85b6f0 305
ed9eccbe
DS
306static inline void comedi_proc_cleanup(void)
307{
308}
309#endif
310
311/* subdevice runflags */
312enum subdevice_runflags {
313 SRF_USER = 0x00000001,
314 SRF_RT = 0x00000002,
e0dcef71
GKH
315 /* indicates an COMEDI_CB_ERROR event has occurred since the last
316 * command was started */
ed9eccbe
DS
317 SRF_ERROR = 0x00000004,
318 SRF_RUNNING = 0x08000000
319};
320
5e220112
MR
321int comedi_check_chanlist(struct comedi_subdevice *s,
322 int n,
323 unsigned int *chanlist);
34c43922 324unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s);
ed9eccbe
DS
325
326/* range stuff */
327
e0dcef71
GKH
328#define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
329#define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
330#define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
baf22b64 331#define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
e0dcef71
GKH
332#define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
333#define UNI_RANGE(a) {0, (a)*1e6, 0}
ed9eccbe 334
9ced1de6
BP
335extern const struct comedi_lrange range_bipolar10;
336extern const struct comedi_lrange range_bipolar5;
337extern const struct comedi_lrange range_bipolar2_5;
338extern const struct comedi_lrange range_unipolar10;
339extern const struct comedi_lrange range_unipolar5;
340extern const struct comedi_lrange range_unknown;
ed9eccbe
DS
341
342#define range_digital range_unipolar5
343
344#if __GNUC__ >= 3
345#define GCC_ZERO_LENGTH_ARRAY
346#else
347#define GCC_ZERO_LENGTH_ARRAY 0
348#endif
349
9ced1de6 350struct comedi_lrange {
ed9eccbe 351 int length;
1f6325d6 352 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
ed9eccbe
DS
353};
354
355/* some silly little inline functions */
356
71b5f4f1 357static inline int alloc_subdevices(struct comedi_device *dev,
e0dcef71 358 unsigned int num_subdevices)
ed9eccbe
DS
359{
360 unsigned i;
361
362 dev->n_subdevices = num_subdevices;
363 dev->subdevices =
0a85b6f0
MT
364 kcalloc(num_subdevices, sizeof(struct comedi_subdevice),
365 GFP_KERNEL);
ed9eccbe
DS
366 if (!dev->subdevices)
367 return -ENOMEM;
368 for (i = 0; i < num_subdevices; ++i) {
369 dev->subdevices[i].device = dev;
370 dev->subdevices[i].async_dma_dir = DMA_NONE;
371 spin_lock_init(&dev->subdevices[i].spin_lock);
372 dev->subdevices[i].minor = -1;
373 }
374 return 0;
375}
376
71b5f4f1 377static inline int alloc_private(struct comedi_device *dev, int size)
ed9eccbe
DS
378{
379 dev->private = kzalloc(size, GFP_KERNEL);
380 if (!dev->private)
381 return -ENOMEM;
382 return 0;
383}
384
34c43922 385static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
ed9eccbe
DS
386{
387 if (subd->subdev_flags & SDF_LSAMPL)
790c5541 388 return sizeof(unsigned int);
ed9eccbe 389 else
790c5541 390 return sizeof(short);
ed9eccbe
DS
391}
392
393/* must be used in attach to set dev->hw_dev if you wish to dma directly
394into comedi's buffer */
0a85b6f0
MT
395static inline void comedi_set_hw_dev(struct comedi_device *dev,
396 struct device *hw_dev)
ed9eccbe 397{
e0dcef71 398 if (dev->hw_dev)
ed9eccbe 399 put_device(dev->hw_dev);
e0dcef71 400
ed9eccbe
DS
401 dev->hw_dev = hw_dev;
402 if (dev->hw_dev) {
403 dev->hw_dev = get_device(dev->hw_dev);
404 BUG_ON(dev->hw_dev == NULL);
405 }
406}
407
d163679c
BP
408int comedi_buf_put(struct comedi_async *async, short x);
409int comedi_buf_get(struct comedi_async *async, short *x);
ed9eccbe 410
d163679c 411unsigned int comedi_buf_write_n_available(struct comedi_async *async);
0a85b6f0
MT
412unsigned int comedi_buf_write_alloc(struct comedi_async *async,
413 unsigned int nbytes);
d163679c 414unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
0a85b6f0 415 unsigned int nbytes);
d163679c
BP
416unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes);
417unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes);
418unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes);
419unsigned int comedi_buf_read_n_available(struct comedi_async *async);
420void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
0a85b6f0 421 const void *source, unsigned int num_bytes);
d163679c 422void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
0a85b6f0 423 void *destination, unsigned int num_bytes);
d163679c 424static inline unsigned comedi_buf_write_n_allocated(struct comedi_async *async)
ed9eccbe
DS
425{
426 return async->buf_write_alloc_count - async->buf_write_count;
427}
0a85b6f0 428
d163679c 429static inline unsigned comedi_buf_read_n_allocated(struct comedi_async *async)
ed9eccbe
DS
430{
431 return async->buf_read_alloc_count - async->buf_read_count;
432}
433
ed9eccbe
DS
434static inline void *comedi_aux_data(int options[], int n)
435{
436 unsigned long address;
437 unsigned long addressLow;
438 int bit_shift;
439 if (sizeof(int) >= sizeof(void *))
440 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
441 else {
442 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
443 bit_shift = sizeof(int) * 8;
444 address <<= bit_shift;
445 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
446 addressLow &= (1UL << bit_shift) - 1;
447 address |= addressLow;
448 }
449 if (n >= 1)
450 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
451 if (n >= 2)
452 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
453 if (n >= 3)
454 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
455 BUG_ON(n > 3);
456 return (void *)address;
457}
458
0a85b6f0
MT
459int comedi_alloc_subdevice_minor(struct comedi_device *dev,
460 struct comedi_subdevice *s);
34c43922 461void comedi_free_subdevice_minor(struct comedi_subdevice *s);
ed9eccbe
DS
462int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name);
463void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
0a85b6f0 464struct usb_device; /* forward declaration */
c28264da
BP
465int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name);
466void comedi_usb_auto_unconfig(struct usb_device *usbdev);
ed9eccbe 467
056d3ce5 468#ifdef CONFIG_COMEDI_PCI_DRIVERS
0a85b6f0 469#define CONFIG_COMEDI_PCI
056d3ce5
GKH
470#endif
471#ifdef CONFIG_COMEDI_PCI_DRIVERS_MODULE
0a85b6f0 472#define CONFIG_COMEDI_PCI
056d3ce5
GKH
473#endif
474#ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
0a85b6f0 475#define CONFIG_COMEDI_PCMCIA
056d3ce5
GKH
476#endif
477#ifdef CONFIG_COMEDI_PCMCIA_DRIVERS_MODULE
0a85b6f0 478#define CONFIG_COMEDI_PCMCIA
056d3ce5
GKH
479#endif
480
ed9eccbe 481#endif /* _COMEDIDEV_H */