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

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

xhci: use device_t for bookkeeping

This started as a little refactoring to move active transfer batch to endpoint. Finding the EP in handler needs devices indexed by slot id. Then I found out we do not use the device_t extendable mechanism. Then there were a lot of errors found while doing all this…

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