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

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

usbhost: renamed bus_device_remove to bus_device_gone

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