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>
46 #define DPRINTK(format, args...) do { \
48 pr_debug("comedi: " format, ## args); \
51 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
52 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \
53 COMEDI_MINORVERSION, COMEDI_MICROVERSION)
54 #define COMEDI_RELEASE VERSION
56 #define COMEDI_NUM_MINORS 0x100
57 #define COMEDI_NUM_BOARD_MINORS 0x30
58 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
60 struct comedi_subdevice {
61 struct comedi_device *device;
66 int len_chanlist; /* maximum length of channel/gain list */
70 struct comedi_async *async;
79 unsigned int maxdata; /* if maxdata==0, use list */
80 const unsigned int *maxdata_list; /* list is channel specific */
83 const unsigned int *flaglist;
85 unsigned int settling_time_0;
87 const struct comedi_lrange *range_table;
88 const struct comedi_lrange *const *range_table_list;
90 unsigned int *chanlist; /* driver-owned chanlist (not used) */
92 int (*insn_read) (struct comedi_device *, struct comedi_subdevice *,
93 struct comedi_insn *, unsigned int *);
94 int (*insn_write) (struct comedi_device *, struct comedi_subdevice *,
95 struct comedi_insn *, unsigned int *);
96 int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *,
97 struct comedi_insn *, unsigned int *);
98 int (*insn_config) (struct comedi_device *, struct comedi_subdevice *,
99 struct comedi_insn *, unsigned int *);
101 int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
102 int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *,
103 struct comedi_cmd *);
104 int (*poll) (struct comedi_device *, struct comedi_subdevice *);
105 int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
106 /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
107 /* int (*do_unlock)(struct comedi_device *, \
108 struct comedi_subdevice *); */
110 /* called when the buffer changes */
111 int (*buf_change) (struct comedi_device *dev,
112 struct comedi_subdevice *s, unsigned long new_size);
114 void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s,
115 void *data, unsigned int num_bytes,
116 unsigned int start_chan_index);
117 enum dma_data_direction async_dma_dir;
121 struct device *class_dev;
125 struct comedi_buf_page {
130 struct comedi_async {
131 struct comedi_subdevice *subdevice;
133 void *prealloc_buf; /* pre-allocated buffer */
134 unsigned int prealloc_bufsz; /* buffer size, in bytes */
135 /* virtual and dma address of each page */
136 struct comedi_buf_page *buf_page_list;
137 unsigned n_buf_pages; /* num elements in buf_page_list */
139 unsigned int max_bufsize; /* maximum buffer size, bytes */
140 /* current number of mmaps of prealloc_buf */
141 unsigned int mmap_count;
143 /* byte count for writer (write completed) */
144 unsigned int buf_write_count;
145 /* byte count for writer (allocated for writing) */
146 unsigned int buf_write_alloc_count;
147 /* byte count for reader (read completed) */
148 unsigned int buf_read_count;
149 /* byte count for reader (allocated for reading) */
150 unsigned int buf_read_alloc_count;
152 unsigned int buf_write_ptr; /* buffer marker for writer */
153 unsigned int buf_read_ptr; /* buffer marker for reader */
155 unsigned int cur_chan; /* useless channel marker for interrupt */
156 /* number of bytes that have been received for current scan */
157 unsigned int scan_progress;
158 /* keeps track of where we are in chanlist as for munging */
159 unsigned int munge_chan;
160 /* number of bytes that have been munged */
161 unsigned int munge_count;
162 /* buffer marker for munging */
163 unsigned int munge_ptr;
165 unsigned int events; /* events that have occurred */
167 struct comedi_cmd cmd;
169 wait_queue_head_t wait_head;
172 unsigned int cb_mask;
173 int (*cb_func) (unsigned int flags, void *);
176 int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
180 struct comedi_driver {
181 struct comedi_driver *next;
183 const char *driver_name;
184 struct module *module;
185 int (*attach) (struct comedi_device *, struct comedi_devconfig *);
186 void (*detach) (struct comedi_device *);
187 int (*auto_attach) (struct comedi_device *, unsigned long);
189 /* number of elements in board_name and board_id arrays */
190 unsigned int num_names;
191 const char *const *board_name;
192 /* offset in bytes from one board name pointer to the next */
196 struct comedi_device {
198 struct comedi_driver *driver;
201 struct device *class_dev;
203 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
204 * for subdevices that have async_dma_dir set to something other than
206 struct device *hw_dev;
208 const char *board_name;
209 const void *board_ptr;
213 int in_request_module;
216 struct comedi_subdevice *subdevices;
219 unsigned long iobase;
222 struct comedi_subdevice *read_subdev;
223 struct comedi_subdevice *write_subdev;
225 struct fasync_struct *async_queue;
227 int (*open) (struct comedi_device *dev);
228 void (*close) (struct comedi_device *dev);
231 static inline const void *comedi_board(const struct comedi_device *dev)
233 return dev->board_ptr;
236 #ifdef CONFIG_COMEDI_DEBUG
237 extern int comedi_debug;
239 static const int comedi_debug;
243 * function prototypes
246 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
247 void comedi_error(const struct comedi_device *dev, const char *s);
249 /* we can expand the number of bits used to encode devices/subdevices into
250 the minor number soon, after more distros support > 8 bit minor numbers
251 (like after Debian Etch gets released) */
252 enum comedi_minor_bits {
253 COMEDI_DEVICE_MINOR_MASK = 0xf,
254 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
256 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
257 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
259 struct comedi_device *comedi_dev_from_minor(unsigned minor);
261 void init_polling(void);
262 void cleanup_polling(void);
263 void start_polling(struct comedi_device *);
264 void stop_polling(struct comedi_device *);
266 /* subdevice runflags */
267 enum subdevice_runflags {
268 SRF_USER = 0x00000001,
270 /* indicates an COMEDI_CB_ERROR event has occurred since the last
271 * command was started */
272 SRF_ERROR = 0x00000004,
273 SRF_RUNNING = 0x08000000
276 bool comedi_is_subdevice_running(struct comedi_subdevice *s);
278 int comedi_check_chanlist(struct comedi_subdevice *s,
280 unsigned int *chanlist);
284 #define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
285 #define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
286 #define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
287 #define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
288 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
289 #define UNI_RANGE(a) {0, (a)*1e6, 0}
291 extern const struct comedi_lrange range_bipolar10;
292 extern const struct comedi_lrange range_bipolar5;
293 extern const struct comedi_lrange range_bipolar2_5;
294 extern const struct comedi_lrange range_unipolar10;
295 extern const struct comedi_lrange range_unipolar5;
296 extern const struct comedi_lrange range_unknown;
298 #define range_digital range_unipolar5
301 #define GCC_ZERO_LENGTH_ARRAY
303 #define GCC_ZERO_LENGTH_ARRAY 0
306 struct comedi_lrange {
308 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
311 /* some silly little inline functions */
313 static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
315 if (subd->subdev_flags & SDF_LSAMPL)
316 return sizeof(unsigned int);
318 return sizeof(short);
322 * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
323 * Also useful for retrieving a previously configured hardware device of
324 * known bus type. Set automatically for auto-configured devices.
325 * Automatically set to NULL when detaching hardware device.
327 static inline void comedi_set_hw_dev(struct comedi_device *dev,
328 struct device *hw_dev)
330 struct device *old_hw_dev = dev->hw_dev;
332 dev->hw_dev = get_device(hw_dev);
333 put_device(old_hw_dev);
336 unsigned int comedi_buf_write_alloc(struct comedi_async *, unsigned int);
337 unsigned int comedi_buf_write_free(struct comedi_async *, unsigned int);
339 unsigned int comedi_buf_read_n_available(struct comedi_async *);
340 unsigned int comedi_buf_read_alloc(struct comedi_async *, unsigned int);
341 unsigned int comedi_buf_read_free(struct comedi_async *, unsigned int);
343 int comedi_buf_put(struct comedi_async *, short);
344 int comedi_buf_get(struct comedi_async *, short *);
346 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
347 const void *source, unsigned int num_bytes);
348 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
349 void *destination, unsigned int num_bytes);
351 /* drivers.c - general comedi driver functions */
353 int comedi_alloc_subdevices(struct comedi_device *, int);
355 int comedi_auto_config(struct device *, struct comedi_driver *,
356 unsigned long context);
357 void comedi_auto_unconfig(struct device *);
359 int comedi_driver_register(struct comedi_driver *);
360 int comedi_driver_unregister(struct comedi_driver *);
363 * module_comedi_driver() - Helper macro for registering a comedi driver
364 * @__comedi_driver: comedi_driver struct
366 * Helper macro for comedi drivers which do not do anything special in module
367 * init/exit. This eliminates a lot of boilerplate. Each module may only use
368 * this macro once, and calling it replaces module_init() and module_exit().
370 #define module_comedi_driver(__comedi_driver) \
371 module_driver(__comedi_driver, comedi_driver_register, \
372 comedi_driver_unregister)
374 #ifdef CONFIG_COMEDI_PCI_DRIVERS
376 /* comedi_pci.c - comedi PCI driver specific functions */
379 * PCI Vendor IDs not in <linux/pci_ids.h>
381 #define PCI_VENDOR_ID_KOLTER 0x1001
382 #define PCI_VENDOR_ID_ICP 0x104c
383 #define PCI_VENDOR_ID_AMCC 0x10e8
384 #define PCI_VENDOR_ID_DT 0x1116
385 #define PCI_VENDOR_ID_IOTECH 0x1616
386 #define PCI_VENDOR_ID_CONTEC 0x1221
387 #define PCI_VENDOR_ID_CB 0x1307 /* Measurement Computing */
388 #define PCI_VENDOR_ID_ADVANTECH 0x13fe
389 #define PCI_VENDOR_ID_MEILHAUS 0x1402
390 #define PCI_VENDOR_ID_RTD 0x1435
391 #define PCI_VENDOR_ID_ADLINK 0x144a
392 #define PCI_VENDOR_ID_AMPLICON 0x14dc
397 struct pci_dev *comedi_to_pci_dev(struct comedi_device *);
399 int comedi_pci_enable(struct pci_dev *, const char *);
400 void comedi_pci_disable(struct pci_dev *);
402 int comedi_pci_auto_config(struct pci_dev *, struct comedi_driver *);
403 void comedi_pci_auto_unconfig(struct pci_dev *);
405 int comedi_pci_driver_register(struct comedi_driver *, struct pci_driver *);
406 void comedi_pci_driver_unregister(struct comedi_driver *, struct pci_driver *);
409 * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
410 * @__comedi_driver: comedi_driver struct
411 * @__pci_driver: pci_driver struct
413 * Helper macro for comedi PCI drivers which do not do anything special
414 * in module init/exit. This eliminates a lot of boilerplate. Each
415 * module may only use this macro once, and calling it replaces
416 * module_init() and module_exit()
418 #define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
419 module_driver(__comedi_driver, comedi_pci_driver_register, \
420 comedi_pci_driver_unregister, &(__pci_driver))
425 * Some of the comedi mixed ISA/PCI drivers call the PCI specific
426 * functions. Provide some dummy functions if CONFIG_COMEDI_PCI_DRIVERS
430 static inline struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
435 static inline int comedi_pci_enable(struct pci_dev *dev, const char *name)
440 static inline void comedi_pci_disable(struct pci_dev *dev)
444 #endif /* CONFIG_COMEDI_PCI_DRIVERS */
446 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
448 /* comedi_pcmcia.c - comedi PCMCIA driver specific functions */
450 struct pcmcia_driver;
451 struct pcmcia_device;
453 struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *);
455 int comedi_pcmcia_auto_config(struct pcmcia_device *, struct comedi_driver *);
456 void comedi_pcmcia_auto_unconfig(struct pcmcia_device *);
458 int comedi_pcmcia_driver_register(struct comedi_driver *,
459 struct pcmcia_driver *);
460 void comedi_pcmcia_driver_unregister(struct comedi_driver *,
461 struct pcmcia_driver *);
464 * module_comedi_pcmcia_driver() - Helper macro for registering a comedi PCMCIA driver
465 * @__comedi_driver: comedi_driver struct
466 * @__pcmcia_driver: pcmcia_driver struct
468 * Helper macro for comedi PCMCIA drivers which do not do anything special
469 * in module init/exit. This eliminates a lot of boilerplate. Each
470 * module may only use this macro once, and calling it replaces
471 * module_init() and module_exit()
473 #define module_comedi_pcmcia_driver(__comedi_driver, __pcmcia_driver) \
474 module_driver(__comedi_driver, comedi_pcmcia_driver_register, \
475 comedi_pcmcia_driver_unregister, &(__pcmcia_driver))
477 #endif /* CONFIG_COMEDI_PCMCIA_DRIVERS */
479 #ifdef CONFIG_COMEDI_USB_DRIVERS
481 /* comedi_usb.c - comedi USB driver specific functions */
484 struct usb_interface;
486 struct usb_interface *comedi_to_usb_interface(struct comedi_device *);
488 int comedi_usb_auto_config(struct usb_interface *, struct comedi_driver *);
489 void comedi_usb_auto_unconfig(struct usb_interface *);
491 int comedi_usb_driver_register(struct comedi_driver *, struct usb_driver *);
492 void comedi_usb_driver_unregister(struct comedi_driver *, struct usb_driver *);
495 * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
496 * @__comedi_driver: comedi_driver struct
497 * @__usb_driver: usb_driver struct
499 * Helper macro for comedi USB drivers which do not do anything special
500 * in module init/exit. This eliminates a lot of boilerplate. Each
501 * module may only use this macro once, and calling it replaces
502 * module_init() and module_exit()
504 #define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
505 module_driver(__comedi_driver, comedi_usb_driver_register, \
506 comedi_usb_driver_unregister, &(__usb_driver))
508 #endif /* CONFIG_COMEDI_USB_DRIVERS */
510 #endif /* _COMEDIDEV_H */