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