Nishad Kamdar | 03f17b5 | 2018-05-24 23:14:40 +0530 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2 | /* |
Ian Abbott | 2750914 | 2015-09-21 18:52:46 +0100 | [diff] [blame] | 3 | * comedidev.h |
| 4 | * header file for kernel-only structures, variables, and constants |
| 5 | * |
| 6 | * COMEDI - Linux Control and Measurement Device Interface |
| 7 | * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org> |
Ian Abbott | 2750914 | 2015-09-21 18:52:46 +0100 | [diff] [blame] | 8 | */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 9 | |
| 10 | #ifndef _COMEDIDEV_H |
| 11 | #define _COMEDIDEV_H |
| 12 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
Ian Abbott | ab3cb2e | 2013-11-08 15:03:23 +0000 | [diff] [blame] | 14 | #include <linux/mutex.h> |
| 15 | #include <linux/spinlock_types.h> |
Ian Abbott | 2f3fdcd | 2013-11-08 15:03:24 +0000 | [diff] [blame] | 16 | #include <linux/rwsem.h> |
Ian Abbott | 5b13ed9 | 2013-11-08 15:03:32 +0000 | [diff] [blame] | 17 | #include <linux/kref.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 18 | |
| 19 | #include "comedi.h" |
| 20 | |
Greg Kroah-Hartman | e0dcef7 | 2008-11-13 16:36:22 -0800 | [diff] [blame] | 21 | #define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c)) |
Shane Warden | 62eeae9 | 2009-09-22 16:13:04 -0700 | [diff] [blame] | 22 | #define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \ |
| 23 | COMEDI_MINORVERSION, COMEDI_MICROVERSION) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 24 | #define COMEDI_RELEASE VERSION |
| 25 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 26 | #define COMEDI_NUM_BOARD_MINORS 0x30 |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 27 | |
Ian Abbott | 0f952fa | 2015-09-21 18:52:51 +0100 | [diff] [blame] | 28 | /** |
| 29 | * struct comedi_subdevice - Working data for a COMEDI subdevice |
| 30 | * @device: COMEDI device to which this subdevice belongs. (Initialized by |
| 31 | * comedi_alloc_subdevices().) |
| 32 | * @index: Index of this subdevice within device's array of subdevices. |
| 33 | * (Initialized by comedi_alloc_subdevices().) |
| 34 | * @type: Type of subdevice from &enum comedi_subdevice_type. (Initialized by |
| 35 | * the low-level driver.) |
| 36 | * @n_chan: Number of channels the subdevice supports. (Initialized by the |
| 37 | * low-level driver.) |
| 38 | * @subdev_flags: Various "SDF" flags indicating aspects of the subdevice to |
| 39 | * the COMEDI core and user application. (Initialized by the low-level |
| 40 | * driver.) |
| 41 | * @len_chanlist: Maximum length of a channel list if the subdevice supports |
| 42 | * asynchronous acquisition commands. (Optionally initialized by the |
| 43 | * low-level driver, or changed from 0 to 1 during post-configuration.) |
| 44 | * @private: Private data pointer which is either set by the low-level driver |
| 45 | * itself, or by a call to comedi_alloc_spriv() which allocates storage. |
| 46 | * In the latter case, the storage is automatically freed after the |
| 47 | * low-level driver's "detach" handler is called for the device. |
| 48 | * (Initialized by the low-level driver.) |
| 49 | * @async: Pointer to &struct comedi_async id the subdevice supports |
| 50 | * asynchronous acquisition commands. (Allocated and initialized during |
| 51 | * post-configuration if needed.) |
| 52 | * @lock: Pointer to a file object that performed a %COMEDI_LOCK ioctl on the |
| 53 | * subdevice. (Initially NULL.) |
| 54 | * @busy: Pointer to a file object that is performing an asynchronous |
| 55 | * acquisition command on the subdevice. (Initially NULL.) |
| 56 | * @runflags: Internal flags for use by COMEDI core, mostly indicating whether |
| 57 | * an asynchronous acquisition command is running. |
| 58 | * @spin_lock: Generic spin-lock for use by the COMEDI core and the low-level |
| 59 | * driver. (Initialized by comedi_alloc_subdevices().) |
| 60 | * @io_bits: Bit-mask indicating the channel directions for a DIO subdevice |
| 61 | * with no more than 32 channels. A '1' at a bit position indicates the |
| 62 | * corresponding channel is configured as an output. (Initialized by the |
| 63 | * low-level driver for a DIO subdevice. Forced to all-outputs during |
| 64 | * post-configuration for a digital output subdevice.) |
| 65 | * @maxdata: If non-zero, this is the maximum raw data value of each channel. |
| 66 | * If zero, the maximum data value is channel-specific. (Initialized by |
| 67 | * the low-level driver.) |
| 68 | * @maxdata_list: If the maximum data value is channel-specific, this points |
| 69 | * to an array of maximum data values indexed by channel index. |
| 70 | * (Initialized by the low-level driver.) |
| 71 | * @range_table: If non-NULL, this points to a COMEDI range table for the |
| 72 | * subdevice. If NULL, the range table is channel-specific. (Initialized |
| 73 | * by the low-level driver, will be set to an "invalid" range table during |
| 74 | * post-configuration if @range_table and @range_table_list are both |
| 75 | * NULL.) |
| 76 | * @range_table_list: If the COMEDI range table is channel-specific, this |
| 77 | * points to an array of pointers to COMEDI range tables indexed by |
| 78 | * channel number. (Initialized by the low-level driver.) |
| 79 | * @chanlist: Not used. |
| 80 | * @insn_read: Optional pointer to a handler for the %INSN_READ instruction. |
| 81 | * (Initialized by the low-level driver, or set to a default handler |
| 82 | * during post-configuration.) |
| 83 | * @insn_write: Optional pointer to a handler for the %INSN_WRITE instruction. |
| 84 | * (Initialized by the low-level driver, or set to a default handler |
| 85 | * during post-configuration.) |
| 86 | * @insn_bits: Optional pointer to a handler for the %INSN_BITS instruction |
| 87 | * for a digital input, digital output or digital input/output subdevice. |
| 88 | * (Initialized by the low-level driver, or set to a default handler |
| 89 | * during post-configuration.) |
| 90 | * @insn_config: Optional pointer to a handler for the %INSN_CONFIG |
| 91 | * instruction. (Initialized by the low-level driver, or set to a default |
| 92 | * handler during post-configuration.) |
| 93 | * @do_cmd: If the subdevice supports asynchronous acquisition commands, this |
| 94 | * points to a handler to set it up in hardware. (Initialized by the |
| 95 | * low-level driver.) |
| 96 | * @do_cmdtest: If the subdevice supports asynchronous acquisition commands, |
| 97 | * this points to a handler used to check and possibly tweak a prospective |
| 98 | * acquisition command without setting it up in hardware. (Initialized by |
| 99 | * the low-level driver.) |
| 100 | * @poll: If the subdevice supports asynchronous acquisition commands, this |
| 101 | * is an optional pointer to a handler for the %COMEDI_POLL ioctl which |
| 102 | * instructs the low-level driver to synchronize buffers. (Initialized by |
| 103 | * the low-level driver if needed.) |
| 104 | * @cancel: If the subdevice supports asynchronous acquisition commands, this |
| 105 | * points to a handler used to terminate a running command. (Initialized |
| 106 | * by the low-level driver.) |
| 107 | * @buf_change: If the subdevice supports asynchronous acquisition commands, |
| 108 | * this is an optional pointer to a handler that is called when the data |
| 109 | * buffer for handling asynchronous commands is allocated or reallocated. |
| 110 | * (Initialized by the low-level driver if needed.) |
| 111 | * @munge: If the subdevice supports asynchronous acquisition commands and |
| 112 | * uses DMA to transfer data from the hardware to the acquisition buffer, |
| 113 | * this points to a function used to "munge" the data values from the |
| 114 | * hardware into the format expected by COMEDI. (Initialized by the |
| 115 | * low-level driver if needed.) |
| 116 | * @async_dma_dir: If the subdevice supports asynchronous acquisition commands |
| 117 | * and uses DMA to transfer data from the hardware to the acquisition |
| 118 | * buffer, this sets the DMA direction for the buffer. (initialized to |
| 119 | * %DMA_NONE by comedi_alloc_subdevices() and changed by the low-level |
| 120 | * driver if necessary.) |
| 121 | * @state: Handy bit-mask indicating the output states for a DIO or digital |
| 122 | * output subdevice with no more than 32 channels. (Initialized by the |
| 123 | * low-level driver.) |
| 124 | * @class_dev: If the subdevice supports asynchronous acquisition commands, |
| 125 | * this points to a sysfs comediX_subdY device where X is the minor device |
| 126 | * number of the COMEDI device and Y is the subdevice number. The minor |
| 127 | * device number for the sysfs device is allocated dynamically in the |
| 128 | * range 48 to 255. This is used to allow the COMEDI device to be opened |
| 129 | * with a different default read or write subdevice. (Allocated during |
| 130 | * post-configuration if needed.) |
| 131 | * @minor: If @class_dev is set, this is its dynamically allocated minor |
| 132 | * device number. (Set during post-configuration if necessary.) |
| 133 | * @readback: Optional pointer to memory allocated by |
| 134 | * comedi_alloc_subdev_readback() used to hold the values written to |
| 135 | * analog output channels so they can be read back. The storage is |
| 136 | * automatically freed after the low-level driver's "detach" handler is |
| 137 | * called for the device. (Initialized by the low-level driver.) |
| 138 | * |
| 139 | * This is the main control structure for a COMEDI subdevice. If the subdevice |
| 140 | * supports asynchronous acquisition commands, additional information is stored |
| 141 | * in the &struct comedi_async pointed to by @async. |
| 142 | * |
| 143 | * Most of the subdevice is initialized by the low-level driver's "attach" or |
| 144 | * "auto_attach" handlers but parts of it are initialized by |
| 145 | * comedi_alloc_subdevices(), and other parts are initialized during |
| 146 | * post-configuration on return from that handler. |
| 147 | * |
| 148 | * A low-level driver that sets @insn_bits for a digital input, digital output, |
| 149 | * or DIO subdevice may leave @insn_read and @insn_write uninitialized, in |
| 150 | * which case they will be set to a default handler during post-configuration |
| 151 | * that uses @insn_bits to emulate the %INSN_READ and %INSN_WRITE instructions. |
| 152 | */ |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 153 | struct comedi_subdevice { |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 154 | struct comedi_device *device; |
H Hartley Sweeten | 90a35c1 | 2012-12-19 17:27:02 -0700 | [diff] [blame] | 155 | int index; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 156 | int type; |
| 157 | int n_chan; |
Greg Kroah-Hartman | baf22b6 | 2010-04-30 15:26:54 -0700 | [diff] [blame] | 158 | int subdev_flags; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 159 | int len_chanlist; /* maximum length of channel/gain list */ |
| 160 | |
| 161 | void *private; |
| 162 | |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 163 | struct comedi_async *async; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 164 | |
| 165 | void *lock; |
| 166 | void *busy; |
Aniket Sharma | ae6b086 | 2016-03-27 10:52:19 -0700 | [diff] [blame] | 167 | unsigned int runflags; |
Ian Abbott | 214e384 | 2015-10-12 18:03:24 +0100 | [diff] [blame] | 168 | spinlock_t spin_lock; /* generic spin-lock for COMEDI and drivers */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 169 | |
H Hartley Sweeten | d4a7dc8 | 2012-06-18 14:45:42 -0700 | [diff] [blame] | 170 | unsigned int io_bits; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 171 | |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 172 | unsigned int maxdata; /* if maxdata==0, use list */ |
| 173 | const unsigned int *maxdata_list; /* list is channel specific */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 174 | |
Bill Pemberton | 9ced1de | 2009-03-16 22:05:31 -0400 | [diff] [blame] | 175 | const struct comedi_lrange *range_table; |
| 176 | const struct comedi_lrange *const *range_table_list; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 177 | |
| 178 | unsigned int *chanlist; /* driver-owned chanlist (not used) */ |
| 179 | |
Michael Gebhard | 2c5f734 | 2017-12-21 17:36:30 +0100 | [diff] [blame] | 180 | int (*insn_read)(struct comedi_device *dev, struct comedi_subdevice *s, |
| 181 | struct comedi_insn *insn, unsigned int *data); |
| 182 | int (*insn_write)(struct comedi_device *dev, struct comedi_subdevice *s, |
| 183 | struct comedi_insn *insn, unsigned int *data); |
| 184 | int (*insn_bits)(struct comedi_device *dev, struct comedi_subdevice *s, |
| 185 | struct comedi_insn *insn, unsigned int *data); |
Eujon Sellers | 2631108 | 2018-01-25 16:21:58 -0700 | [diff] [blame] | 186 | int (*insn_config)(struct comedi_device *dev, |
| 187 | struct comedi_subdevice *s, |
| 188 | struct comedi_insn *insn, |
| 189 | unsigned int *data); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 190 | |
Michael Gebhard | 2c5f734 | 2017-12-21 17:36:30 +0100 | [diff] [blame] | 191 | int (*do_cmd)(struct comedi_device *dev, struct comedi_subdevice *s); |
Eujon Sellers | 2631108 | 2018-01-25 16:21:58 -0700 | [diff] [blame] | 192 | int (*do_cmdtest)(struct comedi_device *dev, |
| 193 | struct comedi_subdevice *s, |
Michael Gebhard | 2c5f734 | 2017-12-21 17:36:30 +0100 | [diff] [blame] | 194 | struct comedi_cmd *cmd); |
| 195 | int (*poll)(struct comedi_device *dev, struct comedi_subdevice *s); |
| 196 | int (*cancel)(struct comedi_device *dev, struct comedi_subdevice *s); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 197 | |
| 198 | /* called when the buffer changes */ |
Eujon Sellers | 2631108 | 2018-01-25 16:21:58 -0700 | [diff] [blame] | 199 | int (*buf_change)(struct comedi_device *dev, |
| 200 | struct comedi_subdevice *s); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 201 | |
Monam Agarwal | 0cb4c151 | 2014-02-25 14:51:26 +0530 | [diff] [blame] | 202 | void (*munge)(struct comedi_device *dev, struct comedi_subdevice *s, |
| 203 | void *data, unsigned int num_bytes, |
| 204 | unsigned int start_chan_index); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 205 | enum dma_data_direction async_dma_dir; |
| 206 | |
| 207 | unsigned int state; |
| 208 | |
Bill Pemberton | 0bfbbe8 | 2009-03-16 22:05:36 -0400 | [diff] [blame] | 209 | struct device *class_dev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 210 | int minor; |
H Hartley Sweeten | d276206 | 2014-08-25 16:03:54 -0700 | [diff] [blame] | 211 | |
| 212 | unsigned int *readback; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 213 | }; |
| 214 | |
Ian Abbott | 11a1083 | 2015-09-21 18:52:52 +0100 | [diff] [blame] | 215 | /** |
| 216 | * struct comedi_buf_page - Describe a page of a COMEDI buffer |
| 217 | * @virt_addr: Kernel address of page. |
| 218 | * @dma_addr: DMA address of page if in DMA coherent memory. |
| 219 | */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 220 | struct comedi_buf_page { |
| 221 | void *virt_addr; |
| 222 | dma_addr_t dma_addr; |
| 223 | }; |
| 224 | |
Ian Abbott | 11a1083 | 2015-09-21 18:52:52 +0100 | [diff] [blame] | 225 | /** |
| 226 | * struct comedi_buf_map - Describe pages in a COMEDI buffer |
| 227 | * @dma_hw_dev: Low-level hardware &struct device pointer copied from the |
| 228 | * COMEDI device's hw_dev member. |
| 229 | * @page_list: Pointer to array of &struct comedi_buf_page, one for each |
| 230 | * page in the buffer. |
| 231 | * @n_pages: Number of pages in the buffer. |
| 232 | * @dma_dir: DMA direction used to allocate pages of DMA coherent memory, |
| 233 | * or %DMA_NONE if pages allocated from regular memory. |
| 234 | * @refcount: &struct kref reference counter used to free the buffer. |
| 235 | * |
| 236 | * A COMEDI data buffer is allocated as individual pages, either in |
| 237 | * conventional memory or DMA coherent memory, depending on the attached, |
| 238 | * low-level hardware device. (The buffer pages also get mapped into the |
| 239 | * kernel's contiguous virtual address space pointed to by the 'prealloc_buf' |
| 240 | * member of &struct comedi_async.) |
| 241 | * |
| 242 | * The buffer is normally freed when the COMEDI device is detached from the |
| 243 | * low-level driver (which may happen due to device removal), but if it happens |
| 244 | * to be mmapped at the time, the pages cannot be freed until the buffer has |
| 245 | * been munmapped. That is what the reference counter is for. (The virtual |
| 246 | * address space pointed by 'prealloc_buf' is freed when the COMEDI device is |
| 247 | * detached.) |
| 248 | */ |
Ian Abbott | af93da3 | 2013-11-08 15:03:43 +0000 | [diff] [blame] | 249 | struct comedi_buf_map { |
| 250 | struct device *dma_hw_dev; |
| 251 | struct comedi_buf_page *page_list; |
| 252 | unsigned int n_pages; |
| 253 | enum dma_data_direction dma_dir; |
| 254 | struct kref refcount; |
| 255 | }; |
| 256 | |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 257 | /** |
Ian Abbott | 9f2f5d3 | 2015-09-21 18:52:54 +0100 | [diff] [blame] | 258 | * struct comedi_async - Control data for asynchronous COMEDI commands |
| 259 | * @prealloc_buf: Kernel virtual address of allocated acquisition buffer. |
| 260 | * @prealloc_bufsz: Buffer size (in bytes). |
| 261 | * @buf_map: Map of buffer pages. |
| 262 | * @max_bufsize: Maximum allowed buffer size (in bytes). |
| 263 | * @buf_write_count: "Write completed" count (in bytes, modulo 2**32). |
| 264 | * @buf_write_alloc_count: "Allocated for writing" count (in bytes, |
| 265 | * modulo 2**32). |
| 266 | * @buf_read_count: "Read completed" count (in bytes, modulo 2**32). |
| 267 | * @buf_read_alloc_count: "Allocated for reading" count (in bytes, |
| 268 | * modulo 2**32). |
| 269 | * @buf_write_ptr: Buffer position for writer. |
| 270 | * @buf_read_ptr: Buffer position for reader. |
| 271 | * @cur_chan: Current position in chanlist for scan (for those drivers that |
| 272 | * use it). |
| 273 | * @scans_done: The number of scans completed. |
| 274 | * @scan_progress: Amount received or sent for current scan (in bytes). |
| 275 | * @munge_chan: Current position in chanlist for "munging". |
| 276 | * @munge_count: "Munge" count (in bytes, modulo 2**32). |
| 277 | * @munge_ptr: Buffer position for "munging". |
| 278 | * @events: Bit-vector of events that have occurred. |
| 279 | * @cmd: Details of comedi command in progress. |
| 280 | * @wait_head: Task wait queue for file reader or writer. |
| 281 | * @cb_mask: Bit-vector of events that should wake waiting tasks. |
| 282 | * @inttrig: Software trigger function for command, or NULL. |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 283 | * |
| 284 | * Note about the ..._count and ..._ptr members: |
| 285 | * |
| 286 | * Think of the _Count values being integers of unlimited size, indexing |
| 287 | * into a buffer of infinite length (though only an advancing portion |
Ian Abbott | a871773 | 2015-09-21 18:52:48 +0100 | [diff] [blame] | 288 | * of the buffer of fixed length prealloc_bufsz is accessible at any |
| 289 | * time). Then: |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 290 | * |
| 291 | * Buf_Read_Count <= Buf_Read_Alloc_Count <= Munge_Count <= |
| 292 | * Buf_Write_Count <= Buf_Write_Alloc_Count <= |
| 293 | * (Buf_Read_Count + prealloc_bufsz) |
| 294 | * |
Ian Abbott | a871773 | 2015-09-21 18:52:48 +0100 | [diff] [blame] | 295 | * (Those aren't the actual members, apart from prealloc_bufsz.) When the |
| 296 | * buffer is reset, those _Count values start at 0 and only increase in value, |
| 297 | * maintaining the above inequalities until the next time the buffer is |
| 298 | * reset. The buffer is divided into the following regions by the inequalities: |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 299 | * |
| 300 | * [0, Buf_Read_Count): |
| 301 | * old region no longer accessible |
Ian Abbott | a871773 | 2015-09-21 18:52:48 +0100 | [diff] [blame] | 302 | * |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 303 | * [Buf_Read_Count, Buf_Read_Alloc_Count): |
| 304 | * filled and munged region allocated for reading but not yet read |
Ian Abbott | a871773 | 2015-09-21 18:52:48 +0100 | [diff] [blame] | 305 | * |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 306 | * [Buf_Read_Alloc_Count, Munge_Count): |
| 307 | * filled and munged region not yet allocated for reading |
Ian Abbott | a871773 | 2015-09-21 18:52:48 +0100 | [diff] [blame] | 308 | * |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 309 | * [Munge_Count, Buf_Write_Count): |
| 310 | * filled region not yet munged |
Ian Abbott | a871773 | 2015-09-21 18:52:48 +0100 | [diff] [blame] | 311 | * |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 312 | * [Buf_Write_Count, Buf_Write_Alloc_Count): |
| 313 | * unfilled region allocated for writing but not yet written |
Ian Abbott | a871773 | 2015-09-21 18:52:48 +0100 | [diff] [blame] | 314 | * |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 315 | * [Buf_Write_Alloc_Count, Buf_Read_Count + prealloc_bufsz): |
| 316 | * unfilled region not yet allocated for writing |
Ian Abbott | a871773 | 2015-09-21 18:52:48 +0100 | [diff] [blame] | 317 | * |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 318 | * [Buf_Read_Count + prealloc_bufsz, infinity): |
| 319 | * unfilled region not yet accessible |
| 320 | * |
| 321 | * Data needs to be written into the buffer before it can be read out, |
| 322 | * and may need to be converted (or "munged") between the two |
| 323 | * operations. Extra unfilled buffer space may need to allocated for |
| 324 | * writing (advancing Buf_Write_Alloc_Count) before new data is written. |
| 325 | * After writing new data, the newly filled space needs to be released |
| 326 | * (advancing Buf_Write_Count). This also results in the new data being |
| 327 | * "munged" (advancing Munge_Count). Before data is read out of the |
| 328 | * buffer, extra space may need to be allocated for reading (advancing |
| 329 | * Buf_Read_Alloc_Count). After the data has been read out, the space |
| 330 | * needs to be released (advancing Buf_Read_Count). |
| 331 | * |
| 332 | * The actual members, buf_read_count, buf_read_alloc_count, |
| 333 | * munge_count, buf_write_count, and buf_write_alloc_count take the |
| 334 | * value of the corresponding capitalized _Count values modulo 2^32 |
| 335 | * (UINT_MAX+1). Subtracting a "higher" _count value from a "lower" |
| 336 | * _count value gives the same answer as subtracting a "higher" _Count |
| 337 | * value from a lower _Count value because prealloc_bufsz < UINT_MAX+1. |
| 338 | * The modulo operation is done implicitly. |
| 339 | * |
| 340 | * The buf_read_ptr, munge_ptr, and buf_write_ptr members take the value |
| 341 | * of the corresponding capitalized _Count values modulo prealloc_bufsz. |
| 342 | * These correspond to byte indices in the physical buffer. The modulo |
| 343 | * operation is done by subtracting prealloc_bufsz when the value |
| 344 | * exceeds prealloc_bufsz (assuming prealloc_bufsz plus the increment is |
| 345 | * less than or equal to UINT_MAX). |
| 346 | */ |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 347 | struct comedi_async { |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 348 | void *prealloc_buf; |
| 349 | unsigned int prealloc_bufsz; |
| 350 | struct comedi_buf_map *buf_map; |
| 351 | unsigned int max_bufsize; |
Shane Warden | 62eeae9 | 2009-09-22 16:13:04 -0700 | [diff] [blame] | 352 | unsigned int buf_write_count; |
Shane Warden | 62eeae9 | 2009-09-22 16:13:04 -0700 | [diff] [blame] | 353 | unsigned int buf_write_alloc_count; |
Shane Warden | 62eeae9 | 2009-09-22 16:13:04 -0700 | [diff] [blame] | 354 | unsigned int buf_read_count; |
Shane Warden | 62eeae9 | 2009-09-22 16:13:04 -0700 | [diff] [blame] | 355 | unsigned int buf_read_alloc_count; |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 356 | unsigned int buf_write_ptr; |
| 357 | unsigned int buf_read_ptr; |
| 358 | unsigned int cur_chan; |
H Hartley Sweeten | 1dacbe5 | 2014-11-05 10:20:52 -0700 | [diff] [blame] | 359 | unsigned int scans_done; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 360 | unsigned int scan_progress; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 361 | unsigned int munge_chan; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 362 | unsigned int munge_count; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 363 | unsigned int munge_ptr; |
Ian Abbott | 55d128b | 2014-06-20 14:15:04 +0100 | [diff] [blame] | 364 | unsigned int events; |
Bill Pemberton | ea6d0d4 | 2009-03-16 22:05:47 -0400 | [diff] [blame] | 365 | struct comedi_cmd cmd; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 366 | wait_queue_head_t wait_head; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 367 | unsigned int cb_mask; |
Monam Agarwal | 0cb4c151 | 2014-02-25 14:51:26 +0530 | [diff] [blame] | 368 | int (*inttrig)(struct comedi_device *dev, struct comedi_subdevice *s, |
| 369 | unsigned int x); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 370 | }; |
| 371 | |
H Hartley Sweeten | bb856b6 | 2014-10-13 09:56:07 -0700 | [diff] [blame] | 372 | /** |
Ian Abbott | 251a16a | 2015-09-21 18:52:47 +0100 | [diff] [blame] | 373 | * enum comedi_cb - &struct comedi_async callback "events" |
H Hartley Sweeten | bb856b6 | 2014-10-13 09:56:07 -0700 | [diff] [blame] | 374 | * @COMEDI_CB_EOS: end-of-scan |
| 375 | * @COMEDI_CB_EOA: end-of-acquisition/output |
| 376 | * @COMEDI_CB_BLOCK: data has arrived, wakes up read() / write() |
| 377 | * @COMEDI_CB_EOBUF: DEPRECATED: end of buffer |
| 378 | * @COMEDI_CB_ERROR: card error during acquisition |
| 379 | * @COMEDI_CB_OVERFLOW: buffer overflow/underflow |
H Hartley Sweeten | 781f933 | 2014-10-13 09:56:08 -0700 | [diff] [blame] | 380 | * @COMEDI_CB_ERROR_MASK: events that indicate an error has occurred |
| 381 | * @COMEDI_CB_CANCEL_MASK: events that will cancel an async command |
H Hartley Sweeten | bb856b6 | 2014-10-13 09:56:07 -0700 | [diff] [blame] | 382 | */ |
Ian Abbott | 251a16a | 2015-09-21 18:52:47 +0100 | [diff] [blame] | 383 | enum comedi_cb { |
| 384 | COMEDI_CB_EOS = BIT(0), |
| 385 | COMEDI_CB_EOA = BIT(1), |
| 386 | COMEDI_CB_BLOCK = BIT(2), |
| 387 | COMEDI_CB_EOBUF = BIT(3), |
| 388 | COMEDI_CB_ERROR = BIT(4), |
| 389 | COMEDI_CB_OVERFLOW = BIT(5), |
| 390 | /* masks */ |
| 391 | COMEDI_CB_ERROR_MASK = (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW), |
| 392 | COMEDI_CB_CANCEL_MASK = (COMEDI_CB_EOA | COMEDI_CB_ERROR_MASK) |
| 393 | }; |
H Hartley Sweeten | 781f933 | 2014-10-13 09:56:08 -0700 | [diff] [blame] | 394 | |
Ian Abbott | a3ed91f | 2015-09-21 18:52:49 +0100 | [diff] [blame] | 395 | /** |
| 396 | * struct comedi_driver - COMEDI driver registration |
| 397 | * @driver_name: Name of driver. |
| 398 | * @module: Owning module. |
| 399 | * @attach: The optional "attach" handler for manually configured COMEDI |
| 400 | * devices. |
| 401 | * @detach: The "detach" handler for deconfiguring COMEDI devices. |
| 402 | * @auto_attach: The optional "auto_attach" handler for automatically |
| 403 | * configured COMEDI devices. |
| 404 | * @num_names: Optional number of "board names" supported. |
| 405 | * @board_name: Optional pointer to a pointer to a board name. The pointer |
| 406 | * to a board name is embedded in an element of a driver-defined array |
| 407 | * of static, read-only board type information. |
| 408 | * @offset: Optional size of each element of the driver-defined array of |
| 409 | * static, read-only board type information, i.e. the offset between each |
| 410 | * pointer to a board name. |
| 411 | * |
| 412 | * This is used with comedi_driver_register() and comedi_driver_unregister() to |
| 413 | * register and unregister a low-level COMEDI driver with the COMEDI core. |
| 414 | * |
| 415 | * If @num_names is non-zero, @board_name should be non-NULL, and @offset |
| 416 | * should be at least sizeof(*board_name). These are used by the handler for |
| 417 | * the %COMEDI_DEVCONFIG ioctl to match a hardware device and its driver by |
| 418 | * board name. If @num_names is zero, the %COMEDI_DEVCONFIG ioctl matches a |
| 419 | * hardware device and its driver by driver name. This is only useful if the |
| 420 | * @attach handler is set. If @num_names is non-zero, the driver's @attach |
| 421 | * handler will be called with the COMEDI device structure's board_ptr member |
| 422 | * pointing to the matched pointer to a board name within the driver's private |
| 423 | * array of static, read-only board type information. |
Ian Abbott | 3e0f9b2 | 2016-12-08 13:53:10 +0000 | [diff] [blame] | 424 | * |
| 425 | * The @detach handler has two roles. If a COMEDI device was successfully |
| 426 | * configured by the @attach or @auto_attach handler, it is called when the |
| 427 | * device is being deconfigured (by the %COMEDI_DEVCONFIG ioctl, or due to |
| 428 | * unloading of the driver, or due to device removal). It is also called when |
| 429 | * the @attach or @auto_attach handler returns an error. Therefore, the |
| 430 | * @attach or @auto_attach handlers can defer clean-up on error until the |
| 431 | * @detach handler is called. If the @attach or @auto_attach handlers free |
| 432 | * any resources themselves, they must prevent the @detach handler from |
| 433 | * freeing the same resources. The @detach handler must not assume that all |
| 434 | * resources requested by the @attach or @auto_attach handler were |
| 435 | * successfully allocated. |
Ian Abbott | a3ed91f | 2015-09-21 18:52:49 +0100 | [diff] [blame] | 436 | */ |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 437 | struct comedi_driver { |
Ian Abbott | a3ed91f | 2015-09-21 18:52:49 +0100 | [diff] [blame] | 438 | /* private: */ |
| 439 | struct comedi_driver *next; /* Next in list of COMEDI drivers. */ |
| 440 | /* public: */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 441 | const char *driver_name; |
| 442 | struct module *module; |
Michael Gebhard | 2c5f734 | 2017-12-21 17:36:30 +0100 | [diff] [blame] | 443 | int (*attach)(struct comedi_device *dev, struct comedi_devconfig *it); |
| 444 | void (*detach)(struct comedi_device *dev); |
| 445 | int (*auto_attach)(struct comedi_device *dev, unsigned long context); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 446 | unsigned int num_names; |
| 447 | const char *const *board_name; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 448 | int offset; |
| 449 | }; |
| 450 | |
Ian Abbott | 9ac5b44 | 2015-09-21 18:52:50 +0100 | [diff] [blame] | 451 | /** |
| 452 | * struct comedi_device - Working data for a COMEDI device |
| 453 | * @use_count: Number of open file objects. |
| 454 | * @driver: Low-level COMEDI driver attached to this COMEDI device. |
| 455 | * @pacer: Optional pointer to a dynamically allocated acquisition pacer |
| 456 | * control. It is freed automatically after the COMEDI device is |
| 457 | * detached from the low-level driver. |
| 458 | * @private: Optional pointer to private data allocated by the low-level |
| 459 | * driver. It is freed automatically after the COMEDI device is |
| 460 | * detached from the low-level driver. |
| 461 | * @class_dev: Sysfs comediX device. |
| 462 | * @minor: Minor device number of COMEDI char device (0-47). |
| 463 | * @detach_count: Counter incremented every time the COMEDI device is detached. |
| 464 | * Used for checking a previous attachment is still valid. |
| 465 | * @hw_dev: Optional pointer to the low-level hardware &struct device. It is |
| 466 | * required for automatically configured COMEDI devices and optional for |
| 467 | * COMEDI devices configured by the %COMEDI_DEVCONFIG ioctl, although |
| 468 | * the bus-specific COMEDI functions only work if it is set correctly. |
| 469 | * It is also passed to dma_alloc_coherent() for COMEDI subdevices that |
| 470 | * have their 'async_dma_dir' member set to something other than |
| 471 | * %DMA_NONE. |
| 472 | * @board_name: Pointer to a COMEDI board name or a COMEDI driver name. When |
| 473 | * the low-level driver's "attach" handler is called by the handler for |
| 474 | * the %COMEDI_DEVCONFIG ioctl, it either points to a matched board name |
| 475 | * string if the 'num_names' member of the &struct comedi_driver is |
| 476 | * non-zero, otherwise it points to the low-level driver name string. |
| 477 | * When the low-lever driver's "auto_attach" handler is called for an |
| 478 | * automatically configured COMEDI device, it points to the low-level |
| 479 | * driver name string. The low-level driver is free to change it in its |
| 480 | * "attach" or "auto_attach" handler if it wishes. |
| 481 | * @board_ptr: Optional pointer to private, read-only board type information in |
| 482 | * the low-level driver. If the 'num_names' member of the &struct |
| 483 | * comedi_driver is non-zero, the handler for the %COMEDI_DEVCONFIG ioctl |
| 484 | * will point it to a pointer to a matched board name string within the |
| 485 | * driver's private array of static, read-only board type information when |
| 486 | * calling the driver's "attach" handler. The low-level driver is free to |
| 487 | * change it. |
| 488 | * @attached: Flag indicating that the COMEDI device is attached to a low-level |
| 489 | * driver. |
| 490 | * @ioenabled: Flag used to indicate that a PCI device has been enabled and |
| 491 | * its regions requested. |
| 492 | * @spinlock: Generic spin-lock for use by the low-level driver. |
| 493 | * @mutex: Generic mutex for use by the COMEDI core module. |
| 494 | * @attach_lock: &struct rw_semaphore used to guard against the COMEDI device |
| 495 | * being detached while an operation is in progress. The down_write() |
| 496 | * operation is only allowed while @mutex is held and is used when |
| 497 | * changing @attached and @detach_count and calling the low-level driver's |
| 498 | * "detach" handler. The down_read() operation is generally used without |
| 499 | * holding @mutex. |
| 500 | * @refcount: &struct kref reference counter for freeing COMEDI device. |
| 501 | * @n_subdevices: Number of COMEDI subdevices allocated by the low-level |
| 502 | * driver for this device. |
| 503 | * @subdevices: Dynamically allocated array of COMEDI subdevices. |
| 504 | * @mmio: Optional pointer to a remapped MMIO region set by the low-level |
| 505 | * driver. |
| 506 | * @iobase: Optional base of an I/O port region requested by the low-level |
| 507 | * driver. |
| 508 | * @iolen: Length of I/O port region requested at @iobase. |
| 509 | * @irq: Optional IRQ number requested by the low-level driver. |
| 510 | * @read_subdev: Optional pointer to a default COMEDI subdevice operated on by |
| 511 | * the read() file operation. Set by the low-level driver. |
| 512 | * @write_subdev: Optional pointer to a default COMEDI subdevice operated on by |
| 513 | * the write() file operation. Set by the low-level driver. |
| 514 | * @async_queue: Storage for fasync_helper(). |
| 515 | * @open: Optional pointer to a function set by the low-level driver to be |
| 516 | * called when @use_count changes from 0 to 1. |
| 517 | * @close: Optional pointer to a function set by the low-level driver to be |
| 518 | * called when @use_count changed from 1 to 0. |
Spencer E. Olson | d7569ad | 2018-10-03 14:56:02 -0600 | [diff] [blame] | 519 | * @insn_device_config: Optional pointer to a handler for all sub-instructions |
| 520 | * except %INSN_DEVICE_CONFIG_GET_ROUTES of the %INSN_DEVICE_CONFIG |
| 521 | * instruction. If this is not initialized by the low-level driver, a |
| 522 | * default handler will be set during post-configuration. |
| 523 | * @get_valid_routes: Optional pointer to a handler for the |
| 524 | * %INSN_DEVICE_CONFIG_GET_ROUTES sub-instruction of the |
| 525 | * %INSN_DEVICE_CONFIG instruction set. If this is not initialized by the |
| 526 | * low-level driver, a default handler that copies zero routes back to the |
| 527 | * user will be used. |
Ian Abbott | 9ac5b44 | 2015-09-21 18:52:50 +0100 | [diff] [blame] | 528 | * |
| 529 | * This is the main control data structure for a COMEDI device (as far as the |
| 530 | * COMEDI core is concerned). There are two groups of COMEDI devices - |
| 531 | * "legacy" devices that are configured by the handler for the |
| 532 | * %COMEDI_DEVCONFIG ioctl, and automatically configured devices resulting |
| 533 | * from a call to comedi_auto_config() as a result of a bus driver probe in |
| 534 | * a low-level COMEDI driver. The "legacy" COMEDI devices are allocated |
| 535 | * during module initialization if the "comedi_num_legacy_minors" module |
| 536 | * parameter is non-zero and use minor device numbers from 0 to |
| 537 | * comedi_num_legacy_minors minus one. The automatically configured COMEDI |
| 538 | * devices are allocated on demand and use minor device numbers from |
| 539 | * comedi_num_legacy_minors to 47. |
| 540 | */ |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 541 | struct comedi_device { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 542 | int use_count; |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 543 | struct comedi_driver *driver; |
H Hartley Sweeten | 43db062 | 2015-02-23 14:57:29 -0700 | [diff] [blame] | 544 | struct comedi_8254 *pacer; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 545 | void *private; |
| 546 | |
Bill Pemberton | 0bfbbe8 | 2009-03-16 22:05:36 -0400 | [diff] [blame] | 547 | struct device *class_dev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 548 | int minor; |
Ian Abbott | ef77c0b | 2013-11-08 15:03:29 +0000 | [diff] [blame] | 549 | unsigned int detach_count; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 550 | struct device *hw_dev; |
| 551 | |
| 552 | const char *board_name; |
| 553 | const void *board_ptr; |
Giulio Benetti | fd02c95 | 2018-06-12 16:50:29 +0200 | [diff] [blame] | 554 | unsigned int attached:1; |
| 555 | unsigned int ioenabled:1; |
Ian Abbott | 214e384 | 2015-10-12 18:03:24 +0100 | [diff] [blame] | 556 | spinlock_t spinlock; /* generic spin-lock for low-level driver */ |
| 557 | struct mutex mutex; /* generic mutex for COMEDI core */ |
Ian Abbott | 2f3fdcd | 2013-11-08 15:03:24 +0000 | [diff] [blame] | 558 | struct rw_semaphore attach_lock; |
Ian Abbott | 5b13ed9 | 2013-11-08 15:03:32 +0000 | [diff] [blame] | 559 | struct kref refcount; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 560 | |
| 561 | int n_subdevices; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 562 | struct comedi_subdevice *subdevices; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 563 | |
| 564 | /* dumb */ |
H Hartley Sweeten | d7e6dc1 | 2014-07-29 15:01:20 -0700 | [diff] [blame] | 565 | void __iomem *mmio; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 566 | unsigned long iobase; |
H Hartley Sweeten | 316f97f | 2013-04-18 14:31:29 -0700 | [diff] [blame] | 567 | unsigned long iolen; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 568 | unsigned int irq; |
| 569 | |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 570 | struct comedi_subdevice *read_subdev; |
| 571 | struct comedi_subdevice *write_subdev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 572 | |
| 573 | struct fasync_struct *async_queue; |
| 574 | |
Monam Agarwal | 0cb4c151 | 2014-02-25 14:51:26 +0530 | [diff] [blame] | 575 | int (*open)(struct comedi_device *dev); |
| 576 | void (*close)(struct comedi_device *dev); |
Spencer E. Olson | d7569ad | 2018-10-03 14:56:02 -0600 | [diff] [blame] | 577 | int (*insn_device_config)(struct comedi_device *dev, |
| 578 | struct comedi_insn *insn, unsigned int *data); |
| 579 | unsigned int (*get_valid_routes)(struct comedi_device *dev, |
| 580 | unsigned int n_pairs, |
| 581 | unsigned int *pair_data); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 582 | }; |
| 583 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 584 | /* |
| 585 | * function prototypes |
| 586 | */ |
| 587 | |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 588 | void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 589 | |
Aniket Sharma | ae6b086 | 2016-03-27 10:52:19 -0700 | [diff] [blame] | 590 | struct comedi_device *comedi_dev_get_from_minor(unsigned int minor); |
Ian Abbott | b449c1c | 2013-11-08 15:03:33 +0000 | [diff] [blame] | 591 | int comedi_dev_put(struct comedi_device *dev); |
H Hartley Sweeten | 85104e9 | 2012-12-19 15:34:40 -0700 | [diff] [blame] | 592 | |
H Hartley Sweeten | e0dac31 | 2012-12-19 15:42:47 -0700 | [diff] [blame] | 593 | bool comedi_is_subdevice_running(struct comedi_subdevice *s); |
H Hartley Sweeten | 0480bcb | 2013-06-19 15:24:36 -0700 | [diff] [blame] | 594 | |
| 595 | void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size); |
Ian Abbott | 8fc369a | 2015-04-21 13:18:10 +0100 | [diff] [blame] | 596 | void comedi_set_spriv_auto_free(struct comedi_subdevice *s); |
H Hartley Sweeten | e0dac31 | 2012-12-19 15:42:47 -0700 | [diff] [blame] | 597 | |
Mark Rankilor | 5e22011 | 2010-05-06 17:49:37 +0800 | [diff] [blame] | 598 | int comedi_check_chanlist(struct comedi_subdevice *s, |
| 599 | int n, |
| 600 | unsigned int *chanlist); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 601 | |
| 602 | /* range stuff */ |
| 603 | |
Ian Abbott | eb36cc2 | 2015-10-12 18:03:25 +0100 | [diff] [blame] | 604 | #define RANGE(a, b) {(a) * 1e6, (b) * 1e6, 0} |
| 605 | #define RANGE_ext(a, b) {(a) * 1e6, (b) * 1e6, RF_EXTERNAL} |
| 606 | #define RANGE_mA(a, b) {(a) * 1e6, (b) * 1e6, UNIT_mA} |
| 607 | #define RANGE_unitless(a, b) {(a) * 1e6, (b) * 1e6, 0} |
| 608 | #define BIP_RANGE(a) {-(a) * 1e6, (a) * 1e6, 0} |
| 609 | #define UNI_RANGE(a) {0, (a) * 1e6, 0} |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 610 | |
Bill Pemberton | 9ced1de | 2009-03-16 22:05:31 -0400 | [diff] [blame] | 611 | extern const struct comedi_lrange range_bipolar10; |
| 612 | extern const struct comedi_lrange range_bipolar5; |
| 613 | extern const struct comedi_lrange range_bipolar2_5; |
| 614 | extern const struct comedi_lrange range_unipolar10; |
| 615 | extern const struct comedi_lrange range_unipolar5; |
H Hartley Sweeten | 5f8eb72 | 2013-04-03 13:38:26 -0700 | [diff] [blame] | 616 | extern const struct comedi_lrange range_unipolar2_5; |
H Hartley Sweeten | 2c71c4f | 2013-04-03 13:40:13 -0700 | [diff] [blame] | 617 | extern const struct comedi_lrange range_0_20mA; |
| 618 | extern const struct comedi_lrange range_4_20mA; |
| 619 | extern const struct comedi_lrange range_0_32mA; |
Bill Pemberton | 9ced1de | 2009-03-16 22:05:31 -0400 | [diff] [blame] | 620 | extern const struct comedi_lrange range_unknown; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 621 | |
| 622 | #define range_digital range_unipolar5 |
| 623 | |
Ian Abbott | 5459bc1 | 2015-09-21 18:52:53 +0100 | [diff] [blame] | 624 | /** |
| 625 | * struct comedi_lrange - Describes a COMEDI range table |
| 626 | * @length: Number of entries in the range table. |
| 627 | * @range: Array of &struct comedi_krange, one for each range. |
| 628 | * |
| 629 | * Each element of @range[] describes the minimum and maximum physical range |
Ethan Edwards | f6a1a42 | 2020-08-24 20:29:55 -0400 | [diff] [blame] | 630 | * and the type of units. Typically, the type of unit is %UNIT_volt |
Ian Abbott | 5459bc1 | 2015-09-21 18:52:53 +0100 | [diff] [blame] | 631 | * (i.e. volts) and the minimum and maximum are in millionths of a volt. |
| 632 | * There may also be a flag that indicates the minimum and maximum are merely |
| 633 | * scale factors for an unknown, external reference. |
| 634 | */ |
Bill Pemberton | 9ced1de | 2009-03-16 22:05:31 -0400 | [diff] [blame] | 635 | struct comedi_lrange { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 636 | int length; |
Cheah Kok Cheong | 3358a0c | 2016-12-22 03:13:19 +0800 | [diff] [blame] | 637 | struct comedi_krange range[]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 638 | }; |
| 639 | |
Ian Abbott | 42e5583 | 2015-09-21 18:52:56 +0100 | [diff] [blame] | 640 | /** |
| 641 | * comedi_range_is_bipolar() - Test if subdevice range is bipolar |
| 642 | * @s: COMEDI subdevice. |
| 643 | * @range: Index of range within a range table. |
| 644 | * |
| 645 | * Tests whether a range is bipolar by checking whether its minimum value |
| 646 | * is negative. |
| 647 | * |
| 648 | * Assumes @range is valid. Does not work for subdevices using a |
| 649 | * channel-specific range table list. |
| 650 | * |
| 651 | * Return: |
| 652 | * %true if the range is bipolar. |
| 653 | * %false if the range is unipolar. |
| 654 | */ |
H Hartley Sweeten | e3693fd | 2013-06-05 15:52:31 -0700 | [diff] [blame] | 655 | static inline bool comedi_range_is_bipolar(struct comedi_subdevice *s, |
| 656 | unsigned int range) |
| 657 | { |
| 658 | return s->range_table->range[range].min < 0; |
| 659 | } |
| 660 | |
Ian Abbott | 42e5583 | 2015-09-21 18:52:56 +0100 | [diff] [blame] | 661 | /** |
| 662 | * comedi_range_is_unipolar() - Test if subdevice range is unipolar |
| 663 | * @s: COMEDI subdevice. |
| 664 | * @range: Index of range within a range table. |
| 665 | * |
| 666 | * Tests whether a range is unipolar by checking whether its minimum value |
| 667 | * is at least 0. |
| 668 | * |
| 669 | * Assumes @range is valid. Does not work for subdevices using a |
| 670 | * channel-specific range table list. |
| 671 | * |
| 672 | * Return: |
| 673 | * %true if the range is unipolar. |
| 674 | * %false if the range is bipolar. |
| 675 | */ |
H Hartley Sweeten | e3693fd | 2013-06-05 15:52:31 -0700 | [diff] [blame] | 676 | static inline bool comedi_range_is_unipolar(struct comedi_subdevice *s, |
| 677 | unsigned int range) |
| 678 | { |
| 679 | return s->range_table->range[range].min >= 0; |
| 680 | } |
| 681 | |
Ian Abbott | 42e5583 | 2015-09-21 18:52:56 +0100 | [diff] [blame] | 682 | /** |
| 683 | * comedi_range_is_external() - Test if subdevice range is external |
| 684 | * @s: COMEDI subdevice. |
| 685 | * @range: Index of range within a range table. |
| 686 | * |
| 687 | * Tests whether a range is externally reference by checking whether its |
| 688 | * %RF_EXTERNAL flag is set. |
| 689 | * |
| 690 | * Assumes @range is valid. Does not work for subdevices using a |
| 691 | * channel-specific range table list. |
| 692 | * |
| 693 | * Return: |
| 694 | * %true if the range is external. |
| 695 | * %false if the range is internal. |
| 696 | */ |
H Hartley Sweeten | a8b677c | 2014-07-14 12:23:39 -0700 | [diff] [blame] | 697 | static inline bool comedi_range_is_external(struct comedi_subdevice *s, |
| 698 | unsigned int range) |
| 699 | { |
| 700 | return !!(s->range_table->range[range].flags & RF_EXTERNAL); |
| 701 | } |
| 702 | |
Ian Abbott | 42e5583 | 2015-09-21 18:52:56 +0100 | [diff] [blame] | 703 | /** |
| 704 | * comedi_chan_range_is_bipolar() - Test if channel-specific range is bipolar |
| 705 | * @s: COMEDI subdevice. |
| 706 | * @chan: The channel number. |
| 707 | * @range: Index of range within a range table. |
| 708 | * |
| 709 | * Tests whether a range is bipolar by checking whether its minimum value |
| 710 | * is negative. |
| 711 | * |
| 712 | * Assumes @chan and @range are valid. Only works for subdevices with a |
| 713 | * channel-specific range table list. |
| 714 | * |
| 715 | * Return: |
| 716 | * %true if the range is bipolar. |
| 717 | * %false if the range is unipolar. |
| 718 | */ |
H Hartley Sweeten | 4916211 | 2013-09-25 15:35:06 -0700 | [diff] [blame] | 719 | static inline bool comedi_chan_range_is_bipolar(struct comedi_subdevice *s, |
| 720 | unsigned int chan, |
| 721 | unsigned int range) |
| 722 | { |
| 723 | return s->range_table_list[chan]->range[range].min < 0; |
| 724 | } |
| 725 | |
Ian Abbott | 42e5583 | 2015-09-21 18:52:56 +0100 | [diff] [blame] | 726 | /** |
| 727 | * comedi_chan_range_is_unipolar() - Test if channel-specific range is unipolar |
| 728 | * @s: COMEDI subdevice. |
| 729 | * @chan: The channel number. |
| 730 | * @range: Index of range within a range table. |
| 731 | * |
| 732 | * Tests whether a range is unipolar by checking whether its minimum value |
| 733 | * is at least 0. |
| 734 | * |
| 735 | * Assumes @chan and @range are valid. Only works for subdevices with a |
| 736 | * channel-specific range table list. |
| 737 | * |
| 738 | * Return: |
| 739 | * %true if the range is unipolar. |
| 740 | * %false if the range is bipolar. |
| 741 | */ |
H Hartley Sweeten | 4916211 | 2013-09-25 15:35:06 -0700 | [diff] [blame] | 742 | static inline bool comedi_chan_range_is_unipolar(struct comedi_subdevice *s, |
| 743 | unsigned int chan, |
| 744 | unsigned int range) |
| 745 | { |
| 746 | return s->range_table_list[chan]->range[range].min >= 0; |
| 747 | } |
| 748 | |
Ian Abbott | 42e5583 | 2015-09-21 18:52:56 +0100 | [diff] [blame] | 749 | /** |
| 750 | * comedi_chan_range_is_external() - Test if channel-specific range is external |
| 751 | * @s: COMEDI subdevice. |
| 752 | * @chan: The channel number. |
| 753 | * @range: Index of range within a range table. |
| 754 | * |
| 755 | * Tests whether a range is externally reference by checking whether its |
| 756 | * %RF_EXTERNAL flag is set. |
| 757 | * |
| 758 | * Assumes @chan and @range are valid. Only works for subdevices with a |
| 759 | * channel-specific range table list. |
| 760 | * |
| 761 | * Return: |
| 762 | * %true if the range is bipolar. |
| 763 | * %false if the range is unipolar. |
| 764 | */ |
H Hartley Sweeten | a8b677c | 2014-07-14 12:23:39 -0700 | [diff] [blame] | 765 | static inline bool comedi_chan_range_is_external(struct comedi_subdevice *s, |
| 766 | unsigned int chan, |
| 767 | unsigned int range) |
| 768 | { |
| 769 | return !!(s->range_table_list[chan]->range[range].flags & RF_EXTERNAL); |
| 770 | } |
| 771 | |
Ian Abbott | 42e5583 | 2015-09-21 18:52:56 +0100 | [diff] [blame] | 772 | /** |
| 773 | * comedi_offset_munge() - Convert between offset binary and 2's complement |
| 774 | * @s: COMEDI subdevice. |
| 775 | * @val: Value to be converted. |
| 776 | * |
| 777 | * Toggles the highest bit of a sample value to toggle between offset binary |
| 778 | * and 2's complement. Assumes that @s->maxdata is a power of 2 minus 1. |
| 779 | * |
| 780 | * Return: The converted value. |
| 781 | */ |
H Hartley Sweeten | f0b215d | 2013-09-18 11:49:33 -0700 | [diff] [blame] | 782 | static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s, |
| 783 | unsigned int val) |
| 784 | { |
| 785 | return val ^ s->maxdata ^ (s->maxdata >> 1); |
| 786 | } |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 787 | |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 788 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 789 | * comedi_bytes_per_sample() - Determine subdevice sample size |
| 790 | * @s: COMEDI subdevice. |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 791 | * |
| 792 | * The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 793 | * whether the %SDF_LSAMPL subdevice flag is set or not. |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 794 | * |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 795 | * Return: The subdevice sample size. |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 796 | */ |
| 797 | static inline unsigned int comedi_bytes_per_sample(struct comedi_subdevice *s) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 798 | { |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 799 | return s->subdev_flags & SDF_LSAMPL ? sizeof(int) : sizeof(short); |
| 800 | } |
Kinka Huang | cb3aada | 2014-07-15 23:11:02 +0800 | [diff] [blame] | 801 | |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 802 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 803 | * comedi_sample_shift() - Determine log2 of subdevice sample size |
| 804 | * @s: COMEDI subdevice. |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 805 | * |
| 806 | * The sample size will be 4 (sizeof int) or 2 (sizeof short) depending on |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 807 | * whether the %SDF_LSAMPL subdevice flag is set or not. The log2 of the |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 808 | * sample size will be 2 or 1 and can be used as the right operand of a |
| 809 | * bit-shift operator to multiply or divide something by the sample size. |
| 810 | * |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 811 | * Return: log2 of the subdevice sample size. |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 812 | */ |
| 813 | static inline unsigned int comedi_sample_shift(struct comedi_subdevice *s) |
| 814 | { |
| 815 | return s->subdev_flags & SDF_LSAMPL ? 2 : 1; |
| 816 | } |
| 817 | |
| 818 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 819 | * comedi_bytes_to_samples() - Convert a number of bytes to a number of samples |
| 820 | * @s: COMEDI subdevice. |
| 821 | * @nbytes: Number of bytes |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 822 | * |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 823 | * Return: The number of bytes divided by the subdevice sample size. |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 824 | */ |
| 825 | static inline unsigned int comedi_bytes_to_samples(struct comedi_subdevice *s, |
| 826 | unsigned int nbytes) |
| 827 | { |
| 828 | return nbytes >> comedi_sample_shift(s); |
| 829 | } |
| 830 | |
| 831 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 832 | * comedi_samples_to_bytes() - Convert a number of samples to a number of bytes |
| 833 | * @s: COMEDI subdevice. |
| 834 | * @nsamples: Number of samples. |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 835 | * |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 836 | * Return: The number of samples multiplied by the subdevice sample size. |
| 837 | * (Does not check for arithmetic overflow.) |
Ian Abbott | bf33eb4 | 2014-10-23 13:47:51 +0100 | [diff] [blame] | 838 | */ |
| 839 | static inline unsigned int comedi_samples_to_bytes(struct comedi_subdevice *s, |
| 840 | unsigned int nsamples) |
| 841 | { |
| 842 | return nsamples << comedi_sample_shift(s); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 843 | } |
| 844 | |
Ian Abbott | 5e1a619 | 2015-03-27 19:13:41 +0000 | [diff] [blame] | 845 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 846 | * comedi_check_trigger_src() - Trivially validate a comedi_cmd trigger source |
| 847 | * @src: Pointer to the trigger source to validate. |
| 848 | * @flags: Bitmask of valid %TRIG_* for the trigger. |
Ian Abbott | 5e1a619 | 2015-03-27 19:13:41 +0000 | [diff] [blame] | 849 | * |
| 850 | * This is used in "step 1" of the do_cmdtest functions of comedi drivers |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 851 | * to validate the comedi_cmd triggers. The mask of the @src against the |
Ian Abbott | 5e1a619 | 2015-03-27 19:13:41 +0000 | [diff] [blame] | 852 | * @flags allows the userspace comedilib to pass all the comedi_cmd |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 853 | * triggers as %TRIG_ANY and get back a bitmask of the valid trigger sources. |
| 854 | * |
| 855 | * Return: |
| 856 | * 0 if trigger sources in *@src are all supported. |
| 857 | * -EINVAL if any trigger source in *@src is unsupported. |
Ian Abbott | 5e1a619 | 2015-03-27 19:13:41 +0000 | [diff] [blame] | 858 | */ |
| 859 | static inline int comedi_check_trigger_src(unsigned int *src, |
| 860 | unsigned int flags) |
| 861 | { |
| 862 | unsigned int orig_src = *src; |
| 863 | |
| 864 | *src = orig_src & flags; |
| 865 | if (*src == TRIG_INVALID || *src != orig_src) |
| 866 | return -EINVAL; |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 871 | * comedi_check_trigger_is_unique() - Make sure a trigger source is unique |
| 872 | * @src: The trigger source to check. |
| 873 | * |
| 874 | * Return: |
| 875 | * 0 if no more than one trigger source is set. |
| 876 | * -EINVAL if more than one trigger source is set. |
Ian Abbott | 5e1a619 | 2015-03-27 19:13:41 +0000 | [diff] [blame] | 877 | */ |
| 878 | static inline int comedi_check_trigger_is_unique(unsigned int src) |
| 879 | { |
| 880 | /* this test is true if more than one _src bit is set */ |
| 881 | if ((src & (src - 1)) != 0) |
| 882 | return -EINVAL; |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 887 | * comedi_check_trigger_arg_is() - Trivially validate a trigger argument |
| 888 | * @arg: Pointer to the trigger arg to validate. |
| 889 | * @val: The value the argument should be. |
| 890 | * |
| 891 | * Forces *@arg to be @val. |
| 892 | * |
| 893 | * Return: |
| 894 | * 0 if *@arg was already @val. |
| 895 | * -EINVAL if *@arg differed from @val. |
Ian Abbott | 5e1a619 | 2015-03-27 19:13:41 +0000 | [diff] [blame] | 896 | */ |
| 897 | static inline int comedi_check_trigger_arg_is(unsigned int *arg, |
| 898 | unsigned int val) |
| 899 | { |
| 900 | if (*arg != val) { |
| 901 | *arg = val; |
| 902 | return -EINVAL; |
| 903 | } |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 908 | * comedi_check_trigger_arg_min() - Trivially validate a trigger argument min |
| 909 | * @arg: Pointer to the trigger arg to validate. |
| 910 | * @val: The minimum value the argument should be. |
| 911 | * |
| 912 | * Forces *@arg to be at least @val, setting it to @val if necessary. |
| 913 | * |
| 914 | * Return: |
| 915 | * 0 if *@arg was already at least @val. |
| 916 | * -EINVAL if *@arg was less than @val. |
Ian Abbott | 5e1a619 | 2015-03-27 19:13:41 +0000 | [diff] [blame] | 917 | */ |
| 918 | static inline int comedi_check_trigger_arg_min(unsigned int *arg, |
| 919 | unsigned int val) |
| 920 | { |
| 921 | if (*arg < val) { |
| 922 | *arg = val; |
| 923 | return -EINVAL; |
| 924 | } |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | /** |
Ian Abbott | 8f78264 | 2015-09-21 18:52:55 +0100 | [diff] [blame] | 929 | * comedi_check_trigger_arg_max() - Trivially validate a trigger argument max |
| 930 | * @arg: Pointer to the trigger arg to validate. |
| 931 | * @val: The maximum value the argument should be. |
| 932 | * |
| 933 | * Forces *@arg to be no more than @val, setting it to @val if necessary. |
| 934 | * |
| 935 | * Return: |
| 936 | * 0 if*@arg was already no more than @val. |
| 937 | * -EINVAL if *@arg was greater than @val. |
Ian Abbott | 5e1a619 | 2015-03-27 19:13:41 +0000 | [diff] [blame] | 938 | */ |
| 939 | static inline int comedi_check_trigger_arg_max(unsigned int *arg, |
| 940 | unsigned int val) |
| 941 | { |
| 942 | if (*arg > val) { |
| 943 | *arg = val; |
| 944 | return -EINVAL; |
| 945 | } |
| 946 | return 0; |
| 947 | } |
| 948 | |
Ian Abbott | bc3954b | 2013-01-29 16:20:17 +0000 | [diff] [blame] | 949 | /* |
| 950 | * Must set dev->hw_dev if you wish to dma directly into comedi's buffer. |
| 951 | * Also useful for retrieving a previously configured hardware device of |
| 952 | * known bus type. Set automatically for auto-configured devices. |
| 953 | * Automatically set to NULL when detaching hardware device. |
| 954 | */ |
Ian Abbott | da71751 | 2013-02-01 13:23:19 +0000 | [diff] [blame] | 955 | int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 956 | |
Ian Abbott | 42e5583 | 2015-09-21 18:52:56 +0100 | [diff] [blame] | 957 | /** |
| 958 | * comedi_buf_n_bytes_ready - Determine amount of unread data in buffer |
| 959 | * @s: COMEDI subdevice. |
| 960 | * |
| 961 | * Determines the number of bytes of unread data in the asynchronous |
| 962 | * acquisition data buffer for a subdevice. The data in question might not |
| 963 | * have been fully "munged" yet. |
| 964 | * |
| 965 | * Returns: The amount of unread data in bytes. |
| 966 | */ |
H Hartley Sweeten | f4f3f7c | 2014-06-20 10:58:28 -0700 | [diff] [blame] | 967 | static inline unsigned int comedi_buf_n_bytes_ready(struct comedi_subdevice *s) |
| 968 | { |
| 969 | return s->async->buf_write_count - s->async->buf_read_count; |
| 970 | } |
| 971 | |
Ian Abbott | 24e894b | 2014-05-06 13:12:04 +0100 | [diff] [blame] | 972 | unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s, unsigned int n); |
Ian Abbott | 940dd35 | 2014-05-06 13:12:05 +0100 | [diff] [blame] | 973 | unsigned int comedi_buf_write_free(struct comedi_subdevice *s, unsigned int n); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 974 | |
Ian Abbott | e9edef3 | 2014-05-06 13:12:09 +0100 | [diff] [blame] | 975 | unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s); |
Ian Abbott | d13be55 | 2014-05-06 13:12:07 +0100 | [diff] [blame] | 976 | unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s, unsigned int n); |
Ian Abbott | f1df866 | 2014-05-06 13:12:08 +0100 | [diff] [blame] | 977 | unsigned int comedi_buf_read_free(struct comedi_subdevice *s, unsigned int n); |
H Hartley Sweeten | 8ae560a | 2013-01-09 13:32:56 -0700 | [diff] [blame] | 978 | |
H Hartley Sweeten | 5438da8 | 2014-10-22 15:36:24 -0700 | [diff] [blame] | 979 | unsigned int comedi_buf_write_samples(struct comedi_subdevice *s, |
| 980 | const void *data, unsigned int nsamples); |
H Hartley Sweeten | 4455d7c | 2014-10-22 14:36:34 -0700 | [diff] [blame] | 981 | unsigned int comedi_buf_read_samples(struct comedi_subdevice *s, |
| 982 | void *data, unsigned int nsamples); |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 983 | |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 984 | /* drivers.c - general comedi driver functions */ |
| 985 | |
H Hartley Sweeten | 9150640 | 2014-02-10 11:49:00 -0700 | [diff] [blame] | 986 | #define COMEDI_TIMEOUT_MS 1000 |
| 987 | |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 988 | int comedi_timeout(struct comedi_device *dev, struct comedi_subdevice *s, |
| 989 | struct comedi_insn *insn, |
| 990 | int (*cb)(struct comedi_device *dev, |
| 991 | struct comedi_subdevice *s, |
| 992 | struct comedi_insn *insn, unsigned long context), |
H Hartley Sweeten | 9150640 | 2014-02-10 11:49:00 -0700 | [diff] [blame] | 993 | unsigned long context); |
| 994 | |
Ian Abbott | 5a78035 | 2014-09-15 13:46:01 +0100 | [diff] [blame] | 995 | unsigned int comedi_handle_events(struct comedi_device *dev, |
| 996 | struct comedi_subdevice *s); |
| 997 | |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 998 | int comedi_dio_insn_config(struct comedi_device *dev, |
| 999 | struct comedi_subdevice *s, |
| 1000 | struct comedi_insn *insn, unsigned int *data, |
H Hartley Sweeten | e523c6c | 2013-08-06 09:31:35 -0700 | [diff] [blame] | 1001 | unsigned int mask); |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1002 | unsigned int comedi_dio_update_state(struct comedi_subdevice *s, |
H Hartley Sweeten | 05e60b1 | 2013-08-30 11:04:56 -0700 | [diff] [blame] | 1003 | unsigned int *data); |
Ian Abbott | bafd9c6 | 2019-03-04 14:33:54 +0000 | [diff] [blame] | 1004 | unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s, |
| 1005 | struct comedi_cmd *cmd); |
Ian Abbott | f146fe6 | 2014-09-15 13:45:57 +0100 | [diff] [blame] | 1006 | unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s); |
H Hartley Sweeten | 2ee3775 | 2014-11-05 10:31:29 -0700 | [diff] [blame] | 1007 | unsigned int comedi_nscans_left(struct comedi_subdevice *s, |
| 1008 | unsigned int nscans); |
H Hartley Sweeten | f615915 | 2014-11-05 10:31:33 -0700 | [diff] [blame] | 1009 | unsigned int comedi_nsamples_left(struct comedi_subdevice *s, |
| 1010 | unsigned int nsamples); |
Ian Abbott | 2b4e1f6 | 2014-09-15 13:45:59 +0100 | [diff] [blame] | 1011 | void comedi_inc_scan_progress(struct comedi_subdevice *s, |
| 1012 | unsigned int num_bytes); |
H Hartley Sweeten | e523c6c | 2013-08-06 09:31:35 -0700 | [diff] [blame] | 1013 | |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1014 | void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size); |
| 1015 | int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices); |
| 1016 | int comedi_alloc_subdev_readback(struct comedi_subdevice *s); |
H Hartley Sweeten | d276206 | 2014-08-25 16:03:54 -0700 | [diff] [blame] | 1017 | |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1018 | int comedi_readback_insn_read(struct comedi_device *dev, |
| 1019 | struct comedi_subdevice *s, |
| 1020 | struct comedi_insn *insn, unsigned int *data); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1021 | |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1022 | int comedi_load_firmware(struct comedi_device *dev, struct device *hw_dev, |
H Hartley Sweeten | 9ff8b15 | 2013-05-17 11:17:00 -0700 | [diff] [blame] | 1023 | const char *name, |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1024 | int (*cb)(struct comedi_device *dev, |
H Hartley Sweeten | d569541 | 2013-05-17 11:18:01 -0700 | [diff] [blame] | 1025 | const u8 *data, size_t size, |
| 1026 | unsigned long context), |
| 1027 | unsigned long context); |
H Hartley Sweeten | 9ff8b15 | 2013-05-17 11:17:00 -0700 | [diff] [blame] | 1028 | |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1029 | int __comedi_request_region(struct comedi_device *dev, |
H Hartley Sweeten | ca8b296 | 2013-04-09 16:30:11 -0700 | [diff] [blame] | 1030 | unsigned long start, unsigned long len); |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1031 | int comedi_request_region(struct comedi_device *dev, |
H Hartley Sweeten | f375ac5 | 2013-04-09 16:05:54 -0700 | [diff] [blame] | 1032 | unsigned long start, unsigned long len); |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1033 | void comedi_legacy_detach(struct comedi_device *dev); |
H Hartley Sweeten | f375ac5 | 2013-04-09 16:05:54 -0700 | [diff] [blame] | 1034 | |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1035 | int comedi_auto_config(struct device *hardware_device, |
| 1036 | struct comedi_driver *driver, unsigned long context); |
| 1037 | void comedi_auto_unconfig(struct device *hardware_device); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1038 | |
Ian Abbott | 9fe3b62 | 2016-12-15 13:19:51 +0000 | [diff] [blame] | 1039 | int comedi_driver_register(struct comedi_driver *driver); |
| 1040 | void comedi_driver_unregister(struct comedi_driver *driver); |
H Hartley Sweeten | 1ae6b20 | 2013-01-30 15:24:38 -0700 | [diff] [blame] | 1041 | |
| 1042 | /** |
| 1043 | * module_comedi_driver() - Helper macro for registering a comedi driver |
| 1044 | * @__comedi_driver: comedi_driver struct |
| 1045 | * |
| 1046 | * Helper macro for comedi drivers which do not do anything special in module |
| 1047 | * init/exit. This eliminates a lot of boilerplate. Each module may only use |
| 1048 | * this macro once, and calling it replaces module_init() and module_exit(). |
| 1049 | */ |
| 1050 | #define module_comedi_driver(__comedi_driver) \ |
| 1051 | module_driver(__comedi_driver, comedi_driver_register, \ |
| 1052 | comedi_driver_unregister) |
Ian Abbott | 581a7dd | 2012-11-14 13:10:40 +0000 | [diff] [blame] | 1053 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1054 | #endif /* _COMEDIDEV_H */ |