pcilib.h 74 KB

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