Deleted Added
full compact
1/* $FreeBSD: head/sys/dev/usb/usb_device.c 193644 2009-06-07 19:41:11Z thompsa $ */
1/* $FreeBSD: head/sys/dev/usb/usb_device.c 194227 2009-06-15 00:33:18Z thompsa $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dev/usb/usb_mfunc.h>
28#include <dev/usb/usb_error.h>
29#include <dev/usb/usb.h>
30#include <dev/usb/usb_ioctl.h>
31#include "usbdevs.h"
32
33#define USB_DEBUG_VAR usb2_debug
34
35#include <dev/usb/usb_core.h>
36#include <dev/usb/usb_debug.h>
37#include <dev/usb/usb_process.h>
38#include <dev/usb/usb_device.h>
39#include <dev/usb/usb_busdma.h>
40#include <dev/usb/usb_transfer.h>
41#include <dev/usb/usb_parse.h>
42#include <dev/usb/usb_request.h>
43#include <dev/usb/usb_dynamic.h>
44#include <dev/usb/usb_hub.h>
45#include <dev/usb/usb_util.h>
46#include <dev/usb/usb_mbuf.h>
47#include <dev/usb/usb_msctest.h>
48#if USB_HAVE_UGEN
49#include <dev/usb/usb_dev.h>
50#include <dev/usb/usb_generic.h>
51#endif
52
53#include <dev/usb/quirk/usb_quirk.h>
54
55#include <dev/usb/usb_controller.h>
56#include <dev/usb/usb_bus.h>
57
58/* function prototypes */
59
60static void usb2_init_endpoint(struct usb_device *, uint8_t,
61 struct usb_endpoint_descriptor *, struct usb_endpoint *);
62static void usb2_unconfigure(struct usb_device *, uint8_t);
63static void usb2_detach_device(struct usb_device *, uint8_t, uint8_t);
64static void usb2_detach_device_sub(struct usb_device *, device_t *,
65 uint8_t);
66static uint8_t usb2_probe_and_attach_sub(struct usb_device *,
67 struct usb_attach_arg *);
68static void usb2_init_attach_arg(struct usb_device *,
69 struct usb_attach_arg *);
70static void usb2_suspend_resume_sub(struct usb_device *, device_t,
71 uint8_t);
72static void usb2_clear_stall_proc(struct usb_proc_msg *_pm);
73usb_error_t usb2_config_parse(struct usb_device *, uint8_t, uint8_t);
74static void usb2_set_device_strings(struct usb_device *);
75#if USB_HAVE_UGEN
76static void usb2_notify_addq(const char *type, struct usb_device *);
77static void usb2_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
78static struct cdev *usb2_make_dev(struct usb_device *, int, int);
79static void usb2_cdev_create(struct usb_device *);
80static void usb2_cdev_free(struct usb_device *);
81static void usb2_cdev_cleanup(void *);
82#endif
83
84/* This variable is global to allow easy access to it: */
85
86int usb2_template = 0;
87
88SYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW,
89 &usb2_template, 0, "Selected USB device side template");
90
91static const char* statestr[USB_STATE_MAX] = {
92 [USB_STATE_DETACHED] = "DETACHED",
93 [USB_STATE_ATTACHED] = "ATTACHED",
94 [USB_STATE_POWERED] = "POWERED",
95 [USB_STATE_ADDRESSED] = "ADDRESSED",
96 [USB_STATE_CONFIGURED] = "CONFIGURED",
97};
98
99const char *
100usb2_statestr(enum usb_dev_state state)
101{
102 return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
103}
104
105/*------------------------------------------------------------------------*
106 * usb2_get_ep_by_addr
107 *
108 * This function searches for an USB ep by endpoint address and
109 * direction.
110 *
111 * Returns:
112 * NULL: Failure
113 * Else: Success
114 *------------------------------------------------------------------------*/
115struct usb_endpoint *
116usb2_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
117{
118 struct usb_endpoint *ep = udev->endpoints;
119 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
120 enum {
121 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
122 };
123
124 /*
125 * According to the USB specification not all bits are used
126 * for the endpoint address. Keep defined bits only:
127 */
128 ea_val &= EA_MASK;
129
130 /*
131 * Iterate accross all the USB endpoints searching for a match
132 * based on the endpoint address:
133 */
134 for (; ep != ep_end; ep++) {
135
136 if (ep->edesc == NULL) {
137 continue;
138 }
139 /* do the mask and check the value */
140 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
141 goto found;
142 }
143 }
144
145 /*
146 * The default endpoint is always present and is checked separately:
147 */
148 if ((udev->default_ep.edesc) &&
149 ((udev->default_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
150 ep = &udev->default_ep;
151 goto found;
152 }
153 return (NULL);
154
155found:
156 return (ep);
157}
158
159/*------------------------------------------------------------------------*
160 * usb2_get_endpoint
161 *
162 * This function searches for an USB endpoint based on the information
163 * given by the passed "struct usb_config" pointer.
164 *
165 * Return values:
166 * NULL: No match.
167 * Else: Pointer to "struct usb_endpoint".
168 *------------------------------------------------------------------------*/
169struct usb_endpoint *
170usb2_get_endpoint(struct usb_device *udev, uint8_t iface_index,
171 const struct usb_config *setup)
172{
173 struct usb_endpoint *ep = udev->endpoints;
174 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
175 uint8_t index = setup->ep_index;
176 uint8_t ea_mask;
177 uint8_t ea_val;
178 uint8_t type_mask;
179 uint8_t type_val;
180
181 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
182 "type=0x%x dir=0x%x index=%d\n",
183 udev, iface_index, setup->endpoint,
184 setup->type, setup->direction, setup->ep_index);
185
186 /* check USB mode */
187
188 if (setup->usb_mode != USB_MODE_DUAL &&
189 udev->flags.usb_mode != setup->usb_mode) {
190 /* wrong mode - no endpoint */
191 return (NULL);
192 }
193
194 /* setup expected endpoint direction mask and value */
195
196 if (setup->direction == UE_DIR_RX) {
197 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
198 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
199 UE_DIR_OUT : UE_DIR_IN;
200 } else if (setup->direction == UE_DIR_TX) {
201 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
202 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
203 UE_DIR_IN : UE_DIR_OUT;
204 } else if (setup->direction == UE_DIR_ANY) {
205 /* match any endpoint direction */
206 ea_mask = 0;
207 ea_val = 0;
208 } else {
209 /* match the given endpoint direction */
210 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
211 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
212 }
213
214 /* setup expected endpoint address */
215
216 if (setup->endpoint == UE_ADDR_ANY) {
217 /* match any endpoint address */
218 } else {
219 /* match the given endpoint address */
220 ea_mask |= UE_ADDR;
221 ea_val |= (setup->endpoint & UE_ADDR);
222 }
223
224 /* setup expected endpoint type */
225
226 if (setup->type == UE_BULK_INTR) {
227 /* this will match BULK and INTERRUPT endpoints */
228 type_mask = 2;
229 type_val = 2;
230 } else if (setup->type == UE_TYPE_ANY) {
231 /* match any endpoint type */
232 type_mask = 0;
233 type_val = 0;
234 } else {
235 /* match the given endpoint type */
236 type_mask = UE_XFERTYPE;
237 type_val = (setup->type & UE_XFERTYPE);
238 }
239
240 /*
241 * Iterate accross all the USB endpoints searching for a match
242 * based on the endpoint address. Note that we are searching
243 * the endpoints from the beginning of the "udev->endpoints" array.
244 */
245 for (; ep != ep_end; ep++) {
246
247 if ((ep->edesc == NULL) ||
248 (ep->iface_index != iface_index)) {
249 continue;
250 }
251 /* do the masks and check the values */
252
253 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
254 ((ep->edesc->bmAttributes & type_mask) == type_val)) {
255 if (!index--) {
256 goto found;
257 }
258 }
259 }
260
261 /*
262 * Match against default endpoint last, so that "any endpoint", "any
263 * address" and "any direction" returns the first endpoint of the
264 * interface. "iface_index" and "direction" is ignored:
265 */
266 if ((udev->default_ep.edesc) &&
267 ((udev->default_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
268 ((udev->default_ep.edesc->bmAttributes & type_mask) == type_val) &&
269 (!index)) {
270 ep = &udev->default_ep;
271 goto found;
272 }
273 return (NULL);
274
275found:
276 return (ep);
277}
278
279/*------------------------------------------------------------------------*
280 * usb2_interface_count
281 *
282 * This function stores the number of USB interfaces excluding
283 * alternate settings, which the USB config descriptor reports into
284 * the unsigned 8-bit integer pointed to by "count".
285 *
286 * Returns:
287 * 0: Success
288 * Else: Failure
289 *------------------------------------------------------------------------*/
290usb_error_t
291usb2_interface_count(struct usb_device *udev, uint8_t *count)
292{
293 if (udev->cdesc == NULL) {
294 *count = 0;
295 return (USB_ERR_NOT_CONFIGURED);
296 }
297 *count = udev->ifaces_max;
298 return (USB_ERR_NORMAL_COMPLETION);
299}
300
301
302/*------------------------------------------------------------------------*
303 * usb2_init_endpoint
304 *
305 * This function will initialise the USB endpoint structure pointed to by
306 * the "endpoint" argument. The structure pointed to by "endpoint" must be
307 * zeroed before calling this function.
308 *------------------------------------------------------------------------*/
309static void
310usb2_init_endpoint(struct usb_device *udev, uint8_t iface_index,
311 struct usb_endpoint_descriptor *edesc, struct usb_endpoint *ep)
312{
313 struct usb_bus_methods *methods;
314
315 methods = udev->bus->methods;
316
317 (methods->endpoint_init) (udev, edesc, ep);
318
319 /* initialise USB endpoint structure */
320 ep->edesc = edesc;
321 ep->iface_index = iface_index;
322 TAILQ_INIT(&ep->endpoint_q.head);
323 ep->endpoint_q.command = &usb2_pipe_start;
324
325 /* the pipe is not supported by the hardware */
326 if (ep->methods == NULL)
327 return;
328
329 /* clear stall, if any */
330 if (methods->clear_stall != NULL) {
331 USB_BUS_LOCK(udev->bus);
332 (methods->clear_stall) (udev, ep);
333 USB_BUS_UNLOCK(udev->bus);
334 }
335}
336
337/*-----------------------------------------------------------------------*
338 * usb2_endpoint_foreach
339 *
340 * This function will iterate all the USB endpoints except the control
341 * endpoint. This function is NULL safe.
342 *
343 * Return values:
344 * NULL: End of USB endpoints
345 * Else: Pointer to next USB endpoint
346 *------------------------------------------------------------------------*/
347struct usb_endpoint *
348usb2_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
349{
350 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
351
352 /* be NULL safe */
353 if (udev == NULL)
354 return (NULL);
355
356 /* get next endpoint */
357 if (ep == NULL)
358 ep = udev->endpoints;
359 else
360 ep++;
361
362 /* find next allocated ep */
363 while (ep != ep_end) {
364 if (ep->edesc != NULL)
365 return (ep);
366 ep++;
367 }
368 return (NULL);
369}
370
371/*------------------------------------------------------------------------*
372 * usb2_unconfigure
373 *
374 * This function will free all USB interfaces and USB endpoints belonging
375 * to an USB device.
376 *
377 * Flag values, see "USB_UNCFG_FLAG_XXX".
378 *------------------------------------------------------------------------*/
379static void
380usb2_unconfigure(struct usb_device *udev, uint8_t flag)
381{
382 uint8_t do_unlock;
383
384 /* automatic locking */
385 if (sx_xlocked(udev->default_sx + 1)) {
386 do_unlock = 0;
387 } else {
388 do_unlock = 1;
389 sx_xlock(udev->default_sx + 1);
390 }
391
392 /* detach all interface drivers */
393 usb2_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
394
395#if USB_HAVE_UGEN
396 /* free all FIFOs except control endpoint FIFOs */
397 usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
398
399 /*
400 * Free all cdev's, if any.
401 */
402 usb2_cdev_free(udev);
403#endif
404
405#if USB_HAVE_COMPAT_LINUX
406 /* free Linux compat device, if any */
407 if (udev->linux_endpoint_start) {
408 usb_linux_free_device(udev);
409 udev->linux_endpoint_start = NULL;
410 }
411#endif
412
413 usb2_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
414
415 /* free "cdesc" after "ifaces" and "endpoints", if any */
416 if (udev->cdesc != NULL) {
417 if (udev->flags.usb_mode != USB_MODE_DEVICE)
418 free(udev->cdesc, M_USB);
419 udev->cdesc = NULL;
420 }
421 /* set unconfigured state */
422 udev->curr_config_no = USB_UNCONFIG_NO;
423 udev->curr_config_index = USB_UNCONFIG_INDEX;
424
425 if (do_unlock) {
426 sx_unlock(udev->default_sx + 1);
427 }
428}
429
430/*------------------------------------------------------------------------*
431 * usb2_set_config_index
432 *
433 * This function selects configuration by index, independent of the
434 * actual configuration number. This function should not be used by
435 * USB drivers.
436 *
437 * Returns:
438 * 0: Success
439 * Else: Failure
440 *------------------------------------------------------------------------*/
441usb_error_t
442usb2_set_config_index(struct usb_device *udev, uint8_t index)
443{
444 struct usb_status ds;
445 struct usb_config_descriptor *cdp;
446 uint16_t power;
447 uint16_t max_power;
448 uint8_t selfpowered;
449 uint8_t do_unlock;
450 usb_error_t err;
451
452 DPRINTFN(6, "udev=%p index=%d\n", udev, index);
453
454 /* automatic locking */
455 if (sx_xlocked(udev->default_sx + 1)) {
456 do_unlock = 0;
457 } else {
458 do_unlock = 1;
459 sx_xlock(udev->default_sx + 1);
460 }
461
462 usb2_unconfigure(udev, USB_UNCFG_FLAG_FREE_SUBDEV);
463
464 if (index == USB_UNCONFIG_INDEX) {
465 /*
466 * Leave unallocated when unconfiguring the
467 * device. "usb2_unconfigure()" will also reset
468 * the current config number and index.
469 */
470 err = usb2_req_set_config(udev, NULL, USB_UNCONFIG_NO);
471 if (udev->state == USB_STATE_CONFIGURED)
472 usb2_set_device_state(udev, USB_STATE_ADDRESSED);
473 goto done;
474 }
475 /* get the full config descriptor */
476 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
477 /* save some memory */
478 err = usb2_req_get_descriptor_ptr(udev, &cdp,
479 (UDESC_CONFIG << 8) | index);
480 } else {
481 /* normal request */
482 err = usb2_req_get_config_desc_full(udev,
483 NULL, &cdp, M_USB, index);
484 }
485 if (err) {
486 goto done;
487 }
488 /* set the new config descriptor */
489
490 udev->cdesc = cdp;
491
492 /* Figure out if the device is self or bus powered. */
493 selfpowered = 0;
494 if ((!udev->flags.uq_bus_powered) &&
495 (cdp->bmAttributes & UC_SELF_POWERED) &&
496 (udev->flags.usb_mode == USB_MODE_HOST)) {
497 /* May be self powered. */
498 if (cdp->bmAttributes & UC_BUS_POWERED) {
499 /* Must ask device. */
500 err = usb2_req_get_device_status(udev, NULL, &ds);
501 if (err) {
502 DPRINTFN(0, "could not read "
503 "device status: %s\n",
504 usb2_errstr(err));
505 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
506 selfpowered = 1;
507 }
508 DPRINTF("status=0x%04x \n",
509 UGETW(ds.wStatus));
510 } else
511 selfpowered = 1;
512 }
513 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
514 "selfpowered=%d, power=%d\n",
515 udev, cdp,
516 udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
517 selfpowered, cdp->bMaxPower * 2);
518
519 /* Check if we have enough power. */
520 power = cdp->bMaxPower * 2;
521
522 if (udev->parent_hub) {
523 max_power = udev->parent_hub->hub->portpower;
524 } else {
525 max_power = USB_MAX_POWER;
526 }
527
528 if (power > max_power) {
529 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
530 err = USB_ERR_NO_POWER;
531 goto done;
532 }
533 /* Only update "self_powered" in USB Host Mode */
534 if (udev->flags.usb_mode == USB_MODE_HOST) {
535 udev->flags.self_powered = selfpowered;
536 }
537 udev->power = power;
538 udev->curr_config_no = cdp->bConfigurationValue;
539 udev->curr_config_index = index;
540 usb2_set_device_state(udev, USB_STATE_CONFIGURED);
541
542 /* Set the actual configuration value. */
543 err = usb2_req_set_config(udev, NULL, cdp->bConfigurationValue);
544 if (err) {
545 goto done;
546 }
547
548 err = usb2_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
549 if (err) {
550 goto done;
551 }
552
553 err = usb2_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
554 if (err) {
555 goto done;
556 }
557
558#if USB_HAVE_UGEN
559 /* create device nodes for each endpoint */
560 usb2_cdev_create(udev);
561#endif
562
563done:
564 DPRINTF("error=%s\n", usb2_errstr(err));
565 if (err) {
566 usb2_unconfigure(udev, USB_UNCFG_FLAG_FREE_SUBDEV);
567 }
568 if (do_unlock) {
569 sx_unlock(udev->default_sx + 1);
570 }
571 return (err);
572}
573
574/*------------------------------------------------------------------------*
575 * usb2_config_parse
576 *
577 * This function will allocate and free USB interfaces and USB endpoints,
578 * parse the USB configuration structure and initialise the USB endpoints
579 * and interfaces. If "iface_index" is not equal to
580 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
581 * alternate_setting to be selected for the given interface. Else the
582 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
583 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
584 * is typically called when setting the configuration or when setting
585 * an alternate interface.
586 *
587 * Returns:
588 * 0: Success
589 * Else: Failure
590 *------------------------------------------------------------------------*/
591usb_error_t
592usb2_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
593{
594 struct usb_idesc_parse_state ips;
595 struct usb_interface_descriptor *id;
596 struct usb_endpoint_descriptor *ed;
597 struct usb_interface *iface;
598 struct usb_endpoint *ep;
599 usb_error_t err;
600 uint8_t ep_curr;
601 uint8_t ep_max;
602 uint8_t temp;
603 uint8_t do_init;
604 uint8_t alt_index;
605
606 if (iface_index != USB_IFACE_INDEX_ANY) {
607 /* parameter overload */
608 alt_index = cmd;
609 cmd = USB_CFG_INIT;
610 } else {
611 /* not used */
612 alt_index = 0;
613 }
614
615 err = 0;
616
617 DPRINTFN(5, "iface_index=%d cmd=%d\n",
618 iface_index, cmd);
619
620 if (cmd == USB_CFG_FREE)
621 goto cleanup;
622
623 if (cmd == USB_CFG_INIT) {
624 sx_assert(udev->default_sx + 1, SA_LOCKED);
625
626 /* check for in-use endpoints */
627
628 ep = udev->endpoints;
629 ep_max = udev->endpoints_max;
630 while (ep_max--) {
631 /* look for matching endpoints */
632 if ((iface_index == USB_IFACE_INDEX_ANY) ||
633 (iface_index == ep->iface_index)) {
634 if (ep->refcount != 0) {
635 /*
636 * This typically indicates a
637 * more serious error.
638 */
639 err = USB_ERR_IN_USE;
640 } else {
641 /* reset endpoint */
642 memset(ep, 0, sizeof(*ep));
643 /* make sure we don't zero the endpoint again */
644 ep->iface_index = USB_IFACE_INDEX_ANY;
645 }
646 }
647 ep++;
648 }
649
650 if (err)
651 return (err);
652 }
653
654 memset(&ips, 0, sizeof(ips));
655
656 ep_curr = 0;
657 ep_max = 0;
658
659 while ((id = usb2_idesc_foreach(udev->cdesc, &ips))) {
660
661 /* check for interface overflow */
662 if (ips.iface_index == USB_IFACE_MAX)
663 break; /* crazy */
664
665 iface = udev->ifaces + ips.iface_index;
666
667 /* check for specific interface match */
668
669 if (cmd == USB_CFG_INIT) {
670 if ((iface_index != USB_IFACE_INDEX_ANY) &&
671 (iface_index != ips.iface_index)) {
672 /* wrong interface */
673 do_init = 0;
674 } else if (alt_index != ips.iface_index_alt) {
675 /* wrong alternate setting */
676 do_init = 0;
677 } else {
678 /* initialise interface */
679 do_init = 1;
680 }
681 } else
682 do_init = 0;
683
684 /* check for new interface */
685 if (ips.iface_index_alt == 0) {
686 /* update current number of endpoints */
687 ep_curr = ep_max;
688 }
689 /* check for init */
690 if (do_init) {
691 /* setup the USB interface structure */
692 iface->idesc = id;
693 /* default setting */
694 iface->parent_iface_index = USB_IFACE_INDEX_ANY;
695 /* set alternate index */
696 iface->alt_index = alt_index;
697 }
698
699 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
700
701 ed = (struct usb_endpoint_descriptor *)id;
702
703 temp = ep_curr;
704
705 /* iterate all the endpoint descriptors */
706 while ((ed = usb2_edesc_foreach(udev->cdesc, ed))) {
707
708 if (temp == USB_EP_MAX)
709 break; /* crazy */
710
711 ep = udev->endpoints + temp;
712
713 if (do_init) {
714 usb2_init_endpoint(udev,
715 ips.iface_index, ed, ep);
716 }
717
718 temp ++;
719
720 /* find maximum number of endpoints */
721 if (ep_max < temp)
722 ep_max = temp;
723
724 /* optimalisation */
725 id = (struct usb_interface_descriptor *)ed;
726 }
727 }
728
729 /* NOTE: It is valid to have no interfaces and no endpoints! */
730
731 if (cmd == USB_CFG_ALLOC) {
732 udev->ifaces_max = ips.iface_index;
733 udev->ifaces = NULL;
734 if (udev->ifaces_max != 0) {
735 udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
736 M_USB, M_WAITOK | M_ZERO);
737 if (udev->ifaces == NULL) {
738 err = USB_ERR_NOMEM;
739 goto done;
740 }
741 }
742 if (ep_max != 0) {
743 udev->endpoints = malloc(sizeof(*ep) * ep_max,
744 M_USB, M_WAITOK | M_ZERO);
745 if (udev->endpoints == NULL) {
746 err = USB_ERR_NOMEM;
747 goto done;
748 }
749 } else {
750 udev->endpoints = NULL;
751 }
752 USB_BUS_LOCK(udev->bus);
753 udev->endpoints_max = ep_max;
754 /* reset any ongoing clear-stall */
755 udev->ep_curr = NULL;
756 USB_BUS_UNLOCK(udev->bus);
757 }
758
759done:
760 if (err) {
761 if (cmd == USB_CFG_ALLOC) {
762cleanup:
763 USB_BUS_LOCK(udev->bus);
764 udev->endpoints_max = 0;
765 /* reset any ongoing clear-stall */
766 udev->ep_curr = NULL;
767 USB_BUS_UNLOCK(udev->bus);
768
769 /* cleanup */
770 if (udev->ifaces != NULL)
771 free(udev->ifaces, M_USB);
772 if (udev->endpoints != NULL)
773 free(udev->endpoints, M_USB);
774
775 udev->ifaces = NULL;
776 udev->endpoints = NULL;
777 udev->ifaces_max = 0;
778 }
779 }
780 return (err);
781}
782
783/*------------------------------------------------------------------------*
784 * usb2_set_alt_interface_index
785 *
786 * This function will select an alternate interface index for the
787 * given interface index. The interface should not be in use when this
788 * function is called. That means there should not be any open USB
789 * transfers. Else an error is returned. If the alternate setting is
790 * already set this function will simply return success. This function
791 * is called in Host mode and Device mode!
792 *
793 * Returns:
794 * 0: Success
795 * Else: Failure
796 *------------------------------------------------------------------------*/
797usb_error_t
798usb2_set_alt_interface_index(struct usb_device *udev,
799 uint8_t iface_index, uint8_t alt_index)
800{
801 struct usb_interface *iface = usb2_get_iface(udev, iface_index);
802 usb_error_t err;
803 uint8_t do_unlock;
804
805 /* automatic locking */
806 if (sx_xlocked(udev->default_sx + 1)) {
807 do_unlock = 0;
808 } else {
809 do_unlock = 1;
810 sx_xlock(udev->default_sx + 1);
811 }
812 if (iface == NULL) {
813 err = USB_ERR_INVAL;
814 goto done;
815 }
816 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
817 usb2_detach_device(udev, iface_index,
818 USB_UNCFG_FLAG_FREE_SUBDEV);
819 } else {
820 if (iface->alt_index == alt_index) {
821 /*
822 * Optimise away duplicate setting of
823 * alternate setting in USB Host Mode!
824 */
825 err = 0;
826 goto done;
827 }
828 }
829#if USB_HAVE_UGEN
830 /*
831 * Free all generic FIFOs for this interface, except control
832 * endpoint FIFOs:
833 */
834 usb2_fifo_free_wrap(udev, iface_index, 0);
835#endif
836
837 err = usb2_config_parse(udev, iface_index, alt_index);
838 if (err) {
839 goto done;
840 }
841 err = usb2_req_set_alt_interface_no(udev, NULL, iface_index,
842 iface->idesc->bAlternateSetting);
843
844done:
845 if (do_unlock) {
846 sx_unlock(udev->default_sx + 1);
847 }
848 return (err);
849}
850
851/*------------------------------------------------------------------------*
852 * usb2_set_endpoint_stall
853 *
854 * This function is used to make a BULK or INTERRUPT endpoint
855 * send STALL tokens.
856 *
857 * Returns:
858 * 0: Success
859 * Else: Failure
860 *------------------------------------------------------------------------*/
861usb_error_t
862usb2_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
863 uint8_t do_stall)
864{
865 struct usb_xfer *xfer;
866 uint8_t et;
867 uint8_t was_stalled;
868
869 if (ep == NULL) {
870 /* nothing to do */
871 DPRINTF("Cannot find endpoint\n");
872 /*
873 * Pretend that the clear or set stall request is
874 * successful else some USB host stacks can do
875 * strange things, especially when a control endpoint
876 * stalls.
877 */
878 return (0);
879 }
880 et = (ep->edesc->bmAttributes & UE_XFERTYPE);
881
882 if ((et != UE_BULK) &&
883 (et != UE_INTERRUPT)) {
884 /*
885 * Should not stall control
886 * nor isochronous endpoints.
887 */
888 DPRINTF("Invalid endpoint\n");
889 return (0);
890 }
891 USB_BUS_LOCK(udev->bus);
892
893 /* store current stall state */
894 was_stalled = ep->is_stalled;
895
896 /* check for no change */
897 if (was_stalled && do_stall) {
898 /* if the endpoint is already stalled do nothing */
899 USB_BUS_UNLOCK(udev->bus);
900 DPRINTF("No change\n");
901 return (0);
902 }
903 /* set stalled state */
904 ep->is_stalled = 1;
905
906 if (do_stall || (!was_stalled)) {
907 if (!was_stalled) {
908 /* lookup the current USB transfer, if any */
909 xfer = ep->endpoint_q.curr;
910 } else {
911 xfer = NULL;
912 }
913
914 /*
915 * If "xfer" is non-NULL the "set_stall" method will
916 * complete the USB transfer like in case of a timeout
917 * setting the error code "USB_ERR_STALLED".
918 */
919 (udev->bus->methods->set_stall) (udev, xfer, ep);
920 }
921 if (!do_stall) {
922 ep->toggle_next = 0; /* reset data toggle */
923 ep->is_stalled = 0; /* clear stalled state */
924
925 (udev->bus->methods->clear_stall) (udev, ep);
926
927 /* start up the current or next transfer, if any */
928 usb2_command_wrapper(&ep->endpoint_q, ep->endpoint_q.curr);
929 }
930 USB_BUS_UNLOCK(udev->bus);
931 return (0);
932}
933
934/*------------------------------------------------------------------------*
935 * usb2_reset_iface_endpoints - used in USB device side mode
936 *------------------------------------------------------------------------*/
937usb_error_t
938usb2_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
939{
940 struct usb_endpoint *ep;
941 struct usb_endpoint *ep_end;
942 usb_error_t err;
943
944 ep = udev->endpoints;
945 ep_end = udev->endpoints + udev->endpoints_max;
946
947 for (; ep != ep_end; ep++) {
948
949 if ((ep->edesc == NULL) ||
950 (ep->iface_index != iface_index)) {
951 continue;
952 }
953 /* simulate a clear stall from the peer */
954 err = usb2_set_endpoint_stall(udev, ep, 0);
955 if (err) {
956 /* just ignore */
957 }
958 }
959 return (0);
960}
961
962/*------------------------------------------------------------------------*
963 * usb2_detach_device_sub
964 *
965 * This function will try to detach an USB device. If it fails a panic
966 * will result.
967 *
968 * Flag values, see "USB_UNCFG_FLAG_XXX".
969 *------------------------------------------------------------------------*/
970static void
971usb2_detach_device_sub(struct usb_device *udev, device_t *ppdev,
972 uint8_t flag)
973{
974 device_t dev;
975 int err;
976
977 if (!(flag & USB_UNCFG_FLAG_FREE_SUBDEV)) {
978
979 *ppdev = NULL;
980
981 } else if (*ppdev) {
982
983 /*
984 * NOTE: It is important to clear "*ppdev" before deleting
985 * the child due to some device methods being called late
986 * during the delete process !
987 */
988 dev = *ppdev;
989 *ppdev = NULL;
990
991 device_printf(dev, "at %s, port %d, addr %d "
992 "(disconnected)\n",
993 device_get_nameunit(udev->parent_dev),
994 udev->port_no, udev->address);
995
996 if (device_is_attached(dev)) {
997 if (udev->flags.peer_suspended) {
998 err = DEVICE_RESUME(dev);
999 if (err) {
1000 device_printf(dev, "Resume failed!\n");
1001 }
1002 }
1003 if (device_detach(dev)) {
1004 goto error;
1005 }
1006 }
1007 if (device_delete_child(udev->parent_dev, dev)) {
1008 goto error;
1009 }
1010 }
1011 return;
1012
1013error:
1014 /* Detach is not allowed to fail in the USB world */
1015 panic("An USB driver would not detach!\n");
1016}
1017
1018/*------------------------------------------------------------------------*
1019 * usb2_detach_device
1020 *
1021 * The following function will detach the matching interfaces.
1022 * This function is NULL safe.
1023 *
1024 * Flag values, see "USB_UNCFG_FLAG_XXX".
1025 *------------------------------------------------------------------------*/
1026void
1027usb2_detach_device(struct usb_device *udev, uint8_t iface_index,
1028 uint8_t flag)
1029{
1030 struct usb_interface *iface;
1031 uint8_t i;
1032
1033 if (udev == NULL) {
1034 /* nothing to do */
1035 return;
1036 }
1037 DPRINTFN(4, "udev=%p\n", udev);
1038
1039 sx_assert(udev->default_sx + 1, SA_LOCKED);
1040
1041 /*
1042 * First detach the child to give the child's detach routine a
1043 * chance to detach the sub-devices in the correct order.
1044 * Then delete the child using "device_delete_child()" which
1045 * will detach all sub-devices from the bottom and upwards!
1046 */
1047 if (iface_index != USB_IFACE_INDEX_ANY) {
1048 i = iface_index;
1049 iface_index = i + 1;
1050 } else {
1051 i = 0;
1052 iface_index = USB_IFACE_MAX;
1053 }
1054
1055 /* do the detach */
1056
1057 for (; i != iface_index; i++) {
1058
1059 iface = usb2_get_iface(udev, i);
1060 if (iface == NULL) {
1061 /* looks like the end of the USB interfaces */
1062 break;
1063 }
1064 usb2_detach_device_sub(udev, &iface->subdev, flag);
1065 }
1066}
1067
1068/*------------------------------------------------------------------------*
1069 * usb2_probe_and_attach_sub
1070 *
1071 * Returns:
1072 * 0: Success
1073 * Else: Failure
1074 *------------------------------------------------------------------------*/
1075static uint8_t
1076usb2_probe_and_attach_sub(struct usb_device *udev,
1077 struct usb_attach_arg *uaa)
1078{
1079 struct usb_interface *iface;
1080 device_t dev;
1081 int err;
1082
1083 iface = uaa->iface;
1084 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1085 /* leave interface alone */
1086 return (0);
1087 }
1088 dev = iface->subdev;
1089 if (dev) {
1090
1091 /* clean up after module unload */
1092
1093 if (device_is_attached(dev)) {
1094 /* already a device there */
1095 return (0);
1096 }
1097 /* clear "iface->subdev" as early as possible */
1098
1099 iface->subdev = NULL;
1100
1101 if (device_delete_child(udev->parent_dev, dev)) {
1102
1103 /*
1104 * Panic here, else one can get a double call
1105 * to device_detach(). USB devices should
1106 * never fail on detach!
1107 */
1108 panic("device_delete_child() failed!\n");
1109 }
1110 }
1111 if (uaa->temp_dev == NULL) {
1112
1113 /* create a new child */
1114 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1115 if (uaa->temp_dev == NULL) {
1116 device_printf(udev->parent_dev,
1117 "Device creation failed!\n");
1118 return (1); /* failure */
1119 }
1120 device_set_ivars(uaa->temp_dev, uaa);
1121 device_quiet(uaa->temp_dev);
1122 }
1123 /*
1124 * Set "subdev" before probe and attach so that "devd" gets
1125 * the information it needs.
1126 */
1127 iface->subdev = uaa->temp_dev;
1128
1129 if (device_probe_and_attach(iface->subdev) == 0) {
1130 /*
1131 * The USB attach arguments are only available during probe
1132 * and attach !
1133 */
1134 uaa->temp_dev = NULL;
1135 device_set_ivars(iface->subdev, NULL);
1136
1137 if (udev->flags.peer_suspended) {
1138 err = DEVICE_SUSPEND(iface->subdev);
1139 if (err)
1140 device_printf(iface->subdev, "Suspend failed\n");
1141 }
1142 return (0); /* success */
1143 } else {
1144 /* No USB driver found */
1145 iface->subdev = NULL;
1146 }
1147 return (1); /* failure */
1148}
1149
1150/*------------------------------------------------------------------------*
1151 * usb2_set_parent_iface
1152 *
1153 * Using this function will lock the alternate interface setting on an
1154 * interface. It is typically used for multi interface drivers. In USB
1155 * device side mode it is assumed that the alternate interfaces all
1156 * have the same endpoint descriptors. The default parent index value
1157 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1158 * locked.
1159 *------------------------------------------------------------------------*/
1160void
1161usb2_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1162 uint8_t parent_index)
1163{
1164 struct usb_interface *iface;
1165
1166 iface = usb2_get_iface(udev, iface_index);
1167 if (iface) {
1168 iface->parent_iface_index = parent_index;
1169 }
1170}
1171
1172static void
1173usb2_init_attach_arg(struct usb_device *udev,
1174 struct usb_attach_arg *uaa)
1175{
1176 bzero(uaa, sizeof(*uaa));
1177
1178 uaa->device = udev;
1179 uaa->usb_mode = udev->flags.usb_mode;
1180 uaa->port = udev->port_no;
1181
1182 uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1183 uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1184 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1185 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1186 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1187 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1188 uaa->info.bConfigIndex = udev->curr_config_index;
1189 uaa->info.bConfigNum = udev->curr_config_no;
1190}
1191
1192/*------------------------------------------------------------------------*
1193 * usb2_probe_and_attach
1194 *
1195 * This function is called from "uhub_explore_sub()",
1196 * "usb2_handle_set_config()" and "usb2_handle_request()".
1197 *
1198 * Returns:
1199 * 0: Success
1200 * Else: A control transfer failed
1201 *------------------------------------------------------------------------*/
1202usb_error_t
1203usb2_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1204{
1205 struct usb_attach_arg uaa;
1206 struct usb_interface *iface;
1207 uint8_t i;
1208 uint8_t j;
1209 uint8_t do_unlock;
1210
1211 if (udev == NULL) {
1212 DPRINTF("udev == NULL\n");
1213 return (USB_ERR_INVAL);
1214 }
1215 /* automatic locking */
1216 if (sx_xlocked(udev->default_sx + 1)) {
1217 do_unlock = 0;
1218 } else {
1219 do_unlock = 1;
1220 sx_xlock(udev->default_sx + 1);
1221 }
1222
1223 if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1224 /* do nothing - no configuration has been set */
1225 goto done;
1226 }
1227 /* setup USB attach arguments */
1228
1229 usb2_init_attach_arg(udev, &uaa);
1230
1231 /* Check if only one interface should be probed: */
1232 if (iface_index != USB_IFACE_INDEX_ANY) {
1233 i = iface_index;
1234 j = i + 1;
1235 } else {
1236 i = 0;
1237 j = USB_IFACE_MAX;
1238 }
1239
1240 /* Do the probe and attach */
1241 for (; i != j; i++) {
1242
1243 iface = usb2_get_iface(udev, i);
1244 if (iface == NULL) {
1245 /*
1246 * Looks like the end of the USB
1247 * interfaces !
1248 */
1249 DPRINTFN(2, "end of interfaces "
1250 "at %u\n", i);
1251 break;
1252 }
1253 if (iface->idesc == NULL) {
1254 /* no interface descriptor */
1255 continue;
1256 }
1257 uaa.iface = iface;
1258
1259 uaa.info.bInterfaceClass =
1260 iface->idesc->bInterfaceClass;
1261 uaa.info.bInterfaceSubClass =
1262 iface->idesc->bInterfaceSubClass;
1263 uaa.info.bInterfaceProtocol =
1264 iface->idesc->bInterfaceProtocol;
1265 uaa.info.bIfaceIndex = i;
1266 uaa.info.bIfaceNum =
1267 iface->idesc->bInterfaceNumber;
1268 uaa.use_generic = 0;
1269
1270 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1271 uaa.info.bInterfaceClass,
1272 uaa.info.bInterfaceSubClass,
1273 uaa.info.bInterfaceProtocol,
1274 uaa.info.bIfaceIndex,
1275 uaa.info.bIfaceNum);
1276
1277 /* try specific interface drivers first */
1278
1279 if (usb2_probe_and_attach_sub(udev, &uaa)) {
1280 /* ignore */
1281 }
1282 /* try generic interface drivers last */
1283
1284 uaa.use_generic = 1;
1285
1286 if (usb2_probe_and_attach_sub(udev, &uaa)) {
1287 /* ignore */
1288 }
1289 }
1290
1291 if (uaa.temp_dev) {
1292 /* remove the last created child; it is unused */
1293
1294 if (device_delete_child(udev->parent_dev, uaa.temp_dev)) {
1295 DPRINTFN(0, "device delete child failed!\n");
1296 }
1297 }
1298done:
1299 if (do_unlock) {
1300 sx_unlock(udev->default_sx + 1);
1301 }
1302 return (0);
1303}
1304
1305/*------------------------------------------------------------------------*
1306 * usb2_suspend_resume_sub
1307 *
1308 * This function is called when the suspend or resume methods should
1309 * be executed on an USB device.
1310 *------------------------------------------------------------------------*/
1311static void
1312usb2_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1313{
1314 int err;
1315
1316 if (dev == NULL) {
1317 return;
1318 }
1319 if (!device_is_attached(dev)) {
1320 return;
1321 }
1322 if (do_suspend) {
1323 err = DEVICE_SUSPEND(dev);
1324 } else {
1325 err = DEVICE_RESUME(dev);
1326 }
1327 if (err) {
1328 device_printf(dev, "%s failed!\n",
1329 do_suspend ? "Suspend" : "Resume");
1330 }
1331}
1332
1333/*------------------------------------------------------------------------*
1334 * usb2_suspend_resume
1335 *
1336 * The following function will suspend or resume the USB device.
1337 *
1338 * Returns:
1339 * 0: Success
1340 * Else: Failure
1341 *------------------------------------------------------------------------*/
1342usb_error_t
1343usb2_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1344{
1345 struct usb_interface *iface;
1346 uint8_t i;
1347
1348 if (udev == NULL) {
1349 /* nothing to do */
1350 return (0);
1351 }
1352 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1353
1354 sx_assert(udev->default_sx + 1, SA_LOCKED);
1355
1356 USB_BUS_LOCK(udev->bus);
1357 /* filter the suspend events */
1358 if (udev->flags.peer_suspended == do_suspend) {
1359 USB_BUS_UNLOCK(udev->bus);
1360 /* nothing to do */
1361 return (0);
1362 }
1363 udev->flags.peer_suspended = do_suspend;
1364 USB_BUS_UNLOCK(udev->bus);
1365
1366 /* do the suspend or resume */
1367
1368 for (i = 0; i != USB_IFACE_MAX; i++) {
1369
1370 iface = usb2_get_iface(udev, i);
1371 if (iface == NULL) {
1372 /* looks like the end of the USB interfaces */
1373 break;
1374 }
1375 usb2_suspend_resume_sub(udev, iface->subdev, do_suspend);
1376 }
1377 return (0);
1378}
1379
1380/*------------------------------------------------------------------------*
1381 * usb2_clear_stall_proc
1382 *
1383 * This function performs generic USB clear stall operations.
1384 *------------------------------------------------------------------------*/
1385static void
1386usb2_clear_stall_proc(struct usb_proc_msg *_pm)
1387{
1388 struct usb_clear_stall_msg *pm = (void *)_pm;
1389 struct usb_device *udev = pm->udev;
1390
1391 /* Change lock */
1392 USB_BUS_UNLOCK(udev->bus);
1393 mtx_lock(udev->default_mtx);
1394
1395 /* Start clear stall callback */
1396 usb2_transfer_start(udev->default_xfer[1]);
1397
1398 /* Change lock */
1399 mtx_unlock(udev->default_mtx);
1400 USB_BUS_LOCK(udev->bus);
1401}
1402
1403/*------------------------------------------------------------------------*
1404 * usb2_alloc_device
1405 *
1406 * This function allocates a new USB device. This function is called
1407 * when a new device has been put in the powered state, but not yet in
1408 * the addressed state. Get initial descriptor, set the address, get
1409 * full descriptor and get strings.
1410 *
1411 * Return values:
1412 * 0: Failure
1413 * Else: Success
1414 *------------------------------------------------------------------------*/
1415struct usb_device *
1416usb2_alloc_device(device_t parent_dev, struct usb_bus *bus,
1417 struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1418 uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1419{
1420 struct usb_attach_arg uaa;
1421 struct usb_device *udev;
1422 struct usb_device *adev;
1423 struct usb_device *hub;
1424 uint8_t *scratch_ptr;
1425 uint32_t scratch_size;
1426 usb_error_t err;
1427 uint8_t device_index;
1428
1429 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1430 "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1431 parent_dev, bus, parent_hub, depth, port_index, port_no,
1432 speed, mode);
1433
1434 /*
1435 * Find an unused device index. In USB Host mode this is the
1436 * same as the device address.
1437 *
1438 * Device index zero is not used and device index 1 should
1439 * always be the root hub.
1440 */
1441 for (device_index = USB_ROOT_HUB_ADDR;
1442 (device_index != bus->devices_max) &&
1443 (bus->devices[device_index] != NULL);
1444 device_index++) /* nop */;
1445
1446 if (device_index == bus->devices_max) {
1447 device_printf(bus->bdev,
1448 "No free USB device index for new device!\n");
1449 return (NULL);
1450 }
1451
1452 if (depth > 0x10) {
1453 device_printf(bus->bdev,
1454 "Invalid device depth!\n");
1455 return (NULL);
1456 }
1457 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1458 if (udev == NULL) {
1459 return (NULL);
1460 }
1461 /* initialise our SX-lock */
1462 sx_init(udev->default_sx, "0123456789ABCDEF - USB device SX lock" + depth);
1463
1464 /* initialise our SX-lock */
1465 sx_init(udev->default_sx + 1, "0123456789ABCDEF - USB config SX lock" + depth);
1466
1467 usb2_cv_init(udev->default_cv, "WCTRL");
1468 usb2_cv_init(udev->default_cv + 1, "UGONE");
1467 cv_init(udev->default_cv, "WCTRL");
1468 cv_init(udev->default_cv + 1, "UGONE");
1469
1470 /* initialise our mutex */
1471 mtx_init(udev->default_mtx, "USB device mutex", NULL, MTX_DEF);
1472
1473 /* initialise generic clear stall */
1474 udev->cs_msg[0].hdr.pm_callback = &usb2_clear_stall_proc;
1475 udev->cs_msg[0].udev = udev;
1476 udev->cs_msg[1].hdr.pm_callback = &usb2_clear_stall_proc;
1477 udev->cs_msg[1].udev = udev;
1478
1479 /* initialise some USB device fields */
1480 udev->parent_hub = parent_hub;
1481 udev->parent_dev = parent_dev;
1482 udev->port_index = port_index;
1483 udev->port_no = port_no;
1484 udev->depth = depth;
1485 udev->bus = bus;
1486 udev->address = USB_START_ADDR; /* default value */
1487 udev->plugtime = (usb_ticks_t)ticks;
1488 usb2_set_device_state(udev, USB_STATE_POWERED);
1489 /*
1490 * We need to force the power mode to "on" because there are plenty
1491 * of USB devices out there that do not work very well with
1492 * automatic suspend and resume!
1493 */
1494 udev->power_mode = USB_POWER_MODE_ON;
1495 udev->pwr_save.last_xfer_time = ticks;
1496 /* we are not ready yet */
1497 udev->refcount = 1;
1498
1499 /* set up default endpoint descriptor */
1500 udev->default_ep_desc.bLength = sizeof(udev->default_ep_desc);
1501 udev->default_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1502 udev->default_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1503 udev->default_ep_desc.bmAttributes = UE_CONTROL;
1504 udev->default_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1505 udev->default_ep_desc.wMaxPacketSize[1] = 0;
1506 udev->default_ep_desc.bInterval = 0;
1507 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1508
1509 udev->speed = speed;
1510 udev->flags.usb_mode = mode;
1511
1512 /* search for our High Speed USB HUB, if any */
1513
1514 adev = udev;
1515 hub = udev->parent_hub;
1516
1517 while (hub) {
1518 if (hub->speed == USB_SPEED_HIGH) {
1519 udev->hs_hub_addr = hub->address;
1520 udev->parent_hs_hub = hub;
1521 udev->hs_port_no = adev->port_no;
1522 break;
1523 }
1524 adev = hub;
1525 hub = hub->parent_hub;
1526 }
1527
1528 /* init the default endpoint */
1529 usb2_init_endpoint(udev, 0,
1530 &udev->default_ep_desc,
1531 &udev->default_ep);
1532
1533 /* set device index */
1534 udev->device_index = device_index;
1535
1536#if USB_HAVE_UGEN
1537 /* Create ugen name */
1538 snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1539 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1540 device_index);
1541 LIST_INIT(&udev->pd_list);
1542
1543 /* Create the control endpoint device */
1544 udev->default_dev = usb2_make_dev(udev, 0, FREAD|FWRITE);
1545
1546 /* Create a link from /dev/ugenX.X to the default endpoint */
1547 make_dev_alias(udev->default_dev, udev->ugen_name);
1548#endif
1549 if (udev->flags.usb_mode == USB_MODE_HOST) {
1550
1551 err = usb2_req_set_address(udev, NULL, device_index);
1552
1553 /* This is the new USB device address from now on */
1554
1555 udev->address = device_index;
1556
1557 /*
1558 * We ignore any set-address errors, hence there are
1559 * buggy USB devices out there that actually receive
1560 * the SETUP PID, but manage to set the address before
1561 * the STATUS stage is ACK'ed. If the device responds
1562 * to the subsequent get-descriptor at the new
1563 * address, then we know that the set-address command
1564 * was successful.
1565 */
1566 if (err) {
1567 DPRINTFN(0, "set address %d failed "
1568 "(%s, ignored)\n", udev->address,
1569 usb2_errstr(err));
1570 }
1571 /* allow device time to set new address */
1572 usb2_pause_mtx(NULL,
1573 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1574 } else {
1575 /* We are not self powered */
1576 udev->flags.self_powered = 0;
1577
1578 /* Set unconfigured state */
1579 udev->curr_config_no = USB_UNCONFIG_NO;
1580 udev->curr_config_index = USB_UNCONFIG_INDEX;
1581
1582 /* Setup USB descriptors */
1583 err = (usb2_temp_setup_by_index_p) (udev, usb2_template);
1584 if (err) {
1585 DPRINTFN(0, "setting up USB template failed maybe the USB "
1586 "template module has not been loaded\n");
1587 goto done;
1588 }
1589 }
1590 usb2_set_device_state(udev, USB_STATE_ADDRESSED);
1591
1592 /*
1593 * Get the first 8 bytes of the device descriptor !
1594 *
1595 * NOTE: "usb2_do_request" will check the device descriptor
1596 * next time we do a request to see if the maximum packet size
1597 * changed! The 8 first bytes of the device descriptor
1598 * contains the maximum packet size to use on control endpoint
1599 * 0. If this value is different from "USB_MAX_IPACKET" a new
1600 * USB control request will be setup!
1601 */
1602 err = usb2_req_get_desc(udev, NULL, NULL, &udev->ddesc,
1603 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1604 if (err) {
1605 DPRINTFN(0, "getting device descriptor "
1606 "at addr %d failed, %s!\n", udev->address,
1607 usb2_errstr(err));
1608 /* XXX try to re-enumerate the device */
1609 err = usb2_req_re_enumerate(udev, NULL);
1610 if (err) {
1611 goto done;
1612 }
1613 }
1614 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1615 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1616 udev->address, UGETW(udev->ddesc.bcdUSB),
1617 udev->ddesc.bDeviceClass,
1618 udev->ddesc.bDeviceSubClass,
1619 udev->ddesc.bDeviceProtocol,
1620 udev->ddesc.bMaxPacketSize,
1621 udev->ddesc.bLength,
1622 udev->speed);
1623
1624 /* get the full device descriptor */
1625 err = usb2_req_get_device_desc(udev, NULL, &udev->ddesc);
1626 if (err) {
1627 DPRINTF("addr=%d, getting full desc failed\n",
1628 udev->address);
1629 goto done;
1630 }
1631 /*
1632 * Setup temporary USB attach args so that we can figure out some
1633 * basic quirks for this device.
1634 */
1635 usb2_init_attach_arg(udev, &uaa);
1636
1637 if (usb2_test_quirk(&uaa, UQ_BUS_POWERED)) {
1638 udev->flags.uq_bus_powered = 1;
1639 }
1640 if (usb2_test_quirk(&uaa, UQ_NO_STRINGS)) {
1641 udev->flags.no_strings = 1;
1642 }
1643 /*
1644 * Workaround for buggy USB devices.
1645 *
1646 * It appears that some string-less USB chips will crash and
1647 * disappear if any attempts are made to read any string
1648 * descriptors.
1649 *
1650 * Try to detect such chips by checking the strings in the USB
1651 * device descriptor. If no strings are present there we
1652 * simply disable all USB strings.
1653 */
1654 scratch_ptr = udev->bus->scratch[0].data;
1655 scratch_size = sizeof(udev->bus->scratch[0].data);
1656
1657 if (udev->ddesc.iManufacturer ||
1658 udev->ddesc.iProduct ||
1659 udev->ddesc.iSerialNumber) {
1660 /* read out the language ID string */
1661 err = usb2_req_get_string_desc(udev, NULL,
1662 (char *)scratch_ptr, 4, scratch_size,
1663 USB_LANGUAGE_TABLE);
1664 } else {
1665 err = USB_ERR_INVAL;
1666 }
1667
1668 if (err || (scratch_ptr[0] < 4)) {
1669 udev->flags.no_strings = 1;
1670 } else {
1671 /* pick the first language as the default */
1672 udev->langid = UGETW(scratch_ptr + 2);
1673 }
1674
1675 /* assume 100mA bus powered for now. Changed when configured. */
1676 udev->power = USB_MIN_POWER;
1677 /* fetch the vendor and product strings from the device */
1678 usb2_set_device_strings(udev);
1679
1680 if (udev->flags.usb_mode == USB_MODE_HOST) {
1681 uint8_t config_index;
1682 uint8_t config_quirk;
1683 uint8_t set_config_failed = 0;
1684
1685 /*
1686 * Most USB devices should attach to config index 0 by
1687 * default
1688 */
1689 if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1690 config_index = 0;
1691 config_quirk = 1;
1692 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1693 config_index = 1;
1694 config_quirk = 1;
1695 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1696 config_index = 2;
1697 config_quirk = 1;
1698 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1699 config_index = 3;
1700 config_quirk = 1;
1701 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1702 config_index = 4;
1703 config_quirk = 1;
1704 } else {
1705 config_index = 0;
1706 config_quirk = 0;
1707 }
1708
1709repeat_set_config:
1710
1711 DPRINTF("setting config %u\n", config_index);
1712
1713 /* get the USB device configured */
1714 err = usb2_set_config_index(udev, config_index);
1715 if (err) {
1716 if (udev->ddesc.bNumConfigurations != 0) {
1717 if (!set_config_failed) {
1718 set_config_failed = 1;
1719 /* XXX try to re-enumerate the device */
1720 err = usb2_req_re_enumerate(
1721 udev, NULL);
1722 if (err == 0)
1723 goto repeat_set_config;
1724 }
1725 DPRINTFN(0, "Failure selecting "
1726 "configuration index %u: %s, port %u, "
1727 "addr %u (ignored)\n",
1728 config_index, usb2_errstr(err), udev->port_no,
1729 udev->address);
1730 }
1731 /*
1732 * Some USB devices do not have any
1733 * configurations. Ignore any set config
1734 * failures!
1735 */
1736 err = 0;
1737 } else if (config_quirk) {
1738 /* user quirk selects configuration index */
1739 } else if ((config_index + 1) < udev->ddesc.bNumConfigurations) {
1740
1741 if ((udev->cdesc->bNumInterface < 2) &&
1742 (usb2_get_no_descriptors(udev->cdesc,
1743 UDESC_ENDPOINT) == 0)) {
1744 DPRINTFN(0, "Found no endpoints "
1745 "(trying next config)!\n");
1746 config_index++;
1747 goto repeat_set_config;
1748 }
1749 if (config_index == 0) {
1750 /*
1751 * Try to figure out if we have an
1752 * auto-install disk there:
1753 */
1754 if (usb2_test_autoinstall(udev, 0, 0) == 0) {
1755 DPRINTFN(0, "Found possible auto-install "
1756 "disk (trying next config)\n");
1757 config_index++;
1758 goto repeat_set_config;
1759 }
1760 }
1761 } else if (usb2_test_huawei_autoinst_p(udev, &uaa) == 0) {
1762 DPRINTFN(0, "Found Huawei auto-install disk!\n");
1763 err = USB_ERR_STALLED; /* fake an error */
1764 }
1765 } else {
1766 err = 0; /* set success */
1767 }
1768
1769 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1770 udev->address, udev, udev->parent_hub);
1771
1772 /* register our device - we are ready */
1773 usb2_bus_port_set_device(bus, parent_hub ?
1774 parent_hub->hub->ports + port_index : NULL, udev, device_index);
1775
1776#if USB_HAVE_UGEN
1777 /* Symlink the ugen device name */
1778 udev->ugen_symlink = usb2_alloc_symlink(udev->ugen_name);
1779
1780 /* Announce device */
1781 printf("%s: <%s> at %s\n", udev->ugen_name, udev->manufacturer,
1782 device_get_nameunit(udev->bus->bdev));
1783
1784 usb2_notify_addq("+", udev);
1785#endif
1786done:
1787 if (err) {
1788 /* free device */
1789 usb2_free_device(udev,
1790 USB_UNCFG_FLAG_FREE_SUBDEV |
1791 USB_UNCFG_FLAG_FREE_EP0);
1792 udev = NULL;
1793 }
1794 return (udev);
1795}
1796
1797#if USB_HAVE_UGEN
1798static struct cdev *
1799usb2_make_dev(struct usb_device *udev, int ep, int mode)
1800{
1801 struct usb_fs_privdata* pd;
1802 char devname[20];
1803
1804 /* Store information to locate ourselves again later */
1805 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1806 M_WAITOK | M_ZERO);
1807 pd->bus_index = device_get_unit(udev->bus->bdev);
1808 pd->dev_index = udev->device_index;
1809 pd->ep_addr = ep;
1810 pd->mode = mode;
1811
1812 /* Now, create the device itself */
1813 snprintf(devname, sizeof(devname), "%u.%u.%u",
1814 pd->bus_index, pd->dev_index, pd->ep_addr);
1815 pd->cdev = make_dev(&usb2_devsw, 0, UID_ROOT,
1816 GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname);
1817 pd->cdev->si_drv1 = pd;
1818
1819 return (pd->cdev);
1820}
1821
1822static void
1823usb2_cdev_create(struct usb_device *udev)
1824{
1825 struct usb_config_descriptor *cd;
1826 struct usb_endpoint_descriptor *ed;
1827 struct usb_descriptor *desc;
1828 struct usb_fs_privdata* pd;
1829 struct cdev *dev;
1830 int inmode, outmode, inmask, outmask, mode;
1831 uint8_t ep;
1832
1833 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
1834
1835 DPRINTFN(2, "Creating device nodes\n");
1836
1837 if (usb2_get_mode(udev) == USB_MODE_DEVICE) {
1838 inmode = FWRITE;
1839 outmode = FREAD;
1840 } else { /* USB_MODE_HOST */
1841 inmode = FREAD;
1842 outmode = FWRITE;
1843 }
1844
1845 inmask = 0;
1846 outmask = 0;
1847 desc = NULL;
1848
1849 /*
1850 * Collect all used endpoint numbers instead of just
1851 * generating 16 static endpoints.
1852 */
1853 cd = usb2_get_config_descriptor(udev);
1854 while ((desc = usb2_desc_foreach(cd, desc))) {
1855 /* filter out all endpoint descriptors */
1856 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
1857 (desc->bLength >= sizeof(*ed))) {
1858 ed = (struct usb_endpoint_descriptor *)desc;
1859
1860 /* update masks */
1861 ep = ed->bEndpointAddress;
1862 if (UE_GET_DIR(ep) == UE_DIR_OUT)
1863 outmask |= 1 << UE_GET_ADDR(ep);
1864 else
1865 inmask |= 1 << UE_GET_ADDR(ep);
1866 }
1867 }
1868
1869 /* Create all available endpoints except EP0 */
1870 for (ep = 1; ep < 16; ep++) {
1871 mode = inmask & (1 << ep) ? inmode : 0;
1872 mode |= outmask & (1 << ep) ? outmode : 0;
1873 if (mode == 0)
1874 continue; /* no IN or OUT endpoint */
1875
1876 dev = usb2_make_dev(udev, ep, mode);
1877 pd = dev->si_drv1;
1878 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
1879 }
1880}
1881
1882static void
1883usb2_cdev_free(struct usb_device *udev)
1884{
1885 struct usb_fs_privdata* pd;
1886
1887 DPRINTFN(2, "Freeing device nodes\n");
1888
1889 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
1890 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
1891
1892 destroy_dev_sched_cb(pd->cdev, usb2_cdev_cleanup, pd);
1893 pd->cdev = NULL;
1894 LIST_REMOVE(pd, pd_next);
1895 }
1896}
1897
1898static void
1899usb2_cdev_cleanup(void* arg)
1900{
1901 free(arg, M_USBDEV);
1902}
1903#endif
1904
1905/*------------------------------------------------------------------------*
1906 * usb2_free_device
1907 *
1908 * This function is NULL safe and will free an USB device.
1909 *
1910 * Flag values, see "USB_UNCFG_FLAG_XXX".
1911 *------------------------------------------------------------------------*/
1912void
1913usb2_free_device(struct usb_device *udev, uint8_t flag)
1914{
1915 struct usb_bus *bus;
1916
1917 if (udev == NULL)
1918 return; /* already freed */
1919
1920 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
1921
1922 bus = udev->bus;
1923 usb2_set_device_state(udev, USB_STATE_DETACHED);
1924
1925#if USB_HAVE_UGEN
1926 usb2_notify_addq("-", udev);
1927
1928 printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
1929 udev->manufacturer, device_get_nameunit(bus->bdev));
1930
1931 /* Destroy UGEN symlink, if any */
1932 if (udev->ugen_symlink) {
1933 usb2_free_symlink(udev->ugen_symlink);
1934 udev->ugen_symlink = NULL;
1935 }
1936#endif
1937 /*
1938 * Unregister our device first which will prevent any further
1939 * references:
1940 */
1941 usb2_bus_port_set_device(bus, udev->parent_hub ?
1942 udev->parent_hub->hub->ports + udev->port_index : NULL,
1943 NULL, USB_ROOT_HUB_ADDR);
1944
1945#if USB_HAVE_UGEN
1946 /* wait for all pending references to go away: */
1947 mtx_lock(&usb2_ref_lock);
1948 udev->refcount--;
1949 while (udev->refcount != 0) {
1950 usb2_cv_wait(udev->default_cv + 1, &usb2_ref_lock);
1950 cv_wait(udev->default_cv + 1, &usb2_ref_lock);
1951 }
1952 mtx_unlock(&usb2_ref_lock);
1953
1954 destroy_dev_sched_cb(udev->default_dev, usb2_cdev_cleanup,
1955 udev->default_dev->si_drv1);
1956#endif
1957
1958 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1959 /* stop receiving any control transfers (Device Side Mode) */
1960 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
1961 }
1962
1963 /* the following will get the device unconfigured in software */
1964 usb2_unconfigure(udev, flag);
1965
1966 /* unsetup any leftover default USB transfers */
1967 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
1968
1969 /* template unsetup, if any */
1970 (usb2_temp_unsetup_p) (udev);
1971
1972 /*
1973 * Make sure that our clear-stall messages are not queued
1974 * anywhere:
1975 */
1976 USB_BUS_LOCK(udev->bus);
1977 usb2_proc_mwait(&udev->bus->non_giant_callback_proc,
1978 &udev->cs_msg[0], &udev->cs_msg[1]);
1979 USB_BUS_UNLOCK(udev->bus);
1980
1981 sx_destroy(udev->default_sx);
1982 sx_destroy(udev->default_sx + 1);
1983
1984 usb2_cv_destroy(udev->default_cv);
1985 usb2_cv_destroy(udev->default_cv + 1);
1984 cv_destroy(udev->default_cv);
1985 cv_destroy(udev->default_cv + 1);
1986
1987 mtx_destroy(udev->default_mtx);
1988#if USB_HAVE_UGEN
1989 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
1990#endif
1991
1992 /* free device */
1993 free(udev->serial, M_USB);
1994 free(udev->manufacturer, M_USB);
1995 free(udev->product, M_USB);
1996 free(udev, M_USB);
1997}
1998
1999/*------------------------------------------------------------------------*
2000 * usb2_get_iface
2001 *
2002 * This function is the safe way to get the USB interface structure
2003 * pointer by interface index.
2004 *
2005 * Return values:
2006 * NULL: Interface not present.
2007 * Else: Pointer to USB interface structure.
2008 *------------------------------------------------------------------------*/
2009struct usb_interface *
2010usb2_get_iface(struct usb_device *udev, uint8_t iface_index)
2011{
2012 struct usb_interface *iface = udev->ifaces + iface_index;
2013
2014 if (iface_index >= udev->ifaces_max)
2015 return (NULL);
2016 return (iface);
2017}
2018
2019/*------------------------------------------------------------------------*
2020 * usb2_find_descriptor
2021 *
2022 * This function will lookup the first descriptor that matches the
2023 * criteria given by the arguments "type" and "subtype". Descriptors
2024 * will only be searched within the interface having the index
2025 * "iface_index". If the "id" argument points to an USB descriptor,
2026 * it will be skipped before the search is started. This allows
2027 * searching for multiple descriptors using the same criteria. Else
2028 * the search is started after the interface descriptor.
2029 *
2030 * Return values:
2031 * NULL: End of descriptors
2032 * Else: A descriptor matching the criteria
2033 *------------------------------------------------------------------------*/
2034void *
2035usb2_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2036 uint8_t type, uint8_t type_mask,
2037 uint8_t subtype, uint8_t subtype_mask)
2038{
2039 struct usb_descriptor *desc;
2040 struct usb_config_descriptor *cd;
2041 struct usb_interface *iface;
2042
2043 cd = usb2_get_config_descriptor(udev);
2044 if (cd == NULL) {
2045 return (NULL);
2046 }
2047 if (id == NULL) {
2048 iface = usb2_get_iface(udev, iface_index);
2049 if (iface == NULL) {
2050 return (NULL);
2051 }
2052 id = usb2_get_interface_descriptor(iface);
2053 if (id == NULL) {
2054 return (NULL);
2055 }
2056 }
2057 desc = (void *)id;
2058
2059 while ((desc = usb2_desc_foreach(cd, desc))) {
2060
2061 if (desc->bDescriptorType == UDESC_INTERFACE) {
2062 break;
2063 }
2064 if (((desc->bDescriptorType & type_mask) == type) &&
2065 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2066 return (desc);
2067 }
2068 }
2069 return (NULL);
2070}
2071
2072/*------------------------------------------------------------------------*
2073 * usb2_devinfo
2074 *
2075 * This function will dump information from the device descriptor
2076 * belonging to the USB device pointed to by "udev", to the string
2077 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2078 * including the terminating zero.
2079 *------------------------------------------------------------------------*/
2080void
2081usb2_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2082{
2083 struct usb_device_descriptor *udd = &udev->ddesc;
2084 uint16_t bcdDevice;
2085 uint16_t bcdUSB;
2086
2087 bcdUSB = UGETW(udd->bcdUSB);
2088 bcdDevice = UGETW(udd->bcdDevice);
2089
2090 if (udd->bDeviceClass != 0xFF) {
2091 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2092 "%x.%02x, addr %d",
2093 udev->manufacturer, udev->product,
2094 udd->bDeviceClass, udd->bDeviceSubClass,
2095 (bcdUSB >> 8), bcdUSB & 0xFF,
2096 (bcdDevice >> 8), bcdDevice & 0xFF,
2097 udev->address);
2098 } else {
2099 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2100 "%x.%02x, addr %d",
2101 udev->manufacturer, udev->product,
2102 (bcdUSB >> 8), bcdUSB & 0xFF,
2103 (bcdDevice >> 8), bcdDevice & 0xFF,
2104 udev->address);
2105 }
2106}
2107
2108#if USB_VERBOSE
2109/*
2110 * Descriptions of of known vendors and devices ("products").
2111 */
2112struct usb_knowndev {
2113 uint16_t vendor;
2114 uint16_t product;
2115 uint32_t flags;
2116 const char *vendorname;
2117 const char *productname;
2118};
2119
2120#define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
2121
2122#include "usbdevs.h"
2123#include "usbdevs_data.h"
2124#endif /* USB_VERBOSE */
2125
2126static void
2127usb2_set_device_strings(struct usb_device *udev)
2128{
2129 struct usb_device_descriptor *udd = &udev->ddesc;
2130#if USB_VERBOSE
2131 const struct usb_knowndev *kdp;
2132#endif
2133 char temp[64];
2134 uint16_t vendor_id;
2135 uint16_t product_id;
2136
2137 vendor_id = UGETW(udd->idVendor);
2138 product_id = UGETW(udd->idProduct);
2139
2140 /* get serial number string */
2141 bzero(temp, sizeof(temp));
2142 usb2_req_get_string_any(udev, NULL, temp, sizeof(temp),
2143 udev->ddesc.iSerialNumber);
2144 udev->serial = strdup(temp, M_USB);
2145
2146 /* get manufacturer string */
2147 bzero(temp, sizeof(temp));
2148 usb2_req_get_string_any(udev, NULL, temp, sizeof(temp),
2149 udev->ddesc.iManufacturer);
2150 usb2_trim_spaces(temp);
2151 if (temp[0] != '\0')
2152 udev->manufacturer = strdup(temp, M_USB);
2153
2154 /* get product string */
2155 bzero(temp, sizeof(temp));
2156 usb2_req_get_string_any(udev, NULL, temp, sizeof(temp),
2157 udev->ddesc.iProduct);
2158 usb2_trim_spaces(temp);
2159 if (temp[0] != '\0')
2160 udev->product = strdup(temp, M_USB);
2161
2162#if USB_VERBOSE
2163 if (udev->manufacturer == NULL || udev->product == NULL) {
2164 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2165 if (kdp->vendor == vendor_id &&
2166 (kdp->product == product_id ||
2167 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2168 break;
2169 }
2170 if (kdp->vendorname != NULL) {
2171 /* XXX should use pointer to knowndevs string */
2172 if (udev->manufacturer == NULL) {
2173 udev->manufacturer = strdup(kdp->vendorname,
2174 M_USB);
2175 }
2176 if (udev->product == NULL &&
2177 (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2178 udev->product = strdup(kdp->productname,
2179 M_USB);
2180 }
2181 }
2182 }
2183#endif
2184 /* Provide default strings if none were found */
2185 if (udev->manufacturer == NULL) {
2186 snprintf(temp, sizeof(temp), "vendor 0x%04x", vendor_id);
2187 udev->manufacturer = strdup(temp, M_USB);
2188 }
2189 if (udev->product == NULL) {
2190 snprintf(temp, sizeof(temp), "product 0x%04x", product_id);
2191 udev->product = strdup(temp, M_USB);
2192 }
2193}
2194
2195/*
2196 * Returns:
2197 * See: USB_MODE_XXX
2198 */
2199enum usb_hc_mode
2200usb2_get_mode(struct usb_device *udev)
2201{
2202 return (udev->flags.usb_mode);
2203}
2204
2205/*
2206 * Returns:
2207 * See: USB_SPEED_XXX
2208 */
2209enum usb_dev_speed
2210usb2_get_speed(struct usb_device *udev)
2211{
2212 return (udev->speed);
2213}
2214
2215uint32_t
2216usb2_get_isoc_fps(struct usb_device *udev)
2217{
2218 ; /* indent fix */
2219 switch (udev->speed) {
2220 case USB_SPEED_LOW:
2221 case USB_SPEED_FULL:
2222 return (1000);
2223 default:
2224 return (8000);
2225 }
2226}
2227
2228struct usb_device_descriptor *
2229usb2_get_device_descriptor(struct usb_device *udev)
2230{
2231 if (udev == NULL)
2232 return (NULL); /* be NULL safe */
2233 return (&udev->ddesc);
2234}
2235
2236struct usb_config_descriptor *
2237usb2_get_config_descriptor(struct usb_device *udev)
2238{
2239 if (udev == NULL)
2240 return (NULL); /* be NULL safe */
2241 return (udev->cdesc);
2242}
2243
2244/*------------------------------------------------------------------------*
2245 * usb2_test_quirk - test a device for a given quirk
2246 *
2247 * Return values:
2248 * 0: The USB device does not have the given quirk.
2249 * Else: The USB device has the given quirk.
2250 *------------------------------------------------------------------------*/
2251uint8_t
2252usb2_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2253{
2254 uint8_t found;
2255
2256 found = (usb2_test_quirk_p) (&uaa->info, quirk);
2257 return (found);
2258}
2259
2260struct usb_interface_descriptor *
2261usb2_get_interface_descriptor(struct usb_interface *iface)
2262{
2263 if (iface == NULL)
2264 return (NULL); /* be NULL safe */
2265 return (iface->idesc);
2266}
2267
2268uint8_t
2269usb2_get_interface_altindex(struct usb_interface *iface)
2270{
2271 return (iface->alt_index);
2272}
2273
2274uint8_t
2275usb2_get_bus_index(struct usb_device *udev)
2276{
2277 return ((uint8_t)device_get_unit(udev->bus->bdev));
2278}
2279
2280uint8_t
2281usb2_get_device_index(struct usb_device *udev)
2282{
2283 return (udev->device_index);
2284}
2285
2286#if USB_HAVE_UGEN
2287/*------------------------------------------------------------------------*
2288 * usb2_notify_addq
2289 *
2290 * This function will generate events for dev.
2291 *------------------------------------------------------------------------*/
2292static void
2293usb2_notify_addq(const char *type, struct usb_device *udev)
2294{
2295 char *data = NULL;
2296 struct malloc_type *mt;
2297
2298 mtx_lock(&malloc_mtx);
2299 mt = malloc_desc2type("bus"); /* XXX M_BUS */
2300 mtx_unlock(&malloc_mtx);
2301 if (mt == NULL)
2302 return;
2303
2304 data = malloc(512, mt, M_NOWAIT);
2305 if (data == NULL)
2306 return;
2307
2308 /* String it all together. */
2309 snprintf(data, 1024,
2310 "%s"
2311 "%s "
2312 "vendor=0x%04x "
2313 "product=0x%04x "
2314 "devclass=0x%02x "
2315 "devsubclass=0x%02x "
2316 "sernum=\"%s\" "
2317 "at "
2318 "port=%u "
2319 "on "
2320 "%s\n",
2321 type,
2322 udev->ugen_name,
2323 UGETW(udev->ddesc.idVendor),
2324 UGETW(udev->ddesc.idProduct),
2325 udev->ddesc.bDeviceClass,
2326 udev->ddesc.bDeviceSubClass,
2327 udev->serial,
2328 udev->port_no,
2329 udev->parent_hub != NULL ?
2330 udev->parent_hub->ugen_name :
2331 device_get_nameunit(device_get_parent(udev->bus->bdev)));
2332
2333 devctl_queue_data(data);
2334}
2335
2336/*------------------------------------------------------------------------*
2337 * usb2_fifo_free_wrap
2338 *
2339 * This function will free the FIFOs.
2340 *
2341 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2342 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2343 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2344 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2345 * control endpoint FIFOs. If "iface_index" is not set to
2346 * "USB_IFACE_INDEX_ANY" the flag has no effect.
2347 *------------------------------------------------------------------------*/
2348static void
2349usb2_fifo_free_wrap(struct usb_device *udev,
2350 uint8_t iface_index, uint8_t flag)
2351{
2352 struct usb_fifo *f;
2353 uint16_t i;
2354
2355 /*
2356 * Free any USB FIFOs on the given interface:
2357 */
2358 for (i = 0; i != USB_FIFO_MAX; i++) {
2359 f = udev->fifo[i];
2360 if (f == NULL) {
2361 continue;
2362 }
2363 /* Check if the interface index matches */
2364 if (iface_index == f->iface_index) {
2365 if (f->methods != &usb2_ugen_methods) {
2366 /*
2367 * Don't free any non-generic FIFOs in
2368 * this case.
2369 */
2370 continue;
2371 }
2372 if ((f->dev_ep_index == 0) &&
2373 (f->fs_xfer == NULL)) {
2374 /* no need to free this FIFO */
2375 continue;
2376 }
2377 } else if (iface_index == USB_IFACE_INDEX_ANY) {
2378 if ((f->methods == &usb2_ugen_methods) &&
2379 (f->dev_ep_index == 0) &&
2380 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2381 (f->fs_xfer == NULL)) {
2382 /* no need to free this FIFO */
2383 continue;
2384 }
2385 } else {
2386 /* no need to free this FIFO */
2387 continue;
2388 }
2389 /* free this FIFO */
2390 usb2_fifo_free(f);
2391 }
2392}
2393#endif
2394
2395/*------------------------------------------------------------------------*
2396 * usb2_peer_can_wakeup
2397 *
2398 * Return values:
2399 * 0: Peer cannot do resume signalling.
2400 * Else: Peer can do resume signalling.
2401 *------------------------------------------------------------------------*/
2402uint8_t
2403usb2_peer_can_wakeup(struct usb_device *udev)
2404{
2405 const struct usb_config_descriptor *cdp;
2406
2407 cdp = udev->cdesc;
2408 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2409 return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2410 }
2411 return (0); /* not supported */
2412}
2413
2414void
2415usb2_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2416{
2417
2418 KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2419
2420 DPRINTF("udev %p state %s -> %s\n", udev,
2421 usb2_statestr(udev->state), usb2_statestr(state));
2422 udev->state = state;
2423}
2424
2425uint8_t
2426usb2_device_attached(struct usb_device *udev)
2427{
2428 return (udev->state > USB_STATE_DETACHED);
2429}