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