source: mainline/uspace/drv/bus/usb/xhci/rh.c@ 867b375

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

hcd_ddf_new_device refactoring

This long function is now split into parts. Instead of passing dozens of arguments, it now creates the usb_dev_t right away, and uses that to pass it along. The address_device part is now modifiable by drivers.

There is still a work to be done. The biggest problem I see is in the addressing - currently, there is usb_address_t, and for high speed transaction translating there is another address. For (near) future extensibility, we should pass address as a structure. Or even better, make a way how to reference a device, maybe in a similar way how we work with endpoints.

  • Property mode set to 100644
File size: 10.3 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/debug.h>
39#include <usb/host/utils/malloc32.h>
40#include <usb/host/ddf_helpers.h>
41
42#include "debug.h"
43#include "commands.h"
44#include "endpoint.h"
45#include "hc.h"
46#include "hw_struct/trb.h"
47#include "rh.h"
48#include "transfers.h"
49
50/* This mask only lists registers, which imply port change. */
51static const uint32_t port_change_mask =
52 XHCI_REG_MASK(XHCI_PORT_CSC) |
53 XHCI_REG_MASK(XHCI_PORT_PEC) |
54 XHCI_REG_MASK(XHCI_PORT_WRC) |
55 XHCI_REG_MASK(XHCI_PORT_OCC) |
56 XHCI_REG_MASK(XHCI_PORT_PRC) |
57 XHCI_REG_MASK(XHCI_PORT_PLC) |
58 XHCI_REG_MASK(XHCI_PORT_CEC);
59
60int xhci_rh_init(xhci_rh_t *rh, xhci_hc_t *hc)
61{
62 assert(rh);
63 assert(hc);
64
65 rh->hc = hc;
66 rh->max_ports = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PORTS);
67
68 return EOK;
69}
70
71// TODO: Check device deallocation, we free device_ctx in hc.c, not
72// sure about the other structs.
73// TODO: This currently assumes the device is attached to rh directly.
74// Also, we should consider moving a lot of functionailty to xhci bus
75int xhci_rh_address_device(xhci_rh_t *rh, usb_speed_t unused_speed, usb_tt_address_t tt, usb_address_t *address)
76{
77 int err;
78 xhci_hc_t *hc = rh->hc;
79
80 xhci_cmd_t cmd;
81 xhci_cmd_init(&cmd);
82
83 uint8_t port = tt.port;
84
85 /* XXX Certainly not generic solution. */
86 uint32_t route_str = 0;
87
88 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port);
89
90 xhci_send_enable_slot_command(hc, &cmd);
91 if ((err = xhci_cmd_wait(&cmd, 100000)) != EOK)
92 return err;
93
94 uint32_t slot_id = cmd.slot_id;
95
96 usb_log_debug2("Obtained slot ID: %u.\n", slot_id);
97 xhci_cmd_fini(&cmd);
98
99 xhci_input_ctx_t *ictx = malloc(sizeof(xhci_input_ctx_t));
100 if (!ictx) {
101 return ENOMEM;
102 }
103
104 memset(ictx, 0, sizeof(xhci_input_ctx_t));
105
106 XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 0);
107 XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
108
109 /* Initialize slot_ctx according to section 4.3.3 point 3. */
110 /* Attaching to root hub port, root string equals to 0. */
111 XHCI_SLOT_ROOT_HUB_PORT_SET(ictx->slot_ctx, port);
112 XHCI_SLOT_CTX_ENTRIES_SET(ictx->slot_ctx, 1);
113 XHCI_SLOT_ROUTE_STRING_SET(ictx->slot_ctx, route_str);
114
115 xhci_trb_ring_t *ep_ring = malloc(sizeof(xhci_trb_ring_t));
116 if (!ep_ring) {
117 err = ENOMEM;
118 goto err_ictx;
119 }
120
121 err = xhci_trb_ring_init(ep_ring, hc);
122 if (err)
123 goto err_ring;
124
125 XHCI_EP_TYPE_SET(ictx->endpoint_ctx[0], EP_TYPE_CONTROL);
126 // TODO: must be changed with a command after USB descriptor is read
127 // See 4.6.5 in XHCI specification, first note
128 XHCI_EP_MAX_PACKET_SIZE_SET(ictx->endpoint_ctx[0],
129 speed->major == 3 ? 512 : 8);
130 XHCI_EP_MAX_BURST_SIZE_SET(ictx->endpoint_ctx[0], 0);
131 /* FIXME physical pointer? */
132 XHCI_EP_TR_DPTR_SET(ictx->endpoint_ctx[0], ep_ring->dequeue);
133 XHCI_EP_DCS_SET(ictx->endpoint_ctx[0], 1);
134 XHCI_EP_INTERVAL_SET(ictx->endpoint_ctx[0], 0);
135 XHCI_EP_MAX_P_STREAMS_SET(ictx->endpoint_ctx[0], 0);
136 XHCI_EP_MULT_SET(ictx->endpoint_ctx[0], 0);
137 XHCI_EP_ERROR_COUNT_SET(ictx->endpoint_ctx[0], 3);
138
139 xhci_device_ctx_t *dctx = malloc32(sizeof(xhci_device_ctx_t));
140 if (!dctx) {
141 err = ENOMEM;
142 goto err_ring;
143 }
144 memset(dctx, 0, sizeof(xhci_device_ctx_t));
145
146 hc->dcbaa[slot_id] = addr_to_phys(dctx);
147
148 memset(&hc->dcbaa_virt[slot_id], 0, sizeof(xhci_virt_device_ctx_t));
149 hc->dcbaa_virt[slot_id].dev_ctx = dctx;
150 hc->dcbaa_virt[slot_id].trs[0] = ep_ring;
151
152 xhci_cmd_init(&cmd);
153 cmd.slot_id = slot_id;
154 xhci_send_address_device_command(hc, &cmd, ictx);
155 if ((err = xhci_cmd_wait(&cmd, 100000)) != EOK)
156 goto err_dctx;
157
158 xhci_cmd_fini(&cmd);
159
160 *address = XHCI_SLOT_DEVICE_ADDRESS(dctx->slot_ctx);
161 usb_log_debug2("Obtained USB address: %d.\n", *address);
162
163 // TODO: Ask libusbhost to create a control endpoint for EP0.
164
165 // TODO: Save all data structures in the corresponding xhci_device_t.
166
167 return EOK;
168
169err_dctx:
170 if (dctx) {
171 free(dctx);
172 hc->dcbaa[slot_id] = 0;
173 memset(&hc->dcbaa_virt[slot_id], 0, sizeof(xhci_virt_device_ctx_t));
174 }
175err_ring:
176 if (ep_ring) {
177 xhci_trb_ring_fini(ep_ring);
178 free(ep_ring);
179 }
180err_ictx:
181 free(ictx);
182 return err;
183}
184
185static int rh_setup_device(xhci_rh_t *rh, uint8_t port_id)
186{
187 /** This should ideally use the libusbhost in a clean and elegant way,
188 * to create child function. The refactoring of libusbhost is not over
189 * yet, so for now it is still quirky.
190 */
191
192 return hcd_roothub_new_device(rh->hcd_rh, port_id);
193}
194
195static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
196{
197 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port_id - 1];
198
199 uint8_t link_state = XHCI_REG_RD(regs, XHCI_PORT_PLS);
200 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, port_id);
201
202 usb_log_info("Detected new %.4s%u.%u device on port %u.", speed->name, speed->major, speed->minor, port_id);
203
204 if (speed->major == 3) {
205 if (link_state == 0) {
206 /* USB3 is automatically advanced to enabled. */
207 return rh_setup_device(rh, port_id);
208 }
209 else if (link_state == 5) {
210 /* USB 3 failed to enable. */
211 usb_log_error("USB 3 port couldn't be enabled.");
212 return EAGAIN;
213 }
214 else {
215 usb_log_error("USB 3 port is in invalid state %u.", link_state);
216 return EINVAL;
217 }
218 }
219 else {
220 usb_log_debug("USB 2 device attached, issuing reset.");
221 xhci_rh_reset_port(rh, port_id);
222 /*
223 FIXME: we need to wait for the event triggered by the reset
224 and then alloc_dev()... can't it be done directly instead of
225 going around?
226 */
227 return EOK;
228 }
229}
230
231/** Handle an incoming Port Change Detected Event.
232 */
233int xhci_rh_handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
234{
235 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
236 usb_log_debug("Port status change event detected for port %u.", port_id);
237
238 /**
239 * We can't be sure that the port change this event announces is the
240 * only port change that happened (see section 4.19.2 of the xHCI
241 * specification). Therefore, we just check all ports for changes.
242 */
243 xhci_rh_handle_port_change(&hc->rh);
244
245 return EOK;
246}
247
248void xhci_rh_handle_port_change(xhci_rh_t *rh)
249{
250 for (uint8_t i = 1; i <= rh->max_ports; ++i) {
251 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[i - 1];
252
253 uint32_t events = XHCI_REG_RD_FIELD(&regs->portsc, 32);
254 XHCI_REG_WR_FIELD(&regs->portsc, events, 32);
255
256 events &= port_change_mask;
257
258 if (events & XHCI_REG_MASK(XHCI_PORT_CSC)) {
259 usb_log_info("Connected state changed on port %u.", i);
260 events &= ~XHCI_REG_MASK(XHCI_PORT_CSC);
261
262 bool connected = XHCI_REG_RD(regs, XHCI_PORT_CCS);
263 if (connected)
264 handle_connected_device(rh, i);
265 }
266
267 if (events & XHCI_REG_MASK(XHCI_PORT_PEC)) {
268 usb_log_info("Port enabled changed on port %u.", i);
269 events &= ~XHCI_REG_MASK(XHCI_PORT_PEC);
270 }
271
272 if (events & XHCI_REG_MASK(XHCI_PORT_WRC)) {
273 usb_log_info("Warm port reset on port %u completed.", i);
274 events &= ~XHCI_REG_MASK(XHCI_PORT_WRC);
275 }
276
277 if (events & XHCI_REG_MASK(XHCI_PORT_OCC)) {
278 usb_log_info("Over-current change on port %u.", i);
279 events &= ~XHCI_REG_MASK(XHCI_PORT_OCC);
280 }
281
282 if (events & XHCI_REG_MASK(XHCI_PORT_PRC)) {
283 usb_log_info("Port reset on port %u completed.", i);
284 events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
285
286 const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
287 if (speed->major != 3) {
288 /* FIXME: We probably don't want to do this
289 * every time USB2 port is reset. This is a
290 * temporary workaround. */
291 rh_setup_device(rh, i);
292 }
293 }
294
295 if (events & XHCI_REG_MASK(XHCI_PORT_PLC)) {
296 usb_log_info("Port link state changed on port %u.", i);
297 events &= ~XHCI_REG_MASK(XHCI_PORT_PLC);
298 }
299
300 if (events & XHCI_REG_MASK(XHCI_PORT_CEC)) {
301 usb_log_info("Port %u failed to configure link.", i);
302 events &= ~XHCI_REG_MASK(XHCI_PORT_CEC);
303 }
304
305 if (events) {
306 usb_log_warning("Port change (0x%08x) ignored on port %u.", events, i);
307 }
308 }
309
310 /**
311 * Theory:
312 *
313 * Although more events could have happened while processing, the PCD
314 * bit in USBSTS will be set on every change. Because the PCD is
315 * cleared even before the interrupt is cleared, it is safe to assume
316 * that this handler will be called again.
317 *
318 * But because we could have handled the event in previous run of this
319 * handler, it is not an error when no event is detected.
320 *
321 * Reality:
322 *
323 * The PCD bit is never set. TODO Check why the interrupt never carries
324 * the PCD flag. Possibly repeat the checking until we're sure the
325 * PSCEG is 0 - check section 4.19.2 of the xHCI spec.
326 */
327}
328
329const xhci_port_speed_t *xhci_rh_get_port_speed(xhci_rh_t *rh, uint8_t port)
330{
331 xhci_port_regs_t *port_regs = &rh->hc->op_regs->portrs[port - 1];
332
333 unsigned psiv = XHCI_REG_RD(port_regs, XHCI_PORT_PS);
334 return &rh->speeds[psiv];
335}
336
337int xhci_rh_reset_port(xhci_rh_t* rh, uint8_t port)
338{
339 usb_log_debug2("Resetting port %u.", port);
340 xhci_port_regs_t *regs = &rh->hc->op_regs->portrs[port-1];
341 XHCI_REG_SET(regs, XHCI_PORT_PR, 1);
342
343 return EOK;
344}
345
346int xhci_rh_fini(xhci_rh_t *rh)
347{
348 /* TODO: Implement me! */
349 usb_log_debug2("Called xhci_rh_fini().");
350 return EOK;
351}
352
353/**
354 * @}
355 */
Note: See TracBrowser for help on using the repository browser.