pcilib.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #ifndef _PCITOOL_PCILIB_H
  2. #define _PCITOOL_PCILIB_H
  3. #define PCILIB_MAX_BANKS 6
  4. #define PCILIB_MAX_DMA_ENGINES 32
  5. #include <sys/time.h>
  6. #include <stdint.h>
  7. #define pcilib_memcpy pcilib_memcpy32
  8. #define pcilib_datacpy pcilib_datacpy32
  9. typedef struct pcilib_s pcilib_t;
  10. typedef struct pcilib_event_context_s pcilib_context_t;
  11. typedef struct pcilib_dma_context_s pcilib_dma_context_t;
  12. typedef struct pcilib_dma_api_description_s pcilib_dma_api_description_t;
  13. typedef struct pcilib_event_api_description_s pcilib_event_api_description_t;
  14. typedef struct pcilib_protocol_description_s pcilib_protocol_description_t;
  15. typedef unsigned int pcilib_irq_hw_source_t;
  16. typedef uint32_t pcilib_irq_source_t;
  17. typedef uint8_t pcilib_bar_t; /**< Type holding the PCI Bar number */
  18. typedef uint8_t pcilib_register_t; /**< Type holding the register ID within the Bank */
  19. typedef uint32_t pcilib_register_addr_t; /**< Type holding the register ID within the Bank */
  20. typedef uint8_t pcilib_register_bank_t; /**< Type holding the register bank number */
  21. typedef uint8_t pcilib_register_bank_addr_t; /**< Type holding the register bank number */
  22. typedef uint8_t pcilib_register_size_t; /**< Type holding the size in bits of the register */
  23. typedef uint32_t pcilib_register_value_t; /**< Type holding the register value */
  24. typedef uint8_t pcilib_dma_engine_addr_t;
  25. typedef uint8_t pcilib_dma_engine_t;
  26. typedef uint64_t pcilib_event_id_t;
  27. typedef uint32_t pcilib_event_t;
  28. typedef uint64_t pcilib_timeout_t; /**< In microseconds */
  29. typedef enum {
  30. PCILIB_HOST_ENDIAN = 0,
  31. PCILIB_LITTLE_ENDIAN,
  32. PCILIB_BIG_ENDIAN
  33. } pcilib_endianess_t;
  34. typedef enum {
  35. PCILIB_MODEL_DETECT,
  36. PCILIB_MODEL_PCI,
  37. PCILIB_MODEL_IPECAMERA,
  38. PCILIB_MODEL_KAPTURE
  39. } pcilib_model_t;
  40. typedef enum {
  41. PCILIB_REGISTER_R = 1,
  42. PCILIB_REGISTER_W = 2,
  43. PCILIB_REGISTER_RW = 3,
  44. PCILIB_REGISTER_W1C = 4, /**< writting 1 resets the flag */
  45. PCILIB_REGISTER_RW1C = 5
  46. } pcilib_register_mode_t;
  47. typedef enum {
  48. PCILIB_DEFAULT_PROTOCOL,
  49. IPECAMERA_REGISTER_PROTOCOL
  50. } pcilib_register_protocol_t;
  51. typedef enum {
  52. PCILIB_EVENT_DATA = 0, /**< default data format */
  53. PCILIB_EVENT_RAW_DATA = 1 /**< raw data */
  54. } pcilib_event_data_type_t;
  55. typedef enum {
  56. PCILIB_DMA_FLAGS_DEFAULT = 0,
  57. PCILIB_DMA_FLAG_EOP = 1, /**< last buffer of the packet */
  58. PCILIB_DMA_FLAG_WAIT = 2, /**< wait completion of write operation / wait for data during read operation */
  59. PCILIB_DMA_FLAG_MULTIPACKET = 4, /**< read multiple packets */
  60. PCILIB_DMA_FLAG_PERSISTENT = 8, /**< do not stop DMA engine on application termination / permanently close DMA engine on dma_stop */
  61. PCILIB_DMA_FLAG_IGNORE_ERRORS = 16 /**< do not crash on errors, but return appropriate error codes */
  62. } pcilib_dma_flags_t;
  63. typedef enum {
  64. PCILIB_STREAMING_STOP = 0, /**< stop streaming */
  65. PCILIB_STREAMING_CONTINUE = 1, /**< wait the default DMA timeout for a new data */
  66. PCILIB_STREAMING_WAIT = 2, /**< wait the specified timeout for a new data */
  67. PCILIB_STREAMING_CHECK = 3, /**< do not wait for the data, bail out imideatly if no data ready */
  68. PCILIB_STREAMING_FAIL = 4, /**< fail if data is not available on timeout */
  69. PCILIB_STREAMING_REQ_FRAGMENT = 5, /**< only fragment of a packet is read, wait for next fragment and fail if no data during DMA timeout */
  70. PCILIB_STREAMING_REQ_PACKET = 6, /**< wait for next packet and fail if no data during the specified timeout */
  71. PCILIB_STREAMING_TIMEOUT_MASK = 3 /**< mask specifying all timeout modes */
  72. } pcilib_streaming_action_t;
  73. typedef enum {
  74. PCILIB_EVENT_FLAGS_DEFAULT = 0,
  75. 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) */
  76. PCILIB_EVENT_FLAG_STOP_ONLY = 1, /**< Do not cleanup, just stop acquiring new frames, the cleanup should be requested afterwards */
  77. PCILIB_EVENT_FLAG_EOF = 2, /**< Indicates that it is the last part of the frame (not required) */
  78. PCILIB_EVENT_FLAG_PREPROCESS = 4 /**< Enables preprocessing of the raw data (decoding frames, etc.) */
  79. } pcilib_event_flags_t;
  80. typedef enum {
  81. PCILIB_EVENT_INFO_FLAG_BROKEN = 1 /**< Indicates broken frames (if this flag is fales, the frame still can be broken) */
  82. } pcilib_event_info_flags_t;
  83. typedef enum {
  84. PCILIB_REGISTER_STANDARD = 0,
  85. PCILIB_REGISTER_FIFO,
  86. PCILIB_REGISTER_BITS
  87. } pcilib_register_type_t;
  88. #define PCILIB_BAR_DETECT ((pcilib_bar_t)-1)
  89. #define PCILIB_BAR_INVALID ((pcilib_bar_t)-1)
  90. #define PCILIB_BAR0 0
  91. #define PCILIB_BAR1 1
  92. #define PCILIB_DMA_ENGINE_INVALID ((pcilib_dma_engine_t)-1)
  93. #define PCILIB_DMA_ENGINE_ALL ((pcilib_dma_engine_t)-1)
  94. #define PCILIB_DMA_FLAGS_DEFAULT ((pcilib_dma_flags_t)0)
  95. #define PCILIB_DMA_ENGINE_ADDR_INVALID ((pcilib_dma_engine_addr_t)-1)
  96. #define PCILIB_REGISTER_INVALID ((pcilib_register_t)-1)
  97. #define PCILIB_ADDRESS_INVALID ((uintptr_t)-1)
  98. #define PCILIB_REGISTER_BANK_INVALID ((pcilib_register_bank_t)-1)
  99. #define PCILIB_REGISTER_BANK0 0
  100. #define PCILIB_REGISTER_BANK1 1
  101. #define PCILIB_REGISTER_BANK2 2
  102. #define PCILIB_REGISTER_BANK3 3
  103. #define PCILIB_REGISTER_BANK_DMA 128
  104. #define PCILIB_EVENT0 1
  105. #define PCILIB_EVENT1 2
  106. #define PCILIB_EVENT2 4
  107. #define PCILIB_EVENT3 8
  108. #define PCILIB_EVENTS_ALL ((pcilib_event_t)-1)
  109. #define PCILIB_EVENT_INVALID ((pcilib_event_t)-1)
  110. #define PCILIB_EVENT_DATA_TYPE_INVALID ((pcilib_event_data_type_t)-1)
  111. #define PCILIB_TIMEOUT_INFINITE ((pcilib_timeout_t)-1)
  112. #define PCILIB_TIMEOUT_IMMEDIATE 0
  113. #define PCILIB_IRQ_TYPE_ALL 0
  114. #define PCILIB_IRQ_SOURCE_DEFAULT 0
  115. #define PCILIB_REGISTER_NO_BITS 0
  116. #define PCILIB_REGISTER_ALL_BITS ((pcilib_register_value_t)-1)
  117. typedef struct {
  118. pcilib_event_t type;
  119. uint64_t seqnum; /**< we will add seqnum_overflow if required */
  120. uint64_t offset; /**< nanoseconds */
  121. struct timeval timestamp; /**< most accurate timestamp */
  122. pcilib_event_info_flags_t flags; /**< flags */
  123. } pcilib_event_info_t;
  124. /**<
  125. * Callback function called when new data is read by DMA streaming function
  126. * @ctx - DMA Engine context
  127. * @flags - DMA Flags
  128. * @bufsize - size of data in bytes
  129. * @buf - data
  130. * @returns
  131. * <0 - error, stop streaming (the value is negative error code)
  132. * 0 - stop streaming (PCILIB_STREAMING_STOP)
  133. * 1 - wait DMA timeout and return gracefuly if no data (PCILIB_STREAMING_CONTINUE)
  134. * 2 - wait the specified timeout and return gracefuly if no data (PCILIB_STREAMING_WAIT)
  135. * 3 - check if more data is available without waiting, return gracefuly if not (PCILIB_STREAMING_CHECK)
  136. * 5 - wait DMA timeout and fail if no data (PCILIB_STREAMING_REQ_FRAGMENT)
  137. * 6 - wait the specified timeout and fail if no data (PCILIB_STREAMING_REQ_PACKET)
  138. */
  139. typedef int (*pcilib_dma_callback_t)(void *ctx, pcilib_dma_flags_t flags, size_t bufsize, void *buf);
  140. typedef int (*pcilib_event_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user);
  141. typedef int (*pcilib_event_rawdata_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, pcilib_event_flags_t flags, size_t size, void *data, void *user);
  142. typedef struct {
  143. pcilib_register_bank_addr_t addr;
  144. pcilib_bar_t bar; // optional
  145. size_t size;
  146. pcilib_register_protocol_t protocol;
  147. uintptr_t read_addr; // or offset if bar specified
  148. uintptr_t write_addr; // or offset if bar specified
  149. uint8_t raw_endianess;
  150. uint8_t access;
  151. uint8_t endianess;
  152. const char *format;
  153. const char *name;
  154. const char *description;
  155. } pcilib_register_bank_description_t;
  156. typedef struct {
  157. pcilib_register_addr_t addr;
  158. pcilib_register_size_t offset;
  159. pcilib_register_size_t bits;
  160. pcilib_register_value_t defvalue;
  161. pcilib_register_value_t rwmask; /**< 1 - read before write bits, 0 - zero should be written to preserve value
  162. Used to define how external bits of PCILIB_REGISTER_BITS registers are treated.
  163. Currently it is a bit confusing, we may find a better way in the next release */
  164. pcilib_register_mode_t mode;
  165. pcilib_register_type_t type;
  166. pcilib_register_bank_t bank;
  167. const char *name;
  168. const char *description;
  169. } pcilib_register_description_t;
  170. /**
  171. * Default mappings
  172. */
  173. typedef struct {
  174. uintptr_t start;
  175. uintptr_t end;
  176. pcilib_register_bank_addr_t bank;
  177. long addr_shift;
  178. } pcilib_register_range_t;
  179. typedef struct {
  180. pcilib_event_t evid;
  181. const char *name;
  182. const char *description;
  183. } pcilib_event_description_t;
  184. typedef struct {
  185. pcilib_event_data_type_t data_type;
  186. pcilib_event_t evid;
  187. const char *name;
  188. const char *description;
  189. } pcilib_event_data_type_description_t;
  190. typedef enum {
  191. PCILIB_DMA_IRQ = 1,
  192. PCILIB_EVENT_IRQ = 2
  193. } pcilib_irq_type_t;
  194. typedef enum {
  195. PCILIB_DMA_TO_DEVICE = 1,
  196. PCILIB_DMA_FROM_DEVICE = 2,
  197. PCILIB_DMA_BIDIRECTIONAL = 3
  198. } pcilib_dma_direction_t;
  199. typedef enum {
  200. PCILIB_DMA_TYPE_BLOCK,
  201. PCILIB_DMA_TYPE_PACKET,
  202. PCILIB_DMA_TYPE_UNKNOWN
  203. } pcilib_dma_engine_type_t;
  204. typedef struct {
  205. pcilib_dma_engine_addr_t addr;
  206. pcilib_dma_engine_type_t type;
  207. pcilib_dma_direction_t direction;
  208. size_t addr_bits;
  209. } pcilib_dma_engine_description_t;
  210. typedef struct {
  211. pcilib_dma_engine_description_t *engines[PCILIB_MAX_DMA_ENGINES + 1];
  212. } pcilib_dma_info_t;
  213. typedef struct {
  214. uint8_t access;
  215. uint8_t endianess;
  216. pcilib_register_description_t *registers;
  217. pcilib_register_bank_description_t *banks;
  218. pcilib_register_range_t *ranges;
  219. pcilib_event_description_t *events;
  220. pcilib_event_data_type_description_t *data_types;
  221. pcilib_dma_api_description_t *dma_api;
  222. pcilib_event_api_description_t *event_api;
  223. } pcilib_model_description_t;
  224. int pcilib_set_error_handler(void (*err)(const char *msg, ...), void (*warn)(const char *msg, ...));
  225. pcilib_model_t pcilib_get_model(pcilib_t *ctx);
  226. pcilib_model_description_t *pcilib_get_model_description(pcilib_t *ctx);
  227. pcilib_context_t *pcilib_get_implementation_context(pcilib_t *ctx);
  228. pcilib_t *pcilib_open(const char *device, pcilib_model_t model);
  229. void pcilib_close(pcilib_t *ctx);
  230. int pcilib_start_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  231. int pcilib_stop_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  232. // Interrupt API is preliminary and can be significantly changed in future
  233. int pcilib_enable_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_dma_flags_t flags);
  234. int pcilib_acknowledge_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_irq_source_t irq_source);
  235. int pcilib_disable_irq(pcilib_t *ctx, pcilib_dma_flags_t flags);
  236. int pcilib_wait_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source, pcilib_timeout_t timeout, size_t *count);
  237. int pcilib_clear_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source);
  238. void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar);
  239. void pcilib_unmap_bar(pcilib_t *ctx, pcilib_bar_t bar, void *data);
  240. char *pcilib_resolve_register_address(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr); // addr is offset if bar is specified
  241. char *pcilib_resolve_data_space(pcilib_t *ctx, uintptr_t addr, size_t *size);
  242. pcilib_register_bank_t pcilib_find_bank_by_addr(pcilib_t *ctx, pcilib_register_bank_addr_t bank);
  243. pcilib_register_bank_t pcilib_find_bank_by_name(pcilib_t *ctx, const char *bankname);
  244. pcilib_register_bank_t pcilib_find_bank(pcilib_t *ctx, const char *bank);
  245. pcilib_register_t pcilib_find_register(pcilib_t *ctx, const char *bank, const char *reg);
  246. pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event);
  247. pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type);
  248. pcilib_dma_engine_t pcilib_find_dma_by_addr(pcilib_t *ctx, pcilib_dma_direction_t direction, pcilib_dma_engine_addr_t dma);
  249. int pcilib_read(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  250. int pcilib_write(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  251. int pcilib_write_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  252. int pcilib_read_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  253. int pcilib_skip_dma(pcilib_t *ctx, pcilib_dma_engine_t dma);
  254. 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);
  255. 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 *written_bytes);
  256. int pcilib_read_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *read_bytes);
  257. 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 *read_bytes);
  258. int pcilib_write_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *written_bytes);
  259. 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);
  260. int pcilib_read_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  261. int pcilib_write_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  262. int pcilib_read_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t *value);
  263. int pcilib_write_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t value);
  264. int pcilib_read_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t *value);
  265. int pcilib_write_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t value);
  266. int pcilib_reset(pcilib_t *ctx);
  267. int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  268. /*
  269. * The recording of new events will be stopped after reaching max_events records
  270. * or when the specified amount of time is elapsed. However, the @pcilib_stop
  271. * function should be called still.
  272. * NOTE: This options may not be respected if the PCILIB_EVENT_FLAG_RAW_DATA_ONLY
  273. * is specified.
  274. */
  275. int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration);
  276. /*
  277. * Request auto-triggering while grabbing
  278. */
  279. int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  280. /*
  281. * Request streaming the rawdata from the event engine. It is fastest way to acuqire data.
  282. * No memory copies will be performed and DMA buffers will be directly passed to the user
  283. * callback. However, to prevent data loss, no processing should be done on the data. The
  284. * user callback is only expected to copy data into the appropriate place and return control
  285. * to the event engine.
  286. * The performance can be boosted further by disabling any data processing within the event
  287. * engine. Just pass PCILIB_EVENT_FLAG_RAW_DATA_ONLY flag to the @pcilib_start function.
  288. */
  289. int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user);
  290. /*
  291. * Configures maximal number of preprocessing threads. Actual amount of threads
  292. * may be bigger. For instance, additionaly a real-time reader thread will be
  293. * executed for most of hardware
  294. */
  295. int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads);
  296. int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags);
  297. int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags);
  298. int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user);
  299. 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);
  300. 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);
  301. 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);
  302. 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);
  303. 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);
  304. /*
  305. * This function is provided to find potentially corrupted data. If the data is overwritten by
  306. * the time return_data is called it will return error.
  307. */
  308. int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data);
  309. /*
  310. * @param data - will be allocated and shuld be freed if NULL, otherwise used and size should contain correct size.
  311. * In case of failure the content of data is undefined.
  312. * @param timeout - will be autotriggered if NULL
  313. */
  314. int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout);
  315. #endif /* _PCITOOL_PCILIB_H */