source: mainline/uspace/drv/bus/usb/xhci/rh.c@ 69a93d02

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

usbhost: renamed bus_device_remove to bus_device_gone

  • Property mode set to 100644
File size: 9.4 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
[eb928c4]63/**
64 * Initialize the roothub subsystem.
65 */
[63431db2]66int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc)
[d32d51d]67{
[5c5c9407]68 assert(rh);
[816335c]69 assert(hc);
70
71 rh->hc = hc;
[9876e34]72 rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
[d33dc780]73 rh->devices_by_port = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
[5c5c9407]74
[6832245]75 const int err = bus_device_init(&rh->device.base, &rh->hc->bus.base);
[2cf28b9]76 if (err)
77 return err;
78
79 /* Initialize route string */
80 rh->device.route_str = 0;
81 rh->device.tier = 0;
82
83 return EOK;
[d32d51d]84}
85
[eb928c4]86/**
87 * Finalize the RH subsystem.
88 */
89int xhci_rh_fini(xhci_rh_t *rh)
90{
91 assert(rh);
92 free(rh->devices_by_port);
93 return EOK;
94}
95
96/**
97 * Create and setup a device directly connected to RH. As the xHCI is not using
98 * a virtual usbhub device for RH, this routine is called for devices directly.
[20eaa82]99 */
[867b375]100static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
101{
[20eaa82]102 int err;
103 assert(rh);
104
[d33dc780]105 assert(rh->devices_by_port[port_id - 1] == NULL);
[2cf28b9]106
[32fb6bce]107 device_t *dev = hcd_ddf_fun_create(&rh->hc->base);
[20eaa82]108 if (!dev) {
109 usb_log_error("Failed to create USB device function.");
110 return ENOMEM;
111 }
112
[0206d35]113 const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
114 xhci_device_t *xhci_dev = xhci_device_get(dev);
115 xhci_dev->usb3 = port_speed->major == 3;
[2cf28b9]116 xhci_dev->rh_port = port_id;
[0206d35]117
[2cf28b9]118 dev->hub = &rh->device.base;
[20eaa82]119 dev->port = port_id;
[f668d60]120 dev->speed = port_speed->usb_speed;
[20eaa82]121
[eb928c4]122 if ((err = bus_device_enumerate(dev))) {
[20eaa82]123 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
124 return err;
125 }
126
127 if (!ddf_fun_get_name(dev->fun)) {
[6832245]128 bus_device_set_default_name(dev);
[20eaa82]129 }
130
131 if ((err = ddf_fun_bind(dev->fun))) {
[9620a54]132 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
133 XHCI_DEV_ARGS(*xhci_dev), str_error(err));
[20eaa82]134 goto err_usb_dev;
135 }
136
[2cf28b9]137 fibril_mutex_lock(&rh->device.base.guard);
138 list_append(&dev->link, &rh->device.base.devices);
[d33dc780]139 rh->devices_by_port[port_id - 1] = xhci_dev;
[2cf28b9]140 fibril_mutex_unlock(&rh->device.base.guard);
[867b375]141
[20eaa82]142 return EOK;
143
144err_usb_dev:
[32fb6bce]145 hcd_ddf_fun_destroy(dev);
[20eaa82]146 return err;
[867b375]147}
148
[eb928c4]149/**
150 * Handle a device connection. USB 3+ devices are set up directly, USB 2 and
151 * below first need to have their port reset.
152 */
[dcf0597]153static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
[c8bb7090]154{
[dcf0597]155 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
[472235a]156
[dcf0597]157 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
158 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
[7bd99bf]159
[dcf0597]160 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
[7bd99bf]161
[dcf0597]162 if (speed->major == 3) {
163 if (link_state == 0) {
164 /* USB3 is automatically advanced to enabled. */
[867b375]165 return rh_setup_device(rh, port_id);
[dcf0597]166 }
167 else if (link_state == 5) {
168 /* USB 3 failed to enable. */
169 usb_log_error("USB 3 port couldn't be enabled.");
170 return EAGAIN;
171 }
172 else {
173 usb_log_error("USB 3 port is in invalid state %u.", link_state);
174 return EINVAL;
175 }
176 }
177 else {
178 usb_log_debug("USB 2 device attached, issuing reset.");
179 xhci_rh_reset_port(rh, port_id);
180 return EOK;
181 }
[816335c]182}
183
[eb928c4]184/**
185 * Deal with a detached device.
[f45c78f]186 */
187static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
188{
[a4e26882]189 assert(rh);
190
[766043c]191 /* Find XHCI device by the port. */
[d33dc780]192 xhci_device_t *dev = rh->devices_by_port[port_id - 1];
[766043c]193 if (!dev) {
[a4e26882]194 /* Must be extraneous call. */
[766043c]195 return EOK;
196 }
197
[9620a54]198 usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
199 XHCI_DEV_ARGS(*dev), port_id);
[a4e26882]200
[40a3bfa]201 /* Mark the device as detached. */
[2cf28b9]202 fibril_mutex_lock(&rh->device.base.guard);
[a4e26882]203 list_remove(&dev->base.link);
[d33dc780]204 rh->devices_by_port[port_id - 1] = NULL;
[2cf28b9]205 fibril_mutex_unlock(&rh->device.base.guard);
206
[31cca4f3]207 /* Remove device from XHCI bus. */
[9848c77]208 bus_device_gone(&dev->base);
[31cca4f3]209
[f45c78f]210 return EOK;
211}
212
[eb928c4]213/**
214 * Handle an incoming Port Change Detected Event.
[dcf0597]215 */
216int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
[c8bb7090]217{
[f7bd246]218 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
[dcf0597]219 usb_log_debug("Port status change event detected for port %u.", port_id);
[c8bb7090]220
[dcf0597]221 /**
222 * We can't be sure that the port change this event announces is the
223 * only port change that happened (see section 4.19.2 of the xHCI
224 * specification). Therefore, we just check all ports for changes.
225 */
226 xhci_rh_handle_port_change(&hc->rh);
[07c08ea]227
228 return EOK;
229}
230
[eb928c4]231/**
232 * Handle all changes on all ports.
233 */
[dcf0597]234void xhci_rh_handle_port_change(xhci_rh_t *rh)
[d32d51d]235{
[dcf0597]236 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
237 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
[07c08ea]238
[dcf0597]239 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
240 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
[5c5c9407]241
[dcf0597]242 events &= port_change_mask;
[5c5c9407]243
[dcf0597]244 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
245 usb_log_info("Connected state changed on port %u.", i);
246 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
[5c5c9407]247
[dcf0597]248 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
[f45c78f]249 if (connected) {
[dcf0597]250 handle_connected_device(rh, i);
[f45c78f]251 } else {
252 handle_disconnected_device(rh, i);
253 }
[dcf0597]254 }
[5c5c9407]255
[dcf0597]256 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
257 usb_log_info("Port enabled changed on port %u.", i);
258 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
259 }
[5c5c9407]260
[dcf0597]261 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
262 usb_log_info("Warm port reset on port %u completed.", i);
263 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
264 }
[5c5c9407]265
[dcf0597]266 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
267 usb_log_info("Over-current change on port %u.", i);
268 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
269 }
[9876e34]270
[dcf0597]271 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
272 usb_log_info("Port reset on port %u completed.", i);
273 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
[2297fab]274
275 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
276 if (speed->major != 3) {
277 /* FIXME: We probably don't want to do this
278 * every time USB2 port is reset. This is a
279 * temporary workaround. */
[867b375]280 rh_setup_device(rh, i);
[2297fab]281 }
[dcf0597]282 }
[07c08ea]283
[dcf0597]284 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
285 usb_log_info("Port link state changed on port %u.", i);
286 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
287 }
[9876e34]288
[dcf0597]289 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
290 usb_log_info("Port %u failed to configure link.", i);
291 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
292 }
[9876e34]293
[dcf0597]294 if (events) {
295 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
296 }
[916991b]297 }
[2297fab]298
[dcf0597]299 /**
300 * Theory:
301 *
302 * Although more events could have happened while processing, the PCD
303 * bit in USBSTS will be set on every change. Because the PCD is
304 * cleared even before the interrupt is cleared, it is safe to assume
305 * that this handler will be called again.
306 *
307 * But because we could have handled the event in previous run of this
308 * handler, it is not an error when no event is detected.
309 *
310 * Reality:
311 *
312 * The PCD bit is never set. TODO Check why the interrupt never carries
313 * the PCD flag. Possibly repeat the checking until we're sure the
314 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
315 */
[07c08ea]316}
317
[eb928c4]318/**
319 * Get a port speed for a given port id.
320 */
[dcf0597]321const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
[07c08ea]322{
[dcf0597]323 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
[916991b]324
[dcf0597]325 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
[f668d60]326 return &rh->hc->speeds[psiv];
[07c08ea]327}
328
[eb928c4]329/**
330 * Issue a port reset for a given port.
331 */
[dcf0597]332int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
[07c08ea]333{
[dcf0597]334 usb_log_debug2("Resetting port %u.", port);
335 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
336 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
[9876e34]337
[dcf0597]338 return EOK;
[d32d51d]339}
340
[c8bb7090]341/**
342 * @}
343 */
Note: See TracBrowser for help on using the repository browser.