259996483c666f2f26c0dc4529fec17aab963442
[platform/adaptation/renesas_rcar/renesas_kernel.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     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23
24 #ifndef _COMEDIDEV_H
25 #define _COMEDIDEV_H
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/kdev_t.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/errno.h>
33 #include <linux/spinlock.h>
34 #include <linux/mutex.h>
35 #include <linux/wait.h>
36 #include <linux/mm.h>
37 #include <linux/init.h>
38 #include <linux/vmalloc.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/uaccess.h>
41 #include <linux/io.h>
42 #include <linux/timer.h>
43 #include <linux/pci.h>
44 #include <linux/usb.h>
45
46 #include "comedi.h"
47
48 #define DPRINTK(format, args...)        do {            \
49         if (comedi_debug)                               \
50                 pr_debug("comedi: " format, ## args);   \
51 } while (0)
52
53 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
54 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \
55         COMEDI_MINORVERSION, COMEDI_MICROVERSION)
56 #define COMEDI_RELEASE VERSION
57
58 /*
59  * PCI Vendor IDs not in <linux/pci_ids.h>
60  */
61 #define PCI_VENDOR_ID_KOLTER            0x1001
62 #define PCI_VENDOR_ID_ICP               0x104c
63 #define PCI_VENDOR_ID_AMCC              0x10e8
64 #define PCI_VENDOR_ID_DT                0x1116
65 #define PCI_VENDOR_ID_IOTECH            0x1616
66 #define PCI_VENDOR_ID_CONTEC            0x1221
67 #define PCI_VENDOR_ID_CB                0x1307  /* Measurement Computing */
68 #define PCI_VENDOR_ID_ADVANTECH         0x13fe
69 #define PCI_VENDOR_ID_MEILHAUS          0x1402
70 #define PCI_VENDOR_ID_RTD               0x1435
71 #define PCI_VENDOR_ID_ADLINK            0x144a
72 #define PCI_VENDOR_ID_AMPLICON          0x14dc
73
74 #define COMEDI_NUM_MINORS 0x100
75 #define COMEDI_NUM_BOARD_MINORS 0x30
76 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
77
78 struct comedi_subdevice {
79         struct comedi_device *device;
80         int index;
81         int type;
82         int n_chan;
83         int subdev_flags;
84         int len_chanlist;       /* maximum length of channel/gain list */
85
86         void *private;
87
88         struct comedi_async *async;
89
90         void *lock;
91         void *busy;
92         unsigned runflags;
93         spinlock_t spin_lock;
94
95         unsigned int io_bits;
96
97         unsigned int maxdata;   /* if maxdata==0, use list */
98         const unsigned int *maxdata_list;       /* list is channel specific */
99
100         unsigned int flags;
101         const unsigned int *flaglist;
102
103         unsigned int settling_time_0;
104
105         const struct comedi_lrange *range_table;
106         const struct comedi_lrange *const *range_table_list;
107
108         unsigned int *chanlist; /* driver-owned chanlist (not used) */
109
110         int (*insn_read) (struct comedi_device *, struct comedi_subdevice *,
111                           struct comedi_insn *, unsigned int *);
112         int (*insn_write) (struct comedi_device *, struct comedi_subdevice *,
113                            struct comedi_insn *, unsigned int *);
114         int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *,
115                           struct comedi_insn *, unsigned int *);
116         int (*insn_config) (struct comedi_device *, struct comedi_subdevice *,
117                             struct comedi_insn *, unsigned int *);
118
119         int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
120         int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *,
121                            struct comedi_cmd *);
122         int (*poll) (struct comedi_device *, struct comedi_subdevice *);
123         int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
124         /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
125         /* int (*do_unlock)(struct comedi_device *, \
126                         struct comedi_subdevice *); */
127
128         /* called when the buffer changes */
129         int (*buf_change) (struct comedi_device *dev,
130                            struct comedi_subdevice *s, unsigned long new_size);
131
132         void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s,
133                        void *data, unsigned int num_bytes,
134                        unsigned int start_chan_index);
135         enum dma_data_direction async_dma_dir;
136
137         unsigned int state;
138
139         struct device *class_dev;
140         int minor;
141 };
142
143 struct comedi_buf_page {
144         void *virt_addr;
145         dma_addr_t dma_addr;
146 };
147
148 struct comedi_async {
149         struct comedi_subdevice *subdevice;
150
151         void *prealloc_buf;     /* pre-allocated buffer */
152         unsigned int prealloc_bufsz;    /* buffer size, in bytes */
153         /* virtual and dma address of each page */
154         struct comedi_buf_page *buf_page_list;
155         unsigned n_buf_pages;   /* num elements in buf_page_list */
156
157         unsigned int max_bufsize;       /* maximum buffer size, bytes */
158         /* current number of mmaps of prealloc_buf */
159         unsigned int mmap_count;
160
161         /* byte count for writer (write completed) */
162         unsigned int buf_write_count;
163         /* byte count for writer (allocated for writing) */
164         unsigned int buf_write_alloc_count;
165         /* byte count for reader (read completed) */
166         unsigned int buf_read_count;
167         /* byte count for reader (allocated for reading) */
168         unsigned int buf_read_alloc_count;
169
170         unsigned int buf_write_ptr;     /* buffer marker for writer */
171         unsigned int buf_read_ptr;      /* buffer marker for reader */
172
173         unsigned int cur_chan;  /* useless channel marker for interrupt */
174         /* number of bytes that have been received for current scan */
175         unsigned int scan_progress;
176         /* keeps track of where we are in chanlist as for munging */
177         unsigned int munge_chan;
178         /* number of bytes that have been munged */
179         unsigned int munge_count;
180         /* buffer marker for munging */
181         unsigned int munge_ptr;
182
183         unsigned int events;    /* events that have occurred */
184
185         struct comedi_cmd cmd;
186
187         wait_queue_head_t wait_head;
188
189         /* callback stuff */
190         unsigned int cb_mask;
191         int (*cb_func) (unsigned int flags, void *);
192         void *cb_arg;
193
194         int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
195                         unsigned int x);
196 };
197
198 struct comedi_driver {
199         struct comedi_driver *next;
200
201         const char *driver_name;
202         struct module *module;
203         int (*attach) (struct comedi_device *, struct comedi_devconfig *);
204         void (*detach) (struct comedi_device *);
205         int (*auto_attach) (struct comedi_device *, unsigned long);
206
207         /* number of elements in board_name and board_id arrays */
208         unsigned int num_names;
209         const char *const *board_name;
210         /* offset in bytes from one board name pointer to the next */
211         int offset;
212 };
213
214 struct comedi_device {
215         int use_count;
216         struct comedi_driver *driver;
217         void *private;
218
219         struct device *class_dev;
220         int minor;
221         /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
222          * for subdevices that have async_dma_dir set to something other than
223          * DMA_NONE */
224         struct device *hw_dev;
225
226         const char *board_name;
227         const void *board_ptr;
228         int attached;
229         spinlock_t spinlock;
230         struct mutex mutex;
231         int in_request_module;
232
233         int n_subdevices;
234         struct comedi_subdevice *subdevices;
235
236         /* dumb */
237         unsigned long iobase;
238         unsigned int irq;
239
240         struct comedi_subdevice *read_subdev;
241         struct comedi_subdevice *write_subdev;
242
243         struct fasync_struct *async_queue;
244
245         int (*open) (struct comedi_device *dev);
246         void (*close) (struct comedi_device *dev);
247 };
248
249 static inline const void *comedi_board(const struct comedi_device *dev)
250 {
251         return dev->board_ptr;
252 }
253
254 #ifdef CONFIG_COMEDI_DEBUG
255 extern int comedi_debug;
256 #else
257 static const int comedi_debug;
258 #endif
259
260 /*
261  * function prototypes
262  */
263
264 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
265 void comedi_error(const struct comedi_device *dev, const char *s);
266
267 /* we can expand the number of bits used to encode devices/subdevices into
268  the minor number soon, after more distros support > 8 bit minor numbers
269  (like after Debian Etch gets released) */
270 enum comedi_minor_bits {
271         COMEDI_DEVICE_MINOR_MASK = 0xf,
272         COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
273 };
274 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
275 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
276
277 struct comedi_device *comedi_dev_from_minor(unsigned minor);
278
279 int comedi_alloc_subdevices(struct comedi_device *, int);
280
281 void comedi_device_detach(struct comedi_device *dev);
282 int comedi_device_attach(struct comedi_device *dev,
283                          struct comedi_devconfig *it);
284 int comedi_driver_register(struct comedi_driver *);
285 int comedi_driver_unregister(struct comedi_driver *);
286
287 /**
288  * module_comedi_driver() - Helper macro for registering a comedi driver
289  * @__comedi_driver: comedi_driver struct
290  *
291  * Helper macro for comedi drivers which do not do anything special in module
292  * init/exit. This eliminates a lot of boilerplate. Each module may only use
293  * this macro once, and calling it replaces module_init() and module_exit().
294  */
295 #define module_comedi_driver(__comedi_driver) \
296         module_driver(__comedi_driver, comedi_driver_register, \
297                         comedi_driver_unregister)
298
299 int comedi_pci_enable(struct pci_dev *, const char *);
300 void comedi_pci_disable(struct pci_dev *);
301
302 int comedi_pci_driver_register(struct comedi_driver *, struct pci_driver *);
303 void comedi_pci_driver_unregister(struct comedi_driver *, struct pci_driver *);
304
305 /**
306  * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
307  * @__comedi_driver: comedi_driver struct
308  * @__pci_driver: pci_driver struct
309  *
310  * Helper macro for comedi PCI drivers which do not do anything special
311  * in module init/exit. This eliminates a lot of boilerplate. Each
312  * module may only use this macro once, and calling it replaces
313  * module_init() and module_exit()
314  */
315 #define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
316         module_driver(__comedi_driver, comedi_pci_driver_register, \
317                         comedi_pci_driver_unregister, &(__pci_driver))
318
319 struct usb_driver;
320
321 int comedi_usb_driver_register(struct comedi_driver *, struct usb_driver *);
322 void comedi_usb_driver_unregister(struct comedi_driver *, struct usb_driver *);
323
324 /**
325  * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
326  * @__comedi_driver: comedi_driver struct
327  * @__usb_driver: usb_driver struct
328  *
329  * Helper macro for comedi USB drivers which do not do anything special
330  * in module init/exit. This eliminates a lot of boilerplate. Each
331  * module may only use this macro once, and calling it replaces
332  * module_init() and module_exit()
333  */
334 #define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
335         module_driver(__comedi_driver, comedi_usb_driver_register, \
336                         comedi_usb_driver_unregister, &(__usb_driver))
337
338 void init_polling(void);
339 void cleanup_polling(void);
340 void start_polling(struct comedi_device *);
341 void stop_polling(struct comedi_device *);
342
343 #ifdef CONFIG_PROC_FS
344 void comedi_proc_init(void);
345 void comedi_proc_cleanup(void);
346 #else
347 static inline void comedi_proc_init(void)
348 {
349 }
350
351 static inline void comedi_proc_cleanup(void)
352 {
353 }
354 #endif
355
356 /* subdevice runflags */
357 enum subdevice_runflags {
358         SRF_USER = 0x00000001,
359         SRF_RT = 0x00000002,
360         /* indicates an COMEDI_CB_ERROR event has occurred since the last
361          * command was started */
362         SRF_ERROR = 0x00000004,
363         SRF_RUNNING = 0x08000000
364 };
365
366 bool comedi_is_subdevice_running(struct comedi_subdevice *s);
367
368 int comedi_check_chanlist(struct comedi_subdevice *s,
369                           int n,
370                           unsigned int *chanlist);
371
372 /* range stuff */
373
374 #define RANGE(a, b)             {(a)*1e6, (b)*1e6, 0}
375 #define RANGE_ext(a, b)         {(a)*1e6, (b)*1e6, RF_EXTERNAL}
376 #define RANGE_mA(a, b)          {(a)*1e6, (b)*1e6, UNIT_mA}
377 #define RANGE_unitless(a, b)    {(a)*1e6, (b)*1e6, 0}
378 #define BIP_RANGE(a)            {-(a)*1e6, (a)*1e6, 0}
379 #define UNI_RANGE(a)            {0, (a)*1e6, 0}
380
381 extern const struct comedi_lrange range_bipolar10;
382 extern const struct comedi_lrange range_bipolar5;
383 extern const struct comedi_lrange range_bipolar2_5;
384 extern const struct comedi_lrange range_unipolar10;
385 extern const struct comedi_lrange range_unipolar5;
386 extern const struct comedi_lrange range_unknown;
387
388 #define range_digital           range_unipolar5
389
390 #if __GNUC__ >= 3
391 #define GCC_ZERO_LENGTH_ARRAY
392 #else
393 #define GCC_ZERO_LENGTH_ARRAY 0
394 #endif
395
396 struct comedi_lrange {
397         int length;
398         struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
399 };
400
401 /* some silly little inline functions */
402
403 static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
404 {
405         if (subd->subdev_flags & SDF_LSAMPL)
406                 return sizeof(unsigned int);
407         else
408                 return sizeof(short);
409 }
410
411 /* must be used in attach to set dev->hw_dev if you wish to dma directly
412 into comedi's buffer */
413 static inline void comedi_set_hw_dev(struct comedi_device *dev,
414                                      struct device *hw_dev)
415 {
416         if (dev->hw_dev == hw_dev)
417                 return;
418         if (dev->hw_dev)
419                 put_device(dev->hw_dev);
420         dev->hw_dev = hw_dev;
421         if (dev->hw_dev) {
422                 dev->hw_dev = get_device(dev->hw_dev);
423                 BUG_ON(dev->hw_dev == NULL);
424         }
425 }
426
427 static inline struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
428 {
429         return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL;
430 }
431
432 static inline struct usb_interface *
433 comedi_to_usb_interface(struct comedi_device *dev)
434 {
435         return dev->hw_dev ? to_usb_interface(dev->hw_dev) : NULL;
436 }
437
438 int comedi_buf_put(struct comedi_async *async, short x);
439 int comedi_buf_get(struct comedi_async *async, short *x);
440
441 unsigned int comedi_buf_write_n_available(struct comedi_async *async);
442 unsigned int comedi_buf_write_alloc(struct comedi_async *async,
443                                     unsigned int nbytes);
444 unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
445                                            unsigned int nbytes);
446 unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes);
447 unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes);
448 unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes);
449 unsigned int comedi_buf_read_n_available(struct comedi_async *async);
450 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
451                           const void *source, unsigned int num_bytes);
452 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
453                             void *destination, unsigned int num_bytes);
454 static inline unsigned comedi_buf_write_n_allocated(struct comedi_async *async)
455 {
456         return async->buf_write_alloc_count - async->buf_write_count;
457 }
458
459 static inline unsigned comedi_buf_read_n_allocated(struct comedi_async *async)
460 {
461         return async->buf_read_alloc_count - async->buf_read_count;
462 }
463
464 static inline void *comedi_aux_data(int options[], int n)
465 {
466         unsigned long address;
467         unsigned long addressLow;
468         int bit_shift;
469         if (sizeof(int) >= sizeof(void *))
470                 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
471         else {
472                 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
473                 bit_shift = sizeof(int) * 8;
474                 address <<= bit_shift;
475                 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
476                 addressLow &= (1UL << bit_shift) - 1;
477                 address |= addressLow;
478         }
479         if (n >= 1)
480                 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
481         if (n >= 2)
482                 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
483         if (n >= 3)
484                 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
485         BUG_ON(n > 3);
486         return (void *)address;
487 }
488
489 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
490                                  struct comedi_subdevice *s);
491 void comedi_free_subdevice_minor(struct comedi_subdevice *s);
492 int comedi_auto_config(struct device *hardware_device,
493                        struct comedi_driver *driver, unsigned long context);
494 void comedi_auto_unconfig(struct device *hardware_device);
495
496 static inline int comedi_pci_auto_config(struct pci_dev *pcidev,
497                                          struct comedi_driver *driver)
498 {
499         return comedi_auto_config(&pcidev->dev, driver, 0);
500 }
501
502 static inline void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
503 {
504         comedi_auto_unconfig(&pcidev->dev);
505 }
506
507 static inline int comedi_usb_auto_config(struct usb_interface *intf,
508                                          struct comedi_driver *driver)
509 {
510         return comedi_auto_config(&intf->dev, driver, 0);
511 }
512
513 static inline void comedi_usb_auto_unconfig(struct usb_interface *intf)
514 {
515         comedi_auto_unconfig(&intf->dev);
516 }
517
518 #endif /* _COMEDIDEV_H */