From e5c98e6d9ab3a2d27c2d0b266be954e78663f482 Mon Sep 17 00:00:00 2001 From: Xiaoguang Wu Date: Wed, 12 Dec 2018 11:50:51 +0800 Subject: [PATCH] DM USB: add usb_dev_path_cmp function for convenience Comparing two USB devices' path is frequently used operation, abstract it as an seperated function for convenience. Tracked-On: #1893 Signed-off-by: Xiaoguang Wu Reviewed-by: Liang Yang Acked-by: Yu Wang --- devicemodel/hw/pci/xhci.c | 14 ++------------ devicemodel/hw/usb_core.c | 10 ++++++++++ devicemodel/include/usb_core.h | 1 + 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/devicemodel/hw/pci/xhci.c b/devicemodel/hw/pci/xhci.c index 51a19bb03..a6ac3e586 100644 --- a/devicemodel/hw/pci/xhci.c +++ b/devicemodel/hw/pci/xhci.c @@ -542,23 +542,13 @@ pci_xhci_get_native_port_index_by_path(struct pci_xhci_vdev *xdev, struct usb_devpath *path) { int i; - struct usb_devpath *p; assert(xdev); assert(path); - for (i = 0; i < XHCI_MAX_VIRT_PORTS; i++) { - p = &xdev->native_ports[i].info.path; - - if (p->bus != path->bus) - continue; - - if (p->depth != path->depth) - continue; - - if (memcmp(p->path, path->path, path->depth) == 0) + for (i = 0; i < XHCI_MAX_VIRT_PORTS; i++) + if (usb_dev_path_cmp(&xdev->native_ports[i].info.path, path)) return i; - } return -1; } diff --git a/devicemodel/hw/usb_core.c b/devicemodel/hw/usb_core.c index 75aa2c58e..61e7ae863 100644 --- a/devicemodel/hw/usb_core.c +++ b/devicemodel/hw/usb_core.c @@ -272,6 +272,16 @@ usb_dev_path(struct usb_devpath *path) return output; } +bool +usb_dev_path_cmp(struct usb_devpath *p1, struct usb_devpath *p2) +{ + if (!p1 || !p2) + return false; + + return (p1->bus == p2->bus && p1->depth == p2->depth && + memcmp(p1->path, p2->path, p1->depth) == 0); +} + int usb_get_hub_port_num(struct usb_devpath *path) { diff --git a/devicemodel/include/usb_core.h b/devicemodel/include/usb_core.h index d779acb2a..78f13963d 100644 --- a/devicemodel/include/usb_core.h +++ b/devicemodel/include/usb_core.h @@ -257,4 +257,5 @@ struct usb_data_xfer_block *usb_data_xfer_append(struct usb_data_xfer *xfer, int ccs); int usb_get_hub_port_num(struct usb_devpath *path); char *usb_dev_path(struct usb_devpath *path); +bool usb_dev_path_cmp(struct usb_devpath *p1, struct usb_devpath *p2); #endif /* _USB_CORE_H_ */