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