pci.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #if defined(__i386__) || defined(__x86_64__)
  2. #include <helpers/helpers.h>
  3. /*
  4. * pci_acc_init
  5. *
  6. * PCI access helper function depending on libpci
  7. *
  8. * **pacc : if a valid pci_dev is returned
  9. * *pacc must be passed to pci_acc_cleanup to free it
  10. *
  11. * domain: domain
  12. * bus: bus
  13. * slot: slot
  14. * func: func
  15. * vendor: vendor
  16. * device: device
  17. * Pass -1 for one of the six above to match any
  18. *
  19. * Returns :
  20. * struct pci_dev which can be used with pci_{read,write}_* functions
  21. * to access the PCI config space of matching pci devices
  22. */
  23. struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain, int bus,
  24. int slot, int func, int vendor, int dev)
  25. {
  26. struct pci_filter filter_nb_link;
  27. struct pci_dev *device;
  28. *pacc = pci_alloc();
  29. if (*pacc == NULL)
  30. return NULL;
  31. pci_filter_init(*pacc, &filter_nb_link);
  32. filter_nb_link.domain = domain;
  33. filter_nb_link.bus = bus;
  34. filter_nb_link.slot = slot;
  35. filter_nb_link.func = func;
  36. filter_nb_link.vendor = vendor;
  37. filter_nb_link.device = dev;
  38. pci_init(*pacc);
  39. pci_scan_bus(*pacc);
  40. for (device = (*pacc)->devices; device; device = device->next) {
  41. if (pci_filter_match(&filter_nb_link, device))
  42. return device;
  43. }
  44. pci_cleanup(*pacc);
  45. return NULL;
  46. }
  47. /* Typically one wants to get a specific slot(device)/func of the root domain
  48. and bus */
  49. struct pci_dev *pci_slot_func_init(struct pci_access **pacc, int slot,
  50. int func)
  51. {
  52. return pci_acc_init(pacc, 0, 0, slot, func, -1, -1);
  53. }
  54. #endif /* defined(__i386__) || defined(__x86_64__) */