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