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/delay.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>
39 #include <linux/dma-mapping.h>
40 #include <linux/uaccess.h>
42 #include <linux/timer.h>
43 #include <linux/pci.h>
47 #define DPRINTK(format, args...) do { \
49 pr_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, \
54 COMEDI_MINORVERSION, COMEDI_MICROVERSION)
55 #define COMEDI_RELEASE VERSION
58 * PCI Vendor IDs not in <linux/pci_ids.h>
60 #define PCI_VENDOR_ID_KOLTER 0x1001
61 #define PCI_VENDOR_ID_ICP 0x104c
62 #define PCI_VENDOR_ID_DT 0x1116
63 #define PCI_VENDOR_ID_IOTECH 0x1616
64 #define PCI_VENDOR_ID_CONTEC 0x1221
65 #define PCI_VENDOR_ID_CB 0x1307 /* Measurement Computing */
66 #define PCI_VENDOR_ID_ADVANTECH 0x13fe
67 #define PCI_VENDOR_ID_MEILHAUS 0x1402
68 #define PCI_VENDOR_ID_RTD 0x1435
69 #define PCI_VENDOR_ID_ADLINK 0x144a
71 #define COMEDI_NUM_MINORS 0x100
72 #define COMEDI_NUM_BOARD_MINORS 0x30
73 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
75 struct comedi_subdevice {
76 struct comedi_device *device;
80 int len_chanlist; /* maximum length of channel/gain list */
84 struct comedi_async *async;
93 unsigned int maxdata; /* if maxdata==0, use list */
94 const unsigned int *maxdata_list; /* list is channel specific */
97 const unsigned int *flaglist;
99 unsigned int settling_time_0;
101 const struct comedi_lrange *range_table;
102 const struct comedi_lrange *const *range_table_list;
104 unsigned int *chanlist; /* driver-owned chanlist (not used) */
106 int (*insn_read) (struct comedi_device *, struct comedi_subdevice *,
107 struct comedi_insn *, unsigned int *);
108 int (*insn_write) (struct comedi_device *, struct comedi_subdevice *,
109 struct comedi_insn *, unsigned int *);
110 int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *,
111 struct comedi_insn *, unsigned int *);
112 int (*insn_config) (struct comedi_device *, struct comedi_subdevice *,
113 struct comedi_insn *, unsigned int *);
115 int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
116 int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *,
117 struct comedi_cmd *);
118 int (*poll) (struct comedi_device *, struct comedi_subdevice *);
119 int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
120 /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
121 /* int (*do_unlock)(struct comedi_device *, \
122 struct comedi_subdevice *); */
124 /* called when the buffer changes */
125 int (*buf_change) (struct comedi_device *dev,
126 struct comedi_subdevice *s, unsigned long new_size);
128 void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s,
129 void *data, unsigned int num_bytes,
130 unsigned int start_chan_index);
131 enum dma_data_direction async_dma_dir;
135 struct device *class_dev;
139 struct comedi_buf_page {
144 struct comedi_async {
145 struct comedi_subdevice *subdevice;
147 void *prealloc_buf; /* pre-allocated buffer */
148 unsigned int prealloc_bufsz; /* buffer size, in bytes */
149 /* virtual and dma address of each page */
150 struct comedi_buf_page *buf_page_list;
151 unsigned n_buf_pages; /* num elements in buf_page_list */
153 unsigned int max_bufsize; /* maximum buffer size, bytes */
154 /* current number of mmaps of prealloc_buf */
155 unsigned int mmap_count;
157 /* byte count for writer (write completed) */
158 unsigned int buf_write_count;
159 /* byte count for writer (allocated for writing) */
160 unsigned int buf_write_alloc_count;
161 /* byte count for reader (read completed) */
162 unsigned int buf_read_count;
163 /* byte count for reader (allocated for reading) */
164 unsigned int buf_read_alloc_count;
166 unsigned int buf_write_ptr; /* buffer marker for writer */
167 unsigned int buf_read_ptr; /* buffer marker for reader */
169 unsigned int cur_chan; /* useless channel marker for interrupt */
170 /* number of bytes that have been received for current scan */
171 unsigned int scan_progress;
172 /* keeps track of where we are in chanlist as for munging */
173 unsigned int munge_chan;
174 /* number of bytes that have been munged */
175 unsigned int munge_count;
176 /* buffer marker for munging */
177 unsigned int munge_ptr;
179 unsigned int events; /* events that have occurred */
181 struct comedi_cmd cmd;
183 wait_queue_head_t wait_head;
186 unsigned int cb_mask;
187 int (*cb_func) (unsigned int flags, void *);
190 int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
194 struct usb_interface;
196 struct comedi_driver {
197 struct comedi_driver *next;
199 const char *driver_name;
200 struct module *module;
201 int (*attach) (struct comedi_device *, struct comedi_devconfig *);
202 void (*detach) (struct comedi_device *);
203 int (*attach_pci) (struct comedi_device *, struct pci_dev *);
204 int (*attach_usb) (struct comedi_device *, struct usb_interface *);
206 /* number of elements in board_name and board_id arrays */
207 unsigned int num_names;
208 const char *const *board_name;
209 /* offset in bytes from one board name pointer to the next */
213 struct comedi_device {
215 struct comedi_driver *driver;
218 struct device *class_dev;
220 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
221 * for subdevices that have async_dma_dir set to something other than
223 struct device *hw_dev;
225 const char *board_name;
226 const void *board_ptr;
230 int in_request_module;
233 struct comedi_subdevice *subdevices;
236 unsigned long iobase;
239 struct comedi_subdevice *read_subdev;
240 struct comedi_subdevice *write_subdev;
242 struct fasync_struct *async_queue;
244 int (*open) (struct comedi_device *dev);
245 void (*close) (struct comedi_device *dev);
248 static inline const void *comedi_board(struct comedi_device *dev)
250 return dev->board_ptr;
253 struct comedi_device_file_info {
254 struct comedi_device *device;
255 struct comedi_subdevice *read_subdevice;
256 struct comedi_subdevice *write_subdevice;
257 struct device *hardware_device;
260 #ifdef CONFIG_COMEDI_DEBUG
261 extern int comedi_debug;
263 static const int comedi_debug;
267 * function prototypes
270 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
271 void comedi_error(const struct comedi_device *dev, const char *s);
273 /* we can expand the number of bits used to encode devices/subdevices into
274 the minor number soon, after more distros support > 8 bit minor numbers
275 (like after Debian Etch gets released) */
276 enum comedi_minor_bits {
277 COMEDI_DEVICE_MINOR_MASK = 0xf,
278 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
280 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
281 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
283 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor);
285 static inline struct comedi_subdevice *comedi_get_read_subdevice(
286 const struct comedi_device_file_info *info)
288 if (info->read_subdevice)
289 return info->read_subdevice;
290 if (info->device == NULL)
292 return info->device->read_subdev;
295 static inline struct comedi_subdevice *comedi_get_write_subdevice(
296 const struct comedi_device_file_info *info)
298 if (info->write_subdevice)
299 return info->write_subdevice;
300 if (info->device == NULL)
302 return info->device->write_subdev;
305 int comedi_alloc_subdevices(struct comedi_device *, int);
307 void comedi_device_detach(struct comedi_device *dev);
308 int comedi_device_attach(struct comedi_device *dev,
309 struct comedi_devconfig *it);
310 int comedi_driver_register(struct comedi_driver *);
311 int comedi_driver_unregister(struct comedi_driver *);
314 * module_comedi_driver() - Helper macro for registering a comedi driver
315 * @__comedi_driver: comedi_driver struct
317 * Helper macro for comedi drivers which do not do anything special in module
318 * init/exit. This eliminates a lot of boilerplate. Each module may only use
319 * this macro once, and calling it replaces module_init() and module_exit().
321 #define module_comedi_driver(__comedi_driver) \
322 module_driver(__comedi_driver, comedi_driver_register, \
323 comedi_driver_unregister)
325 int comedi_pci_enable(struct pci_dev *, const char *);
326 void comedi_pci_disable(struct pci_dev *);
328 int comedi_pci_driver_register(struct comedi_driver *, struct pci_driver *);
329 void comedi_pci_driver_unregister(struct comedi_driver *, struct pci_driver *);
332 * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
333 * @__comedi_driver: comedi_driver struct
334 * @__pci_driver: pci_driver struct
336 * Helper macro for comedi PCI drivers which do not do anything special
337 * in module init/exit. This eliminates a lot of boilerplate. Each
338 * module may only use this macro once, and calling it replaces
339 * module_init() and module_exit()
341 #define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
342 module_driver(__comedi_driver, comedi_pci_driver_register, \
343 comedi_pci_driver_unregister, &(__pci_driver))
347 int comedi_usb_driver_register(struct comedi_driver *, struct usb_driver *);
348 void comedi_usb_driver_unregister(struct comedi_driver *, struct usb_driver *);
351 * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
352 * @__comedi_driver: comedi_driver struct
353 * @__usb_driver: usb_driver struct
355 * Helper macro for comedi USB drivers which do not do anything special
356 * in module init/exit. This eliminates a lot of boilerplate. Each
357 * module may only use this macro once, and calling it replaces
358 * module_init() and module_exit()
360 #define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
361 module_driver(__comedi_driver, comedi_usb_driver_register, \
362 comedi_usb_driver_unregister, &(__usb_driver))
364 void init_polling(void);
365 void cleanup_polling(void);
366 void start_polling(struct comedi_device *);
367 void stop_polling(struct comedi_device *);
369 #ifdef CONFIG_PROC_FS
370 void comedi_proc_init(void);
371 void comedi_proc_cleanup(void);
373 static inline void comedi_proc_init(void)
377 static inline void comedi_proc_cleanup(void)
382 /* subdevice runflags */
383 enum subdevice_runflags {
384 SRF_USER = 0x00000001,
386 /* indicates an COMEDI_CB_ERROR event has occurred since the last
387 * command was started */
388 SRF_ERROR = 0x00000004,
389 SRF_RUNNING = 0x08000000
392 int comedi_check_chanlist(struct comedi_subdevice *s,
394 unsigned int *chanlist);
395 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s);
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}
403 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
404 #define UNI_RANGE(a) {0, (a)*1e6, 0}
406 extern const struct comedi_lrange range_bipolar10;
407 extern const struct comedi_lrange range_bipolar5;
408 extern const struct comedi_lrange range_bipolar2_5;
409 extern const struct comedi_lrange range_unipolar10;
410 extern const struct comedi_lrange range_unipolar5;
411 extern const struct 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 {
423 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
426 /* some silly little inline functions */
428 static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
430 if (subd->subdev_flags & SDF_LSAMPL)
431 return sizeof(unsigned int);
433 return sizeof(short);
436 /* must be used in attach to set dev->hw_dev if you wish to dma directly
437 into comedi's buffer */
438 static inline void comedi_set_hw_dev(struct comedi_device *dev,
439 struct device *hw_dev)
441 if (dev->hw_dev == hw_dev)
444 put_device(dev->hw_dev);
445 dev->hw_dev = hw_dev;
447 dev->hw_dev = get_device(dev->hw_dev);
448 BUG_ON(dev->hw_dev == NULL);
452 static inline struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
454 return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL;
457 int comedi_buf_put(struct comedi_async *async, short x);
458 int comedi_buf_get(struct comedi_async *async, short *x);
460 unsigned int comedi_buf_write_n_available(struct comedi_async *async);
461 unsigned int comedi_buf_write_alloc(struct comedi_async *async,
462 unsigned int nbytes);
463 unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
464 unsigned int nbytes);
465 unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes);
466 unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes);
467 unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes);
468 unsigned int comedi_buf_read_n_available(struct comedi_async *async);
469 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
470 const void *source, unsigned int num_bytes);
471 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
472 void *destination, unsigned int num_bytes);
473 static inline unsigned comedi_buf_write_n_allocated(struct comedi_async *async)
475 return async->buf_write_alloc_count - async->buf_write_count;
478 static inline unsigned comedi_buf_read_n_allocated(struct comedi_async *async)
480 return async->buf_read_alloc_count - async->buf_read_count;
483 static inline void *comedi_aux_data(int options[], int n)
485 unsigned long address;
486 unsigned long addressLow;
488 if (sizeof(int) >= sizeof(void *))
489 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
491 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
492 bit_shift = sizeof(int) * 8;
493 address <<= bit_shift;
494 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
495 addressLow &= (1UL << bit_shift) - 1;
496 address |= addressLow;
499 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
501 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
503 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
505 return (void *)address;
508 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
509 struct comedi_subdevice *s);
510 void comedi_free_subdevice_minor(struct comedi_subdevice *s);
511 int comedi_pci_auto_config(struct pci_dev *pcidev,
512 struct comedi_driver *driver);
513 void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
514 int comedi_usb_auto_config(struct usb_interface *intf,
515 struct comedi_driver *driver);
516 void comedi_usb_auto_unconfig(struct usb_interface *intf);
518 #endif /* _COMEDIDEV_H */