usb_device.c revision 246122
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_device.c 246122 2013-01-30 15:26:04Z 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); 101194228Sthompsastatic void usbd_clear_stall_proc(struct usb_proc_msg *_pm); 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 115194228Sthompsaint usb_template = 0; 116184610Salfred 117197559SthompsaTUNABLE_INT("hw.usb.usb_template", &usb_template); 118242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW | CTLFLAG_TUN, 119194228Sthompsa &usb_template, 0, "Selected USB device side template"); 120184610Salfred 121197559Sthompsa/* English is default language */ 122197559Sthompsa 123197559Sthompsastatic int usb_lang_id = 0x0009; 124197559Sthompsastatic int usb_lang_mask = 0x00FF; 125197559Sthompsa 126197559SthompsaTUNABLE_INT("hw.usb.usb_lang_id", &usb_lang_id); 127242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RW | CTLFLAG_TUN, 128197559Sthompsa &usb_lang_id, 0, "Preferred USB language ID"); 129197559Sthompsa 130197559SthompsaTUNABLE_INT("hw.usb.usb_lang_mask", &usb_lang_mask); 131242126ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RW | CTLFLAG_TUN, 132197559Sthompsa &usb_lang_mask, 0, "Preferred USB language mask"); 133197559Sthompsa 134191498Sthompsastatic const char* statestr[USB_STATE_MAX] = { 135191498Sthompsa [USB_STATE_DETACHED] = "DETACHED", 136191498Sthompsa [USB_STATE_ATTACHED] = "ATTACHED", 137191498Sthompsa [USB_STATE_POWERED] = "POWERED", 138191498Sthompsa [USB_STATE_ADDRESSED] = "ADDRESSED", 139191498Sthompsa [USB_STATE_CONFIGURED] = "CONFIGURED", 140191498Sthompsa}; 141184610Salfred 142191498Sthompsaconst char * 143194228Sthompsausb_statestr(enum usb_dev_state state) 144191498Sthompsa{ 145191824Sthompsa return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN"); 146191498Sthompsa} 147191498Sthompsa 148212136Sthompsaconst char * 149212136Sthompsausb_get_manufacturer(struct usb_device *udev) 150212136Sthompsa{ 151212136Sthompsa return (udev->manufacturer ? udev->manufacturer : "Unknown"); 152212136Sthompsa} 153212136Sthompsa 154212136Sthompsaconst char * 155212136Sthompsausb_get_product(struct usb_device *udev) 156212136Sthompsa{ 157212136Sthompsa return (udev->product ? udev->product : ""); 158212136Sthompsa} 159212136Sthompsa 160212136Sthompsaconst char * 161212136Sthompsausb_get_serial(struct usb_device *udev) 162212136Sthompsa{ 163212136Sthompsa return (udev->serial ? udev->serial : ""); 164212136Sthompsa} 165212136Sthompsa 166184610Salfred/*------------------------------------------------------------------------* 167194228Sthompsa * usbd_get_ep_by_addr 168184610Salfred * 169193644Sthompsa * This function searches for an USB ep by endpoint address and 170184610Salfred * direction. 171184610Salfred * 172184610Salfred * Returns: 173184610Salfred * NULL: Failure 174184610Salfred * Else: Success 175184610Salfred *------------------------------------------------------------------------*/ 176193644Sthompsastruct usb_endpoint * 177194228Sthompsausbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val) 178184610Salfred{ 179193644Sthompsa struct usb_endpoint *ep = udev->endpoints; 180193644Sthompsa struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 181184610Salfred enum { 182184610Salfred EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 183184610Salfred }; 184184610Salfred 185184610Salfred /* 186184610Salfred * According to the USB specification not all bits are used 187184610Salfred * for the endpoint address. Keep defined bits only: 188184610Salfred */ 189184610Salfred ea_val &= EA_MASK; 190184610Salfred 191184610Salfred /* 192193644Sthompsa * Iterate accross all the USB endpoints searching for a match 193184610Salfred * based on the endpoint address: 194184610Salfred */ 195193644Sthompsa for (; ep != ep_end; ep++) { 196184610Salfred 197193644Sthompsa if (ep->edesc == NULL) { 198184610Salfred continue; 199184610Salfred } 200184610Salfred /* do the mask and check the value */ 201193644Sthompsa if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { 202184610Salfred goto found; 203184610Salfred } 204184610Salfred } 205184610Salfred 206184610Salfred /* 207193644Sthompsa * The default endpoint is always present and is checked separately: 208184610Salfred */ 209239177Shselasky if ((udev->ctrl_ep.edesc != NULL) && 210207080Sthompsa ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 211207080Sthompsa ep = &udev->ctrl_ep; 212184610Salfred goto found; 213184610Salfred } 214184610Salfred return (NULL); 215184610Salfred 216184610Salfredfound: 217193644Sthompsa return (ep); 218184610Salfred} 219184610Salfred 220184610Salfred/*------------------------------------------------------------------------* 221194228Sthompsa * usbd_get_endpoint 222184610Salfred * 223193644Sthompsa * This function searches for an USB endpoint based on the information 224192984Sthompsa * given by the passed "struct usb_config" pointer. 225184610Salfred * 226184610Salfred * Return values: 227184610Salfred * NULL: No match. 228193644Sthompsa * Else: Pointer to "struct usb_endpoint". 229184610Salfred *------------------------------------------------------------------------*/ 230193644Sthompsastruct usb_endpoint * 231194228Sthompsausbd_get_endpoint(struct usb_device *udev, uint8_t iface_index, 232192984Sthompsa const struct usb_config *setup) 233184610Salfred{ 234193644Sthompsa struct usb_endpoint *ep = udev->endpoints; 235193644Sthompsa struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 236184610Salfred uint8_t index = setup->ep_index; 237184610Salfred uint8_t ea_mask; 238184610Salfred uint8_t ea_val; 239184610Salfred uint8_t type_mask; 240184610Salfred uint8_t type_val; 241184610Salfred 242184610Salfred DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 243184610Salfred "type=0x%x dir=0x%x index=%d\n", 244184610Salfred udev, iface_index, setup->endpoint, 245184610Salfred setup->type, setup->direction, setup->ep_index); 246184610Salfred 247190734Sthompsa /* check USB mode */ 248190734Sthompsa 249192499Sthompsa if (setup->usb_mode != USB_MODE_DUAL && 250192499Sthompsa udev->flags.usb_mode != setup->usb_mode) { 251193644Sthompsa /* wrong mode - no endpoint */ 252190734Sthompsa return (NULL); 253190734Sthompsa } 254190734Sthompsa 255184610Salfred /* setup expected endpoint direction mask and value */ 256184610Salfred 257190732Sthompsa if (setup->direction == UE_DIR_RX) { 258190732Sthompsa ea_mask = (UE_DIR_IN | UE_DIR_OUT); 259192499Sthompsa ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 260190732Sthompsa UE_DIR_OUT : UE_DIR_IN; 261190732Sthompsa } else if (setup->direction == UE_DIR_TX) { 262190732Sthompsa ea_mask = (UE_DIR_IN | UE_DIR_OUT); 263192499Sthompsa ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 264190732Sthompsa UE_DIR_IN : UE_DIR_OUT; 265190732Sthompsa } else if (setup->direction == UE_DIR_ANY) { 266184610Salfred /* match any endpoint direction */ 267184610Salfred ea_mask = 0; 268184610Salfred ea_val = 0; 269184610Salfred } else { 270184610Salfred /* match the given endpoint direction */ 271184610Salfred ea_mask = (UE_DIR_IN | UE_DIR_OUT); 272184610Salfred ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 273184610Salfred } 274184610Salfred 275184610Salfred /* setup expected endpoint address */ 276184610Salfred 277184610Salfred if (setup->endpoint == UE_ADDR_ANY) { 278184610Salfred /* match any endpoint address */ 279184610Salfred } else { 280184610Salfred /* match the given endpoint address */ 281184610Salfred ea_mask |= UE_ADDR; 282184610Salfred ea_val |= (setup->endpoint & UE_ADDR); 283184610Salfred } 284184610Salfred 285184610Salfred /* setup expected endpoint type */ 286184610Salfred 287184610Salfred if (setup->type == UE_BULK_INTR) { 288184610Salfred /* this will match BULK and INTERRUPT endpoints */ 289184610Salfred type_mask = 2; 290184610Salfred type_val = 2; 291184610Salfred } else if (setup->type == UE_TYPE_ANY) { 292184610Salfred /* match any endpoint type */ 293184610Salfred type_mask = 0; 294184610Salfred type_val = 0; 295184610Salfred } else { 296184610Salfred /* match the given endpoint type */ 297184610Salfred type_mask = UE_XFERTYPE; 298184610Salfred type_val = (setup->type & UE_XFERTYPE); 299184610Salfred } 300184610Salfred 301184610Salfred /* 302193644Sthompsa * Iterate accross all the USB endpoints searching for a match 303184610Salfred * based on the endpoint address. Note that we are searching 304193644Sthompsa * the endpoints from the beginning of the "udev->endpoints" array. 305184610Salfred */ 306193644Sthompsa for (; ep != ep_end; ep++) { 307184610Salfred 308193644Sthompsa if ((ep->edesc == NULL) || 309193644Sthompsa (ep->iface_index != iface_index)) { 310184610Salfred continue; 311184610Salfred } 312184610Salfred /* do the masks and check the values */ 313184610Salfred 314193644Sthompsa if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && 315193644Sthompsa ((ep->edesc->bmAttributes & type_mask) == type_val)) { 316184610Salfred if (!index--) { 317184610Salfred goto found; 318184610Salfred } 319184610Salfred } 320184610Salfred } 321184610Salfred 322184610Salfred /* 323193644Sthompsa * Match against default endpoint last, so that "any endpoint", "any 324193644Sthompsa * address" and "any direction" returns the first endpoint of the 325184610Salfred * interface. "iface_index" and "direction" is ignored: 326184610Salfred */ 327239177Shselasky if ((udev->ctrl_ep.edesc != NULL) && 328207080Sthompsa ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && 329207080Sthompsa ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && 330184610Salfred (!index)) { 331207080Sthompsa ep = &udev->ctrl_ep; 332184610Salfred goto found; 333184610Salfred } 334184610Salfred return (NULL); 335184610Salfred 336184610Salfredfound: 337193644Sthompsa return (ep); 338184610Salfred} 339184610Salfred 340184610Salfred/*------------------------------------------------------------------------* 341194677Sthompsa * usbd_interface_count 342184610Salfred * 343184610Salfred * This function stores the number of USB interfaces excluding 344184610Salfred * alternate settings, which the USB config descriptor reports into 345184610Salfred * the unsigned 8-bit integer pointed to by "count". 346184610Salfred * 347184610Salfred * Returns: 348184610Salfred * 0: Success 349184610Salfred * Else: Failure 350184610Salfred *------------------------------------------------------------------------*/ 351193045Sthompsausb_error_t 352194677Sthompsausbd_interface_count(struct usb_device *udev, uint8_t *count) 353184610Salfred{ 354184610Salfred if (udev->cdesc == NULL) { 355184610Salfred *count = 0; 356184610Salfred return (USB_ERR_NOT_CONFIGURED); 357184610Salfred } 358190730Sthompsa *count = udev->ifaces_max; 359184610Salfred return (USB_ERR_NORMAL_COMPLETION); 360184610Salfred} 361184610Salfred 362184610Salfred/*------------------------------------------------------------------------* 363194228Sthompsa * usb_init_endpoint 364184610Salfred * 365193644Sthompsa * This function will initialise the USB endpoint structure pointed to by 366193644Sthompsa * the "endpoint" argument. The structure pointed to by "endpoint" must be 367190730Sthompsa * zeroed before calling this function. 368184610Salfred *------------------------------------------------------------------------*/ 369184610Salfredstatic void 370194228Sthompsausb_init_endpoint(struct usb_device *udev, uint8_t iface_index, 371213435Shselasky struct usb_endpoint_descriptor *edesc, 372213435Shselasky struct usb_endpoint_ss_comp_descriptor *ecomp, 373213435Shselasky struct usb_endpoint *ep) 374184610Salfred{ 375192984Sthompsa struct usb_bus_methods *methods; 376239214Shselasky usb_stream_t x; 377189110Sthompsa 378190730Sthompsa methods = udev->bus->methods; 379184610Salfred 380193644Sthompsa (methods->endpoint_init) (udev, edesc, ep); 381184610Salfred 382193644Sthompsa /* initialise USB endpoint structure */ 383193644Sthompsa ep->edesc = edesc; 384213435Shselasky ep->ecomp = ecomp; 385193644Sthompsa ep->iface_index = iface_index; 386184610Salfred 387239214Shselasky /* setup USB stream queues */ 388239214Shselasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 389239214Shselasky TAILQ_INIT(&ep->endpoint_q[x].head); 390239214Shselasky ep->endpoint_q[x].command = &usbd_pipe_start; 391239214Shselasky } 392239214Shselasky 393190735Sthompsa /* the pipe is not supported by the hardware */ 394193644Sthompsa if (ep->methods == NULL) 395190735Sthompsa return; 396190735Sthompsa 397239214Shselasky /* check for SUPER-speed streams mode endpoint */ 398239214Shselasky if (udev->speed == USB_SPEED_SUPER && ecomp != NULL && 399239214Shselasky (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK && 400239214Shselasky (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) { 401239214Shselasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS); 402239214Shselasky } else { 403239214Shselasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT); 404239214Shselasky } 405239214Shselasky 406184610Salfred /* clear stall, if any */ 407190730Sthompsa if (methods->clear_stall != NULL) { 408184824Sthompsa USB_BUS_LOCK(udev->bus); 409193644Sthompsa (methods->clear_stall) (udev, ep); 410184824Sthompsa USB_BUS_UNLOCK(udev->bus); 411184610Salfred } 412184610Salfred} 413184610Salfred 414190730Sthompsa/*-----------------------------------------------------------------------* 415194228Sthompsa * usb_endpoint_foreach 416188985Sthompsa * 417188985Sthompsa * This function will iterate all the USB endpoints except the control 418188985Sthompsa * endpoint. This function is NULL safe. 419188985Sthompsa * 420188985Sthompsa * Return values: 421193644Sthompsa * NULL: End of USB endpoints 422193644Sthompsa * Else: Pointer to next USB endpoint 423188985Sthompsa *------------------------------------------------------------------------*/ 424193644Sthompsastruct usb_endpoint * 425194228Sthompsausb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep) 426188985Sthompsa{ 427197558Sthompsa struct usb_endpoint *ep_end; 428188985Sthompsa 429188985Sthompsa /* be NULL safe */ 430188985Sthompsa if (udev == NULL) 431188985Sthompsa return (NULL); 432188985Sthompsa 433197558Sthompsa ep_end = udev->endpoints + udev->endpoints_max; 434197558Sthompsa 435193644Sthompsa /* get next endpoint */ 436193644Sthompsa if (ep == NULL) 437193644Sthompsa ep = udev->endpoints; 438188985Sthompsa else 439193644Sthompsa ep++; 440188985Sthompsa 441193644Sthompsa /* find next allocated ep */ 442193644Sthompsa while (ep != ep_end) { 443193644Sthompsa if (ep->edesc != NULL) 444193644Sthompsa return (ep); 445193644Sthompsa ep++; 446188985Sthompsa } 447188985Sthompsa return (NULL); 448188985Sthompsa} 449188985Sthompsa 450188985Sthompsa/*------------------------------------------------------------------------* 451194228Sthompsa * usb_unconfigure 452184610Salfred * 453193644Sthompsa * This function will free all USB interfaces and USB endpoints belonging 454190730Sthompsa * to an USB device. 455190730Sthompsa * 456190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX". 457184610Salfred *------------------------------------------------------------------------*/ 458190730Sthompsastatic void 459194228Sthompsausb_unconfigure(struct usb_device *udev, uint8_t flag) 460184610Salfred{ 461190730Sthompsa uint8_t do_unlock; 462184610Salfred 463190730Sthompsa /* automatic locking */ 464196498Salfred if (usbd_enum_is_locked(udev)) { 465190730Sthompsa do_unlock = 0; 466190730Sthompsa } else { 467190730Sthompsa do_unlock = 1; 468196498Salfred usbd_enum_lock(udev); 469184610Salfred } 470184610Salfred 471190730Sthompsa /* detach all interface drivers */ 472194228Sthompsa usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag); 473184610Salfred 474190730Sthompsa#if USB_HAVE_UGEN 475190730Sthompsa /* free all FIFOs except control endpoint FIFOs */ 476194228Sthompsa usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag); 477184610Salfred 478184610Salfred /* 479190730Sthompsa * Free all cdev's, if any. 480184610Salfred */ 481194228Sthompsa usb_cdev_free(udev); 482190730Sthompsa#endif 483184610Salfred 484190180Sthompsa#if USB_HAVE_COMPAT_LINUX 485184610Salfred /* free Linux compat device, if any */ 486192984Sthompsa if (udev->linux_endpoint_start) { 487192984Sthompsa usb_linux_free_device(udev); 488192984Sthompsa udev->linux_endpoint_start = NULL; 489184610Salfred } 490190180Sthompsa#endif 491184610Salfred 492194228Sthompsa usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE); 493184610Salfred 494193644Sthompsa /* free "cdesc" after "ifaces" and "endpoints", if any */ 495190727Sthompsa if (udev->cdesc != NULL) { 496192499Sthompsa if (udev->flags.usb_mode != USB_MODE_DEVICE) 497190727Sthompsa free(udev->cdesc, M_USB); 498184610Salfred udev->cdesc = NULL; 499184610Salfred } 500184610Salfred /* set unconfigured state */ 501184610Salfred udev->curr_config_no = USB_UNCONFIG_NO; 502184610Salfred udev->curr_config_index = USB_UNCONFIG_INDEX; 503190730Sthompsa 504196498Salfred if (do_unlock) 505196498Salfred usbd_enum_unlock(udev); 506184610Salfred} 507184610Salfred 508184610Salfred/*------------------------------------------------------------------------* 509194228Sthompsa * usbd_set_config_index 510184610Salfred * 511184610Salfred * This function selects configuration by index, independent of the 512184610Salfred * actual configuration number. This function should not be used by 513184610Salfred * USB drivers. 514184610Salfred * 515184610Salfred * Returns: 516184610Salfred * 0: Success 517184610Salfred * Else: Failure 518184610Salfred *------------------------------------------------------------------------*/ 519193045Sthompsausb_error_t 520194228Sthompsausbd_set_config_index(struct usb_device *udev, uint8_t index) 521184610Salfred{ 522192984Sthompsa struct usb_status ds; 523192984Sthompsa struct usb_config_descriptor *cdp; 524184610Salfred uint16_t power; 525184610Salfred uint16_t max_power; 526184610Salfred uint8_t selfpowered; 527184610Salfred uint8_t do_unlock; 528193045Sthompsa usb_error_t err; 529184610Salfred 530184610Salfred DPRINTFN(6, "udev=%p index=%d\n", udev, index); 531184610Salfred 532184610Salfred /* automatic locking */ 533196498Salfred if (usbd_enum_is_locked(udev)) { 534184610Salfred do_unlock = 0; 535184610Salfred } else { 536184610Salfred do_unlock = 1; 537196498Salfred usbd_enum_lock(udev); 538184610Salfred } 539184610Salfred 540197553Sthompsa usb_unconfigure(udev, 0); 541184610Salfred 542184610Salfred if (index == USB_UNCONFIG_INDEX) { 543184610Salfred /* 544184610Salfred * Leave unallocated when unconfiguring the 545194228Sthompsa * device. "usb_unconfigure()" will also reset 546184610Salfred * the current config number and index. 547184610Salfred */ 548194228Sthompsa err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO); 549191494Sthompsa if (udev->state == USB_STATE_CONFIGURED) 550194228Sthompsa usb_set_device_state(udev, USB_STATE_ADDRESSED); 551184610Salfred goto done; 552184610Salfred } 553184610Salfred /* get the full config descriptor */ 554192499Sthompsa if (udev->flags.usb_mode == USB_MODE_DEVICE) { 555190727Sthompsa /* save some memory */ 556194228Sthompsa err = usbd_req_get_descriptor_ptr(udev, &cdp, 557191402Sthompsa (UDESC_CONFIG << 8) | index); 558190727Sthompsa } else { 559190727Sthompsa /* normal request */ 560194228Sthompsa err = usbd_req_get_config_desc_full(udev, 561190727Sthompsa NULL, &cdp, M_USB, index); 562190727Sthompsa } 563184610Salfred if (err) { 564184610Salfred goto done; 565184610Salfred } 566184610Salfred /* set the new config descriptor */ 567184610Salfred 568184610Salfred udev->cdesc = cdp; 569184610Salfred 570184610Salfred /* Figure out if the device is self or bus powered. */ 571184610Salfred selfpowered = 0; 572184610Salfred if ((!udev->flags.uq_bus_powered) && 573184610Salfred (cdp->bmAttributes & UC_SELF_POWERED) && 574192499Sthompsa (udev->flags.usb_mode == USB_MODE_HOST)) { 575184610Salfred /* May be self powered. */ 576184610Salfred if (cdp->bmAttributes & UC_BUS_POWERED) { 577184610Salfred /* Must ask device. */ 578194228Sthompsa err = usbd_req_get_device_status(udev, NULL, &ds); 579190743Sthompsa if (err) { 580190743Sthompsa DPRINTFN(0, "could not read " 581190743Sthompsa "device status: %s\n", 582194228Sthompsa usbd_errstr(err)); 583190743Sthompsa } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 584190743Sthompsa selfpowered = 1; 585184610Salfred } 586190743Sthompsa DPRINTF("status=0x%04x \n", 587190743Sthompsa UGETW(ds.wStatus)); 588184610Salfred } else 589184610Salfred selfpowered = 1; 590184610Salfred } 591184610Salfred DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 592184610Salfred "selfpowered=%d, power=%d\n", 593184610Salfred udev, cdp, 594190328Sthompsa udev->address, cdp->bConfigurationValue, cdp->bmAttributes, 595184610Salfred selfpowered, cdp->bMaxPower * 2); 596184610Salfred 597184610Salfred /* Check if we have enough power. */ 598184610Salfred power = cdp->bMaxPower * 2; 599184610Salfred 600184610Salfred if (udev->parent_hub) { 601184610Salfred max_power = udev->parent_hub->hub->portpower; 602184610Salfred } else { 603184610Salfred max_power = USB_MAX_POWER; 604184610Salfred } 605184610Salfred 606184610Salfred if (power > max_power) { 607184610Salfred DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 608184610Salfred err = USB_ERR_NO_POWER; 609184610Salfred goto done; 610184610Salfred } 611184610Salfred /* Only update "self_powered" in USB Host Mode */ 612192499Sthompsa if (udev->flags.usb_mode == USB_MODE_HOST) { 613184610Salfred udev->flags.self_powered = selfpowered; 614184610Salfred } 615184610Salfred udev->power = power; 616184610Salfred udev->curr_config_no = cdp->bConfigurationValue; 617184610Salfred udev->curr_config_index = index; 618194228Sthompsa usb_set_device_state(udev, USB_STATE_CONFIGURED); 619184610Salfred 620184610Salfred /* Set the actual configuration value. */ 621194228Sthompsa err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); 622184610Salfred if (err) { 623184610Salfred goto done; 624184610Salfred } 625190730Sthompsa 626194228Sthompsa err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC); 627190730Sthompsa if (err) { 628190730Sthompsa goto done; 629184610Salfred } 630190730Sthompsa 631194228Sthompsa err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT); 632190730Sthompsa if (err) { 633190730Sthompsa goto done; 634190730Sthompsa } 635190730Sthompsa 636189599Sthompsa#if USB_HAVE_UGEN 637189110Sthompsa /* create device nodes for each endpoint */ 638194228Sthompsa usb_cdev_create(udev); 639189599Sthompsa#endif 640184610Salfred 641184610Salfreddone: 642194228Sthompsa DPRINTF("error=%s\n", usbd_errstr(err)); 643184610Salfred if (err) { 644197553Sthompsa usb_unconfigure(udev, 0); 645184610Salfred } 646196498Salfred if (do_unlock) 647196498Salfred usbd_enum_unlock(udev); 648184610Salfred return (err); 649184610Salfred} 650184610Salfred 651184610Salfred/*------------------------------------------------------------------------* 652194228Sthompsa * usb_config_parse 653190730Sthompsa * 654193644Sthompsa * This function will allocate and free USB interfaces and USB endpoints, 655193644Sthompsa * parse the USB configuration structure and initialise the USB endpoints 656190730Sthompsa * and interfaces. If "iface_index" is not equal to 657190730Sthompsa * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the 658190730Sthompsa * alternate_setting to be selected for the given interface. Else the 659190730Sthompsa * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be 660190730Sthompsa * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function 661190730Sthompsa * is typically called when setting the configuration or when setting 662190730Sthompsa * an alternate interface. 663190730Sthompsa * 664190730Sthompsa * Returns: 665190730Sthompsa * 0: Success 666190730Sthompsa * Else: Failure 667190730Sthompsa *------------------------------------------------------------------------*/ 668213435Shselaskystatic usb_error_t 669194228Sthompsausb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd) 670190730Sthompsa{ 671192984Sthompsa struct usb_idesc_parse_state ips; 672192984Sthompsa struct usb_interface_descriptor *id; 673192984Sthompsa struct usb_endpoint_descriptor *ed; 674192984Sthompsa struct usb_interface *iface; 675193644Sthompsa struct usb_endpoint *ep; 676193045Sthompsa usb_error_t err; 677190730Sthompsa uint8_t ep_curr; 678190730Sthompsa uint8_t ep_max; 679190730Sthompsa uint8_t temp; 680190730Sthompsa uint8_t do_init; 681190730Sthompsa uint8_t alt_index; 682190730Sthompsa 683190730Sthompsa if (iface_index != USB_IFACE_INDEX_ANY) { 684190730Sthompsa /* parameter overload */ 685190730Sthompsa alt_index = cmd; 686190730Sthompsa cmd = USB_CFG_INIT; 687190730Sthompsa } else { 688190730Sthompsa /* not used */ 689190730Sthompsa alt_index = 0; 690190730Sthompsa } 691190730Sthompsa 692190730Sthompsa err = 0; 693190730Sthompsa 694190730Sthompsa DPRINTFN(5, "iface_index=%d cmd=%d\n", 695190730Sthompsa iface_index, cmd); 696190730Sthompsa 697190730Sthompsa if (cmd == USB_CFG_FREE) 698190730Sthompsa goto cleanup; 699190730Sthompsa 700190730Sthompsa if (cmd == USB_CFG_INIT) { 701207079Sthompsa sx_assert(&udev->enum_sx, SA_LOCKED); 702190730Sthompsa 703193644Sthompsa /* check for in-use endpoints */ 704190730Sthompsa 705193644Sthompsa ep = udev->endpoints; 706193644Sthompsa ep_max = udev->endpoints_max; 707190730Sthompsa while (ep_max--) { 708193644Sthompsa /* look for matching endpoints */ 709190730Sthompsa if ((iface_index == USB_IFACE_INDEX_ANY) || 710193644Sthompsa (iface_index == ep->iface_index)) { 711199672Sthompsa if (ep->refcount_alloc != 0) { 712190730Sthompsa /* 713190730Sthompsa * This typically indicates a 714190730Sthompsa * more serious error. 715190730Sthompsa */ 716190730Sthompsa err = USB_ERR_IN_USE; 717190730Sthompsa } else { 718193644Sthompsa /* reset endpoint */ 719193644Sthompsa memset(ep, 0, sizeof(*ep)); 720193644Sthompsa /* make sure we don't zero the endpoint again */ 721193644Sthompsa ep->iface_index = USB_IFACE_INDEX_ANY; 722190730Sthompsa } 723190730Sthompsa } 724193644Sthompsa ep++; 725190730Sthompsa } 726190730Sthompsa 727190730Sthompsa if (err) 728190730Sthompsa return (err); 729190730Sthompsa } 730190730Sthompsa 731190730Sthompsa memset(&ips, 0, sizeof(ips)); 732190730Sthompsa 733190730Sthompsa ep_curr = 0; 734190730Sthompsa ep_max = 0; 735190730Sthompsa 736194228Sthompsa while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { 737190730Sthompsa 738190730Sthompsa /* check for interface overflow */ 739190730Sthompsa if (ips.iface_index == USB_IFACE_MAX) 740190730Sthompsa break; /* crazy */ 741190730Sthompsa 742190730Sthompsa iface = udev->ifaces + ips.iface_index; 743190730Sthompsa 744190730Sthompsa /* check for specific interface match */ 745190730Sthompsa 746190730Sthompsa if (cmd == USB_CFG_INIT) { 747190730Sthompsa if ((iface_index != USB_IFACE_INDEX_ANY) && 748190730Sthompsa (iface_index != ips.iface_index)) { 749190730Sthompsa /* wrong interface */ 750190730Sthompsa do_init = 0; 751190730Sthompsa } else if (alt_index != ips.iface_index_alt) { 752190730Sthompsa /* wrong alternate setting */ 753190730Sthompsa do_init = 0; 754190730Sthompsa } else { 755190730Sthompsa /* initialise interface */ 756190730Sthompsa do_init = 1; 757190730Sthompsa } 758190730Sthompsa } else 759190730Sthompsa do_init = 0; 760190730Sthompsa 761190730Sthompsa /* check for new interface */ 762190730Sthompsa if (ips.iface_index_alt == 0) { 763190730Sthompsa /* update current number of endpoints */ 764190730Sthompsa ep_curr = ep_max; 765190730Sthompsa } 766190730Sthompsa /* check for init */ 767190730Sthompsa if (do_init) { 768190730Sthompsa /* setup the USB interface structure */ 769190730Sthompsa iface->idesc = id; 770190730Sthompsa /* set alternate index */ 771190730Sthompsa iface->alt_index = alt_index; 772236407Shselasky /* set default interface parent */ 773236407Shselasky if (iface_index == USB_IFACE_INDEX_ANY) { 774236407Shselasky iface->parent_iface_index = 775236407Shselasky USB_IFACE_INDEX_ANY; 776236407Shselasky } 777190730Sthompsa } 778190730Sthompsa 779190730Sthompsa DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); 780190730Sthompsa 781192984Sthompsa ed = (struct usb_endpoint_descriptor *)id; 782190730Sthompsa 783190730Sthompsa temp = ep_curr; 784190730Sthompsa 785190730Sthompsa /* iterate all the endpoint descriptors */ 786194228Sthompsa while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { 787190730Sthompsa 788190730Sthompsa if (temp == USB_EP_MAX) 789190730Sthompsa break; /* crazy */ 790190730Sthompsa 791193644Sthompsa ep = udev->endpoints + temp; 792190730Sthompsa 793190730Sthompsa if (do_init) { 794213435Shselasky void *ecomp; 795213435Shselasky 796213435Shselasky ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed); 797213435Shselasky if (ecomp != NULL) 798213435Shselasky DPRINTFN(5, "Found endpoint companion descriptor\n"); 799213435Shselasky 800194228Sthompsa usb_init_endpoint(udev, 801213435Shselasky ips.iface_index, ed, ecomp, ep); 802190730Sthompsa } 803190730Sthompsa 804190730Sthompsa temp ++; 805190730Sthompsa 806190730Sthompsa /* find maximum number of endpoints */ 807190730Sthompsa if (ep_max < temp) 808190730Sthompsa ep_max = temp; 809190730Sthompsa 810190730Sthompsa /* optimalisation */ 811192984Sthompsa id = (struct usb_interface_descriptor *)ed; 812190730Sthompsa } 813190730Sthompsa } 814190730Sthompsa 815190730Sthompsa /* NOTE: It is valid to have no interfaces and no endpoints! */ 816190730Sthompsa 817190730Sthompsa if (cmd == USB_CFG_ALLOC) { 818190730Sthompsa udev->ifaces_max = ips.iface_index; 819190730Sthompsa udev->ifaces = NULL; 820190730Sthompsa if (udev->ifaces_max != 0) { 821190730Sthompsa udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max, 822190730Sthompsa M_USB, M_WAITOK | M_ZERO); 823190730Sthompsa if (udev->ifaces == NULL) { 824190730Sthompsa err = USB_ERR_NOMEM; 825190730Sthompsa goto done; 826190730Sthompsa } 827190730Sthompsa } 828191398Sthompsa if (ep_max != 0) { 829193644Sthompsa udev->endpoints = malloc(sizeof(*ep) * ep_max, 830190730Sthompsa M_USB, M_WAITOK | M_ZERO); 831193644Sthompsa if (udev->endpoints == NULL) { 832190730Sthompsa err = USB_ERR_NOMEM; 833190730Sthompsa goto done; 834190730Sthompsa } 835191398Sthompsa } else { 836193644Sthompsa udev->endpoints = NULL; 837190730Sthompsa } 838191398Sthompsa USB_BUS_LOCK(udev->bus); 839193644Sthompsa udev->endpoints_max = ep_max; 840191398Sthompsa /* reset any ongoing clear-stall */ 841193644Sthompsa udev->ep_curr = NULL; 842191398Sthompsa USB_BUS_UNLOCK(udev->bus); 843190730Sthompsa } 844190730Sthompsa 845190730Sthompsadone: 846190730Sthompsa if (err) { 847190730Sthompsa if (cmd == USB_CFG_ALLOC) { 848190730Sthompsacleanup: 849191398Sthompsa USB_BUS_LOCK(udev->bus); 850193644Sthompsa udev->endpoints_max = 0; 851191398Sthompsa /* reset any ongoing clear-stall */ 852193644Sthompsa udev->ep_curr = NULL; 853191398Sthompsa USB_BUS_UNLOCK(udev->bus); 854191398Sthompsa 855190730Sthompsa /* cleanup */ 856190730Sthompsa if (udev->ifaces != NULL) 857190730Sthompsa free(udev->ifaces, M_USB); 858193644Sthompsa if (udev->endpoints != NULL) 859193644Sthompsa free(udev->endpoints, M_USB); 860190730Sthompsa 861190730Sthompsa udev->ifaces = NULL; 862193644Sthompsa udev->endpoints = NULL; 863190730Sthompsa udev->ifaces_max = 0; 864190730Sthompsa } 865190730Sthompsa } 866190730Sthompsa return (err); 867190730Sthompsa} 868190730Sthompsa 869190730Sthompsa/*------------------------------------------------------------------------* 870194228Sthompsa * usbd_set_alt_interface_index 871184610Salfred * 872184610Salfred * This function will select an alternate interface index for the 873184610Salfred * given interface index. The interface should not be in use when this 874188986Sthompsa * function is called. That means there should not be any open USB 875188986Sthompsa * transfers. Else an error is returned. If the alternate setting is 876188986Sthompsa * already set this function will simply return success. This function 877188986Sthompsa * is called in Host mode and Device mode! 878184610Salfred * 879184610Salfred * Returns: 880184610Salfred * 0: Success 881184610Salfred * Else: Failure 882184610Salfred *------------------------------------------------------------------------*/ 883193045Sthompsausb_error_t 884194228Sthompsausbd_set_alt_interface_index(struct usb_device *udev, 885184610Salfred uint8_t iface_index, uint8_t alt_index) 886184610Salfred{ 887194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 888193045Sthompsa usb_error_t err; 889184610Salfred uint8_t do_unlock; 890184610Salfred 891184610Salfred /* automatic locking */ 892196498Salfred if (usbd_enum_is_locked(udev)) { 893184610Salfred do_unlock = 0; 894184610Salfred } else { 895184610Salfred do_unlock = 1; 896196498Salfred usbd_enum_lock(udev); 897184610Salfred } 898184610Salfred if (iface == NULL) { 899184610Salfred err = USB_ERR_INVAL; 900184610Salfred goto done; 901184610Salfred } 902195963Salfred if (iface->alt_index == alt_index) { 903195963Salfred /* 904195963Salfred * Optimise away duplicate setting of 905195963Salfred * alternate setting in USB Host Mode! 906195963Salfred */ 907195963Salfred err = 0; 908195963Salfred goto done; 909184610Salfred } 910189599Sthompsa#if USB_HAVE_UGEN 911185087Salfred /* 912185087Salfred * Free all generic FIFOs for this interface, except control 913185087Salfred * endpoint FIFOs: 914185087Salfred */ 915194228Sthompsa usb_fifo_free_wrap(udev, iface_index, 0); 916189599Sthompsa#endif 917190730Sthompsa 918194228Sthompsa err = usb_config_parse(udev, iface_index, alt_index); 919184610Salfred if (err) { 920184610Salfred goto done; 921184610Salfred } 922195963Salfred if (iface->alt_index != alt_index) { 923195963Salfred /* the alternate setting does not exist */ 924195963Salfred err = USB_ERR_INVAL; 925195963Salfred goto done; 926195963Salfred } 927195963Salfred 928194228Sthompsa err = usbd_req_set_alt_interface_no(udev, NULL, iface_index, 929184610Salfred iface->idesc->bAlternateSetting); 930184610Salfred 931184610Salfreddone: 932196498Salfred if (do_unlock) 933196498Salfred usbd_enum_unlock(udev); 934196498Salfred 935184610Salfred return (err); 936184610Salfred} 937184610Salfred 938184610Salfred/*------------------------------------------------------------------------* 939194228Sthompsa * usbd_set_endpoint_stall 940184610Salfred * 941213435Shselasky * This function is used to make a BULK or INTERRUPT endpoint send 942213435Shselasky * STALL tokens in USB device mode. 943184610Salfred * 944184610Salfred * Returns: 945184610Salfred * 0: Success 946184610Salfred * Else: Failure 947184610Salfred *------------------------------------------------------------------------*/ 948193045Sthompsausb_error_t 949194228Sthompsausbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep, 950184610Salfred uint8_t do_stall) 951184610Salfred{ 952192984Sthompsa struct usb_xfer *xfer; 953239214Shselasky usb_stream_t x; 954184610Salfred uint8_t et; 955184610Salfred uint8_t was_stalled; 956184610Salfred 957193644Sthompsa if (ep == NULL) { 958184610Salfred /* nothing to do */ 959184610Salfred DPRINTF("Cannot find endpoint\n"); 960184610Salfred /* 961184610Salfred * Pretend that the clear or set stall request is 962184610Salfred * successful else some USB host stacks can do 963184610Salfred * strange things, especially when a control endpoint 964184610Salfred * stalls. 965184610Salfred */ 966184610Salfred return (0); 967184610Salfred } 968193644Sthompsa et = (ep->edesc->bmAttributes & UE_XFERTYPE); 969184610Salfred 970184610Salfred if ((et != UE_BULK) && 971184610Salfred (et != UE_INTERRUPT)) { 972184610Salfred /* 973184610Salfred * Should not stall control 974184610Salfred * nor isochronous endpoints. 975184610Salfred */ 976184610Salfred DPRINTF("Invalid endpoint\n"); 977184610Salfred return (0); 978184610Salfred } 979184824Sthompsa USB_BUS_LOCK(udev->bus); 980184610Salfred 981184610Salfred /* store current stall state */ 982193644Sthompsa was_stalled = ep->is_stalled; 983184610Salfred 984184610Salfred /* check for no change */ 985184610Salfred if (was_stalled && do_stall) { 986193644Sthompsa /* if the endpoint is already stalled do nothing */ 987184824Sthompsa USB_BUS_UNLOCK(udev->bus); 988184610Salfred DPRINTF("No change\n"); 989184610Salfred return (0); 990184610Salfred } 991184610Salfred /* set stalled state */ 992193644Sthompsa ep->is_stalled = 1; 993184610Salfred 994184610Salfred if (do_stall || (!was_stalled)) { 995184610Salfred if (!was_stalled) { 996239214Shselasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 997239214Shselasky /* lookup the current USB transfer, if any */ 998239214Shselasky xfer = ep->endpoint_q[x].curr; 999239214Shselasky if (xfer != NULL) { 1000239214Shselasky /* 1001239214Shselasky * The "xfer_stall" method 1002239214Shselasky * will complete the USB 1003239214Shselasky * transfer like in case of a 1004239214Shselasky * timeout setting the error 1005239214Shselasky * code "USB_ERR_STALLED". 1006239214Shselasky */ 1007239214Shselasky (udev->bus->methods->xfer_stall) (xfer); 1008239214Shselasky } 1009239214Shselasky } 1010184610Salfred } 1011239214Shselasky (udev->bus->methods->set_stall) (udev, ep, &do_stall); 1012184610Salfred } 1013184610Salfred if (!do_stall) { 1014193644Sthompsa ep->toggle_next = 0; /* reset data toggle */ 1015193644Sthompsa ep->is_stalled = 0; /* clear stalled state */ 1016184610Salfred 1017193644Sthompsa (udev->bus->methods->clear_stall) (udev, ep); 1018184610Salfred 1019239214Shselasky /* start the current or next transfer, if any */ 1020239214Shselasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 1021239214Shselasky usb_command_wrapper(&ep->endpoint_q[x], 1022239214Shselasky ep->endpoint_q[x].curr); 1023239214Shselasky } 1024184610Salfred } 1025184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1026184610Salfred return (0); 1027184610Salfred} 1028184610Salfred 1029184610Salfred/*------------------------------------------------------------------------* 1030194228Sthompsa * usb_reset_iface_endpoints - used in USB device side mode 1031184610Salfred *------------------------------------------------------------------------*/ 1032193045Sthompsausb_error_t 1033194228Sthompsausb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index) 1034184610Salfred{ 1035193644Sthompsa struct usb_endpoint *ep; 1036193644Sthompsa struct usb_endpoint *ep_end; 1037184610Salfred 1038193644Sthompsa ep = udev->endpoints; 1039193644Sthompsa ep_end = udev->endpoints + udev->endpoints_max; 1040184610Salfred 1041193644Sthompsa for (; ep != ep_end; ep++) { 1042184610Salfred 1043193644Sthompsa if ((ep->edesc == NULL) || 1044193644Sthompsa (ep->iface_index != iface_index)) { 1045184610Salfred continue; 1046184610Salfred } 1047184610Salfred /* simulate a clear stall from the peer */ 1048195963Salfred usbd_set_endpoint_stall(udev, ep, 0); 1049184610Salfred } 1050184610Salfred return (0); 1051184610Salfred} 1052184610Salfred 1053184610Salfred/*------------------------------------------------------------------------* 1054194228Sthompsa * usb_detach_device_sub 1055184610Salfred * 1056184610Salfred * This function will try to detach an USB device. If it fails a panic 1057184610Salfred * will result. 1058190730Sthompsa * 1059190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX". 1060184610Salfred *------------------------------------------------------------------------*/ 1061184610Salfredstatic void 1062194228Sthompsausb_detach_device_sub(struct usb_device *udev, device_t *ppdev, 1063214429Shselasky char **ppnpinfo, uint8_t flag) 1064184610Salfred{ 1065184610Salfred device_t dev; 1066214429Shselasky char *pnpinfo; 1067184610Salfred int err; 1068184610Salfred 1069197553Sthompsa dev = *ppdev; 1070197553Sthompsa if (dev) { 1071184610Salfred /* 1072184610Salfred * NOTE: It is important to clear "*ppdev" before deleting 1073184610Salfred * the child due to some device methods being called late 1074184610Salfred * during the delete process ! 1075184610Salfred */ 1076184610Salfred *ppdev = NULL; 1077184610Salfred 1078184610Salfred device_printf(dev, "at %s, port %d, addr %d " 1079184610Salfred "(disconnected)\n", 1080184610Salfred device_get_nameunit(udev->parent_dev), 1081184610Salfred udev->port_no, udev->address); 1082184610Salfred 1083184610Salfred if (device_is_attached(dev)) { 1084191824Sthompsa if (udev->flags.peer_suspended) { 1085184610Salfred err = DEVICE_RESUME(dev); 1086184610Salfred if (err) { 1087199816Sthompsa device_printf(dev, "Resume failed\n"); 1088184610Salfred } 1089184610Salfred } 1090184610Salfred if (device_detach(dev)) { 1091184610Salfred goto error; 1092184610Salfred } 1093184610Salfred } 1094184610Salfred if (device_delete_child(udev->parent_dev, dev)) { 1095184610Salfred goto error; 1096184610Salfred } 1097184610Salfred } 1098214429Shselasky 1099214429Shselasky pnpinfo = *ppnpinfo; 1100214429Shselasky if (pnpinfo != NULL) { 1101214429Shselasky *ppnpinfo = NULL; 1102214429Shselasky free(pnpinfo, M_USBDEV); 1103214429Shselasky } 1104184610Salfred return; 1105184610Salfred 1106184610Salfrederror: 1107184610Salfred /* Detach is not allowed to fail in the USB world */ 1108214429Shselasky panic("usb_detach_device_sub: A USB driver would not detach\n"); 1109184610Salfred} 1110184610Salfred 1111184610Salfred/*------------------------------------------------------------------------* 1112194228Sthompsa * usb_detach_device 1113184610Salfred * 1114184610Salfred * The following function will detach the matching interfaces. 1115184610Salfred * This function is NULL safe. 1116190730Sthompsa * 1117190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX". 1118184610Salfred *------------------------------------------------------------------------*/ 1119184610Salfredvoid 1120194228Sthompsausb_detach_device(struct usb_device *udev, uint8_t iface_index, 1121190730Sthompsa uint8_t flag) 1122184610Salfred{ 1123192984Sthompsa struct usb_interface *iface; 1124184610Salfred uint8_t i; 1125184610Salfred 1126184610Salfred if (udev == NULL) { 1127184610Salfred /* nothing to do */ 1128184610Salfred return; 1129184610Salfred } 1130184610Salfred DPRINTFN(4, "udev=%p\n", udev); 1131184610Salfred 1132207079Sthompsa sx_assert(&udev->enum_sx, SA_LOCKED); 1133184610Salfred 1134184610Salfred /* 1135184610Salfred * First detach the child to give the child's detach routine a 1136184610Salfred * chance to detach the sub-devices in the correct order. 1137184610Salfred * Then delete the child using "device_delete_child()" which 1138184610Salfred * will detach all sub-devices from the bottom and upwards! 1139184610Salfred */ 1140184610Salfred if (iface_index != USB_IFACE_INDEX_ANY) { 1141184610Salfred i = iface_index; 1142184610Salfred iface_index = i + 1; 1143184610Salfred } else { 1144184610Salfred i = 0; 1145184610Salfred iface_index = USB_IFACE_MAX; 1146184610Salfred } 1147184610Salfred 1148184610Salfred /* do the detach */ 1149184610Salfred 1150184610Salfred for (; i != iface_index; i++) { 1151184610Salfred 1152194228Sthompsa iface = usbd_get_iface(udev, i); 1153184610Salfred if (iface == NULL) { 1154184610Salfred /* looks like the end of the USB interfaces */ 1155184610Salfred break; 1156184610Salfred } 1157214429Shselasky usb_detach_device_sub(udev, &iface->subdev, 1158214429Shselasky &iface->pnpinfo, flag); 1159184610Salfred } 1160184610Salfred} 1161184610Salfred 1162184610Salfred/*------------------------------------------------------------------------* 1163194228Sthompsa * usb_probe_and_attach_sub 1164184610Salfred * 1165184610Salfred * Returns: 1166184610Salfred * 0: Success 1167184610Salfred * Else: Failure 1168184610Salfred *------------------------------------------------------------------------*/ 1169184610Salfredstatic uint8_t 1170194228Sthompsausb_probe_and_attach_sub(struct usb_device *udev, 1171192984Sthompsa struct usb_attach_arg *uaa) 1172184610Salfred{ 1173192984Sthompsa struct usb_interface *iface; 1174184610Salfred device_t dev; 1175184610Salfred int err; 1176184610Salfred 1177184610Salfred iface = uaa->iface; 1178184610Salfred if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 1179184610Salfred /* leave interface alone */ 1180184610Salfred return (0); 1181184610Salfred } 1182184610Salfred dev = iface->subdev; 1183184610Salfred if (dev) { 1184184610Salfred 1185184610Salfred /* clean up after module unload */ 1186184610Salfred 1187184610Salfred if (device_is_attached(dev)) { 1188184610Salfred /* already a device there */ 1189184610Salfred return (0); 1190184610Salfred } 1191184610Salfred /* clear "iface->subdev" as early as possible */ 1192184610Salfred 1193184610Salfred iface->subdev = NULL; 1194184610Salfred 1195184610Salfred if (device_delete_child(udev->parent_dev, dev)) { 1196184610Salfred 1197184610Salfred /* 1198184610Salfred * Panic here, else one can get a double call 1199184610Salfred * to device_detach(). USB devices should 1200184610Salfred * never fail on detach! 1201184610Salfred */ 1202199816Sthompsa panic("device_delete_child() failed\n"); 1203184610Salfred } 1204184610Salfred } 1205184610Salfred if (uaa->temp_dev == NULL) { 1206184610Salfred 1207184610Salfred /* create a new child */ 1208184610Salfred uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 1209184610Salfred if (uaa->temp_dev == NULL) { 1210184610Salfred device_printf(udev->parent_dev, 1211199816Sthompsa "Device creation failed\n"); 1212184610Salfred return (1); /* failure */ 1213184610Salfred } 1214184610Salfred device_set_ivars(uaa->temp_dev, uaa); 1215184610Salfred device_quiet(uaa->temp_dev); 1216184610Salfred } 1217184610Salfred /* 1218184610Salfred * Set "subdev" before probe and attach so that "devd" gets 1219184610Salfred * the information it needs. 1220184610Salfred */ 1221184610Salfred iface->subdev = uaa->temp_dev; 1222184610Salfred 1223184610Salfred if (device_probe_and_attach(iface->subdev) == 0) { 1224184610Salfred /* 1225184610Salfred * The USB attach arguments are only available during probe 1226184610Salfred * and attach ! 1227184610Salfred */ 1228184610Salfred uaa->temp_dev = NULL; 1229184610Salfred device_set_ivars(iface->subdev, NULL); 1230184610Salfred 1231191824Sthompsa if (udev->flags.peer_suspended) { 1232184610Salfred err = DEVICE_SUSPEND(iface->subdev); 1233191397Sthompsa if (err) 1234191397Sthompsa device_printf(iface->subdev, "Suspend failed\n"); 1235184610Salfred } 1236184610Salfred return (0); /* success */ 1237184610Salfred } else { 1238184610Salfred /* No USB driver found */ 1239184610Salfred iface->subdev = NULL; 1240184610Salfred } 1241184610Salfred return (1); /* failure */ 1242184610Salfred} 1243184610Salfred 1244184610Salfred/*------------------------------------------------------------------------* 1245194228Sthompsa * usbd_set_parent_iface 1246184610Salfred * 1247184610Salfred * Using this function will lock the alternate interface setting on an 1248184610Salfred * interface. It is typically used for multi interface drivers. In USB 1249184610Salfred * device side mode it is assumed that the alternate interfaces all 1250184610Salfred * have the same endpoint descriptors. The default parent index value 1251184610Salfred * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 1252184610Salfred * locked. 1253184610Salfred *------------------------------------------------------------------------*/ 1254184610Salfredvoid 1255194228Sthompsausbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index, 1256184610Salfred uint8_t parent_index) 1257184610Salfred{ 1258192984Sthompsa struct usb_interface *iface; 1259184610Salfred 1260236407Shselasky if (udev == NULL) { 1261236407Shselasky /* nothing to do */ 1262236407Shselasky return; 1263236407Shselasky } 1264194228Sthompsa iface = usbd_get_iface(udev, iface_index); 1265236407Shselasky if (iface != NULL) 1266184610Salfred iface->parent_iface_index = parent_index; 1267184610Salfred} 1268184610Salfred 1269184610Salfredstatic void 1270194228Sthompsausb_init_attach_arg(struct usb_device *udev, 1271192984Sthompsa struct usb_attach_arg *uaa) 1272184610Salfred{ 1273225350Shselasky memset(uaa, 0, sizeof(*uaa)); 1274225350Shselasky 1275184610Salfred uaa->device = udev; 1276192499Sthompsa uaa->usb_mode = udev->flags.usb_mode; 1277184610Salfred uaa->port = udev->port_no; 1278200653Sthompsa uaa->dev_state = UAA_DEV_READY; 1279184610Salfred 1280184610Salfred uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 1281184610Salfred uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 1282184610Salfred uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 1283184610Salfred uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 1284184610Salfred uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 1285184610Salfred uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 1286184610Salfred uaa->info.bConfigIndex = udev->curr_config_index; 1287184610Salfred uaa->info.bConfigNum = udev->curr_config_no; 1288184610Salfred} 1289184610Salfred 1290184610Salfred/*------------------------------------------------------------------------* 1291194228Sthompsa * usb_probe_and_attach 1292184610Salfred * 1293184610Salfred * This function is called from "uhub_explore_sub()", 1294194228Sthompsa * "usb_handle_set_config()" and "usb_handle_request()". 1295184610Salfred * 1296184610Salfred * Returns: 1297184610Salfred * 0: Success 1298184610Salfred * Else: A control transfer failed 1299184610Salfred *------------------------------------------------------------------------*/ 1300193045Sthompsausb_error_t 1301194228Sthompsausb_probe_and_attach(struct usb_device *udev, uint8_t iface_index) 1302184610Salfred{ 1303192984Sthompsa struct usb_attach_arg uaa; 1304192984Sthompsa struct usb_interface *iface; 1305184610Salfred uint8_t i; 1306184610Salfred uint8_t j; 1307184610Salfred uint8_t do_unlock; 1308184610Salfred 1309184610Salfred if (udev == NULL) { 1310184610Salfred DPRINTF("udev == NULL\n"); 1311184610Salfred return (USB_ERR_INVAL); 1312184610Salfred } 1313184610Salfred /* automatic locking */ 1314196498Salfred if (usbd_enum_is_locked(udev)) { 1315184610Salfred do_unlock = 0; 1316184610Salfred } else { 1317184610Salfred do_unlock = 1; 1318196498Salfred usbd_enum_lock(udev); 1319184610Salfred } 1320184610Salfred 1321184610Salfred if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 1322184610Salfred /* do nothing - no configuration has been set */ 1323184610Salfred goto done; 1324184610Salfred } 1325184610Salfred /* setup USB attach arguments */ 1326184610Salfred 1327194228Sthompsa usb_init_attach_arg(udev, &uaa); 1328184610Salfred 1329223512Shselasky /* 1330223512Shselasky * If the whole USB device is targeted, invoke the USB event 1331223512Shselasky * handler(s): 1332223512Shselasky */ 1333223512Shselasky if (iface_index == USB_IFACE_INDEX_ANY) { 1334223512Shselasky 1335223512Shselasky EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa); 1336223512Shselasky 1337223512Shselasky if (uaa.dev_state != UAA_DEV_READY) { 1338223512Shselasky /* leave device unconfigured */ 1339223512Shselasky usb_unconfigure(udev, 0); 1340223512Shselasky goto done; 1341223512Shselasky } 1342223512Shselasky } 1343223512Shselasky 1344184610Salfred /* Check if only one interface should be probed: */ 1345184610Salfred if (iface_index != USB_IFACE_INDEX_ANY) { 1346184610Salfred i = iface_index; 1347184610Salfred j = i + 1; 1348184610Salfred } else { 1349184610Salfred i = 0; 1350184610Salfred j = USB_IFACE_MAX; 1351184610Salfred } 1352184610Salfred 1353184610Salfred /* Do the probe and attach */ 1354184610Salfred for (; i != j; i++) { 1355184610Salfred 1356194228Sthompsa iface = usbd_get_iface(udev, i); 1357184610Salfred if (iface == NULL) { 1358184610Salfred /* 1359184610Salfred * Looks like the end of the USB 1360184610Salfred * interfaces ! 1361184610Salfred */ 1362184610Salfred DPRINTFN(2, "end of interfaces " 1363184610Salfred "at %u\n", i); 1364184610Salfred break; 1365184610Salfred } 1366184610Salfred if (iface->idesc == NULL) { 1367184610Salfred /* no interface descriptor */ 1368184610Salfred continue; 1369184610Salfred } 1370184610Salfred uaa.iface = iface; 1371184610Salfred 1372184610Salfred uaa.info.bInterfaceClass = 1373184610Salfred iface->idesc->bInterfaceClass; 1374184610Salfred uaa.info.bInterfaceSubClass = 1375184610Salfred iface->idesc->bInterfaceSubClass; 1376184610Salfred uaa.info.bInterfaceProtocol = 1377184610Salfred iface->idesc->bInterfaceProtocol; 1378184610Salfred uaa.info.bIfaceIndex = i; 1379184610Salfred uaa.info.bIfaceNum = 1380184610Salfred iface->idesc->bInterfaceNumber; 1381195963Salfred uaa.driver_info = 0; /* reset driver_info */ 1382184610Salfred 1383184610Salfred DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 1384184610Salfred uaa.info.bInterfaceClass, 1385184610Salfred uaa.info.bInterfaceSubClass, 1386184610Salfred uaa.info.bInterfaceProtocol, 1387184610Salfred uaa.info.bIfaceIndex, 1388184610Salfred uaa.info.bIfaceNum); 1389184610Salfred 1390223513Shselasky usb_probe_and_attach_sub(udev, &uaa); 1391184610Salfred 1392223513Shselasky /* 1393223513Shselasky * Remove the leftover child, if any, to enforce that 1394223513Shselasky * a new nomatch devd event is generated for the next 1395223513Shselasky * interface if no driver is found: 1396223513Shselasky */ 1397223513Shselasky if (uaa.temp_dev == NULL) 1398223513Shselasky continue; 1399223513Shselasky if (device_delete_child(udev->parent_dev, uaa.temp_dev)) 1400199816Sthompsa DPRINTFN(0, "device delete child failed\n"); 1401223513Shselasky uaa.temp_dev = NULL; 1402184610Salfred } 1403184610Salfreddone: 1404196498Salfred if (do_unlock) 1405196498Salfred usbd_enum_unlock(udev); 1406196498Salfred 1407184610Salfred return (0); 1408184610Salfred} 1409184610Salfred 1410184610Salfred/*------------------------------------------------------------------------* 1411194228Sthompsa * usb_suspend_resume_sub 1412184610Salfred * 1413184610Salfred * This function is called when the suspend or resume methods should 1414184610Salfred * be executed on an USB device. 1415184610Salfred *------------------------------------------------------------------------*/ 1416184610Salfredstatic void 1417194228Sthompsausb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend) 1418184610Salfred{ 1419184610Salfred int err; 1420184610Salfred 1421184610Salfred if (dev == NULL) { 1422184610Salfred return; 1423184610Salfred } 1424184610Salfred if (!device_is_attached(dev)) { 1425184610Salfred return; 1426184610Salfred } 1427184610Salfred if (do_suspend) { 1428184610Salfred err = DEVICE_SUSPEND(dev); 1429184610Salfred } else { 1430184610Salfred err = DEVICE_RESUME(dev); 1431184610Salfred } 1432184610Salfred if (err) { 1433199816Sthompsa device_printf(dev, "%s failed\n", 1434184610Salfred do_suspend ? "Suspend" : "Resume"); 1435184610Salfred } 1436184610Salfred} 1437184610Salfred 1438184610Salfred/*------------------------------------------------------------------------* 1439194228Sthompsa * usb_suspend_resume 1440184610Salfred * 1441184610Salfred * The following function will suspend or resume the USB device. 1442184610Salfred * 1443184610Salfred * Returns: 1444184610Salfred * 0: Success 1445184610Salfred * Else: Failure 1446184610Salfred *------------------------------------------------------------------------*/ 1447193045Sthompsausb_error_t 1448194228Sthompsausb_suspend_resume(struct usb_device *udev, uint8_t do_suspend) 1449184610Salfred{ 1450192984Sthompsa struct usb_interface *iface; 1451184610Salfred uint8_t i; 1452184610Salfred 1453184610Salfred if (udev == NULL) { 1454184610Salfred /* nothing to do */ 1455184610Salfred return (0); 1456184610Salfred } 1457184610Salfred DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 1458184610Salfred 1459208008Sthompsa sx_assert(&udev->sr_sx, SA_LOCKED); 1460184610Salfred 1461184824Sthompsa USB_BUS_LOCK(udev->bus); 1462184610Salfred /* filter the suspend events */ 1463191824Sthompsa if (udev->flags.peer_suspended == do_suspend) { 1464184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1465184610Salfred /* nothing to do */ 1466184610Salfred return (0); 1467184610Salfred } 1468191824Sthompsa udev->flags.peer_suspended = do_suspend; 1469184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1470184610Salfred 1471184610Salfred /* do the suspend or resume */ 1472184610Salfred 1473184610Salfred for (i = 0; i != USB_IFACE_MAX; i++) { 1474184610Salfred 1475194228Sthompsa iface = usbd_get_iface(udev, i); 1476184610Salfred if (iface == NULL) { 1477184610Salfred /* looks like the end of the USB interfaces */ 1478184610Salfred break; 1479184610Salfred } 1480194228Sthompsa usb_suspend_resume_sub(udev, iface->subdev, do_suspend); 1481184610Salfred } 1482184610Salfred return (0); 1483184610Salfred} 1484184610Salfred 1485184610Salfred/*------------------------------------------------------------------------* 1486194228Sthompsa * usbd_clear_stall_proc 1487184610Salfred * 1488184610Salfred * This function performs generic USB clear stall operations. 1489184610Salfred *------------------------------------------------------------------------*/ 1490184610Salfredstatic void 1491194228Sthompsausbd_clear_stall_proc(struct usb_proc_msg *_pm) 1492184610Salfred{ 1493192984Sthompsa struct usb_clear_stall_msg *pm = (void *)_pm; 1494192984Sthompsa struct usb_device *udev = pm->udev; 1495184610Salfred 1496184610Salfred /* Change lock */ 1497184824Sthompsa USB_BUS_UNLOCK(udev->bus); 1498207079Sthompsa mtx_lock(&udev->device_mtx); 1499184610Salfred 1500184610Salfred /* Start clear stall callback */ 1501207080Sthompsa usbd_transfer_start(udev->ctrl_xfer[1]); 1502184610Salfred 1503184610Salfred /* Change lock */ 1504207079Sthompsa mtx_unlock(&udev->device_mtx); 1505184824Sthompsa USB_BUS_LOCK(udev->bus); 1506184610Salfred} 1507184610Salfred 1508184610Salfred/*------------------------------------------------------------------------* 1509194228Sthompsa * usb_alloc_device 1510184610Salfred * 1511184610Salfred * This function allocates a new USB device. This function is called 1512184610Salfred * when a new device has been put in the powered state, but not yet in 1513184610Salfred * the addressed state. Get initial descriptor, set the address, get 1514184610Salfred * full descriptor and get strings. 1515184610Salfred * 1516184610Salfred * Return values: 1517184610Salfred * 0: Failure 1518184610Salfred * Else: Success 1519184610Salfred *------------------------------------------------------------------------*/ 1520192984Sthompsastruct usb_device * 1521194228Sthompsausb_alloc_device(device_t parent_dev, struct usb_bus *bus, 1522192984Sthompsa struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, 1523192500Sthompsa uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode) 1524184610Salfred{ 1525192984Sthompsa struct usb_attach_arg uaa; 1526192984Sthompsa struct usb_device *udev; 1527192984Sthompsa struct usb_device *adev; 1528192984Sthompsa struct usb_device *hub; 1529184610Salfred uint8_t *scratch_ptr; 1530193045Sthompsa usb_error_t err; 1531184610Salfred uint8_t device_index; 1532200653Sthompsa uint8_t config_index; 1533200653Sthompsa uint8_t config_quirk; 1534200653Sthompsa uint8_t set_config_failed; 1535184610Salfred 1536184610Salfred DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1537192499Sthompsa "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n", 1538184610Salfred parent_dev, bus, parent_hub, depth, port_index, port_no, 1539192499Sthompsa speed, mode); 1540184610Salfred 1541184610Salfred /* 1542184610Salfred * Find an unused device index. In USB Host mode this is the 1543184610Salfred * same as the device address. 1544184610Salfred * 1545187170Sthompsa * Device index zero is not used and device index 1 should 1546187170Sthompsa * always be the root hub. 1547184610Salfred */ 1548187171Sthompsa for (device_index = USB_ROOT_HUB_ADDR; 1549187171Sthompsa (device_index != bus->devices_max) && 1550187171Sthompsa (bus->devices[device_index] != NULL); 1551187171Sthompsa device_index++) /* nop */; 1552187171Sthompsa 1553187171Sthompsa if (device_index == bus->devices_max) { 1554187171Sthompsa device_printf(bus->bdev, 1555199816Sthompsa "No free USB device index for new device\n"); 1556187171Sthompsa return (NULL); 1557184610Salfred } 1558184610Salfred 1559184610Salfred if (depth > 0x10) { 1560184610Salfred device_printf(bus->bdev, 1561199816Sthompsa "Invalid device depth\n"); 1562184610Salfred return (NULL); 1563184610Salfred } 1564184610Salfred udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 1565184610Salfred if (udev == NULL) { 1566184610Salfred return (NULL); 1567184610Salfred } 1568184610Salfred /* initialise our SX-lock */ 1569207079Sthompsa sx_init_flags(&udev->ctrl_sx, "USB device SX lock", SX_DUPOK); 1570184610Salfred 1571184610Salfred /* initialise our SX-lock */ 1572207079Sthompsa sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); 1573223512Shselasky sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS); 1574184610Salfred 1575207079Sthompsa cv_init(&udev->ctrlreq_cv, "WCTRL"); 1576207079Sthompsa cv_init(&udev->ref_cv, "UGONE"); 1577184610Salfred 1578184610Salfred /* initialise our mutex */ 1579207079Sthompsa mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); 1580184610Salfred 1581184610Salfred /* initialise generic clear stall */ 1582194228Sthompsa udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; 1583184610Salfred udev->cs_msg[0].udev = udev; 1584194228Sthompsa udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; 1585184610Salfred udev->cs_msg[1].udev = udev; 1586184610Salfred 1587184610Salfred /* initialise some USB device fields */ 1588184610Salfred udev->parent_hub = parent_hub; 1589184610Salfred udev->parent_dev = parent_dev; 1590184610Salfred udev->port_index = port_index; 1591184610Salfred udev->port_no = port_no; 1592184610Salfred udev->depth = depth; 1593184610Salfred udev->bus = bus; 1594184610Salfred udev->address = USB_START_ADDR; /* default value */ 1595193045Sthompsa udev->plugtime = (usb_ticks_t)ticks; 1596186730Salfred /* 1597186730Salfred * We need to force the power mode to "on" because there are plenty 1598186730Salfred * of USB devices out there that do not work very well with 1599186730Salfred * automatic suspend and resume! 1600186730Salfred */ 1601212135Sthompsa udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); 1602186730Salfred udev->pwr_save.last_xfer_time = ticks; 1603184610Salfred /* we are not ready yet */ 1604184610Salfred udev->refcount = 1; 1605184610Salfred 1606184610Salfred /* set up default endpoint descriptor */ 1607207080Sthompsa udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); 1608207080Sthompsa udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1609207080Sthompsa udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1610207080Sthompsa udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; 1611207080Sthompsa udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 1612207080Sthompsa udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; 1613207080Sthompsa udev->ctrl_ep_desc.bInterval = 0; 1614213435Shselasky 1615213435Shselasky /* set up default endpoint companion descriptor */ 1616213435Shselasky udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); 1617213435Shselasky udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; 1618213435Shselasky 1619184610Salfred udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1620184610Salfred 1621184610Salfred udev->speed = speed; 1622192499Sthompsa udev->flags.usb_mode = mode; 1623184610Salfred 1624184610Salfred /* search for our High Speed USB HUB, if any */ 1625184610Salfred 1626184610Salfred adev = udev; 1627184610Salfred hub = udev->parent_hub; 1628184610Salfred 1629184610Salfred while (hub) { 1630184610Salfred if (hub->speed == USB_SPEED_HIGH) { 1631184610Salfred udev->hs_hub_addr = hub->address; 1632191395Sthompsa udev->parent_hs_hub = hub; 1633184610Salfred udev->hs_port_no = adev->port_no; 1634184610Salfred break; 1635184610Salfred } 1636184610Salfred adev = hub; 1637184610Salfred hub = hub->parent_hub; 1638184610Salfred } 1639184610Salfred 1640193644Sthompsa /* init the default endpoint */ 1641194228Sthompsa usb_init_endpoint(udev, 0, 1642207080Sthompsa &udev->ctrl_ep_desc, 1643213435Shselasky &udev->ctrl_ep_comp_desc, 1644207080Sthompsa &udev->ctrl_ep); 1645184610Salfred 1646184610Salfred /* set device index */ 1647184610Salfred udev->device_index = device_index; 1648184610Salfred 1649190191Sthompsa#if USB_HAVE_UGEN 1650189599Sthompsa /* Create ugen name */ 1651189599Sthompsa snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1652189599Sthompsa USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1653189599Sthompsa device_index); 1654190191Sthompsa LIST_INIT(&udev->pd_list); 1655190191Sthompsa 1656189110Sthompsa /* Create the control endpoint device */ 1657224777Shselasky udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0, 1658224777Shselasky FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600); 1659189599Sthompsa 1660189110Sthompsa /* Create a link from /dev/ugenX.X to the default endpoint */ 1661224777Shselasky if (udev->ctrl_dev != NULL) 1662224777Shselasky make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name); 1663189599Sthompsa#endif 1664213435Shselasky /* Initialise device */ 1665213435Shselasky if (bus->methods->device_init != NULL) { 1666213435Shselasky err = (bus->methods->device_init) (udev); 1667213435Shselasky if (err != 0) { 1668213435Shselasky DPRINTFN(0, "device init %d failed " 1669213435Shselasky "(%s, ignored)\n", device_index, 1670213435Shselasky usbd_errstr(err)); 1671213435Shselasky goto done; 1672213435Shselasky } 1673213435Shselasky } 1674213435Shselasky /* set powered device state after device init is complete */ 1675213435Shselasky usb_set_device_state(udev, USB_STATE_POWERED); 1676213435Shselasky 1677192499Sthompsa if (udev->flags.usb_mode == USB_MODE_HOST) { 1678184610Salfred 1679194228Sthompsa err = usbd_req_set_address(udev, NULL, device_index); 1680184610Salfred 1681213435Shselasky /* 1682213435Shselasky * This is the new USB device address from now on, if 1683213435Shselasky * the set address request didn't set it already. 1684213435Shselasky */ 1685213435Shselasky if (udev->address == USB_START_ADDR) 1686213435Shselasky udev->address = device_index; 1687184610Salfred 1688184610Salfred /* 1689184610Salfred * We ignore any set-address errors, hence there are 1690184610Salfred * buggy USB devices out there that actually receive 1691184610Salfred * the SETUP PID, but manage to set the address before 1692184610Salfred * the STATUS stage is ACK'ed. If the device responds 1693184610Salfred * to the subsequent get-descriptor at the new 1694184610Salfred * address, then we know that the set-address command 1695184610Salfred * was successful. 1696184610Salfred */ 1697184610Salfred if (err) { 1698184610Salfred DPRINTFN(0, "set address %d failed " 1699190739Sthompsa "(%s, ignored)\n", udev->address, 1700194228Sthompsa usbd_errstr(err)); 1701184610Salfred } 1702184610Salfred } else { 1703184610Salfred /* We are not self powered */ 1704184610Salfred udev->flags.self_powered = 0; 1705184610Salfred 1706184610Salfred /* Set unconfigured state */ 1707184610Salfred udev->curr_config_no = USB_UNCONFIG_NO; 1708184610Salfred udev->curr_config_index = USB_UNCONFIG_INDEX; 1709184610Salfred 1710184610Salfred /* Setup USB descriptors */ 1711194228Sthompsa err = (usb_temp_setup_by_index_p) (udev, usb_template); 1712184610Salfred if (err) { 1713184610Salfred DPRINTFN(0, "setting up USB template failed maybe the USB " 1714184610Salfred "template module has not been loaded\n"); 1715184610Salfred goto done; 1716184610Salfred } 1717184610Salfred } 1718194228Sthompsa usb_set_device_state(udev, USB_STATE_ADDRESSED); 1719184610Salfred 1720213435Shselasky /* setup the device descriptor and the initial "wMaxPacketSize" */ 1721213435Shselasky err = usbd_setup_device_desc(udev, NULL); 1722213435Shselasky 1723213435Shselasky if (err != 0) { 1724186730Salfred /* XXX try to re-enumerate the device */ 1725194228Sthompsa err = usbd_req_re_enumerate(udev, NULL); 1726213435Shselasky if (err) 1727186730Salfred goto done; 1728184610Salfred } 1729184610Salfred 1730184610Salfred /* 1731184610Salfred * Setup temporary USB attach args so that we can figure out some 1732184610Salfred * basic quirks for this device. 1733184610Salfred */ 1734194228Sthompsa usb_init_attach_arg(udev, &uaa); 1735184610Salfred 1736194228Sthompsa if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) { 1737184610Salfred udev->flags.uq_bus_powered = 1; 1738184610Salfred } 1739194228Sthompsa if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) { 1740184610Salfred udev->flags.no_strings = 1; 1741184610Salfred } 1742184610Salfred /* 1743184610Salfred * Workaround for buggy USB devices. 1744184610Salfred * 1745184610Salfred * It appears that some string-less USB chips will crash and 1746184610Salfred * disappear if any attempts are made to read any string 1747184610Salfred * descriptors. 1748184610Salfred * 1749184610Salfred * Try to detect such chips by checking the strings in the USB 1750184610Salfred * device descriptor. If no strings are present there we 1751184610Salfred * simply disable all USB strings. 1752184610Salfred */ 1753184610Salfred scratch_ptr = udev->bus->scratch[0].data; 1754184610Salfred 1755184610Salfred if (udev->ddesc.iManufacturer || 1756184610Salfred udev->ddesc.iProduct || 1757184610Salfred udev->ddesc.iSerialNumber) { 1758184610Salfred /* read out the language ID string */ 1759194228Sthompsa err = usbd_req_get_string_desc(udev, NULL, 1760201680Sthompsa (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE); 1761184610Salfred } else { 1762184610Salfred err = USB_ERR_INVAL; 1763184610Salfred } 1764184610Salfred 1765184610Salfred if (err || (scratch_ptr[0] < 4)) { 1766184610Salfred udev->flags.no_strings = 1; 1767184610Salfred } else { 1768197559Sthompsa uint16_t langid; 1769197559Sthompsa uint16_t pref; 1770197559Sthompsa uint16_t mask; 1771197559Sthompsa uint8_t x; 1772197559Sthompsa 1773197559Sthompsa /* load preferred value and mask */ 1774197559Sthompsa pref = usb_lang_id; 1775197559Sthompsa mask = usb_lang_mask; 1776197559Sthompsa 1777197559Sthompsa /* align length correctly */ 1778197559Sthompsa scratch_ptr[0] &= ~1; 1779197559Sthompsa 1780197559Sthompsa /* fix compiler warning */ 1781197559Sthompsa langid = 0; 1782197559Sthompsa 1783197559Sthompsa /* search for preferred language */ 1784197559Sthompsa for (x = 2; (x < scratch_ptr[0]); x += 2) { 1785197559Sthompsa langid = UGETW(scratch_ptr + x); 1786197559Sthompsa if ((langid & mask) == pref) 1787197559Sthompsa break; 1788197559Sthompsa } 1789197559Sthompsa if (x >= scratch_ptr[0]) { 1790197559Sthompsa /* pick the first language as the default */ 1791197559Sthompsa DPRINTFN(1, "Using first language\n"); 1792197559Sthompsa langid = UGETW(scratch_ptr + 2); 1793197559Sthompsa } 1794197559Sthompsa 1795197559Sthompsa DPRINTFN(1, "Language selected: 0x%04x\n", langid); 1796197559Sthompsa udev->langid = langid; 1797184610Salfred } 1798184610Salfred 1799184610Salfred /* assume 100mA bus powered for now. Changed when configured. */ 1800184610Salfred udev->power = USB_MIN_POWER; 1801192938Sthompsa /* fetch the vendor and product strings from the device */ 1802194228Sthompsa usbd_set_device_strings(udev); 1803184610Salfred 1804200653Sthompsa if (udev->flags.usb_mode == USB_MODE_DEVICE) { 1805200653Sthompsa /* USB device mode setup is complete */ 1806200653Sthompsa err = 0; 1807200653Sthompsa goto config_done; 1808200653Sthompsa } 1809184610Salfred 1810200653Sthompsa /* 1811200653Sthompsa * Most USB devices should attach to config index 0 by 1812200653Sthompsa * default 1813200653Sthompsa */ 1814200653Sthompsa if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 1815200653Sthompsa config_index = 0; 1816200653Sthompsa config_quirk = 1; 1817200653Sthompsa } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 1818200653Sthompsa config_index = 1; 1819200653Sthompsa config_quirk = 1; 1820200653Sthompsa } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 1821200653Sthompsa config_index = 2; 1822200653Sthompsa config_quirk = 1; 1823200653Sthompsa } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 1824200653Sthompsa config_index = 3; 1825200653Sthompsa config_quirk = 1; 1826200653Sthompsa } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 1827200653Sthompsa config_index = 4; 1828200653Sthompsa config_quirk = 1; 1829200653Sthompsa } else { 1830200653Sthompsa config_index = 0; 1831200653Sthompsa config_quirk = 0; 1832200653Sthompsa } 1833184610Salfred 1834200653Sthompsa set_config_failed = 0; 1835184610Salfredrepeat_set_config: 1836184610Salfred 1837200653Sthompsa DPRINTF("setting config %u\n", config_index); 1838184610Salfred 1839200653Sthompsa /* get the USB device configured */ 1840200653Sthompsa err = usbd_set_config_index(udev, config_index); 1841200653Sthompsa if (err) { 1842200653Sthompsa if (udev->ddesc.bNumConfigurations != 0) { 1843200653Sthompsa if (!set_config_failed) { 1844200653Sthompsa set_config_failed = 1; 1845200653Sthompsa /* XXX try to re-enumerate the device */ 1846200653Sthompsa err = usbd_req_re_enumerate(udev, NULL); 1847200653Sthompsa if (err == 0) 1848200653Sthompsa goto repeat_set_config; 1849186730Salfred } 1850200653Sthompsa DPRINTFN(0, "Failure selecting configuration index %u:" 1851200653Sthompsa "%s, port %u, addr %u (ignored)\n", 1852200653Sthompsa config_index, usbd_errstr(err), udev->port_no, 1853200653Sthompsa udev->address); 1854200653Sthompsa } 1855200653Sthompsa /* 1856200653Sthompsa * Some USB devices do not have any configurations. Ignore any 1857200653Sthompsa * set config failures! 1858200653Sthompsa */ 1859200653Sthompsa err = 0; 1860200653Sthompsa goto config_done; 1861200653Sthompsa } 1862200653Sthompsa if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { 1863200653Sthompsa if ((udev->cdesc->bNumInterface < 2) && 1864200653Sthompsa usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { 1865200653Sthompsa DPRINTFN(0, "Found no endpoints, trying next config\n"); 1866200653Sthompsa config_index++; 1867200653Sthompsa goto repeat_set_config; 1868200653Sthompsa } 1869200653Sthompsa if (config_index == 0) { 1870186730Salfred /* 1871200653Sthompsa * Try to figure out if we have an 1872200653Sthompsa * auto-install disk there: 1873186730Salfred */ 1874201681Sthompsa if (usb_iface_is_cdrom(udev, 0)) { 1875200653Sthompsa DPRINTFN(0, "Found possible auto-install " 1876200653Sthompsa "disk (trying next config)\n"); 1877184610Salfred config_index++; 1878184610Salfred goto repeat_set_config; 1879184610Salfred } 1880184610Salfred } 1881184610Salfred } 1882225350Shselasky if (set_config_failed == 0 && config_index == 0 && 1883226903Shselasky usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 && 1884226903Shselasky usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) { 1885184610Salfred 1886225350Shselasky /* 1887225350Shselasky * Try to figure out if there are any MSC quirks we 1888225350Shselasky * should apply automatically: 1889225350Shselasky */ 1890225350Shselasky err = usb_msc_auto_quirk(udev, 0); 1891225350Shselasky 1892225350Shselasky if (err != 0) { 1893225350Shselasky set_config_failed = 1; 1894225350Shselasky goto repeat_set_config; 1895225350Shselasky } 1896225350Shselasky } 1897225350Shselasky 1898200653Sthompsaconfig_done: 1899184610Salfred DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 1900184610Salfred udev->address, udev, udev->parent_hub); 1901184610Salfred 1902184610Salfred /* register our device - we are ready */ 1903194228Sthompsa usb_bus_port_set_device(bus, parent_hub ? 1904184610Salfred parent_hub->hub->ports + port_index : NULL, udev, device_index); 1905184610Salfred 1906189599Sthompsa#if USB_HAVE_UGEN 1907190191Sthompsa /* Symlink the ugen device name */ 1908194228Sthompsa udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); 1909190191Sthompsa 1910190191Sthompsa /* Announce device */ 1911212136Sthompsa printf("%s: <%s> at %s\n", udev->ugen_name, 1912212136Sthompsa usb_get_manufacturer(udev), 1913184610Salfred device_get_nameunit(udev->bus->bdev)); 1914214221Shselasky#endif 1915190727Sthompsa 1916214221Shselasky#if USB_HAVE_DEVCTL 1917207020Sthompsa usb_notify_addq("ATTACH", udev); 1918190191Sthompsa#endif 1919184610Salfreddone: 1920184610Salfred if (err) { 1921197553Sthompsa /* 1922197553Sthompsa * Free USB device and all subdevices, if any. 1923197553Sthompsa */ 1924197553Sthompsa usb_free_device(udev, 0); 1925184610Salfred udev = NULL; 1926184610Salfred } 1927184610Salfred return (udev); 1928184610Salfred} 1929184610Salfred 1930189599Sthompsa#if USB_HAVE_UGEN 1931224777Shselaskystruct usb_fs_privdata * 1932224777Shselaskyusb_make_dev(struct usb_device *udev, const char *devname, int ep, 1933224777Shselasky int fi, int rwmode, uid_t uid, gid_t gid, int mode) 1934189110Sthompsa{ 1935192984Sthompsa struct usb_fs_privdata* pd; 1936224777Shselasky char buffer[32]; 1937189110Sthompsa 1938189110Sthompsa /* Store information to locate ourselves again later */ 1939192984Sthompsa pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, 1940189110Sthompsa M_WAITOK | M_ZERO); 1941189110Sthompsa pd->bus_index = device_get_unit(udev->bus->bdev); 1942189110Sthompsa pd->dev_index = udev->device_index; 1943189110Sthompsa pd->ep_addr = ep; 1944224777Shselasky pd->fifo_index = fi; 1945224777Shselasky pd->mode = rwmode; 1946189110Sthompsa 1947189110Sthompsa /* Now, create the device itself */ 1948224777Shselasky if (devname == NULL) { 1949224777Shselasky devname = buffer; 1950224777Shselasky snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u", 1951224777Shselasky pd->bus_index, pd->dev_index, pd->ep_addr); 1952224777Shselasky } 1953224777Shselasky 1954224777Shselasky pd->cdev = make_dev(&usb_devsw, 0, uid, gid, mode, "%s", devname); 1955224777Shselasky 1956224777Shselasky if (pd->cdev == NULL) { 1957224777Shselasky DPRINTFN(0, "Failed to create device %s\n", devname); 1958224777Shselasky free(pd, M_USBDEV); 1959224777Shselasky return (NULL); 1960224777Shselasky } 1961224777Shselasky 1962224777Shselasky /* XXX setting si_drv1 and creating the device is not atomic! */ 1963189110Sthompsa pd->cdev->si_drv1 = pd; 1964189110Sthompsa 1965224777Shselasky return (pd); 1966189110Sthompsa} 1967189110Sthompsa 1968224777Shselaskyvoid 1969224777Shselaskyusb_destroy_dev(struct usb_fs_privdata *pd) 1970224777Shselasky{ 1971224777Shselasky if (pd == NULL) 1972224777Shselasky return; 1973224777Shselasky 1974224777Shselasky destroy_dev(pd->cdev); 1975224777Shselasky 1976224777Shselasky free(pd, M_USBDEV); 1977224777Shselasky} 1978224777Shselasky 1979189110Sthompsastatic void 1980194228Sthompsausb_cdev_create(struct usb_device *udev) 1981189110Sthompsa{ 1982192984Sthompsa struct usb_config_descriptor *cd; 1983192984Sthompsa struct usb_endpoint_descriptor *ed; 1984192984Sthompsa struct usb_descriptor *desc; 1985192984Sthompsa struct usb_fs_privdata* pd; 1986189172Sthompsa int inmode, outmode, inmask, outmask, mode; 1987189172Sthompsa uint8_t ep; 1988189110Sthompsa 1989189110Sthompsa KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 1990189110Sthompsa 1991189110Sthompsa DPRINTFN(2, "Creating device nodes\n"); 1992189110Sthompsa 1993194228Sthompsa if (usbd_get_mode(udev) == USB_MODE_DEVICE) { 1994189110Sthompsa inmode = FWRITE; 1995189110Sthompsa outmode = FREAD; 1996189110Sthompsa } else { /* USB_MODE_HOST */ 1997189110Sthompsa inmode = FREAD; 1998189110Sthompsa outmode = FWRITE; 1999189110Sthompsa } 2000189110Sthompsa 2001189172Sthompsa inmask = 0; 2002189172Sthompsa outmask = 0; 2003189172Sthompsa desc = NULL; 2004189110Sthompsa 2005189172Sthompsa /* 2006189172Sthompsa * Collect all used endpoint numbers instead of just 2007189172Sthompsa * generating 16 static endpoints. 2008189172Sthompsa */ 2009194228Sthompsa cd = usbd_get_config_descriptor(udev); 2010194228Sthompsa while ((desc = usb_desc_foreach(cd, desc))) { 2011189172Sthompsa /* filter out all endpoint descriptors */ 2012189172Sthompsa if ((desc->bDescriptorType == UDESC_ENDPOINT) && 2013189172Sthompsa (desc->bLength >= sizeof(*ed))) { 2014192984Sthompsa ed = (struct usb_endpoint_descriptor *)desc; 2015189110Sthompsa 2016189172Sthompsa /* update masks */ 2017189172Sthompsa ep = ed->bEndpointAddress; 2018189172Sthompsa if (UE_GET_DIR(ep) == UE_DIR_OUT) 2019189172Sthompsa outmask |= 1 << UE_GET_ADDR(ep); 2020189172Sthompsa else 2021189172Sthompsa inmask |= 1 << UE_GET_ADDR(ep); 2022189110Sthompsa } 2023189110Sthompsa } 2024189172Sthompsa 2025189172Sthompsa /* Create all available endpoints except EP0 */ 2026189172Sthompsa for (ep = 1; ep < 16; ep++) { 2027224777Shselasky mode = (inmask & (1 << ep)) ? inmode : 0; 2028224777Shselasky mode |= (outmask & (1 << ep)) ? outmode : 0; 2029189172Sthompsa if (mode == 0) 2030189172Sthompsa continue; /* no IN or OUT endpoint */ 2031189172Sthompsa 2032224777Shselasky pd = usb_make_dev(udev, NULL, ep, 0, 2033224777Shselasky mode, UID_ROOT, GID_OPERATOR, 0600); 2034224777Shselasky 2035224777Shselasky if (pd != NULL) 2036224777Shselasky LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 2037189172Sthompsa } 2038189110Sthompsa} 2039189110Sthompsa 2040189110Sthompsastatic void 2041194228Sthompsausb_cdev_free(struct usb_device *udev) 2042189110Sthompsa{ 2043192984Sthompsa struct usb_fs_privdata* pd; 2044189110Sthompsa 2045189110Sthompsa DPRINTFN(2, "Freeing device nodes\n"); 2046189110Sthompsa 2047189110Sthompsa while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 2048189110Sthompsa KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 2049189110Sthompsa 2050189110Sthompsa LIST_REMOVE(pd, pd_next); 2051224777Shselasky 2052224777Shselasky usb_destroy_dev(pd); 2053189110Sthompsa } 2054189110Sthompsa} 2055189599Sthompsa#endif 2056189110Sthompsa 2057184610Salfred/*------------------------------------------------------------------------* 2058194228Sthompsa * usb_free_device 2059184610Salfred * 2060197553Sthompsa * This function is NULL safe and will free an USB device and its 2061197553Sthompsa * children devices, if any. 2062190730Sthompsa * 2063197553Sthompsa * Flag values: Reserved, set to zero. 2064184610Salfred *------------------------------------------------------------------------*/ 2065184610Salfredvoid 2066194228Sthompsausb_free_device(struct usb_device *udev, uint8_t flag) 2067184610Salfred{ 2068192984Sthompsa struct usb_bus *bus; 2069184610Salfred 2070190730Sthompsa if (udev == NULL) 2071190730Sthompsa return; /* already freed */ 2072190730Sthompsa 2073184610Salfred DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 2074184610Salfred 2075191494Sthompsa bus = udev->bus; 2076194228Sthompsa usb_set_device_state(udev, USB_STATE_DETACHED); 2077190730Sthompsa 2078214221Shselasky#if USB_HAVE_DEVCTL 2079207020Sthompsa usb_notify_addq("DETACH", udev); 2080214221Shselasky#endif 2081184610Salfred 2082214221Shselasky#if USB_HAVE_UGEN 2083189110Sthompsa printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name, 2084212136Sthompsa usb_get_manufacturer(udev), device_get_nameunit(bus->bdev)); 2085184610Salfred 2086189110Sthompsa /* Destroy UGEN symlink, if any */ 2087184610Salfred if (udev->ugen_symlink) { 2088194228Sthompsa usb_free_symlink(udev->ugen_symlink); 2089184610Salfred udev->ugen_symlink = NULL; 2090184610Salfred } 2091190191Sthompsa#endif 2092184610Salfred /* 2093184610Salfred * Unregister our device first which will prevent any further 2094184610Salfred * references: 2095184610Salfred */ 2096194228Sthompsa usb_bus_port_set_device(bus, udev->parent_hub ? 2097184610Salfred udev->parent_hub->hub->ports + udev->port_index : NULL, 2098184610Salfred NULL, USB_ROOT_HUB_ADDR); 2099184610Salfred 2100189599Sthompsa#if USB_HAVE_UGEN 2101184610Salfred /* wait for all pending references to go away: */ 2102194228Sthompsa mtx_lock(&usb_ref_lock); 2103184610Salfred udev->refcount--; 2104184610Salfred while (udev->refcount != 0) { 2105207079Sthompsa cv_wait(&udev->ref_cv, &usb_ref_lock); 2106184610Salfred } 2107194228Sthompsa mtx_unlock(&usb_ref_lock); 2108190730Sthompsa 2109224777Shselasky usb_destroy_dev(udev->ctrl_dev); 2110189599Sthompsa#endif 2111184610Salfred 2112192499Sthompsa if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2113184610Salfred /* stop receiving any control transfers (Device Side Mode) */ 2114207080Sthompsa usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 2115184610Salfred } 2116184610Salfred 2117190730Sthompsa /* the following will get the device unconfigured in software */ 2118197553Sthompsa usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0); 2119184610Salfred 2120184610Salfred /* unsetup any leftover default USB transfers */ 2121207080Sthompsa usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 2122184610Salfred 2123187174Sthompsa /* template unsetup, if any */ 2124194228Sthompsa (usb_temp_unsetup_p) (udev); 2125184610Salfred 2126187174Sthompsa /* 2127187174Sthompsa * Make sure that our clear-stall messages are not queued 2128187174Sthompsa * anywhere: 2129187174Sthompsa */ 2130187174Sthompsa USB_BUS_LOCK(udev->bus); 2131194228Sthompsa usb_proc_mwait(&udev->bus->non_giant_callback_proc, 2132187174Sthompsa &udev->cs_msg[0], &udev->cs_msg[1]); 2133187174Sthompsa USB_BUS_UNLOCK(udev->bus); 2134187174Sthompsa 2135207079Sthompsa sx_destroy(&udev->ctrl_sx); 2136207079Sthompsa sx_destroy(&udev->enum_sx); 2137208008Sthompsa sx_destroy(&udev->sr_sx); 2138184610Salfred 2139207079Sthompsa cv_destroy(&udev->ctrlreq_cv); 2140207079Sthompsa cv_destroy(&udev->ref_cv); 2141184610Salfred 2142207079Sthompsa mtx_destroy(&udev->device_mtx); 2143190191Sthompsa#if USB_HAVE_UGEN 2144189110Sthompsa KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 2145190191Sthompsa#endif 2146184610Salfred 2147213435Shselasky /* Uninitialise device */ 2148213435Shselasky if (bus->methods->device_uninit != NULL) 2149213435Shselasky (bus->methods->device_uninit) (udev); 2150213435Shselasky 2151184610Salfred /* free device */ 2152193042Sthompsa free(udev->serial, M_USB); 2153193042Sthompsa free(udev->manufacturer, M_USB); 2154193042Sthompsa free(udev->product, M_USB); 2155184610Salfred free(udev, M_USB); 2156184610Salfred} 2157184610Salfred 2158184610Salfred/*------------------------------------------------------------------------* 2159194228Sthompsa * usbd_get_iface 2160184610Salfred * 2161184610Salfred * This function is the safe way to get the USB interface structure 2162184610Salfred * pointer by interface index. 2163184610Salfred * 2164184610Salfred * Return values: 2165184610Salfred * NULL: Interface not present. 2166184610Salfred * Else: Pointer to USB interface structure. 2167184610Salfred *------------------------------------------------------------------------*/ 2168192984Sthompsastruct usb_interface * 2169194228Sthompsausbd_get_iface(struct usb_device *udev, uint8_t iface_index) 2170184610Salfred{ 2171192984Sthompsa struct usb_interface *iface = udev->ifaces + iface_index; 2172184610Salfred 2173190730Sthompsa if (iface_index >= udev->ifaces_max) 2174184610Salfred return (NULL); 2175184610Salfred return (iface); 2176184610Salfred} 2177184610Salfred 2178184610Salfred/*------------------------------------------------------------------------* 2179194271Sthompsa * usbd_find_descriptor 2180184610Salfred * 2181184610Salfred * This function will lookup the first descriptor that matches the 2182184610Salfred * criteria given by the arguments "type" and "subtype". Descriptors 2183184610Salfred * will only be searched within the interface having the index 2184184610Salfred * "iface_index". If the "id" argument points to an USB descriptor, 2185184610Salfred * it will be skipped before the search is started. This allows 2186184610Salfred * searching for multiple descriptors using the same criteria. Else 2187184610Salfred * the search is started after the interface descriptor. 2188184610Salfred * 2189184610Salfred * Return values: 2190184610Salfred * NULL: End of descriptors 2191184610Salfred * Else: A descriptor matching the criteria 2192184610Salfred *------------------------------------------------------------------------*/ 2193184610Salfredvoid * 2194194271Sthompsausbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index, 2195184610Salfred uint8_t type, uint8_t type_mask, 2196184610Salfred uint8_t subtype, uint8_t subtype_mask) 2197184610Salfred{ 2198192984Sthompsa struct usb_descriptor *desc; 2199192984Sthompsa struct usb_config_descriptor *cd; 2200192984Sthompsa struct usb_interface *iface; 2201184610Salfred 2202194228Sthompsa cd = usbd_get_config_descriptor(udev); 2203184610Salfred if (cd == NULL) { 2204184610Salfred return (NULL); 2205184610Salfred } 2206184610Salfred if (id == NULL) { 2207194228Sthompsa iface = usbd_get_iface(udev, iface_index); 2208184610Salfred if (iface == NULL) { 2209184610Salfred return (NULL); 2210184610Salfred } 2211194228Sthompsa id = usbd_get_interface_descriptor(iface); 2212184610Salfred if (id == NULL) { 2213184610Salfred return (NULL); 2214184610Salfred } 2215184610Salfred } 2216184610Salfred desc = (void *)id; 2217184610Salfred 2218194228Sthompsa while ((desc = usb_desc_foreach(cd, desc))) { 2219184610Salfred 2220184610Salfred if (desc->bDescriptorType == UDESC_INTERFACE) { 2221184610Salfred break; 2222184610Salfred } 2223184610Salfred if (((desc->bDescriptorType & type_mask) == type) && 2224184610Salfred ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 2225184610Salfred return (desc); 2226184610Salfred } 2227184610Salfred } 2228184610Salfred return (NULL); 2229184610Salfred} 2230184610Salfred 2231184610Salfred/*------------------------------------------------------------------------* 2232194228Sthompsa * usb_devinfo 2233184610Salfred * 2234184610Salfred * This function will dump information from the device descriptor 2235184610Salfred * belonging to the USB device pointed to by "udev", to the string 2236184610Salfred * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 2237184610Salfred * including the terminating zero. 2238184610Salfred *------------------------------------------------------------------------*/ 2239184610Salfredvoid 2240194228Sthompsausb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) 2241184610Salfred{ 2242192984Sthompsa struct usb_device_descriptor *udd = &udev->ddesc; 2243184610Salfred uint16_t bcdDevice; 2244184610Salfred uint16_t bcdUSB; 2245184610Salfred 2246184610Salfred bcdUSB = UGETW(udd->bcdUSB); 2247184610Salfred bcdDevice = UGETW(udd->bcdDevice); 2248184610Salfred 2249184610Salfred if (udd->bDeviceClass != 0xFF) { 2250184610Salfred snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 2251190191Sthompsa "%x.%02x, addr %d", 2252212136Sthompsa usb_get_manufacturer(udev), 2253212136Sthompsa usb_get_product(udev), 2254184610Salfred udd->bDeviceClass, udd->bDeviceSubClass, 2255184610Salfred (bcdUSB >> 8), bcdUSB & 0xFF, 2256184610Salfred (bcdDevice >> 8), bcdDevice & 0xFF, 2257184610Salfred udev->address); 2258184610Salfred } else { 2259184610Salfred snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 2260190191Sthompsa "%x.%02x, addr %d", 2261212136Sthompsa usb_get_manufacturer(udev), 2262212136Sthompsa usb_get_product(udev), 2263184610Salfred (bcdUSB >> 8), bcdUSB & 0xFF, 2264184610Salfred (bcdDevice >> 8), bcdDevice & 0xFF, 2265184610Salfred udev->address); 2266184610Salfred } 2267184610Salfred} 2268184610Salfred 2269194677Sthompsa#ifdef USB_VERBOSE 2270184610Salfred/* 2271184610Salfred * Descriptions of of known vendors and devices ("products"). 2272184610Salfred */ 2273184610Salfredstruct usb_knowndev { 2274184610Salfred uint16_t vendor; 2275184610Salfred uint16_t product; 2276184610Salfred uint32_t flags; 2277184610Salfred const char *vendorname; 2278184610Salfred const char *productname; 2279184610Salfred}; 2280184610Salfred 2281184610Salfred#define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 2282184610Salfred 2283188746Sthompsa#include "usbdevs.h" 2284188746Sthompsa#include "usbdevs_data.h" 2285184610Salfred#endif /* USB_VERBOSE */ 2286184610Salfred 2287184610Salfredstatic void 2288194228Sthompsausbd_set_device_strings(struct usb_device *udev) 2289184610Salfred{ 2290192984Sthompsa struct usb_device_descriptor *udd = &udev->ddesc; 2291194677Sthompsa#ifdef USB_VERBOSE 2292184610Salfred const struct usb_knowndev *kdp; 2293184610Salfred#endif 2294208008Sthompsa char *temp_ptr; 2295197559Sthompsa size_t temp_size; 2296184610Salfred uint16_t vendor_id; 2297184610Salfred uint16_t product_id; 2298184610Salfred 2299208008Sthompsa temp_ptr = (char *)udev->bus->scratch[0].data; 2300197559Sthompsa temp_size = sizeof(udev->bus->scratch[0].data); 2301197559Sthompsa 2302192938Sthompsa vendor_id = UGETW(udd->idVendor); 2303192938Sthompsa product_id = UGETW(udd->idProduct); 2304184610Salfred 2305192938Sthompsa /* get serial number string */ 2306197559Sthompsa usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2307192938Sthompsa udev->ddesc.iSerialNumber); 2308197559Sthompsa udev->serial = strdup(temp_ptr, M_USB); 2309184610Salfred 2310192938Sthompsa /* get manufacturer string */ 2311197559Sthompsa usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2312192938Sthompsa udev->ddesc.iManufacturer); 2313197559Sthompsa usb_trim_spaces(temp_ptr); 2314197559Sthompsa if (temp_ptr[0] != '\0') 2315197559Sthompsa udev->manufacturer = strdup(temp_ptr, M_USB); 2316184610Salfred 2317192938Sthompsa /* get product string */ 2318197559Sthompsa usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2319192938Sthompsa udev->ddesc.iProduct); 2320197559Sthompsa usb_trim_spaces(temp_ptr); 2321197559Sthompsa if (temp_ptr[0] != '\0') 2322197559Sthompsa udev->product = strdup(temp_ptr, M_USB); 2323184610Salfred 2324194677Sthompsa#ifdef USB_VERBOSE 2325192938Sthompsa if (udev->manufacturer == NULL || udev->product == NULL) { 2326192938Sthompsa for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { 2327184610Salfred if (kdp->vendor == vendor_id && 2328184610Salfred (kdp->product == product_id || 2329184610Salfred (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 2330184610Salfred break; 2331184610Salfred } 2332184610Salfred if (kdp->vendorname != NULL) { 2333192938Sthompsa /* XXX should use pointer to knowndevs string */ 2334192938Sthompsa if (udev->manufacturer == NULL) { 2335192938Sthompsa udev->manufacturer = strdup(kdp->vendorname, 2336192938Sthompsa M_USB); 2337192938Sthompsa } 2338192938Sthompsa if (udev->product == NULL && 2339192938Sthompsa (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { 2340192938Sthompsa udev->product = strdup(kdp->productname, 2341192938Sthompsa M_USB); 2342192938Sthompsa } 2343184610Salfred } 2344184610Salfred } 2345184610Salfred#endif 2346192938Sthompsa /* Provide default strings if none were found */ 2347192938Sthompsa if (udev->manufacturer == NULL) { 2348197559Sthompsa snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id); 2349197559Sthompsa udev->manufacturer = strdup(temp_ptr, M_USB); 2350184610Salfred } 2351192938Sthompsa if (udev->product == NULL) { 2352197559Sthompsa snprintf(temp_ptr, temp_size, "product 0x%04x", product_id); 2353197559Sthompsa udev->product = strdup(temp_ptr, M_USB); 2354184610Salfred } 2355184610Salfred} 2356184610Salfred 2357188411Sthompsa/* 2358188411Sthompsa * Returns: 2359188411Sthompsa * See: USB_MODE_XXX 2360188411Sthompsa */ 2361192499Sthompsaenum usb_hc_mode 2362194228Sthompsausbd_get_mode(struct usb_device *udev) 2363188411Sthompsa{ 2364192499Sthompsa return (udev->flags.usb_mode); 2365188411Sthompsa} 2366188411Sthompsa 2367188411Sthompsa/* 2368188411Sthompsa * Returns: 2369188411Sthompsa * See: USB_SPEED_XXX 2370188411Sthompsa */ 2371192500Sthompsaenum usb_dev_speed 2372194228Sthompsausbd_get_speed(struct usb_device *udev) 2373184610Salfred{ 2374184610Salfred return (udev->speed); 2375184610Salfred} 2376184610Salfred 2377186730Salfreduint32_t 2378194228Sthompsausbd_get_isoc_fps(struct usb_device *udev) 2379186730Salfred{ 2380186730Salfred ; /* indent fix */ 2381186730Salfred switch (udev->speed) { 2382186730Salfred case USB_SPEED_LOW: 2383186730Salfred case USB_SPEED_FULL: 2384186730Salfred return (1000); 2385186730Salfred default: 2386186730Salfred return (8000); 2387186730Salfred } 2388186730Salfred} 2389186730Salfred 2390192984Sthompsastruct usb_device_descriptor * 2391194228Sthompsausbd_get_device_descriptor(struct usb_device *udev) 2392184610Salfred{ 2393184610Salfred if (udev == NULL) 2394184610Salfred return (NULL); /* be NULL safe */ 2395184610Salfred return (&udev->ddesc); 2396184610Salfred} 2397184610Salfred 2398192984Sthompsastruct usb_config_descriptor * 2399194228Sthompsausbd_get_config_descriptor(struct usb_device *udev) 2400184610Salfred{ 2401184610Salfred if (udev == NULL) 2402184610Salfred return (NULL); /* be NULL safe */ 2403184610Salfred return (udev->cdesc); 2404184610Salfred} 2405184610Salfred 2406184610Salfred/*------------------------------------------------------------------------* 2407194228Sthompsa * usb_test_quirk - test a device for a given quirk 2408184610Salfred * 2409184610Salfred * Return values: 2410184610Salfred * 0: The USB device does not have the given quirk. 2411184610Salfred * Else: The USB device has the given quirk. 2412184610Salfred *------------------------------------------------------------------------*/ 2413184610Salfreduint8_t 2414194228Sthompsausb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk) 2415184610Salfred{ 2416184610Salfred uint8_t found; 2417225469Shselasky uint8_t x; 2418184610Salfred 2419225469Shselasky if (quirk == UQ_NONE) 2420225469Shselasky return (0); 2421225469Shselasky 2422225469Shselasky /* search the automatic per device quirks first */ 2423225469Shselasky 2424225469Shselasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 2425225469Shselasky if (uaa->device->autoQuirk[x] == quirk) 2426225469Shselasky return (1); 2427225469Shselasky } 2428225469Shselasky 2429225469Shselasky /* search global quirk table, if any */ 2430225469Shselasky 2431194228Sthompsa found = (usb_test_quirk_p) (&uaa->info, quirk); 2432225469Shselasky 2433184610Salfred return (found); 2434184610Salfred} 2435184610Salfred 2436192984Sthompsastruct usb_interface_descriptor * 2437194228Sthompsausbd_get_interface_descriptor(struct usb_interface *iface) 2438184610Salfred{ 2439184610Salfred if (iface == NULL) 2440184610Salfred return (NULL); /* be NULL safe */ 2441184610Salfred return (iface->idesc); 2442184610Salfred} 2443184610Salfred 2444184610Salfreduint8_t 2445194228Sthompsausbd_get_interface_altindex(struct usb_interface *iface) 2446184610Salfred{ 2447184610Salfred return (iface->alt_index); 2448184610Salfred} 2449184610Salfred 2450184610Salfreduint8_t 2451194228Sthompsausbd_get_bus_index(struct usb_device *udev) 2452184610Salfred{ 2453184610Salfred return ((uint8_t)device_get_unit(udev->bus->bdev)); 2454184610Salfred} 2455184610Salfred 2456184610Salfreduint8_t 2457194228Sthompsausbd_get_device_index(struct usb_device *udev) 2458184610Salfred{ 2459184610Salfred return (udev->device_index); 2460184610Salfred} 2461184610Salfred 2462214221Shselasky#if USB_HAVE_DEVCTL 2463184610Salfredstatic void 2464207020Sthompsausb_notify_addq(const char *type, struct usb_device *udev) 2465207020Sthompsa{ 2466207020Sthompsa struct usb_interface *iface; 2467207020Sthompsa struct sbuf *sb; 2468207020Sthompsa int i; 2469207020Sthompsa 2470207020Sthompsa /* announce the device */ 2471207020Sthompsa sb = sbuf_new_auto(); 2472207020Sthompsa sbuf_printf(sb, 2473214221Shselasky#if USB_HAVE_UGEN 2474214221Shselasky "ugen=%s " 2475216072Shselasky "cdev=%s " 2476214221Shselasky#endif 2477207020Sthompsa "vendor=0x%04x " 2478207020Sthompsa "product=0x%04x " 2479207020Sthompsa "devclass=0x%02x " 2480207020Sthompsa "devsubclass=0x%02x " 2481207020Sthompsa "sernum=\"%s\" " 2482207020Sthompsa "release=0x%04x " 2483207027Sthompsa "mode=%s " 2484207020Sthompsa "port=%u " 2485214221Shselasky#if USB_HAVE_UGEN 2486214809Sn_hibma "parent=%s" 2487214221Shselasky#endif 2488214221Shselasky "", 2489214221Shselasky#if USB_HAVE_UGEN 2490207020Sthompsa udev->ugen_name, 2491216072Shselasky udev->ugen_name, 2492214221Shselasky#endif 2493207020Sthompsa UGETW(udev->ddesc.idVendor), 2494207020Sthompsa UGETW(udev->ddesc.idProduct), 2495207020Sthompsa udev->ddesc.bDeviceClass, 2496207020Sthompsa udev->ddesc.bDeviceSubClass, 2497212136Sthompsa usb_get_serial(udev), 2498207020Sthompsa UGETW(udev->ddesc.bcdDevice), 2499207027Sthompsa (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 2500214221Shselasky udev->port_no 2501214221Shselasky#if USB_HAVE_UGEN 2502214221Shselasky , udev->parent_hub != NULL ? 2503214221Shselasky udev->parent_hub->ugen_name : 2504214221Shselasky device_get_nameunit(device_get_parent(udev->bus->bdev)) 2505214221Shselasky#endif 2506214221Shselasky ); 2507207020Sthompsa sbuf_finish(sb); 2508207020Sthompsa devctl_notify("USB", "DEVICE", type, sbuf_data(sb)); 2509207020Sthompsa sbuf_delete(sb); 2510207020Sthompsa 2511207020Sthompsa /* announce each interface */ 2512207020Sthompsa for (i = 0; i < USB_IFACE_MAX; i++) { 2513207020Sthompsa iface = usbd_get_iface(udev, i); 2514207020Sthompsa if (iface == NULL) 2515207020Sthompsa break; /* end of interfaces */ 2516207020Sthompsa if (iface->idesc == NULL) 2517207020Sthompsa continue; /* no interface descriptor */ 2518207020Sthompsa 2519207020Sthompsa sb = sbuf_new_auto(); 2520207020Sthompsa sbuf_printf(sb, 2521214221Shselasky#if USB_HAVE_UGEN 2522214221Shselasky "ugen=%s " 2523216072Shselasky "cdev=%s " 2524214221Shselasky#endif 2525207020Sthompsa "vendor=0x%04x " 2526207020Sthompsa "product=0x%04x " 2527207020Sthompsa "devclass=0x%02x " 2528207020Sthompsa "devsubclass=0x%02x " 2529207020Sthompsa "sernum=\"%s\" " 2530207020Sthompsa "release=0x%04x " 2531207027Sthompsa "mode=%s " 2532207020Sthompsa "interface=%d " 2533207020Sthompsa "endpoints=%d " 2534207020Sthompsa "intclass=0x%02x " 2535207020Sthompsa "intsubclass=0x%02x " 2536214809Sn_hibma "intprotocol=0x%02x", 2537214221Shselasky#if USB_HAVE_UGEN 2538207020Sthompsa udev->ugen_name, 2539216072Shselasky udev->ugen_name, 2540214221Shselasky#endif 2541207020Sthompsa UGETW(udev->ddesc.idVendor), 2542207020Sthompsa UGETW(udev->ddesc.idProduct), 2543207020Sthompsa udev->ddesc.bDeviceClass, 2544207020Sthompsa udev->ddesc.bDeviceSubClass, 2545212136Sthompsa usb_get_serial(udev), 2546207020Sthompsa UGETW(udev->ddesc.bcdDevice), 2547207027Sthompsa (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 2548207020Sthompsa iface->idesc->bInterfaceNumber, 2549207020Sthompsa iface->idesc->bNumEndpoints, 2550207020Sthompsa iface->idesc->bInterfaceClass, 2551207020Sthompsa iface->idesc->bInterfaceSubClass, 2552207020Sthompsa iface->idesc->bInterfaceProtocol); 2553207020Sthompsa sbuf_finish(sb); 2554207020Sthompsa devctl_notify("USB", "INTERFACE", type, sbuf_data(sb)); 2555207020Sthompsa sbuf_delete(sb); 2556207020Sthompsa } 2557207020Sthompsa} 2558214221Shselasky#endif 2559207020Sthompsa 2560214221Shselasky#if USB_HAVE_UGEN 2561184610Salfred/*------------------------------------------------------------------------* 2562194228Sthompsa * usb_fifo_free_wrap 2563184610Salfred * 2564185087Salfred * This function will free the FIFOs. 2565185087Salfred * 2566190730Sthompsa * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2567190730Sthompsa * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2568190730Sthompsa * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2569190730Sthompsa * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2570190730Sthompsa * control endpoint FIFOs. If "iface_index" is not set to 2571190730Sthompsa * "USB_IFACE_INDEX_ANY" the flag has no effect. 2572184610Salfred *------------------------------------------------------------------------*/ 2573184610Salfredstatic void 2574194228Sthompsausb_fifo_free_wrap(struct usb_device *udev, 2575185087Salfred uint8_t iface_index, uint8_t flag) 2576184610Salfred{ 2577192984Sthompsa struct usb_fifo *f; 2578184610Salfred uint16_t i; 2579184610Salfred 2580184610Salfred /* 2581184610Salfred * Free any USB FIFOs on the given interface: 2582184610Salfred */ 2583184610Salfred for (i = 0; i != USB_FIFO_MAX; i++) { 2584184610Salfred f = udev->fifo[i]; 2585184610Salfred if (f == NULL) { 2586184610Salfred continue; 2587184610Salfred } 2588185087Salfred /* Check if the interface index matches */ 2589185087Salfred if (iface_index == f->iface_index) { 2590194228Sthompsa if (f->methods != &usb_ugen_methods) { 2591185087Salfred /* 2592185087Salfred * Don't free any non-generic FIFOs in 2593185087Salfred * this case. 2594185087Salfred */ 2595185087Salfred continue; 2596185087Salfred } 2597185290Salfred if ((f->dev_ep_index == 0) && 2598185290Salfred (f->fs_xfer == NULL)) { 2599185290Salfred /* no need to free this FIFO */ 2600185087Salfred continue; 2601185087Salfred } 2602185087Salfred } else if (iface_index == USB_IFACE_INDEX_ANY) { 2603194228Sthompsa if ((f->methods == &usb_ugen_methods) && 2604190730Sthompsa (f->dev_ep_index == 0) && 2605190730Sthompsa (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 2606185290Salfred (f->fs_xfer == NULL)) { 2607185290Salfred /* no need to free this FIFO */ 2608185087Salfred continue; 2609185087Salfred } 2610185087Salfred } else { 2611185290Salfred /* no need to free this FIFO */ 2612184610Salfred continue; 2613184610Salfred } 2614185290Salfred /* free this FIFO */ 2615194228Sthompsa usb_fifo_free(f); 2616184610Salfred } 2617184610Salfred} 2618189599Sthompsa#endif 2619186730Salfred 2620186730Salfred/*------------------------------------------------------------------------* 2621194228Sthompsa * usb_peer_can_wakeup 2622186730Salfred * 2623186730Salfred * Return values: 2624186730Salfred * 0: Peer cannot do resume signalling. 2625186730Salfred * Else: Peer can do resume signalling. 2626186730Salfred *------------------------------------------------------------------------*/ 2627186730Salfreduint8_t 2628194228Sthompsausb_peer_can_wakeup(struct usb_device *udev) 2629186730Salfred{ 2630192984Sthompsa const struct usb_config_descriptor *cdp; 2631186730Salfred 2632186730Salfred cdp = udev->cdesc; 2633192499Sthompsa if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { 2634186730Salfred return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 2635186730Salfred } 2636186730Salfred return (0); /* not supported */ 2637186730Salfred} 2638191494Sthompsa 2639191494Sthompsavoid 2640194228Sthompsausb_set_device_state(struct usb_device *udev, enum usb_dev_state state) 2641191494Sthompsa{ 2642191494Sthompsa 2643191494Sthompsa KASSERT(state < USB_STATE_MAX, ("invalid udev state")); 2644191494Sthompsa 2645191494Sthompsa DPRINTF("udev %p state %s -> %s\n", udev, 2646194228Sthompsa usb_statestr(udev->state), usb_statestr(state)); 2647191494Sthompsa udev->state = state; 2648213435Shselasky 2649213435Shselasky if (udev->bus->methods->device_state_change != NULL) 2650213435Shselasky (udev->bus->methods->device_state_change) (udev); 2651191494Sthompsa} 2652191494Sthompsa 2653213435Shselaskyenum usb_dev_state 2654213435Shselaskyusb_get_device_state(struct usb_device *udev) 2655213435Shselasky{ 2656213435Shselasky if (udev == NULL) 2657213435Shselasky return (USB_STATE_DETACHED); 2658213435Shselasky return (udev->state); 2659213435Shselasky} 2660213435Shselasky 2661191824Sthompsauint8_t 2662194228Sthompsausbd_device_attached(struct usb_device *udev) 2663191494Sthompsa{ 2664191494Sthompsa return (udev->state > USB_STATE_DETACHED); 2665191494Sthompsa} 2666196498Salfred 2667196498Salfred/* The following function locks enumerating the given USB device. */ 2668196498Salfred 2669196498Salfredvoid 2670196498Salfredusbd_enum_lock(struct usb_device *udev) 2671196498Salfred{ 2672207079Sthompsa sx_xlock(&udev->enum_sx); 2673208008Sthompsa sx_xlock(&udev->sr_sx); 2674196498Salfred /* 2675196498Salfred * NEWBUS LOCK NOTE: We should check if any parent SX locks 2676196498Salfred * are locked before locking Giant. Else the lock can be 2677196498Salfred * locked multiple times. 2678196498Salfred */ 2679196498Salfred mtx_lock(&Giant); 2680196498Salfred} 2681196498Salfred 2682196498Salfred/* The following function unlocks enumerating the given USB device. */ 2683196498Salfred 2684196498Salfredvoid 2685196498Salfredusbd_enum_unlock(struct usb_device *udev) 2686196498Salfred{ 2687196498Salfred mtx_unlock(&Giant); 2688207079Sthompsa sx_xunlock(&udev->enum_sx); 2689208008Sthompsa sx_xunlock(&udev->sr_sx); 2690196498Salfred} 2691196498Salfred 2692208008Sthompsa/* The following function locks suspend and resume. */ 2693208008Sthompsa 2694208008Sthompsavoid 2695208008Sthompsausbd_sr_lock(struct usb_device *udev) 2696208008Sthompsa{ 2697208008Sthompsa sx_xlock(&udev->sr_sx); 2698208008Sthompsa /* 2699208008Sthompsa * NEWBUS LOCK NOTE: We should check if any parent SX locks 2700208008Sthompsa * are locked before locking Giant. Else the lock can be 2701208008Sthompsa * locked multiple times. 2702208008Sthompsa */ 2703208008Sthompsa mtx_lock(&Giant); 2704208008Sthompsa} 2705208008Sthompsa 2706208008Sthompsa/* The following function unlocks suspend and resume. */ 2707208008Sthompsa 2708208008Sthompsavoid 2709208008Sthompsausbd_sr_unlock(struct usb_device *udev) 2710208008Sthompsa{ 2711208008Sthompsa mtx_unlock(&Giant); 2712208008Sthompsa sx_xunlock(&udev->sr_sx); 2713208008Sthompsa} 2714208008Sthompsa 2715196498Salfred/* 2716196498Salfred * The following function checks the enumerating lock for the given 2717196498Salfred * USB device. 2718196498Salfred */ 2719196498Salfred 2720196498Salfreduint8_t 2721196498Salfredusbd_enum_is_locked(struct usb_device *udev) 2722196498Salfred{ 2723207079Sthompsa return (sx_xlocked(&udev->enum_sx)); 2724196498Salfred} 2725214429Shselasky 2726214429Shselasky/* 2727214429Shselasky * The following function is used to set the per-interface specific 2728214429Shselasky * plug and play information. The string referred to by the pnpinfo 2729214429Shselasky * argument can safely be freed after calling this function. The 2730214429Shselasky * pnpinfo of an interface will be reset at device detach or when 2731214429Shselasky * passing a NULL argument to this function. This function 2732214429Shselasky * returns zero on success, else a USB_ERR_XXX failure code. 2733214429Shselasky */ 2734214429Shselasky 2735214429Shselaskyusb_error_t 2736214429Shselaskyusbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo) 2737214429Shselasky{ 2738214429Shselasky struct usb_interface *iface; 2739214429Shselasky 2740214429Shselasky iface = usbd_get_iface(udev, iface_index); 2741214429Shselasky if (iface == NULL) 2742214429Shselasky return (USB_ERR_INVAL); 2743214429Shselasky 2744214429Shselasky if (iface->pnpinfo != NULL) { 2745214429Shselasky free(iface->pnpinfo, M_USBDEV); 2746214429Shselasky iface->pnpinfo = NULL; 2747214429Shselasky } 2748214429Shselasky 2749214429Shselasky if (pnpinfo == NULL || pnpinfo[0] == 0) 2750214429Shselasky return (0); /* success */ 2751214429Shselasky 2752214429Shselasky iface->pnpinfo = strdup(pnpinfo, M_USBDEV); 2753214429Shselasky if (iface->pnpinfo == NULL) 2754214429Shselasky return (USB_ERR_NOMEM); 2755214429Shselasky 2756214429Shselasky return (0); /* success */ 2757214429Shselasky} 2758214429Shselasky 2759225350Shselaskyusb_error_t 2760225350Shselaskyusbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk) 2761225350Shselasky{ 2762225350Shselasky uint8_t x; 2763225350Shselasky 2764225350Shselasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 2765225469Shselasky if (udev->autoQuirk[x] == 0 || 2766225469Shselasky udev->autoQuirk[x] == quirk) { 2767225350Shselasky udev->autoQuirk[x] = quirk; 2768225350Shselasky return (0); /* success */ 2769225350Shselasky } 2770225350Shselasky } 2771225350Shselasky return (USB_ERR_NOMEM); 2772225350Shselasky} 2773239214Shselasky 2774239214Shselasky/* 2775239214Shselasky * The following function is used to select the endpoint mode. It 2776239214Shselasky * should not be called outside enumeration context. 2777239214Shselasky */ 2778239214Shselasky 2779239214Shselaskyusb_error_t 2780239214Shselaskyusbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep, 2781239214Shselasky uint8_t ep_mode) 2782239214Shselasky{ 2783239214Shselasky usb_error_t error; 2784239240Shselasky uint8_t do_unlock; 2785239214Shselasky 2786239240Shselasky /* automatic locking */ 2787239240Shselasky if (usbd_enum_is_locked(udev)) { 2788239240Shselasky do_unlock = 0; 2789239240Shselasky } else { 2790239240Shselasky do_unlock = 1; 2791239240Shselasky usbd_enum_lock(udev); 2792239240Shselasky } 2793239214Shselasky 2794239214Shselasky if (udev->bus->methods->set_endpoint_mode != NULL) { 2795239214Shselasky error = (udev->bus->methods->set_endpoint_mode) ( 2796239214Shselasky udev, ep, ep_mode); 2797239214Shselasky } else if (ep_mode != USB_EP_MODE_DEFAULT) { 2798239214Shselasky error = USB_ERR_INVAL; 2799239214Shselasky } else { 2800239214Shselasky error = 0; 2801239214Shselasky } 2802239214Shselasky 2803239214Shselasky /* only set new mode regardless of error */ 2804239214Shselasky ep->ep_mode = ep_mode; 2805239214Shselasky 2806239240Shselasky if (do_unlock) 2807239240Shselasky usbd_enum_unlock(udev); 2808239240Shselasky 2809239214Shselasky return (error); 2810239214Shselasky} 2811239214Shselasky 2812239214Shselaskyuint8_t 2813239214Shselaskyusbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep) 2814239214Shselasky{ 2815239214Shselasky return (ep->ep_mode); 2816239214Shselasky} 2817