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

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

xhci: remove unnecessary field

  • Property mode set to 100644
File size: 10.0 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)
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
72 const int err = bus_device_init(&rh->device.base, &rh->hc->bus.base);
73 if (err)
74 return err;
75
76 /* Initialize route string */
77 rh->device.route_str = 0;
78 rh->device.tier = 0;
79
80 return EOK;
81}
82
83/** Create a device node for device directly connected to RH.
84 */
85static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
86{
87 int err;
88 assert(rh);
89
90 assert(rh->devices_by_port[port_id - 1] == NULL);
91
92 xhci_bus_t *bus = &rh->hc->bus;
93
94 device_t *dev = hcd_ddf_fun_create(&rh->hc->base);
95 if (!dev) {
96 usb_log_error("Failed to create USB device function.");
97 return ENOMEM;
98 }
99
100 const xhci_port_speed_t *port_speed = xhci_rh_get_port_speed(rh, port_id);
101 xhci_device_t *xhci_dev = xhci_device_get(dev);
102 xhci_dev->usb3 = port_speed->major == 3;
103 xhci_dev->rh_port = port_id;
104
105 dev->hub = &rh->device.base;
106 dev->port = port_id;
107 dev->speed = port_speed->usb_speed;
108
109 if ((err = xhci_bus_enumerate_device(bus, dev))) {
110 usb_log_error("Failed to enumerate USB device: %s", str_error(err));
111 return err;
112 }
113
114 if (!ddf_fun_get_name(dev->fun)) {
115 bus_device_set_default_name(dev);
116 }
117
118 if ((err = ddf_fun_bind(dev->fun))) {
119 usb_log_error("Failed to register device " XHCI_DEV_FMT " DDF function: %s.",
120 XHCI_DEV_ARGS(*xhci_dev), str_error(err));
121 goto err_usb_dev;
122 }
123
124 fibril_mutex_lock(&rh->device.base.guard);
125 list_append(&dev->link, &rh->device.base.devices);
126 rh->devices_by_port[port_id - 1] = xhci_dev;
127 fibril_mutex_unlock(&rh->device.base.guard);
128
129 return EOK;
130
131err_usb_dev:
132 hcd_ddf_fun_destroy(dev);
133 return err;
134}
135
136static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
137{
138 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
139
140 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
141 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
142
143 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
144
145 if (speed->major == 3) {
146 if (link_state == 0) {
147 /* USB3 is automatically advanced to enabled. */
148 return rh_setup_device(rh, port_id);
149 }
150 else if (link_state == 5) {
151 /* USB 3 failed to enable. */
152 usb_log_error("USB 3 port couldn't be enabled.");
153 return EAGAIN;
154 }
155 else {
156 usb_log_error("USB 3 port is in invalid state %u.", link_state);
157 return EINVAL;
158 }
159 }
160 else {
161 usb_log_debug("USB 2 device attached, issuing reset.");
162 xhci_rh_reset_port(rh, port_id);
163 /*
164 FIXME: we need to wait for the event triggered by the reset
165 and then alloc_dev()... can't it be done directly instead of
166 going around?
167 */
168 return EOK;
169 }
170}
171
172/** Deal with a detached device.
173 */
174static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
175{
176 assert(rh);
177 int err;
178
179 /* Find XHCI device by the port. */
180 xhci_device_t *dev = rh->devices_by_port[port_id - 1];
181 if (!dev) {
182 /* Must be extraneous call. */
183 return EOK;
184 }
185
186 usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
187 XHCI_DEV_ARGS(*dev), port_id);
188
189 /* Mark the device as detached. */
190 fibril_mutex_lock(&rh->device.base.guard);
191 list_remove(&dev->base.link);
192 rh->devices_by_port[port_id - 1] = NULL;
193 fibril_mutex_unlock(&rh->device.base.guard);
194
195 /* Remove device from XHCI bus. */
196 if ((err = xhci_bus_remove_device(&rh->hc->bus, &dev->base))) {
197 usb_log_warning("Failed to remove device " XHCI_DEV_FMT " from XHCI bus: %s",
198 XHCI_DEV_ARGS(*dev), str_error(err));
199 }
200
201 return EOK;
202}
203
204/** Handle an incoming Port Change Detected Event.
205 */
206int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
207{
208 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
209 usb_log_debug("Port status change event detected for port %u.", port_id);
210
211 /**
212 * We can't be sure that the port change this event announces is the
213 * only port change that happened (see section 4.19.2 of the xHCI
214 * specification). Therefore, we just check all ports for changes.
215 */
216 xhci_rh_handle_port_change(&hc->rh);
217
218 return EOK;
219}
220
221void xhci_rh_handle_port_change(xhci_rh_t *rh)
222{
223 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
224 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
225
226 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
227 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
228
229 events &= port_change_mask;
230
231 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
232 usb_log_info("Connected state changed on port %u.", i);
233 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
234
235 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
236 if (connected) {
237 handle_connected_device(rh, i);
238 } else {
239 handle_disconnected_device(rh, i);
240 }
241 }
242
243 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
244 usb_log_info("Port enabled changed on port %u.", i);
245 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
246 }
247
248 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
249 usb_log_info("Warm port reset on port %u completed.", i);
250 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
251 }
252
253 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
254 usb_log_info("Over-current change on port %u.", i);
255 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
256 }
257
258 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
259 usb_log_info("Port reset on port %u completed.", i);
260 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
261
262 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
263 if (speed->major != 3) {
264 /* FIXME: We probably don't want to do this
265 * every time USB2 port is reset. This is a
266 * temporary workaround. */
267 rh_setup_device(rh, i);
268 }
269 }
270
271 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
272 usb_log_info("Port link state changed on port %u.", i);
273 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
274 }
275
276 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
277 usb_log_info("Port %u failed to configure link.", i);
278 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
279 }
280
281 if (events) {
282 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
283 }
284 }
285
286 /**
287 * Theory:
288 *
289 * Although more events could have happened while processing, the PCD
290 * bit in USBSTS will be set on every change. Because the PCD is
291 * cleared even before the interrupt is cleared, it is safe to assume
292 * that this handler will be called again.
293 *
294 * But because we could have handled the event in previous run of this
295 * handler, it is not an error when no event is detected.
296 *
297 * Reality:
298 *
299 * The PCD bit is never set. TODO Check why the interrupt never carries
300 * the PCD flag. Possibly repeat the checking until we're sure the
301 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
302 */
303}
304
305static inline int get_hub_available_bandwidth(xhci_hc_t *hc, xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx)
306{
307 int err = EOK;
308
309 // TODO: find a correct place for this function + API
310 // We need speed, because a root hub device has both USB 2 and USB 3 speeds
311 // and the command can query only one of them
312 // ctx is an out parameter as of now
313 assert(dev);
314 assert(ctx);
315
316 xhci_cmd_t cmd;
317 xhci_cmd_init(&cmd, XHCI_CMD_GET_PORT_BANDWIDTH);
318
319 if ((err = dma_buffer_alloc(&cmd.bandwidth_ctx, sizeof(xhci_port_bandwidth_ctx_t))))
320 goto end;
321
322 cmd.device_speed = speed;
323
324 if ((err = xhci_cmd_sync(hc, &cmd)))
325 goto end;
326
327 memcpy(ctx, cmd.bandwidth_ctx.virt, sizeof(xhci_port_bandwidth_ctx_t));
328
329end:
330 xhci_cmd_fini(&cmd);
331 return err;
332}
333
334const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
335{
336 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
337
338 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
339 return &rh->hc->speeds[psiv];
340}
341
342int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
343{
344 usb_log_debug2("Resetting port %u.", port);
345 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
346 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
347
348 return EOK;
349}
350
351int xhci_rh_fini(xhci_rh_t *rh)
352{
353 assert(rh);
354 free(rh->devices_by_port);
355 return EOK;
356}
357
358/**
359 * @}
360 */
Note: See TracBrowser for help on using the repository browser.