Staging: comedi: Misc code cleanups for checkpatch
[safe/jmp/linux-2.6] / 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/errno.h>
32 #include <linux/spinlock.h>
33 #include <linux/mutex.h>
34 #include <linux/wait.h>
35 #include <linux/mm.h>
36 #include <linux/init.h>
37 #include <linux/vmalloc.h>
38 #include "interrupt.h"
39 #include <linux/dma-mapping.h>
40 #include <linux/uaccess.h>
41 #include <linux/io.h>
42
43 #include "comedi.h"
44
45 #define DPRINTK(format, args...)        do {            \
46         if (comedi_debug)                               \
47                 printk(KERN_DEBUG "comedi: " format , ## args); \
48 } while (0)
49
50 #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
51 #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, COMEDI_MINORVERSION, COMEDI_MICROVERSION)
52 #define COMEDI_RELEASE VERSION
53
54 #define COMEDI_INITCLEANUP_NOMODULE(x)                                  \
55         static int __init x ## _init_module(void)                       \
56                 {return comedi_driver_register(&(x)); }                 \
57         static void __exit x ## _cleanup_module(void)                   \
58                 {comedi_driver_unregister(&(x)); }                      \
59         module_init(x ## _init_module);                                 \
60         module_exit(x ## _cleanup_module);                                      \
61
62 #define COMEDI_MODULE_MACROS                                            \
63         MODULE_AUTHOR("Comedi http://www.comedi.org");          \
64         MODULE_DESCRIPTION("Comedi low-level driver");                  \
65         MODULE_LICENSE("GPL");                                          \
66
67 #define COMEDI_INITCLEANUP(x)                                           \
68         COMEDI_MODULE_MACROS            \
69         COMEDI_INITCLEANUP_NOMODULE(x)
70
71 #define COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table) \
72         static int __devinit comedi_driver ## _pci_probe(struct pci_dev *dev, \
73                 const struct pci_device_id *ent) \
74         { \
75                 return comedi_pci_auto_config(dev, comedi_driver.driver_name); \
76         } \
77         static void __devexit comedi_driver ## _pci_remove(struct pci_dev *dev) \
78         { \
79                 comedi_pci_auto_unconfig(dev); \
80         } \
81         static struct pci_driver comedi_driver ## _pci_driver = \
82         { \
83                 .id_table = pci_id_table, \
84                 .probe = &comedi_driver ## _pci_probe, \
85                 .remove = __devexit_p(&comedi_driver ## _pci_remove) \
86         }; \
87         static int __init comedi_driver ## _init_module(void) \
88         { \
89                 int retval; \
90                 retval = comedi_driver_register(&comedi_driver); \
91                 if (retval < 0) \
92                         return retval; \
93                 comedi_driver ## _pci_driver.name = (char *)comedi_driver.driver_name; \
94                 return pci_register_driver(&comedi_driver ## _pci_driver); \
95         } \
96         static void __exit comedi_driver ## _cleanup_module(void) \
97         { \
98                 pci_unregister_driver(&comedi_driver ## _pci_driver); \
99                 comedi_driver_unregister(&comedi_driver); \
100         } \
101         module_init(comedi_driver ## _init_module); \
102         module_exit(comedi_driver ## _cleanup_module);
103
104 #define COMEDI_PCI_INITCLEANUP(comedi_driver, pci_id_table) \
105         COMEDI_MODULE_MACROS \
106         COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table)
107
108 #define PCI_VENDOR_ID_INOVA             0x104c
109 #define PCI_VENDOR_ID_NATINST           0x1093
110 #define PCI_VENDOR_ID_DATX              0x1116
111 #define PCI_VENDOR_ID_COMPUTERBOARDS    0x1307
112 #define PCI_VENDOR_ID_ADVANTECH         0x13fe
113 #define PCI_VENDOR_ID_RTD               0x1435
114 #define PCI_VENDOR_ID_AMPLICON          0x14dc
115 #define PCI_VENDOR_ID_ADLINK            0x144a
116 #define PCI_VENDOR_ID_ICP               0x104c
117 #define PCI_VENDOR_ID_CONTEC            0x1221
118 #define PCI_VENDOR_ID_MEILHAUS          0x1402
119
120 #define COMEDI_NUM_MINORS 0x100
121 #define COMEDI_NUM_BOARD_MINORS 0x30
122 #define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
123
124 typedef struct comedi_device_struct comedi_device;
125 typedef struct comedi_subdevice_struct comedi_subdevice;
126 typedef struct comedi_async_struct comedi_async;
127 typedef struct comedi_driver_struct comedi_driver;
128 typedef struct comedi_lrange_struct comedi_lrange;
129
130 typedef struct device device_create_result_type;
131
132 #define COMEDI_DEVICE_CREATE(cs, parent, devt, drvdata, device, fmt...) \
133         device_create(cs, ((parent) ? (parent) : (device)), devt, drvdata, fmt)
134
135 struct comedi_subdevice_struct {
136         comedi_device *device;
137         int type;
138         int n_chan;
139         volatile int subdev_flags;
140         int len_chanlist;       /* maximum length of channel/gain list */
141
142         void *private;
143
144         comedi_async *async;
145
146         void *lock;
147         void *busy;
148         unsigned runflags;
149         spinlock_t spin_lock;
150
151         int io_bits;
152
153         lsampl_t maxdata;       /* if maxdata==0, use list */
154         const lsampl_t *maxdata_list;   /* list is channel specific */
155
156         unsigned int flags;
157         const unsigned int *flaglist;
158
159         unsigned int settling_time_0;
160
161         const comedi_lrange *range_table;
162         const comedi_lrange *const *range_table_list;
163
164         unsigned int *chanlist; /* driver-owned chanlist (not used) */
165
166         int (*insn_read) (comedi_device *, comedi_subdevice *, comedi_insn *,
167                 lsampl_t *);
168         int (*insn_write) (comedi_device *, comedi_subdevice *, comedi_insn *,
169                 lsampl_t *);
170         int (*insn_bits) (comedi_device *, comedi_subdevice *, comedi_insn *,
171                 lsampl_t *);
172         int (*insn_config) (comedi_device *, comedi_subdevice *, comedi_insn *,
173                 lsampl_t *);
174
175         int (*do_cmd) (comedi_device *, comedi_subdevice *);
176         int (*do_cmdtest) (comedi_device *, comedi_subdevice *, comedi_cmd *);
177         int (*poll) (comedi_device *, comedi_subdevice *);
178         int (*cancel) (comedi_device *, comedi_subdevice *);
179         /* int (*do_lock)(comedi_device *,comedi_subdevice *); */
180         /* int (*do_unlock)(comedi_device *,comedi_subdevice *); */
181
182         /* called when the buffer changes */
183         int (*buf_change) (comedi_device *dev, comedi_subdevice *s,
184                 unsigned long new_size);
185
186         void (*munge) (comedi_device *dev, comedi_subdevice *s, void *data,
187                 unsigned int num_bytes, unsigned int start_chan_index);
188         enum dma_data_direction async_dma_dir;
189
190         unsigned int state;
191
192         device_create_result_type *class_dev;
193         int minor;
194 };
195
196 struct comedi_buf_page {
197         void *virt_addr;
198         dma_addr_t dma_addr;
199 };
200
201 struct comedi_async_struct {
202         comedi_subdevice *subdevice;
203
204         void *prealloc_buf;     /* pre-allocated buffer */
205         unsigned int prealloc_bufsz;    /* buffer size, in bytes */
206         struct comedi_buf_page *buf_page_list;  /* virtual and dma address of each page */
207         unsigned n_buf_pages;   /* num elements in buf_page_list */
208
209         unsigned int max_bufsize;       /* maximum buffer size, bytes */
210         unsigned int mmap_count;        /* current number of mmaps of prealloc_buf */
211
212         unsigned int buf_write_count;   /* byte count for writer (write completed) */
213         unsigned int buf_write_alloc_count;     /* byte count for writer (allocated for writing) */
214         unsigned int buf_read_count;    /* byte count for reader (read completed) */
215         unsigned int buf_read_alloc_count;      /* byte count for reader (allocated for reading) */
216
217         unsigned int buf_write_ptr;     /* buffer marker for writer */
218         unsigned int buf_read_ptr;      /* buffer marker for reader */
219
220         unsigned int cur_chan;  /* useless channel marker for interrupt */
221         /* number of bytes that have been received for current scan */
222         unsigned int scan_progress;
223         /* keeps track of where we are in chanlist as for munging */
224         unsigned int munge_chan;
225         /* number of bytes that have been munged */
226         unsigned int munge_count;
227         /* buffer marker for munging */
228         unsigned int munge_ptr;
229
230         unsigned int events;    /* events that have occurred */
231
232         comedi_cmd cmd;
233
234         wait_queue_head_t wait_head;
235
236         /* callback stuff */
237         unsigned int cb_mask;
238         int (*cb_func) (unsigned int flags, void *);
239         void *cb_arg;
240
241         int (*inttrig) (comedi_device *dev, comedi_subdevice *s,
242                         unsigned int x);
243 };
244
245 struct comedi_driver_struct {
246         struct comedi_driver_struct *next;
247
248         const char *driver_name;
249         struct module *module;
250         int (*attach) (comedi_device *, comedi_devconfig *);
251         int (*detach) (comedi_device *);
252
253         /* number of elements in board_name and board_id arrays */
254         unsigned int num_names;
255         const char *const *board_name;
256         /* offset in bytes from one board name pointer to the next */
257         int offset;
258 };
259
260 struct comedi_device_struct {
261         int use_count;
262         comedi_driver *driver;
263         void *private;
264
265         device_create_result_type *class_dev;
266         int minor;
267         /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
268          * for subdevices that have async_dma_dir set to something other than
269          * DMA_NONE */
270         struct device *hw_dev;
271
272         const char *board_name;
273         const void *board_ptr;
274         int attached;
275         int rt;
276         spinlock_t spinlock;
277         struct mutex mutex;
278         int in_request_module;
279
280         int n_subdevices;
281         comedi_subdevice *subdevices;
282
283         /* dumb */
284         unsigned long iobase;
285         unsigned int irq;
286
287         comedi_subdevice *read_subdev;
288         comedi_subdevice *write_subdev;
289
290         struct fasync_struct *async_queue;
291
292         void (*open) (comedi_device *dev);
293         void (*close) (comedi_device *dev);
294 };
295
296 struct comedi_device_file_info {
297         comedi_device *device;
298         comedi_subdevice *read_subdevice;
299         comedi_subdevice *write_subdevice;
300 };
301
302 #ifdef CONFIG_COMEDI_DEBUG
303 extern int comedi_debug;
304 #else
305 static const int comedi_debug;
306 #endif
307
308 /*
309  * function prototypes
310  */
311
312 void comedi_event(comedi_device *dev, comedi_subdevice *s);
313 void comedi_error(const comedi_device *dev, const char *s);
314
315 /* we can expand the number of bits used to encode devices/subdevices into
316  the minor number soon, after more distros support > 8 bit minor numbers
317  (like after Debian Etch gets released) */
318 enum comedi_minor_bits {
319         COMEDI_DEVICE_MINOR_MASK = 0xf,
320         COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
321 };
322 static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
323 static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
324
325 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor);
326
327 static inline comedi_subdevice *comedi_get_read_subdevice(
328                                 const struct comedi_device_file_info *info)
329 {
330         if (info->read_subdevice)
331                 return info->read_subdevice;
332         if (info->device == NULL)
333                 return NULL;
334         return info->device->read_subdev;
335 }
336
337 static inline comedi_subdevice *comedi_get_write_subdevice(
338                                 const struct comedi_device_file_info *info)
339 {
340         if (info->write_subdevice)
341                 return info->write_subdevice;
342         if (info->device == NULL)
343                 return NULL;
344         return info->device->write_subdev;
345 }
346
347 void comedi_device_detach(comedi_device *dev);
348 int comedi_device_attach(comedi_device *dev, comedi_devconfig *it);
349 int comedi_driver_register(comedi_driver *);
350 int comedi_driver_unregister(comedi_driver *);
351
352 void init_polling(void);
353 void cleanup_polling(void);
354 void start_polling(comedi_device *);
355 void stop_polling(comedi_device *);
356
357 int comedi_buf_alloc(comedi_device *dev, comedi_subdevice *s, unsigned long
358         new_size);
359
360 #ifdef CONFIG_PROC_FS
361 void comedi_proc_init(void);
362 void comedi_proc_cleanup(void);
363 #else
364 static inline void comedi_proc_init(void)
365 {
366 }
367 static inline void comedi_proc_cleanup(void)
368 {
369 }
370 #endif
371
372 /* subdevice runflags */
373 enum subdevice_runflags {
374         SRF_USER = 0x00000001,
375         SRF_RT = 0x00000002,
376         /* indicates an COMEDI_CB_ERROR event has occurred since the last
377          * command was started */
378         SRF_ERROR = 0x00000004,
379         SRF_RUNNING = 0x08000000
380 };
381
382 /*
383    various internal comedi functions
384  */
385
386 int do_rangeinfo_ioctl(comedi_device *dev, comedi_rangeinfo *arg);
387 int check_chanlist(comedi_subdevice *s, int n, unsigned int *chanlist);
388 void comedi_set_subdevice_runflags(comedi_subdevice *s, unsigned mask,
389         unsigned bits);
390 unsigned comedi_get_subdevice_runflags(comedi_subdevice *s);
391 int insn_inval(comedi_device *dev, comedi_subdevice *s,
392         comedi_insn *insn, lsampl_t *data);
393
394 /* range stuff */
395
396 #define RANGE(a, b)             {(a)*1e6, (b)*1e6, 0}
397 #define RANGE_ext(a, b)         {(a)*1e6, (b)*1e6, RF_EXTERNAL}
398 #define RANGE_mA(a, b)          {(a)*1e6, (b)*1e6, UNIT_mA}
399 #define RANGE_unitless(a, b)    {(a)*1e6, (b)*1e6, 0}   /* XXX */
400 #define BIP_RANGE(a)            {-(a)*1e6, (a)*1e6, 0}
401 #define UNI_RANGE(a)            {0, (a)*1e6, 0}
402
403 extern const comedi_lrange range_bipolar10;
404 extern const comedi_lrange range_bipolar5;
405 extern const comedi_lrange range_bipolar2_5;
406 extern const comedi_lrange range_unipolar10;
407 extern const comedi_lrange range_unipolar5;
408 extern const comedi_lrange range_unknown;
409
410 #define range_digital           range_unipolar5
411
412 #if __GNUC__ >= 3
413 #define GCC_ZERO_LENGTH_ARRAY
414 #else
415 #define GCC_ZERO_LENGTH_ARRAY 0
416 #endif
417
418 struct comedi_lrange_struct {
419         int length;
420         comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
421 };
422
423 /* some silly little inline functions */
424
425 static inline int alloc_subdevices(comedi_device *dev,
426                                    unsigned int num_subdevices)
427 {
428         unsigned i;
429
430         dev->n_subdevices = num_subdevices;
431         dev->subdevices =
432                 kcalloc(num_subdevices, sizeof(comedi_subdevice), GFP_KERNEL);
433         if (!dev->subdevices)
434                 return -ENOMEM;
435         for (i = 0; i < num_subdevices; ++i) {
436                 dev->subdevices[i].device = dev;
437                 dev->subdevices[i].async_dma_dir = DMA_NONE;
438                 spin_lock_init(&dev->subdevices[i].spin_lock);
439                 dev->subdevices[i].minor = -1;
440         }
441         return 0;
442 }
443
444 static inline int alloc_private(comedi_device *dev, int size)
445 {
446         dev->private = kzalloc(size, GFP_KERNEL);
447         if (!dev->private)
448                 return -ENOMEM;
449         return 0;
450 }
451
452 static inline unsigned int bytes_per_sample(const comedi_subdevice *subd)
453 {
454         if (subd->subdev_flags & SDF_LSAMPL)
455                 return sizeof(lsampl_t);
456         else
457                 return sizeof(sampl_t);
458 }
459
460 /* must be used in attach to set dev->hw_dev if you wish to dma directly
461 into comedi's buffer */
462 static inline void comedi_set_hw_dev(comedi_device *dev, struct device *hw_dev)
463 {
464         if (dev->hw_dev)
465                 put_device(dev->hw_dev);
466
467         dev->hw_dev = hw_dev;
468         if (dev->hw_dev) {
469                 dev->hw_dev = get_device(dev->hw_dev);
470                 BUG_ON(dev->hw_dev == NULL);
471         }
472 }
473
474 int comedi_buf_put(comedi_async *async, sampl_t x);
475 int comedi_buf_get(comedi_async *async, sampl_t *x);
476
477 unsigned int comedi_buf_write_n_available(comedi_async *async);
478 unsigned int comedi_buf_write_alloc(comedi_async *async, unsigned int nbytes);
479 unsigned int comedi_buf_write_alloc_strict(comedi_async *async,
480         unsigned int nbytes);
481 unsigned comedi_buf_write_free(comedi_async *async, unsigned int nbytes);
482 unsigned comedi_buf_read_alloc(comedi_async *async, unsigned nbytes);
483 unsigned comedi_buf_read_free(comedi_async *async, unsigned int nbytes);
484 unsigned int comedi_buf_read_n_available(comedi_async *async);
485 void comedi_buf_memcpy_to(comedi_async *async, unsigned int offset,
486         const void *source, unsigned int num_bytes);
487 void comedi_buf_memcpy_from(comedi_async *async, unsigned int offset,
488         void *destination, unsigned int num_bytes);
489 static inline unsigned comedi_buf_write_n_allocated(comedi_async *async)
490 {
491         return async->buf_write_alloc_count - async->buf_write_count;
492 }
493 static inline unsigned comedi_buf_read_n_allocated(comedi_async *async)
494 {
495         return async->buf_read_alloc_count - async->buf_read_count;
496 }
497
498 void comedi_reset_async_buf(comedi_async *async);
499
500 static inline void *comedi_aux_data(int options[], int n)
501 {
502         unsigned long address;
503         unsigned long addressLow;
504         int bit_shift;
505         if (sizeof(int) >= sizeof(void *))
506                 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
507         else {
508                 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
509                 bit_shift = sizeof(int) * 8;
510                 address <<= bit_shift;
511                 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
512                 addressLow &= (1UL << bit_shift) - 1;
513                 address |= addressLow;
514         }
515         if (n >= 1)
516                 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
517         if (n >= 2)
518                 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
519         if (n >= 3)
520                 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
521         BUG_ON(n > 3);
522         return (void *)address;
523 }
524
525 int comedi_alloc_board_minor(struct device *hardware_device);
526 void comedi_free_board_minor(unsigned minor);
527 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s);
528 void comedi_free_subdevice_minor(comedi_subdevice *s);
529 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name);
530 void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
531 struct usb_device;      /* forward declaration */
532 int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name);
533 void comedi_usb_auto_unconfig(struct usb_device *usbdev);
534
535 #include "comedi_rt.h"
536
537 #endif /* _COMEDIDEV_H */