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/kernel.h>
23 #include <linux/module.h>
24 #include <linux/kdev_t.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/spinlock.h>
29 #include <linux/mutex.h>
30 #include <linux/wait.h>
32 #include <linux/init.h>
33 #include <linux/vmalloc.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/uaccess.h>
37 #include <linux/timer.h>
41 #define DPRINTK(format, args...) do { \
43 pr_debug("comedi: " format, ## args); \
46 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
47 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \
48 COMEDI_MINORVERSION, COMEDI_MICROVERSION)
49 #define COMEDI_RELEASE VERSION
51 #define COMEDI_NUM_BOARD_MINORS 0x30
53 struct comedi_subdevice
{
54 struct comedi_device
*device
;
59 int len_chanlist
; /* maximum length of channel/gain list */
63 struct comedi_async
*async
;
72 unsigned int maxdata
; /* if maxdata==0, use list */
73 const unsigned int *maxdata_list
; /* list is channel specific */
76 const unsigned int *flaglist
;
78 unsigned int settling_time_0
;
80 const struct comedi_lrange
*range_table
;
81 const struct comedi_lrange
*const *range_table_list
;
83 unsigned int *chanlist
; /* driver-owned chanlist (not used) */
85 int (*insn_read
) (struct comedi_device
*, struct comedi_subdevice
*,
86 struct comedi_insn
*, unsigned int *);
87 int (*insn_write
) (struct comedi_device
*, struct comedi_subdevice
*,
88 struct comedi_insn
*, unsigned int *);
89 int (*insn_bits
) (struct comedi_device
*, struct comedi_subdevice
*,
90 struct comedi_insn
*, unsigned int *);
91 int (*insn_config
) (struct comedi_device
*, struct comedi_subdevice
*,
92 struct comedi_insn
*, unsigned int *);
94 int (*do_cmd
) (struct comedi_device
*, struct comedi_subdevice
*);
95 int (*do_cmdtest
) (struct comedi_device
*, struct comedi_subdevice
*,
97 int (*poll
) (struct comedi_device
*, struct comedi_subdevice
*);
98 int (*cancel
) (struct comedi_device
*, struct comedi_subdevice
*);
99 /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
100 /* int (*do_unlock)(struct comedi_device *, \
101 struct comedi_subdevice *); */
103 /* called when the buffer changes */
104 int (*buf_change
) (struct comedi_device
*dev
,
105 struct comedi_subdevice
*s
, unsigned long new_size
);
107 void (*munge
) (struct comedi_device
*dev
, struct comedi_subdevice
*s
,
108 void *data
, unsigned int num_bytes
,
109 unsigned int start_chan_index
);
110 enum dma_data_direction async_dma_dir
;
114 struct device
*class_dev
;
118 struct comedi_buf_page
{
123 struct comedi_async
{
124 struct comedi_subdevice
*subdevice
;
126 void *prealloc_buf
; /* pre-allocated buffer */
127 unsigned int prealloc_bufsz
; /* buffer size, in bytes */
128 /* virtual and dma address of each page */
129 struct comedi_buf_page
*buf_page_list
;
130 unsigned n_buf_pages
; /* num elements in buf_page_list */
132 unsigned int max_bufsize
; /* maximum buffer size, bytes */
133 /* current number of mmaps of prealloc_buf */
134 unsigned int mmap_count
;
136 /* byte count for writer (write completed) */
137 unsigned int buf_write_count
;
138 /* byte count for writer (allocated for writing) */
139 unsigned int buf_write_alloc_count
;
140 /* byte count for reader (read completed) */
141 unsigned int buf_read_count
;
142 /* byte count for reader (allocated for reading) */
143 unsigned int buf_read_alloc_count
;
145 unsigned int buf_write_ptr
; /* buffer marker for writer */
146 unsigned int buf_read_ptr
; /* buffer marker for reader */
148 unsigned int cur_chan
; /* useless channel marker for interrupt */
149 /* number of bytes that have been received for current scan */
150 unsigned int scan_progress
;
151 /* keeps track of where we are in chanlist as for munging */
152 unsigned int munge_chan
;
153 /* number of bytes that have been munged */
154 unsigned int munge_count
;
155 /* buffer marker for munging */
156 unsigned int munge_ptr
;
158 unsigned int events
; /* events that have occurred */
160 struct comedi_cmd cmd
;
162 wait_queue_head_t wait_head
;
165 unsigned int cb_mask
;
166 int (*cb_func
) (unsigned int flags
, void *);
169 int (*inttrig
) (struct comedi_device
*dev
, struct comedi_subdevice
*s
,
173 struct comedi_driver
{
174 struct comedi_driver
*next
;
176 const char *driver_name
;
177 struct module
*module
;
178 int (*attach
) (struct comedi_device
*, struct comedi_devconfig
*);
179 void (*detach
) (struct comedi_device
*);
180 int (*auto_attach
) (struct comedi_device
*, unsigned long);
182 /* number of elements in board_name and board_id arrays */
183 unsigned int num_names
;
184 const char *const *board_name
;
185 /* offset in bytes from one board name pointer to the next */
189 struct comedi_device
{
191 struct comedi_driver
*driver
;
194 struct device
*class_dev
;
196 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
197 * for subdevices that have async_dma_dir set to something other than
199 struct device
*hw_dev
;
201 const char *board_name
;
202 const void *board_ptr
;
204 bool in_request_module
:1;
210 struct comedi_subdevice
*subdevices
;
213 unsigned long iobase
;
217 struct comedi_subdevice
*read_subdev
;
218 struct comedi_subdevice
*write_subdev
;
220 struct fasync_struct
*async_queue
;
222 int (*open
) (struct comedi_device
*dev
);
223 void (*close
) (struct comedi_device
*dev
);
226 static inline const void *comedi_board(const struct comedi_device
*dev
)
228 return dev
->board_ptr
;
231 #ifdef CONFIG_COMEDI_DEBUG
232 extern int comedi_debug
;
234 static const int comedi_debug
;
238 * function prototypes
241 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
242 void comedi_error(const struct comedi_device
*dev
, const char *s
);
244 /* we can expand the number of bits used to encode devices/subdevices into
245 the minor number soon, after more distros support > 8 bit minor numbers
246 (like after Debian Etch gets released) */
247 enum comedi_minor_bits
{
248 COMEDI_DEVICE_MINOR_MASK
= 0xf,
249 COMEDI_SUBDEVICE_MINOR_MASK
= 0xf0
251 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT
= 4;
252 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET
= 1;
254 struct comedi_device
*comedi_dev_from_minor(unsigned minor
);
256 void init_polling(void);
257 void cleanup_polling(void);
258 void start_polling(struct comedi_device
*);
259 void stop_polling(struct comedi_device
*);
261 /* subdevice runflags */
262 enum subdevice_runflags
{
263 SRF_USER
= 0x00000001,
265 /* indicates an COMEDI_CB_ERROR event has occurred since the last
266 * command was started */
267 SRF_ERROR
= 0x00000004,
268 SRF_RUNNING
= 0x08000000
271 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
);
273 int comedi_check_chanlist(struct comedi_subdevice
*s
,
275 unsigned int *chanlist
);
279 #define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
280 #define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
281 #define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
282 #define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
283 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
284 #define UNI_RANGE(a) {0, (a)*1e6, 0}
286 extern const struct comedi_lrange range_bipolar10
;
287 extern const struct comedi_lrange range_bipolar5
;
288 extern const struct comedi_lrange range_bipolar2_5
;
289 extern const struct comedi_lrange range_unipolar10
;
290 extern const struct comedi_lrange range_unipolar5
;
291 extern const struct comedi_lrange range_unipolar2_5
;
292 extern const struct comedi_lrange range_0_20mA
;
293 extern const struct comedi_lrange range_4_20mA
;
294 extern const struct comedi_lrange range_0_32mA
;
295 extern const struct comedi_lrange range_unknown
;
297 #define range_digital range_unipolar5
300 #define GCC_ZERO_LENGTH_ARRAY
302 #define GCC_ZERO_LENGTH_ARRAY 0
305 struct comedi_lrange
{
307 struct comedi_krange range
[GCC_ZERO_LENGTH_ARRAY
];
310 /* some silly little inline functions */
312 static inline unsigned int bytes_per_sample(const struct comedi_subdevice
*subd
)
314 if (subd
->subdev_flags
& SDF_LSAMPL
)
315 return sizeof(unsigned int);
317 return sizeof(short);
321 * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
322 * Also useful for retrieving a previously configured hardware device of
323 * known bus type. Set automatically for auto-configured devices.
324 * Automatically set to NULL when detaching hardware device.
326 int comedi_set_hw_dev(struct comedi_device
*dev
, struct device
*hw_dev
);
328 unsigned int comedi_buf_write_alloc(struct comedi_async
*, unsigned int);
329 unsigned int comedi_buf_write_free(struct comedi_async
*, unsigned int);
331 unsigned int comedi_buf_read_n_available(struct comedi_async
*);
332 unsigned int comedi_buf_read_alloc(struct comedi_async
*, unsigned int);
333 unsigned int comedi_buf_read_free(struct comedi_async
*, unsigned int);
335 int comedi_buf_put(struct comedi_async
*, short);
336 int comedi_buf_get(struct comedi_async
*, short *);
338 void comedi_buf_memcpy_to(struct comedi_async
*async
, unsigned int offset
,
339 const void *source
, unsigned int num_bytes
);
340 void comedi_buf_memcpy_from(struct comedi_async
*async
, unsigned int offset
,
341 void *destination
, unsigned int num_bytes
);
343 /* drivers.c - general comedi driver functions */
345 int comedi_alloc_subdevices(struct comedi_device
*, int);
347 void comedi_spriv_free(struct comedi_device
*, int subdev_num
);
349 int __comedi_request_region(struct comedi_device
*,
350 unsigned long start
, unsigned long len
);
351 int comedi_request_region(struct comedi_device
*,
352 unsigned long start
, unsigned long len
);
353 void comedi_legacy_detach(struct comedi_device
*);
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_RTD 0x1435
392 struct pci_dev
*comedi_to_pci_dev(struct comedi_device
*);
394 int comedi_pci_enable(struct comedi_device
*);
395 void comedi_pci_disable(struct comedi_device
*);
397 int comedi_pci_auto_config(struct pci_dev
*, struct comedi_driver
*,
398 unsigned long context
);
399 void comedi_pci_auto_unconfig(struct pci_dev
*);
401 int comedi_pci_driver_register(struct comedi_driver
*, struct pci_driver
*);
402 void comedi_pci_driver_unregister(struct comedi_driver
*, struct pci_driver
*);
405 * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
406 * @__comedi_driver: comedi_driver struct
407 * @__pci_driver: pci_driver struct
409 * Helper macro for comedi PCI drivers which do not do anything special
410 * in module init/exit. This eliminates a lot of boilerplate. Each
411 * module may only use this macro once, and calling it replaces
412 * module_init() and module_exit()
414 #define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
415 module_driver(__comedi_driver, comedi_pci_driver_register, \
416 comedi_pci_driver_unregister, &(__pci_driver))
421 * Some of the comedi mixed ISA/PCI drivers call the PCI specific
422 * functions. Provide some dummy functions if CONFIG_COMEDI_PCI_DRIVERS
426 static inline struct pci_dev
*comedi_to_pci_dev(struct comedi_device
*dev
)
431 static inline int comedi_pci_enable(struct comedi_device
*dev
)
436 static inline void comedi_pci_disable(struct comedi_device
*dev
)
440 #endif /* CONFIG_COMEDI_PCI_DRIVERS */
442 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
444 /* comedi_pcmcia.c - comedi PCMCIA driver specific functions */
446 struct pcmcia_driver
;
447 struct pcmcia_device
;
449 struct pcmcia_device
*comedi_to_pcmcia_dev(struct comedi_device
*);
451 int comedi_pcmcia_enable(struct comedi_device
*,
452 int (*conf_check
)(struct pcmcia_device
*, void *));
453 void comedi_pcmcia_disable(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 unsigned long context
);
490 void comedi_usb_auto_unconfig(struct usb_interface
*);
492 int comedi_usb_driver_register(struct comedi_driver
*, struct usb_driver
*);
493 void comedi_usb_driver_unregister(struct comedi_driver
*, struct usb_driver
*);
496 * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
497 * @__comedi_driver: comedi_driver struct
498 * @__usb_driver: usb_driver struct
500 * Helper macro for comedi USB drivers which do not do anything special
501 * in module init/exit. This eliminates a lot of boilerplate. Each
502 * module may only use this macro once, and calling it replaces
503 * module_init() and module_exit()
505 #define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
506 module_driver(__comedi_driver, comedi_usb_driver_register, \
507 comedi_usb_driver_unregister, &(__usb_driver))
509 #endif /* CONFIG_COMEDI_USB_DRIVERS */
511 #endif /* _COMEDIDEV_H */