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