Fri Oct 7 10:32:49 2016 UTC ()
Make the code match the comment wrt port powered status


(skrll)
diff -r1.198.2.31 -r1.198.2.32 src/sys/dev/usb/usb_subr.c

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

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