pci.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 = { domain, bus, slot, func,
  27. vendor, dev };
  28. struct pci_dev *device;
  29. *pacc = pci_alloc();
  30. if (*pacc == NULL)
  31. return NULL;
  32. pci_init(*pacc);
  33. pci_scan_bus(*pacc);
  34. for (device = (*pacc)->devices; device; device = device->next) {
  35. if (pci_filter_match(&filter_nb_link, device))
  36. return device;
  37. }
  38. pci_cleanup(*pacc);
  39. return NULL;
  40. }
  41. /* Typically one wants to get a specific slot(device)/func of the root domain
  42. and bus */
  43. struct pci_dev *pci_slot_func_init(struct pci_access **pacc, int slot,
  44. int func)
  45. {
  46. return pci_acc_init(pacc, 0, 0, slot, func, -1, -1);
  47. }
  48. #endif /* defined(__i386__) || defined(__x86_64__) */