1 /* $FreeBSD: releng/12.2/sys/dev/usb/usb_device.c 363664 2020-07-29 14:30:42Z markj $ */
2 /*-
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 *
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "implementation/global_implementation.h"
30 #include "fs/driver.h"
31 #ifdef LOSCFG_DRIVERS_HDF_USB_PNP_NOTIFY
32 #include "usb_pnp_notify.h"
33 #endif
34
35 #undef USB_DEBUG_VAR
36 #define USB_DEBUG_VAR usb_debug
37
38 /* function prototypes */
39 static void usb_init_endpoint(struct usb_device *, uint8_t,
40 struct usb_endpoint_descriptor *,
41 struct usb_endpoint_ss_comp_descriptor *,
42 struct usb_endpoint *);
43 static void usb_unconfigure(struct usb_device *, uint8_t);
44 static void usb_detach_device_sub(struct usb_device *, device_t *,
45 char **, uint8_t);
46 static uint8_t usb_probe_and_attach_sub(struct usb_device *,
47 struct usb_attach_arg *);
48 static void usb_init_attach_arg(struct usb_device *,
49 struct usb_attach_arg *);
50 static void usb_suspend_resume_sub(struct usb_device *, device_t,
51 uint8_t);
52 static usb_proc_callback_t usbd_clear_stall_proc;
53 static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
54 static void usbd_set_device_strings(struct usb_device *);
55 #if USB_HAVE_DEVCTL
56 static void usb_notify_addq(const char *type, struct usb_device *);
57 #endif
58 #if USB_HAVE_UGEN
59 static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
60 static void usb_cdev_create(struct usb_device *);
61 static void usb_cdev_free(struct usb_device *);
62 #endif
63
64 /* This variable is global to allow easy access to it: */
65 #ifdef USB_TEMPLATE
66 int usb_template = USB_TEMPLATE;
67 #else
68 int usb_template;
69 #endif
70
71 static int usb_lang_id = 0x0009;
72 static int usb_lang_mask = 0x00FF;
73
74 static const char* statestr[USB_STATE_MAX] = {
75 [USB_STATE_DETACHED] = "DETACHED",
76 [USB_STATE_ATTACHED] = "ATTACHED",
77 [USB_STATE_POWERED] = "POWERED",
78 [USB_STATE_ADDRESSED] = "ADDRESSED",
79 [USB_STATE_CONFIGURED] = "CONFIGURED",
80 };
81
82 const char *
usb_statestr(enum usb_dev_state state)83 usb_statestr(enum usb_dev_state state)
84 {
85 return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
86 }
87
88 const char *
usb_get_manufacturer(struct usb_device * udev)89 usb_get_manufacturer(struct usb_device *udev)
90 {
91 return (udev->manufacturer ? udev->manufacturer : "Unknown");
92 }
93
94 const char *
usb_get_product(struct usb_device * udev)95 usb_get_product(struct usb_device *udev)
96 {
97 return (udev->product ? udev->product : "");
98 }
99
100 const char *
usb_get_serial(struct usb_device * udev)101 usb_get_serial(struct usb_device *udev)
102 {
103 return (udev->serial ? udev->serial : "");
104 }
105
106 /*------------------------------------------------------------------------*
107 * usbd_get_ep_by_addr
108 *
109 * This function searches for an USB ep by endpoint address and
110 * direction.
111 *
112 * Returns:
113 * NULL: Failure
114 * Else: Success
115 *------------------------------------------------------------------------*/
116 struct usb_endpoint *
usbd_get_ep_by_addr(struct usb_device * udev,uint8_t ea_val)117 usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
118 {
119 struct usb_endpoint *ep = udev->endpoints;
120 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
121 enum {
122 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
123 };
124
125 /*
126 * According to the USB specification not all bits are used
127 * for the endpoint address. Keep defined bits only:
128 */
129 ea_val &= EA_MASK;
130
131 /*
132 * Iterate across all the USB endpoints searching for a match
133 * based on the endpoint address:
134 */
135 for (; ep != ep_end; ep++) {
136
137 if (ep->edesc == NULL) {
138 continue;
139 }
140 /* do the mask and check the value */
141 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
142 goto found;
143 }
144 }
145
146 /*
147 * The default endpoint is always present and is checked separately:
148 */
149 if ((udev->ctrl_ep.edesc != NULL) &&
150 ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
151 ep = &udev->ctrl_ep;
152 goto found;
153 }
154 return (NULL);
155
156 found:
157 return (ep);
158 }
159
160 /*------------------------------------------------------------------------*
161 * usbd_get_endpoint
162 *
163 * This function searches for an USB endpoint based on the information
164 * given by the passed "struct usb_config" pointer.
165 *
166 * Return values:
167 * NULL: No match.
168 * Else: Pointer to "struct usb_endpoint".
169 *------------------------------------------------------------------------*/
170 struct usb_endpoint *
usbd_get_endpoint(struct usb_device * udev,uint8_t iface_index,const struct usb_config * setup)171 usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
172 const struct usb_config *setup)
173 {
174 struct usb_endpoint *ep = udev->endpoints;
175 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
176 uint8_t index = setup->ep_index;
177 uint8_t ea_mask;
178 uint8_t ea_val;
179 uint8_t type_mask;
180 uint8_t type_val;
181
182 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
183 "type=0x%x dir=0x%x index=%d\n",
184 udev, iface_index, setup->endpoint,
185 setup->type, setup->direction, setup->ep_index);
186
187 /* check USB mode */
188
189 if ((setup->usb_mode != USB_MODE_DUAL) &&
190 (udev->flags.usb_mode != setup->usb_mode)) {
191 /* wrong mode - no endpoint */
192 return (NULL);
193 }
194
195 /* setup expected endpoint direction mask and value */
196
197 if (setup->direction == UE_DIR_RX) {
198 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
199 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
200 UE_DIR_OUT : UE_DIR_IN;
201 } else if (setup->direction == UE_DIR_TX) {
202 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
203 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
204 UE_DIR_IN : UE_DIR_OUT;
205 } else if (setup->direction == UE_DIR_ANY) {
206 /* match any endpoint direction */
207 ea_mask = 0;
208 ea_val = 0;
209 } else {
210 /* match the given endpoint direction */
211 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
212 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
213 }
214
215 /* setup expected endpoint address */
216
217 if (setup->endpoint == UE_ADDR_ANY) {
218 /* match any endpoint address */
219 } else {
220 /* match the given endpoint address */
221 ea_mask |= UE_ADDR;
222 ea_val |= (setup->endpoint & UE_ADDR);
223 }
224
225 /* setup expected endpoint type */
226
227 if (setup->type == UE_BULK_INTR) {
228 /* this will match BULK and INTERRUPT endpoints */
229 type_mask = 2;
230 type_val = 2;
231 } else if (setup->type == UE_TYPE_ANY) {
232 /* match any endpoint type */
233 type_mask = 0;
234 type_val = 0;
235 } else {
236 /* match the given endpoint type */
237 type_mask = UE_XFERTYPE;
238 type_val = (setup->type & UE_XFERTYPE);
239 }
240
241 /*
242 * Iterate across all the USB endpoints searching for a match
243 * based on the endpoint address. Note that we are searching
244 * the endpoints from the beginning of the "udev->endpoints" array.
245 */
246 for (; ep != ep_end; ep++) {
247
248 if ((ep->edesc == NULL) ||
249 (ep->iface_index != iface_index)) {
250 continue;
251 }
252 /* do the masks and check the values */
253
254 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
255 ((ep->edesc->bmAttributes & type_mask) == type_val)) {
256 if (!index--) {
257 goto found;
258 }
259 }
260 }
261
262 /*
263 * Match against default endpoint last, so that "any endpoint", "any
264 * address" and "any direction" returns the first endpoint of the
265 * interface. "iface_index" and "direction" is ignored:
266 */
267 if ((udev->ctrl_ep.edesc != NULL) &&
268 ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
269 ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
270 (!index)) {
271 ep = &udev->ctrl_ep;
272 goto found;
273 }
274 return (NULL);
275
276 found:
277 return (ep);
278 }
279
280 /*------------------------------------------------------------------------*
281 * usbd_interface_count
282 *
283 * This function stores the number of USB interfaces excluding
284 * alternate settings, which the USB config descriptor reports into
285 * the unsigned 8-bit integer pointed to by "count".
286 *
287 * Returns:
288 * 0: Success
289 * Else: Failure
290 *------------------------------------------------------------------------*/
291 usb_error_t
usbd_interface_count(struct usb_device * udev,uint8_t * count)292 usbd_interface_count(struct usb_device *udev, uint8_t *count)
293 {
294 if (udev->cdesc == NULL) {
295 *count = 0;
296 return (USB_ERR_NOT_CONFIGURED);
297 }
298 *count = udev->ifaces_max;
299 return (USB_ERR_NORMAL_COMPLETION);
300 }
301
302 /*------------------------------------------------------------------------*
303 * usb_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 *------------------------------------------------------------------------*/
309 static void
usb_init_endpoint(struct usb_device * udev,uint8_t iface_index,struct usb_endpoint_descriptor * edesc,struct usb_endpoint_ss_comp_descriptor * ecomp,struct usb_endpoint * ep)310 usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
311 struct usb_endpoint_descriptor *edesc,
312 struct usb_endpoint_ss_comp_descriptor *ecomp,
313 struct usb_endpoint *ep)
314 {
315 const struct usb_bus_methods *methods;
316 usb_stream_t x;
317
318 methods = udev->bus->methods;
319
320 (methods->endpoint_init) (udev, edesc, ep);
321
322 /* initialise USB endpoint structure */
323 ep->edesc = edesc;
324 ep->ecomp = ecomp;
325 ep->iface_index = iface_index;
326
327 /* setup USB stream queues */
328 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
329 TAILQ_INIT(&ep->endpoint_q[x].head);
330 ep->endpoint_q[x].command = &usbd_pipe_start;
331 }
332
333 /* the pipe is not supported by the hardware */
334 if (ep->methods == NULL)
335 return;
336
337 /* check for SUPER-speed streams mode endpoint */
338 if ((udev->speed == USB_SPEED_SUPER) && (ecomp != NULL) &&
339 ((edesc->bmAttributes & UE_XFERTYPE) == UE_BULK) &&
340 ((UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0))) {
341 (void)usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS);
342 } else {
343 (void)usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT);
344 }
345
346 /* clear stall, if any */
347 if (methods->clear_stall != NULL) {
348 USB_BUS_LOCK(udev->bus);
349 (methods->clear_stall) (udev, ep);
350 USB_BUS_UNLOCK(udev->bus);
351 }
352 }
353
354 /*-----------------------------------------------------------------------*
355 * usb_endpoint_foreach
356 *
357 * This function will iterate all the USB endpoints except the control
358 * endpoint. This function is NULL safe.
359 *
360 * Return values:
361 * NULL: End of USB endpoints
362 * Else: Pointer to next USB endpoint
363 *------------------------------------------------------------------------*/
364 struct usb_endpoint *
usb_endpoint_foreach(struct usb_device * udev,struct usb_endpoint * ep)365 usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
366 {
367 struct usb_endpoint *ep_end;
368
369 /* be NULL safe */
370 if (udev == NULL)
371 return (NULL);
372
373 ep_end = udev->endpoints + udev->endpoints_max;
374
375 /* get next endpoint */
376 if (ep == NULL)
377 ep = udev->endpoints;
378 else
379 ep++;
380
381 /* find next allocated ep */
382 while (ep != ep_end) {
383 if (ep->edesc != NULL)
384 return (ep);
385 ep++;
386 }
387 return (NULL);
388 }
389
390 /*------------------------------------------------------------------------*
391 * usb_wait_pending_refs
392 *
393 * This function will wait for any USB references to go away before
394 * returning. This function is used before freeing a USB device.
395 *------------------------------------------------------------------------*/
396 static void
usb_wait_pending_refs(struct usb_device * udev)397 usb_wait_pending_refs(struct usb_device *udev)
398 {
399 #if USB_HAVE_UGEN
400 DPRINTF("Refcount = %d\n", (int)udev->refcount);
401
402 mtx_lock(&usb_ref_lock);
403 udev->refcount--;
404 while (1) {
405 /* wait for any pending references to go away */
406 if (udev->refcount == 0) {
407 /* prevent further refs being taken, if any */
408 udev->refcount = USB_DEV_REF_MAX;
409 break;
410 }
411 cv_wait(&udev->ref_cv, &usb_ref_lock);
412 }
413 mtx_unlock(&usb_ref_lock);
414 #endif
415 }
416
417 /*------------------------------------------------------------------------*
418 * usb_unconfigure
419 *
420 * This function will free all USB interfaces and USB endpoints belonging
421 * to an USB device.
422 *
423 * Flag values, see "USB_UNCFG_FLAG_XXX".
424 *------------------------------------------------------------------------*/
425 static void
usb_unconfigure(struct usb_device * udev,uint8_t flag)426 usb_unconfigure(struct usb_device *udev, uint8_t flag)
427 {
428 uint8_t do_unlock;
429 usb_error_t err;
430
431 /* Prevent re-enumeration */
432 do_unlock = usbd_enum_lock(udev);
433
434 /* detach all interface drivers */
435 usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
436
437 #if USB_HAVE_UGEN
438 /* free all FIFOs except control endpoint FIFOs */
439 usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
440
441 /*
442 * Free all cdev's, if any.
443 */
444 usb_cdev_free(udev);
445 #endif
446
447 #ifdef LOSCFG_DRIVERS_USB_WIRELESS
448 /* free Linux compat device, if any */
449 if (udev->linux_endpoint_start) {
450 usb_linux_free_device(udev);
451 udev->linux_endpoint_start = NULL;
452 }
453 #endif
454
455 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
456 if (err != 0)
457 return ;
458
459 /* free "cdesc" after "ifaces" and "endpoints", if any */
460 if (udev->cdesc != NULL) {
461 if (udev->flags.usb_mode != USB_MODE_DEVICE)
462 usbd_free_config_desc(udev, udev->cdesc);
463 udev->cdesc = NULL;
464 }
465 /* set unconfigured state */
466 udev->curr_config_no = USB_UNCONFIG_NO;
467 udev->curr_config_index = USB_UNCONFIG_INDEX;
468
469 if (do_unlock)
470 usbd_enum_unlock(udev);
471 }
472
473 /*------------------------------------------------------------------------*
474 * usbd_set_config_index
475 *
476 * This function selects configuration by index, independent of the
477 * actual configuration number. This function should not be used by
478 * USB drivers.
479 *
480 * Returns:
481 * 0: Success
482 * Else: Failure
483 *------------------------------------------------------------------------*/
484 usb_error_t
usbd_set_config_index(struct usb_device * udev,uint8_t index)485 usbd_set_config_index(struct usb_device *udev, uint8_t index)
486 {
487 struct usb_status ds;
488 struct usb_config_descriptor *cdp;
489 uint16_t power;
490 uint16_t max_power;
491 uint8_t selfpowered;
492 uint8_t do_unlock;
493 usb_error_t err;
494
495 DPRINTFN(6, "udev=%p index=%d\n", udev, index);
496
497 /* Prevent re-enumeration */
498 do_unlock = usbd_enum_lock(udev);
499
500 usb_unconfigure(udev, 0);
501
502 if (index == USB_UNCONFIG_INDEX) {
503 /*
504 * Leave unallocated when unconfiguring the
505 * device. "usb_unconfigure()" will also reset
506 * the current config number and index.
507 */
508 err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
509 if (udev->state == USB_STATE_CONFIGURED)
510 usb_set_device_state(udev, USB_STATE_ADDRESSED);
511 goto done;
512 }
513 /* get the full config descriptor */
514 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
515 /* save some memory */
516 err = usbd_req_get_descriptor_ptr(udev, &cdp,
517 (UDESC_CONFIG << 8) | index);
518 } else {
519 /* normal request */
520 err = usbd_req_get_config_desc_full(udev,
521 NULL, &cdp, index);
522 }
523 if (err) {
524 goto done;
525 }
526 /* set the new config descriptor */
527
528 udev->cdesc = cdp;
529
530 /* Figure out if the device is self or bus powered. */
531 selfpowered = 0;
532 if ((!udev->flags.uq_bus_powered) &&
533 (cdp->bmAttributes & UC_SELF_POWERED) &&
534 (udev->flags.usb_mode == USB_MODE_HOST)) {
535 /* May be self powered. */
536 if (cdp->bmAttributes & UC_BUS_POWERED) {
537 /* Must ask device. */
538 err = usbd_req_get_device_status(udev, NULL, &ds);
539 if (err) {
540 DPRINTFN(0, "could not read "
541 "device status: %s\n",
542 usbd_errstr(err));
543 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
544 selfpowered = 1;
545 }
546 DPRINTF("status=0x%04x \n",
547 UGETW(ds.wStatus));
548 } else
549 selfpowered = 1;
550 }
551 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
552 "selfpowered=%d, power=%d\n",
553 udev, cdp,
554 udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
555 selfpowered, cdp->bMaxPower * 2);
556
557 /* Check if we have enough power. */
558 power = cdp->bMaxPower * 2;
559
560 if (udev->parent_hub) {
561 max_power = udev->parent_hub->hub->portpower;
562 } else {
563 max_power = USB_MAX_POWER;
564 }
565
566 if (power > max_power) {
567 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
568 err = USB_ERR_NO_POWER;
569 goto done;
570 }
571 /* Only update "self_powered" in USB Host Mode */
572 if (udev->flags.usb_mode == USB_MODE_HOST) {
573 udev->flags.self_powered = selfpowered;
574 }
575 udev->power = power;
576 udev->curr_config_no = cdp->bConfigurationValue;
577 udev->curr_config_index = index;
578 usb_set_device_state(udev, USB_STATE_CONFIGURED);
579
580 /* Set the actual configuration value. */
581 err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
582 if (err) {
583 goto done;
584 }
585
586 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
587 if (err) {
588 goto done;
589 }
590
591 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
592 if (err) {
593 goto done;
594 }
595
596 #if USB_HAVE_UGEN
597 /* create device nodes for each endpoint */
598 usb_cdev_create(udev);
599 #endif
600
601 done:
602 DPRINTF("error=%s\n", usbd_errstr(err));
603 if (err) {
604 usb_unconfigure(udev, 0);
605 }
606 if (do_unlock)
607 usbd_enum_unlock(udev);
608 return (err);
609 }
610
611 /*------------------------------------------------------------------------*
612 * usb_config_parse
613 *
614 * This function will allocate and free USB interfaces and USB endpoints,
615 * parse the USB configuration structure and initialise the USB endpoints
616 * and interfaces. If "iface_index" is not equal to
617 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
618 * alternate_setting to be selected for the given interface. Else the
619 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
620 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
621 * is typically called when setting the configuration or when setting
622 * an alternate interface.
623 *
624 * Returns:
625 * 0: Success
626 * Else: Failure
627 *------------------------------------------------------------------------*/
628 static usb_error_t
usb_config_parse(struct usb_device * udev,uint8_t iface_index,uint8_t cmd)629 usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
630 {
631 struct usb_idesc_parse_state ips;
632 struct usb_interface_descriptor *id;
633 struct usb_endpoint_descriptor *ed;
634 struct usb_interface *iface;
635 struct usb_endpoint *ep;
636 usb_error_t err;
637 uint8_t ep_curr;
638 uint8_t ep_max;
639 uint8_t temp;
640 uint8_t do_init;
641 uint8_t alt_index;
642
643 if (iface_index != USB_IFACE_INDEX_ANY) {
644 /* parameter overload */
645 alt_index = cmd;
646 cmd = USB_CFG_INIT;
647 } else {
648 /* not used */
649 alt_index = 0;
650 }
651
652 err = USB_ERR_NORMAL_COMPLETION;
653
654 DPRINTFN(5, "iface_index=%d cmd=%d\n",
655 iface_index, cmd);
656
657 if (cmd == USB_CFG_FREE)
658 goto cleanup;
659
660 if (cmd == USB_CFG_INIT) {
661 sx_assert(&udev->enum_sx, SA_LOCKED);
662
663 /* check for in-use endpoints */
664
665 ep = udev->endpoints;
666 ep_max = udev->endpoints_max;
667 while (ep_max--) {
668 /* look for matching endpoints */
669 if ((iface_index == USB_IFACE_INDEX_ANY) ||
670 (iface_index == ep->iface_index)) {
671 if (ep->refcount_alloc != 0) {
672 /*
673 * This typically indicates a
674 * more serious error.
675 */
676 err = USB_ERR_IN_USE;
677 } else {
678 /* reset endpoint */
679 (void)memset_s(ep, sizeof(*ep), 0, sizeof(*ep));
680 /* make sure we don't zero the endpoint again */
681 ep->iface_index = USB_IFACE_INDEX_ANY;
682 }
683 }
684 ep++;
685 }
686
687 if (err)
688 return (err);
689 }
690
691 (void)memset_s(&ips, sizeof(ips), 0, sizeof(ips));
692
693 ep_curr = 0;
694 ep_max = 0;
695
696 while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
697
698 iface = udev->ifaces + ips.iface_index;
699
700 /* check for specific interface match */
701
702 if (cmd == USB_CFG_INIT) {
703 if ((iface_index != USB_IFACE_INDEX_ANY) &&
704 (iface_index != ips.iface_index)) {
705 /* wrong interface */
706 do_init = 0;
707 } else if (alt_index != ips.iface_index_alt) {
708 /* wrong alternate setting */
709 do_init = 0;
710 } else {
711 /* initialise interface */
712 do_init = 1;
713 }
714 } else
715 do_init = 0;
716
717 /* check for new interface */
718 if (ips.iface_index_alt == 0) {
719 /* update current number of endpoints */
720 ep_curr = ep_max;
721 }
722 /* check for init */
723 if (do_init) {
724 /* setup the USB interface structure */
725 iface->idesc = id;
726 /* set alternate index */
727 iface->alt_index = alt_index;
728 /* set default interface parent */
729 if (iface_index == USB_IFACE_INDEX_ANY) {
730 iface->parent_iface_index =
731 USB_IFACE_INDEX_ANY;
732 }
733 }
734
735 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
736
737 ed = (struct usb_endpoint_descriptor *)id;
738
739 temp = ep_curr;
740
741 /* iterate all the endpoint descriptors */
742 while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
743
744 /* check if endpoint limit has been reached */
745 if (temp >= USB_MAX_EP_UNITS) {
746 DPRINTF("Endpoint limit reached\n");
747 break;
748 }
749
750 ep = udev->endpoints + temp;
751
752 if (do_init) {
753 void *ecomp;
754
755 ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
756 if (ecomp != NULL)
757 DPRINTFN(5, "Found endpoint companion descriptor\n");
758
759 usb_init_endpoint(udev,
760 ips.iface_index, ed, ecomp, ep);
761 }
762
763 temp ++;
764
765 /* find maximum number of endpoints */
766 if (ep_max < temp)
767 ep_max = temp;
768 }
769 }
770
771 /* NOTE: It is valid to have no interfaces and no endpoints! */
772
773 if (cmd == USB_CFG_ALLOC) {
774 udev->ifaces_max = ips.iface_index;
775 #if (USB_HAVE_FIXED_IFACE == 0)
776 udev->ifaces = NULL;
777 if (udev->ifaces_max != 0) {
778 udev->ifaces = bsd_malloc(sizeof(*iface) * udev->ifaces_max,
779 M_USB, M_WAITOK | M_ZERO);
780 if (udev->ifaces == NULL) {
781 err = USB_ERR_NOMEM;
782 goto done;
783 }
784 }
785 #endif
786 #if (USB_HAVE_FIXED_ENDPOINT == 0)
787 if (ep_max != 0) {
788 udev->endpoints = bsd_malloc(sizeof(*ep) * ep_max,
789 M_USB, M_WAITOK | M_ZERO);
790 if (udev->endpoints == NULL) {
791 err = USB_ERR_NOMEM;
792 goto done;
793 }
794 } else {
795 udev->endpoints = NULL;
796 }
797 #endif
798 USB_BUS_LOCK(udev->bus);
799 udev->endpoints_max = ep_max;
800 /* reset any ongoing clear-stall */
801 udev->ep_curr = NULL;
802 USB_BUS_UNLOCK(udev->bus);
803 }
804 #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0)
805 done:
806 #endif
807 if (err) {
808 if (cmd == USB_CFG_ALLOC) {
809 cleanup:
810 USB_BUS_LOCK(udev->bus);
811 udev->endpoints_max = 0;
812 /* reset any ongoing clear-stall */
813 udev->ep_curr = NULL;
814 USB_BUS_UNLOCK(udev->bus);
815
816 #if (USB_HAVE_FIXED_IFACE == 0)
817 bsd_free(udev->ifaces, M_USB);
818 udev->ifaces = NULL;
819 #endif
820 #if (USB_HAVE_FIXED_ENDPOINT == 0)
821 bsd_free(udev->endpoints, M_USB);
822 udev->endpoints = NULL;
823 #endif
824 udev->ifaces_max = 0;
825 }
826 }
827 return (err);
828 }
829
830 /*------------------------------------------------------------------------*
831 * usbd_set_alt_interface_index
832 *
833 * This function will select an alternate interface index for the
834 * given interface index. The interface should not be in use when this
835 * function is called. That means there should not be any open USB
836 * transfers. Else an error is returned. If the alternate setting is
837 * already set this function will simply return success. This function
838 * is called in Host mode and Device mode!
839 *
840 * Returns:
841 * 0: Success
842 * Else: Failure
843 *------------------------------------------------------------------------*/
844 usb_error_t
usbd_set_alt_interface_index(struct usb_device * udev,uint8_t iface_index,uint8_t alt_index)845 usbd_set_alt_interface_index(struct usb_device *udev,
846 uint8_t iface_index, uint8_t alt_index)
847 {
848 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
849 usb_error_t err;
850 uint8_t do_unlock;
851
852 /* Prevent re-enumeration */
853 do_unlock = usbd_enum_lock(udev);
854
855 if (iface == NULL) {
856 err = USB_ERR_INVAL;
857 goto done;
858 }
859 if (iface->alt_index == alt_index) {
860 /*
861 * Optimise away duplicate setting of
862 * alternate setting in USB Host Mode!
863 */
864 err = USB_ERR_NORMAL_COMPLETION;
865 goto done;
866 }
867 #if USB_HAVE_UGEN
868 /*
869 * Free all generic FIFOs for this interface, except control
870 * endpoint FIFOs:
871 */
872 usb_fifo_free_wrap(udev, iface_index, 0);
873 #endif
874
875 err = usb_config_parse(udev, iface_index, alt_index);
876 if (err) {
877 goto done;
878 }
879 if (iface->alt_index != alt_index) {
880 /* the alternate setting does not exist */
881 err = USB_ERR_INVAL;
882 goto done;
883 }
884
885 err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
886 iface->idesc->bAlternateSetting);
887
888 done:
889 if (do_unlock)
890 usbd_enum_unlock(udev);
891 return (err);
892 }
893
894 /*------------------------------------------------------------------------*
895 * usbd_set_endpoint_stall
896 *
897 * This function is used to make a BULK or INTERRUPT endpoint send
898 * STALL tokens in USB device mode.
899 *
900 * Returns:
901 * 0: Success
902 * Else: Failure
903 *------------------------------------------------------------------------*/
904 usb_error_t
usbd_set_endpoint_stall(struct usb_device * udev,struct usb_endpoint * ep,uint8_t do_stall)905 usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
906 uint8_t do_stall)
907 {
908 struct usb_xfer *xfer;
909 usb_stream_t x;
910 uint8_t et;
911 uint8_t was_stalled;
912
913 if (ep == NULL) {
914 /* nothing to do */
915 DPRINTF("Cannot find endpoint\n");
916 /*
917 * Pretend that the clear or set stall request is
918 * successful else some USB host stacks can do
919 * strange things, especially when a control endpoint
920 * stalls.
921 */
922 return (USB_ERR_NORMAL_COMPLETION);
923 }
924 et = (ep->edesc->bmAttributes & UE_XFERTYPE);
925
926 if ((et != UE_BULK) &&
927 (et != UE_INTERRUPT)) {
928 /*
929 * Should not stall control
930 * nor isochronous endpoints.
931 */
932 DPRINTF("Invalid endpoint\n");
933 return (USB_ERR_NORMAL_COMPLETION);
934 }
935 USB_BUS_LOCK(udev->bus);
936
937 /* store current stall state */
938 was_stalled = ep->is_stalled;
939
940 /* check for no change */
941 if (was_stalled && do_stall) {
942 /* if the endpoint is already stalled do nothing */
943 USB_BUS_UNLOCK(udev->bus);
944 DPRINTF("No change\n");
945 return (USB_ERR_NORMAL_COMPLETION);
946 }
947 /* set stalled state */
948 ep->is_stalled = 1;
949
950 if (do_stall || (!was_stalled)) {
951 if (!was_stalled) {
952 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
953 /* lookup the current USB transfer, if any */
954 xfer = ep->endpoint_q[x].curr;
955 if (xfer != NULL) {
956 /*
957 * The "xfer_stall" method
958 * will complete the USB
959 * transfer like in case of a
960 * timeout setting the error
961 * code "USB_ERR_STALLED".
962 */
963 (udev->bus->methods->xfer_stall) (xfer);
964 }
965 }
966 }
967 (udev->bus->methods->set_stall) (udev, ep, &do_stall);
968 }
969 if (!do_stall) {
970 ep->toggle_next = 0; /* reset data toggle */
971 ep->is_stalled = 0; /* clear stalled state */
972
973 (udev->bus->methods->clear_stall) (udev, ep);
974
975 /* start the current or next transfer, if any */
976 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
977 usb_command_wrapper(&ep->endpoint_q[x],
978 ep->endpoint_q[x].curr);
979 }
980 }
981 USB_BUS_UNLOCK(udev->bus);
982 return (USB_ERR_NORMAL_COMPLETION);
983 }
984
985 /*------------------------------------------------------------------------*
986 * usb_reset_iface_endpoints - used in USB device side mode
987 *------------------------------------------------------------------------*/
988 usb_error_t
usb_reset_iface_endpoints(struct usb_device * udev,uint8_t iface_index)989 usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
990 {
991 struct usb_endpoint *ep;
992 struct usb_endpoint *ep_end;
993
994 ep = udev->endpoints;
995 ep_end = udev->endpoints + udev->endpoints_max;
996
997 for (; ep != ep_end; ep++) {
998
999 if ((ep->edesc == NULL) ||
1000 (ep->iface_index != iface_index)) {
1001 continue;
1002 }
1003 /* simulate a clear stall from the peer */
1004 (void)usbd_set_endpoint_stall(udev, ep, 0);
1005 }
1006 return (USB_ERR_NORMAL_COMPLETION);
1007 }
1008
1009 /*------------------------------------------------------------------------*
1010 * usb_detach_device_sub
1011 *
1012 * This function will try to detach an USB device. If it fails a panic
1013 * will result.
1014 *
1015 * Flag values, see "USB_UNCFG_FLAG_XXX".
1016 *------------------------------------------------------------------------*/
1017 static void
usb_detach_device_sub(struct usb_device * udev,device_t * ppdev,char ** ppnpinfo,uint8_t flag)1018 usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1019 char **ppnpinfo, uint8_t flag)
1020 {
1021 device_t dev;
1022 char *pnpinfo;
1023 int err;
1024
1025 dev = *ppdev;
1026
1027 if (dev) {
1028 /*
1029 * NOTE: It is important to clear "*ppdev" before deleting
1030 * the child due to some device methods being called late
1031 * during the delete process !
1032 */
1033 *ppdev = NULL;
1034
1035 if (!rebooting) {
1036 device_printf(dev, "at %s, port %d, addr %d "
1037 "(disconnected)\n",
1038 device_get_nameunit(udev->parent_dev),
1039 udev->port_no, udev->address);
1040 }
1041
1042 if (device_is_attached(dev)) {
1043 if (udev->flags.peer_suspended) {
1044 err = DEVICE_RESUME(dev);
1045 if (err) {
1046 device_printf(dev, "Resume failed\n");
1047 }
1048 }
1049 }
1050 /* detach and delete child */
1051 if (device_delete_child(udev->parent_dev, dev)) {
1052 goto error;
1053 }
1054 }
1055
1056 pnpinfo = *ppnpinfo;
1057 if (pnpinfo != NULL) {
1058 *ppnpinfo = NULL;
1059 bsd_free(pnpinfo, M_USBDEV);
1060 }
1061 return;
1062
1063 error:
1064 /* Detach is not allowed to fail in the USB world */
1065 panic("usb_detach_device_sub: A USB driver would not detach\n");
1066 }
1067
1068 /*------------------------------------------------------------------------*
1069 * usb_detach_device
1070 *
1071 * The following function will detach the matching interfaces.
1072 * This function is NULL safe.
1073 *
1074 * Flag values, see "USB_UNCFG_FLAG_XXX".
1075 *------------------------------------------------------------------------*/
1076 void
usb_detach_device(struct usb_device * udev,uint8_t iface_index,uint8_t flag)1077 usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1078 uint8_t flag)
1079 {
1080 struct usb_interface *iface;
1081 uint8_t i;
1082
1083 if (udev == NULL) {
1084 /* nothing to do */
1085 return;
1086 }
1087 DPRINTFN(4, "udev=%p\n", udev);
1088
1089 sx_assert(&udev->enum_sx, SA_LOCKED);
1090
1091 /*
1092 * First detach the child to give the child's detach routine a
1093 * chance to detach the sub-devices in the correct order.
1094 * Then delete the child using "device_delete_child()" which
1095 * will detach all sub-devices from the bottom and upwards!
1096 */
1097 if (iface_index != USB_IFACE_INDEX_ANY) {
1098 i = iface_index;
1099 iface_index = i + 1;
1100 } else {
1101 i = 0;
1102 iface_index = USB_IFACE_MAX;
1103 }
1104
1105 /* do the detach */
1106
1107 for (; i != iface_index; i++) {
1108
1109 iface = usbd_get_iface(udev, i);
1110 if (iface == NULL) {
1111 /* looks like the end of the USB interfaces */
1112 break;
1113 }
1114 usb_detach_device_sub(udev, &iface->subdev,
1115 &iface->pnpinfo, flag);
1116 }
1117 }
1118
1119 /*------------------------------------------------------------------------*
1120 * usb_probe_and_attach_sub
1121 *
1122 * Returns:
1123 * 0: Success
1124 * Else: Failure
1125 *------------------------------------------------------------------------*/
1126 static uint8_t
usb_probe_and_attach_sub(struct usb_device * udev,struct usb_attach_arg * uaa)1127 usb_probe_and_attach_sub(struct usb_device *udev,
1128 struct usb_attach_arg *uaa)
1129 {
1130 struct usb_interface *iface;
1131 device_t dev;
1132 int err;
1133
1134 iface = uaa->iface;
1135 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1136 /* leave interface alone */
1137 return (0);
1138 }
1139 dev = iface->subdev;
1140 if (dev) {
1141
1142 /* clean up after module unload */
1143
1144 if (device_is_attached(dev)) {
1145 /* already a device there */
1146 return (0);
1147 }
1148 /* clear "iface->subdev" as early as possible */
1149
1150 iface->subdev = NULL;
1151
1152 if (device_delete_child(udev->parent_dev, dev)) {
1153
1154 /*
1155 * Panic here, else one can get a double call
1156 * to device_detach(). USB devices should
1157 * never fail on detach!
1158 */
1159 panic("device_delete_child() failed\n");
1160 }
1161 }
1162 if (uaa->temp_dev == NULL) {
1163
1164 /* create a new child */
1165 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1166 if (uaa->temp_dev == NULL) {
1167 device_printf(udev->parent_dev,
1168 "Device creation failed\n");
1169 return (1); /* failure */
1170 }
1171 device_set_ivars(uaa->temp_dev, uaa);
1172 device_quiet(uaa->temp_dev);
1173 }
1174 /*
1175 * Set "subdev" before probe and attach so that "devd" gets
1176 * the information it needs.
1177 */
1178 iface->subdev = uaa->temp_dev;
1179
1180 if (device_probe_and_attach(iface->subdev) == 0) {
1181 /*
1182 * The USB attach arguments are only available during probe
1183 * and attach !
1184 */
1185 uaa->temp_dev = NULL;
1186 device_set_ivars(iface->subdev, NULL);
1187
1188 if (udev->flags.peer_suspended) {
1189 err = DEVICE_SUSPEND(iface->subdev);
1190 if (err)
1191 device_printf(iface->subdev, "Suspend failed\n");
1192 }
1193 return (0); /* success */
1194 } else {
1195 /* No USB driver found */
1196 iface->subdev = NULL;
1197 }
1198 return (1); /* failure */
1199 }
1200
1201 /*------------------------------------------------------------------------*
1202 * usbd_set_parent_iface
1203 *
1204 * Using this function will lock the alternate interface setting on an
1205 * interface. It is typically used for multi interface drivers. In USB
1206 * device side mode it is assumed that the alternate interfaces all
1207 * have the same endpoint descriptors. The default parent index value
1208 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1209 * locked.
1210 *------------------------------------------------------------------------*/
1211 void
usbd_set_parent_iface(struct usb_device * udev,uint8_t iface_index,uint8_t parent_index)1212 usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1213 uint8_t parent_index)
1214 {
1215 struct usb_interface *iface;
1216
1217 if (udev == NULL) {
1218 /* nothing to do */
1219 return;
1220 }
1221 iface = usbd_get_iface(udev, iface_index);
1222 if (iface != NULL)
1223 iface->parent_iface_index = parent_index;
1224 }
1225
1226 static void
usb_init_attach_arg(struct usb_device * udev,struct usb_attach_arg * uaa)1227 usb_init_attach_arg(struct usb_device *udev,
1228 struct usb_attach_arg *uaa)
1229 {
1230 (void)memset_s(uaa, sizeof(*uaa), 0, sizeof(*uaa));
1231
1232 uaa->device = udev;
1233 uaa->usb_mode = udev->flags.usb_mode;
1234 uaa->port = udev->port_no;
1235 uaa->dev_state = UAA_DEV_READY;
1236
1237 uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1238 uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1239 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1240 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1241 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1242 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1243 uaa->info.bConfigIndex = udev->curr_config_index;
1244 uaa->info.bConfigNum = udev->curr_config_no;
1245 DPRINTFN(1, "################################\n");
1246 DPRINTFN(1, "idVendor %d; idProduct %d; bConfigNum %d\n", uaa->info.idVendor,
1247 uaa->info.idProduct, uaa->info.bConfigNum);
1248 DPRINTFN(1, "################################\n");
1249 }
1250
1251 /*------------------------------------------------------------------------*
1252 * usb_probe_and_attach
1253 *
1254 * This function is called from "uhub_explore_sub()",
1255 * "usb_handle_set_config()" and "usb_handle_request()".
1256 *
1257 * Returns:
1258 * 0: Success
1259 * Else: A control transfer failed
1260 *------------------------------------------------------------------------*/
1261 usb_error_t
usb_probe_and_attach(struct usb_device * udev,uint8_t iface_index)1262 usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1263 {
1264 struct usb_attach_arg uaa;
1265 struct usb_interface *iface;
1266 uint8_t i;
1267 uint8_t j;
1268 uint8_t do_unlock;
1269
1270 if (udev == NULL) {
1271 DPRINTF("udev == NULL\n");
1272 return (USB_ERR_INVAL);
1273 }
1274 /* Prevent re-enumeration */
1275 do_unlock = usbd_enum_lock(udev);
1276
1277 if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1278 /* do nothing - no configuration has been set */
1279 goto done;
1280 }
1281 /* setup USB attach arguments */
1282
1283 usb_init_attach_arg(udev, &uaa);
1284
1285 /*
1286 * If the whole USB device is targeted, invoke the USB event
1287 * handler(s):
1288 */
1289 if (iface_index == USB_IFACE_INDEX_ANY) {
1290
1291 EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1292
1293 if (uaa.dev_state != UAA_DEV_READY) {
1294 /* leave device unconfigured */
1295 usb_unconfigure(udev, 0);
1296 goto done;
1297 }
1298 }
1299
1300 /* Check if only one interface should be probed: */
1301 if (iface_index != USB_IFACE_INDEX_ANY) {
1302 i = iface_index;
1303 j = i + 1;
1304 } else {
1305 i = 0;
1306 j = USB_IFACE_MAX;
1307 }
1308
1309 /* Do the probe and attach */
1310 for (; i != j; i++) {
1311
1312 iface = usbd_get_iface(udev, i);
1313 if (iface == NULL) {
1314 /*
1315 * Looks like the end of the USB
1316 * interfaces !
1317 */
1318 DPRINTFN(2, "end of interfaces "
1319 "at %u\n", i);
1320 break;
1321 }
1322 if (iface->idesc == NULL) {
1323 /* no interface descriptor */
1324 continue;
1325 }
1326 uaa.iface = iface;
1327
1328 uaa.info.bInterfaceClass =
1329 iface->idesc->bInterfaceClass;
1330 uaa.info.bInterfaceSubClass =
1331 iface->idesc->bInterfaceSubClass;
1332 uaa.info.bInterfaceProtocol =
1333 iface->idesc->bInterfaceProtocol;
1334 uaa.info.bIfaceIndex = i;
1335 uaa.info.bIfaceNum =
1336 iface->idesc->bInterfaceNumber;
1337 uaa.driver_info = 0; /* reset driver_info */
1338
1339 DPRINTFN(10, "iclass=%u/%u/%u iindex=%u/%u\n",
1340 uaa.info.bInterfaceClass,
1341 uaa.info.bInterfaceSubClass,
1342 uaa.info.bInterfaceProtocol,
1343 uaa.info.bIfaceIndex,
1344 uaa.info.bIfaceNum);
1345
1346 (void)usb_probe_and_attach_sub(udev, &uaa);
1347
1348 /*
1349 * Remove the leftover child, if any, to enforce that
1350 * a new nomatch devd event is generated for the next
1351 * interface if no driver is found:
1352 */
1353 if (uaa.temp_dev == NULL)
1354 continue;
1355 if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1356 PRINTK("device delete child failed\n");
1357 uaa.temp_dev = NULL;
1358 }
1359 done:
1360 if (do_unlock)
1361 usbd_enum_unlock(udev);
1362 return (USB_ERR_NORMAL_COMPLETION);
1363 }
1364
1365 /*------------------------------------------------------------------------*
1366 * usb_suspend_resume_sub
1367 *
1368 * This function is called when the suspend or resume methods should
1369 * be executed on an USB device.
1370 *------------------------------------------------------------------------*/
1371 static void
usb_suspend_resume_sub(struct usb_device * udev,device_t dev,uint8_t do_suspend)1372 usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1373 {
1374 int err;
1375
1376 if (dev == NULL) {
1377 return;
1378 }
1379 if (!device_is_attached(dev)) {
1380 return;
1381 }
1382 if (do_suspend) {
1383 err = DEVICE_SUSPEND(dev);
1384 } else {
1385 err = DEVICE_RESUME(dev);
1386 }
1387 if (err) {
1388 device_printf(dev, "%s failed\n",
1389 do_suspend ? "Suspend" : "Resume");
1390 }
1391 }
1392
1393 /*------------------------------------------------------------------------*
1394 * usb_suspend_resume
1395 *
1396 * The following function will suspend or resume the USB device.
1397 *
1398 * Returns:
1399 * 0: Success
1400 * Else: Failure
1401 *------------------------------------------------------------------------*/
1402 usb_error_t
usb_suspend_resume(struct usb_device * udev,uint8_t do_suspend)1403 usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1404 {
1405 struct usb_interface *iface;
1406 uint8_t i;
1407
1408 if (udev == NULL) {
1409 /* nothing to do */
1410 return (USB_ERR_NORMAL_COMPLETION);
1411 }
1412 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1413
1414 sx_assert(&udev->sr_sx, SA_LOCKED);
1415
1416 USB_BUS_LOCK(udev->bus);
1417 /* filter the suspend events */
1418 if (udev->flags.peer_suspended == do_suspend) {
1419 USB_BUS_UNLOCK(udev->bus);
1420 /* nothing to do */
1421 return (USB_ERR_NORMAL_COMPLETION);
1422 }
1423 udev->flags.peer_suspended = do_suspend;
1424 USB_BUS_UNLOCK(udev->bus);
1425
1426 /* do the suspend or resume */
1427
1428 for (i = 0; i != USB_IFACE_MAX; i++) {
1429
1430 iface = usbd_get_iface(udev, i);
1431 if (iface == NULL) {
1432 /* looks like the end of the USB interfaces */
1433 break;
1434 }
1435 usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1436 }
1437 return (USB_ERR_NORMAL_COMPLETION);
1438 }
1439
1440 /*------------------------------------------------------------------------*
1441 * usbd_clear_stall_proc
1442 *
1443 * This function performs generic USB clear stall operations.
1444 *------------------------------------------------------------------------*/
1445 static void
usbd_clear_stall_proc(struct usb_proc_msg * _pm)1446 usbd_clear_stall_proc(struct usb_proc_msg *_pm)
1447 {
1448 struct usb_udev_msg *pm = (void *)_pm;
1449 struct usb_device *udev = pm->udev;
1450
1451 /* Change lock */
1452 USB_BUS_UNLOCK(udev->bus);
1453 mtx_lock(&udev->device_mtx);
1454
1455 /* Start clear stall callback */
1456 usbd_transfer_start(udev->ctrl_xfer[1]);
1457
1458 /* Change lock */
1459 mtx_unlock(&udev->device_mtx);
1460 USB_BUS_LOCK(udev->bus);
1461 }
1462
1463 /*------------------------------------------------------------------------*
1464 * usb_alloc_device
1465 *
1466 * This function allocates a new USB device. This function is called
1467 * when a new device has been put in the powered state, but not yet in
1468 * the addressed state. Get initial descriptor, set the address, get
1469 * full descriptor and get strings.
1470 *
1471 * Return values:
1472 * 0: Failure
1473 * Else: Success
1474 *------------------------------------------------------------------------*/
1475 struct usb_device *
usb_alloc_device(device_t parent_dev,struct usb_bus * bus,struct usb_device * parent_hub,uint8_t depth,uint8_t port_index,uint8_t port_no,enum usb_dev_speed speed,enum usb_hc_mode mode)1476 usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1477 struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1478 uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1479 {
1480 struct usb_attach_arg uaa;
1481 struct usb_device *udev;
1482 struct usb_device *adev;
1483 struct usb_device *hub;
1484 uint8_t *scratch_ptr;
1485 usb_error_t err;
1486 uint8_t device_index;
1487 uint8_t config_index;
1488 uint8_t config_quirk;
1489 uint8_t set_config_failed;
1490 uint8_t do_unlock;
1491
1492 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1493 "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1494 parent_dev, bus, parent_hub, depth, port_index, port_no,
1495 speed, mode);
1496
1497 /*
1498 * Find an unused device index. In USB Host mode this is the
1499 * same as the device address.
1500 *
1501 * Device index zero is not used and device index 1 should
1502 * always be the root hub.
1503 */
1504 for (device_index = USB_ROOT_HUB_ADDR;
1505 (device_index != bus->devices_max) &&
1506 (bus->devices[device_index] != NULL);
1507 device_index++) /* nop */;
1508
1509 if (device_index == bus->devices_max) {
1510 device_printf(bus->bdev,
1511 "No free USB device index for new device\n");
1512 return (NULL);
1513 }
1514
1515 if (depth > 0x10) {
1516 device_printf(bus->bdev,
1517 "Invalid device depth\n");
1518 return (NULL);
1519 }
1520 udev = bsd_malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1521 if (udev == NULL) {
1522 return (NULL);
1523 }
1524 /* initialise our SX-lock */
1525 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1526 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
1527 sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK);
1528
1529 cv_init(&udev->ctrlreq_cv, "WCTRL");
1530 cv_init(&udev->ref_cv, "UGONE");
1531
1532 /* initialise our mutex */
1533 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1534
1535 /* initialise generic clear stall */
1536 udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1537 udev->cs_msg[0].udev = udev;
1538 udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1539 udev->cs_msg[1].udev = udev;
1540
1541 /* initialise some USB device fields */
1542 udev->parent_hub = parent_hub;
1543 udev->parent_dev = parent_dev;
1544 udev->port_index = port_index;
1545 udev->port_no = port_no;
1546 udev->depth = depth;
1547 udev->bus = bus;
1548 udev->address = USB_START_ADDR; /* default value */
1549 udev->plugtime = (usb_ticks_t)CUR_TICKS;
1550 /*
1551 * We need to force the power mode to "on" because there are plenty
1552 * of USB devices out there that do not work very well with
1553 * automatic suspend and resume!
1554 */
1555 udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1556 udev->pwr_save.last_xfer_time = CUR_TICKS;
1557 /* we are not ready yet */
1558 udev->refcount = 1;
1559
1560 /* set up default endpoint descriptor */
1561 udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1562 udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1563 udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1564 udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1565 udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1566 udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1567 udev->ctrl_ep_desc.bInterval = 0;
1568
1569 /* set up default endpoint companion descriptor */
1570 udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1571 udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1572
1573 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1574
1575 udev->speed = speed;
1576 udev->flags.usb_mode = mode;
1577
1578 /* search for our High Speed USB HUB, if any */
1579
1580 adev = udev;
1581 hub = udev->parent_hub;
1582
1583 while (hub) {
1584 if (hub->speed == USB_SPEED_HIGH) {
1585 udev->hs_hub_addr = hub->address;
1586 udev->parent_hs_hub = hub;
1587 udev->hs_port_no = adev->port_no;
1588 break;
1589 }
1590 adev = hub;
1591 hub = hub->parent_hub;
1592 }
1593
1594 /* init the default endpoint */
1595 usb_init_endpoint(udev, 0,
1596 &udev->ctrl_ep_desc,
1597 &udev->ctrl_ep_comp_desc,
1598 &udev->ctrl_ep);
1599
1600 /* set device index */
1601 udev->device_index = device_index;
1602
1603 #if USB_HAVE_UGEN
1604 /* Create ugen name */
1605 (void)snprintf_s(udev->ugen_name, sizeof(udev->ugen_name),
1606 sizeof(udev->ugen_name) - 1, USB_GENERIC_NAME "%u.%u",
1607 device_get_unit(bus->bdev), device_index);
1608 LIST_FIRST(&udev->pd_list) = NULL;
1609
1610 /* Create the control endpoint device */
1611 udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
1612 FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
1613 #endif
1614 /* Initialise device */
1615 if (bus->methods->device_init != NULL) {
1616 err = (bus->methods->device_init) (udev);
1617 if (err != 0) {
1618 DPRINTFN(0, "device init %d failed "
1619 "(%s, ignored)\n", device_index,
1620 usbd_errstr(err));
1621 goto done;
1622 }
1623 }
1624
1625 /* set powered device state after device init is complete */
1626 usb_set_device_state(udev, USB_STATE_POWERED);
1627
1628 if (udev->flags.usb_mode == USB_MODE_HOST) {
1629
1630 err = usbd_req_set_address(udev, NULL, device_index);
1631
1632 /*
1633 * This is the new USB device address from now on, if
1634 * the set address request didn't set it already.
1635 */
1636 if (udev->address == USB_START_ADDR)
1637 udev->address = device_index;
1638 /*
1639 * We ignore any set-address errors, hence there are
1640 * buggy USB devices out there that actually receive
1641 * the SETUP PID, but manage to set the address before
1642 * the STATUS stage is ACK'ed. If the device responds
1643 * to the subsequent get-descriptor at the new
1644 * address, then we know that the set-address command
1645 * was successful.
1646 */
1647 if (err) {
1648 DPRINTFN(0, "set address %d failed "
1649 "(%s, ignored)\n", udev->address,
1650 usbd_errstr(err));
1651 }
1652 } else {
1653 /* We are not self powered */
1654 udev->flags.self_powered = 0;
1655
1656 /* Set unconfigured state */
1657 udev->curr_config_no = USB_UNCONFIG_NO;
1658 udev->curr_config_index = USB_UNCONFIG_INDEX;
1659
1660 /* Setup USB descriptors */
1661 err = (usb_temp_setup_by_index_p) (udev, usb_template);
1662 if (err) {
1663 DPRINTFN(0, "setting up USB template failed maybe the USB "
1664 "template module has not been loaded\n");
1665 goto done;
1666 }
1667 }
1668
1669 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1670
1671 /* setup the device descriptor and the initial "wMaxPacketSize" */
1672 err = usbd_setup_device_desc(udev, NULL);
1673
1674 if (err != 0) {
1675 /* try to enumerate two more times */
1676 err = usbd_req_re_enumerate(udev, NULL);
1677 if (err != 0) {
1678 err = usbd_req_re_enumerate(udev, NULL);
1679 if (err != 0) {
1680 goto done;
1681 }
1682 }
1683 }
1684
1685 /*
1686 * Setup temporary USB attach args so that we can figure out some
1687 * basic quirks for this device.
1688 */
1689 usb_init_attach_arg(udev, &uaa);
1690
1691 if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1692 udev->flags.uq_bus_powered = 1;
1693 }
1694 if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1695 udev->flags.no_strings = 1;
1696 }
1697 /*
1698 * Workaround for buggy USB devices.
1699 *
1700 * It appears that some string-less USB chips will crash and
1701 * disappear if any attempts are made to read any string
1702 * descriptors.
1703 *
1704 * Try to detect such chips by checking the strings in the USB
1705 * device descriptor. If no strings are present there we
1706 * simply disable all USB strings.
1707 */
1708
1709 /* Protect scratch area */
1710 do_unlock = usbd_ctrl_lock(udev);
1711
1712 scratch_ptr = udev->scratch.data;
1713
1714 if (udev->flags.no_strings) {
1715 err = USB_ERR_INVAL;
1716 } else if (udev->ddesc.iManufacturer ||
1717 udev->ddesc.iProduct ||
1718 udev->ddesc.iSerialNumber) {
1719 /* read out the language ID string */
1720 err = usbd_req_get_string_desc(udev, NULL,
1721 (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1722 } else {
1723 err = USB_ERR_INVAL;
1724 }
1725
1726 if (err || (scratch_ptr[0] < 4)) {
1727 udev->flags.no_strings = 1;
1728 } else {
1729 uint16_t langid;
1730 uint16_t pref;
1731 uint16_t mask;
1732 uint8_t x;
1733
1734 /* load preferred value and mask */
1735 pref = usb_lang_id;
1736 mask = usb_lang_mask;
1737
1738 /* align length correctly */
1739 scratch_ptr[0] &= ~1U;
1740
1741 /* fix compiler warning */
1742 langid = 0;
1743
1744 /* search for preferred language */
1745 for (x = 2; (x < scratch_ptr[0]); x += 2) {
1746 langid = UGETW(scratch_ptr + x);
1747 if ((langid & mask) == pref)
1748 break;
1749 }
1750 if (x >= scratch_ptr[0]) {
1751 /* pick the first language as the default */
1752 DPRINTFN(1, "Using first language\n");
1753 langid = UGETW(scratch_ptr + 2);
1754 }
1755
1756 DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1757 udev->langid = langid;
1758 }
1759
1760 if (do_unlock)
1761 usbd_ctrl_unlock(udev);
1762
1763 /* assume 100mA bus powered for now. Changed when configured. */
1764 udev->power = USB_MIN_POWER;
1765 /* fetch the vendor and product strings from the device */
1766 usbd_set_device_strings(udev);
1767
1768 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1769 /* USB device mode setup is complete */
1770 err = USB_ERR_NORMAL_COMPLETION;
1771 goto config_done;
1772 }
1773
1774
1775 /*
1776 * Most USB devices should attach to config index 0 by
1777 * default
1778 */
1779 if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1780 config_index = 0;
1781 config_quirk = 1;
1782 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1783 config_index = 1;
1784 config_quirk = 1;
1785 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1786 config_index = 2;
1787 config_quirk = 1;
1788 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1789 config_index = 3;
1790 config_quirk = 1;
1791 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1792 config_index = 4;
1793 config_quirk = 1;
1794 } else {
1795 config_index = 0;
1796 config_quirk = 0;
1797 }
1798
1799 set_config_failed = 0;
1800 repeat_set_config:
1801
1802 DPRINTF("setting config %u\n", config_index);
1803
1804 /* get the USB device configured */
1805 err = usbd_set_config_index(udev, config_index);
1806 if (err) {
1807 if (udev->ddesc.bNumConfigurations != 0) {
1808 if (!set_config_failed) {
1809 set_config_failed = 1;
1810 /* XXX try to re-enumerate the device */
1811 err = usbd_req_re_enumerate(udev, NULL);
1812 if (err == 0)
1813 goto repeat_set_config;
1814 }
1815 DPRINTFN(0, "Failure selecting configuration index %u:"
1816 "%s, port %u, addr %u (ignored)\n",
1817 config_index, usbd_errstr(err), udev->port_no,
1818 udev->address);
1819 }
1820 /*
1821 * Some USB devices do not have any configurations. Ignore any
1822 * set config failures!
1823 */
1824 err = USB_ERR_NORMAL_COMPLETION;
1825 goto config_done;
1826 }
1827 if ((!config_quirk) && (config_index + 1 < udev->ddesc.bNumConfigurations)) {
1828 if ((udev->cdesc->bNumInterface < 2) &&
1829 (usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0)) {
1830 DPRINTFN(0, "Found no endpoints, trying next config\n");
1831 config_index++;
1832 goto repeat_set_config;
1833 }
1834 }
1835
1836 config_done:
1837 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1838 udev->address, udev, udev->parent_hub);
1839
1840 /* register our device - we are ready */
1841 usb_bus_port_set_device(bus, parent_hub ?
1842 (parent_hub->hub->ports + port_index) : NULL, udev, device_index);
1843
1844 #if USB_HAVE_UGEN
1845 /* Symlink the ugen device name */
1846 udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
1847
1848 /* Announce device */
1849 PRINTK("%s: <%s> at %s\n", udev->ugen_name,
1850 usb_get_manufacturer(udev),
1851 device_get_nameunit(udev->bus->bdev));
1852 #endif
1853
1854 #ifdef LOSCFG_DRIVERS_HDF_USB_PNP_NOTIFY
1855 UsbPnpNotifyDevice("ATTACH", udev);
1856 #endif
1857
1858 #if USB_HAVE_DEVCTL
1859 usb_notify_addq("ATTACH", udev);
1860 #endif
1861 done:
1862 if (err) {
1863 /*
1864 * Free USB device and all subdevices, if any.
1865 */
1866 usb_free_device(udev, 0);
1867 udev = NULL;
1868 }
1869 return (udev);
1870 }
1871
1872 #if USB_HAVE_UGEN
1873 struct usb_fs_privdata *
usb_make_dev(struct usb_device * udev,const char * devname,int ep,int fi,int rwmode,uid_t uid,gid_t gid,int mode)1874 usb_make_dev(struct usb_device *udev, const char *devname, int ep,
1875 int fi, int rwmode, uid_t uid, gid_t gid, int mode)
1876 {
1877 struct usb_fs_privdata* pd;
1878 char buffer[32];
1879 int ret;
1880
1881 /* Store information to locate ourselves again later */
1882 pd = bsd_malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1883 M_WAITOK | M_ZERO);
1884 if (pd == NULL)
1885 return (NULL);
1886 pd->bus_index = device_get_unit(udev->bus->bdev);
1887 pd->dev_index = udev->device_index;
1888 pd->ep_addr = ep;
1889 pd->fifo_index = fi;
1890 pd->mode = rwmode;
1891
1892 /* Now, create the device itself */
1893 if (devname == NULL) {
1894 devname = buffer;
1895 (void)snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, USB_DEVICE_DIR "/%u.%u.%u",
1896 pd->bus_index, pd->dev_index, pd->ep_addr);
1897 } else {
1898 (void)snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, USB_DEVICE_DIR "/%s",
1899 devname);
1900 }
1901
1902 ret = strncpy_s(pd->cdev_name, sizeof(pd->cdev_name), buffer, strlen(buffer));
1903 if (ret != 0) {
1904 bsd_free(pd, M_USBDEV);
1905 usb_err("strncpy_s failed: %d\n", ret);
1906 return (NULL);
1907 }
1908
1909 ret = register_driver(pd->cdev_name, &usb_devsw, 0666, (void *)pd);
1910 if (ret < 0) {
1911 bsd_free(pd, M_USBDEV);
1912 usb_err("register_driver() failed: %d\n", ret);
1913 return (NULL);
1914 }
1915
1916 return (pd);
1917 }
1918
1919 void
usb_destroy_dev(struct usb_fs_privdata * pd)1920 usb_destroy_dev(struct usb_fs_privdata *pd)
1921 {
1922 int ret;
1923
1924 if (pd == NULL)
1925 return;
1926
1927 ret = unregister_driver(pd->cdev_name);
1928 if (ret < 0) {
1929 usb_err("unregister_driver() failed: %d\n", ret);
1930 return;
1931 }
1932
1933 bsd_free(pd, M_USBDEV);
1934 }
1935
1936 static void
usb_cdev_create(struct usb_device * udev)1937 usb_cdev_create(struct usb_device *udev)
1938 {
1939 struct usb_config_descriptor *cd;
1940 struct usb_endpoint_descriptor *ed;
1941 struct usb_descriptor *desc;
1942 struct usb_fs_privdata* pd;
1943 int inmode, outmode, inmask, outmask, mode;
1944 uint8_t ep;
1945
1946 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
1947
1948 DPRINTFN(2, "Creating device nodes\n");
1949
1950 if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
1951 inmode = FWRITE;
1952 outmode = FREAD;
1953 } else { /* USB_MODE_HOST */
1954 inmode = FREAD;
1955 outmode = FWRITE;
1956 }
1957
1958 inmask = 0;
1959 outmask = 0;
1960 desc = NULL;
1961
1962 /*
1963 * Collect all used endpoint numbers instead of just
1964 * generating 16 static endpoints.
1965 */
1966 cd = usbd_get_config_descriptor(udev);
1967 while ((desc = usb_desc_foreach(cd, desc))) {
1968 /* filter out all endpoint descriptors */
1969 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
1970 (desc->bLength >= sizeof(*ed))) {
1971 ed = (struct usb_endpoint_descriptor *)desc;
1972
1973 /* update masks */
1974 ep = ed->bEndpointAddress;
1975 if (UE_GET_DIR(ep) == UE_DIR_OUT)
1976 outmask = (unsigned int)outmask | (1 << UE_GET_ADDR(ep));
1977 else
1978 inmask = (unsigned int)inmask | (1 << UE_GET_ADDR(ep));
1979 }
1980 }
1981
1982 /* Create all available endpoints except EP0 */
1983 for (ep = 1; ep < 16; ep++) {
1984 mode = ((unsigned int)inmask & (1 << ep)) ? inmode : 0;
1985 mode = (unsigned int)mode | (((unsigned int)outmask & (1 << ep)) ? outmode : 0);
1986 if (mode == 0)
1987 continue; /* no IN or OUT endpoint */
1988
1989 pd = usb_make_dev(udev, NULL, ep, 0,
1990 mode, UID_ROOT, GID_OPERATOR, 0600);
1991
1992 if (pd != NULL)
1993 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
1994 }
1995 }
1996
1997 static void
usb_cdev_free(struct usb_device * udev)1998 usb_cdev_free(struct usb_device *udev)
1999 {
2000 struct usb_fs_privdata* pd;
2001
2002 DPRINTFN(2, "Freeing device nodes\n");
2003
2004 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2005 //KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2006
2007 LIST_REMOVE(pd, pd_next);
2008
2009 usb_destroy_dev(pd);
2010 }
2011 }
2012 #endif
2013
2014 /*------------------------------------------------------------------------*
2015 * usb_free_device
2016 *
2017 * This function is NULL safe and will free an USB device and its
2018 * children devices, if any.
2019 *
2020 * Flag values: Reserved, set to zero.
2021 *------------------------------------------------------------------------*/
2022 void
usb_free_device(struct usb_device * udev,uint8_t flag)2023 usb_free_device(struct usb_device *udev, uint8_t flag)
2024 {
2025 struct usb_bus *bus;
2026
2027 if (udev == NULL)
2028 return; /* already freed */
2029
2030 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
2031
2032 bus = udev->bus;
2033
2034 /* set DETACHED state to prevent any further references */
2035 usb_set_device_state(udev, USB_STATE_DETACHED);
2036
2037 #ifdef LOSCFG_DRIVERS_HDF_USB_PNP_NOTIFY
2038 UsbPnpNotifyDevice("DETACH", udev);
2039 #endif
2040
2041 #if USB_HAVE_DEVCTL
2042 usb_notify_addq("DETACH", udev);
2043 #endif
2044
2045 #if USB_HAVE_UGEN
2046 if (!rebooting) {
2047 PRINTK("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2048 usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2049 }
2050
2051 /* Destroy UGEN symlink, if any */
2052 if (udev->ugen_symlink) {
2053 usb_free_symlink(udev->ugen_symlink);
2054 udev->ugen_symlink = NULL;
2055 }
2056
2057 usb_destroy_dev(udev->ctrl_dev);
2058 #endif
2059
2060 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2061 /* stop receiving any control transfers (Device Side Mode) */
2062 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2063 }
2064
2065 /* the following will get the device unconfigured in software */
2066 usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2067
2068 /* final device unregister after all character devices are closed */
2069 usb_bus_port_set_device(bus, udev->parent_hub ?
2070 (udev->parent_hub->hub->ports + udev->port_index) : NULL,
2071 NULL, USB_ROOT_HUB_ADDR);
2072
2073 /* unsetup any leftover default USB transfers */
2074 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2075
2076 /* template unsetup, if any */
2077 (usb_temp_unsetup_p) (udev);
2078
2079 /*
2080 * Make sure that our clear-stall messages are not queued
2081 * anywhere:
2082 */
2083 USB_BUS_LOCK(udev->bus);
2084 usb_proc_mwait(USB_BUS_CS_PROC(udev->bus),
2085 &udev->cs_msg[0], &udev->cs_msg[1]);
2086 USB_BUS_UNLOCK(udev->bus);
2087
2088 /* wait for all references to go away */
2089 usb_wait_pending_refs(udev);
2090
2091 sx_destroy(&udev->enum_sx);
2092 sx_destroy(&udev->sr_sx);
2093 sx_destroy(&udev->ctrl_sx);
2094
2095 cv_destroy(&udev->ctrlreq_cv);
2096 cv_destroy(&udev->ref_cv);
2097
2098 mtx_destroy(&udev->device_mtx);
2099 #if USB_HAVE_UGEN
2100 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2101 #endif
2102
2103 /* Uninitialise device */
2104 if (bus->methods->device_uninit != NULL)
2105 (bus->methods->device_uninit) (udev);
2106
2107 /* free device */
2108 bsd_free(udev->serial, M_USB);
2109 udev->serial = NULL;
2110 bsd_free(udev->manufacturer, M_USB);
2111 udev->manufacturer = NULL;
2112 bsd_free(udev->product, M_USB);
2113 udev->product = NULL;
2114 bsd_free(udev, M_USB);
2115 }
2116
2117 /*------------------------------------------------------------------------*
2118 * usbd_get_iface
2119 *
2120 * This function is the safe way to get the USB interface structure
2121 * pointer by interface index.
2122 *
2123 * Return values:
2124 * NULL: Interface not present.
2125 * Else: Pointer to USB interface structure.
2126 *------------------------------------------------------------------------*/
2127 struct usb_interface *
usbd_get_iface(struct usb_device * udev,uint8_t iface_index)2128 usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2129 {
2130 struct usb_interface *iface = udev->ifaces + iface_index;
2131
2132 if (iface_index >= udev->ifaces_max)
2133 return (NULL);
2134 return (iface);
2135 }
2136
2137 /*------------------------------------------------------------------------*
2138 * usbd_find_descriptor
2139 *
2140 * This function will lookup the first descriptor that matches the
2141 * criteria given by the arguments "type" and "subtype". Descriptors
2142 * will only be searched within the interface having the index
2143 * "iface_index". If the "id" argument points to an USB descriptor,
2144 * it will be skipped before the search is started. This allows
2145 * searching for multiple descriptors using the same criteria. Else
2146 * the search is started after the interface descriptor.
2147 *
2148 * Return values:
2149 * NULL: End of descriptors
2150 * Else: A descriptor matching the criteria
2151 *------------------------------------------------------------------------*/
2152 void *
usbd_find_descriptor(struct usb_device * udev,void * id,uint8_t iface_index,uint8_t type,uint8_t type_mask,uint8_t subtype,uint8_t subtype_mask)2153 usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2154 uint8_t type, uint8_t type_mask,
2155 uint8_t subtype, uint8_t subtype_mask)
2156 {
2157 struct usb_descriptor *desc;
2158 struct usb_config_descriptor *cd;
2159 struct usb_interface *iface;
2160
2161 cd = usbd_get_config_descriptor(udev);
2162 if (cd == NULL) {
2163 return (NULL);
2164 }
2165 if (id == NULL) {
2166 iface = usbd_get_iface(udev, iface_index);
2167 if (iface == NULL) {
2168 return (NULL);
2169 }
2170 id = usbd_get_interface_descriptor(iface);
2171 if (id == NULL) {
2172 return (NULL);
2173 }
2174 }
2175 desc = (void *)id;
2176
2177 while ((desc = usb_desc_foreach(cd, desc))) {
2178
2179 if (desc->bDescriptorType == UDESC_INTERFACE) {
2180 break;
2181 }
2182 if (((desc->bDescriptorType & type_mask) == type) &&
2183 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2184 return (desc);
2185 }
2186 }
2187 return (NULL);
2188 }
2189
2190 /*------------------------------------------------------------------------*
2191 * usb_devinfo
2192 *
2193 * This function will dump information from the device descriptor
2194 * belonging to the USB device pointed to by "udev", to the string
2195 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2196 * including the terminating zero.
2197 *------------------------------------------------------------------------*/
2198 void
usb_devinfo(struct usb_device * udev,char * dst_ptr,uint16_t dst_len)2199 usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2200 {
2201 struct usb_device_descriptor *udd = &udev->ddesc;
2202 uint16_t bcdDevice;
2203 uint16_t bcdUSB;
2204
2205 bcdUSB = UGETW(udd->bcdUSB);
2206 bcdDevice = UGETW(udd->bcdDevice);
2207
2208 if (udd->bDeviceClass != 0xFF) {
2209 (void)snprintf_s(dst_ptr, dst_len, dst_len - 1, "%s %s, class %d/%d, rev %x.%02x/"
2210 "%x.%02x, addr %d",
2211 usb_get_manufacturer(udev),
2212 usb_get_product(udev),
2213 udd->bDeviceClass, udd->bDeviceSubClass,
2214 (bcdUSB >> 8), bcdUSB & 0xFF,
2215 (bcdDevice >> 8), bcdDevice & 0xFF,
2216 udev->address);
2217 } else {
2218 (void)snprintf_s(dst_ptr, dst_len, dst_len - 1, "%s %s, rev %x.%02x/"
2219 "%x.%02x, addr %d",
2220 usb_get_manufacturer(udev),
2221 usb_get_product(udev),
2222 (bcdUSB >> 8), bcdUSB & 0xFF,
2223 (bcdDevice >> 8), bcdDevice & 0xFF,
2224 udev->address);
2225 }
2226 }
2227
2228 #ifdef USB_VERBOSE
2229 /*
2230 * Descriptions of of known vendors and devices ("products").
2231 */
2232 struct usb_knowndev {
2233 uint16_t vendor;
2234 uint16_t product;
2235 uint32_t flags;
2236 const char *vendorname;
2237 const char *productname;
2238 };
2239
2240 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
2241
2242 #include "implementation/usbdevs.h"
2243 #include "usbdevs_data.h"
2244 #endif /* USB_VERBOSE */
2245
2246 static void
usbd_set_device_strings(struct usb_device * udev)2247 usbd_set_device_strings(struct usb_device *udev)
2248 {
2249 struct usb_device_descriptor *udd = &udev->ddesc;
2250 #ifdef USB_VERBOSE
2251 const struct usb_knowndev *kdp;
2252 #endif
2253 char *temp_ptr;
2254 size_t temp_size;
2255 uint16_t vendor_id;
2256 uint16_t product_id;
2257 uint8_t do_unlock;
2258
2259 /* Protect scratch area */
2260 do_unlock = usbd_ctrl_lock(udev);
2261
2262 temp_ptr = (char *)udev->scratch.data;
2263 temp_size = sizeof(udev->scratch.data);
2264
2265 vendor_id = UGETW(udd->idVendor);
2266 product_id = UGETW(udd->idProduct);
2267
2268 /* get serial number string */
2269 (void)usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2270 udev->ddesc.iSerialNumber);
2271 udev->serial = bsd_strdup(temp_ptr, M_USB);
2272
2273 /* get manufacturer string */
2274 (void)usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2275 udev->ddesc.iManufacturer);
2276 usb_trim_spaces(temp_ptr);
2277 if (temp_ptr[0] != '\0')
2278 udev->manufacturer = bsd_strdup(temp_ptr, M_USB);
2279
2280 /* get product string */
2281 (void)usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2282 udev->ddesc.iProduct);
2283 usb_trim_spaces(temp_ptr);
2284 if (temp_ptr[0] != '\0')
2285 udev->product = bsd_strdup(temp_ptr, M_USB);
2286
2287 #ifdef USB_VERBOSE
2288 if ((udev->manufacturer == NULL) || (udev->product == NULL)) {
2289 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2290 if ((kdp->vendor == vendor_id) &&
2291 ((kdp->product == product_id) ||
2292 ((kdp->flags & USB_KNOWNDEV_NOPROD) != 0)))
2293 break;
2294 }
2295 if (kdp->vendorname != NULL) {
2296 /* XXX should use pointer to knowndevs string */
2297 if (udev->manufacturer == NULL) {
2298 udev->manufacturer = bsd_strdup(kdp->vendorname,
2299 M_USB);
2300 }
2301 if ((udev->product == NULL) &&
2302 ((kdp->flags & USB_KNOWNDEV_NOPROD) == 0)) {
2303 udev->product = bsd_strdup(kdp->productname,
2304 M_USB);
2305 }
2306 }
2307 }
2308 #endif
2309 /* Provide default strings if none were found */
2310 if (udev->manufacturer == NULL) {
2311 (void)snprintf_s(temp_ptr, temp_size, temp_size - 1, "vendor 0x%04x", vendor_id);
2312 udev->manufacturer = bsd_strdup(temp_ptr, M_USB);
2313 }
2314 if (udev->product == NULL) {
2315 (void)snprintf_s(temp_ptr, temp_size, temp_size - 1, "product 0x%04x", product_id);
2316 udev->product = bsd_strdup(temp_ptr, M_USB);
2317 }
2318
2319 if (do_unlock)
2320 usbd_ctrl_unlock(udev);
2321 }
2322
2323 /*
2324 * Returns:
2325 * See: USB_MODE_XXX
2326 */
2327 enum usb_hc_mode
usbd_get_mode(struct usb_device * udev)2328 usbd_get_mode(struct usb_device *udev)
2329 {
2330 return (udev->flags.usb_mode);
2331 }
2332
2333 /*
2334 * Returns:
2335 * See: USB_SPEED_XXX
2336 */
2337 enum usb_dev_speed
usbd_get_speed(struct usb_device * udev)2338 usbd_get_speed(struct usb_device *udev)
2339 {
2340 return (udev->speed);
2341 }
2342
2343 uint32_t
usbd_get_isoc_fps(struct usb_device * udev)2344 usbd_get_isoc_fps(struct usb_device *udev)
2345 {
2346 ; /* indent fix */
2347 switch (udev->speed) {
2348 case USB_SPEED_LOW:
2349 case USB_SPEED_FULL:
2350 return (1000);
2351 default:
2352 return (8000);
2353 }
2354 }
2355
2356 struct usb_device_descriptor *
usbd_get_device_descriptor(struct usb_device * udev)2357 usbd_get_device_descriptor(struct usb_device *udev)
2358 {
2359 if (udev == NULL)
2360 return (NULL); /* be NULL safe */
2361 return (&udev->ddesc);
2362 }
2363
2364 struct usb_config_descriptor *
usbd_get_config_descriptor(struct usb_device * udev)2365 usbd_get_config_descriptor(struct usb_device *udev)
2366 {
2367 if (udev == NULL)
2368 return (NULL); /* be NULL safe */
2369 return (udev->cdesc);
2370 }
2371
2372 /*------------------------------------------------------------------------*
2373 * usb_test_quirk - test a device for a given quirk
2374 *
2375 * Return values:
2376 * 0: The USB device does not have the given quirk.
2377 * Else: The USB device has the given quirk.
2378 *------------------------------------------------------------------------*/
2379 uint8_t
usb_test_quirk(const struct usb_attach_arg * uaa,uint16_t quirk)2380 usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2381 {
2382 uint8_t found;
2383 uint8_t x;
2384
2385 if (quirk == UQ_NONE)
2386 return (0);
2387
2388 /* search the automatic per device quirks first */
2389
2390 for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2391 if (uaa->device->autoQuirk[x] == quirk)
2392 return (1);
2393 }
2394
2395 /* search global quirk table, if any */
2396
2397 found = (usb_test_quirk_p) (&uaa->info, quirk);
2398
2399 return (found);
2400 }
2401
2402 struct usb_interface_descriptor *
usbd_get_interface_descriptor(struct usb_interface * iface)2403 usbd_get_interface_descriptor(struct usb_interface *iface)
2404 {
2405 if (iface == NULL)
2406 return (NULL); /* be NULL safe */
2407 return (iface->idesc);
2408 }
2409
2410 uint8_t
usbd_get_interface_altindex(struct usb_interface * iface)2411 usbd_get_interface_altindex(struct usb_interface *iface)
2412 {
2413 return (iface->alt_index);
2414 }
2415
2416 uint8_t
usbd_get_bus_index(struct usb_device * udev)2417 usbd_get_bus_index(struct usb_device *udev)
2418 {
2419 return ((uint8_t)device_get_unit(udev->bus->bdev));
2420 }
2421
2422 uint8_t
usbd_get_device_index(struct usb_device * udev)2423 usbd_get_device_index(struct usb_device *udev)
2424 {
2425 return (udev->device_index);
2426 }
2427
2428 #if USB_HAVE_DEVCTL
2429 static void
usb_notify_addq(const char * type,struct usb_device * udev)2430 usb_notify_addq(const char *type, struct usb_device *udev)
2431 {
2432 struct usb_interface *iface;
2433 struct sbuf *sb;
2434 int i;
2435
2436 /* announce the device */
2437 sb = sbuf_new_auto();
2438 sbuf_printf(sb,
2439 #if USB_HAVE_UGEN
2440 "ugen=%s "
2441 "cdev=%s "
2442 #endif
2443 "vendor=0x%04x "
2444 "product=0x%04x "
2445 "devclass=0x%02x "
2446 "devsubclass=0x%02x "
2447 "sernum=\"%s\" "
2448 "release=0x%04x "
2449 "mode=%s "
2450 "port=%u "
2451 #if USB_HAVE_UGEN
2452 "parent=%s"
2453 #endif
2454 "",
2455 #if USB_HAVE_UGEN
2456 udev->ugen_name,
2457 udev->ugen_name,
2458 #endif
2459 UGETW(udev->ddesc.idVendor),
2460 UGETW(udev->ddesc.idProduct),
2461 udev->ddesc.bDeviceClass,
2462 udev->ddesc.bDeviceSubClass,
2463 usb_get_serial(udev),
2464 UGETW(udev->ddesc.bcdDevice),
2465 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2466 udev->port_no
2467 #if USB_HAVE_UGEN
2468 , udev->parent_hub != NULL ?
2469 udev->parent_hub->ugen_name :
2470 device_get_nameunit(device_get_parent(udev->bus->bdev))
2471 #endif
2472 );
2473 sbuf_finish(sb);
2474 devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2475 sbuf_delete(sb);
2476
2477 /* announce each interface */
2478 for (i = 0; i < USB_IFACE_MAX; i++) {
2479 iface = usbd_get_iface(udev, i);
2480 if (iface == NULL)
2481 break; /* end of interfaces */
2482 if (iface->idesc == NULL)
2483 continue; /* no interface descriptor */
2484
2485 sb = sbuf_new_auto();
2486 sbuf_printf(sb,
2487 #if USB_HAVE_UGEN
2488 "ugen=%s "
2489 "cdev=%s "
2490 #endif
2491 "vendor=0x%04x "
2492 "product=0x%04x "
2493 "devclass=0x%02x "
2494 "devsubclass=0x%02x "
2495 "sernum=\"%s\" "
2496 "release=0x%04x "
2497 "mode=%s "
2498 "interface=%d "
2499 "endpoints=%d "
2500 "intclass=0x%02x "
2501 "intsubclass=0x%02x "
2502 "intprotocol=0x%02x",
2503 #if USB_HAVE_UGEN
2504 udev->ugen_name,
2505 udev->ugen_name,
2506 #endif
2507 UGETW(udev->ddesc.idVendor),
2508 UGETW(udev->ddesc.idProduct),
2509 udev->ddesc.bDeviceClass,
2510 udev->ddesc.bDeviceSubClass,
2511 usb_get_serial(udev),
2512 UGETW(udev->ddesc.bcdDevice),
2513 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2514 iface->idesc->bInterfaceNumber,
2515 iface->idesc->bNumEndpoints,
2516 iface->idesc->bInterfaceClass,
2517 iface->idesc->bInterfaceSubClass,
2518 iface->idesc->bInterfaceProtocol);
2519 sbuf_finish(sb);
2520 devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2521 sbuf_delete(sb);
2522 }
2523 }
2524 #endif
2525
2526 #if USB_HAVE_UGEN
2527 /*------------------------------------------------------------------------*
2528 * usb_fifo_free_wrap
2529 *
2530 * This function will free the FIFOs.
2531 *
2532 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2533 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2534 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2535 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2536 * control endpoint FIFOs. If "iface_index" is not set to
2537 * "USB_IFACE_INDEX_ANY" the flag has no effect.
2538 *------------------------------------------------------------------------*/
2539 static void
usb_fifo_free_wrap(struct usb_device * udev,uint8_t iface_index,uint8_t flag)2540 usb_fifo_free_wrap(struct usb_device *udev,
2541 uint8_t iface_index, uint8_t flag)
2542 {
2543 struct usb_fifo *f;
2544 uint16_t i;
2545
2546 /*
2547 * Free any USB FIFOs on the given interface:
2548 */
2549 for (i = 0; i != USB_FIFO_MAX; i++) {
2550 f = udev->fifo[i];
2551 if (f == NULL) {
2552 continue;
2553 }
2554 /* Check if the interface index matches */
2555 if (iface_index == f->iface_index) {
2556 if (f->methods != &usb_ugen_methods) {
2557 /*
2558 * Don't free any non-generic FIFOs in
2559 * this case.
2560 */
2561 continue;
2562 }
2563 if ((f->dev_ep_index == 0) &&
2564 (f->fs_xfer == NULL)) {
2565 /* no need to free this FIFO */
2566 continue;
2567 }
2568 } else if (iface_index == USB_IFACE_INDEX_ANY) {
2569 if ((f->methods == &usb_ugen_methods) &&
2570 (f->dev_ep_index == 0) &&
2571 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2572 (f->fs_xfer == NULL)) {
2573 /* no need to free this FIFO */
2574 continue;
2575 }
2576 } else {
2577 /* no need to free this FIFO */
2578 continue;
2579 }
2580 /* free this FIFO */
2581 usb_fifo_free(f);
2582 }
2583 }
2584 #endif
2585
2586 /*------------------------------------------------------------------------*
2587 * usb_peer_can_wakeup
2588 *
2589 * Return values:
2590 * 0: Peer cannot do resume signalling.
2591 * Else: Peer can do resume signalling.
2592 *------------------------------------------------------------------------*/
2593 uint8_t
usb_peer_can_wakeup(struct usb_device * udev)2594 usb_peer_can_wakeup(struct usb_device *udev)
2595 {
2596 const struct usb_config_descriptor *cdp;
2597
2598 cdp = udev->cdesc;
2599 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2600 return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2601 }
2602 return (0); /* not supported */
2603 }
2604
2605 void
usb_set_device_state(struct usb_device * udev,enum usb_dev_state state)2606 usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2607 {
2608
2609 KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2610
2611 DPRINTF("udev %p state %s -> %s\n", udev,
2612 usb_statestr(udev->state), usb_statestr(state));
2613
2614 #if USB_HAVE_UGEN
2615 mtx_lock(&usb_ref_lock);
2616 #endif
2617 udev->state = state;
2618 #if USB_HAVE_UGEN
2619 mtx_unlock(&usb_ref_lock);
2620 #endif
2621 if (udev->bus->methods->device_state_change != NULL)
2622 (udev->bus->methods->device_state_change) (udev);
2623 }
2624
2625 enum usb_dev_state
usb_get_device_state(struct usb_device * udev)2626 usb_get_device_state(struct usb_device *udev)
2627 {
2628 if (udev == NULL)
2629 return (USB_STATE_DETACHED);
2630 return (udev->state);
2631 }
2632
2633 uint8_t
usbd_device_attached(struct usb_device * udev)2634 usbd_device_attached(struct usb_device *udev)
2635 {
2636 return (udev->state > USB_STATE_DETACHED);
2637 }
2638
2639 /*
2640 * The following function locks enumerating the given USB device. If
2641 * the lock is already grabbed this function returns zero. Else a
2642 * non-zero value is returned.
2643 */
2644 uint8_t
usbd_enum_lock(struct usb_device * udev)2645 usbd_enum_lock(struct usb_device *udev)
2646 {
2647 if (sx_xlocked(&udev->enum_sx))
2648 return (0);
2649
2650 sx_xlock(&udev->enum_sx);
2651 sx_xlock(&udev->sr_sx);
2652
2653 /*
2654 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2655 * are locked before locking Giant. Else the lock can be
2656 * locked multiple times.
2657 */
2658 mtx_lock(&Giant);
2659
2660 return (1);
2661 }
2662
2663 /* The following function unlocks enumerating the given USB device. */
2664
2665 void
usbd_enum_unlock(struct usb_device * udev)2666 usbd_enum_unlock(struct usb_device *udev)
2667 {
2668 mtx_unlock(&Giant);
2669 sx_xunlock(&udev->enum_sx);
2670 sx_xunlock(&udev->sr_sx);
2671 }
2672
2673 /* The following function locks suspend and resume. */
2674
2675 void
usbd_sr_lock(struct usb_device * udev)2676 usbd_sr_lock(struct usb_device *udev)
2677 {
2678 sx_xlock(&udev->sr_sx);
2679 /*
2680 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2681 * are locked before locking Giant. Else the lock can be
2682 * locked multiple times.
2683 */
2684 mtx_lock(&Giant);
2685 }
2686
2687 /* The following function unlocks suspend and resume. */
2688
2689 void
usbd_sr_unlock(struct usb_device * udev)2690 usbd_sr_unlock(struct usb_device *udev)
2691 {
2692 mtx_unlock(&Giant);
2693 sx_xunlock(&udev->sr_sx);
2694 }
2695
2696 /*
2697 * The following function checks the enumerating lock for the given
2698 * USB device.
2699 */
2700
2701 uint8_t
usbd_enum_is_locked(struct usb_device * udev)2702 usbd_enum_is_locked(struct usb_device *udev)
2703 {
2704 return (sx_xlocked(&udev->enum_sx));
2705 }
2706
2707 /*
2708 * The following function is used to serialize access to USB control
2709 * transfers and the USB scratch area. If the lock is already grabbed
2710 * this function returns zero. Else a value of one is returned.
2711 */
2712 uint8_t
usbd_ctrl_lock(struct usb_device * udev)2713 usbd_ctrl_lock(struct usb_device *udev)
2714 {
2715 if (sx_xlocked(&udev->ctrl_sx))
2716 return (0);
2717 sx_xlock(&udev->ctrl_sx);
2718
2719 /*
2720 * We need to allow suspend and resume at this point, else the
2721 * control transfer will timeout if the device is suspended!
2722 */
2723 if (usbd_enum_is_locked(udev))
2724 usbd_sr_unlock(udev);
2725 return (1);
2726 }
2727
2728 void
usbd_ctrl_unlock(struct usb_device * udev)2729 usbd_ctrl_unlock(struct usb_device *udev)
2730 {
2731 sx_xunlock(&udev->ctrl_sx);
2732
2733 /*
2734 * Restore the suspend and resume lock after we have unlocked
2735 * the USB control transfer lock to avoid LOR:
2736 */
2737 if (usbd_enum_is_locked(udev))
2738 usbd_sr_lock(udev);
2739 }
2740
2741 /*
2742 * The following function is used to set the per-interface specific
2743 * plug and play information. The string referred to by the pnpinfo
2744 * argument can safely be freed after calling this function. The
2745 * pnpinfo of an interface will be reset at device detach or when
2746 * passing a NULL argument to this function. This function
2747 * returns zero on success, else a USB_ERR_XXX failure code.
2748 */
2749
2750 usb_error_t
usbd_set_pnpinfo(struct usb_device * udev,uint8_t iface_index,const char * pnpinfo)2751 usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
2752 {
2753 struct usb_interface *iface;
2754
2755 iface = usbd_get_iface(udev, iface_index);
2756 if (iface == NULL)
2757 return (USB_ERR_INVAL);
2758
2759 if (iface->pnpinfo != NULL) {
2760 bsd_free(iface->pnpinfo, M_USBDEV);
2761 iface->pnpinfo = NULL;
2762 }
2763
2764 if ((pnpinfo == NULL) || (pnpinfo[0] == 0))
2765 return (USB_ERR_NORMAL_COMPLETION); /* success */
2766
2767 iface->pnpinfo = bsd_strdup(pnpinfo, M_USBDEV);
2768 if (iface->pnpinfo == NULL)
2769 return (USB_ERR_NOMEM);
2770
2771 return (USB_ERR_NORMAL_COMPLETION); /* success */
2772 }
2773
2774 usb_error_t
usbd_add_dynamic_quirk(struct usb_device * udev,uint16_t quirk)2775 usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
2776 {
2777 uint8_t x;
2778
2779 for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2780 if ((udev->autoQuirk[x] == 0) ||
2781 (udev->autoQuirk[x] == quirk)) {
2782 udev->autoQuirk[x] = quirk;
2783 return (USB_ERR_NORMAL_COMPLETION); /* success */
2784 }
2785 }
2786 return (USB_ERR_NOMEM);
2787 }
2788
2789 /*
2790 * The following function is used to select the endpoint mode. It
2791 * should not be called outside enumeration context.
2792 */
2793
2794 usb_error_t
usbd_set_endpoint_mode(struct usb_device * udev,struct usb_endpoint * ep,uint8_t ep_mode)2795 usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
2796 uint8_t ep_mode)
2797 {
2798 usb_error_t error;
2799 uint8_t do_unlock;
2800
2801 /* Prevent re-enumeration */
2802 do_unlock = usbd_enum_lock(udev);
2803
2804 if (udev->bus->methods->set_endpoint_mode != NULL) {
2805 error = (udev->bus->methods->set_endpoint_mode) (
2806 udev, ep, ep_mode);
2807 } else if (ep_mode != USB_EP_MODE_DEFAULT) {
2808 error = USB_ERR_INVAL;
2809 } else {
2810 error = USB_ERR_NORMAL_COMPLETION;
2811 }
2812
2813 /* only set new mode regardless of error */
2814 ep->ep_mode = ep_mode;
2815
2816 if (do_unlock)
2817 usbd_enum_unlock(udev);
2818
2819 return (error);
2820 }
2821
2822 uint8_t
usbd_get_endpoint_mode(struct usb_device * udev,struct usb_endpoint * ep)2823 usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
2824 {
2825 return (ep->ep_mode);
2826 }
2827
2828 #undef USB_DEBUG_VAR
2829