blob: 01679458ab630c19c30cfbab396a7978381f92e0 [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
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.
David Schleefed9eccb2008-11-04 20:29:31 -080017*/
18
19#ifndef _COMEDIDEV_H
20#define _COMEDIDEV_H
21
David Schleefed9eccb2008-11-04 20:29:31 -080022#include <linux/dma-mapping.h>
Ian Abbottab3cb2e2013-11-08 15:03:23 +000023#include <linux/mutex.h>
24#include <linux/spinlock_types.h>
Ian Abbott2f3fdcd2013-11-08 15:03:24 +000025#include <linux/rwsem.h>
Ian Abbott5b13ed92013-11-08 15:03:32 +000026#include <linux/kref.h>
David Schleefed9eccb2008-11-04 20:29:31 -080027
28#include "comedi.h"
29
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -080030#define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
Shane Warden62eeae92009-09-22 16:13:04 -070031#define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \
32 COMEDI_MINORVERSION, COMEDI_MICROVERSION)
David Schleefed9eccb2008-11-04 20:29:31 -080033#define COMEDI_RELEASE VERSION
34
David Schleefed9eccb2008-11-04 20:29:31 -080035#define COMEDI_NUM_BOARD_MINORS 0x30
David Schleefed9eccb2008-11-04 20:29:31 -080036
Bill Pemberton34c43922009-03-16 22:05:14 -040037struct comedi_subdevice {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040038 struct comedi_device *device;
H Hartley Sweeten90a35c12012-12-19 17:27:02 -070039 int index;
David Schleefed9eccb2008-11-04 20:29:31 -080040 int type;
41 int n_chan;
Greg Kroah-Hartmanbaf22b62010-04-30 15:26:54 -070042 int subdev_flags;
David Schleefed9eccb2008-11-04 20:29:31 -080043 int len_chanlist; /* maximum length of channel/gain list */
44
45 void *private;
46
Bill Pembertond1636792009-03-16 22:05:20 -040047 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -080048
49 void *lock;
50 void *busy;
51 unsigned runflags;
52 spinlock_t spin_lock;
53
H Hartley Sweetend4a7dc82012-06-18 14:45:42 -070054 unsigned int io_bits;
David Schleefed9eccb2008-11-04 20:29:31 -080055
Bill Pemberton790c5542009-03-16 22:05:02 -040056 unsigned int maxdata; /* if maxdata==0, use list */
57 const unsigned int *maxdata_list; /* list is channel specific */
David Schleefed9eccb2008-11-04 20:29:31 -080058
Bill Pemberton9ced1de2009-03-16 22:05:31 -040059 const struct comedi_lrange *range_table;
60 const struct comedi_lrange *const *range_table_list;
David Schleefed9eccb2008-11-04 20:29:31 -080061
62 unsigned int *chanlist; /* driver-owned chanlist (not used) */
63
Monam Agarwal0cb4c1512014-02-25 14:51:26 +053064 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 *,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053067 struct comedi_insn *, unsigned int *);
Monam Agarwal0cb4c1512014-02-25 14:51:26 +053068 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 *,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053071 struct comedi_insn *, unsigned int *);
David Schleefed9eccb2008-11-04 20:29:31 -080072
Monam Agarwal0cb4c1512014-02-25 14:51:26 +053073 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 *);
Shane Warden62eeae92009-09-22 16:13:04 -070078 /* int (*do_lock)(struct comedi_device *, struct comedi_subdevice *); */
79 /* int (*do_unlock)(struct comedi_device *, \
80 struct comedi_subdevice *); */
David Schleefed9eccb2008-11-04 20:29:31 -080081
82 /* called when the buffer changes */
Monam Agarwal0cb4c1512014-02-25 14:51:26 +053083 int (*buf_change)(struct comedi_device *dev,
84 struct comedi_subdevice *s, unsigned long new_size);
David Schleefed9eccb2008-11-04 20:29:31 -080085
Monam Agarwal0cb4c1512014-02-25 14:51:26 +053086 void (*munge)(struct comedi_device *dev, struct comedi_subdevice *s,
87 void *data, unsigned int num_bytes,
88 unsigned int start_chan_index);
David Schleefed9eccb2008-11-04 20:29:31 -080089 enum dma_data_direction async_dma_dir;
90
91 unsigned int state;
92
Bill Pemberton0bfbbe82009-03-16 22:05:36 -040093 struct device *class_dev;
David Schleefed9eccb2008-11-04 20:29:31 -080094 int minor;
95};
96
97struct comedi_buf_page {
98 void *virt_addr;
99 dma_addr_t dma_addr;
100};
101
Ian Abbottaf93da32013-11-08 15:03:43 +0000102struct 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
Ian Abbott55d128b2014-06-20 14:15:04 +0100110/**
111 * struct comedi_async - control data for asynchronous comedi commands
112 * @prealloc_buf: preallocated buffer
113 * @prealloc_bufsz: buffer size (in bytes)
114 * @buf_map: map of buffer pages
115 * @max_bufsize: maximum buffer size (in bytes)
116 * @buf_write_count: "write completed" count (in bytes, modulo 2**32)
117 * @buf_write_alloc_count: "allocated for writing" count (in bytes,
118 * modulo 2**32)
119 * @buf_read_count: "read completed" count (in bytes, modulo 2**32)
120 * @buf_read_alloc_count: "allocated for reading" count (in bytes,
121 * modulo 2**32)
122 * @buf_write_ptr: buffer position for writer
123 * @buf_read_ptr: buffer position for reader
124 * @cur_chan: current position in chanlist for scan (for those
125 * drivers that use it)
126 * @scan_progress: amount received or sent for current scan (in bytes)
127 * @munge_chan: current position in chanlist for "munging"
128 * @munge_count: "munge" count (in bytes, modulo 2**32)
129 * @munge_ptr: buffer position for "munging"
130 * @events: bit-vector of events that have occurred
131 * @cmd: details of comedi command in progress
132 * @wait_head: task wait queue for file reader or writer
133 * @cb_mask: bit-vector of events that should wake waiting tasks
134 * @inttrig: software trigger function for command, or NULL
135 *
136 * Note about the ..._count and ..._ptr members:
137 *
138 * Think of the _Count values being integers of unlimited size, indexing
139 * into a buffer of infinite length (though only an advancing portion
140 * of the buffer of fixed length prealloc_bufsz is accessible at any time).
141 * Then:
142 *
143 * Buf_Read_Count <= Buf_Read_Alloc_Count <= Munge_Count <=
144 * Buf_Write_Count <= Buf_Write_Alloc_Count <=
145 * (Buf_Read_Count + prealloc_bufsz)
146 *
147 * (Those aren't the actual members, apart from prealloc_bufsz.) When
148 * the buffer is reset, those _Count values start at 0 and only increase
149 * in value, maintaining the above inequalities until the next time the
150 * buffer is reset. The buffer is divided into the following regions by
151 * the inequalities:
152 *
153 * [0, Buf_Read_Count):
154 * old region no longer accessible
155 * [Buf_Read_Count, Buf_Read_Alloc_Count):
156 * filled and munged region allocated for reading but not yet read
157 * [Buf_Read_Alloc_Count, Munge_Count):
158 * filled and munged region not yet allocated for reading
159 * [Munge_Count, Buf_Write_Count):
160 * filled region not yet munged
161 * [Buf_Write_Count, Buf_Write_Alloc_Count):
162 * unfilled region allocated for writing but not yet written
163 * [Buf_Write_Alloc_Count, Buf_Read_Count + prealloc_bufsz):
164 * unfilled region not yet allocated for writing
165 * [Buf_Read_Count + prealloc_bufsz, infinity):
166 * unfilled region not yet accessible
167 *
168 * Data needs to be written into the buffer before it can be read out,
169 * and may need to be converted (or "munged") between the two
170 * operations. Extra unfilled buffer space may need to allocated for
171 * writing (advancing Buf_Write_Alloc_Count) before new data is written.
172 * After writing new data, the newly filled space needs to be released
173 * (advancing Buf_Write_Count). This also results in the new data being
174 * "munged" (advancing Munge_Count). Before data is read out of the
175 * buffer, extra space may need to be allocated for reading (advancing
176 * Buf_Read_Alloc_Count). After the data has been read out, the space
177 * needs to be released (advancing Buf_Read_Count).
178 *
179 * The actual members, buf_read_count, buf_read_alloc_count,
180 * munge_count, buf_write_count, and buf_write_alloc_count take the
181 * value of the corresponding capitalized _Count values modulo 2^32
182 * (UINT_MAX+1). Subtracting a "higher" _count value from a "lower"
183 * _count value gives the same answer as subtracting a "higher" _Count
184 * value from a lower _Count value because prealloc_bufsz < UINT_MAX+1.
185 * The modulo operation is done implicitly.
186 *
187 * The buf_read_ptr, munge_ptr, and buf_write_ptr members take the value
188 * of the corresponding capitalized _Count values modulo prealloc_bufsz.
189 * These correspond to byte indices in the physical buffer. The modulo
190 * operation is done by subtracting prealloc_bufsz when the value
191 * exceeds prealloc_bufsz (assuming prealloc_bufsz plus the increment is
192 * less than or equal to UINT_MAX).
193 */
Bill Pembertond1636792009-03-16 22:05:20 -0400194struct comedi_async {
Ian Abbott55d128b2014-06-20 14:15:04 +0100195 void *prealloc_buf;
196 unsigned int prealloc_bufsz;
197 struct comedi_buf_map *buf_map;
198 unsigned int max_bufsize;
Shane Warden62eeae92009-09-22 16:13:04 -0700199 unsigned int buf_write_count;
Shane Warden62eeae92009-09-22 16:13:04 -0700200 unsigned int buf_write_alloc_count;
Shane Warden62eeae92009-09-22 16:13:04 -0700201 unsigned int buf_read_count;
Shane Warden62eeae92009-09-22 16:13:04 -0700202 unsigned int buf_read_alloc_count;
Ian Abbott55d128b2014-06-20 14:15:04 +0100203 unsigned int buf_write_ptr;
204 unsigned int buf_read_ptr;
205 unsigned int cur_chan;
David Schleefed9eccb2008-11-04 20:29:31 -0800206 unsigned int scan_progress;
David Schleefed9eccb2008-11-04 20:29:31 -0800207 unsigned int munge_chan;
David Schleefed9eccb2008-11-04 20:29:31 -0800208 unsigned int munge_count;
David Schleefed9eccb2008-11-04 20:29:31 -0800209 unsigned int munge_ptr;
Ian Abbott55d128b2014-06-20 14:15:04 +0100210 unsigned int events;
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400211 struct comedi_cmd cmd;
David Schleefed9eccb2008-11-04 20:29:31 -0800212 wait_queue_head_t wait_head;
David Schleefed9eccb2008-11-04 20:29:31 -0800213 unsigned int cb_mask;
Monam Agarwal0cb4c1512014-02-25 14:51:26 +0530214 int (*inttrig)(struct comedi_device *dev, struct comedi_subdevice *s,
215 unsigned int x);
David Schleefed9eccb2008-11-04 20:29:31 -0800216};
217
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400218struct comedi_driver {
219 struct comedi_driver *next;
David Schleefed9eccb2008-11-04 20:29:31 -0800220
221 const char *driver_name;
222 struct module *module;
Monam Agarwal0cb4c1512014-02-25 14:51:26 +0530223 int (*attach)(struct comedi_device *, struct comedi_devconfig *);
224 void (*detach)(struct comedi_device *);
225 int (*auto_attach)(struct comedi_device *, unsigned long);
David Schleefed9eccb2008-11-04 20:29:31 -0800226
227 /* number of elements in board_name and board_id arrays */
228 unsigned int num_names;
229 const char *const *board_name;
230 /* offset in bytes from one board name pointer to the next */
231 int offset;
232};
233
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400234struct comedi_device {
David Schleefed9eccb2008-11-04 20:29:31 -0800235 int use_count;
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400236 struct comedi_driver *driver;
David Schleefed9eccb2008-11-04 20:29:31 -0800237 void *private;
238
Bill Pemberton0bfbbe82009-03-16 22:05:36 -0400239 struct device *class_dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800240 int minor;
Ian Abbottef77c0b2013-11-08 15:03:29 +0000241 unsigned int detach_count;
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800242 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
243 * for subdevices that have async_dma_dir set to something other than
244 * DMA_NONE */
David Schleefed9eccb2008-11-04 20:29:31 -0800245 struct device *hw_dev;
246
247 const char *board_name;
248 const void *board_ptr;
Ian Abbotta7401cd2013-03-15 13:15:33 +0000249 bool attached:1;
Ian Abbott00ca68842013-03-15 13:15:35 +0000250 bool ioenabled:1;
David Schleefed9eccb2008-11-04 20:29:31 -0800251 spinlock_t spinlock;
252 struct mutex mutex;
Ian Abbott2f3fdcd2013-11-08 15:03:24 +0000253 struct rw_semaphore attach_lock;
Ian Abbott5b13ed92013-11-08 15:03:32 +0000254 struct kref refcount;
David Schleefed9eccb2008-11-04 20:29:31 -0800255
256 int n_subdevices;
Bill Pemberton34c43922009-03-16 22:05:14 -0400257 struct comedi_subdevice *subdevices;
David Schleefed9eccb2008-11-04 20:29:31 -0800258
259 /* dumb */
260 unsigned long iobase;
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700261 unsigned long iolen;
David Schleefed9eccb2008-11-04 20:29:31 -0800262 unsigned int irq;
263
Bill Pemberton34c43922009-03-16 22:05:14 -0400264 struct comedi_subdevice *read_subdev;
265 struct comedi_subdevice *write_subdev;
David Schleefed9eccb2008-11-04 20:29:31 -0800266
267 struct fasync_struct *async_queue;
268
Monam Agarwal0cb4c1512014-02-25 14:51:26 +0530269 int (*open)(struct comedi_device *dev);
270 void (*close)(struct comedi_device *dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800271};
272
Ian Abbott4dba6c02012-11-02 19:18:00 +0000273static inline const void *comedi_board(const struct comedi_device *dev)
H Hartley Sweeten37859f82012-04-27 14:10:15 -0700274{
275 return dev->board_ptr;
276}
277
David Schleefed9eccb2008-11-04 20:29:31 -0800278/*
279 * function prototypes
280 */
281
Bill Pemberton34c43922009-03-16 22:05:14 -0400282void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400283void comedi_error(const struct comedi_device *dev, const char *s);
David Schleefed9eccb2008-11-04 20:29:31 -0800284
285/* we can expand the number of bits used to encode devices/subdevices into
286 the minor number soon, after more distros support > 8 bit minor numbers
287 (like after Debian Etch gets released) */
288enum comedi_minor_bits {
289 COMEDI_DEVICE_MINOR_MASK = 0xf,
290 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
291};
292static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
293static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
294
Ian Abbottb449c1c2013-11-08 15:03:33 +0000295struct comedi_device *comedi_dev_get_from_minor(unsigned minor);
296int comedi_dev_put(struct comedi_device *dev);
H Hartley Sweeten85104e92012-12-19 15:34:40 -0700297
David Schleefed9eccb2008-11-04 20:29:31 -0800298void init_polling(void);
299void cleanup_polling(void);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400300void start_polling(struct comedi_device *);
301void stop_polling(struct comedi_device *);
David Schleefed9eccb2008-11-04 20:29:31 -0800302
David Schleefed9eccb2008-11-04 20:29:31 -0800303/* subdevice runflags */
304enum subdevice_runflags {
David Schleefed9eccb2008-11-04 20:29:31 -0800305 SRF_RT = 0x00000002,
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800306 /* indicates an COMEDI_CB_ERROR event has occurred since the last
307 * command was started */
David Schleefed9eccb2008-11-04 20:29:31 -0800308 SRF_ERROR = 0x00000004,
H Hartley Sweeten588ba6d2013-06-11 11:32:29 -0700309 SRF_RUNNING = 0x08000000,
310 SRF_FREE_SPRIV = 0x80000000, /* free s->private on detach */
David Schleefed9eccb2008-11-04 20:29:31 -0800311};
312
H Hartley Sweetene0dac312012-12-19 15:42:47 -0700313bool comedi_is_subdevice_running(struct comedi_subdevice *s);
H Hartley Sweeten0480bcb2013-06-19 15:24:36 -0700314
315void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size);
H Hartley Sweetene0dac312012-12-19 15:42:47 -0700316
Mark Rankilor5e220112010-05-06 17:49:37 +0800317int comedi_check_chanlist(struct comedi_subdevice *s,
318 int n,
319 unsigned int *chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -0800320
321/* range stuff */
322
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800323#define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
324#define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
325#define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
Greg Kroah-Hartmanbaf22b62010-04-30 15:26:54 -0700326#define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800327#define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
328#define UNI_RANGE(a) {0, (a)*1e6, 0}
David Schleefed9eccb2008-11-04 20:29:31 -0800329
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400330extern const struct comedi_lrange range_bipolar10;
331extern const struct comedi_lrange range_bipolar5;
332extern const struct comedi_lrange range_bipolar2_5;
333extern const struct comedi_lrange range_unipolar10;
334extern const struct comedi_lrange range_unipolar5;
H Hartley Sweeten5f8eb722013-04-03 13:38:26 -0700335extern const struct comedi_lrange range_unipolar2_5;
H Hartley Sweeten2c71c4f2013-04-03 13:40:13 -0700336extern const struct comedi_lrange range_0_20mA;
337extern const struct comedi_lrange range_4_20mA;
338extern const struct comedi_lrange range_0_32mA;
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400339extern const struct comedi_lrange range_unknown;
David Schleefed9eccb2008-11-04 20:29:31 -0800340
341#define range_digital range_unipolar5
342
343#if __GNUC__ >= 3
344#define GCC_ZERO_LENGTH_ARRAY
345#else
346#define GCC_ZERO_LENGTH_ARRAY 0
347#endif
348
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400349struct comedi_lrange {
David Schleefed9eccb2008-11-04 20:29:31 -0800350 int length;
Bill Pemberton1f6325d2009-03-16 22:06:31 -0400351 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
David Schleefed9eccb2008-11-04 20:29:31 -0800352};
353
H Hartley Sweetene3693fd2013-06-05 15:52:31 -0700354static inline bool comedi_range_is_bipolar(struct comedi_subdevice *s,
355 unsigned int range)
356{
357 return s->range_table->range[range].min < 0;
358}
359
360static inline bool comedi_range_is_unipolar(struct comedi_subdevice *s,
361 unsigned int range)
362{
363 return s->range_table->range[range].min >= 0;
364}
365
H Hartley Sweeten49162112013-09-25 15:35:06 -0700366static inline bool comedi_chan_range_is_bipolar(struct comedi_subdevice *s,
367 unsigned int chan,
368 unsigned int range)
369{
370 return s->range_table_list[chan]->range[range].min < 0;
371}
372
373static inline bool comedi_chan_range_is_unipolar(struct comedi_subdevice *s,
374 unsigned int chan,
375 unsigned int range)
376{
377 return s->range_table_list[chan]->range[range].min >= 0;
378}
379
H Hartley Sweetenf0b215d2013-09-18 11:49:33 -0700380/* munge between offset binary and two's complement values */
381static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s,
382 unsigned int val)
383{
384 return val ^ s->maxdata ^ (s->maxdata >> 1);
385}
David Schleefed9eccb2008-11-04 20:29:31 -0800386
Bill Pemberton34c43922009-03-16 22:05:14 -0400387static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
David Schleefed9eccb2008-11-04 20:29:31 -0800388{
389 if (subd->subdev_flags & SDF_LSAMPL)
Bill Pemberton790c5542009-03-16 22:05:02 -0400390 return sizeof(unsigned int);
Kinka Huangcb3aada2014-07-15 23:11:02 +0800391
392 return sizeof(short);
David Schleefed9eccb2008-11-04 20:29:31 -0800393}
394
Ian Abbottbc3954b2013-01-29 16:20:17 +0000395/*
396 * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
397 * Also useful for retrieving a previously configured hardware device of
398 * known bus type. Set automatically for auto-configured devices.
399 * Automatically set to NULL when detaching hardware device.
400 */
Ian Abbottda717512013-02-01 13:23:19 +0000401int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800402
H Hartley Sweetenf4f3f7c2014-06-20 10:58:28 -0700403static inline unsigned int comedi_buf_n_bytes_ready(struct comedi_subdevice *s)
404{
405 return s->async->buf_write_count - s->async->buf_read_count;
406}
407
Ian Abbott24e894b2014-05-06 13:12:04 +0100408unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s, unsigned int n);
Ian Abbott940dd352014-05-06 13:12:05 +0100409unsigned int comedi_buf_write_free(struct comedi_subdevice *s, unsigned int n);
David Schleefed9eccb2008-11-04 20:29:31 -0800410
Ian Abbotte9edef32014-05-06 13:12:09 +0100411unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s);
Ian Abbottd13be552014-05-06 13:12:07 +0100412unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s, unsigned int n);
Ian Abbottf1df8662014-05-06 13:12:08 +0100413unsigned int comedi_buf_read_free(struct comedi_subdevice *s, unsigned int n);
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700414
Ian Abbott3672eff2014-05-06 13:11:59 +0100415int comedi_buf_put(struct comedi_subdevice *s, unsigned short x);
Ian Abbott458c13e2014-05-06 13:12:00 +0100416int comedi_buf_get(struct comedi_subdevice *s, unsigned short *x);
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700417
Ian Abbott00603a92014-05-06 13:12:01 +0100418void comedi_buf_memcpy_to(struct comedi_subdevice *s, unsigned int offset,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530419 const void *source, unsigned int num_bytes);
Ian Abbott2fadffc02014-05-06 13:12:02 +0100420void comedi_buf_memcpy_from(struct comedi_subdevice *s, unsigned int offset,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530421 void *destination, unsigned int num_bytes);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530422
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700423/* drivers.c - general comedi driver functions */
424
H Hartley Sweeten91506402014-02-10 11:49:00 -0700425#define COMEDI_TIMEOUT_MS 1000
426
427int comedi_timeout(struct comedi_device *, struct comedi_subdevice *,
428 struct comedi_insn *,
429 int (*cb)(struct comedi_device *, struct comedi_subdevice *,
430 struct comedi_insn *, unsigned long context),
431 unsigned long context);
432
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700433int comedi_dio_insn_config(struct comedi_device *, struct comedi_subdevice *,
434 struct comedi_insn *, unsigned int *data,
435 unsigned int mask);
H Hartley Sweeten05e60b12013-08-30 11:04:56 -0700436unsigned int comedi_dio_update_state(struct comedi_subdevice *,
437 unsigned int *data);
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700438
H Hartley Sweeten54db9962013-06-24 16:55:14 -0700439void *comedi_alloc_devpriv(struct comedi_device *, size_t);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700440int comedi_alloc_subdevices(struct comedi_device *, int);
441
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700442int comedi_load_firmware(struct comedi_device *, struct device *,
443 const char *name,
444 int (*cb)(struct comedi_device *,
H Hartley Sweetend5695412013-05-17 11:18:01 -0700445 const u8 *data, size_t size,
446 unsigned long context),
447 unsigned long context);
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700448
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700449int __comedi_request_region(struct comedi_device *,
450 unsigned long start, unsigned long len);
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700451int comedi_request_region(struct comedi_device *,
452 unsigned long start, unsigned long len);
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700453void comedi_legacy_detach(struct comedi_device *);
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700454
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700455int comedi_auto_config(struct device *, struct comedi_driver *,
456 unsigned long context);
457void comedi_auto_unconfig(struct device *);
458
459int comedi_driver_register(struct comedi_driver *);
Ian Abbott99c0e262013-06-27 14:50:57 +0100460void comedi_driver_unregister(struct comedi_driver *);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700461
462/**
463 * module_comedi_driver() - Helper macro for registering a comedi driver
464 * @__comedi_driver: comedi_driver struct
465 *
466 * Helper macro for comedi drivers which do not do anything special in module
467 * init/exit. This eliminates a lot of boilerplate. Each module may only use
468 * this macro once, and calling it replaces module_init() and module_exit().
469 */
470#define module_comedi_driver(__comedi_driver) \
471 module_driver(__comedi_driver, comedi_driver_register, \
472 comedi_driver_unregister)
Ian Abbott581a7dd2012-11-14 13:10:40 +0000473
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700474#ifdef CONFIG_COMEDI_PCI_DRIVERS
475
476/* comedi_pci.c - comedi PCI driver specific functions */
477
478/*
479 * PCI Vendor IDs not in <linux/pci_ids.h>
480 */
481#define PCI_VENDOR_ID_KOLTER 0x1001
482#define PCI_VENDOR_ID_ICP 0x104c
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700483#define PCI_VENDOR_ID_DT 0x1116
484#define PCI_VENDOR_ID_IOTECH 0x1616
485#define PCI_VENDOR_ID_CONTEC 0x1221
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700486#define PCI_VENDOR_ID_RTD 0x1435
Rostislav Lisovy04b56502014-01-09 23:46:46 +0100487#define PCI_VENDOR_ID_HUMUSOFT 0x186c
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700488
489struct pci_dev;
490struct pci_driver;
491
492struct pci_dev *comedi_to_pci_dev(struct comedi_device *);
493
H Hartley Sweeten818f5692013-03-13 10:36:31 -0700494int comedi_pci_enable(struct comedi_device *);
H Hartley Sweeten7f072f52013-03-13 10:35:51 -0700495void comedi_pci_disable(struct comedi_device *);
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700496
H Hartley Sweetenb8f4ac22013-03-05 09:53:41 -0700497int comedi_pci_auto_config(struct pci_dev *, struct comedi_driver *,
498 unsigned long context);
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700499void comedi_pci_auto_unconfig(struct pci_dev *);
500
501int comedi_pci_driver_register(struct comedi_driver *, struct pci_driver *);
502void comedi_pci_driver_unregister(struct comedi_driver *, struct pci_driver *);
503
504/**
505 * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
506 * @__comedi_driver: comedi_driver struct
507 * @__pci_driver: pci_driver struct
508 *
509 * Helper macro for comedi PCI drivers which do not do anything special
510 * in module init/exit. This eliminates a lot of boilerplate. Each
511 * module may only use this macro once, and calling it replaces
512 * module_init() and module_exit()
513 */
514#define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
515 module_driver(__comedi_driver, comedi_pci_driver_register, \
516 comedi_pci_driver_unregister, &(__pci_driver))
517
518#else
519
520/*
521 * Some of the comedi mixed ISA/PCI drivers call the PCI specific
522 * functions. Provide some dummy functions if CONFIG_COMEDI_PCI_DRIVERS
523 * is not enabled.
524 */
525
526static inline struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
Ian Abbott581a7dd2012-11-14 13:10:40 +0000527{
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700528 return NULL;
Ian Abbott581a7dd2012-11-14 13:10:40 +0000529}
530
H Hartley Sweeten818f5692013-03-13 10:36:31 -0700531static inline int comedi_pci_enable(struct comedi_device *dev)
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700532{
533 return -ENOSYS;
534}
535
H Hartley Sweeten7f072f52013-03-13 10:35:51 -0700536static inline void comedi_pci_disable(struct comedi_device *dev)
H Hartley Sweeten33782dd2013-01-30 15:22:21 -0700537{
538}
539
540#endif /* CONFIG_COMEDI_PCI_DRIVERS */
Ian Abbott581a7dd2012-11-14 13:10:40 +0000541
H Hartley Sweeten309231d2013-01-30 15:22:44 -0700542#ifdef CONFIG_COMEDI_PCMCIA_DRIVERS
543
544/* comedi_pcmcia.c - comedi PCMCIA driver specific functions */
545
546struct pcmcia_driver;
H Hartley Sweeten1f021e1f2013-01-30 15:23:06 -0700547struct pcmcia_device;
548
549struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *);
550
H Hartley Sweetena3ac9512013-02-05 18:16:07 -0700551int comedi_pcmcia_enable(struct comedi_device *,
552 int (*conf_check)(struct pcmcia_device *, void *));
H Hartley Sweetenddb2d0a2013-02-04 14:19:35 -0700553void comedi_pcmcia_disable(struct comedi_device *);
554
H Hartley Sweeten1f021e1f2013-01-30 15:23:06 -0700555int comedi_pcmcia_auto_config(struct pcmcia_device *, struct comedi_driver *);
556void comedi_pcmcia_auto_unconfig(struct pcmcia_device *);
H Hartley Sweeten309231d2013-01-30 15:22:44 -0700557
558int comedi_pcmcia_driver_register(struct comedi_driver *,
559 struct pcmcia_driver *);
560void comedi_pcmcia_driver_unregister(struct comedi_driver *,
561 struct pcmcia_driver *);
562
563/**
564 * module_comedi_pcmcia_driver() - Helper macro for registering a comedi PCMCIA driver
565 * @__comedi_driver: comedi_driver struct
566 * @__pcmcia_driver: pcmcia_driver struct
567 *
568 * Helper macro for comedi PCMCIA drivers which do not do anything special
569 * in module init/exit. This eliminates a lot of boilerplate. Each
570 * module may only use this macro once, and calling it replaces
571 * module_init() and module_exit()
572 */
573#define module_comedi_pcmcia_driver(__comedi_driver, __pcmcia_driver) \
574 module_driver(__comedi_driver, comedi_pcmcia_driver_register, \
575 comedi_pcmcia_driver_unregister, &(__pcmcia_driver))
576
577#endif /* CONFIG_COMEDI_PCMCIA_DRIVERS */
578
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700579#ifdef CONFIG_COMEDI_USB_DRIVERS
Ian Abbott581a7dd2012-11-14 13:10:40 +0000580
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700581/* comedi_usb.c - comedi USB driver specific functions */
582
583struct usb_driver;
584struct usb_interface;
585
586struct usb_interface *comedi_to_usb_interface(struct comedi_device *);
H Hartley Sweeten61dd1492013-05-20 14:20:02 -0700587struct usb_device *comedi_to_usb_dev(struct comedi_device *);
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700588
H Hartley Sweeten55ab4f62013-02-05 17:23:40 -0700589int comedi_usb_auto_config(struct usb_interface *, struct comedi_driver *,
590 unsigned long context);
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700591void comedi_usb_auto_unconfig(struct usb_interface *);
592
593int comedi_usb_driver_register(struct comedi_driver *, struct usb_driver *);
594void comedi_usb_driver_unregister(struct comedi_driver *, struct usb_driver *);
595
596/**
597 * module_comedi_usb_driver() - Helper macro for registering a comedi USB driver
598 * @__comedi_driver: comedi_driver struct
599 * @__usb_driver: usb_driver struct
600 *
601 * Helper macro for comedi USB drivers which do not do anything special
602 * in module init/exit. This eliminates a lot of boilerplate. Each
603 * module may only use this macro once, and calling it replaces
604 * module_init() and module_exit()
605 */
606#define module_comedi_usb_driver(__comedi_driver, __usb_driver) \
607 module_driver(__comedi_driver, comedi_usb_driver_register, \
608 comedi_usb_driver_unregister, &(__usb_driver))
609
610#endif /* CONFIG_COMEDI_USB_DRIVERS */
David Schleefed9eccb2008-11-04 20:29:31 -0800611
David Schleefed9eccb2008-11-04 20:29:31 -0800612#endif /* _COMEDIDEV_H */