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