|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
-
-
- #include <Arduino.h>
- #include "USBHost_t36.h"
-
-
-
-
- volatile bool USBHub::reset_busy = false;
-
- #define print USBHost::print_
- #define println USBHost::println_
-
- void USBHub::init()
- {
- contribute_Devices(mydevices, sizeof(mydevices)/sizeof(Device_t));
- contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
- contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
- contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
- driver_ready_for_device(this);
- }
-
- bool USBHub::claim(Device_t *dev, int type, const uint8_t *d, uint32_t len)
- {
-
- if (type != 0) return false;
-
- println("USBHub memory usage = ", sizeof(USBHub));
- println("USBHub claim_device this=", (uint32_t)this, HEX);
-
- resettimer.pointer = (void *)"Hello, I'm resettimer";
- debouncetimer.pointer = (void *)"Debounce Timer";
-
-
- if (dev->bDeviceClass != 9 || dev->bDeviceSubClass != 0) return false;
-
- if (dev->bDeviceProtocol > 2) return false;
-
- interface_count = 0;
- while (len >= 16) {
- if (d[0] == 9 && d[1] == 4 &&
- d[4] == 1 &&
- d[5] == 9 &&
- d[7] >= 0 && d[7] <= 2 &&
- d[9] == 7 && d[10] == 5 &&
- (d[11] & 0xF0) == 0x80 &&
- d[12] == 3 &&
- d[13] == 1 && d[14] == 0) {
- println("found possible interface, altsetting=", d[3]);
- if (interface_count == 0) {
- interface_number = d[2];
- altsetting = d[3];
- protocol = d[7];
- endpoint = d[11] & 0x0F;
- interval = d[15];
- } else {
- if (d[2] != interface_number) break;
- if (d[7] > protocol) {
- altsetting = d[3];
- protocol = d[7];
- endpoint = d[11] & 0x0F;
- interval = d[15];
- }
- }
- interface_count++;
- }
- d += 16;
- len -= 16;
- }
- if (interface_count == 0) return false;
- println("number of interfaces found = ", interface_count);
- if (interface_count > 1) {
- print("best interface is ", interface_number);
- println(" using altsetting ", altsetting);
- }
- numports = 0;
- changepipe = NULL;
- changebits = 0;
- sending_control_transfer = 0;
- port_doing_reset = 0;
- memset(portstate, 0, sizeof(portstate));
- memset(devicelist, 0, sizeof(devicelist));
-
- mk_setup(setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
- queue_Control_Transfer(dev, &setup, hub_desc, this);
-
- return true;
- }
-
-
- bool USBHub::can_send_control_now()
- {
- if (sending_control_transfer) return false;
- sending_control_transfer = 1;
- return true;
- }
-
- void USBHub::send_poweron(uint32_t port)
- {
- if (port == 0 || port > numports) return;
- if (can_send_control_now()) {
- mk_setup(setup, 0x23, 3, 8, port, 0);
- queue_Control_Transfer(device, &setup, NULL, this);
- send_pending_poweron &= ~(1 << port);
- } else {
- send_pending_poweron |= (1 << port);
- }
- }
-
- void USBHub::send_getstatus(uint32_t port)
- {
- if (port > numports) return;
- if (can_send_control_now()) {
- println("getstatus, port = ", port);
- mk_setup(setup, ((port > 0) ? 0xA3 : 0xA0), 0, 0, port, 4);
- queue_Control_Transfer(device, &setup, &statusbits, this);
- send_pending_getstatus &= ~(1 << port);
- } else {
- println("deferred getstatus, port = ", port);
- send_pending_getstatus |= (1 << port);
- }
- }
-
- void USBHub::send_clearstatus_connect(uint32_t port)
- {
- if (port == 0 || port > numports) return;
- if (can_send_control_now()) {
- mk_setup(setup, 0x23, 1, 16, port, 0);
- queue_Control_Transfer(device, &setup, NULL, this);
- send_pending_clearstatus_connect &= ~(1 << port);
- } else {
- send_pending_clearstatus_connect |= (1 << port);
- }
- }
-
- void USBHub::send_clearstatus_enable(uint32_t port)
- {
- if (port == 0 || port > numports) return;
- if (can_send_control_now()) {
- mk_setup(setup, 0x23, 1, 17, port, 0);
- queue_Control_Transfer(device, &setup, NULL, this);
- send_pending_clearstatus_enable &= ~(1 << port);
- } else {
- send_pending_clearstatus_enable |= (1 << port);
- }
- }
-
- void USBHub::send_clearstatus_suspend(uint32_t port)
- {
- if (port == 0 || port > numports) return;
- if (can_send_control_now()) {
- mk_setup(setup, 0x23, 1, 18, port, 0);
- queue_Control_Transfer(device, &setup, NULL, this);
- send_pending_clearstatus_suspend &= ~(1 << port);
- } else {
- send_pending_clearstatus_suspend |= (1 << port);
- }
- }
-
- void USBHub::send_clearstatus_overcurrent(uint32_t port)
- {
- if (port == 0 || port > numports) return;
- if (can_send_control_now()) {
- mk_setup(setup, 0x23, 1, 19, port, 0);
- queue_Control_Transfer(device, &setup, NULL, this);
- send_pending_clearstatus_overcurrent &= ~(1 << port);
- } else {
- send_pending_clearstatus_overcurrent |= (1 << port);
- }
- }
-
- void USBHub::send_clearstatus_reset(uint32_t port)
- {
- if (port == 0 || port > numports) return;
- if (can_send_control_now()) {
- mk_setup(setup, 0x23, 1, 20, port, 0);
- queue_Control_Transfer(device, &setup, NULL, this);
- send_pending_clearstatus_reset &= ~(1 << port);
- } else {
- send_pending_clearstatus_reset |= (1 << port);
- }
- }
-
- void USBHub::send_setreset(uint32_t port)
- {
- if (port == 0 || port > numports) return;
- println("send_setreset");
- if (can_send_control_now()) {
- mk_setup(setup, 0x23, 3, 4, port, 0);
- queue_Control_Transfer(device, &setup, NULL, this);
- send_pending_setreset &= ~(1 << port);
- } else {
- send_pending_setreset |= (1 << port);
- }
- }
-
- void USBHub::send_setinterface()
- {
-
- mk_setup(setup, 1, 11, altsetting, interface_number, 0);
- queue_Control_Transfer(device, &setup, NULL, this);
- sending_control_transfer = 1;
- }
-
- static uint32_t lowestbit(uint32_t bitmask)
- {
- return __builtin_ctz(bitmask);
- }
-
- void USBHub::control(const Transfer_t *transfer)
- {
- println("USBHub control callback");
- print_hexbytes(transfer->buffer, transfer->length);
-
- sending_control_transfer = 0;
- uint32_t port = transfer->setup.wIndex;
- uint32_t mesg = transfer->setup.word1;
-
- switch (mesg) {
- case 0x290006A0:
- numports = hub_desc[2];
- characteristics = hub_desc[3];
- powertime = hub_desc[5];
- if (interface_count > 1) {
- send_setinterface();
- }
-
-
- println("Hub ports = ", numports);
- for (uint32_t i=1; i <= numports; i++) {
- send_poweron(i);
- }
- break;
- case 0x00080323:
- if (port == numports && changepipe == NULL) {
- println("power turned on to all ports");
- println("device addr = ", device->address);
- changepipe = new_Pipe(device, 3, endpoint, 1, 1, interval);
- println("pipe cap1 = ", changepipe->qh.capabilities[0], HEX);
- changepipe->callback_function = callback;
- queue_Data_Transfer(changepipe, &changebits, 1, this);
- }
- break;
-
- case 0x000000A0:
- println("New Hub Status");
- break;
- case 0x000000A3:
- println("New Port Status");
- if (transfer->length == 4) {
- uint32_t status = *(uint32_t *)(transfer->buffer);
- if (status != statusbits) println("ERROR: status not same");
- new_port_status(port, status);
- }
-
-
-
-
- break;
- case 0x00100120:
- println("Hub Status Cleared");
- break;
- case 0x00100123:
- println("Port Status Cleared, port=", port);
- break;
- default:
- println("unhandled setup, message = ", mesg, HEX);
- }
-
-
-
-
-
- if (sending_control_transfer) return;
- if (send_pending_poweron) {
- send_poweron(lowestbit(send_pending_poweron));
- } else if (send_pending_clearstatus_connect) {
- send_clearstatus_connect(lowestbit(send_pending_clearstatus_connect));
- } else if (send_pending_clearstatus_enable) {
- send_clearstatus_enable(lowestbit(send_pending_clearstatus_enable));
- } else if (send_pending_clearstatus_suspend) {
- send_clearstatus_suspend(lowestbit(send_pending_clearstatus_suspend));
- } else if (send_pending_clearstatus_overcurrent) {
- send_clearstatus_overcurrent(lowestbit(send_pending_clearstatus_overcurrent));
- } else if (send_pending_clearstatus_reset) {
- send_clearstatus_reset(lowestbit(send_pending_clearstatus_reset));
- } else if (send_pending_getstatus) {
- send_getstatus(lowestbit(send_pending_getstatus));
- } else if (send_pending_setreset) {
- send_setreset(lowestbit(send_pending_setreset));
- }
- }
-
- void USBHub::callback(const Transfer_t *transfer)
- {
-
- if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer);
- }
-
- void USBHub::status_change(const Transfer_t *transfer)
- {
- println("HUB Callback (member)");
- println("status = ", changebits, HEX);
- for (uint32_t i=0; i <= numports; i++) {
- if (changebits & (1 << i)) {
- send_getstatus(i);
- }
- }
- queue_Data_Transfer(changepipe, &changebits, 1, this);
- }
-
- void USBHub::new_port_status(uint32_t port, uint32_t status)
- {
- if (port == 0 || port > numports) return;
- #if 1
- print(" status=");
- print(status, HEX);
- println(" port=", port);
- println(" state=", portstate[port-1]);
-
- if (status & 0x0001) println(" Device is present: ");
- if (status & 0x0002) {
- print(" Enabled, speed = ");
- if (status & 0x0200) {
- print("1.5");
- } else {
- if (status & 0x0400) {
- print("480");
- } else {
- print("12");
- }
- }
- println(" Mbit/sec");
- }
- if (status & 0x0004) println(" Suspended");
- if (status & 0x0008) println(" Over-current");
- if (status & 0x0010) println(" Reset");
- if (status & 0x0100) println(" Has Power");
- if (status & 0x0800) println(" Test Mode");
- if (status & 0x1000) println(" Software Controls LEDs");
- #endif
- uint8_t &state = portstate[port-1];
- switch (state) {
- case PORT_OFF:
- case PORT_DISCONNECT:
- if (status & 0x0001) {
- state = PORT_DEBOUNCE1;
- start_debounce_timer(port);
- send_clearstatus_connect(port);
- }
- break;
- case PORT_DEBOUNCE1:
- case PORT_DEBOUNCE2:
- case PORT_DEBOUNCE3:
- case PORT_DEBOUNCE4:
- case PORT_DEBOUNCE5:
- if (status & 0x0001) {
- if (++state > PORT_DEBOUNCE5) {
- if (USBHub::reset_busy || USBHost::enumeration_busy) {
-
-
- state = PORT_DEBOUNCE5;
- break;
- }
- USBHub::reset_busy = true;
- stop_debounce_timer(port);
- state = PORT_RESET;
- println("sending reset");
- send_setreset(port);
- port_doing_reset = port;
- }
- } else {
- stop_debounce_timer(port);
- state = PORT_DISCONNECT;
- }
- break;
- case PORT_RESET:
- if (status & 0x0002) {
-
- send_clearstatus_reset(port);
- state = PORT_RECOVERY;
- uint8_t speed=0;
- if (status & 0x0200) speed = 1;
- else if (status & 0x0400) speed = 2;
- port_doing_reset_speed = speed;
- resettimer.start(25000);
- } else if (!(status & 0x0001)) {
- send_clearstatus_connect(port);
- USBHub::reset_busy = false;
- state = PORT_DISCONNECT;
- }
- break;
- case PORT_RECOVERY:
- if (!(status & 0x0001)) {
- send_clearstatus_connect(port);
- USBHub::reset_busy = false;
- state = PORT_DISCONNECT;
- }
- break;
- case PORT_ACTIVE:
- if (!(status & 0x0001)) {
- disconnect_Device(devicelist[port-1]);
- devicelist[port-1] = NULL;
- send_clearstatus_connect(port);
- state = PORT_DISCONNECT;
- }
- break;
- }
- }
-
-
- void USBHub::timer_event(USBDriverTimer *timer)
- {
- uint32_t us = micros() - timer->started_micros;
- print("timer event (");
- print(us);
- print(" us): ");
- print((char *)timer->pointer);
- print(", this = ");
- print((uint32_t)this, HEX);
- println(", timer = ", (uint32_t)timer, HEX);
- if (timer == &debouncetimer) {
- uint32_t in_use = debounce_in_use;
- println("ports in use bitmask = ", in_use, HEX);
- if (in_use) {
- for (uint32_t i=1; i <= numports; i++) {
- if (in_use & (1 << i)) send_getstatus(i);
- }
- debouncetimer.start(20000);
- }
- } else if (timer == &resettimer) {
- uint8_t port = port_doing_reset;
- println("port_doing_reset = ", port);
- if (port_doing_reset) {
- uint8_t &state = portstate[port-1];
- if (state == PORT_RECOVERY) {
- port_doing_reset = 0;
- println("PORT_RECOVERY");
-
- uint8_t speed = port_doing_reset_speed;
- devicelist[port-1] = new_Device(speed, device->address, port);
-
-
-
-
-
-
- USBHub::reset_busy = false;
- state = PORT_ACTIVE;
- }
- }
- }
-
-
-
-
- }
-
- void USBHub::start_debounce_timer(uint32_t port)
- {
- if (debounce_in_use == 0) debouncetimer.start(20000);
- debounce_in_use |= (1 << port);
- }
-
- void USBHub::stop_debounce_timer(uint32_t port)
- {
- debounce_in_use &= ~(1 << port);
- }
-
-
- void USBHub::disconnect()
- {
-
- for (uint32_t i=0; i < numports; i++) {
- if (devicelist[i]) disconnect_Device(devicelist[i]);
- }
- numports = 0;
- changepipe = NULL;
- changebits = 0;
- sending_control_transfer = 0;
- port_doing_reset = 0;
- memset(portstate, 0, sizeof(portstate));
- memset(devicelist, 0, sizeof(devicelist));
- send_pending_poweron = 0;
- send_pending_getstatus = 0;
- send_pending_clearstatus_connect = 0;
- send_pending_clearstatus_enable = 0;
- send_pending_clearstatus_suspend = 0;
- send_pending_clearstatus_overcurrent = 0;
- send_pending_clearstatus_reset = 0;
- send_pending_setreset = 0;
- debounce_in_use = 0;
- }
-
-
-
-
-
|