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