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/kdev_t.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/spinlock.h>
33 #include <linux/mutex.h>
34 #include <linux/wait.h>
36 #include <linux/init.h>
37 #include <linux/vmalloc.h>
38 #include "interrupt.h"
39 #include <linux/dma-mapping.h>
40 #include <linux/uaccess.h>
45 #define DPRINTK(format, args...) do { \
47 printk(KERN_DEBUG "comedi: " format , ## args); \
50 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
51 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, COMEDI_MINORVERSION, COMEDI_MICROVERSION)
52 #define COMEDI_RELEASE VERSION
54 #define COMEDI_INITCLEANUP_NOMODULE(x) \
55 static int __init x ## _init_module(void) \
56 {return comedi_driver_register(&(x)); } \
57 static void __exit x ## _cleanup_module(void) \
58 {comedi_driver_unregister(&(x)); } \
59 module_init(x ## _init_module); \
60 module_exit(x ## _cleanup_module); \
62 #define COMEDI_MODULE_MACROS \
63 MODULE_AUTHOR("Comedi http://www.comedi.org"); \
64 MODULE_DESCRIPTION("Comedi low-level driver"); \
65 MODULE_LICENSE("GPL"); \
67 #define COMEDI_INITCLEANUP(x) \
68 COMEDI_MODULE_MACROS \
69 COMEDI_INITCLEANUP_NOMODULE(x)
71 #define COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table) \
72 static int __devinit comedi_driver ## _pci_probe(struct pci_dev *dev, \
73 const struct pci_device_id *ent) \
75 return comedi_pci_auto_config(dev, comedi_driver.driver_name); \
77 static void __devexit comedi_driver ## _pci_remove(struct pci_dev *dev) \
79 comedi_pci_auto_unconfig(dev); \
81 static struct pci_driver comedi_driver ## _pci_driver = \
83 .id_table = pci_id_table, \
84 .probe = &comedi_driver ## _pci_probe, \
85 .remove = __devexit_p(&comedi_driver ## _pci_remove) \
87 static int __init comedi_driver ## _init_module(void) \
90 retval = comedi_driver_register(&comedi_driver); \
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_BOARD_MINORS 0x30
122 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
124 typedef struct comedi_async_struct comedi_async;
125 typedef struct comedi_driver_struct comedi_driver;
126 typedef struct comedi_lrange_struct comedi_lrange;
128 typedef struct device device_create_result_type;
130 #define COMEDI_DEVICE_CREATE(cs, parent, devt, drvdata, device, fmt...) \
131 device_create(cs, ((parent) ? (parent) : (device)), devt, drvdata, fmt)
133 struct comedi_subdevice {
134 struct comedi_device *device;
137 volatile int subdev_flags;
138 int len_chanlist; /* maximum length of channel/gain list */
147 spinlock_t spin_lock;
151 unsigned int maxdata; /* if maxdata==0, use list */
152 const unsigned int *maxdata_list; /* list is channel specific */
155 const unsigned int *flaglist;
157 unsigned int settling_time_0;
159 const comedi_lrange *range_table;
160 const comedi_lrange *const *range_table_list;
162 unsigned int *chanlist; /* driver-owned chanlist (not used) */
164 int (*insn_read) (struct comedi_device *, struct comedi_subdevice *, comedi_insn *,
166 int (*insn_write) (struct comedi_device *, struct comedi_subdevice *, comedi_insn *,
168 int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *, comedi_insn *,
170 int (*insn_config) (struct comedi_device *, struct comedi_subdevice *, comedi_insn *,
173 int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
174 int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *, comedi_cmd *);
175 int (*poll) (struct comedi_device *, struct comedi_subdevice *);
176 int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
177 /* int (*do_lock)(struct comedi_device *,struct comedi_subdevice *); */
178 /* int (*do_unlock)(struct comedi_device *,struct comedi_subdevice *); */
180 /* called when the buffer changes */
181 int (*buf_change) (struct comedi_device *dev, struct comedi_subdevice *s,
182 unsigned long new_size);
184 void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s, void *data,
185 unsigned int num_bytes, unsigned int start_chan_index);
186 enum dma_data_direction async_dma_dir;
190 device_create_result_type *class_dev;
194 struct comedi_buf_page {
199 struct comedi_async_struct {
200 struct comedi_subdevice *subdevice;
202 void *prealloc_buf; /* pre-allocated buffer */
203 unsigned int prealloc_bufsz; /* buffer size, in bytes */
204 struct comedi_buf_page *buf_page_list; /* virtual and dma address of each page */
205 unsigned n_buf_pages; /* num elements in buf_page_list */
207 unsigned int max_bufsize; /* maximum buffer size, bytes */
208 unsigned int mmap_count; /* current number of mmaps of prealloc_buf */
210 unsigned int buf_write_count; /* byte count for writer (write completed) */
211 unsigned int buf_write_alloc_count; /* byte count for writer (allocated for writing) */
212 unsigned int buf_read_count; /* byte count for reader (read completed) */
213 unsigned int buf_read_alloc_count; /* byte count for reader (allocated for reading) */
215 unsigned int buf_write_ptr; /* buffer marker for writer */
216 unsigned int buf_read_ptr; /* buffer marker for reader */
218 unsigned int cur_chan; /* useless channel marker for interrupt */
219 /* number of bytes that have been received for current scan */
220 unsigned int scan_progress;
221 /* keeps track of where we are in chanlist as for munging */
222 unsigned int munge_chan;
223 /* number of bytes that have been munged */
224 unsigned int munge_count;
225 /* buffer marker for munging */
226 unsigned int munge_ptr;
228 unsigned int events; /* events that have occurred */
232 wait_queue_head_t wait_head;
235 unsigned int cb_mask;
236 int (*cb_func) (unsigned int flags, void *);
239 int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
243 struct comedi_driver_struct {
244 struct comedi_driver_struct *next;
246 const char *driver_name;
247 struct module *module;
248 int (*attach) (struct comedi_device *, comedi_devconfig *);
249 int (*detach) (struct comedi_device *);
251 /* number of elements in board_name and board_id arrays */
252 unsigned int num_names;
253 const char *const *board_name;
254 /* offset in bytes from one board name pointer to the next */
258 struct comedi_device {
260 comedi_driver *driver;
263 device_create_result_type *class_dev;
265 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
266 * for subdevices that have async_dma_dir set to something other than
268 struct device *hw_dev;
270 const char *board_name;
271 const void *board_ptr;
276 int in_request_module;
279 struct comedi_subdevice *subdevices;
282 unsigned long iobase;
285 struct comedi_subdevice *read_subdev;
286 struct comedi_subdevice *write_subdev;
288 struct fasync_struct *async_queue;
290 void (*open) (struct comedi_device *dev);
291 void (*close) (struct comedi_device *dev);
294 struct comedi_device_file_info {
295 struct comedi_device *device;
296 struct comedi_subdevice *read_subdevice;
297 struct comedi_subdevice *write_subdevice;
300 #ifdef CONFIG_COMEDI_DEBUG
301 extern int comedi_debug;
303 static const int comedi_debug;
307 * function prototypes
310 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
311 void comedi_error(const struct comedi_device *dev, const char *s);
313 /* we can expand the number of bits used to encode devices/subdevices into
314 the minor number soon, after more distros support > 8 bit minor numbers
315 (like after Debian Etch gets released) */
316 enum comedi_minor_bits {
317 COMEDI_DEVICE_MINOR_MASK = 0xf,
318 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
320 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
321 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
323 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor);
325 static inline struct comedi_subdevice *comedi_get_read_subdevice(
326 const struct comedi_device_file_info *info)
328 if (info->read_subdevice)
329 return info->read_subdevice;
330 if (info->device == NULL)
332 return info->device->read_subdev;
335 static inline struct comedi_subdevice *comedi_get_write_subdevice(
336 const struct comedi_device_file_info *info)
338 if (info->write_subdevice)
339 return info->write_subdevice;
340 if (info->device == NULL)
342 return info->device->write_subdev;
345 void comedi_device_detach(struct comedi_device *dev);
346 int comedi_device_attach(struct comedi_device *dev, comedi_devconfig *it);
347 int comedi_driver_register(comedi_driver *);
348 int comedi_driver_unregister(comedi_driver *);
350 void init_polling(void);
351 void cleanup_polling(void);
352 void start_polling(struct comedi_device *);
353 void stop_polling(struct comedi_device *);
355 int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s, unsigned long
358 #ifdef CONFIG_PROC_FS
359 void comedi_proc_init(void);
360 void comedi_proc_cleanup(void);
362 static inline void comedi_proc_init(void)
365 static inline void comedi_proc_cleanup(void)
370 /* subdevice runflags */
371 enum subdevice_runflags {
372 SRF_USER = 0x00000001,
374 /* indicates an COMEDI_CB_ERROR event has occurred since the last
375 * command was started */
376 SRF_ERROR = 0x00000004,
377 SRF_RUNNING = 0x08000000
381 various internal comedi functions
384 int do_rangeinfo_ioctl(struct comedi_device *dev, comedi_rangeinfo *arg);
385 int check_chanlist(struct comedi_subdevice *s, int n, unsigned int *chanlist);
386 void comedi_set_subdevice_runflags(struct comedi_subdevice *s, unsigned mask,
388 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s);
389 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
390 comedi_insn *insn, unsigned int *data);
394 #define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
395 #define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
396 #define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
397 #define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0} /* XXX */
398 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
399 #define UNI_RANGE(a) {0, (a)*1e6, 0}
401 extern const comedi_lrange range_bipolar10;
402 extern const comedi_lrange range_bipolar5;
403 extern const comedi_lrange range_bipolar2_5;
404 extern const comedi_lrange range_unipolar10;
405 extern const comedi_lrange range_unipolar5;
406 extern const comedi_lrange range_unknown;
408 #define range_digital range_unipolar5
411 #define GCC_ZERO_LENGTH_ARRAY
413 #define GCC_ZERO_LENGTH_ARRAY 0
416 struct comedi_lrange_struct {
418 comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
421 /* some silly little inline functions */
423 static inline int alloc_subdevices(struct comedi_device *dev,
424 unsigned int num_subdevices)
428 dev->n_subdevices = num_subdevices;
430 kcalloc(num_subdevices, sizeof(struct comedi_subdevice), GFP_KERNEL);
431 if (!dev->subdevices)
433 for (i = 0; i < num_subdevices; ++i) {
434 dev->subdevices[i].device = dev;
435 dev->subdevices[i].async_dma_dir = DMA_NONE;
436 spin_lock_init(&dev->subdevices[i].spin_lock);
437 dev->subdevices[i].minor = -1;
442 static inline int alloc_private(struct comedi_device *dev, int size)
444 dev->private = kzalloc(size, GFP_KERNEL);
450 static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
452 if (subd->subdev_flags & SDF_LSAMPL)
453 return sizeof(unsigned int);
455 return sizeof(short);
458 /* must be used in attach to set dev->hw_dev if you wish to dma directly
459 into comedi's buffer */
460 static inline void comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
463 put_device(dev->hw_dev);
465 dev->hw_dev = hw_dev;
467 dev->hw_dev = get_device(dev->hw_dev);
468 BUG_ON(dev->hw_dev == NULL);
472 int comedi_buf_put(comedi_async *async, short x);
473 int comedi_buf_get(comedi_async *async, short *x);
475 unsigned int comedi_buf_write_n_available(comedi_async *async);
476 unsigned int comedi_buf_write_alloc(comedi_async *async, unsigned int nbytes);
477 unsigned int comedi_buf_write_alloc_strict(comedi_async *async,
478 unsigned int nbytes);
479 unsigned comedi_buf_write_free(comedi_async *async, unsigned int nbytes);
480 unsigned comedi_buf_read_alloc(comedi_async *async, unsigned nbytes);
481 unsigned comedi_buf_read_free(comedi_async *async, unsigned int nbytes);
482 unsigned int comedi_buf_read_n_available(comedi_async *async);
483 void comedi_buf_memcpy_to(comedi_async *async, unsigned int offset,
484 const void *source, unsigned int num_bytes);
485 void comedi_buf_memcpy_from(comedi_async *async, unsigned int offset,
486 void *destination, unsigned int num_bytes);
487 static inline unsigned comedi_buf_write_n_allocated(comedi_async *async)
489 return async->buf_write_alloc_count - async->buf_write_count;
491 static inline unsigned comedi_buf_read_n_allocated(comedi_async *async)
493 return async->buf_read_alloc_count - async->buf_read_count;
496 void comedi_reset_async_buf(comedi_async *async);
498 static inline void *comedi_aux_data(int options[], int n)
500 unsigned long address;
501 unsigned long addressLow;
503 if (sizeof(int) >= sizeof(void *))
504 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
506 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
507 bit_shift = sizeof(int) * 8;
508 address <<= bit_shift;
509 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
510 addressLow &= (1UL << bit_shift) - 1;
511 address |= addressLow;
514 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
516 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
518 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
520 return (void *)address;
523 int comedi_alloc_board_minor(struct device *hardware_device);
524 void comedi_free_board_minor(unsigned minor);
525 int comedi_alloc_subdevice_minor(struct comedi_device *dev, struct comedi_subdevice *s);
526 void comedi_free_subdevice_minor(struct comedi_subdevice *s);
527 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name);
528 void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
529 struct usb_device; /* forward declaration */
530 int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name);
531 void comedi_usb_auto_unconfig(struct usb_device *usbdev);
533 #include "comedi_rt.h"
535 #endif /* _COMEDIDEV_H */