Module Name:    src
Committed By:   mrg
Date:           Tue Jan 10 18:20:10 UTC 2023

Modified Files:
        src/sys/dev/usb: ukbd.c

Log Message:
ukbd(4): remember how much of attach worked so detach works.

if ukbd_attach() fails it means it has only really done the pmf
setup, nothing else, so avoid trying to remove callouts, child
devices, or console setup, usb task and uhidev operations.

fixes the crash (but not the underlying ukbd doesn't work issue)
seen in PR#57149.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/dev/usb/ukbd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/usb/ukbd.c
diff -u src/sys/dev/usb/ukbd.c:1.161 src/sys/dev/usb/ukbd.c:1.162
--- src/sys/dev/usb/ukbd.c:1.161	Wed Apr  6 21:51:29 2022
+++ src/sys/dev/usb/ukbd.c	Tue Jan 10 18:20:10 2023
@@ -1,4 +1,4 @@
-/*      $NetBSD: ukbd.c,v 1.161 2022/04/06 21:51:29 mlelstv Exp $        */
+/*      $NetBSD: ukbd.c,v 1.162 2023/01/10 18:20:10 mrg Exp $        */
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.161 2022/04/06 21:51:29 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ukbd.c,v 1.162 2023/01/10 18:20:10 mrg Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -294,7 +294,8 @@ struct ukbd_softc {
 	int sc_npollchar;
 	uint16_t sc_pollchars[MAXKEYS];
 
-	u_char sc_dying;
+	bool sc_dying;
+	bool sc_attached;
 };
 
 #ifdef UKBD_DEBUG
@@ -520,6 +521,8 @@ ukbd_attach(device_t parent, device_t se
 
 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
 
+	sc->sc_attached = true;
+
 	return;
 }
 
@@ -567,7 +570,7 @@ ukbd_activate(device_t self, enum devact
 
 	switch (act) {
 	case DVACT_DEACTIVATE:
-		sc->sc_dying = 1;
+		sc->sc_dying = true;
 		return 0;
 	default:
 		return EOPNOTSUPP;
@@ -584,6 +587,9 @@ ukbd_detach(device_t self, int flags)
 
 	pmf_device_deregister(self);
 
+	if (!sc->sc_attached)
+		return rv;
+
 	if (sc->sc_console_keyboard) {
 		/*
 		 * Disconnect our consops and set ukbd_is_console

Reply via email to