Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* This file implements the UDC (usb device controller) layer to be used with |
| 30 | * the new dwc controller. |
| 31 | * It exposes APIs to initialize UDC (and thus usb) and perform data transfer |
| 32 | * over usb. |
| 33 | */ |
| 34 | |
| 35 | #include <reg.h> |
| 36 | #include <debug.h> |
| 37 | #include <string.h> |
| 38 | #include <malloc.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <arch/defines.h> |
| 41 | #include <dev/udc.h> |
| 42 | #include <platform/iomap.h> |
| 43 | #include <usb30_dwc.h> |
| 44 | #include <usb30_wrapper.h> |
| 45 | #include <usb30_udc.h> |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 46 | #include <smem.h> |
| 47 | #include <board.h> |
| 48 | #include <platform/timer.h> |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 49 | |
| 50 | //#define DEBUG_USB |
| 51 | |
| 52 | #ifdef DEBUG_USB |
| 53 | #define DBG(...) dprintf(ALWAYS, __VA_ARGS__) |
| 54 | #else |
| 55 | #define DBG(...) |
| 56 | #endif |
| 57 | |
| 58 | #define ERR(...) dprintf(ALWAYS, __VA_ARGS__) |
| 59 | |
| 60 | /* control data transfer is max 512 bytes */ |
| 61 | #define UDC_CONTROL_RX_BUF_SIZE 512 |
| 62 | #define UDC_CONTROL_TX_BUF_SIZE 512 |
| 63 | |
| 64 | /* Buffer used by dwc driver to process events. |
| 65 | * Must be multiple of 4: snps 6.2.7.2. |
| 66 | */ |
| 67 | #define UDC_DWC_EVENT_BUF_SIZE 4096 |
| 68 | |
| 69 | /* macro to parse setup request */ |
| 70 | #define SETUP(type,request) (((type) << 8) | (request)) |
| 71 | |
| 72 | /* macro to generate bit representation of an EP */ |
| 73 | #define EPT_TX(n) (1 << ((n) + 16)) |
| 74 | #define EPT_RX(n) (1 << (n)) |
| 75 | |
| 76 | /* Local functions */ |
| 77 | static struct udc_descriptor *udc_descriptor_alloc(uint32_t type, |
| 78 | uint32_t num, |
| 79 | uint32_t len, |
| 80 | udc_desc_spec_t spec); |
| 81 | static uint8_t udc_string_desc_alloc(udc_t *udc, const char *str); |
| 82 | |
| 83 | static void udc_descriptor_register(udc_t *udc, struct udc_descriptor *desc); |
| 84 | static void udc_register_language_desc(udc_t *udc); |
| 85 | static void udc_register_bos_desc(udc_t *udc); |
| 86 | static void udc_register_device_desc_usb_20(udc_t *udc, struct udc_device *dev_info); |
| 87 | static void udc_register_device_desc_usb_30(udc_t *udc, struct udc_device *dev_info); |
| 88 | static void udc_register_config_desc_usb20(udc_t *udc, struct udc_gadget *gadget); |
| 89 | static void udc_register_config_desc_usb30(udc_t *udc, struct udc_gadget *gadget); |
| 90 | |
| 91 | static void udc_ept_desc_fill(struct udc_endpoint *ept, uint8_t *data); |
| 92 | static void udc_ept_comp_desc_fill(struct udc_endpoint *ept, uint8_t *data); |
| 93 | |
| 94 | static void udc_dwc_notify(void *context, dwc_notify_event_t event); |
| 95 | static int udc_handle_setup(void *context, uint8_t *data); |
| 96 | |
| 97 | /* TODO: This must be the only global var in this file, for now. |
| 98 | * Ideally, all APIs should be sending |
| 99 | * this to us and this ptr should be kept outside of this code. |
| 100 | * This needs change in the common udc APIs and thus keeping it here until that |
| 101 | * is done. |
| 102 | */ |
| 103 | static udc_t *udc_dev = NULL; |
| 104 | |
| 105 | |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 106 | __WEAK int platform_is_8974() |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 107 | { |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 108 | return 0; |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 111 | __WEAK int platform_is_8974Pro() |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 112 | { |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static void phy_mux_configure(void) |
| 117 | { |
| 118 | /* configuring of hs phy mux is different for some platforms. */ |
| 119 | target_usb_phy_mux_configure(); |
| 120 | } |
| 121 | |
| 122 | static void phy_reset(usb_wrapper_dev_t *wrapper) |
| 123 | { |
| 124 | /* phy reset is different for some platforms. */ |
| 125 | if (platform_is_8974() || platform_is_8974Pro()) |
| 126 | { |
| 127 | /* SS PHY */ |
| 128 | usb_wrapper_ss_phy_reset(wrapper); |
| 129 | |
| 130 | /* For 8974: hs phy is reset as part of soft reset. |
| 131 | * No need for explicit reset. |
| 132 | */ |
| 133 | } |
| 134 | else if (board_platform_id() == APQ8084) |
| 135 | { |
| 136 | target_usb_phy_reset(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* Initialize HS phy */ |
| 141 | void hs_phy_init(udc_t *dev) |
| 142 | { |
| 143 | /* only for 8974 */ |
| 144 | if (platform_is_8974() || platform_is_8974Pro()) |
| 145 | { |
| 146 | /* 5.a, 5.b */ |
| 147 | usb_wrapper_hs_phy_init(dev->wrapper_dev); |
| 148 | |
| 149 | /* 5.d */ |
| 150 | dwc_usb2_phy_soft_reset(dev->dwc); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* vbus override */ |
| 155 | void vbus_override(udc_t *dev) |
| 156 | { |
| 157 | /* when vbus signal is not available directly to the controller, |
| 158 | * simulate vbus presense. |
| 159 | */ |
| 160 | if (board_platform_id() == APQ8084) |
| 161 | { |
| 162 | usb_wrapper_vbus_override(dev->wrapper_dev); |
| 163 | } |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | /* Initialize usb wrapper and dwc h/w blocks. */ |
| 168 | static void usb30_init(void) |
| 169 | { |
| 170 | usb_wrapper_dev_t* wrapper; |
| 171 | usb_wrapper_config_t wrapper_config; |
| 172 | |
| 173 | dwc_dev_t *dwc; |
| 174 | dwc_config_t dwc_config; |
| 175 | |
| 176 | /* initialize the usb wrapper h/w block */ |
| 177 | wrapper_config.qscratch_base = (void*) MSM_USB30_QSCRATCH_BASE; |
| 178 | |
| 179 | wrapper = usb_wrapper_init(&wrapper_config); |
| 180 | ASSERT(wrapper); |
| 181 | |
| 182 | /* save the wrapper ptr */ |
| 183 | udc_dev->wrapper_dev = wrapper; |
| 184 | |
| 185 | /* initialize the dwc device block */ |
| 186 | dwc_config.base = (void*) MSM_USB30_BASE; |
| 187 | |
| 188 | /* buffer must be aligned to buf size. snps 8.2.2 */ |
| 189 | dwc_config.event_buf = memalign(lcm(CACHE_LINE, UDC_DWC_EVENT_BUF_SIZE), |
| 190 | ROUNDUP(UDC_DWC_EVENT_BUF_SIZE, CACHE_LINE)); |
| 191 | ASSERT(dwc_config.event_buf); |
| 192 | |
| 193 | dwc_config.event_buf_size = UDC_DWC_EVENT_BUF_SIZE; |
| 194 | |
| 195 | /* notify handler */ |
| 196 | dwc_config.notify_context = udc_dev; |
| 197 | dwc_config.notify = udc_dwc_notify; |
| 198 | |
| 199 | /* setup handler */ |
| 200 | dwc_config.setup_context = udc_dev; |
| 201 | dwc_config.setup_handler = udc_handle_setup; |
| 202 | |
| 203 | dwc = dwc_init(&dwc_config); |
| 204 | ASSERT(dwc); |
| 205 | |
| 206 | /* save the dwc dev ptr */ |
| 207 | udc_dev->dwc = dwc; |
| 208 | |
| 209 | |
| 210 | /* USB3.0 core and phy initialization as described in HPG */ |
| 211 | |
| 212 | /* section 4.4.1 Control sequence */ |
| 213 | usb_wrapper_dbm_mode(wrapper, DBM_MODE_BYPASS); |
| 214 | |
| 215 | /* section 4.4.1: use config 0 - all of RAM1 */ |
| 216 | usb_wrapper_ram_configure(wrapper); |
| 217 | |
| 218 | /* section 4.4.2: Initialization and configuration sequences */ |
| 219 | |
| 220 | /* 1. UTMI Mux configuration */ |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 221 | phy_mux_configure(); |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 222 | |
| 223 | /* 2. Put controller in reset */ |
| 224 | dwc_reset(dwc, 1); |
| 225 | |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 226 | /* Steps 3 - 7 must be done while dwc is in reset condition */ |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 227 | |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 228 | /* 3. Reset PHY */ |
| 229 | phy_reset(wrapper); |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 230 | |
| 231 | /* 4. SS phy config */ |
| 232 | usb_wrapper_ss_phy_configure(wrapper); |
| 233 | |
| 234 | /* 5. HS phy init */ |
| 235 | usb_wrapper_hs_phy_init(wrapper); |
| 236 | |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 237 | /* 6. hs phy config */ |
| 238 | usb_wrapper_hs_phy_configure(wrapper); |
| 239 | |
| 240 | /* 7. Reset PHY digital interface */ |
| 241 | dwc_phy_digital_reset(dwc); |
| 242 | |
| 243 | /* 8. Bring dwc controller out of reset */ |
| 244 | dwc_reset(dwc, 0); |
| 245 | |
| 246 | /* 9. */ |
| 247 | usb_wrapper_ss_phy_electrical_config(wrapper); |
| 248 | |
| 249 | /* 10. */ |
| 250 | usb_wrapper_workaround_10(wrapper); |
| 251 | |
| 252 | /* 11. */ |
| 253 | usb_wrapper_workaround_11(wrapper); |
| 254 | |
| 255 | /* 12. */ |
| 256 | dwc_ss_phy_workaround_12(dwc); |
| 257 | |
| 258 | /* 13. */ |
| 259 | usb_wrapper_workaround_13(wrapper); |
| 260 | |
| 261 | /* 14. needed only for host mode. ignored. */ |
| 262 | |
Amol Jadi | 5418da3 | 2013-10-11 14:14:47 -0700 | [diff] [blame] | 263 | /* If the target does not support vbus detection in controller, |
| 264 | * simulate vbus presence. |
| 265 | */ |
| 266 | vbus_override(udc_dev); |
| 267 | |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 268 | /* 15 - 20 */ |
| 269 | dwc_device_init(dwc); |
| 270 | } |
| 271 | |
| 272 | /* udc_init: creates and registers various usb descriptor */ |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 273 | int usb30_udc_init(struct udc_device *dev_info) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 274 | { |
| 275 | /* create and initialize udc instance */ |
| 276 | udc_dev = (udc_t*) malloc(sizeof(udc_t)); |
| 277 | ASSERT(udc_dev); |
| 278 | |
| 279 | /* initialize everything to 0 */ |
| 280 | memset(udc_dev, 0 , sizeof(udc_t)); |
| 281 | |
| 282 | /* malloc control data buffers */ |
| 283 | udc_dev->ctrl_rx_buf = memalign(CACHE_LINE, ROUNDUP(UDC_CONTROL_RX_BUF_SIZE, CACHE_LINE)); |
| 284 | ASSERT(udc_dev->ctrl_rx_buf); |
| 285 | |
| 286 | udc_dev->ctrl_tx_buf = memalign(CACHE_LINE, ROUNDUP(UDC_CONTROL_TX_BUF_SIZE, CACHE_LINE)); |
| 287 | ASSERT(udc_dev->ctrl_tx_buf); |
| 288 | |
| 289 | /* initialize string id */ |
| 290 | udc_dev->next_string_id = 1; |
| 291 | |
| 292 | /* Initialize ept data */ |
| 293 | /* alloc table to assume EP0 In/OUT are already allocated.*/ |
| 294 | udc_dev->ept_alloc_table = EPT_TX(0) | EPT_RX(0); |
| 295 | udc_dev->ept_list = NULL; |
| 296 | |
| 297 | usb30_init(); |
| 298 | |
| 299 | /* register descriptors */ |
| 300 | udc_register_language_desc(udc_dev); |
| 301 | udc_register_device_desc_usb_20(udc_dev, dev_info); |
| 302 | udc_register_device_desc_usb_30(udc_dev, dev_info); |
| 303 | udc_register_bos_desc(udc_dev); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* application registers its gadget by calling this func. |
| 309 | * gadget == interface descriptor |
| 310 | */ |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 311 | int usb30_udc_register_gadget(struct udc_gadget *gadget) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 312 | { |
| 313 | ASSERT(gadget); |
| 314 | |
| 315 | /* check if already registered */ |
| 316 | if (udc_dev->gadget) |
| 317 | { |
| 318 | ERR("\nonly one gadget supported\n"); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | /* create our configuration descriptors based on this gadget data */ |
| 323 | udc_register_config_desc_usb20(udc_dev, gadget); |
| 324 | udc_register_config_desc_usb30(udc_dev, gadget); |
| 325 | |
| 326 | /* save the gadget */ |
| 327 | udc_dev->gadget = gadget; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* udc_start: */ |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 333 | int usb30_udc_start(void) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 334 | { |
| 335 | /* 19. run |
| 336 | * enable device to receive SOF packets and |
| 337 | * respond to control transfers on EP0 and generate events. |
| 338 | */ |
| 339 | dwc_device_run(udc_dev->dwc, 1); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /* Control data rx callback. Called by DWC layer when it receives control |
| 345 | * data from host. |
| 346 | */ |
| 347 | void udc_control_rx_callback(void *context, unsigned actual, int status) |
| 348 | { |
| 349 | udc_t *udc = (udc_t *) context; |
| 350 | |
| 351 | /* Force reload of buffer update by controller from memory */ |
| 352 | arch_invalidate_cache_range((addr_t) udc->ctrl_rx_buf, actual); |
| 353 | |
| 354 | /* TODO: for now, there is only one 3-stage write during 3.0 enumeration |
| 355 | * (SET_SEL), which causes this callback. Ideally, set_periodic() must |
| 356 | * be based on which control rx just happened. |
| 357 | * Also, the value of 0x65 should depend on the data received for SET_SEL. |
| 358 | * For now, this value works just fine. |
| 359 | */ |
| 360 | dwc_device_set_periodic_param(udc->dwc, 0x65); |
| 361 | } |
| 362 | |
| 363 | /* lookup request name for debug purposes */ |
| 364 | static const char *reqname(uint32_t r) |
| 365 | { |
| 366 | switch (r) { |
| 367 | case GET_STATUS: |
| 368 | return "GET_STATUS"; |
| 369 | case CLEAR_FEATURE: |
| 370 | return "CLEAR_FEATURE"; |
| 371 | case SET_FEATURE: |
| 372 | return "SET_FEATURE"; |
| 373 | case SET_ADDRESS: |
| 374 | return "SET_ADDRESS"; |
| 375 | case GET_DESCRIPTOR: |
| 376 | return "GET_DESCRIPTOR"; |
| 377 | case SET_DESCRIPTOR: |
| 378 | return "SET_DESCRIPTOR"; |
| 379 | case GET_CONFIGURATION: |
| 380 | return "GET_CONFIGURATION"; |
| 381 | case SET_CONFIGURATION: |
| 382 | return "SET_CONFIGURATION"; |
| 383 | case GET_INTERFACE: |
| 384 | return "GET_INTERFACE"; |
| 385 | case SET_INTERFACE: |
| 386 | return "SET_INTERFACE"; |
| 387 | case SET_SEL: |
| 388 | return "SET_SEL"; |
| 389 | default: |
| 390 | return "*UNKNOWN*"; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* callback function called by DWC layer when a setup packed is received. |
| 395 | * the return value tells dwc layer whether this setup pkt results in |
| 396 | * a 2-stage or a 3-stage control transfer or stall. |
| 397 | */ |
| 398 | static int udc_handle_setup(void *context, uint8_t *data) |
| 399 | { |
| 400 | udc_t *udc = (udc_t *) context; |
| 401 | uint32_t len; |
| 402 | |
| 403 | ASSERT(udc); |
| 404 | |
| 405 | dwc_dev_t *dwc = udc->dwc; |
| 406 | ASSERT(dwc); |
| 407 | |
| 408 | struct setup_packet s = *((struct setup_packet*) data); |
| 409 | |
| 410 | DBG("\n SETUP request: \n type = 0x%x \n request = 0x%x \n value = 0x%x" |
| 411 | " \n index = 0x%x \n length = 0x%x\n", |
| 412 | s.type, s.request, s.value, s.index, s.length); |
| 413 | |
| 414 | switch (SETUP(s.type, s.request)) |
| 415 | { |
| 416 | case SETUP(DEVICE_READ, GET_STATUS): |
| 417 | { |
| 418 | DBG("\n DEVICE_READ : GET_STATUS: value = %d index = %d" |
| 419 | " length = %d", s.value, s.index, s.length); |
| 420 | |
| 421 | if (s.length == 2) { |
| 422 | |
| 423 | uint16_t zero = 0; |
| 424 | len = 2; |
| 425 | |
| 426 | /* copy to tx buffer */ |
| 427 | memcpy(udc->ctrl_tx_buf, &zero, len); |
| 428 | |
| 429 | /* flush buffer to main memory before queueing the request */ |
| 430 | arch_clean_invalidate_cache_range((addr_t) udc->ctrl_tx_buf, len); |
| 431 | |
| 432 | dwc_transfer_request(udc->dwc, |
| 433 | 0, |
| 434 | DWC_EP_DIRECTION_IN, |
| 435 | udc->ctrl_tx_buf, |
| 436 | len, |
| 437 | NULL, |
| 438 | NULL); |
| 439 | |
| 440 | return DWC_SETUP_3_STAGE; |
| 441 | } |
| 442 | } |
| 443 | break; |
| 444 | case SETUP(DEVICE_READ, GET_DESCRIPTOR): |
| 445 | { |
| 446 | DBG("\n DEVICE_READ : GET_DESCRIPTOR: value = %d", s.value); |
| 447 | |
| 448 | /* setup usb ep0-IN to send our device descriptor */ |
| 449 | struct udc_descriptor *desc; |
| 450 | |
| 451 | for (desc = udc->desc_list; desc; desc = desc->next) |
| 452 | { |
| 453 | /* tag must match the value AND |
| 454 | * if speed is SS, desc must comply with 30 spec OR |
| 455 | * if speed is not SS, desc must comply with 20 spec. |
| 456 | */ |
| 457 | if ((desc->tag == s.value) && |
| 458 | (((udc->speed == UDC_SPEED_SS) && (desc->spec & UDC_DESC_SPEC_30)) || |
| 459 | ((udc->speed != UDC_SPEED_SS) && (desc->spec & UDC_DESC_SPEC_20))) |
| 460 | ) |
| 461 | { |
| 462 | if (desc->len > s.length) |
| 463 | len = s.length; |
| 464 | else |
| 465 | len = desc->len; |
| 466 | |
| 467 | /* copy to tx buffer */ |
| 468 | memcpy(udc->ctrl_tx_buf, desc->data, len); |
| 469 | |
| 470 | /* flush buffer to main memory before queueing the request */ |
| 471 | arch_clean_invalidate_cache_range((addr_t) udc->ctrl_tx_buf, len); |
| 472 | |
| 473 | dwc_transfer_request(udc->dwc, |
| 474 | 0, |
| 475 | DWC_EP_DIRECTION_IN, |
| 476 | udc->ctrl_tx_buf, |
| 477 | len, |
| 478 | NULL, |
| 479 | NULL); |
| 480 | |
| 481 | return DWC_SETUP_3_STAGE; |
| 482 | } |
| 483 | } |
| 484 | DBG("\n Did not find matching descriptor: = 0x%x", s.value); |
| 485 | } |
| 486 | break; |
| 487 | case SETUP(DEVICE_READ, GET_CONFIGURATION): |
| 488 | { |
| 489 | DBG("\n DEVICE_READ : GET_CONFIGURATION"); |
| 490 | |
| 491 | if ((s.value == 0) && (s.index == 0) && (s.length == 1)) { |
| 492 | |
| 493 | len = 1; |
| 494 | |
| 495 | /* copy to tx buffer */ |
| 496 | memcpy(udc->ctrl_tx_buf, &udc->config_selected, len); |
| 497 | |
| 498 | /* flush buffer to main memory before queueing the request */ |
| 499 | arch_clean_invalidate_cache_range((addr_t) udc->ctrl_tx_buf, len); |
| 500 | |
| 501 | dwc_transfer_request(udc->dwc, |
| 502 | 0, |
| 503 | DWC_EP_DIRECTION_IN, |
| 504 | udc->ctrl_tx_buf, |
| 505 | len, |
| 506 | NULL, |
| 507 | NULL); |
| 508 | |
| 509 | return DWC_SETUP_3_STAGE; |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | ASSERT(0); |
| 514 | } |
| 515 | } |
| 516 | break; |
| 517 | case SETUP(DEVICE_WRITE, SET_CONFIGURATION): |
| 518 | { |
| 519 | DBG("\n DEVICE_WRITE : SET_CONFIGURATION"); |
| 520 | |
| 521 | /* select configuration 1 */ |
| 522 | if (s.value == 1) { |
| 523 | struct udc_endpoint *ept; |
| 524 | /* enable endpoints */ |
| 525 | for (ept = udc->ept_list; ept; ept = ept->next) { |
| 526 | if (ept->num == 0) |
| 527 | continue; |
| 528 | else |
| 529 | { |
| 530 | /* add this ep to dwc ep list */ |
| 531 | dwc_ep_t ep; |
| 532 | |
| 533 | ep.number = ept->num; |
| 534 | ep.dir = ept->in; |
| 535 | ep.type = EP_TYPE_BULK; /* the only one supported */ |
| 536 | ep.max_pkt_size = ept->maxpkt; |
| 537 | ep.burst_size = ept->maxburst; |
| 538 | ep.zlp = 0; /* TODO: zlp could be made part of ept */ |
| 539 | ep.trb_count = ept->trb_count; |
| 540 | ep.trb = ept->trb; |
| 541 | |
| 542 | dwc_device_add_ep(dwc, ep); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* now that we have saved the non-control EP details, set config */ |
| 547 | dwc_device_set_configuration(dwc); |
| 548 | |
| 549 | /* inform client that we are configured. */ |
| 550 | udc->gadget->notify(udc_dev->gadget, UDC_EVENT_ONLINE); |
| 551 | |
| 552 | udc->config_selected = 1; |
| 553 | |
| 554 | return DWC_SETUP_2_STAGE; |
| 555 | } |
| 556 | else if (s.value == 0) |
| 557 | { |
| 558 | /* 0 == de-configure. */ |
| 559 | udc->config_selected = 0; |
| 560 | DBG("\n\n CONFIG = 0 !!!!!!!!!\n\n"); |
| 561 | return DWC_SETUP_2_STAGE; |
| 562 | /* TODO: do proper handling for de-config */ |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | ERR("\n CONFIG = %d not supported\n", s.value); |
| 567 | ASSERT(0); |
| 568 | } |
| 569 | } |
| 570 | break; |
| 571 | case SETUP(DEVICE_WRITE, SET_ADDRESS): |
| 572 | { |
| 573 | DBG("\n DEVICE_WRITE : SET_ADDRESS"); |
| 574 | |
| 575 | dwc_device_set_addr(dwc, s.value); |
| 576 | return DWC_SETUP_2_STAGE; |
| 577 | } |
| 578 | break; |
| 579 | case SETUP(INTERFACE_WRITE, SET_INTERFACE): |
| 580 | { |
| 581 | DBG("\n DEVICE_WRITE : SET_INTERFACE"); |
| 582 | /* if we ack this everything hangs */ |
| 583 | /* per spec, STALL is valid if there is not alt func */ |
| 584 | goto stall; |
| 585 | } |
| 586 | break; |
| 587 | case SETUP(DEVICE_WRITE, SET_FEATURE): |
| 588 | { |
| 589 | DBG("\n DEVICE_WRITE : SET_FEATURE"); |
| 590 | goto stall; |
| 591 | } |
| 592 | break; |
| 593 | case SETUP(DEVICE_WRITE, CLEAR_FEATURE): |
| 594 | { |
| 595 | DBG("\n DEVICE_WRITE : CLEAR_FEATURE"); |
| 596 | goto stall; |
| 597 | } |
| 598 | break; |
| 599 | case SETUP(ENDPOINT_WRITE, CLEAR_FEATURE): |
| 600 | { |
| 601 | DBG("\n DEVICE_WRITE : CLEAR_FEATURE"); |
| 602 | goto stall; |
| 603 | } |
| 604 | break; |
| 605 | case SETUP(DEVICE_WRITE, SET_SEL): |
| 606 | { |
| 607 | DBG("\n DEVICE_WRITE : SET_SEL"); |
| 608 | |
| 609 | /* this is 3-stage write. need to receive data of s.length size. */ |
| 610 | if (s.length > 0) { |
| 611 | dwc_transfer_request(udc->dwc, |
| 612 | 0, |
| 613 | DWC_EP_DIRECTION_OUT, |
| 614 | udc->ctrl_rx_buf, |
| 615 | UDC_CONTROL_RX_BUF_SIZE, |
| 616 | udc_control_rx_callback, |
| 617 | (void *) udc); |
| 618 | return DWC_SETUP_3_STAGE; |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | /* length must be non-zero */ |
| 623 | ASSERT(0); |
| 624 | } |
| 625 | } |
| 626 | break; |
| 627 | |
| 628 | default: |
| 629 | ERR("\n Unknown setup req.\n type = 0x%x value = %d index = %d" |
| 630 | " length = %d\n", s.type, s.value, s.index, s.length); |
| 631 | ASSERT(0); |
| 632 | } |
| 633 | |
| 634 | stall: |
| 635 | ERR("\nSTALL. Unsupported setup req: %s %d %d %d %d %d\n", |
| 636 | reqname(s.request), s.type, s.request, s.value, s.index, s.length); |
| 637 | |
| 638 | return DWC_SETUP_ERROR; |
| 639 | } |
| 640 | |
| 641 | /* Callback function called by DWC layer when a request to transfer data |
| 642 | * on non-control EP is completed. |
| 643 | */ |
| 644 | void udc_request_complete(void *context, uint32_t actual, int status) |
| 645 | { |
| 646 | struct udc_request *req = ((udc_t *) context)->queued_req; |
| 647 | |
| 648 | DBG("\n UDC: udc_request_callback: xferred %d bytes status = %d\n", |
| 649 | actual, status); |
| 650 | |
| 651 | /* clear the queued request. */ |
| 652 | ((udc_t *) context)->queued_req = NULL; |
| 653 | |
| 654 | if (req->complete) |
| 655 | { |
| 656 | req->complete(req, actual, status); |
| 657 | } |
| 658 | |
| 659 | DBG("\n UDC: udc_request_callback: done fastboot callback\n"); |
| 660 | } |
| 661 | |
| 662 | /* App interface to queue in data transfer requests for control and data ep */ |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 663 | int usb30_udc_request_queue(struct udc_endpoint *ept, struct udc_request *req) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 664 | { |
| 665 | int ret; |
| 666 | dwc_dev_t *dwc_dev = udc_dev->dwc; |
| 667 | |
| 668 | /* ensure device is initialized before queuing request */ |
| 669 | ASSERT(dwc_dev); |
| 670 | |
| 671 | /* if device is not configured, return error */ |
| 672 | if(udc_dev->config_selected == 0) |
| 673 | { |
| 674 | return -1; |
| 675 | } |
| 676 | |
| 677 | /* only one request at a time is supported. |
| 678 | * check if a request is already queued. |
| 679 | */ |
| 680 | if(udc_dev->queued_req) |
| 681 | { |
| 682 | return -1; |
| 683 | } |
| 684 | |
| 685 | DBG("\n udc_request_queue: entry: ep_usb_num = %d", ept->num); |
| 686 | |
| 687 | /* save the queued request. */ |
| 688 | udc_dev->queued_req = req; |
| 689 | |
| 690 | ret = dwc_transfer_request(dwc_dev, |
| 691 | ept->num, |
| 692 | ept->in ? DWC_EP_DIRECTION_IN : DWC_EP_DIRECTION_OUT, |
| 693 | req->buf, |
| 694 | req->length, |
| 695 | udc_request_complete, |
| 696 | (void *) udc_dev); |
| 697 | |
| 698 | DBG("\n udc_request_queue: exit: ep_usb_num = %d", ept->num); |
| 699 | |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | /* callback function called by dwc layer if any dwc event occurs */ |
| 704 | void udc_dwc_notify(void *context, dwc_notify_event_t event) |
| 705 | { |
| 706 | udc_t *udc = (udc_t *) context; |
| 707 | |
| 708 | switch (event) |
| 709 | { |
| 710 | case DWC_NOTIFY_EVENT_CONNECTED_LS: |
| 711 | udc->speed = UDC_SPEED_LS; |
| 712 | break; |
| 713 | case DWC_NOTIFY_EVENT_CONNECTED_FS: |
| 714 | udc->speed = UDC_SPEED_FS; |
| 715 | break; |
| 716 | case DWC_NOTIFY_EVENT_CONNECTED_HS: |
| 717 | udc->speed = UDC_SPEED_HS; |
| 718 | break; |
| 719 | case DWC_NOTIFY_EVENT_CONNECTED_SS: |
| 720 | udc->speed = UDC_SPEED_SS; |
| 721 | break; |
| 722 | case DWC_NOTIFY_EVENT_DISCONNECTED: |
| 723 | case DWC_NOTIFY_EVENT_OFFLINE: |
| 724 | udc->config_selected = 0; |
| 725 | if (udc->gadget && udc->gadget->notify) |
| 726 | udc->gadget->notify(udc->gadget, UDC_EVENT_OFFLINE); |
| 727 | break; |
| 728 | default: |
| 729 | ASSERT(0); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | |
| 734 | /******************* Function related to descriptor allocation etc.************/ |
| 735 | |
| 736 | static struct udc_endpoint *_udc_endpoint_alloc(uint8_t num, |
| 737 | uint8_t in, |
| 738 | uint16_t max_pkt) |
| 739 | { |
| 740 | struct udc_endpoint *ept; |
| 741 | udc_t *udc = udc_dev; |
| 742 | |
| 743 | ept = malloc(sizeof(*ept)); |
| 744 | ASSERT(ept); |
| 745 | |
| 746 | ept->maxpkt = max_pkt; |
| 747 | ept->num = num; |
| 748 | ept->in = !!in; |
| 749 | ept->maxburst = 4; /* no performance improvement is seen beyond burst size of 4 */ |
| 750 | ept->trb_count = 66; /* each trb can transfer (16MB - 1). 65 for 1GB transfer + 1 for roundup/zero length pkt. */ |
| 751 | ept->trb = memalign(lcm(CACHE_LINE, 16), ROUNDUP(ept->trb_count*sizeof(dwc_trb_t), CACHE_LINE)); /* TRB must be aligned to 16 */ |
| 752 | ASSERT(ept->trb); |
| 753 | |
| 754 | /* push it on top of ept_list */ |
| 755 | ept->next = udc->ept_list; |
| 756 | udc->ept_list = ept; |
| 757 | |
| 758 | return ept; |
| 759 | } |
| 760 | |
| 761 | /* Called to create non-control in/out End Point structures by the APP */ |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 762 | struct udc_endpoint *usb30_udc_endpoint_alloc(unsigned type, unsigned maxpkt) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 763 | { |
| 764 | struct udc_endpoint *ept; |
| 765 | uint8_t in; |
| 766 | uint8_t n; |
| 767 | udc_t *udc = udc_dev; |
| 768 | |
| 769 | if (type == UDC_TYPE_BULK_IN) { |
| 770 | in = 1; |
| 771 | } else if (type == UDC_TYPE_BULK_OUT) { |
| 772 | in = 0; |
| 773 | } else { |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | for (n = 1; n < 16; n++) { |
| 778 | uint32_t bit = in ? EPT_TX(n) : EPT_RX(n); |
| 779 | if (udc->ept_alloc_table & bit) |
| 780 | continue; |
| 781 | ept = _udc_endpoint_alloc(n, in, maxpkt); |
| 782 | if (ept) |
| 783 | udc->ept_alloc_table |= bit; |
| 784 | return ept; |
| 785 | } |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | /* create config + interface + ep desc for 2.0 */ |
| 791 | static void udc_register_config_desc_usb20(udc_t *udc, |
| 792 | struct udc_gadget *gadget) |
| 793 | { |
| 794 | uint8_t *data; |
| 795 | uint16_t size; |
| 796 | struct udc_descriptor *desc; |
| 797 | |
| 798 | ASSERT(udc); |
| 799 | ASSERT(gadget); |
| 800 | |
| 801 | /* create our configuration descriptor */ |
| 802 | |
| 803 | /* size is the total size of (config + interface + all EPs) descriptor */ |
| 804 | size = UDC_DESC_SIZE_CONFIGURATION + |
| 805 | UDC_DESC_SIZE_INTERFACE + |
| 806 | (gadget->ifc_endpoints*UDC_DESC_SIZE_ENDPOINT); |
| 807 | |
| 808 | desc = udc_descriptor_alloc(TYPE_CONFIGURATION, 0, size, UDC_DESC_SPEC_20); |
| 809 | |
| 810 | data = desc->data; |
| 811 | |
| 812 | /* Config desc */ |
| 813 | data[0] = 0x09; |
| 814 | data[1] = TYPE_CONFIGURATION; |
| 815 | data[2] = size; |
| 816 | data[3] = size >> 8; |
| 817 | data[4] = 0x01; /* number of interfaces */ |
| 818 | data[5] = 0x01; /* configuration value */ |
| 819 | data[6] = 0x00; /* configuration string */ |
| 820 | data[7] = 0xC0; /* attributes: reserved and self-powered set */ |
| 821 | data[8] = 0x00; /* max power: 0ma since we are self powered */ |
| 822 | data += 9; |
| 823 | |
| 824 | /* Interface desc */ |
| 825 | data[0] = 0x09; |
| 826 | data[1] = TYPE_INTERFACE; |
| 827 | data[2] = 0x00; /* ifc number */ |
| 828 | data[3] = 0x00; /* alt number */ |
| 829 | data[4] = gadget->ifc_endpoints; |
| 830 | data[5] = gadget->ifc_class; |
| 831 | data[6] = gadget->ifc_subclass; |
| 832 | data[7] = gadget->ifc_protocol; |
| 833 | data[8] = udc_string_desc_alloc(udc, gadget->ifc_string); |
| 834 | data += 9; |
| 835 | |
| 836 | for (uint8_t n = 0; n < gadget->ifc_endpoints; n++) { |
| 837 | udc_ept_desc_fill(gadget->ept[n], data); |
| 838 | data += UDC_DESC_SIZE_ENDPOINT; |
| 839 | } |
| 840 | |
| 841 | udc_descriptor_register(udc, desc); |
| 842 | } |
| 843 | |
| 844 | /* create config + interface + ep desc for 3.0 */ |
| 845 | static void udc_register_config_desc_usb30(udc_t *udc, |
| 846 | struct udc_gadget *gadget) |
| 847 | { |
| 848 | uint8_t *data; |
| 849 | uint16_t size; |
| 850 | struct udc_descriptor *desc; |
| 851 | |
| 852 | ASSERT(udc); |
| 853 | ASSERT(gadget); |
| 854 | |
| 855 | /* create our configuration descriptor */ |
| 856 | |
| 857 | /* size is the total size of (config + interface + all EPs) descriptor */ |
| 858 | size = UDC_DESC_SIZE_CONFIGURATION + |
| 859 | UDC_DESC_SIZE_INTERFACE + |
| 860 | (gadget->ifc_endpoints*(UDC_DESC_SIZE_ENDPOINT + UDC_DESC_SIZE_ENDPOINT_COMP)); |
| 861 | |
| 862 | desc = udc_descriptor_alloc(TYPE_CONFIGURATION, 0, size, UDC_DESC_SPEC_30); |
| 863 | |
| 864 | data = desc->data; |
| 865 | |
| 866 | /* Config desc */ |
| 867 | data[0] = 0x09; |
| 868 | data[1] = TYPE_CONFIGURATION; |
| 869 | data[2] = size; |
| 870 | data[3] = size >> 8; |
| 871 | data[4] = 0x01; /* number of interfaces */ |
| 872 | data[5] = 0x01; /* configuration value */ |
| 873 | data[6] = 0x00; /* configuration string */ |
| 874 | data[7] = 0xC0; /* attributes: reserved and self-powered set */ |
| 875 | data[8] = 0x00; /* max power: 0ma since we are self powered */ |
| 876 | data += 9; |
| 877 | |
| 878 | /* Interface desc */ |
| 879 | data[0] = 0x09; |
| 880 | data[1] = TYPE_INTERFACE; |
| 881 | data[2] = 0x00; /* ifc number */ |
| 882 | data[3] = 0x00; /* alt number */ |
| 883 | data[4] = gadget->ifc_endpoints; |
| 884 | data[5] = gadget->ifc_class; |
| 885 | data[6] = gadget->ifc_subclass; |
| 886 | data[7] = gadget->ifc_protocol; |
| 887 | data[8] = udc_string_desc_alloc(udc, gadget->ifc_string); |
| 888 | data += 9; |
| 889 | |
| 890 | for (uint8_t n = 0; n < gadget->ifc_endpoints; n++) |
| 891 | { |
| 892 | /* fill EP desc */ |
| 893 | udc_ept_desc_fill(gadget->ept[n], data); |
| 894 | data += UDC_DESC_SIZE_ENDPOINT; |
| 895 | |
| 896 | /* fill EP companion desc */ |
| 897 | udc_ept_comp_desc_fill(gadget->ept[n], data); |
| 898 | data += UDC_DESC_SIZE_ENDPOINT_COMP; |
| 899 | } |
| 900 | |
| 901 | udc_descriptor_register(udc, desc); |
| 902 | } |
| 903 | |
| 904 | |
| 905 | static void udc_register_device_desc_usb_20(udc_t *udc, |
| 906 | struct udc_device *dev_info) |
| 907 | { |
| 908 | uint8_t *data; |
| 909 | struct udc_descriptor *desc; |
| 910 | |
| 911 | /* create our device descriptor */ |
| 912 | desc = udc_descriptor_alloc(TYPE_DEVICE, 0, 18, UDC_DESC_SPEC_20); |
| 913 | data = desc->data; |
| 914 | |
| 915 | /* data 0 and 1 is filled by descriptor alloc routine. |
| 916 | * fill in the remaining entries. |
| 917 | */ |
| 918 | data[2] = 0x00; /* usb spec minor rev */ |
| 919 | data[3] = 0x02; /* usb spec major rev */ |
| 920 | data[4] = 0x00; /* class */ |
| 921 | data[5] = 0x00; /* subclass */ |
| 922 | data[6] = 0x00; /* protocol */ |
| 923 | data[7] = 0x40; /* max packet size on ept 0 */ |
| 924 | |
| 925 | memcpy(data + 8, &dev_info->vendor_id, sizeof(short)); |
| 926 | memcpy(data + 10, &dev_info->product_id, sizeof(short)); |
| 927 | memcpy(data + 12, &dev_info->version_id, sizeof(short)); |
| 928 | |
| 929 | data[14] = udc_string_desc_alloc(udc, dev_info->manufacturer); |
| 930 | data[15] = udc_string_desc_alloc(udc, dev_info->product); |
| 931 | data[16] = udc_string_desc_alloc(udc, dev_info->serialno); |
| 932 | data[17] = 1; /* number of configurations */ |
| 933 | |
| 934 | udc_descriptor_register(udc, desc); |
| 935 | } |
| 936 | |
| 937 | static void udc_register_device_desc_usb_30(udc_t *udc, struct udc_device *dev_info) |
| 938 | { |
| 939 | uint8_t *data; |
| 940 | struct udc_descriptor *desc; |
| 941 | |
| 942 | /* create our device descriptor */ |
| 943 | desc = udc_descriptor_alloc(TYPE_DEVICE, 0, 18, UDC_DESC_SPEC_30); |
| 944 | data = desc->data; |
| 945 | |
| 946 | /* data 0 and 1 is filled by descriptor alloc routine. |
| 947 | * fill in the remaining entries. |
| 948 | */ |
| 949 | data[2] = 0x00; /* usb spec minor rev */ |
| 950 | data[3] = 0x03; /* usb spec major rev */ |
| 951 | data[4] = 0x00; /* class */ |
| 952 | data[5] = 0x00; /* subclass */ |
| 953 | data[6] = 0x00; /* protocol */ |
| 954 | data[7] = 0x09; /* max packet size on ept 0 */ |
| 955 | memcpy(data + 8, &dev_info->vendor_id, sizeof(short)); |
| 956 | memcpy(data + 10, &dev_info->product_id, sizeof(short)); |
| 957 | memcpy(data + 12, &dev_info->version_id, sizeof(short)); |
| 958 | data[14] = udc_string_desc_alloc(udc, dev_info->manufacturer); |
| 959 | data[15] = udc_string_desc_alloc(udc, dev_info->product); |
| 960 | data[16] = udc_string_desc_alloc(udc, dev_info->serialno); |
| 961 | data[17] = 1; /* number of configurations */ |
| 962 | |
| 963 | udc_descriptor_register(udc, desc); |
| 964 | } |
| 965 | |
| 966 | static void udc_register_bos_desc(udc_t *udc) |
| 967 | { |
| 968 | uint8_t *data; |
| 969 | struct udc_descriptor *desc; |
| 970 | |
| 971 | /* create our device descriptor */ |
| 972 | desc = udc_descriptor_alloc(TYPE_BOS, 0, 15, UDC_DESC_SPEC_30); /* 15 is total length of bos + other descriptors inside it */ |
| 973 | data = desc->data; |
| 974 | |
| 975 | /* data 0 and 1 is filled by descriptor alloc routine. |
| 976 | * fill in the remaining entries. |
| 977 | */ |
| 978 | data[0] = 0x05; /* BOS desc len */ |
| 979 | data[1] = TYPE_BOS; /* BOS desc type */ |
| 980 | data[2] = 0x0F; /* total len of bos desc and its sub desc */ |
| 981 | data[3] = 0x00; /* total len of bos desc and its sub desc */ |
| 982 | data[4] = 0x01; /* num of sub desc inside bos */ |
| 983 | |
| 984 | data[5] = 0x0A; /* desc len */ |
| 985 | data[6] = 0x10; /* Device Capability desc */ |
| 986 | data[7] = 0x03; /* 3 == SuperSpeed capable */ |
| 987 | data[8] = 0x00; /* Attribute: latency tolerance msg: No */ |
| 988 | data[9] = 0x0F; /* Supported Speeds (bit mask): LS, FS, HS, SS */ |
| 989 | data[10] = 0x00; /* Reserved part of supported wSupportedSpeeds */ |
| 990 | data[11] = 0x01; /* lowest supported speed with full functionality: FS */ |
| 991 | data[12] = 0x00; /* U1 device exit latency */ |
| 992 | data[13] = 0x00; /* U2 device exit latency (lsb) */ |
| 993 | data[14] = 0x00; /* U2 device exit latency (msb) */ |
| 994 | |
| 995 | udc_descriptor_register(udc, desc); |
| 996 | } |
| 997 | |
| 998 | static void udc_register_language_desc(udc_t *udc) |
| 999 | { |
| 1000 | /* create and register a language table descriptor */ |
| 1001 | /* language 0x0409 is US English */ |
| 1002 | struct udc_descriptor *desc = udc_descriptor_alloc(TYPE_STRING, |
| 1003 | 0, |
| 1004 | 4, |
| 1005 | UDC_DESC_SPEC_20 | UDC_DESC_SPEC_30); |
| 1006 | desc->data[2] = 0x09; |
| 1007 | desc->data[3] = 0x04; |
| 1008 | udc_descriptor_register(udc, desc); |
| 1009 | } |
| 1010 | |
| 1011 | static void udc_ept_desc_fill(struct udc_endpoint *ept, uint8_t *data) |
| 1012 | { |
| 1013 | data[0] = 7; |
| 1014 | data[1] = TYPE_ENDPOINT; |
| 1015 | data[2] = ept->num | (ept->in ? 0x80 : 0x00); |
| 1016 | data[3] = 0x02; /* bulk -- the only kind we support */ |
| 1017 | data[4] = ept->maxpkt; |
| 1018 | data[5] = ept->maxpkt >> 8; |
| 1019 | data[6] = 0; /* bInterval: must be 0 for bulk. */ |
| 1020 | } |
| 1021 | |
| 1022 | static void udc_ept_comp_desc_fill(struct udc_endpoint *ept, uint8_t *data) |
| 1023 | { |
| 1024 | data[0] = 6; /* bLength */ |
| 1025 | data[1] = TYPE_SS_EP_COMP; /* ep type */ |
| 1026 | data[2] = ept->maxburst; /* maxBurst */ |
| 1027 | data[3] = 0x0; /* maxStreams */ |
| 1028 | data[4] = 0x0; /* wBytesPerInterval */ |
| 1029 | data[5] = 0x0; /* wBytesPerInterval */ |
| 1030 | } |
| 1031 | |
| 1032 | static uint8_t udc_string_desc_alloc(udc_t *udc, const char *str) |
| 1033 | { |
| 1034 | uint32_t len; |
| 1035 | struct udc_descriptor *desc; |
| 1036 | uint8_t *data; |
| 1037 | |
| 1038 | if (udc->next_string_id > 255) |
| 1039 | return 0; |
| 1040 | |
| 1041 | if (!str) |
| 1042 | return 0; |
| 1043 | |
| 1044 | len = strlen(str); |
| 1045 | desc = udc_descriptor_alloc(TYPE_STRING, |
| 1046 | udc->next_string_id, |
| 1047 | len * 2 + 2, |
| 1048 | UDC_DESC_SPEC_20 | UDC_DESC_SPEC_30); |
| 1049 | if (!desc) |
| 1050 | return 0; |
| 1051 | udc->next_string_id++; |
| 1052 | |
| 1053 | /* expand ascii string to utf16 */ |
| 1054 | data = desc->data + 2; |
| 1055 | while (len-- > 0) { |
| 1056 | *data++ = *str++; |
| 1057 | *data++ = 0; |
| 1058 | } |
| 1059 | |
| 1060 | udc_descriptor_register(udc, desc); |
| 1061 | return desc->tag & 0xff; |
| 1062 | } |
| 1063 | |
| 1064 | |
| 1065 | static struct udc_descriptor *udc_descriptor_alloc(uint32_t type, |
| 1066 | uint32_t num, |
| 1067 | uint32_t len, |
| 1068 | udc_desc_spec_t spec) |
| 1069 | { |
| 1070 | struct udc_descriptor *desc; |
| 1071 | if ((len > 255) || (len < 2) || (num > 255) || (type > 255)) |
| 1072 | return 0; |
| 1073 | |
| 1074 | if (!(desc = malloc(sizeof(struct udc_descriptor) + len))) |
| 1075 | return 0; |
| 1076 | |
| 1077 | desc->next = 0; |
| 1078 | desc->tag = (type << 8) | num; |
| 1079 | desc->len = len; |
| 1080 | desc->spec = spec; |
| 1081 | |
| 1082 | /* descriptor data */ |
| 1083 | desc->data[0] = len; |
| 1084 | desc->data[1] = type; |
| 1085 | |
| 1086 | return desc; |
| 1087 | } |
| 1088 | |
| 1089 | static void udc_descriptor_register(udc_t *udc, struct udc_descriptor *desc) |
| 1090 | { |
| 1091 | desc->next = udc->desc_list; |
| 1092 | udc->desc_list = desc; |
| 1093 | } |
| 1094 | |
| 1095 | |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 1096 | struct udc_request *usb30_udc_request_alloc(void) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 1097 | { |
| 1098 | struct udc_request *req; |
| 1099 | |
| 1100 | req = malloc(sizeof(*req)); |
| 1101 | ASSERT(req); |
| 1102 | |
| 1103 | req->buf = 0; |
| 1104 | req->length = 0; |
| 1105 | req->complete = NULL; |
| 1106 | req->context = 0; |
| 1107 | |
| 1108 | return req; |
| 1109 | } |
| 1110 | |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 1111 | void usb30_udc_request_free(struct udc_request *req) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 1112 | { |
| 1113 | free(req); |
| 1114 | } |
| 1115 | |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 1116 | void usb30_udc_endpoint_free(struct udc_endpoint *ept) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 1117 | { |
| 1118 | /* TODO */ |
| 1119 | } |
| 1120 | |
Amol Jadi | 151f2a5 | 2013-10-07 12:39:11 -0700 | [diff] [blame] | 1121 | int usb30_udc_stop(void) |
Amol Jadi | f3d5a89 | 2013-07-23 16:09:44 -0700 | [diff] [blame] | 1122 | { |
| 1123 | dwc_device_run(udc_dev->dwc, 0); |
| 1124 | |
| 1125 | return 0; |
| 1126 | } |