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