git: e9e4115a0bb4 - main - cp2112: Check status errors and bound I2C waits

From: Adrian Chadd <adrian_at_FreeBSD.org>
Date: Wed, 04 Nov 2026 19:12:20 UTC
The branch main has been updated by adrian:

URL: https://cgit.FreeBSD.org/src/commit/?id=e9e4115a0bb41416bf5e1277627fc6c565073972

commit e9e4115a0bb41416bf5e1277627fc6c565073972
Author:     Patrick Tudor <ptudor@ptudor.net>
AuthorDate: 2026-11-04 18:41:37 +0000
Commit:     Adrian Chadd <adrian@FreeBSD.org>
CommitDate: 2026-11-04 19:12:09 +0000

    cp2112: Check status errors and bound I2C waits
    
    A USB error during an I2C transfer can leave `cp2112(4)` blocked indefinitely,
    preventing USB detach and even reboot from completing.
    
    `cp2112iic_check_req_status()` uses the status response without checking whether
    the exchange succeeded. A failed exchange can therefore reuse a stale busy
    response and retry indefinitely. The output callback also records an error
    without marking the transfer done, causing its waiter to sleep again.
    
    Check the exchange result before using the response, and mark failed output
    transfers complete. Use absolute deadlines based on `USB_DEFAULT_TIMEOUT`
    (five seconds) for output and input condition-variable waits, and stop
    repeated busy status responses after the status loop's own deadline.
    Clear pending input state when the paired output request fails.
    These are per-wait and status-loop bounds; they do not impose a
    single five-second limit on the entire I2C operation.
    
    Add `HQ_HID_IGNORE` entries for both VID/PID pairs matched by
    `cp2112`, so `usbhid` does not claim the interface ahead of the
    bridge driver. Define `LUMANATE` in `usbdevs` and use
    `USB_VENDOR_LUMANATE` for the existing `0x1009` entry in both device tables.
    
    PR: 300017
    
    Related hardware testing: https://reviews.freebsd.org/D60989,
    the RNG90 I2C slave driver. This controller fix applies independently of
    that driver.
    
    Reviewed by:    wulf, adrian
    Differential Revision:  https://reviews.freebsd.org/D61065
    Event: NetApp/Vendor Summit Hackathon 202611
---
 sys/dev/hid/hidquirk.c    |  3 +++
 sys/dev/usb/misc/cp2112.c | 42 ++++++++++++++++++++++++++++++++++++------
 sys/dev/usb/usbdevs       |  1 +
 3 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/sys/dev/hid/hidquirk.c b/sys/dev/hid/hidquirk.c
index 3e746cab22ca..6ceae0416857 100644
--- a/sys/dev/hid/hidquirk.c
+++ b/sys/dev/hid/hidquirk.c
@@ -114,6 +114,9 @@ static struct hidquirk_entry hidquirks[HID_DEV_QUIRKS_MAX] = {
 	USB_QUIRK(POWERCOM, WOW, 0x0000, 0xffff, HQ_HID_IGNORE),
 	USB_QUIRK(POWERCOM, VANGUARD, 0x0000, 0xffff, HQ_HID_IGNORE),
 	USB_QUIRK(POWERCOM, BLACK_KNIGHT_PRO, 0x0000, 0xffff, HQ_HID_IGNORE),
+	USB_QUIRK(SILABS, CP2112, 0x0000, 0xffff, HQ_HID_IGNORE),
+	HID_QUIRK_VP(BUS_USB, USB_VENDOR_LUMANATE, USB_PRODUCT_SILABS_CP2112,
+	    0x0000, 0xffff, HQ_HID_IGNORE),
 	USB_QUIRK(TRIPPLITE2, AVR550U, 0x0000, 0xffff, HQ_HID_IGNORE),
 	USB_QUIRK(TRIPPLITE2, AVR750U, 0x0000, 0xffff, HQ_HID_IGNORE),
 	USB_QUIRK(TRIPPLITE2, ECO550UPS, 0x0000, 0xffff, HQ_HID_IGNORE),
diff --git a/sys/dev/usb/misc/cp2112.c b/sys/dev/usb/misc/cp2112.c
index 201a3ec51ce4..73da3eef4db0 100644
--- a/sys/dev/usb/misc/cp2112.c
+++ b/sys/dev/usb/misc/cp2112.c
@@ -72,6 +72,7 @@
 #define	CP2112_PART_NUM			0x0c
 #define	CP2112_GPIO_COUNT		8
 #define	CP2112_REPORT_SIZE		64
+#define	CP2112_IIC_TIMEOUT		(USB_DEFAULT_TIMEOUT * SBT_1MS)
 
 #define	CP2112_REQ_RESET		0x1
 #define	CP2112_REQ_GPIO_CFG		0x2
@@ -257,7 +258,7 @@ static int cp2112iic_detach(device_t dev);
 
 static const STRUCT_USB_HOST_ID cp2112_devs[] = {
 	{ USB_VP(USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP2112) },
-	{ USB_VP(0x1009, USB_PRODUCT_SILABS_CP2112) }, /* XXX */
+	{ USB_VP(USB_VENDOR_LUMANATE, USB_PRODUCT_SILABS_CP2112) },
 };
 
 static int
@@ -760,6 +761,7 @@ cp2112iic_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
 		device_printf(sc->dev, "write intr state %d error %d\n",
 		    USB_GET_STATE(xfer), error);
 		sc->io.out.error = IIC_EBUSERR;
+		sc->io.out.done = 1;
 		cv_signal(&sc->io.cv);
 		if (error != USB_ERR_CANCELLED) {
 			/* try to clear stall first */
@@ -871,6 +873,8 @@ static int
 cp2112iic_send_req(struct cp2112iic_softc *sc, const void *data,
     uint16_t len)
 {
+	sbintime_t deadline;
+	bool timedout = false;
 	int err;
 
 	mtx_assert(&sc->io.lock, MA_OWNED);
@@ -881,17 +885,23 @@ cp2112iic_send_req(struct cp2112iic_softc *sc, const void *data,
 
 	DTRACE_PROBE1(send__req, uint8_t, *(const uint8_t *)data);
 
+	deadline = sbinuptime() + CP2112_IIC_TIMEOUT;
 	usbd_transfer_start(sc->xfers[CP2112_INTR_OUT]);
 
-	while (!sc->io.out.done)
-		cv_wait(&sc->io.cv, &sc->io.lock);
+	while (!sc->io.out.done) {
+		if (cv_timedwait_sbt(&sc->io.cv, &sc->io.lock, deadline, 0,
+		    C_ABSOLUTE) == EWOULDBLOCK && !sc->io.out.done) {
+			timedout = true;
+			break;
+		}
+	}
 
 	usbd_transfer_stop(sc->xfers[CP2112_INTR_OUT]);
 
 	sc->io.out.done = 0;
 	sc->io.out.data = NULL;
 	sc->io.out.len = 0;
-	err = sc->io.out.error;
+	err = timedout ? IIC_ETIMEOUT : sc->io.out.error;
 	if (err != 0) {
 		device_printf(sc->dev, "output report 0x%02x failed: %d\n",
 		    *(const uint8_t*)data, err);
@@ -903,6 +913,7 @@ static int
 cp2112iic_req_resp(struct cp2112iic_softc *sc, const void *req_data,
     uint16_t req_len, void *resp_data, uint16_t resp_len)
 {
+	sbintime_t deadline;
 	int err;
 
 	mtx_assert(&sc->io.lock, MA_OWNED);
@@ -917,13 +928,26 @@ cp2112iic_req_resp(struct cp2112iic_softc *sc, const void *req_data,
 
 	err = cp2112iic_send_req(sc, req_data, req_len);
 	if (err != 0) {
+		sc->io.in.done = 0;
+		sc->io.in.error = 0;
 		sc->io.in.len = 0;
 		sc->io.in.data = NULL;
 		return (err);
 	}
 
-	while (!sc->io.in.done)
-		cv_wait(&sc->io.cv, &sc->io.lock);
+	deadline = sbinuptime() + CP2112_IIC_TIMEOUT;
+	while (!sc->io.in.done) {
+		if (cv_timedwait_sbt(&sc->io.cv, &sc->io.lock, deadline, 0,
+		    C_ABSOLUTE) == EWOULDBLOCK && !sc->io.in.done) {
+			/*
+			 * A response arriving after this is unsolicited and
+			 * ignored by the interrupt callback, since in.len is
+			 * cleared below.
+			 */
+			sc->io.in.error = IIC_ETIMEOUT;
+			break;
+		}
+	}
 
 	err = sc->io.in.error;
 	sc->io.in.done = 0;
@@ -938,9 +962,11 @@ cp2112iic_check_req_status(struct cp2112iic_softc *sc)
 {
 	struct i2c_xfer_status_req xfer_status_req;
 	struct i2c_xfer_status_resp xfer_status_resp;
+	sbintime_t deadline;
 	int err;
 
 	mtx_assert(&sc->io.lock, MA_OWNED);
+	deadline = sbinuptime() + CP2112_IIC_TIMEOUT;
 
 	do {
 		xfer_status_req.id = CP2112_REQ_SMB_XFER_STATUS_REQ;
@@ -948,6 +974,8 @@ cp2112iic_check_req_status(struct cp2112iic_softc *sc)
 		err = cp2112iic_req_resp(sc,
 		    &xfer_status_req, sizeof(xfer_status_req),
 		    &xfer_status_resp, sizeof(xfer_status_resp));
+		if (err != 0)
+			goto out;
 
 		if (xfer_status_resp.id != CP2112_REQ_SMB_XFER_STATUS_RESP) {
 			device_printf(sc->dev,
@@ -998,6 +1026,8 @@ cp2112iic_check_req_status(struct cp2112iic_softc *sc)
 			err = IIC_EBUSERR;
 			break;
 		}
+		if (err == ERESTART && sbinuptime() >= deadline)
+			err = IIC_ETIMEOUT;
 
 	} while (err == ERESTART);
 out:
diff --git a/sys/dev/usb/usbdevs b/sys/dev/usb/usbdevs
index 0f190a899571..d1494eacace6 100644
--- a/sys/dev/usb/usbdevs
+++ b/sys/dev/usb/usbdevs
@@ -589,6 +589,7 @@ vendor KONTRON		0x0fe6	Kontron AG
 vendor DVICO		0x0fe9	DViCO
 vendor QUALCOMM		0x1004	Qualcomm
 vendor APACER		0x1005	Apacer
+vendor LUMANATE		0x1009	Lumanate
 vendor MOTOROLA4	0x100d	Motorola
 vendor HP3		0x103c	Hewlett Packard
 vendor AIRPLUS		0x1011  Airplus