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-02-27 05:22:02 -05:00
|
|
|
USBHub::USBHub() : /* mytimer(this), */ othertimer(this)
|
2017-02-11 09:03:54 -05:00
|
|
|
{
|
|
|
|
// TODO: free Device_t, Pipe_t & Transfer_t we will need
|
|
|
|
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-18 15:03:10 -05:00
|
|
|
println("USBHub claim_device this=", (uint32_t)this, HEX);
|
2017-02-11 09:03:54 -05:00
|
|
|
|
2017-02-27 05:22:02 -05:00
|
|
|
// timer testing TODO: remove this later
|
|
|
|
mytimer.init(this);
|
2017-02-27 07:17:43 -05:00
|
|
|
mytimer.pointer = (void *)"This is mytimer";
|
2017-02-27 05:22:02 -05:00
|
|
|
mytimer.start(99129);
|
2017-02-27 07:17:43 -05:00
|
|
|
othertimer.pointer = (void *)"Hello, I'm othertimer";
|
2017-02-27 05:22:02 -05:00
|
|
|
othertimer.start(12345);
|
2017-02-27 07:17:43 -05:00
|
|
|
for (int i=0; i < 7; i++) {
|
|
|
|
mytimers[i].init(this);
|
|
|
|
//mytimers[i].start((i + 1) * 10000);
|
|
|
|
}
|
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-11 09:03:54 -05:00
|
|
|
|
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-12 15:03:26 -05:00
|
|
|
changepipe = NULL;
|
|
|
|
changebits = 0;
|
|
|
|
state = 0;
|
2017-02-17 20:44:24 -05:00
|
|
|
memset(portstatus, 0, sizeof(portstatus));
|
|
|
|
memset(portstate, 0, sizeof(portstate));
|
2017-02-12 15:03:26 -05:00
|
|
|
|
2017-02-12 16:12:06 -05:00
|
|
|
mk_setup(setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
|
2017-02-12 21:00:48 -05:00
|
|
|
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
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 15:03:26 -05:00
|
|
|
void USBHub::poweron(uint32_t port)
|
2017-02-11 16:44:23 -05:00
|
|
|
{
|
2017-02-12 16:12:06 -05:00
|
|
|
mk_setup(setup, 0x23, 3, 8, port, 0);
|
2017-02-12 21:00:48 -05:00
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
2017-02-12 15:03:26 -05:00
|
|
|
}
|
2017-02-12 04:42:30 -05:00
|
|
|
|
2017-02-13 15:56:01 -05:00
|
|
|
void USBHub::getstatus(uint32_t port)
|
|
|
|
{
|
|
|
|
if (port == 0) {
|
|
|
|
mk_setup(setup, 0xA0, 0, 0, port, 4); // get hub status
|
|
|
|
} else {
|
|
|
|
mk_setup(setup, 0xA3, 0, 0, port, 4); // get port status
|
|
|
|
}
|
2017-02-17 20:44:24 -05:00
|
|
|
queue_Control_Transfer(device, &setup, &statusbits, this);
|
2017-02-13 15:56:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void USBHub::clearstatus(uint32_t port)
|
|
|
|
{
|
|
|
|
if (port == 0) {
|
|
|
|
mk_setup(setup, 0x20, 1, 0x10, port, 0); // clear hub status
|
|
|
|
} else {
|
|
|
|
mk_setup(setup, 0x23, 1, 0x10, port, 0); // clear port status
|
|
|
|
}
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
|
|
|
}
|
|
|
|
|
2017-02-17 20:44:24 -05:00
|
|
|
void USBHub::reset(uint32_t port)
|
|
|
|
{
|
|
|
|
mk_setup(setup, 0x23, 3, 4, port, 0); // set feature PORT_RESET
|
|
|
|
queue_Control_Transfer(device, &setup, NULL, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-12 15:03:26 -05:00
|
|
|
if (state == 0) {
|
|
|
|
// read hub descriptor to learn hub's capabilities
|
|
|
|
// Hub Descriptor, USB 2.0, 11.23.2.1 page 417
|
|
|
|
if (hub_desc[0] == 9 && hub_desc[1] == 0x29) {
|
|
|
|
numports = hub_desc[2];
|
|
|
|
characteristics = hub_desc[3];
|
|
|
|
powertime = hub_desc[5];
|
|
|
|
// TODO: do we need to use the DeviceRemovable
|
|
|
|
// bits to mke synthetic device connect events?
|
2017-02-18 15:03:10 -05:00
|
|
|
print("Hub has ");
|
|
|
|
print(numports);
|
|
|
|
println(" ports");
|
2017-02-12 15:03:26 -05:00
|
|
|
state = 1;
|
|
|
|
poweron(1);
|
2017-02-12 04:42:30 -05:00
|
|
|
}
|
2017-02-12 15:03:26 -05:00
|
|
|
} else if (state < numports) {
|
|
|
|
// turn on power to all ports
|
|
|
|
poweron(++state);
|
|
|
|
} else if (state == numports) {
|
2017-02-18 15:03:10 -05:00
|
|
|
println("power turned on to all ports");
|
|
|
|
println("device addr = ", device->address);
|
2017-02-26 12:40:49 -05:00
|
|
|
// TODO: use hub's interrupt endpoint interval
|
|
|
|
changepipe = new_Pipe(device, 3, endpoint, 1, 1, 64);
|
2017-02-18 15:03:10 -05:00
|
|
|
println("pipe cap1 = ", changepipe->qh.capabilities[0], HEX);
|
2017-02-13 09:05:09 -05:00
|
|
|
changepipe->callback_function = callback;
|
|
|
|
queue_Data_Transfer(changepipe, &changebits, 1, this);
|
2017-02-12 15:03:26 -05:00
|
|
|
state = 255;
|
|
|
|
} else if (state == 255) {
|
2017-02-13 15:56:01 -05:00
|
|
|
// up and running...
|
|
|
|
switch (setup.word1) {
|
|
|
|
case 0x000000A0: // get hub status
|
2017-02-18 15:03:10 -05:00
|
|
|
println("New Hub Status");
|
2017-02-13 15:56:01 -05:00
|
|
|
clearstatus(0);
|
2017-02-13 16:11:54 -05:00
|
|
|
return;
|
2017-02-13 15:56:01 -05:00
|
|
|
case 0x000000A3: // get port status
|
2017-02-17 20:44:24 -05:00
|
|
|
new_port_status(setup.wIndex, statusbits);
|
2017-02-13 15:56:01 -05:00
|
|
|
clearstatus(setup.wIndex);
|
2017-02-13 16:11:54 -05:00
|
|
|
return;
|
2017-02-13 15:56:01 -05:00
|
|
|
case 0x00100120: // clear hub status
|
2017-02-18 15:03:10 -05:00
|
|
|
println("Hub Status Cleared");
|
2017-02-13 15:56:01 -05:00
|
|
|
changebits &= ~1;
|
|
|
|
break;
|
|
|
|
case 0x00100123: // clear port status
|
2017-02-18 15:03:10 -05:00
|
|
|
println("Port Status Cleared, port=", setup.wIndex);
|
2017-02-13 15:56:01 -05:00
|
|
|
changebits &= ~(1 << setup.wIndex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
update_status();
|
2017-02-12 04:42:30 -05:00
|
|
|
}
|
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-18 15:03:10 -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-13 09:05:09 -05:00
|
|
|
// TODO: do something with the status change info
|
2017-02-13 15:56:01 -05:00
|
|
|
update_status();
|
2017-02-13 09:05:09 -05:00
|
|
|
queue_Data_Transfer(changepipe, &changebits, 1, this);
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:56:01 -05:00
|
|
|
void USBHub::update_status()
|
|
|
|
{
|
|
|
|
uint32_t i, mask;
|
|
|
|
|
|
|
|
for (i=0, mask=1; i <= numports; i++, mask <<= 1) {
|
|
|
|
if (changebits & mask) {
|
|
|
|
getstatus(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
if (port < 1 || port > 7) return;
|
|
|
|
uint32_t priorstatus = portstatus[port - 1];
|
|
|
|
portstatus[port] = status;
|
|
|
|
|
2017-02-18 15:03:10 -05:00
|
|
|
print("New Port Status, port=");
|
|
|
|
print(port);
|
|
|
|
print(", status=");
|
|
|
|
println(status, HEX);
|
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-02-18 15:03:10 -05:00
|
|
|
println(" 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-17 20:44:24 -05:00
|
|
|
|
|
|
|
if ((status & 0x0001) && !(priorstatus & 0x0001)) {
|
2017-02-18 15:03:10 -05:00
|
|
|
println(" connect");
|
2017-02-17 20:44:24 -05:00
|
|
|
// 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
|
|
|
|
delay(100); // TODO: horribly bad... need timing events
|
|
|
|
reset(port);
|
|
|
|
// TODO... reset timer?
|
|
|
|
|
|
|
|
} else if (!(status & 0x0001) && (priorstatus & 0x0001)) {
|
2017-02-18 15:03:10 -05:00
|
|
|
println(" disconnect");
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|