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