source: mainline/uspace/drv/bus/usb/xhci/rh.c@ 53db806

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 53db806 was b80c1ab, checked in by Aearsis <Hlavaty.Ondrej@…>, 8 years ago

xhci: use dma_buffers instead of malloc32 util

A bit of refactoring was needed to adapt scratchpad buffers.

  • Property mode set to 100644
File size: 10.2 KB
RevLine 
[7bd99bf]1/*
[dcf0597]2 * Copyright (c) 2017 Michal Staruch
[7bd99bf]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 */
[d967aa1]35
[7bd99bf]36#include <errno.h>
37#include <str_error.h>
[2770b66]38#include <usb/request.h>
[7bd99bf]39#include <usb/debug.h>
[20eaa82]40#include <usb/host/bus.h>
[867b375]41#include <usb/host/ddf_helpers.h>
[b80c1ab]42#include <usb/host/dma_buffer.h>
[2770b66]43#include <usb/host/hcd.h>
[867b375]44
[7bd99bf]45#include "debug.h"
[174788f]46#include "commands.h"
[370a1c8]47#include "endpoint.h"
[7bd99bf]48#include "hc.h"
49#include "hw_struct/trb.h"
[c8bb7090]50#include "rh.h"
[e9e24f2]51#include "transfers.h"
[7bd99bf]52
[9876e34]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
[0f6b50f]63int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc, ddf_dev_t *device)
[d32d51d]64{
[5c5c9407]65 assert(rh);
[816335c]66 assert(hc);
67
68 rh->hc = hc;
[9876e34]69 rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
[d33dc780]70 rh->devices_by_port = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
[2cf28b9]71 rh->hc_device = device;
[5c5c9407]72
[2cf28b9]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;
[d32d51d]82}
83
[20eaa82]84/** Create a device node for device directly connected to RH.
85 */
[867b375]86static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
87{
[20eaa82]88 int err;
89 assert(rh);
[0f6b50f]90 assert(rh->hc_device);
[20eaa82]91
[d33dc780]92 assert(rh->devices_by_port[port_id - 1] == NULL);
[2cf28b9]93
[20eaa82]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
[0206d35]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;
[2cf28b9]105 xhci_dev->rh_port = port_id;
[0206d35]106
[2cf28b9]107 dev->hub = &rh->device.base;
[20eaa82]108 dev->port = port_id;
[f668d60]109 dev->speed = port_speed->usb_speed;
[20eaa82]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))) {
[9620a54]121 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
122 XHCI_DEV_ARGS(*xhci_dev), str_error(err));
[20eaa82]123 goto err_usb_dev;
124 }
125
[2cf28b9]126 fibril_mutex_lock(&rh->device.base.guard);
127 list_append(&dev->link, &rh->device.base.devices);
[d33dc780]128 rh->devices_by_port[port_id - 1] = xhci_dev;
[2cf28b9]129 fibril_mutex_unlock(&rh->device.base.guard);
[867b375]130
[20eaa82]131 return EOK;
132
133err_usb_dev:
134 hcd_ddf_device_destroy(dev);
135 return err;
[867b375]136}
137
[dcf0597]138static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
[c8bb7090]139{
[dcf0597]140 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
[472235a]141
[dcf0597]142 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
143 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
[7bd99bf]144
[dcf0597]145 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
[7bd99bf]146
[dcf0597]147 if (speed->major == 3) {
148 if (link_state == 0) {
149 /* USB3 is automatically advanced to enabled. */
[867b375]150 return rh_setup_device(rh, port_id);
[dcf0597]151 }
152 else if (link_state == 5) {
153 /* USB 3 failed to enable. */
154 usb_log_error("USB 3 port couldn't be enabled.");
155 return EAGAIN;
156 }
157 else {
158 usb_log_error("USB 3 port is in invalid state %u.", link_state);
159 return EINVAL;
160 }
161 }
162 else {
163 usb_log_debug("USB 2 device attached, issuing reset.");
164 xhci_rh_reset_port(rh, port_id);
165 /*
166 FIXME: we need to wait for the event triggered by the reset
167 and then alloc_dev()... can't it be done directly instead of
168 going around?
169 */
170 return EOK;
171 }
[816335c]172}
173
[f45c78f]174/** Deal with a detached device.
175 */
176static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
177{
[a4e26882]178 assert(rh);
179 int err;
180
[766043c]181 /* Find XHCI device by the port. */
[d33dc780]182 xhci_device_t *dev = rh->devices_by_port[port_id - 1];
[766043c]183 if (!dev) {
[a4e26882]184 /* Must be extraneous call. */
[766043c]185 return EOK;
186 }
187
[9620a54]188 usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
189 XHCI_DEV_ARGS(*dev), port_id);
[a4e26882]190
[40a3bfa]191 /* Mark the device as detached. */
[2cf28b9]192 fibril_mutex_lock(&rh->device.base.guard);
[a4e26882]193 list_remove(&dev->base.link);
[d33dc780]194 rh->devices_by_port[port_id - 1] = NULL;
[2cf28b9]195 fibril_mutex_unlock(&rh->device.base.guard);
196
[31cca4f3]197 /* Remove device from XHCI bus. */
198 if ((err = xhci_bus_remove_device(&rh->hc->bus, rh->hc, &dev->base))) {
[9620a54]199 usb_log_warning("Failed to remove device " XHCI_DEV_FMT " from XHCI bus: %s",
200 XHCI_DEV_ARGS(*dev), str_error(err));
[31cca4f3]201 }
202
[f45c78f]203 return EOK;
204}
205
[dcf0597]206/** Handle an incoming Port Change Detected Event.
207 */
208int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
[c8bb7090]209{
[f7bd246]210 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
[dcf0597]211 usb_log_debug("Port status change event detected for port %u.", port_id);
[c8bb7090]212
[dcf0597]213 /**
214 * We can't be sure that the port change this event announces is the
215 * only port change that happened (see section 4.19.2 of the xHCI
216 * specification). Therefore, we just check all ports for changes.
217 */
218 xhci_rh_handle_port_change(&hc->rh);
[07c08ea]219
220 return EOK;
221}
222
[dcf0597]223void xhci_rh_handle_port_change(xhci_rh_t *rh)
[d32d51d]224{
[dcf0597]225 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
226 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
[07c08ea]227
[dcf0597]228 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
229 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
[5c5c9407]230
[dcf0597]231 events &= port_change_mask;
[5c5c9407]232
[dcf0597]233 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
234 usb_log_info("Connected state changed on port %u.", i);
235 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
[5c5c9407]236
[dcf0597]237 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
[f45c78f]238 if (connected) {
[dcf0597]239 handle_connected_device(rh, i);
[f45c78f]240 } else {
241 handle_disconnected_device(rh, i);
242 }
[dcf0597]243 }
[5c5c9407]244
[dcf0597]245 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
246 usb_log_info("Port enabled changed on port %u.", i);
247 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
248 }
[5c5c9407]249
[dcf0597]250 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
251 usb_log_info("Warm port reset on port %u completed.", i);
252 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
253 }
[5c5c9407]254
[dcf0597]255 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
256 usb_log_info("Over-current change on port %u.", i);
257 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
258 }
[9876e34]259
[dcf0597]260 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
261 usb_log_info("Port reset on port %u completed.", i);
262 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
[2297fab]263
264 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
265 if (speed->major != 3) {
266 /* FIXME: We probably don't want to do this
267 * every time USB2 port is reset. This is a
268 * temporary workaround. */
[867b375]269 rh_setup_device(rh, i);
[2297fab]270 }
[dcf0597]271 }
[07c08ea]272
[dcf0597]273 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
274 usb_log_info("Port link state changed on port %u.", i);
275 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
276 }
[9876e34]277
[dcf0597]278 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
279 usb_log_info("Port %u failed to configure link.", i);
280 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
281 }
[9876e34]282
[dcf0597]283 if (events) {
284 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
285 }
[916991b]286 }
[2297fab]287
[dcf0597]288 /**
289 * Theory:
290 *
291 * Although more events could have happened while processing, the PCD
292 * bit in USBSTS will be set on every change. Because the PCD is
293 * cleared even before the interrupt is cleared, it is safe to assume
294 * that this handler will be called again.
295 *
296 * But because we could have handled the event in previous run of this
297 * handler, it is not an error when no event is detected.
298 *
299 * Reality:
300 *
301 * The PCD bit is never set. TODO Check why the interrupt never carries
302 * the PCD flag. Possibly repeat the checking until we're sure the
303 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
304 */
[07c08ea]305}
306
[b80c1ab]307static inline int get_hub_available_bandwidth(xhci_hc_t *hc, xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx)
308{
309 int err = EOK;
310
[60af4cdb]311 // TODO: find a correct place for this function + API
312 // We need speed, because a root hub device has both USB 2 and USB 3 speeds
313 // and the command can query only one of them
314 // ctx is an out parameter as of now
315 assert(dev);
[c3d926f3]316 assert(ctx);
[60af4cdb]317
318 xhci_cmd_t cmd;
[c3d926f3]319 xhci_cmd_init(&cmd, XHCI_CMD_GET_PORT_BANDWIDTH);
[60af4cdb]320
[b80c1ab]321 if ((err = dma_buffer_alloc(&cmd.bandwidth_ctx, sizeof(xhci_port_bandwidth_ctx_t))))
322 goto end;
323
[c3d926f3]324 cmd.device_speed = speed;
[60af4cdb]325
[b80c1ab]326 if ((err = xhci_cmd_sync(hc, &cmd)))
[c3d926f3]327 goto end;
[60af4cdb]328
[b80c1ab]329 memcpy(ctx, cmd.bandwidth_ctx.virt, sizeof(xhci_port_bandwidth_ctx_t));
[c3d926f3]330
331end:
332 xhci_cmd_fini(&cmd);
[b80c1ab]333 return err;
[60af4cdb]334}
335
[dcf0597]336const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
[07c08ea]337{
[dcf0597]338 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
[916991b]339
[dcf0597]340 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
[f668d60]341 return &rh->hc->speeds[psiv];
[07c08ea]342}
343
[dcf0597]344int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
[07c08ea]345{
[dcf0597]346 usb_log_debug2("Resetting port %u.", port);
347 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
348 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
[9876e34]349
[dcf0597]350 return EOK;
[d32d51d]351}
352
353int xhci_rh_fini(xhci_rh_t *rh)
354{
355 /* TODO: Implement me! */
[07c08ea]356 usb_log_debug2("Called xhci_rh_fini().");
[766043c]357
[d33dc780]358 free(rh->devices_by_port);
[766043c]359
[d32d51d]360 return EOK;
361}
362
[c8bb7090]363/**
364 * @}
365 */
Note: See TracBrowser for help on using the repository browser.