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