diff options
author | Martin Mares <mj@ucw.cz> | 2018-10-14 22:53:52 +0200 |
---|---|---|
committer | Martin Mares <mj@ucw.cz> | 2018-10-14 22:53:52 +0200 |
commit | d7d9e30534eb55145e7033018ee945b09de6928a (patch) | |
tree | 3fc977ae6cf9d0f0035e878dc8ee0c7c3d7b2e5e | |
parent | 6639fd175ae561240639c2c54da409bbc0f94835 (diff) | |
download | pciutils-d7d9e30534eb55145e7033018ee945b09de6928a.tar.gz |
Cleaned up pci_find_cap_nr()
The cap_number is always set to the total number of capability instances
found, regardless of whether a match was found or not.
-rw-r--r-- | lib/caps.c | 42 | ||||
-rw-r--r-- | lib/pci.h | 2 |
2 files changed, 20 insertions, 24 deletions
@@ -110,38 +110,34 @@ pci_find_cap(struct pci_dev *d, unsigned int id, unsigned int type) /** * Finds a particular capability of a device * - * To select one capability if there are more than one with the same id you + * To select one capability if there are more than one with the same id, you * can provide a pointer to an unsigned int that contains the index which you * want as cap_number. If you don't care and are fine with the first one you - * can supply NULL. To cap_number the acutal number of capablities with that id - * will be written. - * - * @param d Which device to target - * @param id Capability ID - * @param type PCI_FILL_CAPS or PCI_FILL_EXT_CAPS - * @param cap_number Which instance of a capability to target - * @returns pointer to capability structure or NULL if not found + * can supply NULL. The cap_number will be replaced by the acutal number + * of capablities with that id. */ struct pci_cap * pci_find_cap_nr(struct pci_dev *d, unsigned int id, unsigned int type, unsigned int *cap_number) { struct pci_cap *c; - unsigned int target = 0; - if (cap_number != NULL) + struct pci_cap *found = NULL; + unsigned int target = (cap_number ? *cap_number : 0); + unsigned int index = 0; + + pci_fill_info_v35(d, ((type == PCI_CAP_NORMAL) ? PCI_FILL_CAPS : PCI_FILL_EXT_CAPS)); + + for (c=d->first_cap; c; c=c->next) { - target = *cap_number; - *cap_number = 0; + if (c->type == type && c->id == id) + { + if (target == index) + found = c; + } + index++; } - pci_fill_info_v35(d, ((type == PCI_CAP_NORMAL) - ? PCI_FILL_CAPS - : PCI_FILL_EXT_CAPS)); - for (c=d->first_cap; c; c=c->next) - if (c->type == type && c->id == id) - if (cap_number == NULL || target == *cap_number) - return c; - else - (*cap_number)++; - return NULL; + if (cap_number) + *cap_number = index; + return found; } @@ -212,7 +212,7 @@ struct pci_cap { struct pci_cap *pci_find_cap(struct pci_dev *, unsigned int id, unsigned int type) PCI_ABI; struct pci_cap *pci_find_cap_nr(struct pci_dev *, unsigned int id, unsigned int type, - unsigned int *cap_number) PCI_ABI; + unsigned int *cap_number) PCI_ABI; /* * Filters |