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