def find_capability()

in pci_lib/pci_lib.py [0:0]


def find_capability(config, cap):
    """
    Finds the offset of a particular capability register in a PCI configuration
    space
    """
    vendor = read_u16(config, PCI_CONFIG_VENDOR_OFFSET)
    if vendor == 0xffff:
        # This indicates the device has likely gone missing
        config.exceptions.add(
            CapabilityDecodeError('PCI config space for device is inaccessible')
        )
        return None
    status = read_u16(config, PCI_STATUS_REGISTER)
    if (status & PCI_HAS_CAP_LIST) == 0:
        return None

    # detect looping
    config.been_there = defaultdict(bool)
    pos = PCI_CAP_LIST_PTR
    while pos < len(config):
        if config.been_there[pos]:
            config.exceptions.add(
                CapabilityDecodeError('Detected looping in capability decoding')
            )
            return None
        config.been_there[pos] = True
        pos = read_u8(config, pos)
        cap_id = read_u8(config, pos)
        if cap_id == cap:
            return pos
        pos += 1