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

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

usbhost: add joinable_fibril utility

  • Property mode set to 100644
File size: 8.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/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
64static const uint32_t port_reset_mask =
65 XHCI_REG_MASK(XHCI_PORT_WRC) |
66 XHCI_REG_MASK(XHCI_PORT_PRC);
67
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
76static int rh_worker(void *);
77
78/**
79 * Initialize the roothub subsystem.
80 */
81int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc)
82{
83 assert(rh);
84 assert(hc);
85
86 rh->hc = hc;
87 rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
88 rh->ports = calloc(rh->max_ports, sizeof(rh_port_t));
89 if (!rh->ports)
90 return ENOMEM;
91
92 const int err = bus_device_init(&rh->device.base, &rh->hc->bus.base);
93 if (err) {
94 free(rh->ports);
95 return err;
96 }
97
98 rh->event_worker = joinable_fibril_create(&rh_worker, rh);
99 if (!rh->event_worker) {
100 free(rh->ports);
101 return err;
102 }
103
104 for (unsigned i = 0; i < rh->max_ports; i++) {
105 usb_port_init(&rh->ports[i].base);
106 rh->ports[i].rh = rh;
107 rh->ports[i].regs = &rh->hc->op_regs->portrs[i];
108 }
109
110 /* Initialize route string */
111 rh->device.route_str = 0;
112
113 xhci_sw_ring_init(&rh->event_ring, rh->max_ports);
114
115 joinable_fibril_start(rh->event_worker);
116
117 return EOK;
118}
119
120/**
121 * Finalize the RH subsystem.
122 */
123int xhci_rh_fini(xhci_rh_t *rh)
124{
125 assert(rh);
126 for (unsigned i = 0; i < rh->max_ports; i++)
127 usb_port_fini(&rh->ports[i].base);
128
129 xhci_sw_ring_stop(&rh->event_ring);
130 joinable_fibril_join(rh->event_worker);
131 xhci_sw_ring_fini(&rh->event_ring);
132 return EOK;
133}
134
135static rh_port_t *get_rh_port(usb_port_t *port)
136{
137 assert(port);
138 return (rh_port_t *) port;
139}
140
141/**
142 * Create and setup a device directly connected to RH. As the xHCI is not using
143 * a virtual usbhub device for RH, this routine is called for devices directly.
144 */
145static int rh_enumerate_device(usb_port_t *usb_port)
146{
147 int err;
148 rh_port_t *port = get_rh_port(usb_port);
149
150 if (port->major <= 2) {
151 /* USB ports for lower speeds needs their port reset first. */
152 XHCI_REG_SET(port->regs, XHCI_PORT_PR, 1);
153 if ((err = usb_port_wait_for_enabled(&port->base)))
154 return err;
155 } else {
156 /* Do the Warm reset to ensure the state is clear. */
157 XHCI_REG_SET(port->regs, XHCI_PORT_WPR, 1);
158 if ((err = usb_port_wait_for_enabled(&port->base)))
159 return err;
160 }
161
162 /*
163 * We cannot know in advance, whether the speed in the status register
164 * is valid - it depends on the protocol. So we read it later, but then
165 * we have to check if the port is still enabled.
166 */
167 uint32_t status = XHCI_REG_RD_FIELD(&port->regs->portsc, 32);
168
169 bool enabled = !!(status & XHCI_REG_MASK(XHCI_PORT_PED));
170 if (!enabled)
171 return ENOENT;
172
173 unsigned psiv = (status & XHCI_REG_MASK(XHCI_PORT_PS))
174 >> XHCI_REG_SHIFT(XHCI_PORT_PS);
175 const usb_speed_t speed = port->rh->hc->speeds[psiv].usb_speed;
176
177 device_t *dev = hcd_ddf_fun_create(&port->rh->hc->base, speed);
178 if (!dev) {
179 usb_log_error("Failed to create USB device function.");
180 return ENOMEM;
181 }
182
183 dev->hub = &port->rh->device.base;
184 dev->tier = 1;
185 dev->port = port - port->rh->ports + 1;
186
187 port->device = xhci_device_get(dev);
188 port->device->rh_port = dev->port;
189
190 usb_log_debug("Enumerating new %s-speed device on port %u.",
191 usb_str_speed(dev->speed), dev->port);
192
193 if ((err = bus_device_enumerate(dev))) {
194 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
195 return err;
196 }
197
198 if (!ddf_fun_get_name(dev->fun)) {
199 bus_device_set_default_name(dev);
200 }
201
202 if ((err = ddf_fun_bind(dev->fun))) {
203 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
204 XHCI_DEV_ARGS(*port->device), str_error(err));
205 goto err_usb_dev;
206 }
207
208 return EOK;
209
210err_usb_dev:
211 hcd_ddf_fun_destroy(dev);
212 port->device = NULL;
213 return err;
214}
215
216/**
217 * Deal with a detached device.
218 */
219static void rh_remove_device(usb_port_t *usb_port)
220{
221 rh_port_t *port = get_rh_port(usb_port);
222
223 assert(port->device);
224 usb_log_info("Device " XHCI_DEV_FMT " at port %zu has been disconnected.",
225 XHCI_DEV_ARGS(*port->device), port - port->rh->ports + 1);
226
227 /* Remove device from XHCI bus. */
228 bus_device_gone(&port->device->base);
229
230 /* Mark the device as detached. */
231 port->device = NULL;
232}
233
234/**
235 * Handle all changes on specified port.
236 */
237static void handle_port_change(xhci_rh_t *rh, uint8_t port_id)
238{
239 rh_port_t * const port = &rh->ports[port_id - 1];
240
241 uint32_t status = XHCI_REG_RD_FIELD(&port->regs->portsc, 32);
242
243 while (status & port_events_mask) {
244 /*
245 * The PED bit in xHCI has RW1C semantics, which means that
246 * writing 1 to it will disable the port. Which means all
247 * standard mechanisms of register handling fails here.
248 */
249 XHCI_REG_WR_FIELD(&port->regs->portsc,
250 status & ~XHCI_REG_MASK(XHCI_PORT_PED), 32);
251
252 const bool connected = !!(status & XHCI_REG_MASK(XHCI_PORT_CCS));
253 const bool enabled = !!(status & XHCI_REG_MASK(XHCI_PORT_PED));
254
255 if (status & XHCI_REG_MASK(XHCI_PORT_CSC)) {
256 usb_log_info("Connected state changed on port %u.", port_id);
257 status &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
258
259 if (connected) {
260 usb_port_connected(&port->base, &rh_enumerate_device);
261 } else {
262 usb_port_disabled(&port->base, &rh_remove_device);
263 }
264 }
265
266 if (status & port_reset_mask) {
267 status &= ~port_reset_mask;
268
269 if (enabled) {
270 usb_port_enabled(&port->base);
271 } else {
272 usb_port_disabled(&port->base, &rh_remove_device);
273 }
274 }
275
276 status &= port_events_mask;
277 if (status != 0)
278 usb_log_debug("RH port %u change not handled: 0x%x", port_id, status);
279
280 /* Make sure that PSCEG is 0 before exiting the loop. */
281 status = XHCI_REG_RD_FIELD(&port->regs->portsc, 32);
282 }
283}
284
285void xhci_rh_set_ports_protocol(xhci_rh_t *rh,
286 unsigned offset, unsigned count, unsigned major)
287{
288 for (unsigned i = offset; i < offset + count; i++)
289 rh->ports[i - 1].major = major;
290}
291
292void xhci_rh_startup(xhci_rh_t *rh)
293{
294 /* The reset changed status of all ports, and SW originated reason does
295 * not cause an interrupt.
296 */
297 for (uint8_t i = 0; i < rh->max_ports; ++i) {
298 handle_port_change(rh, i + 1);
299
300 rh_port_t * const port = &rh->ports[i];
301
302 /*
303 * When xHCI starts, for some reasons USB 3 ports do not have
304 * the CSC bit, even though they are connected. Try to find
305 * such ports.
306 */
307 if (XHCI_REG_RD(port->regs, XHCI_PORT_CCS)
308 && port->base.state == PORT_DISABLED)
309 usb_port_connected(&port->base, &rh_enumerate_device);
310 }
311}
312
313static int rh_worker(void *arg)
314{
315 xhci_rh_t * const rh = arg;
316
317 xhci_trb_t trb;
318 while (xhci_sw_ring_dequeue(&rh->event_ring, &trb) == EOK) {
319 uint8_t port_id = XHCI_QWORD_EXTRACT(trb.parameter, 31, 24);
320 usb_log_debug("Port status change event detected for port %u.", port_id);
321 handle_port_change(rh, port_id);
322 }
323
324 return 0;
325}
326
327/**
328 * @}
329 */
Note: See TracBrowser for help on using the repository browser.