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

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

Very rudimentary support for device disconnection.

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