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.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/version.h>
30 #include <linux/config.h>
31 #include <linux/kdev_t.h>
32 #include <linux/slab.h>
33 #include <linux/errno.h>
34 #include <linux/spinlock.h>
35 #include <linux/wait.h>
37 #include <linux/init.h>
38 #include <linux/vmalloc.h>
40 #include <linux/interrupt.h>
41 #include <linux/dma-mapping.h>
42 #include <asm/uaccess.h>
45 #include <linux/comedi.h>
50 #define DPRINTK(format, args...) do{ \
51 if(comedi_debug)printk("comedi: " format , ## args ); \
54 #define COMEDI_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
55 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION,COMEDI_MINORVERSION,COMEDI_MICROVERSION)
56 #define COMEDI_RELEASE VERSION
58 #define COMEDI_INITCLEANUP_NOMODULE(x) \
59 static int __init x ## _init_module(void) \
60 {return comedi_driver_register(&(x));} \
61 static void __exit x ## _cleanup_module(void) \
62 {comedi_driver_unregister(&(x));} \
63 module_init(x ## _init_module); \
64 module_exit(x ## _cleanup_module); \
66 #define COMEDI_INITCLEANUP(x) \
67 MODULE_AUTHOR("David A. Schleef <ds@schleef.org>"); \
68 MODULE_DESCRIPTION("Comedi low-level driver"); \
69 MODULE_LICENSE("GPL"); \
70 static int __init x ## _init_module(void) \
71 {return comedi_driver_register(&(x));} \
72 static void __exit x ## _cleanup_module(void) \
73 {comedi_driver_unregister(&(x));} \
74 module_init(x ## _init_module); \
75 module_exit(x ## _cleanup_module); \
78 #define PCI_VENDOR_ID_INOVA 0x104c
79 #define PCI_VENDOR_ID_NATINST 0x1093
80 #define PCI_VENDOR_ID_DATX 0x1116
81 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
82 #define PCI_VENDOR_ID_ADVANTECH 0x13fe
83 #define PCI_VENDOR_ID_RTD 0x1435
84 #define PCI_VENDOR_ID_AMPLICON 0x14dc
85 #define PCI_VENDOR_ID_ADLINK 0x144a
86 #define PCI_VENDOR_ID_ICP 0x104c
87 #define PCI_VENDOR_ID_CONTEC 0x1221
88 #define PCI_VENDOR_ID_MEILHAUS 0x1402
91 typedef struct comedi_device_struct comedi_device;
92 typedef struct comedi_subdevice_struct comedi_subdevice;
93 typedef struct comedi_async_struct comedi_async;
94 typedef struct comedi_driver_struct comedi_driver;
95 typedef struct comedi_lrange_struct comedi_lrange;
98 struct comedi_subdevice_struct{
99 comedi_device *device;
102 volatile int subdev_flags;
103 int len_chanlist; /* maximum length of channel/gain list */
111 volatile unsigned int runflags;
115 lsampl_t maxdata; /* if maxdata==0, use list */
116 lsampl_t *maxdata_list; /* list is channel specific */
119 unsigned int *flaglist;
121 unsigned int settling_time_0;
123 comedi_lrange *range_table;
124 comedi_lrange **range_table_list;
126 unsigned int *chanlist; /* driver-owned chanlist (not used) */
128 int (*insn_read)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
129 int (*insn_write)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
130 int (*insn_bits)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
131 int (*insn_config)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
133 int (*do_cmd)(comedi_device *,comedi_subdevice *);
134 int (*do_cmdtest)(comedi_device *,comedi_subdevice *,comedi_cmd *);
135 void (*cmd_cleanup)(comedi_device *, comedi_subdevice *);
136 int (*poll)(comedi_device *,comedi_subdevice *);
137 int (*cancel)(comedi_device *,comedi_subdevice *);
138 //int (*do_lock)(comedi_device *,comedi_subdevice *);
139 //int (*do_unlock)(comedi_device *,comedi_subdevice *);
141 /* called when the buffer changes */
142 int (*buf_change)(comedi_device *dev,comedi_subdevice *s,unsigned long new_size);
144 void (*munge)( comedi_device *dev, comedi_subdevice *s, void *data,
145 unsigned int num_bytes, unsigned int start_chan_index );
146 enum dma_data_direction async_dma_dir;
150 struct class_device *class_dev;
153 struct comedi_buf_page
159 struct comedi_async_struct{
160 comedi_subdevice *subdevice;
162 void *prealloc_buf; /* pre-allocated buffer */
163 unsigned int prealloc_bufsz; /* buffer size, in bytes */
164 struct comedi_buf_page *buf_page_list; /* virtual and dma address of each page */
165 unsigned n_buf_pages; /* num elements in buf_page_list */
167 unsigned int max_bufsize; /* maximum buffer size, bytes */
168 unsigned int mmap_count; /* current number of mmaps of prealloc_buf */
170 unsigned int buf_write_count; /* byte count for writer (write completed) */
171 unsigned int buf_write_alloc_count; /* byte count for writer (allocated for writing) */
172 unsigned int buf_read_count; /* byte count for reader (read completed)*/
173 unsigned int buf_read_alloc_count; /* byte count for reader (allocated for reading)*/
175 unsigned int buf_write_ptr; /* buffer marker for writer */
176 unsigned int buf_read_ptr; /* buffer marker for reader */
178 unsigned int cur_chan; /* useless channel marker for interrupt */
179 /* number of bytes that have been received for current scan */
180 unsigned int scan_progress;
181 /* keeps track of where we are in chanlist as for munging */
182 unsigned int munge_chan;
183 /* number of bytes that have been munged */
184 unsigned int munge_count;
185 /* buffer marker for munging */
186 unsigned int munge_ptr;
188 unsigned int events; /* events that have occurred */
193 unsigned int cb_mask;
194 int (*cb_func)(unsigned int flags,void *);
197 int (*inttrig)(comedi_device *dev,comedi_subdevice *s,unsigned int x);
200 struct comedi_driver_struct{
201 struct comedi_driver_struct *next;
203 const char *driver_name;
204 struct module *module;
205 int (*attach)(comedi_device *,comedi_devconfig *);
206 int (*detach)(comedi_device *);
208 /* number of elements in board_name and board_id arrays */
209 unsigned int num_names;
210 const char **board_name;
211 /* offset in bytes from one board name pointer to the next */
215 struct comedi_device_struct{
217 comedi_driver *driver;
220 struct class_device *class_dev;
222 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers for subdevices
223 that have async_dma_dir set to something other than DMA_NONE */
224 struct device *hw_dev;
226 const char *board_name;
227 const void * board_ptr;
231 int in_request_module;
234 comedi_subdevice *subdevices;
237 unsigned long iobase;
240 comedi_subdevice *read_subdev;
241 wait_queue_head_t read_wait;
243 comedi_subdevice *write_subdev;
244 wait_queue_head_t write_wait;
246 struct fasync_struct *async_queue;
248 void (*open)(comedi_device *dev);
249 void (*close)(comedi_device *dev);
252 struct comedi_inode_private
254 comedi_device *device;
255 comedi_subdevice *subdevice;
258 extern comedi_device *comedi_devices;
259 extern spinlock_t big_comedi_lock;
261 #ifdef CONFIG_COMEDI_DEBUG
262 extern int comedi_debug;
264 static const int comedi_debug = 0;
268 * function prototypes
271 void comedi_event(comedi_device *dev,comedi_subdevice *s,unsigned int mask);
272 void comedi_error(const comedi_device *dev,const char *s);
274 /* we can expand the number of bits used to encode devices/subdevices into
275 the minor number soon, after more distros support > 8 bit minor numbers
276 (like after Debian Etch gets released) */
277 enum comedi_minor_bits
279 COMEDI_DEVICE_MINOR_MASK = 0xf,
280 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
282 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
283 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
284 static const unsigned COMEDI_NUM_MINORS = 0x100;
286 static inline comedi_device * comedi_get_device_by_minor(unsigned minor)
288 unsigned device_index;
289 if(minor >= COMEDI_NUM_MINORS) return NULL;
290 device_index = minor & COMEDI_DEVICE_MINOR_MASK;
291 if(device_index >= COMEDI_NDEVICES) return NULL;
292 return comedi_devices + device_index;
295 static inline comedi_subdevice * comedi_get_subdevice_by_minor(unsigned minor)
297 unsigned subdevice_index;
300 if((minor & COMEDI_SUBDEVICE_MINOR_MASK) == 0) return NULL;
301 dev = comedi_get_device_by_minor(minor);
302 if(dev == NULL) return NULL;
303 subdevice_index = ((minor & COMEDI_SUBDEVICE_MINOR_MASK) >> COMEDI_SUBDEVICE_MINOR_SHIFT) - COMEDI_SUBDEVICE_MINOR_OFFSET;
304 if(subdevice_index >= dev->n_subdevices) return NULL;
305 return dev->subdevices + subdevice_index;
308 static inline unsigned comedi_construct_minor_for_subdevice(comedi_device *dev, unsigned subdevice_index)
311 minor |= dev->minor & COMEDI_DEVICE_MINOR_MASK;
312 minor |= ((subdevice_index + COMEDI_SUBDEVICE_MINOR_OFFSET) << COMEDI_SUBDEVICE_MINOR_SHIFT) & COMEDI_SUBDEVICE_MINOR_MASK;
313 BUG_ON(minor >= COMEDI_NUM_MINORS);
317 int comedi_device_detach(comedi_device *dev);
318 int comedi_device_attach(comedi_device *dev,comedi_devconfig *it);
319 int comedi_driver_register(comedi_driver *);
320 int comedi_driver_unregister(comedi_driver *);
322 comedi_device *comedi_allocate_dev(comedi_driver *);
323 void comedi_deallocate_dev(comedi_device *);
325 void init_polling(void);
326 void cleanup_polling(void);
327 void start_polling(comedi_device *);
328 void stop_polling(comedi_device *);
330 int comedi_buf_alloc(comedi_device *dev, comedi_subdevice *s, unsigned long
333 #ifdef CONFIG_PROC_FS
334 void comedi_proc_init(void);
335 void comedi_proc_cleanup(void);
337 static inline void comedi_proc_init(void) {}
338 static inline void comedi_proc_cleanup(void) {}
341 /* subdevice runflags */
342 #define SRF_USER 0x00000001
343 #define SRF_RT 0x00000002
344 /* indicates an COMEDI_CB_ERROR event has occurred since the last command was started */
345 #define SRF_ERROR 0x00000004
348 various internal comedi functions
351 int do_rangeinfo_ioctl(comedi_device *dev,comedi_rangeinfo *arg);
352 int check_chanlist(comedi_subdevice *s,int n,unsigned int *chanlist);
357 #define RANGE(a,b) {(a)*1e6,(b)*1e6,0}
358 #define RANGE_ext(a,b) {(a)*1e6,(b)*1e6,RF_EXTERNAL}
359 #define RANGE_mA(a,b) {(a)*1e6,(b)*1e6,UNIT_mA}
360 #define RANGE_unitless(a,b) {(a)*1e6,(b)*1e6,0} /* XXX */
361 #define BIP_RANGE(a) {-(a)*1e6,(a)*1e6,0}
362 #define UNI_RANGE(a) {0,(a)*1e6,0}
364 extern comedi_lrange range_bipolar10;
365 extern comedi_lrange range_bipolar5;
366 extern comedi_lrange range_bipolar2_5;
367 extern comedi_lrange range_unipolar10;
368 extern comedi_lrange range_unipolar5;
369 extern comedi_lrange range_unknown;
371 #define range_digital range_unipolar5
374 #define GCC_ZERO_LENGTH_ARRAY
376 #define GCC_ZERO_LENGTH_ARRAY 0
379 struct comedi_lrange_struct{
381 comedi_krange range[ GCC_ZERO_LENGTH_ARRAY ];
386 /* some silly little inline functions */
388 static inline int alloc_subdevices(comedi_device *dev, unsigned int num_subdevices)
390 const int size = sizeof(comedi_subdevice) * num_subdevices;
393 dev->n_subdevices = num_subdevices;
394 dev->subdevices = kmalloc(size,GFP_KERNEL);
397 memset(dev->subdevices,0,size);
398 for(i = 0; i < num_subdevices; ++i)
400 dev->subdevices[i].device = dev;
401 dev->subdevices[i].async_dma_dir = DMA_NONE;
406 static inline int alloc_private(comedi_device *dev,int size)
408 dev->private=kmalloc(size,GFP_KERNEL);
411 memset(dev->private,0,size);
415 static inline unsigned int bytes_per_sample( const comedi_subdevice *subd )
417 if( subd->subdev_flags & SDF_LSAMPL )
418 return sizeof( lsampl_t );
420 return sizeof( sampl_t );
423 static inline void comedi_set_hw_dev(comedi_device *dev, struct device *hw_dev)
427 put_device(dev->hw_dev);
429 dev->hw_dev = hw_dev;
432 get_device(dev->hw_dev);
436 int comedi_buf_put(comedi_async *async, sampl_t x);
437 int comedi_buf_get(comedi_async *async, sampl_t *x);
439 unsigned int comedi_buf_write_n_available(comedi_async *async);
440 unsigned int comedi_buf_write_alloc(comedi_async *async, unsigned int nbytes);
441 unsigned int comedi_buf_write_alloc_strict(comedi_async *async, unsigned int nbytes);
442 unsigned comedi_buf_write_free(comedi_async *async, unsigned int nbytes);
443 unsigned comedi_buf_read_alloc(comedi_async *async, unsigned nbytes);
444 unsigned comedi_buf_read_free(comedi_async *async, unsigned int nbytes);
445 unsigned int comedi_buf_read_n_available(comedi_async *async);
446 void comedi_buf_memcpy_to( comedi_async *async, unsigned int offset, const void *source,
447 unsigned int num_bytes );
448 void comedi_buf_memcpy_from( comedi_async *async, unsigned int offset, void *destination,
449 unsigned int num_bytes );
451 void comedi_reset_async_buf(comedi_async *async);
453 static inline void* comedi_aux_data(int options[], int n)
455 unsigned long address;
456 unsigned long addressLow;
458 if(sizeof(int) >= sizeof(void*))
459 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
462 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
463 bit_shift = sizeof(int) * 8;
464 address <<= bit_shift;
465 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
466 addressLow &= (1UL << bit_shift) - 1;
467 address |= addressLow;
470 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
472 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
474 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
476 return (void*) address;
478 //#ifdef CONFIG_COMEDI_RT
479 #include <linux/comedi_rt.h>
483 #endif /* _COMEDIDEV_H */