| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <libpayload.h> |
| 32 |
#include "ohci_private.h" |
| 33 |
#include "ohci.h" |
| 34 |
|
| 35 |
typedef struct { |
| 36 |
int numports; |
| 37 |
int *port; |
| 38 |
} rh_inst_t; |
| 39 |
|
| 40 |
#define RH_INST(dev) ((rh_inst_t*)(dev)->data) |
| 41 |
|
| 42 |
static void |
| 43 |
ohci_rh_enable_port(usbdev_t *dev, int port) |
| 44 |
{ |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
int total_delay = 100; |
| 51 |
while (total_delay > 0) { |
| 52 |
if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] |
| 53 |
& CurrentConnectStatus)) |
| 54 |
return; |
| 55 |
|
| 56 |
|
| 57 |
OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] = |
| 58 |
SetPortReset; |
| 59 |
int timeout = 200; |
| 60 |
while ((OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] |
| 61 |
& PortResetStatus) |
| 62 |
&& timeout--) { |
| 63 |
udelay(500); total_delay--; |
| 64 |
} |
| 65 |
if (OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] |
| 66 |
& PortResetStatus) { |
| 67 |
usb_debug("Warning: root-hub port reset timed out.\n"); |
| 68 |
break; |
| 69 |
} |
| 70 |
if ((200-timeout) < 20) |
| 71 |
usb_debug("Warning: port reset too short: %dms; " |
| 72 |
"should be at least 10ms.\n", |
| 73 |
(200-timeout)/2); |
| 74 |
|
| 75 |
OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] = |
| 76 |
PortResetStatusChange; |
| 77 |
usb_debug("rh port reset finished after %dms.\n", (200-timeout)/2); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
static void |
| 83 |
ohci_rh_disable_port(usbdev_t *dev, int port) |
| 84 |
{ |
| 85 |
if (RH_INST(dev)->port[port] != -1) { |
| 86 |
usb_detach_device(dev->controller, RH_INST(dev)->port[port]); |
| 87 |
RH_INST(dev)->port[port] = -1; |
| 88 |
} |
| 89 |
|
| 90 |
OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] = ClearPortEnable; |
| 91 |
int timeout = 50; |
| 92 |
while ((OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] |
| 93 |
& PortEnableStatus) |
| 94 |
&& timeout--) { |
| 95 |
udelay(100); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
ohci_rh_scanport(usbdev_t *dev, int port) |
| 101 |
{ |
| 102 |
if (port >= RH_INST(dev)->numports) { |
| 103 |
usb_debug("Invalid port %d\n", port); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
if (RH_INST(dev)->port[port] != -1) { |
| 109 |
usb_detach_device(dev->controller, RH_INST(dev)->port[port]); |
| 110 |
RH_INST(dev)->port[port] = -1; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & CurrentConnectStatus)) |
| 116 |
return; |
| 117 |
|
| 118 |
OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] = ConnectStatusChange; |
| 119 |
ohci_rh_enable_port(dev, port); |
| 120 |
|
| 121 |
mdelay(100); |
| 122 |
|
| 123 |
if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & PortEnableStatus)) { |
| 124 |
usb_debug("port enable failed\n"); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
usb_speed speed = (OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & LowSpeedDeviceAttached) != 0; |
| 129 |
RH_INST(dev)->port[port] = usb_attach_device(dev->controller, dev->address, port, speed); |
| 130 |
} |
| 131 |
|
| 132 |
static int |
| 133 |
ohci_rh_report_port_changes(usbdev_t *dev) |
| 134 |
{ |
| 135 |
ohci_t *const ohcic = OHCI_INST(dev->controller); |
| 136 |
|
| 137 |
int i; |
| 138 |
|
| 139 |
for (i = 0; i < RH_INST(dev)->numports; i++) { |
| 140 |
|
| 141 |
if (ohcic->opreg->HcRhPortStatus[i] & ConnectStatusChange) { |
| 142 |
ohcic->opreg->HcRhPortStatus[i] = ConnectStatusChange; |
| 143 |
usb_debug("attachment change on port %d\n", i); |
| 144 |
return i; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
return -1; |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
ohci_rh_destroy(usbdev_t *dev) |
| 154 |
{ |
| 155 |
int i; |
| 156 |
for (i = 0; i < RH_INST(dev)->numports; i++) |
| 157 |
ohci_rh_disable_port(dev, i); |
| 158 |
free(RH_INST(dev)); |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
ohci_rh_poll(usbdev_t *dev) |
| 163 |
{ |
| 164 |
ohci_t *const ohcic = OHCI_INST(dev->controller); |
| 165 |
|
| 166 |
int port; |
| 167 |
|
| 168 |
|
| 169 |
if (!(ohcic->opreg->HcInterruptStatus & RootHubStatusChange)) |
| 170 |
return; |
| 171 |
ohcic->opreg->HcInterruptStatus = RootHubStatusChange; |
| 172 |
usb_debug("root hub status change\n"); |
| 173 |
|
| 174 |
|
| 175 |
while ((port = ohci_rh_report_port_changes(dev)) != -1) |
| 176 |
ohci_rh_scanport(dev, port); |
| 177 |
} |
| 178 |
|
| 179 |
void |
| 180 |
ohci_rh_init(usbdev_t *dev) |
| 181 |
{ |
| 182 |
int i; |
| 183 |
|
| 184 |
dev->destroy = ohci_rh_destroy; |
| 185 |
dev->poll = ohci_rh_poll; |
| 186 |
|
| 187 |
dev->data = xmalloc(sizeof(rh_inst_t)); |
| 188 |
RH_INST(dev)->numports = OHCI_INST(dev->controller)->opreg->HcRhDescriptorA & NumberDownstreamPortsMask; |
| 189 |
RH_INST(dev)->port = xmalloc(sizeof(int) * RH_INST(dev)->numports); |
| 190 |
usb_debug("%d ports registered\n", RH_INST(dev)->numports); |
| 191 |
|
| 192 |
for (i = 0; i < RH_INST(dev)->numports; i++) { |
| 193 |
ohci_rh_enable_port(dev, i); |
| 194 |
RH_INST(dev)->port[i] = -1; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
dev->address = 0; |
| 200 |
dev->hub = -1; |
| 201 |
dev->port = -1; |
| 202 |
|
| 203 |
usb_debug("rh init done\n"); |
| 204 |
} |