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

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

xhci: rewrite RH to use usb/port

  • Property mode set to 100644
File size: 6.7 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/bus.h>
41#include <usb/host/ddf_helpers.h>
42#include <usb/host/dma_buffer.h>
43#include <usb/host/hcd.h>
44#include <usb/port.h>
45
46#include "debug.h"
47#include "commands.h"
48#include "endpoint.h"
49#include "hc.h"
50#include "hw_struct/trb.h"
51#include "rh.h"
52#include "transfers.h"
53
54/* This mask only lists registers, which imply port change. */
55static const uint32_t port_events_mask =
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
64typedef struct rh_port {
65 usb_port_t base;
66 xhci_rh_t *rh;
67 uint8_t major;
68 xhci_port_regs_t *regs;
69 xhci_device_t *device;
70} rh_port_t;
71
72/**
73 * Initialize the roothub subsystem.
74 */
75int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc)
76{
77 assert(rh);
78 assert(hc);
79
80 rh->hc = hc;
81 rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
82 rh->ports = calloc(rh->max_ports, sizeof(rh_port_t));
83 if (!rh->ports)
84 return ENOMEM;
85
86 const int err = bus_device_init(&rh->device.base, &rh->hc->bus.base);
87 if (err) {
88 free(rh->ports);
89 return err;
90 }
91
92 for (unsigned i = 0; i < rh->max_ports; i++) {
93 usb_port_init(&rh->ports[i].base);
94 rh->ports[i].rh = rh;
95 rh->ports[i].regs = &rh->hc->op_regs->portrs[i];
96 }
97
98 /* Initialize route string */
99 rh->device.route_str = 0;
100 rh->device.tier = 0;
101
102 return EOK;
103}
104
105/**
106 * Finalize the RH subsystem.
107 */
108int xhci_rh_fini(xhci_rh_t *rh)
109{
110 assert(rh);
111 for (unsigned i = 0; i < rh->max_ports; i++)
112 usb_port_fini(&rh->ports[i].base);
113 return EOK;
114}
115
116static rh_port_t *get_rh_port(usb_port_t *port)
117{
118 assert(port);
119 return (rh_port_t *) port;
120}
121
122/**
123 * Create and setup a device directly connected to RH. As the xHCI is not using
124 * a virtual usbhub device for RH, this routine is called for devices directly.
125 */
126static int rh_enumerate_device(usb_port_t *usb_port)
127{
128 int err;
129 rh_port_t *port = get_rh_port(usb_port);
130
131 if (port->major <= 2) {
132 /* USB ports for lower speeds needs their port reset first. */
133 XHCI_REG_SET(port->regs, XHCI_PORT_PR, 1);
134 if ((err = usb_port_wait_for_enabled(&port->base)))
135 return err;
136 }
137
138 device_t *dev = hcd_ddf_fun_create(&port->rh->hc->base, port->base.speed);
139 if (!dev) {
140 usb_log_error("Failed to create USB device function.");
141 return ENOMEM;
142 }
143
144 dev->hub = &port->rh->device.base;
145 dev->port = port - port->rh->ports + 1;
146
147 port->device = xhci_device_get(dev);
148 port->device->rh_port = dev->port;
149
150 if ((err = bus_device_enumerate(dev))) {
151 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
152 return err;
153 }
154
155 if (!ddf_fun_get_name(dev->fun)) {
156 bus_device_set_default_name(dev);
157 }
158
159 if ((err = ddf_fun_bind(dev->fun))) {
160 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
161 XHCI_DEV_ARGS(*port->device), str_error(err));
162 goto err_usb_dev;
163 }
164
165 return EOK;
166
167err_usb_dev:
168 hcd_ddf_fun_destroy(dev);
169 port->device = NULL;
170 return err;
171}
172
173/**
174 * Deal with a detached device.
175 */
176static void rh_remove_device(usb_port_t *usb_port)
177{
178 rh_port_t *port = get_rh_port(usb_port);
179
180 assert(port->device);
181 usb_log_info("Device " XHCI_DEV_FMT " at port %zu has been disconnected.",
182 XHCI_DEV_ARGS(*port->device), port - port->rh->ports + 1);
183
184 /* Remove device from XHCI bus. */
185 bus_device_gone(&port->device->base);
186
187 /* Mark the device as detached. */
188 port->device = NULL;
189}
190
191/**
192 * Handle all changes on specified port.
193 */
194void xhci_rh_handle_port_change(xhci_rh_t *rh, uint8_t port_id)
195{
196 rh_port_t * const port = &rh->ports[port_id - 1];
197
198 uint32_t status = XHCI_REG_RD_FIELD(&port->regs->portsc, 32);
199
200 while (status & port_events_mask) {
201 /*
202 * The PED bit in xHCI has RW1C semantics, which means that
203 * writing 1 to it will disable the port. Which means all
204 * standard mechanisms of register handling fails here.
205 */
206 XHCI_REG_WR_FIELD(&port->regs->portsc, status & ~XHCI_REG_MASK(XHCI_PORT_PED), 32);
207
208 if (status & XHCI_REG_MASK(XHCI_PORT_CSC)) {
209 usb_log_info("Connected state changed on port %u.", port_id);
210 status &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
211
212 bool connected = !!(status & XHCI_REG_MASK(XHCI_PORT_CCS));
213 if (connected) {
214 usb_port_connected(&port->base, &rh_enumerate_device);
215 } else {
216 usb_port_disabled(&port->base, &rh_remove_device);
217 }
218 }
219
220 if (status & XHCI_REG_MASK(XHCI_PORT_PRC)) {
221 status &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
222 bool enabled = !!(status & XHCI_REG_MASK(XHCI_PORT_PED));
223
224 if (enabled) {
225 unsigned psiv = (status & XHCI_REG_MASK(XHCI_PORT_PS)) >> XHCI_REG_SHIFT(XHCI_PORT_PS);
226 const usb_speed_t speed = rh->hc->speeds[psiv].usb_speed;
227 usb_port_enabled(&port->base, speed);
228 } else {
229 usb_port_disabled(&port->base, &rh_remove_device);
230 }
231 }
232
233 status &= port_events_mask;
234 if (status != 0)
235 usb_log_debug("RH port %u change not handled: 0x%x", port_id, status);
236
237 /* Make sure that PSCEG is 0 before exiting the loop. */
238 status = XHCI_REG_RD_FIELD(&port->regs->portsc, 32);
239 }
240}
241
242void xhci_rh_set_ports_protocol(xhci_rh_t *rh, unsigned offset, unsigned count, unsigned major)
243{
244 for (unsigned i = offset; i < offset + count; i++)
245 rh->ports[i - 1].major = major;
246}
247
248/**
249 * @}
250 */
Note: See TracBrowser for help on using the repository browser.