pcilib.h 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. #ifndef _PCILIB_H
  2. #define _PCILIB_H
  3. #include <sys/time.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <stdarg.h>
  7. typedef struct pcilib_s pcilib_t;
  8. typedef struct pcilib_event_context_s pcilib_context_t;
  9. typedef uint32_t pcilib_version_t;
  10. typedef uint8_t pcilib_bar_t; /**< Type holding the PCI Bar number */
  11. typedef uint16_t pcilib_register_t; /**< Type holding the register position within the field listing registers in the model */
  12. typedef uint16_t pcilib_view_t; /**< Type holding the register view position within view listing in the model */
  13. typedef uint16_t pcilib_unit_t; /**< Type holding the value unit position within unit listing in the model */
  14. typedef uint32_t pcilib_register_addr_t; /**< Type holding the register address within address-space of BARs */
  15. typedef uint8_t pcilib_register_size_t; /**< Type holding the size in bits of the register */
  16. typedef uint64_t pcilib_register_value_t; /**< Type holding the register value */
  17. typedef uint8_t pcilib_dma_engine_addr_t;
  18. typedef uint8_t pcilib_dma_engine_t;
  19. typedef uint64_t pcilib_event_id_t;
  20. typedef uint32_t pcilib_event_t;
  21. typedef uint64_t pcilib_timeout_t; /**< In microseconds */
  22. typedef unsigned int pcilib_irq_hw_source_t;
  23. typedef uint32_t pcilib_irq_source_t;
  24. typedef struct _xmlNode pcilib_xml_node_t;
  25. typedef enum {
  26. PCILIB_LOG_DEBUG = 0, /**< Debug messages will be always printed as they should be filtered based on setting of corresponding environmental variable */
  27. PCILIB_LOG_INFO, /**< Informational message are suppresed by default */
  28. PCILIB_LOG_WARNING, /**< Warnings messages indicate that something unexpected happen, but application can continue */
  29. PCILIB_LOG_ERROR /**< The error which is impossible to handle on this level of library */
  30. } pcilib_log_priority_t;
  31. typedef enum {
  32. PCILIB_HOST_ENDIAN = 0, /**< The same byte ordering as on the host system running the driver */
  33. PCILIB_LITTLE_ENDIAN, /**< x86 is Little-endian, least significant bytes are at the lower addresses */
  34. PCILIB_BIG_ENDIAN /**< Old mainframes and network byte order, most significant bytes are at the lower addresses */
  35. } pcilib_endianess_t;
  36. typedef enum {
  37. PCILIB_ACCESS_R = 1, /**< getting property is allowed */
  38. PCILIB_ACCESS_W = 2, /**< setting property is allowed */
  39. PCILIB_ACCESS_RW = 3,
  40. PCILIB_ACCESS_INCONSISTENT = 0x10000 /**< inconsistent access, one will not read that one has written */
  41. } pcilib_access_mode_t;
  42. typedef enum {
  43. PCILIB_REGISTER_R = 1, /**< reading from register is allowed */
  44. PCILIB_REGISTER_W = 2, /**< normal writting to register is allowed */
  45. PCILIB_REGISTER_RW = 3,
  46. PCILIB_REGISTER_W1C = 4, /**< writting 1 resets the bit, writting 0 keeps the value */
  47. PCILIB_REGISTER_RW1C = 5,
  48. PCILIB_REGISTER_W1I = 8, /**< writting 1 inversts the bit, writting 0 keeps the value */
  49. PCILIB_REGISTER_RW1I = 9,
  50. PCILIB_REGISTER_INCONSISTENT = 0x10000 /**< inconsistent register, writting and reading does not match */
  51. } pcilib_register_mode_t;
  52. typedef enum {
  53. PCILIB_TYPE_INVALID = 0, /**< uninitialized */
  54. PCILIB_TYPE_DEFAULT = 0, /**< default type */
  55. PCILIB_TYPE_STRING = 1, /**< char* */
  56. PCILIB_TYPE_DOUBLE = 2, /**< double */
  57. PCILIB_TYPE_LONG = 3
  58. } pcilib_value_type_t;
  59. typedef enum {
  60. PCILIB_DMA_IRQ = 1,
  61. PCILIB_EVENT_IRQ = 2
  62. } pcilib_irq_type_t;
  63. typedef enum { /**< 0x8000 and up are reserved for driver-specific types */
  64. PCILIB_EVENT_DATA = 0, /**< default data format */
  65. PCILIB_EVENT_RAW_DATA = 1 /**< raw data */
  66. } pcilib_event_data_type_t;
  67. typedef enum {
  68. PCILIB_DMA_TO_DEVICE = 1,
  69. PCILIB_DMA_FROM_DEVICE = 2,
  70. PCILIB_DMA_BIDIRECTIONAL = 3
  71. } pcilib_dma_direction_t;
  72. typedef enum {
  73. PCILIB_DMA_FLAGS_DEFAULT = 0,
  74. PCILIB_DMA_FLAG_EOP = 1, /**< last buffer of the packet */
  75. PCILIB_DMA_FLAG_WAIT = 2, /**< wait completion of write operation / wait for data during read operation */
  76. PCILIB_DMA_FLAG_MULTIPACKET = 4, /**< read multiple packets */
  77. PCILIB_DMA_FLAG_PERSISTENT = 8, /**< do not stop DMA engine on application termination / permanently close DMA engine on dma_stop */
  78. PCILIB_DMA_FLAG_IGNORE_ERRORS = 16, /**< do not crash on errors, but return appropriate error codes */
  79. PCILIB_DMA_FLAG_STOP = 32 /**< indicates that we actually calling pcilib_dma_start to stop persistent DMA engine */
  80. } pcilib_dma_flags_t;
  81. typedef enum {
  82. PCILIB_STREAMING_STOP = 0, /**< stop streaming */
  83. PCILIB_STREAMING_CONTINUE = 1, /**< wait DMA timeout and return gracefuly if no new data appeared */
  84. PCILIB_STREAMING_WAIT = 2, /**< wait the specified timeout and return gracefuly if no new data appeared */
  85. PCILIB_STREAMING_CHECK = 3, /**< check if more data is available without waiting, return gracefuly if no data is ready */
  86. PCILIB_STREAMING_TIMEOUT_MASK = 3, /**< mask specifying all timeout modes */
  87. PCILIB_STREAMING_FAIL = 4, /**< a flag indicating that the error should be generated if no data is available upon the timeout (whatever timeout mode is used) */
  88. PCILIB_STREAMING_REQ_FRAGMENT = 5, /**< only fragment of a packet is read, wait for next fragment and fail if no data during DMA timeout */
  89. PCILIB_STREAMING_REQ_PACKET = 6 /**< wait for next packet and fail if no data during the specified timeout */
  90. } pcilib_streaming_action_t;
  91. typedef enum {
  92. PCILIB_EVENT_FLAGS_DEFAULT = 0,
  93. PCILIB_EVENT_FLAG_RAW_DATA_ONLY = 1, /**< Do not parse data, just read raw and pass it to rawdata callback. If passed to rawdata callback, idicates the data is not identified as event (most probably just padding) */
  94. PCILIB_EVENT_FLAG_STOP_ONLY = 1, /**< Do not cleanup, just stop acquiring new frames, the cleanup should be requested afterwards */
  95. PCILIB_EVENT_FLAG_EOF = 2, /**< Indicates that it is the last part of the frame (not required) */
  96. PCILIB_EVENT_FLAG_PREPROCESS = 4 /**< Enables preprocessing of the raw data (decoding frames, etc.) */
  97. } pcilib_event_flags_t;
  98. typedef enum {
  99. PCILIB_EVENT_INFO_FLAG_BROKEN = 1 /**< Indicates broken frames (if this flag is fales, the frame still can be broken) */
  100. } pcilib_event_info_flags_t;
  101. typedef enum {
  102. PCILIB_LIST_FLAGS_DEFAULT = 0,
  103. PCILIB_LIST_FLAG_CHILDS = 1 /**< Request all sub-elements or indicated that sub-elements are available */
  104. } pcilib_list_flags_t;
  105. typedef struct {
  106. pcilib_value_type_t type; /**< Current data type */
  107. const char *unit; /**< Units (if known) */
  108. const char *format; /**< requested printf format (may enforce using output in hex form) */
  109. union {
  110. long ival; /**< The value if type = PCILIB_TYPE_LONG */
  111. double fval; /**< The value if type = PCILIB_TYPE_DOUBLE */
  112. const char *sval; /**< The value if type = PCILIB_TYPE_STRING, the pointer may point to static location or reference actual string in str or data */
  113. };
  114. // This is a private part
  115. size_t size; /**< Size of the data */
  116. void *data; /**< Arbitrary data, for instance actual string referenced by the sval */
  117. char str[16]; /**< Used for shorter strings converted from integer/float types */
  118. } pcilib_value_t;
  119. typedef struct {
  120. pcilib_register_value_t min, max; /**< Minimum and maximum allowed values */
  121. } pcilib_register_value_range_t;
  122. typedef struct {
  123. pcilib_register_value_t value; /**< This value will get assigned instead of the name */
  124. pcilib_register_value_t min, max; /**< the values in the specified range are aliased by name */
  125. const char *name; /**< corresponding string to value */
  126. const char *description; /**< longer description */
  127. } pcilib_register_value_name_t;
  128. typedef struct {
  129. pcilib_register_t id; /**< Direct register ID which can be used in API calls */
  130. const char *name; /**< The access name of the register */
  131. const char *description; /**< Brief description of the register */
  132. const char *bank; /**< The name of the bank register belongs to */
  133. pcilib_register_mode_t mode; /**< Register access (ro/wo/rw) and how writting to register works (clearing/inverting set bits) */
  134. pcilib_register_value_t defvalue; /**< Default register value */
  135. const pcilib_register_value_range_t *range; /**< Specifies default, minimum, and maximum values */
  136. const pcilib_register_value_name_t *values; /**< The list of enum names for the register value */
  137. } pcilib_register_info_t;
  138. typedef struct {
  139. const char *name; /**< Name of the property view */
  140. const char *path; /**< Full path to the property */
  141. const char *description; /**< Short description */
  142. pcilib_value_type_t type; /**< The default data type or PCILIB_TYPE_INVALID if directory */
  143. pcilib_access_mode_t mode; /**< Specifies if the view is read/write-only */
  144. pcilib_list_flags_t flags; /**< Indicates if have sub-folders, etc. */
  145. const char *unit; /**< Returned unit (if any) */
  146. } pcilib_property_info_t;
  147. typedef struct {
  148. pcilib_event_t type;
  149. uint64_t seqnum; /**< we will add seqnum_overflow if required */
  150. uint64_t offset; /**< nanoseconds */
  151. struct timeval timestamp; /**< most accurate timestamp */
  152. pcilib_event_info_flags_t flags; /**< flags */
  153. } pcilib_event_info_t;
  154. #define PCILIB_BAR_DETECT ((pcilib_bar_t)-1)
  155. #define PCILIB_BAR_INVALID ((pcilib_bar_t)-1)
  156. #define PCILIB_BAR_NOBAR ((pcilib_bar_t)-2)
  157. #define PCILIB_BAR0 0
  158. #define PCILIB_BAR1 1
  159. #define PCILIB_DMA_ENGINE_INVALID ((pcilib_dma_engine_t)-1)
  160. #define PCILIB_DMA_ENGINE_ALL ((pcilib_dma_engine_t)-1)
  161. #define PCILIB_DMA_FLAGS_DEFAULT ((pcilib_dma_flags_t)0)
  162. #define PCILIB_DMA_ENGINE_ADDR_INVALID ((pcilib_dma_engine_addr_t)-1)
  163. #define PCILIB_REGISTER_INVALID ((pcilib_register_t)-1)
  164. #define PCILIB_REGISTER_ADDRESS_INVALID ((pcilib_register_addr_t)-1)
  165. #define PCILIB_ADDRESS_INVALID ((uintptr_t)-1)
  166. #define PCILIB_EVENT0 1
  167. #define PCILIB_EVENT1 2
  168. #define PCILIB_EVENT2 4
  169. #define PCILIB_EVENT3 8
  170. #define PCILIB_EVENTS_ALL ((pcilib_event_t)-1)
  171. #define PCILIB_EVENT_INVALID ((pcilib_event_t)-1)
  172. #define PCILIB_EVENT_DATA_TYPE_INVALID ((pcilib_event_data_type_t)-1)
  173. #define PCILIB_TIMEOUT_INFINITE ((pcilib_timeout_t)-1)
  174. #define PCILIB_TIMEOUT_IMMEDIATE 0
  175. #define PCILIB_IRQ_TYPE_ALL 0
  176. #define PCILIB_IRQ_SOURCE_DEFAULT 0
  177. #define PCILIB_MODEL_DETECT NULL
  178. /**
  179. * Callback function called when pcilib wants to log a new message
  180. * @param[in,out] arg - logging context provided with pcilib_set_logger() call
  181. * @param[in] file - source file generating the message
  182. * @param[in] line - source line generating the message
  183. * @param[in] prio - the message priority (messages with priority bellow the currently set loglevel are already filtered)
  184. * @param[in] msg - message or printf-style formating string
  185. * @param[in] va - vairable parameters defined by formating string
  186. */
  187. typedef void (*pcilib_logger_t)(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va);
  188. /**
  189. * Callback function called when new data is read by DMA streaming function
  190. * @param[in,out] ctx - DMA Engine context
  191. * @param[in] flags - DMA Flags
  192. * @param[in] bufsize - size of data in bytes
  193. * @param[in] buf - data
  194. * @return - #pcilib_streaming_action_t instructing how the streaming should continue or a negative error code in the case of error
  195. */
  196. typedef int (*pcilib_dma_callback_t)(void *ctx, pcilib_dma_flags_t flags, size_t bufsize, void *buf);
  197. /**
  198. * Callback function called when new event is generated by event engine
  199. * @param[in] event_id - event id
  200. * @param[in] info - event description (depending on event engine may provide additional fields on top of #pcilib_event_info_t)
  201. * @param[in,out] user - User-specific data provided in pcilib_stream() call
  202. * @return - #pcilib_streaming_action_t instructing how the streaming should continue or a negative error code in the case of error
  203. */
  204. typedef int (*pcilib_event_callback_t)(pcilib_event_id_t event_id, const pcilib_event_info_t *info, void *user);
  205. /**
  206. * Callback function called when new portion of raw data is read by event engine
  207. * @param[in] event_id - id of the event this data will be associated with if processing is successful
  208. * @param[in] info - event description (depending on event engine may provide additional fields on top of #pcilib_event_info_t)
  209. * @param[in] flags - may indicate if it is the last portion of data for current event
  210. * @param[in] size - size of data in bytes
  211. * @param[in,out] data - data (the callback may modify the data, but the size should be kept of course)
  212. * @param[in,out] user - User-specific data provided in pcilib_stream() call
  213. * @return - #pcilib_streaming_action_t instructing how the streaming should continue or a negative error code in the case of error
  214. */
  215. typedef int (*pcilib_event_rawdata_callback_t)(pcilib_event_id_t event_id, const pcilib_event_info_t *info, pcilib_event_flags_t flags, size_t size, void *data, void *user);
  216. #ifdef __cplusplus
  217. extern "C" {
  218. #endif
  219. /***********************************************************************************************************//**
  220. * \defgroup public_api_global Global Public API (logging, etc.)
  221. * Global functions which does not require existing pcilib context
  222. * @{
  223. */
  224. /**
  225. * Replaces default logging function.
  226. * @param min_prio - messages with priority below \a min_prio will be ignored
  227. * @param logger - configures a new logging function or restores the default one if NULL is passed
  228. * @param arg - logging context, this parameter will be passed through to the \a logger
  229. * @return - error code or 0 on success
  230. */
  231. int pcilib_set_logger(pcilib_log_priority_t min_prio, pcilib_logger_t logger, void *arg);
  232. /** public_api_global
  233. * @}
  234. */
  235. /***********************************************************************************************************//**
  236. * \defgroup public_api Public API
  237. * Base pcilib functions which does not belong to the specific APIs
  238. * @{
  239. */
  240. /**
  241. * Initializes pcilib context, detects model configuration, and populates model-specific registers.
  242. * Event and DMA engines will not be started automatically, but calls to pcilib_start() / pcilib_start_dma()
  243. * are provided for this purpose. In the end, the context should be cleaned using pcilib_stop().
  244. * @param[in] device - path to the device file [/dev/fpga0]
  245. * @param[in] model - specifies the model of hardware, autodetected if NULL is passed
  246. * @return - initialized context or NULL in the case of error
  247. */
  248. pcilib_t *pcilib_open(const char *device, const char *model);
  249. /**
  250. * Destroy pcilib context and all memory associated with it. The function will stop event engine if necessary,
  251. * but DMA engine stay running if it was already running before pcilib_open() was called
  252. * @param[in,out] ctx - pcilib context
  253. */
  254. void pcilib_close(pcilib_t *ctx);
  255. /**
  256. * Should reset the hardware and software in default state. It is implementation-specific, but is not actively
  257. * used in existing implementations because the hardware is initialized from bash scripts which are often
  258. * changed by Michele and his band. On the software side, it probably should reset all software registers
  259. * to default value and may be additionaly re-start and clear DMA. However, this is not implemented yet.
  260. * @param[in,out] ctx - pcilib context
  261. * @return - error code or 0 on success
  262. */
  263. int pcilib_reset(pcilib_t *ctx);
  264. /** public_api
  265. * @}
  266. */
  267. /***********************************************************************************************************//**
  268. * \defgroup public_api_pci Public PCI API (MMIO)
  269. * API for manipulation with memory-mapped PCI BAR space
  270. * @{
  271. */
  272. /**
  273. * Resolves the phisycal PCI address to a virtual address in the process address space.
  274. * The required BAR will be mapped if necessary.
  275. * @param[in,out] ctx - pcilib context
  276. * @param[in] bar - specifies the BAR address belong to, use PCILIB_BAR_DETECT for autodetection
  277. * @param[in] addr - specifies the physical address on the PCI bus or offset in the BAR if \a bar is specified
  278. * @return - the virtual address in the process address space or NULL in case of error
  279. */
  280. char *pcilib_resolve_bar_address(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr);
  281. /**
  282. * Performs PIO read from the PCI BAR. The BAR will be automatically mapped and unmapped if necessary.
  283. * @param[in,out] ctx - pcilib context
  284. * @param[in] bar - the BAR to read, use PCILIB_BAR_DETECT to detect bar by the specified physical address
  285. * @param[in] addr - absolute physical address to read or the offset in the specified bar
  286. * @param[in] access - word size (access width in bytes)
  287. * @param[in] n - number of words to read
  288. * @param[out] buf - the read data will be placed in this buffer
  289. * @return - error code or 0 on success
  290. */
  291. int pcilib_read(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t access, size_t n, void *buf);
  292. /**
  293. * Performs PIO write to the PCI BAR. The BAR will be automatically mapped and unmapped if necessary.
  294. * @param[in,out] ctx - pcilib context
  295. * @param[in] bar - the BAR to write, use PCILIB_BAR_DETECT to detect bar by the specified physical address
  296. * @param[in] addr - absolute physical address to write or the offset in the specified bar
  297. * @param[in] access - word size (access width in bytes)
  298. * @param[in] n - number of words to write
  299. * @param[out] buf - the pointer to the data to be written
  300. * @return - error code or 0 on success
  301. */
  302. int pcilib_write(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t access, size_t n, void *buf);
  303. /**
  304. * Performs PIO read from the PCI BAR. The specified address is treated as FIFO and will be read
  305. * \a n times. The BAR will be automatically mapped and unmapped if necessary.
  306. * @param[in,out] ctx - pcilib context
  307. * @param[in] bar - the BAR to read, use PCILIB_BAR_DETECT to detect bar by the specified physical address
  308. * @param[in] addr - absolute physical address to read or the offset in the specified bar
  309. * @param[in] access - the size of FIFO register in bytes (i.e. if `access = 4`, the 32-bit reads from FIFO will be performed)
  310. * @param[in] n - specifies how many times the data should be read from FIFO
  311. * @param[out] buf - the data will be placed in this buffer which should be at least `n * access` bytes long
  312. * @return - error code or 0 on success
  313. */
  314. int pcilib_read_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t access, size_t n, void *buf);
  315. /**
  316. * Performs PIO write to the PCI BAR. The specified address is treated as FIFO and will be written
  317. * \a n times. The BAR will be automatically mapped and unmapped if necessary.
  318. * @param[in,out] ctx - pcilib context
  319. * @param[in] bar - the BAR to write, use PCILIB_BAR_DETECT to detect bar by the specified physical address
  320. * @param[in] addr - absolute physical address to write or the offset in the specified bar
  321. * @param[in] access - the size of FIFO register in bytes (i.e. if `access = 4`, the 32-bit writes to FIFO will be performed)
  322. * @param[in] n - specifies how many times the data should be written to FIFO
  323. * @param[out] buf - buffer to write which should be at least `n * access` bytes long
  324. * @return - error code or 0 on success
  325. */
  326. int pcilib_write_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t access, size_t n, void *buf);
  327. /** public_api_pci
  328. * @}
  329. */
  330. /***********************************************************************************************************//**
  331. * \defgroup public_api_register Public Register API
  332. * API for register manipulations
  333. * @{
  334. */
  335. /**
  336. * Returns the list of registers provided by the hardware model.
  337. * @param[in,out] ctx - pcilib context
  338. * @param[in] bank - if set, only register within the specified bank will be returned
  339. * @param[in] flags - currently ignored
  340. * @return - the list of the register which should be cleaned with pcilib_free_register_info() or NULL in the case of error
  341. */
  342. pcilib_register_info_t *pcilib_get_register_list(pcilib_t *ctx, const char *bank, pcilib_list_flags_t flags);
  343. /**
  344. * Returns the information about the specified register
  345. * @param[in,out] ctx - pcilib context
  346. * @param[in] bank - indicates the bank where to look for register, autodetected if NULL is passed
  347. * @param[in] reg - the name of the register
  348. * @param[in] flags - currently ignored
  349. * @return - information about the specified register which should be cleaned with pcilib_free_register_info() or NULL in the case of error
  350. */
  351. pcilib_register_info_t *pcilib_get_register_info(pcilib_t *ctx, const char *bank, const char *reg, pcilib_list_flags_t flags);
  352. /**
  353. * Cleans up the memory occupied by register list returned from the pcilib_get_register_list() and pcilib_get_register_info() calls
  354. * @param[in,out] ctx - pcilib context
  355. * @param[in,out] info - buffer to clean
  356. */
  357. void pcilib_free_register_info(pcilib_t *ctx, pcilib_register_info_t *info);
  358. /**
  359. * Finds register id corresponding to the specified bank and register names. It is faster
  360. * to access registers by id instead of names. Therefore, in long running applications it
  361. * is preferred to resolve names of all required registers during initialization and access
  362. * them using ids only.
  363. * @param[in,out] ctx - pcilib context
  364. * @param[in] bank - should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
  365. * @param[in] reg - the name of the register
  366. * @return - register id or PCILIB_REGISTER_INVALID if register is not found
  367. */
  368. pcilib_register_t pcilib_find_register(pcilib_t *ctx, const char *bank, const char *reg);
  369. /**
  370. * Extracts additional information about the specified register. The additional information
  371. * is model-specific and are provided as extra XML attributes in XML-described registers.
  372. * The available attributes are only restricted by used XSD schema.
  373. * @param[in,out] ctx - pcilib context
  374. * @param[in] reg - register id
  375. * @param[in] attr - requested attribute name
  376. * @param[in,out] val - the value of attribute is returned here (see \ref public_api_value),
  377. * pcilib_clean_value() will be executed if \a val contains data. Therefore it should be always initialized to 0 before first use
  378. * @return - error code or 0 on success
  379. */
  380. int pcilib_get_register_attr_by_id(pcilib_t *ctx, pcilib_register_t reg, const char *attr, pcilib_value_t *val);
  381. /**
  382. * Extracts additional information about the specified register.
  383. * Equivalent to the pcilib_get_register_attr_by_id(), but first resolves register id using the specified bank and name.
  384. * @param[in,out] ctx - pcilib context
  385. * @param[in] bank - should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
  386. * @param[in] regname - register name
  387. * @param[in] attr - requested attribute name
  388. * @param[in,out] val - the value of attribute is returned here (see \ref public_api_value),
  389. * pcilib_clean_value() will be executed if \a val contains data. Therefore it should be always initialized to 0 before first use
  390. * @return - error code or 0 on success
  391. */
  392. int pcilib_get_register_attr(pcilib_t *ctx, const char *bank, const char *regname, const char *attr, pcilib_value_t *val);
  393. /**
  394. * Reads one or multiple sequential registers from the specified register bank. This function may provide access
  395. * to the undefined registers in the bank. In other cases, the pcilib_read_register() / pcilib_read_register_by_id()
  396. * calls are preferred.
  397. * @param[in,out] ctx - pcilib context
  398. * @param[in] bank - the bank to read (should be specified, no autodetection)
  399. * @param[in] addr - the register address within the bank, addresses are counted in bytes not registers (i.e. it is offset in bytes from the start of register bank)
  400. * @param[in] n - number of registers to read; i.e. `n * register_size` bytes will be read, where \a register_size is defined in the bank configuration (see #pcilib_register_bank_description_t)
  401. * @param[out] buf - the buffer of `n * register_size` bytes long where the data will be stored
  402. * @return - error code or 0 on success
  403. */
  404. int pcilib_read_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  405. /**
  406. * Writes one or multiple sequential registers from the specified register bank. This function may provide access
  407. * to the undefined registers in the bank. In other cases, the pcilib_write_register() / pcilib_write_register_by_id()
  408. * calls are preferred.
  409. * @param[in,out] ctx - pcilib context
  410. * @param[in] bank - the bank to write (should be specified, no autodetection)
  411. * @param[in] addr - the register address within the bank, addresses are counted in bytes not registers (i.e. it is offset in bytes from the start of register bank)
  412. * @param[in] n - number of registers to write; i.e. `n * register_size` bytes will be written, where \a register_size is defined in the bank configuration (see #pcilib_register_bank_description_t)
  413. * @param[in] buf - the buffer of `n * register_size` bytes long with the data
  414. * @return - error code or 0 on success
  415. */
  416. int pcilib_write_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, const pcilib_register_value_t *buf);
  417. /**
  418. * Reads the specified register.
  419. * @param[in,out] ctx - pcilib context
  420. * @param[in] reg - register id
  421. * @param[out] value - the register value is returned here
  422. * @return - error code or 0 on success
  423. */
  424. int pcilib_read_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t *value);
  425. /**
  426. * Writes to the specified register.
  427. * @param[in,out] ctx - pcilib context
  428. * @param[in] reg - register id
  429. * @param[in] value - the register value to write
  430. * @return - error code or 0 on success
  431. */
  432. int pcilib_write_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t value);
  433. /**
  434. * Reads the specified register.
  435. * Equivalent to the pcilib_read_register_by_id(), but first resolves register id using the specified bank and name.
  436. * @param[in,out] ctx - pcilib context
  437. * @param[in] bank - should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
  438. * @param[in] regname - the name of the register
  439. * @param[out] value - the register value is returned here
  440. * @return - error code or 0 on success
  441. */
  442. int pcilib_read_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t *value);
  443. /**
  444. * Writes to the specified register.
  445. * Equivalent to the pcilib_write_register_by_id(), but first resolves register id using the specified bank and name.
  446. * @param[in,out] ctx - pcilib context
  447. * @param[in] bank - should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
  448. * @param[in] regname - the name of the register
  449. * @param[in] value - the register value to write
  450. * @return - error code or 0 on success
  451. */
  452. int pcilib_write_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t value);
  453. /**
  454. * Reads a view of the specified register. The views allow to convert values to standard units
  455. * or get self-explanatory names associated with the values (enums).
  456. * @param[in,out] ctx - pcilib context
  457. * @param[in] reg - register id
  458. * @param[in] view - specifies the name of the view associated with register or desired units
  459. * @param[out] value - the register value is returned here (see \ref public_api_value),
  460. * pcilib_clean_value() will be executed if \a val contains data. Therefore it should be always initialized to 0 before first use.
  461. * @return - error code or 0 on success
  462. */
  463. int pcilib_read_register_view_by_id(pcilib_t *ctx, pcilib_register_t reg, const char *view, pcilib_value_t *value);
  464. /**
  465. * Writes the specified register using value represented in the specified view. The views allow to convert values from standard units
  466. * or self-explanatory names associated with the values (enums).
  467. * @param[in,out] ctx - pcilib context
  468. * @param[in] reg - register id
  469. * @param[in] view - specifies the name of the view associated with register or the used units
  470. * @param[in] value - the register value in the specified view (see \ref public_api_value)
  471. * @return - error code or 0 on success
  472. */
  473. int pcilib_write_register_view_by_id(pcilib_t *ctx, pcilib_register_t reg, const char *view, const pcilib_value_t *value);
  474. /**
  475. * Reads a view of the specified register. The views allow to convert values to standard units
  476. * or get self-explanatory names associated with the values (enums).
  477. * Equivalent to the pcilib_read_register_view_by_id(), but first resolves register id using the specified bank and name.
  478. * @param[in,out] ctx - pcilib context
  479. * @param[in] bank - should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
  480. * @param[in] regname - the name of the register
  481. * @param[in] view - specifies the name of the view associated with register or desired units
  482. * @param[out] value - the register value is returned here (see \ref public_api_value),
  483. * pcilib_clean_value() will be executed if \a val contains data. Therefore it should be always initialized to 0 before first use
  484. * @return - error code or 0 on success
  485. */
  486. int pcilib_read_register_view(pcilib_t *ctx, const char *bank, const char *regname, const char *view, pcilib_value_t *value);
  487. /**
  488. * Writes the specified register using value represented in the specified view. The views allow to convert values from standard units
  489. * or self-explanatory names associated with the values (enums).
  490. * Equivalent to the pcilib_write_register_view_by_id(), but first resolves register id using the specified bank and name.
  491. * @param[in,out] ctx - pcilib context
  492. * @param[in] bank - should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
  493. * @param[in] regname - the name of the register
  494. * @param[in] view - specifies the name of the view associated with register or the used units
  495. * @param[in] value - the register value in the specified view (see \ref public_api_value)
  496. * @return - error code or 0 on success
  497. */
  498. int pcilib_write_register_view(pcilib_t *ctx, const char *bank, const char *regname, const char *view, const pcilib_value_t *value);
  499. /** public_api_register
  500. * @}
  501. */
  502. /***********************************************************************************************************//**
  503. * \defgroup public_api_property Public Property API
  504. * Properties is another abstraction on top of registers allowing arbitrary data types and computed registers
  505. * @{
  506. */
  507. /**
  508. * Returns the list of properties available under the specified path
  509. * @param[in,out] ctx - pcilib context
  510. * @param[in] branch - path or NULL to return the top-level properties
  511. * @param[in] flags - not used at the moment
  512. * @return - the list of the properties which should be cleaned with pcilib_free_property_info() or NULL in the case of error
  513. */
  514. pcilib_property_info_t *pcilib_get_property_list(pcilib_t *ctx, const char *branch, pcilib_list_flags_t flags);
  515. /**
  516. * Cleans up the memory occupied by property list returned from the pcilib_get_property_list() call
  517. * @param[in,out] ctx - pcilib context
  518. * @param[in,out] info - buffer to clean
  519. */
  520. void pcilib_free_property_info(pcilib_t *ctx, pcilib_property_info_t *info);
  521. /**
  522. * Extracts additional information about the specified register.
  523. * Equivalent to the pcilib_get_register_attr_by_id(), but first resolves register id using the specified bank and name.
  524. * @param[in,out] ctx - pcilib context
  525. * @param[in] prop - property name (full name including path)
  526. * @param[in] attr - requested attribute name
  527. * @param[in,out] val - the value of attribute is returned here (see \ref public_api_value),
  528. * pcilib_clean_value() will be executed if \a val contains data. Therefore it should be always initialized to 0 before first use
  529. * @return - error code or 0 on success
  530. */
  531. int pcilib_get_property_attr(pcilib_t *ctx, const char *prop, const char *attr, pcilib_value_t *val);
  532. /**
  533. * Reads / computes the property value.
  534. * @param[in,out] ctx - pcilib context
  535. * @param[in] prop - property name (full name including path)
  536. * @param[out] val - the register value is returned here (see \ref public_api_value),
  537. * pcilib_clean_value() will be executed if \a val contains data. Therefore it should be always initialized to 0 before first use
  538. * @return - error code or 0 on success
  539. */
  540. int pcilib_get_property(pcilib_t *ctx, const char *prop, pcilib_value_t *val);
  541. /**
  542. * Writes the property value or executes the code associated with property
  543. * @param[in,out] ctx - pcilib context
  544. * @param[in] prop - property name (full name including path)
  545. * @param[out] val - the property value (see \ref public_api_value),
  546. * @return - error code or 0 on success
  547. */
  548. int pcilib_set_property(pcilib_t *ctx, const char *prop, const pcilib_value_t *val);
  549. /** public_api_property
  550. * @}
  551. */
  552. /***********************************************************************************************************//**
  553. * \defgroup public_api_dma Public DMA API
  554. * High speed interface for reading and writting unstructured data
  555. * @{
  556. */
  557. /**
  558. * This function resolves the ID of DMA engine from its address and direction.
  559. * It is a bit confusing, but addresses should be able to represent bi-directional
  560. * DMA engines. Unfortunatelly, implementation often is limited to uni-directional
  561. * engines. In this case, two DMA engines with different IDs can be virtually
  562. * combined in a DMA engine with the uniq address. This will allow user to specify
  563. * the same engine address in all types of accesses and we will resolve the appropriate
  564. * engine ID depending on the requested direction.
  565. * @param[in,out] ctx - pcilib context
  566. * @param[in] direction - DMA direction (to/from device)
  567. * @param[in] dma - address of DMA engine
  568. * @return - ID of DMA engine or PCILIB_DMA_INVALID if the specified engine does not exist
  569. */
  570. pcilib_dma_engine_t pcilib_find_dma_by_addr(pcilib_t *ctx, pcilib_dma_direction_t direction, pcilib_dma_engine_addr_t dma);
  571. /**
  572. * Starts DMA engine. This call will allocate DMA buffers and pass their bus addresses to the hardware.
  573. * - During the call, the C2S engine may start writting data. The written buffers are marked as
  574. * ready and can be read-out using pcilib_stream_dma() and pcilib_read_dma() calls. If no empty
  575. * buffers left, the C2S DMA engine will stall until some buffers are read out.
  576. * - The S2C engine waits until the data is pushed with pcilib_push_data() call
  577. *
  578. * After pcilib_start_dma() call, the pcilib_stop_dma() function should be necessarily called. However,
  579. * it will clean up the process memory, but only in some cases actually stop the DMA engine.
  580. * This depends on \a flags passed to both pcilib_start_dma() and pcilib_stop_dma() calls.
  581. * if PCILIB_DMA_FLAG_PERSISTENT flag is passed to the pcilib_start_dma(), the DMA engine
  582. * will remain running unless the same flag is also passed to the pcilib_stop_dma() call.
  583. * The allocated DMA buffers will stay intact and the hardware may continue reading / writting
  584. * data while there is space/data left. However, the other functions of DMA API should not
  585. * be called after pcilib_stop_dma() until new pcilib_start_dma() call is issued.
  586. *
  587. * The process- and thread-safety is implementation depedent. However, currently the process-
  588. * safety is ensured while accessing the kernel memory (todo: this may get complicated if we
  589. * provide a way to perform DMA directly to the user-supplied pages).
  590. * The thread-safety is not provided by currently implemented DMA engines. The DMA functions
  591. * may be called from multiple threads, but it is user responsibility to ensure that only a
  592. * single DMA-related call is running. On other hand, the DMA and register APIs may be used
  593. * in parallel.
  594. *
  595. * @param[in,out] ctx - pcilib context
  596. * @param[in] dma - ID of DMA engine, the ID should first be resolved using pcilib_find_dma_by_addr()
  597. * @param[in] flags - PCILIB_DMA_FLAG_PERSISTENT indicates that engine should be kept running after pcilib_stop_dma() call
  598. * @return - error code or 0 on success
  599. */
  600. int pcilib_start_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  601. /**
  602. * Stops DMA engine or just cleans up the process-specific memory buffers (see
  603. * pcilib_start_dma() for details). No DMA API calls allowed after this point
  604. * until pcilib_start_dma() is called anew.
  605. *
  606. * @param[in,out] ctx - pcilib context
  607. * @param[in] dma - ID of DMA engine, the ID should first be resolved using pcilib_find_dma_by_addr()
  608. * @param[in] flags - PCILIB_DMA_FLAG_PERSISTENT indicates that engine should be actually stopped independent of the flags passed to pcilib_start_dma() call
  609. * @return - error code or 0 on success
  610. */
  611. int pcilib_stop_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  612. /**
  613. * Tries to drop all pending data in the DMA channel. It will drop not only data currently in the DMA buffers,
  614. * but will allow hardware to write more and will drop the newly written data as well. The standard DMA timeout
  615. * is allowed to receive new data. If hardware continuously writes data, after #PCILIB_DMA_SKIP_TIMEOUT
  616. * microseconds the function will exit with #PCILIB_ERROR_TIMEOUT error.
  617. *
  618. * @param[in,out] ctx - pcilib context
  619. * @param[in] dma - ID of DMA engine, the ID should first be resolved using pcilib_find_dma_by_addr()
  620. * @return - error code or 0 on success
  621. */
  622. int pcilib_skip_dma(pcilib_t *ctx, pcilib_dma_engine_t dma);
  623. /**
  624. * Reads data from DMA buffers and pass it to the specified callback function. The return code of the callback
  625. * function determines if streaming should continue and how much to wait for next data to arrive before
  626. * triggering timeout.
  627. * The function is process- and thread-safe. The PCILIB_ERROR_BUSY will be returned immediately if DMA is used
  628. * by another thread or process.
  629. *
  630. * The function waits the specified \a timeout microseconds for the first data. Afterwards, the waiting time
  631. * for next portion of data depends on the last return code \a (ret) from callback function.
  632. * If `ret & PCILIB_STREAMING_TIMEOUT_MASK` is
  633. * - PCILIB_STREAMING_STOP - the streaming stops
  634. * - PCILIB_STREAMING_CONTINUE - the standard DMA timeout will be used to wait for a new data
  635. * - PCILIB_STREAMING_WAIT - the timeout specified in the function arguments will be used to wait for a new data
  636. * - PCILIB_STREAMING_CHECK - the function will return if new data is not available immediately
  637. * The function return code depends on the return code from the callback as well. If no data received within the specified
  638. * timeout and no callback is called, the PCILIB_ERROR_TIMEOUT is returned. Otherwise, success is returned unless
  639. * PCILIB_STREAMING_FAIL flag has been set in the callback return code before the timeout was triggered.
  640. *
  641. * @param[in,out] ctx - pcilib context
  642. * @param[in] dma - ID of DMA engine, the ID should first be resolved using pcilib_find_dma_by_addr()
  643. * @param[in] addr - instructs DMA to start reading at the specified address (not supported by existing DMA engines)
  644. * @param[in] size - instructs DMA to read only \a size bytes (not supported by existing DMA engines)
  645. * @param[in] flags - not used by existing DMA engines
  646. * @param[in] timeout - specifies number of microseconds to wait before reporting timeout, special values #PCILIB_TIMEOUT_IMMEDIATE and #PCILIB_TIMEOUT_INFINITE are supported.
  647. * @param[in] cb - callback function to call
  648. * @param[in,out] cbattr - passed through as the first parameter of callback function
  649. * @return - error code or 0 on success
  650. */
  651. int pcilib_stream_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, pcilib_dma_callback_t cb, void *cbattr);
  652. /**
  653. * Reads data from DMA until timeout is hit, a full DMA packet is read, or the specified number of bytes are read.
  654. * The function is process- and thread-safe. The #PCILIB_ERROR_BUSY will be returned immediately if DMA is used
  655. * by another thread or process.
  656. *
  657. * We can't read arbitrary number of bytes from DMA. A full DMA packet is always read. The DMA packet is not equal
  658. * to DMA buffer, but may consist of multiple buffers and the size may vary during run time. This may cause problems
  659. * if not treated properly. While the number of actually read bytes is bellow the specified size, the function may
  660. * continue to read the data. But as the new packet is started, it should fit completely in the provided buffer space
  661. * or PCILIB_ERROR_TOOBIG error will be returned. Therefore, it is a good practice to read only a single packet at
  662. * once and provide buffer capable to store the larges possible packet.
  663. *
  664. * Unless #PCILIB_DMA_FLAG_MULTIPACKET flag is specified, the function will stop after the first full packet is read.
  665. * Otherwise, the reading will continue until all `size` bytes are read or timeout is hit. The stanard DMA timeout
  666. * should be met while reading DMA buffers belonging to the same packet. Otherwise, PCILIB_ERROR_TIMEOUT is returned
  667. * and number of bytes read so far is returned in the `rdsize`.
  668. * If #PCILIB_DMA_FLAG_WAIT flag is specified, the number of microseconds specified in the `timeout` parameter are
  669. * allowed for a new packet to come. If no new data arrived in the specified timeout, the function returns successfuly
  670. * and number of read bytes is returned in the `rdsize`.
  671. *
  672. * We can't put the read data back into the DMA. Therefore, even in the case of error some data may be returned. The
  673. * number of actually read bytes is always reported in `rdsize` and the specified amount of data is always written
  674. * to the provided buffer.
  675. *
  676. * @param[in,out] ctx - pcilib context
  677. * @param[in] dma - ID of DMA engine, the ID should first be resolved using pcilib_find_dma_by_addr()
  678. * @param[in] addr - instructs DMA to start reading at the specified address (not supported by existing DMA engines)
  679. * @param[in] size - specifies how many bytes should be read
  680. * @param[in] flags - Various flags controlling the function behavior
  681. * - #PCILIB_DMA_FLAG_MULTIPACKET indicates that multiple DMA packets will be read (not recommended, use pcilib_stream_dma() in this case)
  682. * - #PCILIB_DMA_FLAG_WAIT indicates that we need to wait the specified timeout between consequitive DMA packets (default DMA timeout is used otherwise)
  683. * @param[in] timeout - specifies number of microseconds to wait before reporting timeout, special values #PCILIB_TIMEOUT_IMMEDIATE and #PCILIB_TIMEOUT_INFINITE are supported.
  684. * This parameter specifies timeout between consequtive DMA packets, the standard DMA timeout is expected between buffers belonging to the same DMA packet.
  685. * @param[out] buf - the buffer of \a size bytes long to store the data
  686. * @param[out] rdsize - number of bytes which were actually read. The correct value will be reported in both case if function has finished successfully or if error has happened.
  687. * @return - error code or 0 on success. In both cases some data may be returned in the buffer, check `rdsize`.
  688. */
  689. int pcilib_read_dma_custom(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, void *buf, size_t *rdsize);
  690. /**
  691. * Reads data from DMA until timeout is hit, a full DMA packet is read, or the specified number of bytes are read.
  692. * Please, check detailed explanation when reading is stopped in the description of pcilib_read_dma_custom().
  693. * The function is process- and thread-safe. The #PCILIB_ERROR_BUSY will be returned immediately if DMA is used
  694. * by another thread or process.
  695. *
  696. * The function actually executes the pcilib_read_dma_custom() without special flags and with default DMA timeout
  697. *
  698. * @param[in,out] ctx - pcilib context
  699. * @param[in] dma - ID of DMA engine, the ID should first be resolved using pcilib_find_dma_by_addr()
  700. * @param[in] addr - instructs DMA to start reading at the specified address (not supported by existing DMA engines)
  701. * @param[in] size - specifies how many bytes should be read
  702. * @param[out] buf - the buffer of \a size bytes long to store the data
  703. * @param[out] rdsize - number of bytes which were actually read. The correct value will be reported in both case if function has finished successfully or if error has happened.
  704. * @return - error code or 0 on success. In both cases some data may be returned in the buffer, check `rdsize`.
  705. */
  706. int pcilib_read_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *rdsize);
  707. /**
  708. * Pushes new data to the DMA engine. The actual behavior is implementation dependent. The successful exit does not mean
  709. * what all data have reached hardware, but only guarantees that it is stored in DMA buffers and the hardware is instructed
  710. * to start reading buffers. The function may return #PCILIB_ERROR_TIMEOUT is all DMA buffers are occupied and no buffers is
  711. * read by the hardware within the specified timeout. Even if an error is returned, part of the data may be already send to
  712. * DMA and can't be revoked back. Number of actually written bytes is always returned in the `wrsize`.
  713. *
  714. * The function is process- and thread-safe. The #PCILIB_ERROR_BUSY will be returned immediately if DMA is used
  715. * by another thread or process.
  716. *
  717. * @param[in,out] ctx - pcilib context
  718. * @param[in] dma - ID of DMA engine, the ID should first be resolved using pcilib_find_dma_by_addr()
  719. * @param[in] addr - instructs DMA to start writting at the specified address (not supported by existing DMA engines)
  720. * @param[in] size - specifies how many bytes should be written
  721. * @param[in] flags - Various flags controlling the function behavior
  722. * - #PCILIB_DMA_FLAG_EOP indicates that this is the last data in the DMA packet
  723. * - #PCILIB_DMA_FLAG_WAIT requires function to block until the data actually reach hardware. #PCILIB_ERROR_TIMEOUT may be returned if it takes longer when the specified timeout.
  724. * @param[in] timeout - specifies number of microseconds to wait before reporting timeout, special values #PCILIB_TIMEOUT_IMMEDIATE and #PCILIB_TIMEOUT_INFINITE are supported.
  725. * @param[out] buf - the buffer with the data
  726. * @param[out] wrsize - number of bytes which were actually written. The correct value will be reported in both case if function has finished successfully or if error has happened.
  727. * @return - error code or 0 on success. In both cases some data may be written to the DMA, check `wrsize`.
  728. */
  729. int pcilib_push_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, void *buf, size_t *wrsize);
  730. /**
  731. * Pushes new data to the DMA engine and blocks until hardware gets it all. Even if an error has occured, a
  732. * part of the data may be already had send to DMA and can't be revoked back. Number of actually written bytes
  733. * is always returned in the `wrsize`.
  734. *
  735. * The function is process- and thread-safe. The #PCILIB_ERROR_BUSY will be returned immediately if DMA is used
  736. * by another thread or process.
  737. * The function actually executes the pcilib_push_dma() with #PCILIB_DMA_FLAG_EOP and #PCILIB_DMA_FLAG_WAIT flags set and
  738. * the default DMA timeout.
  739. *
  740. * @param[in,out] ctx - pcilib context
  741. * @param[in] dma - ID of DMA engine, the ID should first be resolved using pcilib_find_dma_by_addr()
  742. * @param[in] addr - instructs DMA to start writting at the specified address (not supported by existing DMA engines)
  743. * @param[in] size - specifies how many bytes should be written
  744. * @param[out] buf - the buffer with the data
  745. * @param[out] wrsize - number of bytes which were actually written. The correct value will be reported in both case if function has finished successfully or if error has happened.
  746. * @return - error code or 0 on success. In both cases some data may be written to the DMA, check `wrsize`.
  747. */
  748. int pcilib_write_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *wrsize);
  749. /**
  750. * Benchmarks the DMA implementation. The reported performance may be significantly affected by several environmental variables.
  751. * - PCILIB_BENCHMARK_HARDWARE - if set will not copy the data out, but immediately drop as it lended in DMA buffers. This allows to remove influence of memcpy performance.
  752. * - PCILIB_BENCHMARK_STREAMING - if set will not re-initialized the DMA on each iteration. If DMA is properly implemented this should have only marginal influence on performance
  753. *
  754. * The function is process- and thread-safe. The #PCILIB_ERROR_BUSY will be returned immediately if DMA is used
  755. * by another thread or process.
  756. *
  757. * @param[in,out] ctx - pcilib context
  758. * @param[in] dma - Address of DMA engine
  759. * @param[in] addr - Benchmark will read and write data at the specified address (ignored by existing DMA engines)
  760. * @param[in] size - specifies how many bytes should be read and written at each iteration
  761. * @param[in] iterations - number of iterations to execute
  762. * @param[in] direction - Specifies if DMA reading or writting should be benchmarked, bi-directional benchmark is possible as well
  763. * @return - bandwidth in MiB/s (Mebibytes per second)
  764. */
  765. double pcilib_benchmark_dma(pcilib_t *ctx, pcilib_dma_engine_addr_t dma, uintptr_t addr, size_t size, size_t iterations, pcilib_dma_direction_t direction);
  766. /** public_api_dma
  767. * @}
  768. */
  769. /***********************************************************************************************************//**
  770. * \defgroup public_api_irq Public IRQ API
  771. * This is actually part of DMA API. IRQ handling is currently provided by DMA engine.
  772. * However, the IRQs are barely used so far. Therefore, the API can be significantly changed when confronted with harsh reallity.
  773. * @{
  774. */
  775. int pcilib_enable_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_dma_flags_t flags);
  776. int pcilib_disable_irq(pcilib_t *ctx, pcilib_dma_flags_t flags);
  777. int pcilib_wait_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source, pcilib_timeout_t timeout, size_t *count);
  778. int pcilib_acknowledge_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_irq_source_t irq_source);
  779. int pcilib_clear_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source);
  780. /** public_api_irq
  781. * @}
  782. */
  783. /***********************************************************************************************************//**
  784. * \defgroup public_api_event Public Event API
  785. * High level API for reading the structured data from hardware
  786. * @{
  787. */
  788. /**
  789. * Provides access to the context of event engine. This may be required to call implementation-specific
  790. * functions of event engine.
  791. * @param [in,out] ctx - pcilib context
  792. * @return - context of event engine or NULL in the case of error. Depending on the implementation can be extension of pcilib_contex_t, it should be safe to type cast if you are running the correct engine.
  793. */
  794. pcilib_context_t *pcilib_get_event_engine(pcilib_t *ctx);
  795. /**
  796. * Resolves the event ID based on its name.
  797. * @param [in,out] ctx - pcilib context
  798. * @param [in] event - the event name
  799. * @return - the event ID or PCILIB_EVENT_INVALID if event is not found
  800. */
  801. pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event);
  802. /**
  803. * Analyzes current configuration, allocates necessary buffers and spawns required data processing threads.
  804. * Depending on the implementation and the current configuration, the actual event grabbing may start already
  805. * here. In this case, the preprocessed events will be storred in the temporary buffers and may be lost if
  806. * pcilib_stop() is called before reading them out. Alternatively, the actual grabbing may only commend when
  807. * the pcilib_stream() or pcilib_get_next_event() functions are executed.
  808. * The function is non-blocking and will return immediately after allocating required memory and spawning
  809. * of the preprocessing threads. It is important to call pcilib_stop() in the end.
  810. *
  811. * The grabbing will stop automatically if conditions defined using pcilib_configure_autostop() function
  812. * are met. It also possible to stop grabbing using pcilib_stop() call at any moment.
  813. *
  814. * The process- and thread-safety is implementation depedent. However, normally the event engine will depend
  815. * on DMA and if DMA engine is process-safe it will ensure the process-safety for event engine as well.
  816. * The thread-safety is not directly ensured by currently implemented Event engines. The functions of Event
  817. * engine may be called from multiple threads, but it is the user responsibility to ensure that only a single
  818. * function of Event engine is running at each moment. On other hand, the Event and register APIs may be used
  819. * in parallel.
  820. *
  821. * @param[in,out] ctx - pcilib context
  822. * @param[in] ev_mask - specifies events to listen for, use #PCILIB_EVENTS_ALL to grab all events
  823. * @param[in] flags - various implementation-specific flags controlling operation of event engine
  824. * - #PCILIB_EVENT_FLAG_PREPROCESS - requires event preprocessing (i.e. event data is decoded before it is requested, this is often required for multi-threaded processing)
  825. * - #PCILIB_EVENT_FLAG_RAW_DATA_ONLY - disables data processing at all, only raw data will be provided in this case
  826. * @return - error code or 0 on success
  827. */
  828. int pcilib_start(pcilib_t *ctx, pcilib_event_t ev_mask, pcilib_event_flags_t flags);
  829. /**
  830. * Stops event grabbing and optionally cleans up the used memory.
  831. * This function operates in two modes. If #PCILIB_EVENT_FLAG_STOP_ONLY flag is specified, the
  832. * event grabbing is stopped, but all memory structures are kept intact. This also forces the
  833. * blocked pcilib_stream() and pcilib_get_next_event() to return after a short while.
  834. * Unlike DMA engine, the event engine is not persistent and is always completely stopped when
  835. * application is finished. Therefore, later the pcilib_stop() should be necessarily called
  836. * again without #PCILIB_EVENT_FLAG_STOP_ONLY flag to commend full clean up and release all
  837. * used memory. Such call may only be issued when no threads are using Event engine any more.
  838. * There should be no functions waiting for next event to appear and all of the obtained event
  839. * data should be already returned back to the system.
  840. *
  841. * pcilib_stop executed with #PCILIB_EVENT_FLAG_STOP_ONLY is thread safe. The full version of
  842. * the function is not and should be never called in parallel with any other action of the
  843. * event engine. Actually, it is thread safe in the case of ipecamera, but this is not
  844. * guaranteed for other event engines.
  845. *
  846. * @param[in,out] ctx - pcilib context
  847. * @param[in] flags - flags specifying operation mode
  848. * - #PCILIB_EVENT_FLAG_STOP_ONLY - instructs library to keep all the data structures intact and to onlystop the data grabbing
  849. * @return - error code or 0 on success
  850. */
  851. int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags);
  852. /**
  853. * Streams the incomming events to the provided callback function. If Event engine is not started
  854. * yet, it will be started and terminated upon the completion of the call. The streaming will
  855. * continue while Event engine is started and the callback function does not return an error (negative)
  856. * or #PCILIB_STREAMING_STOP.
  857. *
  858. * The callback is called only when all the data associated with the event is received from hardware.
  859. * So, the raw data is necessarily present, but availability of alternative data formats is
  860. * optional. Depending on the implementation and current configuration, the data decoding can be
  861. * performed beforehand, in parallel with callback execution, or only them the data is
  862. * requested with pcilib_get_data() or pcilib_copy_data() calls.
  863. *
  864. * The function is thread-safe. The multiple requests to pcilib_stream() and pcilib_get_next_event()
  865. * will be automatically serialized by the event engine. The pcilib_stream() is running in the
  866. * single thread and no new calls to callback are issued until currently executed callback
  867. * returns. The client application may get hold on the data from multiple events simultaneously.
  868. * However, the data could be overwritten meanwhile by the hardware. The pcilib_return_data()
  869. * will inform if it has happened by returning #PCILIB_ERROR_OVERWRITTEN.
  870. *
  871. * @param[in,out] ctx - pcilib context
  872. * @param[in] callback - callback function to call for each event, the streaming is stopped if it return #PCILIB_STREAMING_STOP or negative value indicating the error
  873. * @param[in,out] user - used data is passed through as the last parameter of callback function
  874. * @return - error code or 0 on success
  875. */
  876. int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user);
  877. /**
  878. * Waits until the next event is available.
  879. * The event is only returned when all the data associated with the event is received from hardware.
  880. * So, the raw data is necessarily present, but availability of alternative data formats is
  881. * optional. Depending on the implementation and current configuration, the data decoding can be
  882. * performed beforehand, in parallel, or only them the data is requested with pcilib_get_data()
  883. * or pcilib_copy_data() calls.
  884. *
  885. * The function is thread-safe. The multiple requests to pcilib_stream() and pcilib_get_next_event()
  886. * will be automatically serialized by the event engine. The client application may get hold on
  887. * the data from multiple events simultaneously. However, the data could be overwritten meanwhile
  888. * by the hardware. The pcilib_return_data() will inform if it has happened by returning
  889. * #PCILIB_ERROR_OVERWRITTEN.
  890. *
  891. * @param[in,out] ctx - pcilib context
  892. * @param[in] timeout - specifies number of microseconds to wait for next event before reporting timeout, special values #PCILIB_TIMEOUT_IMMEDIATE and #PCILIB_TIMEOUT_INFINITE are supported.
  893. * @param[out] evid - the event ID is returned in this parameter
  894. * @param[in] info_size - the size of the passed event info structure (the implementation of event engine may extend the standad #pcilib_event_info_t definition and provide extra information about the event.
  895. If big enough info buffer is provided, this additional information will be copied as well. Otherwise only standard information is provided.
  896. * @param[out] info - The information about the recorded event is written to `info`
  897. * @return - error code or 0 on success
  898. */
  899. int pcilib_get_next_event(pcilib_t *ctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info);
  900. /**
  901. * Requests the streaming the rawdata from the event engine. The callback will be called
  902. * each time new DMA packet is received. It is the fastest way to acuqire data.
  903. * No memory copies performed and DMA buffers are directly passed to the specified callback.
  904. * However, to prevent data loss, no long processing is allowed is only expected to copy data
  905. * into the appropriate place and return control to the event engine.
  906. *
  907. * This function should be exectued before the grabbing is started with pcilib_start().
  908. * The performance can be boosted further by disabling any data processing within the event
  909. * engine. This is achieved by passing the #PCILIB_EVENT_FLAG_RAW_DATA_ONLY flag to pcilib_start()
  910. * function while starting the grabbing.
  911. *
  912. * @param[in,out] ctx - pcilib context
  913. * @param[in] callback - callback function to call for each event, the streaming is stopped if it return #PCILIB_STREAMING_STOP or negative value indicating the error
  914. * @param[in,out] user - used data is passed through as the last parameter of callback function
  915. * @return - error code or 0 on success
  916. */
  917. int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user);
  918. /**
  919. * Configures conditions when the grabbing will be stopped automatically. The recording of new events may be
  920. * stopped after reaching max_events records or when the specified amount of time is elapsed whatever happens
  921. * first. However, the pcilib_stop() function still must be called afterwards.
  922. *
  923. * This function should be exectued before the grabbing is started with pcilib_start().
  924. * NOTE that this options might not be respected if grabbing is started with the
  925. * #PCILIB_EVENT_FLAG_RAW_DATA_ONLY flag specified.
  926. *
  927. * @param[in,out] ctx - pcilib context
  928. * @param[in] max_events - specifies number of events after which the grabbing is stopped
  929. * @param[in] duration - specifies number of microseconds to run the grabbing, special values #PCILIB_TIMEOUT_IMMEDIATE and #PCILIB_TIMEOUT_INFINITE are supported.
  930. * @return - error code or 0 on success
  931. */
  932. int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration);
  933. /**
  934. * Request the auto-triggering while grabbing. The software triggering is currently not supported (but planned).
  935. * Therefore it is fully relied on hardware support. If no hardware support is available, #PCILIB_ERROR_NOTSUPPORTED
  936. * will be returned.
  937. *
  938. * This function should be exectued before the grabbing is started with pcilib_start().
  939. *
  940. * @param[in,out] ctx - pcilib context
  941. * @param[in] interval - instructs hardware that each `interval` microseconds a new trigger should be issued
  942. * @param[in] event - specifies ID of the event which will be triggered
  943. * @param[in] trigger_size - specifies the size of `trigger` buffer
  944. * @param[in] trigger_data - this implementation-specific buffer which will be passed through to the Event engine
  945. * @return - error code or 0 on success
  946. */
  947. int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  948. /**
  949. * Issues a single software trigger for the specified event. No hardware support is required. The function
  950. * is fully thread safe and can be called while other thread is blocked in the pcilib_stream() or pcilib_get_next_event()
  951. * calls.
  952. *
  953. * @param[in,out] ctx - pcilib context
  954. * @param[in] event - specifies ID of the event to trigger
  955. * @param[in] trigger_size - specifies the size of `trigger` buffer
  956. * @param[in] trigger_data - this implementation-specific buffer which will be passed through to the Event engine
  957. * @return - error code or 0 on success
  958. */
  959. int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  960. /** public_api_event
  961. * @}
  962. */
  963. /***********************************************************************************************************//**
  964. * \defgroup public_api_event_data Public Event Data API
  965. * A part of Event API actually providing access to the data
  966. * @{
  967. */
  968. /**
  969. * Resolves the data type based on its name.
  970. * @param[in,out] ctx - pcilib context
  971. * @param[in] event - the ID of the event producing the data
  972. * @param[in] data_type - the name of data type
  973. * @return - the data type or PCILIB_EVENT_DATA_TYPE_INVALID if the specified type is not found
  974. */
  975. pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type);
  976. /**
  977. * This is a simples way to grab the data from the event engine. The event engine is started, a software trigger
  978. * for the specified event is issued if `timeout` is equal to PCILIB_TIMEOUT_IMMEDIATE, event is grabbed and the
  979. * default data is copied into the user buffer. Then, the grabbing is stopped.
  980. * @param[in,out] ctx - pcilib context
  981. * @param[in] event - the event to trigger and/or event mask to grab
  982. * @param[in,out] size - specifies the size of the provided buffer (if user supplies the buffer), the amount of data actually written is returned in this paramter
  983. * @param[in,out] data - Either contains a pointer to user-supplied buffer where the data will be written or pointer to NULL otherwise.
  984. In the later case, the pointer to newly allocated buffer will be returned in case of success. It is responsibility of the user to free the memory in this case.
  985. In case of failure, the content of data is undefined.
  986. * @param[in] timeout - either is equal to #PCILIB_TIMEOUT_IMMEDIATE for immediate software trigger or specifies number of microseconds to wait for event triggered by hardware
  987. * @return - error code or 0 on success
  988. */
  989. int pcilib_grab(pcilib_t *ctx, pcilib_event_t event, size_t *size, void **data, pcilib_timeout_t timeout);
  990. /**
  991. * Copies the data of the specified type associated with the specified event into the provided buffer. May return #PCILIB_ERROR_OVERWRITTEN
  992. * if the data was overwritten during the call.
  993. *
  994. * @param[in,out] ctx - pcilib context
  995. * @param[in] event_id - specifies the ID of event to get data from
  996. * @param[in] data_type - specifies the desired type of data
  997. - PCILIB_EVENT_DATA will request the default data type
  998. - PCILIB_EVENT_RAW_DATA will request the raw data
  999. - The other types of data may be defined by event engine
  1000. * @param[in] size - specifies the size of provided buffer in bytes
  1001. * @param[out] buf - the data will be copied in this buffer if it is big enough, otherwise #PCILIB_ERROR_TOOBIG will be returned
  1002. * @param[out] retsize - the number of actually written bytes will be returned in this parameter
  1003. * @return - error code or 0 on success
  1004. */
  1005. int pcilib_copy_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t size, void *buf, size_t *retsize);
  1006. /**
  1007. * Copies the data of the specified type associated with the specified event into the provided buffer. May return #PCILIB_ERROR_OVERWRITTEN
  1008. * if the data was overwritten during the call. This is very similar to pcilib_copy_data(), but allows to specify implementation specific
  1009. * argument explaining the requested data format.
  1010. *
  1011. * @param[in,out] ctx - pcilib context
  1012. * @param[in] event_id - specifies the ID of event to get data from
  1013. * @param[in] data_type - specifies the desired type of data
  1014. - PCILIB_EVENT_DATA will request the default data type
  1015. - PCILIB_EVENT_RAW_DATA will request the raw data
  1016. - The other types of data may be defined by event engine
  1017. * @param[in] arg_size - specifies the size of `arg` in bytes
  1018. * @param[in] arg - implementation-specific argument expalining the requested data format
  1019. * @param[in] size - specifies the size of provided buffer in bytes
  1020. * @param[out] buf - the data will be copied in this buffer if it is big enough, otherwise #PCILIB_ERROR_TOOBIG will be returned
  1021. * @param[out] retsize - the number of actually written bytes will be returned in this parameter
  1022. * @return - error code or 0 on success
  1023. */
  1024. int pcilib_copy_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t size, void *buf, size_t *retsize);
  1025. /**
  1026. * Returns pointer to the data of the specified type associated with the specified event. The data should be returned back to the
  1027. * Event engine using pcilib_return_data() call. WARNING: Current implementation may overwrite the data before the pcilib_return_data()
  1028. * call is executed. In this case the #PCILIB_ERROR_OVERWRITTEN error will be returned by pcilib_return_data() call. I guess this is
  1029. * a bad idea and have to be changed. Meanwhile, for the ipecamera implementation the image data will be never overwritten. However,
  1030. * the raw data may get overwritten and the error code of pcilib_return_data() has to be consulted.
  1031. *
  1032. * @param[in,out] ctx - pcilib context
  1033. * @param[in] event_id - specifies the ID of event to get data from
  1034. * @param[in] data_type - specifies the desired type of data
  1035. - PCILIB_EVENT_DATA will request the default data type
  1036. - PCILIB_EVENT_RAW_DATA will request the raw data
  1037. - The other types of data may be defined by event engine
  1038. * @param[out] size_or_err - contain error code if function returns NULL or number of actually written bytes otherwise
  1039. * @return - the pointer to the requested data or NULL otherwise
  1040. */
  1041. void *pcilib_get_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size_or_err);
  1042. /**
  1043. * Returns pointer to the data of the specified type associated with the specified event. The data should be returned back to the
  1044. * Event engine using pcilib_return_data() call. WARNING: Current implementation may overwrite the data before the pcilib_return_data()
  1045. * call is executed, @see pcilib_get_data() for details. Overall this function is very similar to pcilib_get_data(), but allows to
  1046. * specify implementation specific argument explaining the requested data format.
  1047. *
  1048. * @param[in,out] ctx - pcilib context
  1049. * @param[in] event_id - specifies the ID of event to get data from
  1050. * @param[in] data_type - specifies the desired type of data
  1051. - PCILIB_EVENT_DATA will request the default data type
  1052. - PCILIB_EVENT_RAW_DATA will request the raw data
  1053. - The other types of data may be defined by event engine
  1054. * @param[in] arg_size - specifies the size of `arg` in bytes
  1055. * @param[in] arg - implementation-specific argument expalining the requested data format
  1056. * @param[out] size_or_err - contain error code if function returns NULL or number of actually written bytes otherwise
  1057. * @return - the pointer to the requested data or NULL otherwise
  1058. */
  1059. void *pcilib_get_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t *size_or_err);
  1060. /*
  1061. * This function returns data obtained using pcilib_get_data() or pcilib_get_data_with_argument() calls.
  1062. * It occasionally may return #PCILIB_ERROR_OVERWRITTEN error indicating that the data was overwritten
  1063. * between pcilib_get_data() and pcilib_return_data() calls, @see pcilib_get_data() for details.
  1064. * @param[in,out] ctx - pcilib context
  1065. * @param[in] event_id - specifies the ID of event to get data from
  1066. * @param[in] data_type - specifies the data type request in the pcilib_get_data() call
  1067. * @param[in,out] data - specifies the buffer returned by pcilib_get_data() call
  1068. * @return - #PCILIB_ERROR_OVERWRITTEN or 0 if data is still valid
  1069. */
  1070. int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data);
  1071. /** public_api_event_data
  1072. * @}
  1073. */
  1074. /***********************************************************************************************************//**
  1075. * \defgroup public_api_value Polymorphic values
  1076. * API for manipulation of data formats and units
  1077. * @{
  1078. */
  1079. /**
  1080. * Destroys the polymorphic value and frees any extra used memory, but does not free #pcilib_value_t itself
  1081. * @param[in] ctx - pcilib context
  1082. * @param[in,out] val - initialized polymorphic value
  1083. */
  1084. void pcilib_clean_value(pcilib_t *ctx, pcilib_value_t *val);
  1085. /**
  1086. * Copies the polymorphic value. If `dst` already contains the value, cleans it first.
  1087. * Therefore, before first usage the value should be always initialized to 0.
  1088. * @param[in] ctx - pcilib context
  1089. * @param[in,out] dst - initialized polymorphic value
  1090. * @param[in] src - initialized polymorphic value
  1091. * @return - 0 on success or memory error
  1092. */
  1093. int pcilib_copy_value(pcilib_t *ctx, pcilib_value_t *dst, const pcilib_value_t *src);
  1094. /**
  1095. * Initializes the polymorphic value from floating-point number. If `val` already contains the value, cleans it first.
  1096. * Therefore, before first usage the value should be always initialized to 0.
  1097. * @param[in] ctx - pcilib context
  1098. * @param[in,out] val - initialized polymorphic value
  1099. * @param[in] fval - initializer
  1100. * @return - 0 on success or memory error
  1101. */
  1102. int pcilib_set_value_from_float(pcilib_t *ctx, pcilib_value_t *val, double fval);
  1103. /**
  1104. * Initializes the polymorphic value from integer. If `val` already contains the value, cleans it first.
  1105. * Therefore, before first usage the value should be always initialized to 0.
  1106. * @param[in] ctx - pcilib context
  1107. * @param[in,out] val - initialized polymorphic value
  1108. * @param[in] ival - initializer
  1109. * @return - 0 on success or memory error
  1110. */
  1111. int pcilib_set_value_from_int(pcilib_t *ctx, pcilib_value_t *val, long ival);
  1112. /**
  1113. * Initializes the polymorphic value from the register value. If `val` already contains the value, cleans it first.
  1114. * Therefore, before first usage the value should be always initialized to 0.
  1115. * @param[in] ctx - pcilib context
  1116. * @param[in,out] val - initialized polymorphic value
  1117. * @param[in] regval - initializer
  1118. * @return - 0 on success or memory error
  1119. */
  1120. int pcilib_set_value_from_register_value(pcilib_t *ctx, pcilib_value_t *val, pcilib_register_value_t regval);
  1121. /**
  1122. * Initializes the polymorphic value from the static string. The string is not copied, but only referenced.
  1123. * If `val` already contains the value, cleans it first. Therefore, before first usage the value should be always initialized to 0.
  1124. * @param[in] ctx - pcilib context
  1125. * @param[in,out] val - initialized polymorphic value
  1126. * @param[in] str - initializer
  1127. * @return - 0 on success or memory error
  1128. */
  1129. int pcilib_set_value_from_static_string(pcilib_t *ctx, pcilib_value_t *val, const char *str);
  1130. /**
  1131. * Initializes the polymorphic value from the string. The string is copied.
  1132. * If `val` already contains the value, cleans it first. Therefore, before first usage the value should be always initialized to 0.
  1133. * @param[in] ctx - pcilib context
  1134. * @param[in,out] val - initialized polymorphic value
  1135. * @param[in] str - initializer
  1136. * @return - 0 on success or memory error
  1137. */
  1138. int pcilib_set_value_from_string(pcilib_t *ctx, pcilib_value_t *val, const char *str);
  1139. /**
  1140. * Get the floating point value from the polymorphic type. May inmply impliced type conversion,
  1141. * for isntance parsing the number from the string. Will return 0. and report an error if
  1142. * conversion failed.
  1143. * @param[in] ctx - pcilib context
  1144. * @param[in] val - initialized polymorphic value of arbitrary type
  1145. * @param[out] err - error code or 0 on sccuess
  1146. * @return - the value or 0 in the case of error
  1147. */
  1148. double pcilib_get_value_as_float(pcilib_t *ctx, const pcilib_value_t *val, int *err);
  1149. /**
  1150. * Get the integer value from the polymorphic type. May inmply impliced type conversion
  1151. * resulting in precision loss if the `val` stores floating-point number. The warning
  1152. * message will be printed in this case, but no error returned.
  1153. * @param[in] ctx - pcilib context
  1154. * @param[in] val - initialized polymorphic value of arbitrary type
  1155. * @param[out] err - error code or 0 on sccuess
  1156. * @return - the value or 0 in the case of error
  1157. */
  1158. long pcilib_get_value_as_int(pcilib_t *ctx, const pcilib_value_t *val, int *err);
  1159. /**
  1160. * Get the integer value from the polymorphic type. May inmply impliced type conversion
  1161. * resulting in precision loss if the `val` stores floating-point number or complete
  1162. * data corruption if negative number is stored. The warning message will be printed
  1163. * in this case, but no error returned.
  1164. * @param[in] ctx - pcilib context
  1165. * @param[in] val - initialized polymorphic value of arbitrary type
  1166. * @param[out] err - error code or 0 on sccuess
  1167. * @return - the value or 0 in the case of error
  1168. */
  1169. pcilib_register_value_t pcilib_get_value_as_register_value(pcilib_t *ctx, const pcilib_value_t *val, int *err);
  1170. /**
  1171. * Convert the units of the supplied polymorphic value. The error will be reported if currently used units of the
  1172. * value are unknown, the requested conversion is not supported, or the value is not numeric.
  1173. * @param[in] ctx - pcilib context
  1174. * @param[in,out] val - initialized polymorphic value of any numeric type
  1175. * @param[in] unit_name - the requested units
  1176. * @return err - error code or 0 on sccuess
  1177. */
  1178. int pcilib_convert_value_unit(pcilib_t *ctx, pcilib_value_t *val, const char *unit_name);
  1179. /**
  1180. * Convert the type of the supplied polymorphic value. The error will be reported if conversion
  1181. * is not supported or failed due to non-conformant content.
  1182. * @param[in] ctx - pcilib context
  1183. * @param[in,out] val - initialized polymorphic value of any type
  1184. * @param[in] type - the requested type
  1185. * @return err - error code or 0 on sccuess
  1186. */
  1187. int pcilib_convert_value_type(pcilib_t *ctx, pcilib_value_t *val, pcilib_value_type_t type);
  1188. /** public_api_value
  1189. * @}
  1190. */
  1191. #ifdef __cplusplus
  1192. }
  1193. #endif
  1194. #endif /* _PCILIB_H */