This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit a023f61a3811452e95156f7b68bd1a81910db93a
Author: Justin Hammond <[email protected]>
AuthorDate: Wed Aug 5 19:09:26 2026 +0800

    drivers/usbhost: Do not unregister a HID keyboard that never registered.
    
    usbhost_destroy() unregisters the keyboard unconditionally, and it runs
    for a device that never got as far as being registered as well: an
    enumeration that failed part way through, or a device unplugged while it
    was still being set up.
    
    The upper half does not tolerate that call.  It asserts that the lower
    half carries the state keyboard_register() puts there, so a keyboard that
    fails to come up takes the system down with an assertion rather than
    being cleaned up and forgotten.  Seen on a low speed keyboard that
    attaches and then does not finish enumerating.
    
    The state the registration leaves behind is what says whether there is
    anything to undo, so look at it first.
    
    Assisted-by: Claude:claude-opus-5
    Signed-off-by: Justin Hammond <[email protected]>
---
 drivers/usbhost/usbhost_hidkbd.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/usbhost/usbhost_hidkbd.c b/drivers/usbhost/usbhost_hidkbd.c
index 67c8778e64d..5b9837e4345 100644
--- a/drivers/usbhost/usbhost_hidkbd.c
+++ b/drivers/usbhost/usbhost_hidkbd.c
@@ -764,11 +764,20 @@ static void usbhost_destroy(FAR void *arg)
 
   uinfo("crefs: %d\n", priv->crefs);
 
-  /* Unregister the driver */
+  /* Unregister the driver, but only if it was ever registered.
+   *
+   * This runs for a device that never got that far as well: an enumeration
+   * that failed part way through, or a device unplugged while it was still
+   * being set up.  The upper half has nothing to undo in that case, and
+   * asserts rather than tolerating the call.
+   */
 
-  uinfo("Unregister driver\n");
-  usbhost_mkdevname(priv, devname);
-  keyboard_unregister(&priv->lower, devname);
+  if (priv->lower.priv != NULL)
+    {
+      uinfo("Unregister driver\n");
+      usbhost_mkdevname(priv, devname);
+      keyboard_unregister(&priv->lower, devname);
+    }
 
   /* Release the device name used by this connection */
 

Reply via email to