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

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

xhci: removed leftover function

  • Property mode set to 100644
File size: 10.2 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
[49e62998]83 fibril_mutex_initialize(&rh->event_guard);
84 fibril_condvar_initialize(&rh->event_ready);
85 fibril_condvar_initialize(&rh->event_handled);
86
[2cf28b9]87 return EOK;
[d32d51d]88}
89
[eb928c4]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
[49e62998]100static int rh_event_wait_timeout(xhci_rh_t *rh, suseconds_t timeout)
101{
102 assert(fibril_mutex_is_locked(&rh->event_guard));
103
104 ++rh->event_readers_waiting;
105 const int r = fibril_condvar_wait_timeout(&rh->event_ready, &rh->event_guard, timeout);
106 --rh->event_readers_waiting;
107 if (--rh->event_readers_to_go == 0)
108 fibril_condvar_broadcast(&rh->event_handled);
109 return r;
110}
111
112static void rh_event_run_handlers(xhci_rh_t *rh)
113{
114 fibril_mutex_lock(&rh->event_guard);
115 assert(rh->event_readers_to_go == 0);
116
117 rh->event_readers_to_go = rh->event_readers_waiting;
118 fibril_condvar_broadcast(&rh->event_ready);
119 while (rh->event_readers_to_go)
120 fibril_condvar_wait(&rh->event_handled, &rh->event_guard);
121 fibril_mutex_unlock(&rh->event_guard);
122}
123
[eb928c4]124/**
125 * Create and setup a device directly connected to RH. As the xHCI is not using
126 * a virtual usbhub device for RH, this routine is called for devices directly.
[20eaa82]127 */
[867b375]128static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
129{
[20eaa82]130 int err;
131 assert(rh);
132
[d33dc780]133 assert(rh->devices_by_port[port_id - 1] == NULL);
[2cf28b9]134
[32fb6bce]135 device_t *dev = hcd_ddf_fun_create(&rh->hc->base);
[20eaa82]136 if (!dev) {
137 usb_log_error("Failed to create USB device function.");
138 return ENOMEM;
139 }
140
[0206d35]141 const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
142 xhci_device_t *xhci_dev = xhci_device_get(dev);
143 xhci_dev->usb3 = port_speed->major == 3;
[2cf28b9]144 xhci_dev->rh_port = port_id;
[0206d35]145
[2cf28b9]146 dev->hub = &rh->device.base;
[20eaa82]147 dev->port = port_id;
[f668d60]148 dev->speed = port_speed->usb_speed;
[20eaa82]149
[eb928c4]150 if ((err = bus_device_enumerate(dev))) {
[20eaa82]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)) {
[6832245]156 bus_device_set_default_name(dev);
[20eaa82]157 }
158
159 if ((err = ddf_fun_bind(dev->fun))) {
[9620a54]160 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
161 XHCI_DEV_ARGS(*xhci_dev), str_error(err));
[20eaa82]162 goto err_usb_dev;
163 }
164
[2cf28b9]165 fibril_mutex_lock(&rh->device.base.guard);
166 list_append(&dev->link, &rh->device.base.devices);
[d33dc780]167 rh->devices_by_port[port_id - 1] = xhci_dev;
[2cf28b9]168 fibril_mutex_unlock(&rh->device.base.guard);
[867b375]169
[20eaa82]170 return EOK;
171
172err_usb_dev:
[32fb6bce]173 hcd_ddf_fun_destroy(dev);
[20eaa82]174 return err;
[867b375]175}
176
[49e62998]177
178static int rh_port_reset_sync(xhci_rh_t *rh, uint8_t port_id)
179{
180 int r;
181 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
182
183 fibril_mutex_lock(&rh->event_guard);
184 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
185
186 while (true) {
187 r = rh_event_wait_timeout(rh, 0);
188 if (r != EOK)
189 break;
190 if (rh->event.port_id == port_id
191 && rh->event.events & XHCI_REG_MASK(XHCI_PORT_PRC))
192 break;
193 }
194 fibril_mutex_unlock(&rh->event_guard);
195
196 return r;
197}
198
[eb928c4]199/**
200 * Handle a device connection. USB 3+ devices are set up directly, USB 2 and
201 * below first need to have their port reset.
202 */
[dcf0597]203static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
[c8bb7090]204{
[dcf0597]205 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
[472235a]206
[dcf0597]207 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
208 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
[7bd99bf]209
[dcf0597]210 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
[7bd99bf]211
[dcf0597]212 if (speed->major == 3) {
213 if (link_state == 0) {
214 /* USB3 is automatically advanced to enabled. */
[867b375]215 return rh_setup_device(rh, port_id);
[dcf0597]216 }
217 else if (link_state == 5) {
218 /* USB 3 failed to enable. */
219 usb_log_error("USB 3 port couldn't be enabled.");
220 return EAGAIN;
221 }
222 else {
223 usb_log_error("USB 3 port is in invalid state %u.", link_state);
224 return EINVAL;
225 }
226 }
227 else {
228 usb_log_debug("USB 2 device attached, issuing reset.");
[49e62998]229 const int err = rh_port_reset_sync(rh, port_id);
230 if (err)
231 return err;
232
233 rh_setup_device(rh, port_id);
[dcf0597]234 return EOK;
235 }
[816335c]236}
237
[eb928c4]238/**
239 * Deal with a detached device.
[f45c78f]240 */
241static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
242{
[a4e26882]243 assert(rh);
244
[766043c]245 /* Find XHCI device by the port. */
[d33dc780]246 xhci_device_t *dev = rh->devices_by_port[port_id - 1];
[766043c]247 if (!dev) {
[a4e26882]248 /* Must be extraneous call. */
[766043c]249 return EOK;
250 }
251
[9620a54]252 usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
253 XHCI_DEV_ARGS(*dev), port_id);
[a4e26882]254
[40a3bfa]255 /* Mark the device as detached. */
[2cf28b9]256 fibril_mutex_lock(&rh->device.base.guard);
[a4e26882]257 list_remove(&dev->base.link);
[d33dc780]258 rh->devices_by_port[port_id - 1] = NULL;
[2cf28b9]259 fibril_mutex_unlock(&rh->device.base.guard);
260
[31cca4f3]261 /* Remove device from XHCI bus. */
[9848c77]262 bus_device_gone(&dev->base);
[31cca4f3]263
[f45c78f]264 return EOK;
265}
266
[eb928c4]267/**
268 * Handle an incoming Port Change Detected Event.
[dcf0597]269 */
270int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
[c8bb7090]271{
[f7bd246]272 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
[dcf0597]273 usb_log_debug("Port status change event detected for port %u.", port_id);
[c8bb7090]274
[dcf0597]275 /**
276 * We can't be sure that the port change this event announces is the
277 * only port change that happened (see section 4.19.2 of the xHCI
278 * specification). Therefore, we just check all ports for changes.
279 */
280 xhci_rh_handle_port_change(&hc->rh);
[07c08ea]281
282 return EOK;
283}
284
[49e62998]285typedef int (*rh_event_handler_t)(xhci_rh_t *, uint8_t);
286
287typedef struct rh_event_args {
288 xhci_rh_t *rh;
289 uint8_t port_id;
290 rh_event_handler_t handler;
291} rh_event_args_t;
292
293static int rh_event_handler_fibril(void *arg) {
294 rh_event_args_t *rh_args = arg;
295 xhci_rh_t *rh = rh_args->rh;
296 uint8_t port_id = rh_args->port_id;
297 rh_event_handler_t handler = rh_args->handler;
298
299 free(rh_args);
300
301 return handler(rh, port_id);
302}
303
304static fid_t handle_in_fibril(xhci_rh_t *rh, uint8_t port_id, rh_event_handler_t handler)
305{
306 rh_event_args_t *args = malloc(sizeof(*args));
307 *args = (rh_event_args_t) {
308 .rh = rh,
309 .port_id = port_id,
310 .handler = handler,
311 };
312
313 const fid_t fid = fibril_create(rh_event_handler_fibril, args);
314 fibril_add_ready(fid);
315 return fid;
316}
317
[eb928c4]318/**
319 * Handle all changes on all ports.
320 */
[dcf0597]321void xhci_rh_handle_port_change(xhci_rh_t *rh)
[d32d51d]322{
[dcf0597]323 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
324 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
[07c08ea]325
[dcf0597]326 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
327 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
[5c5c9407]328
[dcf0597]329 events &= port_change_mask;
[5c5c9407]330
[dcf0597]331 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
332 usb_log_info("Connected state changed on port %u.", i);
333 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
[5c5c9407]334
[dcf0597]335 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
[f45c78f]336 if (connected) {
[49e62998]337 handle_in_fibril(rh, i, handle_connected_device);
[f45c78f]338 } else {
[49e62998]339 handle_in_fibril(rh, i, handle_disconnected_device);
[f45c78f]340 }
[dcf0597]341 }
[5c5c9407]342
[49e62998]343 if (events != 0) {
344 rh->event.port_id = i;
345 rh->event.events = events;
346 rh_event_run_handlers(rh);
[dcf0597]347 }
[916991b]348 }
[2297fab]349
[dcf0597]350 /**
351 * Theory:
352 *
353 * Although more events could have happened while processing, the PCD
354 * bit in USBSTS will be set on every change. Because the PCD is
355 * cleared even before the interrupt is cleared, it is safe to assume
356 * that this handler will be called again.
357 *
358 * But because we could have handled the event in previous run of this
359 * handler, it is not an error when no event is detected.
360 *
361 * Reality:
362 *
363 * The PCD bit is never set. TODO Check why the interrupt never carries
364 * the PCD flag. Possibly repeat the checking until we're sure the
365 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
366 */
[07c08ea]367}
368
[eb928c4]369/**
370 * Get a port speed for a given port id.
371 */
[dcf0597]372const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
[07c08ea]373{
[dcf0597]374 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
[916991b]375
[dcf0597]376 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
[f668d60]377 return &rh->hc->speeds[psiv];
[07c08ea]378}
379
[c8bb7090]380/**
381 * @}
382 */
Note: See TracBrowser for help on using the repository browser.