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.
22 #include <linux/dma-mapping.h>
23 #include <linux/mutex.h>
24 #include <linux/spinlock_types.h>
25 #include <linux/rwsem.h>
26 #include <linux/kref.h>
30 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
31 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \
32 COMEDI_MINORVERSION, COMEDI_MICROVERSION)
33 #define COMEDI_RELEASE VERSION
35 #define COMEDI_NUM_BOARD_MINORS 0x30
37 struct comedi_subdevice {
38 struct comedi_device *device;
43 int len_chanlist; /* maximum length of channel/gain list */
47 struct comedi_async *async;
56 unsigned int maxdata; /* if maxdata==0, use list */
57 const unsigned int *maxdata_list; /* list is channel specific */
59 const struct comedi_lrange *range_table;
60 const struct comedi_lrange *const *range_table_list;
62 unsigned int *chanlist; /* driver-owned chanlist (not used) */
64 int (*insn_read)(struct comedi_device *, struct comedi_subdevice *,
65 struct comedi_insn *, unsigned int *);
66 int (*insn_write)(struct comedi_device *, struct comedi_subdevice *,
67 struct comedi_insn *, unsigned int *);
68 int (*insn_bits)(struct comedi_device *, struct comedi_subdevice *,
69 struct comedi_insn *, unsigned int *);
70 int (*insn_config)(struct comedi_device *, struct comedi_subdevice *,
71 struct comedi_insn *, unsigned int *);
73 int (*do_cmd)(struct comedi_device *, struct comedi_subdevice *);
74 int (*do_cmdtest)(struct comedi_device *, struct comedi_subdevice *,
76 int (*poll)(struct comedi_device *, struct comedi_subdevice *);
77 int (*cancel)(struct comedi_device *, struct comedi_subdevice *);
79 /* called when the buffer changes */
80 int (*buf_change)(struct comedi_device *, struct comedi_subdevice *);
82 void (*munge)(struct comedi_device *dev, struct comedi_subdevice *s,
83 void *data, unsigned int num_bytes,
84 unsigned int start_chan_index);
85 enum dma_data_direction async_dma_dir;
89 struct device *class_dev;
92 unsigned int *readback;
95 struct comedi_buf_page {
100 struct comedi_buf_map {
101 struct device *dma_hw_dev;
102 struct comedi_buf_page *page_list;
103 unsigned int n_pages;
104 enum dma_data_direction dma_dir;
105 struct kref refcount;
109 * struct comedi_async - control data for asynchronous comedi commands
110 * @prealloc_buf: preallocated buffer
111 * @prealloc_bufsz: buffer size (in bytes)
112 * @buf_map: map of buffer pages
113 * @max_bufsize: maximum buffer size (in bytes)
114 * @buf_write_count: "write completed" count (in bytes, modulo 2**32)
115 * @buf_write_alloc_count: "allocated for writing" count (in bytes,
117 * @buf_read_count: "read completed" count (in bytes, modulo 2**32)
118 * @buf_read_alloc_count: "allocated for reading" count (in bytes,
120 * @buf_write_ptr: buffer position for writer
121 * @buf_read_ptr: buffer position for reader
122 * @cur_chan: current position in chanlist for scan (for those
123 * drivers that use it)
124 * @scan_progress: amount received or sent for current scan (in bytes)
125 * @munge_chan: current position in chanlist for "munging"
126 * @munge_count: "munge" count (in bytes, modulo 2**32)
127 * @munge_ptr: buffer position for "munging"
128 * @events: bit-vector of events that have occurred
129 * @cmd: details of comedi command in progress
130 * @wait_head: task wait queue for file reader or writer
131 * @cb_mask: bit-vector of events that should wake waiting tasks
132 * @inttrig: software trigger function for command, or NULL
134 * Note about the ..._count and ..._ptr members:
136 * Think of the _Count values being integers of unlimited size, indexing
137 * into a buffer of infinite length (though only an advancing portion
138 * of the buffer of fixed length prealloc_bufsz is accessible at any time).
141 * Buf_Read_Count <= Buf_Read_Alloc_Count <= Munge_Count <=
142 * Buf_Write_Count <= Buf_Write_Alloc_Count <=
143 * (Buf_Read_Count + prealloc_bufsz)
145 * (Those aren't the actual members, apart from prealloc_bufsz.) When
146 * the buffer is reset, those _Count values start at 0 and only increase
147 * in value, maintaining the above inequalities until the next time the
148 * buffer is reset. The buffer is divided into the following regions by
151 * [0, Buf_Read_Count):
152 * old region no longer accessible
153 * [Buf_Read_Count, Buf_Read_Alloc_Count):
154 * filled and munged region allocated for reading but not yet read
155 * [Buf_Read_Alloc_Count, Munge_Count):
156 * filled and munged region not yet allocated for reading
157 * [Munge_Count, Buf_Write_Count):
158 * filled region not yet munged
159 * [Buf_Write_Count, Buf_Write_Alloc_Count):
160 * unfilled region allocated for writing but not yet written
161 * [Buf_Write_Alloc_Count, Buf_Read_Count + prealloc_bufsz):
162 * unfilled region not yet allocated for writing
163 * [Buf_Read_Count + prealloc_bufsz, infinity):
164 * unfilled region not yet accessible
166 * Data needs to be written into the buffer before it can be read out,
167 * and may need to be converted (or "munged") between the two
168 * operations. Extra unfilled buffer space may need to allocated for
169 * writing (advancing Buf_Write_Alloc_Count) before new data is written.
170 * After writing new data, the newly filled space needs to be released
171 * (advancing Buf_Write_Count). This also results in the new data being
172 * "munged" (advancing Munge_Count). Before data is read out of the
173 * buffer, extra space may need to be allocated for reading (advancing
174 * Buf_Read_Alloc_Count). After the data has been read out, the space
175 * needs to be released (advancing Buf_Read_Count).
177 * The actual members, buf_read_count, buf_read_alloc_count,
178 * munge_count, buf_write_count, and buf_write_alloc_count take the
179 * value of the corresponding capitalized _Count values modulo 2^32
180 * (UINT_MAX+1). Subtracting a "higher" _count value from a "lower"
181 * _count value gives the same answer as subtracting a "higher" _Count
182 * value from a lower _Count value because prealloc_bufsz < UINT_MAX+1.
183 * The modulo operation is done implicitly.
185 * The buf_read_ptr, munge_ptr, and buf_write_ptr members take the value
186 * of the corresponding capitalized _Count values modulo prealloc_bufsz.
187 * These correspond to byte indices in the physical buffer. The modulo
188 * operation is done by subtracting prealloc_bufsz when the value
189 * exceeds prealloc_bufsz (assuming prealloc_bufsz plus the increment is
190 * less than or equal to UINT_MAX).
192 struct comedi_async {
194 unsigned int prealloc_bufsz;
195 struct comedi_buf_map *buf_map;
196 unsigned int max_bufsize;
197 unsigned int buf_write_count;
198 unsigned int buf_write_alloc_count;
199 unsigned int buf_read_count;
200 unsigned int buf_read_alloc_count;
201 unsigned int buf_write_ptr;
202 unsigned int buf_read_ptr;
203 unsigned int cur_chan;
204 unsigned int scan_progress;
205 unsigned int munge_chan;
206 unsigned int munge_count;
207 unsigned int munge_ptr;
209 struct comedi_cmd cmd;
210 wait_queue_head_t wait_head;
211 unsigned int cb_mask;
212 int (*inttrig)(struct comedi_device *dev, struct comedi_subdevice *s,
217 * comedi_async callback "events"
218 * @COMEDI_CB_EOS: end-of-scan
219 * @COMEDI_CB_EOA: end-of-acquisition/output
220 * @COMEDI_CB_BLOCK: data has arrived, wakes up read() / write()
221 * @COMEDI_CB_EOBUF: DEPRECATED: end of buffer
222 * @COMEDI_CB_ERROR: card error during acquisition
223 * @COMEDI_CB_OVERFLOW: buffer overflow/underflow
225 * @COMEDI_CB_ERROR_MASK: events that indicate an error has occurred
226 * @COMEDI_CB_CANCEL_MASK: events that will cancel an async command
228 #define COMEDI_CB_EOS (1 << 0)
229 #define COMEDI_CB_EOA (1 << 1)
230 #define COMEDI_CB_BLOCK (1 << 2)
231 #define COMEDI_CB_EOBUF (1 << 3)
232 #define COMEDI_CB_ERROR (1 << 4)
233 #define COMEDI_CB_OVERFLOW (1 << 5)
235 #define COMEDI_CB_ERROR_MASK (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)
236 #define COMEDI_CB_CANCEL_MASK (COMEDI_CB_EOA | COMEDI_CB_ERROR_MASK)
238 struct comedi_driver {
239 struct comedi_driver *next;
241 const char *driver_name;
242 struct module *module;
243 int (*attach)(struct comedi_device *, struct comedi_devconfig *);
244 void (*detach)(struct comedi_device *);
245 int (*auto_attach)(struct comedi_device *, unsigned long);
247 /* number of elements in board_name and board_id arrays */
248 unsigned int num_names;
249 const char *const *board_name;
250 /* offset in bytes from one board name pointer to the next */
254 struct comedi_device {
256 struct comedi_driver *driver;
259 struct device *class_dev;
261 unsigned int detach_count;
262 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
263 * for subdevices that have async_dma_dir set to something other than
265 struct device *hw_dev;
267 const char *board_name;
268 const void *board_ptr;
273 struct rw_semaphore attach_lock;
274 struct kref refcount;
277 struct comedi_subdevice *subdevices;
281 unsigned long iobase;
285 struct comedi_subdevice *read_subdev;
286 struct comedi_subdevice *write_subdev;
288 struct fasync_struct *async_queue;
290 int (*open)(struct comedi_device *dev);
291 void (*close)(struct comedi_device *dev);
295 * function prototypes
298 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
300 /* we can expand the number of bits used to encode devices/subdevices into
301 the minor number soon, after more distros support > 8 bit minor numbers
302 (like after Debian Etch gets released) */
303 enum comedi_minor_bits {
304 COMEDI_DEVICE_MINOR_MASK = 0xf,
305 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
308 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
309 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
311 struct comedi_device *comedi_dev_get_from_minor(unsigned minor);
312 int comedi_dev_put(struct comedi_device *dev);
314 void init_polling(void);
315 void cleanup_polling(void);
316 void start_polling(struct comedi_device *);
317 void stop_polling(struct comedi_device *);
319 /* subdevice runflags */
320 enum subdevice_runflags {
322 /* indicates an COMEDI_CB_ERROR event has occurred since the last
323 * command was started */
324 SRF_ERROR = 0x00000004,
325 SRF_RUNNING = 0x08000000,
326 SRF_FREE_SPRIV = 0x80000000, /* free s->private on detach */
329 bool comedi_is_subdevice_running(struct comedi_subdevice *s);
331 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size);
333 int comedi_check_chanlist(struct comedi_subdevice *s,
335 unsigned int *chanlist);
339 #define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
340 #define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
341 #define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
342 #define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
343 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
344 #define UNI_RANGE(a) {0, (a)*1e6, 0}
346 extern const struct comedi_lrange range_bipolar10;
347 extern const struct comedi_lrange range_bipolar5;
348 extern const struct comedi_lrange range_bipolar2_5;
349 extern const struct comedi_lrange range_unipolar10;
350 extern const struct comedi_lrange range_unipolar5;
351 extern const struct comedi_lrange range_unipolar2_5;
352 extern const struct comedi_lrange range_0_20mA;
353 extern const struct comedi_lrange range_4_20mA;
354 extern const struct comedi_lrange range_0_32mA;
355 extern const struct comedi_lrange range_unknown;
357 #define range_digital range_unipolar5
360 #define GCC_ZERO_LENGTH_ARRAY
362 #define GCC_ZERO_LENGTH_ARRAY 0
365 struct comedi_lrange {
367 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
370 static inline bool comedi_range_is_bipolar(struct comedi_subdevice *s,
373 return s->range_table->range[range].min < 0;
376 static inline bool comedi_range_is_unipolar(struct comedi_subdevice *s,
379 return s->range_table->range[range].min >= 0;
382 static inline bool comedi_range_is_external(struct comedi_subdevice *s,
385 return !!(s->range_table->range[range].flags & RF_EXTERNAL);
388 static inline bool comedi_chan_range_is_bipolar(struct comedi_subdevice *s,
392 return s->range_table_list[chan]->range[range].min < 0;
395 static inline bool comedi_chan_range_is_unipolar(struct comedi_subdevice *s,
399 return s->range_table_list[chan]->range[range].min >= 0;
402 static inline bool comedi_chan_range_is_external(struct comedi_subdevice *s,
406 return !!(s->range_table_list[chan]->range[range].flags & RF_EXTERNAL);
409 /* munge between offset binary and two's complement values */
410 static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s,
413 return val ^ s->maxdata ^ (s->maxdata >> 1);
416 static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
418 if (subd->subdev_flags & SDF_LSAMPL)
419 return sizeof(unsigned int);
421 return sizeof(short);
425 * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
426 * Also useful for retrieving a previously configured hardware device of
427 * known bus type. Set automatically for auto-configured devices.
428 * Automatically set to NULL when detaching hardware device.
430 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev);
432 static inline unsigned int comedi_buf_n_bytes_ready(struct comedi_subdevice *s)
434 return s->async->buf_write_count - s->async->buf_read_count;
437 unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s, unsigned int n);
438 unsigned int comedi_buf_write_free(struct comedi_subdevice *s, unsigned int n);
440 unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s);
441 unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s, unsigned int n);
442 unsigned int comedi_buf_read_free(struct comedi_subdevice *s, unsigned int n);
444 unsigned int comedi_write_array_to_buffer(struct comedi_subdevice *s,
446 unsigned int num_bytes);
447 unsigned int comedi_buf_write_samples(struct comedi_subdevice *s,
448 const void *data, unsigned int nsamples);
449 unsigned int comedi_buf_read_samples(struct comedi_subdevice *s,
450 void *data, unsigned int nsamples);
452 /* drivers.c - general comedi driver functions */
454 #define COMEDI_TIMEOUT_MS 1000
456 int comedi_timeout(struct comedi_device *, struct comedi_subdevice *,
457 struct comedi_insn *,
458 int (*cb)(struct comedi_device *, struct comedi_subdevice *,
459 struct comedi_insn *, unsigned long context),
460 unsigned long context);
462 unsigned int comedi_handle_events(struct comedi_device *dev,
463 struct comedi_subdevice *s);
465 int comedi_dio_insn_config(struct comedi_device *, struct comedi_subdevice *,
466 struct comedi_insn *, unsigned int *data,
468 unsigned int comedi_dio_update_state(struct comedi_subdevice *,
470 unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s);
471 void comedi_inc_scan_progress(struct comedi_subdevice *s,
472 unsigned int num_bytes);
474 void *comedi_alloc_devpriv(struct comedi_device *, size_t);
475 int comedi_alloc_subdevices(struct comedi_device *, int);
476 int comedi_alloc_subdev_readback(struct comedi_subdevice *);
478 int comedi_readback_insn_read(struct comedi_device *, struct comedi_subdevice *,
479 struct comedi_insn *, unsigned int *data);
481 int comedi_load_firmware(struct comedi_device *, struct device *,
483 int (*cb)(struct comedi_device *,
484 const u8 *data, size_t size,
485 unsigned long context),
486 unsigned long context);
488 int __comedi_request_region(struct comedi_device *,
489 unsigned long start, unsigned long len);
490 int comedi_request_region(struct comedi_device *,
491 unsigned long start, unsigned long len);
492 void comedi_legacy_detach(struct comedi_device *);
494 int comedi_auto_config(struct device *, struct comedi_driver *,
495 unsigned long context);
496 void comedi_auto_unconfig(struct device *);
498 int comedi_driver_register(struct comedi_driver *);
499 void comedi_driver_unregister(struct comedi_driver *);
502 * module_comedi_driver() - Helper macro for registering a comedi driver
503 * @__comedi_driver: comedi_driver struct
505 * Helper macro for comedi drivers which do not do anything special in module
506 * init/exit. This eliminates a lot of boilerplate. Each module may only use
507 * this macro once, and calling it replaces module_init() and module_exit().
509 #define module_comedi_driver(__comedi_driver) \
510 module_driver(__comedi_driver, comedi_driver_register, \
511 comedi_driver_unregister)
513 #ifdef CONFIG_COMEDI_PCI_DRIVERS
515 /* comedi_pci.c - comedi PCI driver specific functions */
518 * PCI Vendor IDs not in <linux/pci_ids.h>
520 #define PCI_VENDOR_ID_KOLTER 0x1001
521 #define PCI_VENDOR_ID_ICP 0x104c
522 #define PCI_VENDOR_ID_DT 0x1116
523 #define PCI_VENDOR_ID_IOTECH 0x1616
524 #define PCI_VENDOR_ID_CONTEC 0x1221
525 #define PCI_VENDOR_ID_RTD 0x1435
526 #define PCI_VENDOR_ID_HUMUSOFT 0x186c
531 struct pci_dev *comedi_to_pci_dev(struct comedi_device *);
533 int comedi_pci_enable(struct comedi_device *);
534 void comedi_pci_disable(struct comedi_device *);
535 void comedi_pci_detach(struct comedi_device *);
537 int comedi_pci_auto_config(struct pci_dev *, struct comedi_driver *,
538 unsigned long context);
539 void comedi_pci_auto_unconfig(struct pci_dev *);
541 int comedi_pci_driver_register(struct comedi_driver *, struct pci_driver *);
542 void comedi_pci_driver_unregister(struct comedi_driver *, struct pci_driver *);
545 * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
546 * @__comedi_driver: comedi_driver struct
547 * @__pci_driver: pci_driver struct
549 * Helper macro for comedi PCI drivers which do not do anything special
550 * in module init/exit. This eliminates a lot of boilerplate. Each
551 * module may only use this macro once, and calling it replaces
552 * module_init() and module_exit()
554 #define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
555 module_driver(__comedi_driver, comedi_pci_driver_register, \
556 comedi_pci_driver_unregister, &(__pci_driver))
561 * Some of the comedi mixed ISA/PCI drivers call the PCI specific
562 * functions. Provide some dummy functions if CONFIG_COMEDI_PCI_DRIVERS
566 static inline struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
571 static inline int comedi_pci_enable(struct comedi_device *dev)
576 static inline void comedi_pci_disable(struct comedi_device *dev)
580 static inline void comedi_pci_detach(struct comedi_device *dev)
584 #endif /* CONFIG_COMEDI_PCI_DRIVERS */
586 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
588 /* comedi_pcmcia.c - comedi PCMCIA driver specific functions */
590 struct pcmcia_driver;
591 struct pcmcia_device;
593 struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *);
595 int comedi_pcmcia_enable(struct comedi_device *,
596 int (*conf_check)(struct pcmcia_device *, void *));
597 void comedi_pcmcia_disable(struct comedi_device *);
599 int comedi_pcmcia_auto_config(struct pcmcia_device *, struct comedi_driver *);
600 void comedi_pcmcia_auto_unconfig(struct pcmcia_device *);
602 int comedi_pcmcia_driver_register(struct comedi_driver *,
603 struct pcmcia_driver *);
604 void comedi_pcmcia_driver_unregister(struct comedi_driver *,
605 struct pcmcia_driver *);
608 * module_comedi_pcmcia_driver() - Helper macro for registering a comedi PCMCIA driver
609 * @__comedi_driver: comedi_driver struct
610 * @__pcmcia_driver: pcmcia_driver struct
612 * Helper macro for comedi PCMCIA drivers which do not do anything special
613 * in module init/exit. This eliminates a lot of boilerplate. Each
614 * module may only use this macro once, and calling it replaces
615 * module_init() and module_exit()
617 #define module_comedi_pcmcia_driver(__comedi_driver, __pcmcia_driver) \
618 module_driver(__comedi_driver, comedi_pcmcia_driver_register, \
619 comedi_pcmcia_driver_unregister, &(__pcmcia_driver))
621 #endif /* CONFIG_COMEDI_PCMCIA_DRIVERS */
623 #ifdef CONFIG_COMEDI_USB_DRIVERS
625 /* comedi_usb.c - comedi USB driver specific functions */
628 struct usb_interface;
630 struct usb_interface *comedi_to_usb_interface(struct comedi_device *);
631 struct usb_device *comedi_to_usb_dev(struct comedi_device *);
633 int comedi_usb_auto_config(struct usb_interface *, struct comedi_driver *,
634 unsigned long context);
635 void comedi_usb_auto_unconfig(struct usb_interface *);
637 int comedi_usb_driver_register(struct comedi_driver *, struct usb_driver *);
638 void comedi_usb_driver_unregister(struct comedi_driver *, struct usb_driver *);
641 * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
642 * @__comedi_driver: comedi_driver struct
643 * @__usb_driver: usb_driver struct
645 * Helper macro for comedi USB drivers which do not do anything special
646 * in module init/exit. This eliminates a lot of boilerplate. Each
647 * module may only use this macro once, and calling it replaces
648 * module_init() and module_exit()
650 #define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
651 module_driver(__comedi_driver, comedi_usb_driver_register, \
652 comedi_usb_driver_unregister, &(__usb_driver))
654 #endif /* CONFIG_COMEDI_USB_DRIVERS */
656 #endif /* _COMEDIDEV_H */