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

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

xhci: cstyle

  • Property mode set to 100644
File size: 8.0 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>
[a9fcd73]44#include <usb/port.h>
[867b375]45
[7bd99bf]46#include "debug.h"
[174788f]47#include "commands.h"
[370a1c8]48#include "endpoint.h"
[7bd99bf]49#include "hc.h"
50#include "hw_struct/trb.h"
[c8bb7090]51#include "rh.h"
[e9e24f2]52#include "transfers.h"
[7bd99bf]53
[9876e34]54/* This mask only lists registers, which imply port change. */
[9b56e528]55static const uint32_t port_events_mask =
[9876e34]56 XHCI_REG_MASK(XHCI_PORT_CSC) |
57 XHCI_REG_MASK(XHCI_PORT_PEC) |
58 XHCI_REG_MASK(XHCI_PORT_WRC) |
59 XHCI_REG_MASK(XHCI_PORT_OCC) |
60 XHCI_REG_MASK(XHCI_PORT_PRC) |
61 XHCI_REG_MASK(XHCI_PORT_PLC) |
62 XHCI_REG_MASK(XHCI_PORT_CEC);
63
[58f4c0f]64static const uint32_t port_reset_mask =
65 XHCI_REG_MASK(XHCI_PORT_WRC) |
66 XHCI_REG_MASK(XHCI_PORT_PRC);
67
[a9fcd73]68typedef struct rh_port {
69 usb_port_t base;
70 xhci_rh_t *rh;
71 uint8_t major;
72 xhci_port_regs_t *regs;
73 xhci_device_t *device;
74} rh_port_t;
75
[eb928c4]76/**
77 * Initialize the roothub subsystem.
78 */
[63431db2]79int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc)
[d32d51d]80{
[5c5c9407]81 assert(rh);
[816335c]82 assert(hc);
83
84 rh->hc = hc;
[9876e34]85 rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
[a9fcd73]86 rh->ports = calloc(rh->max_ports, sizeof(rh_port_t));
87 if (!rh->ports)
88 return ENOMEM;
[5c5c9407]89
[6832245]90 const int err = bus_device_init(&rh->device.base, &rh->hc->bus.base);
[a9fcd73]91 if (err) {
92 free(rh->ports);
[2cf28b9]93 return err;
[a9fcd73]94 }
95
96 for (unsigned i = 0; i < rh->max_ports; i++) {
97 usb_port_init(&rh->ports[i].base);
98 rh->ports[i].rh = rh;
99 rh->ports[i].regs = &rh->hc->op_regs->portrs[i];
100 }
[2cf28b9]101
102 /* Initialize route string */
103 rh->device.route_str = 0;
104
105 return EOK;
[d32d51d]106}
107
[eb928c4]108/**
109 * Finalize the RH subsystem.
110 */
111int xhci_rh_fini(xhci_rh_t *rh)
112{
113 assert(rh);
[a9fcd73]114 for (unsigned i = 0; i < rh->max_ports; i++)
115 usb_port_fini(&rh->ports[i].base);
[eb928c4]116 return EOK;
117}
118
[a9fcd73]119static rh_port_t *get_rh_port(usb_port_t *port)
[49e62998]120{
[a9fcd73]121 assert(port);
122 return (rh_port_t *) port;
[49e62998]123}
124
[eb928c4]125/**
126 * Create and setup a device directly connected to RH. As the xHCI is not using
127 * a virtual usbhub device for RH, this routine is called for devices directly.
[20eaa82]128 */
[a9fcd73]129static int rh_enumerate_device(usb_port_t *usb_port)
[867b375]130{
[20eaa82]131 int err;
[a9fcd73]132 rh_port_t *port = get_rh_port(usb_port);
[2cf28b9]133
[a9fcd73]134 if (port->major <= 2) {
135 /* USB ports for lower speeds needs their port reset first. */
136 XHCI_REG_SET(port->regs, XHCI_PORT_PR, 1);
137 if ((err = usb_port_wait_for_enabled(&port->base)))
138 return err;
[58f4c0f]139 } else {
140 /* Do the Warm reset to ensure the state is clear. */
141 XHCI_REG_SET(port->regs, XHCI_PORT_WPR, 1);
142 if ((err = usb_port_wait_for_enabled(&port->base)))
143 return err;
[9b56e528]144 }
145
[0f79283b]146 /*
147 * We cannot know in advance, whether the speed in the status register
148 * is valid - it depends on the protocol. So we read it later, but then
149 * we have to check if the port is still enabled.
150 */
151 uint32_t status = XHCI_REG_RD_FIELD(&port->regs->portsc, 32);
152
153 bool enabled = !!(status & XHCI_REG_MASK(XHCI_PORT_PED));
154 if (!enabled)
155 return ENOENT;
156
[8033f89]157 unsigned psiv = (status & XHCI_REG_MASK(XHCI_PORT_PS))
158 >> XHCI_REG_SHIFT(XHCI_PORT_PS);
[0f79283b]159 const usb_speed_t speed = port->rh->hc->speeds[psiv].usb_speed;
160
161 device_t *dev = hcd_ddf_fun_create(&port->rh->hc->base, speed);
[20eaa82]162 if (!dev) {
163 usb_log_error("Failed to create USB device function.");
164 return ENOMEM;
165 }
166
[a9fcd73]167 dev->hub = &port->rh->device.base;
[2aaba7e]168 dev->tier = 1;
[a9fcd73]169 dev->port = port - port->rh->ports + 1;
[eeca8a6]170
[a9fcd73]171 port->device = xhci_device_get(dev);
172 port->device->rh_port = dev->port;
[0206d35]173
[8033f89]174 usb_log_debug("Enumerating new %s-speed device on port %u.",
175 usb_str_speed(dev->speed), dev->port);
[36e8a0c8]176
[eb928c4]177 if ((err = bus_device_enumerate(dev))) {
[20eaa82]178 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
179 return err;
180 }
181
182 if (!ddf_fun_get_name(dev->fun)) {
[6832245]183 bus_device_set_default_name(dev);
[20eaa82]184 }
185
186 if ((err = ddf_fun_bind(dev->fun))) {
[9620a54]187 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
[a9fcd73]188 XHCI_DEV_ARGS(*port->device), str_error(err));
[20eaa82]189 goto err_usb_dev;
190 }
191
192 return EOK;
193
194err_usb_dev:
[32fb6bce]195 hcd_ddf_fun_destroy(dev);
[a9fcd73]196 port->device = NULL;
[20eaa82]197 return err;
[867b375]198}
199
[eb928c4]200/**
201 * Deal with a detached device.
[f45c78f]202 */
[a9fcd73]203static void rh_remove_device(usb_port_t *usb_port)
[f45c78f]204{
[a9fcd73]205 rh_port_t *port = get_rh_port(usb_port);
[a4e26882]206
[a9fcd73]207 assert(port->device);
208 usb_log_info("Device " XHCI_DEV_FMT " at port %zu has been disconnected.",
209 XHCI_DEV_ARGS(*port->device), port - port->rh->ports + 1);
[2cf28b9]210
[31cca4f3]211 /* Remove device from XHCI bus. */
[a9fcd73]212 bus_device_gone(&port->device->base);
[49e62998]213
[a9fcd73]214 /* Mark the device as detached. */
215 port->device = NULL;
[49e62998]216}
217
[eb928c4]218/**
[fb154e13]219 * Handle all changes on specified port.
[eb928c4]220 */
[fb154e13]221void xhci_rh_handle_port_change(xhci_rh_t *rh, uint8_t port_id)
[d32d51d]222{
[a9fcd73]223 rh_port_t * const port = &rh->ports[port_id - 1];
[07c08ea]224
[a9fcd73]225 uint32_t status = XHCI_REG_RD_FIELD(&port->regs->portsc, 32);
[5c5c9407]226
[a9fcd73]227 while (status & port_events_mask) {
[9b56e528]228 /*
229 * The PED bit in xHCI has RW1C semantics, which means that
230 * writing 1 to it will disable the port. Which means all
231 * standard mechanisms of register handling fails here.
232 */
[8033f89]233 XHCI_REG_WR_FIELD(&port->regs->portsc,
234 status & ~XHCI_REG_MASK(XHCI_PORT_PED), 32);
[5c5c9407]235
[58f4c0f]236 const bool connected = !!(status & XHCI_REG_MASK(XHCI_PORT_CCS));
237 const bool enabled = !!(status & XHCI_REG_MASK(XHCI_PORT_PED));
238
[a9fcd73]239 if (status & XHCI_REG_MASK(XHCI_PORT_CSC)) {
[fb154e13]240 usb_log_info("Connected state changed on port %u.", port_id);
[a9fcd73]241 status &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
[5c5c9407]242
[f45c78f]243 if (connected) {
[a9fcd73]244 usb_port_connected(&port->base, &rh_enumerate_device);
[f45c78f]245 } else {
[a9fcd73]246 usb_port_disabled(&port->base, &rh_remove_device);
[f45c78f]247 }
[dcf0597]248 }
[5c5c9407]249
[58f4c0f]250 if (status & port_reset_mask) {
251 status &= ~port_reset_mask;
[fb154e13]252
[a9fcd73]253 if (enabled) {
[0f79283b]254 usb_port_enabled(&port->base);
[a9fcd73]255 } else {
256 usb_port_disabled(&port->base, &rh_remove_device);
257 }
258 }
259
260 status &= port_events_mask;
261 if (status != 0)
262 usb_log_debug("RH port %u change not handled: 0x%x", port_id, status);
[fb154e13]263
264 /* Make sure that PSCEG is 0 before exiting the loop. */
[a9fcd73]265 status = XHCI_REG_RD_FIELD(&port->regs->portsc, 32);
[916991b]266 }
[07c08ea]267}
268
[8033f89]269void xhci_rh_set_ports_protocol(xhci_rh_t *rh,
270 unsigned offset, unsigned count, unsigned major)
[07c08ea]271{
[a9fcd73]272 for (unsigned i = offset; i < offset + count; i++)
273 rh->ports[i - 1].major = major;
[07c08ea]274}
275
[05770666]276void xhci_rh_startup(xhci_rh_t *rh)
277{
278 /* The reset changed status of all ports, and SW originated reason does
279 * not cause an interrupt.
280 */
[09187c6e]281 for (uint8_t i = 0; i < rh->max_ports; ++i) {
[05770666]282 xhci_rh_handle_port_change(rh, i + 1);
283
284 rh_port_t * const port = &rh->ports[i];
285
286 /*
287 * When xHCI starts, for some reasons USB 3 ports do not have
288 * the CSC bit, even though they are connected. Try to find
289 * such ports.
290 */
[8033f89]291 if (XHCI_REG_RD(port->regs, XHCI_PORT_CCS)
292 && port->base.state == PORT_DISABLED)
[05770666]293 usb_port_connected(&port->base, &rh_enumerate_device);
294 }
295}
296
[c8bb7090]297/**
298 * @}
299 */
Note: See TracBrowser for help on using the repository browser.