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