1
0
mirror of https://github.com/gdsports/USBHost_t36 synced 2024-11-27 19:42:15 -05:00

Restructure hub driver to stateless approach

This commit is contained in:
PaulStoffregen 2017-02-28 08:39:43 -08:00
parent edb04e60e7
commit d0ee653192
4 changed files with 107 additions and 112 deletions

View File

@ -131,16 +131,15 @@ struct Transfer_struct {
// Linked list of queued, not-yet-completed transfers // Linked list of queued, not-yet-completed transfers
Transfer_t *next_followup; Transfer_t *next_followup;
Transfer_t *prev_followup; Transfer_t *prev_followup;
Pipe_t *pipe;
// Data to be used by callback function. When a group // Data to be used by callback function. When a group
// of Transfer_t are created, these fields and the // of Transfer_t are created, these fields and the
// interrupt-on-complete bit in the qTD token are only // interrupt-on-complete bit in the qTD token are only
// set in the last Transfer_t of the list. // set in the last Transfer_t of the list.
Pipe_t *pipe;
void *buffer; void *buffer;
uint32_t length; uint32_t length;
setup_t *setup; setup_t setup;
USBDriver *driver; USBDriver *driver;
uint32_t unused;
}; };
@ -198,7 +197,7 @@ protected:
static void println(unsigned long n) { Serial.println(n); } static void println(unsigned long n) { Serial.println(n); }
static void println() { Serial.println(); } static void println() { Serial.println(); }
static void print(uint32_t n, uint8_t b) { Serial.print(n, b); } static void print(uint32_t n, uint8_t b) { Serial.print(n, b); }
static void println(uint32_t n, uint8_t b) { Serial.print(n, b); } static void println(uint32_t n, uint8_t b) { Serial.println(n, b); }
static void println(const char *s, int n) { static void println(const char *s, int n) {
Serial.print(s); Serial.println(n); } Serial.print(s); Serial.println(n); }
static void println(const char *s, unsigned int n) { static void println(const char *s, unsigned int n) {
@ -307,34 +306,35 @@ private:
class USBHub : public USBDriver { class USBHub : public USBDriver {
public: public:
USBHub(); USBHub();
enum { MAXPORTS = 7 };
protected: protected:
virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len); virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
virtual void control(const Transfer_t *transfer); virtual void control(const Transfer_t *transfer);
virtual void timer_event(USBDriverTimer *whichTimer); virtual void timer_event(USBDriverTimer *whichTimer);
virtual void disconnect(); virtual void disconnect();
void poweron(uint32_t port); void send_poweron(uint32_t port);
void getstatus(uint32_t port); void send_getstatus(uint32_t port);
void clearstatus(uint32_t port); void send_clearstatus(uint32_t port);
void reset(uint32_t port); void send_reset(uint32_t port);
static void callback(const Transfer_t *transfer); static void callback(const Transfer_t *transfer);
void status_change(const Transfer_t *transfer); void status_change(const Transfer_t *transfer);
void new_port_status(uint32_t port, uint32_t status); void new_port_status(uint32_t port, uint32_t status);
void update_status();
USBDriverTimer mytimer; USBDriverTimer mytimer;
USBDriverTimer othertimer; USBDriverTimer othertimer;
USBDriverTimer mytimers[7]; USBDriverTimer mytimers[MAXPORTS];
setup_t setup; setup_t setup[MAXPORTS+1];
uint8_t hub_desc[16]; uint8_t hub_desc[16];
uint8_t endpoint; uint8_t endpoint;
uint8_t interval;
uint8_t numports; uint8_t numports;
uint8_t characteristics; uint8_t characteristics;
uint8_t powertime; uint8_t powertime;
uint8_t state;
Pipe_t *changepipe; Pipe_t *changepipe;
uint32_t changebits; uint32_t changebits;
uint32_t statusbits; uint32_t statusbits[MAXPORTS+1];
uint16_t portstatus[7]; uint16_t portstatus[MAXPORTS];
uint8_t portstate[7]; uint8_t portstate[MAXPORTS];
}; };
class KeyboardController : public USBDriver { class KeyboardController : public USBDriver {

View File

@ -551,15 +551,20 @@ bool USBHost::queue_Control_Transfer(Device_t *dev, setup_t *setup, void *buf, U
println("new_Control_Transfer"); println("new_Control_Transfer");
if (setup->wLength > 16384) return false; // max 16K data for control if (setup->wLength > 16384) return false; // max 16K data for control
transfer = allocate_Transfer(); transfer = allocate_Transfer();
if (!transfer) return false; if (!transfer) {
println(" error allocating setup transfer");
return false;
}
status = allocate_Transfer(); status = allocate_Transfer();
if (!status) { if (!status) {
println(" error allocating status transfer");
free_Transfer(transfer); free_Transfer(transfer);
return false; return false;
} }
if (setup->wLength > 0) { if (setup->wLength > 0) {
data = allocate_Transfer(); data = allocate_Transfer();
if (!data) { if (!data) {
println(" error allocating data transfer");
free_Transfer(transfer); free_Transfer(transfer);
free_Transfer(status); free_Transfer(status);
return false; return false;
@ -579,7 +584,8 @@ bool USBHost::queue_Control_Transfer(Device_t *dev, setup_t *setup, void *buf, U
status->pipe = dev->control_pipe; status->pipe = dev->control_pipe;
status->buffer = buf; status->buffer = buf;
status->length = setup->wLength; status->length = setup->wLength;
status->setup = setup; status->setup.word1 = setup->word1;
status->setup.word2 = setup->word2;
status->driver = driver; status->driver = driver;
status->qtd.next = 1; status->qtd.next = 1;
return queue_Transfer(dev->control_pipe, transfer); return queue_Transfer(dev->control_pipe, transfer);
@ -622,7 +628,8 @@ bool USBHost::queue_Data_Transfer(Pipe_t *pipe, void *buffer, uint32_t len, USBD
data->pipe = pipe; data->pipe = pipe;
data->buffer = buffer; data->buffer = buffer;
data->length = len; data->length = len;
data->setup = NULL; data->setup.word1 = 0;
data->setup.word2 = 0;
data->driver = driver; data->driver = driver;
// initialize all qTDs // initialize all qTDs
data = transfer; data = transfer;
@ -873,8 +880,7 @@ bool USBHost::allocate_interrupt_pipe_bandwidth(Pipe_t *pipe, uint32_t maxlen, u
} else { } else {
pipe->start_mask = 0x01 << (best_offset & 7); pipe->start_mask = 0x01 << (best_offset & 7);
} }
uint32_t poffset = best_offset >> 3; pipe->periodic_offset = best_offset >> 3;
pipe->periodic_offset = (poffset > 0) ? poffset : 1;
pipe->complete_mask = 0; pipe->complete_mask = 0;
} else { } else {
// full speed 12 Mbit/sec or low speed 1.5 Mbit/sec // full speed 12 Mbit/sec or low speed 1.5 Mbit/sec

169
hub.cpp
View File

@ -35,14 +35,15 @@ bool USBHub::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t
// only claim entire device, never at interface level // only claim entire device, never at interface level
if (type != 0) return false; if (type != 0) return false;
println("USBHub memory usage = ", sizeof(USBHub));
println("USBHub claim_device this=", (uint32_t)this, HEX); println("USBHub claim_device this=", (uint32_t)this, HEX);
// timer testing TODO: remove this later // timer testing TODO: remove this later
mytimer.init(this); mytimer.init(this);
mytimer.pointer = (void *)"This is mytimer"; mytimer.pointer = (void *)"This is mytimer";
mytimer.start(99129); //mytimer.start(99129);
othertimer.pointer = (void *)"Hello, I'm othertimer"; othertimer.pointer = (void *)"Hello, I'm othertimer";
othertimer.start(12345); //othertimer.start(12345);
for (int i=0; i < 7; i++) { for (int i=0; i < 7; i++) {
mytimers[i].init(this); mytimers[i].init(this);
//mytimers[i].start((i + 1) * 10000); //mytimers[i].start((i + 1) * 10000);
@ -65,7 +66,8 @@ bool USBHub::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t
uint32_t maxsize = descriptors[13] | (descriptors[14] << 8); uint32_t maxsize = descriptors[13] | (descriptors[14] << 8);
if (maxsize == 0) return false; if (maxsize == 0) return false;
if (maxsize > 1) return false; // do hub chips with > 7 ports exist? if (maxsize > 1) return false; // do hub chips with > 7 ports exist?
interval = descriptors[15];
println(" polling interval = ", interval);
println(descriptors[9]); println(descriptors[9]);
println(descriptors[10]); println(descriptors[10]);
println(descriptors[11], HEX); println(descriptors[11], HEX);
@ -78,14 +80,14 @@ bool USBHub::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t
println("bDeviceSubClass = ", dev->bDeviceSubClass); println("bDeviceSubClass = ", dev->bDeviceSubClass);
println("bDeviceProtocol = ", dev->bDeviceProtocol); println("bDeviceProtocol = ", dev->bDeviceProtocol);
numports = 0; // unknown until hub descriptor is read
changepipe = NULL; changepipe = NULL;
changebits = 0; changebits = 0;
state = 0;
memset(portstatus, 0, sizeof(portstatus)); memset(portstatus, 0, sizeof(portstatus));
memset(portstate, 0, sizeof(portstate)); memset(portstate, 0, sizeof(portstate));
mk_setup(setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc)); mk_setup(setup[0], 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
queue_Control_Transfer(dev, &setup, hub_desc, this); queue_Control_Transfer(dev, &setup[0], hub_desc, this);
return true; return true;
} }
@ -103,36 +105,33 @@ void USBHub::timer_event(USBDriverTimer *timer)
} }
void USBHub::poweron(uint32_t port) void USBHub::send_poweron(uint32_t port)
{ {
mk_setup(setup, 0x23, 3, 8, port, 0); if (port == 0 || port > numports) return;
queue_Control_Transfer(device, &setup, NULL, this); mk_setup(setup[port], 0x23, 3, 8, port, 0);
queue_Control_Transfer(device, &setup[port], NULL, this);
} }
void USBHub::getstatus(uint32_t port) void USBHub::send_getstatus(uint32_t port)
{ {
if (port == 0) { if (port > numports) return;
mk_setup(setup, 0xA0, 0, 0, port, 4); // get hub status println("getstatus, port = ", port);
} else { mk_setup(setup[port], ((port > 0) ? 0xA3 : 0xA0), 0, 0, port, 4);
mk_setup(setup, 0xA3, 0, 0, port, 4); // get port status queue_Control_Transfer(device, &setup[port], &statusbits[port], this);
}
queue_Control_Transfer(device, &setup, &statusbits, this);
} }
void USBHub::clearstatus(uint32_t port) void USBHub::send_clearstatus(uint32_t port)
{ {
if (port == 0) { if (port > numports) return;
mk_setup(setup, 0x20, 1, 0x10, port, 0); // clear hub status mk_setup(setup[port], ((port > 0) ? 0x23 : 0x20), 1, 0x10, port, 0);
} else { queue_Control_Transfer(device, &setup[port], NULL, this);
mk_setup(setup, 0x23, 1, 0x10, port, 0); // clear port status
}
queue_Control_Transfer(device, &setup, NULL, this);
} }
void USBHub::reset(uint32_t port) void USBHub::send_reset(uint32_t port)
{ {
mk_setup(setup, 0x23, 3, 4, port, 0); // set feature PORT_RESET if (port == 0 || port > numports) return;
queue_Control_Transfer(device, &setup, NULL, this); mk_setup(setup[port], 0x23, 3, 4, port, 0); // set feature PORT_RESET
queue_Control_Transfer(device, &setup[port], NULL, this);
} }
@ -141,60 +140,59 @@ void USBHub::control(const Transfer_t *transfer)
println("USBHub control callback"); println("USBHub control callback");
print_hexbytes(transfer->buffer, transfer->length); print_hexbytes(transfer->buffer, transfer->length);
if (state == 0) { uint32_t port = transfer->setup.wIndex;
// read hub descriptor to learn hub's capabilities uint32_t mesg = transfer->setup.word1;
// Hub Descriptor, USB 2.0, 11.23.2.1 page 417
if (hub_desc[0] == 9 && hub_desc[1] == 0x29) { switch (mesg) {
numports = hub_desc[2]; case 0x290006A0: // read hub descriptor
characteristics = hub_desc[3]; numports = hub_desc[2];
powertime = hub_desc[5]; characteristics = hub_desc[3];
// TODO: do we need to use the DeviceRemovable powertime = hub_desc[5];
// bits to mke synthetic device connect events? // TODO: do we need to use the DeviceRemovable
print("Hub has "); // bits to make synthetic device connect events?
print(numports); println("Hub ports = ", numports);
println(" ports"); for (uint32_t i=1; i <= numports; i++) {
state = 1; send_poweron(i);
poweron(1);
} }
} else if (state < numports) { break;
// turn on power to all ports case 0x00080323: // power turned on
poweron(++state); if (port == numports && changepipe == NULL) {
} else if (state == numports) { println("power turned on to all ports");
println("power turned on to all ports"); println("device addr = ", device->address);
println("device addr = ", device->address); changepipe = new_Pipe(device, 3, endpoint, 1, 1, interval);
// TODO: use hub's interrupt endpoint interval println("pipe cap1 = ", changepipe->qh.capabilities[0], HEX);
changepipe = new_Pipe(device, 3, endpoint, 1, 1, 64); changepipe->callback_function = callback;
println("pipe cap1 = ", changepipe->qh.capabilities[0], HEX); queue_Data_Transfer(changepipe, &changebits, 1, this);
changepipe->callback_function = callback;
queue_Data_Transfer(changepipe, &changebits, 1, this);
state = 255;
} else if (state == 255) {
// up and running...
switch (setup.word1) {
case 0x000000A0: // get hub status
println("New Hub Status");
clearstatus(0);
return;
case 0x000000A3: // get port status
new_port_status(setup.wIndex, statusbits);
clearstatus(setup.wIndex);
return;
case 0x00100120: // clear hub status
println("Hub Status Cleared");
changebits &= ~1;
break;
case 0x00100123: // clear port status
println("Port Status Cleared, port=", setup.wIndex);
changebits &= ~(1 << setup.wIndex);
break;
} }
update_status(); break;
case 0x000000A0: // get hub status
println("New Hub Status");
send_clearstatus(0);
break;
case 0x000000A3: // get port status
println("New Port Status");
if (transfer->length == 4) {
uint32_t status = *(uint32_t *)(transfer->buffer);
if (status != statusbits[port]) println("ERROR: status not same");
new_port_status(port, status);
}
send_clearstatus(port);
break;
case 0x00100120: // clear hub status
println("Hub Status Cleared");
break;
case 0x00100123: // clear port status
println("Port Status Cleared, port=", port);
break;
default:
println("unhandled setup, message = ", mesg, HEX);
} }
} }
void USBHub::callback(const Transfer_t *transfer) void USBHub::callback(const Transfer_t *transfer)
{ {
println("HUB Callback (static)"); //println("HUB Callback (static)");
if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer); if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer);
} }
@ -202,30 +200,21 @@ void USBHub::status_change(const Transfer_t *transfer)
{ {
println("HUB Callback (member)"); println("HUB Callback (member)");
println("status = ", changebits, HEX); println("status = ", changebits, HEX);
// TODO: do something with the status change info for (uint32_t i=0; i <= numports; i++) {
update_status(); if (changebits & (1 << i)) {
queue_Data_Transfer(changepipe, &changebits, 1, this); send_getstatus(i);
}
void USBHub::update_status()
{
uint32_t i, mask;
for (i=0, mask=1; i <= numports; i++, mask <<= 1) {
if (changebits & mask) {
getstatus(i);
return;
} }
} }
queue_Data_Transfer(changepipe, &changebits, 1, this);
} }
void USBHub::new_port_status(uint32_t port, uint32_t status) void USBHub::new_port_status(uint32_t port, uint32_t status)
{ {
if (port < 1 || port > 7) return; if (port == 0 || port > numports) return;
uint32_t priorstatus = portstatus[port - 1]; uint32_t priorstatus = portstatus[port - 1];
portstatus[port] = status; portstatus[port] = status;
print("New Port Status, port="); print(" Status: port=");
print(port); print(port);
print(", status="); print(", status=");
println(status, HEX); println(status, HEX);
@ -255,8 +244,8 @@ void USBHub::new_port_status(uint32_t port, uint32_t status)
if ((status & 0x0001) && !(priorstatus & 0x0001)) { if ((status & 0x0001) && !(priorstatus & 0x0001)) {
println(" connect"); println(" connect");
// 100 ms debounce (USB 2.0: TATTDB, page 150 & 188) // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
delay(100); // TODO: horribly bad... need timing events //delay(100); // TODO: horribly bad... need timing events
reset(port); //reset(port);
// TODO... reset timer? // TODO... reset timer?
} else if (!(status & 0x0001) && (priorstatus & 0x0001)) { } else if (!(status & 0x0001) && (priorstatus & 0x0001)) {

View File

@ -28,8 +28,8 @@
// Memory allocation // Memory allocation
static Device_t memory_Device[3]; static Device_t memory_Device[3];
static Pipe_t memory_Pipe[6] __attribute__ ((aligned(64))); static Pipe_t memory_Pipe[8] __attribute__ ((aligned(32)));
static Transfer_t memory_Transfer[24] __attribute__ ((aligned(64))); static Transfer_t memory_Transfer[34] __attribute__ ((aligned(32)));
static Device_t * free_Device_list = NULL; static Device_t * free_Device_list = NULL;
static Pipe_t * free_Pipe_list = NULL; static Pipe_t * free_Pipe_list = NULL;