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