2018.07.26
Dependencies: FATFileSystem2 mbed-rtos
Fork of USBHost by
USBHost/USBHost.cpp@44:e437b1c7c61e, 2018-07-26 (annotated)
- Committer:
- sayzyas
- Date:
- Thu Jul 26 00:29:30 2018 +0000
- Revision:
- 44:e437b1c7c61e
- Parent:
- 43:78f328f311dc
2018.07.26
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sayzyas | 43:78f328f311dc | 1 | /* mbed USBHost Library |
sayzyas | 43:78f328f311dc | 2 | * Copyright (c) 2006-2013 ARM Limited |
sayzyas | 43:78f328f311dc | 3 | * |
sayzyas | 43:78f328f311dc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sayzyas | 43:78f328f311dc | 5 | * you may not use this file except in compliance with the License. |
sayzyas | 43:78f328f311dc | 6 | * You may obtain a copy of the License at |
sayzyas | 43:78f328f311dc | 7 | * |
sayzyas | 43:78f328f311dc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sayzyas | 43:78f328f311dc | 9 | * |
sayzyas | 43:78f328f311dc | 10 | * Unless required by applicable law or agreed to in writing, software |
sayzyas | 43:78f328f311dc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sayzyas | 43:78f328f311dc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sayzyas | 43:78f328f311dc | 13 | * See the License for the specific language governing permissions and |
sayzyas | 43:78f328f311dc | 14 | * limitations under the License. |
sayzyas | 43:78f328f311dc | 15 | */ |
sayzyas | 43:78f328f311dc | 16 | |
sayzyas | 43:78f328f311dc | 17 | |
sayzyas | 43:78f328f311dc | 18 | #include "USBHost.h" |
sayzyas | 43:78f328f311dc | 19 | #include "USBHostHub.h" |
sayzyas | 43:78f328f311dc | 20 | |
sayzyas | 43:78f328f311dc | 21 | USBHost * USBHost::instHost = NULL; |
sayzyas | 43:78f328f311dc | 22 | |
sayzyas | 43:78f328f311dc | 23 | #define DEVICE_CONNECTED_EVENT (1 << 0) |
sayzyas | 43:78f328f311dc | 24 | #define DEVICE_DISCONNECTED_EVENT (1 << 1) |
sayzyas | 43:78f328f311dc | 25 | #define TD_PROCESSED_EVENT (1 << 2) |
sayzyas | 43:78f328f311dc | 26 | |
sayzyas | 43:78f328f311dc | 27 | #define MAX_TRY_ENUMERATE_HUB 3 |
sayzyas | 43:78f328f311dc | 28 | |
sayzyas | 43:78f328f311dc | 29 | #define MIN(a, b) ((a > b) ? b : a) |
sayzyas | 43:78f328f311dc | 30 | |
sayzyas | 43:78f328f311dc | 31 | /** |
sayzyas | 43:78f328f311dc | 32 | * How interrupts are processed: |
sayzyas | 43:78f328f311dc | 33 | * - new device connected: |
sayzyas | 43:78f328f311dc | 34 | * - a message is queued in queue_usb_event with the id DEVICE_CONNECTED_EVENT |
sayzyas | 43:78f328f311dc | 35 | * - when the usb_thread receives the event, it: |
sayzyas | 43:78f328f311dc | 36 | * - resets the device |
sayzyas | 43:78f328f311dc | 37 | * - reads the device descriptor |
sayzyas | 43:78f328f311dc | 38 | * - sets the address of the device |
sayzyas | 43:78f328f311dc | 39 | * - if it is a hub, enumerates it |
sayzyas | 43:78f328f311dc | 40 | * - device disconnected: |
sayzyas | 43:78f328f311dc | 41 | * - a message is queued in queue_usb_event with the id DEVICE_DISCONNECTED_EVENT |
sayzyas | 43:78f328f311dc | 42 | * - when the usb_thread receives the event, it: |
sayzyas | 43:78f328f311dc | 43 | * - free the device and all its children (hub) |
sayzyas | 43:78f328f311dc | 44 | * - td processed |
sayzyas | 43:78f328f311dc | 45 | * - a message is queued in queue_usb_event with the id TD_PROCESSED_EVENT |
sayzyas | 43:78f328f311dc | 46 | * - when the usb_thread receives the event, it: |
sayzyas | 43:78f328f311dc | 47 | * - call the callback attached to the endpoint where the td is attached |
sayzyas | 43:78f328f311dc | 48 | */ |
sayzyas | 43:78f328f311dc | 49 | void USBHost::usb_process() { |
sayzyas | 43:78f328f311dc | 50 | |
sayzyas | 43:78f328f311dc | 51 | bool controlListState; |
sayzyas | 43:78f328f311dc | 52 | bool bulkListState; |
sayzyas | 43:78f328f311dc | 53 | bool interruptListState; |
sayzyas | 43:78f328f311dc | 54 | USBEndpoint * ep; |
sayzyas | 43:78f328f311dc | 55 | uint8_t i, j, res, timeout_set_addr = 10; |
sayzyas | 43:78f328f311dc | 56 | uint8_t buf[8]; |
sayzyas | 43:78f328f311dc | 57 | bool too_many_hub; |
sayzyas | 43:78f328f311dc | 58 | int idx; |
sayzyas | 43:78f328f311dc | 59 | |
sayzyas | 43:78f328f311dc | 60 | #if DEBUG_TRANSFER |
sayzyas | 43:78f328f311dc | 61 | uint8_t * buf_transfer; |
sayzyas | 43:78f328f311dc | 62 | #endif |
sayzyas | 43:78f328f311dc | 63 | |
sayzyas | 43:78f328f311dc | 64 | #if MAX_HUB_NB |
sayzyas | 43:78f328f311dc | 65 | uint8_t k; |
sayzyas | 43:78f328f311dc | 66 | #endif |
sayzyas | 43:78f328f311dc | 67 | |
sayzyas | 43:78f328f311dc | 68 | while(1) { |
sayzyas | 43:78f328f311dc | 69 | osEvent evt = mail_usb_event.get(); |
sayzyas | 43:78f328f311dc | 70 | |
sayzyas | 43:78f328f311dc | 71 | if (evt.status == osEventMail) { |
sayzyas | 43:78f328f311dc | 72 | |
sayzyas | 43:78f328f311dc | 73 | message_t * usb_msg = (message_t*)evt.value.p; |
sayzyas | 43:78f328f311dc | 74 | |
sayzyas | 43:78f328f311dc | 75 | switch (usb_msg->event_id) { |
sayzyas | 43:78f328f311dc | 76 | |
sayzyas | 43:78f328f311dc | 77 | // a new device has been connected |
sayzyas | 43:78f328f311dc | 78 | case DEVICE_CONNECTED_EVENT: |
sayzyas | 43:78f328f311dc | 79 | too_many_hub = false; |
sayzyas | 43:78f328f311dc | 80 | buf[4] = 0; |
sayzyas | 43:78f328f311dc | 81 | |
sayzyas | 43:78f328f311dc | 82 | do |
sayzyas | 43:78f328f311dc | 83 | { |
sayzyas | 43:78f328f311dc | 84 | Lock lock(this); |
sayzyas | 43:78f328f311dc | 85 | |
sayzyas | 43:78f328f311dc | 86 | for (i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
sayzyas | 43:78f328f311dc | 87 | if (!deviceInUse[i]) { |
sayzyas | 43:78f328f311dc | 88 | USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]); |
sayzyas | 43:78f328f311dc | 89 | devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed); |
sayzyas | 43:78f328f311dc | 90 | deviceReset[i] = false; |
sayzyas | 43:78f328f311dc | 91 | deviceInited[i] = true; |
sayzyas | 43:78f328f311dc | 92 | break; |
sayzyas | 43:78f328f311dc | 93 | } |
sayzyas | 43:78f328f311dc | 94 | } |
sayzyas | 43:78f328f311dc | 95 | |
sayzyas | 43:78f328f311dc | 96 | if (i == MAX_DEVICE_CONNECTED) { |
sayzyas | 43:78f328f311dc | 97 | USB_ERR("Too many device connected!!\r\n"); |
sayzyas | 43:78f328f311dc | 98 | continue; |
sayzyas | 43:78f328f311dc | 99 | } |
sayzyas | 43:78f328f311dc | 100 | |
sayzyas | 43:78f328f311dc | 101 | if (!controlEndpointAllocated) { |
sayzyas | 43:78f328f311dc | 102 | control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00); |
sayzyas | 43:78f328f311dc | 103 | addEndpoint(NULL, 0, (USBEndpoint*)control); |
sayzyas | 43:78f328f311dc | 104 | controlEndpointAllocated = true; |
sayzyas | 43:78f328f311dc | 105 | } |
sayzyas | 43:78f328f311dc | 106 | |
sayzyas | 43:78f328f311dc | 107 | #if MAX_HUB_NB |
sayzyas | 43:78f328f311dc | 108 | if (usb_msg->hub_parent) |
sayzyas | 43:78f328f311dc | 109 | devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent)); |
sayzyas | 43:78f328f311dc | 110 | #endif |
sayzyas | 43:78f328f311dc | 111 | |
sayzyas | 43:78f328f311dc | 112 | for (j = 0; j < timeout_set_addr; j++) { |
sayzyas | 43:78f328f311dc | 113 | |
sayzyas | 43:78f328f311dc | 114 | resetDevice(&devices[i]); |
sayzyas | 43:78f328f311dc | 115 | |
sayzyas | 43:78f328f311dc | 116 | // set size of control endpoint |
sayzyas | 43:78f328f311dc | 117 | devices[i].setSizeControlEndpoint(8); |
sayzyas | 43:78f328f311dc | 118 | |
sayzyas | 43:78f328f311dc | 119 | devices[i].activeAddress(false); |
sayzyas | 43:78f328f311dc | 120 | |
sayzyas | 43:78f328f311dc | 121 | // get first 8 bit of device descriptor |
sayzyas | 43:78f328f311dc | 122 | // and check if we deal with a hub |
sayzyas | 43:78f328f311dc | 123 | USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]); |
sayzyas | 43:78f328f311dc | 124 | res = getDeviceDescriptor(&devices[i], buf, 8); |
sayzyas | 43:78f328f311dc | 125 | |
sayzyas | 43:78f328f311dc | 126 | if (res != USB_TYPE_OK) { |
sayzyas | 43:78f328f311dc | 127 | USB_ERR("usb_thread could not read dev descr"); |
sayzyas | 43:78f328f311dc | 128 | continue; |
sayzyas | 43:78f328f311dc | 129 | } |
sayzyas | 43:78f328f311dc | 130 | |
sayzyas | 43:78f328f311dc | 131 | // set size of control endpoint |
sayzyas | 43:78f328f311dc | 132 | devices[i].setSizeControlEndpoint(buf[7]); |
sayzyas | 43:78f328f311dc | 133 | |
sayzyas | 43:78f328f311dc | 134 | // second step: set an address to the device |
sayzyas | 43:78f328f311dc | 135 | res = setAddress(&devices[i], devices[i].getAddress()); |
sayzyas | 43:78f328f311dc | 136 | |
sayzyas | 43:78f328f311dc | 137 | if (res != USB_TYPE_OK) { |
sayzyas | 43:78f328f311dc | 138 | USB_ERR("SET ADDR FAILED"); |
sayzyas | 43:78f328f311dc | 139 | continue; |
sayzyas | 43:78f328f311dc | 140 | } |
sayzyas | 43:78f328f311dc | 141 | devices[i].activeAddress(true); |
sayzyas | 43:78f328f311dc | 142 | USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress()); |
sayzyas | 43:78f328f311dc | 143 | |
sayzyas | 43:78f328f311dc | 144 | // try to read again the device descriptor to check if the device |
sayzyas | 43:78f328f311dc | 145 | // answers to its new address |
sayzyas | 43:78f328f311dc | 146 | res = getDeviceDescriptor(&devices[i], buf, 8); |
sayzyas | 43:78f328f311dc | 147 | |
sayzyas | 43:78f328f311dc | 148 | if (res == USB_TYPE_OK) { |
sayzyas | 43:78f328f311dc | 149 | |
sayzyas | 43:78f328f311dc | 150 | break; |
sayzyas | 43:78f328f311dc | 151 | } |
sayzyas | 43:78f328f311dc | 152 | |
sayzyas | 43:78f328f311dc | 153 | Thread::wait(100); |
sayzyas | 43:78f328f311dc | 154 | } |
sayzyas | 43:78f328f311dc | 155 | |
sayzyas | 43:78f328f311dc | 156 | USB_INFO("New device is connected"); |
sayzyas | 43:78f328f311dc | 157 | USB_INFO("%p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port); |
sayzyas | 43:78f328f311dc | 158 | |
sayzyas | 43:78f328f311dc | 159 | #if MAX_HUB_NB |
sayzyas | 43:78f328f311dc | 160 | if (buf[4] == HUB_CLASS) { |
sayzyas | 43:78f328f311dc | 161 | for (k = 0; k < MAX_HUB_NB; k++) { |
sayzyas | 43:78f328f311dc | 162 | if (hub_in_use[k] == false) { |
sayzyas | 43:78f328f311dc | 163 | for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) { |
sayzyas | 43:78f328f311dc | 164 | if (hubs[k].connect(&devices[i])) { |
sayzyas | 43:78f328f311dc | 165 | devices[i].hub = &hubs[k]; |
sayzyas | 43:78f328f311dc | 166 | hub_in_use[k] = true; |
sayzyas | 43:78f328f311dc | 167 | USB_INFO("XX1"); |
sayzyas | 43:78f328f311dc | 168 | break; |
sayzyas | 43:78f328f311dc | 169 | } |
sayzyas | 43:78f328f311dc | 170 | } |
sayzyas | 43:78f328f311dc | 171 | if (hub_in_use[k] == true){ |
sayzyas | 43:78f328f311dc | 172 | USB_INFO("XX2"); |
sayzyas | 43:78f328f311dc | 173 | break; |
sayzyas | 43:78f328f311dc | 174 | } |
sayzyas | 43:78f328f311dc | 175 | } |
sayzyas | 43:78f328f311dc | 176 | } |
sayzyas | 43:78f328f311dc | 177 | |
sayzyas | 43:78f328f311dc | 178 | if (k == MAX_HUB_NB) { |
sayzyas | 43:78f328f311dc | 179 | USB_ERR("Too many hubs connected!!\r\n"); |
sayzyas | 43:78f328f311dc | 180 | too_many_hub = true; |
sayzyas | 43:78f328f311dc | 181 | } |
sayzyas | 43:78f328f311dc | 182 | } |
sayzyas | 43:78f328f311dc | 183 | else{ |
sayzyas | 43:78f328f311dc | 184 | USB_INFO("XX3"); |
sayzyas | 43:78f328f311dc | 185 | } |
sayzyas | 43:78f328f311dc | 186 | |
sayzyas | 43:78f328f311dc | 187 | if (usb_msg->hub_parent){ |
sayzyas | 43:78f328f311dc | 188 | USB_INFO("XX31"); |
sayzyas | 43:78f328f311dc | 189 | ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]); |
sayzyas | 43:78f328f311dc | 190 | } |
sayzyas | 43:78f328f311dc | 191 | else{ |
sayzyas | 43:78f328f311dc | 192 | USB_INFO("XX4"); |
sayzyas | 43:78f328f311dc | 193 | } |
sayzyas | 43:78f328f311dc | 194 | #endif |
sayzyas | 43:78f328f311dc | 195 | |
sayzyas | 43:78f328f311dc | 196 | if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) { |
sayzyas | 43:78f328f311dc | 197 | deviceInUse[i] = true; |
sayzyas | 43:78f328f311dc | 198 | USB_INFO("XX5"); |
sayzyas | 43:78f328f311dc | 199 | } |
sayzyas | 43:78f328f311dc | 200 | else{ |
sayzyas | 43:78f328f311dc | 201 | deviceInUse[i] = true; |
sayzyas | 43:78f328f311dc | 202 | } |
sayzyas | 43:78f328f311dc | 203 | } while(0); |
sayzyas | 43:78f328f311dc | 204 | |
sayzyas | 43:78f328f311dc | 205 | break; |
sayzyas | 43:78f328f311dc | 206 | |
sayzyas | 43:78f328f311dc | 207 | // a device has been disconnected |
sayzyas | 43:78f328f311dc | 208 | case DEVICE_DISCONNECTED_EVENT: |
sayzyas | 43:78f328f311dc | 209 | |
sayzyas | 43:78f328f311dc | 210 | do |
sayzyas | 43:78f328f311dc | 211 | { |
sayzyas | 43:78f328f311dc | 212 | Lock lock(this); |
sayzyas | 43:78f328f311dc | 213 | |
sayzyas | 43:78f328f311dc | 214 | controlListState = disableList(CONTROL_ENDPOINT); |
sayzyas | 43:78f328f311dc | 215 | bulkListState = disableList(BULK_ENDPOINT); |
sayzyas | 43:78f328f311dc | 216 | interruptListState = disableList(INTERRUPT_ENDPOINT); |
sayzyas | 43:78f328f311dc | 217 | |
sayzyas | 43:78f328f311dc | 218 | idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent)); |
sayzyas | 43:78f328f311dc | 219 | if (idx != -1) { |
sayzyas | 43:78f328f311dc | 220 | freeDevice((USBDeviceConnected*)&devices[idx]); |
sayzyas | 43:78f328f311dc | 221 | } |
sayzyas | 43:78f328f311dc | 222 | |
sayzyas | 43:78f328f311dc | 223 | if (controlListState) enableList(CONTROL_ENDPOINT); |
sayzyas | 43:78f328f311dc | 224 | if (bulkListState) enableList(BULK_ENDPOINT); |
sayzyas | 43:78f328f311dc | 225 | if (interruptListState) enableList(INTERRUPT_ENDPOINT); |
sayzyas | 43:78f328f311dc | 226 | |
sayzyas | 43:78f328f311dc | 227 | } while(0); |
sayzyas | 43:78f328f311dc | 228 | |
sayzyas | 43:78f328f311dc | 229 | break; |
sayzyas | 43:78f328f311dc | 230 | |
sayzyas | 43:78f328f311dc | 231 | // a td has been processed |
sayzyas | 43:78f328f311dc | 232 | // call callback on the ed associated to the td |
sayzyas | 43:78f328f311dc | 233 | // we are not in ISR -> users can use printf in their callback method |
sayzyas | 43:78f328f311dc | 234 | case TD_PROCESSED_EVENT: |
sayzyas | 43:78f328f311dc | 235 | ep = (USBEndpoint *) ((HCTD *)usb_msg->td_addr)->ep; |
sayzyas | 43:78f328f311dc | 236 | if (usb_msg->td_state == USB_TYPE_IDLE) { |
sayzyas | 43:78f328f311dc | 237 | USB_DBG_EVENT("call callback on td %p [ep: %p state: %s - dev: %p - %s]", usb_msg->td_addr, ep, ep->getStateString(), ep->dev, ep->dev->getName(ep->getIntfNb())); |
sayzyas | 43:78f328f311dc | 238 | |
sayzyas | 43:78f328f311dc | 239 | #if DEBUG_TRANSFER |
sayzyas | 43:78f328f311dc | 240 | if (ep->getDir() == IN) { |
sayzyas | 43:78f328f311dc | 241 | buf_transfer = ep->getBufStart(); |
sayzyas | 43:78f328f311dc | 242 | printf("READ SUCCESS [%d bytes transferred - td: 0x%08X] on ep: [%p - addr: %02X]: ", ep->getLengthTransferred(), usb_msg->td_addr, ep, ep->getAddress()); |
sayzyas | 43:78f328f311dc | 243 | for (int i = 0; i < ep->getLengthTransferred(); i++) |
sayzyas | 43:78f328f311dc | 244 | printf("%02X ", buf_transfer[i]); |
sayzyas | 43:78f328f311dc | 245 | printf("\r\n\r\n"); |
sayzyas | 43:78f328f311dc | 246 | } |
sayzyas | 43:78f328f311dc | 247 | #endif |
sayzyas | 43:78f328f311dc | 248 | ep->call(); |
sayzyas | 43:78f328f311dc | 249 | } else { |
sayzyas | 43:78f328f311dc | 250 | idx = findDevice(ep->dev); |
sayzyas | 43:78f328f311dc | 251 | if (idx != -1) { |
sayzyas | 43:78f328f311dc | 252 | if (deviceInUse[idx]) { |
sayzyas | 43:78f328f311dc | 253 | USB_WARN("td %p processed but not in idle state: %s [ep: %p - dev: %p - %s]", usb_msg->td_addr, ep->getStateString(), ep, ep->dev, ep->dev->getName(ep->getIntfNb())); |
sayzyas | 43:78f328f311dc | 254 | ep->setState(USB_TYPE_IDLE); |
sayzyas | 43:78f328f311dc | 255 | } |
sayzyas | 43:78f328f311dc | 256 | } |
sayzyas | 43:78f328f311dc | 257 | } |
sayzyas | 43:78f328f311dc | 258 | break; |
sayzyas | 43:78f328f311dc | 259 | } |
sayzyas | 43:78f328f311dc | 260 | |
sayzyas | 43:78f328f311dc | 261 | mail_usb_event.free(usb_msg); |
sayzyas | 43:78f328f311dc | 262 | } |
sayzyas | 43:78f328f311dc | 263 | } |
sayzyas | 43:78f328f311dc | 264 | } |
sayzyas | 43:78f328f311dc | 265 | |
sayzyas | 43:78f328f311dc | 266 | /* static */void USBHost::usb_process_static(void const * arg) { |
sayzyas | 43:78f328f311dc | 267 | ((USBHost *)arg)->usb_process(); |
sayzyas | 43:78f328f311dc | 268 | } |
sayzyas | 43:78f328f311dc | 269 | |
sayzyas | 43:78f328f311dc | 270 | USBHost::USBHost() : usbThread(USBHost::usb_process_static, (void *)this, osPriorityNormal, USB_THREAD_STACK) |
sayzyas | 43:78f328f311dc | 271 | { |
sayzyas | 43:78f328f311dc | 272 | headControlEndpoint = NULL; |
sayzyas | 43:78f328f311dc | 273 | headBulkEndpoint = NULL; |
sayzyas | 43:78f328f311dc | 274 | headInterruptEndpoint = NULL; |
sayzyas | 43:78f328f311dc | 275 | tailControlEndpoint = NULL; |
sayzyas | 43:78f328f311dc | 276 | tailBulkEndpoint = NULL; |
sayzyas | 43:78f328f311dc | 277 | tailInterruptEndpoint = NULL; |
sayzyas | 43:78f328f311dc | 278 | |
sayzyas | 43:78f328f311dc | 279 | lenReportDescr = 0; |
sayzyas | 43:78f328f311dc | 280 | |
sayzyas | 43:78f328f311dc | 281 | controlEndpointAllocated = false; |
sayzyas | 43:78f328f311dc | 282 | |
sayzyas | 43:78f328f311dc | 283 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
sayzyas | 43:78f328f311dc | 284 | deviceInUse[i] = false; |
sayzyas | 43:78f328f311dc | 285 | devices[i].setAddress(i + 1); |
sayzyas | 43:78f328f311dc | 286 | deviceReset[i] = false; |
sayzyas | 43:78f328f311dc | 287 | deviceInited[i] = false; |
sayzyas | 43:78f328f311dc | 288 | for (uint8_t j = 0; j < MAX_INTF; j++) |
sayzyas | 43:78f328f311dc | 289 | deviceAttachedDriver[i][j] = false; |
sayzyas | 43:78f328f311dc | 290 | } |
sayzyas | 43:78f328f311dc | 291 | |
sayzyas | 43:78f328f311dc | 292 | #if MAX_HUB_NB |
sayzyas | 43:78f328f311dc | 293 | for (uint8_t i = 0; i < MAX_HUB_NB; i++) { |
sayzyas | 43:78f328f311dc | 294 | hubs[i].setHost(this); |
sayzyas | 43:78f328f311dc | 295 | hub_in_use[i] = false; |
sayzyas | 43:78f328f311dc | 296 | } |
sayzyas | 43:78f328f311dc | 297 | #endif |
sayzyas | 43:78f328f311dc | 298 | } |
sayzyas | 43:78f328f311dc | 299 | |
sayzyas | 43:78f328f311dc | 300 | USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost) |
sayzyas | 43:78f328f311dc | 301 | { |
sayzyas | 43:78f328f311dc | 302 | m_pHost->usb_mutex.lock(); |
sayzyas | 43:78f328f311dc | 303 | } |
sayzyas | 43:78f328f311dc | 304 | |
sayzyas | 43:78f328f311dc | 305 | USBHost::Lock::~Lock() |
sayzyas | 43:78f328f311dc | 306 | { |
sayzyas | 43:78f328f311dc | 307 | m_pHost->usb_mutex.unlock(); |
sayzyas | 43:78f328f311dc | 308 | } |
sayzyas | 43:78f328f311dc | 309 | |
sayzyas | 43:78f328f311dc | 310 | void USBHost::transferCompleted(volatile uint32_t addr) |
sayzyas | 43:78f328f311dc | 311 | { |
sayzyas | 43:78f328f311dc | 312 | uint8_t state; |
sayzyas | 43:78f328f311dc | 313 | |
sayzyas | 43:78f328f311dc | 314 | if(addr == 0) |
sayzyas | 43:78f328f311dc | 315 | return; |
sayzyas | 43:78f328f311dc | 316 | |
sayzyas | 43:78f328f311dc | 317 | volatile HCTD* tdList = NULL; |
sayzyas | 43:78f328f311dc | 318 | |
sayzyas | 43:78f328f311dc | 319 | //First we must reverse the list order and dequeue each TD |
sayzyas | 43:78f328f311dc | 320 | do { |
sayzyas | 43:78f328f311dc | 321 | volatile HCTD* td = (volatile HCTD*)addr; |
sayzyas | 43:78f328f311dc | 322 | addr = (uint32_t)td->nextTD; //Dequeue from physical list |
sayzyas | 43:78f328f311dc | 323 | td->nextTD = tdList; //Enqueue into reversed list |
sayzyas | 43:78f328f311dc | 324 | tdList = td; |
sayzyas | 43:78f328f311dc | 325 | } while(addr); |
sayzyas | 43:78f328f311dc | 326 | |
sayzyas | 43:78f328f311dc | 327 | while(tdList != NULL) { |
sayzyas | 43:78f328f311dc | 328 | volatile HCTD* td = tdList; |
sayzyas | 43:78f328f311dc | 329 | tdList = (volatile HCTD*)td->nextTD; //Dequeue element now as it could be modified below |
sayzyas | 43:78f328f311dc | 330 | if (td->ep != NULL) { |
sayzyas | 43:78f328f311dc | 331 | USBEndpoint * ep = (USBEndpoint *)(td->ep); |
sayzyas | 43:78f328f311dc | 332 | |
sayzyas | 43:78f328f311dc | 333 | if (((HCTD *)td)->control >> 28) { |
sayzyas | 43:78f328f311dc | 334 | state = ((HCTD *)td)->control >> 28; |
sayzyas | 43:78f328f311dc | 335 | } else { |
sayzyas | 43:78f328f311dc | 336 | if (td->currBufPtr) |
sayzyas | 43:78f328f311dc | 337 | ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); |
sayzyas | 43:78f328f311dc | 338 | state = 16 /*USB_TYPE_IDLE*/; |
sayzyas | 43:78f328f311dc | 339 | } |
sayzyas | 43:78f328f311dc | 340 | |
sayzyas | 43:78f328f311dc | 341 | ep->unqueueTransfer(td); |
sayzyas | 43:78f328f311dc | 342 | |
sayzyas | 43:78f328f311dc | 343 | if (ep->getType() != CONTROL_ENDPOINT) { |
sayzyas | 43:78f328f311dc | 344 | // callback on the processed td will be called from the usb_thread (not in ISR) |
sayzyas | 43:78f328f311dc | 345 | message_t * usb_msg = mail_usb_event.alloc(); |
sayzyas | 43:78f328f311dc | 346 | usb_msg->event_id = TD_PROCESSED_EVENT; |
sayzyas | 43:78f328f311dc | 347 | usb_msg->td_addr = (void *)td; |
sayzyas | 43:78f328f311dc | 348 | usb_msg->td_state = state; |
sayzyas | 43:78f328f311dc | 349 | mail_usb_event.put(usb_msg); |
sayzyas | 43:78f328f311dc | 350 | } |
sayzyas | 43:78f328f311dc | 351 | ep->setState(state); |
sayzyas | 43:78f328f311dc | 352 | ep->ep_queue.put((uint8_t*)1); |
sayzyas | 43:78f328f311dc | 353 | } |
sayzyas | 43:78f328f311dc | 354 | } |
sayzyas | 43:78f328f311dc | 355 | } |
sayzyas | 43:78f328f311dc | 356 | |
sayzyas | 43:78f328f311dc | 357 | USBHost * USBHost::getHostInst() |
sayzyas | 43:78f328f311dc | 358 | { |
sayzyas | 43:78f328f311dc | 359 | if (instHost == NULL) { |
sayzyas | 43:78f328f311dc | 360 | instHost = new USBHost(); |
sayzyas | 43:78f328f311dc | 361 | instHost->init(); |
sayzyas | 43:78f328f311dc | 362 | } |
sayzyas | 43:78f328f311dc | 363 | return instHost; |
sayzyas | 43:78f328f311dc | 364 | } |
sayzyas | 43:78f328f311dc | 365 | |
sayzyas | 43:78f328f311dc | 366 | |
sayzyas | 43:78f328f311dc | 367 | /* |
sayzyas | 43:78f328f311dc | 368 | * Called when a device has been connected |
sayzyas | 43:78f328f311dc | 369 | * Called in ISR!!!! (no printf) |
sayzyas | 43:78f328f311dc | 370 | */ |
sayzyas | 43:78f328f311dc | 371 | /* virtual */ void USBHost::deviceConnected(int hub, int port, bool lowSpeed, USBHostHub * hub_parent) |
sayzyas | 43:78f328f311dc | 372 | { |
sayzyas | 43:78f328f311dc | 373 | // be sure that the new device connected is not already connected... |
sayzyas | 43:78f328f311dc | 374 | int idx = findDevice(hub, port, hub_parent); |
sayzyas | 43:78f328f311dc | 375 | if (idx != -1) { |
sayzyas | 43:78f328f311dc | 376 | if (deviceInited[idx]) |
sayzyas | 43:78f328f311dc | 377 | return; |
sayzyas | 43:78f328f311dc | 378 | } |
sayzyas | 43:78f328f311dc | 379 | |
sayzyas | 43:78f328f311dc | 380 | message_t * usb_msg = mail_usb_event.alloc(); |
sayzyas | 43:78f328f311dc | 381 | usb_msg->event_id = DEVICE_CONNECTED_EVENT; |
sayzyas | 43:78f328f311dc | 382 | usb_msg->hub = hub; |
sayzyas | 43:78f328f311dc | 383 | usb_msg->port = port; |
sayzyas | 43:78f328f311dc | 384 | usb_msg->lowSpeed = lowSpeed; |
sayzyas | 43:78f328f311dc | 385 | usb_msg->hub_parent = hub_parent; |
sayzyas | 43:78f328f311dc | 386 | mail_usb_event.put(usb_msg); |
sayzyas | 43:78f328f311dc | 387 | } |
sayzyas | 43:78f328f311dc | 388 | |
sayzyas | 43:78f328f311dc | 389 | /* |
sayzyas | 43:78f328f311dc | 390 | * Called when a device has been disconnected |
sayzyas | 43:78f328f311dc | 391 | * Called in ISR!!!! (no printf) |
sayzyas | 43:78f328f311dc | 392 | */ |
sayzyas | 43:78f328f311dc | 393 | /* virtual */ void USBHost::deviceDisconnected(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr) |
sayzyas | 43:78f328f311dc | 394 | { |
sayzyas | 43:78f328f311dc | 395 | // be sure that the device disconnected is connected... |
sayzyas | 43:78f328f311dc | 396 | int idx = findDevice(hub, port, hub_parent); |
sayzyas | 43:78f328f311dc | 397 | if (idx != -1) { |
sayzyas | 43:78f328f311dc | 398 | if (!deviceInUse[idx]) |
sayzyas | 43:78f328f311dc | 399 | return; |
sayzyas | 43:78f328f311dc | 400 | } else { |
sayzyas | 43:78f328f311dc | 401 | return; |
sayzyas | 43:78f328f311dc | 402 | } |
sayzyas | 43:78f328f311dc | 403 | |
sayzyas | 43:78f328f311dc | 404 | message_t * usb_msg = mail_usb_event.alloc(); |
sayzyas | 43:78f328f311dc | 405 | usb_msg->event_id = DEVICE_DISCONNECTED_EVENT; |
sayzyas | 43:78f328f311dc | 406 | usb_msg->hub = hub; |
sayzyas | 43:78f328f311dc | 407 | usb_msg->port = port; |
sayzyas | 43:78f328f311dc | 408 | usb_msg->hub_parent = hub_parent; |
sayzyas | 43:78f328f311dc | 409 | mail_usb_event.put(usb_msg); |
sayzyas | 43:78f328f311dc | 410 | } |
sayzyas | 43:78f328f311dc | 411 | |
sayzyas | 43:78f328f311dc | 412 | void USBHost::freeDevice(USBDeviceConnected * dev) |
sayzyas | 43:78f328f311dc | 413 | { |
sayzyas | 43:78f328f311dc | 414 | USBEndpoint * ep = NULL; |
sayzyas | 43:78f328f311dc | 415 | HCED * ed = NULL; |
sayzyas | 43:78f328f311dc | 416 | |
sayzyas | 43:78f328f311dc | 417 | #if MAX_HUB_NB |
sayzyas | 43:78f328f311dc | 418 | if (dev->getClass() == HUB_CLASS) { |
sayzyas | 43:78f328f311dc | 419 | if (dev->hub == NULL) { |
sayzyas | 43:78f328f311dc | 420 | USB_ERR("HUB NULL!!!!!\r\n"); |
sayzyas | 43:78f328f311dc | 421 | } else { |
sayzyas | 43:78f328f311dc | 422 | dev->hub->hubDisconnected(); |
sayzyas | 43:78f328f311dc | 423 | for (uint8_t i = 0; i < MAX_HUB_NB; i++) { |
sayzyas | 43:78f328f311dc | 424 | if (dev->hub == &hubs[i]) { |
sayzyas | 43:78f328f311dc | 425 | hub_in_use[i] = false; |
sayzyas | 43:78f328f311dc | 426 | break; |
sayzyas | 43:78f328f311dc | 427 | } |
sayzyas | 43:78f328f311dc | 428 | } |
sayzyas | 43:78f328f311dc | 429 | } |
sayzyas | 43:78f328f311dc | 430 | } |
sayzyas | 43:78f328f311dc | 431 | |
sayzyas | 43:78f328f311dc | 432 | // notify hub parent that this device has been disconnected |
sayzyas | 43:78f328f311dc | 433 | if (dev->getHubParent()) |
sayzyas | 43:78f328f311dc | 434 | dev->getHubParent()->deviceDisconnected(dev); |
sayzyas | 43:78f328f311dc | 435 | |
sayzyas | 43:78f328f311dc | 436 | #endif |
sayzyas | 43:78f328f311dc | 437 | |
sayzyas | 43:78f328f311dc | 438 | int idx = findDevice(dev); |
sayzyas | 43:78f328f311dc | 439 | if (idx != -1) { |
sayzyas | 43:78f328f311dc | 440 | deviceInUse[idx] = false; |
sayzyas | 43:78f328f311dc | 441 | deviceReset[idx] = false; |
sayzyas | 43:78f328f311dc | 442 | |
sayzyas | 43:78f328f311dc | 443 | for (uint8_t j = 0; j < MAX_INTF; j++) { |
sayzyas | 43:78f328f311dc | 444 | deviceAttachedDriver[idx][j] = false; |
sayzyas | 43:78f328f311dc | 445 | if (dev->getInterface(j) != NULL) { |
sayzyas | 43:78f328f311dc | 446 | USB_DBG("FREE INTF %d on dev: %p, %p, nb_endpot: %d, %s", j, (void *)dev->getInterface(j), dev, dev->getInterface(j)->nb_endpoint, dev->getName(j)); |
sayzyas | 43:78f328f311dc | 447 | for (int i = 0; i < dev->getInterface(j)->nb_endpoint; i++) { |
sayzyas | 43:78f328f311dc | 448 | if ((ep = dev->getEndpoint(j, i)) != NULL) { |
sayzyas | 43:78f328f311dc | 449 | ed = (HCED *)ep->getHCED(); |
sayzyas | 43:78f328f311dc | 450 | ed->control |= (1 << 14); //sKip bit |
sayzyas | 43:78f328f311dc | 451 | unqueueEndpoint(ep); |
sayzyas | 43:78f328f311dc | 452 | |
sayzyas | 43:78f328f311dc | 453 | freeTD((volatile uint8_t*)ep->getTDList()[0]); |
sayzyas | 43:78f328f311dc | 454 | freeTD((volatile uint8_t*)ep->getTDList()[1]); |
sayzyas | 43:78f328f311dc | 455 | |
sayzyas | 43:78f328f311dc | 456 | freeED((uint8_t *)ep->getHCED()); |
sayzyas | 43:78f328f311dc | 457 | } |
sayzyas | 43:78f328f311dc | 458 | printList(BULK_ENDPOINT); |
sayzyas | 43:78f328f311dc | 459 | printList(INTERRUPT_ENDPOINT); |
sayzyas | 43:78f328f311dc | 460 | } |
sayzyas | 43:78f328f311dc | 461 | USB_INFO("Device disconnected [%p - %s - hub: %d - port: %d]", dev, dev->getName(j), dev->getHub(), dev->getPort()); |
sayzyas | 43:78f328f311dc | 462 | } |
sayzyas | 43:78f328f311dc | 463 | } |
sayzyas | 43:78f328f311dc | 464 | dev->disconnect(); |
sayzyas | 43:78f328f311dc | 465 | } |
sayzyas | 43:78f328f311dc | 466 | } |
sayzyas | 43:78f328f311dc | 467 | |
sayzyas | 43:78f328f311dc | 468 | |
sayzyas | 43:78f328f311dc | 469 | void USBHost::unqueueEndpoint(USBEndpoint * ep) |
sayzyas | 43:78f328f311dc | 470 | { |
sayzyas | 43:78f328f311dc | 471 | USBEndpoint * prec = NULL; |
sayzyas | 43:78f328f311dc | 472 | USBEndpoint * current = NULL; |
sayzyas | 43:78f328f311dc | 473 | |
sayzyas | 43:78f328f311dc | 474 | for (int i = 0; i < 2; i++) { |
sayzyas | 43:78f328f311dc | 475 | current = (i == 0) ? (USBEndpoint*)headBulkEndpoint : (USBEndpoint*)headInterruptEndpoint; |
sayzyas | 43:78f328f311dc | 476 | prec = current; |
sayzyas | 43:78f328f311dc | 477 | while (current != NULL) { |
sayzyas | 43:78f328f311dc | 478 | if (current == ep) { |
sayzyas | 43:78f328f311dc | 479 | if (current->nextEndpoint() != NULL) { |
sayzyas | 43:78f328f311dc | 480 | prec->queueEndpoint(current->nextEndpoint()); |
sayzyas | 43:78f328f311dc | 481 | if (current == headBulkEndpoint) { |
sayzyas | 43:78f328f311dc | 482 | updateBulkHeadED((uint32_t)current->nextEndpoint()->getHCED()); |
sayzyas | 43:78f328f311dc | 483 | headBulkEndpoint = current->nextEndpoint(); |
sayzyas | 43:78f328f311dc | 484 | } else if (current == headInterruptEndpoint) { |
sayzyas | 43:78f328f311dc | 485 | updateInterruptHeadED((uint32_t)current->nextEndpoint()->getHCED()); |
sayzyas | 43:78f328f311dc | 486 | headInterruptEndpoint = current->nextEndpoint(); |
sayzyas | 43:78f328f311dc | 487 | } |
sayzyas | 43:78f328f311dc | 488 | } |
sayzyas | 43:78f328f311dc | 489 | // here we are dequeuing the queue of ed |
sayzyas | 43:78f328f311dc | 490 | // we need to update the tail pointer |
sayzyas | 43:78f328f311dc | 491 | else { |
sayzyas | 43:78f328f311dc | 492 | prec->queueEndpoint(NULL); |
sayzyas | 43:78f328f311dc | 493 | if (current == headBulkEndpoint) { |
sayzyas | 43:78f328f311dc | 494 | updateBulkHeadED(0); |
sayzyas | 43:78f328f311dc | 495 | headBulkEndpoint = current->nextEndpoint(); |
sayzyas | 43:78f328f311dc | 496 | } else if (current == headInterruptEndpoint) { |
sayzyas | 43:78f328f311dc | 497 | updateInterruptHeadED(0); |
sayzyas | 43:78f328f311dc | 498 | headInterruptEndpoint = current->nextEndpoint(); |
sayzyas | 43:78f328f311dc | 499 | } |
sayzyas | 43:78f328f311dc | 500 | |
sayzyas | 43:78f328f311dc | 501 | // modify tail |
sayzyas | 43:78f328f311dc | 502 | switch (current->getType()) { |
sayzyas | 43:78f328f311dc | 503 | case BULK_ENDPOINT: |
sayzyas | 43:78f328f311dc | 504 | tailBulkEndpoint = prec; |
sayzyas | 43:78f328f311dc | 505 | break; |
sayzyas | 43:78f328f311dc | 506 | case INTERRUPT_ENDPOINT: |
sayzyas | 43:78f328f311dc | 507 | tailInterruptEndpoint = prec; |
sayzyas | 43:78f328f311dc | 508 | break; |
sayzyas | 43:78f328f311dc | 509 | default: |
sayzyas | 43:78f328f311dc | 510 | break; |
sayzyas | 43:78f328f311dc | 511 | } |
sayzyas | 43:78f328f311dc | 512 | } |
sayzyas | 43:78f328f311dc | 513 | current->setState(USB_TYPE_FREE); |
sayzyas | 43:78f328f311dc | 514 | return; |
sayzyas | 43:78f328f311dc | 515 | } |
sayzyas | 43:78f328f311dc | 516 | prec = current; |
sayzyas | 43:78f328f311dc | 517 | current = current->nextEndpoint(); |
sayzyas | 43:78f328f311dc | 518 | } |
sayzyas | 43:78f328f311dc | 519 | } |
sayzyas | 43:78f328f311dc | 520 | } |
sayzyas | 43:78f328f311dc | 521 | |
sayzyas | 43:78f328f311dc | 522 | |
sayzyas | 43:78f328f311dc | 523 | USBDeviceConnected * USBHost::getDevice(uint8_t index) |
sayzyas | 43:78f328f311dc | 524 | { |
sayzyas | 43:78f328f311dc | 525 | if ((index >= MAX_DEVICE_CONNECTED) || (!deviceInUse[index])) { |
sayzyas | 43:78f328f311dc | 526 | return NULL; |
sayzyas | 43:78f328f311dc | 527 | } |
sayzyas | 43:78f328f311dc | 528 | return (USBDeviceConnected*)&devices[index]; |
sayzyas | 43:78f328f311dc | 529 | } |
sayzyas | 43:78f328f311dc | 530 | |
sayzyas | 43:78f328f311dc | 531 | // create an USBEndpoint descriptor. the USBEndpoint is not linked |
sayzyas | 43:78f328f311dc | 532 | USBEndpoint * USBHost::newEndpoint(ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t addr) |
sayzyas | 43:78f328f311dc | 533 | { |
sayzyas | 43:78f328f311dc | 534 | int i = 0; |
sayzyas | 43:78f328f311dc | 535 | HCED * ed = (HCED *)getED(); |
sayzyas | 43:78f328f311dc | 536 | HCTD* td_list[2] = { (HCTD*)getTD(), (HCTD*)getTD() }; |
sayzyas | 43:78f328f311dc | 537 | |
sayzyas | 43:78f328f311dc | 538 | memset((void *)td_list[0], 0x00, sizeof(HCTD)); |
sayzyas | 43:78f328f311dc | 539 | memset((void *)td_list[1], 0x00, sizeof(HCTD)); |
sayzyas | 43:78f328f311dc | 540 | |
sayzyas | 43:78f328f311dc | 541 | // search a free USBEndpoint |
sayzyas | 43:78f328f311dc | 542 | for (i = 0; i < MAX_ENDPOINT; i++) { |
sayzyas | 43:78f328f311dc | 543 | if (endpoints[i].getState() == USB_TYPE_FREE) { |
sayzyas | 43:78f328f311dc | 544 | endpoints[i].init(ed, type, dir, size, addr, td_list); |
sayzyas | 43:78f328f311dc | 545 | USB_DBG("USBEndpoint created (%p): type: %d, dir: %d, size: %d, addr: %d, state: %s", &endpoints[i], type, dir, size, addr, endpoints[i].getStateString()); |
sayzyas | 43:78f328f311dc | 546 | return &endpoints[i]; |
sayzyas | 43:78f328f311dc | 547 | } |
sayzyas | 43:78f328f311dc | 548 | } |
sayzyas | 43:78f328f311dc | 549 | USB_ERR("could not allocate more endpoints!!!!"); |
sayzyas | 43:78f328f311dc | 550 | return NULL; |
sayzyas | 43:78f328f311dc | 551 | } |
sayzyas | 43:78f328f311dc | 552 | |
sayzyas | 43:78f328f311dc | 553 | |
sayzyas | 43:78f328f311dc | 554 | USB_TYPE USBHost::resetDevice(USBDeviceConnected * dev) |
sayzyas | 43:78f328f311dc | 555 | { |
sayzyas | 43:78f328f311dc | 556 | int index = findDevice(dev); |
sayzyas | 43:78f328f311dc | 557 | if (index != -1) { |
sayzyas | 43:78f328f311dc | 558 | USB_DBG("Resetting hub %d, port %d\n", dev->getHub(), dev->getPort()); |
sayzyas | 43:78f328f311dc | 559 | Thread::wait(100); |
sayzyas | 43:78f328f311dc | 560 | if (dev->getHub() == 0) { |
sayzyas | 43:78f328f311dc | 561 | resetRootHub(); |
sayzyas | 43:78f328f311dc | 562 | } |
sayzyas | 43:78f328f311dc | 563 | #if MAX_HUB_NB |
sayzyas | 43:78f328f311dc | 564 | else { |
sayzyas | 43:78f328f311dc | 565 | dev->getHubParent()->portReset(dev->getPort()); |
sayzyas | 43:78f328f311dc | 566 | } |
sayzyas | 43:78f328f311dc | 567 | #endif |
sayzyas | 43:78f328f311dc | 568 | Thread::wait(100); |
sayzyas | 43:78f328f311dc | 569 | deviceReset[index] = true; |
sayzyas | 43:78f328f311dc | 570 | return USB_TYPE_OK; |
sayzyas | 43:78f328f311dc | 571 | } |
sayzyas | 43:78f328f311dc | 572 | |
sayzyas | 43:78f328f311dc | 573 | return USB_TYPE_ERROR; |
sayzyas | 43:78f328f311dc | 574 | } |
sayzyas | 43:78f328f311dc | 575 | |
sayzyas | 43:78f328f311dc | 576 | // link the USBEndpoint to the linked list and attach an USBEndpoint to a device |
sayzyas | 43:78f328f311dc | 577 | bool USBHost::addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint * ep) |
sayzyas | 43:78f328f311dc | 578 | { |
sayzyas | 43:78f328f311dc | 579 | |
sayzyas | 43:78f328f311dc | 580 | if (ep == NULL) { |
sayzyas | 43:78f328f311dc | 581 | return false; |
sayzyas | 43:78f328f311dc | 582 | } |
sayzyas | 43:78f328f311dc | 583 | |
sayzyas | 43:78f328f311dc | 584 | HCED * prevEd; |
sayzyas | 43:78f328f311dc | 585 | |
sayzyas | 43:78f328f311dc | 586 | // set device address in the USBEndpoint descriptor |
sayzyas | 43:78f328f311dc | 587 | if (dev == NULL) { |
sayzyas | 43:78f328f311dc | 588 | ep->setDeviceAddress(0); |
sayzyas | 43:78f328f311dc | 589 | } else { |
sayzyas | 43:78f328f311dc | 590 | ep->setDeviceAddress(dev->getAddress()); |
sayzyas | 43:78f328f311dc | 591 | } |
sayzyas | 43:78f328f311dc | 592 | |
sayzyas | 43:78f328f311dc | 593 | if ((dev != NULL) && dev->getSpeed()) { |
sayzyas | 43:78f328f311dc | 594 | ep->setSpeed(dev->getSpeed()); |
sayzyas | 43:78f328f311dc | 595 | } |
sayzyas | 43:78f328f311dc | 596 | |
sayzyas | 43:78f328f311dc | 597 | ep->setIntfNb(intf_nb); |
sayzyas | 43:78f328f311dc | 598 | |
sayzyas | 43:78f328f311dc | 599 | // queue the new USBEndpoint on the ED list |
sayzyas | 43:78f328f311dc | 600 | switch (ep->getType()) { |
sayzyas | 43:78f328f311dc | 601 | |
sayzyas | 43:78f328f311dc | 602 | case CONTROL_ENDPOINT: |
sayzyas | 43:78f328f311dc | 603 | prevEd = ( HCED*) controlHeadED(); |
sayzyas | 43:78f328f311dc | 604 | if (!prevEd) { |
sayzyas | 43:78f328f311dc | 605 | updateControlHeadED((uint32_t) ep->getHCED()); |
sayzyas | 43:78f328f311dc | 606 | USB_DBG_TRANSFER("First control USBEndpoint: %08X", (uint32_t) ep->getHCED()); |
sayzyas | 43:78f328f311dc | 607 | headControlEndpoint = ep; |
sayzyas | 43:78f328f311dc | 608 | tailControlEndpoint = ep; |
sayzyas | 43:78f328f311dc | 609 | return true; |
sayzyas | 43:78f328f311dc | 610 | } |
sayzyas | 43:78f328f311dc | 611 | tailControlEndpoint->queueEndpoint(ep); |
sayzyas | 43:78f328f311dc | 612 | tailControlEndpoint = ep; |
sayzyas | 43:78f328f311dc | 613 | return true; |
sayzyas | 43:78f328f311dc | 614 | |
sayzyas | 43:78f328f311dc | 615 | case BULK_ENDPOINT: |
sayzyas | 43:78f328f311dc | 616 | prevEd = ( HCED*) bulkHeadED(); |
sayzyas | 43:78f328f311dc | 617 | if (!prevEd) { |
sayzyas | 43:78f328f311dc | 618 | updateBulkHeadED((uint32_t) ep->getHCED()); |
sayzyas | 43:78f328f311dc | 619 | USB_DBG_TRANSFER("First bulk USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED()); |
sayzyas | 43:78f328f311dc | 620 | headBulkEndpoint = ep; |
sayzyas | 43:78f328f311dc | 621 | tailBulkEndpoint = ep; |
sayzyas | 43:78f328f311dc | 622 | break; |
sayzyas | 43:78f328f311dc | 623 | } |
sayzyas | 43:78f328f311dc | 624 | USB_DBG_TRANSFER("Queue BULK Ed %p after %p\r\n",ep->getHCED(), prevEd); |
sayzyas | 43:78f328f311dc | 625 | tailBulkEndpoint->queueEndpoint(ep); |
sayzyas | 43:78f328f311dc | 626 | tailBulkEndpoint = ep; |
sayzyas | 43:78f328f311dc | 627 | break; |
sayzyas | 43:78f328f311dc | 628 | |
sayzyas | 43:78f328f311dc | 629 | case INTERRUPT_ENDPOINT: |
sayzyas | 43:78f328f311dc | 630 | prevEd = ( HCED*) interruptHeadED(); |
sayzyas | 43:78f328f311dc | 631 | if (!prevEd) { |
sayzyas | 43:78f328f311dc | 632 | updateInterruptHeadED((uint32_t) ep->getHCED()); |
sayzyas | 43:78f328f311dc | 633 | USB_DBG_TRANSFER("First interrupt USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED()); |
sayzyas | 43:78f328f311dc | 634 | headInterruptEndpoint = ep; |
sayzyas | 43:78f328f311dc | 635 | tailInterruptEndpoint = ep; |
sayzyas | 43:78f328f311dc | 636 | break; |
sayzyas | 43:78f328f311dc | 637 | } |
sayzyas | 43:78f328f311dc | 638 | USB_DBG_TRANSFER("Queue INTERRUPT Ed %p after %p\r\n",ep->getHCED(), prevEd); |
sayzyas | 43:78f328f311dc | 639 | tailInterruptEndpoint->queueEndpoint(ep); |
sayzyas | 43:78f328f311dc | 640 | tailInterruptEndpoint = ep; |
sayzyas | 43:78f328f311dc | 641 | break; |
sayzyas | 43:78f328f311dc | 642 | default: |
sayzyas | 43:78f328f311dc | 643 | return false; |
sayzyas | 43:78f328f311dc | 644 | } |
sayzyas | 43:78f328f311dc | 645 | |
sayzyas | 43:78f328f311dc | 646 | ep->dev = dev; |
sayzyas | 43:78f328f311dc | 647 | dev->addEndpoint(intf_nb, ep); |
sayzyas | 43:78f328f311dc | 648 | |
sayzyas | 43:78f328f311dc | 649 | return true; |
sayzyas | 43:78f328f311dc | 650 | } |
sayzyas | 43:78f328f311dc | 651 | |
sayzyas | 43:78f328f311dc | 652 | |
sayzyas | 43:78f328f311dc | 653 | int USBHost::findDevice(USBDeviceConnected * dev) |
sayzyas | 43:78f328f311dc | 654 | { |
sayzyas | 43:78f328f311dc | 655 | for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
sayzyas | 43:78f328f311dc | 656 | if (dev == &devices[i]) { |
sayzyas | 43:78f328f311dc | 657 | return i; |
sayzyas | 43:78f328f311dc | 658 | } |
sayzyas | 43:78f328f311dc | 659 | } |
sayzyas | 43:78f328f311dc | 660 | return -1; |
sayzyas | 43:78f328f311dc | 661 | } |
sayzyas | 43:78f328f311dc | 662 | |
sayzyas | 43:78f328f311dc | 663 | int USBHost::findDevice(uint8_t hub, uint8_t port, USBHostHub * hub_parent) |
sayzyas | 43:78f328f311dc | 664 | { |
sayzyas | 43:78f328f311dc | 665 | for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
sayzyas | 43:78f328f311dc | 666 | if (devices[i].getHub() == hub && devices[i].getPort() == port) { |
sayzyas | 43:78f328f311dc | 667 | if (hub_parent != NULL) { |
sayzyas | 43:78f328f311dc | 668 | if (hub_parent == devices[i].getHubParent()) |
sayzyas | 43:78f328f311dc | 669 | return i; |
sayzyas | 43:78f328f311dc | 670 | } else { |
sayzyas | 43:78f328f311dc | 671 | return i; |
sayzyas | 43:78f328f311dc | 672 | } |
sayzyas | 43:78f328f311dc | 673 | } |
sayzyas | 43:78f328f311dc | 674 | } |
sayzyas | 43:78f328f311dc | 675 | return -1; |
sayzyas | 43:78f328f311dc | 676 | } |
sayzyas | 43:78f328f311dc | 677 | |
sayzyas | 43:78f328f311dc | 678 | void USBHost::printList(ENDPOINT_TYPE type) |
sayzyas | 43:78f328f311dc | 679 | { |
sayzyas | 43:78f328f311dc | 680 | #if DEBUG_EP_STATE |
sayzyas | 43:78f328f311dc | 681 | volatile HCED * hced; |
sayzyas | 43:78f328f311dc | 682 | switch(type) { |
sayzyas | 43:78f328f311dc | 683 | case CONTROL_ENDPOINT: |
sayzyas | 43:78f328f311dc | 684 | hced = (HCED *)controlHeadED(); |
sayzyas | 43:78f328f311dc | 685 | break; |
sayzyas | 43:78f328f311dc | 686 | case BULK_ENDPOINT: |
sayzyas | 43:78f328f311dc | 687 | hced = (HCED *)bulkHeadED(); |
sayzyas | 43:78f328f311dc | 688 | break; |
sayzyas | 43:78f328f311dc | 689 | case INTERRUPT_ENDPOINT: |
sayzyas | 43:78f328f311dc | 690 | hced = (HCED *)interruptHeadED(); |
sayzyas | 43:78f328f311dc | 691 | break; |
sayzyas | 43:78f328f311dc | 692 | } |
sayzyas | 43:78f328f311dc | 693 | volatile HCTD * hctd = NULL; |
sayzyas | 43:78f328f311dc | 694 | const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : |
sayzyas | 43:78f328f311dc | 695 | ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : |
sayzyas | 43:78f328f311dc | 696 | ((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS")); |
sayzyas | 43:78f328f311dc | 697 | printf("State of %s:\r\n", type_str); |
sayzyas | 43:78f328f311dc | 698 | while (hced != NULL) { |
sayzyas | 43:78f328f311dc | 699 | uint8_t dir = ((hced->control & (3 << 11)) >> 11); |
sayzyas | 43:78f328f311dc | 700 | printf("hced: %p [ADDR: %d, DIR: %s, EP_NB: 0x%X]\r\n", hced, |
sayzyas | 43:78f328f311dc | 701 | hced->control & 0x7f, |
sayzyas | 43:78f328f311dc | 702 | (dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"), |
sayzyas | 43:78f328f311dc | 703 | (hced->control & (0xf << 7)) >> 7); |
sayzyas | 43:78f328f311dc | 704 | hctd = (HCTD *)((uint32_t)(hced->headTD) & ~(0xf)); |
sayzyas | 43:78f328f311dc | 705 | while (hctd != hced->tailTD) { |
sayzyas | 43:78f328f311dc | 706 | printf("\thctd: %p [DIR: %s]\r\n", hctd, ((hctd->control & (3 << 19)) >> 19) == 1 ? "OUT" : "IN"); |
sayzyas | 43:78f328f311dc | 707 | hctd = hctd->nextTD; |
sayzyas | 43:78f328f311dc | 708 | } |
sayzyas | 43:78f328f311dc | 709 | printf("\thctd: %p\r\n", hctd); |
sayzyas | 43:78f328f311dc | 710 | hced = hced->nextED; |
sayzyas | 43:78f328f311dc | 711 | } |
sayzyas | 43:78f328f311dc | 712 | printf("\r\n\r\n"); |
sayzyas | 43:78f328f311dc | 713 | #endif |
sayzyas | 43:78f328f311dc | 714 | } |
sayzyas | 43:78f328f311dc | 715 | |
sayzyas | 43:78f328f311dc | 716 | |
sayzyas | 43:78f328f311dc | 717 | // add a transfer on the TD linked list |
sayzyas | 43:78f328f311dc | 718 | USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len) |
sayzyas | 43:78f328f311dc | 719 | { |
sayzyas | 43:78f328f311dc | 720 | td_mutex.lock(); |
sayzyas | 43:78f328f311dc | 721 | |
sayzyas | 43:78f328f311dc | 722 | // allocate a TD which will be freed in TDcompletion |
sayzyas | 43:78f328f311dc | 723 | volatile HCTD * td = ed->getNextTD(); |
sayzyas | 43:78f328f311dc | 724 | if (td == NULL) { |
sayzyas | 43:78f328f311dc | 725 | return USB_TYPE_ERROR; |
sayzyas | 43:78f328f311dc | 726 | } |
sayzyas | 43:78f328f311dc | 727 | |
sayzyas | 43:78f328f311dc | 728 | uint32_t token = (ed->isSetup() ? TD_SETUP : ( (ed->getDir() == IN) ? TD_IN : TD_OUT )); |
sayzyas | 43:78f328f311dc | 729 | |
sayzyas | 43:78f328f311dc | 730 | uint32_t td_toggle; |
sayzyas | 43:78f328f311dc | 731 | |
sayzyas | 43:78f328f311dc | 732 | if (ed->getType() == CONTROL_ENDPOINT) { |
sayzyas | 43:78f328f311dc | 733 | if (ed->isSetup()) { |
sayzyas | 43:78f328f311dc | 734 | td_toggle = TD_TOGGLE_0; |
sayzyas | 43:78f328f311dc | 735 | } else { |
sayzyas | 43:78f328f311dc | 736 | td_toggle = TD_TOGGLE_1; |
sayzyas | 43:78f328f311dc | 737 | } |
sayzyas | 43:78f328f311dc | 738 | } else { |
sayzyas | 43:78f328f311dc | 739 | td_toggle = 0; |
sayzyas | 43:78f328f311dc | 740 | } |
sayzyas | 43:78f328f311dc | 741 | |
sayzyas | 43:78f328f311dc | 742 | td->control = (TD_ROUNDING | token | TD_DELAY_INT(0) | td_toggle | TD_CC); |
sayzyas | 43:78f328f311dc | 743 | td->currBufPtr = buf; |
sayzyas | 43:78f328f311dc | 744 | td->bufEnd = (buf + (len - 1)); |
sayzyas | 43:78f328f311dc | 745 | |
sayzyas | 43:78f328f311dc | 746 | ENDPOINT_TYPE type = ed->getType(); |
sayzyas | 43:78f328f311dc | 747 | |
sayzyas | 43:78f328f311dc | 748 | disableList(type); |
sayzyas | 43:78f328f311dc | 749 | ed->queueTransfer(); |
sayzyas | 43:78f328f311dc | 750 | printList(type); |
sayzyas | 43:78f328f311dc | 751 | enableList(type); |
sayzyas | 43:78f328f311dc | 752 | |
sayzyas | 43:78f328f311dc | 753 | td_mutex.unlock(); |
sayzyas | 43:78f328f311dc | 754 | |
sayzyas | 43:78f328f311dc | 755 | return USB_TYPE_PROCESSING; |
sayzyas | 43:78f328f311dc | 756 | } |
sayzyas | 43:78f328f311dc | 757 | |
sayzyas | 43:78f328f311dc | 758 | |
sayzyas | 43:78f328f311dc | 759 | |
sayzyas | 43:78f328f311dc | 760 | USB_TYPE USBHost::getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr) |
sayzyas | 43:78f328f311dc | 761 | { |
sayzyas | 43:78f328f311dc | 762 | USB_TYPE t = controlRead( dev, |
sayzyas | 43:78f328f311dc | 763 | USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, |
sayzyas | 43:78f328f311dc | 764 | GET_DESCRIPTOR, |
sayzyas | 43:78f328f311dc | 765 | (DEVICE_DESCRIPTOR << 8) | (0), |
sayzyas | 43:78f328f311dc | 766 | 0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf)); |
sayzyas | 43:78f328f311dc | 767 | if (len_dev_descr) |
sayzyas | 43:78f328f311dc | 768 | *len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf); |
sayzyas | 43:78f328f311dc | 769 | |
sayzyas | 43:78f328f311dc | 770 | return t; |
sayzyas | 43:78f328f311dc | 771 | } |
sayzyas | 43:78f328f311dc | 772 | |
sayzyas | 43:78f328f311dc | 773 | USB_TYPE USBHost::getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_conf_descr) |
sayzyas | 43:78f328f311dc | 774 | { |
sayzyas | 43:78f328f311dc | 775 | USB_TYPE res; |
sayzyas | 43:78f328f311dc | 776 | uint16_t total_conf_descr_length = 0; |
sayzyas | 43:78f328f311dc | 777 | |
sayzyas | 43:78f328f311dc | 778 | // fourth step: get the beginning of the configuration descriptor to have the total length of the conf descr |
sayzyas | 43:78f328f311dc | 779 | res = controlRead( dev, |
sayzyas | 43:78f328f311dc | 780 | USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, |
sayzyas | 43:78f328f311dc | 781 | GET_DESCRIPTOR, |
sayzyas | 43:78f328f311dc | 782 | (CONFIGURATION_DESCRIPTOR << 8) | (0), |
sayzyas | 43:78f328f311dc | 783 | 0, buf, CONFIGURATION_DESCRIPTOR_LENGTH); |
sayzyas | 43:78f328f311dc | 784 | |
sayzyas | 43:78f328f311dc | 785 | if (res != USB_TYPE_OK) { |
sayzyas | 43:78f328f311dc | 786 | USB_ERR("GET CONF 1 DESCR FAILED"); |
sayzyas | 43:78f328f311dc | 787 | return res; |
sayzyas | 43:78f328f311dc | 788 | } |
sayzyas | 43:78f328f311dc | 789 | total_conf_descr_length = buf[2] | (buf[3] << 8); |
sayzyas | 43:78f328f311dc | 790 | total_conf_descr_length = MIN(max_len_buf, total_conf_descr_length); |
sayzyas | 43:78f328f311dc | 791 | |
sayzyas | 43:78f328f311dc | 792 | if (len_conf_descr) |
sayzyas | 43:78f328f311dc | 793 | *len_conf_descr = total_conf_descr_length; |
sayzyas | 43:78f328f311dc | 794 | |
sayzyas | 43:78f328f311dc | 795 | USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]); |
sayzyas | 43:78f328f311dc | 796 | |
sayzyas | 43:78f328f311dc | 797 | return controlRead( dev, |
sayzyas | 43:78f328f311dc | 798 | USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, |
sayzyas | 43:78f328f311dc | 799 | GET_DESCRIPTOR, |
sayzyas | 43:78f328f311dc | 800 | (CONFIGURATION_DESCRIPTOR << 8) | (0), |
sayzyas | 43:78f328f311dc | 801 | 0, buf, total_conf_descr_length); |
sayzyas | 43:78f328f311dc | 802 | } |
sayzyas | 43:78f328f311dc | 803 | |
sayzyas | 43:78f328f311dc | 804 | |
sayzyas | 43:78f328f311dc | 805 | USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address) { |
sayzyas | 43:78f328f311dc | 806 | return controlWrite( dev, |
sayzyas | 43:78f328f311dc | 807 | USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, |
sayzyas | 43:78f328f311dc | 808 | SET_ADDRESS, |
sayzyas | 43:78f328f311dc | 809 | address, |
sayzyas | 43:78f328f311dc | 810 | 0, NULL, 0); |
sayzyas | 43:78f328f311dc | 811 | |
sayzyas | 43:78f328f311dc | 812 | } |
sayzyas | 43:78f328f311dc | 813 | |
sayzyas | 43:78f328f311dc | 814 | USB_TYPE USBHost::setConfiguration(USBDeviceConnected * dev, uint8_t conf) |
sayzyas | 43:78f328f311dc | 815 | { |
sayzyas | 43:78f328f311dc | 816 | return controlWrite( dev, |
sayzyas | 43:78f328f311dc | 817 | USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, |
sayzyas | 43:78f328f311dc | 818 | SET_CONFIGURATION, |
sayzyas | 43:78f328f311dc | 819 | conf, |
sayzyas | 43:78f328f311dc | 820 | 0, NULL, 0); |
sayzyas | 43:78f328f311dc | 821 | } |
sayzyas | 43:78f328f311dc | 822 | |
sayzyas | 43:78f328f311dc | 823 | uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev) { |
sayzyas | 43:78f328f311dc | 824 | int index = findDevice(dev); |
sayzyas | 43:78f328f311dc | 825 | uint8_t cnt = 0; |
sayzyas | 43:78f328f311dc | 826 | if (index == -1) |
sayzyas | 43:78f328f311dc | 827 | return 0; |
sayzyas | 43:78f328f311dc | 828 | for (uint8_t i = 0; i < MAX_INTF; i++) { |
sayzyas | 43:78f328f311dc | 829 | if (deviceAttachedDriver[index][i]) |
sayzyas | 43:78f328f311dc | 830 | cnt++; |
sayzyas | 43:78f328f311dc | 831 | } |
sayzyas | 43:78f328f311dc | 832 | return cnt; |
sayzyas | 43:78f328f311dc | 833 | } |
sayzyas | 43:78f328f311dc | 834 | |
sayzyas | 43:78f328f311dc | 835 | // enumerate a device with the control USBEndpoint |
sayzyas | 43:78f328f311dc | 836 | USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator) |
sayzyas | 43:78f328f311dc | 837 | { |
sayzyas | 43:78f328f311dc | 838 | uint16_t total_conf_descr_length = 0; |
sayzyas | 43:78f328f311dc | 839 | USB_TYPE res; |
sayzyas | 43:78f328f311dc | 840 | |
sayzyas | 43:78f328f311dc | 841 | do |
sayzyas | 43:78f328f311dc | 842 | { |
sayzyas | 43:78f328f311dc | 843 | Lock lock(this); |
sayzyas | 43:78f328f311dc | 844 | |
sayzyas | 43:78f328f311dc | 845 | // don't enumerate a device which all interfaces are registered to a specific driver |
sayzyas | 43:78f328f311dc | 846 | int index = findDevice(dev); |
sayzyas | 43:78f328f311dc | 847 | |
sayzyas | 43:78f328f311dc | 848 | if (index == -1) { |
sayzyas | 43:78f328f311dc | 849 | return USB_TYPE_ERROR; |
sayzyas | 43:78f328f311dc | 850 | } |
sayzyas | 43:78f328f311dc | 851 | |
sayzyas | 43:78f328f311dc | 852 | uint8_t nb_intf_attached = numberDriverAttached(dev); |
sayzyas | 43:78f328f311dc | 853 | USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf()); |
sayzyas | 43:78f328f311dc | 854 | USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached); |
sayzyas | 43:78f328f311dc | 855 | if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) { |
sayzyas | 43:78f328f311dc | 856 | USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev); |
sayzyas | 43:78f328f311dc | 857 | return USB_TYPE_OK; |
sayzyas | 43:78f328f311dc | 858 | } |
sayzyas | 43:78f328f311dc | 859 | |
sayzyas | 43:78f328f311dc | 860 | USB_DBG("Enumerate dev: %p", dev); |
sayzyas | 43:78f328f311dc | 861 | |
sayzyas | 43:78f328f311dc | 862 | // third step: get the whole device descriptor to see vid, pid |
sayzyas | 43:78f328f311dc | 863 | res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH); |
sayzyas | 43:78f328f311dc | 864 | |
sayzyas | 43:78f328f311dc | 865 | if (res != USB_TYPE_OK) { |
sayzyas | 43:78f328f311dc | 866 | USB_DBG("GET DEV DESCR FAILED"); |
sayzyas | 43:78f328f311dc | 867 | return res; |
sayzyas | 43:78f328f311dc | 868 | } |
sayzyas | 43:78f328f311dc | 869 | |
sayzyas | 43:78f328f311dc | 870 | dev->setClass(data[4]); |
sayzyas | 43:78f328f311dc | 871 | dev->setSubClass(data[5]); |
sayzyas | 43:78f328f311dc | 872 | dev->setProtocol(data[6]); |
sayzyas | 43:78f328f311dc | 873 | dev->setVid(data[8] | (data[9] << 8)); |
sayzyas | 43:78f328f311dc | 874 | dev->setPid(data[10] | (data[11] << 8)); |
sayzyas | 43:78f328f311dc | 875 | USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8)); |
sayzyas | 43:78f328f311dc | 876 | |
sayzyas | 43:78f328f311dc | 877 | pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) ); |
sayzyas | 43:78f328f311dc | 878 | |
sayzyas | 43:78f328f311dc | 879 | res = getConfigurationDescriptor(dev, data, sizeof(data), &total_conf_descr_length); |
sayzyas | 43:78f328f311dc | 880 | if (res != USB_TYPE_OK) { |
sayzyas | 43:78f328f311dc | 881 | return res; |
sayzyas | 43:78f328f311dc | 882 | } |
sayzyas | 43:78f328f311dc | 883 | |
sayzyas | 43:78f328f311dc | 884 | #if (DEBUG > 3) |
sayzyas | 43:78f328f311dc | 885 | USB_DBG("CONFIGURATION DESCRIPTOR:\r\n"); |
sayzyas | 43:78f328f311dc | 886 | for (int i = 0; i < total_conf_descr_length; i++) |
sayzyas | 43:78f328f311dc | 887 | printf("%02X ", data[i]); |
sayzyas | 43:78f328f311dc | 888 | printf("\r\n\r\n"); |
sayzyas | 43:78f328f311dc | 889 | #endif |
sayzyas | 43:78f328f311dc | 890 | |
sayzyas | 43:78f328f311dc | 891 | // Parse the configuration descriptor |
sayzyas | 43:78f328f311dc | 892 | parseConfDescr(dev, data, total_conf_descr_length, pEnumerator); |
sayzyas | 43:78f328f311dc | 893 | |
sayzyas | 43:78f328f311dc | 894 | // only set configuration if not enumerated before |
sayzyas | 43:78f328f311dc | 895 | if (!dev->isEnumerated()) { |
sayzyas | 43:78f328f311dc | 896 | |
sayzyas | 43:78f328f311dc | 897 | USB_DBG("Set configuration 1 on dev: %p", dev); |
sayzyas | 43:78f328f311dc | 898 | // sixth step: set configuration (only 1 supported) |
sayzyas | 43:78f328f311dc | 899 | res = setConfiguration(dev, 1); |
sayzyas | 43:78f328f311dc | 900 | |
sayzyas | 43:78f328f311dc | 901 | if (res != USB_TYPE_OK) { |
sayzyas | 43:78f328f311dc | 902 | USB_DBG("SET CONF FAILED"); |
sayzyas | 43:78f328f311dc | 903 | return res; |
sayzyas | 43:78f328f311dc | 904 | } |
sayzyas | 43:78f328f311dc | 905 | } |
sayzyas | 43:78f328f311dc | 906 | |
sayzyas | 43:78f328f311dc | 907 | dev->setEnumerated(); |
sayzyas | 43:78f328f311dc | 908 | |
sayzyas | 43:78f328f311dc | 909 | // Now the device is enumerated! |
sayzyas | 43:78f328f311dc | 910 | USB_DBG("dev %p is enumerated\r\n", dev); |
sayzyas | 43:78f328f311dc | 911 | |
sayzyas | 43:78f328f311dc | 912 | } while(0); |
sayzyas | 43:78f328f311dc | 913 | |
sayzyas | 43:78f328f311dc | 914 | // Some devices may require this delay |
sayzyas | 43:78f328f311dc | 915 | Thread::wait(100); |
sayzyas | 43:78f328f311dc | 916 | |
sayzyas | 43:78f328f311dc | 917 | return USB_TYPE_OK; |
sayzyas | 43:78f328f311dc | 918 | } |
sayzyas | 43:78f328f311dc | 919 | // this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor. |
sayzyas | 43:78f328f311dc | 920 | void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator) |
sayzyas | 43:78f328f311dc | 921 | { |
sayzyas | 43:78f328f311dc | 922 | uint32_t index = 0; |
sayzyas | 43:78f328f311dc | 923 | uint32_t len_desc = 0; |
sayzyas | 43:78f328f311dc | 924 | uint8_t id = 0; |
sayzyas | 43:78f328f311dc | 925 | int nb_endpoints_used = 0; |
sayzyas | 43:78f328f311dc | 926 | USBEndpoint * ep = NULL; |
sayzyas | 43:78f328f311dc | 927 | uint8_t intf_nb = 0; |
sayzyas | 43:78f328f311dc | 928 | bool parsing_intf = false; |
sayzyas | 43:78f328f311dc | 929 | uint8_t current_intf = 0; |
sayzyas | 43:78f328f311dc | 930 | |
sayzyas | 43:78f328f311dc | 931 | while (index < len) { |
sayzyas | 43:78f328f311dc | 932 | len_desc = conf_descr[index]; |
sayzyas | 43:78f328f311dc | 933 | id = conf_descr[index+1]; |
sayzyas | 43:78f328f311dc | 934 | switch (id) { |
sayzyas | 43:78f328f311dc | 935 | case CONFIGURATION_DESCRIPTOR: |
sayzyas | 43:78f328f311dc | 936 | USB_DBG("dev: %p has %d intf", dev, conf_descr[4]); |
sayzyas | 43:78f328f311dc | 937 | dev->setNbIntf(conf_descr[4]); |
sayzyas | 43:78f328f311dc | 938 | break; |
sayzyas | 43:78f328f311dc | 939 | case INTERFACE_DESCRIPTOR: |
sayzyas | 43:78f328f311dc | 940 | if(pEnumerator->parseInterface(conf_descr[index + 2], conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7])) { |
sayzyas | 43:78f328f311dc | 941 | if (intf_nb++ <= MAX_INTF) { |
sayzyas | 43:78f328f311dc | 942 | current_intf = conf_descr[index + 2]; |
sayzyas | 43:78f328f311dc | 943 | dev->addInterface(current_intf, conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7]); |
sayzyas | 43:78f328f311dc | 944 | nb_endpoints_used = 0; |
sayzyas | 43:78f328f311dc | 945 | USB_DBG("ADD INTF %d on device %p: class: %d, subclass: %d, proto: %d", current_intf, dev, conf_descr[index + 5],conf_descr[index + 6],conf_descr[index + 7]); |
sayzyas | 43:78f328f311dc | 946 | } else { |
sayzyas | 43:78f328f311dc | 947 | USB_DBG("Drop intf..."); |
sayzyas | 43:78f328f311dc | 948 | } |
sayzyas | 43:78f328f311dc | 949 | parsing_intf = true; |
sayzyas | 43:78f328f311dc | 950 | } else { |
sayzyas | 43:78f328f311dc | 951 | parsing_intf = false; |
sayzyas | 43:78f328f311dc | 952 | } |
sayzyas | 43:78f328f311dc | 953 | break; |
sayzyas | 43:78f328f311dc | 954 | case ENDPOINT_DESCRIPTOR: |
sayzyas | 43:78f328f311dc | 955 | if (parsing_intf && (intf_nb <= MAX_INTF) ) { |
sayzyas | 43:78f328f311dc | 956 | if (nb_endpoints_used < MAX_ENDPOINT_PER_INTERFACE) { |
sayzyas | 43:78f328f311dc | 957 | if( pEnumerator->useEndpoint(current_intf, (ENDPOINT_TYPE)(conf_descr[index + 3] & 0x03), (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1)) ) { |
sayzyas | 43:78f328f311dc | 958 | // if the USBEndpoint is isochronous -> skip it (TODO: fix this) |
sayzyas | 43:78f328f311dc | 959 | if ((conf_descr[index + 3] & 0x03) != ISOCHRONOUS_ENDPOINT) { |
sayzyas | 43:78f328f311dc | 960 | ep = newEndpoint((ENDPOINT_TYPE)(conf_descr[index+3] & 0x03), |
sayzyas | 43:78f328f311dc | 961 | (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1), |
sayzyas | 43:78f328f311dc | 962 | conf_descr[index + 4] | (conf_descr[index + 5] << 8), |
sayzyas | 43:78f328f311dc | 963 | conf_descr[index + 2] & 0x0f); |
sayzyas | 43:78f328f311dc | 964 | USB_DBG("ADD USBEndpoint %p, on interf %d on device %p", ep, current_intf, dev); |
sayzyas | 43:78f328f311dc | 965 | if (ep != NULL && dev != NULL) { |
sayzyas | 43:78f328f311dc | 966 | addEndpoint(dev, current_intf, ep); |
sayzyas | 43:78f328f311dc | 967 | } else { |
sayzyas | 43:78f328f311dc | 968 | USB_DBG("EP NULL"); |
sayzyas | 43:78f328f311dc | 969 | } |
sayzyas | 43:78f328f311dc | 970 | nb_endpoints_used++; |
sayzyas | 43:78f328f311dc | 971 | } else { |
sayzyas | 43:78f328f311dc | 972 | USB_DBG("ISO USBEndpoint NOT SUPPORTED"); |
sayzyas | 43:78f328f311dc | 973 | } |
sayzyas | 43:78f328f311dc | 974 | } |
sayzyas | 43:78f328f311dc | 975 | } |
sayzyas | 43:78f328f311dc | 976 | } |
sayzyas | 43:78f328f311dc | 977 | break; |
sayzyas | 43:78f328f311dc | 978 | case HID_DESCRIPTOR: |
sayzyas | 43:78f328f311dc | 979 | lenReportDescr = conf_descr[index + 7] | (conf_descr[index + 8] << 8); |
sayzyas | 43:78f328f311dc | 980 | break; |
sayzyas | 43:78f328f311dc | 981 | default: |
sayzyas | 43:78f328f311dc | 982 | break; |
sayzyas | 43:78f328f311dc | 983 | } |
sayzyas | 43:78f328f311dc | 984 | index += len_desc; |
sayzyas | 43:78f328f311dc | 985 | } |
sayzyas | 43:78f328f311dc | 986 | } |
sayzyas | 43:78f328f311dc | 987 | |
sayzyas | 43:78f328f311dc | 988 | |
sayzyas | 43:78f328f311dc | 989 | USB_TYPE USBHost::bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) |
sayzyas | 43:78f328f311dc | 990 | { |
sayzyas | 43:78f328f311dc | 991 | return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, true); |
sayzyas | 43:78f328f311dc | 992 | } |
sayzyas | 43:78f328f311dc | 993 | |
sayzyas | 43:78f328f311dc | 994 | USB_TYPE USBHost::bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) |
sayzyas | 43:78f328f311dc | 995 | { |
sayzyas | 43:78f328f311dc | 996 | return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, false); |
sayzyas | 43:78f328f311dc | 997 | } |
sayzyas | 43:78f328f311dc | 998 | |
sayzyas | 43:78f328f311dc | 999 | USB_TYPE USBHost::interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) |
sayzyas | 43:78f328f311dc | 1000 | { |
sayzyas | 43:78f328f311dc | 1001 | return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, true); |
sayzyas | 43:78f328f311dc | 1002 | } |
sayzyas | 43:78f328f311dc | 1003 | |
sayzyas | 43:78f328f311dc | 1004 | USB_TYPE USBHost::interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) |
sayzyas | 43:78f328f311dc | 1005 | { |
sayzyas | 43:78f328f311dc | 1006 | return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, false); |
sayzyas | 43:78f328f311dc | 1007 | } |
sayzyas | 43:78f328f311dc | 1008 | |
sayzyas | 43:78f328f311dc | 1009 | USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write) { |
sayzyas | 43:78f328f311dc | 1010 | |
sayzyas | 43:78f328f311dc | 1011 | #if DEBUG_TRANSFER |
sayzyas | 43:78f328f311dc | 1012 | const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : "ISOCHRONOUS"); |
sayzyas | 43:78f328f311dc | 1013 | USB_DBG_TRANSFER("----- %s %s [dev: %p - %s - hub: %d - port: %d - addr: %d - ep: %02X]------", type_str, (write) ? "WRITE" : "READ", dev, dev->getName(ep->getIntfNb()), dev->getHub(), dev->getPort(), dev->getAddress(), ep->getAddress()); |
sayzyas | 43:78f328f311dc | 1014 | #endif |
sayzyas | 43:78f328f311dc | 1015 | |
sayzyas | 43:78f328f311dc | 1016 | Lock lock(this); |
sayzyas | 43:78f328f311dc | 1017 | |
sayzyas | 43:78f328f311dc | 1018 | USB_TYPE res; |
sayzyas | 43:78f328f311dc | 1019 | ENDPOINT_DIRECTION dir = (write) ? OUT : IN; |
sayzyas | 43:78f328f311dc | 1020 | |
sayzyas | 43:78f328f311dc | 1021 | if (dev == NULL) { |
sayzyas | 43:78f328f311dc | 1022 | USB_ERR("dev NULL"); |
sayzyas | 43:78f328f311dc | 1023 | return USB_TYPE_ERROR; |
sayzyas | 43:78f328f311dc | 1024 | } |
sayzyas | 43:78f328f311dc | 1025 | |
sayzyas | 43:78f328f311dc | 1026 | if (ep == NULL) { |
sayzyas | 43:78f328f311dc | 1027 | USB_ERR("ep NULL"); |
sayzyas | 43:78f328f311dc | 1028 | return USB_TYPE_ERROR; |
sayzyas | 43:78f328f311dc | 1029 | } |
sayzyas | 43:78f328f311dc | 1030 | |
sayzyas | 43:78f328f311dc | 1031 | if (ep->getState() != USB_TYPE_IDLE) { |
sayzyas | 43:78f328f311dc | 1032 | USB_WARN("[ep: %p - dev: %p - %s] NOT IDLE: %s", ep, ep->dev, ep->dev->getName(ep->getIntfNb()), ep->getStateString()); |
sayzyas | 43:78f328f311dc | 1033 | return ep->getState(); |
sayzyas | 43:78f328f311dc | 1034 | } |
sayzyas | 43:78f328f311dc | 1035 | |
sayzyas | 43:78f328f311dc | 1036 | if ((ep->getDir() != dir) || (ep->getType() != type)) { |
sayzyas | 43:78f328f311dc | 1037 | USB_ERR("[ep: %p - dev: %p] wrong dir or bad USBEndpoint type", ep, ep->dev); |
sayzyas | 43:78f328f311dc | 1038 | return USB_TYPE_ERROR; |
sayzyas | 43:78f328f311dc | 1039 | } |
sayzyas | 43:78f328f311dc | 1040 | |
sayzyas | 43:78f328f311dc | 1041 | if (dev->getAddress() != ep->getDeviceAddress()) { |
sayzyas | 43:78f328f311dc | 1042 | USB_ERR("[ep: %p - dev: %p] USBEndpoint addr and device addr don't match", ep, ep->dev); |
sayzyas | 43:78f328f311dc | 1043 | return USB_TYPE_ERROR; |
sayzyas | 43:78f328f311dc | 1044 | } |
sayzyas | 43:78f328f311dc | 1045 | |
sayzyas | 43:78f328f311dc | 1046 | #if DEBUG_TRANSFER |
sayzyas | 43:78f328f311dc | 1047 | if (write) { |
sayzyas | 43:78f328f311dc | 1048 | USB_DBG_TRANSFER("%s WRITE buffer", type_str); |
sayzyas | 43:78f328f311dc | 1049 | for (int i = 0; i < ep->getLengthTransferred(); i++) |
sayzyas | 43:78f328f311dc | 1050 | printf("%02X ", buf[i]); |
sayzyas | 43:78f328f311dc | 1051 | printf("\r\n\r\n"); |
sayzyas | 43:78f328f311dc | 1052 | } |
sayzyas | 43:78f328f311dc | 1053 | #endif |
sayzyas | 43:78f328f311dc | 1054 | addTransfer(ep, buf, len); |
sayzyas | 43:78f328f311dc | 1055 | |
sayzyas | 43:78f328f311dc | 1056 | if (blocking) { |
sayzyas | 43:78f328f311dc | 1057 | |
sayzyas | 43:78f328f311dc | 1058 | ep->ep_queue.get(); |
sayzyas | 43:78f328f311dc | 1059 | res = ep->getState(); |
sayzyas | 43:78f328f311dc | 1060 | |
sayzyas | 43:78f328f311dc | 1061 | USB_DBG_TRANSFER("%s TRANSFER res: %s on ep: %p\r\n", type_str, ep->getStateString(), ep); |
sayzyas | 43:78f328f311dc | 1062 | |
sayzyas | 43:78f328f311dc | 1063 | if (res != USB_TYPE_IDLE) { |
sayzyas | 43:78f328f311dc | 1064 | return res; |
sayzyas | 43:78f328f311dc | 1065 | } |
sayzyas | 43:78f328f311dc | 1066 | |
sayzyas | 43:78f328f311dc | 1067 | return USB_TYPE_OK; |
sayzyas | 43:78f328f311dc | 1068 | } |
sayzyas | 43:78f328f311dc | 1069 | |
sayzyas | 43:78f328f311dc | 1070 | return USB_TYPE_PROCESSING; |
sayzyas | 43:78f328f311dc | 1071 | |
sayzyas | 43:78f328f311dc | 1072 | } |
sayzyas | 43:78f328f311dc | 1073 | |
sayzyas | 43:78f328f311dc | 1074 | |
sayzyas | 43:78f328f311dc | 1075 | USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { |
sayzyas | 43:78f328f311dc | 1076 | return controlTransfer(dev, requestType, request, value, index, buf, len, false); |
sayzyas | 43:78f328f311dc | 1077 | } |
sayzyas | 43:78f328f311dc | 1078 | |
sayzyas | 43:78f328f311dc | 1079 | USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { |
sayzyas | 43:78f328f311dc | 1080 | return controlTransfer(dev, requestType, request, value, index, buf, len, true); |
sayzyas | 43:78f328f311dc | 1081 | } |
sayzyas | 43:78f328f311dc | 1082 | |
sayzyas | 43:78f328f311dc | 1083 | USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len, bool write) |
sayzyas | 43:78f328f311dc | 1084 | { |
sayzyas | 43:78f328f311dc | 1085 | Lock lock(this); |
sayzyas | 43:78f328f311dc | 1086 | USB_DBG_TRANSFER("----- CONTROL %s [dev: %p - hub: %d - port: %d] ------", (write) ? "WRITE" : "READ", dev, dev->getHub(), dev->getPort()); |
sayzyas | 43:78f328f311dc | 1087 | |
sayzyas | 43:78f328f311dc | 1088 | int length_transfer = len; |
sayzyas | 43:78f328f311dc | 1089 | USB_TYPE res; |
sayzyas | 43:78f328f311dc | 1090 | uint32_t token; |
sayzyas | 43:78f328f311dc | 1091 | |
sayzyas | 43:78f328f311dc | 1092 | control->setSpeed(dev->getSpeed()); |
sayzyas | 43:78f328f311dc | 1093 | control->setSize(dev->getSizeControlEndpoint()); |
sayzyas | 43:78f328f311dc | 1094 | if (dev->isActiveAddress()) { |
sayzyas | 43:78f328f311dc | 1095 | control->setDeviceAddress(dev->getAddress()); |
sayzyas | 43:78f328f311dc | 1096 | } else { |
sayzyas | 43:78f328f311dc | 1097 | control->setDeviceAddress(0); |
sayzyas | 43:78f328f311dc | 1098 | } |
sayzyas | 43:78f328f311dc | 1099 | |
sayzyas | 43:78f328f311dc | 1100 | USB_DBG_TRANSFER("Control transfer on device: %d\r\n", control->getDeviceAddress()); |
sayzyas | 43:78f328f311dc | 1101 | fillControlBuf(requestType, request, value, index, len); |
sayzyas | 43:78f328f311dc | 1102 | |
sayzyas | 43:78f328f311dc | 1103 | #if DEBUG_TRANSFER |
sayzyas | 43:78f328f311dc | 1104 | USB_DBG_TRANSFER("SETUP PACKET: "); |
sayzyas | 43:78f328f311dc | 1105 | for (int i = 0; i < 8; i++) |
sayzyas | 43:78f328f311dc | 1106 | printf("%01X ", setupPacket[i]); |
sayzyas | 43:78f328f311dc | 1107 | printf("\r\n"); |
sayzyas | 43:78f328f311dc | 1108 | #endif |
sayzyas | 43:78f328f311dc | 1109 | |
sayzyas | 43:78f328f311dc | 1110 | control->setNextToken(TD_SETUP); |
sayzyas | 43:78f328f311dc | 1111 | addTransfer(control, (uint8_t*)setupPacket, 8); |
sayzyas | 43:78f328f311dc | 1112 | |
sayzyas | 43:78f328f311dc | 1113 | control->ep_queue.get(); |
sayzyas | 43:78f328f311dc | 1114 | res = control->getState(); |
sayzyas | 43:78f328f311dc | 1115 | |
sayzyas | 43:78f328f311dc | 1116 | USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString()); |
sayzyas | 43:78f328f311dc | 1117 | |
sayzyas | 43:78f328f311dc | 1118 | if (res != USB_TYPE_IDLE) { |
sayzyas | 43:78f328f311dc | 1119 | return res; |
sayzyas | 43:78f328f311dc | 1120 | } |
sayzyas | 43:78f328f311dc | 1121 | |
sayzyas | 43:78f328f311dc | 1122 | if (length_transfer) { |
sayzyas | 43:78f328f311dc | 1123 | token = (write) ? TD_OUT : TD_IN; |
sayzyas | 43:78f328f311dc | 1124 | control->setNextToken(token); |
sayzyas | 43:78f328f311dc | 1125 | addTransfer(control, (uint8_t *)buf, length_transfer); |
sayzyas | 43:78f328f311dc | 1126 | |
sayzyas | 43:78f328f311dc | 1127 | control->ep_queue.get(); |
sayzyas | 43:78f328f311dc | 1128 | res = control->getState(); |
sayzyas | 43:78f328f311dc | 1129 | |
sayzyas | 43:78f328f311dc | 1130 | #if DEBUG_TRANSFER |
sayzyas | 43:78f328f311dc | 1131 | USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString()); |
sayzyas | 43:78f328f311dc | 1132 | if (write) { |
sayzyas | 43:78f328f311dc | 1133 | USB_DBG_TRANSFER("CONTROL WRITE buffer"); |
sayzyas | 43:78f328f311dc | 1134 | for (int i = 0; i < control->getLengthTransferred(); i++) |
sayzyas | 43:78f328f311dc | 1135 | printf("%02X ", buf[i]); |
sayzyas | 43:78f328f311dc | 1136 | printf("\r\n\r\n"); |
sayzyas | 43:78f328f311dc | 1137 | } else { |
sayzyas | 43:78f328f311dc | 1138 | USB_DBG_TRANSFER("CONTROL READ SUCCESS [%d bytes transferred]", control->getLengthTransferred()); |
sayzyas | 43:78f328f311dc | 1139 | for (int i = 0; i < control->getLengthTransferred(); i++) |
sayzyas | 43:78f328f311dc | 1140 | printf("%02X ", buf[i]); |
sayzyas | 43:78f328f311dc | 1141 | printf("\r\n\r\n"); |
sayzyas | 43:78f328f311dc | 1142 | } |
sayzyas | 43:78f328f311dc | 1143 | #endif |
sayzyas | 43:78f328f311dc | 1144 | |
sayzyas | 43:78f328f311dc | 1145 | if (res != USB_TYPE_IDLE) { |
sayzyas | 43:78f328f311dc | 1146 | return res; |
sayzyas | 43:78f328f311dc | 1147 | } |
sayzyas | 43:78f328f311dc | 1148 | } |
sayzyas | 43:78f328f311dc | 1149 | |
sayzyas | 43:78f328f311dc | 1150 | token = (write) ? TD_IN : TD_OUT; |
sayzyas | 43:78f328f311dc | 1151 | control->setNextToken(token); |
sayzyas | 43:78f328f311dc | 1152 | addTransfer(control, NULL, 0); |
sayzyas | 43:78f328f311dc | 1153 | |
sayzyas | 43:78f328f311dc | 1154 | control->ep_queue.get(); |
sayzyas | 43:78f328f311dc | 1155 | res = control->getState(); |
sayzyas | 43:78f328f311dc | 1156 | |
sayzyas | 43:78f328f311dc | 1157 | USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString()); |
sayzyas | 43:78f328f311dc | 1158 | |
sayzyas | 43:78f328f311dc | 1159 | if (res != USB_TYPE_IDLE) |
sayzyas | 43:78f328f311dc | 1160 | return res; |
sayzyas | 43:78f328f311dc | 1161 | |
sayzyas | 43:78f328f311dc | 1162 | return USB_TYPE_OK; |
sayzyas | 43:78f328f311dc | 1163 | } |
sayzyas | 43:78f328f311dc | 1164 | |
sayzyas | 43:78f328f311dc | 1165 | |
sayzyas | 43:78f328f311dc | 1166 | void USBHost::fillControlBuf(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, int len) |
sayzyas | 43:78f328f311dc | 1167 | { |
sayzyas | 43:78f328f311dc | 1168 | setupPacket[0] = requestType; |
sayzyas | 43:78f328f311dc | 1169 | setupPacket[1] = request; |
sayzyas | 43:78f328f311dc | 1170 | setupPacket[2] = (uint8_t) value; |
sayzyas | 43:78f328f311dc | 1171 | setupPacket[3] = (uint8_t) (value >> 8); |
sayzyas | 43:78f328f311dc | 1172 | setupPacket[4] = (uint8_t) index; |
sayzyas | 43:78f328f311dc | 1173 | setupPacket[5] = (uint8_t) (index >> 8); |
sayzyas | 43:78f328f311dc | 1174 | setupPacket[6] = (uint8_t) len; |
sayzyas | 43:78f328f311dc | 1175 | setupPacket[7] = (uint8_t) (len >> 8); |
sayzyas | 43:78f328f311dc | 1176 | } |