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