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