1 | /*
|
---|
2 | * Copyright (c) 2017 Michal Staruch
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup drvusbxhci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief The roothub structures abstraction.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <usb/request.h>
|
---|
39 | #include <usb/debug.h>
|
---|
40 | #include <usb/host/utils/malloc32.h>
|
---|
41 | #include <usb/host/bus.h>
|
---|
42 | #include <usb/host/ddf_helpers.h>
|
---|
43 | #include <usb/host/hcd.h>
|
---|
44 |
|
---|
45 | #include "debug.h"
|
---|
46 | #include "commands.h"
|
---|
47 | #include "endpoint.h"
|
---|
48 | #include "hc.h"
|
---|
49 | #include "hw_struct/trb.h"
|
---|
50 | #include "rh.h"
|
---|
51 | #include "transfers.h"
|
---|
52 |
|
---|
53 | /* This mask only lists registers, which imply port change. */
|
---|
54 | static const uint32_t port_change_mask =
|
---|
55 | XHCI_REG_MASK(XHCI_PORT_CSC) |
|
---|
56 | XHCI_REG_MASK(XHCI_PORT_PEC) |
|
---|
57 | XHCI_REG_MASK(XHCI_PORT_WRC) |
|
---|
58 | XHCI_REG_MASK(XHCI_PORT_OCC) |
|
---|
59 | XHCI_REG_MASK(XHCI_PORT_PRC) |
|
---|
60 | XHCI_REG_MASK(XHCI_PORT_PLC) |
|
---|
61 | XHCI_REG_MASK(XHCI_PORT_CEC);
|
---|
62 |
|
---|
63 | int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc, ddf_dev_t *device)
|
---|
64 | {
|
---|
65 | assert(rh);
|
---|
66 | assert(hc);
|
---|
67 |
|
---|
68 | rh->hc = hc;
|
---|
69 | rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
|
---|
70 | rh->devices = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
|
---|
71 | rh->hc_device = device;
|
---|
72 |
|
---|
73 | const int err = device_init(&rh->device.base);
|
---|
74 | if (err)
|
---|
75 | return err;
|
---|
76 |
|
---|
77 | /* Initialize route string */
|
---|
78 | rh->device.route_str = 0;
|
---|
79 | rh->device.tier = 0;
|
---|
80 |
|
---|
81 | return EOK;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Create a device node for device directly connected to RH.
|
---|
85 | */
|
---|
86 | static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
87 | {
|
---|
88 | int err;
|
---|
89 | assert(rh);
|
---|
90 | assert(rh->hc_device);
|
---|
91 |
|
---|
92 | assert(rh->devices[port_id - 1] == NULL);
|
---|
93 |
|
---|
94 | xhci_bus_t *bus = &rh->hc->bus;
|
---|
95 |
|
---|
96 | device_t *dev = hcd_ddf_device_create(rh->hc_device, bus->base.device_size);
|
---|
97 | if (!dev) {
|
---|
98 | usb_log_error("Failed to create USB device function.");
|
---|
99 | return ENOMEM;
|
---|
100 | }
|
---|
101 |
|
---|
102 | const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
|
---|
103 | xhci_device_t *xhci_dev = xhci_device_get(dev);
|
---|
104 | xhci_dev->usb3 = port_speed->major == 3;
|
---|
105 | xhci_dev->rh_port = port_id;
|
---|
106 |
|
---|
107 | dev->hub = &rh->device.base;
|
---|
108 | dev->port = port_id;
|
---|
109 | dev->speed = port_speed->usb_speed;
|
---|
110 |
|
---|
111 | if ((err = xhci_bus_enumerate_device(bus, rh->hc, dev))) {
|
---|
112 | usb_log_error("Failed to enumerate USB device: %s", str_error(err));
|
---|
113 | return err;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (!ddf_fun_get_name(dev->fun)) {
|
---|
117 | device_set_default_name(dev);
|
---|
118 | }
|
---|
119 |
|
---|
120 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
121 | usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
|
---|
122 | goto err_usb_dev;
|
---|
123 | }
|
---|
124 |
|
---|
125 | fibril_mutex_lock(&rh->device.base.guard);
|
---|
126 | list_append(&dev->link, &rh->device.base.devices);
|
---|
127 | rh->devices[port_id - 1] = xhci_dev;
|
---|
128 | fibril_mutex_unlock(&rh->device.base.guard);
|
---|
129 |
|
---|
130 | return EOK;
|
---|
131 |
|
---|
132 | err_usb_dev:
|
---|
133 | hcd_ddf_device_destroy(dev);
|
---|
134 | return err;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
138 | {
|
---|
139 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
|
---|
140 |
|
---|
141 | uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
|
---|
142 | const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
|
---|
143 |
|
---|
144 | usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
|
---|
145 |
|
---|
146 | if (speed->major == 3) {
|
---|
147 | if (link_state == 0) {
|
---|
148 | /* USB3 is automatically advanced to enabled. */
|
---|
149 | return rh_setup_device(rh, port_id);
|
---|
150 | }
|
---|
151 | else if (link_state == 5) {
|
---|
152 | /* USB 3 failed to enable. */
|
---|
153 | usb_log_error("USB 3 port couldn't be enabled.");
|
---|
154 | return EAGAIN;
|
---|
155 | }
|
---|
156 | else {
|
---|
157 | usb_log_error("USB 3 port is in invalid state %u.", link_state);
|
---|
158 | return EINVAL;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | else {
|
---|
162 | usb_log_debug("USB 2 device attached, issuing reset.");
|
---|
163 | xhci_rh_reset_port(rh, port_id);
|
---|
164 | /*
|
---|
165 | FIXME: we need to wait for the event triggered by the reset
|
---|
166 | and then alloc_dev()... can't it be done directly instead of
|
---|
167 | going around?
|
---|
168 | */
|
---|
169 | return EOK;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** Deal with a detached device.
|
---|
174 | */
|
---|
175 | static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
176 | {
|
---|
177 | assert(rh);
|
---|
178 | int err;
|
---|
179 |
|
---|
180 | /* Find XHCI device by the port. */
|
---|
181 | xhci_device_t *dev = rh->devices[port_id - 1];
|
---|
182 | if (!dev) {
|
---|
183 | /* Must be extraneous call. */
|
---|
184 | return EOK;
|
---|
185 | }
|
---|
186 |
|
---|
187 | usb_log_info("Device '%s' at port %u has been disconnected.", ddf_fun_get_name(dev->base.fun), port_id);
|
---|
188 |
|
---|
189 | /* Mark the device as detached. */
|
---|
190 | fibril_mutex_lock(&dev->base.guard);
|
---|
191 | dev->detached = true;
|
---|
192 | fibril_mutex_unlock(&dev->base.guard);
|
---|
193 |
|
---|
194 | fibril_mutex_lock(&rh->device.base.guard);
|
---|
195 | list_remove(&dev->base.link);
|
---|
196 | rh->devices[port_id - 1] = NULL;
|
---|
197 | fibril_mutex_unlock(&rh->device.base.guard);
|
---|
198 |
|
---|
199 | /* Remove device from XHCI bus. */
|
---|
200 | if ((err = xhci_bus_remove_device(&rh->hc->bus, rh->hc, &dev->base))) {
|
---|
201 | usb_log_warning("Failed to remove device '%s' from XHCI bus: %s",
|
---|
202 | ddf_fun_get_name(dev->base.fun), str_error(err));
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* Destroy DDF device. */
|
---|
206 | hcd_ddf_device_destroy(&dev->base);
|
---|
207 |
|
---|
208 | /* TODO: Figure out what was forgotten and free that as well. */
|
---|
209 |
|
---|
210 | return EOK;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Handle an incoming Port Change Detected Event.
|
---|
214 | */
|
---|
215 | int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
216 | {
|
---|
217 | uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
|
---|
218 | usb_log_debug("Port status change event detected for port %u.", port_id);
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * We can't be sure that the port change this event announces is the
|
---|
222 | * only port change that happened (see section 4.19.2 of the xHCI
|
---|
223 | * specification). Therefore, we just check all ports for changes.
|
---|
224 | */
|
---|
225 | xhci_rh_handle_port_change(&hc->rh);
|
---|
226 |
|
---|
227 | return EOK;
|
---|
228 | }
|
---|
229 |
|
---|
230 | void xhci_rh_handle_port_change(xhci_rh_t *rh)
|
---|
231 | {
|
---|
232 | for (uint8_t i = 1; i <= rh->max_ports; ++i) {
|
---|
233 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
|
---|
234 |
|
---|
235 | uint32_t events = XHCI_REG_RD_FIELD(®s->portsc, 32);
|
---|
236 | XHCI_REG_WR_FIELD(®s->portsc, events, 32);
|
---|
237 |
|
---|
238 | events &= port_change_mask;
|
---|
239 |
|
---|
240 | if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
|
---|
241 | usb_log_info("Connected state changed on port %u.", i);
|
---|
242 | events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
|
---|
243 |
|
---|
244 | bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
|
---|
245 | if (connected) {
|
---|
246 | handle_connected_device(rh, i);
|
---|
247 | } else {
|
---|
248 | handle_disconnected_device(rh, i);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
|
---|
253 | usb_log_info("Port enabled changed on port %u.", i);
|
---|
254 | events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
|
---|
258 | usb_log_info("Warm port reset on port %u completed.", i);
|
---|
259 | events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
|
---|
263 | usb_log_info("Over-current change on port %u.", i);
|
---|
264 | events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
|
---|
265 | }
|
---|
266 |
|
---|
267 | if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
|
---|
268 | usb_log_info("Port reset on port %u completed.", i);
|
---|
269 | events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
|
---|
270 |
|
---|
271 | const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
|
---|
272 | if (speed->major != 3) {
|
---|
273 | /* FIXME: We probably don't want to do this
|
---|
274 | * every time USB2 port is reset. This is a
|
---|
275 | * temporary workaround. */
|
---|
276 | rh_setup_device(rh, i);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
|
---|
281 | usb_log_info("Port link state changed on port %u.", i);
|
---|
282 | events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
|
---|
286 | usb_log_info("Port %u failed to configure link.", i);
|
---|
287 | events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (events) {
|
---|
291 | usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Theory:
|
---|
297 | *
|
---|
298 | * Although more events could have happened while processing, the PCD
|
---|
299 | * bit in USBSTS will be set on every change. Because the PCD is
|
---|
300 | * cleared even before the interrupt is cleared, it is safe to assume
|
---|
301 | * that this handler will be called again.
|
---|
302 | *
|
---|
303 | * But because we could have handled the event in previous run of this
|
---|
304 | * handler, it is not an error when no event is detected.
|
---|
305 | *
|
---|
306 | * Reality:
|
---|
307 | *
|
---|
308 | * The PCD bit is never set. TODO Check why the interrupt never carries
|
---|
309 | * the PCD flag. Possibly repeat the checking until we're sure the
|
---|
310 | * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
|
---|
311 | */
|
---|
312 | }
|
---|
313 |
|
---|
314 | static inline int get_hub_available_bandwidth(xhci_hc_t *hc, xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx) {
|
---|
315 | // TODO: find a correct place for this function + API
|
---|
316 | // We need speed, because a root hub device has both USB 2 and USB 3 speeds
|
---|
317 | // and the command can query only one of them
|
---|
318 | // ctx is an out parameter as of now
|
---|
319 | assert(dev);
|
---|
320 | assert(ctx);
|
---|
321 |
|
---|
322 | xhci_port_bandwidth_ctx_t *in_ctx = malloc32(sizeof(xhci_port_bandwidth_ctx_t));
|
---|
323 | if (!in_ctx) {
|
---|
324 | return ENOMEM;
|
---|
325 | }
|
---|
326 |
|
---|
327 | xhci_cmd_t cmd;
|
---|
328 | xhci_cmd_init(&cmd, XHCI_CMD_GET_PORT_BANDWIDTH);
|
---|
329 |
|
---|
330 | cmd.bandwidth_ctx = in_ctx;
|
---|
331 | cmd.device_speed = speed;
|
---|
332 |
|
---|
333 | int err;
|
---|
334 | if ((err = xhci_cmd_sync(hc, &cmd))) {
|
---|
335 | goto end;
|
---|
336 | }
|
---|
337 |
|
---|
338 | memcpy(ctx, in_ctx, sizeof(xhci_port_bandwidth_ctx_t));
|
---|
339 |
|
---|
340 | end:
|
---|
341 | xhci_cmd_fini(&cmd);
|
---|
342 | return EOK;
|
---|
343 | }
|
---|
344 |
|
---|
345 | const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
|
---|
346 | {
|
---|
347 | xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
|
---|
348 |
|
---|
349 | unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
|
---|
350 | return &rh->hc->speeds[psiv];
|
---|
351 | }
|
---|
352 |
|
---|
353 | int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
|
---|
354 | {
|
---|
355 | usb_log_debug2("Resetting port %u.", port);
|
---|
356 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
|
---|
357 | XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
|
---|
358 |
|
---|
359 | return EOK;
|
---|
360 | }
|
---|
361 |
|
---|
362 | int xhci_rh_fini(xhci_rh_t *rh)
|
---|
363 | {
|
---|
364 | /* TODO: Implement me! */
|
---|
365 | usb_log_debug2("Called xhci_rh_fini().");
|
---|
366 |
|
---|
367 | free(rh->devices);
|
---|
368 |
|
---|
369 | return EOK;
|
---|
370 | }
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * @}
|
---|
374 | */
|
---|