Linux 3.3-rc6
[linux/fpc-iii.git] / drivers / staging / comedi / comedidev.h
blob7a0d4bcbc355f944673fa8ac8c9f4738e075126f
1 /*
2 include/linux/comedidev.h
3 header file for kernel-only structures, variables, and constants
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
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.
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.
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.
24 #ifndef _COMEDIDEV_H
25 #define _COMEDIDEV_H
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/kdev_t.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
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>
39 #include <linux/dma-mapping.h>
40 #include <linux/uaccess.h>
41 #include <linux/io.h>
42 #include <linux/timer.h>
44 #include "comedi.h"
46 #define DPRINTK(format, args...) do { \
47 if (comedi_debug) \
48 printk(KERN_DEBUG "comedi: " format , ## args); \
49 } while (0)
51 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
52 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \
53 COMEDI_MINORVERSION, COMEDI_MICROVERSION)
54 #define COMEDI_RELEASE VERSION
56 #define PCI_VENDOR_ID_ADLINK 0x144a
57 #define PCI_VENDOR_ID_ICP 0x104c
58 #define PCI_VENDOR_ID_CONTEC 0x1221
60 #define COMEDI_NUM_MINORS 0x100
61 #define COMEDI_NUM_BOARD_MINORS 0x30
62 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
64 struct comedi_subdevice {
65 struct comedi_device *device;
66 int type;
67 int n_chan;
68 int subdev_flags;
69 int len_chanlist; /* maximum length of channel/gain list */
71 void *private;
73 struct comedi_async *async;
75 void *lock;
76 void *busy;
77 unsigned runflags;
78 spinlock_t spin_lock;
80 int io_bits;
82 unsigned int maxdata; /* if maxdata==0, use list */
83 const unsigned int *maxdata_list; /* list is channel specific */
85 unsigned int flags;
86 const unsigned int *flaglist;
88 unsigned int settling_time_0;
90 const struct comedi_lrange *range_table;
91 const struct comedi_lrange *const *range_table_list;
93 unsigned int *chanlist; /* driver-owned chanlist (not used) */
95 int (*insn_read) (struct comedi_device *, struct comedi_subdevice *,
96 struct comedi_insn *, unsigned int *);
97 int (*insn_write) (struct comedi_device *, struct comedi_subdevice *,
98 struct comedi_insn *, unsigned int *);
99 int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *,
100 struct comedi_insn *, unsigned int *);
101 int (*insn_config) (struct comedi_device *, struct comedi_subdevice *,
102 struct comedi_insn *, unsigned int *);
104 int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
105 int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *,
106 struct comedi_cmd *);
107 int (*poll) (struct comedi_device *, struct comedi_subdevice *);
108 int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
109 /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
110 /* int (*do_unlock)(struct comedi_device *, \
111 struct comedi_subdevice *); */
113 /* called when the buffer changes */
114 int (*buf_change) (struct comedi_device *dev,
115 struct comedi_subdevice *s, unsigned long new_size);
117 void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s,
118 void *data, unsigned int num_bytes,
119 unsigned int start_chan_index);
120 enum dma_data_direction async_dma_dir;
122 unsigned int state;
124 struct device *class_dev;
125 int minor;
128 struct comedi_buf_page {
129 void *virt_addr;
130 dma_addr_t dma_addr;
133 struct comedi_async {
134 struct comedi_subdevice *subdevice;
136 void *prealloc_buf; /* pre-allocated buffer */
137 unsigned int prealloc_bufsz; /* buffer size, in bytes */
138 /* virtual and dma address of each page */
139 struct comedi_buf_page *buf_page_list;
140 unsigned n_buf_pages; /* num elements in buf_page_list */
142 unsigned int max_bufsize; /* maximum buffer size, bytes */
143 /* current number of mmaps of prealloc_buf */
144 unsigned int mmap_count;
146 /* byte count for writer (write completed) */
147 unsigned int buf_write_count;
148 /* byte count for writer (allocated for writing) */
149 unsigned int buf_write_alloc_count;
150 /* byte count for reader (read completed) */
151 unsigned int buf_read_count;
152 /* byte count for reader (allocated for reading) */
153 unsigned int buf_read_alloc_count;
155 unsigned int buf_write_ptr; /* buffer marker for writer */
156 unsigned int buf_read_ptr; /* buffer marker for reader */
158 unsigned int cur_chan; /* useless channel marker for interrupt */
159 /* number of bytes that have been received for current scan */
160 unsigned int scan_progress;
161 /* keeps track of where we are in chanlist as for munging */
162 unsigned int munge_chan;
163 /* number of bytes that have been munged */
164 unsigned int munge_count;
165 /* buffer marker for munging */
166 unsigned int munge_ptr;
168 unsigned int events; /* events that have occurred */
170 struct comedi_cmd cmd;
172 wait_queue_head_t wait_head;
174 /* callback stuff */
175 unsigned int cb_mask;
176 int (*cb_func) (unsigned int flags, void *);
177 void *cb_arg;
179 int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
180 unsigned int x);
183 struct comedi_driver {
184 struct comedi_driver *next;
186 const char *driver_name;
187 struct module *module;
188 int (*attach) (struct comedi_device *, struct comedi_devconfig *);
189 int (*detach) (struct comedi_device *);
191 /* number of elements in board_name and board_id arrays */
192 unsigned int num_names;
193 const char *const *board_name;
194 /* offset in bytes from one board name pointer to the next */
195 int offset;
198 struct comedi_device {
199 int use_count;
200 struct comedi_driver *driver;
201 void *private;
203 struct device *class_dev;
204 int minor;
205 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
206 * for subdevices that have async_dma_dir set to something other than
207 * DMA_NONE */
208 struct device *hw_dev;
210 const char *board_name;
211 const void *board_ptr;
212 int attached;
213 spinlock_t spinlock;
214 struct mutex mutex;
215 int in_request_module;
217 int n_subdevices;
218 struct comedi_subdevice *subdevices;
220 /* dumb */
221 unsigned long iobase;
222 unsigned int irq;
224 struct comedi_subdevice *read_subdev;
225 struct comedi_subdevice *write_subdev;
227 struct fasync_struct *async_queue;
229 int (*open) (struct comedi_device *dev);
230 void (*close) (struct comedi_device *dev);
233 struct comedi_device_file_info {
234 struct comedi_device *device;
235 struct comedi_subdevice *read_subdevice;
236 struct comedi_subdevice *write_subdevice;
239 #ifdef CONFIG_COMEDI_DEBUG
240 extern int comedi_debug;
241 #else
242 static const int comedi_debug;
243 #endif
246 * function prototypes
249 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
250 void comedi_error(const struct comedi_device *dev, const char *s);
252 /* we can expand the number of bits used to encode devices/subdevices into
253 the minor number soon, after more distros support > 8 bit minor numbers
254 (like after Debian Etch gets released) */
255 enum comedi_minor_bits {
256 COMEDI_DEVICE_MINOR_MASK = 0xf,
257 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
259 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
260 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
262 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor);
264 static inline struct comedi_subdevice *comedi_get_read_subdevice(
265 const struct comedi_device_file_info *info)
267 if (info->read_subdevice)
268 return info->read_subdevice;
269 if (info->device == NULL)
270 return NULL;
271 return info->device->read_subdev;
274 static inline struct comedi_subdevice *comedi_get_write_subdevice(
275 const struct comedi_device_file_info *info)
277 if (info->write_subdevice)
278 return info->write_subdevice;
279 if (info->device == NULL)
280 return NULL;
281 return info->device->write_subdev;
284 void comedi_device_detach(struct comedi_device *dev);
285 int comedi_device_attach(struct comedi_device *dev,
286 struct comedi_devconfig *it);
287 int comedi_driver_register(struct comedi_driver *);
288 int comedi_driver_unregister(struct comedi_driver *);
290 void init_polling(void);
291 void cleanup_polling(void);
292 void start_polling(struct comedi_device *);
293 void stop_polling(struct comedi_device *);
295 #ifdef CONFIG_PROC_FS
296 void comedi_proc_init(void);
297 void comedi_proc_cleanup(void);
298 #else
299 static inline void comedi_proc_init(void)
303 static inline void comedi_proc_cleanup(void)
306 #endif
308 /* subdevice runflags */
309 enum subdevice_runflags {
310 SRF_USER = 0x00000001,
311 SRF_RT = 0x00000002,
312 /* indicates an COMEDI_CB_ERROR event has occurred since the last
313 * command was started */
314 SRF_ERROR = 0x00000004,
315 SRF_RUNNING = 0x08000000
318 int comedi_check_chanlist(struct comedi_subdevice *s,
319 int n,
320 unsigned int *chanlist);
321 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s);
323 /* range stuff */
325 #define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
326 #define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
327 #define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
328 #define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
329 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
330 #define UNI_RANGE(a) {0, (a)*1e6, 0}
332 extern const struct comedi_lrange range_bipolar10;
333 extern const struct comedi_lrange range_bipolar5;
334 extern const struct comedi_lrange range_bipolar2_5;
335 extern const struct comedi_lrange range_unipolar10;
336 extern const struct comedi_lrange range_unipolar5;
337 extern const struct comedi_lrange range_unknown;
339 #define range_digital range_unipolar5
341 #if __GNUC__ >= 3
342 #define GCC_ZERO_LENGTH_ARRAY
343 #else
344 #define GCC_ZERO_LENGTH_ARRAY 0
345 #endif
347 struct comedi_lrange {
348 int length;
349 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
352 /* some silly little inline functions */
354 static inline int alloc_subdevices(struct comedi_device *dev,
355 unsigned int num_subdevices)
357 unsigned i;
359 dev->n_subdevices = num_subdevices;
360 dev->subdevices =
361 kcalloc(num_subdevices, sizeof(struct comedi_subdevice),
362 GFP_KERNEL);
363 if (!dev->subdevices)
364 return -ENOMEM;
365 for (i = 0; i < num_subdevices; ++i) {
366 dev->subdevices[i].device = dev;
367 dev->subdevices[i].async_dma_dir = DMA_NONE;
368 spin_lock_init(&dev->subdevices[i].spin_lock);
369 dev->subdevices[i].minor = -1;
371 return 0;
374 static inline int alloc_private(struct comedi_device *dev, int size)
376 dev->private = kzalloc(size, GFP_KERNEL);
377 if (!dev->private)
378 return -ENOMEM;
379 return 0;
382 static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
384 if (subd->subdev_flags & SDF_LSAMPL)
385 return sizeof(unsigned int);
386 else
387 return sizeof(short);
390 /* must be used in attach to set dev->hw_dev if you wish to dma directly
391 into comedi's buffer */
392 static inline void comedi_set_hw_dev(struct comedi_device *dev,
393 struct device *hw_dev)
395 if (dev->hw_dev)
396 put_device(dev->hw_dev);
398 dev->hw_dev = hw_dev;
399 if (dev->hw_dev) {
400 dev->hw_dev = get_device(dev->hw_dev);
401 BUG_ON(dev->hw_dev == NULL);
405 int comedi_buf_put(struct comedi_async *async, short x);
406 int comedi_buf_get(struct comedi_async *async, short *x);
408 unsigned int comedi_buf_write_n_available(struct comedi_async *async);
409 unsigned int comedi_buf_write_alloc(struct comedi_async *async,
410 unsigned int nbytes);
411 unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
412 unsigned int nbytes);
413 unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes);
414 unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes);
415 unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes);
416 unsigned int comedi_buf_read_n_available(struct comedi_async *async);
417 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
418 const void *source, unsigned int num_bytes);
419 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
420 void *destination, unsigned int num_bytes);
421 static inline unsigned comedi_buf_write_n_allocated(struct comedi_async *async)
423 return async->buf_write_alloc_count - async->buf_write_count;
426 static inline unsigned comedi_buf_read_n_allocated(struct comedi_async *async)
428 return async->buf_read_alloc_count - async->buf_read_count;
431 static inline void *comedi_aux_data(int options[], int n)
433 unsigned long address;
434 unsigned long addressLow;
435 int bit_shift;
436 if (sizeof(int) >= sizeof(void *))
437 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
438 else {
439 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
440 bit_shift = sizeof(int) * 8;
441 address <<= bit_shift;
442 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
443 addressLow &= (1UL << bit_shift) - 1;
444 address |= addressLow;
446 if (n >= 1)
447 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
448 if (n >= 2)
449 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
450 if (n >= 3)
451 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
452 BUG_ON(n > 3);
453 return (void *)address;
456 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
457 struct comedi_subdevice *s);
458 void comedi_free_subdevice_minor(struct comedi_subdevice *s);
459 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name);
460 void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
461 struct usb_device; /* forward declaration */
462 int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name);
463 void comedi_usb_auto_unconfig(struct usb_device *usbdev);
465 #ifdef CONFIG_COMEDI_PCI_DRIVERS
466 #define CONFIG_COMEDI_PCI
467 #endif
468 #ifdef CONFIG_COMEDI_PCI_DRIVERS_MODULE
469 #define CONFIG_COMEDI_PCI
470 #endif
471 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
472 #define CONFIG_COMEDI_PCMCIA
473 #endif
474 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS_MODULE
475 #define CONFIG_COMEDI_PCMCIA
476 #endif
478 #endif /* _COMEDIDEV_H */