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

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

Vaguer log level. Extracted timeout constant from command completion waiting and increased it for a bit.

  • Property mode set to 100644
File size: 12.4 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 hc->rh.hc_device = device;
71
72 return device_init(&hc->rh.device);
73}
74
75// TODO: Check device deallocation, we free device_ctx in hc.c, not
76// sure about the other structs.
77// TODO: This currently assumes the device is attached to rh directly.
78// Also, we should consider moving a lot of functionailty to xhci bus
79int xhci_rh_address_device(xhci_rh_t *rh, device_t *dev, xhci_bus_t *bus)
80{
81 int err;
82 xhci_hc_t *hc = rh->hc;
83
84 xhci_cmd_t cmd;
85 xhci_cmd_init(&cmd);
86
87 /* XXX Certainly not generic solution. */
88 uint32_t route_str = 0;
89
90 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, dev->port);
91
92 xhci_send_enable_slot_command(hc, &cmd);
93 if ((err = xhci_cmd_wait(&cmd)) != EOK)
94 return err;
95
96 uint32_t slot_id = cmd.slot_id;
97
98 usb_log_debug2("Obtained slot ID: %u.\n", slot_id);
99 xhci_cmd_fini(&cmd);
100
101 xhci_input_ctx_t *ictx = malloc(sizeof(xhci_input_ctx_t));
102 if (!ictx) {
103 return ENOMEM;
104 }
105
106 memset(ictx, 0, sizeof(xhci_input_ctx_t));
107
108 XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 0);
109 XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
110
111 /* Initialize slot_ctx according to section 4.3.3 point 3. */
112 /* Attaching to root hub port, root string equals to 0. */
113 XHCI_SLOT_ROOT_HUB_PORT_SET(ictx->slot_ctx, dev->port);
114 XHCI_SLOT_CTX_ENTRIES_SET(ictx->slot_ctx, 1);
115 XHCI_SLOT_ROUTE_STRING_SET(ictx->slot_ctx, route_str);
116
117 xhci_trb_ring_t *ep_ring = malloc(sizeof(xhci_trb_ring_t));
118 if (!ep_ring) {
119 err = ENOMEM;
120 goto err_ictx;
121 }
122
123 err = xhci_trb_ring_init(ep_ring);
124 if (err)
125 goto err_ring;
126
127 XHCI_EP_TYPE_SET(ictx->endpoint_ctx[0], EP_TYPE_CONTROL);
128 // TODO: must be changed with a command after USB descriptor is read
129 // See 4.6.5 in XHCI specification, first note
130 XHCI_EP_MAX_PACKET_SIZE_SET(ictx->endpoint_ctx[0],
131 speed->major == 3 ? 512 : 8);
132 XHCI_EP_MAX_BURST_SIZE_SET(ictx->endpoint_ctx[0], 0);
133 /* FIXME physical pointer? */
134 XHCI_EP_TR_DPTR_SET(ictx->endpoint_ctx[0], ep_ring->dequeue);
135 XHCI_EP_DCS_SET(ictx->endpoint_ctx[0], 1);
136 XHCI_EP_INTERVAL_SET(ictx->endpoint_ctx[0], 0);
137 XHCI_EP_MAX_P_STREAMS_SET(ictx->endpoint_ctx[0], 0);
138 XHCI_EP_MULT_SET(ictx->endpoint_ctx[0], 0);
139 XHCI_EP_ERROR_COUNT_SET(ictx->endpoint_ctx[0], 3);
140
141 xhci_device_ctx_t *dctx = malloc32(sizeof(xhci_device_ctx_t));
142 if (!dctx) {
143 err = ENOMEM;
144 goto err_ring;
145 }
146 memset(dctx, 0, sizeof(xhci_device_ctx_t));
147
148 hc->dcbaa[slot_id] = addr_to_phys(dctx);
149
150 memset(&hc->dcbaa_virt[slot_id], 0, sizeof(xhci_virt_device_ctx_t));
151 hc->dcbaa_virt[slot_id].dev_ctx = dctx;
152 hc->dcbaa_virt[slot_id].trs[0] = ep_ring;
153
154 xhci_cmd_init(&cmd);
155 cmd.slot_id = slot_id;
156 xhci_send_address_device_command(hc, &cmd, ictx);
157 if ((err = xhci_cmd_wait(&cmd)) != EOK)
158 goto err_dctx;
159
160 xhci_cmd_fini(&cmd);
161
162 dev->address = XHCI_SLOT_DEVICE_ADDRESS(dctx->slot_ctx);
163 usb_log_debug2("Obtained USB address: %d.\n", dev->address);
164
165 // Ask libusbhost to create a control endpoint for EP0.
166 bus_t *bus_base = &bus->base;
167 usb_target_t ep0_target = { .address = dev->address, .endpoint = 0 };
168 usb_direction_t ep0_direction = USB_DIRECTION_BOTH;
169
170 // TODO: Should this call be unified with other calls to `bus_add_ep()`?
171 err = bus_add_ep(bus_base, dev, ep0_target.endpoint, ep0_direction,
172 USB_TRANSFER_CONTROL, CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE, 1);
173
174 if (err != EOK)
175 goto err_add_ep;
176
177 // Save all data structures in the corresponding xhci_device_t.
178 endpoint_t *ep0_base = bus_find_endpoint(bus_base, ep0_target, ep0_direction);
179 xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
180 xhci_device_t *xhci_dev = ep0->device;
181
182 xhci_dev->device = dev;
183 xhci_dev->slot_id = slot_id;
184 xhci_dev->usb3 = speed->major == 3;
185 xhci_dev->hc = hc;
186
187 // TODO: Save anything else?
188
189 return EOK;
190
191err_add_ep:
192err_dctx:
193 if (dctx) {
194 free(dctx);
195 hc->dcbaa[slot_id] = 0;
196 memset(&hc->dcbaa_virt[slot_id], 0, sizeof(xhci_virt_device_ctx_t));
197 }
198err_ring:
199 if (ep_ring) {
200 xhci_trb_ring_fini(ep_ring);
201 free(ep_ring);
202 }
203err_ictx:
204 free(ictx);
205 return err;
206}
207
208/** Create a device node for device directly connected to RH.
209 */
210static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
211{
212 int err;
213 assert(rh);
214 assert(rh->hc_device);
215
216 xhci_bus_t *bus = &rh->hc->bus;
217
218 device_t *dev = hcd_ddf_device_create(rh->hc_device, bus->base.device_size);
219 if (!dev) {
220 usb_log_error("Failed to create USB device function.");
221 return ENOMEM;
222 }
223
224 dev->hub = &rh->device;
225 dev->port = port_id;
226
227 if ((err = xhci_bus_enumerate_device(bus, rh->hc, dev))) {
228 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
229 return err;
230 }
231
232 if (!ddf_fun_get_name(dev->fun)) {
233 device_set_default_name(dev);
234 }
235
236 if ((err = ddf_fun_bind(dev->fun))) {
237 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
238 goto err_usb_dev;
239 }
240
241 fibril_mutex_lock(&rh->device.guard);
242 list_append(&dev->link, &rh->device.devices);
243 fibril_mutex_unlock(&rh->device.guard);
244
245 return EOK;
246
247err_usb_dev:
248 hcd_ddf_device_destroy(dev);
249 return err;
250}
251
252static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
253{
254 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
255
256 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
257 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
258
259 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
260
261 if (speed->major == 3) {
262 if (link_state == 0) {
263 /* USB3 is automatically advanced to enabled. */
264 return rh_setup_device(rh, port_id);
265 }
266 else if (link_state == 5) {
267 /* USB 3 failed to enable. */
268 usb_log_error("USB 3 port couldn't be enabled.");
269 return EAGAIN;
270 }
271 else {
272 usb_log_error("USB 3 port is in invalid state %u.", link_state);
273 return EINVAL;
274 }
275 }
276 else {
277 usb_log_debug("USB 2 device attached, issuing reset.");
278 xhci_rh_reset_port(rh, port_id);
279 /*
280 FIXME: we need to wait for the event triggered by the reset
281 and then alloc_dev()... can't it be done directly instead of
282 going around?
283 */
284 return EOK;
285 }
286}
287
288/** Handle an incoming Port Change Detected Event.
289 */
290int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
291{
292 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
293 usb_log_debug("Port status change event detected for port %u.", port_id);
294
295 /**
296 * We can't be sure that the port change this event announces is the
297 * only port change that happened (see section 4.19.2 of the xHCI
298 * specification). Therefore, we just check all ports for changes.
299 */
300 xhci_rh_handle_port_change(&hc->rh);
301
302 return EOK;
303}
304
305void xhci_rh_handle_port_change(xhci_rh_t *rh)
306{
307 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
308 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
309
310 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
311 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
312
313 events &= port_change_mask;
314
315 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
316 usb_log_info("Connected state changed on port %u.", i);
317 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
318
319 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
320 if (connected)
321 handle_connected_device(rh, i);
322 }
323
324 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
325 usb_log_info("Port enabled changed on port %u.", i);
326 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
327 }
328
329 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
330 usb_log_info("Warm port reset on port %u completed.", i);
331 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
332 }
333
334 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
335 usb_log_info("Over-current change on port %u.", i);
336 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
337 }
338
339 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
340 usb_log_info("Port reset on port %u completed.", i);
341 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
342
343 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
344 if (speed->major != 3) {
345 /* FIXME: We probably don't want to do this
346 * every time USB2 port is reset. This is a
347 * temporary workaround. */
348 rh_setup_device(rh, i);
349 }
350 }
351
352 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
353 usb_log_info("Port link state changed on port %u.", i);
354 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
355 }
356
357 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
358 usb_log_info("Port %u failed to configure link.", i);
359 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
360 }
361
362 if (events) {
363 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
364 }
365 }
366
367 /**
368 * Theory:
369 *
370 * Although more events could have happened while processing, the PCD
371 * bit in USBSTS will be set on every change. Because the PCD is
372 * cleared even before the interrupt is cleared, it is safe to assume
373 * that this handler will be called again.
374 *
375 * But because we could have handled the event in previous run of this
376 * handler, it is not an error when no event is detected.
377 *
378 * Reality:
379 *
380 * The PCD bit is never set. TODO Check why the interrupt never carries
381 * the PCD flag. Possibly repeat the checking until we're sure the
382 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
383 */
384}
385
386static inline int get_hub_available_bandwidth(xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx) {
387 // TODO: find a correct place for this function + API
388 // We need speed, because a root hub device has both USB 2 and USB 3 speeds
389 // and the command can query only one of them
390 // ctx is an out parameter as of now
391 assert(dev);
392
393 ctx = malloc(sizeof(xhci_port_bandwidth_ctx_t));
394 if(!ctx)
395 return ENOMEM;
396
397 xhci_cmd_t cmd;
398 xhci_cmd_init(&cmd);
399
400 xhci_get_port_bandwidth_command(dev->hc, &cmd, ctx, speed);
401
402 int err = xhci_cmd_wait(&cmd);
403 if(err != EOK) {
404 free(ctx);
405 ctx = NULL;
406 }
407
408 return EOK;
409}
410
411const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
412{
413 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
414
415 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
416 return &rh->speeds[psiv];
417}
418
419int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
420{
421 usb_log_debug2("Resetting port %u.", port);
422 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
423 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
424
425 return EOK;
426}
427
428int xhci_rh_fini(xhci_rh_t *rh)
429{
430 /* TODO: Implement me! */
431 usb_log_debug2("Called xhci_rh_fini().");
432 return EOK;
433}
434
435/**
436 * @}
437 */
Note: See TracBrowser for help on using the repository browser.