comedidev.h revision 9ced1de69125b60f40127eddaa3be2a92bb0a1df
1/*
2    include/linux/comedidev.h
3    header file for kernel-only structures, variables, and constants
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7
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.
12
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.
17
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.
21
22*/
23
24#ifndef _COMEDIDEV_H
25#define _COMEDIDEV_H
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/kdev_t.h>
30#include <linux/slab.h>
31#include <linux/errno.h>
32#include <linux/spinlock.h>
33#include <linux/mutex.h>
34#include <linux/wait.h>
35#include <linux/mm.h>
36#include <linux/init.h>
37#include <linux/vmalloc.h>
38#include "interrupt.h"
39#include <linux/dma-mapping.h>
40#include <linux/uaccess.h>
41#include <linux/io.h>
42
43#include "comedi.h"
44
45#define DPRINTK(format, args...)	do {		\
46	if (comedi_debug)				\
47		printk(KERN_DEBUG "comedi: " format , ## args);	\
48} while (0)
49
50#define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
51#define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, COMEDI_MINORVERSION, COMEDI_MICROVERSION)
52#define COMEDI_RELEASE VERSION
53
54#define COMEDI_INITCLEANUP_NOMODULE(x)					\
55	static int __init x ## _init_module(void)			\
56		{return comedi_driver_register(&(x)); }			\
57	static void __exit x ## _cleanup_module(void)			\
58		{comedi_driver_unregister(&(x)); } 			\
59	module_init(x ## _init_module);					\
60	module_exit(x ## _cleanup_module);					\
61
62#define COMEDI_MODULE_MACROS						\
63	MODULE_AUTHOR("Comedi http://www.comedi.org");		\
64	MODULE_DESCRIPTION("Comedi low-level driver");			\
65	MODULE_LICENSE("GPL");						\
66
67#define COMEDI_INITCLEANUP(x)						\
68	COMEDI_MODULE_MACROS		\
69	COMEDI_INITCLEANUP_NOMODULE(x)
70
71#define COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table) \
72	static int __devinit comedi_driver ## _pci_probe(struct pci_dev *dev, \
73		const struct pci_device_id *ent) \
74	{ \
75		return comedi_pci_auto_config(dev, comedi_driver.driver_name); \
76	} \
77	static void __devexit comedi_driver ## _pci_remove(struct pci_dev *dev) \
78	{ \
79		comedi_pci_auto_unconfig(dev); \
80	} \
81	static struct pci_driver comedi_driver ## _pci_driver = \
82	{ \
83		.id_table = pci_id_table, \
84		.probe = &comedi_driver ## _pci_probe, \
85		.remove = __devexit_p(&comedi_driver ## _pci_remove) \
86	}; \
87	static int __init comedi_driver ## _init_module(void) \
88	{ \
89		int retval; \
90		retval = comedi_driver_register(&comedi_driver); \
91		if (retval < 0) \
92			return retval; \
93		comedi_driver ## _pci_driver.name = (char *)comedi_driver.driver_name; \
94		return pci_register_driver(&comedi_driver ## _pci_driver); \
95	} \
96	static void __exit comedi_driver ## _cleanup_module(void) \
97	{ \
98		pci_unregister_driver(&comedi_driver ## _pci_driver); \
99		comedi_driver_unregister(&comedi_driver); \
100	} \
101	module_init(comedi_driver ## _init_module); \
102	module_exit(comedi_driver ## _cleanup_module);
103
104#define COMEDI_PCI_INITCLEANUP(comedi_driver, pci_id_table) \
105	COMEDI_MODULE_MACROS \
106	COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table)
107
108#define PCI_VENDOR_ID_INOVA		0x104c
109#define PCI_VENDOR_ID_NATINST		0x1093
110#define PCI_VENDOR_ID_DATX		0x1116
111#define PCI_VENDOR_ID_COMPUTERBOARDS	0x1307
112#define PCI_VENDOR_ID_ADVANTECH		0x13fe
113#define PCI_VENDOR_ID_RTD		0x1435
114#define PCI_VENDOR_ID_AMPLICON		0x14dc
115#define PCI_VENDOR_ID_ADLINK		0x144a
116#define PCI_VENDOR_ID_ICP		0x104c
117#define PCI_VENDOR_ID_CONTEC		0x1221
118#define PCI_VENDOR_ID_MEILHAUS		0x1402
119
120#define COMEDI_NUM_MINORS 0x100
121#define COMEDI_NUM_BOARD_MINORS 0x30
122#define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
123
124typedef struct device device_create_result_type;
125
126#define COMEDI_DEVICE_CREATE(cs, parent, devt, drvdata, device, fmt...) \
127	device_create(cs, ((parent) ? (parent) : (device)), devt, drvdata, fmt)
128
129struct comedi_subdevice {
130	struct comedi_device *device;
131	int type;
132	int n_chan;
133	volatile int subdev_flags;
134	int len_chanlist;	/* maximum length of channel/gain list */
135
136	void *private;
137
138	struct comedi_async *async;
139
140	void *lock;
141	void *busy;
142	unsigned runflags;
143	spinlock_t spin_lock;
144
145	int io_bits;
146
147	unsigned int maxdata;	/* if maxdata==0, use list */
148	const unsigned int *maxdata_list;	/* list is channel specific */
149
150	unsigned int flags;
151	const unsigned int *flaglist;
152
153	unsigned int settling_time_0;
154
155	const struct comedi_lrange *range_table;
156	const struct comedi_lrange *const *range_table_list;
157
158	unsigned int *chanlist;	/* driver-owned chanlist (not used) */
159
160	int (*insn_read) (struct comedi_device *, struct comedi_subdevice *, comedi_insn *,
161		unsigned int *);
162	int (*insn_write) (struct comedi_device *, struct comedi_subdevice *, comedi_insn *,
163		unsigned int *);
164	int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *, comedi_insn *,
165		unsigned int *);
166	int (*insn_config) (struct comedi_device *, struct comedi_subdevice *, comedi_insn *,
167		unsigned int *);
168
169	int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
170	int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *, comedi_cmd *);
171	int (*poll) (struct comedi_device *, struct comedi_subdevice *);
172	int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
173	/* int (*do_lock)(struct comedi_device *,struct comedi_subdevice *); */
174	/* int (*do_unlock)(struct comedi_device *,struct comedi_subdevice *); */
175
176	/* called when the buffer changes */
177	int (*buf_change) (struct comedi_device *dev, struct comedi_subdevice *s,
178		unsigned long new_size);
179
180	void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s, void *data,
181		unsigned int num_bytes, unsigned int start_chan_index);
182	enum dma_data_direction async_dma_dir;
183
184	unsigned int state;
185
186	device_create_result_type *class_dev;
187	int minor;
188};
189
190struct comedi_buf_page {
191	void *virt_addr;
192	dma_addr_t dma_addr;
193};
194
195struct comedi_async {
196	struct comedi_subdevice *subdevice;
197
198	void *prealloc_buf;	/* pre-allocated buffer */
199	unsigned int prealloc_bufsz;	/* buffer size, in bytes */
200	struct comedi_buf_page *buf_page_list;	/* virtual and dma address of each page */
201	unsigned n_buf_pages;	/* num elements in buf_page_list */
202
203	unsigned int max_bufsize;	/* maximum buffer size, bytes */
204	unsigned int mmap_count;	/* current number of mmaps of prealloc_buf */
205
206	unsigned int buf_write_count;	/* byte count for writer (write completed) */
207	unsigned int buf_write_alloc_count;	/* byte count for writer (allocated for writing) */
208	unsigned int buf_read_count;	/* byte count for reader (read completed) */
209	unsigned int buf_read_alloc_count;	/* byte count for reader (allocated for reading) */
210
211	unsigned int buf_write_ptr;	/* buffer marker for writer */
212	unsigned int buf_read_ptr;	/* buffer marker for reader */
213
214	unsigned int cur_chan;	/* useless channel marker for interrupt */
215	/* number of bytes that have been received for current scan */
216	unsigned int scan_progress;
217	/* keeps track of where we are in chanlist as for munging */
218	unsigned int munge_chan;
219	/* number of bytes that have been munged */
220	unsigned int munge_count;
221	/* buffer marker for munging */
222	unsigned int munge_ptr;
223
224	unsigned int events;	/* events that have occurred */
225
226	comedi_cmd cmd;
227
228	wait_queue_head_t wait_head;
229
230	/* callback stuff */
231	unsigned int cb_mask;
232	int (*cb_func) (unsigned int flags, void *);
233	void *cb_arg;
234
235	int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
236			unsigned int x);
237};
238
239struct comedi_driver {
240	struct comedi_driver *next;
241
242	const char *driver_name;
243	struct module *module;
244	int (*attach) (struct comedi_device *, comedi_devconfig *);
245	int (*detach) (struct comedi_device *);
246
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 */
251	int offset;
252};
253
254struct comedi_device {
255	int use_count;
256	struct comedi_driver *driver;
257	void *private;
258
259	device_create_result_type *class_dev;
260	int minor;
261	/* hw_dev is passed to dma_alloc_coherent when allocating async buffers
262	 * for subdevices that have async_dma_dir set to something other than
263	 * DMA_NONE */
264	struct device *hw_dev;
265
266	const char *board_name;
267	const void *board_ptr;
268	int attached;
269	int rt;
270	spinlock_t spinlock;
271	struct mutex mutex;
272	int in_request_module;
273
274	int n_subdevices;
275	struct comedi_subdevice *subdevices;
276
277	/* dumb */
278	unsigned long iobase;
279	unsigned int irq;
280
281	struct comedi_subdevice *read_subdev;
282	struct comedi_subdevice *write_subdev;
283
284	struct fasync_struct *async_queue;
285
286	void (*open) (struct comedi_device *dev);
287	void (*close) (struct comedi_device *dev);
288};
289
290struct comedi_device_file_info {
291	struct comedi_device *device;
292	struct comedi_subdevice *read_subdevice;
293	struct comedi_subdevice *write_subdevice;
294};
295
296#ifdef CONFIG_COMEDI_DEBUG
297extern int comedi_debug;
298#else
299static const int comedi_debug;
300#endif
301
302/*
303 * function prototypes
304 */
305
306void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
307void comedi_error(const struct comedi_device *dev, const char *s);
308
309/* we can expand the number of bits used to encode devices/subdevices into
310 the minor number soon, after more distros support > 8 bit minor numbers
311 (like after Debian Etch gets released) */
312enum comedi_minor_bits {
313	COMEDI_DEVICE_MINOR_MASK = 0xf,
314	COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
315};
316static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
317static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
318
319struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor);
320
321static inline struct comedi_subdevice *comedi_get_read_subdevice(
322				const struct comedi_device_file_info *info)
323{
324	if (info->read_subdevice)
325		return info->read_subdevice;
326	if (info->device == NULL)
327		return NULL;
328	return info->device->read_subdev;
329}
330
331static inline struct comedi_subdevice *comedi_get_write_subdevice(
332				const struct comedi_device_file_info *info)
333{
334	if (info->write_subdevice)
335		return info->write_subdevice;
336	if (info->device == NULL)
337		return NULL;
338	return info->device->write_subdev;
339}
340
341void comedi_device_detach(struct comedi_device *dev);
342int comedi_device_attach(struct comedi_device *dev, comedi_devconfig *it);
343int comedi_driver_register(struct comedi_driver *);
344int comedi_driver_unregister(struct comedi_driver *);
345
346void init_polling(void);
347void cleanup_polling(void);
348void start_polling(struct comedi_device *);
349void stop_polling(struct comedi_device *);
350
351int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s, unsigned long
352	new_size);
353
354#ifdef CONFIG_PROC_FS
355void comedi_proc_init(void);
356void comedi_proc_cleanup(void);
357#else
358static inline void comedi_proc_init(void)
359{
360}
361static inline void comedi_proc_cleanup(void)
362{
363}
364#endif
365
366/* subdevice runflags */
367enum subdevice_runflags {
368	SRF_USER = 0x00000001,
369	SRF_RT = 0x00000002,
370	/* indicates an COMEDI_CB_ERROR event has occurred since the last
371	 * command was started */
372	SRF_ERROR = 0x00000004,
373	SRF_RUNNING = 0x08000000
374};
375
376/*
377   various internal comedi functions
378 */
379
380int do_rangeinfo_ioctl(struct comedi_device *dev, comedi_rangeinfo *arg);
381int check_chanlist(struct comedi_subdevice *s, int n, unsigned int *chanlist);
382void comedi_set_subdevice_runflags(struct comedi_subdevice *s, unsigned mask,
383	unsigned bits);
384unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s);
385int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
386	comedi_insn *insn, unsigned int *data);
387
388/* range stuff */
389
390#define RANGE(a, b)		{(a)*1e6, (b)*1e6, 0}
391#define RANGE_ext(a, b)		{(a)*1e6, (b)*1e6, RF_EXTERNAL}
392#define RANGE_mA(a, b)		{(a)*1e6, (b)*1e6, UNIT_mA}
393#define RANGE_unitless(a, b)	{(a)*1e6, (b)*1e6, 0}	/* XXX */
394#define BIP_RANGE(a)		{-(a)*1e6, (a)*1e6, 0}
395#define UNI_RANGE(a)		{0, (a)*1e6, 0}
396
397extern const struct comedi_lrange range_bipolar10;
398extern const struct comedi_lrange range_bipolar5;
399extern const struct comedi_lrange range_bipolar2_5;
400extern const struct comedi_lrange range_unipolar10;
401extern const struct comedi_lrange range_unipolar5;
402extern const struct comedi_lrange range_unknown;
403
404#define range_digital		range_unipolar5
405
406#if __GNUC__ >= 3
407#define GCC_ZERO_LENGTH_ARRAY
408#else
409#define GCC_ZERO_LENGTH_ARRAY 0
410#endif
411
412struct comedi_lrange {
413	int length;
414	comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
415};
416
417/* some silly little inline functions */
418
419static inline int alloc_subdevices(struct comedi_device *dev,
420				   unsigned int num_subdevices)
421{
422	unsigned i;
423
424	dev->n_subdevices = num_subdevices;
425	dev->subdevices =
426		kcalloc(num_subdevices, sizeof(struct comedi_subdevice), GFP_KERNEL);
427	if (!dev->subdevices)
428		return -ENOMEM;
429	for (i = 0; i < num_subdevices; ++i) {
430		dev->subdevices[i].device = dev;
431		dev->subdevices[i].async_dma_dir = DMA_NONE;
432		spin_lock_init(&dev->subdevices[i].spin_lock);
433		dev->subdevices[i].minor = -1;
434	}
435	return 0;
436}
437
438static inline int alloc_private(struct comedi_device *dev, int size)
439{
440	dev->private = kzalloc(size, GFP_KERNEL);
441	if (!dev->private)
442		return -ENOMEM;
443	return 0;
444}
445
446static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
447{
448	if (subd->subdev_flags & SDF_LSAMPL)
449		return sizeof(unsigned int);
450	else
451		return sizeof(short);
452}
453
454/* must be used in attach to set dev->hw_dev if you wish to dma directly
455into comedi's buffer */
456static inline void comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
457{
458	if (dev->hw_dev)
459		put_device(dev->hw_dev);
460
461	dev->hw_dev = hw_dev;
462	if (dev->hw_dev) {
463		dev->hw_dev = get_device(dev->hw_dev);
464		BUG_ON(dev->hw_dev == NULL);
465	}
466}
467
468int comedi_buf_put(struct comedi_async *async, short x);
469int comedi_buf_get(struct comedi_async *async, short *x);
470
471unsigned int comedi_buf_write_n_available(struct comedi_async *async);
472unsigned int comedi_buf_write_alloc(struct comedi_async *async, unsigned int nbytes);
473unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
474	unsigned int nbytes);
475unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes);
476unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes);
477unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes);
478unsigned int comedi_buf_read_n_available(struct comedi_async *async);
479void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
480	const void *source, unsigned int num_bytes);
481void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
482	void *destination, unsigned int num_bytes);
483static inline unsigned comedi_buf_write_n_allocated(struct comedi_async *async)
484{
485	return async->buf_write_alloc_count - async->buf_write_count;
486}
487static inline unsigned comedi_buf_read_n_allocated(struct comedi_async *async)
488{
489	return async->buf_read_alloc_count - async->buf_read_count;
490}
491
492void comedi_reset_async_buf(struct comedi_async *async);
493
494static inline void *comedi_aux_data(int options[], int n)
495{
496	unsigned long address;
497	unsigned long addressLow;
498	int bit_shift;
499	if (sizeof(int) >= sizeof(void *))
500		address = options[COMEDI_DEVCONF_AUX_DATA_LO];
501	else {
502		address = options[COMEDI_DEVCONF_AUX_DATA_HI];
503		bit_shift = sizeof(int) * 8;
504		address <<= bit_shift;
505		addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
506		addressLow &= (1UL << bit_shift) - 1;
507		address |= addressLow;
508	}
509	if (n >= 1)
510		address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
511	if (n >= 2)
512		address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
513	if (n >= 3)
514		address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
515	BUG_ON(n > 3);
516	return (void *)address;
517}
518
519int comedi_alloc_board_minor(struct device *hardware_device);
520void comedi_free_board_minor(unsigned minor);
521int comedi_alloc_subdevice_minor(struct comedi_device *dev, struct comedi_subdevice *s);
522void comedi_free_subdevice_minor(struct comedi_subdevice *s);
523int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name);
524void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
525struct usb_device;	/* forward declaration */
526int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name);
527void comedi_usb_auto_unconfig(struct usb_device *usbdev);
528
529#include "comedi_rt.h"
530
531#endif /* _COMEDIDEV_H */
532