2010-08-13 11:18:58 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Patrick Georgi
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define USB_DEBUG
|
|
|
|
|
|
|
|
#include <libpayload.h>
|
|
|
|
#include "ohci_private.h"
|
|
|
|
#include "ohci.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int numports;
|
|
|
|
int *port;
|
|
|
|
} rh_inst_t;
|
|
|
|
|
|
|
|
#define RH_INST(dev) ((rh_inst_t*)(dev)->data)
|
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_rh_enable_port(usbdev_t *dev, int port)
|
2010-08-13 11:18:58 +02:00
|
|
|
{
|
2012-05-21 14:46:26 +02:00
|
|
|
/* Reset RH port should hold 50ms with pulses of at least 10ms and
|
|
|
|
* gaps of at most 3ms (usb20 spec 7.1.7.5).
|
|
|
|
* After reset, the port will be enabled automatically (ohci spec
|
|
|
|
* 7.4.4).
|
|
|
|
*/
|
2012-11-23 11:52:18 +01:00
|
|
|
int total_delay = 100; /* 100 * 500us == 50ms */
|
|
|
|
while (total_delay > 0) {
|
2012-05-21 14:46:26 +02:00
|
|
|
if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port]
|
|
|
|
& CurrentConnectStatus))
|
|
|
|
return;
|
2010-08-13 11:18:58 +02:00
|
|
|
|
2012-05-21 14:46:26 +02:00
|
|
|
/* start reset */
|
2022-08-10 08:59:18 +02:00
|
|
|
OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] =
|
2012-05-21 14:46:26 +02:00
|
|
|
SetPortReset;
|
|
|
|
int timeout = 200; /* timeout after 200 * 500us == 100ms */
|
2022-08-10 08:59:18 +02:00
|
|
|
while ((OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port]
|
2012-05-21 14:46:26 +02:00
|
|
|
& PortResetStatus)
|
|
|
|
&& timeout--) {
|
2012-11-23 11:52:18 +01:00
|
|
|
udelay(500); total_delay--;
|
2012-05-21 14:46:26 +02:00
|
|
|
}
|
2022-08-10 08:59:18 +02:00
|
|
|
if (OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port]
|
2012-05-21 14:46:26 +02:00
|
|
|
& PortResetStatus) {
|
2012-11-01 23:44:10 +01:00
|
|
|
usb_debug("Warning: root-hub port reset timed out.\n");
|
2012-05-21 14:46:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((200-timeout) < 20)
|
2012-11-01 23:44:10 +01:00
|
|
|
usb_debug("Warning: port reset too short: %dms; "
|
2012-05-21 14:46:26 +02:00
|
|
|
"should be at least 10ms.\n",
|
|
|
|
(200-timeout)/2);
|
|
|
|
/* clear reset status change */
|
2022-08-10 08:59:18 +02:00
|
|
|
OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] =
|
2012-05-21 14:46:26 +02:00
|
|
|
PortResetStatusChange;
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("rh port reset finished after %dms.\n", (200-timeout)/2);
|
2012-05-21 14:46:26 +02:00
|
|
|
}
|
2010-08-13 11:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* disable root hub */
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_rh_disable_port(usbdev_t *dev, int port)
|
2010-08-13 11:18:58 +02:00
|
|
|
{
|
2022-08-10 08:59:18 +02:00
|
|
|
if (RH_INST(dev)->port[port] != -1) {
|
|
|
|
usb_detach_device(dev->controller, RH_INST(dev)->port[port]);
|
|
|
|
RH_INST(dev)->port[port] = -1;
|
2014-04-08 21:54:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] = ClearPortEnable; // disable port
|
2012-05-21 14:46:26 +02:00
|
|
|
int timeout = 50; /* timeout after 50 * 100us == 5ms */
|
2022-08-10 08:59:18 +02:00
|
|
|
while ((OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port]
|
2012-05-21 14:46:26 +02:00
|
|
|
& PortEnableStatus)
|
|
|
|
&& timeout--) {
|
|
|
|
udelay(100);
|
|
|
|
}
|
2010-08-13 11:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_rh_scanport(usbdev_t *dev, int port)
|
2010-08-13 11:18:58 +02:00
|
|
|
{
|
|
|
|
if (port >= RH_INST(dev)->numports) {
|
2012-11-01 23:44:10 +01:00
|
|
|
usb_debug("Invalid port %d\n", port);
|
2010-08-13 11:18:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* device registered, and device change logged, so something must have happened */
|
2022-08-10 08:59:18 +02:00
|
|
|
if (RH_INST(dev)->port[port] != -1) {
|
|
|
|
usb_detach_device(dev->controller, RH_INST(dev)->port[port]);
|
|
|
|
RH_INST(dev)->port[port] = -1;
|
2010-08-13 11:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* no device attached
|
|
|
|
previously registered devices are detached, nothing left to do */
|
|
|
|
if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & CurrentConnectStatus))
|
|
|
|
return;
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] = ConnectStatusChange; // clear port state change
|
|
|
|
ohci_rh_enable_port(dev, port);
|
2010-08-13 11:18:58 +02:00
|
|
|
|
|
|
|
mdelay(100); // wait for signal to stabilize
|
|
|
|
|
|
|
|
if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & PortEnableStatus)) {
|
2022-08-10 08:59:18 +02:00
|
|
|
usb_debug("port enable failed\n");
|
2010-08-13 11:18:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 05:03:54 +02:00
|
|
|
usb_speed speed = (OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & LowSpeedDeviceAttached) != 0;
|
2022-08-10 08:59:18 +02:00
|
|
|
RH_INST(dev)->port[port] = usb_attach_device(dev->controller, dev->address, port, speed);
|
2010-08-13 11:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_rh_report_port_changes(usbdev_t *dev)
|
2010-08-13 11:18:58 +02:00
|
|
|
{
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_t *const ohcic = OHCI_INST(dev->controller);
|
2010-08-13 11:18:58 +02:00
|
|
|
|
2012-11-22 15:45:48 +01:00
|
|
|
int i;
|
2010-08-13 11:18:58 +02:00
|
|
|
|
|
|
|
for (i = 0; i < RH_INST(dev)->numports; i++) {
|
|
|
|
// maybe detach+attach happened between two scans?
|
2012-11-22 15:45:48 +01:00
|
|
|
if (ohcic->opreg->HcRhPortStatus[i] & ConnectStatusChange) {
|
|
|
|
ohcic->opreg->HcRhPortStatus[i] = ConnectStatusChange;
|
2012-11-01 23:44:10 +01:00
|
|
|
usb_debug("attachment change on port %d\n", i);
|
2010-08-13 11:18:58 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no change
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_rh_destroy(usbdev_t *dev)
|
2010-08-13 11:18:58 +02:00
|
|
|
{
|
|
|
|
int i;
|
2022-08-10 08:59:18 +02:00
|
|
|
for (i = 0; i < RH_INST(dev)->numports; i++)
|
|
|
|
ohci_rh_disable_port(dev, i);
|
|
|
|
free(RH_INST(dev));
|
2010-08-13 11:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_rh_poll(usbdev_t *dev)
|
2010-08-13 11:18:58 +02:00
|
|
|
{
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_t *const ohcic = OHCI_INST(dev->controller);
|
2012-11-22 15:45:48 +01:00
|
|
|
|
2010-08-13 11:18:58 +02:00
|
|
|
int port;
|
2012-11-22 15:45:48 +01:00
|
|
|
|
|
|
|
/* Check if anything changed. */
|
|
|
|
if (!(ohcic->opreg->HcInterruptStatus & RootHubStatusChange))
|
|
|
|
return;
|
|
|
|
ohcic->opreg->HcInterruptStatus = RootHubStatusChange;
|
|
|
|
usb_debug("root hub status change\n");
|
|
|
|
|
|
|
|
/* Scan ports with changed connection status. */
|
2022-08-10 08:59:18 +02:00
|
|
|
while ((port = ohci_rh_report_port_changes(dev)) != -1)
|
|
|
|
ohci_rh_scanport(dev, port);
|
2010-08-13 11:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-08-10 08:59:18 +02:00
|
|
|
ohci_rh_init(usbdev_t *dev)
|
2010-08-13 11:18:58 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dev->destroy = ohci_rh_destroy;
|
|
|
|
dev->poll = ohci_rh_poll;
|
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
dev->data = xmalloc(sizeof(rh_inst_t));
|
|
|
|
RH_INST(dev)->numports = OHCI_INST(dev->controller)->opreg->HcRhDescriptorA & NumberDownstreamPortsMask;
|
|
|
|
RH_INST(dev)->port = xmalloc(sizeof(int) * RH_INST(dev)->numports);
|
|
|
|
usb_debug("%d ports registered\n", RH_INST(dev)->numports);
|
2010-08-13 11:18:58 +02:00
|
|
|
|
2022-08-10 08:59:18 +02:00
|
|
|
for (i = 0; i < RH_INST(dev)->numports; i++) {
|
|
|
|
ohci_rh_enable_port(dev, i);
|
|
|
|
RH_INST(dev)->port[i] = -1;
|
2010-08-13 11:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we can set them here because a root hub _really_ shouldn't
|
|
|
|
appear elsewhere */
|
|
|
|
dev->address = 0;
|
|
|
|
dev->hub = -1;
|
|
|
|
dev->port = -1;
|
|
|
|
|
2012-11-01 23:44:10 +01:00
|
|
|
usb_debug("rh init done\n");
|
2010-08-13 11:18:58 +02:00
|
|
|
}
|