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. */
|
---|
54 | static 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 | */
|
---|
66 | int 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 | */
|
---|
89 | int 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 | */
|
---|
100 | static 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 |
|
---|
144 | err_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 | */
|
---|
153 | static 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 | */
|
---|
187 | static int handle_disconnected_device(xhci_rh_t *rh, uint8_t port_id)
|
---|
188 | {
|
---|
189 | assert(rh);
|
---|
190 | int err;
|
---|
191 |
|
---|
192 | /* Find XHCI device by the port. */
|
---|
193 | xhci_device_t *dev = rh->devices_by_port[port_id - 1];
|
---|
194 | if (!dev) {
|
---|
195 | /* Must be extraneous call. */
|
---|
196 | return EOK;
|
---|
197 | }
|
---|
198 |
|
---|
199 | usb_log_info("Device " XHCI_DEV_FMT " at port %u has been disconnected.",
|
---|
200 | XHCI_DEV_ARGS(*dev), port_id);
|
---|
201 |
|
---|
202 | /* Mark the device as detached. */
|
---|
203 | fibril_mutex_lock(&rh->device.base.guard);
|
---|
204 | list_remove(&dev->base.link);
|
---|
205 | rh->devices_by_port[port_id - 1] = NULL;
|
---|
206 | fibril_mutex_unlock(&rh->device.base.guard);
|
---|
207 |
|
---|
208 | /* Remove device from XHCI bus. */
|
---|
209 | if ((err = bus_device_remove(&dev->base))) {
|
---|
210 | usb_log_warning("Failed to remove device " XHCI_DEV_FMT " from XHCI bus: %s",
|
---|
211 | XHCI_DEV_ARGS(*dev), str_error(err));
|
---|
212 | }
|
---|
213 |
|
---|
214 | return EOK;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Handle an incoming Port Change Detected Event.
|
---|
219 | */
|
---|
220 | int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
221 | {
|
---|
222 | uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
|
---|
223 | usb_log_debug("Port status change event detected for port %u.", port_id);
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * We can't be sure that the port change this event announces is the
|
---|
227 | * only port change that happened (see section 4.19.2 of the xHCI
|
---|
228 | * specification). Therefore, we just check all ports for changes.
|
---|
229 | */
|
---|
230 | xhci_rh_handle_port_change(&hc->rh);
|
---|
231 |
|
---|
232 | return EOK;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Handle all changes on all ports.
|
---|
237 | */
|
---|
238 | void xhci_rh_handle_port_change(xhci_rh_t *rh)
|
---|
239 | {
|
---|
240 | for (uint8_t i = 1; i <= rh->max_ports; ++i) {
|
---|
241 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
|
---|
242 |
|
---|
243 | uint32_t events = XHCI_REG_RD_FIELD(®s->portsc, 32);
|
---|
244 | XHCI_REG_WR_FIELD(®s->portsc, events, 32);
|
---|
245 |
|
---|
246 | events &= port_change_mask;
|
---|
247 |
|
---|
248 | if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
|
---|
249 | usb_log_info("Connected state changed on port %u.", i);
|
---|
250 | events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
|
---|
251 |
|
---|
252 | bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
|
---|
253 | if (connected) {
|
---|
254 | handle_connected_device(rh, i);
|
---|
255 | } else {
|
---|
256 | handle_disconnected_device(rh, i);
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
|
---|
261 | usb_log_info("Port enabled changed on port %u.", i);
|
---|
262 | events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
|
---|
266 | usb_log_info("Warm port reset on port %u completed.", i);
|
---|
267 | events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
|
---|
268 | }
|
---|
269 |
|
---|
270 | if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
|
---|
271 | usb_log_info("Over-current change on port %u.", i);
|
---|
272 | events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
|
---|
276 | usb_log_info("Port reset on port %u completed.", i);
|
---|
277 | events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
|
---|
278 |
|
---|
279 | const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
|
---|
280 | if (speed->major != 3) {
|
---|
281 | /* FIXME: We probably don't want to do this
|
---|
282 | * every time USB2 port is reset. This is a
|
---|
283 | * temporary workaround. */
|
---|
284 | rh_setup_device(rh, i);
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
|
---|
289 | usb_log_info("Port link state changed on port %u.", i);
|
---|
290 | events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
|
---|
291 | }
|
---|
292 |
|
---|
293 | if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
|
---|
294 | usb_log_info("Port %u failed to configure link.", i);
|
---|
295 | events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (events) {
|
---|
299 | usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Theory:
|
---|
305 | *
|
---|
306 | * Although more events could have happened while processing, the PCD
|
---|
307 | * bit in USBSTS will be set on every change. Because the PCD is
|
---|
308 | * cleared even before the interrupt is cleared, it is safe to assume
|
---|
309 | * that this handler will be called again.
|
---|
310 | *
|
---|
311 | * But because we could have handled the event in previous run of this
|
---|
312 | * handler, it is not an error when no event is detected.
|
---|
313 | *
|
---|
314 | * Reality:
|
---|
315 | *
|
---|
316 | * The PCD bit is never set. TODO Check why the interrupt never carries
|
---|
317 | * the PCD flag. Possibly repeat the checking until we're sure the
|
---|
318 | * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
|
---|
319 | */
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Get a port speed for a given port id.
|
---|
324 | */
|
---|
325 | const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
|
---|
326 | {
|
---|
327 | xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
|
---|
328 |
|
---|
329 | unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
|
---|
330 | return &rh->hc->speeds[psiv];
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Issue a port reset for a given port.
|
---|
335 | */
|
---|
336 | int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
|
---|
337 | {
|
---|
338 | usb_log_debug2("Resetting port %u.", port);
|
---|
339 | xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
|
---|
340 | XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
|
---|
341 |
|
---|
342 | return EOK;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * @}
|
---|
347 | */
|
---|