Skip to content

Commit 540ce05

Browse files
xiaoguangwuNanlinXie
authored andcommitted
DM USB: introduce function usb_get_native_devinfo
There are many places in USB emulation implementation to get native USB device infomation, and the related codes are long and repeated many times. This patch introduces function usb_get_native_devinfo to remove redundent codes for the purpose mentioned above. Tracked-On: #1434 Signed-off-by: Xiaoguang Wu <xiaoguang.wu@intel.com> Signed-off-by: Liang Yang <liang3.yang@intel.com> Acked-by: Yu Wang <yu1.wang@intel.com>
1 parent e8f7b6f commit 540ce05

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

devicemodel/hw/platform/usb_pmapper.c

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@ static struct usb_dev_sys_ctx_info g_ctx;
2121
static inline uint8_t usb_dev_get_ep_type(struct usb_dev *udev, int pid,
2222
int epnum);
2323

24+
static bool
25+
usb_get_native_devinfo(struct libusb_device *ldev,
26+
struct usb_native_devinfo *info,
27+
struct libusb_device_descriptor *desc)
28+
{
29+
struct libusb_device_descriptor d;
30+
int rc;
31+
32+
if (!ldev || !info)
33+
return false;
34+
35+
memset(info, 0, sizeof(*info));
36+
info->speed = libusb_get_device_speed(ldev);
37+
info->priv_data = ldev;
38+
info->path.bus = libusb_get_bus_number(ldev);
39+
info->path.depth = libusb_get_port_numbers(ldev, info->path.path,
40+
USB_MAX_TIERS);
41+
42+
rc = libusb_get_device_descriptor(ldev, &d);
43+
if (rc) {
44+
UPRINTF(LWRN, "fail to get descriptor for %d-%s\r\n",
45+
info->path.bus, usb_dev_path(&info->path));
46+
return false;
47+
}
48+
49+
info->pid = d.idProduct;
50+
info->vid = d.idVendor;
51+
info->bcd = d.bcdUSB;
52+
53+
if (desc != NULL)
54+
*desc = d;
55+
56+
return true;
57+
}
58+
2459
static int
2560
usb_dev_scan_dev()
2661
{
@@ -29,7 +64,7 @@ usb_dev_scan_dev()
2964
struct libusb_device *ldev;
3065
struct usb_native_devinfo di;
3166
struct libusb_device_descriptor d;
32-
int rc;
67+
bool ret;
3368

3469
if (!g_ctx.libusb_ctx)
3570
return -1;
@@ -41,24 +76,9 @@ usb_dev_scan_dev()
4176
for (i = 0; i < num_devs; ++i) {
4277
ldev = devlist[i];
4378

44-
memset(&di, 0, sizeof(di));
45-
di.path.bus = libusb_get_bus_number(ldev);
46-
di.path.depth = libusb_get_port_numbers(ldev, di.path.path,
47-
USB_MAX_TIERS);
48-
di.speed = libusb_get_device_speed(ldev);
49-
50-
rc = libusb_get_device_descriptor(ldev, &d);
51-
if (rc) {
52-
UPRINTF(LWRN, "fail to get descriptor for %d-%s\r\n",
53-
di.path.bus, usb_dev_path(&di.path));
79+
ret = usb_get_native_devinfo(ldev, &di, &d);
80+
if (ret == false)
5481
continue;
55-
}
56-
57-
di.pid = d.idProduct;
58-
di.vid = d.idVendor;
59-
di.bcd = d.bcdUSB;
60-
di.priv_data = ldev;
61-
6282
if (ROOTHUB_PORT(di.path) == 0)
6383
continue;
6484
if (d.bDeviceClass == LIBUSB_CLASS_HUB)
@@ -1067,8 +1087,9 @@ static int
10671087
usb_dev_native_sys_conn_cb(struct libusb_context *ctx, struct libusb_device
10681088
*ldev, libusb_hotplug_event event, void *pdata)
10691089
{
1070-
struct libusb_device_descriptor d;
10711090
struct usb_native_devinfo di;
1091+
struct libusb_device_descriptor d;
1092+
bool ret;
10721093

10731094
UPRINTF(LDBG, "connect event\r\n");
10741095

@@ -1077,15 +1098,9 @@ usb_dev_native_sys_conn_cb(struct libusb_context *ctx, struct libusb_device
10771098
return -1;
10781099
}
10791100

1080-
libusb_get_device_descriptor(ldev, &d);
1081-
di.path.bus = libusb_get_bus_number(ldev);
1082-
di.path.depth = libusb_get_port_numbers(ldev, di.path.path,
1083-
USB_MAX_TIERS);
1084-
di.speed = libusb_get_device_speed(ldev);
1085-
di.pid = d.idProduct;
1086-
di.vid = d.idVendor;
1087-
di.bcd = d.bcdUSB;
1088-
di.priv_data = ldev;
1101+
ret = usb_get_native_devinfo(ldev, &di, &d);
1102+
if (ret == false)
1103+
return 0;
10891104

10901105
if (d.bDeviceClass == LIBUSB_CLASS_HUB)
10911106
return 0;
@@ -1100,8 +1115,9 @@ static int
11001115
usb_dev_native_sys_disconn_cb(struct libusb_context *ctx, struct libusb_device
11011116
*ldev, libusb_hotplug_event event, void *pdata)
11021117
{
1103-
struct libusb_device_descriptor d;
11041118
struct usb_native_devinfo di;
1119+
struct libusb_device_descriptor d;
1120+
bool ret;
11051121

11061122
UPRINTF(LDBG, "disconnect event\r\n");
11071123

@@ -1110,16 +1126,9 @@ usb_dev_native_sys_disconn_cb(struct libusb_context *ctx, struct libusb_device
11101126
return -1;
11111127
}
11121128

1113-
libusb_get_device_descriptor(ldev, &d);
1114-
di.path.bus = libusb_get_bus_number(ldev);
1115-
di.speed = libusb_get_device_speed(ldev);
1116-
di.path.depth = libusb_get_port_numbers(ldev, di.path.path,
1117-
USB_MAX_TIERS);
1118-
1119-
di.pid = d.idProduct;
1120-
di.vid = d.idVendor;
1121-
di.bcd = d.bcdUSB;
1122-
di.priv_data = ldev;
1129+
ret = usb_get_native_devinfo(ldev, &di, &d);
1130+
if (ret == false)
1131+
return 0;
11231132

11241133
if (d.bDeviceClass == LIBUSB_CLASS_HUB)
11251134
return 0;

0 commit comments

Comments
 (0)