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