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 "interrupt.h"
41 #include <linux/dma-mapping.h>
42 #include <linux/uaccess.h>
47 #define DPRINTK(format, args...) do { \
49 printk(KERN_DEBUG "comedi: " format , ## args); \
52 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
53 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, COMEDI_MINORVERSION, COMEDI_MICROVERSION)
54 #define COMEDI_RELEASE VERSION
56 #define COMEDI_INITCLEANUP_NOMODULE(x) \
57 static int __init x ## _init_module(void) \
58 {return comedi_driver_register(&(x));} \
59 static void __exit x ## _cleanup_module(void) \
60 {comedi_driver_unregister(&(x));} \
61 module_init(x ## _init_module); \
62 module_exit(x ## _cleanup_module); \
64 #define COMEDI_MODULE_MACROS \
65 MODULE_AUTHOR("Comedi http://www.comedi.org"); \
66 MODULE_DESCRIPTION("Comedi low-level driver"); \
67 MODULE_LICENSE("GPL"); \
69 #define COMEDI_INITCLEANUP(x) \
70 COMEDI_MODULE_MACROS \
71 COMEDI_INITCLEANUP_NOMODULE(x)
73 #define COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table) \
74 static int __devinit comedi_driver ## _pci_probe(struct pci_dev *dev, \
75 const struct pci_device_id *ent) \
77 return comedi_pci_auto_config(dev, comedi_driver.driver_name); \
79 static void __devexit comedi_driver ## _pci_remove(struct pci_dev *dev) \
81 comedi_pci_auto_unconfig(dev); \
83 static struct pci_driver comedi_driver ## _pci_driver = \
85 .id_table = pci_id_table, \
86 .probe = &comedi_driver ## _pci_probe, \
87 .remove = __devexit_p(&comedi_driver ## _pci_remove) \
89 static int __init comedi_driver ## _init_module(void) \
92 retval = comedi_driver_register(&comedi_driver); \
95 comedi_driver ## _pci_driver.name = (char *)comedi_driver.driver_name; \
96 return pci_register_driver(&comedi_driver ## _pci_driver); \
98 static void __exit comedi_driver ## _cleanup_module(void) \
100 pci_unregister_driver(&comedi_driver ## _pci_driver); \
101 comedi_driver_unregister(&comedi_driver); \
103 module_init(comedi_driver ## _init_module); \
104 module_exit(comedi_driver ## _cleanup_module);
106 #define COMEDI_PCI_INITCLEANUP(comedi_driver, pci_id_table) \
107 COMEDI_MODULE_MACROS \
108 COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table)
110 #define PCI_VENDOR_ID_INOVA 0x104c
111 #define PCI_VENDOR_ID_NATINST 0x1093
112 #define PCI_VENDOR_ID_DATX 0x1116
113 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
114 #define PCI_VENDOR_ID_ADVANTECH 0x13fe
115 #define PCI_VENDOR_ID_RTD 0x1435
116 #define PCI_VENDOR_ID_AMPLICON 0x14dc
117 #define PCI_VENDOR_ID_ADLINK 0x144a
118 #define PCI_VENDOR_ID_ICP 0x104c
119 #define PCI_VENDOR_ID_CONTEC 0x1221
120 #define PCI_VENDOR_ID_MEILHAUS 0x1402
122 #define COMEDI_NUM_MINORS 0x100
123 #define COMEDI_NUM_LEGACY_MINORS 0x10
124 #define COMEDI_NUM_BOARD_MINORS 0x30
125 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
127 typedef struct comedi_device_struct comedi_device;
128 typedef struct comedi_subdevice_struct comedi_subdevice;
129 typedef struct comedi_async_struct comedi_async;
130 typedef struct comedi_driver_struct comedi_driver;
131 typedef struct comedi_lrange_struct comedi_lrange;
133 typedef struct device device_create_result_type;
135 #define COMEDI_DEVICE_CREATE(cs, parent, devt, drvdata, device, fmt...) \
136 device_create(cs, ((parent) ? (parent) : (device)), devt, drvdata, fmt)
138 struct comedi_subdevice_struct {
139 comedi_device *device;
142 volatile int subdev_flags;
143 int len_chanlist; /* maximum length of channel/gain list */
152 spinlock_t spin_lock;
156 lsampl_t maxdata; /* if maxdata==0, use list */
157 const lsampl_t *maxdata_list; /* list is channel specific */
160 const unsigned int *flaglist;
162 unsigned int settling_time_0;
164 const comedi_lrange *range_table;
165 const comedi_lrange *const *range_table_list;
167 unsigned int *chanlist; /* driver-owned chanlist (not used) */
169 int (*insn_read) (comedi_device *, comedi_subdevice *, comedi_insn *,
171 int (*insn_write) (comedi_device *, comedi_subdevice *, comedi_insn *,
173 int (*insn_bits) (comedi_device *, comedi_subdevice *, comedi_insn *,
175 int (*insn_config) (comedi_device *, comedi_subdevice *, comedi_insn *,
178 int (*do_cmd) (comedi_device *, comedi_subdevice *);
179 int (*do_cmdtest) (comedi_device *, comedi_subdevice *, comedi_cmd *);
180 int (*poll) (comedi_device *, comedi_subdevice *);
181 int (*cancel) (comedi_device *, comedi_subdevice *);
182 /* int (*do_lock)(comedi_device *,comedi_subdevice *); */
183 /* int (*do_unlock)(comedi_device *,comedi_subdevice *); */
185 /* called when the buffer changes */
186 int (*buf_change) (comedi_device *dev, comedi_subdevice *s,
187 unsigned long new_size);
189 void (*munge) (comedi_device *dev, comedi_subdevice *s, void *data,
190 unsigned int num_bytes, unsigned int start_chan_index);
191 enum dma_data_direction async_dma_dir;
195 device_create_result_type *class_dev;
199 struct comedi_buf_page {
204 struct comedi_async_struct {
205 comedi_subdevice *subdevice;
207 void *prealloc_buf; /* pre-allocated buffer */
208 unsigned int prealloc_bufsz; /* buffer size, in bytes */
209 struct comedi_buf_page *buf_page_list; /* virtual and dma address of each page */
210 unsigned n_buf_pages; /* num elements in buf_page_list */
212 unsigned int max_bufsize; /* maximum buffer size, bytes */
213 unsigned int mmap_count; /* current number of mmaps of prealloc_buf */
215 unsigned int buf_write_count; /* byte count for writer (write completed) */
216 unsigned int buf_write_alloc_count; /* byte count for writer (allocated for writing) */
217 unsigned int buf_read_count; /* byte count for reader (read completed) */
218 unsigned int buf_read_alloc_count; /* byte count for reader (allocated for reading) */
220 unsigned int buf_write_ptr; /* buffer marker for writer */
221 unsigned int buf_read_ptr; /* buffer marker for reader */
223 unsigned int cur_chan; /* useless channel marker for interrupt */
224 /* number of bytes that have been received for current scan */
225 unsigned int scan_progress;
226 /* keeps track of where we are in chanlist as for munging */
227 unsigned int munge_chan;
228 /* number of bytes that have been munged */
229 unsigned int munge_count;
230 /* buffer marker for munging */
231 unsigned int munge_ptr;
233 unsigned int events; /* events that have occurred */
237 wait_queue_head_t wait_head;
240 unsigned int cb_mask;
241 int (*cb_func) (unsigned int flags, void *);
244 int (*inttrig) (comedi_device *dev, comedi_subdevice *s,
248 struct comedi_driver_struct {
249 struct comedi_driver_struct *next;
251 const char *driver_name;
252 struct module *module;
253 int (*attach) (comedi_device *, comedi_devconfig *);
254 int (*detach) (comedi_device *);
256 /* number of elements in board_name and board_id arrays */
257 unsigned int num_names;
258 const char *const *board_name;
259 /* offset in bytes from one board name pointer to the next */
263 struct comedi_device_struct {
265 comedi_driver *driver;
268 device_create_result_type *class_dev;
270 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
271 * for subdevices that have async_dma_dir set to something other than
273 struct device *hw_dev;
275 const char *board_name;
276 const void *board_ptr;
281 int in_request_module;
284 comedi_subdevice *subdevices;
287 unsigned long iobase;
290 comedi_subdevice *read_subdev;
291 comedi_subdevice *write_subdev;
293 struct fasync_struct *async_queue;
295 void (*open) (comedi_device *dev);
296 void (*close) (comedi_device *dev);
299 struct comedi_device_file_info {
300 comedi_device *device;
301 comedi_subdevice *read_subdevice;
302 comedi_subdevice *write_subdevice;
305 #ifdef CONFIG_COMEDI_DEBUG
306 extern int comedi_debug;
308 static const int comedi_debug;
312 * function prototypes
315 void comedi_event(comedi_device *dev, comedi_subdevice *s);
316 void comedi_error(const comedi_device *dev, const char *s);
318 /* we can expand the number of bits used to encode devices/subdevices into
319 the minor number soon, after more distros support > 8 bit minor numbers
320 (like after Debian Etch gets released) */
321 enum comedi_minor_bits {
322 COMEDI_DEVICE_MINOR_MASK = 0xf,
323 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
325 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
326 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
328 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor);
330 static inline comedi_subdevice *comedi_get_read_subdevice(
331 const struct comedi_device_file_info *info)
333 if (info->read_subdevice)
334 return info->read_subdevice;
335 if (info->device == NULL)
337 return info->device->read_subdev;
340 static inline comedi_subdevice *comedi_get_write_subdevice(
341 const struct comedi_device_file_info *info)
343 if (info->write_subdevice)
344 return info->write_subdevice;
345 if (info->device == NULL)
347 return info->device->write_subdev;
350 void comedi_device_detach(comedi_device *dev);
351 int comedi_device_attach(comedi_device *dev, comedi_devconfig *it);
352 int comedi_driver_register(comedi_driver *);
353 int comedi_driver_unregister(comedi_driver *);
355 void init_polling(void);
356 void cleanup_polling(void);
357 void start_polling(comedi_device *);
358 void stop_polling(comedi_device *);
360 int comedi_buf_alloc(comedi_device *dev, comedi_subdevice *s, unsigned long
363 #ifdef CONFIG_PROC_FS
364 void comedi_proc_init(void);
365 void comedi_proc_cleanup(void);
367 static inline void comedi_proc_init(void)
370 static inline void comedi_proc_cleanup(void)
375 /* subdevice runflags */
376 enum subdevice_runflags {
377 SRF_USER = 0x00000001,
379 /* indicates an COMEDI_CB_ERROR event has occurred since the last
380 * command was started */
381 SRF_ERROR = 0x00000004,
382 SRF_RUNNING = 0x08000000
386 various internal comedi functions
389 int do_rangeinfo_ioctl(comedi_device *dev, comedi_rangeinfo *arg);
390 int check_chanlist(comedi_subdevice *s, int n, unsigned int *chanlist);
391 void comedi_set_subdevice_runflags(comedi_subdevice *s, unsigned mask,
393 unsigned comedi_get_subdevice_runflags(comedi_subdevice *s);
394 int insn_inval(comedi_device *dev, comedi_subdevice *s,
395 comedi_insn *insn, lsampl_t *data);
399 #define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
400 #define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
401 #define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
402 #define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0} /* XXX */
403 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
404 #define UNI_RANGE(a) {0, (a)*1e6, 0}
406 extern const comedi_lrange range_bipolar10;
407 extern const comedi_lrange range_bipolar5;
408 extern const comedi_lrange range_bipolar2_5;
409 extern const comedi_lrange range_unipolar10;
410 extern const comedi_lrange range_unipolar5;
411 extern const comedi_lrange range_unknown;
413 #define range_digital range_unipolar5
416 #define GCC_ZERO_LENGTH_ARRAY
418 #define GCC_ZERO_LENGTH_ARRAY 0
421 struct comedi_lrange_struct {
423 comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
426 /* some silly little inline functions */
428 static inline int alloc_subdevices(comedi_device *dev,
429 unsigned int num_subdevices)
433 dev->n_subdevices = num_subdevices;
435 kcalloc(num_subdevices, sizeof(comedi_subdevice), GFP_KERNEL);
436 if (!dev->subdevices)
438 for (i = 0; i < num_subdevices; ++i) {
439 dev->subdevices[i].device = dev;
440 dev->subdevices[i].async_dma_dir = DMA_NONE;
441 spin_lock_init(&dev->subdevices[i].spin_lock);
442 dev->subdevices[i].minor = -1;
447 static inline int alloc_private(comedi_device *dev, int size)
449 dev->private = kzalloc(size, GFP_KERNEL);
455 static inline unsigned int bytes_per_sample(const comedi_subdevice *subd)
457 if (subd->subdev_flags & SDF_LSAMPL)
458 return sizeof(lsampl_t);
460 return sizeof(sampl_t);
463 /* must be used in attach to set dev->hw_dev if you wish to dma directly
464 into comedi's buffer */
465 static inline void comedi_set_hw_dev(comedi_device *dev, struct device *hw_dev)
468 put_device(dev->hw_dev);
470 dev->hw_dev = hw_dev;
472 dev->hw_dev = get_device(dev->hw_dev);
473 BUG_ON(dev->hw_dev == NULL);
477 int comedi_buf_put(comedi_async *async, sampl_t x);
478 int comedi_buf_get(comedi_async *async, sampl_t *x);
480 unsigned int comedi_buf_write_n_available(comedi_async *async);
481 unsigned int comedi_buf_write_alloc(comedi_async *async, unsigned int nbytes);
482 unsigned int comedi_buf_write_alloc_strict(comedi_async *async,
483 unsigned int nbytes);
484 unsigned comedi_buf_write_free(comedi_async *async, unsigned int nbytes);
485 unsigned comedi_buf_read_alloc(comedi_async *async, unsigned nbytes);
486 unsigned comedi_buf_read_free(comedi_async *async, unsigned int nbytes);
487 unsigned int comedi_buf_read_n_available(comedi_async *async);
488 void comedi_buf_memcpy_to(comedi_async *async, unsigned int offset,
489 const void *source, unsigned int num_bytes);
490 void comedi_buf_memcpy_from(comedi_async *async, unsigned int offset,
491 void *destination, unsigned int num_bytes);
492 static inline unsigned comedi_buf_write_n_allocated(comedi_async *async)
494 return async->buf_write_alloc_count - async->buf_write_count;
496 static inline unsigned comedi_buf_read_n_allocated(comedi_async *async)
498 return async->buf_read_alloc_count - async->buf_read_count;
501 void comedi_reset_async_buf(comedi_async *async);
503 static inline void *comedi_aux_data(int options[], int n)
505 unsigned long address;
506 unsigned long addressLow;
508 if (sizeof(int) >= sizeof(void *))
509 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
511 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
512 bit_shift = sizeof(int) * 8;
513 address <<= bit_shift;
514 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
515 addressLow &= (1UL << bit_shift) - 1;
516 address |= addressLow;
519 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
521 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
523 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
525 return (void *)address;
528 int comedi_alloc_board_minor(struct device *hardware_device);
529 void comedi_free_board_minor(unsigned minor);
530 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s);
531 void comedi_free_subdevice_minor(comedi_subdevice *s);
532 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name);
533 void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
535 #include "comedi_rt.h"
537 #endif /* _COMEDIDEV_H */