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/kdev_t.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/spinlock.h>
34 #include <linux/mutex.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>
47 #define DPRINTK(format, args...) do{ \
48 if(comedi_debug)printk("comedi: " format , ## args ); \
51 #define COMEDI_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
52 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION,COMEDI_MINORVERSION,COMEDI_MICROVERSION)
53 #define COMEDI_RELEASE VERSION
55 #define COMEDI_INITCLEANUP_NOMODULE(x) \
56 static int __init x ## _init_module(void) \
57 {return comedi_driver_register(&(x));} \
58 static void __exit x ## _cleanup_module(void) \
59 {comedi_driver_unregister(&(x));} \
60 module_init(x ## _init_module); \
61 module_exit(x ## _cleanup_module); \
63 #define COMEDI_MODULE_MACROS \
64 MODULE_AUTHOR("Comedi http://www.comedi.org"); \
65 MODULE_DESCRIPTION("Comedi low-level driver"); \
66 MODULE_LICENSE("GPL"); \
68 #define COMEDI_INITCLEANUP(x) \
69 COMEDI_MODULE_MACROS \
70 COMEDI_INITCLEANUP_NOMODULE(x)
72 #define COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table) \
73 static int __devinit comedi_driver ## _pci_probe(struct pci_dev *dev, \
74 const struct pci_device_id *ent) \
76 return comedi_pci_auto_config(dev, comedi_driver.driver_name); \
78 static void __devexit comedi_driver ## _pci_remove(struct pci_dev *dev) \
80 comedi_pci_auto_unconfig(dev); \
82 static struct pci_driver comedi_driver ## _pci_driver = \
84 .id_table = pci_id_table, \
85 .probe = & comedi_driver ## _pci_probe, \
86 .remove = __devexit_p(& comedi_driver ## _pci_remove) \
88 static int __init comedi_driver ## _init_module(void) \
91 retval = comedi_driver_register(& comedi_driver); \
92 if(retval < 0) return retval; \
93 comedi_driver ## _pci_driver.name = (char*)comedi_driver.driver_name; \
94 return pci_register_driver(& comedi_driver ## _pci_driver); \
96 static void __exit comedi_driver ## _cleanup_module(void) \
98 pci_unregister_driver(& comedi_driver ## _pci_driver); \
99 comedi_driver_unregister(& comedi_driver); \
101 module_init(comedi_driver ## _init_module); \
102 module_exit(comedi_driver ## _cleanup_module);
104 #define COMEDI_PCI_INITCLEANUP(comedi_driver, pci_id_table) \
105 COMEDI_MODULE_MACROS \
106 COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table)
108 #define PCI_VENDOR_ID_INOVA 0x104c
109 #define PCI_VENDOR_ID_NATINST 0x1093
110 #define PCI_VENDOR_ID_DATX 0x1116
111 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
112 #define PCI_VENDOR_ID_ADVANTECH 0x13fe
113 #define PCI_VENDOR_ID_RTD 0x1435
114 #define PCI_VENDOR_ID_AMPLICON 0x14dc
115 #define PCI_VENDOR_ID_ADLINK 0x144a
116 #define PCI_VENDOR_ID_ICP 0x104c
117 #define PCI_VENDOR_ID_CONTEC 0x1221
118 #define PCI_VENDOR_ID_MEILHAUS 0x1402
120 #define COMEDI_NUM_MINORS 0x100
121 #define COMEDI_NUM_LEGACY_MINORS 0x10
122 #define COMEDI_NUM_BOARD_MINORS 0x30
123 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
125 typedef struct comedi_device_struct comedi_device;
126 typedef struct comedi_subdevice_struct comedi_subdevice;
127 typedef struct comedi_async_struct comedi_async;
128 typedef struct comedi_driver_struct comedi_driver;
129 typedef struct comedi_lrange_struct comedi_lrange;
131 typedef struct device device_create_result_type;
133 #define COMEDI_DEVICE_CREATE(cs, parent, devt, drvdata, device, fmt...) \
134 device_create(cs, ((parent) ? (parent) : (device)), devt, drvdata, fmt)
136 struct comedi_subdevice_struct {
137 comedi_device *device;
140 volatile int subdev_flags;
141 int len_chanlist; /* maximum length of channel/gain list */
150 spinlock_t spin_lock;
154 lsampl_t maxdata; /* if maxdata==0, use list */
155 const lsampl_t *maxdata_list; /* list is channel specific */
158 const unsigned int *flaglist;
160 unsigned int settling_time_0;
162 const comedi_lrange *range_table;
163 const comedi_lrange *const *range_table_list;
165 unsigned int *chanlist; /* driver-owned chanlist (not used) */
167 int (*insn_read) (comedi_device *, comedi_subdevice *, comedi_insn *,
169 int (*insn_write) (comedi_device *, comedi_subdevice *, comedi_insn *,
171 int (*insn_bits) (comedi_device *, comedi_subdevice *, comedi_insn *,
173 int (*insn_config) (comedi_device *, comedi_subdevice *, comedi_insn *,
176 int (*do_cmd) (comedi_device *, comedi_subdevice *);
177 int (*do_cmdtest) (comedi_device *, comedi_subdevice *, comedi_cmd *);
178 int (*poll) (comedi_device *, comedi_subdevice *);
179 int (*cancel) (comedi_device *, comedi_subdevice *);
180 //int (*do_lock)(comedi_device *,comedi_subdevice *);
181 //int (*do_unlock)(comedi_device *,comedi_subdevice *);
183 /* called when the buffer changes */
184 int (*buf_change) (comedi_device * dev, comedi_subdevice * s,
185 unsigned long new_size);
187 void (*munge) (comedi_device * dev, comedi_subdevice * s, void *data,
188 unsigned int num_bytes, unsigned int start_chan_index);
189 enum dma_data_direction async_dma_dir;
193 device_create_result_type *class_dev;
197 struct comedi_buf_page {
202 struct comedi_async_struct {
203 comedi_subdevice *subdevice;
205 void *prealloc_buf; /* pre-allocated buffer */
206 unsigned int prealloc_bufsz; /* buffer size, in bytes */
207 struct comedi_buf_page *buf_page_list; /* virtual and dma address of each page */
208 unsigned n_buf_pages; /* num elements in buf_page_list */
210 unsigned int max_bufsize; /* maximum buffer size, bytes */
211 unsigned int mmap_count; /* current number of mmaps of prealloc_buf */
213 unsigned int buf_write_count; /* byte count for writer (write completed) */
214 unsigned int buf_write_alloc_count; /* byte count for writer (allocated for writing) */
215 unsigned int buf_read_count; /* byte count for reader (read completed) */
216 unsigned int buf_read_alloc_count; /* byte count for reader (allocated for reading) */
218 unsigned int buf_write_ptr; /* buffer marker for writer */
219 unsigned int buf_read_ptr; /* buffer marker for reader */
221 unsigned int cur_chan; /* useless channel marker for interrupt */
222 /* number of bytes that have been received for current scan */
223 unsigned int scan_progress;
224 /* keeps track of where we are in chanlist as for munging */
225 unsigned int munge_chan;
226 /* number of bytes that have been munged */
227 unsigned int munge_count;
228 /* buffer marker for munging */
229 unsigned int munge_ptr;
231 unsigned int events; /* events that have occurred */
235 wait_queue_head_t wait_head;
238 unsigned int cb_mask;
239 int (*cb_func) (unsigned int flags, void *);
242 int (*inttrig) (comedi_device * dev, comedi_subdevice * s,
246 struct comedi_driver_struct {
247 struct comedi_driver_struct *next;
249 const char *driver_name;
250 struct module *module;
251 int (*attach) (comedi_device *, comedi_devconfig *);
252 int (*detach) (comedi_device *);
254 /* number of elements in board_name and board_id arrays */
255 unsigned int num_names;
256 const char *const *board_name;
257 /* offset in bytes from one board name pointer to the next */
261 struct comedi_device_struct {
263 comedi_driver *driver;
266 device_create_result_type *class_dev;
268 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers for subdevices
269 that have async_dma_dir set to something other than DMA_NONE */
270 struct device *hw_dev;
272 const char *board_name;
273 const void *board_ptr;
278 int in_request_module;
281 comedi_subdevice *subdevices;
284 unsigned long iobase;
287 comedi_subdevice *read_subdev;
288 comedi_subdevice *write_subdev;
290 struct fasync_struct *async_queue;
292 void (*open) (comedi_device * dev);
293 void (*close) (comedi_device * dev);
296 struct comedi_device_file_info {
297 comedi_device *device;
298 comedi_subdevice *read_subdevice;
299 comedi_subdevice *write_subdevice;
302 #ifdef CONFIG_COMEDI_DEBUG
303 extern int comedi_debug;
305 static const int comedi_debug = 0;
309 * function prototypes
312 void comedi_event(comedi_device * dev, comedi_subdevice * s);
313 void comedi_error(const comedi_device * dev, const char *s);
315 /* we can expand the number of bits used to encode devices/subdevices into
316 the minor number soon, after more distros support > 8 bit minor numbers
317 (like after Debian Etch gets released) */
318 enum comedi_minor_bits {
319 COMEDI_DEVICE_MINOR_MASK = 0xf,
320 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
322 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
323 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
325 struct comedi_device_file_info* comedi_get_device_file_info(unsigned minor);
327 static inline comedi_subdevice* comedi_get_read_subdevice(const struct comedi_device_file_info *info)
329 if(info->read_subdevice) return info->read_subdevice;
330 if(info->device == NULL) return NULL;
331 return info->device->read_subdev;
334 static inline comedi_subdevice* comedi_get_write_subdevice(const struct comedi_device_file_info *info)
336 if(info->write_subdevice) return info->write_subdevice;
337 if(info->device == NULL) return NULL;
338 return info->device->write_subdev;
341 void comedi_device_detach(comedi_device * dev);
342 int comedi_device_attach(comedi_device * dev, comedi_devconfig * it);
343 int comedi_driver_register(comedi_driver *);
344 int comedi_driver_unregister(comedi_driver *);
346 void init_polling(void);
347 void cleanup_polling(void);
348 void start_polling(comedi_device *);
349 void stop_polling(comedi_device *);
351 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s, unsigned long
354 #ifdef CONFIG_PROC_FS
355 void comedi_proc_init(void);
356 void comedi_proc_cleanup(void);
358 static inline void comedi_proc_init(void)
361 static inline void comedi_proc_cleanup(void)
366 /* subdevice runflags */
367 enum subdevice_runflags {
368 SRF_USER = 0x00000001,
370 /* indicates an COMEDI_CB_ERROR event has occurred since the last command was started */
371 SRF_ERROR = 0x00000004,
372 SRF_RUNNING = 0x08000000
376 various internal comedi functions
379 int do_rangeinfo_ioctl(comedi_device * dev, comedi_rangeinfo * arg);
380 int check_chanlist(comedi_subdevice * s, int n, unsigned int *chanlist);
381 void comedi_set_subdevice_runflags(comedi_subdevice * s, unsigned mask,
383 unsigned comedi_get_subdevice_runflags(comedi_subdevice * s);
384 int insn_inval(comedi_device * dev, comedi_subdevice * s,
385 comedi_insn * insn, lsampl_t * data);
389 #define RANGE(a,b) {(a)*1e6,(b)*1e6,0}
390 #define RANGE_ext(a,b) {(a)*1e6,(b)*1e6,RF_EXTERNAL}
391 #define RANGE_mA(a,b) {(a)*1e6,(b)*1e6,UNIT_mA}
392 #define RANGE_unitless(a,b) {(a)*1e6,(b)*1e6,0} /* XXX */
393 #define BIP_RANGE(a) {-(a)*1e6,(a)*1e6,0}
394 #define UNI_RANGE(a) {0,(a)*1e6,0}
396 extern const comedi_lrange range_bipolar10;
397 extern const comedi_lrange range_bipolar5;
398 extern const comedi_lrange range_bipolar2_5;
399 extern const comedi_lrange range_unipolar10;
400 extern const comedi_lrange range_unipolar5;
401 extern const comedi_lrange range_unknown;
403 #define range_digital range_unipolar5
406 #define GCC_ZERO_LENGTH_ARRAY
408 #define GCC_ZERO_LENGTH_ARRAY 0
411 struct comedi_lrange_struct {
413 comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
416 /* some silly little inline functions */
418 static inline int alloc_subdevices(comedi_device * dev,
419 unsigned int num_subdevices)
423 dev->n_subdevices = num_subdevices;
425 kcalloc(num_subdevices, sizeof(comedi_subdevice), GFP_KERNEL);
426 if (!dev->subdevices)
428 for (i = 0; i < num_subdevices; ++i) {
429 dev->subdevices[i].device = dev;
430 dev->subdevices[i].async_dma_dir = DMA_NONE;
431 spin_lock_init(&dev->subdevices[i].spin_lock);
432 dev->subdevices[i].minor = -1;
437 static inline int alloc_private(comedi_device * dev, int size)
439 dev->private = kzalloc(size, GFP_KERNEL);
445 static inline unsigned int bytes_per_sample(const comedi_subdevice * subd)
447 if (subd->subdev_flags & SDF_LSAMPL)
448 return sizeof(lsampl_t);
450 return sizeof(sampl_t);
453 /* must be used in attach to set dev->hw_dev if you wish to dma directly
454 into comedi's buffer */
455 static inline void comedi_set_hw_dev(comedi_device * dev, struct device *hw_dev)
458 put_device(dev->hw_dev);
460 dev->hw_dev = hw_dev;
462 dev->hw_dev = get_device(dev->hw_dev);
463 BUG_ON(dev->hw_dev == NULL);
467 int comedi_buf_put(comedi_async * async, sampl_t x);
468 int comedi_buf_get(comedi_async * async, sampl_t * x);
470 unsigned int comedi_buf_write_n_available(comedi_async * async);
471 unsigned int comedi_buf_write_alloc(comedi_async * async, unsigned int nbytes);
472 unsigned int comedi_buf_write_alloc_strict(comedi_async * async,
473 unsigned int nbytes);
474 unsigned comedi_buf_write_free(comedi_async * async, unsigned int nbytes);
475 unsigned comedi_buf_read_alloc(comedi_async * async, unsigned nbytes);
476 unsigned comedi_buf_read_free(comedi_async * async, unsigned int nbytes);
477 unsigned int comedi_buf_read_n_available(comedi_async * async);
478 void comedi_buf_memcpy_to(comedi_async * async, unsigned int offset,
479 const void *source, unsigned int num_bytes);
480 void comedi_buf_memcpy_from(comedi_async * async, unsigned int offset,
481 void *destination, unsigned int num_bytes);
482 static inline unsigned comedi_buf_write_n_allocated(comedi_async * async)
484 return async->buf_write_alloc_count - async->buf_write_count;
486 static inline unsigned comedi_buf_read_n_allocated(comedi_async * async)
488 return async->buf_read_alloc_count - async->buf_read_count;
491 void comedi_reset_async_buf(comedi_async * async);
493 static inline void *comedi_aux_data(int options[], int n)
495 unsigned long address;
496 unsigned long addressLow;
498 if (sizeof(int) >= sizeof(void *))
499 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
501 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
502 bit_shift = sizeof(int) * 8;
503 address <<= bit_shift;
504 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
505 addressLow &= (1UL << bit_shift) - 1;
506 address |= addressLow;
509 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
511 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
513 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
515 return (void *)address;
518 int comedi_alloc_board_minor(struct device *hardware_device);
519 void comedi_free_board_minor(unsigned minor);
520 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s);
521 void comedi_free_subdevice_minor(comedi_subdevice *s);
522 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name);
523 void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
525 //#ifdef CONFIG_COMEDI_RT
526 #include "comedi_rt.h"
529 #endif /* _COMEDIDEV_H */