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

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

xhci rh: fixed broken event system

The previous one broke when two events were to be run simultaneously.

  • Property mode set to 100644
File size: 9.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
45#include "debug.h"
46#include "commands.h"
47#include "endpoint.h"
48#include "hc.h"
49#include "hw_struct/trb.h"
50#include "rh.h"
51#include "transfers.h"
52
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
63/**
64 * Initialize the roothub subsystem.
65 */
66int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc)
67{
68 assert(rh);
69 assert(hc);
70
71 rh->hc = hc;
72 rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
73 rh->devices_by_port = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
74
75 const int err = bus_device_init(&rh->device.base, &rh->hc->bus.base);
76 if (err)
77 return err;
78
79 /* Initialize route string */
80 rh->device.route_str = 0;
81 rh->device.tier = 0;
82
83 fibril_mutex_initialize(&rh->event_guard);
84 fibril_condvar_initialize(&rh->event_ready);
85 fibril_condvar_initialize(&rh->event_handled);
86
87 return EOK;
88}
89
90/**
91 * Finalize the RH subsystem.
92 */
93int xhci_rh_fini(xhci_rh_t *rh)
94{
95 assert(rh);
96 free(rh->devices_by_port);
97 return EOK;
98}
99
100typedef struct rh_event {
101 uint8_t port_id;
102 uint32_t events;
103 unsigned readers_to_go;
104} rh_event_t;
105
106static int rh_event_wait_timeout(xhci_rh_t *rh, uint8_t port_id, uint32_t mask, suseconds_t timeout)
107{
108 int r;
109 assert(fibril_mutex_is_locked(&rh->event_guard));
110
111 ++rh->event_readers_waiting;
112
113 do {
114 r = fibril_condvar_wait_timeout(&rh->event_ready, &rh->event_guard, timeout);
115 if (r != EOK)
116 break;
117
118 assert(rh->event);
119 if (--rh->event->readers_to_go == 0)
120 fibril_condvar_broadcast(&rh->event_handled);
121 } while (rh->event->port_id != port_id || (rh->event->events & mask) != mask);
122
123 if (r == EOK)
124 rh->event->events &= ~mask;
125
126 --rh->event_readers_waiting;
127
128 return r;
129}
130
131static void rh_event_run_handlers(xhci_rh_t *rh, uint8_t port_id, uint32_t *events)
132{
133 assert(fibril_mutex_is_locked(&rh->event_guard));
134
135 /* There can be different event running already */
136 while (rh->event)
137 fibril_condvar_wait(&rh->event_handled, &rh->event_guard);
138
139 rh_event_t event = {
140 .port_id = port_id,
141 .events = *events,
142 .readers_to_go = rh->event_readers_waiting,
143 };
144
145 rh->event = &event;
146 fibril_condvar_broadcast(&rh->event_ready);
147 while (event.readers_to_go)
148 fibril_condvar_wait(&rh->event_handled, &rh->event_guard);
149 *events = event.events;
150 rh->event = NULL;
151}
152
153/**
154 * Create and setup a device directly connected to RH. As the xHCI is not using
155 * a virtual usbhub device for RH, this routine is called for devices directly.
156 */
157static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
158{
159 int err;
160 assert(rh);
161
162 assert(rh->devices_by_port[port_id - 1] == NULL);
163
164 device_t *dev = hcd_ddf_fun_create(&rh->hc->base);
165 if (!dev) {
166 usb_log_error("Failed to create USB device function.");
167 return ENOMEM;
168 }
169
170 const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
171 xhci_device_t *xhci_dev = xhci_device_get(dev);
172 xhci_dev->usb3 = port_speed->major == 3;
173 xhci_dev->rh_port = port_id;
174
175 dev->hub = &rh->device.base;
176 dev->port = port_id;
177 dev->speed = port_speed->usb_speed;
178
179 if ((err = bus_device_enumerate(dev))) {
180 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
181 return err;
182 }
183
184 if (!ddf_fun_get_name(dev->fun)) {
185 bus_device_set_default_name(dev);
186 }
187
188 if ((err = ddf_fun_bind(dev->fun))) {
189 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
190 XHCI_DEV_ARGS(*xhci_dev), str_error(err));
191 goto err_usb_dev;
192 }
193
194 fibril_mutex_lock(&rh->device.base.guard);
195 list_append(&dev->link, &rh->device.base.devices);
196 rh->devices_by_port[port_id - 1] = xhci_dev;
197 fibril_mutex_unlock(&rh->device.base.guard);
198
199 return EOK;
200
201err_usb_dev:
202 hcd_ddf_fun_destroy(dev);
203 return err;
204}
205
206
207static int rh_port_reset_sync(xhci_rh_t *rh, uint8_t port_id)
208{
209 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
210
211 fibril_mutex_lock(&rh->event_guard);
212 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
213 const int r = rh_event_wait_timeout(rh, port_id, XHCI_REG_MASK(XHCI_PORT_PRC), 0);
214 fibril_mutex_unlock(&rh->event_guard);
215 return r;
216}
217
218/**
219 * Handle a device connection. USB 3+ devices are set up directly, USB 2 and
220 * below first need to have their port reset.
221 */
222static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
223{
224 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
225
226 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
227 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
228
229 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
230
231 if (speed->major == 3) {
232 if (link_state == 0) {
233 /* USB3 is automatically advanced to enabled. */
234 return rh_setup_device(rh, port_id);
235 }
236 else if (link_state == 5) {
237 /* USB 3 failed to enable. */
238 usb_log_error("USB 3 port couldn't be enabled.");
239 return EAGAIN;
240 }
241 else {
242 usb_log_error("USB 3 port is in invalid state %u.", link_state);
243 return EINVAL;
244 }
245 }
246 else {
247 usb_log_debug("USB 2 device attached, issuing reset.");
248 const int err = rh_port_reset_sync(rh, port_id);
249 if (err)
250 return err;
251
252 rh_setup_device(rh, port_id);
253 return EOK;
254 }
255}
256
257/**
258 * Deal with a detached device.
259 */
260static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
261{
262 assert(rh);
263
264 /* Find XHCI device by the port. */
265 xhci_device_t *dev = rh->devices_by_port[port_id - 1];
266 if (!dev) {
267 /* Must be extraneous call. */
268 return EOK;
269 }
270
271 usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
272 XHCI_DEV_ARGS(*dev), port_id);
273
274 /* Mark the device as detached. */
275 fibril_mutex_lock(&rh->device.base.guard);
276 list_remove(&dev->base.link);
277 rh->devices_by_port[port_id - 1] = NULL;
278 fibril_mutex_unlock(&rh->device.base.guard);
279
280 /* Remove device from XHCI bus. */
281 bus_device_gone(&dev->base);
282
283 return EOK;
284}
285
286typedef int (*rh_event_handler_t)(xhci_rh_t *, uint8_t);
287
288typedef struct rh_event_args {
289 xhci_rh_t *rh;
290 uint8_t port_id;
291 rh_event_handler_t handler;
292} rh_event_args_t;
293
294static int rh_event_handler_fibril(void *arg) {
295 rh_event_args_t *rh_args = arg;
296 xhci_rh_t *rh = rh_args->rh;
297 uint8_t port_id = rh_args->port_id;
298 rh_event_handler_t handler = rh_args->handler;
299
300 free(rh_args);
301
302 return handler(rh, port_id);
303}
304
305static fid_t handle_in_fibril(xhci_rh_t *rh, uint8_t port_id, rh_event_handler_t handler)
306{
307 rh_event_args_t *args = malloc(sizeof(*args));
308 *args = (rh_event_args_t) {
309 .rh = rh,
310 .port_id = port_id,
311 .handler = handler,
312 };
313
314 const fid_t fid = fibril_create(rh_event_handler_fibril, args);
315 fibril_add_ready(fid);
316 return fid;
317}
318
319/**
320 * Handle all changes on specified port.
321 */
322void xhci_rh_handle_port_change(xhci_rh_t *rh, uint8_t port_id)
323{
324 fibril_mutex_lock(&rh->event_guard);
325 xhci_port_regs_t * const regs = &rh->hc->op_regs->portrs[port_id - 1];
326
327 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32) & port_change_mask;
328
329 while (events) {
330 XHCI_REG_SET_FIELD(&regs->portsc, events, 32);
331
332 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
333 usb_log_info("Connected state changed on port %u.", port_id);
334 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
335
336 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
337 if (connected) {
338 handle_in_fibril(rh, port_id, handle_connected_device);
339 } else {
340 handle_in_fibril(rh, port_id, handle_disconnected_device);
341 }
342 }
343
344 if (events != 0)
345 rh_event_run_handlers(rh, port_id, &events);
346
347 if (events != 0)
348 usb_log_debug("RH port %u change not handled: 0x%x", port_id, events);
349
350 /* Make sure that PSCEG is 0 before exiting the loop. */
351 events = XHCI_REG_RD_FIELD(&regs->portsc, 32) & port_change_mask;
352 }
353
354 fibril_mutex_unlock(&rh->event_guard);
355}
356
357/**
358 * Get a port speed for a given port id.
359 */
360const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
361{
362 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
363
364 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
365 return &rh->hc->speeds[psiv];
366}
367
368/**
369 * @}
370 */
Note: See TracBrowser for help on using the repository browser.