USBHost : fix indentation USBHost.cpp

pull/4231/head
Michel Jaouen 2017-04-26 15:58:10 +02:00
parent 27877bd4e8
commit 41f12260b1
1 changed files with 232 additions and 194 deletions

View File

@ -46,7 +46,8 @@ USBHost * USBHost::instHost = NULL;
* - when the usb_thread receives the event, it: * - when the usb_thread receives the event, it:
* - call the callback attached to the endpoint where the td is attached * - call the callback attached to the endpoint where the td is attached
*/ */
void USBHost::usb_process() { void USBHost::usb_process()
{
bool controlListState; bool controlListState;
bool bulkListState; bool bulkListState;
@ -79,110 +80,112 @@ void USBHost::usb_process() {
too_many_hub = false; too_many_hub = false;
buf[4] = 0; buf[4] = 0;
do do {
{ Lock lock(this);
Lock lock(this);
for (i = 0; i < MAX_DEVICE_CONNECTED; i++) { for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if (!deviceInUse[i]) { if (!deviceInUse[i]) {
USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]); USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]);
devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed); devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed);
deviceReset[i] = false; deviceReset[i] = false;
deviceInited[i] = true; deviceInited[i] = true;
break; break;
} }
} }
if (i == MAX_DEVICE_CONNECTED) { if (i == MAX_DEVICE_CONNECTED) {
USB_ERR("Too many device connected!!\r\n"); USB_ERR("Too many device connected!!\r\n");
continue; continue;
} }
if (!controlEndpointAllocated) { if (!controlEndpointAllocated) {
control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00); control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00);
addEndpoint(NULL, 0, (USBEndpoint*)control); addEndpoint(NULL, 0, (USBEndpoint*)control);
controlEndpointAllocated = true; controlEndpointAllocated = true;
} }
#if MAX_HUB_NB #if MAX_HUB_NB
if (usb_msg->hub_parent) if (usb_msg->hub_parent) {
devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent)); devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent));
#endif }
#endif
for (j = 0; j < timeout_set_addr; j++) { for (j = 0; j < timeout_set_addr; j++) {
resetDevice(&devices[i]); resetDevice(&devices[i]);
// set size of control endpoint // set size of control endpoint
devices[i].setSizeControlEndpoint(8); devices[i].setSizeControlEndpoint(8);
devices[i].activeAddress(false); devices[i].activeAddress(false);
// get first 8 bit of device descriptor // get first 8 bit of device descriptor
// and check if we deal with a hub // and check if we deal with a hub
USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]); USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]);
res = getDeviceDescriptor(&devices[i], buf, 8); res = getDeviceDescriptor(&devices[i], buf, 8);
if (res != USB_TYPE_OK) { if (res != USB_TYPE_OK) {
USB_ERR("usb_thread could not read dev descr"); USB_ERR("usb_thread could not read dev descr");
continue; continue;
} }
// set size of control endpoint // set size of control endpoint
devices[i].setSizeControlEndpoint(buf[7]); devices[i].setSizeControlEndpoint(buf[7]);
// second step: set an address to the device // second step: set an address to the device
res = setAddress(&devices[i], devices[i].getAddress()); res = setAddress(&devices[i], devices[i].getAddress());
if (res != USB_TYPE_OK) { if (res != USB_TYPE_OK) {
USB_ERR("SET ADDR FAILED"); USB_ERR("SET ADDR FAILED");
continue; continue;
} }
devices[i].activeAddress(true); devices[i].activeAddress(true);
USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress()); USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress());
// try to read again the device descriptor to check if the device // try to read again the device descriptor to check if the device
// answers to its new address // answers to its new address
res = getDeviceDescriptor(&devices[i], buf, 8); res = getDeviceDescriptor(&devices[i], buf, 8);
if (res == USB_TYPE_OK) { if (res == USB_TYPE_OK) {
break; break;
} }
Thread::wait(100); Thread::wait(100);
} }
USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port); USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port);
#if MAX_HUB_NB #if MAX_HUB_NB
if (buf[4] == HUB_CLASS) { if (buf[4] == HUB_CLASS) {
for (k = 0; k < MAX_HUB_NB; k++) { for (k = 0; k < MAX_HUB_NB; k++) {
if (hub_in_use[k] == false) { if (hub_in_use[k] == false) {
for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) { for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) {
if (hubs[k].connect(&devices[i])) { if (hubs[k].connect(&devices[i])) {
devices[i].hub = &hubs[k]; devices[i].hub = &hubs[k];
hub_in_use[k] = true; hub_in_use[k] = true;
break; break;
} }
} }
if (hub_in_use[k] == true) if (hub_in_use[k] == true) {
break; break;
} }
} }
}
if (k == MAX_HUB_NB) { if (k == MAX_HUB_NB) {
USB_ERR("Too many hubs connected!!\r\n"); USB_ERR("Too many hubs connected!!\r\n");
too_many_hub = true; too_many_hub = true;
} }
} }
if (usb_msg->hub_parent) if (usb_msg->hub_parent) {
((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]); ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]);
#endif }
#endif
if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) { if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) {
deviceInUse[i] = true; deviceInUse[i] = true;
} }
} while(0); } while(0);
@ -191,22 +194,27 @@ void USBHost::usb_process() {
// a device has been disconnected // a device has been disconnected
case DEVICE_DISCONNECTED_EVENT: case DEVICE_DISCONNECTED_EVENT:
do do {
{ Lock lock(this);
Lock lock(this);
controlListState = disableList(CONTROL_ENDPOINT); controlListState = disableList(CONTROL_ENDPOINT);
bulkListState = disableList(BULK_ENDPOINT); bulkListState = disableList(BULK_ENDPOINT);
interruptListState = disableList(INTERRUPT_ENDPOINT); interruptListState = disableList(INTERRUPT_ENDPOINT);
idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent)); idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent));
if (idx != -1) { if (idx != -1) {
freeDevice((USBDeviceConnected*)&devices[idx]); freeDevice((USBDeviceConnected*)&devices[idx]);
} }
if (controlListState) enableList(CONTROL_ENDPOINT); if (controlListState) {
if (bulkListState) enableList(BULK_ENDPOINT); enableList(CONTROL_ENDPOINT);
if (interruptListState) enableList(INTERRUPT_ENDPOINT); }
if (bulkListState) {
enableList(BULK_ENDPOINT);
}
if (interruptListState) {
enableList(INTERRUPT_ENDPOINT);
}
} while(0); } while(0);
@ -224,8 +232,9 @@ void USBHost::usb_process() {
if (ep->getDir() == IN) { if (ep->getDir() == IN) {
buf_transfer = ep->getBufStart(); buf_transfer = ep->getBufStart();
printf("READ SUCCESS [%d bytes transferred - td: 0x%08X] on ep: [%p - addr: %02X]: ", ep->getLengthTransferred(), usb_msg->td_addr, ep, ep->getAddress()); printf("READ SUCCESS [%d bytes transferred - td: 0x%08X] on ep: [%p - addr: %02X]: ", ep->getLengthTransferred(), usb_msg->td_addr, ep, ep->getAddress());
for (int i = 0; i < ep->getLengthTransferred(); i++) for (int i = 0; i < ep->getLengthTransferred(); i++) {
printf("%02X ", buf_transfer[i]); printf("%02X ", buf_transfer[i]);
}
printf("\r\n\r\n"); printf("\r\n\r\n");
} }
#endif #endif
@ -266,8 +275,9 @@ USBHost::USBHost() : usbThread(osPriorityNormal, USB_THREAD_STACK)
devices[i].setAddress(i + 1); devices[i].setAddress(i + 1);
deviceReset[i] = false; deviceReset[i] = false;
deviceInited[i] = false; deviceInited[i] = false;
for (uint8_t j = 0; j < MAX_INTF; j++) for (uint8_t j = 0; j < MAX_INTF; j++) {
deviceAttachedDriver[i][j] = false; deviceAttachedDriver[i][j] = false;
}
} }
#if MAX_HUB_NB #if MAX_HUB_NB
@ -282,20 +292,21 @@ USBHost::USBHost() : usbThread(osPriorityNormal, USB_THREAD_STACK)
USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost) USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost)
{ {
m_pHost->usb_mutex.lock(); m_pHost->usb_mutex.lock();
} }
USBHost::Lock::~Lock() USBHost::Lock::~Lock()
{ {
m_pHost->usb_mutex.unlock(); m_pHost->usb_mutex.unlock();
} }
void USBHost::transferCompleted(volatile uint32_t addr) void USBHost::transferCompleted(volatile uint32_t addr)
{ {
uint8_t state; uint8_t state;
if(addr == 0) if(addr == 0) {
return; return;
}
volatile HCTD* tdList = NULL; volatile HCTD* tdList = NULL;
@ -315,20 +326,23 @@ void USBHost::transferCompleted(volatile uint32_t addr)
#ifdef USBHOST_OTHER #ifdef USBHOST_OTHER
state = ((HCTD *)td)->state; state = ((HCTD *)td)->state;
if (state == USB_TYPE_IDLE) if (state == USB_TYPE_IDLE) {
ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart());
}
#else #else
if (((HCTD *)td)->control >> 28) { if (((HCTD *)td)->control >> 28) {
state = ((HCTD *)td)->control >> 28; state = ((HCTD *)td)->control >> 28;
} else { } else {
if (td->currBufPtr) if (td->currBufPtr) {
ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart());
}
state = 16 /*USB_TYPE_IDLE*/; state = 16 /*USB_TYPE_IDLE*/;
} }
#endif #endif
if (state == USB_TYPE_IDLE) if (state == USB_TYPE_IDLE) {
ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart());
}
ep->unqueueTransfer(td); ep->unqueueTransfer(td);
@ -365,8 +379,9 @@ USBHost * USBHost::getHostInst()
// be sure that the new device connected is not already connected... // be sure that the new device connected is not already connected...
int idx = findDevice(hub, port, hub_parent); int idx = findDevice(hub, port, hub_parent);
if (idx != -1) { if (idx != -1) {
if (deviceInited[idx]) if (deviceInited[idx]) {
return; return;
}
} }
message_t * usb_msg = mail_usb_event.alloc(); message_t * usb_msg = mail_usb_event.alloc();
@ -387,8 +402,9 @@ USBHost * USBHost::getHostInst()
// be sure that the device disconnected is connected... // be sure that the device disconnected is connected...
int idx = findDevice(hub, port, hub_parent); int idx = findDevice(hub, port, hub_parent);
if (idx != -1) { if (idx != -1) {
if (!deviceInUse[idx]) if (!deviceInUse[idx]) {
return; return;
}
} else { } else {
return; return;
} }
@ -422,8 +438,9 @@ void USBHost::freeDevice(USBDeviceConnected * dev)
} }
// notify hub parent that this device has been disconnected // notify hub parent that this device has been disconnected
if (dev->getHubParent()) if (dev->getHubParent()) {
dev->getHubParent()->deviceDisconnected(dev); dev->getHubParent()->deviceDisconnected(dev);
}
#endif #endif
@ -667,8 +684,9 @@ int USBHost::findDevice(uint8_t hub, uint8_t port, USBHostHub * hub_parent)
for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) { for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if (devices[i].getHub() == hub && devices[i].getPort() == port) { if (devices[i].getHub() == hub && devices[i].getPort() == port) {
if (hub_parent != NULL) { if (hub_parent != NULL) {
if (hub_parent == devices[i].getHubParent()) if (hub_parent == devices[i].getHubParent()) {
return i; return i;
}
} else { } else {
return i; return i;
} }
@ -695,14 +713,14 @@ void USBHost::printList(ENDPOINT_TYPE type)
volatile HCTD * hctd = NULL; volatile HCTD * hctd = NULL;
const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : const char * type_str = (type == BULK_ENDPOINT) ? "BULK" :
((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" :
((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS")); ((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS"));
printf("State of %s:\r\n", type_str); printf("State of %s:\r\n", type_str);
while (hced != NULL) { while (hced != NULL) {
uint8_t dir = ((hced->control & (3 << 11)) >> 11); uint8_t dir = ((hced->control & (3 << 11)) >> 11);
printf("hced: %p [ADDR: %d, DIR: %s, EP_NB: 0x%X]\r\n", hced, printf("hced: %p [ADDR: %d, DIR: %s, EP_NB: 0x%X]\r\n", hced,
hced->control & 0x7f, hced->control & 0x7f,
(dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"), (dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"),
(hced->control & (0xf << 7)) >> 7); (hced->control & (0xf << 7)) >> 7);
hctd = (HCTD *)((uint32_t)(hced->headTD) & ~(0xf)); hctd = (HCTD *)((uint32_t)(hced->headTD) & ~(0xf));
while (hctd != hced->tailTD) { while (hctd != hced->tailTD) {
printf("\thctd: %p [DIR: %s]\r\n", hctd, ((hctd->control & (3 << 19)) >> 19) == 1 ? "OUT" : "IN"); printf("\thctd: %p [DIR: %s]\r\n", hctd, ((hctd->control & (3 << 19)) >> 19) == 1 ? "OUT" : "IN");
@ -720,7 +738,7 @@ void USBHost::printList(ENDPOINT_TYPE type)
USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len) USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
{ {
USB_TYPE ret=USB_TYPE_PROCESSING; USB_TYPE ret=USB_TYPE_PROCESSING;
td_mutex.lock(); td_mutex.lock();
// allocate a TD which will be freed in TDcompletion // allocate a TD which will be freed in TDcompletion
volatile HCTD * td = ed->getNextTD(); volatile HCTD * td = ed->getNextTD();
@ -728,7 +746,7 @@ USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
return USB_TYPE_ERROR; return USB_TYPE_ERROR;
} }
#ifndef USBHOST_OTHER #ifndef USBHOST_OTHER
uint32_t token = (ed->isSetup() ? TD_SETUP : ( (ed->getDir() == IN) ? TD_IN : TD_OUT )); uint32_t token = (ed->isSetup() ? TD_SETUP : ( (ed->getDir() == IN) ? TD_IN : TD_OUT ));
uint32_t td_toggle; uint32_t td_toggle;
@ -754,10 +772,10 @@ USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
printList(type); printList(type);
enableList(type); enableList(type);
#else #else
/* call method specific for endpoint */ /* call method specific for endpoint */
td->currBufPtr = buf; td->currBufPtr = buf;
td->size = len; td->size = len;
ret = ed->queueTransfer(); ret = ed->queueTransfer();
#endif #endif
td_mutex.unlock(); td_mutex.unlock();
@ -770,12 +788,13 @@ USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
USB_TYPE USBHost::getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr) USB_TYPE USBHost::getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr)
{ {
USB_TYPE t = controlRead( dev, USB_TYPE t = controlRead( dev,
USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
GET_DESCRIPTOR, GET_DESCRIPTOR,
(DEVICE_DESCRIPTOR << 8) | (0), (DEVICE_DESCRIPTOR << 8) | (0),
0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf)); 0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf));
if (len_dev_descr) if (len_dev_descr) {
*len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf); *len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf);
}
return t; return t;
} }
@ -799,8 +818,9 @@ USB_TYPE USBHost::getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t *
total_conf_descr_length = buf[2] | (buf[3] << 8); total_conf_descr_length = buf[2] | (buf[3] << 8);
total_conf_descr_length = MIN(max_len_buf, total_conf_descr_length); total_conf_descr_length = MIN(max_len_buf, total_conf_descr_length);
if (len_conf_descr) if (len_conf_descr) {
*len_conf_descr = total_conf_descr_length; *len_conf_descr = total_conf_descr_length;
}
USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]); USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]);
@ -812,7 +832,8 @@ USB_TYPE USBHost::getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t *
} }
USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address) { USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address)
{
return controlWrite( dev, return controlWrite( dev,
USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE,
SET_ADDRESS, SET_ADDRESS,
@ -830,14 +851,17 @@ USB_TYPE USBHost::setConfiguration(USBDeviceConnected * dev, uint8_t conf)
0, NULL, 0); 0, NULL, 0);
} }
uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev) { uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev)
{
int index = findDevice(dev); int index = findDevice(dev);
uint8_t cnt = 0; uint8_t cnt = 0;
if (index == -1) if (index == -1) {
return 0; return 0;
}
for (uint8_t i = 0; i < MAX_INTF; i++) { for (uint8_t i = 0; i < MAX_INTF; i++) {
if (deviceAttachedDriver[index][i]) if (deviceAttachedDriver[index][i]) {
cnt++; cnt++;
}
} }
return cnt; return cnt;
} }
@ -848,76 +872,76 @@ USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerato
uint16_t total_conf_descr_length = 0; uint16_t total_conf_descr_length = 0;
USB_TYPE res; USB_TYPE res;
do do {
{ Lock lock(this);
Lock lock(this);
// don't enumerate a device which all interfaces are registered to a specific driver // don't enumerate a device which all interfaces are registered to a specific driver
int index = findDevice(dev); int index = findDevice(dev);
if (index == -1) { if (index == -1) {
return USB_TYPE_ERROR; return USB_TYPE_ERROR;
} }
uint8_t nb_intf_attached = numberDriverAttached(dev); uint8_t nb_intf_attached = numberDriverAttached(dev);
USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf()); USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf());
USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached); USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached);
if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) { if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) {
USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev); USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev);
return USB_TYPE_OK; return USB_TYPE_OK;
} }
USB_DBG("Enumerate dev: %p", dev); USB_DBG("Enumerate dev: %p", dev);
// third step: get the whole device descriptor to see vid, pid // third step: get the whole device descriptor to see vid, pid
res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH); res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH);
if (res != USB_TYPE_OK) { if (res != USB_TYPE_OK) {
USB_DBG("GET DEV DESCR FAILED"); USB_DBG("GET DEV DESCR FAILED");
return res; return res;
} }
dev->setClass(data[4]); dev->setClass(data[4]);
dev->setSubClass(data[5]); dev->setSubClass(data[5]);
dev->setProtocol(data[6]); dev->setProtocol(data[6]);
dev->setVid(data[8] | (data[9] << 8)); dev->setVid(data[8] | (data[9] << 8));
dev->setPid(data[10] | (data[11] << 8)); dev->setPid(data[10] | (data[11] << 8));
USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8)); USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8));
pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) ); pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) );
res = getConfigurationDescriptor(dev, data, sizeof(data), &total_conf_descr_length); res = getConfigurationDescriptor(dev, data, sizeof(data), &total_conf_descr_length);
if (res != USB_TYPE_OK) { if (res != USB_TYPE_OK) {
return res; return res;
} }
#if (DEBUG > 3) #if (DEBUG > 3)
USB_DBG("CONFIGURATION DESCRIPTOR:\r\n"); USB_DBG("CONFIGURATION DESCRIPTOR:\r\n");
for (int i = 0; i < total_conf_descr_length; i++) for (int i = 0; i < total_conf_descr_length; i++) {
printf("%02X ", data[i]); printf("%02X ", data[i]);
printf("\r\n\r\n"); }
#endif printf("\r\n\r\n");
#endif
// Parse the configuration descriptor // Parse the configuration descriptor
parseConfDescr(dev, data, total_conf_descr_length, pEnumerator); parseConfDescr(dev, data, total_conf_descr_length, pEnumerator);
// only set configuration if not enumerated before // only set configuration if not enumerated before
if (!dev->isEnumerated()) { if (!dev->isEnumerated()) {
USB_DBG("Set configuration 1 on dev: %p", dev); USB_DBG("Set configuration 1 on dev: %p", dev);
// sixth step: set configuration (only 1 supported) // sixth step: set configuration (only 1 supported)
res = setConfiguration(dev, 1); res = setConfiguration(dev, 1);
if (res != USB_TYPE_OK) { if (res != USB_TYPE_OK) {
USB_DBG("SET CONF FAILED"); USB_DBG("SET CONF FAILED");
return res; return res;
} }
} }
dev->setEnumerated(); dev->setEnumerated();
// Now the device is enumerated! // Now the device is enumerated!
USB_DBG("dev %p is enumerated\r\n", dev); USB_DBG("dev %p is enumerated\r\n", dev);
} while(0); } while(0);
@ -1016,7 +1040,8 @@ USB_TYPE USBHost::interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint
return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, false); return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, false);
} }
USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write) { USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write)
{
#if DEBUG_TRANSFER #if DEBUG_TRANSFER
const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : "ISOCHRONOUS"); const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : "ISOCHRONOUS");
@ -1056,8 +1081,9 @@ USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, ui
#if DEBUG_TRANSFER #if DEBUG_TRANSFER
if (write) { if (write) {
USB_DBG_TRANSFER("%s WRITE buffer", type_str); USB_DBG_TRANSFER("%s WRITE buffer", type_str);
for (int i = 0; i < ep->getLengthTransferred(); i++) for (int i = 0; i < ep->getLengthTransferred(); i++) {
printf("%02X ", buf[i]); printf("%02X ", buf[i]);
}
printf("\r\n\r\n"); printf("\r\n\r\n");
} }
#endif #endif
@ -1082,11 +1108,13 @@ USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, ui
} }
USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len)
{
return controlTransfer(dev, requestType, request, value, index, buf, len, false); return controlTransfer(dev, requestType, request, value, index, buf, len, false);
} }
USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len)
{
return controlTransfer(dev, requestType, request, value, index, buf, len, true); return controlTransfer(dev, requestType, request, value, index, buf, len, true);
} }
@ -1112,15 +1140,18 @@ USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType,
#if DEBUG_TRANSFER #if DEBUG_TRANSFER
USB_DBG_TRANSFER("SETUP PACKET: "); USB_DBG_TRANSFER("SETUP PACKET: ");
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++) {
printf("%01X ", setupPacket[i]); printf("%01X ", setupPacket[i]);
}
printf("\r\n"); printf("\r\n");
#endif #endif
control->setNextToken(TD_SETUP); control->setNextToken(TD_SETUP);
res = addTransfer(control, (uint8_t*)setupPacket, 8); res = addTransfer(control, (uint8_t*)setupPacket, 8);
if (res == USB_TYPE_PROCESSING) control->ep_queue.get(); if (res == USB_TYPE_PROCESSING) {
control->ep_queue.get();
}
res = control->getState(); res = control->getState();
USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString()); USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString());
@ -1134,20 +1165,24 @@ USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType,
control->setNextToken(token); control->setNextToken(token);
res = addTransfer(control, (uint8_t *)buf, length_transfer); res = addTransfer(control, (uint8_t *)buf, length_transfer);
if (res == USB_TYPE_PROCESSING) control->ep_queue.get(); if (res == USB_TYPE_PROCESSING) {
control->ep_queue.get();
}
res = control->getState(); res = control->getState();
#if DEBUG_TRANSFER #if DEBUG_TRANSFER
USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString()); USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString());
if (write) { if (write) {
USB_DBG_TRANSFER("CONTROL WRITE buffer"); USB_DBG_TRANSFER("CONTROL WRITE buffer");
for (int i = 0; i < control->getLengthTransferred(); i++) for (int i = 0; i < control->getLengthTransferred(); i++) {
printf("%02X ", buf[i]); printf("%02X ", buf[i]);
}
printf("\r\n\r\n"); printf("\r\n\r\n");
} else { } else {
USB_DBG_TRANSFER("CONTROL READ SUCCESS [%d bytes transferred]", control->getLengthTransferred()); USB_DBG_TRANSFER("CONTROL READ SUCCESS [%d bytes transferred]", control->getLengthTransferred());
for (int i = 0; i < control->getLengthTransferred(); i++) for (int i = 0; i < control->getLengthTransferred(); i++) {
printf("%02X ", buf[i]); printf("%02X ", buf[i]);
}
printf("\r\n\r\n"); printf("\r\n\r\n");
} }
#endif #endif
@ -1161,13 +1196,16 @@ USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType,
control->setNextToken(token); control->setNextToken(token);
res = addTransfer(control, NULL, 0); res = addTransfer(control, NULL, 0);
if (res == USB_TYPE_PROCESSING) control->ep_queue.get(); if (res == USB_TYPE_PROCESSING) {
control->ep_queue.get();
}
res = control->getState(); res = control->getState();
USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString()); USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString());
if (res != USB_TYPE_IDLE) if (res != USB_TYPE_IDLE) {
return res; return res;
}
return USB_TYPE_OK; return USB_TYPE_OK;
} }