+ if (h && h->fd)
+ res = stlink_usb_current_mode(handle, &mode);
+ else
+ res = ERROR_FAIL;
+ /* do not exit if return code != ERROR_OK,
+ it prevents us from closing jtag_libusb */
+
+ if (res == ERROR_OK) {
+ /* try to exit current mode */
+ switch (mode) {
+ case STLINK_DEV_DFU_MODE:
+ emode = STLINK_MODE_DFU;
+ break;
+ case STLINK_DEV_DEBUG_MODE:
+ emode = STLINK_MODE_DEBUG_SWD;
+ break;
+ case STLINK_DEV_SWIM_MODE:
+ emode = STLINK_MODE_DEBUG_SWIM;
+ break;
+ case STLINK_DEV_BOOTLOADER_MODE:
+ case STLINK_DEV_MASS_MODE:
+ default:
+ emode = STLINK_MODE_UNKNOWN;
+ break;
+ }
+
+ if (emode != STLINK_MODE_UNKNOWN)
+ stlink_usb_mode_leave(handle, emode);
+ /* do not check return code, it prevent
+ us from closing jtag_libusb */
+ }
+