usb_subr.c revision 1.143.8.3 1 1.143.8.3 itohy /* $NetBSD: usb_subr.c,v 1.143.8.3 2007/06/21 15:27:58 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.3 itohy __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.143.8.3 2007/06/21 15:27:58 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.143.8.3 itohy if (index >= dev->ddesc.bNumConfigurations &&
605 1.143.8.3 itohy index != USB_UNCONFIG_NO) {
606 1.143.8.3 itohy /* panic? */
607 1.143.8.3 itohy printf("usbd_set_config_index: illegal index\n");
608 1.143.8.3 itohy return (USBD_INVAL);
609 1.143.8.3 itohy }
610 1.143.8.3 itohy
611 1.75 augustss if (dev->config != USB_UNCONFIG_NO) {
612 1.143.8.1 itohy nifc = dev->cdesc->bNumInterface;
613 1.143.8.1 itohy
614 1.143.8.1 itohy /* Check that all interfaces are idle */
615 1.143.8.1 itohy for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
616 1.143.8.1 itohy if (LIST_EMPTY(&dev->ifaces[ifcidx].pipes))
617 1.143.8.1 itohy continue;
618 1.143.8.1 itohy DPRINTF(("usbd_set_config_index: open pipes exist\n"));
619 1.143.8.1 itohy return (USBD_IN_USE);
620 1.143.8.1 itohy }
621 1.143.8.1 itohy
622 1.12 augustss DPRINTF(("usbd_set_config_index: free old config\n"));
623 1.1 augustss /* Free all configuration data structures. */
624 1.12 augustss for (ifcidx = 0; ifcidx < nifc; ifcidx++)
625 1.12 augustss usbd_free_iface_data(dev, ifcidx);
626 1.1 augustss free(dev->ifaces, M_USB);
627 1.1 augustss free(dev->cdesc, M_USB);
628 1.57 augustss dev->ifaces = NULL;
629 1.57 augustss dev->cdesc = NULL;
630 1.75 augustss dev->config = USB_UNCONFIG_NO;
631 1.75 augustss }
632 1.75 augustss
633 1.75 augustss if (index == USB_UNCONFIG_INDEX) {
634 1.75 augustss /* We are unconfiguring the device, so leave unallocated. */
635 1.75 augustss DPRINTF(("usbd_set_config_index: set config 0\n"));
636 1.75 augustss err = usbd_set_config(dev, USB_UNCONFIG_NO);
637 1.136 christos if (err) {
638 1.75 augustss DPRINTF(("usbd_set_config_index: setting config=0 "
639 1.75 augustss "failed, error=%s\n", usbd_errstr(err)));
640 1.136 christos }
641 1.75 augustss return (err);
642 1.1 augustss }
643 1.1 augustss
644 1.74 augustss /* Get the short descriptor. */
645 1.53 augustss err = usbd_get_config_desc(dev, index, &cd);
646 1.53 augustss if (err)
647 1.53 augustss return (err);
648 1.1 augustss len = UGETW(cd.wTotalLength);
649 1.1 augustss cdp = malloc(len, M_USB, M_NOWAIT);
650 1.53 augustss if (cdp == NULL)
651 1.1 augustss return (USBD_NOMEM);
652 1.103 augustss
653 1.103 augustss /* Get the full descriptor. Try a few times for slow devices. */
654 1.103 augustss for (i = 0; i < 3; i++) {
655 1.103 augustss err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
656 1.103 augustss if (!err)
657 1.103 augustss break;
658 1.103 augustss usbd_delay_ms(dev, 200);
659 1.103 augustss }
660 1.53 augustss if (err)
661 1.1 augustss goto bad;
662 1.18 augustss if (cdp->bDescriptorType != UDESC_CONFIG) {
663 1.18 augustss DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
664 1.18 augustss cdp->bDescriptorType));
665 1.53 augustss err = USBD_INVAL;
666 1.18 augustss goto bad;
667 1.18 augustss }
668 1.74 augustss
669 1.142 drochner /*
670 1.142 drochner * Figure out if the device is self or bus powered.
671 1.142 drochner */
672 1.142 drochner #if 0 /* XXX various devices don't report the power state correctly */
673 1.1 augustss selfpowered = 0;
674 1.141 drochner err = usbd_get_device_status(dev, &ds);
675 1.141 drochner if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
676 1.141 drochner selfpowered = 1;
677 1.142 drochner #endif
678 1.142 drochner /*
679 1.142 drochner * Use the power state in the configuration we are going
680 1.142 drochner * to set. This doesn't necessarily reflect the actual
681 1.142 drochner * power state of the device; the driver can control this
682 1.142 drochner * by choosing the appropriate configuration.
683 1.142 drochner */
684 1.142 drochner selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
685 1.141 drochner
686 1.81 augustss DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
687 1.81 augustss "selfpowered=%d, power=%d\n",
688 1.99 augustss cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
689 1.33 augustss selfpowered, cdp->bMaxPower * 2));
690 1.74 augustss
691 1.74 augustss /* Check if we have enough power. */
692 1.142 drochner #if 0 /* this is a no-op, see above */
693 1.141 drochner if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
694 1.141 drochner if (msg)
695 1.141 drochner printf("%s: device addr %d (config %d): "
696 1.141 drochner "can't set self powered configuration\n",
697 1.141 drochner USBDEVNAME(dev->bus->bdev), dev->address,
698 1.141 drochner cdp->bConfigurationValue);
699 1.141 drochner err = USBD_NO_POWER;
700 1.141 drochner goto bad;
701 1.141 drochner }
702 1.142 drochner #endif
703 1.1 augustss #ifdef USB_DEBUG
704 1.53 augustss if (dev->powersrc == NULL) {
705 1.44 augustss DPRINTF(("usbd_set_config_index: No power source?\n"));
706 1.141 drochner err = USBD_IOERROR;
707 1.141 drochner goto bad;
708 1.1 augustss }
709 1.1 augustss #endif
710 1.1 augustss power = cdp->bMaxPower * 2;
711 1.1 augustss if (power > dev->powersrc->power) {
712 1.81 augustss DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
713 1.1 augustss /* XXX print nicer message. */
714 1.7 augustss if (msg)
715 1.18 augustss printf("%s: device addr %d (config %d) exceeds power "
716 1.18 augustss "budget, %d mA > %d mA\n",
717 1.99 augustss USBDEVNAME(dev->bus->bdev), dev->address,
718 1.99 augustss cdp->bConfigurationValue,
719 1.7 augustss power, dev->powersrc->power);
720 1.53 augustss err = USBD_NO_POWER;
721 1.1 augustss goto bad;
722 1.1 augustss }
723 1.1 augustss dev->power = power;
724 1.1 augustss dev->self_powered = selfpowered;
725 1.1 augustss
726 1.74 augustss /* Set the actual configuration value. */
727 1.18 augustss DPRINTF(("usbd_set_config_index: set config %d\n",
728 1.18 augustss cdp->bConfigurationValue));
729 1.53 augustss err = usbd_set_config(dev, cdp->bConfigurationValue);
730 1.53 augustss if (err) {
731 1.14 drochner DPRINTF(("usbd_set_config_index: setting config=%d failed, "
732 1.39 augustss "error=%s\n",
733 1.53 augustss cdp->bConfigurationValue, usbd_errstr(err)));
734 1.1 augustss goto bad;
735 1.1 augustss }
736 1.74 augustss
737 1.74 augustss /* Allocate and fill interface data. */
738 1.1 augustss nifc = cdp->bNumInterface;
739 1.99 augustss dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
740 1.1 augustss M_USB, M_NOWAIT);
741 1.58 augustss if (dev->ifaces == NULL) {
742 1.53 augustss err = USBD_NOMEM;
743 1.1 augustss goto bad;
744 1.1 augustss }
745 1.12 augustss DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
746 1.1 augustss dev->cdesc = cdp;
747 1.1 augustss dev->config = cdp->bConfigurationValue;
748 1.12 augustss for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
749 1.53 augustss err = usbd_fill_iface_data(dev, ifcidx, 0);
750 1.53 augustss if (err) {
751 1.12 augustss while (--ifcidx >= 0)
752 1.12 augustss usbd_free_iface_data(dev, ifcidx);
753 1.1 augustss goto bad;
754 1.1 augustss }
755 1.1 augustss }
756 1.1 augustss
757 1.1 augustss return (USBD_NORMAL_COMPLETION);
758 1.1 augustss
759 1.1 augustss bad:
760 1.1 augustss free(cdp, M_USB);
761 1.53 augustss return (err);
762 1.1 augustss }
763 1.1 augustss
764 1.1 augustss /* XXX add function for alternate settings */
765 1.1 augustss
766 1.1 augustss usbd_status
767 1.78 augustss usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
768 1.78 augustss struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
769 1.1 augustss {
770 1.1 augustss usbd_pipe_handle p;
771 1.53 augustss usbd_status err;
772 1.1 augustss
773 1.1 augustss DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
774 1.1 augustss dev, iface, ep, pipe));
775 1.1 augustss p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
776 1.53 augustss if (p == NULL)
777 1.1 augustss return (USBD_NOMEM);
778 1.1 augustss p->device = dev;
779 1.1 augustss p->iface = iface;
780 1.1 augustss p->endpoint = ep;
781 1.1 augustss ep->refcnt++;
782 1.1 augustss p->refcnt = 1;
783 1.53 augustss p->intrxfer = 0;
784 1.1 augustss p->running = 0;
785 1.70 augustss p->aborting = 0;
786 1.34 augustss p->repeat = 0;
787 1.63 augustss p->interval = ival;
788 1.1 augustss SIMPLEQ_INIT(&p->queue);
789 1.53 augustss err = dev->bus->methods->open_pipe(p);
790 1.53 augustss if (err) {
791 1.39 augustss DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
792 1.39 augustss "%s\n",
793 1.53 augustss ep->edesc->bEndpointAddress, usbd_errstr(err)));
794 1.1 augustss free(p, M_USB);
795 1.53 augustss return (err);
796 1.1 augustss }
797 1.1 augustss *pipe = p;
798 1.1 augustss return (USBD_NORMAL_COMPLETION);
799 1.1 augustss }
800 1.1 augustss
801 1.1 augustss /* Abort the device control pipe. */
802 1.1 augustss void
803 1.78 augustss usbd_kill_pipe(usbd_pipe_handle pipe)
804 1.1 augustss {
805 1.89 augustss usbd_abort_pipe(pipe);
806 1.1 augustss pipe->methods->close(pipe);
807 1.1 augustss pipe->endpoint->refcnt--;
808 1.1 augustss free(pipe, M_USB);
809 1.1 augustss }
810 1.1 augustss
811 1.1 augustss int
812 1.78 augustss usbd_getnewaddr(usbd_bus_handle bus)
813 1.1 augustss {
814 1.18 augustss int addr;
815 1.1 augustss
816 1.18 augustss for (addr = 1; addr < USB_MAX_DEVICES; addr++)
817 1.18 augustss if (bus->devices[addr] == 0)
818 1.18 augustss return (addr);
819 1.1 augustss return (-1);
820 1.1 augustss }
821 1.1 augustss
822 1.18 augustss
823 1.18 augustss usbd_status
824 1.78 augustss usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
825 1.78 augustss int port, int addr)
826 1.18 augustss {
827 1.18 augustss struct usb_attach_arg uaa;
828 1.143.8.2 itohy #ifdef USB_USE_IFATTACH
829 1.143.8.2 itohy struct usbif_attach_arg uiaa;
830 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
831 1.18 augustss usb_device_descriptor_t *dd = &dev->ddesc;
832 1.53 augustss int found, i, confi, nifaces;
833 1.53 augustss usbd_status err;
834 1.43 augustss device_ptr_t dv;
835 1.132 christos usbd_interface_handle *ifaces;
836 1.143.8.1 itohy device_ptr_t *tmpdv;
837 1.18 augustss #if defined(__FreeBSD__)
838 1.143.8.1 itohy char *devinfo;
839 1.43 augustss device_t bdev;
840 1.143.8.1 itohy struct usb_attach_arg *uaap;
841 1.143.8.2 itohy #ifdef USB_USE_IFATTACH
842 1.143.8.2 itohy struct usbif_attach_arg *uiaap;
843 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
844 1.143.8.1 itohy #endif
845 1.143.8.1 itohy
846 1.143.8.1 itohy #ifdef __FreeBSD__
847 1.143.8.1 itohy #define FREEDEVINFO free(devinfo, M_USB)
848 1.143.8.1 itohy #else
849 1.143.8.1 itohy #define FREEDEVINFO /* empty */
850 1.143.8.1 itohy #endif
851 1.143.8.1 itohy
852 1.143.8.1 itohy #if defined(__FreeBSD__)
853 1.143.8.1 itohy /* XXX FreeBSD may leak resources on failure cases -- fixme */
854 1.143.8.1 itohy devinfo = malloc(DEVINFOSIZE, M_USB, M_NOWAIT);
855 1.143.8.1 itohy if (devinfo == NULL) {
856 1.143.8.1 itohy device_printf(parent, "Can't allocate memory for probe string\n");
857 1.143.8.1 itohy return (USBD_NOMEM);
858 1.143.8.1 itohy }
859 1.143.8.1 itohy bdev = device_add_child(parent, NULL, -1);
860 1.26 augustss if (!bdev) {
861 1.143.8.1 itohy FREEDEVINFO;
862 1.143.8.1 itohy device_printf(parent, "Device creation failed\n");
863 1.143.8.1 itohy return (USBD_INVAL);
864 1.143.8.1 itohy }
865 1.143.8.1 itohy uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
866 1.143.8.1 itohy if (uaap == NULL) {
867 1.143.8.1 itohy FREEDEVINFO;
868 1.143.8.1 itohy return (USBD_INVAL);
869 1.18 augustss }
870 1.143.8.1 itohy device_set_ivars(bdev, uaap);
871 1.18 augustss #endif
872 1.18 augustss
873 1.18 augustss uaa.device = dev;
874 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
875 1.72 augustss uaa.iface = NULL;
876 1.72 augustss uaa.ifaces = NULL;
877 1.20 augustss uaa.nifaces = 0;
878 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
879 1.18 augustss uaa.usegeneric = 0;
880 1.18 augustss uaa.port = port;
881 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
882 1.18 augustss uaa.configno = UHUB_UNK_CONFIGURATION;
883 1.18 augustss uaa.ifaceno = UHUB_UNK_INTERFACE;
884 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
885 1.32 augustss uaa.vendor = UGETW(dd->idVendor);
886 1.32 augustss uaa.product = UGETW(dd->idProduct);
887 1.32 augustss uaa.release = UGETW(dd->bcdDevice);
888 1.143.8.2 itohy #ifdef USB_USE_IFATTACH
889 1.143.8.2 itohy uaa.class = dd->bDeviceClass;
890 1.143.8.2 itohy uaa.subclass = dd->bDeviceSubClass;
891 1.143.8.2 itohy uaa.proto = dd->bDeviceProtocol;
892 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
893 1.18 augustss
894 1.18 augustss /* First try with device specific drivers. */
895 1.60 augustss DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
896 1.143.8.1 itohy
897 1.143.8.1 itohy #ifdef __FreeBSD__
898 1.143.8.1 itohy dev->ifacenums = NULL;
899 1.143.8.1 itohy dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
900 1.143.8.1 itohy if (dev->subdevs == NULL) {
901 1.143.8.1 itohy FREEDEVINFO;
902 1.143.8.1 itohy return (USBD_NOMEM);
903 1.143.8.1 itohy }
904 1.143.8.1 itohy dev->subdevs[0] = bdev;
905 1.143.8.1 itohy dev->subdevs[1] = 0;
906 1.143.8.1 itohy *uaap = uaa;
907 1.143.8.1 itohy usbd_devinfo(dev, 1, devinfo, DEVINFOSIZE);
908 1.143.8.1 itohy device_set_desc_copy(bdev, devinfo);
909 1.143.8.1 itohy #endif
910 1.34 augustss dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
911 1.34 augustss if (dv) {
912 1.143.8.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
913 1.34 augustss dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
914 1.57 augustss if (dev->subdevs == NULL)
915 1.34 augustss return (USBD_NOMEM);
916 1.34 augustss dev->subdevs[0] = dv;
917 1.34 augustss dev->subdevs[1] = 0;
918 1.143.8.1 itohy #endif
919 1.143.8.1 itohy FREEDEVINFO;
920 1.18 augustss return (USBD_NORMAL_COMPLETION);
921 1.34 augustss }
922 1.143.8.1 itohy #ifdef __FreeBSD__
923 1.143.8.1 itohy /*
924 1.143.8.1 itohy * Free subdevs so we can reallocate it larger for the number of
925 1.143.8.1 itohy * interfaces
926 1.143.8.1 itohy */
927 1.143.8.1 itohy tmpdv = dev->subdevs;
928 1.143.8.1 itohy dev->subdevs = NULL;
929 1.143.8.1 itohy free(tmpdv, M_USB);
930 1.143.8.2 itohy #ifdef USB_USE_IFATTACH
931 1.143.8.2 itohy free(uaap, M_USB);
932 1.143.8.2 itohy uiaap = malloc(sizeof(uiaa), M_USB, M_NOWAIT);
933 1.143.8.2 itohy if (uiaap == NULL) {
934 1.143.8.2 itohy FREEDEVINFO;
935 1.143.8.2 itohy return (USBD_INVAL);
936 1.143.8.2 itohy }
937 1.143.8.2 itohy device_set_ivars(bdev, uiaap);
938 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
939 1.143.8.1 itohy #endif
940 1.18 augustss DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
941 1.18 augustss
942 1.143.8.2 itohy #ifdef USB_USE_IFATTACH
943 1.143.8.2 itohy uiaa.device = dev;
944 1.143.8.2 itohy uiaa.port = port;
945 1.143.8.2 itohy uiaa.vendor = UGETW(dd->idVendor);
946 1.143.8.2 itohy uiaa.product = UGETW(dd->idProduct);
947 1.143.8.2 itohy uiaa.release = UGETW(dd->bcdDevice);
948 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
949 1.143.8.2 itohy
950 1.60 augustss DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
951 1.60 augustss dd->bNumConfigurations));
952 1.18 augustss /* Next try with interface drivers. */
953 1.18 augustss for (confi = 0; confi < dd->bNumConfigurations; confi++) {
954 1.18 augustss DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
955 1.18 augustss confi));
956 1.53 augustss err = usbd_set_config_index(dev, confi, 1);
957 1.53 augustss if (err) {
958 1.18 augustss #ifdef USB_DEBUG
959 1.18 augustss DPRINTF(("%s: port %d, set config at addr %d failed, "
960 1.43 augustss "error=%s\n", USBDEVPTRNAME(parent), port,
961 1.53 augustss addr, usbd_errstr(err)));
962 1.18 augustss #else
963 1.18 augustss printf("%s: port %d, set config at addr %d failed\n",
964 1.43 augustss USBDEVPTRNAME(parent), port, addr);
965 1.18 augustss #endif
966 1.143.8.1 itohy FREEDEVINFO;
967 1.143.8.1 itohy return (err);
968 1.18 augustss }
969 1.20 augustss nifaces = dev->cdesc->bNumInterface;
970 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
971 1.23 augustss uaa.configno = dev->cdesc->bConfigurationValue;
972 1.143.8.2 itohy #else
973 1.143.8.2 itohy uiaa.configno = dev->cdesc->bConfigurationValue;
974 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
975 1.132 christos ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT);
976 1.132 christos if (ifaces == NULL)
977 1.132 christos goto nomem;
978 1.20 augustss for (i = 0; i < nifaces; i++)
979 1.20 augustss ifaces[i] = &dev->ifaces[i];
980 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
981 1.20 augustss uaa.ifaces = ifaces;
982 1.20 augustss uaa.nifaces = nifaces;
983 1.143.8.2 itohy #else
984 1.143.8.2 itohy uiaa.ifaces = ifaces;
985 1.143.8.2 itohy uiaa.nifaces = nifaces;
986 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
987 1.34 augustss dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
988 1.57 augustss if (dev->subdevs == NULL) {
989 1.132 christos free(ifaces, M_USB);
990 1.132 christos nomem:
991 1.143.8.1 itohy FREEDEVINFO;
992 1.143.8.1 itohy return (USBD_NOMEM);
993 1.143.8.1 itohy }
994 1.143.8.1 itohy dev->ifacenums = malloc((nifaces) * sizeof(*dev->ifacenums),
995 1.143.8.1 itohy M_USB,M_NOWAIT);
996 1.143.8.1 itohy if (dev->ifacenums == NULL) {
997 1.143.8.1 itohy free(ifaces, M_USB);
998 1.143.8.1 itohy FREEDEVINFO;
999 1.34 augustss return (USBD_NOMEM);
1000 1.52 augustss }
1001 1.52 augustss
1002 1.52 augustss found = 0;
1003 1.52 augustss for (i = 0; i < nifaces; i++) {
1004 1.53 augustss if (ifaces[i] == NULL)
1005 1.20 augustss continue; /* interface already claimed */
1006 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
1007 1.20 augustss uaa.iface = ifaces[i];
1008 1.20 augustss uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
1009 1.143.8.2 itohy #else
1010 1.143.8.2 itohy uiaa.iface = ifaces[i];
1011 1.143.8.2 itohy uiaa.class = ifaces[i]->idesc->bInterfaceClass;
1012 1.143.8.2 itohy uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass;
1013 1.143.8.2 itohy uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol;
1014 1.143.8.2 itohy uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
1015 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1016 1.143.8.1 itohy dev->ifacenums[found] = i;
1017 1.143.8.1 itohy #ifdef __FreeBSD__
1018 1.143.8.1 itohy dev->subdevs[found] = bdev;
1019 1.143.8.1 itohy dev->subdevs[found + 1] = 0;
1020 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
1021 1.143.8.1 itohy *uaap = uaa;
1022 1.143.8.2 itohy #else
1023 1.143.8.2 itohy *uiaap = uaa;
1024 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1025 1.143.8.1 itohy usbd_devinfo(dev, 1, devinfo, DEVINFOSIZE);
1026 1.143.8.1 itohy device_set_desc_copy(bdev, devinfo);
1027 1.143.8.1 itohy #endif
1028 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
1029 1.34 augustss dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
1030 1.34 augustss usbd_submatch);
1031 1.143.8.2 itohy #else
1032 1.143.8.2 itohy dv = USB_DO_IFATTACH(dev, bdev, parent, &uiaa,
1033 1.143.8.2 itohy usbd_ifprint, usbd_ifsubmatch);
1034 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1035 1.53 augustss if (dv != NULL) {
1036 1.143.8.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
1037 1.143.8.1 itohy dev->subdevs[found] = dv;
1038 1.143.8.1 itohy dev->subdevs[found + 1] = 0;
1039 1.143.8.1 itohy #endif
1040 1.20 augustss ifaces[i] = 0; /* consumed */
1041 1.143.8.1 itohy found++;
1042 1.143.8.1 itohy #ifdef __FreeBSD__
1043 1.52 augustss /* create another child for the next iface */
1044 1.143.8.1 itohy bdev = device_add_child(parent, NULL, -1);
1045 1.52 augustss if (!bdev) {
1046 1.143.8.1 itohy device_printf(parent,
1047 1.143.8.1 itohy "Device add failed\n");
1048 1.143.8.1 itohy FREEDEVINFO;
1049 1.132 christos free(ifaces, M_USB);
1050 1.52 augustss return (USBD_NORMAL_COMPLETION);
1051 1.52 augustss }
1052 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
1053 1.143.8.1 itohy uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
1054 1.143.8.1 itohy if (uaap == NULL) {
1055 1.143.8.1 itohy FREEDEVINFO;
1056 1.143.8.1 itohy free(ifaces, M_USB);
1057 1.143.8.1 itohy return (USBD_NOMEM);
1058 1.143.8.1 itohy }
1059 1.143.8.1 itohy device_set_ivars(bdev, uaap);
1060 1.143.8.2 itohy #else
1061 1.143.8.2 itohy uiaap = malloc(sizeof(uiaa), M_USB, M_NOWAIT);
1062 1.143.8.2 itohy if (uiaap == NULL) {
1063 1.143.8.2 itohy FREEDEVINFO;
1064 1.143.8.2 itohy free(ifaces, M_USB);
1065 1.143.8.2 itohy return (USBD_NOMEM);
1066 1.143.8.2 itohy }
1067 1.143.8.2 itohy device_set_ivars(bdev, uiaap);
1068 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1069 1.143.8.1 itohy #endif /* __FreBSD__ */
1070 1.143.8.1 itohy } else {
1071 1.143.8.1 itohy dev->subdevs[found] = 0;
1072 1.20 augustss }
1073 1.18 augustss }
1074 1.143.8.1 itohy free(ifaces, M_USB);
1075 1.52 augustss if (found != 0) {
1076 1.52 augustss #if defined(__FreeBSD__)
1077 1.143.8.1 itohy /* remove the last created child. It is unused */
1078 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
1079 1.143.8.1 itohy free(uaap, M_USB);
1080 1.143.8.2 itohy #else
1081 1.143.8.2 itohy free(uiaap, M_USB);
1082 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1083 1.143.8.1 itohy FREEDEVINFO;
1084 1.52 augustss device_delete_child(parent, bdev);
1085 1.52 augustss #endif
1086 1.18 augustss return (USBD_NORMAL_COMPLETION);
1087 1.52 augustss }
1088 1.143.8.1 itohy tmpdv = dev->subdevs;
1089 1.143.8.1 itohy dev->subdevs = NULL;
1090 1.143.8.1 itohy free(tmpdv, M_USB);
1091 1.143.8.1 itohy free(dev->ifacenums, M_USB);
1092 1.143.8.1 itohy dev->ifacenums = NULL;
1093 1.18 augustss }
1094 1.21 augustss /* No interfaces were attached in any of the configurations. */
1095 1.34 augustss
1096 1.34 augustss if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
1097 1.18 augustss usbd_set_config_index(dev, 0, 0);
1098 1.18 augustss
1099 1.18 augustss DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
1100 1.18 augustss
1101 1.18 augustss /* Finally try the generic driver. */
1102 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
1103 1.72 augustss uaa.iface = NULL;
1104 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1105 1.18 augustss uaa.usegeneric = 1;
1106 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
1107 1.18 augustss uaa.configno = UHUB_UNK_CONFIGURATION;
1108 1.18 augustss uaa.ifaceno = UHUB_UNK_INTERFACE;
1109 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1110 1.143.8.1 itohy #ifdef __FreeBSD__
1111 1.143.8.2 itohy #ifdef USB_USE_IFATTACH
1112 1.143.8.2 itohy free(uiaap, M_USB);
1113 1.143.8.2 itohy uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
1114 1.143.8.2 itohy if (uaap == NULL) {
1115 1.143.8.2 itohy FREEDEVINFO;
1116 1.143.8.2 itohy return (USBD_INVAL);
1117 1.143.8.2 itohy }
1118 1.143.8.2 itohy device_set_ivars(bdev, uaap);
1119 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1120 1.143.8.1 itohy dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
1121 1.143.8.1 itohy if (dev->subdevs == 0) {
1122 1.143.8.1 itohy FREEDEVINFO;
1123 1.143.8.1 itohy return (USBD_NOMEM);
1124 1.143.8.1 itohy }
1125 1.143.8.1 itohy dev->subdevs[0] = bdev;
1126 1.143.8.1 itohy dev->subdevs[1] = 0;
1127 1.143.8.1 itohy *uaap = uaa;
1128 1.143.8.1 itohy usbd_devinfo(dev, 1, devinfo, DEVINFOSIZE);
1129 1.143.8.1 itohy device_set_desc_copy(bdev, devinfo);
1130 1.143.8.1 itohy FREEDEVINFO;
1131 1.143.8.1 itohy #endif
1132 1.34 augustss dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
1133 1.53 augustss if (dv != NULL) {
1134 1.143.8.1 itohy #if defined(__NetBSD__) || defined(__OpenBSD__)
1135 1.34 augustss dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
1136 1.34 augustss if (dev->subdevs == 0)
1137 1.34 augustss return (USBD_NOMEM);
1138 1.34 augustss dev->subdevs[0] = dv;
1139 1.34 augustss dev->subdevs[1] = 0;
1140 1.143.8.1 itohy #endif
1141 1.18 augustss return (USBD_NORMAL_COMPLETION);
1142 1.34 augustss }
1143 1.18 augustss
1144 1.99 augustss /*
1145 1.18 augustss * The generic attach failed, but leave the device as it is.
1146 1.18 augustss * We just did not find any drivers, that's all. The device is
1147 1.18 augustss * fully operational and not harming anyone.
1148 1.18 augustss */
1149 1.18 augustss DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
1150 1.143.8.1 itohy return (USBD_NORMAL_COMPLETION);
1151 1.143.8.1 itohy #undef FREEDEVINFO
1152 1.18 augustss }
1153 1.18 augustss
1154 1.18 augustss
1155 1.1 augustss /*
1156 1.1 augustss * Called when a new device has been put in the powered state,
1157 1.1 augustss * but not yet in the addressed state.
1158 1.1 augustss * Get initial descriptor, set the address, get full descriptor,
1159 1.1 augustss * and attach a driver.
1160 1.1 augustss */
1161 1.1 augustss usbd_status
1162 1.78 augustss usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
1163 1.94 augustss int speed, int port, struct usbd_port *up)
1164 1.1 augustss {
1165 1.120 augustss usbd_device_handle dev, adev;
1166 1.94 augustss struct usbd_device *hub;
1167 1.18 augustss usb_device_descriptor_t *dd;
1168 1.107 augustss usb_port_status_t ps;
1169 1.53 augustss usbd_status err;
1170 1.1 augustss int addr;
1171 1.18 augustss int i;
1172 1.120 augustss int p;
1173 1.1 augustss
1174 1.94 augustss DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1175 1.94 augustss bus, port, depth, speed));
1176 1.1 augustss addr = usbd_getnewaddr(bus);
1177 1.1 augustss if (addr < 0) {
1178 1.99 augustss printf("%s: No free USB addresses, new device ignored.\n",
1179 1.18 augustss USBDEVNAME(bus->bdev));
1180 1.1 augustss return (USBD_NO_ADDR);
1181 1.1 augustss }
1182 1.1 augustss
1183 1.97 tsutsui dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1184 1.53 augustss if (dev == NULL)
1185 1.1 augustss return (USBD_NOMEM);
1186 1.1 augustss
1187 1.1 augustss dev->bus = bus;
1188 1.1 augustss
1189 1.1 augustss /* Set up default endpoint handle. */
1190 1.1 augustss dev->def_ep.edesc = &dev->def_ep_desc;
1191 1.1 augustss
1192 1.1 augustss /* Set up default endpoint descriptor. */
1193 1.1 augustss dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1194 1.1 augustss dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1195 1.1 augustss dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1196 1.1 augustss dev->def_ep_desc.bmAttributes = UE_CONTROL;
1197 1.1 augustss USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1198 1.1 augustss dev->def_ep_desc.bInterval = 0;
1199 1.1 augustss
1200 1.1 augustss dev->quirks = &usbd_no_quirk;
1201 1.1 augustss dev->address = USB_START_ADDR;
1202 1.4 augustss dev->ddesc.bMaxPacketSize = 0;
1203 1.1 augustss dev->depth = depth;
1204 1.1 augustss dev->powersrc = up;
1205 1.94 augustss dev->myhub = up->parent;
1206 1.120 augustss
1207 1.120 augustss up->device = dev;
1208 1.120 augustss
1209 1.120 augustss /* Locate port on upstream high speed hub */
1210 1.120 augustss for (adev = dev, hub = up->parent;
1211 1.94 augustss hub != NULL && hub->speed != USB_SPEED_HIGH;
1212 1.120 augustss adev = hub, hub = hub->myhub)
1213 1.94 augustss ;
1214 1.120 augustss if (hub) {
1215 1.120 augustss for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1216 1.120 augustss if (hub->hub->ports[p].device == adev) {
1217 1.120 augustss dev->myhsport = &hub->hub->ports[p];
1218 1.120 augustss goto found;
1219 1.120 augustss }
1220 1.120 augustss }
1221 1.120 augustss panic("usbd_new_device: cannot find HS port\n");
1222 1.120 augustss found:
1223 1.120 augustss DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1224 1.120 augustss } else {
1225 1.120 augustss dev->myhsport = NULL;
1226 1.120 augustss }
1227 1.94 augustss dev->speed = speed;
1228 1.22 augustss dev->langid = USBD_NOLANG;
1229 1.50 augustss dev->cookie.cookie = ++usb_cookie_no;
1230 1.1 augustss
1231 1.67 soren /* Establish the default pipe. */
1232 1.63 augustss err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1233 1.63 augustss &dev->default_pipe);
1234 1.53 augustss if (err) {
1235 1.18 augustss usbd_remove_device(dev, up);
1236 1.53 augustss return (err);
1237 1.18 augustss }
1238 1.1 augustss
1239 1.111 augustss /* Set the address. Do this early; some devices need that. */
1240 1.143.8.1 itohy /* Try a few times in case the device is slow (i.e. outside specs.) */
1241 1.111 augustss DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1242 1.143.8.1 itohy for (i = 0; i < 15; i++) {
1243 1.143.8.1 itohy err = usbd_set_address(dev, addr);
1244 1.143.8.1 itohy if (!err)
1245 1.143.8.1 itohy break;
1246 1.143.8.1 itohy usbd_delay_ms(dev, 200);
1247 1.143.8.1 itohy if ((i & 3) == 3) {
1248 1.143.8.3 itohy DPRINTFN(-1,("usbd_new_device: set address %d "
1249 1.143.8.1 itohy "failed - trying a port reset\n", addr));
1250 1.143.8.1 itohy usbd_reset_port(up->parent, port, &ps);
1251 1.143.8.1 itohy }
1252 1.143.8.1 itohy }
1253 1.111 augustss if (err) {
1254 1.143.8.3 itohy DPRINTFN(-1,("usbd_new_device: set address %d failed\n", addr));
1255 1.111 augustss err = USBD_SET_ADDR_FAILED;
1256 1.111 augustss usbd_remove_device(dev, up);
1257 1.111 augustss return (err);
1258 1.111 augustss }
1259 1.111 augustss /* Allow device time to set new address */
1260 1.111 augustss usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1261 1.111 augustss dev->address = addr; /* New device address now */
1262 1.111 augustss bus->devices[addr] = dev;
1263 1.111 augustss
1264 1.143.8.1 itohy /* Re-establish the default pipe with the new address. */
1265 1.143.8.1 itohy usbd_kill_pipe(dev->default_pipe);
1266 1.143.8.1 itohy err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1267 1.143.8.1 itohy &dev->default_pipe);
1268 1.143.8.1 itohy if (err) {
1269 1.143.8.1 itohy usbd_remove_device(dev, up);
1270 1.143.8.1 itohy return (err);
1271 1.6 augustss }
1272 1.143.8.1 itohy
1273 1.143.8.1 itohy dd = &dev->ddesc;
1274 1.143.8.1 itohy /* Get the first 8 bytes of the device descriptor. */
1275 1.143.8.1 itohy err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1276 1.53 augustss if (err) {
1277 1.14 drochner DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1278 1.53 augustss "failed\n", addr));
1279 1.18 augustss usbd_remove_device(dev, up);
1280 1.53 augustss return (err);
1281 1.95 augustss }
1282 1.95 augustss
1283 1.95 augustss if (speed == USB_SPEED_HIGH) {
1284 1.95 augustss /* Max packet size must be 64 (sec 5.5.3). */
1285 1.95 augustss if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1286 1.95 augustss #ifdef DIAGNOSTIC
1287 1.95 augustss printf("usbd_new_device: addr=%d bad max packet size\n",
1288 1.95 augustss addr);
1289 1.95 augustss #endif
1290 1.95 augustss dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1291 1.95 augustss }
1292 1.18 augustss }
1293 1.18 augustss
1294 1.39 augustss DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1295 1.99 augustss "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1296 1.39 augustss addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1297 1.99 augustss dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1298 1.94 augustss dev->speed));
1299 1.39 augustss
1300 1.18 augustss if (dd->bDescriptorType != UDESC_DEVICE) {
1301 1.18 augustss /* Illegal device descriptor */
1302 1.18 augustss DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1303 1.18 augustss dd->bDescriptorType));
1304 1.18 augustss usbd_remove_device(dev, up);
1305 1.18 augustss return (USBD_INVAL);
1306 1.1 augustss }
1307 1.1 augustss
1308 1.39 augustss if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1309 1.39 augustss DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1310 1.39 augustss usbd_remove_device(dev, up);
1311 1.39 augustss return (USBD_INVAL);
1312 1.39 augustss }
1313 1.4 augustss
1314 1.18 augustss USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1315 1.1 augustss
1316 1.143.8.1 itohy /* Re-establish the default pipe with the new max packet size. */
1317 1.143.8.1 itohy usbd_kill_pipe(dev->default_pipe);
1318 1.143.8.1 itohy err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1319 1.143.8.1 itohy &dev->default_pipe);
1320 1.143.8.1 itohy if (err) {
1321 1.143.8.1 itohy usbd_remove_device(dev, up);
1322 1.143.8.1 itohy return (err);
1323 1.143.8.1 itohy }
1324 1.143.8.1 itohy
1325 1.62 augustss err = usbd_reload_device_desc(dev);
1326 1.53 augustss if (err) {
1327 1.14 drochner DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1328 1.14 drochner "failed\n", addr));
1329 1.18 augustss usbd_remove_device(dev, up);
1330 1.53 augustss return (err);
1331 1.1 augustss }
1332 1.1 augustss
1333 1.1 augustss /* Assume 100mA bus powered for now. Changed when configured. */
1334 1.1 augustss dev->power = USB_MIN_POWER;
1335 1.1 augustss dev->self_powered = 0;
1336 1.1 augustss
1337 1.99 augustss DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1338 1.1 augustss addr, dev, parent));
1339 1.99 augustss
1340 1.53 augustss err = usbd_probe_and_attach(parent, dev, port, addr);
1341 1.53 augustss if (err) {
1342 1.18 augustss usbd_remove_device(dev, up);
1343 1.53 augustss return (err);
1344 1.143.8.1 itohy }
1345 1.143.8.1 itohy
1346 1.143.8.1 itohy usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1347 1.65 augustss
1348 1.143.8.1 itohy return (USBD_NORMAL_COMPLETION);
1349 1.62 augustss }
1350 1.62 augustss
1351 1.62 augustss usbd_status
1352 1.78 augustss usbd_reload_device_desc(usbd_device_handle dev)
1353 1.62 augustss {
1354 1.62 augustss usbd_status err;
1355 1.143.8.1 itohy int i;
1356 1.62 augustss
1357 1.62 augustss /* Get the full device descriptor. */
1358 1.143.8.1 itohy for (i = 0; ; ++i) {
1359 1.143.8.1 itohy err = usbd_get_device_desc(dev, &dev->ddesc);
1360 1.143.8.1 itohy if (!err || i >= 3)
1361 1.143.8.1 itohy break;
1362 1.143.8.1 itohy usbd_delay_ms(dev, 200);
1363 1.143.8.1 itohy }
1364 1.62 augustss if (err)
1365 1.62 augustss return (err);
1366 1.62 augustss
1367 1.62 augustss /* Figure out what's wrong with this device. */
1368 1.62 augustss dev->quirks = usbd_find_quirk(&dev->ddesc);
1369 1.62 augustss
1370 1.62 augustss return (USBD_NORMAL_COMPLETION);
1371 1.18 augustss }
1372 1.1 augustss
1373 1.18 augustss void
1374 1.78 augustss usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1375 1.18 augustss {
1376 1.18 augustss DPRINTF(("usbd_remove_device: %p\n", dev));
1377 1.99 augustss
1378 1.53 augustss if (dev->default_pipe != NULL)
1379 1.18 augustss usbd_kill_pipe(dev->default_pipe);
1380 1.120 augustss up->device = NULL;
1381 1.120 augustss dev->bus->devices[dev->address] = NULL;
1382 1.1 augustss
1383 1.1 augustss free(dev, M_USB);
1384 1.1 augustss }
1385 1.1 augustss
1386 1.37 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
1387 1.1 augustss int
1388 1.78 augustss usbd_print(void *aux, const char *pnp)
1389 1.1 augustss {
1390 1.1 augustss struct usb_attach_arg *uaa = aux;
1391 1.143.8.1 itohy char *devinfo;
1392 1.1 augustss
1393 1.1 augustss DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1394 1.1 augustss if (pnp) {
1395 1.1 augustss if (!uaa->usegeneric)
1396 1.1 augustss return (QUIET);
1397 1.143.8.1 itohy devinfo = usbd_devinfo_alloc(uaa->device, 1);
1398 1.101 thorpej aprint_normal("%s, %s", devinfo, pnp);
1399 1.143.8.1 itohy usbd_devinfo_free(devinfo);
1400 1.1 augustss }
1401 1.1 augustss if (uaa->port != 0)
1402 1.101 thorpej aprint_normal(" port %d", uaa->port);
1403 1.143.8.2 itohy #ifndef USB_USE_IFATTACH
1404 1.11 augustss if (uaa->configno != UHUB_UNK_CONFIGURATION)
1405 1.101 thorpej aprint_normal(" configuration %d", uaa->configno);
1406 1.11 augustss if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1407 1.101 thorpej aprint_normal(" interface %d", uaa->ifaceno);
1408 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1409 1.32 augustss #if 0
1410 1.99 augustss /*
1411 1.32 augustss * It gets very crowded with these locators on the attach line.
1412 1.32 augustss * They are not really needed since they are printed in the clear
1413 1.32 augustss * by each driver.
1414 1.32 augustss */
1415 1.32 augustss if (uaa->vendor != UHUB_UNK_VENDOR)
1416 1.101 thorpej aprint_normal(" vendor 0x%04x", uaa->vendor);
1417 1.32 augustss if (uaa->product != UHUB_UNK_PRODUCT)
1418 1.101 thorpej aprint_normal(" product 0x%04x", uaa->product);
1419 1.32 augustss if (uaa->release != UHUB_UNK_RELEASE)
1420 1.101 thorpej aprint_normal(" release 0x%04x", uaa->release);
1421 1.32 augustss #endif
1422 1.1 augustss return (UNCONF);
1423 1.1 augustss }
1424 1.1 augustss
1425 1.143.8.2 itohy #ifdef USB_USE_IFATTACH
1426 1.143.8.2 itohy int
1427 1.143.8.2 itohy usbd_ifprint(void *aux, const char *pnp)
1428 1.143.8.2 itohy {
1429 1.143.8.2 itohy struct usbif_attach_arg *uaa = aux;
1430 1.143.8.2 itohy
1431 1.143.8.2 itohy DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1432 1.143.8.2 itohy if (pnp)
1433 1.143.8.2 itohy return (QUIET);
1434 1.143.8.2 itohy if (uaa->port != 0)
1435 1.143.8.2 itohy aprint_normal(" port %d", uaa->port);
1436 1.143.8.2 itohy if (uaa->configno != UHUB_UNK_CONFIGURATION)
1437 1.143.8.2 itohy aprint_normal(" configuration %d", uaa->configno);
1438 1.143.8.2 itohy if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1439 1.143.8.2 itohy aprint_normal(" interface %d", uaa->ifaceno);
1440 1.143.8.2 itohy #if 0
1441 1.143.8.2 itohy /*
1442 1.143.8.2 itohy * It gets very crowded with these locators on the attach line.
1443 1.143.8.2 itohy * They are not really needed since they are printed in the clear
1444 1.143.8.2 itohy * by each driver.
1445 1.143.8.2 itohy */
1446 1.143.8.2 itohy if (uaa->vendor != UHUB_UNK_VENDOR)
1447 1.143.8.2 itohy aprint_normal(" vendor 0x%04x", uaa->vendor);
1448 1.143.8.2 itohy if (uaa->product != UHUB_UNK_PRODUCT)
1449 1.143.8.2 itohy aprint_normal(" product 0x%04x", uaa->product);
1450 1.143.8.2 itohy if (uaa->release != UHUB_UNK_RELEASE)
1451 1.143.8.2 itohy aprint_normal(" release 0x%04x", uaa->release);
1452 1.143.8.2 itohy #endif
1453 1.143.8.2 itohy return (UNCONF);
1454 1.143.8.2 itohy }
1455 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1456 1.143.8.2 itohy
1457 1.37 augustss #if defined(__NetBSD__)
1458 1.1 augustss int
1459 1.118 drochner usbd_submatch(struct device *parent, struct cfdata *cf,
1460 1.138 christos const int *ldesc, void *aux)
1461 1.1 augustss {
1462 1.37 augustss #elif defined(__OpenBSD__)
1463 1.37 augustss int
1464 1.78 augustss usbd_submatch(struct device *parent, void *match, void *aux)
1465 1.37 augustss {
1466 1.37 augustss struct cfdata *cf = match;
1467 1.37 augustss #endif
1468 1.1 augustss struct usb_attach_arg *uaa = aux;
1469 1.1 augustss
1470 1.143.8.2 itohy #ifdef USB_USE_IFATTACH
1471 1.143.8.2 itohy DPRINTFN(5,("usbd_submatch port=%d,%d "
1472 1.143.8.2 itohy "vendor=%d,%d product=%d,%d release=%d,%d\n",
1473 1.143.8.2 itohy uaa->port, cf->uhubcf_port,
1474 1.143.8.2 itohy uaa->vendor, cf->uhubcf_vendor,
1475 1.143.8.2 itohy uaa->product, cf->uhubcf_product,
1476 1.143.8.2 itohy uaa->release, cf->uhubcf_release));
1477 1.143.8.2 itohy if (uaa->port != 0 && /* root hub has port 0, it should match */
1478 1.143.8.2 itohy ((cf->uhubcf_port != UHUB_UNK_PORT &&
1479 1.143.8.2 itohy cf->uhubcf_port != uaa->port) ||
1480 1.143.8.2 itohy (uaa->vendor != UHUB_UNK_VENDOR &&
1481 1.143.8.2 itohy cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1482 1.143.8.2 itohy cf->uhubcf_vendor != uaa->vendor) ||
1483 1.143.8.2 itohy (uaa->product != UHUB_UNK_PRODUCT &&
1484 1.143.8.2 itohy cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1485 1.143.8.2 itohy cf->uhubcf_product != uaa->product) ||
1486 1.143.8.2 itohy (uaa->release != UHUB_UNK_RELEASE &&
1487 1.143.8.2 itohy cf->uhubcf_release != UHUB_UNK_RELEASE &&
1488 1.143.8.2 itohy cf->uhubcf_release != uaa->release)
1489 1.143.8.2 itohy )
1490 1.143.8.2 itohy )
1491 1.143.8.2 itohy return 0;
1492 1.143.8.2 itohy return (config_match(parent, cf, aux));
1493 1.143.8.2 itohy }
1494 1.143.8.2 itohy
1495 1.143.8.2 itohy int
1496 1.143.8.2 itohy usbd_ifsubmatch(struct device *parent, struct cfdata *cf,
1497 1.143.8.2 itohy const int *ldesc, void *aux)
1498 1.143.8.2 itohy {
1499 1.143.8.2 itohy struct usbif_attach_arg *uaa = aux;
1500 1.143.8.2 itohy
1501 1.143.8.2 itohy #endif /* USB_USE_IFATTACH */
1502 1.72 augustss DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1503 1.72 augustss "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1504 1.72 augustss uaa->port, cf->uhubcf_port,
1505 1.72 augustss uaa->configno, cf->uhubcf_configuration,
1506 1.72 augustss uaa->ifaceno, cf->uhubcf_interface,
1507 1.72 augustss uaa->vendor, cf->uhubcf_vendor,
1508 1.72 augustss uaa->product, cf->uhubcf_product,
1509 1.72 augustss uaa->release, cf->uhubcf_release));
1510 1.72 augustss if (uaa->port != 0 && /* root hub has port 0, it should match */
1511 1.117 drochner ((cf->uhubcf_port != UHUB_UNK_PORT &&
1512 1.72 augustss cf->uhubcf_port != uaa->port) ||
1513 1.72 augustss (uaa->configno != UHUB_UNK_CONFIGURATION &&
1514 1.72 augustss cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1515 1.72 augustss cf->uhubcf_configuration != uaa->configno) ||
1516 1.72 augustss (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1517 1.72 augustss cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1518 1.72 augustss cf->uhubcf_interface != uaa->ifaceno) ||
1519 1.72 augustss (uaa->vendor != UHUB_UNK_VENDOR &&
1520 1.72 augustss cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1521 1.72 augustss cf->uhubcf_vendor != uaa->vendor) ||
1522 1.72 augustss (uaa->product != UHUB_UNK_PRODUCT &&
1523 1.72 augustss cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1524 1.72 augustss cf->uhubcf_product != uaa->product) ||
1525 1.72 augustss (uaa->release != UHUB_UNK_RELEASE &&
1526 1.72 augustss cf->uhubcf_release != UHUB_UNK_RELEASE &&
1527 1.72 augustss cf->uhubcf_release != uaa->release)
1528 1.72 augustss )
1529 1.32 augustss )
1530 1.1 augustss return 0;
1531 1.100 thorpej return (config_match(parent, cf, aux));
1532 1.13 augustss }
1533 1.143.8.1 itohy #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1534 1.13 augustss
1535 1.13 augustss void
1536 1.82 augustss usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1537 1.82 augustss int usedev)
1538 1.13 augustss {
1539 1.13 augustss struct usbd_port *p;
1540 1.56 augustss int i, err, s;
1541 1.13 augustss
1542 1.98 christos di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1543 1.98 christos di->udi_addr = dev->address;
1544 1.98 christos di->udi_cookie = dev->cookie;
1545 1.139 pavel usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 1);
1546 1.113 itojun usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1547 1.113 itojun UGETW(dev->ddesc.bcdDevice));
1548 1.123 augustss di->udi_serial[0] = 0;
1549 1.123 augustss if (usedev)
1550 1.123 augustss (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1551 1.123 augustss di->udi_serial);
1552 1.98 christos di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1553 1.98 christos di->udi_productNo = UGETW(dev->ddesc.idProduct);
1554 1.98 christos di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1555 1.98 christos di->udi_class = dev->ddesc.bDeviceClass;
1556 1.98 christos di->udi_subclass = dev->ddesc.bDeviceSubClass;
1557 1.98 christos di->udi_protocol = dev->ddesc.bDeviceProtocol;
1558 1.98 christos di->udi_config = dev->config;
1559 1.98 christos di->udi_power = dev->self_powered ? 0 : dev->power;
1560 1.98 christos di->udi_speed = dev->speed;
1561 1.65 augustss
1562 1.65 augustss if (dev->subdevs != NULL) {
1563 1.143.8.1 itohy for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
1564 1.143.8.1 itohy #ifdef __FreeBSD__
1565 1.143.8.1 itohy /* FreeBSD may have device structure without attach. */
1566 1.143.8.1 itohy if (!device_is_attached(dev->subdevs[i]))
1567 1.143.8.1 itohy di->udi_devnames[i][0] = 0;
1568 1.143.8.1 itohy else
1569 1.143.8.1 itohy #endif
1570 1.143.8.1 itohy {
1571 1.143.8.1 itohy strlcpy(di->udi_devnames[i],
1572 1.143.8.1 itohy USBDEVPTRNAME(dev->subdevs[i]),
1573 1.143.8.1 itohy USB_MAX_DEVNAMELEN);
1574 1.143.8.1 itohy }
1575 1.143.8.1 itohy }
1576 1.143.8.1 itohy } else {
1577 1.143.8.1 itohy i = 0;
1578 1.143.8.1 itohy }
1579 1.143.8.1 itohy for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1580 1.143.8.1 itohy di->udi_devnames[i][0] = 0; /* empty */
1581 1.65 augustss
1582 1.13 augustss if (dev->hub) {
1583 1.99 augustss for (i = 0;
1584 1.98 christos i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1585 1.13 augustss i < dev->hub->hubdesc.bNbrPorts;
1586 1.13 augustss i++) {
1587 1.13 augustss p = &dev->hub->ports[i];
1588 1.13 augustss if (p->device)
1589 1.56 augustss err = p->device->address;
1590 1.13 augustss else {
1591 1.13 augustss s = UGETW(p->status.wPortStatus);
1592 1.13 augustss if (s & UPS_PORT_ENABLED)
1593 1.56 augustss err = USB_PORT_ENABLED;
1594 1.13 augustss else if (s & UPS_SUSPEND)
1595 1.56 augustss err = USB_PORT_SUSPENDED;
1596 1.13 augustss else if (s & UPS_PORT_POWER)
1597 1.56 augustss err = USB_PORT_POWERED;
1598 1.13 augustss else
1599 1.56 augustss err = USB_PORT_DISABLED;
1600 1.13 augustss }
1601 1.98 christos di->udi_ports[i] = err;
1602 1.13 augustss }
1603 1.98 christos di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1604 1.13 augustss } else
1605 1.98 christos di->udi_nports = 0;
1606 1.34 augustss }
1607 1.34 augustss
1608 1.143.8.1 itohy #if defined(__NetBSD__) && defined(COMPAT_30)
1609 1.139 pavel void
1610 1.139 pavel usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
1611 1.143.8.1 itohy int usedev)
1612 1.139 pavel {
1613 1.139 pavel struct usbd_port *p;
1614 1.139 pavel int i, err, s;
1615 1.139 pavel
1616 1.139 pavel di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1617 1.139 pavel di->udi_addr = dev->address;
1618 1.139 pavel di->udi_cookie = dev->cookie;
1619 1.139 pavel usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 0);
1620 1.139 pavel usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1621 1.139 pavel UGETW(dev->ddesc.bcdDevice));
1622 1.139 pavel di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1623 1.139 pavel di->udi_productNo = UGETW(dev->ddesc.idProduct);
1624 1.139 pavel di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1625 1.139 pavel di->udi_class = dev->ddesc.bDeviceClass;
1626 1.139 pavel di->udi_subclass = dev->ddesc.bDeviceSubClass;
1627 1.139 pavel di->udi_protocol = dev->ddesc.bDeviceProtocol;
1628 1.139 pavel di->udi_config = dev->config;
1629 1.139 pavel di->udi_power = dev->self_powered ? 0 : dev->power;
1630 1.139 pavel di->udi_speed = dev->speed;
1631 1.139 pavel
1632 1.139 pavel if (dev->subdevs != NULL) {
1633 1.139 pavel for (i = 0; dev->subdevs[i] &&
1634 1.139 pavel i < USB_MAX_DEVNAMES; i++) {
1635 1.139 pavel strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1636 1.139 pavel USB_MAX_DEVNAMELEN);
1637 1.139 pavel di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1638 1.139 pavel }
1639 1.139 pavel } else {
1640 1.139 pavel i = 0;
1641 1.139 pavel }
1642 1.139 pavel for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1643 1.139 pavel di->udi_devnames[i][0] = 0; /* empty */
1644 1.139 pavel
1645 1.139 pavel if (dev->hub) {
1646 1.139 pavel for (i = 0;
1647 1.139 pavel i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1648 1.139 pavel i < dev->hub->hubdesc.bNbrPorts;
1649 1.139 pavel i++) {
1650 1.139 pavel p = &dev->hub->ports[i];
1651 1.139 pavel if (p->device)
1652 1.139 pavel err = p->device->address;
1653 1.139 pavel else {
1654 1.139 pavel s = UGETW(p->status.wPortStatus);
1655 1.139 pavel if (s & UPS_PORT_ENABLED)
1656 1.139 pavel err = USB_PORT_ENABLED;
1657 1.139 pavel else if (s & UPS_SUSPEND)
1658 1.139 pavel err = USB_PORT_SUSPENDED;
1659 1.139 pavel else if (s & UPS_PORT_POWER)
1660 1.139 pavel err = USB_PORT_POWERED;
1661 1.139 pavel else
1662 1.139 pavel err = USB_PORT_DISABLED;
1663 1.139 pavel }
1664 1.139 pavel di->udi_ports[i] = err;
1665 1.139 pavel }
1666 1.139 pavel di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1667 1.139 pavel } else
1668 1.139 pavel di->udi_nports = 0;
1669 1.139 pavel }
1670 1.143.8.1 itohy #endif /* defined(__NetBSD__) && defined(COMPAT_30) */
1671 1.139 pavel
1672 1.139 pavel
1673 1.34 augustss void
1674 1.78 augustss usb_free_device(usbd_device_handle dev)
1675 1.34 augustss {
1676 1.34 augustss int ifcidx, nifc;
1677 1.34 augustss
1678 1.53 augustss if (dev->default_pipe != NULL)
1679 1.34 augustss usbd_kill_pipe(dev->default_pipe);
1680 1.53 augustss if (dev->ifaces != NULL) {
1681 1.34 augustss nifc = dev->cdesc->bNumInterface;
1682 1.34 augustss for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1683 1.34 augustss usbd_free_iface_data(dev, ifcidx);
1684 1.34 augustss free(dev->ifaces, M_USB);
1685 1.34 augustss }
1686 1.53 augustss if (dev->cdesc != NULL)
1687 1.34 augustss free(dev->cdesc, M_USB);
1688 1.53 augustss if (dev->subdevs != NULL)
1689 1.34 augustss free(dev->subdevs, M_USB);
1690 1.143.8.1 itohy if (dev->ifacenums != NULL)
1691 1.143.8.1 itohy free(dev->ifacenums, M_USB);
1692 1.34 augustss free(dev, M_USB);
1693 1.1 augustss }
1694 1.47 augustss
1695 1.47 augustss /*
1696 1.47 augustss * The general mechanism for detaching drivers works as follows: Each
1697 1.47 augustss * driver is responsible for maintaining a reference count on the
1698 1.47 augustss * number of outstanding references to its softc (e.g. from
1699 1.47 augustss * processing hanging in a read or write). The detach method of the
1700 1.47 augustss * driver decrements this counter and flags in the softc that the
1701 1.47 augustss * driver is dying and then wakes any sleepers. It then sleeps on the
1702 1.47 augustss * softc. Each place that can sleep must maintain the reference
1703 1.47 augustss * count. When the reference count drops to -1 (0 is the normal value
1704 1.47 augustss * of the reference count) the a wakeup on the softc is performed
1705 1.47 augustss * signaling to the detach waiter that all references are gone.
1706 1.47 augustss */
1707 1.47 augustss
1708 1.47 augustss /*
1709 1.47 augustss * Called from process context when we discover that a port has
1710 1.47 augustss * been disconnected.
1711 1.47 augustss */
1712 1.47 augustss void
1713 1.78 augustss usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1714 1.47 augustss {
1715 1.47 augustss usbd_device_handle dev = up->device;
1716 1.143.8.1 itohy const char *hubname = USBDEVPTRNAME(parent);
1717 1.47 augustss int i;
1718 1.47 augustss
1719 1.99 augustss DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1720 1.47 augustss up, dev, up->portno));
1721 1.48 augustss
1722 1.48 augustss #ifdef DIAGNOSTIC
1723 1.57 augustss if (dev == NULL) {
1724 1.48 augustss printf("usb_disconnect_port: no device\n");
1725 1.48 augustss return;
1726 1.48 augustss }
1727 1.48 augustss #endif
1728 1.47 augustss
1729 1.55 augustss if (dev->subdevs != NULL) {
1730 1.57 augustss DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1731 1.47 augustss for (i = 0; dev->subdevs[i]; i++) {
1732 1.99 augustss printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1733 1.51 augustss hubname);
1734 1.51 augustss if (up->portno != 0)
1735 1.51 augustss printf(" port %d", up->portno);
1736 1.143.8.3 itohy printf(" (addr %d) disconnected\n", dev->address);
1737 1.143.8.3 itohy config_detach(dev->subdevs[i], DETACH_FORCE);
1738 1.143.8.3 itohy dev->subdevs[i] = NULL;
1739 1.143.8.3 itohy }
1740 1.143.8.3 itohy }
1741 1.143.8.3 itohy
1742 1.143.8.3 itohy usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1743 1.143.8.3 itohy dev->bus->devices[dev->address] = NULL;
1744 1.143.8.3 itohy up->device = NULL;
1745 1.143.8.3 itohy usb_free_device(dev);
1746 1.143.8.3 itohy }
1747