Skip to content

Commit

Permalink
remove invalid error logs generated by an usbipd while listing devices
Browse files Browse the repository at this point in the history
When an usipd lists exported devices, invalid error logs are generated.
(reported from #137) To suppress unnecessary logs, a stub traversing
routine first checks a traversed device has a stub device interface.
  • Loading branch information
cezanne committed Apr 28, 2020
1 parent 6c6273e commit 4406358
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
55 changes: 21 additions & 34 deletions userspace/lib/usbip_setupdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,21 @@ get_devno_from_inst_id(unsigned char devno_map[], const char *id_inst)
return devno;
}

int
traverse_usbdevs(walkfunc_t walker, BOOL present_only, void *ctx)
static int
traverse_dev_info(HDEVINFO dev_info, walkfunc_t walker, void *ctx)
{
HDEVINFO dev_info;
SP_DEVINFO_DATA dev_info_data;
DWORD flags = DIGCF_ALLCLASSES;
unsigned char devno_map[255];
int ret = 0;
int idx;

if (present_only)
flags |= DIGCF_PRESENT;
dev_info = SetupDiGetClassDevs(NULL, "USB", NULL, flags);
if (dev_info == INVALID_HANDLE_VALUE) {
err("%s: SetupDiGetClassDevs failed: 0x%lx\n", __FUNCTION__, GetLastError());
return -1;
}
int ret = 0;

memset(devno_map, 0, 255);
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
for (idx = 0;; idx++) {
char *id_inst;
devno_t devno;

if (!SetupDiEnumDeviceInfo(dev_info, idx, &dev_info_data))
{
if (!SetupDiEnumDeviceInfo(dev_info, idx, &dev_info_data)) {
DWORD err = GetLastError();

if (err != ERROR_NO_MORE_ITEMS) {
Expand All @@ -245,36 +234,34 @@ traverse_usbdevs(walkfunc_t walker, BOOL present_only, void *ctx)
}

int
traverse_intfdevs(walkfunc_t walker, LPCGUID pguid, void *ctx)
traverse_usbdevs(walkfunc_t walker, BOOL present_only, void *ctx)
{
HDEVINFO dev_info;
SP_DEVINFO_DATA dev_info_data;
int ret = 0;
int idx;
DWORD flags = DIGCF_ALLCLASSES;

dev_info = SetupDiGetClassDevs(pguid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (present_only)
flags |= DIGCF_PRESENT;
dev_info = SetupDiGetClassDevs(NULL, "USB", NULL, flags);
if (dev_info == INVALID_HANDLE_VALUE) {
err("%s: SetupDiGetClassDevs failed: 0x%lx\n", __FUNCTION__, GetLastError());
return -1;
}

dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
for (idx = 0;; idx++) {
if (!SetupDiEnumDeviceInfo(dev_info, idx, &dev_info_data)) {
DWORD err = GetLastError();
return traverse_dev_info(dev_info, walker, ctx);
}

if (err != ERROR_NO_MORE_ITEMS) {
err("%s: failed to get device information: err: %d\n", __FUNCTION__, err);
}
break;
}
ret = walker(dev_info, &dev_info_data, 0, ctx);
if (ret != 0)
break;
int
traverse_intfdevs(walkfunc_t walker, LPCGUID pguid, void *ctx)
{
HDEVINFO dev_info;

dev_info = SetupDiGetClassDevs(pguid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (dev_info == INVALID_HANDLE_VALUE) {
err("%s: SetupDiGetClassDevs failed: 0x%lx\n", __FUNCTION__, GetLastError());
return -1;
}

SetupDiDestroyDeviceInfoList(dev_info);
return ret;
return traverse_dev_info(dev_info, walker, ctx);
}

static BOOL
Expand Down
2 changes: 2 additions & 0 deletions userspace/src/usbipd/usbipd_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ walker_edev_list(HDEVINFO dev_info, PSP_DEVINFO_DATA pdev_info_data, devno_t dev
err("%s: out of memory", __FUNCTION__);
return 0;
}
if (!is_stub_devno(devno))
return 0;
if (!build_udev(devno, &edev->udev)) {
err("%s: cannot build usbip dev", __FUNCTION__);
free(edev);
Expand Down
21 changes: 21 additions & 0 deletions userspace/src/usbipd/usbipd_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ get_devpath_from_devno(devno_t devno)
return devpath;
}

static int
walker_check_stub(HDEVINFO dev_info, PSP_DEVINFO_DATA pdev_info_data, devno_t devno, void *ctx)
{
devno_t *pdevno = (devno_t *)ctx;

if (*pdevno == devno)
return 1;
return 0;
}

BOOL
is_stub_devno(devno_t devno)
{
int rc;

rc = traverse_intfdevs(walker_check_stub, &GUID_DEVINTERFACE_STUB_USBIP, &devno);
if (rc == 1)
return TRUE;
return FALSE;
}

BOOL
build_udev(devno_t devno, struct usbip_usb_device *pudev)
{
Expand Down
1 change: 1 addition & 0 deletions userspace/src/usbipd/usbipd_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@

#include <winsock2.h>

BOOL is_stub_devno(devno_t devno);
BOOL build_udev(devno_t devno, struct usbip_usb_device *pudev);
HANDLE open_stub_dev(devno_t devno);

0 comments on commit 4406358

Please sign in to comment.