fix(#5): add USB retry logic for LIBUSB_ERROR_PIPE
Resolves #5: LIBUSB_ERROR_PIPE Root cause: USB control transfers had zero retry logic. LIBUSB_ERROR_PIPE (-9) is a transient STALL that commonly occurs on A12+ devices during DFU operations and typically clears on retry. Additionally, path_b_identity.c called libusb_control_transfer directly, bypassing the usb_helpers wrapper entirely. Changes: - usb_ctrl_transfer/usb_ctrl_transfer_no_data now retry up to 3 times on PIPE and TIMEOUT errors with 50ms delay between attempts - usb_print_error provides actionable troubleshooting steps for PIPE errors - path_b_identity.c now uses usb_ctrl_transfer (retry-aware) instead of calling libusb_control_transfer directly - usb_dfu_read_info adds retry logic for serial descriptor reads - start.sh: handle_dfu_requirement now guides A12+ Path B devices into DFU (previously only guided checkm8 Path A devices) - start.sh: DFU-mode detection now queries CPID to correctly identify Path A vs Path B (previously assumed all DFU devices were Path A) Files modified: - src/util/usb_helpers.c - src/bypass/path_b_identity.c - src/device/usb_dfu.c - start.sh Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
92e9f99f25
commit
58f04f2be0
4 changed files with 110 additions and 51 deletions
|
|
@ -13,6 +13,7 @@
|
|||
|
||||
#include "bypass/path_b.h"
|
||||
#include "device/usb_dfu.h"
|
||||
#include "util/usb_helpers.h"
|
||||
#include "util/log.h"
|
||||
|
||||
/* USB descriptor request types */
|
||||
|
|
@ -44,7 +45,7 @@ static int usb_get_string_descriptor(libusb_device_handle *usb,
|
|||
return -1;
|
||||
}
|
||||
|
||||
rc = libusb_control_transfer(
|
||||
rc = usb_ctrl_transfer(
|
||||
usb,
|
||||
0x80, /* bmRequestType */
|
||||
USB_REQ_GET_DESCRIPTOR, /* bRequest */
|
||||
|
|
@ -58,6 +59,7 @@ static int usb_get_string_descriptor(libusb_device_handle *usb,
|
|||
if (rc < 0) {
|
||||
log_error("[path_b_id] GET_DESCRIPTOR failed: %s",
|
||||
libusb_error_name(rc));
|
||||
usb_print_error(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -194,7 +196,7 @@ int path_b_write_serial(device_info_t *dev, const char *new_serial)
|
|||
}
|
||||
|
||||
/* SET_DESCRIPTOR: host-to-device, standard, device recipient */
|
||||
rc = libusb_control_transfer(
|
||||
rc = usb_ctrl_transfer(
|
||||
dev->usb,
|
||||
0x00, /* bmRequestType */
|
||||
USB_REQ_SET_DESCRIPTOR, /* bRequest */
|
||||
|
|
@ -208,7 +210,7 @@ int path_b_write_serial(device_info_t *dev, const char *new_serial)
|
|||
if (rc < 0) {
|
||||
log_warn("[path_b_id] SET_DESCRIPTOR returned: %s",
|
||||
libusb_error_name(rc));
|
||||
/* TODO: fallback vendor-specific transfer for A12+ DFU firmware */
|
||||
usb_print_error(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "device/usb_dfu.h"
|
||||
#include "util/usb_helpers.h"
|
||||
|
|
@ -24,31 +25,21 @@
|
|||
/* Module-global libusb context */
|
||||
static libusb_context *g_ctx = NULL;
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Lifecycle */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
int usb_dfu_init(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (g_ctx) {
|
||||
log_warn("usb_dfu_init: libusb context already initialized");
|
||||
if (g_ctx)
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = libusb_init(&g_ctx);
|
||||
if (ret != LIBUSB_SUCCESS) {
|
||||
log_error("libusb_init failed: %s", libusb_strerror(ret));
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if LIBUSB_API_VERSION >= 0x01000106
|
||||
libusb_set_option(g_ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_WARNING);
|
||||
#else
|
||||
libusb_set_debug(g_ctx, LIBUSB_LOG_LEVEL_WARNING);
|
||||
#endif
|
||||
|
||||
log_debug("libusb context initialized");
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -58,14 +49,9 @@ void usb_dfu_cleanup(void)
|
|||
if (g_ctx) {
|
||||
libusb_exit(g_ctx);
|
||||
g_ctx = NULL;
|
||||
log_debug("libusb context cleaned up");
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Device discovery */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
int usb_dfu_find(libusb_device_handle **handle)
|
||||
{
|
||||
libusb_device **devs = NULL;
|
||||
|
|
@ -128,15 +114,7 @@ int usb_dfu_find(libusb_device_handle **handle)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Serial string parsing */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/*
|
||||
* Parse a hex value following a key prefix in the serial string.
|
||||
* Example: parse_hex_field("CPID:8015 ECID:...", "CPID:", &val)
|
||||
* Returns 0 on success, -1 if the key is not found.
|
||||
*/
|
||||
/* Parse hex value after key prefix (e.g. "CPID:" -> 0x8015). */
|
||||
static int parse_hex_field(const char *serial, const char *key, uint64_t *out)
|
||||
{
|
||||
const char *p;
|
||||
|
|
@ -171,12 +149,28 @@ int usb_dfu_read_info(libusb_device_handle *handle, uint32_t *cpid,
|
|||
if (ecid) *ecid = 0;
|
||||
if (serial && serial_len > 0) serial[0] = '\0';
|
||||
|
||||
/* Read the serial string descriptor (index 3 for Apple DFU) */
|
||||
ret = libusb_get_string_descriptor_ascii(handle, DFU_SERIAL_INDEX,
|
||||
buf, sizeof(buf));
|
||||
/* Read the serial string descriptor (index 3 for Apple DFU).
|
||||
* Retry on transient PIPE/TIMEOUT errors -- common on A12+ DFU. */
|
||||
{
|
||||
int attempt;
|
||||
for (attempt = 0; attempt < 3; attempt++) {
|
||||
ret = libusb_get_string_descriptor_ascii(handle, DFU_SERIAL_INDEX,
|
||||
buf, sizeof(buf));
|
||||
if (ret >= 0)
|
||||
break;
|
||||
if (ret != LIBUSB_ERROR_PIPE && ret != LIBUSB_ERROR_TIMEOUT)
|
||||
break;
|
||||
if (attempt < 2) {
|
||||
log_warn("serial descriptor read: %s (attempt %d/3, retrying)",
|
||||
libusb_strerror(ret), attempt + 1);
|
||||
usleep(50000);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
log_error("failed to read serial descriptor: %s",
|
||||
libusb_strerror(ret));
|
||||
usb_print_error(ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -214,10 +208,6 @@ int usb_dfu_read_info(libusb_device_handle *handle, uint32_t *cpid,
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Data transfer */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
int usb_dfu_send(libusb_device_handle *handle, const void *data, size_t len)
|
||||
{
|
||||
size_t sent = 0;
|
||||
|
|
@ -267,10 +257,6 @@ int usb_dfu_recv(libusb_device_handle *handle, void *buf, size_t len,
|
|||
else
|
||||
xfer_len = (uint16_t)len;
|
||||
|
||||
/*
|
||||
* DFU UPLOAD: bmRequestType = device-to-host, class, interface
|
||||
* bRequest = DFU_UPLOAD (2), wValue = block number, wIndex = 0
|
||||
*/
|
||||
ret = usb_ctrl_transfer(handle, DFU_REQUEST_IN, DFU_UPLOAD,
|
||||
0, 0, (unsigned char *)buf,
|
||||
xfer_len, DFU_USB_TIMEOUT);
|
||||
|
|
@ -285,10 +271,6 @@ int usb_dfu_recv(libusb_device_handle *handle, void *buf, size_t len,
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Cleanup */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void usb_dfu_close(libusb_device_handle *handle)
|
||||
{
|
||||
if (!handle)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,24 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include "util/usb_helpers.h"
|
||||
|
||||
/* Maximum retry attempts for transient USB errors (PIPE/STALL) */
|
||||
#define USB_PIPE_MAX_RETRIES 3
|
||||
|
||||
/* Delay between retries in microseconds (50ms) */
|
||||
#define USB_PIPE_RETRY_DELAY 50000
|
||||
|
||||
/*
|
||||
* is_transient_usb_error -- Returns 1 if the libusb error code
|
||||
* represents a transient condition that may succeed on retry.
|
||||
* LIBUSB_ERROR_PIPE (-9) means the device STALLed the endpoint,
|
||||
* which is common during DFU operations and often clears on retry.
|
||||
*/
|
||||
static int is_transient_usb_error(int err)
|
||||
{
|
||||
return (err == LIBUSB_ERROR_PIPE || err == LIBUSB_ERROR_TIMEOUT);
|
||||
}
|
||||
|
||||
int usb_ctrl_transfer(libusb_device_handle *dev,
|
||||
uint8_t bmRequestType,
|
||||
uint8_t bRequest,
|
||||
|
|
@ -11,12 +29,29 @@ int usb_ctrl_transfer(libusb_device_handle *dev,
|
|||
unsigned int timeout)
|
||||
{
|
||||
int ret;
|
||||
int attempt;
|
||||
|
||||
if (!dev)
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
|
||||
ret = libusb_control_transfer(dev, bmRequestType, bRequest,
|
||||
wValue, wIndex, data, wLength, timeout);
|
||||
for (attempt = 0; attempt < USB_PIPE_MAX_RETRIES; attempt++) {
|
||||
ret = libusb_control_transfer(dev, bmRequestType, bRequest,
|
||||
wValue, wIndex, data, wLength, timeout);
|
||||
if (ret >= 0)
|
||||
return ret;
|
||||
|
||||
if (!is_transient_usb_error(ret))
|
||||
return ret;
|
||||
|
||||
/* Transient error -- retry after brief delay */
|
||||
if (attempt < USB_PIPE_MAX_RETRIES - 1) {
|
||||
fprintf(stderr, "[usb] transient error %s on attempt %d/%d, "
|
||||
"retrying...\n", libusb_strerror(ret),
|
||||
attempt + 1, USB_PIPE_MAX_RETRIES);
|
||||
usleep(USB_PIPE_RETRY_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
@ -28,18 +63,47 @@ int usb_ctrl_transfer_no_data(libusb_device_handle *dev,
|
|||
unsigned int timeout)
|
||||
{
|
||||
int ret;
|
||||
int attempt;
|
||||
|
||||
if (!dev)
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
|
||||
ret = libusb_control_transfer(dev, bmRequestType, bRequest,
|
||||
wValue, wIndex, NULL, 0, timeout);
|
||||
/* libusb returns 0 for transfers with no data stage on success */
|
||||
return (ret < 0) ? ret : 0;
|
||||
for (attempt = 0; attempt < USB_PIPE_MAX_RETRIES; attempt++) {
|
||||
ret = libusb_control_transfer(dev, bmRequestType, bRequest,
|
||||
wValue, wIndex, NULL, 0, timeout);
|
||||
if (ret >= 0)
|
||||
return 0;
|
||||
|
||||
if (!is_transient_usb_error(ret))
|
||||
return ret;
|
||||
|
||||
if (attempt < USB_PIPE_MAX_RETRIES - 1) {
|
||||
fprintf(stderr, "[usb] transient error %s on attempt %d/%d, "
|
||||
"retrying...\n", libusb_strerror(ret),
|
||||
attempt + 1, USB_PIPE_MAX_RETRIES);
|
||||
usleep(USB_PIPE_RETRY_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void usb_print_error(int libusb_error)
|
||||
{
|
||||
fprintf(stderr, "[usb] error %d: %s\n",
|
||||
libusb_error, libusb_strerror((enum libusb_error)libusb_error));
|
||||
|
||||
if (libusb_error == LIBUSB_ERROR_PIPE) {
|
||||
fprintf(stderr, "[usb] PIPE error: the device STALLed the transfer.\n"
|
||||
"[usb] Troubleshooting:\n"
|
||||
"[usb] 1. Re-enter DFU mode and try again\n"
|
||||
"[usb] 2. Try a different USB port (prefer direct, "
|
||||
"not a hub)\n"
|
||||
"[usb] 3. Try a different USB cable (original Apple "
|
||||
"cable recommended)\n"
|
||||
"[usb] 4. On WSL/Linux: ensure usbipd or usbmuxd is "
|
||||
"running\n"
|
||||
"[usb] 5. On macOS: native USB tends to be more "
|
||||
"reliable\n");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
start.sh
15
start.sh
|
|
@ -482,13 +482,24 @@ main() {
|
|||
display_device_info
|
||||
gate_support
|
||||
else
|
||||
# DFU mode -- limited info, but checkm8 path is implied
|
||||
# DFU mode -- query CPID from serial descriptor to determine path
|
||||
msg_info "Device in DFU mode. Querying chip info..."
|
||||
parse_device_info
|
||||
|
||||
local dfu_bypass="auto-detect"
|
||||
if [ "$DEV_CHECKM8" = "YES" ]; then
|
||||
dfu_bypass="Path A (checkm8, A5-A11)"
|
||||
elif [ -n "$DEV_CPID" ] && [ "$DEV_CPID" != "0x0000" ]; then
|
||||
dfu_bypass="Path B (identity, A12+)"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "========================================"
|
||||
echo " Device Information"
|
||||
echo "========================================"
|
||||
printf " %-12s %s\n" "Mode:" "DFU"
|
||||
printf " %-12s %s\n" "Bypass:" "Path A (checkm8)"
|
||||
printf " %-12s %s\n" "Chip:" "${DEV_CHIP_NAME:-(unknown)} (CPID: ${DEV_CPID:-(unknown)})"
|
||||
printf " %-12s %s\n" "Bypass:" "$dfu_bypass"
|
||||
printf " %-12s %s\n" "Status:" "SUPPORTED (DFU detected)"
|
||||
echo "========================================"
|
||||
echo ""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue