source: mainline/uspace/drv/bus/usb/xhci/rh.c@ 25251bb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 25251bb was 25251bb, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

xhci: move pointer to hc from device to bus

Also, fixes the bug of hc ptr not set on tier 2+ device.

  • Property mode set to 100644
File size: 12.2 KB
Line 
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. */
54static 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
63int 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 */
86static 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
132err_usb_dev:
133 hcd_ddf_device_destroy(dev);
134 return err;
135}
136
137static 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 */
175static 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 /* Block creation of new endpoints and transfers. */
190 fibril_mutex_lock(&dev->base.guard);
191 dev->online = false;
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 usb_log_debug2("Aborting all active transfers to '%s'.", ddf_fun_get_name(dev->base.fun));
200
201 /* Abort running transfers. */
202 for (size_t i = 0; i < ARRAY_SIZE(dev->endpoints); ++i) {
203 xhci_endpoint_t *ep = dev->endpoints[i];
204 if (!ep || !ep->base.active)
205 continue;
206
207 /* FIXME: This is racy. */
208 if ((err = xhci_transfer_abort(&ep->active_transfer))) {
209 usb_log_warning("Failed to abort active %s transfer to "
210 " endpoint %d of detached device '%s': %s",
211 usb_str_transfer_type(ep->base.transfer_type),
212 ep->base.endpoint, ddf_fun_get_name(dev->base.fun),
213 str_error(err));
214 }
215 }
216
217 /* TODO: Figure out how to handle errors here. So far, they are reported and skipped. */
218 /* TODO: Move parts of the code below to xhci_bus_remove_device() */
219
220 /* Make DDF (and all drivers) forget about the device. */
221 if ((err = ddf_fun_unbind(dev->base.fun))) {
222 usb_log_warning("Failed to unbind DDF function of detached device '%s': %s",
223 ddf_fun_get_name(dev->base.fun), str_error(err));
224 }
225
226 /* Unregister EP0. */
227 if ((err = bus_remove_endpoint(&rh->hc->bus.base, &dev->endpoints[0]->base))) {
228 usb_log_warning("Failed to unregister configuration endpoint of device '%s' from XHCI bus: %s",
229 ddf_fun_get_name(dev->base.fun), str_error(err));
230 }
231
232 /* Deconfigure device. */
233 if ((err = hc_deconfigure_device(rh->hc, dev->slot_id))) {
234 usb_log_warning("Failed to deconfigure detached device '%s': %s",
235 ddf_fun_get_name(dev->base.fun), str_error(err));
236 }
237
238 /* TODO: Free EP0 structures. */
239 /* TODO: Destroy EP0 by removing its last reference. */
240
241 /* Remove device from XHCI bus. */
242 if ((err = xhci_bus_remove_device(&rh->hc->bus, rh->hc, &dev->base))) {
243 usb_log_warning("Failed to remove device '%s' from XHCI bus: %s",
244 ddf_fun_get_name(dev->base.fun), str_error(err));
245 }
246
247 /* Disable device slot. */
248 if ((err = hc_disable_slot(rh->hc, dev->slot_id))) {
249 usb_log_warning("Failed to disable slot for device '%s': %s",
250 ddf_fun_get_name(dev->base.fun), str_error(err));
251 }
252
253 /* Destroy DDF device. */
254 hcd_ddf_device_destroy(&dev->base);
255
256 // TODO: Free device context.
257 // TODO: Free TRB rings.
258 // TODO: Figure out what was forgotten and free that as well.
259
260 return EOK;
261}
262
263/** Handle an incoming Port Change Detected Event.
264 */
265int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
266{
267 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
268 usb_log_debug("Port status change event detected for port %u.", port_id);
269
270 /**
271 * We can't be sure that the port change this event announces is the
272 * only port change that happened (see section 4.19.2 of the xHCI
273 * specification). Therefore, we just check all ports for changes.
274 */
275 xhci_rh_handle_port_change(&hc->rh);
276
277 return EOK;
278}
279
280void xhci_rh_handle_port_change(xhci_rh_t *rh)
281{
282 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
283 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
284
285 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
286 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
287
288 events &= port_change_mask;
289
290 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
291 usb_log_info("Connected state changed on port %u.", i);
292 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
293
294 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
295 if (connected) {
296 handle_connected_device(rh, i);
297 } else {
298 handle_disconnected_device(rh, i);
299 }
300 }
301
302 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
303 usb_log_info("Port enabled changed on port %u.", i);
304 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
305 }
306
307 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
308 usb_log_info("Warm port reset on port %u completed.", i);
309 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
310 }
311
312 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
313 usb_log_info("Over-current change on port %u.", i);
314 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
315 }
316
317 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
318 usb_log_info("Port reset on port %u completed.", i);
319 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
320
321 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
322 if (speed->major != 3) {
323 /* FIXME: We probably don't want to do this
324 * every time USB2 port is reset. This is a
325 * temporary workaround. */
326 rh_setup_device(rh, i);
327 }
328 }
329
330 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
331 usb_log_info("Port link state changed on port %u.", i);
332 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
333 }
334
335 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
336 usb_log_info("Port %u failed to configure link.", i);
337 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
338 }
339
340 if (events) {
341 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
342 }
343 }
344
345 /**
346 * Theory:
347 *
348 * Although more events could have happened while processing, the PCD
349 * bit in USBSTS will be set on every change. Because the PCD is
350 * cleared even before the interrupt is cleared, it is safe to assume
351 * that this handler will be called again.
352 *
353 * But because we could have handled the event in previous run of this
354 * handler, it is not an error when no event is detected.
355 *
356 * Reality:
357 *
358 * The PCD bit is never set. TODO Check why the interrupt never carries
359 * the PCD flag. Possibly repeat the checking until we're sure the
360 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
361 */
362}
363
364static inline int get_hub_available_bandwidth(xhci_hc_t *hc, xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx) {
365 // TODO: find a correct place for this function + API
366 // We need speed, because a root hub device has both USB 2 and USB 3 speeds
367 // and the command can query only one of them
368 // ctx is an out parameter as of now
369 assert(dev);
370 assert(ctx);
371
372 xhci_port_bandwidth_ctx_t *in_ctx = malloc32(sizeof(xhci_port_bandwidth_ctx_t));
373 if (!in_ctx) {
374 return ENOMEM;
375 }
376
377 xhci_cmd_t cmd;
378 xhci_cmd_init(&cmd, XHCI_CMD_GET_PORT_BANDWIDTH);
379
380 cmd.bandwidth_ctx = in_ctx;
381 cmd.device_speed = speed;
382
383 int err;
384 if ((err = xhci_cmd_sync(hc, &cmd))) {
385 goto end;
386 }
387
388 memcpy(ctx, in_ctx, sizeof(xhci_port_bandwidth_ctx_t));
389
390end:
391 xhci_cmd_fini(&cmd);
392 return EOK;
393}
394
395const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
396{
397 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
398
399 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
400 return &rh->hc->speeds[psiv];
401}
402
403int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
404{
405 usb_log_debug2("Resetting port %u.", port);
406 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
407 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
408
409 return EOK;
410}
411
412int xhci_rh_fini(xhci_rh_t *rh)
413{
414 /* TODO: Implement me! */
415 usb_log_debug2("Called xhci_rh_fini().");
416
417 free(rh->devices);
418
419 return EOK;
420}
421
422/**
423 * @}
424 */
Note: See TracBrowser for help on using the repository browser.