Sun Nov 8 23:25:17 2015 UTC ()
Attach serial number as property to all USB devices having one.


(joerg)
diff -r1.203 -r1.204 src/sys/dev/usb/usb_subr.c

cvs diff -r1.203 -r1.204 src/sys/dev/usb/usb_subr.c (switch to unified diff)

--- src/sys/dev/usb/usb_subr.c 2015/10/26 15:07:07 1.203
+++ src/sys/dev/usb/usb_subr.c 2015/11/08 23:25:17 1.204
@@ -1,1592 +1,1614 @@ @@ -1,1592 +1,1614 @@
1/* $NetBSD: usb_subr.c,v 1.203 2015/10/26 15:07:07 skrll Exp $ */ 1/* $NetBSD: usb_subr.c,v 1.204 2015/11/08 23:25:17 joerg Exp $ */
2/* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */ 2/* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc. 5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * This code is derived from software contributed to The NetBSD Foundation 8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at 9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology. 10 * Carlstedt Research & Technology.
11 * 11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 13 * modification, are permitted provided that the following conditions
14 * are met: 14 * are met:
15 * 1. Redistributions of source code must retain the above copyright 15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer. 16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright 17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the 18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution. 19 * documentation and/or other materials provided with the distribution.
20 * 20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE. 31 * POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.203 2015/10/26 15:07:07 skrll Exp $"); 35__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.204 2015/11/08 23:25:17 joerg Exp $");
36 36
37#ifdef _KERNEL_OPT 37#ifdef _KERNEL_OPT
38#include "opt_compat_netbsd.h" 38#include "opt_compat_netbsd.h"
39#include "opt_usb.h" 39#include "opt_usb.h"
40#include "opt_usbverbose.h" 40#include "opt_usbverbose.h"
41#endif 41#endif
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <sys/systm.h> 44#include <sys/systm.h>
45#include <sys/kernel.h> 45#include <sys/kernel.h>
46#include <sys/malloc.h> 46#include <sys/malloc.h>
47#include <sys/device.h> 47#include <sys/device.h>
48#include <sys/select.h> 48#include <sys/select.h>
49#include <sys/proc.h> 49#include <sys/proc.h>
50 50
51#include <sys/bus.h> 51#include <sys/bus.h>
52#include <sys/module.h> 52#include <sys/module.h>
53 53
54#include <dev/usb/usb.h> 54#include <dev/usb/usb.h>
55 55
56#include <dev/usb/usbdi.h> 56#include <dev/usb/usbdi.h>
57#include <dev/usb/usbdi_util.h> 57#include <dev/usb/usbdi_util.h>
58#include <dev/usb/usbdivar.h> 58#include <dev/usb/usbdivar.h>
59#include <dev/usb/usbdevs.h> 59#include <dev/usb/usbdevs.h>
60#include <dev/usb/usb_quirks.h> 60#include <dev/usb/usb_quirks.h>
61#include <dev/usb/usb_verbose.h> 61#include <dev/usb/usb_verbose.h>
62 62
63#include "locators.h" 63#include "locators.h"
64 64
65#ifdef USB_DEBUG 65#ifdef USB_DEBUG
66#define DPRINTF(x) if (usbdebug) printf x 66#define DPRINTF(x) if (usbdebug) printf x
67#define DPRINTFN(n,x) if (usbdebug>(n)) printf x 67#define DPRINTFN(n,x) if (usbdebug>(n)) printf x
68extern int usbdebug; 68extern int usbdebug;
69#else 69#else
70#define DPRINTF(x) 70#define DPRINTF(x)
71#define DPRINTFN(n,x) 71#define DPRINTFN(n,x)
72#endif 72#endif
73 73
74MALLOC_DEFINE(M_USB, "USB", "USB misc. memory"); 74MALLOC_DEFINE(M_USB, "USB", "USB misc. memory");
75MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver"); 75MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver");
76MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller"); 76MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller");
77 77
78Static usbd_status usbd_set_config(usbd_device_handle, int); 78Static usbd_status usbd_set_config(usbd_device_handle, int);
79Static void usbd_devinfo(usbd_device_handle, int, char *, size_t); 79Static void usbd_devinfo(usbd_device_handle, int, char *, size_t);
80Static void usbd_devinfo_vp(usbd_device_handle, char *, size_t, char *, size_t, 80Static void usbd_devinfo_vp(usbd_device_handle, char *, size_t, char *, size_t,
81 int, int); 81 int, int);
82Static int usbd_getnewaddr(usbd_bus_handle); 82Static int usbd_getnewaddr(usbd_bus_handle);
83Static int usbd_print(void *, const char *); 83Static int usbd_print(void *, const char *);
84Static int usbd_ifprint(void *, const char *); 84Static int usbd_ifprint(void *, const char *);
85Static void usbd_free_iface_data(usbd_device_handle, int); 85Static void usbd_free_iface_data(usbd_device_handle, int);
86 86
87uint32_t usb_cookie_no = 0; 87uint32_t usb_cookie_no = 0;
88 88
89Static const char * const usbd_error_strs[] = { 89Static const char * const usbd_error_strs[] = {
90 "NORMAL_COMPLETION", 90 "NORMAL_COMPLETION",
91 "IN_PROGRESS", 91 "IN_PROGRESS",
92 "PENDING_REQUESTS", 92 "PENDING_REQUESTS",
93 "NOT_STARTED", 93 "NOT_STARTED",
94 "INVAL", 94 "INVAL",
95 "NOMEM", 95 "NOMEM",
96 "CANCELLED", 96 "CANCELLED",
97 "BAD_ADDRESS", 97 "BAD_ADDRESS",
98 "IN_USE", 98 "IN_USE",
99 "NO_ADDR", 99 "NO_ADDR",
100 "SET_ADDR_FAILED", 100 "SET_ADDR_FAILED",
101 "NO_POWER", 101 "NO_POWER",
102 "TOO_DEEP", 102 "TOO_DEEP",
103 "IOERROR", 103 "IOERROR",
104 "NOT_CONFIGURED", 104 "NOT_CONFIGURED",
105 "TIMEOUT", 105 "TIMEOUT",
106 "SHORT_XFER", 106 "SHORT_XFER",
107 "STALLED", 107 "STALLED",
108 "INTERRUPTED", 108 "INTERRUPTED",
109 "XXX", 109 "XXX",
110}; 110};
111 111
112DEV_VERBOSE_DEFINE(usb); 112DEV_VERBOSE_DEFINE(usb);
113 113
114const char * 114const char *
115usbd_errstr(usbd_status err) 115usbd_errstr(usbd_status err)
116{ 116{
117 static char buffer[5]; 117 static char buffer[5];
118 118
119 if (err < USBD_ERROR_MAX) { 119 if (err < USBD_ERROR_MAX) {
120 return usbd_error_strs[err]; 120 return usbd_error_strs[err];
121 } else { 121 } else {
122 snprintf(buffer, sizeof buffer, "%d", err); 122 snprintf(buffer, sizeof buffer, "%d", err);
123 return buffer; 123 return buffer;
124 } 124 }
125} 125}
126 126
127usbd_status 127usbd_status
128usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid, 128usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
129 usb_string_descriptor_t *sdesc, int *sizep) 129 usb_string_descriptor_t *sdesc, int *sizep)
130{ 130{
131 usb_device_request_t req; 131 usb_device_request_t req;
132 usbd_status err; 132 usbd_status err;
133 int actlen; 133 int actlen;
134 134
135 req.bmRequestType = UT_READ_DEVICE; 135 req.bmRequestType = UT_READ_DEVICE;
136 req.bRequest = UR_GET_DESCRIPTOR; 136 req.bRequest = UR_GET_DESCRIPTOR;
137 USETW2(req.wValue, UDESC_STRING, sindex); 137 USETW2(req.wValue, UDESC_STRING, sindex);
138 USETW(req.wIndex, langid); 138 USETW(req.wIndex, langid);
139 USETW(req.wLength, 2); /* only size byte first */ 139 USETW(req.wLength, 2); /* only size byte first */
140 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, 140 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
141 &actlen, USBD_DEFAULT_TIMEOUT); 141 &actlen, USBD_DEFAULT_TIMEOUT);
142 if (err) 142 if (err)
143 return (err); 143 return (err);
144 144
145 if (actlen < 2) 145 if (actlen < 2)
146 return (USBD_SHORT_XFER); 146 return (USBD_SHORT_XFER);
147 147
148 USETW(req.wLength, sdesc->bLength); /* the whole string */ 148 USETW(req.wLength, sdesc->bLength); /* the whole string */
149 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, 149 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
150 &actlen, USBD_DEFAULT_TIMEOUT); 150 &actlen, USBD_DEFAULT_TIMEOUT);
151 if (err) 151 if (err)
152 return (err); 152 return (err);
153 153
154 if (actlen != sdesc->bLength) { 154 if (actlen != sdesc->bLength) {
155 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n", 155 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
156 sdesc->bLength, actlen)); 156 sdesc->bLength, actlen));
157 } 157 }
158 158
159 *sizep = actlen; 159 *sizep = actlen;
160 return (USBD_NORMAL_COMPLETION); 160 return (USBD_NORMAL_COMPLETION);
161} 161}
162 162
163static void 163static void
164usbd_trim_spaces(char *p) 164usbd_trim_spaces(char *p)
165{ 165{
166 char *q, *e; 166 char *q, *e;
167 167
168 q = e = p; 168 q = e = p;
169 while (*q == ' ') /* skip leading spaces */ 169 while (*q == ' ') /* skip leading spaces */
170 q++; 170 q++;
171 while ((*p = *q++)) /* copy string */ 171 while ((*p = *q++)) /* copy string */
172 if (*p++ != ' ') /* remember last non-space */ 172 if (*p++ != ' ') /* remember last non-space */
173 e = p; 173 e = p;
174 *e = '\0'; /* kill trailing spaces */ 174 *e = '\0'; /* kill trailing spaces */
175} 175}
176 176
177Static void 177Static void
178usbd_devinfo_vp(usbd_device_handle dev, char *v, size_t vl, char *p, 178usbd_devinfo_vp(usbd_device_handle dev, char *v, size_t vl, char *p,
179 size_t pl, int usedev, int useencoded) 179 size_t pl, int usedev, int useencoded)
180{ 180{
181 usb_device_descriptor_t *udd = &dev->ddesc; 181 usb_device_descriptor_t *udd = &dev->ddesc;
182 if (dev == NULL) 182 if (dev == NULL)
183 return; 183 return;
184 184
185 v[0] = p[0] = '\0'; 185 v[0] = p[0] = '\0';
186 186
187 if (usedev) { 187 if (usedev) {
188 if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) == 188 if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
189 USBD_NORMAL_COMPLETION) 189 USBD_NORMAL_COMPLETION)
190 usbd_trim_spaces(v); 190 usbd_trim_spaces(v);
191 if (usbd_get_string0(dev, udd->iProduct, p, useencoded) == 191 if (usbd_get_string0(dev, udd->iProduct, p, useencoded) ==
192 USBD_NORMAL_COMPLETION) 192 USBD_NORMAL_COMPLETION)
193 usbd_trim_spaces(p); 193 usbd_trim_spaces(p);
194 } 194 }
195 if (v[0] == '\0') 195 if (v[0] == '\0')
196 usb_findvendor(v, vl, UGETW(udd->idVendor)); 196 usb_findvendor(v, vl, UGETW(udd->idVendor));
197 if (p[0] == '\0') 197 if (p[0] == '\0')
198 usb_findproduct(p, pl, UGETW(udd->idVendor), 198 usb_findproduct(p, pl, UGETW(udd->idVendor),
199 UGETW(udd->idProduct)); 199 UGETW(udd->idProduct));
200} 200}
201 201
202int 202int
203usbd_printBCD(char *cp, size_t l, int bcd) 203usbd_printBCD(char *cp, size_t l, int bcd)
204{ 204{
205 return snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff); 205 return snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff);
206} 206}
207 207
208Static void 208Static void
209usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l) 209usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l)
210{ 210{
211 usb_device_descriptor_t *udd = &dev->ddesc; 211 usb_device_descriptor_t *udd = &dev->ddesc;
212 char *vendor, *product; 212 char *vendor, *product;
213 int bcdDevice, bcdUSB; 213 int bcdDevice, bcdUSB;
214 char *ep; 214 char *ep;
215 215
216 vendor = malloc(USB_MAX_ENCODED_STRING_LEN * 2, M_USB, M_NOWAIT); 216 vendor = malloc(USB_MAX_ENCODED_STRING_LEN * 2, M_USB, M_NOWAIT);
217 if (vendor == NULL) { 217 if (vendor == NULL) {
218 *cp = '\0'; 218 *cp = '\0';
219 return; 219 return;
220 } 220 }
221 product = &vendor[USB_MAX_ENCODED_STRING_LEN]; 221 product = &vendor[USB_MAX_ENCODED_STRING_LEN];
222 222
223 ep = cp + l; 223 ep = cp + l;
224 224
225 usbd_devinfo_vp(dev, vendor, USB_MAX_ENCODED_STRING_LEN, 225 usbd_devinfo_vp(dev, vendor, USB_MAX_ENCODED_STRING_LEN,
226 product, USB_MAX_ENCODED_STRING_LEN, 1, 1); 226 product, USB_MAX_ENCODED_STRING_LEN, 1, 1);
227 cp += snprintf(cp, ep - cp, "%s %s", vendor, product); 227 cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
228 if (showclass) 228 if (showclass)
229 cp += snprintf(cp, ep - cp, ", class %d/%d", 229 cp += snprintf(cp, ep - cp, ", class %d/%d",
230 udd->bDeviceClass, udd->bDeviceSubClass); 230 udd->bDeviceClass, udd->bDeviceSubClass);
231 bcdUSB = UGETW(udd->bcdUSB); 231 bcdUSB = UGETW(udd->bcdUSB);
232 bcdDevice = UGETW(udd->bcdDevice); 232 bcdDevice = UGETW(udd->bcdDevice);
233 cp += snprintf(cp, ep - cp, ", rev "); 233 cp += snprintf(cp, ep - cp, ", rev ");
234 cp += usbd_printBCD(cp, ep - cp, bcdUSB); 234 cp += usbd_printBCD(cp, ep - cp, bcdUSB);
235 *cp++ = '/'; 235 *cp++ = '/';
236 cp += usbd_printBCD(cp, ep - cp, bcdDevice); 236 cp += usbd_printBCD(cp, ep - cp, bcdDevice);
237 cp += snprintf(cp, ep - cp, ", addr %d", dev->address); 237 cp += snprintf(cp, ep - cp, ", addr %d", dev->address);
238 *cp = 0; 238 *cp = 0;
239 free(vendor, M_USB); 239 free(vendor, M_USB);
240} 240}
241 241
242char * 242char *
243usbd_devinfo_alloc(usbd_device_handle dev, int showclass) 243usbd_devinfo_alloc(usbd_device_handle dev, int showclass)
244{ 244{
245 char *devinfop; 245 char *devinfop;
246 246
247 devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK); 247 devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK);
248 usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE); 248 usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE);
249 return devinfop; 249 return devinfop;
250} 250}
251 251
252void 252void
253usbd_devinfo_free(char *devinfop) 253usbd_devinfo_free(char *devinfop)
254{ 254{
255 free(devinfop, M_TEMP); 255 free(devinfop, M_TEMP);
256} 256}
257 257
258/* Delay for a certain number of ms */ 258/* Delay for a certain number of ms */
259void 259void
260usb_delay_ms_locked(usbd_bus_handle bus, u_int ms, kmutex_t *lock) 260usb_delay_ms_locked(usbd_bus_handle bus, u_int ms, kmutex_t *lock)
261{ 261{
262 /* Wait at least two clock ticks so we know the time has passed. */ 262 /* Wait at least two clock ticks so we know the time has passed. */
263 if (bus->use_polling || cold) 263 if (bus->use_polling || cold)
264 delay((ms+1) * 1000); 264 delay((ms+1) * 1000);
265 else 265 else
266 kpause("usbdly", false, (ms*hz+999)/1000 + 1, lock); 266 kpause("usbdly", false, (ms*hz+999)/1000 + 1, lock);
267} 267}
268 268
269void 269void
270usb_delay_ms(usbd_bus_handle bus, u_int ms) 270usb_delay_ms(usbd_bus_handle bus, u_int ms)
271{ 271{
272 usb_delay_ms_locked(bus, ms, NULL); 272 usb_delay_ms_locked(bus, ms, NULL);
273} 273}
274 274
275/* Delay given a device handle. */ 275/* Delay given a device handle. */
276void 276void
277usbd_delay_ms_locked(usbd_device_handle dev, u_int ms, kmutex_t *lock) 277usbd_delay_ms_locked(usbd_device_handle dev, u_int ms, kmutex_t *lock)
278{ 278{
279 usb_delay_ms_locked(dev->bus, ms, lock); 279 usb_delay_ms_locked(dev->bus, ms, lock);
280} 280}
281 281
282/* Delay given a device handle. */ 282/* Delay given a device handle. */
283void 283void
284usbd_delay_ms(usbd_device_handle dev, u_int ms) 284usbd_delay_ms(usbd_device_handle dev, u_int ms)
285{ 285{
286 usb_delay_ms_locked(dev->bus, ms, NULL); 286 usb_delay_ms_locked(dev->bus, ms, NULL);
287} 287}
288 288
289usbd_status 289usbd_status
290usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps) 290usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
291{ 291{
292 usb_device_request_t req; 292 usb_device_request_t req;
293 usbd_status err; 293 usbd_status err;
294 int n; 294 int n;
295 295
296 req.bmRequestType = UT_WRITE_CLASS_OTHER; 296 req.bmRequestType = UT_WRITE_CLASS_OTHER;
297 req.bRequest = UR_SET_FEATURE; 297 req.bRequest = UR_SET_FEATURE;
298 USETW(req.wValue, UHF_PORT_RESET); 298 USETW(req.wValue, UHF_PORT_RESET);
299 USETW(req.wIndex, port); 299 USETW(req.wIndex, port);
300 USETW(req.wLength, 0); 300 USETW(req.wLength, 0);
301 err = usbd_do_request(dev, &req, 0); 301 err = usbd_do_request(dev, &req, 0);
302 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n", 302 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
303 port, usbd_errstr(err))); 303 port, usbd_errstr(err)));
304 if (err) 304 if (err)
305 return (err); 305 return (err);
306 n = 10; 306 n = 10;
307 do { 307 do {
308 /* Wait for device to recover from reset. */ 308 /* Wait for device to recover from reset. */
309 usbd_delay_ms(dev, USB_PORT_RESET_DELAY); 309 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
310 err = usbd_get_port_status(dev, port, ps); 310 err = usbd_get_port_status(dev, port, ps);
311 if (err) { 311 if (err) {
312 DPRINTF(("usbd_reset_port: get status failed %d\n", 312 DPRINTF(("usbd_reset_port: get status failed %d\n",
313 err)); 313 err));
314 return (err); 314 return (err);
315 } 315 }
316 /* If the device disappeared, just give up. */ 316 /* If the device disappeared, just give up. */
317 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) 317 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
318 return (USBD_NORMAL_COMPLETION); 318 return (USBD_NORMAL_COMPLETION);
319 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0); 319 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
320 if (n == 0) 320 if (n == 0)
321 return (USBD_TIMEOUT); 321 return (USBD_TIMEOUT);
322 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET); 322 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
323#ifdef USB_DEBUG 323#ifdef USB_DEBUG
324 if (err) 324 if (err)
325 DPRINTF(("usbd_reset_port: clear port feature failed %d\n", 325 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
326 err)); 326 err));
327#endif 327#endif
328 328
329 /* Wait for the device to recover from reset. */ 329 /* Wait for the device to recover from reset. */
330 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY); 330 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
331 return (err); 331 return (err);
332} 332}
333 333
334usb_interface_descriptor_t * 334usb_interface_descriptor_t *
335usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx) 335usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
336{ 336{
337 char *p = (char *)cd; 337 char *p = (char *)cd;
338 char *end = p + UGETW(cd->wTotalLength); 338 char *end = p + UGETW(cd->wTotalLength);
339 usb_interface_descriptor_t *d; 339 usb_interface_descriptor_t *d;
340 int curidx, lastidx, curaidx = 0; 340 int curidx, lastidx, curaidx = 0;
341 341
342 for (curidx = lastidx = -1; p < end; ) { 342 for (curidx = lastidx = -1; p < end; ) {
343 d = (usb_interface_descriptor_t *)p; 343 d = (usb_interface_descriptor_t *)p;
344 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d " 344 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
345 "type=%d\n", 345 "type=%d\n",
346 ifaceidx, curidx, altidx, curaidx, 346 ifaceidx, curidx, altidx, curaidx,
347 d->bLength, d->bDescriptorType)); 347 d->bLength, d->bDescriptorType));
348 if (d->bLength == 0) /* bad descriptor */ 348 if (d->bLength == 0) /* bad descriptor */
349 break; 349 break;
350 p += d->bLength; 350 p += d->bLength;
351 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) { 351 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
352 if (d->bInterfaceNumber != lastidx) { 352 if (d->bInterfaceNumber != lastidx) {
353 lastidx = d->bInterfaceNumber; 353 lastidx = d->bInterfaceNumber;
354 curidx++; 354 curidx++;
355 curaidx = 0; 355 curaidx = 0;
356 } else 356 } else
357 curaidx++; 357 curaidx++;
358 if (ifaceidx == curidx && altidx == curaidx) 358 if (ifaceidx == curidx && altidx == curaidx)
359 return (d); 359 return (d);
360 } 360 }
361 } 361 }
362 return (NULL); 362 return (NULL);
363} 363}
364 364
365usb_endpoint_descriptor_t * 365usb_endpoint_descriptor_t *
366usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx, 366usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
367 int endptidx) 367 int endptidx)
368{ 368{
369 char *p = (char *)cd; 369 char *p = (char *)cd;
370 char *end = p + UGETW(cd->wTotalLength); 370 char *end = p + UGETW(cd->wTotalLength);
371 usb_interface_descriptor_t *d; 371 usb_interface_descriptor_t *d;
372 usb_endpoint_descriptor_t *e; 372 usb_endpoint_descriptor_t *e;
373 int curidx; 373 int curidx;
374 374
375 d = usbd_find_idesc(cd, ifaceidx, altidx); 375 d = usbd_find_idesc(cd, ifaceidx, altidx);
376 if (d == NULL) 376 if (d == NULL)
377 return (NULL); 377 return (NULL);
378 if (endptidx >= d->bNumEndpoints) /* quick exit */ 378 if (endptidx >= d->bNumEndpoints) /* quick exit */
379 return (NULL); 379 return (NULL);
380 380
381 curidx = -1; 381 curidx = -1;
382 for (p = (char *)d + d->bLength; p < end; ) { 382 for (p = (char *)d + d->bLength; p < end; ) {
383 e = (usb_endpoint_descriptor_t *)p; 383 e = (usb_endpoint_descriptor_t *)p;
384 if (e->bLength == 0) /* bad descriptor */ 384 if (e->bLength == 0) /* bad descriptor */
385 break; 385 break;
386 p += e->bLength; 386 p += e->bLength;
387 if (p <= end && e->bDescriptorType == UDESC_INTERFACE) 387 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
388 return (NULL); 388 return (NULL);
389 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) { 389 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
390 curidx++; 390 curidx++;
391 if (curidx == endptidx) 391 if (curidx == endptidx)
392 return (e); 392 return (e);
393 } 393 }
394 } 394 }
395 return (NULL); 395 return (NULL);
396} 396}
397 397
398usbd_status 398usbd_status
399usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx) 399usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
400{ 400{
401 usbd_interface_handle ifc = &dev->ifaces[ifaceidx]; 401 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
402 usb_interface_descriptor_t *idesc; 402 usb_interface_descriptor_t *idesc;
403 char *p, *end; 403 char *p, *end;
404 int endpt, nendpt; 404 int endpt, nendpt;
405 405
406 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n", 406 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
407 ifaceidx, altidx)); 407 ifaceidx, altidx));
408 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx); 408 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
409 if (idesc == NULL) 409 if (idesc == NULL)
410 return (USBD_INVAL); 410 return (USBD_INVAL);
411 ifc->device = dev; 411 ifc->device = dev;
412 ifc->idesc = idesc; 412 ifc->idesc = idesc;
413 ifc->index = ifaceidx; 413 ifc->index = ifaceidx;
414 ifc->altindex = altidx; 414 ifc->altindex = altidx;
415 nendpt = ifc->idesc->bNumEndpoints; 415 nendpt = ifc->idesc->bNumEndpoints;
416 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt)); 416 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
417 if (nendpt != 0) { 417 if (nendpt != 0) {
418 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint), 418 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
419 M_USB, M_NOWAIT); 419 M_USB, M_NOWAIT);
420 if (ifc->endpoints == NULL) 420 if (ifc->endpoints == NULL)
421 return (USBD_NOMEM); 421 return (USBD_NOMEM);
422 } else 422 } else
423 ifc->endpoints = NULL; 423 ifc->endpoints = NULL;
424 ifc->priv = NULL; 424 ifc->priv = NULL;
425 p = (char *)ifc->idesc + ifc->idesc->bLength; 425 p = (char *)ifc->idesc + ifc->idesc->bLength;
426 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength); 426 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
427#define ed ((usb_endpoint_descriptor_t *)p) 427#define ed ((usb_endpoint_descriptor_t *)p)
428 for (endpt = 0; endpt < nendpt; endpt++) { 428 for (endpt = 0; endpt < nendpt; endpt++) {
429 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt)); 429 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
430 for (; p < end; p += ed->bLength) { 430 for (; p < end; p += ed->bLength) {
431 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p " 431 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
432 "len=%d type=%d\n", 432 "len=%d type=%d\n",
433 p, end, ed->bLength, ed->bDescriptorType)); 433 p, end, ed->bLength, ed->bDescriptorType));
434 if (p + ed->bLength <= end && ed->bLength != 0 && 434 if (p + ed->bLength <= end && ed->bLength != 0 &&
435 ed->bDescriptorType == UDESC_ENDPOINT) 435 ed->bDescriptorType == UDESC_ENDPOINT)
436 goto found; 436 goto found;
437 if (ed->bLength == 0 || 437 if (ed->bLength == 0 ||
438 ed->bDescriptorType == UDESC_INTERFACE) 438 ed->bDescriptorType == UDESC_INTERFACE)
439 break; 439 break;
440 } 440 }
441 /* passed end, or bad desc */ 441 /* passed end, or bad desc */
442 printf("usbd_fill_iface_data: bad descriptor(s): %s\n", 442 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
443 ed->bLength == 0 ? "0 length" : 443 ed->bLength == 0 ? "0 length" :
444 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc": 444 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
445 "out of data"); 445 "out of data");
446 goto bad; 446 goto bad;
447 found: 447 found:
448 ifc->endpoints[endpt].edesc = ed; 448 ifc->endpoints[endpt].edesc = ed;
449 if (dev->speed == USB_SPEED_HIGH) { 449 if (dev->speed == USB_SPEED_HIGH) {
450 u_int mps; 450 u_int mps;
451 /* Control and bulk endpoints have max packet limits. */ 451 /* Control and bulk endpoints have max packet limits. */
452 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 452 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
453 case UE_CONTROL: 453 case UE_CONTROL:
454 mps = USB_2_MAX_CTRL_PACKET; 454 mps = USB_2_MAX_CTRL_PACKET;
455 goto check; 455 goto check;
456 case UE_BULK: 456 case UE_BULK:
457 mps = USB_2_MAX_BULK_PACKET; 457 mps = USB_2_MAX_BULK_PACKET;
458 check: 458 check:
459 if (UGETW(ed->wMaxPacketSize) != mps) { 459 if (UGETW(ed->wMaxPacketSize) != mps) {
460 USETW(ed->wMaxPacketSize, mps); 460 USETW(ed->wMaxPacketSize, mps);
461#ifdef DIAGNOSTIC 461#ifdef DIAGNOSTIC
462 printf("usbd_fill_iface_data: bad max " 462 printf("usbd_fill_iface_data: bad max "
463 "packet size\n"); 463 "packet size\n");
464#endif 464#endif
465 } 465 }
466 break; 466 break;
467 default: 467 default:
468 break; 468 break;
469 } 469 }
470 } 470 }
471 ifc->endpoints[endpt].refcnt = 0; 471 ifc->endpoints[endpt].refcnt = 0;
472 ifc->endpoints[endpt].datatoggle = 0; 472 ifc->endpoints[endpt].datatoggle = 0;
473 p += ed->bLength; 473 p += ed->bLength;
474 } 474 }
475#undef ed 475#undef ed
476 LIST_INIT(&ifc->pipes); 476 LIST_INIT(&ifc->pipes);
477 return (USBD_NORMAL_COMPLETION); 477 return (USBD_NORMAL_COMPLETION);
478 478
479 bad: 479 bad:
480 if (ifc->endpoints != NULL) { 480 if (ifc->endpoints != NULL) {
481 free(ifc->endpoints, M_USB); 481 free(ifc->endpoints, M_USB);
482 ifc->endpoints = NULL; 482 ifc->endpoints = NULL;
483 } 483 }
484 return (USBD_INVAL); 484 return (USBD_INVAL);
485} 485}
486 486
487void 487void
488usbd_free_iface_data(usbd_device_handle dev, int ifcno) 488usbd_free_iface_data(usbd_device_handle dev, int ifcno)
489{ 489{
490 usbd_interface_handle ifc = &dev->ifaces[ifcno]; 490 usbd_interface_handle ifc = &dev->ifaces[ifcno];
491 if (ifc->endpoints) 491 if (ifc->endpoints)
492 free(ifc->endpoints, M_USB); 492 free(ifc->endpoints, M_USB);
493} 493}
494 494
495Static usbd_status 495Static usbd_status
496usbd_set_config(usbd_device_handle dev, int conf) 496usbd_set_config(usbd_device_handle dev, int conf)
497{ 497{
498 usb_device_request_t req; 498 usb_device_request_t req;
499 499
500 req.bmRequestType = UT_WRITE_DEVICE; 500 req.bmRequestType = UT_WRITE_DEVICE;
501 req.bRequest = UR_SET_CONFIG; 501 req.bRequest = UR_SET_CONFIG;
502 USETW(req.wValue, conf); 502 USETW(req.wValue, conf);
503 USETW(req.wIndex, 0); 503 USETW(req.wIndex, 0);
504 USETW(req.wLength, 0); 504 USETW(req.wLength, 0);
505 return (usbd_do_request(dev, &req, 0)); 505 return (usbd_do_request(dev, &req, 0));
506} 506}
507 507
508usbd_status 508usbd_status
509usbd_set_config_no(usbd_device_handle dev, int no, int msg) 509usbd_set_config_no(usbd_device_handle dev, int no, int msg)
510{ 510{
511 int index; 511 int index;
512 usb_config_descriptor_t cd; 512 usb_config_descriptor_t cd;
513 usbd_status err; 513 usbd_status err;
514 514
515 if (no == USB_UNCONFIG_NO) 515 if (no == USB_UNCONFIG_NO)
516 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg)); 516 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
517 517
518 DPRINTFN(5,("usbd_set_config_no: %d\n", no)); 518 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
519 /* Figure out what config index to use. */ 519 /* Figure out what config index to use. */
520 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) { 520 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
521 err = usbd_get_config_desc(dev, index, &cd); 521 err = usbd_get_config_desc(dev, index, &cd);
522 if (err) 522 if (err)
523 return (err); 523 return (err);
524 if (cd.bConfigurationValue == no) 524 if (cd.bConfigurationValue == no)
525 return (usbd_set_config_index(dev, index, msg)); 525 return (usbd_set_config_index(dev, index, msg));
526 } 526 }
527 return (USBD_INVAL); 527 return (USBD_INVAL);
528} 528}
529 529
530usbd_status 530usbd_status
531usbd_set_config_index(usbd_device_handle dev, int index, int msg) 531usbd_set_config_index(usbd_device_handle dev, int index, int msg)
532{ 532{
533 usb_config_descriptor_t cd, *cdp; 533 usb_config_descriptor_t cd, *cdp;
534 usbd_status err; 534 usbd_status err;
535 int i, ifcidx, nifc, len, selfpowered, power; 535 int i, ifcidx, nifc, len, selfpowered, power;
536 536
537 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index)); 537 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
538 538
539 if (index >= dev->ddesc.bNumConfigurations && 539 if (index >= dev->ddesc.bNumConfigurations &&
540 index != USB_UNCONFIG_INDEX) { 540 index != USB_UNCONFIG_INDEX) {
541 /* panic? */ 541 /* panic? */
542 printf("usbd_set_config_index: illegal index\n"); 542 printf("usbd_set_config_index: illegal index\n");
543 return (USBD_INVAL); 543 return (USBD_INVAL);
544 } 544 }
545 545
546 /* XXX check that all interfaces are idle */ 546 /* XXX check that all interfaces are idle */
547 if (dev->config != USB_UNCONFIG_NO) { 547 if (dev->config != USB_UNCONFIG_NO) {
548 DPRINTF(("usbd_set_config_index: free old config\n")); 548 DPRINTF(("usbd_set_config_index: free old config\n"));
549 /* Free all configuration data structures. */ 549 /* Free all configuration data structures. */
550 nifc = dev->cdesc->bNumInterface; 550 nifc = dev->cdesc->bNumInterface;
551 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 551 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
552 usbd_free_iface_data(dev, ifcidx); 552 usbd_free_iface_data(dev, ifcidx);
553 free(dev->ifaces, M_USB); 553 free(dev->ifaces, M_USB);
554 free(dev->cdesc, M_USB); 554 free(dev->cdesc, M_USB);
555 dev->ifaces = NULL; 555 dev->ifaces = NULL;
556 dev->cdesc = NULL; 556 dev->cdesc = NULL;
557 dev->config = USB_UNCONFIG_NO; 557 dev->config = USB_UNCONFIG_NO;
558 } 558 }
559 559
560 if (index == USB_UNCONFIG_INDEX) { 560 if (index == USB_UNCONFIG_INDEX) {
561 /* We are unconfiguring the device, so leave unallocated. */ 561 /* We are unconfiguring the device, so leave unallocated. */
562 DPRINTF(("usbd_set_config_index: set config 0\n")); 562 DPRINTF(("usbd_set_config_index: set config 0\n"));
563 err = usbd_set_config(dev, USB_UNCONFIG_NO); 563 err = usbd_set_config(dev, USB_UNCONFIG_NO);
564 if (err) { 564 if (err) {
565 DPRINTF(("usbd_set_config_index: setting config=0 " 565 DPRINTF(("usbd_set_config_index: setting config=0 "
566 "failed, error=%s\n", usbd_errstr(err))); 566 "failed, error=%s\n", usbd_errstr(err)));
567 } 567 }
568 return (err); 568 return (err);
569 } 569 }
570 570
571 /* Get the short descriptor. */ 571 /* Get the short descriptor. */
572 err = usbd_get_config_desc(dev, index, &cd); 572 err = usbd_get_config_desc(dev, index, &cd);
573 if (err) { 573 if (err) {
574 DPRINTF(("usbd_set_config_index: get_config_desc=%d\n", err)); 574 DPRINTF(("usbd_set_config_index: get_config_desc=%d\n", err));
575 return (err); 575 return (err);
576 } 576 }
577 len = UGETW(cd.wTotalLength); 577 len = UGETW(cd.wTotalLength);
578 cdp = malloc(len, M_USB, M_NOWAIT); 578 cdp = malloc(len, M_USB, M_NOWAIT);
579 if (cdp == NULL) 579 if (cdp == NULL)
580 return (USBD_NOMEM); 580 return (USBD_NOMEM);
581 581
582 /* Get the full descriptor. Try a few times for slow devices. */ 582 /* Get the full descriptor. Try a few times for slow devices. */
583 for (i = 0; i < 3; i++) { 583 for (i = 0; i < 3; i++) {
584 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp); 584 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
585 if (!err) 585 if (!err)
586 break; 586 break;
587 usbd_delay_ms(dev, 200); 587 usbd_delay_ms(dev, 200);
588 } 588 }
589 if (err) { 589 if (err) {
590 DPRINTF(("usbd_set_config_index: get_desc=%d\n", err)); 590 DPRINTF(("usbd_set_config_index: get_desc=%d\n", err));
591 goto bad; 591 goto bad;
592 } 592 }
593 if (cdp->bDescriptorType != UDESC_CONFIG) { 593 if (cdp->bDescriptorType != UDESC_CONFIG) {
594 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n", 594 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
595 cdp->bDescriptorType)); 595 cdp->bDescriptorType));
596 err = USBD_INVAL; 596 err = USBD_INVAL;
597 goto bad; 597 goto bad;
598 } 598 }
599 599
600 /* 600 /*
601 * Figure out if the device is self or bus powered. 601 * Figure out if the device is self or bus powered.
602 */ 602 */
603#if 0 /* XXX various devices don't report the power state correctly */ 603#if 0 /* XXX various devices don't report the power state correctly */
604 selfpowered = 0; 604 selfpowered = 0;
605 err = usbd_get_device_status(dev, &ds); 605 err = usbd_get_device_status(dev, &ds);
606 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED)) 606 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
607 selfpowered = 1; 607 selfpowered = 1;
608#endif 608#endif
609 /* 609 /*
610 * Use the power state in the configuration we are going 610 * Use the power state in the configuration we are going
611 * to set. This doesn't necessarily reflect the actual 611 * to set. This doesn't necessarily reflect the actual
612 * power state of the device; the driver can control this 612 * power state of the device; the driver can control this
613 * by choosing the appropriate configuration. 613 * by choosing the appropriate configuration.
614 */ 614 */
615 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED); 615 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
616 616
617 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, " 617 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
618 "selfpowered=%d, power=%d\n", 618 "selfpowered=%d, power=%d\n",
619 cdp->bConfigurationValue, dev->address, cdp->bmAttributes, 619 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
620 selfpowered, cdp->bMaxPower * 2)); 620 selfpowered, cdp->bMaxPower * 2));
621 621
622 /* Check if we have enough power. */ 622 /* Check if we have enough power. */
623#if 0 /* this is a no-op, see above */ 623#if 0 /* this is a no-op, see above */
624 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) { 624 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
625 if (msg) 625 if (msg)
626 printf("%s: device addr %d (config %d): " 626 printf("%s: device addr %d (config %d): "
627 "can't set self powered configuration\n", 627 "can't set self powered configuration\n",
628 device_xname(dev->bus->bdev), dev->address, 628 device_xname(dev->bus->bdev), dev->address,
629 cdp->bConfigurationValue); 629 cdp->bConfigurationValue);
630 err = USBD_NO_POWER; 630 err = USBD_NO_POWER;
631 goto bad; 631 goto bad;
632 } 632 }
633#endif 633#endif
634#ifdef USB_DEBUG 634#ifdef USB_DEBUG
635 if (dev->powersrc == NULL) { 635 if (dev->powersrc == NULL) {
636 DPRINTF(("usbd_set_config_index: No power source?\n")); 636 DPRINTF(("usbd_set_config_index: No power source?\n"));
637 err = USBD_IOERROR; 637 err = USBD_IOERROR;
638 goto bad; 638 goto bad;
639 } 639 }
640#endif 640#endif
641 power = cdp->bMaxPower * 2; 641 power = cdp->bMaxPower * 2;
642 if (power > dev->powersrc->power) { 642 if (power > dev->powersrc->power) {
643 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power)); 643 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
644 /* XXX print nicer message. */ 644 /* XXX print nicer message. */
645 if (msg) 645 if (msg)
646 printf("%s: device addr %d (config %d) exceeds power " 646 printf("%s: device addr %d (config %d) exceeds power "
647 "budget, %d mA > %d mA\n", 647 "budget, %d mA > %d mA\n",
648 device_xname(dev->bus->usbctl), dev->address, 648 device_xname(dev->bus->usbctl), dev->address,
649 cdp->bConfigurationValue, 649 cdp->bConfigurationValue,
650 power, dev->powersrc->power); 650 power, dev->powersrc->power);
651 err = USBD_NO_POWER; 651 err = USBD_NO_POWER;
652 goto bad; 652 goto bad;
653 } 653 }
654 dev->power = power; 654 dev->power = power;
655 dev->self_powered = selfpowered; 655 dev->self_powered = selfpowered;
656 656
657 /* Set the actual configuration value. */ 657 /* Set the actual configuration value. */
658 DPRINTF(("usbd_set_config_index: set config %d\n", 658 DPRINTF(("usbd_set_config_index: set config %d\n",
659 cdp->bConfigurationValue)); 659 cdp->bConfigurationValue));
660 err = usbd_set_config(dev, cdp->bConfigurationValue); 660 err = usbd_set_config(dev, cdp->bConfigurationValue);
661 if (err) { 661 if (err) {
662 DPRINTF(("usbd_set_config_index: setting config=%d failed, " 662 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
663 "error=%s\n", 663 "error=%s\n",
664 cdp->bConfigurationValue, usbd_errstr(err))); 664 cdp->bConfigurationValue, usbd_errstr(err)));
665 goto bad; 665 goto bad;
666 } 666 }
667 667
668 /* Allocate and fill interface data. */ 668 /* Allocate and fill interface data. */
669 nifc = cdp->bNumInterface; 669 nifc = cdp->bNumInterface;
670 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface), 670 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
671 M_USB, M_NOWAIT); 671 M_USB, M_NOWAIT);
672 if (dev->ifaces == NULL) { 672 if (dev->ifaces == NULL) {
673 err = USBD_NOMEM; 673 err = USBD_NOMEM;
674 goto bad; 674 goto bad;
675 } 675 }
676 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp)); 676 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
677 dev->cdesc = cdp; 677 dev->cdesc = cdp;
678 dev->config = cdp->bConfigurationValue; 678 dev->config = cdp->bConfigurationValue;
679 for (ifcidx = 0; ifcidx < nifc; ifcidx++) { 679 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
680 err = usbd_fill_iface_data(dev, ifcidx, 0); 680 err = usbd_fill_iface_data(dev, ifcidx, 0);
681 if (err) { 681 if (err) {
682 while (--ifcidx >= 0) 682 while (--ifcidx >= 0)
683 usbd_free_iface_data(dev, ifcidx); 683 usbd_free_iface_data(dev, ifcidx);
684 goto bad; 684 goto bad;
685 } 685 }
686 } 686 }
687 687
688 return (USBD_NORMAL_COMPLETION); 688 return (USBD_NORMAL_COMPLETION);
689 689
690 bad: 690 bad:
691 free(cdp, M_USB); 691 free(cdp, M_USB);
692 return (err); 692 return (err);
693} 693}
694 694
695/* XXX add function for alternate settings */ 695/* XXX add function for alternate settings */
696 696
697usbd_status 697usbd_status
698usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface, 698usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
699 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe) 699 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
700{ 700{
701 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0); 701 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0);
702} 702}
703 703
704usbd_status 704usbd_status
705usbd_setup_pipe_flags(usbd_device_handle dev, usbd_interface_handle iface, 705usbd_setup_pipe_flags(usbd_device_handle dev, usbd_interface_handle iface,
706 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe, uint8_t flags) 706 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe, uint8_t flags)
707{ 707{
708 usbd_pipe_handle p; 708 usbd_pipe_handle p;
709 usbd_status err; 709 usbd_status err;
710 710
711 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT); 711 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
712 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n", 712 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
713 dev, iface, ep, p)); 713 dev, iface, ep, p));
714 if (p == NULL) 714 if (p == NULL)
715 return (USBD_NOMEM); 715 return (USBD_NOMEM);
716 p->device = dev; 716 p->device = dev;
717 p->iface = iface; 717 p->iface = iface;
718 p->endpoint = ep; 718 p->endpoint = ep;
719 ep->refcnt++; 719 ep->refcnt++;
720 p->refcnt = 1; 720 p->refcnt = 1;
721 p->intrxfer = NULL; 721 p->intrxfer = NULL;
722 p->running = 0; 722 p->running = 0;
723 p->aborting = 0; 723 p->aborting = 0;
724 p->repeat = 0; 724 p->repeat = 0;
725 p->interval = ival; 725 p->interval = ival;
726 p->flags = flags; 726 p->flags = flags;
727 SIMPLEQ_INIT(&p->queue); 727 SIMPLEQ_INIT(&p->queue);
728 err = dev->bus->methods->open_pipe(p); 728 err = dev->bus->methods->open_pipe(p);
729 if (err) { 729 if (err) {
730 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error=" 730 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
731 "%s\n", 731 "%s\n",
732 ep->edesc->bEndpointAddress, usbd_errstr(err))); 732 ep->edesc->bEndpointAddress, usbd_errstr(err)));
733 free(p, M_USB); 733 free(p, M_USB);
734 return (err); 734 return (err);
735 } 735 }
736 usb_init_task(&p->async_task, usbd_clear_endpoint_stall_task, p, 736 usb_init_task(&p->async_task, usbd_clear_endpoint_stall_task, p,
737 USB_TASKQ_MPSAFE); 737 USB_TASKQ_MPSAFE);
738 *pipe = p; 738 *pipe = p;
739 return (USBD_NORMAL_COMPLETION); 739 return (USBD_NORMAL_COMPLETION);
740} 740}
741 741
742/* Abort the device control pipe. */ 742/* Abort the device control pipe. */
743void 743void
744usbd_kill_pipe(usbd_pipe_handle pipe) 744usbd_kill_pipe(usbd_pipe_handle pipe)
745{ 745{
746 usbd_abort_pipe(pipe); 746 usbd_abort_pipe(pipe);
747 usbd_lock_pipe(pipe); 747 usbd_lock_pipe(pipe);
748 pipe->methods->close(pipe); 748 pipe->methods->close(pipe);
749 usbd_unlock_pipe(pipe); 749 usbd_unlock_pipe(pipe);
750 usb_rem_task(pipe->device, &pipe->async_task); 750 usb_rem_task(pipe->device, &pipe->async_task);
751 pipe->endpoint->refcnt--; 751 pipe->endpoint->refcnt--;
752 free(pipe, M_USB); 752 free(pipe, M_USB);
753} 753}
754 754
755int 755int
756usbd_getnewaddr(usbd_bus_handle bus) 756usbd_getnewaddr(usbd_bus_handle bus)
757{ 757{
758 int addr; 758 int addr;
759 759
760 for (addr = 1; addr < USB_MAX_DEVICES; addr++) 760 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
761 if (bus->devices[addr] == NULL) 761 if (bus->devices[addr] == NULL)
762 return (addr); 762 return (addr);
763 return (-1); 763 return (-1);
764} 764}
765 765
766usbd_status 766usbd_status
767usbd_attach_roothub(device_t parent, usbd_device_handle dev) 767usbd_attach_roothub(device_t parent, usbd_device_handle dev)
768{ 768{
769 struct usb_attach_arg uaa; 769 struct usb_attach_arg uaa;
770 usb_device_descriptor_t *dd = &dev->ddesc; 770 usb_device_descriptor_t *dd = &dev->ddesc;
771 device_t dv; 771 device_t dv;
772 772
773 uaa.device = dev; 773 uaa.device = dev;
774 uaa.usegeneric = 0; 774 uaa.usegeneric = 0;
775 uaa.port = 0; 775 uaa.port = 0;
776 uaa.vendor = UGETW(dd->idVendor); 776 uaa.vendor = UGETW(dd->idVendor);
777 uaa.product = UGETW(dd->idProduct); 777 uaa.product = UGETW(dd->idProduct);
778 uaa.release = UGETW(dd->bcdDevice); 778 uaa.release = UGETW(dd->bcdDevice);
779 uaa.class = dd->bDeviceClass; 779 uaa.class = dd->bDeviceClass;
780 uaa.subclass = dd->bDeviceSubClass; 780 uaa.subclass = dd->bDeviceSubClass;
781 uaa.proto = dd->bDeviceProtocol; 781 uaa.proto = dd->bDeviceProtocol;
782 782
783 dv = config_found_ia(parent, "usbroothubif", &uaa, 0); 783 dv = config_found_ia(parent, "usbroothubif", &uaa, 0);
784 if (dv) { 784 if (dv) {
785 dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT); 785 dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
786 if (dev->subdevs == NULL) 786 if (dev->subdevs == NULL)
787 return (USBD_NOMEM); 787 return (USBD_NOMEM);
788 dev->subdevs[0] = dv; 788 dev->subdevs[0] = dv;
789 dev->subdevlen = 1; 789 dev->subdevlen = 1;
790 } 790 }
791 return (USBD_NORMAL_COMPLETION); 791 return (USBD_NORMAL_COMPLETION);
792} 792}
793 793
 794static void
 795usbd_serialnumber(device_t dv, usbd_device_handle dev)
 796{
 797 usb_device_descriptor_t *dd = &dev->ddesc;
 798 char *serialnumber;
 799
 800 serialnumber = malloc(USB_MAX_ENCODED_STRING_LEN, M_USB, M_NOWAIT);
 801 if (serialnumber == NULL)
 802 return;
 803 serialnumber[0] = '\0';
 804 (void)usbd_get_string(dev, dd->iSerialNumber, serialnumber);
 805 if (serialnumber[0]) {
 806 prop_dictionary_set_cstring(device_properties(dv),
 807 "serialnumber", serialnumber);
 808 }
 809 free(serialnumber, M_USB);
 810}
 811
794static usbd_status 812static usbd_status
795usbd_attachwholedevice(device_t parent, usbd_device_handle dev, int port, 813usbd_attachwholedevice(device_t parent, usbd_device_handle dev, int port,
796 int usegeneric) 814 int usegeneric)
797{ 815{
798 struct usb_attach_arg uaa; 816 struct usb_attach_arg uaa;
799 usb_device_descriptor_t *dd = &dev->ddesc; 817 usb_device_descriptor_t *dd = &dev->ddesc;
800 device_t dv; 818 device_t dv;
801 int dlocs[USBDEVIFCF_NLOCS]; 819 int dlocs[USBDEVIFCF_NLOCS];
802 820
803 uaa.device = dev; 821 uaa.device = dev;
804 uaa.usegeneric = usegeneric; 822 uaa.usegeneric = usegeneric;
805 uaa.port = port; 823 uaa.port = port;
806 uaa.vendor = UGETW(dd->idVendor); 824 uaa.vendor = UGETW(dd->idVendor);
807 uaa.product = UGETW(dd->idProduct); 825 uaa.product = UGETW(dd->idProduct);
808 uaa.release = UGETW(dd->bcdDevice); 826 uaa.release = UGETW(dd->bcdDevice);
809 uaa.class = dd->bDeviceClass; 827 uaa.class = dd->bDeviceClass;
810 uaa.subclass = dd->bDeviceSubClass; 828 uaa.subclass = dd->bDeviceSubClass;
811 uaa.proto = dd->bDeviceProtocol; 829 uaa.proto = dd->bDeviceProtocol;
812 830
813 dlocs[USBDEVIFCF_PORT] = uaa.port; 831 dlocs[USBDEVIFCF_PORT] = uaa.port;
814 dlocs[USBDEVIFCF_VENDOR] = uaa.vendor; 832 dlocs[USBDEVIFCF_VENDOR] = uaa.vendor;
815 dlocs[USBDEVIFCF_PRODUCT] = uaa.product; 833 dlocs[USBDEVIFCF_PRODUCT] = uaa.product;
816 dlocs[USBDEVIFCF_RELEASE] = uaa.release; 834 dlocs[USBDEVIFCF_RELEASE] = uaa.release;
817 /* the rest is historical ballast */ 835 /* the rest is historical ballast */
818 dlocs[USBDEVIFCF_CONFIGURATION] = -1; 836 dlocs[USBDEVIFCF_CONFIGURATION] = -1;
819 dlocs[USBDEVIFCF_INTERFACE] = -1; 837 dlocs[USBDEVIFCF_INTERFACE] = -1;
820 838
821 dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print, 839 dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
822 config_stdsubmatch); 840 config_stdsubmatch);
823 if (dv) { 841 if (dv) {
824 dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT); 842 dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
825 if (dev->subdevs == NULL) 843 if (dev->subdevs == NULL)
826 return (USBD_NOMEM); 844 return (USBD_NOMEM);
827 dev->subdevs[0] = dv; 845 dev->subdevs[0] = dv;
828 dev->subdevlen = 1; 846 dev->subdevlen = 1;
829 dev->nifaces_claimed = 1; /* XXX */ 847 dev->nifaces_claimed = 1; /* XXX */
 848 usbd_serialnumber(dv, dev);
830 } 849 }
831 return (USBD_NORMAL_COMPLETION); 850 return (USBD_NORMAL_COMPLETION);
832} 851}
833 852
834static usbd_status 853static usbd_status
835usbd_attachinterfaces(device_t parent, usbd_device_handle dev, 854usbd_attachinterfaces(device_t parent, usbd_device_handle dev,
836 int port, const int *locators) 855 int port, const int *locators)
837{ 856{
838 struct usbif_attach_arg uiaa; 857 struct usbif_attach_arg uiaa;
839 int ilocs[USBIFIFCF_NLOCS]; 858 int ilocs[USBIFIFCF_NLOCS];
840 usb_device_descriptor_t *dd = &dev->ddesc; 859 usb_device_descriptor_t *dd = &dev->ddesc;
841 int nifaces; 860 int nifaces;
842 usbd_interface_handle *ifaces; 861 usbd_interface_handle *ifaces;
843 int i, j, loc; 862 int i, j, loc;
844 device_t dv; 863 device_t dv;
845 864
846 nifaces = dev->cdesc->bNumInterface; 865 nifaces = dev->cdesc->bNumInterface;
847 ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT|M_ZERO); 866 ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT|M_ZERO);
848 if (!ifaces) 867 if (!ifaces)
849 return (USBD_NOMEM); 868 return (USBD_NOMEM);
850 for (i = 0; i < nifaces; i++) { 869 for (i = 0; i < nifaces; i++) {
851 if (!dev->subdevs[i]) { 870 if (!dev->subdevs[i]) {
852 ifaces[i] = &dev->ifaces[i]; 871 ifaces[i] = &dev->ifaces[i];
853 } 872 }
854 DPRINTF(("%s: interface %d %p\n", __func__, i, ifaces[i])); 873 DPRINTF(("%s: interface %d %p\n", __func__, i, ifaces[i]));
855 } 874 }
856 875
857 uiaa.device = dev; 876 uiaa.device = dev;
858 uiaa.port = port; 877 uiaa.port = port;
859 uiaa.vendor = UGETW(dd->idVendor); 878 uiaa.vendor = UGETW(dd->idVendor);
860 uiaa.product = UGETW(dd->idProduct); 879 uiaa.product = UGETW(dd->idProduct);
861 uiaa.release = UGETW(dd->bcdDevice); 880 uiaa.release = UGETW(dd->bcdDevice);
862 uiaa.configno = dev->cdesc->bConfigurationValue; 881 uiaa.configno = dev->cdesc->bConfigurationValue;
863 uiaa.ifaces = ifaces; 882 uiaa.ifaces = ifaces;
864 uiaa.nifaces = nifaces; 883 uiaa.nifaces = nifaces;
865 ilocs[USBIFIFCF_PORT] = uiaa.port; 884 ilocs[USBIFIFCF_PORT] = uiaa.port;
866 ilocs[USBIFIFCF_VENDOR] = uiaa.vendor; 885 ilocs[USBIFIFCF_VENDOR] = uiaa.vendor;
867 ilocs[USBIFIFCF_PRODUCT] = uiaa.product; 886 ilocs[USBIFIFCF_PRODUCT] = uiaa.product;
868 ilocs[USBIFIFCF_RELEASE] = uiaa.release; 887 ilocs[USBIFIFCF_RELEASE] = uiaa.release;
869 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.configno; 888 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.configno;
870 889
871 for (i = 0; i < nifaces; i++) { 890 for (i = 0; i < nifaces; i++) {
872 if (!ifaces[i]) { 891 if (!ifaces[i]) {
873 DPRINTF(("%s: interface %d claimed\n", __func__, i)); 892 DPRINTF(("%s: interface %d claimed\n", __func__, i));
874 continue; /* interface already claimed */ 893 continue; /* interface already claimed */
875 } 894 }
876 uiaa.iface = ifaces[i]; 895 uiaa.iface = ifaces[i];
877 uiaa.class = ifaces[i]->idesc->bInterfaceClass; 896 uiaa.class = ifaces[i]->idesc->bInterfaceClass;
878 uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass; 897 uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass;
879 uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol; 898 uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol;
880 uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber; 899 uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
881 900
882 DPRINTF(("%s: searching for interface %d " 901 DPRINTF(("%s: searching for interface %d "
883 "class %x subclass %x proto %x ifaceno %d\n", __func__, i, 902 "class %x subclass %x proto %x ifaceno %d\n", __func__, i,
884 uiaa.class, uiaa.subclass, uiaa.proto, uiaa.ifaceno)); 903 uiaa.class, uiaa.subclass, uiaa.proto, uiaa.ifaceno));
885 ilocs[USBIFIFCF_INTERFACE] = uiaa.ifaceno; 904 ilocs[USBIFIFCF_INTERFACE] = uiaa.ifaceno;
886 if (locators != NULL) { 905 if (locators != NULL) {
887 loc = locators[USBIFIFCF_CONFIGURATION]; 906 loc = locators[USBIFIFCF_CONFIGURATION];
888 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT && 907 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
889 loc != uiaa.configno) 908 loc != uiaa.configno)
890 continue; 909 continue;
891 loc = locators[USBIFIFCF_INTERFACE]; 910 loc = locators[USBIFIFCF_INTERFACE];
892 if (loc != USBIFIFCF_INTERFACE_DEFAULT && 911 if (loc != USBIFIFCF_INTERFACE_DEFAULT &&
893 loc != uiaa.ifaceno) 912 loc != uiaa.ifaceno)
894 continue; 913 continue;
895 } 914 }
896 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa, 915 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
897 usbd_ifprint, config_stdsubmatch); 916 usbd_ifprint, config_stdsubmatch);
898 if (!dv) 917 if (!dv)
899 continue; 918 continue;
 919
 920 usbd_serialnumber(dv, dev);
 921
900 /* claim */ 922 /* claim */
901 ifaces[i] = NULL; 923 ifaces[i] = NULL;
902 /* account for ifaces claimed by the driver behind our back */ 924 /* account for ifaces claimed by the driver behind our back */
903 for (j = 0; j < nifaces; j++) { 925 for (j = 0; j < nifaces; j++) {
904 if (!ifaces[j] && !dev->subdevs[j]) { 926 if (!ifaces[j] && !dev->subdevs[j]) {
905 DPRINTF(("%s: interface %d claimed " 927 DPRINTF(("%s: interface %d claimed "
906 "behind our back\n", __func__, j)); 928 "behind our back\n", __func__, j));
907 dev->subdevs[j] = dv; 929 dev->subdevs[j] = dv;
908 dev->nifaces_claimed++; 930 dev->nifaces_claimed++;
909 } 931 }
910 } 932 }
911 } 933 }
912 934
913 free(ifaces, M_USB); 935 free(ifaces, M_USB);
914 return (USBD_NORMAL_COMPLETION); 936 return (USBD_NORMAL_COMPLETION);
915} 937}
916 938
917usbd_status 939usbd_status
918usbd_probe_and_attach(device_t parent, usbd_device_handle dev, 940usbd_probe_and_attach(device_t parent, usbd_device_handle dev,
919 int port, int addr) 941 int port, int addr)
920{ 942{
921 usb_device_descriptor_t *dd = &dev->ddesc; 943 usb_device_descriptor_t *dd = &dev->ddesc;
922 int confi, nifaces; 944 int confi, nifaces;
923 usbd_status err; 945 usbd_status err;
924 946
925 /* First try with device specific drivers. */ 947 /* First try with device specific drivers. */
926 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n")); 948 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
927 err = usbd_attachwholedevice(parent, dev, port, 0); 949 err = usbd_attachwholedevice(parent, dev, port, 0);
928 if (dev->nifaces_claimed || err) 950 if (dev->nifaces_claimed || err)
929 return (err); 951 return (err);
930 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n")); 952 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
931 953
932 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n", 954 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
933 dd->bNumConfigurations)); 955 dd->bNumConfigurations));
934 for (confi = 0; confi < dd->bNumConfigurations; confi++) { 956 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
935 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n", 957 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
936 confi)); 958 confi));
937 err = usbd_set_config_index(dev, confi, 1); 959 err = usbd_set_config_index(dev, confi, 1);
938 if (err) { 960 if (err) {
939#ifdef USB_DEBUG 961#ifdef USB_DEBUG
940 DPRINTF(("%s: port %d, set config at addr %d failed, " 962 DPRINTF(("%s: port %d, set config at addr %d failed, "
941 "error=%s\n", device_xname(parent), port, 963 "error=%s\n", device_xname(parent), port,
942 addr, usbd_errstr(err))); 964 addr, usbd_errstr(err)));
943#else 965#else
944 printf("%s: port %d, set config at addr %d failed\n", 966 printf("%s: port %d, set config at addr %d failed\n",
945 device_xname(parent), port, addr); 967 device_xname(parent), port, addr);
946#endif 968#endif
947 return (err); 969 return (err);
948 } 970 }
949 nifaces = dev->cdesc->bNumInterface; 971 nifaces = dev->cdesc->bNumInterface;
950 dev->subdevs = malloc(nifaces * sizeof(device_t), M_USB, 972 dev->subdevs = malloc(nifaces * sizeof(device_t), M_USB,
951 M_NOWAIT|M_ZERO); 973 M_NOWAIT|M_ZERO);
952 if (dev->subdevs == NULL) 974 if (dev->subdevs == NULL)
953 return (USBD_NOMEM); 975 return (USBD_NOMEM);
954 dev->subdevlen = nifaces; 976 dev->subdevlen = nifaces;
955 977
956 err = usbd_attachinterfaces(parent, dev, port, NULL); 978 err = usbd_attachinterfaces(parent, dev, port, NULL);
957 979
958 if (!dev->nifaces_claimed) { 980 if (!dev->nifaces_claimed) {
959 free(dev->subdevs, M_USB); 981 free(dev->subdevs, M_USB);
960 dev->subdevs = 0; 982 dev->subdevs = 0;
961 dev->subdevlen = 0; 983 dev->subdevlen = 0;
962 } 984 }
963 if (dev->nifaces_claimed || err) 985 if (dev->nifaces_claimed || err)
964 return (err); 986 return (err);
965 } 987 }
966 /* No interfaces were attached in any of the configurations. */ 988 /* No interfaces were attached in any of the configurations. */
967 989
968 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */ 990 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
969 usbd_set_config_index(dev, 0, 0); 991 usbd_set_config_index(dev, 0, 0);
970 992
971 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n")); 993 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
972 994
973 /* Finally try the generic driver. */ 995 /* Finally try the generic driver. */
974 err = usbd_attachwholedevice(parent, dev, port, 1); 996 err = usbd_attachwholedevice(parent, dev, port, 1);
975 997
976 /* 998 /*
977 * The generic attach failed, but leave the device as it is. 999 * The generic attach failed, but leave the device as it is.
978 * We just did not find any drivers, that's all. The device is 1000 * We just did not find any drivers, that's all. The device is
979 * fully operational and not harming anyone. 1001 * fully operational and not harming anyone.
980 */ 1002 */
981 DPRINTF(("usbd_probe_and_attach: generic attach failed\n")); 1003 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
982 return (USBD_NORMAL_COMPLETION); 1004 return (USBD_NORMAL_COMPLETION);
983} 1005}
984 1006
985/** 1007/**
986 * Called from uhub_rescan(). usbd_new_device() for the target dev must be 1008 * Called from uhub_rescan(). usbd_new_device() for the target dev must be
987 * called before calling this. 1009 * called before calling this.
988 */ 1010 */
989usbd_status 1011usbd_status
990usbd_reattach_device(device_t parent, usbd_device_handle dev, 1012usbd_reattach_device(device_t parent, usbd_device_handle dev,
991 int port, const int *locators) 1013 int port, const int *locators)
992{ 1014{
993 int i, loc; 1015 int i, loc;
994 1016
995 if (locators != NULL) { 1017 if (locators != NULL) {
996 loc = locators[USBIFIFCF_PORT]; 1018 loc = locators[USBIFIFCF_PORT];
997 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port) 1019 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
998 return USBD_NORMAL_COMPLETION; 1020 return USBD_NORMAL_COMPLETION;
999 loc = locators[USBIFIFCF_VENDOR]; 1021 loc = locators[USBIFIFCF_VENDOR];
1000 if (loc != USBIFIFCF_VENDOR_DEFAULT && 1022 if (loc != USBIFIFCF_VENDOR_DEFAULT &&
1001 loc != UGETW(dev->ddesc.idVendor)) 1023 loc != UGETW(dev->ddesc.idVendor))
1002 return USBD_NORMAL_COMPLETION; 1024 return USBD_NORMAL_COMPLETION;
1003 loc = locators[USBIFIFCF_PRODUCT]; 1025 loc = locators[USBIFIFCF_PRODUCT];
1004 if (loc != USBIFIFCF_PRODUCT_DEFAULT && 1026 if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
1005 loc != UGETW(dev->ddesc.idProduct)) 1027 loc != UGETW(dev->ddesc.idProduct))
1006 return USBD_NORMAL_COMPLETION; 1028 return USBD_NORMAL_COMPLETION;
1007 loc = locators[USBIFIFCF_RELEASE]; 1029 loc = locators[USBIFIFCF_RELEASE];
1008 if (loc != USBIFIFCF_RELEASE_DEFAULT && 1030 if (loc != USBIFIFCF_RELEASE_DEFAULT &&
1009 loc != UGETW(dev->ddesc.bcdDevice)) 1031 loc != UGETW(dev->ddesc.bcdDevice))
1010 return USBD_NORMAL_COMPLETION; 1032 return USBD_NORMAL_COMPLETION;
1011 } 1033 }
1012 if (dev->subdevlen == 0) { 1034 if (dev->subdevlen == 0) {
1013 /* XXX: check USBIFIFCF_CONFIGURATION and 1035 /* XXX: check USBIFIFCF_CONFIGURATION and
1014 * USBIFIFCF_INTERFACE too */ 1036 * USBIFIFCF_INTERFACE too */
1015 return usbd_probe_and_attach(parent, dev, port, dev->address); 1037 return usbd_probe_and_attach(parent, dev, port, dev->address);
1016 } else if (dev->subdevlen != dev->cdesc->bNumInterface) { 1038 } else if (dev->subdevlen != dev->cdesc->bNumInterface) {
1017 /* device-specific or generic driver is already attached. */ 1039 /* device-specific or generic driver is already attached. */
1018 return USBD_NORMAL_COMPLETION; 1040 return USBD_NORMAL_COMPLETION;
1019 } 1041 }
1020 /* Does the device have unconfigured interfaces? */ 1042 /* Does the device have unconfigured interfaces? */
1021 for (i = 0; i < dev->subdevlen; i++) { 1043 for (i = 0; i < dev->subdevlen; i++) {
1022 if (dev->subdevs[i] == NULL) { 1044 if (dev->subdevs[i] == NULL) {
1023 break; 1045 break;
1024 } 1046 }
1025 } 1047 }
1026 if (i >= dev->subdevlen) 1048 if (i >= dev->subdevlen)
1027 return USBD_NORMAL_COMPLETION; 1049 return USBD_NORMAL_COMPLETION;
1028 return usbd_attachinterfaces(parent, dev, port, locators); 1050 return usbd_attachinterfaces(parent, dev, port, locators);
1029} 1051}
1030 1052
1031/* 1053/*
1032 * Get the first 8 bytes of the device descriptor. 1054 * Get the first 8 bytes of the device descriptor.
1033 * Do as Windows does: try to read 64 bytes -- there are devices which 1055 * Do as Windows does: try to read 64 bytes -- there are devices which
1034 * recognize the initial descriptor fetch (before the control endpoint's 1056 * recognize the initial descriptor fetch (before the control endpoint's
1035 * MaxPacketSize is known by the host) by exactly this length. 1057 * MaxPacketSize is known by the host) by exactly this length.
1036 */ 1058 */
1037usbd_status 1059usbd_status
1038usbd_get_initial_ddesc(usbd_device_handle dev, usb_device_descriptor_t *desc) 1060usbd_get_initial_ddesc(usbd_device_handle dev, usb_device_descriptor_t *desc)
1039{ 1061{
1040 usb_device_request_t req; 1062 usb_device_request_t req;
1041 char buf[64]; 1063 char buf[64];
1042 int res, actlen; 1064 int res, actlen;
1043 1065
1044 req.bmRequestType = UT_READ_DEVICE; 1066 req.bmRequestType = UT_READ_DEVICE;
1045 req.bRequest = UR_GET_DESCRIPTOR; 1067 req.bRequest = UR_GET_DESCRIPTOR;
1046 USETW2(req.wValue, UDESC_DEVICE, 0); 1068 USETW2(req.wValue, UDESC_DEVICE, 0);
1047 USETW(req.wIndex, 0); 1069 USETW(req.wIndex, 0);
1048 USETW(req.wLength, 64); 1070 USETW(req.wLength, 64);
1049 res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK, 1071 res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
1050 &actlen, USBD_DEFAULT_TIMEOUT); 1072 &actlen, USBD_DEFAULT_TIMEOUT);
1051 if (res) 1073 if (res)
1052 return res; 1074 return res;
1053 if (actlen < 8) 1075 if (actlen < 8)
1054 return USBD_SHORT_XFER; 1076 return USBD_SHORT_XFER;
1055 memcpy(desc, buf, 8); 1077 memcpy(desc, buf, 8);
1056 return USBD_NORMAL_COMPLETION; 1078 return USBD_NORMAL_COMPLETION;
1057} 1079}
1058 1080
1059/* 1081/*
1060 * Called when a new device has been put in the powered state, 1082 * Called when a new device has been put in the powered state,
1061 * but not yet in the addressed state. 1083 * but not yet in the addressed state.
1062 * Get initial descriptor, set the address, get full descriptor, 1084 * Get initial descriptor, set the address, get full descriptor,
1063 * and attach a driver. 1085 * and attach a driver.
1064 */ 1086 */
1065usbd_status 1087usbd_status
1066usbd_new_device(device_t parent, usbd_bus_handle bus, int depth, 1088usbd_new_device(device_t parent, usbd_bus_handle bus, int depth,
1067 int speed, int port, struct usbd_port *up) 1089 int speed, int port, struct usbd_port *up)
1068{ 1090{
1069 usbd_device_handle dev, adev; 1091 usbd_device_handle dev, adev;
1070 struct usbd_device *hub; 1092 struct usbd_device *hub;
1071 usb_device_descriptor_t *dd; 1093 usb_device_descriptor_t *dd;
1072 usb_port_status_t ps; 1094 usb_port_status_t ps;
1073 usbd_status err; 1095 usbd_status err;
1074 int addr; 1096 int addr;
1075 int i; 1097 int i;
1076 int p; 1098 int p;
1077 1099
1078 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n", 1100 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1079 bus, port, depth, speed)); 1101 bus, port, depth, speed));
1080 1102
1081 if (bus->methods->new_device != NULL) 1103 if (bus->methods->new_device != NULL)
1082 return (bus->methods->new_device)(parent, bus, depth, speed, 1104 return (bus->methods->new_device)(parent, bus, depth, speed,
1083 port, up); 1105 port, up);
1084 1106
1085 addr = usbd_getnewaddr(bus); 1107 addr = usbd_getnewaddr(bus);
1086 if (addr < 0) { 1108 if (addr < 0) {
1087 printf("%s: No free USB addresses, new device ignored.\n", 1109 printf("%s: No free USB addresses, new device ignored.\n",
1088 device_xname(bus->usbctl)); 1110 device_xname(bus->usbctl));
1089 return (USBD_NO_ADDR); 1111 return (USBD_NO_ADDR);
1090 } 1112 }
1091 1113
1092 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO); 1114 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1093 if (dev == NULL) 1115 if (dev == NULL)
1094 return (USBD_NOMEM); 1116 return (USBD_NOMEM);
1095 1117
1096 dev->bus = bus; 1118 dev->bus = bus;
1097 1119
1098 /* Set up default endpoint handle. */ 1120 /* Set up default endpoint handle. */
1099 dev->def_ep.edesc = &dev->def_ep_desc; 1121 dev->def_ep.edesc = &dev->def_ep_desc;
1100 1122
1101 /* Set up default endpoint descriptor. */ 1123 /* Set up default endpoint descriptor. */
1102 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 1124 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1103 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1125 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1104 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1126 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1105 dev->def_ep_desc.bmAttributes = UE_CONTROL; 1127 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1106 /* 1128 /*
1107 * temporary, will be fixed after first descriptor fetch 1129 * temporary, will be fixed after first descriptor fetch
1108 * (which uses 64 bytes so it shouldn't be less), 1130 * (which uses 64 bytes so it shouldn't be less),
1109 * highspeed devices must support 64 byte packets anyway 1131 * highspeed devices must support 64 byte packets anyway
1110 */ 1132 */
1111 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL) 1133 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL)
1112 USETW(dev->def_ep_desc.wMaxPacketSize, 64); 1134 USETW(dev->def_ep_desc.wMaxPacketSize, 64);
1113 else 1135 else
1114 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET); 1136 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1115 1137
1116 dev->def_ep_desc.bInterval = 0; 1138 dev->def_ep_desc.bInterval = 0;
1117 1139
1118 /* doesn't matter, just don't leave it uninitialized */ 1140 /* doesn't matter, just don't leave it uninitialized */
1119 dev->def_ep.datatoggle = 0; 1141 dev->def_ep.datatoggle = 0;
1120 1142
1121 dev->quirks = &usbd_no_quirk; 1143 dev->quirks = &usbd_no_quirk;
1122 dev->address = USB_START_ADDR; 1144 dev->address = USB_START_ADDR;
1123 dev->ddesc.bMaxPacketSize = 0; 1145 dev->ddesc.bMaxPacketSize = 0;
1124 dev->depth = depth; 1146 dev->depth = depth;
1125 dev->powersrc = up; 1147 dev->powersrc = up;
1126 dev->myhub = up->parent; 1148 dev->myhub = up->parent;
1127 1149
1128 up->device = dev; 1150 up->device = dev;
1129 1151
1130 /* Locate port on upstream high speed hub */ 1152 /* Locate port on upstream high speed hub */
1131 for (adev = dev, hub = up->parent; 1153 for (adev = dev, hub = up->parent;
1132 hub != NULL && hub->speed != USB_SPEED_HIGH; 1154 hub != NULL && hub->speed != USB_SPEED_HIGH;
1133 adev = hub, hub = hub->myhub) 1155 adev = hub, hub = hub->myhub)
1134 ; 1156 ;
1135 if (hub) { 1157 if (hub) {
1136 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) { 1158 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1137 if (hub->hub->ports[p].device == adev) { 1159 if (hub->hub->ports[p].device == adev) {
1138 dev->myhsport = &hub->hub->ports[p]; 1160 dev->myhsport = &hub->hub->ports[p];
1139 goto found; 1161 goto found;
1140 } 1162 }
1141 } 1163 }
1142 panic("usbd_new_device: cannot find HS port"); 1164 panic("usbd_new_device: cannot find HS port");
1143 found: 1165 found:
1144 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p)); 1166 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1145 } else { 1167 } else {
1146 dev->myhsport = NULL; 1168 dev->myhsport = NULL;
1147 } 1169 }
1148 dev->speed = speed; 1170 dev->speed = speed;
1149 dev->langid = USBD_NOLANG; 1171 dev->langid = USBD_NOLANG;
1150 dev->cookie.cookie = ++usb_cookie_no; 1172 dev->cookie.cookie = ++usb_cookie_no;
1151 1173
1152 /* Establish the default pipe. */ 1174 /* Establish the default pipe. */
1153 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1175 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1154 &dev->default_pipe, USBD_MPSAFE); 1176 &dev->default_pipe, USBD_MPSAFE);
1155 if (err) { 1177 if (err) {
1156 usbd_remove_device(dev, up); 1178 usbd_remove_device(dev, up);
1157 return (err); 1179 return (err);
1158 } 1180 }
1159 1181
1160 dd = &dev->ddesc; 1182 dd = &dev->ddesc;
1161 /* Try a few times in case the device is slow (i.e. outside specs.) */ 1183 /* Try a few times in case the device is slow (i.e. outside specs.) */
1162 for (i = 0; i < 10; i++) { 1184 for (i = 0; i < 10; i++) {
1163 /* Get the first 8 bytes of the device descriptor. */ 1185 /* Get the first 8 bytes of the device descriptor. */
1164 err = usbd_get_initial_ddesc(dev, dd); 1186 err = usbd_get_initial_ddesc(dev, dd);
1165 if (!err) 1187 if (!err)
1166 break; 1188 break;
1167 usbd_delay_ms(dev, 200); 1189 usbd_delay_ms(dev, 200);
1168 if ((i & 3) == 3) 1190 if ((i & 3) == 3)
1169 usbd_reset_port(up->parent, port, &ps); 1191 usbd_reset_port(up->parent, port, &ps);
1170 } 1192 }
1171 if (err) { 1193 if (err) {
1172 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc " 1194 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1173 "failed: %d\n", addr, err)); 1195 "failed: %d\n", addr, err));
1174 usbd_remove_device(dev, up); 1196 usbd_remove_device(dev, up);
1175 return (err); 1197 return (err);
1176 } 1198 }
1177 1199
1178 /* Windows resets the port here, do likewise */ 1200 /* Windows resets the port here, do likewise */
1179 if (up->parent) 1201 if (up->parent)
1180 usbd_reset_port(up->parent, port, &ps); 1202 usbd_reset_port(up->parent, port, &ps);
1181 1203
1182 if (speed == USB_SPEED_HIGH) { 1204 if (speed == USB_SPEED_HIGH) {
1183 /* Max packet size must be 64 (sec 5.5.3). */ 1205 /* Max packet size must be 64 (sec 5.5.3). */
1184 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) { 1206 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1185#ifdef DIAGNOSTIC 1207#ifdef DIAGNOSTIC
1186 printf("usbd_new_device: addr=%d bad max packet " 1208 printf("usbd_new_device: addr=%d bad max packet "
1187 "size=%d. adjusting to %d.\n", 1209 "size=%d. adjusting to %d.\n",
1188 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET); 1210 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
1189#endif 1211#endif
1190 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET; 1212 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1191 } 1213 }
1192 } 1214 }
1193 1215
1194 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, " 1216 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1195 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1217 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1196 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass, 1218 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1197 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, 1219 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1198 dev->speed)); 1220 dev->speed));
1199 1221
1200 if (dd->bDescriptorType != UDESC_DEVICE) { 1222 if (dd->bDescriptorType != UDESC_DEVICE) {
1201 /* Illegal device descriptor */ 1223 /* Illegal device descriptor */
1202 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n", 1224 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1203 dd->bDescriptorType)); 1225 dd->bDescriptorType));
1204 usbd_remove_device(dev, up); 1226 usbd_remove_device(dev, up);
1205 return (USBD_INVAL); 1227 return (USBD_INVAL);
1206 } 1228 }
1207 1229
1208 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) { 1230 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1209 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength)); 1231 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1210 usbd_remove_device(dev, up); 1232 usbd_remove_device(dev, up);
1211 return (USBD_INVAL); 1233 return (USBD_INVAL);
1212 } 1234 }
1213 1235
1214 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize); 1236 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1215 1237
1216 /* Re-establish the default pipe with the new MPS. */ 1238 /* Re-establish the default pipe with the new MPS. */
1217 usbd_kill_pipe(dev->default_pipe); 1239 usbd_kill_pipe(dev->default_pipe);
1218 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1240 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1219 &dev->default_pipe, USBD_MPSAFE); 1241 &dev->default_pipe, USBD_MPSAFE);
1220 if (err) { 1242 if (err) {
1221 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n")); 1243 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n"));
1222 usbd_remove_device(dev, up); 1244 usbd_remove_device(dev, up);
1223 return err; 1245 return err;
1224 } 1246 }
1225 1247
1226 /* Set the address */ 1248 /* Set the address */
1227 DPRINTFN(5, ("usbd_new_device: setting device address=%d\n", addr)); 1249 DPRINTFN(5, ("usbd_new_device: setting device address=%d\n", addr));
1228 err = usbd_set_address(dev, addr); 1250 err = usbd_set_address(dev, addr);
1229 if (err) { 1251 if (err) {
1230 DPRINTFN(-1, ("usbd_new_device: set address %d failed\n", addr)); 1252 DPRINTFN(-1, ("usbd_new_device: set address %d failed\n", addr));
1231 err = USBD_SET_ADDR_FAILED; 1253 err = USBD_SET_ADDR_FAILED;
1232 usbd_remove_device(dev, up); 1254 usbd_remove_device(dev, up);
1233 return err; 1255 return err;
1234 } 1256 }
1235 1257
1236 /* Allow device time to set new address */ 1258 /* Allow device time to set new address */
1237 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); 1259 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1238 dev->address = addr; /* new device address now */ 1260 dev->address = addr; /* new device address now */
1239 bus->devices[addr] = dev; 1261 bus->devices[addr] = dev;
1240 1262
1241 /* Re-establish the default pipe with the new address. */ 1263 /* Re-establish the default pipe with the new address. */
1242 usbd_kill_pipe(dev->default_pipe); 1264 usbd_kill_pipe(dev->default_pipe);
1243 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1265 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1244 &dev->default_pipe, USBD_MPSAFE); 1266 &dev->default_pipe, USBD_MPSAFE);
1245 if (err) { 1267 if (err) {
1246 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n")); 1268 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n"));
1247 usbd_remove_device(dev, up); 1269 usbd_remove_device(dev, up);
1248 return err; 1270 return err;
1249 } 1271 }
1250 1272
1251 err = usbd_reload_device_desc(dev); 1273 err = usbd_reload_device_desc(dev);
1252 if (err) { 1274 if (err) {
1253 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc " 1275 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1254 "failed\n", addr)); 1276 "failed\n", addr));
1255 usbd_remove_device(dev, up); 1277 usbd_remove_device(dev, up);
1256 return (err); 1278 return (err);
1257 } 1279 }
1258 1280
1259 /* Assume 100mA bus powered for now. Changed when configured. */ 1281 /* Assume 100mA bus powered for now. Changed when configured. */
1260 dev->power = USB_MIN_POWER; 1282 dev->power = USB_MIN_POWER;
1261 dev->self_powered = 0; 1283 dev->self_powered = 0;
1262 1284
1263 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n", 1285 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1264 addr, dev, parent)); 1286 addr, dev, parent));
1265 1287
1266 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); 1288 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1267 1289
1268 if (port == 0) { /* root hub */ 1290 if (port == 0) { /* root hub */
1269 KASSERT(addr == 1); 1291 KASSERT(addr == 1);
1270 usbd_attach_roothub(parent, dev); 1292 usbd_attach_roothub(parent, dev);
1271 return (USBD_NORMAL_COMPLETION); 1293 return (USBD_NORMAL_COMPLETION);
1272 } 1294 }
1273 1295
1274 err = usbd_probe_and_attach(parent, dev, port, addr); 1296 err = usbd_probe_and_attach(parent, dev, port, addr);
1275 if (err) { 1297 if (err) {
1276 usbd_remove_device(dev, up); 1298 usbd_remove_device(dev, up);
1277 return (err); 1299 return (err);
1278 } 1300 }
1279 1301
1280 return (USBD_NORMAL_COMPLETION); 1302 return (USBD_NORMAL_COMPLETION);
1281} 1303}
1282 1304
1283usbd_status 1305usbd_status
1284usbd_reload_device_desc(usbd_device_handle dev) 1306usbd_reload_device_desc(usbd_device_handle dev)
1285{ 1307{
1286 usbd_status err; 1308 usbd_status err;
1287 1309
1288 /* Get the full device descriptor. */ 1310 /* Get the full device descriptor. */
1289 err = usbd_get_device_desc(dev, &dev->ddesc); 1311 err = usbd_get_device_desc(dev, &dev->ddesc);
1290 if (err) 1312 if (err)
1291 return (err); 1313 return (err);
1292 1314
1293 /* Figure out what's wrong with this device. */ 1315 /* Figure out what's wrong with this device. */
1294 dev->quirks = usbd_find_quirk(&dev->ddesc); 1316 dev->quirks = usbd_find_quirk(&dev->ddesc);
1295 1317
1296 return (USBD_NORMAL_COMPLETION); 1318 return (USBD_NORMAL_COMPLETION);
1297} 1319}
1298 1320
1299void 1321void
1300usbd_remove_device(usbd_device_handle dev, struct usbd_port *up) 1322usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1301{ 1323{
1302 1324
1303 DPRINTF(("usbd_remove_device: %p\n", dev)); 1325 DPRINTF(("usbd_remove_device: %p\n", dev));
1304 1326
1305 if (dev->default_pipe != NULL) 1327 if (dev->default_pipe != NULL)
1306 usbd_kill_pipe(dev->default_pipe); 1328 usbd_kill_pipe(dev->default_pipe);
1307 up->device = NULL; 1329 up->device = NULL;
1308 dev->bus->devices[dev->address] = NULL; 1330 dev->bus->devices[dev->address] = NULL;
1309 1331
1310 free(dev, M_USB); 1332 free(dev, M_USB);
1311} 1333}
1312 1334
1313int 1335int
1314usbd_print(void *aux, const char *pnp) 1336usbd_print(void *aux, const char *pnp)
1315{ 1337{
1316 struct usb_attach_arg *uaa = aux; 1338 struct usb_attach_arg *uaa = aux;
1317 1339
1318 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device)); 1340 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1319 if (pnp) { 1341 if (pnp) {
1320#define USB_DEVINFO 1024 1342#define USB_DEVINFO 1024
1321 char *devinfo; 1343 char *devinfo;
1322 if (!uaa->usegeneric) 1344 if (!uaa->usegeneric)
1323 return (QUIET); 1345 return (QUIET);
1324 devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK); 1346 devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
1325 usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO); 1347 usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
1326 aprint_normal("%s, %s", devinfo, pnp); 1348 aprint_normal("%s, %s", devinfo, pnp);
1327 free(devinfo, M_TEMP); 1349 free(devinfo, M_TEMP);
1328 } 1350 }
1329 aprint_normal(" port %d", uaa->port); 1351 aprint_normal(" port %d", uaa->port);
1330#if 0 1352#if 0
1331 /* 1353 /*
1332 * It gets very crowded with these locators on the attach line. 1354 * It gets very crowded with these locators on the attach line.
1333 * They are not really needed since they are printed in the clear 1355 * They are not really needed since they are printed in the clear
1334 * by each driver. 1356 * by each driver.
1335 */ 1357 */
1336 if (uaa->vendor != UHUB_UNK_VENDOR) 1358 if (uaa->vendor != UHUB_UNK_VENDOR)
1337 aprint_normal(" vendor 0x%04x", uaa->vendor); 1359 aprint_normal(" vendor 0x%04x", uaa->vendor);
1338 if (uaa->product != UHUB_UNK_PRODUCT) 1360 if (uaa->product != UHUB_UNK_PRODUCT)
1339 aprint_normal(" product 0x%04x", uaa->product); 1361 aprint_normal(" product 0x%04x", uaa->product);
1340 if (uaa->release != UHUB_UNK_RELEASE) 1362 if (uaa->release != UHUB_UNK_RELEASE)
1341 aprint_normal(" release 0x%04x", uaa->release); 1363 aprint_normal(" release 0x%04x", uaa->release);
1342#endif 1364#endif
1343 return (UNCONF); 1365 return (UNCONF);
1344} 1366}
1345 1367
1346int 1368int
1347usbd_ifprint(void *aux, const char *pnp) 1369usbd_ifprint(void *aux, const char *pnp)
1348{ 1370{
1349 struct usbif_attach_arg *uaa = aux; 1371 struct usbif_attach_arg *uaa = aux;
1350 1372
1351 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device)); 1373 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1352 if (pnp) 1374 if (pnp)
1353 return (QUIET); 1375 return (QUIET);
1354 aprint_normal(" port %d", uaa->port); 1376 aprint_normal(" port %d", uaa->port);
1355 aprint_normal(" configuration %d", uaa->configno); 1377 aprint_normal(" configuration %d", uaa->configno);
1356 aprint_normal(" interface %d", uaa->ifaceno); 1378 aprint_normal(" interface %d", uaa->ifaceno);
1357#if 0 1379#if 0
1358 /* 1380 /*
1359 * It gets very crowded with these locators on the attach line. 1381 * It gets very crowded with these locators on the attach line.
1360 * They are not really needed since they are printed in the clear 1382 * They are not really needed since they are printed in the clear
1361 * by each driver. 1383 * by each driver.
1362 */ 1384 */
1363 if (uaa->vendor != UHUB_UNK_VENDOR) 1385 if (uaa->vendor != UHUB_UNK_VENDOR)
1364 aprint_normal(" vendor 0x%04x", uaa->vendor); 1386 aprint_normal(" vendor 0x%04x", uaa->vendor);
1365 if (uaa->product != UHUB_UNK_PRODUCT) 1387 if (uaa->product != UHUB_UNK_PRODUCT)
1366 aprint_normal(" product 0x%04x", uaa->product); 1388 aprint_normal(" product 0x%04x", uaa->product);
1367 if (uaa->release != UHUB_UNK_RELEASE) 1389 if (uaa->release != UHUB_UNK_RELEASE)
1368 aprint_normal(" release 0x%04x", uaa->release); 1390 aprint_normal(" release 0x%04x", uaa->release);
1369#endif 1391#endif
1370 return (UNCONF); 1392 return (UNCONF);
1371} 1393}
1372 1394
1373void 1395void
1374usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di, 1396usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1375 int usedev) 1397 int usedev)
1376{ 1398{
1377 struct usbd_port *p; 1399 struct usbd_port *p;
1378 int i, j, err, s; 1400 int i, j, err, s;
1379 1401
1380 di->udi_bus = device_unit(dev->bus->usbctl); 1402 di->udi_bus = device_unit(dev->bus->usbctl);
1381 di->udi_addr = dev->address; 1403 di->udi_addr = dev->address;
1382 di->udi_cookie = dev->cookie; 1404 di->udi_cookie = dev->cookie;
1383 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor), 1405 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1384 di->udi_product, sizeof(di->udi_product), usedev, 1); 1406 di->udi_product, sizeof(di->udi_product), usedev, 1);
1385 usbd_printBCD(di->udi_release, sizeof(di->udi_release), 1407 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1386 UGETW(dev->ddesc.bcdDevice)); 1408 UGETW(dev->ddesc.bcdDevice));
1387 di->udi_serial[0] = 0; 1409 di->udi_serial[0] = 0;
1388 if (usedev) 1410 if (usedev)
1389 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, 1411 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1390 di->udi_serial); 1412 di->udi_serial);
1391 di->udi_vendorNo = UGETW(dev->ddesc.idVendor); 1413 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1392 di->udi_productNo = UGETW(dev->ddesc.idProduct); 1414 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1393 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice); 1415 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1394 di->udi_class = dev->ddesc.bDeviceClass; 1416 di->udi_class = dev->ddesc.bDeviceClass;
1395 di->udi_subclass = dev->ddesc.bDeviceSubClass; 1417 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1396 di->udi_protocol = dev->ddesc.bDeviceProtocol; 1418 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1397 di->udi_config = dev->config; 1419 di->udi_config = dev->config;
1398 di->udi_power = dev->self_powered ? 0 : dev->power; 1420 di->udi_power = dev->self_powered ? 0 : dev->power;
1399 di->udi_speed = dev->speed; 1421 di->udi_speed = dev->speed;
1400 1422
1401 if (dev->subdevlen > 0) { 1423 if (dev->subdevlen > 0) {
1402 for (i = 0, j = 0; i < dev->subdevlen && 1424 for (i = 0, j = 0; i < dev->subdevlen &&
1403 j < USB_MAX_DEVNAMES; i++) { 1425 j < USB_MAX_DEVNAMES; i++) {
1404 if (!dev->subdevs[i]) 1426 if (!dev->subdevs[i])
1405 continue; 1427 continue;
1406 strncpy(di->udi_devnames[j], 1428 strncpy(di->udi_devnames[j],
1407 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN); 1429 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN);
1408 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0'; 1430 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1409 j++; 1431 j++;
1410 } 1432 }
1411 } else { 1433 } else {
1412 j = 0; 1434 j = 0;
1413 } 1435 }
1414 for (/* j is set */; j < USB_MAX_DEVNAMES; j++) 1436 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1415 di->udi_devnames[j][0] = 0; /* empty */ 1437 di->udi_devnames[j][0] = 0; /* empty */
1416 1438
1417 if (dev->hub) { 1439 if (dev->hub) {
1418 for (i = 0; 1440 for (i = 0;
1419 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) && 1441 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1420 i < dev->hub->hubdesc.bNbrPorts; 1442 i < dev->hub->hubdesc.bNbrPorts;
1421 i++) { 1443 i++) {
1422 p = &dev->hub->ports[i]; 1444 p = &dev->hub->ports[i];
1423 if (p->device) 1445 if (p->device)
1424 err = p->device->address; 1446 err = p->device->address;
1425 else { 1447 else {
1426 s = UGETW(p->status.wPortStatus); 1448 s = UGETW(p->status.wPortStatus);
1427 if (s & UPS_PORT_ENABLED) 1449 if (s & UPS_PORT_ENABLED)
1428 err = USB_PORT_ENABLED; 1450 err = USB_PORT_ENABLED;
1429 else if (s & UPS_SUSPEND) 1451 else if (s & UPS_SUSPEND)
1430 err = USB_PORT_SUSPENDED; 1452 err = USB_PORT_SUSPENDED;
1431 else if (s & UPS_PORT_POWER) 1453 else if (s & UPS_PORT_POWER)
1432 err = USB_PORT_POWERED; 1454 err = USB_PORT_POWERED;
1433 else 1455 else
1434 err = USB_PORT_DISABLED; 1456 err = USB_PORT_DISABLED;
1435 } 1457 }
1436 di->udi_ports[i] = err; 1458 di->udi_ports[i] = err;
1437 } 1459 }
1438 di->udi_nports = dev->hub->hubdesc.bNbrPorts; 1460 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1439 } else 1461 } else
1440 di->udi_nports = 0; 1462 di->udi_nports = 0;
1441} 1463}
1442 1464
1443#ifdef COMPAT_30 1465#ifdef COMPAT_30
1444void 1466void
1445usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di, 1467usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
1446 int usedev) 1468 int usedev)
1447{ 1469{
1448 struct usbd_port *p; 1470 struct usbd_port *p;
1449 int i, j, err, s; 1471 int i, j, err, s;
1450 1472
1451 di->udi_bus = device_unit(dev->bus->usbctl); 1473 di->udi_bus = device_unit(dev->bus->usbctl);
1452 di->udi_addr = dev->address; 1474 di->udi_addr = dev->address;
1453 di->udi_cookie = dev->cookie; 1475 di->udi_cookie = dev->cookie;
1454 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor), 1476 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1455 di->udi_product, sizeof(di->udi_product), usedev, 0); 1477 di->udi_product, sizeof(di->udi_product), usedev, 0);
1456 usbd_printBCD(di->udi_release, sizeof(di->udi_release), 1478 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1457 UGETW(dev->ddesc.bcdDevice)); 1479 UGETW(dev->ddesc.bcdDevice));
1458 di->udi_vendorNo = UGETW(dev->ddesc.idVendor); 1480 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1459 di->udi_productNo = UGETW(dev->ddesc.idProduct); 1481 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1460 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice); 1482 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1461 di->udi_class = dev->ddesc.bDeviceClass; 1483 di->udi_class = dev->ddesc.bDeviceClass;
1462 di->udi_subclass = dev->ddesc.bDeviceSubClass; 1484 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1463 di->udi_protocol = dev->ddesc.bDeviceProtocol; 1485 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1464 di->udi_config = dev->config; 1486 di->udi_config = dev->config;
1465 di->udi_power = dev->self_powered ? 0 : dev->power; 1487 di->udi_power = dev->self_powered ? 0 : dev->power;
1466 di->udi_speed = dev->speed; 1488 di->udi_speed = dev->speed;
1467 1489
1468 if (dev->subdevlen > 0) { 1490 if (dev->subdevlen > 0) {
1469 for (i = 0, j = 0; i < dev->subdevlen && 1491 for (i = 0, j = 0; i < dev->subdevlen &&
1470 j < USB_MAX_DEVNAMES; i++) { 1492 j < USB_MAX_DEVNAMES; i++) {
1471 if (!dev->subdevs[i]) 1493 if (!dev->subdevs[i])
1472 continue; 1494 continue;
1473 strncpy(di->udi_devnames[j], 1495 strncpy(di->udi_devnames[j],
1474 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN); 1496 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN);
1475 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0'; 1497 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1476 j++; 1498 j++;
1477 } 1499 }
1478 } else { 1500 } else {
1479 j = 0; 1501 j = 0;
1480 } 1502 }
1481 for (/* j is set */; j < USB_MAX_DEVNAMES; j++) 1503 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1482 di->udi_devnames[j][0] = 0; /* empty */ 1504 di->udi_devnames[j][0] = 0; /* empty */
1483 1505
1484 if (dev->hub) { 1506 if (dev->hub) {
1485 for (i = 0; 1507 for (i = 0;
1486 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) && 1508 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1487 i < dev->hub->hubdesc.bNbrPorts; 1509 i < dev->hub->hubdesc.bNbrPorts;
1488 i++) { 1510 i++) {
1489 p = &dev->hub->ports[i]; 1511 p = &dev->hub->ports[i];
1490 if (p->device) 1512 if (p->device)
1491 err = p->device->address; 1513 err = p->device->address;
1492 else { 1514 else {
1493 s = UGETW(p->status.wPortStatus); 1515 s = UGETW(p->status.wPortStatus);
1494 if (s & UPS_PORT_ENABLED) 1516 if (s & UPS_PORT_ENABLED)
1495 err = USB_PORT_ENABLED; 1517 err = USB_PORT_ENABLED;
1496 else if (s & UPS_SUSPEND) 1518 else if (s & UPS_SUSPEND)
1497 err = USB_PORT_SUSPENDED; 1519 err = USB_PORT_SUSPENDED;
1498 else if (s & UPS_PORT_POWER) 1520 else if (s & UPS_PORT_POWER)
1499 err = USB_PORT_POWERED; 1521 err = USB_PORT_POWERED;
1500 else 1522 else
1501 err = USB_PORT_DISABLED; 1523 err = USB_PORT_DISABLED;
1502 } 1524 }
1503 di->udi_ports[i] = err; 1525 di->udi_ports[i] = err;
1504 } 1526 }
1505 di->udi_nports = dev->hub->hubdesc.bNbrPorts; 1527 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1506 } else 1528 } else
1507 di->udi_nports = 0; 1529 di->udi_nports = 0;
1508} 1530}
1509#endif 1531#endif
1510 1532
1511 1533
1512void 1534void
1513usb_free_device(usbd_device_handle dev) 1535usb_free_device(usbd_device_handle dev)
1514{ 1536{
1515 int ifcidx, nifc; 1537 int ifcidx, nifc;
1516 1538
1517 if (dev->default_pipe != NULL) 1539 if (dev->default_pipe != NULL)
1518 usbd_kill_pipe(dev->default_pipe); 1540 usbd_kill_pipe(dev->default_pipe);
1519 if (dev->ifaces != NULL) { 1541 if (dev->ifaces != NULL) {
1520 nifc = dev->cdesc->bNumInterface; 1542 nifc = dev->cdesc->bNumInterface;
1521 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 1543 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1522 usbd_free_iface_data(dev, ifcidx); 1544 usbd_free_iface_data(dev, ifcidx);
1523 free(dev->ifaces, M_USB); 1545 free(dev->ifaces, M_USB);
1524 } 1546 }
1525 if (dev->cdesc != NULL) 1547 if (dev->cdesc != NULL)
1526 free(dev->cdesc, M_USB); 1548 free(dev->cdesc, M_USB);
1527 if (dev->subdevlen > 0) { 1549 if (dev->subdevlen > 0) {
1528 free(dev->subdevs, M_USB); 1550 free(dev->subdevs, M_USB);
1529 dev->subdevlen = 0; 1551 dev->subdevlen = 0;
1530 } 1552 }
1531 free(dev, M_USB); 1553 free(dev, M_USB);
1532} 1554}
1533 1555
1534/* 1556/*
1535 * The general mechanism for detaching drivers works as follows: Each 1557 * The general mechanism for detaching drivers works as follows: Each
1536 * driver is responsible for maintaining a reference count on the 1558 * driver is responsible for maintaining a reference count on the
1537 * number of outstanding references to its softc (e.g. from 1559 * number of outstanding references to its softc (e.g. from
1538 * processing hanging in a read or write). The detach method of the 1560 * processing hanging in a read or write). The detach method of the
1539 * driver decrements this counter and flags in the softc that the 1561 * driver decrements this counter and flags in the softc that the
1540 * driver is dying and then wakes any sleepers. It then sleeps on the 1562 * driver is dying and then wakes any sleepers. It then sleeps on the
1541 * softc. Each place that can sleep must maintain the reference 1563 * softc. Each place that can sleep must maintain the reference
1542 * count. When the reference count drops to -1 (0 is the normal value 1564 * count. When the reference count drops to -1 (0 is the normal value
1543 * of the reference count) the a wakeup on the softc is performed 1565 * of the reference count) the a wakeup on the softc is performed
1544 * signaling to the detach waiter that all references are gone. 1566 * signaling to the detach waiter that all references are gone.
1545 */ 1567 */
1546 1568
1547/* 1569/*
1548 * Called from process context when we discover that a port has 1570 * Called from process context when we discover that a port has
1549 * been disconnected. 1571 * been disconnected.
1550 */ 1572 */
1551int 1573int
1552usb_disconnect_port(struct usbd_port *up, device_t parent, int flags) 1574usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
1553{ 1575{
1554 usbd_device_handle dev = up->device; 1576 usbd_device_handle dev = up->device;
1555 device_t subdev; 1577 device_t subdev;
1556 char subdevname[16]; 1578 char subdevname[16];
1557 const char *hubname = device_xname(parent); 1579 const char *hubname = device_xname(parent);
1558 int i, rc; 1580 int i, rc;
1559 1581
1560 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n", 1582 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1561 up, dev, up->portno)); 1583 up, dev, up->portno));
1562 1584
1563 if (dev == NULL) { 1585 if (dev == NULL) {
1564#ifdef DIAGNOSTIC 1586#ifdef DIAGNOSTIC
1565 printf("usb_disconnect_port: no device\n"); 1587 printf("usb_disconnect_port: no device\n");
1566#endif 1588#endif
1567 return 0; 1589 return 0;
1568 } 1590 }
1569 1591
1570 if (dev->subdevlen > 0) { 1592 if (dev->subdevlen > 0) {
1571 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n")); 1593 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1572 for (i = 0; i < dev->subdevlen; i++) { 1594 for (i = 0; i < dev->subdevlen; i++) {
1573 if ((subdev = dev->subdevs[i]) == NULL) 1595 if ((subdev = dev->subdevs[i]) == NULL)
1574 continue; 1596 continue;
1575 strlcpy(subdevname, device_xname(subdev), 1597 strlcpy(subdevname, device_xname(subdev),
1576 sizeof(subdevname)); 1598 sizeof(subdevname));
1577 if ((rc = config_detach(subdev, flags)) != 0) 1599 if ((rc = config_detach(subdev, flags)) != 0)
1578 return rc; 1600 return rc;
1579 printf("%s: at %s", subdevname, hubname); 1601 printf("%s: at %s", subdevname, hubname);
1580 if (up->portno != 0) 1602 if (up->portno != 0)
1581 printf(" port %d", up->portno); 1603 printf(" port %d", up->portno);
1582 printf(" (addr %d) disconnected\n", dev->address); 1604 printf(" (addr %d) disconnected\n", dev->address);
1583 } 1605 }
1584 KASSERT(!dev->nifaces_claimed); 1606 KASSERT(!dev->nifaces_claimed);
1585 } 1607 }
1586 1608
1587 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev); 1609 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1588 dev->bus->devices[dev->address] = NULL; 1610 dev->bus->devices[dev->address] = NULL;
1589 up->device = NULL; 1611 up->device = NULL;
1590 usb_free_device(dev); 1612 usb_free_device(dev);
1591 return 0; 1613 return 0;
1592} 1614}