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