int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,

                       uint8_t offset, uint8_t size,

                       Error **errp)

{

    uint8_t *config;

    int i, overlapping_cap;



    if (!offset) {

        offset = pci_find_space(pdev, size);

        /* out of PCI config space is programming error */

        assert(offset);

    } else {

        /* Verify that capabilities don't overlap.  Note: device assignment

         * depends on this check to verify that the device is not broken.

         * Should never trigger for emulated devices, but it's helpful

         * for debugging these. */

        for (i = offset; i < offset + size; i++) {

            overlapping_cap = pci_find_capability_at_offset(pdev, i);

            if (overlapping_cap) {

                error_setg(errp, "%s:%02x:%02x.%x "

                           "Attempt to add PCI capability %x at offset "

                           "%x overlaps existing capability %x at offset %x",

                           pci_root_bus_path(pdev), pci_bus_num(pdev->bus),

                           PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),

                           cap_id, offset, overlapping_cap, i);

                return -EINVAL;

            }

        }

    }



    config = pdev->config + offset;

    config[PCI_CAP_LIST_ID] = cap_id;

    config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];

    pdev->config[PCI_CAPABILITY_LIST] = offset;

    pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;

    memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));

    /* Make capability read-only by default */

    memset(pdev->wmask + offset, 0, size);

    /* Check capability by default */

    memset(pdev->cmask + offset, 0xFF, size);

    return offset;

}

int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,

                       uint8_t offset, uint8_t size,

                       Error **errp)

{

    uint8_t *config;

    int i, overlapping_cap;



    if (!offset) {

        offset = pci_find_space(pdev, size);

        

        assert(offset);

    } else {

        







        for (i = offset; i < offset + size; i++) {

            overlapping_cap = pci_find_capability_at_offset(pdev, i);

            if (overlapping_cap) {

                error_setg(errp, "%s:%02x:%02x.%x "

                           "Attempt to add PCI capability %x at offset "

                           "%x overlaps existing capability %x at offset %x",

                           pci_root_bus_path(pdev), pci_bus_num(pdev->bus),

                           PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),

                           cap_id, offset, overlapping_cap, i);

                return -EINVAL;

            }

        }

    }



    config = pdev->config + offset;

    config[PCI_CAP_LIST_ID] = cap_id;

    config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];

    pdev->config[PCI_CAPABILITY_LIST] = offset;

    pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;

    memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));

    

    memset(pdev->wmask + offset, 0, size);

    

    memset(pdev->cmask + offset, 0xFF, size);

    return offset;

}

int FUNC_0(PCIDevice *VAR_0, uint8_t VAR_1,

                       uint8_t VAR_2, uint8_t VAR_3,

                       Error **VAR_4)

{

    uint8_t *config;

    int VAR_5, VAR_6;



    if (!VAR_2) {

        VAR_2 = pci_find_space(VAR_0, VAR_3);

        

        assert(VAR_2);

    } else {

        







        for (VAR_5 = VAR_2; VAR_5 < VAR_2 + VAR_3; VAR_5++) {

            VAR_6 = pci_find_capability_at_offset(VAR_0, VAR_5);

            if (VAR_6) {

                error_setg(VAR_4, "%s:%02x:%02x.%x "

                           "Attempt to add PCI capability %x at VAR_2 "

                           "%x overlaps existing capability %x at VAR_2 %x",

                           pci_root_bus_path(VAR_0), pci_bus_num(VAR_0->bus),

                           PCI_SLOT(VAR_0->devfn), PCI_FUNC(VAR_0->devfn),

                           VAR_1, VAR_2, VAR_6, VAR_5);

                return -EINVAL;

            }

        }

    }



    config = VAR_0->config + VAR_2;

    config[PCI_CAP_LIST_ID] = VAR_1;

    config[PCI_CAP_LIST_NEXT] = VAR_0->config[PCI_CAPABILITY_LIST];

    VAR_0->config[PCI_CAPABILITY_LIST] = VAR_2;

    VAR_0->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;

    memset(VAR_0->used + VAR_2, 0xFF, QEMU_ALIGN_UP(VAR_3, 4));

    

    memset(VAR_0->wmask + VAR_2, 0, VAR_3);

    

    memset(VAR_0->cmask + VAR_2, 0xFF, VAR_3);

    return VAR_2;

}

int FUNC_0(PCIDevice *VAR_0, uint8_t VAR_1,
uint8_t VAR_2, uint8_t VAR_3,
Error **VAR_4)
{ uint8_t *config; int VAR_5, VAR_6; if (!VAR_2) { VAR_2 = pci_find_space(VAR_0, VAR_3); assert(VAR_2); } else { for (VAR_5 = VAR_2; VAR_5 < VAR_2 + VAR_3; VAR_5++) { VAR_6 = pci_find_capability_at_offset(VAR_0, VAR_5); if (VAR_6) { error_setg(VAR_4, "%s:%02x:%02x.%x "
"Attempt to add PCI capability %x at VAR_2 "
"%x overlaps existing capability %x at VAR_2 %x",
pci_root_bus_path(VAR_0), pci_bus_num(VAR_0->bus),
PCI_SLOT(VAR_0->devfn), PCI_FUNC(VAR_0->devfn),
VAR_1, VAR_2, VAR_6, VAR_5); return -EINVAL; } } } config = VAR_0->config + VAR_2; config[PCI_CAP_LIST_ID] = VAR_1; config[PCI_CAP_LIST_NEXT] = VAR_0->config[PCI_CAPABILITY_LIST]; VAR_0->config[PCI_CAPABILITY_LIST] = VAR_2; VAR_0->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; memset(VAR_0->used + VAR_2, 0xFF, QEMU_ALIGN_UP(VAR_3, 4)); memset(VAR_0->wmask + VAR_2, 0, VAR_3); memset(VAR_0->cmask + VAR_2, 0xFF, VAR_3); return VAR_2; }