1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Most of this source has been derived from the Linux USB 3*4882a593Smuzhiyun * project: 4*4882a593Smuzhiyun * (C) Copyright Linus Torvalds 1999 5*4882a593Smuzhiyun * (C) Copyright Johannes Erdfelt 1999-2001 6*4882a593Smuzhiyun * (C) Copyright Andreas Gal 1999 7*4882a593Smuzhiyun * (C) Copyright Gregory P. Smith 1999 8*4882a593Smuzhiyun * (C) Copyright Deti Fliegl 1999 (new USB architecture) 9*4882a593Smuzhiyun * (C) Copyright Randy Dunlap 2000 10*4882a593Smuzhiyun * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id) 11*4882a593Smuzhiyun * (C) Copyright Yggdrasil Computing, Inc. 2000 12*4882a593Smuzhiyun * (usb_device_id matching changes by Adam J. Richter) 13*4882a593Smuzhiyun * 14*4882a593Smuzhiyun * Adapted for U-Boot: 15*4882a593Smuzhiyun * (C) Copyright 2001 Denis Peter, MPL AG Switzerland 16*4882a593Smuzhiyun * 17*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+ 18*4882a593Smuzhiyun */ 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun /* 21*4882a593Smuzhiyun * How it works: 22*4882a593Smuzhiyun * 23*4882a593Smuzhiyun * Since this is a bootloader, the devices will not be automatic 24*4882a593Smuzhiyun * (re)configured on hotplug, but after a restart of the USB the 25*4882a593Smuzhiyun * device should work. 26*4882a593Smuzhiyun * 27*4882a593Smuzhiyun * For each transfer (except "Interrupt") we wait for completion. 28*4882a593Smuzhiyun */ 29*4882a593Smuzhiyun #include <common.h> 30*4882a593Smuzhiyun #include <command.h> 31*4882a593Smuzhiyun #include <dm.h> 32*4882a593Smuzhiyun #include <memalign.h> 33*4882a593Smuzhiyun #include <asm/processor.h> 34*4882a593Smuzhiyun #include <linux/compiler.h> 35*4882a593Smuzhiyun #include <linux/ctype.h> 36*4882a593Smuzhiyun #include <asm/byteorder.h> 37*4882a593Smuzhiyun #include <asm/unaligned.h> 38*4882a593Smuzhiyun #include <errno.h> 39*4882a593Smuzhiyun #include <usb.h> 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun #define USB_BUFSIZ 512 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun static int asynch_allowed; 44*4882a593Smuzhiyun char usb_started; /* flag for the started/stopped USB status */ 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB) 47*4882a593Smuzhiyun static struct usb_device usb_dev[USB_MAX_DEVICE]; 48*4882a593Smuzhiyun static int dev_index; 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT 51*4882a593Smuzhiyun #define CONFIG_USB_MAX_CONTROLLER_COUNT 1 52*4882a593Smuzhiyun #endif 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun /*************************************************************************** 55*4882a593Smuzhiyun * Init USB Device 56*4882a593Smuzhiyun */ 57*4882a593Smuzhiyun int usb_init(void) 58*4882a593Smuzhiyun { 59*4882a593Smuzhiyun void *ctrl; 60*4882a593Smuzhiyun struct usb_device *dev; 61*4882a593Smuzhiyun int i, start_index = 0; 62*4882a593Smuzhiyun int controllers_initialized = 0; 63*4882a593Smuzhiyun int ret; 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun dev_index = 0; 66*4882a593Smuzhiyun asynch_allowed = 1; 67*4882a593Smuzhiyun usb_hub_reset(); 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun /* first make all devices unknown */ 70*4882a593Smuzhiyun for (i = 0; i < USB_MAX_DEVICE; i++) { 71*4882a593Smuzhiyun memset(&usb_dev[i], 0, sizeof(struct usb_device)); 72*4882a593Smuzhiyun usb_dev[i].devnum = -1; 73*4882a593Smuzhiyun } 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun /* init low_level USB */ 76*4882a593Smuzhiyun for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) { 77*4882a593Smuzhiyun /* init low_level USB */ 78*4882a593Smuzhiyun printf("USB%d: ", i); 79*4882a593Smuzhiyun ret = usb_lowlevel_init(i, USB_INIT_HOST, &ctrl); 80*4882a593Smuzhiyun if (ret == -ENODEV) { /* No such device. */ 81*4882a593Smuzhiyun puts("Port not available.\n"); 82*4882a593Smuzhiyun controllers_initialized++; 83*4882a593Smuzhiyun continue; 84*4882a593Smuzhiyun } 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun if (ret) { /* Other error. */ 87*4882a593Smuzhiyun puts("lowlevel init failed\n"); 88*4882a593Smuzhiyun continue; 89*4882a593Smuzhiyun } 90*4882a593Smuzhiyun /* 91*4882a593Smuzhiyun * lowlevel init is OK, now scan the bus for devices 92*4882a593Smuzhiyun * i.e. search HUBs and configure them 93*4882a593Smuzhiyun */ 94*4882a593Smuzhiyun controllers_initialized++; 95*4882a593Smuzhiyun start_index = dev_index; 96*4882a593Smuzhiyun printf("scanning bus %d for devices... ", i); 97*4882a593Smuzhiyun ret = usb_alloc_new_device(ctrl, &dev); 98*4882a593Smuzhiyun if (ret) 99*4882a593Smuzhiyun break; 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun /* 102*4882a593Smuzhiyun * device 0 is always present 103*4882a593Smuzhiyun * (root hub, so let it analyze) 104*4882a593Smuzhiyun */ 105*4882a593Smuzhiyun ret = usb_new_device(dev); 106*4882a593Smuzhiyun if (ret) 107*4882a593Smuzhiyun usb_free_device(dev->controller); 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun if (start_index == dev_index) { 110*4882a593Smuzhiyun puts("No USB Device found\n"); 111*4882a593Smuzhiyun continue; 112*4882a593Smuzhiyun } else { 113*4882a593Smuzhiyun printf("%d USB Device(s) found\n", 114*4882a593Smuzhiyun dev_index - start_index); 115*4882a593Smuzhiyun } 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun usb_started = 1; 118*4882a593Smuzhiyun } 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun debug("scan end\n"); 121*4882a593Smuzhiyun /* if we were not able to find at least one working bus, bail out */ 122*4882a593Smuzhiyun if (controllers_initialized == 0) 123*4882a593Smuzhiyun puts("USB error: all controllers failed lowlevel init\n"); 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun return usb_started ? 0 : -ENODEV; 126*4882a593Smuzhiyun } 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun /****************************************************************************** 129*4882a593Smuzhiyun * Stop USB this stops the LowLevel Part and deregisters USB devices. 130*4882a593Smuzhiyun */ 131*4882a593Smuzhiyun int usb_stop(void) 132*4882a593Smuzhiyun { 133*4882a593Smuzhiyun int i; 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun if (usb_started) { 136*4882a593Smuzhiyun asynch_allowed = 1; 137*4882a593Smuzhiyun usb_started = 0; 138*4882a593Smuzhiyun usb_hub_reset(); 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) { 141*4882a593Smuzhiyun if (usb_lowlevel_stop(i)) 142*4882a593Smuzhiyun printf("failed to stop USB controller %d\n", i); 143*4882a593Smuzhiyun } 144*4882a593Smuzhiyun } 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun return 0; 147*4882a593Smuzhiyun } 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun /****************************************************************************** 150*4882a593Smuzhiyun * Detect if a USB device has been plugged or unplugged. 151*4882a593Smuzhiyun */ 152*4882a593Smuzhiyun int usb_detect_change(void) 153*4882a593Smuzhiyun { 154*4882a593Smuzhiyun int i, j; 155*4882a593Smuzhiyun int change = 0; 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun for (j = 0; j < USB_MAX_DEVICE; j++) { 158*4882a593Smuzhiyun for (i = 0; i < usb_dev[j].maxchild; i++) { 159*4882a593Smuzhiyun struct usb_port_status status; 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun if (usb_get_port_status(&usb_dev[j], i + 1, 162*4882a593Smuzhiyun &status) < 0) 163*4882a593Smuzhiyun /* USB request failed */ 164*4882a593Smuzhiyun continue; 165*4882a593Smuzhiyun 166*4882a593Smuzhiyun if (le16_to_cpu(status.wPortChange) & 167*4882a593Smuzhiyun USB_PORT_STAT_C_CONNECTION) 168*4882a593Smuzhiyun change++; 169*4882a593Smuzhiyun } 170*4882a593Smuzhiyun } 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun return change; 173*4882a593Smuzhiyun } 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun /* 176*4882a593Smuzhiyun * disables the asynch behaviour of the control message. This is used for data 177*4882a593Smuzhiyun * transfers that uses the exclusiv access to the control and bulk messages. 178*4882a593Smuzhiyun * Returns the old value so it can be restored later. 179*4882a593Smuzhiyun */ 180*4882a593Smuzhiyun int usb_disable_asynch(int disable) 181*4882a593Smuzhiyun { 182*4882a593Smuzhiyun int old_value = asynch_allowed; 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun asynch_allowed = !disable; 185*4882a593Smuzhiyun return old_value; 186*4882a593Smuzhiyun } 187*4882a593Smuzhiyun #endif /* !CONFIG_IS_ENABLED(DM_USB) */ 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun /*------------------------------------------------------------------- 191*4882a593Smuzhiyun * Message wrappers. 192*4882a593Smuzhiyun * 193*4882a593Smuzhiyun */ 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun /* 196*4882a593Smuzhiyun * submits an Interrupt Message. Some drivers may implement non-blocking 197*4882a593Smuzhiyun * polling: when non-block is true and the device is not responding return 198*4882a593Smuzhiyun * -EAGAIN instead of waiting for device to respond. 199*4882a593Smuzhiyun */ 200*4882a593Smuzhiyun int usb_int_msg(struct usb_device *dev, unsigned long pipe, 201*4882a593Smuzhiyun void *buffer, int transfer_len, int interval, bool nonblock) 202*4882a593Smuzhiyun { 203*4882a593Smuzhiyun return submit_int_msg(dev, pipe, buffer, transfer_len, interval, 204*4882a593Smuzhiyun nonblock); 205*4882a593Smuzhiyun } 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun /* 208*4882a593Smuzhiyun * submits a control message and waits for comletion (at least timeout * 1ms) 209*4882a593Smuzhiyun * If timeout is 0, we don't wait for completion (used as example to set and 210*4882a593Smuzhiyun * clear keyboards LEDs). For data transfers, (storage transfers) we don't 211*4882a593Smuzhiyun * allow control messages with 0 timeout, by previousely resetting the flag 212*4882a593Smuzhiyun * asynch_allowed (usb_disable_asynch(1)). 213*4882a593Smuzhiyun * returns the transferred length if OK or -1 if error. The transferred length 214*4882a593Smuzhiyun * and the current status are stored in the dev->act_len and dev->status. 215*4882a593Smuzhiyun */ 216*4882a593Smuzhiyun int usb_control_msg(struct usb_device *dev, unsigned int pipe, 217*4882a593Smuzhiyun unsigned char request, unsigned char requesttype, 218*4882a593Smuzhiyun unsigned short value, unsigned short index, 219*4882a593Smuzhiyun void *data, unsigned short size, int timeout) 220*4882a593Smuzhiyun { 221*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(struct devrequest, setup_packet, 1); 222*4882a593Smuzhiyun int err; 223*4882a593Smuzhiyun 224*4882a593Smuzhiyun if ((timeout == 0) && (!asynch_allowed)) { 225*4882a593Smuzhiyun /* request for a asynch control pipe is not allowed */ 226*4882a593Smuzhiyun return -EINVAL; 227*4882a593Smuzhiyun } 228*4882a593Smuzhiyun 229*4882a593Smuzhiyun /* set setup command */ 230*4882a593Smuzhiyun setup_packet->requesttype = requesttype; 231*4882a593Smuzhiyun setup_packet->request = request; 232*4882a593Smuzhiyun setup_packet->value = cpu_to_le16(value); 233*4882a593Smuzhiyun setup_packet->index = cpu_to_le16(index); 234*4882a593Smuzhiyun setup_packet->length = cpu_to_le16(size); 235*4882a593Smuzhiyun debug("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \ 236*4882a593Smuzhiyun "value 0x%X index 0x%X length 0x%X\n", 237*4882a593Smuzhiyun request, requesttype, value, index, size); 238*4882a593Smuzhiyun dev->status = USB_ST_NOT_PROC; /*not yet processed */ 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun err = submit_control_msg(dev, pipe, data, size, setup_packet); 241*4882a593Smuzhiyun if (err < 0) 242*4882a593Smuzhiyun return err; 243*4882a593Smuzhiyun if (timeout == 0) 244*4882a593Smuzhiyun return (int)size; 245*4882a593Smuzhiyun 246*4882a593Smuzhiyun /* 247*4882a593Smuzhiyun * Wait for status to update until timeout expires, USB driver 248*4882a593Smuzhiyun * interrupt handler may set the status when the USB operation has 249*4882a593Smuzhiyun * been completed. 250*4882a593Smuzhiyun */ 251*4882a593Smuzhiyun while (timeout--) { 252*4882a593Smuzhiyun if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC)) 253*4882a593Smuzhiyun break; 254*4882a593Smuzhiyun mdelay(1); 255*4882a593Smuzhiyun } 256*4882a593Smuzhiyun if (dev->status) 257*4882a593Smuzhiyun return -1; 258*4882a593Smuzhiyun 259*4882a593Smuzhiyun if(dev->descriptor.idVendor == 0x058f && dev->descriptor.idProduct == 0x6387) 260*4882a593Smuzhiyun udelay(200); 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun return dev->act_len; 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun } 265*4882a593Smuzhiyun 266*4882a593Smuzhiyun /*------------------------------------------------------------------- 267*4882a593Smuzhiyun * submits bulk message, and waits for completion. returns 0 if Ok or 268*4882a593Smuzhiyun * negative if Error. 269*4882a593Smuzhiyun * synchronous behavior 270*4882a593Smuzhiyun */ 271*4882a593Smuzhiyun int usb_bulk_msg(struct usb_device *dev, unsigned int pipe, 272*4882a593Smuzhiyun void *data, int len, int *actual_length, int timeout) 273*4882a593Smuzhiyun { 274*4882a593Smuzhiyun if (len < 0) 275*4882a593Smuzhiyun return -EINVAL; 276*4882a593Smuzhiyun dev->status = USB_ST_NOT_PROC; /*not yet processed */ 277*4882a593Smuzhiyun if (submit_bulk_msg(dev, pipe, data, len) < 0) 278*4882a593Smuzhiyun return -EIO; 279*4882a593Smuzhiyun while (timeout--) { 280*4882a593Smuzhiyun if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC)) 281*4882a593Smuzhiyun break; 282*4882a593Smuzhiyun mdelay(1); 283*4882a593Smuzhiyun } 284*4882a593Smuzhiyun *actual_length = dev->act_len; 285*4882a593Smuzhiyun if (dev->status == 0) 286*4882a593Smuzhiyun return 0; 287*4882a593Smuzhiyun else 288*4882a593Smuzhiyun return -EIO; 289*4882a593Smuzhiyun } 290*4882a593Smuzhiyun 291*4882a593Smuzhiyun 292*4882a593Smuzhiyun /*------------------------------------------------------------------- 293*4882a593Smuzhiyun * Max Packet stuff 294*4882a593Smuzhiyun */ 295*4882a593Smuzhiyun 296*4882a593Smuzhiyun /* 297*4882a593Smuzhiyun * returns the max packet size, depending on the pipe direction and 298*4882a593Smuzhiyun * the configurations values 299*4882a593Smuzhiyun */ 300*4882a593Smuzhiyun int usb_maxpacket(struct usb_device *dev, unsigned long pipe) 301*4882a593Smuzhiyun { 302*4882a593Smuzhiyun /* direction is out -> use emaxpacket out */ 303*4882a593Smuzhiyun if ((pipe & USB_DIR_IN) == 0) 304*4882a593Smuzhiyun return dev->epmaxpacketout[((pipe>>15) & 0xf)]; 305*4882a593Smuzhiyun else 306*4882a593Smuzhiyun return dev->epmaxpacketin[((pipe>>15) & 0xf)]; 307*4882a593Smuzhiyun } 308*4882a593Smuzhiyun 309*4882a593Smuzhiyun /* 310*4882a593Smuzhiyun * The routine usb_set_maxpacket_ep() is extracted from the loop of routine 311*4882a593Smuzhiyun * usb_set_maxpacket(), because the optimizer of GCC 4.x chokes on this routine 312*4882a593Smuzhiyun * when it is inlined in 1 single routine. What happens is that the register r3 313*4882a593Smuzhiyun * is used as loop-count 'i', but gets overwritten later on. 314*4882a593Smuzhiyun * This is clearly a compiler bug, but it is easier to workaround it here than 315*4882a593Smuzhiyun * to update the compiler (Occurs with at least several GCC 4.{1,2},x 316*4882a593Smuzhiyun * CodeSourcery compilers like e.g. 2007q3, 2008q1, 2008q3 lite editions on ARM) 317*4882a593Smuzhiyun * 318*4882a593Smuzhiyun * NOTE: Similar behaviour was observed with GCC4.6 on ARMv5. 319*4882a593Smuzhiyun */ 320*4882a593Smuzhiyun static void noinline 321*4882a593Smuzhiyun usb_set_maxpacket_ep(struct usb_device *dev, int if_idx, int ep_idx) 322*4882a593Smuzhiyun { 323*4882a593Smuzhiyun int b; 324*4882a593Smuzhiyun struct usb_endpoint_descriptor *ep; 325*4882a593Smuzhiyun u16 ep_wMaxPacketSize; 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun ep = &dev->config.if_desc[if_idx].ep_desc[ep_idx]; 328*4882a593Smuzhiyun 329*4882a593Smuzhiyun b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 330*4882a593Smuzhiyun ep_wMaxPacketSize = get_unaligned(&ep->wMaxPacketSize); 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 333*4882a593Smuzhiyun USB_ENDPOINT_XFER_CONTROL) { 334*4882a593Smuzhiyun /* Control => bidirectional */ 335*4882a593Smuzhiyun dev->epmaxpacketout[b] = ep_wMaxPacketSize; 336*4882a593Smuzhiyun dev->epmaxpacketin[b] = ep_wMaxPacketSize; 337*4882a593Smuzhiyun debug("##Control EP epmaxpacketout/in[%d] = %d\n", 338*4882a593Smuzhiyun b, dev->epmaxpacketin[b]); 339*4882a593Smuzhiyun } else { 340*4882a593Smuzhiyun if ((ep->bEndpointAddress & 0x80) == 0) { 341*4882a593Smuzhiyun /* OUT Endpoint */ 342*4882a593Smuzhiyun if (ep_wMaxPacketSize > dev->epmaxpacketout[b]) { 343*4882a593Smuzhiyun dev->epmaxpacketout[b] = ep_wMaxPacketSize; 344*4882a593Smuzhiyun debug("##EP epmaxpacketout[%d] = %d\n", 345*4882a593Smuzhiyun b, dev->epmaxpacketout[b]); 346*4882a593Smuzhiyun } 347*4882a593Smuzhiyun } else { 348*4882a593Smuzhiyun /* IN Endpoint */ 349*4882a593Smuzhiyun if (ep_wMaxPacketSize > dev->epmaxpacketin[b]) { 350*4882a593Smuzhiyun dev->epmaxpacketin[b] = ep_wMaxPacketSize; 351*4882a593Smuzhiyun debug("##EP epmaxpacketin[%d] = %d\n", 352*4882a593Smuzhiyun b, dev->epmaxpacketin[b]); 353*4882a593Smuzhiyun } 354*4882a593Smuzhiyun } /* if out */ 355*4882a593Smuzhiyun } /* if control */ 356*4882a593Smuzhiyun } 357*4882a593Smuzhiyun 358*4882a593Smuzhiyun /* 359*4882a593Smuzhiyun * set the max packed value of all endpoints in the given configuration 360*4882a593Smuzhiyun */ 361*4882a593Smuzhiyun static int usb_set_maxpacket(struct usb_device *dev) 362*4882a593Smuzhiyun { 363*4882a593Smuzhiyun int i, ii; 364*4882a593Smuzhiyun 365*4882a593Smuzhiyun for (i = 0; i < dev->config.desc.bNumInterfaces; i++) 366*4882a593Smuzhiyun for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++) 367*4882a593Smuzhiyun usb_set_maxpacket_ep(dev, i, ii); 368*4882a593Smuzhiyun 369*4882a593Smuzhiyun return 0; 370*4882a593Smuzhiyun } 371*4882a593Smuzhiyun 372*4882a593Smuzhiyun /******************************************************************************* 373*4882a593Smuzhiyun * Parse the config, located in buffer, and fills the dev->config structure. 374*4882a593Smuzhiyun * Note that all little/big endian swapping are done automatically. 375*4882a593Smuzhiyun * (wTotalLength has already been swapped and sanitized when it was read.) 376*4882a593Smuzhiyun */ 377*4882a593Smuzhiyun static int usb_parse_config(struct usb_device *dev, 378*4882a593Smuzhiyun unsigned char *buffer, int cfgno) 379*4882a593Smuzhiyun { 380*4882a593Smuzhiyun struct usb_descriptor_header *head; 381*4882a593Smuzhiyun int index, ifno, epno, curr_if_num; 382*4882a593Smuzhiyun u16 ep_wMaxPacketSize; 383*4882a593Smuzhiyun struct usb_interface *if_desc = NULL; 384*4882a593Smuzhiyun 385*4882a593Smuzhiyun ifno = -1; 386*4882a593Smuzhiyun epno = -1; 387*4882a593Smuzhiyun curr_if_num = -1; 388*4882a593Smuzhiyun 389*4882a593Smuzhiyun dev->configno = cfgno; 390*4882a593Smuzhiyun head = (struct usb_descriptor_header *) &buffer[0]; 391*4882a593Smuzhiyun if (head->bDescriptorType != USB_DT_CONFIG) { 392*4882a593Smuzhiyun printf(" ERROR: NOT USB_CONFIG_DESC %x\n", 393*4882a593Smuzhiyun head->bDescriptorType); 394*4882a593Smuzhiyun return -EINVAL; 395*4882a593Smuzhiyun } 396*4882a593Smuzhiyun if (head->bLength != USB_DT_CONFIG_SIZE) { 397*4882a593Smuzhiyun printf("ERROR: Invalid USB CFG length (%d)\n", head->bLength); 398*4882a593Smuzhiyun return -EINVAL; 399*4882a593Smuzhiyun } 400*4882a593Smuzhiyun memcpy(&dev->config, head, USB_DT_CONFIG_SIZE); 401*4882a593Smuzhiyun dev->config.no_of_if = 0; 402*4882a593Smuzhiyun 403*4882a593Smuzhiyun index = dev->config.desc.bLength; 404*4882a593Smuzhiyun /* Ok the first entry must be a configuration entry, 405*4882a593Smuzhiyun * now process the others */ 406*4882a593Smuzhiyun head = (struct usb_descriptor_header *) &buffer[index]; 407*4882a593Smuzhiyun while (index + 1 < dev->config.desc.wTotalLength && head->bLength) { 408*4882a593Smuzhiyun switch (head->bDescriptorType) { 409*4882a593Smuzhiyun case USB_DT_INTERFACE: 410*4882a593Smuzhiyun if (head->bLength != USB_DT_INTERFACE_SIZE) { 411*4882a593Smuzhiyun printf("ERROR: Invalid USB IF length (%d)\n", 412*4882a593Smuzhiyun head->bLength); 413*4882a593Smuzhiyun break; 414*4882a593Smuzhiyun } 415*4882a593Smuzhiyun if (index + USB_DT_INTERFACE_SIZE > 416*4882a593Smuzhiyun dev->config.desc.wTotalLength) { 417*4882a593Smuzhiyun puts("USB IF descriptor overflowed buffer!\n"); 418*4882a593Smuzhiyun break; 419*4882a593Smuzhiyun } 420*4882a593Smuzhiyun if (((struct usb_interface_descriptor *) \ 421*4882a593Smuzhiyun head)->bInterfaceNumber != curr_if_num) { 422*4882a593Smuzhiyun /* this is a new interface, copy new desc */ 423*4882a593Smuzhiyun ifno = dev->config.no_of_if; 424*4882a593Smuzhiyun if (ifno >= USB_MAXINTERFACES) { 425*4882a593Smuzhiyun puts("Too many USB interfaces!\n"); 426*4882a593Smuzhiyun /* try to go on with what we have */ 427*4882a593Smuzhiyun return -EINVAL; 428*4882a593Smuzhiyun } 429*4882a593Smuzhiyun if_desc = &dev->config.if_desc[ifno]; 430*4882a593Smuzhiyun dev->config.no_of_if++; 431*4882a593Smuzhiyun memcpy(if_desc, head, 432*4882a593Smuzhiyun USB_DT_INTERFACE_SIZE); 433*4882a593Smuzhiyun if_desc->no_of_ep = 0; 434*4882a593Smuzhiyun if_desc->num_altsetting = 1; 435*4882a593Smuzhiyun curr_if_num = 436*4882a593Smuzhiyun if_desc->desc.bInterfaceNumber; 437*4882a593Smuzhiyun } else { 438*4882a593Smuzhiyun /* found alternate setting for the interface */ 439*4882a593Smuzhiyun if (ifno >= 0) { 440*4882a593Smuzhiyun if_desc = &dev->config.if_desc[ifno]; 441*4882a593Smuzhiyun if_desc->num_altsetting++; 442*4882a593Smuzhiyun } 443*4882a593Smuzhiyun } 444*4882a593Smuzhiyun break; 445*4882a593Smuzhiyun case USB_DT_ENDPOINT: 446*4882a593Smuzhiyun if (head->bLength != USB_DT_ENDPOINT_SIZE) { 447*4882a593Smuzhiyun printf("ERROR: Invalid USB EP length (%d)\n", 448*4882a593Smuzhiyun head->bLength); 449*4882a593Smuzhiyun break; 450*4882a593Smuzhiyun } 451*4882a593Smuzhiyun if (index + USB_DT_ENDPOINT_SIZE > 452*4882a593Smuzhiyun dev->config.desc.wTotalLength) { 453*4882a593Smuzhiyun puts("USB EP descriptor overflowed buffer!\n"); 454*4882a593Smuzhiyun break; 455*4882a593Smuzhiyun } 456*4882a593Smuzhiyun if (ifno < 0) { 457*4882a593Smuzhiyun puts("Endpoint descriptor out of order!\n"); 458*4882a593Smuzhiyun break; 459*4882a593Smuzhiyun } 460*4882a593Smuzhiyun epno = dev->config.if_desc[ifno].no_of_ep; 461*4882a593Smuzhiyun if_desc = &dev->config.if_desc[ifno]; 462*4882a593Smuzhiyun if (epno >= USB_MAXENDPOINTS) { 463*4882a593Smuzhiyun printf("Interface %d has too many endpoints!\n", 464*4882a593Smuzhiyun if_desc->desc.bInterfaceNumber); 465*4882a593Smuzhiyun return -EINVAL; 466*4882a593Smuzhiyun } 467*4882a593Smuzhiyun /* found an endpoint */ 468*4882a593Smuzhiyun if_desc->no_of_ep++; 469*4882a593Smuzhiyun memcpy(&if_desc->ep_desc[epno], head, 470*4882a593Smuzhiyun USB_DT_ENDPOINT_SIZE); 471*4882a593Smuzhiyun ep_wMaxPacketSize = get_unaligned(&dev->config.\ 472*4882a593Smuzhiyun if_desc[ifno].\ 473*4882a593Smuzhiyun ep_desc[epno].\ 474*4882a593Smuzhiyun wMaxPacketSize); 475*4882a593Smuzhiyun put_unaligned(le16_to_cpu(ep_wMaxPacketSize), 476*4882a593Smuzhiyun &dev->config.\ 477*4882a593Smuzhiyun if_desc[ifno].\ 478*4882a593Smuzhiyun ep_desc[epno].\ 479*4882a593Smuzhiyun wMaxPacketSize); 480*4882a593Smuzhiyun debug("if %d, ep %d\n", ifno, epno); 481*4882a593Smuzhiyun break; 482*4882a593Smuzhiyun case USB_DT_SS_ENDPOINT_COMP: 483*4882a593Smuzhiyun if (head->bLength != USB_DT_SS_EP_COMP_SIZE) { 484*4882a593Smuzhiyun printf("ERROR: Invalid USB EPC length (%d)\n", 485*4882a593Smuzhiyun head->bLength); 486*4882a593Smuzhiyun break; 487*4882a593Smuzhiyun } 488*4882a593Smuzhiyun if (index + USB_DT_SS_EP_COMP_SIZE > 489*4882a593Smuzhiyun dev->config.desc.wTotalLength) { 490*4882a593Smuzhiyun puts("USB EPC descriptor overflowed buffer!\n"); 491*4882a593Smuzhiyun break; 492*4882a593Smuzhiyun } 493*4882a593Smuzhiyun if (ifno < 0 || epno < 0) { 494*4882a593Smuzhiyun puts("EPC descriptor out of order!\n"); 495*4882a593Smuzhiyun break; 496*4882a593Smuzhiyun } 497*4882a593Smuzhiyun if_desc = &dev->config.if_desc[ifno]; 498*4882a593Smuzhiyun memcpy(&if_desc->ss_ep_comp_desc[epno], head, 499*4882a593Smuzhiyun USB_DT_SS_EP_COMP_SIZE); 500*4882a593Smuzhiyun break; 501*4882a593Smuzhiyun default: 502*4882a593Smuzhiyun if (head->bLength == 0) 503*4882a593Smuzhiyun return -EINVAL; 504*4882a593Smuzhiyun 505*4882a593Smuzhiyun debug("unknown Description Type : %x\n", 506*4882a593Smuzhiyun head->bDescriptorType); 507*4882a593Smuzhiyun 508*4882a593Smuzhiyun #ifdef DEBUG 509*4882a593Smuzhiyun { 510*4882a593Smuzhiyun unsigned char *ch = (unsigned char *)head; 511*4882a593Smuzhiyun int i; 512*4882a593Smuzhiyun 513*4882a593Smuzhiyun for (i = 0; i < head->bLength; i++) 514*4882a593Smuzhiyun debug("%02X ", *ch++); 515*4882a593Smuzhiyun debug("\n\n\n"); 516*4882a593Smuzhiyun } 517*4882a593Smuzhiyun #endif 518*4882a593Smuzhiyun break; 519*4882a593Smuzhiyun } 520*4882a593Smuzhiyun index += head->bLength; 521*4882a593Smuzhiyun head = (struct usb_descriptor_header *)&buffer[index]; 522*4882a593Smuzhiyun } 523*4882a593Smuzhiyun 524*4882a593Smuzhiyun /** 525*4882a593Smuzhiyun * Some odd devices respond the Endpoint descriptor items are less 526*4882a593Smuzhiyun * then the bNumEndpoints in Interface descriptor, so fix it here. 527*4882a593Smuzhiyun */ 528*4882a593Smuzhiyun for (ifno = 0; ifno < dev->config.no_of_if; ifno++) { 529*4882a593Smuzhiyun if_desc = &dev->config.if_desc[ifno]; 530*4882a593Smuzhiyun if (if_desc->desc.bNumEndpoints != if_desc->no_of_ep) { 531*4882a593Smuzhiyun printf("WARN: interface %d has %d endpoint descriptor, " 532*4882a593Smuzhiyun "different from the interface descriptor's value: %d\n", 533*4882a593Smuzhiyun ifno, if_desc->no_of_ep, if_desc->desc.bNumEndpoints); 534*4882a593Smuzhiyun if_desc->desc.bNumEndpoints = if_desc->no_of_ep; 535*4882a593Smuzhiyun } 536*4882a593Smuzhiyun } 537*4882a593Smuzhiyun return 0; 538*4882a593Smuzhiyun } 539*4882a593Smuzhiyun 540*4882a593Smuzhiyun /*********************************************************************** 541*4882a593Smuzhiyun * Clears an endpoint 542*4882a593Smuzhiyun * endp: endpoint number in bits 0-3; 543*4882a593Smuzhiyun * direction flag in bit 7 (1 = IN, 0 = OUT) 544*4882a593Smuzhiyun */ 545*4882a593Smuzhiyun int usb_clear_halt(struct usb_device *dev, int pipe) 546*4882a593Smuzhiyun { 547*4882a593Smuzhiyun int result; 548*4882a593Smuzhiyun int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7); 549*4882a593Smuzhiyun 550*4882a593Smuzhiyun result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 551*4882a593Smuzhiyun USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0, 552*4882a593Smuzhiyun endp, NULL, 0, USB_CNTL_TIMEOUT * 3); 553*4882a593Smuzhiyun 554*4882a593Smuzhiyun /* don't clear if failed */ 555*4882a593Smuzhiyun if (result < 0) 556*4882a593Smuzhiyun return result; 557*4882a593Smuzhiyun 558*4882a593Smuzhiyun /* 559*4882a593Smuzhiyun * NOTE: we do not get status and verify reset was successful 560*4882a593Smuzhiyun * as some devices are reported to lock up upon this check.. 561*4882a593Smuzhiyun */ 562*4882a593Smuzhiyun 563*4882a593Smuzhiyun usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); 564*4882a593Smuzhiyun 565*4882a593Smuzhiyun /* toggle is reset on clear */ 566*4882a593Smuzhiyun usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0); 567*4882a593Smuzhiyun return 0; 568*4882a593Smuzhiyun } 569*4882a593Smuzhiyun 570*4882a593Smuzhiyun 571*4882a593Smuzhiyun /********************************************************************** 572*4882a593Smuzhiyun * get_descriptor type 573*4882a593Smuzhiyun */ 574*4882a593Smuzhiyun static int usb_get_descriptor(struct usb_device *dev, unsigned char type, 575*4882a593Smuzhiyun unsigned char index, void *buf, int size) 576*4882a593Smuzhiyun { 577*4882a593Smuzhiyun return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 578*4882a593Smuzhiyun USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 579*4882a593Smuzhiyun (type << 8) + index, 0, buf, size, 580*4882a593Smuzhiyun USB_CNTL_TIMEOUT); 581*4882a593Smuzhiyun } 582*4882a593Smuzhiyun 583*4882a593Smuzhiyun /********************************************************************** 584*4882a593Smuzhiyun * gets len of configuration cfgno 585*4882a593Smuzhiyun */ 586*4882a593Smuzhiyun int usb_get_configuration_len(struct usb_device *dev, int cfgno) 587*4882a593Smuzhiyun { 588*4882a593Smuzhiyun int result; 589*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, 9); 590*4882a593Smuzhiyun struct usb_config_descriptor *config; 591*4882a593Smuzhiyun 592*4882a593Smuzhiyun config = (struct usb_config_descriptor *)&buffer[0]; 593*4882a593Smuzhiyun result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9); 594*4882a593Smuzhiyun if (result < 9) { 595*4882a593Smuzhiyun if (result < 0) 596*4882a593Smuzhiyun printf("unable to get descriptor, error %lX\n", 597*4882a593Smuzhiyun dev->status); 598*4882a593Smuzhiyun else 599*4882a593Smuzhiyun printf("config descriptor too short " \ 600*4882a593Smuzhiyun "(expected %i, got %i)\n", 9, result); 601*4882a593Smuzhiyun return -EIO; 602*4882a593Smuzhiyun } 603*4882a593Smuzhiyun return le16_to_cpu(config->wTotalLength); 604*4882a593Smuzhiyun } 605*4882a593Smuzhiyun 606*4882a593Smuzhiyun /********************************************************************** 607*4882a593Smuzhiyun * gets configuration cfgno and store it in the buffer 608*4882a593Smuzhiyun */ 609*4882a593Smuzhiyun int usb_get_configuration_no(struct usb_device *dev, int cfgno, 610*4882a593Smuzhiyun unsigned char *buffer, int length) 611*4882a593Smuzhiyun { 612*4882a593Smuzhiyun int result; 613*4882a593Smuzhiyun struct usb_config_descriptor *config; 614*4882a593Smuzhiyun 615*4882a593Smuzhiyun config = (struct usb_config_descriptor *)&buffer[0]; 616*4882a593Smuzhiyun result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, length); 617*4882a593Smuzhiyun debug("get_conf_no %d Result %d, wLength %d\n", cfgno, result, 618*4882a593Smuzhiyun le16_to_cpu(config->wTotalLength)); 619*4882a593Smuzhiyun config->wTotalLength = result; /* validated, with CPU byte order */ 620*4882a593Smuzhiyun 621*4882a593Smuzhiyun return result; 622*4882a593Smuzhiyun } 623*4882a593Smuzhiyun 624*4882a593Smuzhiyun /******************************************************************** 625*4882a593Smuzhiyun * set address of a device to the value in dev->devnum. 626*4882a593Smuzhiyun * This can only be done by addressing the device via the default address (0) 627*4882a593Smuzhiyun */ 628*4882a593Smuzhiyun static int usb_set_address(struct usb_device *dev) 629*4882a593Smuzhiyun { 630*4882a593Smuzhiyun debug("set address %d\n", dev->devnum); 631*4882a593Smuzhiyun 632*4882a593Smuzhiyun return usb_control_msg(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS, 633*4882a593Smuzhiyun 0, (dev->devnum), 0, NULL, 0, USB_CNTL_TIMEOUT); 634*4882a593Smuzhiyun } 635*4882a593Smuzhiyun 636*4882a593Smuzhiyun /******************************************************************** 637*4882a593Smuzhiyun * set interface number to interface 638*4882a593Smuzhiyun */ 639*4882a593Smuzhiyun int usb_set_interface(struct usb_device *dev, int interface, int alternate) 640*4882a593Smuzhiyun { 641*4882a593Smuzhiyun struct usb_interface *if_face = NULL; 642*4882a593Smuzhiyun int ret, i; 643*4882a593Smuzhiyun 644*4882a593Smuzhiyun for (i = 0; i < dev->config.desc.bNumInterfaces; i++) { 645*4882a593Smuzhiyun if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) { 646*4882a593Smuzhiyun if_face = &dev->config.if_desc[i]; 647*4882a593Smuzhiyun break; 648*4882a593Smuzhiyun } 649*4882a593Smuzhiyun } 650*4882a593Smuzhiyun if (!if_face) { 651*4882a593Smuzhiyun printf("selecting invalid interface %d", interface); 652*4882a593Smuzhiyun return -EINVAL; 653*4882a593Smuzhiyun } 654*4882a593Smuzhiyun /* 655*4882a593Smuzhiyun * We should return now for devices with only one alternate setting. 656*4882a593Smuzhiyun * According to 9.4.10 of the Universal Serial Bus Specification 657*4882a593Smuzhiyun * Revision 2.0 such devices can return with a STALL. This results in 658*4882a593Smuzhiyun * some USB sticks timeouting during initialization and then being 659*4882a593Smuzhiyun * unusable in U-Boot. 660*4882a593Smuzhiyun */ 661*4882a593Smuzhiyun if (if_face->num_altsetting == 1) 662*4882a593Smuzhiyun return 0; 663*4882a593Smuzhiyun 664*4882a593Smuzhiyun ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 665*4882a593Smuzhiyun USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE, 666*4882a593Smuzhiyun alternate, interface, NULL, 0, 667*4882a593Smuzhiyun USB_CNTL_TIMEOUT * 5); 668*4882a593Smuzhiyun if (ret < 0) 669*4882a593Smuzhiyun return ret; 670*4882a593Smuzhiyun 671*4882a593Smuzhiyun return 0; 672*4882a593Smuzhiyun } 673*4882a593Smuzhiyun 674*4882a593Smuzhiyun /******************************************************************** 675*4882a593Smuzhiyun * set configuration number to configuration 676*4882a593Smuzhiyun */ 677*4882a593Smuzhiyun static int usb_set_configuration(struct usb_device *dev, int configuration) 678*4882a593Smuzhiyun { 679*4882a593Smuzhiyun int res; 680*4882a593Smuzhiyun debug("set configuration %d\n", configuration); 681*4882a593Smuzhiyun /* set setup command */ 682*4882a593Smuzhiyun res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 683*4882a593Smuzhiyun USB_REQ_SET_CONFIGURATION, 0, 684*4882a593Smuzhiyun configuration, 0, 685*4882a593Smuzhiyun NULL, 0, USB_CNTL_TIMEOUT); 686*4882a593Smuzhiyun if (res == 0) { 687*4882a593Smuzhiyun dev->toggle[0] = 0; 688*4882a593Smuzhiyun dev->toggle[1] = 0; 689*4882a593Smuzhiyun return 0; 690*4882a593Smuzhiyun } else 691*4882a593Smuzhiyun return -EIO; 692*4882a593Smuzhiyun } 693*4882a593Smuzhiyun 694*4882a593Smuzhiyun /******************************************************************** 695*4882a593Smuzhiyun * set protocol to protocol 696*4882a593Smuzhiyun */ 697*4882a593Smuzhiyun int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol) 698*4882a593Smuzhiyun { 699*4882a593Smuzhiyun return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 700*4882a593Smuzhiyun USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE, 701*4882a593Smuzhiyun protocol, ifnum, NULL, 0, USB_CNTL_TIMEOUT); 702*4882a593Smuzhiyun } 703*4882a593Smuzhiyun 704*4882a593Smuzhiyun /******************************************************************** 705*4882a593Smuzhiyun * set idle 706*4882a593Smuzhiyun */ 707*4882a593Smuzhiyun int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id) 708*4882a593Smuzhiyun { 709*4882a593Smuzhiyun return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 710*4882a593Smuzhiyun USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, 711*4882a593Smuzhiyun (duration << 8) | report_id, ifnum, NULL, 0, USB_CNTL_TIMEOUT); 712*4882a593Smuzhiyun } 713*4882a593Smuzhiyun 714*4882a593Smuzhiyun /******************************************************************** 715*4882a593Smuzhiyun * get report 716*4882a593Smuzhiyun */ 717*4882a593Smuzhiyun int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type, 718*4882a593Smuzhiyun unsigned char id, void *buf, int size) 719*4882a593Smuzhiyun { 720*4882a593Smuzhiyun return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 721*4882a593Smuzhiyun USB_REQ_GET_REPORT, 722*4882a593Smuzhiyun USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 723*4882a593Smuzhiyun (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT); 724*4882a593Smuzhiyun } 725*4882a593Smuzhiyun 726*4882a593Smuzhiyun /******************************************************************** 727*4882a593Smuzhiyun * get class descriptor 728*4882a593Smuzhiyun */ 729*4882a593Smuzhiyun int usb_get_class_descriptor(struct usb_device *dev, int ifnum, 730*4882a593Smuzhiyun unsigned char type, unsigned char id, void *buf, int size) 731*4882a593Smuzhiyun { 732*4882a593Smuzhiyun return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 733*4882a593Smuzhiyun USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN, 734*4882a593Smuzhiyun (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT); 735*4882a593Smuzhiyun } 736*4882a593Smuzhiyun 737*4882a593Smuzhiyun /******************************************************************** 738*4882a593Smuzhiyun * get string index in buffer 739*4882a593Smuzhiyun */ 740*4882a593Smuzhiyun static int usb_get_string(struct usb_device *dev, unsigned short langid, 741*4882a593Smuzhiyun unsigned char index, void *buf, int size) 742*4882a593Smuzhiyun { 743*4882a593Smuzhiyun int i; 744*4882a593Smuzhiyun int result; 745*4882a593Smuzhiyun 746*4882a593Smuzhiyun for (i = 0; i < 3; ++i) { 747*4882a593Smuzhiyun /* some devices are flaky */ 748*4882a593Smuzhiyun result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 749*4882a593Smuzhiyun USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 750*4882a593Smuzhiyun (USB_DT_STRING << 8) + index, langid, buf, size, 751*4882a593Smuzhiyun USB_CNTL_TIMEOUT); 752*4882a593Smuzhiyun 753*4882a593Smuzhiyun if (result > 0) 754*4882a593Smuzhiyun break; 755*4882a593Smuzhiyun } 756*4882a593Smuzhiyun 757*4882a593Smuzhiyun return result; 758*4882a593Smuzhiyun } 759*4882a593Smuzhiyun 760*4882a593Smuzhiyun 761*4882a593Smuzhiyun static void usb_try_string_workarounds(unsigned char *buf, int *length) 762*4882a593Smuzhiyun { 763*4882a593Smuzhiyun int newlength, oldlength = *length; 764*4882a593Smuzhiyun 765*4882a593Smuzhiyun for (newlength = 2; newlength + 1 < oldlength; newlength += 2) 766*4882a593Smuzhiyun if (!isprint(buf[newlength]) || buf[newlength + 1]) 767*4882a593Smuzhiyun break; 768*4882a593Smuzhiyun 769*4882a593Smuzhiyun if (newlength > 2) { 770*4882a593Smuzhiyun buf[0] = newlength; 771*4882a593Smuzhiyun *length = newlength; 772*4882a593Smuzhiyun } 773*4882a593Smuzhiyun } 774*4882a593Smuzhiyun 775*4882a593Smuzhiyun 776*4882a593Smuzhiyun static int usb_string_sub(struct usb_device *dev, unsigned int langid, 777*4882a593Smuzhiyun unsigned int index, unsigned char *buf) 778*4882a593Smuzhiyun { 779*4882a593Smuzhiyun int rc; 780*4882a593Smuzhiyun 781*4882a593Smuzhiyun /* Try to read the string descriptor by asking for the maximum 782*4882a593Smuzhiyun * possible number of bytes */ 783*4882a593Smuzhiyun rc = usb_get_string(dev, langid, index, buf, 255); 784*4882a593Smuzhiyun 785*4882a593Smuzhiyun /* If that failed try to read the descriptor length, then 786*4882a593Smuzhiyun * ask for just that many bytes */ 787*4882a593Smuzhiyun if (rc < 2) { 788*4882a593Smuzhiyun rc = usb_get_string(dev, langid, index, buf, 2); 789*4882a593Smuzhiyun if (rc == 2) 790*4882a593Smuzhiyun rc = usb_get_string(dev, langid, index, buf, buf[0]); 791*4882a593Smuzhiyun } 792*4882a593Smuzhiyun 793*4882a593Smuzhiyun if (rc >= 2) { 794*4882a593Smuzhiyun if (!buf[0] && !buf[1]) 795*4882a593Smuzhiyun usb_try_string_workarounds(buf, &rc); 796*4882a593Smuzhiyun 797*4882a593Smuzhiyun /* There might be extra junk at the end of the descriptor */ 798*4882a593Smuzhiyun if (buf[0] < rc) 799*4882a593Smuzhiyun rc = buf[0]; 800*4882a593Smuzhiyun 801*4882a593Smuzhiyun rc = rc - (rc & 1); /* force a multiple of two */ 802*4882a593Smuzhiyun } 803*4882a593Smuzhiyun 804*4882a593Smuzhiyun if (rc < 2) 805*4882a593Smuzhiyun rc = -EINVAL; 806*4882a593Smuzhiyun 807*4882a593Smuzhiyun return rc; 808*4882a593Smuzhiyun } 809*4882a593Smuzhiyun 810*4882a593Smuzhiyun 811*4882a593Smuzhiyun /******************************************************************** 812*4882a593Smuzhiyun * usb_string: 813*4882a593Smuzhiyun * Get string index and translate it to ascii. 814*4882a593Smuzhiyun * returns string length (> 0) or error (< 0) 815*4882a593Smuzhiyun */ 816*4882a593Smuzhiyun int usb_string(struct usb_device *dev, int index, char *buf, size_t size) 817*4882a593Smuzhiyun { 818*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ); 819*4882a593Smuzhiyun unsigned char *tbuf; 820*4882a593Smuzhiyun int err; 821*4882a593Smuzhiyun unsigned int u, idx; 822*4882a593Smuzhiyun 823*4882a593Smuzhiyun if (size <= 0 || !buf || !index) 824*4882a593Smuzhiyun return -EINVAL; 825*4882a593Smuzhiyun buf[0] = 0; 826*4882a593Smuzhiyun tbuf = &mybuf[0]; 827*4882a593Smuzhiyun 828*4882a593Smuzhiyun /* get langid for strings if it's not yet known */ 829*4882a593Smuzhiyun if (!dev->have_langid) { 830*4882a593Smuzhiyun err = usb_string_sub(dev, 0, 0, tbuf); 831*4882a593Smuzhiyun if (err < 0) { 832*4882a593Smuzhiyun debug("error getting string descriptor 0 " \ 833*4882a593Smuzhiyun "(error=%lx)\n", dev->status); 834*4882a593Smuzhiyun return -EIO; 835*4882a593Smuzhiyun } else if (tbuf[0] < 4) { 836*4882a593Smuzhiyun debug("string descriptor 0 too short\n"); 837*4882a593Smuzhiyun return -EIO; 838*4882a593Smuzhiyun } else { 839*4882a593Smuzhiyun dev->have_langid = -1; 840*4882a593Smuzhiyun dev->string_langid = tbuf[2] | (tbuf[3] << 8); 841*4882a593Smuzhiyun /* always use the first langid listed */ 842*4882a593Smuzhiyun debug("USB device number %d default " \ 843*4882a593Smuzhiyun "language ID 0x%x\n", 844*4882a593Smuzhiyun dev->devnum, dev->string_langid); 845*4882a593Smuzhiyun } 846*4882a593Smuzhiyun } 847*4882a593Smuzhiyun 848*4882a593Smuzhiyun err = usb_string_sub(dev, dev->string_langid, index, tbuf); 849*4882a593Smuzhiyun if (err < 0) 850*4882a593Smuzhiyun return err; 851*4882a593Smuzhiyun 852*4882a593Smuzhiyun size--; /* leave room for trailing NULL char in output buffer */ 853*4882a593Smuzhiyun for (idx = 0, u = 2; u < err; u += 2) { 854*4882a593Smuzhiyun if (idx >= size) 855*4882a593Smuzhiyun break; 856*4882a593Smuzhiyun if (tbuf[u+1]) /* high byte */ 857*4882a593Smuzhiyun buf[idx++] = '?'; /* non-ASCII character */ 858*4882a593Smuzhiyun else 859*4882a593Smuzhiyun buf[idx++] = tbuf[u]; 860*4882a593Smuzhiyun } 861*4882a593Smuzhiyun buf[idx] = 0; 862*4882a593Smuzhiyun err = idx; 863*4882a593Smuzhiyun return err; 864*4882a593Smuzhiyun } 865*4882a593Smuzhiyun 866*4882a593Smuzhiyun 867*4882a593Smuzhiyun /******************************************************************** 868*4882a593Smuzhiyun * USB device handling: 869*4882a593Smuzhiyun * the USB device are static allocated [USB_MAX_DEVICE]. 870*4882a593Smuzhiyun */ 871*4882a593Smuzhiyun 872*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB) 873*4882a593Smuzhiyun 874*4882a593Smuzhiyun /* returns a pointer to the device with the index [index]. 875*4882a593Smuzhiyun * if the device is not assigned (dev->devnum==-1) returns NULL 876*4882a593Smuzhiyun */ 877*4882a593Smuzhiyun struct usb_device *usb_get_dev_index(int index) 878*4882a593Smuzhiyun { 879*4882a593Smuzhiyun if (usb_dev[index].devnum == -1) 880*4882a593Smuzhiyun return NULL; 881*4882a593Smuzhiyun else 882*4882a593Smuzhiyun return &usb_dev[index]; 883*4882a593Smuzhiyun } 884*4882a593Smuzhiyun 885*4882a593Smuzhiyun int usb_alloc_new_device(struct udevice *controller, struct usb_device **devp) 886*4882a593Smuzhiyun { 887*4882a593Smuzhiyun int i; 888*4882a593Smuzhiyun debug("New Device %d\n", dev_index); 889*4882a593Smuzhiyun if (dev_index == USB_MAX_DEVICE) { 890*4882a593Smuzhiyun printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE); 891*4882a593Smuzhiyun return -ENOSPC; 892*4882a593Smuzhiyun } 893*4882a593Smuzhiyun /* default Address is 0, real addresses start with 1 */ 894*4882a593Smuzhiyun usb_dev[dev_index].devnum = dev_index + 1; 895*4882a593Smuzhiyun usb_dev[dev_index].maxchild = 0; 896*4882a593Smuzhiyun for (i = 0; i < USB_MAXCHILDREN; i++) 897*4882a593Smuzhiyun usb_dev[dev_index].children[i] = NULL; 898*4882a593Smuzhiyun usb_dev[dev_index].parent = NULL; 899*4882a593Smuzhiyun usb_dev[dev_index].controller = controller; 900*4882a593Smuzhiyun dev_index++; 901*4882a593Smuzhiyun *devp = &usb_dev[dev_index - 1]; 902*4882a593Smuzhiyun 903*4882a593Smuzhiyun return 0; 904*4882a593Smuzhiyun } 905*4882a593Smuzhiyun 906*4882a593Smuzhiyun /* 907*4882a593Smuzhiyun * Free the newly created device node. 908*4882a593Smuzhiyun * Called in error cases where configuring a newly attached 909*4882a593Smuzhiyun * device fails for some reason. 910*4882a593Smuzhiyun */ 911*4882a593Smuzhiyun void usb_free_device(struct udevice *controller) 912*4882a593Smuzhiyun { 913*4882a593Smuzhiyun dev_index--; 914*4882a593Smuzhiyun debug("Freeing device node: %d\n", dev_index); 915*4882a593Smuzhiyun memset(&usb_dev[dev_index], 0, sizeof(struct usb_device)); 916*4882a593Smuzhiyun usb_dev[dev_index].devnum = -1; 917*4882a593Smuzhiyun } 918*4882a593Smuzhiyun 919*4882a593Smuzhiyun /* 920*4882a593Smuzhiyun * XHCI issues Enable Slot command and thereafter 921*4882a593Smuzhiyun * allocates device contexts. Provide a weak alias 922*4882a593Smuzhiyun * function for the purpose, so that XHCI overrides it 923*4882a593Smuzhiyun * and EHCI/OHCI just work out of the box. 924*4882a593Smuzhiyun */ 925*4882a593Smuzhiyun __weak int usb_alloc_device(struct usb_device *udev) 926*4882a593Smuzhiyun { 927*4882a593Smuzhiyun return 0; 928*4882a593Smuzhiyun } 929*4882a593Smuzhiyun #endif /* !CONFIG_IS_ENABLED(DM_USB) */ 930*4882a593Smuzhiyun 931*4882a593Smuzhiyun static int usb_hub_port_reset(struct usb_device *dev, struct usb_device *hub) 932*4882a593Smuzhiyun { 933*4882a593Smuzhiyun if (!hub) 934*4882a593Smuzhiyun usb_reset_root_port(dev); 935*4882a593Smuzhiyun 936*4882a593Smuzhiyun return 0; 937*4882a593Smuzhiyun } 938*4882a593Smuzhiyun 939*4882a593Smuzhiyun static int get_descriptor_len(struct usb_device *dev, int len, int expect_len) 940*4882a593Smuzhiyun { 941*4882a593Smuzhiyun __maybe_unused struct usb_device_descriptor *desc; 942*4882a593Smuzhiyun ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ); 943*4882a593Smuzhiyun int err; 944*4882a593Smuzhiyun 945*4882a593Smuzhiyun desc = (struct usb_device_descriptor *)tmpbuf; 946*4882a593Smuzhiyun 947*4882a593Smuzhiyun err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, len); 948*4882a593Smuzhiyun if (err < expect_len) { 949*4882a593Smuzhiyun if (err < 0) { 950*4882a593Smuzhiyun printf("unable to get device descriptor (error=%d)\n", 951*4882a593Smuzhiyun err); 952*4882a593Smuzhiyun return err; 953*4882a593Smuzhiyun } else { 954*4882a593Smuzhiyun printf("USB device descriptor short read (expected %i, got %i)\n", 955*4882a593Smuzhiyun expect_len, err); 956*4882a593Smuzhiyun return -EIO; 957*4882a593Smuzhiyun } 958*4882a593Smuzhiyun } 959*4882a593Smuzhiyun memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor)); 960*4882a593Smuzhiyun 961*4882a593Smuzhiyun return 0; 962*4882a593Smuzhiyun } 963*4882a593Smuzhiyun 964*4882a593Smuzhiyun static int usb_setup_descriptor(struct usb_device *dev, bool do_read) 965*4882a593Smuzhiyun { 966*4882a593Smuzhiyun /* 967*4882a593Smuzhiyun * This is a Windows scheme of initialization sequence, with double 968*4882a593Smuzhiyun * reset of the device (Linux uses the same sequence) 969*4882a593Smuzhiyun * Some equipment is said to work only with such init sequence; this 970*4882a593Smuzhiyun * patch is based on the work by Alan Stern: 971*4882a593Smuzhiyun * http://sourceforge.net/mailarchive/forum.php? 972*4882a593Smuzhiyun * thread_id=5729457&forum_id=5398 973*4882a593Smuzhiyun */ 974*4882a593Smuzhiyun 975*4882a593Smuzhiyun /* 976*4882a593Smuzhiyun * send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is 977*4882a593Smuzhiyun * only 18 bytes long, this will terminate with a short packet. But if 978*4882a593Smuzhiyun * the maxpacket size is 8 or 16 the device may be waiting to transmit 979*4882a593Smuzhiyun * some more, or keeps on retransmitting the 8 byte header. 980*4882a593Smuzhiyun */ 981*4882a593Smuzhiyun 982*4882a593Smuzhiyun if (dev->speed == USB_SPEED_LOW) { 983*4882a593Smuzhiyun dev->descriptor.bMaxPacketSize0 = 8; 984*4882a593Smuzhiyun dev->maxpacketsize = PACKET_SIZE_8; 985*4882a593Smuzhiyun } else { 986*4882a593Smuzhiyun dev->descriptor.bMaxPacketSize0 = 64; 987*4882a593Smuzhiyun dev->maxpacketsize = PACKET_SIZE_64; 988*4882a593Smuzhiyun } 989*4882a593Smuzhiyun dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0; 990*4882a593Smuzhiyun dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0; 991*4882a593Smuzhiyun 992*4882a593Smuzhiyun if (do_read && dev->speed == USB_SPEED_FULL) { 993*4882a593Smuzhiyun int err; 994*4882a593Smuzhiyun 995*4882a593Smuzhiyun /* 996*4882a593Smuzhiyun * Validate we've received only at least 8 bytes, not that 997*4882a593Smuzhiyun * we've received the entire descriptor. The reasoning is: 998*4882a593Smuzhiyun * - The code only uses fields in the first 8 bytes, so 999*4882a593Smuzhiyun * that's all we need to have fetched at this stage. 1000*4882a593Smuzhiyun * - The smallest maxpacket size is 8 bytes. Before we know 1001*4882a593Smuzhiyun * the actual maxpacket the device uses, the USB controller 1002*4882a593Smuzhiyun * may only accept a single packet. Consequently we are only 1003*4882a593Smuzhiyun * guaranteed to receive 1 packet (at least 8 bytes) even in 1004*4882a593Smuzhiyun * a non-error case. 1005*4882a593Smuzhiyun * 1006*4882a593Smuzhiyun * At least the DWC2 controller needs to be programmed with 1007*4882a593Smuzhiyun * the number of packets in addition to the number of bytes. 1008*4882a593Smuzhiyun * A request for 64 bytes of data with the maxpacket guessed 1009*4882a593Smuzhiyun * as 64 (above) yields a request for 1 packet. 1010*4882a593Smuzhiyun */ 1011*4882a593Smuzhiyun err = get_descriptor_len(dev, 64, 8); 1012*4882a593Smuzhiyun if (err) 1013*4882a593Smuzhiyun return err; 1014*4882a593Smuzhiyun } 1015*4882a593Smuzhiyun 1016*4882a593Smuzhiyun dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0; 1017*4882a593Smuzhiyun dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0; 1018*4882a593Smuzhiyun switch (dev->descriptor.bMaxPacketSize0) { 1019*4882a593Smuzhiyun case 8: 1020*4882a593Smuzhiyun dev->maxpacketsize = PACKET_SIZE_8; 1021*4882a593Smuzhiyun break; 1022*4882a593Smuzhiyun case 16: 1023*4882a593Smuzhiyun dev->maxpacketsize = PACKET_SIZE_16; 1024*4882a593Smuzhiyun break; 1025*4882a593Smuzhiyun case 32: 1026*4882a593Smuzhiyun dev->maxpacketsize = PACKET_SIZE_32; 1027*4882a593Smuzhiyun break; 1028*4882a593Smuzhiyun case 64: 1029*4882a593Smuzhiyun dev->maxpacketsize = PACKET_SIZE_64; 1030*4882a593Smuzhiyun break; 1031*4882a593Smuzhiyun default: 1032*4882a593Smuzhiyun printf("%s: invalid max packet size\n", __func__); 1033*4882a593Smuzhiyun return -EIO; 1034*4882a593Smuzhiyun } 1035*4882a593Smuzhiyun 1036*4882a593Smuzhiyun return 0; 1037*4882a593Smuzhiyun } 1038*4882a593Smuzhiyun 1039*4882a593Smuzhiyun static int usb_prepare_device(struct usb_device *dev, int addr, bool do_read, 1040*4882a593Smuzhiyun struct usb_device *parent) 1041*4882a593Smuzhiyun { 1042*4882a593Smuzhiyun int err; 1043*4882a593Smuzhiyun 1044*4882a593Smuzhiyun /* 1045*4882a593Smuzhiyun * Allocate usb 3.0 device context. 1046*4882a593Smuzhiyun * USB 3.0 (xHCI) protocol tries to allocate device slot 1047*4882a593Smuzhiyun * and related data structures first. This call does that. 1048*4882a593Smuzhiyun * Refer to sec 4.3.2 in xHCI spec rev1.0 1049*4882a593Smuzhiyun */ 1050*4882a593Smuzhiyun err = usb_alloc_device(dev); 1051*4882a593Smuzhiyun if (err) { 1052*4882a593Smuzhiyun printf("Cannot allocate device context to get SLOT_ID\n"); 1053*4882a593Smuzhiyun return err; 1054*4882a593Smuzhiyun } 1055*4882a593Smuzhiyun err = usb_setup_descriptor(dev, do_read); 1056*4882a593Smuzhiyun if (err) 1057*4882a593Smuzhiyun return err; 1058*4882a593Smuzhiyun err = usb_hub_port_reset(dev, parent); 1059*4882a593Smuzhiyun if (err) 1060*4882a593Smuzhiyun return err; 1061*4882a593Smuzhiyun 1062*4882a593Smuzhiyun dev->devnum = addr; 1063*4882a593Smuzhiyun 1064*4882a593Smuzhiyun err = usb_set_address(dev); /* set address */ 1065*4882a593Smuzhiyun 1066*4882a593Smuzhiyun if (err < 0) { 1067*4882a593Smuzhiyun printf("\n USB device not accepting new address " \ 1068*4882a593Smuzhiyun "(error=%lX)\n", dev->status); 1069*4882a593Smuzhiyun return err; 1070*4882a593Smuzhiyun } 1071*4882a593Smuzhiyun 1072*4882a593Smuzhiyun mdelay(10); /* Let the SET_ADDRESS settle */ 1073*4882a593Smuzhiyun 1074*4882a593Smuzhiyun /* 1075*4882a593Smuzhiyun * If we haven't read device descriptor before, read it here 1076*4882a593Smuzhiyun * after device is assigned an address. This is only applicable 1077*4882a593Smuzhiyun * to xHCI so far. 1078*4882a593Smuzhiyun */ 1079*4882a593Smuzhiyun if (!do_read) { 1080*4882a593Smuzhiyun err = usb_setup_descriptor(dev, true); 1081*4882a593Smuzhiyun if (err) 1082*4882a593Smuzhiyun return err; 1083*4882a593Smuzhiyun } 1084*4882a593Smuzhiyun 1085*4882a593Smuzhiyun return 0; 1086*4882a593Smuzhiyun } 1087*4882a593Smuzhiyun 1088*4882a593Smuzhiyun int usb_select_config(struct usb_device *dev) 1089*4882a593Smuzhiyun { 1090*4882a593Smuzhiyun unsigned char *tmpbuf = NULL; 1091*4882a593Smuzhiyun int err; 1092*4882a593Smuzhiyun 1093*4882a593Smuzhiyun err = get_descriptor_len(dev, USB_DT_DEVICE_SIZE, USB_DT_DEVICE_SIZE); 1094*4882a593Smuzhiyun if (err) 1095*4882a593Smuzhiyun return err; 1096*4882a593Smuzhiyun 1097*4882a593Smuzhiyun /* correct le values */ 1098*4882a593Smuzhiyun le16_to_cpus(&dev->descriptor.bcdUSB); 1099*4882a593Smuzhiyun le16_to_cpus(&dev->descriptor.idVendor); 1100*4882a593Smuzhiyun le16_to_cpus(&dev->descriptor.idProduct); 1101*4882a593Smuzhiyun le16_to_cpus(&dev->descriptor.bcdDevice); 1102*4882a593Smuzhiyun 1103*4882a593Smuzhiyun /* 1104*4882a593Smuzhiyun * Kingston DT Ultimate 32GB USB 3.0 seems to be extremely sensitive 1105*4882a593Smuzhiyun * about this first Get Descriptor request. If there are any other 1106*4882a593Smuzhiyun * requests in the first microframe, the stick crashes. Wait about 1107*4882a593Smuzhiyun * one microframe duration here (1mS for USB 1.x , 125uS for USB 2.0). 1108*4882a593Smuzhiyun */ 1109*4882a593Smuzhiyun mdelay(1); 1110*4882a593Smuzhiyun 1111*4882a593Smuzhiyun /* only support for one config for now */ 1112*4882a593Smuzhiyun err = usb_get_configuration_len(dev, 0); 1113*4882a593Smuzhiyun if (err >= 0) { 1114*4882a593Smuzhiyun tmpbuf = (unsigned char *)malloc_cache_aligned(err); 1115*4882a593Smuzhiyun if (!tmpbuf) 1116*4882a593Smuzhiyun err = -ENOMEM; 1117*4882a593Smuzhiyun else 1118*4882a593Smuzhiyun err = usb_get_configuration_no(dev, 0, tmpbuf, err); 1119*4882a593Smuzhiyun } 1120*4882a593Smuzhiyun if (err < 0) { 1121*4882a593Smuzhiyun printf("usb_new_device: Cannot read configuration, " \ 1122*4882a593Smuzhiyun "skipping device %04x:%04x\n", 1123*4882a593Smuzhiyun dev->descriptor.idVendor, dev->descriptor.idProduct); 1124*4882a593Smuzhiyun free(tmpbuf); 1125*4882a593Smuzhiyun return err; 1126*4882a593Smuzhiyun } 1127*4882a593Smuzhiyun usb_parse_config(dev, tmpbuf, 0); 1128*4882a593Smuzhiyun free(tmpbuf); 1129*4882a593Smuzhiyun usb_set_maxpacket(dev); 1130*4882a593Smuzhiyun /* 1131*4882a593Smuzhiyun * we set the default configuration here 1132*4882a593Smuzhiyun * This seems premature. If the driver wants a different configuration 1133*4882a593Smuzhiyun * it will need to select itself. 1134*4882a593Smuzhiyun */ 1135*4882a593Smuzhiyun err = usb_set_configuration(dev, dev->config.desc.bConfigurationValue); 1136*4882a593Smuzhiyun if (err < 0) { 1137*4882a593Smuzhiyun printf("failed to set default configuration " \ 1138*4882a593Smuzhiyun "len %d, status %lX\n", dev->act_len, dev->status); 1139*4882a593Smuzhiyun return err; 1140*4882a593Smuzhiyun } 1141*4882a593Smuzhiyun 1142*4882a593Smuzhiyun /* 1143*4882a593Smuzhiyun * Wait until the Set Configuration request gets processed by the 1144*4882a593Smuzhiyun * device. This is required by at least SanDisk Cruzer Pop USB 2.0 1145*4882a593Smuzhiyun * and Kingston DT Ultimate 32GB USB 3.0 on DWC2 OTG controller. 1146*4882a593Smuzhiyun */ 1147*4882a593Smuzhiyun mdelay(10); 1148*4882a593Smuzhiyun 1149*4882a593Smuzhiyun debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", 1150*4882a593Smuzhiyun dev->descriptor.iManufacturer, dev->descriptor.iProduct, 1151*4882a593Smuzhiyun dev->descriptor.iSerialNumber); 1152*4882a593Smuzhiyun memset(dev->mf, 0, sizeof(dev->mf)); 1153*4882a593Smuzhiyun memset(dev->prod, 0, sizeof(dev->prod)); 1154*4882a593Smuzhiyun memset(dev->serial, 0, sizeof(dev->serial)); 1155*4882a593Smuzhiyun if (dev->descriptor.iManufacturer) 1156*4882a593Smuzhiyun usb_string(dev, dev->descriptor.iManufacturer, 1157*4882a593Smuzhiyun dev->mf, sizeof(dev->mf)); 1158*4882a593Smuzhiyun if (dev->descriptor.iProduct) 1159*4882a593Smuzhiyun usb_string(dev, dev->descriptor.iProduct, 1160*4882a593Smuzhiyun dev->prod, sizeof(dev->prod)); 1161*4882a593Smuzhiyun if (dev->descriptor.iSerialNumber) 1162*4882a593Smuzhiyun usb_string(dev, dev->descriptor.iSerialNumber, 1163*4882a593Smuzhiyun dev->serial, sizeof(dev->serial)); 1164*4882a593Smuzhiyun debug("Manufacturer %s\n", dev->mf); 1165*4882a593Smuzhiyun debug("Product %s\n", dev->prod); 1166*4882a593Smuzhiyun debug("SerialNumber %s\n", dev->serial); 1167*4882a593Smuzhiyun 1168*4882a593Smuzhiyun return 0; 1169*4882a593Smuzhiyun } 1170*4882a593Smuzhiyun 1171*4882a593Smuzhiyun int usb_setup_device(struct usb_device *dev, bool do_read, 1172*4882a593Smuzhiyun struct usb_device *parent) 1173*4882a593Smuzhiyun { 1174*4882a593Smuzhiyun int addr; 1175*4882a593Smuzhiyun int ret; 1176*4882a593Smuzhiyun 1177*4882a593Smuzhiyun /* We still haven't set the Address yet */ 1178*4882a593Smuzhiyun addr = dev->devnum; 1179*4882a593Smuzhiyun dev->devnum = 0; 1180*4882a593Smuzhiyun 1181*4882a593Smuzhiyun ret = usb_prepare_device(dev, addr, do_read, parent); 1182*4882a593Smuzhiyun if (ret) 1183*4882a593Smuzhiyun return ret; 1184*4882a593Smuzhiyun ret = usb_select_config(dev); 1185*4882a593Smuzhiyun 1186*4882a593Smuzhiyun return ret; 1187*4882a593Smuzhiyun } 1188*4882a593Smuzhiyun 1189*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB) 1190*4882a593Smuzhiyun /* 1191*4882a593Smuzhiyun * By the time we get here, the device has gotten a new device ID 1192*4882a593Smuzhiyun * and is in the default state. We need to identify the thing and 1193*4882a593Smuzhiyun * get the ball rolling.. 1194*4882a593Smuzhiyun * 1195*4882a593Smuzhiyun * Returns 0 for success, != 0 for error. 1196*4882a593Smuzhiyun */ 1197*4882a593Smuzhiyun int usb_new_device(struct usb_device *dev) 1198*4882a593Smuzhiyun { 1199*4882a593Smuzhiyun bool do_read = true; 1200*4882a593Smuzhiyun int err; 1201*4882a593Smuzhiyun 1202*4882a593Smuzhiyun /* 1203*4882a593Smuzhiyun * XHCI needs to issue a Address device command to setup 1204*4882a593Smuzhiyun * proper device context structures, before it can interact 1205*4882a593Smuzhiyun * with the device. So a get_descriptor will fail before any 1206*4882a593Smuzhiyun * of that is done for XHCI unlike EHCI. 1207*4882a593Smuzhiyun */ 1208*4882a593Smuzhiyun #ifdef CONFIG_USB_XHCI_HCD 1209*4882a593Smuzhiyun do_read = false; 1210*4882a593Smuzhiyun #endif 1211*4882a593Smuzhiyun err = usb_setup_device(dev, do_read, dev->parent); 1212*4882a593Smuzhiyun if (err) 1213*4882a593Smuzhiyun return err; 1214*4882a593Smuzhiyun 1215*4882a593Smuzhiyun /* Now probe if the device is a hub */ 1216*4882a593Smuzhiyun err = usb_hub_probe(dev, 0); 1217*4882a593Smuzhiyun if (err < 0) 1218*4882a593Smuzhiyun return err; 1219*4882a593Smuzhiyun 1220*4882a593Smuzhiyun return 0; 1221*4882a593Smuzhiyun } 1222*4882a593Smuzhiyun #endif 1223*4882a593Smuzhiyun 1224*4882a593Smuzhiyun __weak 1225*4882a593Smuzhiyun int board_usb_init(int index, enum usb_init_type init) 1226*4882a593Smuzhiyun { 1227*4882a593Smuzhiyun return 0; 1228*4882a593Smuzhiyun } 1229*4882a593Smuzhiyun 1230*4882a593Smuzhiyun __weak 1231*4882a593Smuzhiyun int board_usb_cleanup(int index, enum usb_init_type init) 1232*4882a593Smuzhiyun { 1233*4882a593Smuzhiyun return 0; 1234*4882a593Smuzhiyun } 1235*4882a593Smuzhiyun 1236*4882a593Smuzhiyun bool usb_device_has_child_on_port(struct usb_device *parent, int port) 1237*4882a593Smuzhiyun { 1238*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB) 1239*4882a593Smuzhiyun return false; 1240*4882a593Smuzhiyun #else 1241*4882a593Smuzhiyun return parent->children[port] != NULL; 1242*4882a593Smuzhiyun #endif 1243*4882a593Smuzhiyun } 1244*4882a593Smuzhiyun 1245*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB) 1246*4882a593Smuzhiyun void usb_find_usb2_hub_address_port(struct usb_device *udev, 1247*4882a593Smuzhiyun uint8_t *hub_address, uint8_t *hub_port) 1248*4882a593Smuzhiyun { 1249*4882a593Smuzhiyun struct udevice *parent; 1250*4882a593Smuzhiyun struct usb_device *uparent, *ttdev; 1251*4882a593Smuzhiyun 1252*4882a593Smuzhiyun /* 1253*4882a593Smuzhiyun * When called from usb-uclass.c: usb_scan_device() udev->dev points 1254*4882a593Smuzhiyun * to the parent udevice, not the actual udevice belonging to the 1255*4882a593Smuzhiyun * udev as the device is not instantiated yet. So when searching 1256*4882a593Smuzhiyun * for the first usb-2 parent start with udev->dev not 1257*4882a593Smuzhiyun * udev->dev->parent . 1258*4882a593Smuzhiyun */ 1259*4882a593Smuzhiyun ttdev = udev; 1260*4882a593Smuzhiyun parent = udev->dev; 1261*4882a593Smuzhiyun uparent = dev_get_parent_priv(parent); 1262*4882a593Smuzhiyun 1263*4882a593Smuzhiyun while (uparent->speed != USB_SPEED_HIGH) { 1264*4882a593Smuzhiyun struct udevice *dev = parent; 1265*4882a593Smuzhiyun 1266*4882a593Smuzhiyun if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB) { 1267*4882a593Smuzhiyun printf("Error: Cannot find high speed parent of usb-1 device\n"); 1268*4882a593Smuzhiyun *hub_address = 0; 1269*4882a593Smuzhiyun *hub_port = 0; 1270*4882a593Smuzhiyun return; 1271*4882a593Smuzhiyun } 1272*4882a593Smuzhiyun 1273*4882a593Smuzhiyun ttdev = dev_get_parent_priv(dev); 1274*4882a593Smuzhiyun parent = dev->parent; 1275*4882a593Smuzhiyun uparent = dev_get_parent_priv(parent); 1276*4882a593Smuzhiyun } 1277*4882a593Smuzhiyun *hub_address = uparent->devnum; 1278*4882a593Smuzhiyun *hub_port = ttdev->portnr; 1279*4882a593Smuzhiyun } 1280*4882a593Smuzhiyun #else 1281*4882a593Smuzhiyun void usb_find_usb2_hub_address_port(struct usb_device *udev, 1282*4882a593Smuzhiyun uint8_t *hub_address, uint8_t *hub_port) 1283*4882a593Smuzhiyun { 1284*4882a593Smuzhiyun /* Find out the nearest parent which is high speed */ 1285*4882a593Smuzhiyun while (udev->parent->parent != NULL) 1286*4882a593Smuzhiyun if (udev->parent->speed != USB_SPEED_HIGH) { 1287*4882a593Smuzhiyun udev = udev->parent; 1288*4882a593Smuzhiyun } else { 1289*4882a593Smuzhiyun *hub_address = udev->parent->devnum; 1290*4882a593Smuzhiyun *hub_port = udev->portnr; 1291*4882a593Smuzhiyun return; 1292*4882a593Smuzhiyun } 1293*4882a593Smuzhiyun 1294*4882a593Smuzhiyun printf("Error: Cannot find high speed parent of usb-1 device\n"); 1295*4882a593Smuzhiyun *hub_address = 0; 1296*4882a593Smuzhiyun *hub_port = 0; 1297*4882a593Smuzhiyun } 1298*4882a593Smuzhiyun #endif 1299*4882a593Smuzhiyun 1300*4882a593Smuzhiyun 1301*4882a593Smuzhiyun /* EOF */ 1302