blob: dfab5a84b011f50215d626a92d2a31ebc877b835 [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 *);
David Schleefed9eccb2008-11-04 20:29:31 -080078
79 /* called when the buffer changes */
H Hartley Sweetend546b892014-07-21 11:48:32 -070080 int (*buf_change)(struct comedi_device *, struct comedi_subdevice *);
David Schleefed9eccb2008-11-04 20:29:31 -080081
Monam Agarwal0cb4c1512014-02-25 14:51:26 +053082 void (*munge)(struct comedi_device *dev, struct comedi_subdevice *s,
83 void *data, unsigned int num_bytes,
84 unsigned int start_chan_index);
David Schleefed9eccb2008-11-04 20:29:31 -080085 enum dma_data_direction async_dma_dir;
86
87 unsigned int state;
88
Bill Pemberton0bfbbe82009-03-16 22:05:36 -040089 struct device *class_dev;
David Schleefed9eccb2008-11-04 20:29:31 -080090 int minor;
H Hartley Sweetend2762062014-08-25 16:03:54 -070091
92 unsigned int *readback;
David Schleefed9eccb2008-11-04 20:29:31 -080093};
94
95struct comedi_buf_page {
96 void *virt_addr;
97 dma_addr_t dma_addr;
98};
99
Ian Abbottaf93da32013-11-08 15:03:43 +0000100struct comedi_buf_map {
101 struct device *dma_hw_dev;
102 struct comedi_buf_page *page_list;
103 unsigned int n_pages;
104 enum dma_data_direction dma_dir;
105 struct kref refcount;
106};
107
Ian Abbott55d128b2014-06-20 14:15:04 +0100108/**
109 * struct comedi_async - control data for asynchronous comedi commands
110 * @prealloc_buf: preallocated buffer
111 * @prealloc_bufsz: buffer size (in bytes)
112 * @buf_map: map of buffer pages
113 * @max_bufsize: maximum buffer size (in bytes)
114 * @buf_write_count: "write completed" count (in bytes, modulo 2**32)
115 * @buf_write_alloc_count: "allocated for writing" count (in bytes,
116 * modulo 2**32)
117 * @buf_read_count: "read completed" count (in bytes, modulo 2**32)
118 * @buf_read_alloc_count: "allocated for reading" count (in bytes,
119 * modulo 2**32)
120 * @buf_write_ptr: buffer position for writer
121 * @buf_read_ptr: buffer position for reader
122 * @cur_chan: current position in chanlist for scan (for those
123 * drivers that use it)
H Hartley Sweeten1dacbe52014-11-05 10:20:52 -0700124 * @scans_done: the number of scans completed (COMEDI_CB_EOS)
Ian Abbott55d128b2014-06-20 14:15:04 +0100125 * @scan_progress: amount received or sent for current scan (in bytes)
126 * @munge_chan: current position in chanlist for "munging"
127 * @munge_count: "munge" count (in bytes, modulo 2**32)
128 * @munge_ptr: buffer position for "munging"
129 * @events: bit-vector of events that have occurred
130 * @cmd: details of comedi command in progress
131 * @wait_head: task wait queue for file reader or writer
132 * @cb_mask: bit-vector of events that should wake waiting tasks
133 * @inttrig: software trigger function for command, or NULL
134 *
135 * Note about the ..._count and ..._ptr members:
136 *
137 * Think of the _Count values being integers of unlimited size, indexing
138 * into a buffer of infinite length (though only an advancing portion
139 * of the buffer of fixed length prealloc_bufsz is accessible at any time).
140 * Then:
141 *
142 * Buf_Read_Count <= Buf_Read_Alloc_Count <= Munge_Count <=
143 * Buf_Write_Count <= Buf_Write_Alloc_Count <=
144 * (Buf_Read_Count + prealloc_bufsz)
145 *
146 * (Those aren't the actual members, apart from prealloc_bufsz.) When
147 * the buffer is reset, those _Count values start at 0 and only increase
148 * in value, maintaining the above inequalities until the next time the
149 * buffer is reset. The buffer is divided into the following regions by
150 * the inequalities:
151 *
152 * [0, Buf_Read_Count):
153 * old region no longer accessible
154 * [Buf_Read_Count, Buf_Read_Alloc_Count):
155 * filled and munged region allocated for reading but not yet read
156 * [Buf_Read_Alloc_Count, Munge_Count):
157 * filled and munged region not yet allocated for reading
158 * [Munge_Count, Buf_Write_Count):
159 * filled region not yet munged
160 * [Buf_Write_Count, Buf_Write_Alloc_Count):
161 * unfilled region allocated for writing but not yet written
162 * [Buf_Write_Alloc_Count, Buf_Read_Count + prealloc_bufsz):
163 * unfilled region not yet allocated for writing
164 * [Buf_Read_Count + prealloc_bufsz, infinity):
165 * unfilled region not yet accessible
166 *
167 * Data needs to be written into the buffer before it can be read out,
168 * and may need to be converted (or "munged") between the two
169 * operations. Extra unfilled buffer space may need to allocated for
170 * writing (advancing Buf_Write_Alloc_Count) before new data is written.
171 * After writing new data, the newly filled space needs to be released
172 * (advancing Buf_Write_Count). This also results in the new data being
173 * "munged" (advancing Munge_Count). Before data is read out of the
174 * buffer, extra space may need to be allocated for reading (advancing
175 * Buf_Read_Alloc_Count). After the data has been read out, the space
176 * needs to be released (advancing Buf_Read_Count).
177 *
178 * The actual members, buf_read_count, buf_read_alloc_count,
179 * munge_count, buf_write_count, and buf_write_alloc_count take the
180 * value of the corresponding capitalized _Count values modulo 2^32
181 * (UINT_MAX+1). Subtracting a "higher" _count value from a "lower"
182 * _count value gives the same answer as subtracting a "higher" _Count
183 * value from a lower _Count value because prealloc_bufsz < UINT_MAX+1.
184 * The modulo operation is done implicitly.
185 *
186 * The buf_read_ptr, munge_ptr, and buf_write_ptr members take the value
187 * of the corresponding capitalized _Count values modulo prealloc_bufsz.
188 * These correspond to byte indices in the physical buffer. The modulo
189 * operation is done by subtracting prealloc_bufsz when the value
190 * exceeds prealloc_bufsz (assuming prealloc_bufsz plus the increment is
191 * less than or equal to UINT_MAX).
192 */
Bill Pembertond1636792009-03-16 22:05:20 -0400193struct comedi_async {
Ian Abbott55d128b2014-06-20 14:15:04 +0100194 void *prealloc_buf;
195 unsigned int prealloc_bufsz;
196 struct comedi_buf_map *buf_map;
197 unsigned int max_bufsize;
Shane Warden62eeae92009-09-22 16:13:04 -0700198 unsigned int buf_write_count;
Shane Warden62eeae92009-09-22 16:13:04 -0700199 unsigned int buf_write_alloc_count;
Shane Warden62eeae92009-09-22 16:13:04 -0700200 unsigned int buf_read_count;
Shane Warden62eeae92009-09-22 16:13:04 -0700201 unsigned int buf_read_alloc_count;
Ian Abbott55d128b2014-06-20 14:15:04 +0100202 unsigned int buf_write_ptr;
203 unsigned int buf_read_ptr;
204 unsigned int cur_chan;
H Hartley Sweeten1dacbe52014-11-05 10:20:52 -0700205 unsigned int scans_done;
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
H Hartley Sweetenbb856b62014-10-13 09:56:07 -0700218/**
219 * comedi_async callback "events"
220 * @COMEDI_CB_EOS: end-of-scan
221 * @COMEDI_CB_EOA: end-of-acquisition/output
222 * @COMEDI_CB_BLOCK: data has arrived, wakes up read() / write()
223 * @COMEDI_CB_EOBUF: DEPRECATED: end of buffer
224 * @COMEDI_CB_ERROR: card error during acquisition
225 * @COMEDI_CB_OVERFLOW: buffer overflow/underflow
H Hartley Sweeten781f9332014-10-13 09:56:08 -0700226 *
227 * @COMEDI_CB_ERROR_MASK: events that indicate an error has occurred
228 * @COMEDI_CB_CANCEL_MASK: events that will cancel an async command
H Hartley Sweetenbb856b62014-10-13 09:56:07 -0700229 */
230#define COMEDI_CB_EOS (1 << 0)
231#define COMEDI_CB_EOA (1 << 1)
232#define COMEDI_CB_BLOCK (1 << 2)
233#define COMEDI_CB_EOBUF (1 << 3)
234#define COMEDI_CB_ERROR (1 << 4)
235#define COMEDI_CB_OVERFLOW (1 << 5)
236
H Hartley Sweeten781f9332014-10-13 09:56:08 -0700237#define COMEDI_CB_ERROR_MASK (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)
238#define COMEDI_CB_CANCEL_MASK (COMEDI_CB_EOA | COMEDI_CB_ERROR_MASK)
239
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400240struct comedi_driver {
241 struct comedi_driver *next;
David Schleefed9eccb2008-11-04 20:29:31 -0800242
243 const char *driver_name;
244 struct module *module;
Monam Agarwal0cb4c1512014-02-25 14:51:26 +0530245 int (*attach)(struct comedi_device *, struct comedi_devconfig *);
246 void (*detach)(struct comedi_device *);
247 int (*auto_attach)(struct comedi_device *, unsigned long);
David Schleefed9eccb2008-11-04 20:29:31 -0800248
249 /* number of elements in board_name and board_id arrays */
250 unsigned int num_names;
251 const char *const *board_name;
252 /* offset in bytes from one board name pointer to the next */
253 int offset;
254};
255
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400256struct comedi_device {
David Schleefed9eccb2008-11-04 20:29:31 -0800257 int use_count;
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400258 struct comedi_driver *driver;
H Hartley Sweeten43db0622015-02-23 14:57:29 -0700259 struct comedi_8254 *pacer;
David Schleefed9eccb2008-11-04 20:29:31 -0800260 void *private;
261
Bill Pemberton0bfbbe82009-03-16 22:05:36 -0400262 struct device *class_dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800263 int minor;
Ian Abbottef77c0b2013-11-08 15:03:29 +0000264 unsigned int detach_count;
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800265 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
266 * for subdevices that have async_dma_dir set to something other than
267 * DMA_NONE */
David Schleefed9eccb2008-11-04 20:29:31 -0800268 struct device *hw_dev;
269
270 const char *board_name;
271 const void *board_ptr;
Ian Abbotta7401cd2013-03-15 13:15:33 +0000272 bool attached:1;
Ian Abbott00ca68842013-03-15 13:15:35 +0000273 bool ioenabled:1;
David Schleefed9eccb2008-11-04 20:29:31 -0800274 spinlock_t spinlock;
275 struct mutex mutex;
Ian Abbott2f3fdcd2013-11-08 15:03:24 +0000276 struct rw_semaphore attach_lock;
Ian Abbott5b13ed92013-11-08 15:03:32 +0000277 struct kref refcount;
David Schleefed9eccb2008-11-04 20:29:31 -0800278
279 int n_subdevices;
Bill Pemberton34c43922009-03-16 22:05:14 -0400280 struct comedi_subdevice *subdevices;
David Schleefed9eccb2008-11-04 20:29:31 -0800281
282 /* dumb */
H Hartley Sweetend7e6dc12014-07-29 15:01:20 -0700283 void __iomem *mmio;
David Schleefed9eccb2008-11-04 20:29:31 -0800284 unsigned long iobase;
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700285 unsigned long iolen;
David Schleefed9eccb2008-11-04 20:29:31 -0800286 unsigned int irq;
287
Bill Pemberton34c43922009-03-16 22:05:14 -0400288 struct comedi_subdevice *read_subdev;
289 struct comedi_subdevice *write_subdev;
David Schleefed9eccb2008-11-04 20:29:31 -0800290
291 struct fasync_struct *async_queue;
292
Monam Agarwal0cb4c1512014-02-25 14:51:26 +0530293 int (*open)(struct comedi_device *dev);
294 void (*close)(struct comedi_device *dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800295};
296
David Schleefed9eccb2008-11-04 20:29:31 -0800297/*
298 * function prototypes
299 */
300
Bill Pemberton34c43922009-03-16 22:05:14 -0400301void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
David Schleefed9eccb2008-11-04 20:29:31 -0800302
Ian Abbottb449c1c2013-11-08 15:03:33 +0000303struct comedi_device *comedi_dev_get_from_minor(unsigned minor);
304int comedi_dev_put(struct comedi_device *dev);
H Hartley Sweeten85104e92012-12-19 15:34:40 -0700305
H Hartley Sweeten84bb0bc2015-01-20 12:06:00 -0700306/**
307 * comedi_subdevice "runflags"
308 * @COMEDI_SRF_RT: DEPRECATED: command is running real-time
309 * @COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
310 * since the last command was started
311 * @COMEDI_SRF_RUNNING: command is running
312 * @COMEDI_SRF_FREE_SPRIV: free s->private on detach
313 *
314 * @COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
315 */
316#define COMEDI_SRF_RT BIT(1)
317#define COMEDI_SRF_ERROR BIT(2)
318#define COMEDI_SRF_RUNNING BIT(27)
319#define COMEDI_SRF_FREE_SPRIV BIT(31)
320
321#define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
David Schleefed9eccb2008-11-04 20:29:31 -0800322
H Hartley Sweetene0dac312012-12-19 15:42:47 -0700323bool comedi_is_subdevice_running(struct comedi_subdevice *s);
H Hartley Sweeten0480bcb2013-06-19 15:24:36 -0700324
325void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size);
H Hartley Sweetene0dac312012-12-19 15:42:47 -0700326
Mark Rankilor5e220112010-05-06 17:49:37 +0800327int comedi_check_chanlist(struct comedi_subdevice *s,
328 int n,
329 unsigned int *chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -0800330
331/* range stuff */
332
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800333#define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
334#define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
335#define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
Greg Kroah-Hartmanbaf22b62010-04-30 15:26:54 -0700336#define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0}
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800337#define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
338#define UNI_RANGE(a) {0, (a)*1e6, 0}
David Schleefed9eccb2008-11-04 20:29:31 -0800339
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400340extern const struct comedi_lrange range_bipolar10;
341extern const struct comedi_lrange range_bipolar5;
342extern const struct comedi_lrange range_bipolar2_5;
343extern const struct comedi_lrange range_unipolar10;
344extern const struct comedi_lrange range_unipolar5;
H Hartley Sweeten5f8eb722013-04-03 13:38:26 -0700345extern const struct comedi_lrange range_unipolar2_5;
H Hartley Sweeten2c71c4f2013-04-03 13:40:13 -0700346extern const struct comedi_lrange range_0_20mA;
347extern const struct comedi_lrange range_4_20mA;
348extern const struct comedi_lrange range_0_32mA;
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400349extern const struct comedi_lrange range_unknown;
David Schleefed9eccb2008-11-04 20:29:31 -0800350
351#define range_digital range_unipolar5
352
353#if __GNUC__ >= 3
354#define GCC_ZERO_LENGTH_ARRAY
355#else
356#define GCC_ZERO_LENGTH_ARRAY 0
357#endif
358
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400359struct comedi_lrange {
David Schleefed9eccb2008-11-04 20:29:31 -0800360 int length;
Bill Pemberton1f6325d2009-03-16 22:06:31 -0400361 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
David Schleefed9eccb2008-11-04 20:29:31 -0800362};
363
H Hartley Sweetene3693fd2013-06-05 15:52:31 -0700364static inline bool comedi_range_is_bipolar(struct comedi_subdevice *s,
365 unsigned int range)
366{
367 return s->range_table->range[range].min < 0;
368}
369
370static inline bool comedi_range_is_unipolar(struct comedi_subdevice *s,
371 unsigned int range)
372{
373 return s->range_table->range[range].min >= 0;
374}
375
H Hartley Sweetena8b677c2014-07-14 12:23:39 -0700376static inline bool comedi_range_is_external(struct comedi_subdevice *s,
377 unsigned int range)
378{
379 return !!(s->range_table->range[range].flags & RF_EXTERNAL);
380}
381
H Hartley Sweeten49162112013-09-25 15:35:06 -0700382static inline bool comedi_chan_range_is_bipolar(struct comedi_subdevice *s,
383 unsigned int chan,
384 unsigned int range)
385{
386 return s->range_table_list[chan]->range[range].min < 0;
387}
388
389static inline bool comedi_chan_range_is_unipolar(struct comedi_subdevice *s,
390 unsigned int chan,
391 unsigned int range)
392{
393 return s->range_table_list[chan]->range[range].min >= 0;
394}
395
H Hartley Sweetena8b677c2014-07-14 12:23:39 -0700396static inline bool comedi_chan_range_is_external(struct comedi_subdevice *s,
397 unsigned int chan,
398 unsigned int range)
399{
400 return !!(s->range_table_list[chan]->range[range].flags & RF_EXTERNAL);
401}
402
H Hartley Sweetenf0b215d2013-09-18 11:49:33 -0700403/* munge between offset binary and two's complement values */
404static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s,
405 unsigned int val)
406{
407 return val ^ s->maxdata ^ (s->maxdata >> 1);
408}
David Schleefed9eccb2008-11-04 20:29:31 -0800409
Ian Abbottbf33eb42014-10-23 13:47:51 +0100410/**
411 * comedi_bytes_per_sample - determine subdevice sample size
412 * @s: comedi_subdevice struct
413 *
414 * The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on
415 * whether the SDF_LSAMPL subdevice flag is set or not.
416 *
417 * Returns the subdevice sample size.
418 */
419static inline unsigned int comedi_bytes_per_sample(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -0800420{
Ian Abbottbf33eb42014-10-23 13:47:51 +0100421 return s->subdev_flags & SDF_LSAMPL ? sizeof(int) : sizeof(short);
422}
Kinka Huangcb3aada2014-07-15 23:11:02 +0800423
Ian Abbottbf33eb42014-10-23 13:47:51 +0100424/**
425 * comedi_sample_shift - determine log2 of subdevice sample size
426 * @s: comedi_subdevice struct
427 *
428 * The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on
429 * whether the SDF_LSAMPL subdevice flag is set or not. The log2 of the
430 * sample size will be 2 or 1 and can be used as the right operand of a
431 * bit-shift operator to multiply or divide something by the sample size.
432 *
433 * Returns log2 of the subdevice sample size.
434 */
435static inline unsigned int comedi_sample_shift(struct comedi_subdevice *s)
436{
437 return s->subdev_flags & SDF_LSAMPL ? 2 : 1;
438}
439
440/**
441 * comedi_bytes_to_samples - converts a number of bytes to a number of samples
442 * @s: comedi_subdevice struct
443 * @nbytes: number of bytes
444 *
445 * Returns the number of bytes divided by the subdevice sample size.
446 */
447static inline unsigned int comedi_bytes_to_samples(struct comedi_subdevice *s,
448 unsigned int nbytes)
449{
450 return nbytes >> comedi_sample_shift(s);
451}
452
453/**
454 * comedi_samples_to_bytes - converts a number of samples to a number of bytes
455 * @s: comedi_subdevice struct
456 * @nsamples: number of samples
457 *
458 * Returns the number of samples multiplied by the subdevice sample size.
459 * Does not check for arithmetic overflow.
460 */
461static inline unsigned int comedi_samples_to_bytes(struct comedi_subdevice *s,
462 unsigned int nsamples)
463{
464 return nsamples << comedi_sample_shift(s);
David Schleefed9eccb2008-11-04 20:29:31 -0800465}
466
Ian Abbott5e1a6192015-03-27 19:13:41 +0000467/**
468 * comedi_check_trigger_src() - trivially validate a comedi_cmd trigger source
469 * @src: pointer to the trigger source to validate
470 * @flags: bitmask of valid TRIG_* for the trigger
471 *
472 * This is used in "step 1" of the do_cmdtest functions of comedi drivers
473 * to vaildate the comedi_cmd triggers. The mask of the @src against the
474 * @flags allows the userspace comedilib to pass all the comedi_cmd
475 * triggers as TRIG_ANY and get back a bitmask of the valid trigger sources.
476 */
477static inline int comedi_check_trigger_src(unsigned int *src,
478 unsigned int flags)
479{
480 unsigned int orig_src = *src;
481
482 *src = orig_src & flags;
483 if (*src == TRIG_INVALID || *src != orig_src)
484 return -EINVAL;
485 return 0;
486}
487
488/**
489 * comedi_check_trigger_is_unique() - make sure a trigger source is unique
490 * @src: the trigger source to check
491 */
492static inline int comedi_check_trigger_is_unique(unsigned int src)
493{
494 /* this test is true if more than one _src bit is set */
495 if ((src & (src - 1)) != 0)
496 return -EINVAL;
497 return 0;
498}
499
500/**
501 * comedi_check_trigger_arg_is() - trivially validate a trigger argument
502 * @arg: pointer to the trigger arg to validate
503 * @val: the value the argument should be
504 */
505static inline int comedi_check_trigger_arg_is(unsigned int *arg,
506 unsigned int val)
507{
508 if (*arg != val) {
509 *arg = val;
510 return -EINVAL;
511 }
512 return 0;
513}
514
515/**
516 * comedi_check_trigger_arg_min() - trivially validate a trigger argument
517 * @arg: pointer to the trigger arg to validate
518 * @val: the minimum value the argument should be
519 */
520static inline int comedi_check_trigger_arg_min(unsigned int *arg,
521 unsigned int val)
522{
523 if (*arg < val) {
524 *arg = val;
525 return -EINVAL;
526 }
527 return 0;
528}
529
530/**
531 * comedi_check_trigger_arg_max() - trivially validate a trigger argument
532 * @arg: pointer to the trigger arg to validate
533 * @val: the maximum value the argument should be
534 */
535static inline int comedi_check_trigger_arg_max(unsigned int *arg,
536 unsigned int val)
537{
538 if (*arg > val) {
539 *arg = val;
540 return -EINVAL;
541 }
542 return 0;
543}
544
Ian Abbottbc3954b2013-01-29 16:20:17 +0000545/*
546 * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
547 * Also useful for retrieving a previously configured hardware device of
548 * known bus type. Set automatically for auto-configured devices.
549 * Automatically set to NULL when detaching hardware device.
550 */
Ian Abbottda717512013-02-01 13:23:19 +0000551int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800552
H Hartley Sweetenf4f3f7c2014-06-20 10:58:28 -0700553static inline unsigned int comedi_buf_n_bytes_ready(struct comedi_subdevice *s)
554{
555 return s->async->buf_write_count - s->async->buf_read_count;
556}
557
Ian Abbott24e894b2014-05-06 13:12:04 +0100558unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s, unsigned int n);
Ian Abbott940dd352014-05-06 13:12:05 +0100559unsigned int comedi_buf_write_free(struct comedi_subdevice *s, unsigned int n);
David Schleefed9eccb2008-11-04 20:29:31 -0800560
Ian Abbotte9edef32014-05-06 13:12:09 +0100561unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s);
Ian Abbottd13be552014-05-06 13:12:07 +0100562unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s, unsigned int n);
Ian Abbottf1df8662014-05-06 13:12:08 +0100563unsigned int comedi_buf_read_free(struct comedi_subdevice *s, unsigned int n);
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700564
H Hartley Sweeten5438da82014-10-22 15:36:24 -0700565unsigned int comedi_buf_write_samples(struct comedi_subdevice *s,
566 const void *data, unsigned int nsamples);
H Hartley Sweeten4455d7c2014-10-22 14:36:34 -0700567unsigned int comedi_buf_read_samples(struct comedi_subdevice *s,
568 void *data, unsigned int nsamples);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530569
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700570/* drivers.c - general comedi driver functions */
571
H Hartley Sweeten91506402014-02-10 11:49:00 -0700572#define COMEDI_TIMEOUT_MS 1000
573
574int comedi_timeout(struct comedi_device *, struct comedi_subdevice *,
575 struct comedi_insn *,
576 int (*cb)(struct comedi_device *, struct comedi_subdevice *,
577 struct comedi_insn *, unsigned long context),
578 unsigned long context);
579
Ian Abbott5a780352014-09-15 13:46:01 +0100580unsigned int comedi_handle_events(struct comedi_device *dev,
581 struct comedi_subdevice *s);
582
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700583int comedi_dio_insn_config(struct comedi_device *, struct comedi_subdevice *,
584 struct comedi_insn *, unsigned int *data,
585 unsigned int mask);
H Hartley Sweeten05e60b12013-08-30 11:04:56 -0700586unsigned int comedi_dio_update_state(struct comedi_subdevice *,
587 unsigned int *data);
Ian Abbottf146fe62014-09-15 13:45:57 +0100588unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s);
H Hartley Sweeten2ee37752014-11-05 10:31:29 -0700589unsigned int comedi_nscans_left(struct comedi_subdevice *s,
590 unsigned int nscans);
H Hartley Sweetenf6159152014-11-05 10:31:33 -0700591unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
592 unsigned int nsamples);
Ian Abbott2b4e1f62014-09-15 13:45:59 +0100593void comedi_inc_scan_progress(struct comedi_subdevice *s,
594 unsigned int num_bytes);
H Hartley Sweetene523c6c2013-08-06 09:31:35 -0700595
H Hartley Sweeten54db9962013-06-24 16:55:14 -0700596void *comedi_alloc_devpriv(struct comedi_device *, size_t);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700597int comedi_alloc_subdevices(struct comedi_device *, int);
H Hartley Sweetend2762062014-08-25 16:03:54 -0700598int comedi_alloc_subdev_readback(struct comedi_subdevice *);
599
600int comedi_readback_insn_read(struct comedi_device *, struct comedi_subdevice *,
601 struct comedi_insn *, unsigned int *data);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700602
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700603int comedi_load_firmware(struct comedi_device *, struct device *,
604 const char *name,
605 int (*cb)(struct comedi_device *,
H Hartley Sweetend5695412013-05-17 11:18:01 -0700606 const u8 *data, size_t size,
607 unsigned long context),
608 unsigned long context);
H Hartley Sweeten9ff8b152013-05-17 11:17:00 -0700609
H Hartley Sweetenca8b2962013-04-09 16:30:11 -0700610int __comedi_request_region(struct comedi_device *,
611 unsigned long start, unsigned long len);
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700612int comedi_request_region(struct comedi_device *,
613 unsigned long start, unsigned long len);
H Hartley Sweeten316f97f2013-04-18 14:31:29 -0700614void comedi_legacy_detach(struct comedi_device *);
H Hartley Sweetenf375ac52013-04-09 16:05:54 -0700615
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700616int comedi_auto_config(struct device *, struct comedi_driver *,
617 unsigned long context);
618void comedi_auto_unconfig(struct device *);
619
620int comedi_driver_register(struct comedi_driver *);
Ian Abbott99c0e262013-06-27 14:50:57 +0100621void comedi_driver_unregister(struct comedi_driver *);
H Hartley Sweeten1ae6b202013-01-30 15:24:38 -0700622
623/**
624 * module_comedi_driver() - Helper macro for registering a comedi driver
625 * @__comedi_driver: comedi_driver struct
626 *
627 * Helper macro for comedi drivers which do not do anything special in module
628 * init/exit. This eliminates a lot of boilerplate. Each module may only use
629 * this macro once, and calling it replaces module_init() and module_exit().
630 */
631#define module_comedi_driver(__comedi_driver) \
632 module_driver(__comedi_driver, comedi_driver_register, \
633 comedi_driver_unregister)
Ian Abbott581a7dd2012-11-14 13:10:40 +0000634
David Schleefed9eccb2008-11-04 20:29:31 -0800635#endif /* _COMEDIDEV_H */