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 printk(KERN_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 PCI_VENDOR_ID_ADLINK 0x144a
57 #define PCI_VENDOR_ID_ICP 0x104c
58 #define PCI_VENDOR_ID_CONTEC 0x1221
60 #define COMEDI_NUM_MINORS 0x100
61 #define COMEDI_NUM_BOARD_MINORS 0x30
62 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
64 struct comedi_subdevice
{
65 struct comedi_device
*device
;
69 int len_chanlist
; /* maximum length of channel/gain list */
73 struct comedi_async
*async
;
82 unsigned int maxdata
; /* if maxdata==0, use list */
83 const unsigned int *maxdata_list
; /* list is channel specific */
86 const unsigned int *flaglist
;
88 unsigned int settling_time_0
;
90 const struct comedi_lrange
*range_table
;
91 const struct comedi_lrange
*const *range_table_list
;
93 unsigned int *chanlist
; /* driver-owned chanlist (not used) */
95 int (*insn_read
) (struct comedi_device
*, struct comedi_subdevice
*,
96 struct comedi_insn
*, unsigned int *);
97 int (*insn_write
) (struct comedi_device
*, struct comedi_subdevice
*,
98 struct comedi_insn
*, unsigned int *);
99 int (*insn_bits
) (struct comedi_device
*, struct comedi_subdevice
*,
100 struct comedi_insn
*, unsigned int *);
101 int (*insn_config
) (struct comedi_device
*, struct comedi_subdevice
*,
102 struct comedi_insn
*, unsigned int *);
104 int (*do_cmd
) (struct comedi_device
*, struct comedi_subdevice
*);
105 int (*do_cmdtest
) (struct comedi_device
*, struct comedi_subdevice
*,
106 struct comedi_cmd
*);
107 int (*poll
) (struct comedi_device
*, struct comedi_subdevice
*);
108 int (*cancel
) (struct comedi_device
*, struct comedi_subdevice
*);
109 /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
110 /* int (*do_unlock)(struct comedi_device *, \
111 struct comedi_subdevice *); */
113 /* called when the buffer changes */
114 int (*buf_change
) (struct comedi_device
*dev
,
115 struct comedi_subdevice
*s
, unsigned long new_size
);
117 void (*munge
) (struct comedi_device
*dev
, struct comedi_subdevice
*s
,
118 void *data
, unsigned int num_bytes
,
119 unsigned int start_chan_index
);
120 enum dma_data_direction async_dma_dir
;
124 struct device
*class_dev
;
128 struct comedi_buf_page
{
133 struct comedi_async
{
134 struct comedi_subdevice
*subdevice
;
136 void *prealloc_buf
; /* pre-allocated buffer */
137 unsigned int prealloc_bufsz
; /* buffer size, in bytes */
138 /* virtual and dma address of each page */
139 struct comedi_buf_page
*buf_page_list
;
140 unsigned n_buf_pages
; /* num elements in buf_page_list */
142 unsigned int max_bufsize
; /* maximum buffer size, bytes */
143 /* current number of mmaps of prealloc_buf */
144 unsigned int mmap_count
;
146 /* byte count for writer (write completed) */
147 unsigned int buf_write_count
;
148 /* byte count for writer (allocated for writing) */
149 unsigned int buf_write_alloc_count
;
150 /* byte count for reader (read completed) */
151 unsigned int buf_read_count
;
152 /* byte count for reader (allocated for reading) */
153 unsigned int buf_read_alloc_count
;
155 unsigned int buf_write_ptr
; /* buffer marker for writer */
156 unsigned int buf_read_ptr
; /* buffer marker for reader */
158 unsigned int cur_chan
; /* useless channel marker for interrupt */
159 /* number of bytes that have been received for current scan */
160 unsigned int scan_progress
;
161 /* keeps track of where we are in chanlist as for munging */
162 unsigned int munge_chan
;
163 /* number of bytes that have been munged */
164 unsigned int munge_count
;
165 /* buffer marker for munging */
166 unsigned int munge_ptr
;
168 unsigned int events
; /* events that have occurred */
170 struct comedi_cmd cmd
;
172 wait_queue_head_t wait_head
;
175 unsigned int cb_mask
;
176 int (*cb_func
) (unsigned int flags
, void *);
179 int (*inttrig
) (struct comedi_device
*dev
, struct comedi_subdevice
*s
,
184 struct usb_interface
;
186 struct comedi_driver
{
187 struct comedi_driver
*next
;
189 const char *driver_name
;
190 struct module
*module
;
191 int (*attach
) (struct comedi_device
*, struct comedi_devconfig
*);
192 int (*detach
) (struct comedi_device
*);
193 int (*attach_pci
) (struct comedi_device
*, struct pci_dev
*);
194 int (*attach_usb
) (struct comedi_device
*, struct usb_interface
*);
196 /* number of elements in board_name and board_id arrays */
197 unsigned int num_names
;
198 const char *const *board_name
;
199 /* offset in bytes from one board name pointer to the next */
203 struct comedi_device
{
205 struct comedi_driver
*driver
;
208 struct device
*class_dev
;
210 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
211 * for subdevices that have async_dma_dir set to something other than
213 struct device
*hw_dev
;
215 const char *board_name
;
216 const void *board_ptr
;
220 int in_request_module
;
223 struct comedi_subdevice
*subdevices
;
226 unsigned long iobase
;
229 struct comedi_subdevice
*read_subdev
;
230 struct comedi_subdevice
*write_subdev
;
232 struct fasync_struct
*async_queue
;
234 int (*open
) (struct comedi_device
*dev
);
235 void (*close
) (struct comedi_device
*dev
);
238 static inline const void *comedi_board(struct comedi_device
*dev
)
240 return dev
->board_ptr
;
243 struct comedi_device_file_info
{
244 struct comedi_device
*device
;
245 struct comedi_subdevice
*read_subdevice
;
246 struct comedi_subdevice
*write_subdevice
;
247 struct device
*hardware_device
;
250 #ifdef CONFIG_COMEDI_DEBUG
251 extern int comedi_debug
;
253 static const int comedi_debug
;
257 * function prototypes
260 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
261 void comedi_error(const struct comedi_device
*dev
, const char *s
);
263 /* we can expand the number of bits used to encode devices/subdevices into
264 the minor number soon, after more distros support > 8 bit minor numbers
265 (like after Debian Etch gets released) */
266 enum comedi_minor_bits
{
267 COMEDI_DEVICE_MINOR_MASK
= 0xf,
268 COMEDI_SUBDEVICE_MINOR_MASK
= 0xf0
270 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT
= 4;
271 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET
= 1;
273 struct comedi_device_file_info
*comedi_get_device_file_info(unsigned minor
);
275 static inline struct comedi_subdevice
*comedi_get_read_subdevice(
276 const struct comedi_device_file_info
*info
)
278 if (info
->read_subdevice
)
279 return info
->read_subdevice
;
280 if (info
->device
== NULL
)
282 return info
->device
->read_subdev
;
285 static inline struct comedi_subdevice
*comedi_get_write_subdevice(
286 const struct comedi_device_file_info
*info
)
288 if (info
->write_subdevice
)
289 return info
->write_subdevice
;
290 if (info
->device
== NULL
)
292 return info
->device
->write_subdev
;
295 void comedi_device_detach(struct comedi_device
*dev
);
296 int comedi_device_attach(struct comedi_device
*dev
,
297 struct comedi_devconfig
*it
);
298 int comedi_driver_register(struct comedi_driver
*);
299 int comedi_driver_unregister(struct comedi_driver
*);
302 * module_comedi_driver() - Helper macro for registering a comedi driver
303 * @__comedi_driver: comedi_driver struct
305 * Helper macro for comedi drivers which do not do anything special in module
306 * init/exit. This eliminates a lot of boilerplate. Each module may only use
307 * this macro once, and calling it replaces module_init() and module_exit().
309 #define module_comedi_driver(__comedi_driver) \
310 module_driver(__comedi_driver, comedi_driver_register, \
311 comedi_driver_unregister)
315 int comedi_pci_driver_register(struct comedi_driver
*, struct pci_driver
*);
316 void comedi_pci_driver_unregister(struct comedi_driver
*, struct pci_driver
*);
319 * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
320 * @__comedi_driver: comedi_driver struct
321 * @__pci_driver: pci_driver struct
323 * Helper macro for comedi PCI drivers which do not do anything special
324 * in module init/exit. This eliminates a lot of boilerplate. Each
325 * module may only use this macro once, and calling it replaces
326 * module_init() and module_exit()
328 #define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
329 module_driver(__comedi_driver, comedi_pci_driver_register, \
330 comedi_pci_driver_unregister, &(__pci_driver))
334 int comedi_usb_driver_register(struct comedi_driver
*, struct usb_driver
*);
335 void comedi_usb_driver_unregister(struct comedi_driver
*, struct usb_driver
*);
338 * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
339 * @__comedi_driver: comedi_driver struct
340 * @__usb_driver: usb_driver struct
342 * Helper macro for comedi USB drivers which do not do anything special
343 * in module init/exit. This eliminates a lot of boilerplate. Each
344 * module may only use this macro once, and calling it replaces
345 * module_init() and module_exit()
347 #define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
348 module_driver(__comedi_driver, comedi_usb_driver_register, \
349 comedi_usb_driver_unregister, &(__usb_driver))
351 void init_polling(void);
352 void cleanup_polling(void);
353 void start_polling(struct comedi_device
*);
354 void stop_polling(struct comedi_device
*);
356 #ifdef CONFIG_PROC_FS
357 void comedi_proc_init(void);
358 void comedi_proc_cleanup(void);
360 static inline void comedi_proc_init(void)
364 static inline void comedi_proc_cleanup(void)
369 /* subdevice runflags */
370 enum subdevice_runflags
{
371 SRF_USER
= 0x00000001,
373 /* indicates an COMEDI_CB_ERROR event has occurred since the last
374 * command was started */
375 SRF_ERROR
= 0x00000004,
376 SRF_RUNNING
= 0x08000000
379 int comedi_check_chanlist(struct comedi_subdevice
*s
,
381 unsigned int *chanlist
);
382 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
);
386 #define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
387 #define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
388 #define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
389 #define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
390 #define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
391 #define UNI_RANGE(a) {0, (a)*1e6, 0}
393 extern const struct comedi_lrange range_bipolar10
;
394 extern const struct comedi_lrange range_bipolar5
;
395 extern const struct comedi_lrange range_bipolar2_5
;
396 extern const struct comedi_lrange range_unipolar10
;
397 extern const struct comedi_lrange range_unipolar5
;
398 extern const struct comedi_lrange range_unknown
;
400 #define range_digital range_unipolar5
403 #define GCC_ZERO_LENGTH_ARRAY
405 #define GCC_ZERO_LENGTH_ARRAY 0
408 struct comedi_lrange
{
410 struct comedi_krange range
[GCC_ZERO_LENGTH_ARRAY
];
413 /* some silly little inline functions */
415 static inline int alloc_subdevices(struct comedi_device
*dev
,
416 unsigned int num_subdevices
)
420 dev
->n_subdevices
= num_subdevices
;
422 kcalloc(num_subdevices
, sizeof(struct comedi_subdevice
),
424 if (!dev
->subdevices
)
426 for (i
= 0; i
< num_subdevices
; ++i
) {
427 dev
->subdevices
[i
].device
= dev
;
428 dev
->subdevices
[i
].async_dma_dir
= DMA_NONE
;
429 spin_lock_init(&dev
->subdevices
[i
].spin_lock
);
430 dev
->subdevices
[i
].minor
= -1;
435 static inline int alloc_private(struct comedi_device
*dev
, int size
)
437 dev
->private = kzalloc(size
, GFP_KERNEL
);
443 static inline unsigned int bytes_per_sample(const struct comedi_subdevice
*subd
)
445 if (subd
->subdev_flags
& SDF_LSAMPL
)
446 return sizeof(unsigned int);
448 return sizeof(short);
451 /* must be used in attach to set dev->hw_dev if you wish to dma directly
452 into comedi's buffer */
453 static inline void comedi_set_hw_dev(struct comedi_device
*dev
,
454 struct device
*hw_dev
)
457 put_device(dev
->hw_dev
);
459 dev
->hw_dev
= hw_dev
;
461 dev
->hw_dev
= get_device(dev
->hw_dev
);
462 BUG_ON(dev
->hw_dev
== NULL
);
466 int comedi_buf_put(struct comedi_async
*async
, short x
);
467 int comedi_buf_get(struct comedi_async
*async
, short *x
);
469 unsigned int comedi_buf_write_n_available(struct comedi_async
*async
);
470 unsigned int comedi_buf_write_alloc(struct comedi_async
*async
,
471 unsigned int nbytes
);
472 unsigned int comedi_buf_write_alloc_strict(struct comedi_async
*async
,
473 unsigned int nbytes
);
474 unsigned comedi_buf_write_free(struct comedi_async
*async
, unsigned int nbytes
);
475 unsigned comedi_buf_read_alloc(struct comedi_async
*async
, unsigned nbytes
);
476 unsigned comedi_buf_read_free(struct comedi_async
*async
, unsigned int nbytes
);
477 unsigned int comedi_buf_read_n_available(struct comedi_async
*async
);
478 void comedi_buf_memcpy_to(struct comedi_async
*async
, unsigned int offset
,
479 const void *source
, unsigned int num_bytes
);
480 void comedi_buf_memcpy_from(struct comedi_async
*async
, unsigned int offset
,
481 void *destination
, unsigned int num_bytes
);
482 static inline unsigned comedi_buf_write_n_allocated(struct comedi_async
*async
)
484 return async
->buf_write_alloc_count
- async
->buf_write_count
;
487 static inline unsigned comedi_buf_read_n_allocated(struct comedi_async
*async
)
489 return async
->buf_read_alloc_count
- async
->buf_read_count
;
492 static inline void *comedi_aux_data(int options
[], int n
)
494 unsigned long address
;
495 unsigned long addressLow
;
497 if (sizeof(int) >= sizeof(void *))
498 address
= options
[COMEDI_DEVCONF_AUX_DATA_LO
];
500 address
= options
[COMEDI_DEVCONF_AUX_DATA_HI
];
501 bit_shift
= sizeof(int) * 8;
502 address
<<= bit_shift
;
503 addressLow
= options
[COMEDI_DEVCONF_AUX_DATA_LO
];
504 addressLow
&= (1UL << bit_shift
) - 1;
505 address
|= addressLow
;
508 address
+= options
[COMEDI_DEVCONF_AUX_DATA0_LENGTH
];
510 address
+= options
[COMEDI_DEVCONF_AUX_DATA1_LENGTH
];
512 address
+= options
[COMEDI_DEVCONF_AUX_DATA2_LENGTH
];
514 return (void *)address
;
517 int comedi_alloc_subdevice_minor(struct comedi_device
*dev
,
518 struct comedi_subdevice
*s
);
519 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
);
520 int comedi_pci_auto_config(struct pci_dev
*pcidev
,
521 struct comedi_driver
*driver
);
522 void comedi_pci_auto_unconfig(struct pci_dev
*pcidev
);
523 int comedi_usb_auto_config(struct usb_interface
*intf
,
524 struct comedi_driver
*driver
);
525 void comedi_usb_auto_unconfig(struct usb_interface
*intf
);
527 #ifdef CONFIG_COMEDI_PCI_DRIVERS
528 #define CONFIG_COMEDI_PCI
530 #ifdef CONFIG_COMEDI_PCI_DRIVERS_MODULE
531 #define CONFIG_COMEDI_PCI
533 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
534 #define CONFIG_COMEDI_PCMCIA
536 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS_MODULE
537 #define CONFIG_COMEDI_PCMCIA
540 #endif /* _COMEDIDEV_H */