lowl-pci.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef BFG_LOWL_PCI_H
  2. #define BFG_LOWL_PCI_H
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. struct lowl_pci_handle;
  9. struct _lowl_pci_config {
  10. int bar;
  11. size_t sz;
  12. int mode;
  13. };
  14. extern struct lowl_pci_handle *lowl_pci_open(const char *, const struct _lowl_pci_config *);
  15. #define LP_BARINFO(...) (struct _lowl_pci_config[]){__VA_ARGS__ { .bar = -1 }}
  16. #define LP_BAR(barno, size, mode) {barno, size, mode}
  17. extern void lowl_pci_close(struct lowl_pci_handle *);
  18. // Don't assume buf is used in any specific way! Memory returned may be mmap'd (and thus change after call)
  19. extern const uint32_t *lowl_pci_get_words(struct lowl_pci_handle *, void *buf, size_t words, int bar, off_t);
  20. extern bool lowl_pci_set_words(struct lowl_pci_handle *, const uint32_t *, size_t, int bar, off_t);
  21. // buf passed to lowl_pci_get_data must have at least LOWL_PCI_GET_DATA_PADDING bytes more than size to read
  22. #define LOWL_PCI_GET_DATA_PADDING 6
  23. extern const void *lowl_pci_get_data(struct lowl_pci_handle *, void *buf, size_t, int bar, off_t);
  24. extern bool lowl_pci_set_data(struct lowl_pci_handle *, const void *, size_t, int bar, off_t);
  25. static inline
  26. uint32_t lowl_pci_get_word(struct lowl_pci_handle * const lph, const int bar, const off_t offset)
  27. {
  28. uint32_t buf[1];
  29. const uint32_t * const p = lowl_pci_get_words(lph, buf, 1, bar, offset);
  30. if (!p)
  31. return 0;
  32. return *p;
  33. }
  34. static inline
  35. bool lowl_pci_set_word(struct lowl_pci_handle * const lph, const int bar, const off_t offset, const uint32_t val)
  36. {
  37. return lowl_pci_set_words(lph, &val, 1, bar, offset);
  38. }
  39. static inline
  40. bool lowl_pci_read_words(struct lowl_pci_handle * const lph, void * const buf, const size_t words, const int bar, const off_t offset)
  41. {
  42. const void * const p = lowl_pci_get_words(lph, buf, words, bar, offset);
  43. if (!p)
  44. return false;
  45. if (buf != p)
  46. memmove(buf, p, words * 4);
  47. return true;
  48. }
  49. static inline
  50. bool lowl_pci_read_data(struct lowl_pci_handle * const lph, void * const buf, const size_t sz, const int bar, const off_t offset)
  51. {
  52. const void * const p = lowl_pci_get_data(lph, buf, sz, bar, offset);
  53. if (!p)
  54. return false;
  55. if (buf != p)
  56. memmove(buf, p, sz);
  57. return true;
  58. }
  59. #endif