comedidev.h 40 KB

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