wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 2 | * Most of this source has been derived from the Linux USB |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 3 | * project: |
| 4 | * (C) Copyright Linus Torvalds 1999 |
| 5 | * (C) Copyright Johannes Erdfelt 1999-2001 |
| 6 | * (C) Copyright Andreas Gal 1999 |
| 7 | * (C) Copyright Gregory P. Smith 1999 |
| 8 | * (C) Copyright Deti Fliegl 1999 (new USB architecture) |
| 9 | * (C) Copyright Randy Dunlap 2000 |
| 10 | * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id) |
| 11 | * (C) Copyright Yggdrasil Computing, Inc. 2000 |
| 12 | * (usb_device_id matching changes by Adam J. Richter) |
| 13 | * |
| 14 | * Adapted for U-Boot: |
| 15 | * (C) Copyright 2001 Denis Peter, MPL AG Switzerland |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 16 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 17 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 20 | /* |
| 21 | * How it works: |
| 22 | * |
| 23 | * Since this is a bootloader, the devices will not be automatic |
| 24 | * (re)configured on hotplug, but after a restart of the USB the |
| 25 | * device should work. |
| 26 | * |
| 27 | * For each transfer (except "Interrupt") we wait for completion. |
| 28 | */ |
| 29 | #include <common.h> |
| 30 | #include <command.h> |
| 31 | #include <asm/processor.h> |
Mike Frysinger | 6d31114 | 2012-04-04 18:44:53 +0000 | [diff] [blame] | 32 | #include <linux/compiler.h> |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 33 | #include <linux/ctype.h> |
Christian Eggers | 4e0e8d0 | 2008-05-21 22:12:00 +0200 | [diff] [blame] | 34 | #include <asm/byteorder.h> |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 35 | #include <asm/unaligned.h> |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 36 | #include <compiler.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 37 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 38 | #include <usb.h> |
| 39 | #ifdef CONFIG_4xx |
Stefan Roese | ca8725f | 2007-10-03 15:01:02 +0200 | [diff] [blame] | 40 | #include <asm/4xx_pci.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 43 | #define USB_BUFSIZ 512 |
| 44 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 45 | static struct usb_device usb_dev[USB_MAX_DEVICE]; |
| 46 | static int dev_index; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 47 | static int asynch_allowed; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 48 | |
Bartlomiej Sieka | e39589f | 2006-08-03 23:20:13 +0200 | [diff] [blame] | 49 | char usb_started; /* flag for the started/stopped USB status */ |
| 50 | |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 51 | #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT |
| 52 | #define CONFIG_USB_MAX_CONTROLLER_COUNT 1 |
| 53 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 54 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 55 | /*************************************************************************** |
| 56 | * Init USB Device |
| 57 | */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 58 | int usb_init(void) |
| 59 | { |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 60 | void *ctrl; |
| 61 | struct usb_device *dev; |
| 62 | int i, start_index = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 63 | |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 64 | dev_index = 0; |
| 65 | asynch_allowed = 1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 66 | usb_hub_reset(); |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 67 | |
| 68 | /* first make all devices unknown */ |
| 69 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 70 | memset(&usb_dev[i], 0, sizeof(struct usb_device)); |
| 71 | usb_dev[i].devnum = -1; |
| 72 | } |
| 73 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 74 | /* init low_level USB */ |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 75 | for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) { |
| 76 | /* init low_level USB */ |
| 77 | printf("USB%d: ", i); |
Troy Kisky | 8f9c49d | 2013-10-10 15:27:56 -0700 | [diff] [blame] | 78 | if (usb_lowlevel_init(i, USB_INIT_HOST, &ctrl)) { |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 79 | puts("lowlevel init failed\n"); |
| 80 | continue; |
| 81 | } |
| 82 | /* |
| 83 | * lowlevel init is OK, now scan the bus for devices |
| 84 | * i.e. search HUBs and configure them |
| 85 | */ |
| 86 | start_index = dev_index; |
| 87 | printf("scanning bus %d for devices... ", i); |
| 88 | dev = usb_alloc_new_device(ctrl); |
| 89 | /* |
| 90 | * device 0 is always present |
| 91 | * (root hub, so let it analyze) |
| 92 | */ |
| 93 | if (dev) |
| 94 | usb_new_device(dev); |
| 95 | |
| 96 | if (start_index == dev_index) |
| 97 | puts("No USB Device found\n"); |
| 98 | else |
| 99 | printf("%d USB Device(s) found\n", |
| 100 | dev_index - start_index); |
| 101 | |
Bartlomiej Sieka | e39589f | 2006-08-03 23:20:13 +0200 | [diff] [blame] | 102 | usb_started = 1; |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 105 | debug("scan end\n"); |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 106 | /* if we were not able to find at least one working bus, bail out */ |
| 107 | if (!usb_started) { |
| 108 | puts("USB error: all controllers failed lowlevel init\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 109 | return -1; |
| 110 | } |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 111 | |
| 112 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /****************************************************************************** |
| 116 | * Stop USB this stops the LowLevel Part and deregisters USB devices. |
| 117 | */ |
| 118 | int usb_stop(void) |
| 119 | { |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 120 | int i; |
Remy Bohmer | 743dd49 | 2008-08-20 11:22:02 +0200 | [diff] [blame] | 121 | |
| 122 | if (usb_started) { |
| 123 | asynch_allowed = 1; |
| 124 | usb_started = 0; |
| 125 | usb_hub_reset(); |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 126 | |
| 127 | for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) { |
| 128 | if (usb_lowlevel_stop(i)) |
| 129 | printf("failed to stop USB controller %d\n", i); |
| 130 | } |
Remy Bohmer | 743dd49 | 2008-08-20 11:22:02 +0200 | [diff] [blame] | 131 | } |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 132 | |
| 133 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
| 137 | * disables the asynch behaviour of the control message. This is used for data |
| 138 | * transfers that uses the exclusiv access to the control and bulk messages. |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 139 | * Returns the old value so it can be restored later. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 140 | */ |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 141 | int usb_disable_asynch(int disable) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 142 | { |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 143 | int old_value = asynch_allowed; |
| 144 | |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 145 | asynch_allowed = !disable; |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 146 | return old_value; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | /*------------------------------------------------------------------- |
| 151 | * Message wrappers. |
| 152 | * |
| 153 | */ |
| 154 | |
| 155 | /* |
| 156 | * submits an Interrupt Message |
| 157 | */ |
| 158 | int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe, |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 159 | void *buffer, int transfer_len, int interval) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 160 | { |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 161 | return submit_int_msg(dev, pipe, buffer, transfer_len, interval); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * submits a control message and waits for comletion (at least timeout * 1ms) |
| 166 | * If timeout is 0, we don't wait for completion (used as example to set and |
| 167 | * clear keyboards LEDs). For data transfers, (storage transfers) we don't |
| 168 | * allow control messages with 0 timeout, by previousely resetting the flag |
| 169 | * asynch_allowed (usb_disable_asynch(1)). |
| 170 | * returns the transfered length if OK or -1 if error. The transfered length |
| 171 | * and the current status are stored in the dev->act_len and dev->status. |
| 172 | */ |
| 173 | int usb_control_msg(struct usb_device *dev, unsigned int pipe, |
| 174 | unsigned char request, unsigned char requesttype, |
| 175 | unsigned short value, unsigned short index, |
| 176 | void *data, unsigned short size, int timeout) |
| 177 | { |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 178 | ALLOC_CACHE_ALIGN_BUFFER(struct devrequest, setup_packet, 1); |
Marek Vasut | f16090f | 2012-02-13 18:58:18 +0000 | [diff] [blame] | 179 | |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 180 | if ((timeout == 0) && (!asynch_allowed)) { |
| 181 | /* request for a asynch control pipe is not allowed */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 182 | return -1; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 183 | } |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 184 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 185 | /* set setup command */ |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 186 | setup_packet->requesttype = requesttype; |
| 187 | setup_packet->request = request; |
| 188 | setup_packet->value = cpu_to_le16(value); |
| 189 | setup_packet->index = cpu_to_le16(index); |
| 190 | setup_packet->length = cpu_to_le16(size); |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 191 | debug("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \ |
| 192 | "value 0x%X index 0x%X length 0x%X\n", |
| 193 | request, requesttype, value, index, size); |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 194 | dev->status = USB_ST_NOT_PROC; /*not yet processed */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 195 | |
Ilya Yanok | b7161dd | 2012-07-15 04:43:51 +0000 | [diff] [blame] | 196 | if (submit_control_msg(dev, pipe, data, size, setup_packet) < 0) |
| 197 | return -1; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 198 | if (timeout == 0) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 199 | return (int)size; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 200 | |
Remy Bohmer | 93cf093 | 2010-02-01 19:40:47 +0100 | [diff] [blame] | 201 | /* |
| 202 | * Wait for status to update until timeout expires, USB driver |
| 203 | * interrupt handler may set the status when the USB operation has |
| 204 | * been completed. |
| 205 | */ |
| 206 | while (timeout--) { |
| 207 | if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC)) |
| 208 | break; |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 209 | mdelay(1); |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 210 | } |
Remy Bohmer | 93cf093 | 2010-02-01 19:40:47 +0100 | [diff] [blame] | 211 | if (dev->status) |
| 212 | return -1; |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 213 | |
| 214 | return dev->act_len; |
Remy Bohmer | 93cf093 | 2010-02-01 19:40:47 +0100 | [diff] [blame] | 215 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /*------------------------------------------------------------------- |
| 219 | * submits bulk message, and waits for completion. returns 0 if Ok or |
| 220 | * -1 if Error. |
| 221 | * synchronous behavior |
| 222 | */ |
| 223 | int usb_bulk_msg(struct usb_device *dev, unsigned int pipe, |
| 224 | void *data, int len, int *actual_length, int timeout) |
| 225 | { |
| 226 | if (len < 0) |
| 227 | return -1; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 228 | dev->status = USB_ST_NOT_PROC; /*not yet processed */ |
Ilya Yanok | b7161dd | 2012-07-15 04:43:51 +0000 | [diff] [blame] | 229 | if (submit_bulk_msg(dev, pipe, data, len) < 0) |
| 230 | return -1; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 231 | while (timeout--) { |
| 232 | if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC)) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 233 | break; |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 234 | mdelay(1); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 235 | } |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 236 | *actual_length = dev->act_len; |
| 237 | if (dev->status == 0) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 238 | return 0; |
| 239 | else |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /*------------------------------------------------------------------- |
| 245 | * Max Packet stuff |
| 246 | */ |
| 247 | |
| 248 | /* |
| 249 | * returns the max packet size, depending on the pipe direction and |
| 250 | * the configurations values |
| 251 | */ |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 252 | int usb_maxpacket(struct usb_device *dev, unsigned long pipe) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 253 | { |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 254 | /* direction is out -> use emaxpacket out */ |
| 255 | if ((pipe & USB_DIR_IN) == 0) |
Michael Trimarchi | 5f2e7fc | 2008-11-26 17:41:34 +0100 | [diff] [blame] | 256 | return dev->epmaxpacketout[((pipe>>15) & 0xf)]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 257 | else |
Michael Trimarchi | 5f2e7fc | 2008-11-26 17:41:34 +0100 | [diff] [blame] | 258 | return dev->epmaxpacketin[((pipe>>15) & 0xf)]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Marek Vasut | d38ad25 | 2011-11-05 23:35:12 +0100 | [diff] [blame] | 261 | /* |
| 262 | * The routine usb_set_maxpacket_ep() is extracted from the loop of routine |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 263 | * usb_set_maxpacket(), because the optimizer of GCC 4.x chokes on this routine |
| 264 | * when it is inlined in 1 single routine. What happens is that the register r3 |
| 265 | * is used as loop-count 'i', but gets overwritten later on. |
| 266 | * This is clearly a compiler bug, but it is easier to workaround it here than |
| 267 | * to update the compiler (Occurs with at least several GCC 4.{1,2},x |
| 268 | * CodeSourcery compilers like e.g. 2007q3, 2008q1, 2008q3 lite editions on ARM) |
Marek Vasut | d38ad25 | 2011-11-05 23:35:12 +0100 | [diff] [blame] | 269 | * |
| 270 | * NOTE: Similar behaviour was observed with GCC4.6 on ARMv5. |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 271 | */ |
Mike Frysinger | 6d31114 | 2012-04-04 18:44:53 +0000 | [diff] [blame] | 272 | static void noinline |
Marek Vasut | d38ad25 | 2011-11-05 23:35:12 +0100 | [diff] [blame] | 273 | usb_set_maxpacket_ep(struct usb_device *dev, int if_idx, int ep_idx) |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 274 | { |
| 275 | int b; |
Marek Vasut | d38ad25 | 2011-11-05 23:35:12 +0100 | [diff] [blame] | 276 | struct usb_endpoint_descriptor *ep; |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 277 | u16 ep_wMaxPacketSize; |
Marek Vasut | d38ad25 | 2011-11-05 23:35:12 +0100 | [diff] [blame] | 278 | |
| 279 | ep = &dev->config.if_desc[if_idx].ep_desc[ep_idx]; |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 280 | |
| 281 | b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 282 | ep_wMaxPacketSize = get_unaligned(&ep->wMaxPacketSize); |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 283 | |
| 284 | if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
| 285 | USB_ENDPOINT_XFER_CONTROL) { |
| 286 | /* Control => bidirectional */ |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 287 | dev->epmaxpacketout[b] = ep_wMaxPacketSize; |
| 288 | dev->epmaxpacketin[b] = ep_wMaxPacketSize; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 289 | debug("##Control EP epmaxpacketout/in[%d] = %d\n", |
| 290 | b, dev->epmaxpacketin[b]); |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 291 | } else { |
| 292 | if ((ep->bEndpointAddress & 0x80) == 0) { |
| 293 | /* OUT Endpoint */ |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 294 | if (ep_wMaxPacketSize > dev->epmaxpacketout[b]) { |
| 295 | dev->epmaxpacketout[b] = ep_wMaxPacketSize; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 296 | debug("##EP epmaxpacketout[%d] = %d\n", |
| 297 | b, dev->epmaxpacketout[b]); |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 298 | } |
| 299 | } else { |
| 300 | /* IN Endpoint */ |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 301 | if (ep_wMaxPacketSize > dev->epmaxpacketin[b]) { |
| 302 | dev->epmaxpacketin[b] = ep_wMaxPacketSize; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 303 | debug("##EP epmaxpacketin[%d] = %d\n", |
| 304 | b, dev->epmaxpacketin[b]); |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 305 | } |
| 306 | } /* if out */ |
| 307 | } /* if control */ |
| 308 | } |
| 309 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 310 | /* |
| 311 | * set the max packed value of all endpoints in the given configuration |
| 312 | */ |
Marek Vasut | 3a16a6a | 2012-02-13 18:58:16 +0000 | [diff] [blame] | 313 | static int usb_set_maxpacket(struct usb_device *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 314 | { |
Remy Bohmer | cb2df64 | 2008-09-16 14:55:42 +0200 | [diff] [blame] | 315 | int i, ii; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 316 | |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 317 | for (i = 0; i < dev->config.desc.bNumInterfaces; i++) |
| 318 | for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++) |
Marek Vasut | d38ad25 | 2011-11-05 23:35:12 +0100 | [diff] [blame] | 319 | usb_set_maxpacket_ep(dev, i, ii); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 320 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /******************************************************************************* |
| 325 | * Parse the config, located in buffer, and fills the dev->config structure. |
| 326 | * Note that all little/big endian swapping are done automatically. |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 327 | * (wTotalLength has already been swapped and sanitized when it was read.) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 328 | */ |
Marek Vasut | 3a16a6a | 2012-02-13 18:58:16 +0000 | [diff] [blame] | 329 | static int usb_parse_config(struct usb_device *dev, |
| 330 | unsigned char *buffer, int cfgno) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 331 | { |
| 332 | struct usb_descriptor_header *head; |
Bartlomiej Sieka | 978333a | 2006-08-02 00:54:18 +0200 | [diff] [blame] | 333 | int index, ifno, epno, curr_if_num; |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 334 | u16 ep_wMaxPacketSize; |
Vivek Gautam | 23cbd29 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 335 | struct usb_interface *if_desc = NULL; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 336 | |
Bartlomiej Sieka | 978333a | 2006-08-02 00:54:18 +0200 | [diff] [blame] | 337 | ifno = -1; |
| 338 | epno = -1; |
| 339 | curr_if_num = -1; |
| 340 | |
| 341 | dev->configno = cfgno; |
| 342 | head = (struct usb_descriptor_header *) &buffer[0]; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 343 | if (head->bDescriptorType != USB_DT_CONFIG) { |
| 344 | printf(" ERROR: NOT USB_CONFIG_DESC %x\n", |
| 345 | head->bDescriptorType); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 346 | return -1; |
| 347 | } |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 348 | if (head->bLength != USB_DT_CONFIG_SIZE) { |
| 349 | printf("ERROR: Invalid USB CFG length (%d)\n", head->bLength); |
| 350 | return -1; |
| 351 | } |
| 352 | memcpy(&dev->config, head, USB_DT_CONFIG_SIZE); |
Bartlomiej Sieka | 978333a | 2006-08-02 00:54:18 +0200 | [diff] [blame] | 353 | dev->config.no_of_if = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 354 | |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 355 | index = dev->config.desc.bLength; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 356 | /* Ok the first entry must be a configuration entry, |
| 357 | * now process the others */ |
Bartlomiej Sieka | 978333a | 2006-08-02 00:54:18 +0200 | [diff] [blame] | 358 | head = (struct usb_descriptor_header *) &buffer[index]; |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 359 | while (index + 1 < dev->config.desc.wTotalLength && head->bLength) { |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 360 | switch (head->bDescriptorType) { |
| 361 | case USB_DT_INTERFACE: |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 362 | if (head->bLength != USB_DT_INTERFACE_SIZE) { |
| 363 | printf("ERROR: Invalid USB IF length (%d)\n", |
| 364 | head->bLength); |
| 365 | break; |
| 366 | } |
| 367 | if (index + USB_DT_INTERFACE_SIZE > |
| 368 | dev->config.desc.wTotalLength) { |
| 369 | puts("USB IF descriptor overflowed buffer!\n"); |
| 370 | break; |
| 371 | } |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 372 | if (((struct usb_interface_descriptor *) \ |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 373 | head)->bInterfaceNumber != curr_if_num) { |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 374 | /* this is a new interface, copy new desc */ |
| 375 | ifno = dev->config.no_of_if; |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 376 | if (ifno >= USB_MAXINTERFACES) { |
| 377 | puts("Too many USB interfaces!\n"); |
| 378 | /* try to go on with what we have */ |
| 379 | return 1; |
| 380 | } |
Vivek Gautam | 23cbd29 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 381 | if_desc = &dev->config.if_desc[ifno]; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 382 | dev->config.no_of_if++; |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 383 | memcpy(if_desc, head, |
| 384 | USB_DT_INTERFACE_SIZE); |
Vivek Gautam | 23cbd29 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 385 | if_desc->no_of_ep = 0; |
| 386 | if_desc->num_altsetting = 1; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 387 | curr_if_num = |
Vivek Gautam | 23cbd29 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 388 | if_desc->desc.bInterfaceNumber; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 389 | } else { |
| 390 | /* found alternate setting for the interface */ |
Vivek Gautam | 23cbd29 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 391 | if (ifno >= 0) { |
| 392 | if_desc = &dev->config.if_desc[ifno]; |
| 393 | if_desc->num_altsetting++; |
| 394 | } |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 395 | } |
| 396 | break; |
| 397 | case USB_DT_ENDPOINT: |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 398 | if (head->bLength != USB_DT_ENDPOINT_SIZE) { |
| 399 | printf("ERROR: Invalid USB EP length (%d)\n", |
| 400 | head->bLength); |
| 401 | break; |
| 402 | } |
| 403 | if (index + USB_DT_ENDPOINT_SIZE > |
| 404 | dev->config.desc.wTotalLength) { |
| 405 | puts("USB EP descriptor overflowed buffer!\n"); |
| 406 | break; |
| 407 | } |
| 408 | if (ifno < 0) { |
| 409 | puts("Endpoint descriptor out of order!\n"); |
| 410 | break; |
| 411 | } |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 412 | epno = dev->config.if_desc[ifno].no_of_ep; |
Vivek Gautam | 23cbd29 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 413 | if_desc = &dev->config.if_desc[ifno]; |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 414 | if (epno > USB_MAXENDPOINTS) { |
| 415 | printf("Interface %d has too many endpoints!\n", |
| 416 | if_desc->desc.bInterfaceNumber); |
| 417 | return 1; |
| 418 | } |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 419 | /* found an endpoint */ |
Vivek Gautam | 23cbd29 | 2013-04-12 16:34:34 +0530 | [diff] [blame] | 420 | if_desc->no_of_ep++; |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 421 | memcpy(&if_desc->ep_desc[epno], head, |
| 422 | USB_DT_ENDPOINT_SIZE); |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 423 | ep_wMaxPacketSize = get_unaligned(&dev->config.\ |
| 424 | if_desc[ifno].\ |
| 425 | ep_desc[epno].\ |
| 426 | wMaxPacketSize); |
| 427 | put_unaligned(le16_to_cpu(ep_wMaxPacketSize), |
| 428 | &dev->config.\ |
| 429 | if_desc[ifno].\ |
| 430 | ep_desc[epno].\ |
| 431 | wMaxPacketSize); |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 432 | debug("if %d, ep %d\n", ifno, epno); |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 433 | break; |
Vivek Gautam | df3f221 | 2013-04-12 16:34:38 +0530 | [diff] [blame] | 434 | case USB_DT_SS_ENDPOINT_COMP: |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 435 | if (head->bLength != USB_DT_SS_EP_COMP_SIZE) { |
| 436 | printf("ERROR: Invalid USB EPC length (%d)\n", |
| 437 | head->bLength); |
| 438 | break; |
| 439 | } |
| 440 | if (index + USB_DT_SS_EP_COMP_SIZE > |
| 441 | dev->config.desc.wTotalLength) { |
| 442 | puts("USB EPC descriptor overflowed buffer!\n"); |
| 443 | break; |
| 444 | } |
| 445 | if (ifno < 0 || epno < 0) { |
| 446 | puts("EPC descriptor out of order!\n"); |
| 447 | break; |
| 448 | } |
Vivek Gautam | df3f221 | 2013-04-12 16:34:38 +0530 | [diff] [blame] | 449 | if_desc = &dev->config.if_desc[ifno]; |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 450 | memcpy(&if_desc->ss_ep_comp_desc[epno], head, |
| 451 | USB_DT_SS_EP_COMP_SIZE); |
Vivek Gautam | df3f221 | 2013-04-12 16:34:38 +0530 | [diff] [blame] | 452 | break; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 453 | default: |
| 454 | if (head->bLength == 0) |
| 455 | return 1; |
| 456 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 457 | debug("unknown Description Type : %x\n", |
| 458 | head->bDescriptorType); |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 459 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 460 | #ifdef DEBUG |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 461 | { |
Wolfgang Denk | 2410348 | 2011-10-05 23:11:19 +0200 | [diff] [blame] | 462 | unsigned char *ch = (unsigned char *)head; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 463 | int i; |
| 464 | |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 465 | for (i = 0; i < head->bLength; i++) |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 466 | debug("%02X ", *ch++); |
| 467 | debug("\n\n\n"); |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 468 | } |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 469 | #endif |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 470 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 471 | } |
Bartlomiej Sieka | 978333a | 2006-08-02 00:54:18 +0200 | [diff] [blame] | 472 | index += head->bLength; |
| 473 | head = (struct usb_descriptor_header *)&buffer[index]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 474 | } |
| 475 | return 1; |
| 476 | } |
| 477 | |
| 478 | /*********************************************************************** |
| 479 | * Clears an endpoint |
| 480 | * endp: endpoint number in bits 0-3; |
| 481 | * direction flag in bit 7 (1 = IN, 0 = OUT) |
| 482 | */ |
| 483 | int usb_clear_halt(struct usb_device *dev, int pipe) |
| 484 | { |
| 485 | int result; |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 486 | int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 487 | |
| 488 | result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 489 | USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0, |
| 490 | endp, NULL, 0, USB_CNTL_TIMEOUT * 3); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 491 | |
| 492 | /* don't clear if failed */ |
| 493 | if (result < 0) |
| 494 | return result; |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 495 | |
Wolfgang Denk | d06ce5d | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 496 | /* |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 497 | * NOTE: we do not get status and verify reset was successful |
| 498 | * as some devices are reported to lock up upon this check.. |
| 499 | */ |
| 500 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 501 | usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 502 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 503 | /* toggle is reset on clear */ |
| 504 | usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | /********************************************************************** |
| 510 | * get_descriptor type |
| 511 | */ |
Marek Vasut | 3a16a6a | 2012-02-13 18:58:16 +0000 | [diff] [blame] | 512 | static int usb_get_descriptor(struct usb_device *dev, unsigned char type, |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 513 | unsigned char index, void *buf, int size) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 514 | { |
| 515 | int res; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 516 | res = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 517 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| 518 | (type << 8) + index, 0, |
| 519 | buf, size, USB_CNTL_TIMEOUT); |
| 520 | return res; |
| 521 | } |
| 522 | |
| 523 | /********************************************************************** |
| 524 | * gets configuration cfgno and store it in the buffer |
| 525 | */ |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 526 | int usb_get_configuration_no(struct usb_device *dev, |
| 527 | unsigned char *buffer, int cfgno) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 528 | { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 529 | int result; |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 530 | unsigned int length; |
Ilya Yanok | a1cf10f | 2012-11-06 13:48:20 +0000 | [diff] [blame] | 531 | struct usb_config_descriptor *config; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 532 | |
Ilya Yanok | a1cf10f | 2012-11-06 13:48:20 +0000 | [diff] [blame] | 533 | config = (struct usb_config_descriptor *)&buffer[0]; |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 534 | result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9); |
| 535 | if (result < 9) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 536 | if (result < 0) |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 537 | printf("unable to get descriptor, error %lX\n", |
| 538 | dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 539 | else |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 540 | printf("config descriptor too short " \ |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 541 | "(expected %i, got %i)\n", 9, result); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 542 | return -1; |
| 543 | } |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 544 | length = le16_to_cpu(config->wTotalLength); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 545 | |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 546 | if (length > USB_BUFSIZ) { |
| 547 | printf("%s: failed to get descriptor - too long: %d\n", |
| 548 | __func__, length); |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 549 | return -1; |
| 550 | } |
| 551 | |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 552 | result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, length); |
| 553 | debug("get_conf_no %d Result %d, wLength %d\n", cfgno, result, length); |
| 554 | config->wTotalLength = length; /* validated, with CPU byte order */ |
| 555 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 556 | return result; |
| 557 | } |
| 558 | |
| 559 | /******************************************************************** |
| 560 | * set address of a device to the value in dev->devnum. |
| 561 | * This can only be done by addressing the device via the default address (0) |
| 562 | */ |
Marek Vasut | 3a16a6a | 2012-02-13 18:58:16 +0000 | [diff] [blame] | 563 | static int usb_set_address(struct usb_device *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 564 | { |
| 565 | int res; |
| 566 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 567 | debug("set address %d\n", dev->devnum); |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 568 | res = usb_control_msg(dev, usb_snddefctrl(dev), |
| 569 | USB_REQ_SET_ADDRESS, 0, |
| 570 | (dev->devnum), 0, |
| 571 | NULL, 0, USB_CNTL_TIMEOUT); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 572 | return res; |
| 573 | } |
| 574 | |
| 575 | /******************************************************************** |
| 576 | * set interface number to interface |
| 577 | */ |
| 578 | int usb_set_interface(struct usb_device *dev, int interface, int alternate) |
| 579 | { |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 580 | struct usb_interface *if_face = NULL; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 581 | int ret, i; |
| 582 | |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 583 | for (i = 0; i < dev->config.desc.bNumInterfaces; i++) { |
| 584 | if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 585 | if_face = &dev->config.if_desc[i]; |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | if (!if_face) { |
| 590 | printf("selecting invalid interface %d", interface); |
| 591 | return -1; |
| 592 | } |
Bartlomiej Sieka | 978333a | 2006-08-02 00:54:18 +0200 | [diff] [blame] | 593 | /* |
| 594 | * We should return now for devices with only one alternate setting. |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 595 | * According to 9.4.10 of the Universal Serial Bus Specification |
| 596 | * Revision 2.0 such devices can return with a STALL. This results in |
| 597 | * some USB sticks timeouting during initialization and then being |
| 598 | * unusable in U-Boot. |
Bartlomiej Sieka | 978333a | 2006-08-02 00:54:18 +0200 | [diff] [blame] | 599 | */ |
| 600 | if (if_face->num_altsetting == 1) |
| 601 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 602 | |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 603 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 604 | USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE, |
| 605 | alternate, interface, NULL, 0, |
| 606 | USB_CNTL_TIMEOUT * 5); |
| 607 | if (ret < 0) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 608 | return ret; |
| 609 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /******************************************************************** |
| 614 | * set configuration number to configuration |
| 615 | */ |
Marek Vasut | 3a16a6a | 2012-02-13 18:58:16 +0000 | [diff] [blame] | 616 | static int usb_set_configuration(struct usb_device *dev, int configuration) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 617 | { |
| 618 | int res; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 619 | debug("set configuration %d\n", configuration); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 620 | /* set setup command */ |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 621 | res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 622 | USB_REQ_SET_CONFIGURATION, 0, |
| 623 | configuration, 0, |
| 624 | NULL, 0, USB_CNTL_TIMEOUT); |
| 625 | if (res == 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 626 | dev->toggle[0] = 0; |
| 627 | dev->toggle[1] = 0; |
| 628 | return 0; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 629 | } else |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 630 | return -1; |
| 631 | } |
| 632 | |
| 633 | /******************************************************************** |
| 634 | * set protocol to protocol |
| 635 | */ |
| 636 | int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol) |
| 637 | { |
| 638 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 639 | USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 640 | protocol, ifnum, NULL, 0, USB_CNTL_TIMEOUT); |
| 641 | } |
| 642 | |
| 643 | /******************************************************************** |
| 644 | * set idle |
| 645 | */ |
| 646 | int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id) |
| 647 | { |
| 648 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 649 | USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 650 | (duration << 8) | report_id, ifnum, NULL, 0, USB_CNTL_TIMEOUT); |
| 651 | } |
| 652 | |
| 653 | /******************************************************************** |
| 654 | * get report |
| 655 | */ |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 656 | int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type, |
| 657 | unsigned char id, void *buf, int size) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 658 | { |
| 659 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 660 | USB_REQ_GET_REPORT, |
| 661 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 662 | (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /******************************************************************** |
| 666 | * get class descriptor |
| 667 | */ |
| 668 | int usb_get_class_descriptor(struct usb_device *dev, int ifnum, |
| 669 | unsigned char type, unsigned char id, void *buf, int size) |
| 670 | { |
| 671 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 672 | USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN, |
| 673 | (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT); |
| 674 | } |
| 675 | |
| 676 | /******************************************************************** |
| 677 | * get string index in buffer |
| 678 | */ |
Marek Vasut | 3a16a6a | 2012-02-13 18:58:16 +0000 | [diff] [blame] | 679 | static int usb_get_string(struct usb_device *dev, unsigned short langid, |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 680 | unsigned char index, void *buf, int size) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 681 | { |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 682 | int i; |
| 683 | int result; |
| 684 | |
| 685 | for (i = 0; i < 3; ++i) { |
| 686 | /* some devices are flaky */ |
| 687 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 688 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
Wolfgang Denk | d06ce5d | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 689 | (USB_DT_STRING << 8) + index, langid, buf, size, |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 690 | USB_CNTL_TIMEOUT); |
| 691 | |
| 692 | if (result > 0) |
| 693 | break; |
Wolfgang Denk | d06ce5d | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 696 | return result; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 699 | |
| 700 | static void usb_try_string_workarounds(unsigned char *buf, int *length) |
| 701 | { |
| 702 | int newlength, oldlength = *length; |
| 703 | |
| 704 | for (newlength = 2; newlength + 1 < oldlength; newlength += 2) |
| 705 | if (!isprint(buf[newlength]) || buf[newlength + 1]) |
| 706 | break; |
| 707 | |
| 708 | if (newlength > 2) { |
| 709 | buf[0] = newlength; |
| 710 | *length = newlength; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | |
| 715 | static int usb_string_sub(struct usb_device *dev, unsigned int langid, |
| 716 | unsigned int index, unsigned char *buf) |
| 717 | { |
| 718 | int rc; |
| 719 | |
| 720 | /* Try to read the string descriptor by asking for the maximum |
| 721 | * possible number of bytes */ |
| 722 | rc = usb_get_string(dev, langid, index, buf, 255); |
| 723 | |
| 724 | /* If that failed try to read the descriptor length, then |
| 725 | * ask for just that many bytes */ |
| 726 | if (rc < 2) { |
| 727 | rc = usb_get_string(dev, langid, index, buf, 2); |
| 728 | if (rc == 2) |
| 729 | rc = usb_get_string(dev, langid, index, buf, buf[0]); |
| 730 | } |
| 731 | |
| 732 | if (rc >= 2) { |
| 733 | if (!buf[0] && !buf[1]) |
| 734 | usb_try_string_workarounds(buf, &rc); |
| 735 | |
| 736 | /* There might be extra junk at the end of the descriptor */ |
| 737 | if (buf[0] < rc) |
| 738 | rc = buf[0]; |
| 739 | |
| 740 | rc = rc - (rc & 1); /* force a multiple of two */ |
| 741 | } |
| 742 | |
| 743 | if (rc < 2) |
Wolfgang Denk | d06ce5d | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 744 | rc = -1; |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 745 | |
| 746 | return rc; |
| 747 | } |
| 748 | |
| 749 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 750 | /******************************************************************** |
| 751 | * usb_string: |
| 752 | * Get string index and translate it to ascii. |
| 753 | * returns string length (> 0) or error (< 0) |
| 754 | */ |
| 755 | int usb_string(struct usb_device *dev, int index, char *buf, size_t size) |
| 756 | { |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 757 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 758 | unsigned char *tbuf; |
| 759 | int err; |
| 760 | unsigned int u, idx; |
| 761 | |
| 762 | if (size <= 0 || !buf || !index) |
| 763 | return -1; |
| 764 | buf[0] = 0; |
Wolfgang Denk | dc770c7 | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 765 | tbuf = &mybuf[0]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 766 | |
| 767 | /* get langid for strings if it's not yet known */ |
| 768 | if (!dev->have_langid) { |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 769 | err = usb_string_sub(dev, 0, 0, tbuf); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 770 | if (err < 0) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 771 | debug("error getting string descriptor 0 " \ |
| 772 | "(error=%lx)\n", dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 773 | return -1; |
| 774 | } else if (tbuf[0] < 4) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 775 | debug("string descriptor 0 too short\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 776 | return -1; |
| 777 | } else { |
| 778 | dev->have_langid = -1; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 779 | dev->string_langid = tbuf[2] | (tbuf[3] << 8); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 780 | /* always use the first langid listed */ |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 781 | debug("USB device number %d default " \ |
| 782 | "language ID 0x%x\n", |
| 783 | dev->devnum, dev->string_langid); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
wdenk | f70cbb2 | 2004-02-23 20:48:38 +0000 | [diff] [blame] | 786 | |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 787 | err = usb_string_sub(dev, dev->string_langid, index, tbuf); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 788 | if (err < 0) |
| 789 | return err; |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 790 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 791 | size--; /* leave room for trailing NULL char in output buffer */ |
| 792 | for (idx = 0, u = 2; u < err; u += 2) { |
| 793 | if (idx >= size) |
| 794 | break; |
| 795 | if (tbuf[u+1]) /* high byte */ |
| 796 | buf[idx++] = '?'; /* non-ASCII character */ |
| 797 | else |
| 798 | buf[idx++] = tbuf[u]; |
| 799 | } |
| 800 | buf[idx] = 0; |
| 801 | err = idx; |
| 802 | return err; |
| 803 | } |
| 804 | |
| 805 | |
| 806 | /******************************************************************** |
| 807 | * USB device handling: |
| 808 | * the USB device are static allocated [USB_MAX_DEVICE]. |
| 809 | */ |
| 810 | |
| 811 | |
| 812 | /* returns a pointer to the device with the index [index]. |
| 813 | * if the device is not assigned (dev->devnum==-1) returns NULL |
| 814 | */ |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 815 | struct usb_device *usb_get_dev_index(int index) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 816 | { |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 817 | if (usb_dev[index].devnum == -1) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 818 | return NULL; |
| 819 | else |
| 820 | return &usb_dev[index]; |
| 821 | } |
| 822 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 823 | /* returns a pointer of a new device structure or NULL, if |
| 824 | * no device struct is available |
| 825 | */ |
Lucas Stach | a323128 | 2012-09-26 00:14:34 +0200 | [diff] [blame] | 826 | struct usb_device *usb_alloc_new_device(void *controller) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 827 | { |
| 828 | int i; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 829 | debug("New Device %d\n", dev_index); |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 830 | if (dev_index == USB_MAX_DEVICE) { |
| 831 | printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 832 | return NULL; |
| 833 | } |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 834 | /* default Address is 0, real addresses start with 1 */ |
| 835 | usb_dev[dev_index].devnum = dev_index + 1; |
| 836 | usb_dev[dev_index].maxchild = 0; |
| 837 | for (i = 0; i < USB_MAXCHILDREN; i++) |
| 838 | usb_dev[dev_index].children[i] = NULL; |
| 839 | usb_dev[dev_index].parent = NULL; |
Lucas Stach | a323128 | 2012-09-26 00:14:34 +0200 | [diff] [blame] | 840 | usb_dev[dev_index].controller = controller; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 841 | dev_index++; |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 842 | return &usb_dev[dev_index - 1]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Milind Choudhary | ebb2478 | 2012-12-12 17:55:28 -0800 | [diff] [blame] | 845 | /* |
| 846 | * Free the newly created device node. |
| 847 | * Called in error cases where configuring a newly attached |
| 848 | * device fails for some reason. |
| 849 | */ |
| 850 | void usb_free_device(void) |
| 851 | { |
| 852 | dev_index--; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 853 | debug("Freeing device node: %d\n", dev_index); |
Milind Choudhary | ebb2478 | 2012-12-12 17:55:28 -0800 | [diff] [blame] | 854 | memset(&usb_dev[dev_index], 0, sizeof(struct usb_device)); |
| 855 | usb_dev[dev_index].devnum = -1; |
| 856 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 857 | |
| 858 | /* |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 859 | * XHCI issues Enable Slot command and thereafter |
| 860 | * allocates device contexts. Provide a weak alias |
| 861 | * function for the purpose, so that XHCI overrides it |
| 862 | * and EHCI/OHCI just work out of the box. |
| 863 | */ |
| 864 | __weak int usb_alloc_device(struct usb_device *udev) |
| 865 | { |
| 866 | return 0; |
| 867 | } |
| 868 | /* |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 869 | * By the time we get here, the device has gotten a new device ID |
| 870 | * and is in the default state. We need to identify the thing and |
| 871 | * get the ball rolling.. |
| 872 | * |
| 873 | * Returns 0 for success, != 0 for error. |
| 874 | */ |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 875 | int usb_new_device(struct usb_device *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 876 | { |
| 877 | int addr, err; |
| 878 | int tmp; |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 879 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 880 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 881 | /* |
| 882 | * Allocate usb 3.0 device context. |
| 883 | * USB 3.0 (xHCI) protocol tries to allocate device slot |
| 884 | * and related data structures first. This call does that. |
| 885 | * Refer to sec 4.3.2 in xHCI spec rev1.0 |
| 886 | */ |
| 887 | if (usb_alloc_device(dev)) { |
| 888 | printf("Cannot allocate device context to get SLOT_ID\n"); |
| 889 | return -1; |
| 890 | } |
| 891 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 892 | /* We still haven't set the Address yet */ |
| 893 | addr = dev->devnum; |
| 894 | dev->devnum = 0; |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 895 | |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 896 | #ifdef CONFIG_LEGACY_USB_INIT_SEQ |
| 897 | /* this is the old and known way of initializing devices, it is |
| 898 | * different than what Windows and Linux are doing. Windows and Linux |
| 899 | * both retrieve 64 bytes while reading the device descriptor |
| 900 | * Several USB stick devices report ERR: CTL_TIMEOUT, caused by an |
| 901 | * invalid header while reading 8 bytes as device descriptor. */ |
| 902 | dev->descriptor.bMaxPacketSize0 = 8; /* Start off at 8 bytes */ |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 903 | dev->maxpacketsize = PACKET_SIZE_8; |
Michael Trimarchi | 5f2e7fc | 2008-11-26 17:41:34 +0100 | [diff] [blame] | 904 | dev->epmaxpacketin[0] = 8; |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 905 | dev->epmaxpacketout[0] = 8; |
| 906 | |
Ilya Yanok | da87587 | 2012-07-15 04:43:50 +0000 | [diff] [blame] | 907 | err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, tmpbuf, 8); |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 908 | if (err < 8) { |
| 909 | printf("\n USB device not responding, " \ |
Michael Trimarchi | 5f2e7fc | 2008-11-26 17:41:34 +0100 | [diff] [blame] | 910 | "giving up (status=%lX)\n", dev->status); |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 911 | return 1; |
| 912 | } |
Ilya Yanok | da87587 | 2012-07-15 04:43:50 +0000 | [diff] [blame] | 913 | memcpy(&dev->descriptor, tmpbuf, 8); |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 914 | #else |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 915 | /* This is a Windows scheme of initialization sequence, with double |
| 916 | * reset of the device (Linux uses the same sequence) |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 917 | * Some equipment is said to work only with such init sequence; this |
| 918 | * patch is based on the work by Alan Stern: |
Michael Trimarchi | 5f2e7fc | 2008-11-26 17:41:34 +0100 | [diff] [blame] | 919 | * http://sourceforge.net/mailarchive/forum.php? |
| 920 | * thread_id=5729457&forum_id=5398 |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 921 | */ |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 922 | __maybe_unused struct usb_device_descriptor *desc; |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 923 | int port = -1; |
| 924 | struct usb_device *parent = dev->parent; |
| 925 | unsigned short portstatus; |
| 926 | |
| 927 | /* send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is |
| 928 | * only 18 bytes long, this will terminate with a short packet. But if |
| 929 | * the maxpacket size is 8 or 16 the device may be waiting to transmit |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 930 | * some more, or keeps on retransmitting the 8 byte header. */ |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 931 | |
| 932 | desc = (struct usb_device_descriptor *)tmpbuf; |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 933 | dev->descriptor.bMaxPacketSize0 = 64; /* Start off at 64 bytes */ |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 934 | /* Default to 64 byte max packet size */ |
| 935 | dev->maxpacketsize = PACKET_SIZE_64; |
Michael Trimarchi | 5f2e7fc | 2008-11-26 17:41:34 +0100 | [diff] [blame] | 936 | dev->epmaxpacketin[0] = 64; |
Remy Bohmer | aca9b6e | 2008-09-16 14:55:44 +0200 | [diff] [blame] | 937 | dev->epmaxpacketout[0] = 64; |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 938 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 939 | /* |
| 940 | * XHCI needs to issue a Address device command to setup |
| 941 | * proper device context structures, before it can interact |
| 942 | * with the device. So a get_descriptor will fail before any |
| 943 | * of that is done for XHCI unlike EHCI. |
| 944 | */ |
| 945 | #ifndef CONFIG_USB_XHCI |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 946 | err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, 64); |
| 947 | if (err < 0) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 948 | debug("usb_new_device: usb_get_descriptor() failed\n"); |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 949 | return 1; |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 950 | } |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 951 | |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 952 | dev->descriptor.bMaxPacketSize0 = desc->bMaxPacketSize0; |
Vivek Gautam | de70166 | 2013-04-12 16:34:36 +0530 | [diff] [blame] | 953 | /* |
| 954 | * Fetch the device class, driver can use this info |
| 955 | * to differentiate between HUB and DEVICE. |
| 956 | */ |
| 957 | dev->descriptor.bDeviceClass = desc->bDeviceClass; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 958 | #endif |
Wolfgang Denk | d06ce5d | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 959 | |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 960 | if (parent) { |
Remy Bohmer | 47e7208 | 2008-10-10 10:23:21 +0200 | [diff] [blame] | 961 | int j; |
Wolfgang Denk | d06ce5d | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 962 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 963 | /* find the port number we're at */ |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 964 | for (j = 0; j < parent->maxchild; j++) { |
| 965 | if (parent->children[j] == dev) { |
| 966 | port = j; |
| 967 | break; |
| 968 | } |
| 969 | } |
| 970 | if (port < 0) { |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 971 | printf("usb_new_device:cannot locate device's port.\n"); |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 972 | return 1; |
| 973 | } |
| 974 | |
| 975 | /* reset the port for the second time */ |
| 976 | err = hub_port_reset(dev->parent, port, &portstatus); |
| 977 | if (err < 0) { |
| 978 | printf("\n Couldn't reset port %i\n", port); |
| 979 | return 1; |
| 980 | } |
| 981 | } |
Wolfgang Denk | c7a4f7d | 2005-07-21 11:57:57 +0200 | [diff] [blame] | 982 | #endif |
| 983 | |
Michael Trimarchi | 5f2e7fc | 2008-11-26 17:41:34 +0100 | [diff] [blame] | 984 | dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 985 | dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0; |
| 986 | switch (dev->descriptor.bMaxPacketSize0) { |
Michael Trimarchi | 5f2e7fc | 2008-11-26 17:41:34 +0100 | [diff] [blame] | 987 | case 8: |
| 988 | dev->maxpacketsize = PACKET_SIZE_8; |
| 989 | break; |
| 990 | case 16: |
| 991 | dev->maxpacketsize = PACKET_SIZE_16; |
| 992 | break; |
| 993 | case 32: |
| 994 | dev->maxpacketsize = PACKET_SIZE_32; |
| 995 | break; |
| 996 | case 64: |
| 997 | dev->maxpacketsize = PACKET_SIZE_64; |
| 998 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 999 | } |
| 1000 | dev->devnum = addr; |
| 1001 | |
| 1002 | err = usb_set_address(dev); /* set address */ |
| 1003 | |
| 1004 | if (err < 0) { |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1005 | printf("\n USB device not accepting new address " \ |
| 1006 | "(error=%lX)\n", dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1007 | return 1; |
| 1008 | } |
| 1009 | |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 1010 | mdelay(10); /* Let the SET_ADDRESS settle */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1011 | |
| 1012 | tmp = sizeof(dev->descriptor); |
| 1013 | |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1014 | err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, |
Ilya Yanok | da87587 | 2012-07-15 04:43:50 +0000 | [diff] [blame] | 1015 | tmpbuf, sizeof(dev->descriptor)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1016 | if (err < tmp) { |
| 1017 | if (err < 0) |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1018 | printf("unable to get device descriptor (error=%d)\n", |
| 1019 | err); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1020 | else |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1021 | printf("USB device descriptor short read " \ |
| 1022 | "(expected %i, got %i)\n", tmp, err); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1023 | return 1; |
| 1024 | } |
Ilya Yanok | da87587 | 2012-07-15 04:43:50 +0000 | [diff] [blame] | 1025 | memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1026 | /* correct le values */ |
Christian Eggers | 4e0e8d0 | 2008-05-21 22:12:00 +0200 | [diff] [blame] | 1027 | le16_to_cpus(&dev->descriptor.bcdUSB); |
| 1028 | le16_to_cpus(&dev->descriptor.idVendor); |
| 1029 | le16_to_cpus(&dev->descriptor.idProduct); |
| 1030 | le16_to_cpus(&dev->descriptor.bcdDevice); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1031 | /* only support for one config for now */ |
Vincent Palatin | 6a23866 | 2012-07-24 07:12:02 +0000 | [diff] [blame] | 1032 | err = usb_get_configuration_no(dev, tmpbuf, 0); |
| 1033 | if (err < 0) { |
| 1034 | printf("usb_new_device: Cannot read configuration, " \ |
| 1035 | "skipping device %04x:%04x\n", |
| 1036 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 1037 | return -1; |
| 1038 | } |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 1039 | usb_parse_config(dev, tmpbuf, 0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1040 | usb_set_maxpacket(dev); |
| 1041 | /* we set the default configuration here */ |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 1042 | if (usb_set_configuration(dev, dev->config.desc.bConfigurationValue)) { |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1043 | printf("failed to set default configuration " \ |
| 1044 | "len %d, status %lX\n", dev->act_len, dev->status); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1045 | return -1; |
| 1046 | } |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1047 | debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", |
| 1048 | dev->descriptor.iManufacturer, dev->descriptor.iProduct, |
| 1049 | dev->descriptor.iSerialNumber); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1050 | memset(dev->mf, 0, sizeof(dev->mf)); |
| 1051 | memset(dev->prod, 0, sizeof(dev->prod)); |
| 1052 | memset(dev->serial, 0, sizeof(dev->serial)); |
| 1053 | if (dev->descriptor.iManufacturer) |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1054 | usb_string(dev, dev->descriptor.iManufacturer, |
| 1055 | dev->mf, sizeof(dev->mf)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1056 | if (dev->descriptor.iProduct) |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1057 | usb_string(dev, dev->descriptor.iProduct, |
| 1058 | dev->prod, sizeof(dev->prod)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1059 | if (dev->descriptor.iSerialNumber) |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1060 | usb_string(dev, dev->descriptor.iSerialNumber, |
| 1061 | dev->serial, sizeof(dev->serial)); |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 1062 | debug("Manufacturer %s\n", dev->mf); |
| 1063 | debug("Product %s\n", dev->prod); |
| 1064 | debug("SerialNumber %s\n", dev->serial); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1065 | /* now prode if the device is a hub */ |
Remy Bohmer | 172a113 | 2008-09-16 14:55:43 +0200 | [diff] [blame] | 1066 | usb_hub_probe(dev, 0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1067 | return 0; |
| 1068 | } |
| 1069 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 1070 | __weak |
Troy Kisky | de8ae7b | 2013-10-10 15:27:55 -0700 | [diff] [blame] | 1071 | int board_usb_init(int index, enum usb_init_type init) |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 1072 | { |
| 1073 | return 0; |
| 1074 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1075 | /* EOF */ |