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

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

xhci: connecting devices deeper than to roothub

It still does not work, because the address command fails, but there should not be any fundamental problem.

  • Property mode set to 100644
File size: 12.2 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/utils/malloc32.h>
41#include <usb/host/bus.h>
42#include <usb/host/ddf_helpers.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
63int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc, ddf_dev_t *device)
64{
65 assert(rh);
66 assert(hc);
67
68 rh->hc = hc;
69 rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
70 rh->devices = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
71 rh->hc_device = device;
72
73 const int err = device_init(&rh->device.base);
74 if (err)
75 return err;
76
77 /* Initialize route string */
78 rh->device.route_str = 0;
79 rh->device.tier = 0;
80
81 return EOK;
82}
83
84/** Create a device node for device directly connected to RH.
85 */
86static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
87{
88 int err;
89 assert(rh);
90 assert(rh->hc_device);
91
92 assert(rh->devices[port_id - 1] == NULL);
93
94 xhci_bus_t *bus = &rh->hc->bus;
95
96 device_t *dev = hcd_ddf_device_create(rh->hc_device, bus->base.device_size);
97 if (!dev) {
98 usb_log_error("Failed to create USB device function.");
99 return ENOMEM;
100 }
101
102 const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
103 xhci_device_t *xhci_dev = xhci_device_get(dev);
104 xhci_dev->hc = rh->hc;
105 xhci_dev->usb3 = port_speed->major == 3;
106 xhci_dev->rh_port = port_id;
107
108 dev->hub = &rh->device.base;
109 dev->port = port_id;
110 dev->speed = port_speed->usb_speed;
111
112 if ((err = xhci_bus_enumerate_device(bus, rh->hc, dev))) {
113 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
114 return err;
115 }
116
117 if (!ddf_fun_get_name(dev->fun)) {
118 device_set_default_name(dev);
119 }
120
121 if ((err = ddf_fun_bind(dev->fun))) {
122 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
123 goto err_usb_dev;
124 }
125
126 fibril_mutex_lock(&rh->device.base.guard);
127 list_append(&dev->link, &rh->device.base.devices);
128 rh->devices[port_id - 1] = xhci_dev;
129 fibril_mutex_unlock(&rh->device.base.guard);
130
131 return EOK;
132
133err_usb_dev:
134 hcd_ddf_device_destroy(dev);
135 return err;
136}
137
138static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
139{
140 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
141
142 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
143 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
144
145 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
146
147 if (speed->major == 3) {
148 if (link_state == 0) {
149 /* USB3 is automatically advanced to enabled. */
150 return rh_setup_device(rh, port_id);
151 }
152 else if (link_state == 5) {
153 /* USB 3 failed to enable. */
154 usb_log_error("USB 3 port couldn't be enabled.");
155 return EAGAIN;
156 }
157 else {
158 usb_log_error("USB 3 port is in invalid state %u.", link_state);
159 return EINVAL;
160 }
161 }
162 else {
163 usb_log_debug("USB 2 device attached, issuing reset.");
164 xhci_rh_reset_port(rh, port_id);
165 /*
166 FIXME: we need to wait for the event triggered by the reset
167 and then alloc_dev()... can't it be done directly instead of
168 going around?
169 */
170 return EOK;
171 }
172}
173
174/** Deal with a detached device.
175 */
176static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
177{
178 assert(rh);
179 int err;
180
181 /* Find XHCI device by the port. */
182 xhci_device_t *dev = rh->devices[port_id - 1];
183 if (!dev) {
184 /* Must be extraneous call. */
185 return EOK;
186 }
187
188 usb_log_info("Device '%s' at port %u has been disconnected.", ddf_fun_get_name(dev->base.fun), port_id);
189
190 /* Block creation of new endpoints and transfers. */
191 fibril_mutex_lock(&dev->base.guard);
192 dev->online = false;
193 fibril_mutex_unlock(&dev->base.guard);
194
195 fibril_mutex_lock(&rh->device.base.guard);
196 list_remove(&dev->base.link);
197 rh->devices[port_id - 1] = NULL;
198 fibril_mutex_unlock(&rh->device.base.guard);
199
200 usb_log_debug2("Aborting all active transfers to '%s'.", ddf_fun_get_name(dev->base.fun));
201
202 /* Abort running transfers. */
203 for (size_t i = 0; i < ARRAY_SIZE(dev->endpoints); ++i) {
204 xhci_endpoint_t *ep = dev->endpoints[i];
205 if (!ep || !ep->base.active)
206 continue;
207
208 /* FIXME: This is racy. */
209 if ((err = xhci_transfer_abort(&ep->active_transfer))) {
210 usb_log_warning("Failed to abort active %s transfer to "
211 " endpoint %d of detached device '%s': %s",
212 usb_str_transfer_type(ep->base.transfer_type),
213 ep->base.endpoint, ddf_fun_get_name(dev->base.fun),
214 str_error(err));
215 }
216 }
217
218 /* TODO: Figure out how to handle errors here. So far, they are reported and skipped. */
219 /* TODO: Move parts of the code below to xhci_bus_remove_device() */
220
221 /* Make DDF (and all drivers) forget about the device. */
222 if ((err = ddf_fun_unbind(dev->base.fun))) {
223 usb_log_warning("Failed to unbind DDF function of detached device '%s': %s",
224 ddf_fun_get_name(dev->base.fun), str_error(err));
225 }
226
227 /* Unregister EP0. */
228 if ((err = bus_remove_endpoint(&rh->hc->bus.base, &dev->endpoints[0]->base))) {
229 usb_log_warning("Failed to unregister configuration endpoint of device '%s' from XHCI bus: %s",
230 ddf_fun_get_name(dev->base.fun), str_error(err));
231 }
232
233 /* Deconfigure device. */
234 if ((err = hc_deconfigure_device(rh->hc, dev->slot_id))) {
235 usb_log_warning("Failed to deconfigure detached device '%s': %s",
236 ddf_fun_get_name(dev->base.fun), str_error(err));
237 }
238
239 /* TODO: Free EP0 structures. */
240 /* TODO: Destroy EP0 by removing its last reference. */
241
242 /* Remove device from XHCI bus. */
243 if ((err = xhci_bus_remove_device(&rh->hc->bus, rh->hc, &dev->base))) {
244 usb_log_warning("Failed to remove device '%s' from XHCI bus: %s",
245 ddf_fun_get_name(dev->base.fun), str_error(err));
246 }
247
248 /* Disable device slot. */
249 if ((err = hc_disable_slot(rh->hc, dev->slot_id))) {
250 usb_log_warning("Failed to disable slot for device '%s': %s",
251 ddf_fun_get_name(dev->base.fun), str_error(err));
252 }
253
254 /* Destroy DDF device. */
255 hcd_ddf_device_destroy(&dev->base);
256
257 // TODO: Free device context.
258 // TODO: Free TRB rings.
259 // TODO: Figure out what was forgotten and free that as well.
260
261 return EOK;
262}
263
264/** Handle an incoming Port Change Detected Event.
265 */
266int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
267{
268 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
269 usb_log_debug("Port status change event detected for port %u.", port_id);
270
271 /**
272 * We can't be sure that the port change this event announces is the
273 * only port change that happened (see section 4.19.2 of the xHCI
274 * specification). Therefore, we just check all ports for changes.
275 */
276 xhci_rh_handle_port_change(&hc->rh);
277
278 return EOK;
279}
280
281void xhci_rh_handle_port_change(xhci_rh_t *rh)
282{
283 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
284 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
285
286 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
287 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
288
289 events &= port_change_mask;
290
291 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
292 usb_log_info("Connected state changed on port %u.", i);
293 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
294
295 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
296 if (connected) {
297 handle_connected_device(rh, i);
298 } else {
299 handle_disconnected_device(rh, i);
300 }
301 }
302
303 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
304 usb_log_info("Port enabled changed on port %u.", i);
305 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
306 }
307
308 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
309 usb_log_info("Warm port reset on port %u completed.", i);
310 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
311 }
312
313 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
314 usb_log_info("Over-current change on port %u.", i);
315 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
316 }
317
318 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
319 usb_log_info("Port reset on port %u completed.", i);
320 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
321
322 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
323 if (speed->major != 3) {
324 /* FIXME: We probably don't want to do this
325 * every time USB2 port is reset. This is a
326 * temporary workaround. */
327 rh_setup_device(rh, i);
328 }
329 }
330
331 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
332 usb_log_info("Port link state changed on port %u.", i);
333 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
334 }
335
336 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
337 usb_log_info("Port %u failed to configure link.", i);
338 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
339 }
340
341 if (events) {
342 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
343 }
344 }
345
346 /**
347 * Theory:
348 *
349 * Although more events could have happened while processing, the PCD
350 * bit in USBSTS will be set on every change. Because the PCD is
351 * cleared even before the interrupt is cleared, it is safe to assume
352 * that this handler will be called again.
353 *
354 * But because we could have handled the event in previous run of this
355 * handler, it is not an error when no event is detected.
356 *
357 * Reality:
358 *
359 * The PCD bit is never set. TODO Check why the interrupt never carries
360 * the PCD flag. Possibly repeat the checking until we're sure the
361 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
362 */
363}
364
365static inline int get_hub_available_bandwidth(xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx) {
366 // TODO: find a correct place for this function + API
367 // We need speed, because a root hub device has both USB 2 and USB 3 speeds
368 // and the command can query only one of them
369 // ctx is an out parameter as of now
370 assert(dev);
371 assert(ctx);
372
373 xhci_port_bandwidth_ctx_t *in_ctx = malloc32(sizeof(xhci_port_bandwidth_ctx_t));
374 if (!in_ctx) {
375 return ENOMEM;
376 }
377
378 xhci_cmd_t cmd;
379 xhci_cmd_init(&cmd, XHCI_CMD_GET_PORT_BANDWIDTH);
380
381 cmd.bandwidth_ctx = in_ctx;
382 cmd.device_speed = speed;
383
384 int err;
385 if ((err = xhci_cmd_sync(dev->hc, &cmd))) {
386 goto end;
387 }
388
389 memcpy(ctx, in_ctx, sizeof(xhci_port_bandwidth_ctx_t));
390
391end:
392 xhci_cmd_fini(&cmd);
393 return EOK;
394}
395
396const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
397{
398 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
399
400 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
401 return &rh->hc->speeds[psiv];
402}
403
404int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
405{
406 usb_log_debug2("Resetting port %u.", port);
407 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
408 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
409
410 return EOK;
411}
412
413int xhci_rh_fini(xhci_rh_t *rh)
414{
415 /* TODO: Implement me! */
416 usb_log_debug2("Called xhci_rh_fini().");
417
418 free(rh->devices);
419
420 return EOK;
421}
422
423/**
424 * @}
425 */
Note: See TracBrowser for help on using the repository browser.