Adaptation of the official mbed USBHost repository to work with the LPC4088 Display Module

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_USBHost by EmbeddedArtists AB

Committer:
embeddedartists
Date:
Tue Jan 20 09:09:10 2015 +0100
Revision:
30:ffec1f271b7c
Parent:
27:aa2fd412f1d3
Child:
32:f2d129436056
Added notification upon connect/disconnect so that the user knows when it is
time to scan for disconnected devices and/or attempt to connect to new devices.

Who changed what in which revision?

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