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