usb_subr.c revision 1.122 1 1.122 mycroft /* $NetBSD: usb_subr.c,v 1.122 2005/03/04 05:03:19 mycroft 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 * 3. All advertising materials mentioning features or use of this software
21 1.1 augustss * must display the following acknowledgement:
22 1.1 augustss * This product includes software developed by the NetBSD
23 1.1 augustss * Foundation, Inc. and its contributors.
24 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
25 1.1 augustss * contributors may be used to endorse or promote products derived
26 1.1 augustss * from this software without specific prior written permission.
27 1.1 augustss *
28 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
39 1.1 augustss */
40 1.91 lukem
41 1.91 lukem #include <sys/cdefs.h>
42 1.122 mycroft __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.122 2005/03/04 05:03:19 mycroft Exp $");
43 1.104 martin
44 1.104 martin #include "opt_usbverbose.h"
45 1.1 augustss
46 1.1 augustss #include <sys/param.h>
47 1.1 augustss #include <sys/systm.h>
48 1.1 augustss #include <sys/kernel.h>
49 1.1 augustss #include <sys/malloc.h>
50 1.37 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
51 1.1 augustss #include <sys/device.h>
52 1.56 augustss #include <sys/select.h>
53 1.18 augustss #elif defined(__FreeBSD__)
54 1.18 augustss #include <sys/module.h>
55 1.18 augustss #include <sys/bus.h>
56 1.18 augustss #endif
57 1.1 augustss #include <sys/proc.h>
58 1.46 augustss
59 1.46 augustss #include <machine/bus.h>
60 1.1 augustss
61 1.1 augustss #include <dev/usb/usb.h>
62 1.1 augustss
63 1.1 augustss #include <dev/usb/usbdi.h>
64 1.1 augustss #include <dev/usb/usbdi_util.h>
65 1.1 augustss #include <dev/usb/usbdivar.h>
66 1.1 augustss #include <dev/usb/usbdevs.h>
67 1.1 augustss #include <dev/usb/usb_quirks.h>
68 1.1 augustss
69 1.27 augustss #if defined(__FreeBSD__)
70 1.27 augustss #include <machine/clock.h>
71 1.27 augustss #define delay(d) DELAY(d)
72 1.27 augustss #endif
73 1.27 augustss
74 1.1 augustss #ifdef USB_DEBUG
75 1.35 augustss #define DPRINTF(x) if (usbdebug) logprintf x
76 1.35 augustss #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
77 1.1 augustss extern int usbdebug;
78 1.1 augustss #else
79 1.1 augustss #define DPRINTF(x)
80 1.1 augustss #define DPRINTFN(n,x)
81 1.1 augustss #endif
82 1.1 augustss
83 1.82 augustss Static usbd_status usbd_set_config(usbd_device_handle, int);
84 1.112 itojun Static void usbd_devinfo_vp(usbd_device_handle, char *, size_t, char *,
85 1.112 itojun size_t, int);
86 1.78 augustss Static int usbd_getnewaddr(usbd_bus_handle bus);
87 1.56 augustss #if defined(__NetBSD__)
88 1.118 drochner Static int usbd_print(void *, const char *);
89 1.118 drochner Static int usbd_submatch(device_ptr_t, struct cfdata *,
90 1.118 drochner const locdesc_t *, void *);
91 1.37 augustss #elif defined(__OpenBSD__)
92 1.78 augustss Static int usbd_print(void *aux, const char *pnp);
93 1.78 augustss Static int usbd_submatch(device_ptr_t, void *, void *);
94 1.18 augustss #endif
95 1.78 augustss Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
96 1.78 augustss Static void usbd_kill_pipe(usbd_pipe_handle);
97 1.78 augustss Static usbd_status usbd_probe_and_attach(device_ptr_t parent,
98 1.78 augustss usbd_device_handle dev, int port, int addr);
99 1.1 augustss
100 1.69 augustss Static u_int32_t usb_cookie_no = 0;
101 1.27 augustss
102 1.1 augustss #ifdef USBVERBOSE
103 1.1 augustss typedef u_int16_t usb_vendor_id_t;
104 1.1 augustss typedef u_int16_t usb_product_id_t;
105 1.1 augustss
106 1.1 augustss /*
107 1.1 augustss * Descriptions of of known vendors and devices ("products").
108 1.1 augustss */
109 1.122 mycroft struct usb_vendor {
110 1.122 mycroft usb_vendor_id_t vendor;
111 1.122 mycroft char *vendorname;
112 1.122 mycroft };
113 1.122 mycroft struct usb_product {
114 1.1 augustss usb_vendor_id_t vendor;
115 1.1 augustss usb_product_id_t product;
116 1.122 mycroft char *productname;
117 1.1 augustss };
118 1.1 augustss
119 1.1 augustss #include <dev/usb/usbdevs_data.h>
120 1.1 augustss #endif /* USBVERBOSE */
121 1.1 augustss
122 1.84 jdolecek Static const char * const usbd_error_strs[] = {
123 1.12 augustss "NORMAL_COMPLETION",
124 1.12 augustss "IN_PROGRESS",
125 1.12 augustss "PENDING_REQUESTS",
126 1.12 augustss "NOT_STARTED",
127 1.12 augustss "INVAL",
128 1.12 augustss "NOMEM",
129 1.12 augustss "CANCELLED",
130 1.12 augustss "BAD_ADDRESS",
131 1.12 augustss "IN_USE",
132 1.12 augustss "NO_ADDR",
133 1.12 augustss "SET_ADDR_FAILED",
134 1.12 augustss "NO_POWER",
135 1.12 augustss "TOO_DEEP",
136 1.12 augustss "IOERROR",
137 1.12 augustss "NOT_CONFIGURED",
138 1.12 augustss "TIMEOUT",
139 1.12 augustss "SHORT_XFER",
140 1.12 augustss "STALLED",
141 1.33 augustss "INTERRUPTED",
142 1.12 augustss "XXX",
143 1.12 augustss };
144 1.1 augustss
145 1.42 thorpej const char *
146 1.78 augustss usbd_errstr(usbd_status err)
147 1.39 augustss {
148 1.39 augustss static char buffer[5];
149 1.39 augustss
150 1.42 thorpej if (err < USBD_ERROR_MAX) {
151 1.39 augustss return usbd_error_strs[err];
152 1.39 augustss } else {
153 1.39 augustss snprintf(buffer, sizeof buffer, "%d", err);
154 1.39 augustss return buffer;
155 1.39 augustss }
156 1.39 augustss }
157 1.39 augustss
158 1.13 augustss usbd_status
159 1.78 augustss usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
160 1.114 mycroft usb_string_descriptor_t *sdesc, int *sizep)
161 1.13 augustss {
162 1.13 augustss usb_device_request_t req;
163 1.116 mycroft usbd_status err;
164 1.116 mycroft int actlen;
165 1.13 augustss
166 1.13 augustss req.bmRequestType = UT_READ_DEVICE;
167 1.13 augustss req.bRequest = UR_GET_DESCRIPTOR;
168 1.13 augustss USETW2(req.wValue, UDESC_STRING, sindex);
169 1.13 augustss USETW(req.wIndex, langid);
170 1.116 mycroft USETW(req.wLength, 2); /* only size byte first */
171 1.116 mycroft err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
172 1.116 mycroft &actlen, USBD_DEFAULT_TIMEOUT);
173 1.116 mycroft if (err)
174 1.116 mycroft return (err);
175 1.116 mycroft
176 1.116 mycroft if (actlen < 2)
177 1.116 mycroft return (USBD_SHORT_XFER);
178 1.116 mycroft
179 1.116 mycroft USETW(req.wLength, sdesc->bLength); /* the whole string */
180 1.116 mycroft err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
181 1.116 mycroft &actlen, USBD_DEFAULT_TIMEOUT);
182 1.116 mycroft if (err)
183 1.116 mycroft return (err);
184 1.116 mycroft
185 1.116 mycroft if (actlen != sdesc->bLength) {
186 1.116 mycroft DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
187 1.116 mycroft sdesc->bLength, actlen));
188 1.116 mycroft }
189 1.116 mycroft
190 1.116 mycroft *sizep = actlen;
191 1.116 mycroft return (USBD_NORMAL_COMPLETION);
192 1.13 augustss }
193 1.13 augustss
194 1.83 augustss static void
195 1.85 augustss usbd_trim_spaces(char *p)
196 1.83 augustss {
197 1.85 augustss char *q, *e;
198 1.83 augustss
199 1.83 augustss if (p == NULL)
200 1.83 augustss return;
201 1.85 augustss q = e = p;
202 1.85 augustss while (*q == ' ') /* skip leading spaces */
203 1.85 augustss q++;
204 1.85 augustss while ((*p = *q++)) /* copy string */
205 1.85 augustss if (*p++ != ' ') /* remember last non-space */
206 1.85 augustss e = p;
207 1.85 augustss *e = 0; /* kill trailing spaces */
208 1.83 augustss }
209 1.83 augustss
210 1.1 augustss void
211 1.112 itojun usbd_devinfo_vp(usbd_device_handle dev, char *v, size_t lv, char *p, size_t lp,
212 1.112 itojun int usedev)
213 1.1 augustss {
214 1.1 augustss usb_device_descriptor_t *udd = &dev->ddesc;
215 1.108 augustss char *vendor = NULL, *product = NULL;
216 1.2 is #ifdef USBVERBOSE
217 1.122 mycroft int n;
218 1.2 is #endif
219 1.1 augustss
220 1.53 augustss if (dev == NULL) {
221 1.52 augustss v[0] = p[0] = '\0';
222 1.52 augustss return;
223 1.52 augustss }
224 1.52 augustss
225 1.82 augustss if (usedev) {
226 1.119 augustss if (usbd_get_string(dev, udd->iManufacturer, v))
227 1.119 augustss vendor = NULL;
228 1.119 augustss else
229 1.119 augustss vendor = v;
230 1.85 augustss usbd_trim_spaces(vendor);
231 1.119 augustss if (usbd_get_string(dev, udd->iProduct, p))
232 1.119 augustss product = NULL;
233 1.119 augustss else
234 1.119 augustss product = p;
235 1.85 augustss usbd_trim_spaces(product);
236 1.109 augustss if (vendor && !*vendor)
237 1.108 augustss vendor = NULL;
238 1.108 augustss if (product && !*product)
239 1.108 augustss product = NULL;
240 1.82 augustss } else {
241 1.82 augustss vendor = NULL;
242 1.82 augustss product = NULL;
243 1.82 augustss }
244 1.1 augustss #ifdef USBVERBOSE
245 1.122 mycroft if (vendor == NULL) {
246 1.122 mycroft for (n = 0; n < usb_nvendors; n++)
247 1.122 mycroft if (usb_vendors[n].vendor == UGETW(udd->idVendor))
248 1.122 mycroft vendor = usb_vendors[n].vendorname;
249 1.122 mycroft }
250 1.122 mycroft if (product == NULL) {
251 1.122 mycroft for (n = 0; n < usb_nproducts; n++)
252 1.122 mycroft if (usb_products[n].vendor == UGETW(udd->idVendor) &&
253 1.122 mycroft usb_products[n].product == UGETW(udd->idProduct))
254 1.122 mycroft product = usb_products[n].productname;
255 1.1 augustss }
256 1.1 augustss #endif
257 1.80 augustss if (vendor != NULL && *vendor)
258 1.112 itojun strlcpy(v, vendor, lv);
259 1.1 augustss else
260 1.112 itojun snprintf(v, lv, "vendor 0x%04x", UGETW(udd->idVendor));
261 1.80 augustss if (product != NULL && *product)
262 1.112 itojun strlcpy(p, product, lp);
263 1.1 augustss else
264 1.112 itojun snprintf(p, lp, "product 0x%04x", UGETW(udd->idProduct));
265 1.1 augustss }
266 1.1 augustss
267 1.1 augustss int
268 1.113 itojun usbd_printBCD(char *cp, size_t l, int bcd)
269 1.1 augustss {
270 1.113 itojun return (snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff));
271 1.1 augustss }
272 1.1 augustss
273 1.1 augustss void
274 1.113 itojun usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l)
275 1.1 augustss {
276 1.1 augustss usb_device_descriptor_t *udd = &dev->ddesc;
277 1.1 augustss char vendor[USB_MAX_STRING_LEN];
278 1.1 augustss char product[USB_MAX_STRING_LEN];
279 1.1 augustss int bcdDevice, bcdUSB;
280 1.113 itojun char *ep;
281 1.113 itojun
282 1.113 itojun ep = cp + l;
283 1.1 augustss
284 1.112 itojun usbd_devinfo_vp(dev, vendor, sizeof(vendor), product,
285 1.112 itojun sizeof(product), 1);
286 1.113 itojun cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
287 1.1 augustss if (showclass)
288 1.113 itojun cp += snprintf(cp, ep - cp, ", class %d/%d",
289 1.113 itojun udd->bDeviceClass, udd->bDeviceSubClass);
290 1.1 augustss bcdUSB = UGETW(udd->bcdUSB);
291 1.1 augustss bcdDevice = UGETW(udd->bcdDevice);
292 1.113 itojun cp += snprintf(cp, ep - cp, ", rev ");
293 1.113 itojun cp += usbd_printBCD(cp, ep - cp, bcdUSB);
294 1.1 augustss *cp++ = '/';
295 1.113 itojun cp += usbd_printBCD(cp, ep - cp, bcdDevice);
296 1.113 itojun cp += snprintf(cp, ep - cp, ", addr %d", dev->address);
297 1.10 augustss *cp = 0;
298 1.1 augustss }
299 1.1 augustss
300 1.1 augustss /* Delay for a certain number of ms */
301 1.1 augustss void
302 1.78 augustss usb_delay_ms(usbd_bus_handle bus, u_int ms)
303 1.1 augustss {
304 1.1 augustss /* Wait at least two clock ticks so we know the time has passed. */
305 1.64 augustss if (bus->use_polling || cold)
306 1.1 augustss delay((ms+1) * 1000);
307 1.1 augustss else
308 1.1 augustss tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
309 1.1 augustss }
310 1.1 augustss
311 1.23 augustss /* Delay given a device handle. */
312 1.23 augustss void
313 1.78 augustss usbd_delay_ms(usbd_device_handle dev, u_int ms)
314 1.23 augustss {
315 1.23 augustss usb_delay_ms(dev->bus, ms);
316 1.23 augustss }
317 1.23 augustss
318 1.1 augustss usbd_status
319 1.78 augustss usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
320 1.1 augustss {
321 1.1 augustss usb_device_request_t req;
322 1.53 augustss usbd_status err;
323 1.1 augustss int n;
324 1.99 augustss
325 1.1 augustss req.bmRequestType = UT_WRITE_CLASS_OTHER;
326 1.1 augustss req.bRequest = UR_SET_FEATURE;
327 1.1 augustss USETW(req.wValue, UHF_PORT_RESET);
328 1.1 augustss USETW(req.wIndex, port);
329 1.1 augustss USETW(req.wLength, 0);
330 1.53 augustss err = usbd_do_request(dev, &req, 0);
331 1.39 augustss DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
332 1.53 augustss port, usbd_errstr(err)));
333 1.53 augustss if (err)
334 1.53 augustss return (err);
335 1.1 augustss n = 10;
336 1.1 augustss do {
337 1.1 augustss /* Wait for device to recover from reset. */
338 1.23 augustss usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
339 1.53 augustss err = usbd_get_port_status(dev, port, ps);
340 1.53 augustss if (err) {
341 1.53 augustss DPRINTF(("usbd_reset_port: get status failed %d\n",
342 1.53 augustss err));
343 1.53 augustss return (err);
344 1.1 augustss }
345 1.92 augustss /* If the device disappeared, just give up. */
346 1.92 augustss if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
347 1.92 augustss return (USBD_NORMAL_COMPLETION);
348 1.1 augustss } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
349 1.73 augustss if (n == 0)
350 1.73 augustss return (USBD_TIMEOUT);
351 1.53 augustss err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
352 1.1 augustss #ifdef USB_DEBUG
353 1.53 augustss if (err)
354 1.53 augustss DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
355 1.53 augustss err));
356 1.1 augustss #endif
357 1.18 augustss
358 1.18 augustss /* Wait for the device to recover from reset. */
359 1.23 augustss usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
360 1.53 augustss return (err);
361 1.1 augustss }
362 1.1 augustss
363 1.12 augustss usb_interface_descriptor_t *
364 1.78 augustss usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
365 1.12 augustss {
366 1.12 augustss char *p = (char *)cd;
367 1.12 augustss char *end = p + UGETW(cd->wTotalLength);
368 1.12 augustss usb_interface_descriptor_t *d;
369 1.19 augustss int curidx, lastidx, curaidx = 0;
370 1.12 augustss
371 1.19 augustss for (curidx = lastidx = -1; p < end; ) {
372 1.12 augustss d = (usb_interface_descriptor_t *)p;
373 1.18 augustss DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
374 1.99 augustss "type=%d\n",
375 1.18 augustss ifaceidx, curidx, altidx, curaidx,
376 1.18 augustss d->bLength, d->bDescriptorType));
377 1.12 augustss if (d->bLength == 0) /* bad descriptor */
378 1.12 augustss break;
379 1.12 augustss p += d->bLength;
380 1.12 augustss if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
381 1.19 augustss if (d->bInterfaceNumber != lastidx) {
382 1.19 augustss lastidx = d->bInterfaceNumber;
383 1.12 augustss curidx++;
384 1.12 augustss curaidx = 0;
385 1.12 augustss } else
386 1.12 augustss curaidx++;
387 1.12 augustss if (ifaceidx == curidx && altidx == curaidx)
388 1.12 augustss return (d);
389 1.12 augustss }
390 1.12 augustss }
391 1.58 augustss return (NULL);
392 1.12 augustss }
393 1.12 augustss
394 1.12 augustss usb_endpoint_descriptor_t *
395 1.99 augustss usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
396 1.78 augustss int endptidx)
397 1.12 augustss {
398 1.12 augustss char *p = (char *)cd;
399 1.12 augustss char *end = p + UGETW(cd->wTotalLength);
400 1.12 augustss usb_interface_descriptor_t *d;
401 1.12 augustss usb_endpoint_descriptor_t *e;
402 1.12 augustss int curidx;
403 1.12 augustss
404 1.12 augustss d = usbd_find_idesc(cd, ifaceidx, altidx);
405 1.53 augustss if (d == NULL)
406 1.58 augustss return (NULL);
407 1.12 augustss if (endptidx >= d->bNumEndpoints) /* quick exit */
408 1.58 augustss return (NULL);
409 1.12 augustss
410 1.12 augustss curidx = -1;
411 1.12 augustss for (p = (char *)d + d->bLength; p < end; ) {
412 1.12 augustss e = (usb_endpoint_descriptor_t *)p;
413 1.12 augustss if (e->bLength == 0) /* bad descriptor */
414 1.12 augustss break;
415 1.12 augustss p += e->bLength;
416 1.12 augustss if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
417 1.58 augustss return (NULL);
418 1.12 augustss if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
419 1.12 augustss curidx++;
420 1.12 augustss if (curidx == endptidx)
421 1.12 augustss return (e);
422 1.12 augustss }
423 1.1 augustss }
424 1.58 augustss return (NULL);
425 1.1 augustss }
426 1.1 augustss
427 1.1 augustss usbd_status
428 1.78 augustss usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
429 1.1 augustss {
430 1.12 augustss usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
431 1.77 augustss usb_interface_descriptor_t *idesc;
432 1.1 augustss char *p, *end;
433 1.1 augustss int endpt, nendpt;
434 1.1 augustss
435 1.18 augustss DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
436 1.14 drochner ifaceidx, altidx));
437 1.77 augustss idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
438 1.77 augustss if (idesc == NULL)
439 1.77 augustss return (USBD_INVAL);
440 1.1 augustss ifc->device = dev;
441 1.77 augustss ifc->idesc = idesc;
442 1.13 augustss ifc->index = ifaceidx;
443 1.13 augustss ifc->altindex = altidx;
444 1.1 augustss nendpt = ifc->idesc->bNumEndpoints;
445 1.59 augustss DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
446 1.1 augustss if (nendpt != 0) {
447 1.1 augustss ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
448 1.1 augustss M_USB, M_NOWAIT);
449 1.58 augustss if (ifc->endpoints == NULL)
450 1.1 augustss return (USBD_NOMEM);
451 1.1 augustss } else
452 1.58 augustss ifc->endpoints = NULL;
453 1.58 augustss ifc->priv = NULL;
454 1.1 augustss p = (char *)ifc->idesc + ifc->idesc->bLength;
455 1.1 augustss end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
456 1.19 augustss #define ed ((usb_endpoint_descriptor_t *)p)
457 1.1 augustss for (endpt = 0; endpt < nendpt; endpt++) {
458 1.1 augustss DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
459 1.1 augustss for (; p < end; p += ed->bLength) {
460 1.14 drochner DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
461 1.14 drochner "len=%d type=%d\n",
462 1.1 augustss p, end, ed->bLength, ed->bDescriptorType));
463 1.24 augustss if (p + ed->bLength <= end && ed->bLength != 0 &&
464 1.1 augustss ed->bDescriptorType == UDESC_ENDPOINT)
465 1.24 augustss goto found;
466 1.59 augustss if (ed->bLength == 0 ||
467 1.59 augustss ed->bDescriptorType == UDESC_INTERFACE)
468 1.1 augustss break;
469 1.1 augustss }
470 1.24 augustss /* passed end, or bad desc */
471 1.95 augustss printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
472 1.95 augustss ed->bLength == 0 ? "0 length" :
473 1.95 augustss ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
474 1.95 augustss "out of data");
475 1.24 augustss goto bad;
476 1.24 augustss found:
477 1.1 augustss ifc->endpoints[endpt].edesc = ed;
478 1.95 augustss if (dev->speed == USB_SPEED_HIGH) {
479 1.95 augustss u_int mps;
480 1.95 augustss /* Control and bulk endpoints have max packet limits. */
481 1.95 augustss switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
482 1.95 augustss case UE_CONTROL:
483 1.95 augustss mps = USB_2_MAX_CTRL_PACKET;
484 1.95 augustss goto check;
485 1.95 augustss case UE_BULK:
486 1.95 augustss mps = USB_2_MAX_BULK_PACKET;
487 1.95 augustss check:
488 1.95 augustss if (UGETW(ed->wMaxPacketSize) != mps) {
489 1.95 augustss USETW(ed->wMaxPacketSize, mps);
490 1.95 augustss #ifdef DIAGNOSTIC
491 1.95 augustss printf("usbd_fill_iface_data: bad max "
492 1.95 augustss "packet size\n");
493 1.95 augustss #endif
494 1.95 augustss }
495 1.95 augustss break;
496 1.95 augustss default:
497 1.95 augustss break;
498 1.95 augustss }
499 1.95 augustss }
500 1.1 augustss ifc->endpoints[endpt].refcnt = 0;
501 1.24 augustss p += ed->bLength;
502 1.1 augustss }
503 1.19 augustss #undef ed
504 1.1 augustss LIST_INIT(&ifc->pipes);
505 1.1 augustss return (USBD_NORMAL_COMPLETION);
506 1.24 augustss
507 1.1 augustss bad:
508 1.77 augustss if (ifc->endpoints != NULL) {
509 1.59 augustss free(ifc->endpoints, M_USB);
510 1.77 augustss ifc->endpoints = NULL;
511 1.77 augustss }
512 1.24 augustss return (USBD_INVAL);
513 1.1 augustss }
514 1.1 augustss
515 1.1 augustss void
516 1.78 augustss usbd_free_iface_data(usbd_device_handle dev, int ifcno)
517 1.1 augustss {
518 1.1 augustss usbd_interface_handle ifc = &dev->ifaces[ifcno];
519 1.1 augustss if (ifc->endpoints)
520 1.1 augustss free(ifc->endpoints, M_USB);
521 1.1 augustss }
522 1.1 augustss
523 1.69 augustss Static usbd_status
524 1.78 augustss usbd_set_config(usbd_device_handle dev, int conf)
525 1.7 augustss {
526 1.7 augustss usb_device_request_t req;
527 1.7 augustss
528 1.7 augustss req.bmRequestType = UT_WRITE_DEVICE;
529 1.7 augustss req.bRequest = UR_SET_CONFIG;
530 1.7 augustss USETW(req.wValue, conf);
531 1.7 augustss USETW(req.wIndex, 0);
532 1.7 augustss USETW(req.wLength, 0);
533 1.7 augustss return (usbd_do_request(dev, &req, 0));
534 1.7 augustss }
535 1.7 augustss
536 1.1 augustss usbd_status
537 1.78 augustss usbd_set_config_no(usbd_device_handle dev, int no, int msg)
538 1.1 augustss {
539 1.12 augustss int index;
540 1.12 augustss usb_config_descriptor_t cd;
541 1.53 augustss usbd_status err;
542 1.12 augustss
543 1.75 augustss if (no == USB_UNCONFIG_NO)
544 1.75 augustss return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
545 1.75 augustss
546 1.12 augustss DPRINTFN(5,("usbd_set_config_no: %d\n", no));
547 1.12 augustss /* Figure out what config index to use. */
548 1.12 augustss for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
549 1.53 augustss err = usbd_get_config_desc(dev, index, &cd);
550 1.53 augustss if (err)
551 1.53 augustss return (err);
552 1.12 augustss if (cd.bConfigurationValue == no)
553 1.12 augustss return (usbd_set_config_index(dev, index, msg));
554 1.12 augustss }
555 1.12 augustss return (USBD_INVAL);
556 1.12 augustss }
557 1.12 augustss
558 1.12 augustss usbd_status
559 1.78 augustss usbd_set_config_index(usbd_device_handle dev, int index, int msg)
560 1.12 augustss {
561 1.1 augustss usb_status_t ds;
562 1.1 augustss usb_config_descriptor_t cd, *cdp;
563 1.53 augustss usbd_status err;
564 1.103 augustss int i, ifcidx, nifc, len, selfpowered, power;
565 1.1 augustss
566 1.12 augustss DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
567 1.1 augustss
568 1.1 augustss /* XXX check that all interfaces are idle */
569 1.75 augustss if (dev->config != USB_UNCONFIG_NO) {
570 1.12 augustss DPRINTF(("usbd_set_config_index: free old config\n"));
571 1.1 augustss /* Free all configuration data structures. */
572 1.1 augustss nifc = dev->cdesc->bNumInterface;
573 1.12 augustss for (ifcidx = 0; ifcidx < nifc; ifcidx++)
574 1.12 augustss usbd_free_iface_data(dev, ifcidx);
575 1.1 augustss free(dev->ifaces, M_USB);
576 1.1 augustss free(dev->cdesc, M_USB);
577 1.57 augustss dev->ifaces = NULL;
578 1.57 augustss dev->cdesc = NULL;
579 1.75 augustss dev->config = USB_UNCONFIG_NO;
580 1.75 augustss }
581 1.75 augustss
582 1.75 augustss if (index == USB_UNCONFIG_INDEX) {
583 1.75 augustss /* We are unconfiguring the device, so leave unallocated. */
584 1.75 augustss DPRINTF(("usbd_set_config_index: set config 0\n"));
585 1.75 augustss err = usbd_set_config(dev, USB_UNCONFIG_NO);
586 1.75 augustss if (err)
587 1.75 augustss DPRINTF(("usbd_set_config_index: setting config=0 "
588 1.75 augustss "failed, error=%s\n", usbd_errstr(err)));
589 1.75 augustss return (err);
590 1.1 augustss }
591 1.1 augustss
592 1.74 augustss /* Get the short descriptor. */
593 1.53 augustss err = usbd_get_config_desc(dev, index, &cd);
594 1.53 augustss if (err)
595 1.53 augustss return (err);
596 1.1 augustss len = UGETW(cd.wTotalLength);
597 1.1 augustss cdp = malloc(len, M_USB, M_NOWAIT);
598 1.53 augustss if (cdp == NULL)
599 1.1 augustss return (USBD_NOMEM);
600 1.103 augustss
601 1.103 augustss /* Get the full descriptor. Try a few times for slow devices. */
602 1.103 augustss for (i = 0; i < 3; i++) {
603 1.103 augustss err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
604 1.103 augustss if (!err)
605 1.103 augustss break;
606 1.103 augustss usbd_delay_ms(dev, 200);
607 1.103 augustss }
608 1.53 augustss if (err)
609 1.1 augustss goto bad;
610 1.103 augustss
611 1.18 augustss if (cdp->bDescriptorType != UDESC_CONFIG) {
612 1.18 augustss DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
613 1.18 augustss cdp->bDescriptorType));
614 1.53 augustss err = USBD_INVAL;
615 1.18 augustss goto bad;
616 1.18 augustss }
617 1.74 augustss
618 1.74 augustss /* Figure out if the device is self or bus powered. */
619 1.1 augustss selfpowered = 0;
620 1.49 augustss if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
621 1.74 augustss (cdp->bmAttributes & UC_SELF_POWERED)) {
622 1.1 augustss /* May be self powered. */
623 1.1 augustss if (cdp->bmAttributes & UC_BUS_POWERED) {
624 1.1 augustss /* Must ask device. */
625 1.81 augustss if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
626 1.81 augustss /*
627 1.81 augustss * Hub claims to be self powered, but isn't.
628 1.81 augustss * It seems that the power status can be
629 1.81 augustss * determined by the hub characteristics.
630 1.81 augustss */
631 1.81 augustss usb_hub_descriptor_t hd;
632 1.81 augustss usb_device_request_t req;
633 1.81 augustss req.bmRequestType = UT_READ_CLASS_DEVICE;
634 1.81 augustss req.bRequest = UR_GET_DESCRIPTOR;
635 1.81 augustss USETW(req.wValue, 0);
636 1.81 augustss USETW(req.wIndex, 0);
637 1.81 augustss USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
638 1.81 augustss err = usbd_do_request(dev, &req, &hd);
639 1.81 augustss if (!err &&
640 1.81 augustss (UGETW(hd.wHubCharacteristics) &
641 1.81 augustss UHD_PWR_INDIVIDUAL))
642 1.81 augustss selfpowered = 1;
643 1.81 augustss DPRINTF(("usbd_set_config_index: charac=0x%04x"
644 1.81 augustss ", error=%s\n",
645 1.81 augustss UGETW(hd.wHubCharacteristics),
646 1.81 augustss usbd_errstr(err)));
647 1.81 augustss } else {
648 1.81 augustss err = usbd_get_device_status(dev, &ds);
649 1.81 augustss if (!err &&
650 1.81 augustss (UGETW(ds.wStatus) & UDS_SELF_POWERED))
651 1.81 augustss selfpowered = 1;
652 1.81 augustss DPRINTF(("usbd_set_config_index: status=0x%04x"
653 1.81 augustss ", error=%s\n",
654 1.81 augustss UGETW(ds.wStatus), usbd_errstr(err)));
655 1.81 augustss }
656 1.1 augustss } else
657 1.1 augustss selfpowered = 1;
658 1.1 augustss }
659 1.81 augustss DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
660 1.81 augustss "selfpowered=%d, power=%d\n",
661 1.99 augustss cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
662 1.33 augustss selfpowered, cdp->bMaxPower * 2));
663 1.74 augustss
664 1.74 augustss /* Check if we have enough power. */
665 1.1 augustss #ifdef USB_DEBUG
666 1.53 augustss if (dev->powersrc == NULL) {
667 1.44 augustss DPRINTF(("usbd_set_config_index: No power source?\n"));
668 1.13 augustss return (USBD_IOERROR);
669 1.1 augustss }
670 1.1 augustss #endif
671 1.1 augustss power = cdp->bMaxPower * 2;
672 1.1 augustss if (power > dev->powersrc->power) {
673 1.81 augustss DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
674 1.1 augustss /* XXX print nicer message. */
675 1.7 augustss if (msg)
676 1.18 augustss printf("%s: device addr %d (config %d) exceeds power "
677 1.18 augustss "budget, %d mA > %d mA\n",
678 1.99 augustss USBDEVNAME(dev->bus->bdev), dev->address,
679 1.99 augustss cdp->bConfigurationValue,
680 1.7 augustss power, dev->powersrc->power);
681 1.53 augustss err = USBD_NO_POWER;
682 1.1 augustss goto bad;
683 1.1 augustss }
684 1.1 augustss dev->power = power;
685 1.1 augustss dev->self_powered = selfpowered;
686 1.1 augustss
687 1.74 augustss /* Set the actual configuration value. */
688 1.18 augustss DPRINTF(("usbd_set_config_index: set config %d\n",
689 1.18 augustss cdp->bConfigurationValue));
690 1.53 augustss err = usbd_set_config(dev, cdp->bConfigurationValue);
691 1.53 augustss if (err) {
692 1.14 drochner DPRINTF(("usbd_set_config_index: setting config=%d failed, "
693 1.39 augustss "error=%s\n",
694 1.53 augustss cdp->bConfigurationValue, usbd_errstr(err)));
695 1.1 augustss goto bad;
696 1.1 augustss }
697 1.74 augustss
698 1.74 augustss /* Allocate and fill interface data. */
699 1.1 augustss nifc = cdp->bNumInterface;
700 1.99 augustss dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
701 1.1 augustss M_USB, M_NOWAIT);
702 1.58 augustss if (dev->ifaces == NULL) {
703 1.53 augustss err = USBD_NOMEM;
704 1.1 augustss goto bad;
705 1.1 augustss }
706 1.12 augustss DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
707 1.1 augustss dev->cdesc = cdp;
708 1.1 augustss dev->config = cdp->bConfigurationValue;
709 1.12 augustss for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
710 1.53 augustss err = usbd_fill_iface_data(dev, ifcidx, 0);
711 1.53 augustss if (err) {
712 1.12 augustss while (--ifcidx >= 0)
713 1.12 augustss usbd_free_iface_data(dev, ifcidx);
714 1.1 augustss goto bad;
715 1.1 augustss }
716 1.1 augustss }
717 1.1 augustss
718 1.1 augustss return (USBD_NORMAL_COMPLETION);
719 1.1 augustss
720 1.1 augustss bad:
721 1.1 augustss free(cdp, M_USB);
722 1.53 augustss return (err);
723 1.1 augustss }
724 1.1 augustss
725 1.1 augustss /* XXX add function for alternate settings */
726 1.1 augustss
727 1.1 augustss usbd_status
728 1.78 augustss usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
729 1.78 augustss struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
730 1.1 augustss {
731 1.1 augustss usbd_pipe_handle p;
732 1.53 augustss usbd_status err;
733 1.1 augustss
734 1.1 augustss DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
735 1.1 augustss dev, iface, ep, pipe));
736 1.1 augustss p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
737 1.53 augustss if (p == NULL)
738 1.1 augustss return (USBD_NOMEM);
739 1.1 augustss p->device = dev;
740 1.1 augustss p->iface = iface;
741 1.1 augustss p->endpoint = ep;
742 1.1 augustss ep->refcnt++;
743 1.1 augustss p->refcnt = 1;
744 1.53 augustss p->intrxfer = 0;
745 1.1 augustss p->running = 0;
746 1.70 augustss p->aborting = 0;
747 1.34 augustss p->repeat = 0;
748 1.63 augustss p->interval = ival;
749 1.1 augustss SIMPLEQ_INIT(&p->queue);
750 1.53 augustss err = dev->bus->methods->open_pipe(p);
751 1.53 augustss if (err) {
752 1.39 augustss DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
753 1.39 augustss "%s\n",
754 1.53 augustss ep->edesc->bEndpointAddress, usbd_errstr(err)));
755 1.1 augustss free(p, M_USB);
756 1.53 augustss return (err);
757 1.1 augustss }
758 1.38 augustss /* Clear any stall and make sure DATA0 toggle will be used next. */
759 1.105 mycroft if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT) {
760 1.105 mycroft err = usbd_clear_endpoint_stall(p);
761 1.106 mycroft /* Some devices reject this command, so ignore a STALL. */
762 1.106 mycroft if (err && err != USBD_STALLED) {
763 1.105 mycroft printf("usbd_setup_pipe: failed to start endpoint, %s\n", usbd_errstr(err));
764 1.105 mycroft return (err);
765 1.105 mycroft }
766 1.105 mycroft }
767 1.1 augustss *pipe = p;
768 1.1 augustss return (USBD_NORMAL_COMPLETION);
769 1.1 augustss }
770 1.1 augustss
771 1.1 augustss /* Abort the device control pipe. */
772 1.1 augustss void
773 1.78 augustss usbd_kill_pipe(usbd_pipe_handle pipe)
774 1.1 augustss {
775 1.89 augustss usbd_abort_pipe(pipe);
776 1.1 augustss pipe->methods->close(pipe);
777 1.1 augustss pipe->endpoint->refcnt--;
778 1.1 augustss free(pipe, M_USB);
779 1.1 augustss }
780 1.1 augustss
781 1.1 augustss int
782 1.78 augustss usbd_getnewaddr(usbd_bus_handle bus)
783 1.1 augustss {
784 1.18 augustss int addr;
785 1.1 augustss
786 1.18 augustss for (addr = 1; addr < USB_MAX_DEVICES; addr++)
787 1.18 augustss if (bus->devices[addr] == 0)
788 1.18 augustss return (addr);
789 1.1 augustss return (-1);
790 1.1 augustss }
791 1.1 augustss
792 1.18 augustss
793 1.18 augustss usbd_status
794 1.78 augustss usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
795 1.78 augustss int port, int addr)
796 1.18 augustss {
797 1.18 augustss struct usb_attach_arg uaa;
798 1.18 augustss usb_device_descriptor_t *dd = &dev->ddesc;
799 1.53 augustss int found, i, confi, nifaces;
800 1.53 augustss usbd_status err;
801 1.43 augustss device_ptr_t dv;
802 1.20 augustss usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
803 1.18 augustss
804 1.18 augustss #if defined(__FreeBSD__)
805 1.99 augustss /*
806 1.28 augustss * XXX uaa is a static var. Not a problem as it _should_ be used only
807 1.28 augustss * during probe and attach. Should be changed however.
808 1.28 augustss */
809 1.43 augustss device_t bdev;
810 1.52 augustss bdev = device_add_child(parent, NULL, -1, &uaa);
811 1.26 augustss if (!bdev) {
812 1.18 augustss printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
813 1.18 augustss return (USBD_INVAL);
814 1.18 augustss }
815 1.52 augustss device_quiet(bdev);
816 1.18 augustss #endif
817 1.18 augustss
818 1.18 augustss uaa.device = dev;
819 1.72 augustss uaa.iface = NULL;
820 1.72 augustss uaa.ifaces = NULL;
821 1.20 augustss uaa.nifaces = 0;
822 1.18 augustss uaa.usegeneric = 0;
823 1.18 augustss uaa.port = port;
824 1.18 augustss uaa.configno = UHUB_UNK_CONFIGURATION;
825 1.18 augustss uaa.ifaceno = UHUB_UNK_INTERFACE;
826 1.32 augustss uaa.vendor = UGETW(dd->idVendor);
827 1.32 augustss uaa.product = UGETW(dd->idProduct);
828 1.32 augustss uaa.release = UGETW(dd->bcdDevice);
829 1.18 augustss
830 1.18 augustss /* First try with device specific drivers. */
831 1.60 augustss DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
832 1.34 augustss dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
833 1.34 augustss if (dv) {
834 1.34 augustss dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
835 1.57 augustss if (dev->subdevs == NULL)
836 1.34 augustss return (USBD_NOMEM);
837 1.34 augustss dev->subdevs[0] = dv;
838 1.34 augustss dev->subdevs[1] = 0;
839 1.18 augustss return (USBD_NORMAL_COMPLETION);
840 1.34 augustss }
841 1.18 augustss
842 1.18 augustss DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
843 1.18 augustss
844 1.60 augustss DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
845 1.60 augustss dd->bNumConfigurations));
846 1.18 augustss /* Next try with interface drivers. */
847 1.18 augustss for (confi = 0; confi < dd->bNumConfigurations; confi++) {
848 1.18 augustss DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
849 1.18 augustss confi));
850 1.53 augustss err = usbd_set_config_index(dev, confi, 1);
851 1.53 augustss if (err) {
852 1.18 augustss #ifdef USB_DEBUG
853 1.18 augustss DPRINTF(("%s: port %d, set config at addr %d failed, "
854 1.43 augustss "error=%s\n", USBDEVPTRNAME(parent), port,
855 1.53 augustss addr, usbd_errstr(err)));
856 1.18 augustss #else
857 1.18 augustss printf("%s: port %d, set config at addr %d failed\n",
858 1.43 augustss USBDEVPTRNAME(parent), port, addr);
859 1.18 augustss #endif
860 1.27 augustss #if defined(__FreeBSD__)
861 1.52 augustss device_delete_child(parent, bdev);
862 1.27 augustss #endif
863 1.52 augustss
864 1.53 augustss return (err);
865 1.18 augustss }
866 1.20 augustss nifaces = dev->cdesc->bNumInterface;
867 1.23 augustss uaa.configno = dev->cdesc->bConfigurationValue;
868 1.20 augustss for (i = 0; i < nifaces; i++)
869 1.20 augustss ifaces[i] = &dev->ifaces[i];
870 1.20 augustss uaa.ifaces = ifaces;
871 1.20 augustss uaa.nifaces = nifaces;
872 1.34 augustss dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
873 1.57 augustss if (dev->subdevs == NULL) {
874 1.52 augustss #if defined(__FreeBSD__)
875 1.52 augustss device_delete_child(parent, bdev);
876 1.52 augustss #endif
877 1.34 augustss return (USBD_NOMEM);
878 1.52 augustss }
879 1.52 augustss
880 1.52 augustss found = 0;
881 1.52 augustss for (i = 0; i < nifaces; i++) {
882 1.53 augustss if (ifaces[i] == NULL)
883 1.20 augustss continue; /* interface already claimed */
884 1.20 augustss uaa.iface = ifaces[i];
885 1.20 augustss uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
886 1.34 augustss dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
887 1.34 augustss usbd_submatch);
888 1.53 augustss if (dv != NULL) {
889 1.34 augustss dev->subdevs[found++] = dv;
890 1.34 augustss dev->subdevs[found] = 0;
891 1.20 augustss ifaces[i] = 0; /* consumed */
892 1.52 augustss
893 1.52 augustss #if defined(__FreeBSD__)
894 1.52 augustss /* create another child for the next iface */
895 1.52 augustss bdev = device_add_child(parent, NULL, -1,&uaa);
896 1.52 augustss if (!bdev) {
897 1.52 augustss printf("%s: Device creation failed\n",
898 1.52 augustss USBDEVNAME(dev->bus->bdev));
899 1.52 augustss return (USBD_NORMAL_COMPLETION);
900 1.52 augustss }
901 1.52 augustss device_quiet(bdev);
902 1.52 augustss #endif
903 1.20 augustss }
904 1.18 augustss }
905 1.52 augustss if (found != 0) {
906 1.52 augustss #if defined(__FreeBSD__)
907 1.52 augustss /* remove the last created child again; it is unused */
908 1.52 augustss device_delete_child(parent, bdev);
909 1.52 augustss #endif
910 1.18 augustss return (USBD_NORMAL_COMPLETION);
911 1.52 augustss }
912 1.34 augustss free(dev->subdevs, M_USB);
913 1.34 augustss dev->subdevs = 0;
914 1.18 augustss }
915 1.21 augustss /* No interfaces were attached in any of the configurations. */
916 1.34 augustss
917 1.34 augustss if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
918 1.18 augustss usbd_set_config_index(dev, 0, 0);
919 1.18 augustss
920 1.18 augustss DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
921 1.18 augustss
922 1.18 augustss /* Finally try the generic driver. */
923 1.72 augustss uaa.iface = NULL;
924 1.18 augustss uaa.usegeneric = 1;
925 1.18 augustss uaa.configno = UHUB_UNK_CONFIGURATION;
926 1.18 augustss uaa.ifaceno = UHUB_UNK_INTERFACE;
927 1.34 augustss dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
928 1.53 augustss if (dv != NULL) {
929 1.34 augustss dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
930 1.34 augustss if (dev->subdevs == 0)
931 1.34 augustss return (USBD_NOMEM);
932 1.34 augustss dev->subdevs[0] = dv;
933 1.34 augustss dev->subdevs[1] = 0;
934 1.18 augustss return (USBD_NORMAL_COMPLETION);
935 1.34 augustss }
936 1.18 augustss
937 1.99 augustss /*
938 1.18 augustss * The generic attach failed, but leave the device as it is.
939 1.18 augustss * We just did not find any drivers, that's all. The device is
940 1.18 augustss * fully operational and not harming anyone.
941 1.18 augustss */
942 1.18 augustss DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
943 1.27 augustss #if defined(__FreeBSD__)
944 1.52 augustss device_delete_child(parent, bdev);
945 1.27 augustss #endif
946 1.27 augustss return (USBD_NORMAL_COMPLETION);
947 1.18 augustss }
948 1.18 augustss
949 1.18 augustss
950 1.1 augustss /*
951 1.1 augustss * Called when a new device has been put in the powered state,
952 1.1 augustss * but not yet in the addressed state.
953 1.1 augustss * Get initial descriptor, set the address, get full descriptor,
954 1.1 augustss * and attach a driver.
955 1.1 augustss */
956 1.1 augustss usbd_status
957 1.78 augustss usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
958 1.94 augustss int speed, int port, struct usbd_port *up)
959 1.1 augustss {
960 1.120 augustss usbd_device_handle dev, adev;
961 1.94 augustss struct usbd_device *hub;
962 1.18 augustss usb_device_descriptor_t *dd;
963 1.107 augustss usb_port_status_t ps;
964 1.53 augustss usbd_status err;
965 1.1 augustss int addr;
966 1.18 augustss int i;
967 1.120 augustss int p;
968 1.1 augustss
969 1.94 augustss DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
970 1.94 augustss bus, port, depth, speed));
971 1.1 augustss addr = usbd_getnewaddr(bus);
972 1.1 augustss if (addr < 0) {
973 1.99 augustss printf("%s: No free USB addresses, new device ignored.\n",
974 1.18 augustss USBDEVNAME(bus->bdev));
975 1.1 augustss return (USBD_NO_ADDR);
976 1.1 augustss }
977 1.1 augustss
978 1.97 tsutsui dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
979 1.53 augustss if (dev == NULL)
980 1.1 augustss return (USBD_NOMEM);
981 1.1 augustss
982 1.1 augustss dev->bus = bus;
983 1.1 augustss
984 1.1 augustss /* Set up default endpoint handle. */
985 1.1 augustss dev->def_ep.edesc = &dev->def_ep_desc;
986 1.1 augustss
987 1.1 augustss /* Set up default endpoint descriptor. */
988 1.1 augustss dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
989 1.1 augustss dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
990 1.1 augustss dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
991 1.1 augustss dev->def_ep_desc.bmAttributes = UE_CONTROL;
992 1.1 augustss USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
993 1.1 augustss dev->def_ep_desc.bInterval = 0;
994 1.1 augustss
995 1.1 augustss dev->quirks = &usbd_no_quirk;
996 1.1 augustss dev->address = USB_START_ADDR;
997 1.4 augustss dev->ddesc.bMaxPacketSize = 0;
998 1.1 augustss dev->depth = depth;
999 1.1 augustss dev->powersrc = up;
1000 1.94 augustss dev->myhub = up->parent;
1001 1.120 augustss
1002 1.120 augustss up->device = dev;
1003 1.120 augustss
1004 1.120 augustss /* Locate port on upstream high speed hub */
1005 1.120 augustss for (adev = dev, hub = up->parent;
1006 1.94 augustss hub != NULL && hub->speed != USB_SPEED_HIGH;
1007 1.120 augustss adev = hub, hub = hub->myhub)
1008 1.94 augustss ;
1009 1.120 augustss if (hub) {
1010 1.120 augustss for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1011 1.120 augustss if (hub->hub->ports[p].device == adev) {
1012 1.120 augustss dev->myhsport = &hub->hub->ports[p];
1013 1.120 augustss goto found;
1014 1.120 augustss }
1015 1.120 augustss }
1016 1.120 augustss panic("usbd_new_device: cannot find HS port\n");
1017 1.120 augustss found:
1018 1.120 augustss DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1019 1.120 augustss } else {
1020 1.120 augustss dev->myhsport = NULL;
1021 1.120 augustss }
1022 1.94 augustss dev->speed = speed;
1023 1.22 augustss dev->langid = USBD_NOLANG;
1024 1.50 augustss dev->cookie.cookie = ++usb_cookie_no;
1025 1.1 augustss
1026 1.67 soren /* Establish the default pipe. */
1027 1.63 augustss err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1028 1.63 augustss &dev->default_pipe);
1029 1.53 augustss if (err) {
1030 1.18 augustss usbd_remove_device(dev, up);
1031 1.53 augustss return (err);
1032 1.18 augustss }
1033 1.1 augustss
1034 1.111 augustss /* Set the address. Do this early; some devices need that. */
1035 1.111 augustss err = usbd_set_address(dev, addr);
1036 1.111 augustss DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1037 1.111 augustss if (err) {
1038 1.111 augustss DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1039 1.111 augustss err = USBD_SET_ADDR_FAILED;
1040 1.111 augustss usbd_remove_device(dev, up);
1041 1.111 augustss return (err);
1042 1.111 augustss }
1043 1.111 augustss /* Allow device time to set new address */
1044 1.111 augustss usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1045 1.111 augustss dev->address = addr; /* New device address now */
1046 1.111 augustss bus->devices[addr] = dev;
1047 1.111 augustss
1048 1.18 augustss dd = &dev->ddesc;
1049 1.6 augustss /* Try a few times in case the device is slow (i.e. outside specs.) */
1050 1.107 augustss for (i = 0; i < 10; i++) {
1051 1.6 augustss /* Get the first 8 bytes of the device descriptor. */
1052 1.53 augustss err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1053 1.53 augustss if (!err)
1054 1.6 augustss break;
1055 1.23 augustss usbd_delay_ms(dev, 200);
1056 1.107 augustss if ((i & 3) == 3)
1057 1.107 augustss usbd_reset_port(up->parent, port, &ps);
1058 1.6 augustss }
1059 1.53 augustss if (err) {
1060 1.14 drochner DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1061 1.53 augustss "failed\n", addr));
1062 1.18 augustss usbd_remove_device(dev, up);
1063 1.53 augustss return (err);
1064 1.95 augustss }
1065 1.95 augustss
1066 1.95 augustss if (speed == USB_SPEED_HIGH) {
1067 1.95 augustss /* Max packet size must be 64 (sec 5.5.3). */
1068 1.95 augustss if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1069 1.95 augustss #ifdef DIAGNOSTIC
1070 1.95 augustss printf("usbd_new_device: addr=%d bad max packet size\n",
1071 1.95 augustss addr);
1072 1.95 augustss #endif
1073 1.95 augustss dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1074 1.95 augustss }
1075 1.18 augustss }
1076 1.18 augustss
1077 1.39 augustss DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1078 1.99 augustss "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1079 1.39 augustss addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1080 1.99 augustss dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1081 1.94 augustss dev->speed));
1082 1.39 augustss
1083 1.18 augustss if (dd->bDescriptorType != UDESC_DEVICE) {
1084 1.18 augustss /* Illegal device descriptor */
1085 1.18 augustss DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1086 1.18 augustss dd->bDescriptorType));
1087 1.18 augustss usbd_remove_device(dev, up);
1088 1.18 augustss return (USBD_INVAL);
1089 1.1 augustss }
1090 1.1 augustss
1091 1.39 augustss if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1092 1.39 augustss DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1093 1.39 augustss usbd_remove_device(dev, up);
1094 1.39 augustss return (USBD_INVAL);
1095 1.39 augustss }
1096 1.4 augustss
1097 1.18 augustss USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1098 1.1 augustss
1099 1.62 augustss err = usbd_reload_device_desc(dev);
1100 1.53 augustss if (err) {
1101 1.14 drochner DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1102 1.14 drochner "failed\n", addr));
1103 1.18 augustss usbd_remove_device(dev, up);
1104 1.53 augustss return (err);
1105 1.1 augustss }
1106 1.1 augustss
1107 1.1 augustss /* Assume 100mA bus powered for now. Changed when configured. */
1108 1.1 augustss dev->power = USB_MIN_POWER;
1109 1.1 augustss dev->self_powered = 0;
1110 1.1 augustss
1111 1.99 augustss DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1112 1.1 augustss addr, dev, parent));
1113 1.99 augustss
1114 1.65 augustss usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1115 1.1 augustss
1116 1.53 augustss err = usbd_probe_and_attach(parent, dev, port, addr);
1117 1.53 augustss if (err) {
1118 1.18 augustss usbd_remove_device(dev, up);
1119 1.53 augustss return (err);
1120 1.18 augustss }
1121 1.65 augustss
1122 1.18 augustss return (USBD_NORMAL_COMPLETION);
1123 1.62 augustss }
1124 1.62 augustss
1125 1.62 augustss usbd_status
1126 1.78 augustss usbd_reload_device_desc(usbd_device_handle dev)
1127 1.62 augustss {
1128 1.62 augustss usbd_status err;
1129 1.62 augustss
1130 1.62 augustss /* Get the full device descriptor. */
1131 1.62 augustss err = usbd_get_device_desc(dev, &dev->ddesc);
1132 1.62 augustss if (err)
1133 1.62 augustss return (err);
1134 1.62 augustss
1135 1.62 augustss /* Figure out what's wrong with this device. */
1136 1.62 augustss dev->quirks = usbd_find_quirk(&dev->ddesc);
1137 1.62 augustss
1138 1.62 augustss return (USBD_NORMAL_COMPLETION);
1139 1.18 augustss }
1140 1.1 augustss
1141 1.18 augustss void
1142 1.78 augustss usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1143 1.18 augustss {
1144 1.18 augustss DPRINTF(("usbd_remove_device: %p\n", dev));
1145 1.99 augustss
1146 1.53 augustss if (dev->default_pipe != NULL)
1147 1.18 augustss usbd_kill_pipe(dev->default_pipe);
1148 1.120 augustss up->device = NULL;
1149 1.120 augustss dev->bus->devices[dev->address] = NULL;
1150 1.1 augustss
1151 1.1 augustss free(dev, M_USB);
1152 1.1 augustss }
1153 1.1 augustss
1154 1.37 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
1155 1.1 augustss int
1156 1.78 augustss usbd_print(void *aux, const char *pnp)
1157 1.1 augustss {
1158 1.1 augustss struct usb_attach_arg *uaa = aux;
1159 1.1 augustss char devinfo[1024];
1160 1.1 augustss
1161 1.1 augustss DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1162 1.1 augustss if (pnp) {
1163 1.1 augustss if (!uaa->usegeneric)
1164 1.1 augustss return (QUIET);
1165 1.113 itojun usbd_devinfo(uaa->device, 1, devinfo, sizeof(devinfo));
1166 1.101 thorpej aprint_normal("%s, %s", devinfo, pnp);
1167 1.1 augustss }
1168 1.1 augustss if (uaa->port != 0)
1169 1.101 thorpej aprint_normal(" port %d", uaa->port);
1170 1.11 augustss if (uaa->configno != UHUB_UNK_CONFIGURATION)
1171 1.101 thorpej aprint_normal(" configuration %d", uaa->configno);
1172 1.11 augustss if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1173 1.101 thorpej aprint_normal(" interface %d", uaa->ifaceno);
1174 1.32 augustss #if 0
1175 1.99 augustss /*
1176 1.32 augustss * It gets very crowded with these locators on the attach line.
1177 1.32 augustss * They are not really needed since they are printed in the clear
1178 1.32 augustss * by each driver.
1179 1.32 augustss */
1180 1.32 augustss if (uaa->vendor != UHUB_UNK_VENDOR)
1181 1.101 thorpej aprint_normal(" vendor 0x%04x", uaa->vendor);
1182 1.32 augustss if (uaa->product != UHUB_UNK_PRODUCT)
1183 1.101 thorpej aprint_normal(" product 0x%04x", uaa->product);
1184 1.32 augustss if (uaa->release != UHUB_UNK_RELEASE)
1185 1.101 thorpej aprint_normal(" release 0x%04x", uaa->release);
1186 1.32 augustss #endif
1187 1.1 augustss return (UNCONF);
1188 1.1 augustss }
1189 1.1 augustss
1190 1.37 augustss #if defined(__NetBSD__)
1191 1.1 augustss int
1192 1.118 drochner usbd_submatch(struct device *parent, struct cfdata *cf,
1193 1.118 drochner const locdesc_t *ldesc, void *aux)
1194 1.1 augustss {
1195 1.37 augustss #elif defined(__OpenBSD__)
1196 1.37 augustss int
1197 1.78 augustss usbd_submatch(struct device *parent, void *match, void *aux)
1198 1.37 augustss {
1199 1.37 augustss struct cfdata *cf = match;
1200 1.37 augustss #endif
1201 1.1 augustss struct usb_attach_arg *uaa = aux;
1202 1.1 augustss
1203 1.72 augustss DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1204 1.72 augustss "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1205 1.72 augustss uaa->port, cf->uhubcf_port,
1206 1.72 augustss uaa->configno, cf->uhubcf_configuration,
1207 1.72 augustss uaa->ifaceno, cf->uhubcf_interface,
1208 1.72 augustss uaa->vendor, cf->uhubcf_vendor,
1209 1.72 augustss uaa->product, cf->uhubcf_product,
1210 1.72 augustss uaa->release, cf->uhubcf_release));
1211 1.72 augustss if (uaa->port != 0 && /* root hub has port 0, it should match */
1212 1.117 drochner ((cf->uhubcf_port != UHUB_UNK_PORT &&
1213 1.72 augustss cf->uhubcf_port != uaa->port) ||
1214 1.72 augustss (uaa->configno != UHUB_UNK_CONFIGURATION &&
1215 1.72 augustss cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1216 1.72 augustss cf->uhubcf_configuration != uaa->configno) ||
1217 1.72 augustss (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1218 1.72 augustss cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1219 1.72 augustss cf->uhubcf_interface != uaa->ifaceno) ||
1220 1.72 augustss (uaa->vendor != UHUB_UNK_VENDOR &&
1221 1.72 augustss cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1222 1.72 augustss cf->uhubcf_vendor != uaa->vendor) ||
1223 1.72 augustss (uaa->product != UHUB_UNK_PRODUCT &&
1224 1.72 augustss cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1225 1.72 augustss cf->uhubcf_product != uaa->product) ||
1226 1.72 augustss (uaa->release != UHUB_UNK_RELEASE &&
1227 1.72 augustss cf->uhubcf_release != UHUB_UNK_RELEASE &&
1228 1.72 augustss cf->uhubcf_release != uaa->release)
1229 1.72 augustss )
1230 1.32 augustss )
1231 1.1 augustss return 0;
1232 1.87 augustss if (cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1233 1.87 augustss cf->uhubcf_vendor == uaa->vendor &&
1234 1.87 augustss cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1235 1.87 augustss cf->uhubcf_product == uaa->product) {
1236 1.87 augustss /* We have a vendor&product locator match */
1237 1.87 augustss if (cf->uhubcf_release != UHUB_UNK_RELEASE &&
1238 1.87 augustss cf->uhubcf_release == uaa->release)
1239 1.87 augustss uaa->matchlvl = UMATCH_VENDOR_PRODUCT_REV;
1240 1.87 augustss else
1241 1.87 augustss uaa->matchlvl = UMATCH_VENDOR_PRODUCT;
1242 1.87 augustss } else
1243 1.87 augustss uaa->matchlvl = 0;
1244 1.100 thorpej return (config_match(parent, cf, aux));
1245 1.13 augustss }
1246 1.18 augustss
1247 1.18 augustss #endif
1248 1.13 augustss
1249 1.13 augustss void
1250 1.82 augustss usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1251 1.82 augustss int usedev)
1252 1.13 augustss {
1253 1.13 augustss struct usbd_port *p;
1254 1.56 augustss int i, err, s;
1255 1.13 augustss
1256 1.98 christos di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1257 1.98 christos di->udi_addr = dev->address;
1258 1.98 christos di->udi_cookie = dev->cookie;
1259 1.112 itojun usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1260 1.112 itojun di->udi_product, sizeof(di->udi_product), usedev);
1261 1.113 itojun usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1262 1.113 itojun UGETW(dev->ddesc.bcdDevice));
1263 1.98 christos di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1264 1.98 christos di->udi_productNo = UGETW(dev->ddesc.idProduct);
1265 1.98 christos di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1266 1.98 christos di->udi_class = dev->ddesc.bDeviceClass;
1267 1.98 christos di->udi_subclass = dev->ddesc.bDeviceSubClass;
1268 1.98 christos di->udi_protocol = dev->ddesc.bDeviceProtocol;
1269 1.98 christos di->udi_config = dev->config;
1270 1.98 christos di->udi_power = dev->self_powered ? 0 : dev->power;
1271 1.98 christos di->udi_speed = dev->speed;
1272 1.65 augustss
1273 1.65 augustss if (dev->subdevs != NULL) {
1274 1.65 augustss for (i = 0; dev->subdevs[i] &&
1275 1.65 augustss i < USB_MAX_DEVNAMES; i++) {
1276 1.98 christos strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1277 1.65 augustss USB_MAX_DEVNAMELEN);
1278 1.98 christos di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1279 1.65 augustss }
1280 1.65 augustss } else {
1281 1.65 augustss i = 0;
1282 1.65 augustss }
1283 1.65 augustss for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1284 1.98 christos di->udi_devnames[i][0] = 0; /* empty */
1285 1.65 augustss
1286 1.13 augustss if (dev->hub) {
1287 1.99 augustss for (i = 0;
1288 1.98 christos i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1289 1.13 augustss i < dev->hub->hubdesc.bNbrPorts;
1290 1.13 augustss i++) {
1291 1.13 augustss p = &dev->hub->ports[i];
1292 1.13 augustss if (p->device)
1293 1.56 augustss err = p->device->address;
1294 1.13 augustss else {
1295 1.13 augustss s = UGETW(p->status.wPortStatus);
1296 1.13 augustss if (s & UPS_PORT_ENABLED)
1297 1.56 augustss err = USB_PORT_ENABLED;
1298 1.13 augustss else if (s & UPS_SUSPEND)
1299 1.56 augustss err = USB_PORT_SUSPENDED;
1300 1.13 augustss else if (s & UPS_PORT_POWER)
1301 1.56 augustss err = USB_PORT_POWERED;
1302 1.13 augustss else
1303 1.56 augustss err = USB_PORT_DISABLED;
1304 1.13 augustss }
1305 1.98 christos di->udi_ports[i] = err;
1306 1.13 augustss }
1307 1.98 christos di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1308 1.13 augustss } else
1309 1.98 christos di->udi_nports = 0;
1310 1.34 augustss }
1311 1.34 augustss
1312 1.34 augustss void
1313 1.78 augustss usb_free_device(usbd_device_handle dev)
1314 1.34 augustss {
1315 1.34 augustss int ifcidx, nifc;
1316 1.34 augustss
1317 1.53 augustss if (dev->default_pipe != NULL)
1318 1.34 augustss usbd_kill_pipe(dev->default_pipe);
1319 1.53 augustss if (dev->ifaces != NULL) {
1320 1.34 augustss nifc = dev->cdesc->bNumInterface;
1321 1.34 augustss for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1322 1.34 augustss usbd_free_iface_data(dev, ifcidx);
1323 1.34 augustss free(dev->ifaces, M_USB);
1324 1.34 augustss }
1325 1.53 augustss if (dev->cdesc != NULL)
1326 1.34 augustss free(dev->cdesc, M_USB);
1327 1.53 augustss if (dev->subdevs != NULL)
1328 1.34 augustss free(dev->subdevs, M_USB);
1329 1.34 augustss free(dev, M_USB);
1330 1.1 augustss }
1331 1.47 augustss
1332 1.47 augustss /*
1333 1.47 augustss * The general mechanism for detaching drivers works as follows: Each
1334 1.47 augustss * driver is responsible for maintaining a reference count on the
1335 1.47 augustss * number of outstanding references to its softc (e.g. from
1336 1.47 augustss * processing hanging in a read or write). The detach method of the
1337 1.47 augustss * driver decrements this counter and flags in the softc that the
1338 1.47 augustss * driver is dying and then wakes any sleepers. It then sleeps on the
1339 1.47 augustss * softc. Each place that can sleep must maintain the reference
1340 1.47 augustss * count. When the reference count drops to -1 (0 is the normal value
1341 1.47 augustss * of the reference count) the a wakeup on the softc is performed
1342 1.47 augustss * signaling to the detach waiter that all references are gone.
1343 1.47 augustss */
1344 1.47 augustss
1345 1.47 augustss /*
1346 1.47 augustss * Called from process context when we discover that a port has
1347 1.47 augustss * been disconnected.
1348 1.47 augustss */
1349 1.47 augustss void
1350 1.78 augustss usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1351 1.47 augustss {
1352 1.47 augustss usbd_device_handle dev = up->device;
1353 1.51 augustss char *hubname = USBDEVPTRNAME(parent);
1354 1.47 augustss int i;
1355 1.47 augustss
1356 1.99 augustss DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1357 1.47 augustss up, dev, up->portno));
1358 1.48 augustss
1359 1.48 augustss #ifdef DIAGNOSTIC
1360 1.57 augustss if (dev == NULL) {
1361 1.48 augustss printf("usb_disconnect_port: no device\n");
1362 1.48 augustss return;
1363 1.48 augustss }
1364 1.48 augustss #endif
1365 1.47 augustss
1366 1.55 augustss if (dev->subdevs != NULL) {
1367 1.57 augustss DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1368 1.47 augustss for (i = 0; dev->subdevs[i]; i++) {
1369 1.99 augustss printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1370 1.51 augustss hubname);
1371 1.51 augustss if (up->portno != 0)
1372 1.51 augustss printf(" port %d", up->portno);
1373 1.51 augustss printf(" (addr %d) disconnected\n", dev->address);
1374 1.47 augustss config_detach(dev->subdevs[i], DETACH_FORCE);
1375 1.110 drochner dev->subdevs[i] = 0;
1376 1.47 augustss }
1377 1.47 augustss }
1378 1.47 augustss
1379 1.65 augustss usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1380 1.57 augustss dev->bus->devices[dev->address] = NULL;
1381 1.57 augustss up->device = NULL;
1382 1.47 augustss usb_free_device(dev);
1383 1.47 augustss }
1384 1.47 augustss
1385 1.54 augustss #ifdef __OpenBSD__
1386 1.79 augustss void *usb_realloc(void *p, u_int size, int pool, int flags)
1387 1.54 augustss {
1388 1.54 augustss void *q;
1389 1.54 augustss
1390 1.54 augustss q = malloc(size, pool, flags);
1391 1.54 augustss if (q == NULL)
1392 1.54 augustss return (NULL);
1393 1.54 augustss bcopy(p, q, size);
1394 1.54 augustss free(p, pool);
1395 1.54 augustss return (q);
1396 1.54 augustss }
1397 1.54 augustss #endif
1398