comedidev.h revision 00ca6884186f18a758eae37e94f7c3c0527f8f30
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		pr_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_NUM_MINORS 0x100
57#define COMEDI_NUM_BOARD_MINORS 0x30
58#define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
59
60struct comedi_subdevice {
61	struct comedi_device *device;
62	int index;
63	int type;
64	int n_chan;
65	int subdev_flags;
66	int len_chanlist;	/* maximum length of channel/gain list */
67
68	void *private;
69
70	struct comedi_async *async;
71
72	void *lock;
73	void *busy;
74	unsigned runflags;
75	spinlock_t spin_lock;
76
77	unsigned int io_bits;
78
79	unsigned int maxdata;	/* if maxdata==0, use list */
80	const unsigned int *maxdata_list;	/* list is channel specific */
81
82	unsigned int flags;
83	const unsigned int *flaglist;
84
85	unsigned int settling_time_0;
86
87	const struct comedi_lrange *range_table;
88	const struct comedi_lrange *const *range_table_list;
89
90	unsigned int *chanlist;	/* driver-owned chanlist (not used) */
91
92	int (*insn_read) (struct comedi_device *, struct comedi_subdevice *,
93			  struct comedi_insn *, unsigned int *);
94	int (*insn_write) (struct comedi_device *, struct comedi_subdevice *,
95			   struct comedi_insn *, unsigned int *);
96	int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *,
97			  struct comedi_insn *, unsigned int *);
98	int (*insn_config) (struct comedi_device *, struct comedi_subdevice *,
99			    struct comedi_insn *, unsigned int *);
100
101	int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
102	int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *,
103			   struct comedi_cmd *);
104	int (*poll) (struct comedi_device *, struct comedi_subdevice *);
105	int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
106	/* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
107	/* int (*do_unlock)(struct comedi_device *, \
108			struct comedi_subdevice *); */
109
110	/* called when the buffer changes */
111	int (*buf_change) (struct comedi_device *dev,
112			   struct comedi_subdevice *s, unsigned long new_size);
113
114	void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s,
115		       void *data, unsigned int num_bytes,
116		       unsigned int start_chan_index);
117	enum dma_data_direction async_dma_dir;
118
119	unsigned int state;
120
121	struct device *class_dev;
122	int minor;
123};
124
125struct comedi_buf_page {
126	void *virt_addr;
127	dma_addr_t dma_addr;
128};
129
130struct comedi_async {
131	struct comedi_subdevice *subdevice;
132
133	void *prealloc_buf;	/* pre-allocated buffer */
134	unsigned int prealloc_bufsz;	/* buffer size, in bytes */
135	/* virtual and dma address of each page */
136	struct comedi_buf_page *buf_page_list;
137	unsigned n_buf_pages;	/* num elements in buf_page_list */
138
139	unsigned int max_bufsize;	/* maximum buffer size, bytes */
140	/* current number of mmaps of prealloc_buf */
141	unsigned int mmap_count;
142
143	/* byte count for writer (write completed) */
144	unsigned int buf_write_count;
145	/* byte count for writer (allocated for writing) */
146	unsigned int buf_write_alloc_count;
147	/* byte count for reader (read completed) */
148	unsigned int buf_read_count;
149	/* byte count for reader (allocated for reading) */
150	unsigned int buf_read_alloc_count;
151
152	unsigned int buf_write_ptr;	/* buffer marker for writer */
153	unsigned int buf_read_ptr;	/* buffer marker for reader */
154
155	unsigned int cur_chan;	/* useless channel marker for interrupt */
156	/* number of bytes that have been received for current scan */
157	unsigned int scan_progress;
158	/* keeps track of where we are in chanlist as for munging */
159	unsigned int munge_chan;
160	/* number of bytes that have been munged */
161	unsigned int munge_count;
162	/* buffer marker for munging */
163	unsigned int munge_ptr;
164
165	unsigned int events;	/* events that have occurred */
166
167	struct comedi_cmd cmd;
168
169	wait_queue_head_t wait_head;
170
171	/* callback stuff */
172	unsigned int cb_mask;
173	int (*cb_func) (unsigned int flags, void *);
174	void *cb_arg;
175
176	int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
177			unsigned int x);
178};
179
180struct comedi_driver {
181	struct comedi_driver *next;
182
183	const char *driver_name;
184	struct module *module;
185	int (*attach) (struct comedi_device *, struct comedi_devconfig *);
186	void (*detach) (struct comedi_device *);
187	int (*auto_attach) (struct comedi_device *, unsigned long);
188
189	/* number of elements in board_name and board_id arrays */
190	unsigned int num_names;
191	const char *const *board_name;
192	/* offset in bytes from one board name pointer to the next */
193	int offset;
194};
195
196struct comedi_device {
197	int use_count;
198	struct comedi_driver *driver;
199	void *private;
200
201	struct device *class_dev;
202	int minor;
203	/* hw_dev is passed to dma_alloc_coherent when allocating async buffers
204	 * for subdevices that have async_dma_dir set to something other than
205	 * DMA_NONE */
206	struct device *hw_dev;
207
208	const char *board_name;
209	const void *board_ptr;
210	bool attached:1;
211	bool in_request_module:1;
212	bool ioenabled:1;
213	spinlock_t spinlock;
214	struct mutex mutex;
215
216	int n_subdevices;
217	struct comedi_subdevice *subdevices;
218
219	/* dumb */
220	unsigned long iobase;
221	unsigned int irq;
222
223	struct comedi_subdevice *read_subdev;
224	struct comedi_subdevice *write_subdev;
225
226	struct fasync_struct *async_queue;
227
228	int (*open) (struct comedi_device *dev);
229	void (*close) (struct comedi_device *dev);
230};
231
232static inline const void *comedi_board(const struct comedi_device *dev)
233{
234	return dev->board_ptr;
235}
236
237#ifdef CONFIG_COMEDI_DEBUG
238extern int comedi_debug;
239#else
240static const int comedi_debug;
241#endif
242
243/*
244 * function prototypes
245 */
246
247void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
248void comedi_error(const struct comedi_device *dev, const char *s);
249
250/* we can expand the number of bits used to encode devices/subdevices into
251 the minor number soon, after more distros support > 8 bit minor numbers
252 (like after Debian Etch gets released) */
253enum comedi_minor_bits {
254	COMEDI_DEVICE_MINOR_MASK = 0xf,
255	COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
256};
257static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
258static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
259
260struct comedi_device *comedi_dev_from_minor(unsigned minor);
261
262void init_polling(void);
263void cleanup_polling(void);
264void start_polling(struct comedi_device *);
265void stop_polling(struct comedi_device *);
266
267/* subdevice runflags */
268enum subdevice_runflags {
269	SRF_USER = 0x00000001,
270	SRF_RT = 0x00000002,
271	/* indicates an COMEDI_CB_ERROR event has occurred since the last
272	 * command was started */
273	SRF_ERROR = 0x00000004,
274	SRF_RUNNING = 0x08000000
275};
276
277bool comedi_is_subdevice_running(struct comedi_subdevice *s);
278
279int comedi_check_chanlist(struct comedi_subdevice *s,
280			  int n,
281			  unsigned int *chanlist);
282
283/* range stuff */
284
285#define RANGE(a, b)		{(a)*1e6, (b)*1e6, 0}
286#define RANGE_ext(a, b)		{(a)*1e6, (b)*1e6, RF_EXTERNAL}
287#define RANGE_mA(a, b)		{(a)*1e6, (b)*1e6, UNIT_mA}
288#define RANGE_unitless(a, b)	{(a)*1e6, (b)*1e6, 0}
289#define BIP_RANGE(a)		{-(a)*1e6, (a)*1e6, 0}
290#define UNI_RANGE(a)		{0, (a)*1e6, 0}
291
292extern const struct comedi_lrange range_bipolar10;
293extern const struct comedi_lrange range_bipolar5;
294extern const struct comedi_lrange range_bipolar2_5;
295extern const struct comedi_lrange range_unipolar10;
296extern const struct comedi_lrange range_unipolar5;
297extern const struct comedi_lrange range_unknown;
298
299#define range_digital		range_unipolar5
300
301#if __GNUC__ >= 3
302#define GCC_ZERO_LENGTH_ARRAY
303#else
304#define GCC_ZERO_LENGTH_ARRAY 0
305#endif
306
307struct comedi_lrange {
308	int length;
309	struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
310};
311
312/* some silly little inline functions */
313
314static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
315{
316	if (subd->subdev_flags & SDF_LSAMPL)
317		return sizeof(unsigned int);
318	else
319		return sizeof(short);
320}
321
322/*
323 * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
324 * Also useful for retrieving a previously configured hardware device of
325 * known bus type.  Set automatically for auto-configured devices.
326 * Automatically set to NULL when detaching hardware device.
327 */
328int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev);
329
330unsigned int comedi_buf_write_alloc(struct comedi_async *, unsigned int);
331unsigned int comedi_buf_write_free(struct comedi_async *, unsigned int);
332
333unsigned int comedi_buf_read_n_available(struct comedi_async *);
334unsigned int comedi_buf_read_alloc(struct comedi_async *, unsigned int);
335unsigned int comedi_buf_read_free(struct comedi_async *, unsigned int);
336
337int comedi_buf_put(struct comedi_async *, short);
338int comedi_buf_get(struct comedi_async *, short *);
339
340void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
341			  const void *source, unsigned int num_bytes);
342void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
343			    void *destination, unsigned int num_bytes);
344
345/* drivers.c - general comedi driver functions */
346
347int comedi_alloc_subdevices(struct comedi_device *, int);
348
349int comedi_auto_config(struct device *, struct comedi_driver *,
350		       unsigned long context);
351void comedi_auto_unconfig(struct device *);
352
353int comedi_driver_register(struct comedi_driver *);
354int comedi_driver_unregister(struct comedi_driver *);
355
356/**
357 * module_comedi_driver() - Helper macro for registering a comedi driver
358 * @__comedi_driver: comedi_driver struct
359 *
360 * Helper macro for comedi drivers which do not do anything special in module
361 * init/exit. This eliminates a lot of boilerplate. Each module may only use
362 * this macro once, and calling it replaces module_init() and module_exit().
363 */
364#define module_comedi_driver(__comedi_driver) \
365	module_driver(__comedi_driver, comedi_driver_register, \
366			comedi_driver_unregister)
367
368#ifdef CONFIG_COMEDI_PCI_DRIVERS
369
370/* comedi_pci.c - comedi PCI driver specific functions */
371
372/*
373 * PCI Vendor IDs not in <linux/pci_ids.h>
374 */
375#define PCI_VENDOR_ID_KOLTER		0x1001
376#define PCI_VENDOR_ID_ICP		0x104c
377#define PCI_VENDOR_ID_AMCC		0x10e8
378#define PCI_VENDOR_ID_DT		0x1116
379#define PCI_VENDOR_ID_IOTECH		0x1616
380#define PCI_VENDOR_ID_CONTEC		0x1221
381#define PCI_VENDOR_ID_RTD		0x1435
382
383struct pci_dev;
384struct pci_driver;
385
386struct pci_dev *comedi_to_pci_dev(struct comedi_device *);
387
388int comedi_pci_enable(struct comedi_device *);
389void comedi_pci_disable(struct comedi_device *);
390
391int comedi_pci_auto_config(struct pci_dev *, struct comedi_driver *,
392			   unsigned long context);
393void comedi_pci_auto_unconfig(struct pci_dev *);
394
395int comedi_pci_driver_register(struct comedi_driver *, struct pci_driver *);
396void comedi_pci_driver_unregister(struct comedi_driver *, struct pci_driver *);
397
398/**
399 * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
400 * @__comedi_driver: comedi_driver struct
401 * @__pci_driver: pci_driver struct
402 *
403 * Helper macro for comedi PCI drivers which do not do anything special
404 * in module init/exit. This eliminates a lot of boilerplate. Each
405 * module may only use this macro once, and calling it replaces
406 * module_init() and module_exit()
407 */
408#define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
409	module_driver(__comedi_driver, comedi_pci_driver_register, \
410			comedi_pci_driver_unregister, &(__pci_driver))
411
412#else
413
414/*
415 * Some of the comedi mixed ISA/PCI drivers call the PCI specific
416 * functions. Provide some dummy functions if CONFIG_COMEDI_PCI_DRIVERS
417 * is not enabled.
418 */
419
420static inline struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
421{
422	return NULL;
423}
424
425static inline int comedi_pci_enable(struct comedi_device *dev)
426{
427	return -ENOSYS;
428}
429
430static inline void comedi_pci_disable(struct comedi_device *dev)
431{
432}
433
434#endif /* CONFIG_COMEDI_PCI_DRIVERS */
435
436#ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
437
438/* comedi_pcmcia.c - comedi PCMCIA driver specific functions */
439
440struct pcmcia_driver;
441struct pcmcia_device;
442
443struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *);
444
445int comedi_pcmcia_enable(struct comedi_device *,
446			 int (*conf_check)(struct pcmcia_device *, void *));
447void comedi_pcmcia_disable(struct comedi_device *);
448
449int comedi_pcmcia_auto_config(struct pcmcia_device *, struct comedi_driver *);
450void comedi_pcmcia_auto_unconfig(struct pcmcia_device *);
451
452int comedi_pcmcia_driver_register(struct comedi_driver *,
453					struct pcmcia_driver *);
454void comedi_pcmcia_driver_unregister(struct comedi_driver *,
455					struct pcmcia_driver *);
456
457/**
458 * module_comedi_pcmcia_driver() - Helper macro for registering a comedi PCMCIA driver
459 * @__comedi_driver: comedi_driver struct
460 * @__pcmcia_driver: pcmcia_driver struct
461 *
462 * Helper macro for comedi PCMCIA drivers which do not do anything special
463 * in module init/exit. This eliminates a lot of boilerplate. Each
464 * module may only use this macro once, and calling it replaces
465 * module_init() and module_exit()
466 */
467#define module_comedi_pcmcia_driver(__comedi_driver, __pcmcia_driver) \
468	module_driver(__comedi_driver, comedi_pcmcia_driver_register, \
469			comedi_pcmcia_driver_unregister, &(__pcmcia_driver))
470
471#endif /* CONFIG_COMEDI_PCMCIA_DRIVERS */
472
473#ifdef CONFIG_COMEDI_USB_DRIVERS
474
475/* comedi_usb.c - comedi USB driver specific functions */
476
477struct usb_driver;
478struct usb_interface;
479
480struct usb_interface *comedi_to_usb_interface(struct comedi_device *);
481
482int comedi_usb_auto_config(struct usb_interface *, struct comedi_driver *,
483			   unsigned long context);
484void comedi_usb_auto_unconfig(struct usb_interface *);
485
486int comedi_usb_driver_register(struct comedi_driver *, struct usb_driver *);
487void comedi_usb_driver_unregister(struct comedi_driver *, struct usb_driver *);
488
489/**
490 * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
491 * @__comedi_driver: comedi_driver struct
492 * @__usb_driver: usb_driver struct
493 *
494 * Helper macro for comedi USB drivers which do not do anything special
495 * in module init/exit. This eliminates a lot of boilerplate. Each
496 * module may only use this macro once, and calling it replaces
497 * module_init() and module_exit()
498 */
499#define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
500	module_driver(__comedi_driver, comedi_usb_driver_register, \
501			comedi_usb_driver_unregister, &(__usb_driver))
502
503#endif /* CONFIG_COMEDI_USB_DRIVERS */
504
505#endif /* _COMEDIDEV_H */
506