Module Name:    src
Committed By:   snj
Date:           Sat Feb  6 20:58:13 UTC 2016

Modified Files:
        src/sys/dev/usb [netbsd-7]: usb.c usb_subr.c usbdivar.h xhci.c

Log Message:
Pull up following revision(s) (requested by skrll in ticket #1097):
        sys/dev/usb/usb.c: revision 1.161
        sys/dev/usb/usb_subr.c: revisions 1.207, 1.208
        sys/dev/usb/usbdivar.h: revision 1.111
        sys/dev/usb/xhci.c: revision 1.33
Get the iManufacturer, iProduct, and iSerialNumber strings before probing
for drivers and cache them for later use.  This reduces bus transactions
and fixes attachment for at least two of my umass(4)s.
--
Need sys/kmem.h


To generate a diff of this commit:
cvs rdiff -u -r1.154 -r1.154.2.1 src/sys/dev/usb/usb.c
cvs rdiff -u -r1.196.4.1 -r1.196.4.2 src/sys/dev/usb/usb_subr.c
cvs rdiff -u -r1.107 -r1.107.4.1 src/sys/dev/usb/usbdivar.h
cvs rdiff -u -r1.23.2.3 -r1.23.2.4 src/sys/dev/usb/xhci.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/usb.c
diff -u src/sys/dev/usb/usb.c:1.154 src/sys/dev/usb/usb.c:1.154.2.1
--- src/sys/dev/usb/usb.c:1.154	Fri Jul 25 08:10:39 2014
+++ src/sys/dev/usb/usb.c	Sat Feb  6 20:58:13 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb.c,v 1.154 2014/07/25 08:10:39 dholland Exp $	*/
+/*	$NetBSD: usb.c,v 1.154.2.1 2016/02/06 20:58:13 snj Exp $	*/
 
 /*
  * Copyright (c) 1998, 2002, 2008, 2012 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.154 2014/07/25 08:10:39 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.154.2.1 2016/02/06 20:58:13 snj Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -899,7 +899,7 @@ usbd_add_dev_event(int type, usbd_device
 {
 	struct usb_event *ue = usb_alloc_event();
 
-	usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type));
+	usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
 	usb_add_event(type, ue);
 }
 

Index: src/sys/dev/usb/usb_subr.c
diff -u src/sys/dev/usb/usb_subr.c:1.196.4.1 src/sys/dev/usb/usb_subr.c:1.196.4.2
--- src/sys/dev/usb/usb_subr.c:1.196.4.1	Mon Nov 16 14:45:03 2015
+++ src/sys/dev/usb/usb_subr.c	Sat Feb  6 20:58:13 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb_subr.c,v 1.196.4.1 2015/11/16 14:45:03 msaitoh Exp $	*/
+/*	$NetBSD: usb_subr.c,v 1.196.4.2 2016/02/06 20:58:13 snj Exp $	*/
 /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.196.4.1 2015/11/16 14:45:03 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.196.4.2 2016/02/06 20:58:13 snj Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -42,6 +42,7 @@ __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/kernel.h>
+#include <sys/kmem.h>
 #include <sys/malloc.h>
 #include <sys/device.h>
 #include <sys/select.h>
@@ -206,6 +207,33 @@ usbd_trim_spaces(char *p)
 	*e = '\0';			/* kill trailing spaces */
 }
 
+static void
+usbd_get_device_string(struct usbd_device *ud, uByte index, char **buf)
+{
+	char *b = kmem_alloc(USB_MAX_ENCODED_STRING_LEN, KM_SLEEP);
+	if (b) {
+		usbd_status err = usbd_get_string0(ud, index, b, true);
+		if (err != USBD_NORMAL_COMPLETION) {
+			kmem_free(b, USB_MAX_ENCODED_STRING_LEN);
+			b = NULL;
+		} else {
+			usbd_trim_spaces(b);
+		}
+	}
+	*buf = b;
+}
+
+void
+usbd_get_device_strings(struct usbd_device *ud)
+{
+	usb_device_descriptor_t *udd = &ud->ddesc;
+
+	usbd_get_device_string(ud, udd->iManufacturer, &ud->ud_vendor);
+	usbd_get_device_string(ud, udd->iProduct, &ud->ud_product);
+	usbd_get_device_string(ud, udd->iSerialNumber, &ud->ud_serial);
+}
+
+
 Static void
 usbd_devinfo_vp(usbd_device_handle dev, char *v, size_t vl, char *p,
     size_t pl, int usedev, int useencoded)
@@ -223,6 +251,13 @@ usbd_devinfo_vp(usbd_device_handle dev, 
 		if (usbd_get_string0(dev, udd->iProduct, p, useencoded) ==
 		    USBD_NORMAL_COMPLETION)
 			usbd_trim_spaces(p);
+	} else {
+		if (dev->ud_vendor) {
+			strlcpy(v, dev->ud_vendor, vl);
+		}
+		if (dev->ud_product) {
+			strlcpy(p, dev->ud_product, pl);
+		}
 	}
 	if (v[0] == '\0')
 		get_usb_vendor(v, vl, UGETW(udd->idVendor));
@@ -260,7 +295,7 @@ usbd_devinfo(usbd_device_handle dev, int
 	ep = cp + l;
 
 	usbd_devinfo_vp(dev, vendor, USB_MAX_ENCODED_STRING_LEN,
-	    product, USB_MAX_ENCODED_STRING_LEN, 1, 1);
+	    product, USB_MAX_ENCODED_STRING_LEN, 0, 1);
 	cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
 	if (showclass)
 		cp += snprintf(cp, ep - cp, ", class %d/%d",
@@ -831,19 +866,10 @@ usbd_attach_roothub(device_t parent, usb
 static void
 usbd_serialnumber(device_t dv, usbd_device_handle dev)
 {
-	usb_device_descriptor_t *dd = &dev->ddesc;
-	char *serialnumber;
-
-	serialnumber = malloc(USB_MAX_ENCODED_STRING_LEN, M_USB, M_NOWAIT);
-	if (serialnumber == NULL)
-		return;
-	serialnumber[0] = '\0';
-	(void)usbd_get_string(dev, dd->iSerialNumber, serialnumber);
-	if (serialnumber[0]) {
+	if (dev->ud_serial) {
 		prop_dictionary_set_cstring(device_properties(dv),
-		    "serialnumber", serialnumber);
+		    "serialnumber", dev->ud_serial);
 	}
-	free(serialnumber, M_USB);
 }
 
 static usbd_status
@@ -1309,6 +1335,8 @@ usbd_new_device(device_t parent, usbd_bu
 	DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
 		 addr, dev, parent));
 
+	usbd_get_device_strings(dev);
+
 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
 
 	if (port == 0) { /* root hub */
@@ -1430,10 +1458,21 @@ usbd_fill_deviceinfo(usbd_device_handle 
 	    di->udi_product, sizeof(di->udi_product), usedev, 1);
 	usbd_printBCD(di->udi_release, sizeof(di->udi_release),
 	    UGETW(dev->ddesc.bcdDevice));
-	di->udi_serial[0] = 0;
-	if (usedev)
-		(void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
-				      di->udi_serial);
+	if (usedev) {
+		usbd_status uerr = usbd_get_string(dev,
+		    dev->ddesc.iSerialNumber, di->udi_serial);
+		if (uerr != USBD_NORMAL_COMPLETION) {
+			di->udi_serial[0] = '\0';
+		} else {
+			usbd_trim_spaces(di->udi_serial);
+		}
+	} else {
+		di->udi_serial[0] = '\0';
+		if (dev->ud_serial) {
+			strlcpy(di->udi_serial, dev->ud_serial,
+			    sizeof(di->udi_serial));
+		}
+	}
 	di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
 	di->udi_productNo = UGETW(dev->ddesc.idProduct);
 	di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
@@ -1574,6 +1613,15 @@ usb_free_device(usbd_device_handle dev)
 		free(dev->subdevs, M_USB);
 		dev->subdevlen = 0;
 	}
+	if (dev->ud_vendor) {
+		kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN);
+	}
+	if (dev->ud_product) {
+		kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN);
+	}
+	if (dev->ud_serial) {
+		kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN);
+	}
 	free(dev, M_USB);
 }
 

Index: src/sys/dev/usb/usbdivar.h
diff -u src/sys/dev/usb/usbdivar.h:1.107 src/sys/dev/usb/usbdivar.h:1.107.4.1
--- src/sys/dev/usb/usbdivar.h:1.107	Thu Oct  3 19:04:00 2013
+++ src/sys/dev/usb/usbdivar.h	Sat Feb  6 20:58:13 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdivar.h,v 1.107 2013/10/03 19:04:00 skrll Exp $	*/
+/*	$NetBSD: usbdivar.h,v 1.107.4.1 2016/02/06 20:58:13 snj Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
@@ -196,6 +196,10 @@ struct usbd_device {
 	device_t	       *subdevs;       /* sub-devices */
 	int			nifaces_claimed; /* number of ifaces in use */
 	void		       *hci_private;
+
+	char		       *ud_serial;	/* serial number, can be NULL */
+	char		       *ud_vendor;	/* vendor string, can be NULL */
+	char		       *ud_product;	/* product string can be NULL */
 };
 
 struct usbd_interface {
@@ -285,6 +289,7 @@ void usbd_dump_pipe(usbd_pipe_handle pip
 
 /* Routines from usb_subr.c */
 int		usbctlprint(void *, const char *);
+void		usbd_get_device_strings(struct usbd_device *);
 void		usb_delay_ms_locked(usbd_bus_handle, u_int, kmutex_t *);
 void		usb_delay_ms(usbd_bus_handle, u_int);
 void		usbd_delay_ms_locked(usbd_device_handle, u_int, kmutex_t *);

Index: src/sys/dev/usb/xhci.c
diff -u src/sys/dev/usb/xhci.c:1.23.2.3 src/sys/dev/usb/xhci.c:1.23.2.4
--- src/sys/dev/usb/xhci.c:1.23.2.3	Fri Jan  2 22:44:34 2015
+++ src/sys/dev/usb/xhci.c	Sat Feb  6 20:58:13 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: xhci.c,v 1.23.2.3 2015/01/02 22:44:34 martin Exp $	*/
+/*	$NetBSD: xhci.c,v 1.23.2.4 2016/02/06 20:58:13 snj Exp $	*/
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.23.2.3 2015/01/02 22:44:34 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.23.2.4 2016/02/06 20:58:13 snj Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1562,6 +1562,8 @@ xhci_new_device(device_t parent, usbd_bu
 		 dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
 		 dev->speed));
 
+	usbd_get_device_strings(dev);
+
 	usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
 
 	if ((depth == 0) && (port == 0)) {

Reply via email to