comedidev.h 40 KB

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