2017-02-11 09:03:54 -05:00
|
|
|
/* USB EHCI Host for Teensy 3.6
|
|
|
|
* Copyright 2017 Paul Stoffregen (paul@pjrc.com)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include "USBHost.h"
|
|
|
|
|
2017-03-03 07:58:10 -05:00
|
|
|
// True when any hub port is in the reset or reset recovery phase.
|
|
|
|
// Only one USB device may be reset at a time, because it will
|
|
|
|
// begin responding to address zero.
|
|
|
|
volatile bool USBHub::reset_busy = false;
|
|
|
|
|
|
|
|
USBHub::USBHub() : debouncetimer(this), resettimer(this)
|
2017-02-11 09:03:54 -05:00
|
|
|
{
|
2017-03-05 18:48:50 -05:00
|
|
|
contribute_Devices(mydevices, sizeof(mydevices)/sizeof(Device_t));
|
|
|
|
contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
|
|
|
|
contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
|
2017-02-11 09:03:54 -05:00
|
|
|
driver_ready_for_device(this);
|
|
|
|
}
|
|
|
|
|
2017-02-18 14:21:28 -05:00
|
|
|
bool USBHub::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
|
2017-02-11 09:03:54 -05:00
|
|
|
{
|
2017-02-12 04:42:30 -05:00
|
|
|
// only claim entire device, never at interface level
|
|
|
|
if (type != 0) return false;
|
|
|
|
|
2017-02-28 11:39:43 -05:00
|
|
|
println("USBHub memory usage = ", sizeof(USBHub));
|
2017-02-18 15:03:10 -05:00
|
|
|
println("USBHub claim_device this=", (uint32_t)this, HEX);
|
2017-02-11 09:03:54 -05:00
|
|
|
|
2017-03-01 15:08:47 -05:00
|
|
|
resettimer.pointer = (void *)"Hello, I'm resettimer";
|
2017-02-28 14:39:59 -05:00
|
|
|
debouncetimer.pointer = (void *)"Debounce Timer";
|
2017-02-27 05:22:02 -05:00
|
|
|
|
2017-02-11 16:44:23 -05:00
|
|
|
// check for HUB type
|
2017-02-11 09:03:54 -05:00
|
|
|
if (dev->bDeviceClass != 9 || dev->bDeviceSubClass != 0) return false;
|
2017-02-11 16:44:23 -05:00
|
|
|
// protocol must be 0=FS, 1=HS Single-TT, or 2=HS Multi-TT
|
|
|
|
if (dev->bDeviceProtocol > 2) return false;
|
|
|
|
// check for endpoint descriptor
|
|
|
|
if (descriptors[9] != 7 || descriptors[10] != 5) return false;
|
|
|
|
// endpoint must be IN direction
|
|
|
|
if ((descriptors[11] & 0xF0) != 0x80) return false;
|
|
|
|
// endpoint type must be interrupt
|
|
|
|
if (descriptors[12] != 3) return false;
|
|
|
|
// get the endpoint number, must not be zero
|
2017-02-12 15:03:26 -05:00
|
|
|
endpoint = descriptors[11] & 0x0F;
|
2017-02-11 16:44:23 -05:00
|
|
|
if (endpoint == 0) return false;
|
|
|
|
// get the maximum packet size
|
|
|
|
uint32_t maxsize = descriptors[13] | (descriptors[14] << 8);
|
2017-02-12 15:03:26 -05:00
|
|
|
if (maxsize == 0) return false;
|
|
|
|
if (maxsize > 1) return false; // do hub chips with > 7 ports exist?
|
2017-02-28 11:39:43 -05:00
|
|
|
interval = descriptors[15];
|
|
|
|
println(" polling interval = ", interval);
|
2017-02-18 15:03:10 -05:00
|
|
|
println(descriptors[9]);
|
|
|
|
println(descriptors[10]);
|
|
|
|
println(descriptors[11], HEX);
|
|
|
|
println(maxsize);
|
2017-02-11 09:03:54 -05:00
|
|
|
// bDeviceProtocol = 0 is full speed
|
|
|
|
// bDeviceProtocol = 1 is high speed single TT
|
|
|
|
// bDeviceProtocol = 2 is high speed multiple TT
|
|
|
|
|
2017-02-18 15:03:10 -05:00
|
|
|
println("bDeviceClass = ", dev->bDeviceClass);
|
|
|
|
println("bDeviceSubClass = ", dev->bDeviceSubClass);
|
|
|
|
println("bDeviceProtocol = ", dev->bDeviceProtocol);
|
2017-02-11 09:03:54 -05:00
|
|
|
|
2017-02-28 11:39:43 -05:00
|
|
|
numports = 0; // unknown until hub descriptor is read
|
2017-02-12 15:03:26 -05:00
|
|
|
changepipe = NULL;
|
|
|
|
changebits = 0;
|
2017-03-01 14:33:07 -05:00
|
|
|
sending_control_transfer = 0;
|
2017-03-01 16:29:00 -05:00
|
|
|
port_doing_reset = 0;
|
2017-02-17 20:44:24 -05:00
|
|
|
memset(portstate, 0, sizeof(portstate));
|
2017-03-01 16:29:00 -05:00
|
|
|
memset(devicelist, 0, sizeof(devicelist));
|
2017-02-12 15:03:26 -05:00
|
|
|
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
|
|
|
|
queue_Control_Transfer(dev, &setup, hub_desc, this);
|
2017-02-11 16:44:23 -05:00
|
|
|
|
2017-02-11 09:03:54 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-27 07:17:43 -05:00
|
|
|
|
2017-03-01 14:33:07 -05:00
|
|
|
bool USBHub::can_send_control_now()
|
|
|
|
{
|
|
|
|
if (sending_control_transfer) return false;
|
|
|
|
sending_control_transfer = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-28 11:39:43 -05:00
|
|
|
void USBHub::send_poweron(uint32_t port)
|
2017-02-11 16:44:23 -05:00
|
|
|
{
|
2017-02-28 11:39:43 -05:00
|
|
|
if (port == 0 || port > numports) return;
|
2017-03-01 14:33:07 -05:00
|
|
|
if (can_send_control_now()) {
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, 0x23, 3, 8, port, 0);
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
2017-03-01 14:33:07 -05:00
|
|
|
send_pending_poweron &= ~(1 << port);
|
|
|
|
} else {
|
|
|
|
send_pending_poweron |= (1 << port);
|
|
|
|
}
|
2017-02-12 15:03:26 -05:00
|
|
|
}
|
2017-02-12 04:42:30 -05:00
|
|
|
|
2017-02-28 11:39:43 -05:00
|
|
|
void USBHub::send_getstatus(uint32_t port)
|
2017-02-13 15:56:01 -05:00
|
|
|
{
|
2017-02-28 11:39:43 -05:00
|
|
|
if (port > numports) return;
|
2017-03-01 14:33:07 -05:00
|
|
|
if (can_send_control_now()) {
|
2017-03-03 08:15:57 -05:00
|
|
|
println("getstatus, port = ", port);
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, ((port > 0) ? 0xA3 : 0xA0), 0, 0, port, 4);
|
|
|
|
queue_Control_Transfer(device, &setup, &statusbits, this);
|
2017-03-01 14:33:07 -05:00
|
|
|
send_pending_getstatus &= ~(1 << port);
|
|
|
|
} else {
|
2017-03-03 08:15:57 -05:00
|
|
|
println("deferred getstatus, port = ", port);
|
2017-03-01 14:33:07 -05:00
|
|
|
send_pending_getstatus |= (1 << port);
|
|
|
|
}
|
2017-02-13 15:56:01 -05:00
|
|
|
}
|
|
|
|
|
2017-03-01 14:33:07 -05:00
|
|
|
void USBHub::send_clearstatus_connect(uint32_t port)
|
2017-02-13 15:56:01 -05:00
|
|
|
{
|
2017-03-01 14:33:07 -05:00
|
|
|
if (port == 0 || port > numports) return;
|
|
|
|
if (can_send_control_now()) {
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, 0x23, 1, 16, port, 0); // 16=C_PORT_CONNECTION
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
2017-03-01 14:33:07 -05:00
|
|
|
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()) {
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, 0x23, 1, 17, port, 0); // 17=C_PORT_ENABLE
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
2017-03-01 14:33:07 -05:00
|
|
|
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()) {
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, 0x23, 1, 18, port, 0); // 18=C_PORT_SUSPEND
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
2017-03-01 14:33:07 -05:00
|
|
|
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()) {
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, 0x23, 1, 19, port, 0); // 19=C_PORT_OVER_CURRENT
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
2017-03-01 14:33:07 -05:00
|
|
|
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()) {
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, 0x23, 1, 20, port, 0); // 20=C_PORT_RESET
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
2017-03-01 14:33:07 -05:00
|
|
|
send_pending_clearstatus_reset &= ~(1 << port);
|
|
|
|
} else {
|
|
|
|
send_pending_clearstatus_reset |= (1 << port);
|
|
|
|
}
|
2017-02-13 15:56:01 -05:00
|
|
|
}
|
|
|
|
|
2017-02-28 14:39:59 -05:00
|
|
|
void USBHub::send_setreset(uint32_t port)
|
2017-02-17 20:44:24 -05:00
|
|
|
{
|
2017-02-28 11:39:43 -05:00
|
|
|
if (port == 0 || port > numports) return;
|
2017-02-28 14:39:59 -05:00
|
|
|
println("send_setreset");
|
2017-03-01 14:33:07 -05:00
|
|
|
if (can_send_control_now()) {
|
2017-03-01 15:08:47 -05:00
|
|
|
mk_setup(setup, 0x23, 3, 4, port, 0); // set feature PORT_RESET
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
2017-03-01 14:33:07 -05:00
|
|
|
send_pending_setreset &= ~(1 << port);
|
|
|
|
} else {
|
|
|
|
send_pending_setreset |= (1 << port);
|
|
|
|
}
|
2017-02-17 20:44:24 -05:00
|
|
|
}
|
|
|
|
|
2017-03-01 14:33:07 -05:00
|
|
|
static uint32_t lowestbit(uint32_t bitmask)
|
|
|
|
{
|
|
|
|
return 31 - __builtin_clz(bitmask);
|
|
|
|
}
|
2017-02-17 20:44:24 -05:00
|
|
|
|
2017-02-13 16:11:54 -05:00
|
|
|
void USBHub::control(const Transfer_t *transfer)
|
2017-02-12 15:03:26 -05:00
|
|
|
{
|
2017-02-18 15:03:10 -05:00
|
|
|
println("USBHub control callback");
|
2017-02-12 15:03:26 -05:00
|
|
|
print_hexbytes(transfer->buffer, transfer->length);
|
2017-02-12 04:42:30 -05:00
|
|
|
|
2017-02-28 11:39:43 -05:00
|
|
|
uint32_t port = transfer->setup.wIndex;
|
|
|
|
uint32_t mesg = transfer->setup.word1;
|
|
|
|
|
|
|
|
switch (mesg) {
|
|
|
|
case 0x290006A0: // read hub descriptor
|
|
|
|
numports = hub_desc[2];
|
|
|
|
characteristics = hub_desc[3];
|
|
|
|
powertime = hub_desc[5];
|
|
|
|
// TODO: do we need to use the DeviceRemovable
|
|
|
|
// bits to make synthetic device connect events?
|
|
|
|
println("Hub ports = ", numports);
|
|
|
|
for (uint32_t i=1; i <= numports; i++) {
|
|
|
|
send_poweron(i);
|
2017-02-12 04:42:30 -05:00
|
|
|
}
|
2017-02-28 11:39:43 -05:00
|
|
|
break;
|
|
|
|
case 0x00080323: // power turned on
|
|
|
|
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);
|
2017-02-13 15:56:01 -05:00
|
|
|
}
|
2017-02-28 11:39:43 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x000000A0: // get hub status
|
|
|
|
println("New Hub Status");
|
|
|
|
break;
|
|
|
|
case 0x000000A3: // get port status
|
|
|
|
println("New Port Status");
|
|
|
|
if (transfer->length == 4) {
|
|
|
|
uint32_t status = *(uint32_t *)(transfer->buffer);
|
2017-03-01 15:08:47 -05:00
|
|
|
if (status != statusbits) println("ERROR: status not same");
|
2017-02-28 11:39:43 -05:00
|
|
|
new_port_status(port, status);
|
|
|
|
}
|
2017-02-28 14:39:59 -05:00
|
|
|
//if (changebits & (1 << port)) {
|
|
|
|
//changebits &= ~(1 << port);
|
2017-03-01 14:33:07 -05:00
|
|
|
//send_clearstatus(port);
|
2017-02-28 14:39:59 -05:00
|
|
|
//}
|
2017-02-28 11:39:43 -05:00
|
|
|
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);
|
2017-02-12 04:42:30 -05:00
|
|
|
}
|
2017-03-01 14:33:07 -05:00
|
|
|
// After we've completed processing for this control
|
|
|
|
// transfer, check if any more need to be sent. These
|
|
|
|
// allow only a single control transfer to occur at once
|
|
|
|
// which isn't fast, but requires only 3 Transfer_t and
|
|
|
|
// allows reusing the setup and other buffers
|
|
|
|
sending_control_transfer = 0;
|
|
|
|
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));
|
|
|
|
}
|
2017-02-11 16:44:23 -05:00
|
|
|
}
|
|
|
|
|
2017-02-13 09:05:09 -05:00
|
|
|
void USBHub::callback(const Transfer_t *transfer)
|
|
|
|
{
|
2017-02-28 11:39:43 -05:00
|
|
|
//println("HUB Callback (static)");
|
2017-02-13 09:05:09 -05:00
|
|
|
if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void USBHub::status_change(const Transfer_t *transfer)
|
|
|
|
{
|
2017-02-18 15:03:10 -05:00
|
|
|
println("HUB Callback (member)");
|
|
|
|
println("status = ", changebits, HEX);
|
2017-02-28 11:39:43 -05:00
|
|
|
for (uint32_t i=0; i <= numports; i++) {
|
|
|
|
if (changebits & (1 << i)) {
|
|
|
|
send_getstatus(i);
|
2017-02-13 15:56:01 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-28 11:39:43 -05:00
|
|
|
queue_Data_Transfer(changepipe, &changebits, 1, this);
|
2017-02-13 15:56:01 -05:00
|
|
|
}
|
2017-02-11 16:44:23 -05:00
|
|
|
|
2017-02-17 20:44:24 -05:00
|
|
|
void USBHub::new_port_status(uint32_t port, uint32_t status)
|
|
|
|
{
|
2017-02-28 11:39:43 -05:00
|
|
|
if (port == 0 || port > numports) return;
|
2017-02-28 14:39:59 -05:00
|
|
|
#if 1
|
|
|
|
print(" status=");
|
|
|
|
print(status, HEX);
|
|
|
|
println(" port=", port);
|
|
|
|
println(" state=", portstate[port-1]);
|
2017-02-17 20:44:24 -05:00
|
|
|
// status bits, USB 2.0: 11.24.2.7.1 page 427
|
2017-02-18 15:03:10 -05:00
|
|
|
if (status & 0x0001) println(" Device is present: ");
|
2017-02-17 20:44:24 -05:00
|
|
|
if (status & 0x0002) {
|
2017-03-01 14:33:07 -05:00
|
|
|
print(" Enabled, speed = ");
|
2017-02-17 20:44:24 -05:00
|
|
|
if (status & 0x0200) {
|
2017-02-18 15:03:10 -05:00
|
|
|
print("1.5");
|
2017-02-17 20:44:24 -05:00
|
|
|
} else {
|
|
|
|
if (status & 0x0400) {
|
2017-02-18 15:03:10 -05:00
|
|
|
print("480");
|
2017-02-17 20:44:24 -05:00
|
|
|
} else {
|
2017-02-18 15:03:10 -05:00
|
|
|
print("12");
|
2017-02-17 20:44:24 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 15:03:10 -05:00
|
|
|
println(" Mbit/sec");
|
2017-02-17 20:44:24 -05:00
|
|
|
}
|
2017-02-18 15:03:10 -05:00
|
|
|
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");
|
2017-02-28 14:39:59 -05:00
|
|
|
#endif
|
|
|
|
uint8_t &state = portstate[port-1];
|
|
|
|
switch (state) {
|
|
|
|
case PORT_OFF:
|
|
|
|
case PORT_DISCONNECT:
|
|
|
|
if (status & 0x0001) { // connected
|
|
|
|
state = PORT_DEBOUNCE1;
|
|
|
|
start_debounce_timer(port);
|
2017-03-01 14:33:07 -05:00
|
|
|
send_clearstatus_connect(port);
|
2017-02-28 14:39:59 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PORT_DEBOUNCE1:
|
|
|
|
case PORT_DEBOUNCE2:
|
|
|
|
case PORT_DEBOUNCE3:
|
|
|
|
case PORT_DEBOUNCE4:
|
|
|
|
case PORT_DEBOUNCE5:
|
|
|
|
if (status & 0x0001) {
|
|
|
|
if (++state > PORT_DEBOUNCE5) {
|
2017-03-03 07:58:10 -05:00
|
|
|
if (USBHub::reset_busy || USBHost::enumeration_busy) {
|
|
|
|
// wait in debounce state if another port is
|
|
|
|
// resetting or a device is busy enumerating
|
|
|
|
state = PORT_DEBOUNCE5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
USBHub::reset_busy = true;
|
2017-02-28 14:39:59 -05:00
|
|
|
stop_debounce_timer(port);
|
2017-03-01 16:29:00 -05:00
|
|
|
state = PORT_RESET;
|
2017-02-28 14:39:59 -05:00
|
|
|
println("sending reset");
|
|
|
|
send_setreset(port);
|
2017-03-01 16:29:00 -05:00
|
|
|
port_doing_reset = port;
|
2017-02-28 14:39:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-03 09:04:02 -05:00
|
|
|
stop_debounce_timer(port);
|
2017-02-28 14:39:59 -05:00
|
|
|
state = PORT_DISCONNECT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PORT_RESET:
|
2017-03-01 14:33:07 -05:00
|
|
|
if (status & 0x0002) {
|
|
|
|
// port is now enabled
|
|
|
|
send_clearstatus_reset(port);
|
2017-03-01 16:29:00 -05:00
|
|
|
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);
|
2017-03-03 09:04:02 -05:00
|
|
|
} else if (!(status & 0x0001)) {
|
|
|
|
send_clearstatus_connect(port);
|
|
|
|
USBHub::reset_busy = false;
|
|
|
|
state = PORT_DISCONNECT;
|
2017-03-01 14:33:07 -05:00
|
|
|
}
|
2017-03-01 16:29:00 -05:00
|
|
|
break;
|
2017-02-28 14:39:59 -05:00
|
|
|
case PORT_RECOVERY:
|
2017-03-03 09:04:02 -05:00
|
|
|
if (!(status & 0x0001)) {
|
|
|
|
send_clearstatus_connect(port);
|
|
|
|
USBHub::reset_busy = false;
|
|
|
|
state = PORT_DISCONNECT;
|
|
|
|
}
|
2017-03-01 16:29:00 -05:00
|
|
|
break;
|
2017-02-28 14:39:59 -05:00
|
|
|
case PORT_ACTIVE:
|
2017-03-03 09:04:02 -05:00
|
|
|
if (!(status & 0x0001)) {
|
|
|
|
disconnect_Device(devicelist[port-1]);
|
|
|
|
devicelist[port-1] = NULL;
|
|
|
|
send_clearstatus_connect(port);
|
|
|
|
state = PORT_DISCONNECT;
|
|
|
|
}
|
2017-02-28 14:39:59 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 20:44:24 -05:00
|
|
|
|
2017-02-28 14:39:59 -05:00
|
|
|
|
|
|
|
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;
|
2017-03-03 08:15:57 -05:00
|
|
|
println("ports in use bitmask = ", in_use, HEX);
|
2017-03-01 16:29:00 -05:00
|
|
|
if (in_use) {
|
2017-03-03 08:15:57 -05:00
|
|
|
for (uint32_t i=1; i <= numports; i++) {
|
2017-02-28 14:39:59 -05:00
|
|
|
if (in_use & (1 << i)) send_getstatus(i);
|
|
|
|
}
|
|
|
|
debouncetimer.start(20000);
|
|
|
|
}
|
2017-03-01 16:29:00 -05:00
|
|
|
} 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");
|
|
|
|
// begin enumeration process
|
|
|
|
uint8_t speed = port_doing_reset_speed;
|
|
|
|
devicelist[port-1] = new_Device(speed, device->address, port);
|
2017-03-03 07:58:10 -05:00
|
|
|
// TODO: if return is NULL, what to do? Panic?
|
|
|
|
// Can we disable the port? Will this device
|
|
|
|
// play havoc if it sits unconfigured responding
|
|
|
|
// to address zero? Does that even matter? Maybe
|
|
|
|
// we have far worse issues when memory isn't
|
|
|
|
// available?!
|
|
|
|
USBHub::reset_busy = false;
|
2017-03-01 16:29:00 -05:00
|
|
|
state = PORT_ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 14:39:59 -05:00
|
|
|
}
|
2017-03-03 07:58:10 -05:00
|
|
|
|
|
|
|
// TODO: testing only!!!
|
2017-03-03 09:04:02 -05:00
|
|
|
//static uint32_t count=0;
|
|
|
|
//if (++count > 36) while (1) ; // stop here
|
2017-02-28 14:39:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-02-17 20:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-13 16:11:54 -05:00
|
|
|
void USBHub::disconnect()
|
|
|
|
{
|
|
|
|
// TODO: free resources
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-11 16:44:23 -05:00
|
|
|
/*
|
|
|
|
config descriptor from a Multi-TT hub
|
|
|
|
09 02 29 00 01 01 00 E0 32
|
|
|
|
09 04 00 00 01 09 00 01 00
|
|
|
|
07 05 81 03 01 00 0C
|
|
|
|
09 04 00 01 01 09 00 02 00
|
|
|
|
07 05 81 03 01 00 0C
|
|
|
|
*/
|
|
|
|
|
|
|
|
|