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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 81487c4a was 89cefe78, checked in by Petr Manek <petr.manek@…>, 8 years ago

Refactored transfer DS allocation in preparation for streams. Also, converted EP context setup to table.

  • Property mode set to 100644
File size: 14.3 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 hc->rh.hc_device = device;
72
73 return device_init(&hc->rh.device);
74}
75
76static void setup_control_ep0_ctx(xhci_ep_ctx_t *ctx, xhci_trb_ring_t *ring,
77 const xhci_port_speed_t *speed)
78{
79 XHCI_EP_TYPE_SET(*ctx, EP_TYPE_CONTROL);
80 // TODO: must be changed with a command after USB descriptor is read
81 // See 4.6.5 in XHCI specification, first note
82 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, speed->major == 3 ? 512 : 8);
83 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, 0);
84 XHCI_EP_TR_DPTR_SET(*ctx, ring->dequeue);
85 XHCI_EP_DCS_SET(*ctx, 1);
86 XHCI_EP_INTERVAL_SET(*ctx, 0);
87 XHCI_EP_MAX_P_STREAMS_SET(*ctx, 0);
88 XHCI_EP_MULT_SET(*ctx, 0);
89 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
90}
91
92// TODO: This currently assumes the device is attached to rh directly.
93// Also, we should consider moving a lot of functionailty to xhci bus
94int xhci_rh_address_device(xhci_rh_t *rh, device_t *dev, xhci_bus_t *bus)
95{
96 int err;
97 xhci_device_t *xhci_dev = xhci_device_get(dev);
98
99 /* FIXME: Certainly not generic solution. */
100 const uint32_t route_str = 0;
101
102 xhci_dev->hc = rh->hc;
103
104 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, dev->port);
105 xhci_dev->usb3 = speed->major == 3;
106
107 /* Enable new slot */
108 if ((err = hc_enable_slot(rh->hc, &xhci_dev->slot_id)) != EOK)
109 return err;
110 usb_log_debug2("Obtained slot ID: %u.\n", xhci_dev->slot_id);
111
112 /* Setup input context */
113 xhci_input_ctx_t *ictx = malloc32(sizeof(xhci_input_ctx_t));
114 if (!ictx)
115 return ENOMEM;
116 memset(ictx, 0, sizeof(xhci_input_ctx_t));
117
118 XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 0);
119 XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
120
121 /* Initialize slot_ctx according to section 4.3.3 point 3. */
122 /* Attaching to root hub port, root string equals to 0. */
123 XHCI_SLOT_ROOT_HUB_PORT_SET(ictx->slot_ctx, dev->port);
124 XHCI_SLOT_CTX_ENTRIES_SET(ictx->slot_ctx, 1);
125 XHCI_SLOT_ROUTE_STRING_SET(ictx->slot_ctx, route_str);
126
127 endpoint_t *ep0_base = bus_create_endpoint(&rh->hc->bus.base);
128 if (!ep0_base)
129 goto err_ictx;
130 xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
131
132 /* Control endpoints don't use streams. */
133 /* FIXME: Sync this with xhci_device_add_endpoint. */
134 ep0->max_streams = 0;
135 ep0->max_burst = 0;
136 ep0->mult = 0;
137 if ((err = xhci_endpoint_alloc_transfer_ds(ep0)))
138 goto err_ictx;
139
140 setup_control_ep0_ctx(&ictx->endpoint_ctx[0], &ep0->ring, speed);
141
142 /* Setup and register device context */
143 xhci_device_ctx_t *dctx = malloc32(sizeof(xhci_device_ctx_t));
144 if (!dctx) {
145 err = ENOMEM;
146 goto err_ep;
147 }
148 xhci_dev->dev_ctx = dctx;
149 rh->hc->dcbaa[xhci_dev->slot_id] = addr_to_phys(dctx);
150 memset(dctx, 0, sizeof(xhci_device_ctx_t));
151
152 /* Address device */
153 if ((err = hc_address_device(rh->hc, xhci_dev->slot_id, ictx)) != EOK)
154 goto err_dctx;
155 dev->address = XHCI_SLOT_DEVICE_ADDRESS(dctx->slot_ctx);
156 usb_log_debug2("Obtained USB address: %d.\n", dev->address);
157
158 /* From now on, the device is officially online, yay! */
159 fibril_mutex_lock(&dev->guard);
160 xhci_dev->online = true;
161 fibril_mutex_unlock(&dev->guard);
162
163 // XXX: Going around bus, duplicating code
164 ep0_base->device = dev;
165 ep0_base->target.address = dev->address;
166 ep0_base->target.endpoint = 0;
167 ep0_base->direction = USB_DIRECTION_BOTH;
168 ep0_base->transfer_type = USB_TRANSFER_CONTROL;
169 ep0_base->max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE;
170 ep0_base->packets = 1;
171 ep0_base->bandwidth = CTRL_PIPE_MIN_PACKET_SIZE;
172
173 bus_register_endpoint(&rh->hc->bus.base, ep0_base);
174
175 if (!rh->devices[dev->port - 1]) {
176 /* Only save the device if it's the first one connected to this port. */
177 rh->devices[dev->port - 1] = xhci_dev;
178 }
179
180 free32(ictx);
181 return EOK;
182
183err_dctx:
184 free32(dctx);
185 rh->hc->dcbaa[xhci_dev->slot_id] = 0;
186err_ep:
187 xhci_endpoint_fini(ep0);
188 free(ep0);
189err_ictx:
190 free32(ictx);
191 return err;
192}
193
194/** Create a device node for device directly connected to RH.
195 */
196static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
197{
198 int err;
199 assert(rh);
200 assert(rh->hc_device);
201
202 xhci_bus_t *bus = &rh->hc->bus;
203
204 device_t *dev = hcd_ddf_device_create(rh->hc_device, bus->base.device_size);
205 if (!dev) {
206 usb_log_error("Failed to create USB device function.");
207 return ENOMEM;
208 }
209
210 dev->hub = &rh->device;
211 dev->port = port_id;
212
213 if ((err = xhci_bus_enumerate_device(bus, rh->hc, dev))) {
214 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
215 return err;
216 }
217
218 if (!ddf_fun_get_name(dev->fun)) {
219 device_set_default_name(dev);
220 }
221
222 if ((err = ddf_fun_bind(dev->fun))) {
223 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
224 goto err_usb_dev;
225 }
226
227 fibril_mutex_lock(&rh->device.guard);
228 list_append(&dev->link, &rh->device.devices);
229 fibril_mutex_unlock(&rh->device.guard);
230
231 return EOK;
232
233err_usb_dev:
234 hcd_ddf_device_destroy(dev);
235 return err;
236}
237
238static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
239{
240 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
241
242 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
243 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
244
245 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
246
247 if (speed->major == 3) {
248 if (link_state == 0) {
249 /* USB3 is automatically advanced to enabled. */
250 return rh_setup_device(rh, port_id);
251 }
252 else if (link_state == 5) {
253 /* USB 3 failed to enable. */
254 usb_log_error("USB 3 port couldn't be enabled.");
255 return EAGAIN;
256 }
257 else {
258 usb_log_error("USB 3 port is in invalid state %u.", link_state);
259 return EINVAL;
260 }
261 }
262 else {
263 usb_log_debug("USB 2 device attached, issuing reset.");
264 xhci_rh_reset_port(rh, port_id);
265 /*
266 FIXME: we need to wait for the event triggered by the reset
267 and then alloc_dev()... can't it be done directly instead of
268 going around?
269 */
270 return EOK;
271 }
272}
273
274/** Deal with a detached device.
275 */
276static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
277{
278 assert(rh);
279 int err;
280
281 /* Find XHCI device by the port. */
282 xhci_device_t *dev = rh->devices[port_id - 1];
283 if (!dev) {
284 /* Must be extraneous call. */
285 return EOK;
286 }
287
288 usb_log_info("Device '%s' at port %u has been disconnected.", ddf_fun_get_name(dev->base.fun), port_id);
289
290 /* Block creation of new endpoints and transfers. */
291 fibril_mutex_lock(&dev->base.guard);
292 dev->online = false;
293 fibril_mutex_unlock(&dev->base.guard);
294
295 fibril_mutex_lock(&rh->device.guard);
296 list_remove(&dev->base.link);
297 fibril_mutex_unlock(&rh->device.guard);
298
299 rh->devices[port_id - 1] = NULL;
300 usb_log_debug2("Aborting all active transfers to '%s'.", ddf_fun_get_name(dev->base.fun));
301
302 /* Abort running transfers. */
303 for (size_t i = 0; i < ARRAY_SIZE(dev->endpoints); ++i) {
304 xhci_endpoint_t *ep = dev->endpoints[i];
305 if (!ep || !ep->base.active)
306 continue;
307
308 if ((err = xhci_transfer_abort(&ep->active_transfer))) {
309 usb_log_warning("Failed to abort active %s transfer to "
310 " endpoint %d of detached device '%s': %s",
311 usb_str_transfer_type(ep->base.transfer_type),
312 ep->base.target.endpoint, ddf_fun_get_name(dev->base.fun),
313 str_error(err));
314 }
315 }
316
317 /* Make DDF (and all drivers) forget about the device. */
318 if ((err = ddf_fun_unbind(dev->base.fun))) {
319 usb_log_warning("Failed to unbind DDF function of detached device '%s': %s",
320 ddf_fun_get_name(dev->base.fun), str_error(err));
321 }
322
323 /* FIXME:
324 * A deadlock happens on the previous line. For some reason, the HID driver
325 * does not fully release its DDF function (tracked it down to release_endpoint
326 * in usb_remote.c so far).
327 *
328 * For that reason as well, the following 3 lines are untested.
329 */
330
331 xhci_bus_remove_device(&rh->hc->bus, rh->hc, &dev->base);
332 hc_disable_slot(rh->hc, dev->slot_id);
333 hcd_ddf_device_destroy(&dev->base);
334
335 // TODO: Free device context.
336 // TODO: Free TRB rings.
337 // TODO: Figure out what was forgotten and free that as well.
338
339 return EOK;
340}
341
342/** Handle an incoming Port Change Detected Event.
343 */
344int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
345{
346 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
347 usb_log_debug("Port status change event detected for port %u.", port_id);
348
349 /**
350 * We can't be sure that the port change this event announces is the
351 * only port change that happened (see section 4.19.2 of the xHCI
352 * specification). Therefore, we just check all ports for changes.
353 */
354 xhci_rh_handle_port_change(&hc->rh);
355
356 return EOK;
357}
358
359void xhci_rh_handle_port_change(xhci_rh_t *rh)
360{
361 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
362 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
363
364 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
365 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
366
367 events &= port_change_mask;
368
369 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
370 usb_log_info("Connected state changed on port %u.", i);
371 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
372
373 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
374 if (connected) {
375 handle_connected_device(rh, i);
376 } else {
377 handle_disconnected_device(rh, i);
378 }
379 }
380
381 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
382 usb_log_info("Port enabled changed on port %u.", i);
383 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
384 }
385
386 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
387 usb_log_info("Warm port reset on port %u completed.", i);
388 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
389 }
390
391 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
392 usb_log_info("Over-current change on port %u.", i);
393 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
394 }
395
396 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
397 usb_log_info("Port reset on port %u completed.", i);
398 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
399
400 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
401 if (speed->major != 3) {
402 /* FIXME: We probably don't want to do this
403 * every time USB2 port is reset. This is a
404 * temporary workaround. */
405 rh_setup_device(rh, i);
406 }
407 }
408
409 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
410 usb_log_info("Port link state changed on port %u.", i);
411 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
412 }
413
414 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
415 usb_log_info("Port %u failed to configure link.", i);
416 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
417 }
418
419 if (events) {
420 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
421 }
422 }
423
424 /**
425 * Theory:
426 *
427 * Although more events could have happened while processing, the PCD
428 * bit in USBSTS will be set on every change. Because the PCD is
429 * cleared even before the interrupt is cleared, it is safe to assume
430 * that this handler will be called again.
431 *
432 * But because we could have handled the event in previous run of this
433 * handler, it is not an error when no event is detected.
434 *
435 * Reality:
436 *
437 * The PCD bit is never set. TODO Check why the interrupt never carries
438 * the PCD flag. Possibly repeat the checking until we're sure the
439 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
440 */
441}
442
443static inline int get_hub_available_bandwidth(xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx) {
444 // TODO: find a correct place for this function + API
445 // We need speed, because a root hub device has both USB 2 and USB 3 speeds
446 // and the command can query only one of them
447 // ctx is an out parameter as of now
448 assert(dev);
449
450 ctx = malloc(sizeof(xhci_port_bandwidth_ctx_t));
451 if(!ctx)
452 return ENOMEM;
453
454 xhci_cmd_t cmd;
455 xhci_cmd_init(&cmd);
456
457 xhci_get_port_bandwidth_command(dev->hc, &cmd, ctx, speed);
458
459 int err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT);
460 if(err != EOK) {
461 free(ctx);
462 ctx = NULL;
463 }
464
465 return EOK;
466}
467
468const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
469{
470 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
471
472 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
473 return &rh->speeds[psiv];
474}
475
476int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
477{
478 usb_log_debug2("Resetting port %u.", port);
479 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
480 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
481
482 return EOK;
483}
484
485int xhci_rh_fini(xhci_rh_t *rh)
486{
487 /* TODO: Implement me! */
488 usb_log_debug2("Called xhci_rh_fini().");
489
490 free(rh->devices);
491
492 return EOK;
493}
494
495/**
496 * @}
497 */
Note: See TracBrowser for help on using the repository browser.