@@ -21,6 +21,41 @@ static struct usb_dev_sys_ctx_info g_ctx;
21
21
static inline uint8_t usb_dev_get_ep_type (struct usb_dev * udev , int pid ,
22
22
int epnum );
23
23
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
+
24
59
static int
25
60
usb_dev_scan_dev ()
26
61
{
@@ -29,7 +64,7 @@ usb_dev_scan_dev()
29
64
struct libusb_device * ldev ;
30
65
struct usb_native_devinfo di ;
31
66
struct libusb_device_descriptor d ;
32
- int rc ;
67
+ bool ret ;
33
68
34
69
if (!g_ctx .libusb_ctx )
35
70
return -1 ;
@@ -41,24 +76,9 @@ usb_dev_scan_dev()
41
76
for (i = 0 ; i < num_devs ; ++ i ) {
42
77
ldev = devlist [i ];
43
78
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)
54
81
continue ;
55
- }
56
-
57
- di .pid = d .idProduct ;
58
- di .vid = d .idVendor ;
59
- di .bcd = d .bcdUSB ;
60
- di .priv_data = ldev ;
61
-
62
82
if (ROOTHUB_PORT (di .path ) == 0 )
63
83
continue ;
64
84
if (d .bDeviceClass == LIBUSB_CLASS_HUB )
@@ -1067,8 +1087,9 @@ static int
1067
1087
usb_dev_native_sys_conn_cb (struct libusb_context * ctx , struct libusb_device
1068
1088
* ldev , libusb_hotplug_event event , void * pdata )
1069
1089
{
1070
- struct libusb_device_descriptor d ;
1071
1090
struct usb_native_devinfo di ;
1091
+ struct libusb_device_descriptor d ;
1092
+ bool ret ;
1072
1093
1073
1094
UPRINTF (LDBG , "connect event\r\n" );
1074
1095
@@ -1077,15 +1098,9 @@ usb_dev_native_sys_conn_cb(struct libusb_context *ctx, struct libusb_device
1077
1098
return -1 ;
1078
1099
}
1079
1100
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 ;
1089
1104
1090
1105
if (d .bDeviceClass == LIBUSB_CLASS_HUB )
1091
1106
return 0 ;
@@ -1100,8 +1115,9 @@ static int
1100
1115
usb_dev_native_sys_disconn_cb (struct libusb_context * ctx , struct libusb_device
1101
1116
* ldev , libusb_hotplug_event event , void * pdata )
1102
1117
{
1103
- struct libusb_device_descriptor d ;
1104
1118
struct usb_native_devinfo di ;
1119
+ struct libusb_device_descriptor d ;
1120
+ bool ret ;
1105
1121
1106
1122
UPRINTF (LDBG , "disconnect event\r\n" );
1107
1123
@@ -1110,16 +1126,9 @@ usb_dev_native_sys_disconn_cb(struct libusb_context *ctx, struct libusb_device
1110
1126
return -1 ;
1111
1127
}
1112
1128
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 ;
1123
1132
1124
1133
if (d .bDeviceClass == LIBUSB_CLASS_HUB )
1125
1134
return 0 ;
0 commit comments