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_BOARD_MINORS 0x30
58 struct comedi_subdevice
{
59 struct comedi_device
*device
;
64 int len_chanlist
; /* maximum length of channel/gain list */
68 struct comedi_async
*async
;
77 unsigned int maxdata
; /* if maxdata==0, use list */
78 const unsigned int *maxdata_list
; /* list is channel specific */
81 const unsigned int *flaglist
;
83 unsigned int settling_time_0
;
85 const struct comedi_lrange
*range_table
;
86 const struct comedi_lrange
*const *range_table_list
;
88 unsigned int *chanlist
; /* driver-owned chanlist (not used) */
90 int (*insn_read
) (struct comedi_device
*, struct comedi_subdevice
*,
91 struct comedi_insn
*, unsigned int *);
92 int (*insn_write
) (struct comedi_device
*, struct comedi_subdevice
*,
93 struct comedi_insn
*, unsigned int *);
94 int (*insn_bits
) (struct comedi_device
*, struct comedi_subdevice
*,
95 struct comedi_insn
*, unsigned int *);
96 int (*insn_config
) (struct comedi_device
*, struct comedi_subdevice
*,
97 struct comedi_insn
*, unsigned int *);
99 int (*do_cmd
) (struct comedi_device
*, struct comedi_subdevice
*);
100 int (*do_cmdtest
) (struct comedi_device
*, struct comedi_subdevice
*,
101 struct comedi_cmd
*);
102 int (*poll
) (struct comedi_device
*, struct comedi_subdevice
*);
103 int (*cancel
) (struct comedi_device
*, struct comedi_subdevice
*);
104 /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
105 /* int (*do_unlock)(struct comedi_device *, \
106 struct comedi_subdevice *); */
108 /* called when the buffer changes */
109 int (*buf_change
) (struct comedi_device
*dev
,
110 struct comedi_subdevice
*s
, unsigned long new_size
);
112 void (*munge
) (struct comedi_device
*dev
, struct comedi_subdevice
*s
,
113 void *data
, unsigned int num_bytes
,
114 unsigned int start_chan_index
);
115 enum dma_data_direction async_dma_dir
;
119 struct device
*class_dev
;
123 struct comedi_buf_page
{
128 struct comedi_async
{
129 struct comedi_subdevice
*subdevice
;
131 void *prealloc_buf
; /* pre-allocated buffer */
132 unsigned int prealloc_bufsz
; /* buffer size, in bytes */
133 /* virtual and dma address of each page */
134 struct comedi_buf_page
*buf_page_list
;
135 unsigned n_buf_pages
; /* num elements in buf_page_list */
137 unsigned int max_bufsize
; /* maximum buffer size, bytes */
138 /* current number of mmaps of prealloc_buf */
139 unsigned int mmap_count
;
141 /* byte count for writer (write completed) */
142 unsigned int buf_write_count
;
143 /* byte count for writer (allocated for writing) */
144 unsigned int buf_write_alloc_count
;
145 /* byte count for reader (read completed) */
146 unsigned int buf_read_count
;
147 /* byte count for reader (allocated for reading) */
148 unsigned int buf_read_alloc_count
;
150 unsigned int buf_write_ptr
; /* buffer marker for writer */
151 unsigned int buf_read_ptr
; /* buffer marker for reader */
153 unsigned int cur_chan
; /* useless channel marker for interrupt */
154 /* number of bytes that have been received for current scan */
155 unsigned int scan_progress
;
156 /* keeps track of where we are in chanlist as for munging */
157 unsigned int munge_chan
;
158 /* number of bytes that have been munged */
159 unsigned int munge_count
;
160 /* buffer marker for munging */
161 unsigned int munge_ptr
;
163 unsigned int events
; /* events that have occurred */
165 struct comedi_cmd cmd
;
167 wait_queue_head_t wait_head
;
170 unsigned int cb_mask
;
171 int (*cb_func
) (unsigned int flags
, void *);
174 int (*inttrig
) (struct comedi_device
*dev
, struct comedi_subdevice
*s
,
178 struct comedi_driver
{
179 struct comedi_driver
*next
;
181 const char *driver_name
;
182 struct module
*module
;
183 int (*attach
) (struct comedi_device
*, struct comedi_devconfig
*);
184 void (*detach
) (struct comedi_device
*);
185 int (*auto_attach
) (struct comedi_device
*, unsigned long);
187 /* number of elements in board_name and board_id arrays */
188 unsigned int num_names
;
189 const char *const *board_name
;
190 /* offset in bytes from one board name pointer to the next */
194 struct comedi_device
{
196 struct comedi_driver
*driver
;
199 struct device
*class_dev
;
201 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
202 * for subdevices that have async_dma_dir set to something other than
204 struct device
*hw_dev
;
206 const char *board_name
;
207 const void *board_ptr
;
209 bool in_request_module
:1;
215 struct comedi_subdevice
*subdevices
;
218 unsigned long iobase
;
221 struct comedi_subdevice
*read_subdev
;
222 struct comedi_subdevice
*write_subdev
;
224 struct fasync_struct
*async_queue
;
226 int (*open
) (struct comedi_device
*dev
);
227 void (*close
) (struct comedi_device
*dev
);
230 static inline const void *comedi_board(const struct comedi_device
*dev
)
232 return dev
->board_ptr
;
235 #ifdef CONFIG_COMEDI_DEBUG
236 extern int comedi_debug
;
238 static const int comedi_debug
;
242 * function prototypes
245 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
246 void comedi_error(const struct comedi_device
*dev
, const char *s
);
248 /* we can expand the number of bits used to encode devices/subdevices into
249 the minor number soon, after more distros support > 8 bit minor numbers
250 (like after Debian Etch gets released) */
251 enum comedi_minor_bits
{
252 COMEDI_DEVICE_MINOR_MASK
= 0xf,
253 COMEDI_SUBDEVICE_MINOR_MASK
= 0xf0
255 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT
= 4;
256 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET
= 1;
258 struct comedi_device
*comedi_dev_from_minor(unsigned minor
);
260 void init_polling(void);
261 void cleanup_polling(void);
262 void start_polling(struct comedi_device
*);
263 void stop_polling(struct comedi_device
*);
265 /* subdevice runflags */
266 enum subdevice_runflags
{
267 SRF_USER
= 0x00000001,
269 /* indicates an COMEDI_CB_ERROR event has occurred since the last
270 * command was started */
271 SRF_ERROR
= 0x00000004,
272 SRF_RUNNING
= 0x08000000
275 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
);
277 int comedi_check_chanlist(struct comedi_subdevice
*s
,
279 unsigned int *chanlist
);
283 #define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
284 #define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
285 #define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
286 #define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
287 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
288 #define UNI_RANGE(a) {0, (a)*1e6, 0}
290 extern const struct comedi_lrange range_bipolar10
;
291 extern const struct comedi_lrange range_bipolar5
;
292 extern const struct comedi_lrange range_bipolar2_5
;
293 extern const struct comedi_lrange range_unipolar10
;
294 extern const struct comedi_lrange range_unipolar5
;
295 extern const struct comedi_lrange range_unipolar2_5
;
296 extern const struct comedi_lrange range_0_20mA
;
297 extern const struct comedi_lrange range_4_20mA
;
298 extern const struct comedi_lrange range_0_32mA
;
299 extern const struct comedi_lrange range_unknown
;
301 #define range_digital range_unipolar5
304 #define GCC_ZERO_LENGTH_ARRAY
306 #define GCC_ZERO_LENGTH_ARRAY 0
309 struct comedi_lrange
{
311 struct comedi_krange range
[GCC_ZERO_LENGTH_ARRAY
];
314 /* some silly little inline functions */
316 static inline unsigned int bytes_per_sample(const struct comedi_subdevice
*subd
)
318 if (subd
->subdev_flags
& SDF_LSAMPL
)
319 return sizeof(unsigned int);
321 return sizeof(short);
325 * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
326 * Also useful for retrieving a previously configured hardware device of
327 * known bus type. Set automatically for auto-configured devices.
328 * Automatically set to NULL when detaching hardware device.
330 int comedi_set_hw_dev(struct comedi_device
*dev
, struct device
*hw_dev
);
332 unsigned int comedi_buf_write_alloc(struct comedi_async
*, unsigned int);
333 unsigned int comedi_buf_write_free(struct comedi_async
*, unsigned int);
335 unsigned int comedi_buf_read_n_available(struct comedi_async
*);
336 unsigned int comedi_buf_read_alloc(struct comedi_async
*, unsigned int);
337 unsigned int comedi_buf_read_free(struct comedi_async
*, unsigned int);
339 int comedi_buf_put(struct comedi_async
*, short);
340 int comedi_buf_get(struct comedi_async
*, short *);
342 void comedi_buf_memcpy_to(struct comedi_async
*async
, unsigned int offset
,
343 const void *source
, unsigned int num_bytes
);
344 void comedi_buf_memcpy_from(struct comedi_async
*async
, unsigned int offset
,
345 void *destination
, unsigned int num_bytes
);
347 /* drivers.c - general comedi driver functions */
349 int comedi_alloc_subdevices(struct comedi_device
*, int);
351 void comedi_spriv_free(struct comedi_device
*, int subdev_num
);
353 int __comedi_request_region(struct comedi_device
*,
354 unsigned long start
, unsigned long len
);
355 int comedi_request_region(struct comedi_device
*,
356 unsigned long start
, unsigned long len
);
358 int comedi_auto_config(struct device
*, struct comedi_driver
*,
359 unsigned long context
);
360 void comedi_auto_unconfig(struct device
*);
362 int comedi_driver_register(struct comedi_driver
*);
363 int comedi_driver_unregister(struct comedi_driver
*);
366 * module_comedi_driver() - Helper macro for registering a comedi driver
367 * @__comedi_driver: comedi_driver struct
369 * Helper macro for comedi drivers which do not do anything special in module
370 * init/exit. This eliminates a lot of boilerplate. Each module may only use
371 * this macro once, and calling it replaces module_init() and module_exit().
373 #define module_comedi_driver(__comedi_driver) \
374 module_driver(__comedi_driver, comedi_driver_register, \
375 comedi_driver_unregister)
377 #ifdef CONFIG_COMEDI_PCI_DRIVERS
379 /* comedi_pci.c - comedi PCI driver specific functions */
382 * PCI Vendor IDs not in <linux/pci_ids.h>
384 #define PCI_VENDOR_ID_KOLTER 0x1001
385 #define PCI_VENDOR_ID_ICP 0x104c
386 #define PCI_VENDOR_ID_AMCC 0x10e8
387 #define PCI_VENDOR_ID_DT 0x1116
388 #define PCI_VENDOR_ID_IOTECH 0x1616
389 #define PCI_VENDOR_ID_CONTEC 0x1221
390 #define PCI_VENDOR_ID_RTD 0x1435
395 struct pci_dev
*comedi_to_pci_dev(struct comedi_device
*);
397 int comedi_pci_enable(struct comedi_device
*);
398 void comedi_pci_disable(struct comedi_device
*);
400 int comedi_pci_auto_config(struct pci_dev
*, struct comedi_driver
*,
401 unsigned long context
);
402 void comedi_pci_auto_unconfig(struct pci_dev
*);
404 int comedi_pci_driver_register(struct comedi_driver
*, struct pci_driver
*);
405 void comedi_pci_driver_unregister(struct comedi_driver
*, struct pci_driver
*);
408 * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
409 * @__comedi_driver: comedi_driver struct
410 * @__pci_driver: pci_driver struct
412 * Helper macro for comedi PCI drivers which do not do anything special
413 * in module init/exit. This eliminates a lot of boilerplate. Each
414 * module may only use this macro once, and calling it replaces
415 * module_init() and module_exit()
417 #define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
418 module_driver(__comedi_driver, comedi_pci_driver_register, \
419 comedi_pci_driver_unregister, &(__pci_driver))
424 * Some of the comedi mixed ISA/PCI drivers call the PCI specific
425 * functions. Provide some dummy functions if CONFIG_COMEDI_PCI_DRIVERS
429 static inline struct pci_dev
*comedi_to_pci_dev(struct comedi_device
*dev
)
434 static inline int comedi_pci_enable(struct comedi_device
*dev
)
439 static inline void comedi_pci_disable(struct comedi_device
*dev
)
443 #endif /* CONFIG_COMEDI_PCI_DRIVERS */
445 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
447 /* comedi_pcmcia.c - comedi PCMCIA driver specific functions */
449 struct pcmcia_driver
;
450 struct pcmcia_device
;
452 struct pcmcia_device
*comedi_to_pcmcia_dev(struct comedi_device
*);
454 int comedi_pcmcia_enable(struct comedi_device
*,
455 int (*conf_check
)(struct pcmcia_device
*, void *));
456 void comedi_pcmcia_disable(struct comedi_device
*);
458 int comedi_pcmcia_auto_config(struct pcmcia_device
*, struct comedi_driver
*);
459 void comedi_pcmcia_auto_unconfig(struct pcmcia_device
*);
461 int comedi_pcmcia_driver_register(struct comedi_driver
*,
462 struct pcmcia_driver
*);
463 void comedi_pcmcia_driver_unregister(struct comedi_driver
*,
464 struct pcmcia_driver
*);
467 * module_comedi_pcmcia_driver() - Helper macro for registering a comedi PCMCIA driver
468 * @__comedi_driver: comedi_driver struct
469 * @__pcmcia_driver: pcmcia_driver struct
471 * Helper macro for comedi PCMCIA drivers which do not do anything special
472 * in module init/exit. This eliminates a lot of boilerplate. Each
473 * module may only use this macro once, and calling it replaces
474 * module_init() and module_exit()
476 #define module_comedi_pcmcia_driver(__comedi_driver, __pcmcia_driver) \
477 module_driver(__comedi_driver, comedi_pcmcia_driver_register, \
478 comedi_pcmcia_driver_unregister, &(__pcmcia_driver))
480 #endif /* CONFIG_COMEDI_PCMCIA_DRIVERS */
482 #ifdef CONFIG_COMEDI_USB_DRIVERS
484 /* comedi_usb.c - comedi USB driver specific functions */
487 struct usb_interface
;
489 struct usb_interface
*comedi_to_usb_interface(struct comedi_device
*);
491 int comedi_usb_auto_config(struct usb_interface
*, struct comedi_driver
*,
492 unsigned long context
);
493 void comedi_usb_auto_unconfig(struct usb_interface
*);
495 int comedi_usb_driver_register(struct comedi_driver
*, struct usb_driver
*);
496 void comedi_usb_driver_unregister(struct comedi_driver
*, struct usb_driver
*);
499 * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
500 * @__comedi_driver: comedi_driver struct
501 * @__usb_driver: usb_driver struct
503 * Helper macro for comedi USB drivers which do not do anything special
504 * in module init/exit. This eliminates a lot of boilerplate. Each
505 * module may only use this macro once, and calling it replaces
506 * module_init() and module_exit()
508 #define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
509 module_driver(__comedi_driver, comedi_usb_driver_register, \
510 comedi_usb_driver_unregister, &(__usb_driver))
512 #endif /* CONFIG_COMEDI_USB_DRIVERS */
514 #endif /* _COMEDIDEV_H */