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

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

usbhost: refactoring

This commit moves interrupt, status and schedule to bus
operations. Then the purpose of hcd_t is better defined, and split into
hc_driver_t and hc_device_t. hc_driver_t is used to wrap driver
implementation by the library (similar to how usb_driver_t is used to
wrap usb device drivers). hc_device_t is used as a parent for hc_t
inside drivers, and is allocated inside the DDF device node.

To support these changes, some local identifiers were renamed, some
functions were moved and/or renamed and their arguments changed. The
most notable one being hcd_send_batch → bus_device_send_batch.

  • Property mode set to 100644
File size: 10.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/bus.h>
41#include <usb/host/ddf_helpers.h>
42#include <usb/host/dma_buffer.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_by_port = (xhci_device_t **) calloc(rh->max_ports, sizeof(xhci_device_t *));
71 rh->hc_device = device;
72
73 const int err = bus_device_init(&rh->device.base, &rh->hc->bus.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_by_port[port_id - 1] == NULL);
93
94 xhci_bus_t *bus = &rh->hc->bus;
95
96 device_t *dev = hcd_ddf_fun_create(&rh->hc->base);
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->usb3 = port_speed->major == 3;
105 xhci_dev->rh_port = port_id;
106
107 dev->hub = &rh->device.base;
108 dev->port = port_id;
109 dev->speed = port_speed->usb_speed;
110
111 if ((err = xhci_bus_enumerate_device(bus, dev))) {
112 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
113 return err;
114 }
115
116 if (!ddf_fun_get_name(dev->fun)) {
117 bus_device_set_default_name(dev);
118 }
119
120 if ((err = ddf_fun_bind(dev->fun))) {
121 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
122 XHCI_DEV_ARGS(*xhci_dev), 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_by_port[port_id - 1] = xhci_dev;
129 fibril_mutex_unlock(&rh->device.base.guard);
130
131 return EOK;
132
133err_usb_dev:
134 hcd_ddf_fun_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_by_port[port_id - 1];
183 if (!dev) {
184 /* Must be extraneous call. */
185 return EOK;
186 }
187
188 usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
189 XHCI_DEV_ARGS(*dev), port_id);
190
191 /* Mark the device as detached. */
192 fibril_mutex_lock(&rh->device.base.guard);
193 list_remove(&dev->base.link);
194 rh->devices_by_port[port_id - 1] = NULL;
195 fibril_mutex_unlock(&rh->device.base.guard);
196
197 /* Remove device from XHCI bus. */
198 if ((err = xhci_bus_remove_device(&rh->hc->bus, &dev->base))) {
199 usb_log_warning("Failed to remove device " XHCI_DEV_FMT " from XHCI bus: %s",
200 XHCI_DEV_ARGS(*dev), str_error(err));
201 }
202
203 return EOK;
204}
205
206/** Handle an incoming Port Change Detected Event.
207 */
208int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
209{
210 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
211 usb_log_debug("Port status change event detected for port %u.", port_id);
212
213 /**
214 * We can't be sure that the port change this event announces is the
215 * only port change that happened (see section 4.19.2 of the xHCI
216 * specification). Therefore, we just check all ports for changes.
217 */
218 xhci_rh_handle_port_change(&hc->rh);
219
220 return EOK;
221}
222
223void xhci_rh_handle_port_change(xhci_rh_t *rh)
224{
225 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
226 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
227
228 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
229 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
230
231 events &= port_change_mask;
232
233 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
234 usb_log_info("Connected state changed on port %u.", i);
235 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
236
237 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
238 if (connected) {
239 handle_connected_device(rh, i);
240 } else {
241 handle_disconnected_device(rh, i);
242 }
243 }
244
245 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
246 usb_log_info("Port enabled changed on port %u.", i);
247 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
248 }
249
250 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
251 usb_log_info("Warm port reset on port %u completed.", i);
252 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
253 }
254
255 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
256 usb_log_info("Over-current change on port %u.", i);
257 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
258 }
259
260 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
261 usb_log_info("Port reset on port %u completed.", i);
262 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
263
264 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
265 if (speed->major != 3) {
266 /* FIXME: We probably don't want to do this
267 * every time USB2 port is reset. This is a
268 * temporary workaround. */
269 rh_setup_device(rh, i);
270 }
271 }
272
273 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
274 usb_log_info("Port link state changed on port %u.", i);
275 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
276 }
277
278 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
279 usb_log_info("Port %u failed to configure link.", i);
280 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
281 }
282
283 if (events) {
284 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
285 }
286 }
287
288 /**
289 * Theory:
290 *
291 * Although more events could have happened while processing, the PCD
292 * bit in USBSTS will be set on every change. Because the PCD is
293 * cleared even before the interrupt is cleared, it is safe to assume
294 * that this handler will be called again.
295 *
296 * But because we could have handled the event in previous run of this
297 * handler, it is not an error when no event is detected.
298 *
299 * Reality:
300 *
301 * The PCD bit is never set. TODO Check why the interrupt never carries
302 * the PCD flag. Possibly repeat the checking until we're sure the
303 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
304 */
305}
306
307static inline int get_hub_available_bandwidth(xhci_hc_t *hc, xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx)
308{
309 int err = EOK;
310
311 // TODO: find a correct place for this function + API
312 // We need speed, because a root hub device has both USB 2 and USB 3 speeds
313 // and the command can query only one of them
314 // ctx is an out parameter as of now
315 assert(dev);
316 assert(ctx);
317
318 xhci_cmd_t cmd;
319 xhci_cmd_init(&cmd, XHCI_CMD_GET_PORT_BANDWIDTH);
320
321 if ((err = dma_buffer_alloc(&cmd.bandwidth_ctx, sizeof(xhci_port_bandwidth_ctx_t))))
322 goto end;
323
324 cmd.device_speed = speed;
325
326 if ((err = xhci_cmd_sync(hc, &cmd)))
327 goto end;
328
329 memcpy(ctx, cmd.bandwidth_ctx.virt, sizeof(xhci_port_bandwidth_ctx_t));
330
331end:
332 xhci_cmd_fini(&cmd);
333 return err;
334}
335
336const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
337{
338 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
339
340 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
341 return &rh->hc->speeds[psiv];
342}
343
344int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
345{
346 usb_log_debug2("Resetting port %u.", port);
347 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
348 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
349
350 return EOK;
351}
352
353int xhci_rh_fini(xhci_rh_t *rh)
354{
355 /* TODO: Implement me! */
356 usb_log_debug2("Called xhci_rh_fini().");
357
358 free(rh->devices_by_port);
359
360 return EOK;
361}
362
363/**
364 * @}
365 */
Note: See TracBrowser for help on using the repository browser.