1184610Salfred/* $FreeBSD: stable/10/sys/dev/usb/usb_device.c 361908 2020-06-08 09:25:01Z hselasky $ */ 2184610Salfred/*- 3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4184610Salfred * 5184610Salfred * Redistribution and use in source and binary forms, with or without 6184610Salfred * modification, are permitted provided that the following conditions 7184610Salfred * are met: 8184610Salfred * 1. Redistributions of source code must retain the above copyright 9184610Salfred * notice, this list of conditions and the following disclaimer. 10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer in the 12184610Salfred * documentation and/or other materials provided with the distribution. 13184610Salfred * 14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24184610Salfred * SUCH DAMAGE. 25184610Salfred */ 26184610Salfred 27246122Shselasky#ifdef USB_GLOBAL_INCLUDE_FILE 28246122Shselasky#include USB_GLOBAL_INCLUDE_FILE 29246122Shselasky#else 30194677Sthompsa#include <sys/stdint.h> 31194677Sthompsa#include <sys/stddef.h> 32194677Sthompsa#include <sys/param.h> 33194677Sthompsa#include <sys/queue.h> 34194677Sthompsa#include <sys/types.h> 35194677Sthompsa#include <sys/systm.h> 36194677Sthompsa#include <sys/kernel.h> 37194677Sthompsa#include <sys/bus.h> 38194677Sthompsa#include <sys/module.h> 39194677Sthompsa#include <sys/lock.h> 40194677Sthompsa#include <sys/mutex.h> 41194677Sthompsa#include <sys/condvar.h> 42194677Sthompsa#include <sys/sysctl.h> 43194677Sthompsa#include <sys/sx.h> 44194677Sthompsa#include <sys/unistd.h> 45194677Sthompsa#include <sys/callout.h> 46194677Sthompsa#include <sys/malloc.h> 47194677Sthompsa#include <sys/priv.h> 48194677Sthompsa#include <sys/conf.h> 49194677Sthompsa#include <sys/fcntl.h> 50194677Sthompsa 51188942Sthompsa#include <dev/usb/usb.h> 52194677Sthompsa#include <dev/usb/usbdi.h> 53194677Sthompsa#include <dev/usb/usbdi_util.h> 54188942Sthompsa#include <dev/usb/usb_ioctl.h> 55213435Shselasky 56213435Shselasky#if USB_HAVE_UGEN 57213435Shselasky#include <sys/sbuf.h> 58213435Shselasky#endif 59213435Shselasky 60188746Sthompsa#include "usbdevs.h" 61184610Salfred 62194228Sthompsa#define USB_DEBUG_VAR usb_debug 63184610Salfred 64188942Sthompsa#include <dev/usb/usb_core.h> 65188942Sthompsa#include <dev/usb/usb_debug.h> 66188942Sthompsa#include <dev/usb/usb_process.h> 67188942Sthompsa#include <dev/usb/usb_device.h> 68188942Sthompsa#include <dev/usb/usb_busdma.h> 69188942Sthompsa#include <dev/usb/usb_transfer.h> 70188942Sthompsa#include <dev/usb/usb_request.h> 71188942Sthompsa#include <dev/usb/usb_dynamic.h> 72188942Sthompsa#include <dev/usb/usb_hub.h> 73188942Sthompsa#include <dev/usb/usb_util.h> 74190180Sthompsa#include <dev/usb/usb_msctest.h> 75190180Sthompsa#if USB_HAVE_UGEN 76188942Sthompsa#include <dev/usb/usb_dev.h> 77188942Sthompsa#include <dev/usb/usb_generic.h> 78190180Sthompsa#endif 79184610Salfred 80188942Sthompsa#include <dev/usb/quirk/usb_quirk.h> 81184610Salfred 82188942Sthompsa#include <dev/usb/usb_controller.h> 83188942Sthompsa#include <dev/usb/usb_bus.h> 84246122Shselasky#endif /* USB_GLOBAL_INCLUDE_FILE */ 85184610Salfred 86190754Sthompsa/* function prototypes */ 87184610Salfred 88194228Sthompsastatic void usb_init_endpoint(struct usb_device *, uint8_t, 89213435Shselasky struct usb_endpoint_descriptor *, 90213435Shselasky struct usb_endpoint_ss_comp_descriptor *, 91213435Shselasky struct usb_endpoint *); 92194228Sthompsastatic void usb_unconfigure(struct usb_device *, uint8_t); 93194228Sthompsastatic void usb_detach_device_sub(struct usb_device *, device_t *, 94214429Shselasky char **, uint8_t); 95194228Sthompsastatic uint8_t usb_probe_and_attach_sub(struct usb_device *, 96192984Sthompsa struct usb_attach_arg *); 97194228Sthompsastatic void usb_init_attach_arg(struct usb_device *, 98192984Sthompsa struct usb_attach_arg *); 99194228Sthompsastatic void usb_suspend_resume_sub(struct usb_device *, device_t, 100185948Sthompsa uint8_t); 101261105Shselaskystatic usb_proc_callback_t usbd_clear_stall_proc; 102213435Shselaskystatic usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t); 103194228Sthompsastatic void usbd_set_device_strings(struct usb_device *); 104214221Shselasky#if USB_HAVE_DEVCTL 105214221Shselaskystatic void usb_notify_addq(const char *type, struct usb_device *); 106214221Shselasky#endif 107190191Sthompsa#if USB_HAVE_UGEN 108194228Sthompsastatic void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t); 109194228Sthompsastatic void usb_cdev_create(struct usb_device *); 110194228Sthompsastatic void usb_cdev_free(struct usb_device *); 111189599Sthompsa#endif 112184610Salfred 113184610Salfred/* This variable is global to allow easy access to it: */ 114184610Salfred 115250202Shselasky#ifdef USB_TEMPLATE 116250202Shselaskyint usb_template = USB_TEMPLATE; 117250202Shselasky#else 118250202Shselaskyint usb_template; 119250202Shselasky#endif 120184610Salfred 121197559SthompsaTUNABLE_INT("hw.usb.usb_template", &usb_template); 122242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW | CTLFLAG_TUN, 123194228Sthompsa &usb_template, 0, "Selected USB device side template"); 124184610Salfred 125197559Sthompsa/* English is default language */ 126197559Sthompsa 127197559Sthompsastatic int usb_lang_id = 0x0009; 128197559Sthompsastatic int usb_lang_mask = 0x00FF; 129197559Sthompsa 130197559SthompsaTUNABLE_INT("hw.usb.usb_lang_id", &usb_lang_id); 131242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RW | CTLFLAG_TUN, 132197559Sthompsa &usb_lang_id, 0, "Preferred USB language ID"); 133197559Sthompsa 134197559SthompsaTUNABLE_INT("hw.usb.usb_lang_mask", &usb_lang_mask); 135242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RW | CTLFLAG_TUN, 136197559Sthompsa &usb_lang_mask, 0, "Preferred USB language mask"); 137197559Sthompsa 138191498Sthompsastatic const char* statestr[USB_STATE_MAX] = { 139191498Sthompsa [USB_STATE_DETACHED] = "DETACHED", 140191498Sthompsa [USB_STATE_ATTACHED] = "ATTACHED", 141191498Sthompsa [USB_STATE_POWERED] = "POWERED", 142191498Sthompsa [USB_STATE_ADDRESSED] = "ADDRESSED", 143191498Sthompsa [USB_STATE_CONFIGURED] = "CONFIGURED", 144191498Sthompsa}; 145184610Salfred 146191498Sthompsaconst char * 147194228Sthompsausb_statestr(enum usb_dev_state state) 148191498Sthompsa{ 149191824Sthompsa return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN"); 150191498Sthompsa} 151191498Sthompsa 152212136Sthompsaconst char * 153212136Sthompsausb_get_manufacturer(struct usb_device *udev) 154212136Sthompsa{ 155212136Sthompsa return (udev->manufacturer ? udev->manufacturer : "Unknown"); 156212136Sthompsa} 157212136Sthompsa 158212136Sthompsaconst char * 159212136Sthompsausb_get_product(struct usb_device *udev) 160212136Sthompsa{ 161212136Sthompsa return (udev->product ? udev->product : ""); 162212136Sthompsa} 163212136Sthompsa 164212136Sthompsaconst char * 165212136Sthompsausb_get_serial(struct usb_device *udev) 166212136Sthompsa{ 167212136Sthompsa return (udev->serial ? udev->serial : ""); 168212136Sthompsa} 169212136Sthompsa 170184610Salfred/*------------------------------------------------------------------------* 171194228Sthompsa * usbd_get_ep_by_addr 172184610Salfred * 173193644Sthompsa * This function searches for an USB ep by endpoint address and 174184610Salfred * direction. 175184610Salfred * 176184610Salfred * Returns: 177184610Salfred * NULL: Failure 178184610Salfred * Else: Success 179184610Salfred *------------------------------------------------------------------------*/ 180193644Sthompsastruct usb_endpoint * 181194228Sthompsausbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val) 182184610Salfred{ 183193644Sthompsa struct usb_endpoint *ep = udev->endpoints; 184193644Sthompsa struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 185184610Salfred enum { 186184610Salfred EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 187184610Salfred }; 188184610Salfred 189184610Salfred /* 190184610Salfred * According to the USB specification not all bits are used 191184610Salfred * for the endpoint address. Keep defined bits only: 192184610Salfred */ 193184610Salfred ea_val &= EA_MASK; 194184610Salfred 195184610Salfred /* 196193644Sthompsa * Iterate accross all the USB endpoints searching for a match 197184610Salfred * based on the endpoint address: 198184610Salfred */ 199193644Sthompsa for (; ep != ep_end; ep++) { 200184610Salfred 201193644Sthompsa if (ep->edesc == NULL) { 202184610Salfred continue; 203184610Salfred } 204184610Salfred /* do the mask and check the value */ 205193644Sthompsa if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { 206184610Salfred goto found; 207184610Salfred } 208184610Salfred } 209184610Salfred 210184610Salfred /* 211193644Sthompsa * The default endpoint is always present and is checked separately: 212184610Salfred */ 213239177Shselasky if ((udev->ctrl_ep.edesc != NULL) && 214207080Sthompsa ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 215207080Sthompsa ep = &udev->ctrl_ep; 216184610Salfred goto found; 217184610Salfred } 218184610Salfred return (NULL); 219184610Salfred 220184610Salfredfound: 221193644Sthompsa return (ep); 222184610Salfred} 223184610Salfred 224184610Salfred/*------------------------------------------------------------------------* 225194228Sthompsa * usbd_get_endpoint 226184610Salfred * 227193644Sthompsa * This function searches for an USB endpoint based on the information 228192984Sthompsa * given by the passed "struct usb_config" pointer. 229184610Salfred * 230184610Salfred * Return values: 231184610Salfred * NULL: No match. 232193644Sthompsa * Else: Pointer to "struct usb_endpoint". 233184610Salfred *------------------------------------------------------------------------*/ 234193644Sthompsastruct usb_endpoint * 235194228Sthompsausbd_get_endpoint(struct usb_device *udev, uint8_t iface_index, 236192984Sthompsa const struct usb_config *setup) 237184610Salfred{ 238193644Sthompsa struct usb_endpoint *ep = udev->endpoints; 239193644Sthompsa struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 240184610Salfred uint8_t index = setup->ep_index; 241184610Salfred uint8_t ea_mask; 242184610Salfred uint8_t ea_val; 243184610Salfred uint8_t type_mask; 244184610Salfred uint8_t type_val; 245184610Salfred 246184610Salfred DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 247184610Salfred "type=0x%x dir=0x%x index=%d\n", 248184610Salfred udev, iface_index, setup->endpoint, 249184610Salfred setup->type, setup->direction, setup->ep_index); 250184610Salfred 251190734Sthompsa /* check USB mode */ 252190734Sthompsa 253192499Sthompsa if (setup->usb_mode != USB_MODE_DUAL && 254192499Sthompsa udev->flags.usb_mode != setup->usb_mode) { 255193644Sthompsa /* wrong mode - no endpoint */ 256190734Sthompsa return (NULL); 257190734Sthompsa } 258190734Sthompsa 259184610Salfred /* setup expected endpoint direction mask and value */ 260184610Salfred 261190732Sthompsa if (setup->direction == UE_DIR_RX) { 262190732Sthompsa ea_mask = (UE_DIR_IN | UE_DIR_OUT); 263192499Sthompsa ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 264190732Sthompsa UE_DIR_OUT : UE_DIR_IN; 265190732Sthompsa } else if (setup->direction == UE_DIR_TX) { 266190732Sthompsa ea_mask = (UE_DIR_IN | UE_DIR_OUT); 267192499Sthompsa ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 268190732Sthompsa UE_DIR_IN : UE_DIR_OUT; 269190732Sthompsa } else if (setup->direction == UE_DIR_ANY) { 270184610Salfred /* match any endpoint direction */ 271184610Salfred ea_mask = 0; 272184610Salfred ea_val = 0; 273184610Salfred } else { 274184610Salfred /* match the given endpoint direction */ 275184610Salfred ea_mask = (UE_DIR_IN | UE_DIR_OUT); 276184610Salfred ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 277184610Salfred } 278184610Salfred 279184610Salfred /* setup expected endpoint address */ 280184610Salfred 281184610Salfred if (setup->endpoint == UE_ADDR_ANY) { 282184610Salfred /* match any endpoint address */ 283184610Salfred } else { 284184610Salfred /* match the given endpoint address */ 285184610Salfred ea_mask |= UE_ADDR; 286184610Salfred ea_val |= (setup->endpoint & UE_ADDR); 287184610Salfred } 288184610Salfred 289184610Salfred /* setup expected endpoint type */ 290184610Salfred 291184610Salfred if (setup->type == UE_BULK_INTR) { 292184610Salfred /* this will match BULK and INTERRUPT endpoints */ 293184610Salfred type_mask = 2; 294184610Salfred type_val = 2; 295184610Salfred } else if (setup->type == UE_TYPE_ANY) { 296184610Salfred /* match any endpoint type */ 297184610Salfred type_mask = 0; 298184610Salfred type_val = 0; 299184610Salfred } else { 300184610Salfred /* match the given endpoint type */ 301184610Salfred type_mask = UE_XFERTYPE; 302184610Salfred type_val = (setup->type & UE_XFERTYPE); 303184610Salfred } 304184610Salfred 305184610Salfred /* 306193644Sthompsa * Iterate accross all the USB endpoints searching for a match 307184610Salfred * based on the endpoint address. Note that we are searching 308193644Sthompsa * the endpoints from the beginning of the "udev->endpoints" array. 309184610Salfred */ 310193644Sthompsa for (; ep != ep_end; ep++) { 311184610Salfred 312193644Sthompsa if ((ep->edesc == NULL) || 313193644Sthompsa (ep->iface_index != iface_index)) { 314184610Salfred continue; 315184610Salfred } 316184610Salfred /* do the masks and check the values */ 317184610Salfred 318193644Sthompsa if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && 319193644Sthompsa ((ep->edesc->bmAttributes & type_mask) == type_val)) { 320184610Salfred if (!index--) { 321184610Salfred goto found; 322184610Salfred } 323184610Salfred } 324184610Salfred } 325184610Salfred 326184610Salfred /* 327193644Sthompsa * Match against default endpoint last, so that "any endpoint", "any 328193644Sthompsa * address" and "any direction" returns the first endpoint of the 329184610Salfred * interface. "iface_index" and "direction" is ignored: 330184610Salfred */ 331239177Shselasky if ((udev->ctrl_ep.edesc != NULL) && 332207080Sthompsa ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && 333207080Sthompsa ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && 334184610Salfred (!index)) { 335207080Sthompsa ep = &udev->ctrl_ep; 336184610Salfred goto found; 337184610Salfred } 338184610Salfred return (NULL); 339184610Salfred 340184610Salfredfound: 341193644Sthompsa return (ep); 342184610Salfred} 343184610Salfred 344184610Salfred/*------------------------------------------------------------------------* 345194677Sthompsa * usbd_interface_count 346184610Salfred * 347184610Salfred * This function stores the number of USB interfaces excluding 348184610Salfred * alternate settings, which the USB config descriptor reports into 349184610Salfred * the unsigned 8-bit integer pointed to by "count". 350184610Salfred * 351184610Salfred * Returns: 352184610Salfred * 0: Success 353184610Salfred * Else: Failure 354184610Salfred *------------------------------------------------------------------------*/ 355193045Sthompsausb_error_t 356194677Sthompsausbd_interface_count(struct usb_device *udev, uint8_t *count) 357184610Salfred{ 358184610Salfred if (udev->cdesc == NULL) { 359184610Salfred *count = 0; 360184610Salfred return (USB_ERR_NOT_CONFIGURED); 361184610Salfred } 362190730Sthompsa *count = udev->ifaces_max; 363184610Salfred return (USB_ERR_NORMAL_COMPLETION); 364184610Salfred} 365184610Salfred 366184610Salfred/*------------------------------------------------------------------------* 367194228Sthompsa * usb_init_endpoint 368184610Salfred * 369193644Sthompsa * This function will initialise the USB endpoint structure pointed to by 370193644Sthompsa * the "endpoint" argument. The structure pointed to by "endpoint" must be 371190730Sthompsa * zeroed before calling this function. 372184610Salfred *------------------------------------------------------------------------*/ 373184610Salfredstatic void 374194228Sthompsausb_init_endpoint(struct usb_device *udev, uint8_t iface_index, 375213435Shselasky struct usb_endpoint_descriptor *edesc, 376213435Shselasky struct usb_endpoint_ss_comp_descriptor *ecomp, 377213435Shselasky struct usb_endpoint *ep) 378184610Salfred{ 379192984Sthompsa struct usb_bus_methods *methods; 380239214Shselasky usb_stream_t x; 381189110Sthompsa 382190730Sthompsa methods = udev->bus->methods; 383184610Salfred 384193644Sthompsa (methods->endpoint_init) (udev, edesc, ep); 385184610Salfred 386193644Sthompsa /* initialise USB endpoint structure */ 387193644Sthompsa ep->edesc = edesc; 388213435Shselasky ep->ecomp = ecomp; 389193644Sthompsa ep->iface_index = iface_index; 390184610Salfred 391239214Shselasky /* setup USB stream queues */ 392239214Shselasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 393239214Shselasky TAILQ_INIT(&ep->endpoint_q[x].head); 394239214Shselasky ep->endpoint_q[x].command = &usbd_pipe_start; 395239214Shselasky } 396239214Shselasky 397190735Sthompsa /* the pipe is not supported by the hardware */ 398193644Sthompsa if (ep->methods == NULL) 399190735Sthompsa return; 400190735Sthompsa 401239214Shselasky /* check for SUPER-speed streams mode endpoint */ 402239214Shselasky if (udev->speed == USB_SPEED_SUPER && ecomp != NULL && 403239214Shselasky (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK && 404239214Shselasky (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) { 405239214Shselasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS); 406239214Shselasky } else { 407239214Shselasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT); 408239214Shselasky } 409239214Shselasky 410184610Salfred /* clear stall, if any */ 411190730Sthompsa if (methods->clear_stall != NULL) { 412184824Sthompsa USB_BUS_LOCK(udev->bus); 413193644Sthompsa (methods->clear_stall) (udev, ep); 414184824Sthompsa USB_BUS_UNLOCK(udev->bus); 415184610Salfred } 416184610Salfred} 417184610Salfred 418190730Sthompsa/*-----------------------------------------------------------------------* 419194228Sthompsa * usb_endpoint_foreach 420188985Sthompsa * 421188985Sthompsa * This function will iterate all the USB endpoints except the control 422188985Sthompsa * endpoint. This function is NULL safe. 423188985Sthompsa * 424188985Sthompsa * Return values: 425193644Sthompsa * NULL: End of USB endpoints 426193644Sthompsa * Else: Pointer to next USB endpoint 427188985Sthompsa *------------------------------------------------------------------------*/ 428193644Sthompsastruct usb_endpoint * 429194228Sthompsausb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep) 430188985Sthompsa{ 431197558Sthompsa struct usb_endpoint *ep_end; 432188985Sthompsa 433188985Sthompsa /* be NULL safe */ 434188985Sthompsa if (udev == NULL) 435188985Sthompsa return (NULL); 436188985Sthompsa 437197558Sthompsa ep_end = udev->endpoints + udev->endpoints_max; 438197558Sthompsa 439193644Sthompsa /* get next endpoint */ 440193644Sthompsa if (ep == NULL) 441193644Sthompsa ep = udev->endpoints; 442188985Sthompsa else 443193644Sthompsa ep++; 444188985Sthompsa 445193644Sthompsa /* find next allocated ep */ 446193644Sthompsa while (ep != ep_end) { 447193644Sthompsa if (ep->edesc != NULL) 448193644Sthompsa return (ep); 449193644Sthompsa ep++; 450188985Sthompsa } 451188985Sthompsa return (NULL); 452188985Sthompsa} 453188985Sthompsa 454188985Sthompsa/*------------------------------------------------------------------------* 455278289Shselasky * usb_wait_pending_refs 456263799Shselasky * 457263799Shselasky * This function will wait for any USB references to go away before 458278289Shselasky * returning. This function is used before freeing a USB device. 459263799Shselasky *------------------------------------------------------------------------*/ 460263799Shselaskystatic void 461278289Shselaskyusb_wait_pending_refs(struct usb_device *udev) 462263799Shselasky{ 463263799Shselasky#if USB_HAVE_UGEN 464278289Shselasky DPRINTF("Refcount = %d\n", (int)udev->refcount); 465263799Shselasky 466278289Shselasky mtx_lock(&usb_ref_lock); 467278289Shselasky udev->refcount--; 468263799Shselasky while (1) { 469263799Shselasky /* wait for any pending references to go away */ 470278289Shselasky if (udev->refcount == 0) { 471278289Shselasky /* prevent further refs being taken, if any */ 472263799Shselasky udev->refcount = USB_DEV_REF_MAX; 473263799Shselasky break; 474263799Shselasky } 475263799Shselasky cv_wait(&udev->ref_cv, &usb_ref_lock); 476263799Shselasky } 477263799Shselasky mtx_unlock(&usb_ref_lock); 478263799Shselasky#endif 479263799Shselasky} 480263799Shselasky 481263799Shselasky/*------------------------------------------------------------------------* 482194228Sthompsa * usb_unconfigure 483184610Salfred * 484193644Sthompsa * This function will free all USB interfaces and USB endpoints belonging 485190730Sthompsa * to an USB device. 486190730Sthompsa * 487190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX". 488184610Salfred *------------------------------------------------------------------------*/ 489190730Sthompsastatic void 490194228Sthompsausb_unconfigure(struct usb_device *udev, uint8_t flag) 491184610Salfred{ 492190730Sthompsa uint8_t do_unlock; 493184610Salfred 494246616Shselasky /* Prevent re-enumeration */ 495246616Shselasky do_unlock = usbd_enum_lock(udev); 496184610Salfred 497190730Sthompsa /* detach all interface drivers */ 498194228Sthompsa usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag); 499184610Salfred 500190730Sthompsa#if USB_HAVE_UGEN 501190730Sthompsa /* free all FIFOs except control endpoint FIFOs */ 502194228Sthompsa usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag); 503184610Salfred 504184610Salfred /* 505190730Sthompsa * Free all cdev's, if any. 506184610Salfred */ 507194228Sthompsa usb_cdev_free(udev); 508190730Sthompsa#endif 509184610Salfred 510190180Sthompsa#if USB_HAVE_COMPAT_LINUX 511184610Salfred /* free Linux compat device, if any */ 512192984Sthompsa if (udev->linux_endpoint_start) { 513192984Sthompsa usb_linux_free_device(udev); 514192984Sthompsa udev->linux_endpoint_start = NULL; 515184610Salfred } 516190180Sthompsa#endif 517184610Salfred 518194228Sthompsa usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE); 519184610Salfred 520193644Sthompsa /* free "cdesc" after "ifaces" and "endpoints", if any */ 521190727Sthompsa if (udev->cdesc != NULL) { 522192499Sthompsa if (udev->flags.usb_mode != USB_MODE_DEVICE) 523250207Shselasky usbd_free_config_desc(udev, udev->cdesc); 524184610Salfred udev->cdesc = NULL; 525184610Salfred } 526184610Salfred /* set unconfigured state */ 527184610Salfred udev->curr_config_no = USB_UNCONFIG_NO; 528184610Salfred udev->curr_config_index = USB_UNCONFIG_INDEX; 529190730Sthompsa 530196498Salfred if (do_unlock) 531196498Salfred usbd_enum_unlock(udev); 532184610Salfred} 533184610Salfred 534184610Salfred/*------------------------------------------------------------------------* 535194228Sthompsa * usbd_set_config_index 536184610Salfred * 537184610Salfred * This function selects configuration by index, independent of the 538184610Salfred * actual configuration number. This function should not be used by 539184610Salfred * USB drivers. 540184610Salfred * 541184610Salfred * Returns: 542184610Salfred * 0: Success 543184610Salfred * Else: Failure 544184610Salfred *------------------------------------------------------------------------*/ 545193045Sthompsausb_error_t 546194228Sthompsausbd_set_config_index(struct usb_device *udev, uint8_t index) 547184610Salfred{ 548192984Sthompsa struct usb_status ds; 549192984Sthompsa struct usb_config_descriptor *cdp; 550184610Salfred uint16_t power; 551184610Salfred uint16_t max_power; 552184610Salfred uint8_t selfpowered; 553184610Salfred uint8_t do_unlock; 554193045Sthompsa usb_error_t err; 555184610Salfred 556184610Salfred DPRINTFN(6, "udev=%p index=%d\n", udev, index); 557184610Salfred 558246616Shselasky /* Prevent re-enumeration */ 559246616Shselasky do_unlock = usbd_enum_lock(udev); 560184610Salfred 561197553Sthompsa usb_unconfigure(udev, 0); 562184610Salfred 563184610Salfred if (index == USB_UNCONFIG_INDEX) { 564184610Salfred /* 565184610Salfred * Leave unallocated when unconfiguring the 566194228Sthompsa * device. "usb_unconfigure()" will also reset 567184610Salfred * the current config number and index. 568184610Salfred */ 569194228Sthompsa err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO); 570191494Sthompsa if (udev->state == USB_STATE_CONFIGURED) 571194228Sthompsa usb_set_device_state(udev, USB_STATE_ADDRESSED); 572184610Salfred goto done; 573184610Salfred } 574184610Salfred /* get the full config descriptor */ 575192499Sthompsa if (udev->flags.usb_mode == USB_MODE_DEVICE) { 576190727Sthompsa /* save some memory */ 577194228Sthompsa err = usbd_req_get_descriptor_ptr(udev, &cdp, 578191402Sthompsa (UDESC_CONFIG << 8) | index); 579190727Sthompsa } else { 580190727Sthompsa /* normal request */ 581194228Sthompsa err = usbd_req_get_config_desc_full(udev, 582250207Shselasky NULL, &cdp, index); 583190727Sthompsa } 584184610Salfred if (err) { 585184610Salfred goto done; 586184610Salfred } 587184610Salfred /* set the new config descriptor */ 588184610Salfred 589184610Salfred udev->cdesc = cdp; 590184610Salfred 591184610Salfred /* Figure out if the device is self or bus powered. */ 592184610Salfred selfpowered = 0; 593184610Salfred if ((!udev->flags.uq_bus_powered) && 594184610Salfred (cdp->bmAttributes & UC_SELF_POWERED) && 595192499Sthompsa (udev->flags.usb_mode == USB_MODE_HOST)) { 596184610Salfred /* May be self powered. */ 597184610Salfred if (cdp->bmAttributes & UC_BUS_POWERED) { 598184610Salfred /* Must ask device. */ 599194228Sthompsa err = usbd_req_get_device_status(udev, NULL, &ds); 600190743Sthompsa if (err) { 601190743Sthompsa DPRINTFN(0, "could not read " 602190743Sthompsa "device status: %s\n", 603194228Sthompsa usbd_errstr(err)); 604190743Sthompsa } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 605190743Sthompsa selfpowered = 1; 606184610Salfred } 607190743Sthompsa DPRINTF("status=0x%04x \n", 608190743Sthompsa UGETW(ds.wStatus)); 609184610Salfred } else 610184610Salfred selfpowered = 1; 611184610Salfred } 612184610Salfred DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 613184610Salfred "selfpowered=%d, power=%d\n", 614184610Salfred udev, cdp, 615190328Sthompsa udev->address, cdp->bConfigurationValue, cdp->bmAttributes, 616184610Salfred selfpowered, cdp->bMaxPower * 2); 617184610Salfred 618184610Salfred /* Check if we have enough power. */ 619184610Salfred power = cdp->bMaxPower * 2; 620184610Salfred 621184610Salfred if (udev->parent_hub) { 622184610Salfred max_power = udev->parent_hub->hub->portpower; 623184610Salfred } else { 624184610Salfred max_power = USB_MAX_POWER; 625184610Salfred } 626184610Salfred 627184610Salfred if (power > max_power) { 628184610Salfred DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 629184610Salfred err = USB_ERR_NO_POWER; 630184610Salfred goto done; 631184610Salfred } 632184610Salfred /* Only update "self_powered" in USB Host Mode */ 633192499Sthompsa if (udev->flags.usb_mode == USB_MODE_HOST) { 634184610Salfred udev->flags.self_powered = selfpowered; 635184610Salfred } 636184610Salfred udev->power = power; 637184610Salfred udev->curr_config_no = cdp->bConfigurationValue; 638184610Salfred udev->curr_config_index = index; 639194228Sthompsa usb_set_device_state(udev, USB_STATE_CONFIGURED); 640184610Salfred 641184610Salfred /* Set the actual configuration value. */ 642194228Sthompsa err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); 643184610Salfred if (err) { 644184610Salfred goto done; 645184610Salfred } 646190730Sthompsa 647194228Sthompsa err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC); 648190730Sthompsa if (err) { 649190730Sthompsa goto done; 650184610Salfred } 651190730Sthompsa 652194228Sthompsa err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT); 653190730Sthompsa if (err) { 654190730Sthompsa goto done; 655190730Sthompsa } 656190730Sthompsa 657189599Sthompsa#if USB_HAVE_UGEN 658189110Sthompsa /* create device nodes for each endpoint */ 659194228Sthompsa usb_cdev_create(udev); 660189599Sthompsa#endif 661184610Salfred 662184610Salfreddone: 663194228Sthompsa DPRINTF("error=%s\n", usbd_errstr(err)); 664184610Salfred if (err) { 665197553Sthompsa usb_unconfigure(udev, 0); 666184610Salfred } 667196498Salfred if (do_unlock) 668196498Salfred usbd_enum_unlock(udev); 669184610Salfred return (err); 670184610Salfred} 671184610Salfred 672184610Salfred/*------------------------------------------------------------------------* 673194228Sthompsa * usb_config_parse 674190730Sthompsa * 675193644Sthompsa * This function will allocate and free USB interfaces and USB endpoints, 676193644Sthompsa * parse the USB configuration structure and initialise the USB endpoints 677190730Sthompsa * and interfaces. If "iface_index" is not equal to 678190730Sthompsa * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the 679190730Sthompsa * alternate_setting to be selected for the given interface. Else the 680190730Sthompsa * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be 681190730Sthompsa * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function 682190730Sthompsa * is typically called when setting the configuration or when setting 683190730Sthompsa * an alternate interface. 684190730Sthompsa * 685190730Sthompsa * Returns: 686190730Sthompsa * 0: Success 687190730Sthompsa * Else: Failure 688190730Sthompsa *------------------------------------------------------------------------*/ 689213435Shselaskystatic usb_error_t 690194228Sthompsausb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd) 691190730Sthompsa{ 692192984Sthompsa struct usb_idesc_parse_state ips; 693192984Sthompsa struct usb_interface_descriptor *id; 694192984Sthompsa struct usb_endpoint_descriptor *ed; 695192984Sthompsa struct usb_interface *iface; 696193644Sthompsa struct usb_endpoint *ep; 697193045Sthompsa usb_error_t err; 698190730Sthompsa uint8_t ep_curr; 699190730Sthompsa uint8_t ep_max; 700190730Sthompsa uint8_t temp; 701190730Sthompsa uint8_t do_init; 702190730Sthompsa uint8_t alt_index; 703190730Sthompsa 704190730Sthompsa if (iface_index != USB_IFACE_INDEX_ANY) { 705190730Sthompsa /* parameter overload */ 706190730Sthompsa alt_index = cmd; 707190730Sthompsa cmd = USB_CFG_INIT; 708190730Sthompsa } else { 709190730Sthompsa /* not used */ 710190730Sthompsa alt_index = 0; 711190730Sthompsa } 712190730Sthompsa 713190730Sthompsa err = 0; 714190730Sthompsa 715190730Sthompsa DPRINTFN(5, "iface_index=%d cmd=%d\n", 716190730Sthompsa iface_index, cmd); 717190730Sthompsa 718190730Sthompsa if (cmd == USB_CFG_FREE) 719190730Sthompsa goto cleanup; 720190730Sthompsa 721190730Sthompsa if (cmd == USB_CFG_INIT) { 722207079Sthompsa sx_assert(&udev->enum_sx, SA_LOCKED); 723190730Sthompsa 724193644Sthompsa /* check for in-use endpoints */ 725190730Sthompsa 726193644Sthompsa ep = udev->endpoints; 727193644Sthompsa ep_max = udev->endpoints_max; 728190730Sthompsa while (ep_max--) { 729193644Sthompsa /* look for matching endpoints */ 730190730Sthompsa if ((iface_index == USB_IFACE_INDEX_ANY) || 731193644Sthompsa (iface_index == ep->iface_index)) { 732199672Sthompsa if (ep->refcount_alloc != 0) { 733190730Sthompsa /* 734190730Sthompsa * This typically indicates a 735190730Sthompsa * more serious error. 736190730Sthompsa */ 737190730Sthompsa err = USB_ERR_IN_USE; 738190730Sthompsa } else { 739193644Sthompsa /* reset endpoint */ 740193644Sthompsa memset(ep, 0, sizeof(*ep)); 741193644Sthompsa /* make sure we don't zero the endpoint again */ 742193644Sthompsa ep->iface_index = USB_IFACE_INDEX_ANY; 743190730Sthompsa } 744190730Sthompsa } 745193644Sthompsa ep++; 746190730Sthompsa } 747190730Sthompsa 748190730Sthompsa if (err) 749190730Sthompsa return (err); 750190730Sthompsa } 751190730Sthompsa 752190730Sthompsa memset(&ips, 0, sizeof(ips)); 753190730Sthompsa 754190730Sthompsa ep_curr = 0; 755190730Sthompsa ep_max = 0; 756190730Sthompsa 757194228Sthompsa while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { 758190730Sthompsa 759190730Sthompsa iface = udev->ifaces + ips.iface_index; 760190730Sthompsa 761190730Sthompsa /* check for specific interface match */ 762190730Sthompsa 763190730Sthompsa if (cmd == USB_CFG_INIT) { 764190730Sthompsa if ((iface_index != USB_IFACE_INDEX_ANY) && 765190730Sthompsa (iface_index != ips.iface_index)) { 766190730Sthompsa /* wrong interface */ 767190730Sthompsa do_init = 0; 768190730Sthompsa } else if (alt_index != ips.iface_index_alt) { 769190730Sthompsa /* wrong alternate setting */ 770190730Sthompsa do_init = 0; 771190730Sthompsa } else { 772190730Sthompsa /* initialise interface */ 773190730Sthompsa do_init = 1; 774190730Sthompsa } 775190730Sthompsa } else 776190730Sthompsa do_init = 0; 777190730Sthompsa 778190730Sthompsa /* check for new interface */ 779190730Sthompsa if (ips.iface_index_alt == 0) { 780190730Sthompsa /* update current number of endpoints */ 781190730Sthompsa ep_curr = ep_max; 782190730Sthompsa } 783190730Sthompsa /* check for init */ 784190730Sthompsa if (do_init) { 785190730Sthompsa /* setup the USB interface structure */ 786190730Sthompsa iface->idesc = id; 787190730Sthompsa /* set alternate index */ 788190730Sthompsa iface->alt_index = alt_index; 789236407Shselasky /* set default interface parent */ 790236407Shselasky if (iface_index == USB_IFACE_INDEX_ANY) { 791236407Shselasky iface->parent_iface_index = 792236407Shselasky USB_IFACE_INDEX_ANY; 793236407Shselasky } 794190730Sthompsa } 795190730Sthompsa 796190730Sthompsa DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); 797190730Sthompsa 798192984Sthompsa ed = (struct usb_endpoint_descriptor *)id; 799190730Sthompsa 800190730Sthompsa temp = ep_curr; 801190730Sthompsa 802190730Sthompsa /* iterate all the endpoint descriptors */ 803194228Sthompsa while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { 804190730Sthompsa 805250204Shselasky /* check if endpoint limit has been reached */ 806250204Shselasky if (temp >= USB_MAX_EP_UNITS) { 807250204Shselasky DPRINTF("Endpoint limit reached\n"); 808250204Shselasky break; 809250204Shselasky } 810190730Sthompsa 811193644Sthompsa ep = udev->endpoints + temp; 812190730Sthompsa 813190730Sthompsa if (do_init) { 814213435Shselasky void *ecomp; 815213435Shselasky 816213435Shselasky ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed); 817213435Shselasky if (ecomp != NULL) 818213435Shselasky DPRINTFN(5, "Found endpoint companion descriptor\n"); 819213435Shselasky 820194228Sthompsa usb_init_endpoint(udev, 821213435Shselasky ips.iface_index, ed, ecomp, ep); 822190730Sthompsa } 823190730Sthompsa 824190730Sthompsa temp ++; 825190730Sthompsa 826190730Sthompsa /* find maximum number of endpoints */ 827190730Sthompsa if (ep_max < temp) 828190730Sthompsa ep_max = temp; 829190730Sthompsa } 830190730Sthompsa } 831190730Sthompsa 832190730Sthompsa /* NOTE: It is valid to have no interfaces and no endpoints! */ 833190730Sthompsa 834190730Sthompsa if (cmd == USB_CFG_ALLOC) { 835190730Sthompsa udev->ifaces_max = ips.iface_index; 836250204Shselasky#if (USB_HAVE_FIXED_IFACE == 0) 837190730Sthompsa udev->ifaces = NULL; 838190730Sthompsa if (udev->ifaces_max != 0) { 839190730Sthompsa udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max, 840190730Sthompsa M_USB, M_WAITOK | M_ZERO); 841190730Sthompsa if (udev->ifaces == NULL) { 842190730Sthompsa err = USB_ERR_NOMEM; 843190730Sthompsa goto done; 844190730Sthompsa } 845190730Sthompsa } 846250204Shselasky#endif 847250204Shselasky#if (USB_HAVE_FIXED_ENDPOINT == 0) 848191398Sthompsa if (ep_max != 0) { 849193644Sthompsa udev->endpoints = malloc(sizeof(*ep) * ep_max, 850190730Sthompsa M_USB, M_WAITOK | M_ZERO); 851193644Sthompsa if (udev->endpoints == NULL) { 852190730Sthompsa err = USB_ERR_NOMEM; 853190730Sthompsa goto done; 854190730Sthompsa } 855191398Sthompsa } else { 856193644Sthompsa udev->endpoints = NULL; 857190730Sthompsa } 858250204Shselasky#endif 859191398Sthompsa USB_BUS_LOCK(udev->bus); 860193644Sthompsa udev->endpoints_max = ep_max; 861191398Sthompsa /* reset any ongoing clear-stall */ 862193644Sthompsa udev->ep_curr = NULL; 863191398Sthompsa USB_BUS_UNLOCK(udev->bus); 864190730Sthompsa } 865250212Shselasky#if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0) 866190730Sthompsadone: 867250212Shselasky#endif 868190730Sthompsa if (err) { 869190730Sthompsa if (cmd == USB_CFG_ALLOC) { 870190730Sthompsacleanup: 871191398Sthompsa USB_BUS_LOCK(udev->bus); 872193644Sthompsa udev->endpoints_max = 0; 873191398Sthompsa /* reset any ongoing clear-stall */ 874193644Sthompsa udev->ep_curr = NULL; 875191398Sthompsa USB_BUS_UNLOCK(udev->bus); 876191398Sthompsa 877250204Shselasky#if (USB_HAVE_FIXED_IFACE == 0) 878250204Shselasky free(udev->ifaces, M_USB); 879190730Sthompsa udev->ifaces = NULL; 880250204Shselasky#endif 881250204Shselasky#if (USB_HAVE_FIXED_ENDPOINT == 0) 882250204Shselasky free(udev->endpoints, M_USB); 883193644Sthompsa udev->endpoints = NULL; 884250204Shselasky#endif 885190730Sthompsa udev->ifaces_max = 0; 886190730Sthompsa } 887190730Sthompsa } 888190730Sthompsa return (err); 889190730Sthompsa} 890190730Sthompsa 891190730Sthompsa/*------------------------------------------------------------------------* 892194228Sthompsa * usbd_set_alt_interface_index 893184610Salfred * 894184610Salfred * This function will select an alternate interface index for the 895184610Salfred * given interface index. The interface should not be in use when this 896188986Sthompsa * function is called. That means there should not be any open USB 897188986Sthompsa * transfers. Else an error is returned. If the alternate setting is 898188986Sthompsa * already set this function will simply return success. This function 899188986Sthompsa * is called in Host mode and Device mode! 900184610Salfred * 901184610Salfred * Returns: 902184610Salfred * 0: Success 903184610Salfred * Else: Failure 904184610Salfred *------------------------------------------------------------------------*/ 905193045Sthompsausb_error_t 906194228Sthompsausbd_set_alt_interface_index(struct usb_device *udev, 907184610Salfred uint8_t iface_index, uint8_t alt_index) 908184610Salfred{ 909194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 910193045Sthompsa usb_error_t err; 911184610Salfred uint8_t do_unlock; 912184610Salfred 913246616Shselasky /* Prevent re-enumeration */ 914246616Shselasky do_unlock = usbd_enum_lock(udev); 915246616Shselasky 916184610Salfred if (iface == NULL) { 917184610Salfred err = USB_ERR_INVAL; 918184610Salfred goto done; 919184610Salfred } 920195963Salfred if (iface->alt_index == alt_index) { 921195963Salfred /* 922195963Salfred * Optimise away duplicate setting of 923195963Salfred * alternate setting in USB Host Mode! 924195963Salfred */ 925195963Salfred err = 0; 926195963Salfred goto done; 927184610Salfred } 928189599Sthompsa#if USB_HAVE_UGEN 929185087Salfred /* 930185087Salfred * Free all generic FIFOs for this interface, except control 931185087Salfred * endpoint FIFOs: 932185087Salfred */ 933194228Sthompsa usb_fifo_free_wrap(udev, iface_index, 0); 934189599Sthompsa#endif 935190730Sthompsa 936194228Sthompsa err = usb_config_parse(udev, iface_index, alt_index); 937184610Salfred if (err) { 938184610Salfred goto done; 939184610Salfred } 940195963Salfred if (iface->alt_index != alt_index) { 941195963Salfred /* the alternate setting does not exist */ 942195963Salfred err = USB_ERR_INVAL; 943195963Salfred goto done; 944195963Salfred } 945195963Salfred 946194228Sthompsa err = usbd_req_set_alt_interface_no(udev, NULL, iface_index, 947184610Salfred iface->idesc->bAlternateSetting); 948184610Salfred 949184610Salfreddone: 950196498Salfred if (do_unlock) 951196498Salfred usbd_enum_unlock(udev); 952184610Salfred return (err); 953184610Salfred} 954184610Salfred 955184610Salfred/*------------------------------------------------------------------------* 956194228Sthompsa * usbd_set_endpoint_stall 957184610Salfred * 958213435Shselasky * This function is used to make a BULK or INTERRUPT endpoint send 959213435Shselasky * STALL tokens in USB device mode. 960184610Salfred * 961184610Salfred * Returns: 962184610Salfred * 0: Success 963184610Salfred * Else: Failure 964184610Salfred *------------------------------------------------------------------------*/ 965193045Sthompsausb_error_t 966194228Sthompsausbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep, 967184610Salfred uint8_t do_stall) 968184610Salfred{ 969192984Sthompsa struct usb_xfer *xfer; 970239214Shselasky usb_stream_t x; 971184610Salfred uint8_t et; 972184610Salfred uint8_t was_stalled; 973184610Salfred 974193644Sthompsa if (ep == NULL) { 975184610Salfred /* nothing to do */ 976184610Salfred DPRINTF("Cannot find endpoint\n"); 977184610Salfred /* 978184610Salfred * Pretend that the clear or set stall request is 979184610Salfred * successful else some USB host stacks can do 980184610Salfred * strange things, especially when a control endpoint 981184610Salfred * stalls. 982184610Salfred */ 983184610Salfred return (0); 984184610Salfred } 985193644Sthompsa et = (ep->edesc->bmAttributes & UE_XFERTYPE); 986184610Salfred 987184610Salfred if ((et != UE_BULK) && 988184610Salfred (et != UE_INTERRUPT)) { 989184610Salfred /* 990184610Salfred * Should not stall control 991184610Salfred * nor isochronous endpoints. 992184610Salfred */ 993184610Salfred DPRINTF("Invalid endpoint\n"); 994184610Salfred return (0); 995184610Salfred } 996184824Sthompsa USB_BUS_LOCK(udev->bus); 997184610Salfred 998184610Salfred /* store current stall state */ 999193644Sthompsa was_stalled = ep->is_stalled; 1000184610Salfred 1001184610Salfred /* check for no change */ 1002184610Salfred if (was_stalled && do_stall) { 1003193644Sthompsa /* if the endpoint is already stalled do nothing */ 1004184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1005184610Salfred DPRINTF("No change\n"); 1006184610Salfred return (0); 1007184610Salfred } 1008184610Salfred /* set stalled state */ 1009193644Sthompsa ep->is_stalled = 1; 1010184610Salfred 1011184610Salfred if (do_stall || (!was_stalled)) { 1012184610Salfred if (!was_stalled) { 1013239214Shselasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 1014239214Shselasky /* lookup the current USB transfer, if any */ 1015239214Shselasky xfer = ep->endpoint_q[x].curr; 1016239214Shselasky if (xfer != NULL) { 1017239214Shselasky /* 1018239214Shselasky * The "xfer_stall" method 1019239214Shselasky * will complete the USB 1020239214Shselasky * transfer like in case of a 1021239214Shselasky * timeout setting the error 1022239214Shselasky * code "USB_ERR_STALLED". 1023239214Shselasky */ 1024239214Shselasky (udev->bus->methods->xfer_stall) (xfer); 1025239214Shselasky } 1026239214Shselasky } 1027184610Salfred } 1028239214Shselasky (udev->bus->methods->set_stall) (udev, ep, &do_stall); 1029184610Salfred } 1030184610Salfred if (!do_stall) { 1031193644Sthompsa ep->toggle_next = 0; /* reset data toggle */ 1032193644Sthompsa ep->is_stalled = 0; /* clear stalled state */ 1033184610Salfred 1034193644Sthompsa (udev->bus->methods->clear_stall) (udev, ep); 1035184610Salfred 1036239214Shselasky /* start the current or next transfer, if any */ 1037239214Shselasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 1038239214Shselasky usb_command_wrapper(&ep->endpoint_q[x], 1039239214Shselasky ep->endpoint_q[x].curr); 1040239214Shselasky } 1041184610Salfred } 1042184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1043184610Salfred return (0); 1044184610Salfred} 1045184610Salfred 1046184610Salfred/*------------------------------------------------------------------------* 1047194228Sthompsa * usb_reset_iface_endpoints - used in USB device side mode 1048184610Salfred *------------------------------------------------------------------------*/ 1049193045Sthompsausb_error_t 1050194228Sthompsausb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index) 1051184610Salfred{ 1052193644Sthompsa struct usb_endpoint *ep; 1053193644Sthompsa struct usb_endpoint *ep_end; 1054184610Salfred 1055193644Sthompsa ep = udev->endpoints; 1056193644Sthompsa ep_end = udev->endpoints + udev->endpoints_max; 1057184610Salfred 1058193644Sthompsa for (; ep != ep_end; ep++) { 1059184610Salfred 1060193644Sthompsa if ((ep->edesc == NULL) || 1061193644Sthompsa (ep->iface_index != iface_index)) { 1062184610Salfred continue; 1063184610Salfred } 1064184610Salfred /* simulate a clear stall from the peer */ 1065195963Salfred usbd_set_endpoint_stall(udev, ep, 0); 1066184610Salfred } 1067184610Salfred return (0); 1068184610Salfred} 1069184610Salfred 1070184610Salfred/*------------------------------------------------------------------------* 1071194228Sthompsa * usb_detach_device_sub 1072184610Salfred * 1073184610Salfred * This function will try to detach an USB device. If it fails a panic 1074184610Salfred * will result. 1075190730Sthompsa * 1076190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX". 1077184610Salfred *------------------------------------------------------------------------*/ 1078184610Salfredstatic void 1079194228Sthompsausb_detach_device_sub(struct usb_device *udev, device_t *ppdev, 1080214429Shselasky char **ppnpinfo, uint8_t flag) 1081184610Salfred{ 1082184610Salfred device_t dev; 1083214429Shselasky char *pnpinfo; 1084184610Salfred int err; 1085184610Salfred 1086197553Sthompsa dev = *ppdev; 1087197553Sthompsa if (dev) { 1088184610Salfred /* 1089184610Salfred * NOTE: It is important to clear "*ppdev" before deleting 1090184610Salfred * the child due to some device methods being called late 1091184610Salfred * during the delete process ! 1092184610Salfred */ 1093184610Salfred *ppdev = NULL; 1094184610Salfred 1095267775Smarius if (!rebooting) { 1096267775Smarius device_printf(dev, "at %s, port %d, addr %d " 1097267775Smarius "(disconnected)\n", 1098267775Smarius device_get_nameunit(udev->parent_dev), 1099267775Smarius udev->port_no, udev->address); 1100267775Smarius } 1101184610Salfred 1102184610Salfred if (device_is_attached(dev)) { 1103191824Sthompsa if (udev->flags.peer_suspended) { 1104184610Salfred err = DEVICE_RESUME(dev); 1105184610Salfred if (err) { 1106199816Sthompsa device_printf(dev, "Resume failed\n"); 1107184610Salfred } 1108184610Salfred } 1109184610Salfred } 1110308402Shselasky /* detach and delete child */ 1111184610Salfred if (device_delete_child(udev->parent_dev, dev)) { 1112184610Salfred goto error; 1113184610Salfred } 1114184610Salfred } 1115214429Shselasky 1116214429Shselasky pnpinfo = *ppnpinfo; 1117214429Shselasky if (pnpinfo != NULL) { 1118214429Shselasky *ppnpinfo = NULL; 1119214429Shselasky free(pnpinfo, M_USBDEV); 1120214429Shselasky } 1121184610Salfred return; 1122184610Salfred 1123184610Salfrederror: 1124184610Salfred /* Detach is not allowed to fail in the USB world */ 1125214429Shselasky panic("usb_detach_device_sub: A USB driver would not detach\n"); 1126184610Salfred} 1127184610Salfred 1128184610Salfred/*------------------------------------------------------------------------* 1129194228Sthompsa * usb_detach_device 1130184610Salfred * 1131184610Salfred * The following function will detach the matching interfaces. 1132184610Salfred * This function is NULL safe. 1133190730Sthompsa * 1134190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX". 1135184610Salfred *------------------------------------------------------------------------*/ 1136184610Salfredvoid 1137194228Sthompsausb_detach_device(struct usb_device *udev, uint8_t iface_index, 1138190730Sthompsa uint8_t flag) 1139184610Salfred{ 1140192984Sthompsa struct usb_interface *iface; 1141184610Salfred uint8_t i; 1142184610Salfred 1143184610Salfred if (udev == NULL) { 1144184610Salfred /* nothing to do */ 1145184610Salfred return; 1146184610Salfred } 1147184610Salfred DPRINTFN(4, "udev=%p\n", udev); 1148184610Salfred 1149207079Sthompsa sx_assert(&udev->enum_sx, SA_LOCKED); 1150184610Salfred 1151184610Salfred /* 1152184610Salfred * First detach the child to give the child's detach routine a 1153184610Salfred * chance to detach the sub-devices in the correct order. 1154184610Salfred * Then delete the child using "device_delete_child()" which 1155184610Salfred * will detach all sub-devices from the bottom and upwards! 1156184610Salfred */ 1157184610Salfred if (iface_index != USB_IFACE_INDEX_ANY) { 1158184610Salfred i = iface_index; 1159184610Salfred iface_index = i + 1; 1160184610Salfred } else { 1161184610Salfred i = 0; 1162184610Salfred iface_index = USB_IFACE_MAX; 1163184610Salfred } 1164184610Salfred 1165184610Salfred /* do the detach */ 1166184610Salfred 1167184610Salfred for (; i != iface_index; i++) { 1168184610Salfred 1169194228Sthompsa iface = usbd_get_iface(udev, i); 1170184610Salfred if (iface == NULL) { 1171184610Salfred /* looks like the end of the USB interfaces */ 1172184610Salfred break; 1173184610Salfred } 1174214429Shselasky usb_detach_device_sub(udev, &iface->subdev, 1175214429Shselasky &iface->pnpinfo, flag); 1176184610Salfred } 1177184610Salfred} 1178184610Salfred 1179184610Salfred/*------------------------------------------------------------------------* 1180194228Sthompsa * usb_probe_and_attach_sub 1181184610Salfred * 1182184610Salfred * Returns: 1183184610Salfred * 0: Success 1184184610Salfred * Else: Failure 1185184610Salfred *------------------------------------------------------------------------*/ 1186184610Salfredstatic uint8_t 1187194228Sthompsausb_probe_and_attach_sub(struct usb_device *udev, 1188192984Sthompsa struct usb_attach_arg *uaa) 1189184610Salfred{ 1190192984Sthompsa struct usb_interface *iface; 1191184610Salfred device_t dev; 1192184610Salfred int err; 1193184610Salfred 1194184610Salfred iface = uaa->iface; 1195184610Salfred if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 1196184610Salfred /* leave interface alone */ 1197184610Salfred return (0); 1198184610Salfred } 1199184610Salfred dev = iface->subdev; 1200184610Salfred if (dev) { 1201184610Salfred 1202184610Salfred /* clean up after module unload */ 1203184610Salfred 1204184610Salfred if (device_is_attached(dev)) { 1205184610Salfred /* already a device there */ 1206184610Salfred return (0); 1207184610Salfred } 1208184610Salfred /* clear "iface->subdev" as early as possible */ 1209184610Salfred 1210184610Salfred iface->subdev = NULL; 1211184610Salfred 1212184610Salfred if (device_delete_child(udev->parent_dev, dev)) { 1213184610Salfred 1214184610Salfred /* 1215184610Salfred * Panic here, else one can get a double call 1216184610Salfred * to device_detach(). USB devices should 1217184610Salfred * never fail on detach! 1218184610Salfred */ 1219199816Sthompsa panic("device_delete_child() failed\n"); 1220184610Salfred } 1221184610Salfred } 1222184610Salfred if (uaa->temp_dev == NULL) { 1223184610Salfred 1224184610Salfred /* create a new child */ 1225184610Salfred uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 1226184610Salfred if (uaa->temp_dev == NULL) { 1227184610Salfred device_printf(udev->parent_dev, 1228199816Sthompsa "Device creation failed\n"); 1229184610Salfred return (1); /* failure */ 1230184610Salfred } 1231184610Salfred device_set_ivars(uaa->temp_dev, uaa); 1232184610Salfred device_quiet(uaa->temp_dev); 1233184610Salfred } 1234184610Salfred /* 1235184610Salfred * Set "subdev" before probe and attach so that "devd" gets 1236184610Salfred * the information it needs. 1237184610Salfred */ 1238184610Salfred iface->subdev = uaa->temp_dev; 1239184610Salfred 1240184610Salfred if (device_probe_and_attach(iface->subdev) == 0) { 1241184610Salfred /* 1242184610Salfred * The USB attach arguments are only available during probe 1243184610Salfred * and attach ! 1244184610Salfred */ 1245184610Salfred uaa->temp_dev = NULL; 1246184610Salfred device_set_ivars(iface->subdev, NULL); 1247184610Salfred 1248191824Sthompsa if (udev->flags.peer_suspended) { 1249184610Salfred err = DEVICE_SUSPEND(iface->subdev); 1250191397Sthompsa if (err) 1251191397Sthompsa device_printf(iface->subdev, "Suspend failed\n"); 1252184610Salfred } 1253184610Salfred return (0); /* success */ 1254184610Salfred } else { 1255184610Salfred /* No USB driver found */ 1256184610Salfred iface->subdev = NULL; 1257184610Salfred } 1258184610Salfred return (1); /* failure */ 1259184610Salfred} 1260184610Salfred 1261184610Salfred/*------------------------------------------------------------------------* 1262194228Sthompsa * usbd_set_parent_iface 1263184610Salfred * 1264184610Salfred * Using this function will lock the alternate interface setting on an 1265184610Salfred * interface. It is typically used for multi interface drivers. In USB 1266184610Salfred * device side mode it is assumed that the alternate interfaces all 1267184610Salfred * have the same endpoint descriptors. The default parent index value 1268184610Salfred * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 1269184610Salfred * locked. 1270184610Salfred *------------------------------------------------------------------------*/ 1271184610Salfredvoid 1272194228Sthompsausbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index, 1273184610Salfred uint8_t parent_index) 1274184610Salfred{ 1275192984Sthompsa struct usb_interface *iface; 1276184610Salfred 1277361908Shselasky if (udev == NULL || iface_index == parent_index) { 1278236407Shselasky /* nothing to do */ 1279236407Shselasky return; 1280236407Shselasky } 1281194228Sthompsa iface = usbd_get_iface(udev, iface_index); 1282236407Shselasky if (iface != NULL) 1283184610Salfred iface->parent_iface_index = parent_index; 1284184610Salfred} 1285184610Salfred 1286184610Salfredstatic void 1287194228Sthompsausb_init_attach_arg(struct usb_device *udev, 1288192984Sthompsa struct usb_attach_arg *uaa) 1289184610Salfred{ 1290225350Shselasky memset(uaa, 0, sizeof(*uaa)); 1291225350Shselasky 1292184610Salfred uaa->device = udev; 1293192499Sthompsa uaa->usb_mode = udev->flags.usb_mode; 1294184610Salfred uaa->port = udev->port_no; 1295200653Sthompsa uaa->dev_state = UAA_DEV_READY; 1296184610Salfred 1297184610Salfred uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 1298184610Salfred uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 1299184610Salfred uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 1300184610Salfred uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 1301184610Salfred uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 1302184610Salfred uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 1303184610Salfred uaa->info.bConfigIndex = udev->curr_config_index; 1304184610Salfred uaa->info.bConfigNum = udev->curr_config_no; 1305184610Salfred} 1306184610Salfred 1307184610Salfred/*------------------------------------------------------------------------* 1308194228Sthompsa * usb_probe_and_attach 1309184610Salfred * 1310184610Salfred * This function is called from "uhub_explore_sub()", 1311194228Sthompsa * "usb_handle_set_config()" and "usb_handle_request()". 1312184610Salfred * 1313184610Salfred * Returns: 1314184610Salfred * 0: Success 1315184610Salfred * Else: A control transfer failed 1316184610Salfred *------------------------------------------------------------------------*/ 1317193045Sthompsausb_error_t 1318194228Sthompsausb_probe_and_attach(struct usb_device *udev, uint8_t iface_index) 1319184610Salfred{ 1320192984Sthompsa struct usb_attach_arg uaa; 1321192984Sthompsa struct usb_interface *iface; 1322184610Salfred uint8_t i; 1323184610Salfred uint8_t j; 1324184610Salfred uint8_t do_unlock; 1325184610Salfred 1326184610Salfred if (udev == NULL) { 1327184610Salfred DPRINTF("udev == NULL\n"); 1328184610Salfred return (USB_ERR_INVAL); 1329184610Salfred } 1330246616Shselasky /* Prevent re-enumeration */ 1331246616Shselasky do_unlock = usbd_enum_lock(udev); 1332184610Salfred 1333184610Salfred if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 1334184610Salfred /* do nothing - no configuration has been set */ 1335184610Salfred goto done; 1336184610Salfred } 1337184610Salfred /* setup USB attach arguments */ 1338184610Salfred 1339194228Sthompsa usb_init_attach_arg(udev, &uaa); 1340184610Salfred 1341223512Shselasky /* 1342223512Shselasky * If the whole USB device is targeted, invoke the USB event 1343223512Shselasky * handler(s): 1344223512Shselasky */ 1345223512Shselasky if (iface_index == USB_IFACE_INDEX_ANY) { 1346223512Shselasky 1347283173Shselasky if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 && 1348283173Shselasky usb_dymo_eject(udev, 0) == 0) { 1349283173Shselasky /* success, mark the udev as disappearing */ 1350283173Shselasky uaa.dev_state = UAA_DEV_EJECTING; 1351283173Shselasky } 1352283173Shselasky 1353223512Shselasky EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa); 1354223512Shselasky 1355223512Shselasky if (uaa.dev_state != UAA_DEV_READY) { 1356223512Shselasky /* leave device unconfigured */ 1357223512Shselasky usb_unconfigure(udev, 0); 1358223512Shselasky goto done; 1359223512Shselasky } 1360223512Shselasky } 1361223512Shselasky 1362184610Salfred /* Check if only one interface should be probed: */ 1363184610Salfred if (iface_index != USB_IFACE_INDEX_ANY) { 1364184610Salfred i = iface_index; 1365184610Salfred j = i + 1; 1366184610Salfred } else { 1367184610Salfred i = 0; 1368184610Salfred j = USB_IFACE_MAX; 1369184610Salfred } 1370184610Salfred 1371184610Salfred /* Do the probe and attach */ 1372184610Salfred for (; i != j; i++) { 1373184610Salfred 1374194228Sthompsa iface = usbd_get_iface(udev, i); 1375184610Salfred if (iface == NULL) { 1376184610Salfred /* 1377184610Salfred * Looks like the end of the USB 1378184610Salfred * interfaces ! 1379184610Salfred */ 1380184610Salfred DPRINTFN(2, "end of interfaces " 1381184610Salfred "at %u\n", i); 1382184610Salfred break; 1383184610Salfred } 1384184610Salfred if (iface->idesc == NULL) { 1385184610Salfred /* no interface descriptor */ 1386184610Salfred continue; 1387184610Salfred } 1388184610Salfred uaa.iface = iface; 1389184610Salfred 1390184610Salfred uaa.info.bInterfaceClass = 1391184610Salfred iface->idesc->bInterfaceClass; 1392184610Salfred uaa.info.bInterfaceSubClass = 1393184610Salfred iface->idesc->bInterfaceSubClass; 1394184610Salfred uaa.info.bInterfaceProtocol = 1395184610Salfred iface->idesc->bInterfaceProtocol; 1396184610Salfred uaa.info.bIfaceIndex = i; 1397184610Salfred uaa.info.bIfaceNum = 1398184610Salfred iface->idesc->bInterfaceNumber; 1399195963Salfred uaa.driver_info = 0; /* reset driver_info */ 1400184610Salfred 1401184610Salfred DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 1402184610Salfred uaa.info.bInterfaceClass, 1403184610Salfred uaa.info.bInterfaceSubClass, 1404184610Salfred uaa.info.bInterfaceProtocol, 1405184610Salfred uaa.info.bIfaceIndex, 1406184610Salfred uaa.info.bIfaceNum); 1407184610Salfred 1408223513Shselasky usb_probe_and_attach_sub(udev, &uaa); 1409184610Salfred 1410223513Shselasky /* 1411223513Shselasky * Remove the leftover child, if any, to enforce that 1412223513Shselasky * a new nomatch devd event is generated for the next 1413223513Shselasky * interface if no driver is found: 1414223513Shselasky */ 1415223513Shselasky if (uaa.temp_dev == NULL) 1416223513Shselasky continue; 1417223513Shselasky if (device_delete_child(udev->parent_dev, uaa.temp_dev)) 1418199816Sthompsa DPRINTFN(0, "device delete child failed\n"); 1419223513Shselasky uaa.temp_dev = NULL; 1420184610Salfred } 1421184610Salfreddone: 1422196498Salfred if (do_unlock) 1423196498Salfred usbd_enum_unlock(udev); 1424184610Salfred return (0); 1425184610Salfred} 1426184610Salfred 1427184610Salfred/*------------------------------------------------------------------------* 1428194228Sthompsa * usb_suspend_resume_sub 1429184610Salfred * 1430184610Salfred * This function is called when the suspend or resume methods should 1431184610Salfred * be executed on an USB device. 1432184610Salfred *------------------------------------------------------------------------*/ 1433184610Salfredstatic void 1434194228Sthompsausb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend) 1435184610Salfred{ 1436184610Salfred int err; 1437184610Salfred 1438184610Salfred if (dev == NULL) { 1439184610Salfred return; 1440184610Salfred } 1441184610Salfred if (!device_is_attached(dev)) { 1442184610Salfred return; 1443184610Salfred } 1444184610Salfred if (do_suspend) { 1445184610Salfred err = DEVICE_SUSPEND(dev); 1446184610Salfred } else { 1447184610Salfred err = DEVICE_RESUME(dev); 1448184610Salfred } 1449184610Salfred if (err) { 1450199816Sthompsa device_printf(dev, "%s failed\n", 1451184610Salfred do_suspend ? "Suspend" : "Resume"); 1452184610Salfred } 1453184610Salfred} 1454184610Salfred 1455184610Salfred/*------------------------------------------------------------------------* 1456194228Sthompsa * usb_suspend_resume 1457184610Salfred * 1458184610Salfred * The following function will suspend or resume the USB device. 1459184610Salfred * 1460184610Salfred * Returns: 1461184610Salfred * 0: Success 1462184610Salfred * Else: Failure 1463184610Salfred *------------------------------------------------------------------------*/ 1464193045Sthompsausb_error_t 1465194228Sthompsausb_suspend_resume(struct usb_device *udev, uint8_t do_suspend) 1466184610Salfred{ 1467192984Sthompsa struct usb_interface *iface; 1468184610Salfred uint8_t i; 1469184610Salfred 1470184610Salfred if (udev == NULL) { 1471184610Salfred /* nothing to do */ 1472184610Salfred return (0); 1473184610Salfred } 1474184610Salfred DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 1475184610Salfred 1476208008Sthompsa sx_assert(&udev->sr_sx, SA_LOCKED); 1477184610Salfred 1478184824Sthompsa USB_BUS_LOCK(udev->bus); 1479184610Salfred /* filter the suspend events */ 1480191824Sthompsa if (udev->flags.peer_suspended == do_suspend) { 1481184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1482184610Salfred /* nothing to do */ 1483184610Salfred return (0); 1484184610Salfred } 1485191824Sthompsa udev->flags.peer_suspended = do_suspend; 1486184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1487184610Salfred 1488184610Salfred /* do the suspend or resume */ 1489184610Salfred 1490184610Salfred for (i = 0; i != USB_IFACE_MAX; i++) { 1491184610Salfred 1492194228Sthompsa iface = usbd_get_iface(udev, i); 1493184610Salfred if (iface == NULL) { 1494184610Salfred /* looks like the end of the USB interfaces */ 1495184610Salfred break; 1496184610Salfred } 1497194228Sthompsa usb_suspend_resume_sub(udev, iface->subdev, do_suspend); 1498184610Salfred } 1499184610Salfred return (0); 1500184610Salfred} 1501184610Salfred 1502184610Salfred/*------------------------------------------------------------------------* 1503194228Sthompsa * usbd_clear_stall_proc 1504184610Salfred * 1505184610Salfred * This function performs generic USB clear stall operations. 1506184610Salfred *------------------------------------------------------------------------*/ 1507184610Salfredstatic void 1508194228Sthompsausbd_clear_stall_proc(struct usb_proc_msg *_pm) 1509184610Salfred{ 1510261105Shselasky struct usb_udev_msg *pm = (void *)_pm; 1511192984Sthompsa struct usb_device *udev = pm->udev; 1512184610Salfred 1513184610Salfred /* Change lock */ 1514184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1515207079Sthompsa mtx_lock(&udev->device_mtx); 1516184610Salfred 1517184610Salfred /* Start clear stall callback */ 1518207080Sthompsa usbd_transfer_start(udev->ctrl_xfer[1]); 1519184610Salfred 1520184610Salfred /* Change lock */ 1521207079Sthompsa mtx_unlock(&udev->device_mtx); 1522184824Sthompsa USB_BUS_LOCK(udev->bus); 1523184610Salfred} 1524184610Salfred 1525184610Salfred/*------------------------------------------------------------------------* 1526194228Sthompsa * usb_alloc_device 1527184610Salfred * 1528184610Salfred * This function allocates a new USB device. This function is called 1529184610Salfred * when a new device has been put in the powered state, but not yet in 1530184610Salfred * the addressed state. Get initial descriptor, set the address, get 1531184610Salfred * full descriptor and get strings. 1532184610Salfred * 1533184610Salfred * Return values: 1534184610Salfred * 0: Failure 1535184610Salfred * Else: Success 1536184610Salfred *------------------------------------------------------------------------*/ 1537192984Sthompsastruct usb_device * 1538194228Sthompsausb_alloc_device(device_t parent_dev, struct usb_bus *bus, 1539192984Sthompsa struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, 1540192500Sthompsa uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode) 1541184610Salfred{ 1542192984Sthompsa struct usb_attach_arg uaa; 1543192984Sthompsa struct usb_device *udev; 1544192984Sthompsa struct usb_device *adev; 1545192984Sthompsa struct usb_device *hub; 1546184610Salfred uint8_t *scratch_ptr; 1547193045Sthompsa usb_error_t err; 1548184610Salfred uint8_t device_index; 1549200653Sthompsa uint8_t config_index; 1550200653Sthompsa uint8_t config_quirk; 1551200653Sthompsa uint8_t set_config_failed; 1552246616Shselasky uint8_t do_unlock; 1553184610Salfred 1554184610Salfred DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1555192499Sthompsa "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n", 1556184610Salfred parent_dev, bus, parent_hub, depth, port_index, port_no, 1557192499Sthompsa speed, mode); 1558184610Salfred 1559184610Salfred /* 1560184610Salfred * Find an unused device index. In USB Host mode this is the 1561184610Salfred * same as the device address. 1562184610Salfred * 1563187170Sthompsa * Device index zero is not used and device index 1 should 1564187170Sthompsa * always be the root hub. 1565184610Salfred */ 1566187171Sthompsa for (device_index = USB_ROOT_HUB_ADDR; 1567187171Sthompsa (device_index != bus->devices_max) && 1568187171Sthompsa (bus->devices[device_index] != NULL); 1569187171Sthompsa device_index++) /* nop */; 1570187171Sthompsa 1571187171Sthompsa if (device_index == bus->devices_max) { 1572187171Sthompsa device_printf(bus->bdev, 1573199816Sthompsa "No free USB device index for new device\n"); 1574187171Sthompsa return (NULL); 1575184610Salfred } 1576184610Salfred 1577184610Salfred if (depth > 0x10) { 1578184610Salfred device_printf(bus->bdev, 1579199816Sthompsa "Invalid device depth\n"); 1580184610Salfred return (NULL); 1581184610Salfred } 1582184610Salfred udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 1583184610Salfred if (udev == NULL) { 1584184610Salfred return (NULL); 1585184610Salfred } 1586184610Salfred /* initialise our SX-lock */ 1587207079Sthompsa sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); 1588223512Shselasky sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS); 1589305734Shselasky sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK); 1590184610Salfred 1591207079Sthompsa cv_init(&udev->ctrlreq_cv, "WCTRL"); 1592207079Sthompsa cv_init(&udev->ref_cv, "UGONE"); 1593184610Salfred 1594184610Salfred /* initialise our mutex */ 1595207079Sthompsa mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); 1596184610Salfred 1597184610Salfred /* initialise generic clear stall */ 1598194228Sthompsa udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; 1599184610Salfred udev->cs_msg[0].udev = udev; 1600194228Sthompsa udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; 1601184610Salfred udev->cs_msg[1].udev = udev; 1602184610Salfred 1603184610Salfred /* initialise some USB device fields */ 1604184610Salfred udev->parent_hub = parent_hub; 1605184610Salfred udev->parent_dev = parent_dev; 1606184610Salfred udev->port_index = port_index; 1607184610Salfred udev->port_no = port_no; 1608184610Salfred udev->depth = depth; 1609184610Salfred udev->bus = bus; 1610184610Salfred udev->address = USB_START_ADDR; /* default value */ 1611193045Sthompsa udev->plugtime = (usb_ticks_t)ticks; 1612186730Salfred /* 1613186730Salfred * We need to force the power mode to "on" because there are plenty 1614186730Salfred * of USB devices out there that do not work very well with 1615186730Salfred * automatic suspend and resume! 1616186730Salfred */ 1617212135Sthompsa udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); 1618186730Salfred udev->pwr_save.last_xfer_time = ticks; 1619184610Salfred /* we are not ready yet */ 1620184610Salfred udev->refcount = 1; 1621184610Salfred 1622184610Salfred /* set up default endpoint descriptor */ 1623207080Sthompsa udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); 1624207080Sthompsa udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1625207080Sthompsa udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1626207080Sthompsa udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; 1627207080Sthompsa udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 1628207080Sthompsa udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; 1629207080Sthompsa udev->ctrl_ep_desc.bInterval = 0; 1630213435Shselasky 1631213435Shselasky /* set up default endpoint companion descriptor */ 1632213435Shselasky udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); 1633213435Shselasky udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; 1634213435Shselasky 1635184610Salfred udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1636184610Salfred 1637184610Salfred udev->speed = speed; 1638192499Sthompsa udev->flags.usb_mode = mode; 1639184610Salfred 1640184610Salfred /* search for our High Speed USB HUB, if any */ 1641184610Salfred 1642184610Salfred adev = udev; 1643184610Salfred hub = udev->parent_hub; 1644184610Salfred 1645184610Salfred while (hub) { 1646184610Salfred if (hub->speed == USB_SPEED_HIGH) { 1647184610Salfred udev->hs_hub_addr = hub->address; 1648191395Sthompsa udev->parent_hs_hub = hub; 1649184610Salfred udev->hs_port_no = adev->port_no; 1650184610Salfred break; 1651184610Salfred } 1652184610Salfred adev = hub; 1653184610Salfred hub = hub->parent_hub; 1654184610Salfred } 1655184610Salfred 1656193644Sthompsa /* init the default endpoint */ 1657194228Sthompsa usb_init_endpoint(udev, 0, 1658207080Sthompsa &udev->ctrl_ep_desc, 1659213435Shselasky &udev->ctrl_ep_comp_desc, 1660207080Sthompsa &udev->ctrl_ep); 1661184610Salfred 1662184610Salfred /* set device index */ 1663184610Salfred udev->device_index = device_index; 1664184610Salfred 1665190191Sthompsa#if USB_HAVE_UGEN 1666189599Sthompsa /* Create ugen name */ 1667189599Sthompsa snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1668189599Sthompsa USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1669189599Sthompsa device_index); 1670190191Sthompsa LIST_INIT(&udev->pd_list); 1671190191Sthompsa 1672189110Sthompsa /* Create the control endpoint device */ 1673224777Shselasky udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0, 1674224777Shselasky FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600); 1675189599Sthompsa 1676189110Sthompsa /* Create a link from /dev/ugenX.X to the default endpoint */ 1677224777Shselasky if (udev->ctrl_dev != NULL) 1678224777Shselasky make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name); 1679189599Sthompsa#endif 1680213435Shselasky /* Initialise device */ 1681213435Shselasky if (bus->methods->device_init != NULL) { 1682213435Shselasky err = (bus->methods->device_init) (udev); 1683213435Shselasky if (err != 0) { 1684213435Shselasky DPRINTFN(0, "device init %d failed " 1685213435Shselasky "(%s, ignored)\n", device_index, 1686213435Shselasky usbd_errstr(err)); 1687213435Shselasky goto done; 1688213435Shselasky } 1689213435Shselasky } 1690213435Shselasky /* set powered device state after device init is complete */ 1691213435Shselasky usb_set_device_state(udev, USB_STATE_POWERED); 1692213435Shselasky 1693192499Sthompsa if (udev->flags.usb_mode == USB_MODE_HOST) { 1694184610Salfred 1695194228Sthompsa err = usbd_req_set_address(udev, NULL, device_index); 1696184610Salfred 1697213435Shselasky /* 1698213435Shselasky * This is the new USB device address from now on, if 1699213435Shselasky * the set address request didn't set it already. 1700213435Shselasky */ 1701213435Shselasky if (udev->address == USB_START_ADDR) 1702213435Shselasky udev->address = device_index; 1703184610Salfred 1704184610Salfred /* 1705184610Salfred * We ignore any set-address errors, hence there are 1706184610Salfred * buggy USB devices out there that actually receive 1707184610Salfred * the SETUP PID, but manage to set the address before 1708184610Salfred * the STATUS stage is ACK'ed. If the device responds 1709184610Salfred * to the subsequent get-descriptor at the new 1710184610Salfred * address, then we know that the set-address command 1711184610Salfred * was successful. 1712184610Salfred */ 1713184610Salfred if (err) { 1714184610Salfred DPRINTFN(0, "set address %d failed " 1715190739Sthompsa "(%s, ignored)\n", udev->address, 1716194228Sthompsa usbd_errstr(err)); 1717184610Salfred } 1718184610Salfred } else { 1719184610Salfred /* We are not self powered */ 1720184610Salfred udev->flags.self_powered = 0; 1721184610Salfred 1722184610Salfred /* Set unconfigured state */ 1723184610Salfred udev->curr_config_no = USB_UNCONFIG_NO; 1724184610Salfred udev->curr_config_index = USB_UNCONFIG_INDEX; 1725184610Salfred 1726184610Salfred /* Setup USB descriptors */ 1727194228Sthompsa err = (usb_temp_setup_by_index_p) (udev, usb_template); 1728184610Salfred if (err) { 1729184610Salfred DPRINTFN(0, "setting up USB template failed maybe the USB " 1730184610Salfred "template module has not been loaded\n"); 1731184610Salfred goto done; 1732184610Salfred } 1733184610Salfred } 1734194228Sthompsa usb_set_device_state(udev, USB_STATE_ADDRESSED); 1735184610Salfred 1736213435Shselasky /* setup the device descriptor and the initial "wMaxPacketSize" */ 1737213435Shselasky err = usbd_setup_device_desc(udev, NULL); 1738213435Shselasky 1739213435Shselasky if (err != 0) { 1740248246Shselasky /* try to enumerate two more times */ 1741194228Sthompsa err = usbd_req_re_enumerate(udev, NULL); 1742248246Shselasky if (err != 0) { 1743248246Shselasky err = usbd_req_re_enumerate(udev, NULL); 1744248246Shselasky if (err != 0) { 1745248246Shselasky goto done; 1746248246Shselasky } 1747248246Shselasky } 1748184610Salfred } 1749184610Salfred 1750184610Salfred /* 1751184610Salfred * Setup temporary USB attach args so that we can figure out some 1752184610Salfred * basic quirks for this device. 1753184610Salfred */ 1754194228Sthompsa usb_init_attach_arg(udev, &uaa); 1755184610Salfred 1756194228Sthompsa if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) { 1757184610Salfred udev->flags.uq_bus_powered = 1; 1758184610Salfred } 1759194228Sthompsa if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) { 1760184610Salfred udev->flags.no_strings = 1; 1761184610Salfred } 1762184610Salfred /* 1763184610Salfred * Workaround for buggy USB devices. 1764184610Salfred * 1765184610Salfred * It appears that some string-less USB chips will crash and 1766184610Salfred * disappear if any attempts are made to read any string 1767184610Salfred * descriptors. 1768184610Salfred * 1769184610Salfred * Try to detect such chips by checking the strings in the USB 1770184610Salfred * device descriptor. If no strings are present there we 1771184610Salfred * simply disable all USB strings. 1772184610Salfred */ 1773184610Salfred 1774246616Shselasky /* Protect scratch area */ 1775305734Shselasky do_unlock = usbd_ctrl_lock(udev); 1776246616Shselasky 1777246616Shselasky scratch_ptr = udev->scratch.data; 1778246616Shselasky 1779301250Shselasky if (udev->flags.no_strings) { 1780301250Shselasky err = USB_ERR_INVAL; 1781301250Shselasky } else if (udev->ddesc.iManufacturer || 1782184610Salfred udev->ddesc.iProduct || 1783184610Salfred udev->ddesc.iSerialNumber) { 1784184610Salfred /* read out the language ID string */ 1785194228Sthompsa err = usbd_req_get_string_desc(udev, NULL, 1786201680Sthompsa (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE); 1787184610Salfred } else { 1788184610Salfred err = USB_ERR_INVAL; 1789184610Salfred } 1790184610Salfred 1791184610Salfred if (err || (scratch_ptr[0] < 4)) { 1792184610Salfred udev->flags.no_strings = 1; 1793184610Salfred } else { 1794197559Sthompsa uint16_t langid; 1795197559Sthompsa uint16_t pref; 1796197559Sthompsa uint16_t mask; 1797197559Sthompsa uint8_t x; 1798197559Sthompsa 1799197559Sthompsa /* load preferred value and mask */ 1800197559Sthompsa pref = usb_lang_id; 1801197559Sthompsa mask = usb_lang_mask; 1802197559Sthompsa 1803197559Sthompsa /* align length correctly */ 1804246616Shselasky scratch_ptr[0] &= ~1U; 1805197559Sthompsa 1806197559Sthompsa /* fix compiler warning */ 1807197559Sthompsa langid = 0; 1808197559Sthompsa 1809197559Sthompsa /* search for preferred language */ 1810197559Sthompsa for (x = 2; (x < scratch_ptr[0]); x += 2) { 1811197559Sthompsa langid = UGETW(scratch_ptr + x); 1812197559Sthompsa if ((langid & mask) == pref) 1813197559Sthompsa break; 1814197559Sthompsa } 1815197559Sthompsa if (x >= scratch_ptr[0]) { 1816197559Sthompsa /* pick the first language as the default */ 1817197559Sthompsa DPRINTFN(1, "Using first language\n"); 1818197559Sthompsa langid = UGETW(scratch_ptr + 2); 1819197559Sthompsa } 1820197559Sthompsa 1821197559Sthompsa DPRINTFN(1, "Language selected: 0x%04x\n", langid); 1822197559Sthompsa udev->langid = langid; 1823184610Salfred } 1824184610Salfred 1825246616Shselasky if (do_unlock) 1826305734Shselasky usbd_ctrl_unlock(udev); 1827246616Shselasky 1828184610Salfred /* assume 100mA bus powered for now. Changed when configured. */ 1829184610Salfred udev->power = USB_MIN_POWER; 1830192938Sthompsa /* fetch the vendor and product strings from the device */ 1831194228Sthompsa usbd_set_device_strings(udev); 1832184610Salfred 1833200653Sthompsa if (udev->flags.usb_mode == USB_MODE_DEVICE) { 1834200653Sthompsa /* USB device mode setup is complete */ 1835200653Sthompsa err = 0; 1836200653Sthompsa goto config_done; 1837200653Sthompsa } 1838184610Salfred 1839200653Sthompsa /* 1840200653Sthompsa * Most USB devices should attach to config index 0 by 1841200653Sthompsa * default 1842200653Sthompsa */ 1843200653Sthompsa if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 1844200653Sthompsa config_index = 0; 1845200653Sthompsa config_quirk = 1; 1846200653Sthompsa } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 1847200653Sthompsa config_index = 1; 1848200653Sthompsa config_quirk = 1; 1849200653Sthompsa } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 1850200653Sthompsa config_index = 2; 1851200653Sthompsa config_quirk = 1; 1852200653Sthompsa } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 1853200653Sthompsa config_index = 3; 1854200653Sthompsa config_quirk = 1; 1855200653Sthompsa } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 1856200653Sthompsa config_index = 4; 1857200653Sthompsa config_quirk = 1; 1858200653Sthompsa } else { 1859200653Sthompsa config_index = 0; 1860200653Sthompsa config_quirk = 0; 1861200653Sthompsa } 1862184610Salfred 1863200653Sthompsa set_config_failed = 0; 1864184610Salfredrepeat_set_config: 1865184610Salfred 1866200653Sthompsa DPRINTF("setting config %u\n", config_index); 1867184610Salfred 1868200653Sthompsa /* get the USB device configured */ 1869200653Sthompsa err = usbd_set_config_index(udev, config_index); 1870200653Sthompsa if (err) { 1871200653Sthompsa if (udev->ddesc.bNumConfigurations != 0) { 1872200653Sthompsa if (!set_config_failed) { 1873200653Sthompsa set_config_failed = 1; 1874200653Sthompsa /* XXX try to re-enumerate the device */ 1875200653Sthompsa err = usbd_req_re_enumerate(udev, NULL); 1876200653Sthompsa if (err == 0) 1877200653Sthompsa goto repeat_set_config; 1878186730Salfred } 1879200653Sthompsa DPRINTFN(0, "Failure selecting configuration index %u:" 1880200653Sthompsa "%s, port %u, addr %u (ignored)\n", 1881200653Sthompsa config_index, usbd_errstr(err), udev->port_no, 1882200653Sthompsa udev->address); 1883200653Sthompsa } 1884200653Sthompsa /* 1885200653Sthompsa * Some USB devices do not have any configurations. Ignore any 1886200653Sthompsa * set config failures! 1887200653Sthompsa */ 1888200653Sthompsa err = 0; 1889200653Sthompsa goto config_done; 1890200653Sthompsa } 1891200653Sthompsa if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { 1892200653Sthompsa if ((udev->cdesc->bNumInterface < 2) && 1893200653Sthompsa usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { 1894200653Sthompsa DPRINTFN(0, "Found no endpoints, trying next config\n"); 1895200653Sthompsa config_index++; 1896200653Sthompsa goto repeat_set_config; 1897200653Sthompsa } 1898249043Shselasky#if USB_HAVE_MSCTEST 1899200653Sthompsa if (config_index == 0) { 1900186730Salfred /* 1901200653Sthompsa * Try to figure out if we have an 1902200653Sthompsa * auto-install disk there: 1903186730Salfred */ 1904201681Sthompsa if (usb_iface_is_cdrom(udev, 0)) { 1905200653Sthompsa DPRINTFN(0, "Found possible auto-install " 1906200653Sthompsa "disk (trying next config)\n"); 1907184610Salfred config_index++; 1908184610Salfred goto repeat_set_config; 1909184610Salfred } 1910184610Salfred } 1911249043Shselasky#endif 1912184610Salfred } 1913249043Shselasky#if USB_HAVE_MSCTEST 1914225350Shselasky if (set_config_failed == 0 && config_index == 0 && 1915226903Shselasky usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 && 1916226903Shselasky usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) { 1917184610Salfred 1918225350Shselasky /* 1919225350Shselasky * Try to figure out if there are any MSC quirks we 1920225350Shselasky * should apply automatically: 1921225350Shselasky */ 1922225350Shselasky err = usb_msc_auto_quirk(udev, 0); 1923225350Shselasky 1924225350Shselasky if (err != 0) { 1925225350Shselasky set_config_failed = 1; 1926225350Shselasky goto repeat_set_config; 1927225350Shselasky } 1928225350Shselasky } 1929249043Shselasky#endif 1930225350Shselasky 1931200653Sthompsaconfig_done: 1932184610Salfred DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 1933184610Salfred udev->address, udev, udev->parent_hub); 1934184610Salfred 1935184610Salfred /* register our device - we are ready */ 1936194228Sthompsa usb_bus_port_set_device(bus, parent_hub ? 1937184610Salfred parent_hub->hub->ports + port_index : NULL, udev, device_index); 1938184610Salfred 1939189599Sthompsa#if USB_HAVE_UGEN 1940190191Sthompsa /* Symlink the ugen device name */ 1941194228Sthompsa udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); 1942190191Sthompsa 1943190191Sthompsa /* Announce device */ 1944310281Strasz printf("%s: <%s %s> at %s\n", udev->ugen_name, 1945310281Strasz usb_get_manufacturer(udev), usb_get_product(udev), 1946184610Salfred device_get_nameunit(udev->bus->bdev)); 1947214221Shselasky#endif 1948190727Sthompsa 1949214221Shselasky#if USB_HAVE_DEVCTL 1950207020Sthompsa usb_notify_addq("ATTACH", udev); 1951190191Sthompsa#endif 1952184610Salfreddone: 1953184610Salfred if (err) { 1954197553Sthompsa /* 1955197553Sthompsa * Free USB device and all subdevices, if any. 1956197553Sthompsa */ 1957197553Sthompsa usb_free_device(udev, 0); 1958184610Salfred udev = NULL; 1959184610Salfred } 1960184610Salfred return (udev); 1961184610Salfred} 1962184610Salfred 1963189599Sthompsa#if USB_HAVE_UGEN 1964224777Shselaskystruct usb_fs_privdata * 1965224777Shselaskyusb_make_dev(struct usb_device *udev, const char *devname, int ep, 1966224777Shselasky int fi, int rwmode, uid_t uid, gid_t gid, int mode) 1967189110Sthompsa{ 1968192984Sthompsa struct usb_fs_privdata* pd; 1969224777Shselasky char buffer[32]; 1970189110Sthompsa 1971189110Sthompsa /* Store information to locate ourselves again later */ 1972192984Sthompsa pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, 1973189110Sthompsa M_WAITOK | M_ZERO); 1974189110Sthompsa pd->bus_index = device_get_unit(udev->bus->bdev); 1975189110Sthompsa pd->dev_index = udev->device_index; 1976189110Sthompsa pd->ep_addr = ep; 1977224777Shselasky pd->fifo_index = fi; 1978224777Shselasky pd->mode = rwmode; 1979189110Sthompsa 1980189110Sthompsa /* Now, create the device itself */ 1981224777Shselasky if (devname == NULL) { 1982224777Shselasky devname = buffer; 1983224777Shselasky snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u", 1984224777Shselasky pd->bus_index, pd->dev_index, pd->ep_addr); 1985224777Shselasky } 1986224777Shselasky 1987224777Shselasky pd->cdev = make_dev(&usb_devsw, 0, uid, gid, mode, "%s", devname); 1988224777Shselasky 1989224777Shselasky if (pd->cdev == NULL) { 1990224777Shselasky DPRINTFN(0, "Failed to create device %s\n", devname); 1991224777Shselasky free(pd, M_USBDEV); 1992224777Shselasky return (NULL); 1993224777Shselasky } 1994224777Shselasky 1995224777Shselasky /* XXX setting si_drv1 and creating the device is not atomic! */ 1996189110Sthompsa pd->cdev->si_drv1 = pd; 1997189110Sthompsa 1998224777Shselasky return (pd); 1999189110Sthompsa} 2000189110Sthompsa 2001224777Shselaskyvoid 2002278289Shselaskyusb_destroy_dev_sync(struct usb_fs_privdata *pd) 2003278289Shselasky{ 2004278289Shselasky DPRINTFN(1, "Destroying device at ugen%d.%d\n", 2005278289Shselasky pd->bus_index, pd->dev_index); 2006278289Shselasky 2007278289Shselasky /* 2008278289Shselasky * Destroy character device synchronously. After this 2009278289Shselasky * all system calls are returned. Can block. 2010278289Shselasky */ 2011278289Shselasky destroy_dev(pd->cdev); 2012278289Shselasky 2013278289Shselasky free(pd, M_USBDEV); 2014278289Shselasky} 2015278289Shselasky 2016278289Shselaskyvoid 2017224777Shselaskyusb_destroy_dev(struct usb_fs_privdata *pd) 2018224777Shselasky{ 2019278289Shselasky struct usb_bus *bus; 2020278289Shselasky 2021224777Shselasky if (pd == NULL) 2022224777Shselasky return; 2023224777Shselasky 2024278289Shselasky mtx_lock(&usb_ref_lock); 2025278289Shselasky bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index); 2026278289Shselasky mtx_unlock(&usb_ref_lock); 2027224777Shselasky 2028278289Shselasky if (bus == NULL) { 2029278289Shselasky usb_destroy_dev_sync(pd); 2030278289Shselasky return; 2031278289Shselasky } 2032278289Shselasky 2033278289Shselasky /* make sure we can re-use the device name */ 2034278289Shselasky delist_dev(pd->cdev); 2035278289Shselasky 2036278289Shselasky USB_BUS_LOCK(bus); 2037278289Shselasky LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next); 2038278289Shselasky /* get cleanup going */ 2039278289Shselasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2040278289Shselasky &bus->cleanup_msg[0], &bus->cleanup_msg[1]); 2041278289Shselasky USB_BUS_UNLOCK(bus); 2042224777Shselasky} 2043224777Shselasky 2044189110Sthompsastatic void 2045194228Sthompsausb_cdev_create(struct usb_device *udev) 2046189110Sthompsa{ 2047192984Sthompsa struct usb_config_descriptor *cd; 2048192984Sthompsa struct usb_endpoint_descriptor *ed; 2049192984Sthompsa struct usb_descriptor *desc; 2050192984Sthompsa struct usb_fs_privdata* pd; 2051189172Sthompsa int inmode, outmode, inmask, outmask, mode; 2052189172Sthompsa uint8_t ep; 2053189110Sthompsa 2054189110Sthompsa KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 2055189110Sthompsa 2056189110Sthompsa DPRINTFN(2, "Creating device nodes\n"); 2057189110Sthompsa 2058194228Sthompsa if (usbd_get_mode(udev) == USB_MODE_DEVICE) { 2059189110Sthompsa inmode = FWRITE; 2060189110Sthompsa outmode = FREAD; 2061189110Sthompsa } else { /* USB_MODE_HOST */ 2062189110Sthompsa inmode = FREAD; 2063189110Sthompsa outmode = FWRITE; 2064189110Sthompsa } 2065189110Sthompsa 2066189172Sthompsa inmask = 0; 2067189172Sthompsa outmask = 0; 2068189172Sthompsa desc = NULL; 2069189110Sthompsa 2070189172Sthompsa /* 2071189172Sthompsa * Collect all used endpoint numbers instead of just 2072189172Sthompsa * generating 16 static endpoints. 2073189172Sthompsa */ 2074194228Sthompsa cd = usbd_get_config_descriptor(udev); 2075194228Sthompsa while ((desc = usb_desc_foreach(cd, desc))) { 2076189172Sthompsa /* filter out all endpoint descriptors */ 2077189172Sthompsa if ((desc->bDescriptorType == UDESC_ENDPOINT) && 2078189172Sthompsa (desc->bLength >= sizeof(*ed))) { 2079192984Sthompsa ed = (struct usb_endpoint_descriptor *)desc; 2080189110Sthompsa 2081189172Sthompsa /* update masks */ 2082189172Sthompsa ep = ed->bEndpointAddress; 2083189172Sthompsa if (UE_GET_DIR(ep) == UE_DIR_OUT) 2084189172Sthompsa outmask |= 1 << UE_GET_ADDR(ep); 2085189172Sthompsa else 2086189172Sthompsa inmask |= 1 << UE_GET_ADDR(ep); 2087189110Sthompsa } 2088189110Sthompsa } 2089189172Sthompsa 2090189172Sthompsa /* Create all available endpoints except EP0 */ 2091189172Sthompsa for (ep = 1; ep < 16; ep++) { 2092224777Shselasky mode = (inmask & (1 << ep)) ? inmode : 0; 2093224777Shselasky mode |= (outmask & (1 << ep)) ? outmode : 0; 2094189172Sthompsa if (mode == 0) 2095189172Sthompsa continue; /* no IN or OUT endpoint */ 2096189172Sthompsa 2097224777Shselasky pd = usb_make_dev(udev, NULL, ep, 0, 2098224777Shselasky mode, UID_ROOT, GID_OPERATOR, 0600); 2099224777Shselasky 2100224777Shselasky if (pd != NULL) 2101224777Shselasky LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 2102189172Sthompsa } 2103189110Sthompsa} 2104189110Sthompsa 2105189110Sthompsastatic void 2106194228Sthompsausb_cdev_free(struct usb_device *udev) 2107189110Sthompsa{ 2108192984Sthompsa struct usb_fs_privdata* pd; 2109189110Sthompsa 2110189110Sthompsa DPRINTFN(2, "Freeing device nodes\n"); 2111189110Sthompsa 2112189110Sthompsa while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 2113189110Sthompsa KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 2114189110Sthompsa 2115189110Sthompsa LIST_REMOVE(pd, pd_next); 2116224777Shselasky 2117224777Shselasky usb_destroy_dev(pd); 2118189110Sthompsa } 2119189110Sthompsa} 2120189599Sthompsa#endif 2121189110Sthompsa 2122184610Salfred/*------------------------------------------------------------------------* 2123194228Sthompsa * usb_free_device 2124184610Salfred * 2125197553Sthompsa * This function is NULL safe and will free an USB device and its 2126197553Sthompsa * children devices, if any. 2127190730Sthompsa * 2128197553Sthompsa * Flag values: Reserved, set to zero. 2129184610Salfred *------------------------------------------------------------------------*/ 2130184610Salfredvoid 2131194228Sthompsausb_free_device(struct usb_device *udev, uint8_t flag) 2132184610Salfred{ 2133192984Sthompsa struct usb_bus *bus; 2134184610Salfred 2135190730Sthompsa if (udev == NULL) 2136190730Sthompsa return; /* already freed */ 2137190730Sthompsa 2138184610Salfred DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 2139184610Salfred 2140191494Sthompsa bus = udev->bus; 2141261110Shselasky 2142261110Shselasky /* set DETACHED state to prevent any further references */ 2143194228Sthompsa usb_set_device_state(udev, USB_STATE_DETACHED); 2144190730Sthompsa 2145214221Shselasky#if USB_HAVE_DEVCTL 2146207020Sthompsa usb_notify_addq("DETACH", udev); 2147214221Shselasky#endif 2148184610Salfred 2149214221Shselasky#if USB_HAVE_UGEN 2150267775Smarius if (!rebooting) { 2151310281Strasz printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name, 2152310281Strasz usb_get_manufacturer(udev), usb_get_product(udev), 2153310281Strasz device_get_nameunit(bus->bdev)); 2154267775Smarius } 2155184610Salfred 2156189110Sthompsa /* Destroy UGEN symlink, if any */ 2157184610Salfred if (udev->ugen_symlink) { 2158194228Sthompsa usb_free_symlink(udev->ugen_symlink); 2159184610Salfred udev->ugen_symlink = NULL; 2160184610Salfred } 2161184610Salfred 2162224777Shselasky usb_destroy_dev(udev->ctrl_dev); 2163189599Sthompsa#endif 2164184610Salfred 2165192499Sthompsa if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2166184610Salfred /* stop receiving any control transfers (Device Side Mode) */ 2167207080Sthompsa usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 2168184610Salfred } 2169184610Salfred 2170190730Sthompsa /* the following will get the device unconfigured in software */ 2171197553Sthompsa usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0); 2172184610Salfred 2173261110Shselasky /* final device unregister after all character devices are closed */ 2174261110Shselasky usb_bus_port_set_device(bus, udev->parent_hub ? 2175261110Shselasky udev->parent_hub->hub->ports + udev->port_index : NULL, 2176261110Shselasky NULL, USB_ROOT_HUB_ADDR); 2177261110Shselasky 2178184610Salfred /* unsetup any leftover default USB transfers */ 2179207080Sthompsa usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 2180184610Salfred 2181187174Sthompsa /* template unsetup, if any */ 2182194228Sthompsa (usb_temp_unsetup_p) (udev); 2183184610Salfred 2184187174Sthompsa /* 2185187174Sthompsa * Make sure that our clear-stall messages are not queued 2186187174Sthompsa * anywhere: 2187187174Sthompsa */ 2188187174Sthompsa USB_BUS_LOCK(udev->bus); 2189287274Shselasky usb_proc_mwait(USB_BUS_CS_PROC(udev->bus), 2190187174Sthompsa &udev->cs_msg[0], &udev->cs_msg[1]); 2191187174Sthompsa USB_BUS_UNLOCK(udev->bus); 2192187174Sthompsa 2193278289Shselasky /* wait for all references to go away */ 2194278289Shselasky usb_wait_pending_refs(udev); 2195278289Shselasky 2196207079Sthompsa sx_destroy(&udev->enum_sx); 2197208008Sthompsa sx_destroy(&udev->sr_sx); 2198305734Shselasky sx_destroy(&udev->ctrl_sx); 2199184610Salfred 2200207079Sthompsa cv_destroy(&udev->ctrlreq_cv); 2201207079Sthompsa cv_destroy(&udev->ref_cv); 2202184610Salfred 2203207079Sthompsa mtx_destroy(&udev->device_mtx); 2204190191Sthompsa#if USB_HAVE_UGEN 2205189110Sthompsa KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 2206190191Sthompsa#endif 2207184610Salfred 2208213435Shselasky /* Uninitialise device */ 2209213435Shselasky if (bus->methods->device_uninit != NULL) 2210213435Shselasky (bus->methods->device_uninit) (udev); 2211213435Shselasky 2212184610Salfred /* free device */ 2213193042Sthompsa free(udev->serial, M_USB); 2214193042Sthompsa free(udev->manufacturer, M_USB); 2215193042Sthompsa free(udev->product, M_USB); 2216184610Salfred free(udev, M_USB); 2217184610Salfred} 2218184610Salfred 2219184610Salfred/*------------------------------------------------------------------------* 2220194228Sthompsa * usbd_get_iface 2221184610Salfred * 2222184610Salfred * This function is the safe way to get the USB interface structure 2223184610Salfred * pointer by interface index. 2224184610Salfred * 2225184610Salfred * Return values: 2226184610Salfred * NULL: Interface not present. 2227184610Salfred * Else: Pointer to USB interface structure. 2228184610Salfred *------------------------------------------------------------------------*/ 2229192984Sthompsastruct usb_interface * 2230194228Sthompsausbd_get_iface(struct usb_device *udev, uint8_t iface_index) 2231184610Salfred{ 2232192984Sthompsa struct usb_interface *iface = udev->ifaces + iface_index; 2233184610Salfred 2234190730Sthompsa if (iface_index >= udev->ifaces_max) 2235184610Salfred return (NULL); 2236184610Salfred return (iface); 2237184610Salfred} 2238184610Salfred 2239184610Salfred/*------------------------------------------------------------------------* 2240194271Sthompsa * usbd_find_descriptor 2241184610Salfred * 2242184610Salfred * This function will lookup the first descriptor that matches the 2243184610Salfred * criteria given by the arguments "type" and "subtype". Descriptors 2244184610Salfred * will only be searched within the interface having the index 2245184610Salfred * "iface_index". If the "id" argument points to an USB descriptor, 2246184610Salfred * it will be skipped before the search is started. This allows 2247184610Salfred * searching for multiple descriptors using the same criteria. Else 2248184610Salfred * the search is started after the interface descriptor. 2249184610Salfred * 2250184610Salfred * Return values: 2251184610Salfred * NULL: End of descriptors 2252184610Salfred * Else: A descriptor matching the criteria 2253184610Salfred *------------------------------------------------------------------------*/ 2254184610Salfredvoid * 2255194271Sthompsausbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index, 2256184610Salfred uint8_t type, uint8_t type_mask, 2257184610Salfred uint8_t subtype, uint8_t subtype_mask) 2258184610Salfred{ 2259192984Sthompsa struct usb_descriptor *desc; 2260192984Sthompsa struct usb_config_descriptor *cd; 2261192984Sthompsa struct usb_interface *iface; 2262184610Salfred 2263194228Sthompsa cd = usbd_get_config_descriptor(udev); 2264184610Salfred if (cd == NULL) { 2265184610Salfred return (NULL); 2266184610Salfred } 2267184610Salfred if (id == NULL) { 2268194228Sthompsa iface = usbd_get_iface(udev, iface_index); 2269184610Salfred if (iface == NULL) { 2270184610Salfred return (NULL); 2271184610Salfred } 2272194228Sthompsa id = usbd_get_interface_descriptor(iface); 2273184610Salfred if (id == NULL) { 2274184610Salfred return (NULL); 2275184610Salfred } 2276184610Salfred } 2277184610Salfred desc = (void *)id; 2278184610Salfred 2279194228Sthompsa while ((desc = usb_desc_foreach(cd, desc))) { 2280184610Salfred 2281184610Salfred if (desc->bDescriptorType == UDESC_INTERFACE) { 2282184610Salfred break; 2283184610Salfred } 2284184610Salfred if (((desc->bDescriptorType & type_mask) == type) && 2285184610Salfred ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 2286184610Salfred return (desc); 2287184610Salfred } 2288184610Salfred } 2289184610Salfred return (NULL); 2290184610Salfred} 2291184610Salfred 2292184610Salfred/*------------------------------------------------------------------------* 2293194228Sthompsa * usb_devinfo 2294184610Salfred * 2295184610Salfred * This function will dump information from the device descriptor 2296184610Salfred * belonging to the USB device pointed to by "udev", to the string 2297184610Salfred * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 2298184610Salfred * including the terminating zero. 2299184610Salfred *------------------------------------------------------------------------*/ 2300184610Salfredvoid 2301194228Sthompsausb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) 2302184610Salfred{ 2303192984Sthompsa struct usb_device_descriptor *udd = &udev->ddesc; 2304184610Salfred uint16_t bcdDevice; 2305184610Salfred uint16_t bcdUSB; 2306184610Salfred 2307184610Salfred bcdUSB = UGETW(udd->bcdUSB); 2308184610Salfred bcdDevice = UGETW(udd->bcdDevice); 2309184610Salfred 2310184610Salfred if (udd->bDeviceClass != 0xFF) { 2311184610Salfred snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 2312190191Sthompsa "%x.%02x, addr %d", 2313212136Sthompsa usb_get_manufacturer(udev), 2314212136Sthompsa usb_get_product(udev), 2315184610Salfred udd->bDeviceClass, udd->bDeviceSubClass, 2316184610Salfred (bcdUSB >> 8), bcdUSB & 0xFF, 2317184610Salfred (bcdDevice >> 8), bcdDevice & 0xFF, 2318184610Salfred udev->address); 2319184610Salfred } else { 2320184610Salfred snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 2321190191Sthompsa "%x.%02x, addr %d", 2322212136Sthompsa usb_get_manufacturer(udev), 2323212136Sthompsa usb_get_product(udev), 2324184610Salfred (bcdUSB >> 8), bcdUSB & 0xFF, 2325184610Salfred (bcdDevice >> 8), bcdDevice & 0xFF, 2326184610Salfred udev->address); 2327184610Salfred } 2328184610Salfred} 2329184610Salfred 2330194677Sthompsa#ifdef USB_VERBOSE 2331184610Salfred/* 2332184610Salfred * Descriptions of of known vendors and devices ("products"). 2333184610Salfred */ 2334184610Salfredstruct usb_knowndev { 2335184610Salfred uint16_t vendor; 2336184610Salfred uint16_t product; 2337184610Salfred uint32_t flags; 2338184610Salfred const char *vendorname; 2339184610Salfred const char *productname; 2340184610Salfred}; 2341184610Salfred 2342184610Salfred#define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 2343184610Salfred 2344188746Sthompsa#include "usbdevs.h" 2345188746Sthompsa#include "usbdevs_data.h" 2346184610Salfred#endif /* USB_VERBOSE */ 2347184610Salfred 2348184610Salfredstatic void 2349194228Sthompsausbd_set_device_strings(struct usb_device *udev) 2350184610Salfred{ 2351192984Sthompsa struct usb_device_descriptor *udd = &udev->ddesc; 2352194677Sthompsa#ifdef USB_VERBOSE 2353184610Salfred const struct usb_knowndev *kdp; 2354184610Salfred#endif 2355208008Sthompsa char *temp_ptr; 2356197559Sthompsa size_t temp_size; 2357184610Salfred uint16_t vendor_id; 2358184610Salfred uint16_t product_id; 2359246616Shselasky uint8_t do_unlock; 2360184610Salfred 2361246616Shselasky /* Protect scratch area */ 2362305734Shselasky do_unlock = usbd_ctrl_lock(udev); 2363197559Sthompsa 2364246616Shselasky temp_ptr = (char *)udev->scratch.data; 2365246616Shselasky temp_size = sizeof(udev->scratch.data); 2366246616Shselasky 2367192938Sthompsa vendor_id = UGETW(udd->idVendor); 2368192938Sthompsa product_id = UGETW(udd->idProduct); 2369184610Salfred 2370192938Sthompsa /* get serial number string */ 2371197559Sthompsa usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2372192938Sthompsa udev->ddesc.iSerialNumber); 2373197559Sthompsa udev->serial = strdup(temp_ptr, M_USB); 2374184610Salfred 2375192938Sthompsa /* get manufacturer string */ 2376197559Sthompsa usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2377192938Sthompsa udev->ddesc.iManufacturer); 2378197559Sthompsa usb_trim_spaces(temp_ptr); 2379197559Sthompsa if (temp_ptr[0] != '\0') 2380197559Sthompsa udev->manufacturer = strdup(temp_ptr, M_USB); 2381184610Salfred 2382192938Sthompsa /* get product string */ 2383197559Sthompsa usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2384192938Sthompsa udev->ddesc.iProduct); 2385197559Sthompsa usb_trim_spaces(temp_ptr); 2386197559Sthompsa if (temp_ptr[0] != '\0') 2387197559Sthompsa udev->product = strdup(temp_ptr, M_USB); 2388184610Salfred 2389194677Sthompsa#ifdef USB_VERBOSE 2390192938Sthompsa if (udev->manufacturer == NULL || udev->product == NULL) { 2391192938Sthompsa for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { 2392184610Salfred if (kdp->vendor == vendor_id && 2393184610Salfred (kdp->product == product_id || 2394184610Salfred (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 2395184610Salfred break; 2396184610Salfred } 2397184610Salfred if (kdp->vendorname != NULL) { 2398192938Sthompsa /* XXX should use pointer to knowndevs string */ 2399192938Sthompsa if (udev->manufacturer == NULL) { 2400192938Sthompsa udev->manufacturer = strdup(kdp->vendorname, 2401192938Sthompsa M_USB); 2402192938Sthompsa } 2403192938Sthompsa if (udev->product == NULL && 2404192938Sthompsa (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { 2405192938Sthompsa udev->product = strdup(kdp->productname, 2406192938Sthompsa M_USB); 2407192938Sthompsa } 2408184610Salfred } 2409184610Salfred } 2410184610Salfred#endif 2411192938Sthompsa /* Provide default strings if none were found */ 2412192938Sthompsa if (udev->manufacturer == NULL) { 2413197559Sthompsa snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id); 2414197559Sthompsa udev->manufacturer = strdup(temp_ptr, M_USB); 2415184610Salfred } 2416192938Sthompsa if (udev->product == NULL) { 2417197559Sthompsa snprintf(temp_ptr, temp_size, "product 0x%04x", product_id); 2418197559Sthompsa udev->product = strdup(temp_ptr, M_USB); 2419184610Salfred } 2420246616Shselasky 2421246616Shselasky if (do_unlock) 2422305734Shselasky usbd_ctrl_unlock(udev); 2423184610Salfred} 2424184610Salfred 2425188411Sthompsa/* 2426188411Sthompsa * Returns: 2427188411Sthompsa * See: USB_MODE_XXX 2428188411Sthompsa */ 2429192499Sthompsaenum usb_hc_mode 2430194228Sthompsausbd_get_mode(struct usb_device *udev) 2431188411Sthompsa{ 2432192499Sthompsa return (udev->flags.usb_mode); 2433188411Sthompsa} 2434188411Sthompsa 2435188411Sthompsa/* 2436188411Sthompsa * Returns: 2437188411Sthompsa * See: USB_SPEED_XXX 2438188411Sthompsa */ 2439192500Sthompsaenum usb_dev_speed 2440194228Sthompsausbd_get_speed(struct usb_device *udev) 2441184610Salfred{ 2442184610Salfred return (udev->speed); 2443184610Salfred} 2444184610Salfred 2445186730Salfreduint32_t 2446194228Sthompsausbd_get_isoc_fps(struct usb_device *udev) 2447186730Salfred{ 2448186730Salfred ; /* indent fix */ 2449186730Salfred switch (udev->speed) { 2450186730Salfred case USB_SPEED_LOW: 2451186730Salfred case USB_SPEED_FULL: 2452186730Salfred return (1000); 2453186730Salfred default: 2454186730Salfred return (8000); 2455186730Salfred } 2456186730Salfred} 2457186730Salfred 2458192984Sthompsastruct usb_device_descriptor * 2459194228Sthompsausbd_get_device_descriptor(struct usb_device *udev) 2460184610Salfred{ 2461184610Salfred if (udev == NULL) 2462184610Salfred return (NULL); /* be NULL safe */ 2463184610Salfred return (&udev->ddesc); 2464184610Salfred} 2465184610Salfred 2466192984Sthompsastruct usb_config_descriptor * 2467194228Sthompsausbd_get_config_descriptor(struct usb_device *udev) 2468184610Salfred{ 2469184610Salfred if (udev == NULL) 2470184610Salfred return (NULL); /* be NULL safe */ 2471184610Salfred return (udev->cdesc); 2472184610Salfred} 2473184610Salfred 2474184610Salfred/*------------------------------------------------------------------------* 2475194228Sthompsa * usb_test_quirk - test a device for a given quirk 2476184610Salfred * 2477184610Salfred * Return values: 2478184610Salfred * 0: The USB device does not have the given quirk. 2479184610Salfred * Else: The USB device has the given quirk. 2480184610Salfred *------------------------------------------------------------------------*/ 2481184610Salfreduint8_t 2482194228Sthompsausb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk) 2483184610Salfred{ 2484184610Salfred uint8_t found; 2485225469Shselasky uint8_t x; 2486184610Salfred 2487225469Shselasky if (quirk == UQ_NONE) 2488225469Shselasky return (0); 2489225469Shselasky 2490225469Shselasky /* search the automatic per device quirks first */ 2491225469Shselasky 2492225469Shselasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 2493225469Shselasky if (uaa->device->autoQuirk[x] == quirk) 2494225469Shselasky return (1); 2495225469Shselasky } 2496225469Shselasky 2497225469Shselasky /* search global quirk table, if any */ 2498225469Shselasky 2499194228Sthompsa found = (usb_test_quirk_p) (&uaa->info, quirk); 2500225469Shselasky 2501184610Salfred return (found); 2502184610Salfred} 2503184610Salfred 2504192984Sthompsastruct usb_interface_descriptor * 2505194228Sthompsausbd_get_interface_descriptor(struct usb_interface *iface) 2506184610Salfred{ 2507184610Salfred if (iface == NULL) 2508184610Salfred return (NULL); /* be NULL safe */ 2509184610Salfred return (iface->idesc); 2510184610Salfred} 2511184610Salfred 2512184610Salfreduint8_t 2513194228Sthompsausbd_get_interface_altindex(struct usb_interface *iface) 2514184610Salfred{ 2515184610Salfred return (iface->alt_index); 2516184610Salfred} 2517184610Salfred 2518184610Salfreduint8_t 2519194228Sthompsausbd_get_bus_index(struct usb_device *udev) 2520184610Salfred{ 2521184610Salfred return ((uint8_t)device_get_unit(udev->bus->bdev)); 2522184610Salfred} 2523184610Salfred 2524184610Salfreduint8_t 2525194228Sthompsausbd_get_device_index(struct usb_device *udev) 2526184610Salfred{ 2527184610Salfred return (udev->device_index); 2528184610Salfred} 2529184610Salfred 2530214221Shselasky#if USB_HAVE_DEVCTL 2531184610Salfredstatic void 2532207020Sthompsausb_notify_addq(const char *type, struct usb_device *udev) 2533207020Sthompsa{ 2534207020Sthompsa struct usb_interface *iface; 2535207020Sthompsa struct sbuf *sb; 2536207020Sthompsa int i; 2537207020Sthompsa 2538207020Sthompsa /* announce the device */ 2539207020Sthompsa sb = sbuf_new_auto(); 2540207020Sthompsa sbuf_printf(sb, 2541214221Shselasky#if USB_HAVE_UGEN 2542214221Shselasky "ugen=%s " 2543216072Shselasky "cdev=%s " 2544214221Shselasky#endif 2545207020Sthompsa "vendor=0x%04x " 2546207020Sthompsa "product=0x%04x " 2547207020Sthompsa "devclass=0x%02x " 2548207020Sthompsa "devsubclass=0x%02x " 2549207020Sthompsa "sernum=\"%s\" " 2550207020Sthompsa "release=0x%04x " 2551207027Sthompsa "mode=%s " 2552207020Sthompsa "port=%u " 2553214221Shselasky#if USB_HAVE_UGEN 2554214809Sn_hibma "parent=%s" 2555214221Shselasky#endif 2556214221Shselasky "", 2557214221Shselasky#if USB_HAVE_UGEN 2558207020Sthompsa udev->ugen_name, 2559216072Shselasky udev->ugen_name, 2560214221Shselasky#endif 2561207020Sthompsa UGETW(udev->ddesc.idVendor), 2562207020Sthompsa UGETW(udev->ddesc.idProduct), 2563207020Sthompsa udev->ddesc.bDeviceClass, 2564207020Sthompsa udev->ddesc.bDeviceSubClass, 2565212136Sthompsa usb_get_serial(udev), 2566207020Sthompsa UGETW(udev->ddesc.bcdDevice), 2567207027Sthompsa (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 2568214221Shselasky udev->port_no 2569214221Shselasky#if USB_HAVE_UGEN 2570214221Shselasky , udev->parent_hub != NULL ? 2571214221Shselasky udev->parent_hub->ugen_name : 2572214221Shselasky device_get_nameunit(device_get_parent(udev->bus->bdev)) 2573214221Shselasky#endif 2574214221Shselasky ); 2575207020Sthompsa sbuf_finish(sb); 2576207020Sthompsa devctl_notify("USB", "DEVICE", type, sbuf_data(sb)); 2577207020Sthompsa sbuf_delete(sb); 2578207020Sthompsa 2579207020Sthompsa /* announce each interface */ 2580207020Sthompsa for (i = 0; i < USB_IFACE_MAX; i++) { 2581207020Sthompsa iface = usbd_get_iface(udev, i); 2582207020Sthompsa if (iface == NULL) 2583207020Sthompsa break; /* end of interfaces */ 2584207020Sthompsa if (iface->idesc == NULL) 2585207020Sthompsa continue; /* no interface descriptor */ 2586207020Sthompsa 2587207020Sthompsa sb = sbuf_new_auto(); 2588207020Sthompsa sbuf_printf(sb, 2589214221Shselasky#if USB_HAVE_UGEN 2590214221Shselasky "ugen=%s " 2591216072Shselasky "cdev=%s " 2592214221Shselasky#endif 2593207020Sthompsa "vendor=0x%04x " 2594207020Sthompsa "product=0x%04x " 2595207020Sthompsa "devclass=0x%02x " 2596207020Sthompsa "devsubclass=0x%02x " 2597207020Sthompsa "sernum=\"%s\" " 2598207020Sthompsa "release=0x%04x " 2599207027Sthompsa "mode=%s " 2600207020Sthompsa "interface=%d " 2601207020Sthompsa "endpoints=%d " 2602207020Sthompsa "intclass=0x%02x " 2603207020Sthompsa "intsubclass=0x%02x " 2604214809Sn_hibma "intprotocol=0x%02x", 2605214221Shselasky#if USB_HAVE_UGEN 2606207020Sthompsa udev->ugen_name, 2607216072Shselasky udev->ugen_name, 2608214221Shselasky#endif 2609207020Sthompsa UGETW(udev->ddesc.idVendor), 2610207020Sthompsa UGETW(udev->ddesc.idProduct), 2611207020Sthompsa udev->ddesc.bDeviceClass, 2612207020Sthompsa udev->ddesc.bDeviceSubClass, 2613212136Sthompsa usb_get_serial(udev), 2614207020Sthompsa UGETW(udev->ddesc.bcdDevice), 2615207027Sthompsa (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 2616207020Sthompsa iface->idesc->bInterfaceNumber, 2617207020Sthompsa iface->idesc->bNumEndpoints, 2618207020Sthompsa iface->idesc->bInterfaceClass, 2619207020Sthompsa iface->idesc->bInterfaceSubClass, 2620207020Sthompsa iface->idesc->bInterfaceProtocol); 2621207020Sthompsa sbuf_finish(sb); 2622207020Sthompsa devctl_notify("USB", "INTERFACE", type, sbuf_data(sb)); 2623207020Sthompsa sbuf_delete(sb); 2624207020Sthompsa } 2625207020Sthompsa} 2626214221Shselasky#endif 2627207020Sthompsa 2628214221Shselasky#if USB_HAVE_UGEN 2629184610Salfred/*------------------------------------------------------------------------* 2630194228Sthompsa * usb_fifo_free_wrap 2631184610Salfred * 2632185087Salfred * This function will free the FIFOs. 2633185087Salfred * 2634190730Sthompsa * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2635190730Sthompsa * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2636190730Sthompsa * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2637190730Sthompsa * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2638190730Sthompsa * control endpoint FIFOs. If "iface_index" is not set to 2639190730Sthompsa * "USB_IFACE_INDEX_ANY" the flag has no effect. 2640184610Salfred *------------------------------------------------------------------------*/ 2641184610Salfredstatic void 2642194228Sthompsausb_fifo_free_wrap(struct usb_device *udev, 2643185087Salfred uint8_t iface_index, uint8_t flag) 2644184610Salfred{ 2645192984Sthompsa struct usb_fifo *f; 2646184610Salfred uint16_t i; 2647184610Salfred 2648184610Salfred /* 2649184610Salfred * Free any USB FIFOs on the given interface: 2650184610Salfred */ 2651184610Salfred for (i = 0; i != USB_FIFO_MAX; i++) { 2652184610Salfred f = udev->fifo[i]; 2653184610Salfred if (f == NULL) { 2654184610Salfred continue; 2655184610Salfred } 2656185087Salfred /* Check if the interface index matches */ 2657185087Salfred if (iface_index == f->iface_index) { 2658194228Sthompsa if (f->methods != &usb_ugen_methods) { 2659185087Salfred /* 2660185087Salfred * Don't free any non-generic FIFOs in 2661185087Salfred * this case. 2662185087Salfred */ 2663185087Salfred continue; 2664185087Salfred } 2665185290Salfred if ((f->dev_ep_index == 0) && 2666185290Salfred (f->fs_xfer == NULL)) { 2667185290Salfred /* no need to free this FIFO */ 2668185087Salfred continue; 2669185087Salfred } 2670185087Salfred } else if (iface_index == USB_IFACE_INDEX_ANY) { 2671194228Sthompsa if ((f->methods == &usb_ugen_methods) && 2672190730Sthompsa (f->dev_ep_index == 0) && 2673190730Sthompsa (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 2674185290Salfred (f->fs_xfer == NULL)) { 2675185290Salfred /* no need to free this FIFO */ 2676185087Salfred continue; 2677185087Salfred } 2678185087Salfred } else { 2679185290Salfred /* no need to free this FIFO */ 2680184610Salfred continue; 2681184610Salfred } 2682185290Salfred /* free this FIFO */ 2683194228Sthompsa usb_fifo_free(f); 2684184610Salfred } 2685184610Salfred} 2686189599Sthompsa#endif 2687186730Salfred 2688186730Salfred/*------------------------------------------------------------------------* 2689194228Sthompsa * usb_peer_can_wakeup 2690186730Salfred * 2691186730Salfred * Return values: 2692186730Salfred * 0: Peer cannot do resume signalling. 2693186730Salfred * Else: Peer can do resume signalling. 2694186730Salfred *------------------------------------------------------------------------*/ 2695186730Salfreduint8_t 2696194228Sthompsausb_peer_can_wakeup(struct usb_device *udev) 2697186730Salfred{ 2698192984Sthompsa const struct usb_config_descriptor *cdp; 2699186730Salfred 2700186730Salfred cdp = udev->cdesc; 2701192499Sthompsa if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { 2702186730Salfred return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 2703186730Salfred } 2704186730Salfred return (0); /* not supported */ 2705186730Salfred} 2706191494Sthompsa 2707191494Sthompsavoid 2708194228Sthompsausb_set_device_state(struct usb_device *udev, enum usb_dev_state state) 2709191494Sthompsa{ 2710191494Sthompsa 2711191494Sthompsa KASSERT(state < USB_STATE_MAX, ("invalid udev state")); 2712191494Sthompsa 2713191494Sthompsa DPRINTF("udev %p state %s -> %s\n", udev, 2714194228Sthompsa usb_statestr(udev->state), usb_statestr(state)); 2715261110Shselasky 2716261110Shselasky#if USB_HAVE_UGEN 2717261110Shselasky mtx_lock(&usb_ref_lock); 2718261110Shselasky#endif 2719191494Sthompsa udev->state = state; 2720261110Shselasky#if USB_HAVE_UGEN 2721261110Shselasky mtx_unlock(&usb_ref_lock); 2722261110Shselasky#endif 2723213435Shselasky if (udev->bus->methods->device_state_change != NULL) 2724213435Shselasky (udev->bus->methods->device_state_change) (udev); 2725191494Sthompsa} 2726191494Sthompsa 2727213435Shselaskyenum usb_dev_state 2728213435Shselaskyusb_get_device_state(struct usb_device *udev) 2729213435Shselasky{ 2730213435Shselasky if (udev == NULL) 2731213435Shselasky return (USB_STATE_DETACHED); 2732213435Shselasky return (udev->state); 2733213435Shselasky} 2734213435Shselasky 2735191824Sthompsauint8_t 2736194228Sthompsausbd_device_attached(struct usb_device *udev) 2737191494Sthompsa{ 2738191494Sthompsa return (udev->state > USB_STATE_DETACHED); 2739191494Sthompsa} 2740196498Salfred 2741246616Shselasky/* 2742246616Shselasky * The following function locks enumerating the given USB device. If 2743246616Shselasky * the lock is already grabbed this function returns zero. Else a 2744301253Shselasky * a value of one is returned. 2745246616Shselasky */ 2746246616Shselaskyuint8_t 2747196498Salfredusbd_enum_lock(struct usb_device *udev) 2748196498Salfred{ 2749246616Shselasky if (sx_xlocked(&udev->enum_sx)) 2750246616Shselasky return (0); 2751246616Shselasky 2752207079Sthompsa sx_xlock(&udev->enum_sx); 2753208008Sthompsa sx_xlock(&udev->sr_sx); 2754196498Salfred /* 2755196498Salfred * NEWBUS LOCK NOTE: We should check if any parent SX locks 2756196498Salfred * are locked before locking Giant. Else the lock can be 2757196498Salfred * locked multiple times. 2758196498Salfred */ 2759196498Salfred mtx_lock(&Giant); 2760246616Shselasky return (1); 2761196498Salfred} 2762196498Salfred 2763301253Shselasky#if USB_HAVE_UGEN 2764301253Shselasky/* 2765301253Shselasky * This function is the same like usbd_enum_lock() except a value of 2766301253Shselasky * 255 is returned when a signal is pending: 2767301253Shselasky */ 2768301253Shselaskyuint8_t 2769301253Shselaskyusbd_enum_lock_sig(struct usb_device *udev) 2770301253Shselasky{ 2771301253Shselasky if (sx_xlocked(&udev->enum_sx)) 2772301253Shselasky return (0); 2773301253Shselasky if (sx_xlock_sig(&udev->enum_sx)) 2774301253Shselasky return (255); 2775301253Shselasky if (sx_xlock_sig(&udev->sr_sx)) { 2776301253Shselasky sx_xunlock(&udev->enum_sx); 2777301253Shselasky return (255); 2778301253Shselasky } 2779301253Shselasky mtx_lock(&Giant); 2780301253Shselasky return (1); 2781301253Shselasky} 2782301253Shselasky#endif 2783301253Shselasky 2784196498Salfred/* The following function unlocks enumerating the given USB device. */ 2785196498Salfred 2786196498Salfredvoid 2787196498Salfredusbd_enum_unlock(struct usb_device *udev) 2788196498Salfred{ 2789196498Salfred mtx_unlock(&Giant); 2790207079Sthompsa sx_xunlock(&udev->enum_sx); 2791208008Sthompsa sx_xunlock(&udev->sr_sx); 2792196498Salfred} 2793196498Salfred 2794208008Sthompsa/* The following function locks suspend and resume. */ 2795208008Sthompsa 2796208008Sthompsavoid 2797208008Sthompsausbd_sr_lock(struct usb_device *udev) 2798208008Sthompsa{ 2799208008Sthompsa sx_xlock(&udev->sr_sx); 2800208008Sthompsa /* 2801208008Sthompsa * NEWBUS LOCK NOTE: We should check if any parent SX locks 2802208008Sthompsa * are locked before locking Giant. Else the lock can be 2803208008Sthompsa * locked multiple times. 2804208008Sthompsa */ 2805208008Sthompsa mtx_lock(&Giant); 2806208008Sthompsa} 2807208008Sthompsa 2808208008Sthompsa/* The following function unlocks suspend and resume. */ 2809208008Sthompsa 2810208008Sthompsavoid 2811208008Sthompsausbd_sr_unlock(struct usb_device *udev) 2812208008Sthompsa{ 2813208008Sthompsa mtx_unlock(&Giant); 2814208008Sthompsa sx_xunlock(&udev->sr_sx); 2815208008Sthompsa} 2816208008Sthompsa 2817196498Salfred/* 2818196498Salfred * The following function checks the enumerating lock for the given 2819196498Salfred * USB device. 2820196498Salfred */ 2821196498Salfred 2822196498Salfreduint8_t 2823196498Salfredusbd_enum_is_locked(struct usb_device *udev) 2824196498Salfred{ 2825207079Sthompsa return (sx_xlocked(&udev->enum_sx)); 2826196498Salfred} 2827214429Shselasky 2828214429Shselasky/* 2829305734Shselasky * The following function is used to serialize access to USB control 2830305734Shselasky * transfers and the USB scratch area. If the lock is already grabbed 2831305734Shselasky * this function returns zero. Else a value of one is returned. 2832305734Shselasky */ 2833305734Shselaskyuint8_t 2834305734Shselaskyusbd_ctrl_lock(struct usb_device *udev) 2835305734Shselasky{ 2836305734Shselasky if (sx_xlocked(&udev->ctrl_sx)) 2837305734Shselasky return (0); 2838305734Shselasky sx_xlock(&udev->ctrl_sx); 2839305734Shselasky 2840305734Shselasky /* 2841305734Shselasky * We need to allow suspend and resume at this point, else the 2842305734Shselasky * control transfer will timeout if the device is suspended! 2843305734Shselasky */ 2844305734Shselasky if (usbd_enum_is_locked(udev)) 2845305734Shselasky usbd_sr_unlock(udev); 2846305734Shselasky return (1); 2847305734Shselasky} 2848305734Shselasky 2849305734Shselaskyvoid 2850305734Shselaskyusbd_ctrl_unlock(struct usb_device *udev) 2851305734Shselasky{ 2852305734Shselasky sx_xunlock(&udev->ctrl_sx); 2853305734Shselasky 2854305734Shselasky /* 2855305734Shselasky * Restore the suspend and resume lock after we have unlocked 2856305734Shselasky * the USB control transfer lock to avoid LOR: 2857305734Shselasky */ 2858305734Shselasky if (usbd_enum_is_locked(udev)) 2859305734Shselasky usbd_sr_lock(udev); 2860305734Shselasky} 2861305734Shselasky 2862305734Shselasky/* 2863214429Shselasky * The following function is used to set the per-interface specific 2864214429Shselasky * plug and play information. The string referred to by the pnpinfo 2865214429Shselasky * argument can safely be freed after calling this function. The 2866214429Shselasky * pnpinfo of an interface will be reset at device detach or when 2867214429Shselasky * passing a NULL argument to this function. This function 2868214429Shselasky * returns zero on success, else a USB_ERR_XXX failure code. 2869214429Shselasky */ 2870214429Shselasky 2871214429Shselaskyusb_error_t 2872214429Shselaskyusbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo) 2873214429Shselasky{ 2874214429Shselasky struct usb_interface *iface; 2875214429Shselasky 2876214429Shselasky iface = usbd_get_iface(udev, iface_index); 2877214429Shselasky if (iface == NULL) 2878214429Shselasky return (USB_ERR_INVAL); 2879214429Shselasky 2880214429Shselasky if (iface->pnpinfo != NULL) { 2881214429Shselasky free(iface->pnpinfo, M_USBDEV); 2882214429Shselasky iface->pnpinfo = NULL; 2883214429Shselasky } 2884214429Shselasky 2885214429Shselasky if (pnpinfo == NULL || pnpinfo[0] == 0) 2886214429Shselasky return (0); /* success */ 2887214429Shselasky 2888214429Shselasky iface->pnpinfo = strdup(pnpinfo, M_USBDEV); 2889214429Shselasky if (iface->pnpinfo == NULL) 2890214429Shselasky return (USB_ERR_NOMEM); 2891214429Shselasky 2892214429Shselasky return (0); /* success */ 2893214429Shselasky} 2894214429Shselasky 2895225350Shselaskyusb_error_t 2896225350Shselaskyusbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk) 2897225350Shselasky{ 2898225350Shselasky uint8_t x; 2899225350Shselasky 2900225350Shselasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 2901225469Shselasky if (udev->autoQuirk[x] == 0 || 2902225469Shselasky udev->autoQuirk[x] == quirk) { 2903225350Shselasky udev->autoQuirk[x] = quirk; 2904225350Shselasky return (0); /* success */ 2905225350Shselasky } 2906225350Shselasky } 2907225350Shselasky return (USB_ERR_NOMEM); 2908225350Shselasky} 2909239214Shselasky 2910239214Shselasky/* 2911239214Shselasky * The following function is used to select the endpoint mode. It 2912239214Shselasky * should not be called outside enumeration context. 2913239214Shselasky */ 2914239214Shselasky 2915239214Shselaskyusb_error_t 2916239214Shselaskyusbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep, 2917239214Shselasky uint8_t ep_mode) 2918239214Shselasky{ 2919239214Shselasky usb_error_t error; 2920239240Shselasky uint8_t do_unlock; 2921239214Shselasky 2922246616Shselasky /* Prevent re-enumeration */ 2923246616Shselasky do_unlock = usbd_enum_lock(udev); 2924239214Shselasky 2925239214Shselasky if (udev->bus->methods->set_endpoint_mode != NULL) { 2926239214Shselasky error = (udev->bus->methods->set_endpoint_mode) ( 2927239214Shselasky udev, ep, ep_mode); 2928239214Shselasky } else if (ep_mode != USB_EP_MODE_DEFAULT) { 2929239214Shselasky error = USB_ERR_INVAL; 2930239214Shselasky } else { 2931239214Shselasky error = 0; 2932239214Shselasky } 2933239214Shselasky 2934239214Shselasky /* only set new mode regardless of error */ 2935239214Shselasky ep->ep_mode = ep_mode; 2936239214Shselasky 2937239240Shselasky if (do_unlock) 2938239240Shselasky usbd_enum_unlock(udev); 2939239214Shselasky return (error); 2940239214Shselasky} 2941239214Shselasky 2942239214Shselaskyuint8_t 2943239214Shselaskyusbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep) 2944239214Shselasky{ 2945239214Shselasky return (ep->ep_mode); 2946239214Shselasky} 2947