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