Skip to content

Commit d6cc701

Browse files
xiaoguangwulijinxia
authored andcommitted
DM USB: refine logic of toggling interface state
Refine the logic of usb interface state transition. The libusb uses two pair of APIs to deal with usb interface: 1. libusb_claim_interface & libusb_detach_kernel_driver; 2. libusb_release_interface & libusb_attach_kernel_driver. The calling sequences of those APIs are very important, so this patch add some error handling code to make this process more robust. Change-Id: I0f7950aae806dee9a21f16cc293f51609eede0d8 Tracked-On: Signed-off-by: Xiaoguang Wu <xiaoguang.wu@intel.com> Reviewed-by: Liang Yang <liang3.yang@intel.com> Acked-by: Yu Wang <yu1.wang@intel.com>
1 parent 5317124 commit d6cc701

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

devicemodel/hw/platform/usb_pmapper.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,21 @@ usb_dev_native_toggle_if(struct usb_dev *udev, int claim)
356356
for (i = 0; i < config->bNumInterfaces; i++) {
357357
if (claim == 1)
358358
r = libusb_claim_interface(udev->handle, i);
359-
else
359+
else {
360360
r = libusb_release_interface(udev->handle, i);
361-
361+
/* according to libusb, if libusb_release_interface
362+
* return LIBUSB_ERROR_NOT_FOUND, it means that this
363+
* interface is not claimed before. This case should
364+
* not be considered as an error here.
365+
*/
366+
if (r == LIBUSB_ERROR_NOT_FOUND)
367+
r = 0;
368+
}
362369
if (r) {
363370
rc = -1;
364-
UPRINTF(LWRN, "%d-%d:%d.%d can't %s if\r\n", b, p, c, i,
365-
claim == 1 ? "claim" : "release");
371+
UPRINTF(LWRN, "%d-%d:%d.%d can't %s if, r %d\r\n", b,
372+
p, c, i, claim == 1 ? "claim" :
373+
"release", r);
366374
}
367375
}
368376
if (rc)
@@ -393,17 +401,23 @@ usb_dev_native_toggle_if_drivers(struct usb_dev *udev, int attach)
393401
return -1;
394402
}
395403

404+
UPRINTF(LDBG, "%s driver\r\n", attach == 1 ? "attach" : "detach");
405+
396406
c = config->bConfigurationValue;
397407
for (i = 0; i < config->bNumInterfaces; i++) {
398408
if (attach == 1)
399409
r = libusb_attach_kernel_driver(udev->handle, i);
400-
else
401-
r = libusb_detach_kernel_driver(udev->handle, i);
410+
else {
411+
if (libusb_kernel_driver_active(udev->handle, i) == 1)
412+
r = libusb_detach_kernel_driver(udev->handle,
413+
i);
414+
}
402415

403416
if (r) {
404417
rc = -1;
405-
UPRINTF(LWRN, "%d-%d:%d.%d can't %stach if driver\r\n",
406-
b, p, c, i, attach == 1 ? "at" : "de");
418+
UPRINTF(LWRN, "%d-%d:%d.%d can't %stach if driver, r %d"
419+
"\r\n", b, p, c, i, attach == 1 ? "at" :
420+
"de", r);
407421
}
408422
}
409423
if (rc)

0 commit comments

Comments
 (0)