Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[cascardo/linux.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         struct comedi_subdevice *subdevice;
112
113         void *prealloc_buf;     /* pre-allocated buffer */
114         unsigned int prealloc_bufsz;    /* buffer size, in bytes */
115         struct comedi_buf_map *buf_map; /* map of buffer pages */
116
117         unsigned int max_bufsize;       /* maximum buffer size, bytes */
118
119         /* byte count for writer (write completed) */
120         unsigned int buf_write_count;
121         /* byte count for writer (allocated for writing) */
122         unsigned int buf_write_alloc_count;
123         /* byte count for reader (read completed) */
124         unsigned int buf_read_count;
125         /* byte count for reader (allocated for reading) */
126         unsigned int buf_read_alloc_count;
127
128         unsigned int buf_write_ptr;     /* buffer marker for writer */
129         unsigned int buf_read_ptr;      /* buffer marker for reader */
130
131         unsigned int cur_chan;  /* useless channel marker for interrupt */
132         /* number of bytes that have been received for current scan */
133         unsigned int scan_progress;
134         /* keeps track of where we are in chanlist as for munging */
135         unsigned int munge_chan;
136         /* number of bytes that have been munged */
137         unsigned int munge_count;
138         /* buffer marker for munging */
139         unsigned int munge_ptr;
140
141         unsigned int events;    /* events that have occurred */
142
143         struct comedi_cmd cmd;
144
145         wait_queue_head_t wait_head;
146
147         unsigned int cb_mask;
148
149         int (*inttrig)(struct comedi_device *dev, struct comedi_subdevice *s,
150                        unsigned int x);
151 };
152
153 struct comedi_driver {
154         struct comedi_driver *next;
155
156         const char *driver_name;
157         struct module *module;
158         int (*attach)(struct comedi_device *, struct comedi_devconfig *);
159         void (*detach)(struct comedi_device *);
160         int (*auto_attach)(struct comedi_device *, unsigned long);
161
162         /* number of elements in board_name and board_id arrays */
163         unsigned int num_names;
164         const char *const *board_name;
165         /* offset in bytes from one board name pointer to the next */
166         int offset;
167 };
168
169 struct comedi_device {
170         int use_count;
171         struct comedi_driver *driver;
172         void *private;
173
174         struct device *class_dev;
175         int minor;
176         unsigned int detach_count;
177         /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
178          * for subdevices that have async_dma_dir set to something other than
179          * DMA_NONE */
180         struct device *hw_dev;
181
182         const char *board_name;
183         const void *board_ptr;
184         bool attached:1;
185         bool in_request_module:1;
186         bool ioenabled:1;
187         spinlock_t spinlock;
188         struct mutex mutex;
189         struct rw_semaphore attach_lock;
190         struct kref refcount;
191
192         int n_subdevices;
193         struct comedi_subdevice *subdevices;
194
195         /* dumb */
196         unsigned long iobase;
197         unsigned long iolen;
198         unsigned int irq;
199
200         struct comedi_subdevice *read_subdev;
201         struct comedi_subdevice *write_subdev;
202
203         struct fasync_struct *async_queue;
204
205         int (*open)(struct comedi_device *dev);
206         void (*close)(struct comedi_device *dev);
207 };
208
209 static inline const void *comedi_board(const struct comedi_device *dev)
210 {
211         return dev->board_ptr;
212 }
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_get_from_minor(unsigned minor);
232 int comedi_dev_put(struct comedi_device *dev);
233
234 void init_polling(void);
235 void cleanup_polling(void);
236 void start_polling(struct comedi_device *);
237 void stop_polling(struct comedi_device *);
238
239 /* subdevice runflags */
240 enum subdevice_runflags {
241         SRF_RT = 0x00000002,
242         /* indicates an COMEDI_CB_ERROR event has occurred since the last
243          * command was started */
244         SRF_ERROR = 0x00000004,
245         SRF_RUNNING = 0x08000000,
246         SRF_FREE_SPRIV = 0x80000000,    /* free s->private on detach */
247 };
248
249 bool comedi_is_subdevice_running(struct comedi_subdevice *s);
250
251 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size);
252
253 int comedi_check_chanlist(struct comedi_subdevice *s,
254                           int n,
255                           unsigned int *chanlist);
256
257 /* range stuff */
258
259 #define RANGE(a, b)             {(a)*1e6, (b)*1e6, 0}
260 #define RANGE_ext(a, b)         {(a)*1e6, (b)*1e6, RF_EXTERNAL}
261 #define RANGE_mA(a, b)          {(a)*1e6, (b)*1e6, UNIT_mA}
262 #define RANGE_unitless(a, b)    {(a)*1e6, (b)*1e6, 0}
263 #define BIP_RANGE(a)            {-(a)*1e6, (a)*1e6, 0}
264 #define UNI_RANGE(a)            {0, (a)*1e6, 0}
265
266 extern const struct comedi_lrange range_bipolar10;
267 extern const struct comedi_lrange range_bipolar5;
268 extern const struct comedi_lrange range_bipolar2_5;
269 extern const struct comedi_lrange range_unipolar10;
270 extern const struct comedi_lrange range_unipolar5;
271 extern const struct comedi_lrange range_unipolar2_5;
272 extern const struct comedi_lrange range_0_20mA;
273 extern const struct comedi_lrange range_4_20mA;
274 extern const struct comedi_lrange range_0_32mA;
275 extern const struct comedi_lrange range_unknown;
276
277 #define range_digital           range_unipolar5
278
279 #if __GNUC__ >= 3
280 #define GCC_ZERO_LENGTH_ARRAY
281 #else
282 #define GCC_ZERO_LENGTH_ARRAY 0
283 #endif
284
285 struct comedi_lrange {
286         int length;
287         struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
288 };
289
290 static inline bool comedi_range_is_bipolar(struct comedi_subdevice *s,
291                                            unsigned int range)
292 {
293         return s->range_table->range[range].min < 0;
294 }
295
296 static inline bool comedi_range_is_unipolar(struct comedi_subdevice *s,
297                                             unsigned int range)
298 {
299         return s->range_table->range[range].min >= 0;
300 }
301
302 static inline bool comedi_chan_range_is_bipolar(struct comedi_subdevice *s,
303                                                 unsigned int chan,
304                                                 unsigned int range)
305 {
306         return s->range_table_list[chan]->range[range].min < 0;
307 }
308
309 static inline bool comedi_chan_range_is_unipolar(struct comedi_subdevice *s,
310                                                  unsigned int chan,
311                                                  unsigned int range)
312 {
313         return s->range_table_list[chan]->range[range].min >= 0;
314 }
315
316 /* munge between offset binary and two's complement values */
317 static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s,
318                                                unsigned int val)
319 {
320         return val ^ s->maxdata ^ (s->maxdata >> 1);
321 }
322
323 static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
324 {
325         if (subd->subdev_flags & SDF_LSAMPL)
326                 return sizeof(unsigned int);
327         else
328                 return sizeof(short);
329 }
330
331 /*
332  * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
333  * Also useful for retrieving a previously configured hardware device of
334  * known bus type.  Set automatically for auto-configured devices.
335  * Automatically set to NULL when detaching hardware device.
336  */
337 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev);
338
339 unsigned int comedi_buf_write_alloc(struct comedi_async *, unsigned int);
340 unsigned int comedi_buf_write_free(struct comedi_async *, unsigned int);
341
342 unsigned int comedi_buf_read_n_available(struct comedi_async *);
343 unsigned int comedi_buf_read_alloc(struct comedi_async *, unsigned int);
344 unsigned int comedi_buf_read_free(struct comedi_async *, unsigned int);
345
346 int comedi_buf_put(struct comedi_async *, unsigned short);
347 int comedi_buf_get(struct comedi_async *, unsigned short *);
348
349 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
350                           const void *source, unsigned int num_bytes);
351 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
352                             void *destination, unsigned int num_bytes);
353
354 /* drivers.c - general comedi driver functions */
355
356 #define COMEDI_TIMEOUT_MS       1000
357
358 int comedi_timeout(struct comedi_device *, struct comedi_subdevice *,
359                    struct comedi_insn *,
360                    int (*cb)(struct comedi_device *, struct comedi_subdevice *,
361                              struct comedi_insn *, unsigned long context),
362                    unsigned long context);
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 #define PCI_VENDOR_ID_HUMUSOFT          0x186c
419
420 struct pci_dev;
421 struct pci_driver;
422
423 struct pci_dev *comedi_to_pci_dev(struct comedi_device *);
424
425 int comedi_pci_enable(struct comedi_device *);
426 void comedi_pci_disable(struct comedi_device *);
427
428 int comedi_pci_auto_config(struct pci_dev *, struct comedi_driver *,
429                            unsigned long context);
430 void comedi_pci_auto_unconfig(struct pci_dev *);
431
432 int comedi_pci_driver_register(struct comedi_driver *, struct pci_driver *);
433 void comedi_pci_driver_unregister(struct comedi_driver *, struct pci_driver *);
434
435 /**
436  * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
437  * @__comedi_driver: comedi_driver struct
438  * @__pci_driver: pci_driver struct
439  *
440  * Helper macro for comedi PCI drivers which do not do anything special
441  * in module init/exit. This eliminates a lot of boilerplate. Each
442  * module may only use this macro once, and calling it replaces
443  * module_init() and module_exit()
444  */
445 #define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
446         module_driver(__comedi_driver, comedi_pci_driver_register, \
447                         comedi_pci_driver_unregister, &(__pci_driver))
448
449 #else
450
451 /*
452  * Some of the comedi mixed ISA/PCI drivers call the PCI specific
453  * functions. Provide some dummy functions if CONFIG_COMEDI_PCI_DRIVERS
454  * is not enabled.
455  */
456
457 static inline struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
458 {
459         return NULL;
460 }
461
462 static inline int comedi_pci_enable(struct comedi_device *dev)
463 {
464         return -ENOSYS;
465 }
466
467 static inline void comedi_pci_disable(struct comedi_device *dev)
468 {
469 }
470
471 #endif /* CONFIG_COMEDI_PCI_DRIVERS */
472
473 #ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
474
475 /* comedi_pcmcia.c - comedi PCMCIA driver specific functions */
476
477 struct pcmcia_driver;
478 struct pcmcia_device;
479
480 struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *);
481
482 int comedi_pcmcia_enable(struct comedi_device *,
483                          int (*conf_check)(struct pcmcia_device *, void *));
484 void comedi_pcmcia_disable(struct comedi_device *);
485
486 int comedi_pcmcia_auto_config(struct pcmcia_device *, struct comedi_driver *);
487 void comedi_pcmcia_auto_unconfig(struct pcmcia_device *);
488
489 int comedi_pcmcia_driver_register(struct comedi_driver *,
490                                         struct pcmcia_driver *);
491 void comedi_pcmcia_driver_unregister(struct comedi_driver *,
492                                         struct pcmcia_driver *);
493
494 /**
495  * module_comedi_pcmcia_driver() - Helper macro for registering a comedi PCMCIA driver
496  * @__comedi_driver: comedi_driver struct
497  * @__pcmcia_driver: pcmcia_driver struct
498  *
499  * Helper macro for comedi PCMCIA drivers which do not do anything special
500  * in module init/exit. This eliminates a lot of boilerplate. Each
501  * module may only use this macro once, and calling it replaces
502  * module_init() and module_exit()
503  */
504 #define module_comedi_pcmcia_driver(__comedi_driver, __pcmcia_driver) \
505         module_driver(__comedi_driver, comedi_pcmcia_driver_register, \
506                         comedi_pcmcia_driver_unregister, &(__pcmcia_driver))
507
508 #endif /* CONFIG_COMEDI_PCMCIA_DRIVERS */
509
510 #ifdef CONFIG_COMEDI_USB_DRIVERS
511
512 /* comedi_usb.c - comedi USB driver specific functions */
513
514 struct usb_driver;
515 struct usb_interface;
516
517 struct usb_interface *comedi_to_usb_interface(struct comedi_device *);
518 struct usb_device *comedi_to_usb_dev(struct comedi_device *);
519
520 int comedi_usb_auto_config(struct usb_interface *, struct comedi_driver *,
521                            unsigned long context);
522 void comedi_usb_auto_unconfig(struct usb_interface *);
523
524 int comedi_usb_driver_register(struct comedi_driver *, struct usb_driver *);
525 void comedi_usb_driver_unregister(struct comedi_driver *, struct usb_driver *);
526
527 /**
528  * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
529  * @__comedi_driver: comedi_driver struct
530  * @__usb_driver: usb_driver struct
531  *
532  * Helper macro for comedi USB drivers which do not do anything special
533  * in module init/exit. This eliminates a lot of boilerplate. Each
534  * module may only use this macro once, and calling it replaces
535  * module_init() and module_exit()
536  */
537 #define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
538         module_driver(__comedi_driver, comedi_usb_driver_register, \
539                         comedi_usb_driver_unregister, &(__usb_driver))
540
541 #endif /* CONFIG_COMEDI_USB_DRIVERS */
542
543 #endif /* _COMEDIDEV_H */