source: mainline/uspace/drv/bus/usb/xhci/hc.c@ 2aaba7e

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

xhci: move HC semantics from endpoint/device to hc module

  • Property mode set to 100644
File size: 29.4 KB
RevLine 
[5cbccd4]1/*
2 * Copyright (c) 2017 Ondrej Hlavaty
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 host controller data bookkeeping.
34 */
35
36#include <errno.h>
[cb89430]37#include <str_error.h>
[5cbccd4]38#include <usb/debug.h>
[5fd9c30]39#include <usb/host/endpoint.h>
[5cbccd4]40#include "debug.h"
41#include "hc.h"
[7bd99bf]42#include "rh.h"
[cb89430]43#include "hw_struct/trb.h"
[0206d35]44#include "hw_struct/context.h"
45#include "endpoint.h"
[e9e24f2]46#include "transfers.h"
47#include "trb_ring.h"
[5cbccd4]48
[91ca111]49/**
50 * Default USB Speed ID mapping: Table 157
51 */
52#define PSI_TO_BPS(psie, psim) (((uint64_t) psim) << (10 * psie))
[f668d60]53#define PORT_SPEED(usb, mjr, psie, psim) { \
[816335c]54 .name = "USB ", \
55 .major = mjr, \
56 .minor = 0, \
[f668d60]57 .usb_speed = USB_SPEED_##usb, \
[91ca111]58 .rx_bps = PSI_TO_BPS(psie, psim), \
59 .tx_bps = PSI_TO_BPS(psie, psim) \
60}
[a75f9cbc]61
62static const xhci_port_speed_t default_psiv_to_port_speed [] = {
63 [1] = PORT_SPEED(FULL, 2, 2, 12),
64 [2] = PORT_SPEED(LOW, 2, 1, 1500),
65 [3] = PORT_SPEED(HIGH, 2, 2, 480),
66 [4] = PORT_SPEED(SUPER, 3, 3, 5),
67};
68
69static const unsigned usb_speed_to_psiv [] = {
70 [USB_SPEED_FULL] = 1,
71 [USB_SPEED_LOW] = 2,
72 [USB_SPEED_HIGH] = 3,
73 [USB_SPEED_SUPER] = 4,
74};
[91ca111]75
76/**
77 * Walk the list of extended capabilities.
[eb928c4]78 *
79 * The most interesting thing hidden in extended capabilities is the mapping of
80 * ports to protocol versions and speeds.
[91ca111]81 */
82static int hc_parse_ec(xhci_hc_t *hc)
83{
[816335c]84 unsigned psic, major, minor;
85 xhci_sp_name_t name;
86
[f668d60]87 xhci_port_speed_t *speeds = hc->speeds;
[91ca111]88
89 for (xhci_extcap_t *ec = hc->xecp; ec; ec = xhci_extcap_next(ec)) {
90 xhci_dump_extcap(ec);
91 switch (XHCI_REG_RD(ec, XHCI_EC_CAP_ID)) {
92 case XHCI_EC_USB_LEGACY:
93 assert(hc->legsup == NULL);
94 hc->legsup = (xhci_legsup_t *) ec;
95 break;
96 case XHCI_EC_SUPPORTED_PROTOCOL:
97 psic = XHCI_REG_RD(ec, XHCI_EC_SP_PSIC);
98 major = XHCI_REG_RD(ec, XHCI_EC_SP_MAJOR);
[816335c]99 minor = XHCI_REG_RD(ec, XHCI_EC_SP_MINOR);
100 name.packed = host2uint32_t_le(XHCI_REG_RD(ec, XHCI_EC_SP_NAME));
101
102 if (name.packed != xhci_name_usb.packed) {
103 /**
104 * The detection of such protocol would work,
105 * but the rest of the implementation is made
106 * for the USB protocol only.
107 */
108 usb_log_error("Unknown protocol %.4s.", name.str);
109 return ENOTSUP;
110 }
[91ca111]111
[a9fcd73]112 unsigned offset = XHCI_REG_RD(ec, XHCI_EC_SP_CP_OFF);
113 unsigned count = XHCI_REG_RD(ec, XHCI_EC_SP_CP_COUNT);
114 xhci_rh_set_ports_protocol(&hc->rh, offset, count, major);
115
[91ca111]116 // "Implied" speed
117 if (psic == 0) {
[816335c]118 assert(minor == 0);
[370a1c8]119
[91ca111]120 if (major == 2) {
[a75f9cbc]121 speeds[1] = default_psiv_to_port_speed[1];
122 speeds[2] = default_psiv_to_port_speed[2];
123 speeds[3] = default_psiv_to_port_speed[3];
[91ca111]124 } else if (major == 3) {
[a75f9cbc]125 speeds[4] = default_psiv_to_port_speed[4];
[91ca111]126 } else {
127 return EINVAL;
128 }
129
[816335c]130 usb_log_debug2("Implied speed of USB %u.0 set up.", major);
[91ca111]131 } else {
132 for (unsigned i = 0; i < psic; i++) {
133 xhci_psi_t *psi = xhci_extcap_psi(ec, i);
134 unsigned sim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
135 unsigned psiv = XHCI_REG_RD(psi, XHCI_PSI_PSIV);
136 unsigned psie = XHCI_REG_RD(psi, XHCI_PSI_PSIE);
137 unsigned psim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
[a75f9cbc]138 uint64_t bps = PSI_TO_BPS(psie, psim);
139
140 /*
141 * Speed is not implied, but using one of default PSIV. This is
142 * not clearly stated in xHCI spec. There is a clear intention
143 * to allow xHCI to specify its own speed parameters, but
144 * throughout the document, they used fixed values for e.g.
145 * High-speed (3), without stating the controller shall have
146 * implied default speeds - and for instance Intel controllers
147 * do not. So let's check if the values match and if so, accept
148 * the implied USB speed too.
149 *
150 * The main reason we need this is the usb_speed to have
151 * mapping also for devices connected to hubs.
152 */
153 if (psiv < ARRAY_SIZE(default_psiv_to_port_speed)
154 && default_psiv_to_port_speed[psiv].major == major
155 && default_psiv_to_port_speed[psiv].minor == minor
156 && default_psiv_to_port_speed[psiv].rx_bps == bps
157 && default_psiv_to_port_speed[psiv].tx_bps == bps) {
158 speeds[psiv] = default_psiv_to_port_speed[psiv];
159 usb_log_debug2("Assumed default %s speed of USB %u.", usb_str_speed(speeds[psiv].usb_speed), major);
160 continue;
161 }
[91ca111]162
[a75f9cbc]163 // Custom speed
[816335c]164 speeds[psiv].major = major;
165 speeds[psiv].minor = minor;
166 str_ncpy(speeds[psiv].name, 4, name.str, 4);
[f668d60]167 speeds[psiv].usb_speed = USB_SPEED_MAX;
[816335c]168
[91ca111]169 if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_RX)
[816335c]170 speeds[psiv].rx_bps = bps;
[91ca111]171 if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_TX) {
[816335c]172 speeds[psiv].tx_bps = bps;
173 usb_log_debug2("Speed %u set up for bps %" PRIu64 " / %" PRIu64 ".", psiv, speeds[psiv].rx_bps, speeds[psiv].tx_bps);
[91ca111]174 }
175 }
176 }
177 }
178 }
179 return EOK;
180}
181
[eb928c4]182/**
183 * Initialize MMIO spaces of xHC.
184 */
[e4d7363]185int hc_init_mmio(xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
186{
187 int err;
188
189 if (hw_res->mem_ranges.count != 1) {
190 usb_log_error("Unexpected MMIO area, bailing out.");
191 return EINVAL;
192 }
193
194 hc->mmio_range = hw_res->mem_ranges.ranges[0];
195
[a1732929]196 usb_log_debug("MMIO area at %p (size %zu), IRQ %d.",
[e4d7363]197 RNGABSPTR(hc->mmio_range), RNGSZ(hc->mmio_range), hw_res->irqs.irqs[0]);
198
199 if (RNGSZ(hc->mmio_range) < sizeof(xhci_cap_regs_t))
200 return EOVERFLOW;
201
202 void *base;
203 if ((err = pio_enable_range(&hc->mmio_range, &base)))
204 return err;
205
[20eaa82]206 hc->reg_base = base;
[e4d7363]207 hc->cap_regs = (xhci_cap_regs_t *) base;
208 hc->op_regs = (xhci_op_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH));
209 hc->rt_regs = (xhci_rt_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF));
210 hc->db_arry = (xhci_doorbell_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_DBOFF));
211
[91ca111]212 uintptr_t xec_offset = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_XECP) * sizeof(xhci_dword_t);
213 if (xec_offset > 0)
214 hc->xecp = (xhci_extcap_t *) (base + xec_offset);
215
[e4d7363]216 usb_log_debug2("Initialized MMIO reg areas:");
217 usb_log_debug2("\tCapability regs: %p", hc->cap_regs);
218 usb_log_debug2("\tOperational regs: %p", hc->op_regs);
219 usb_log_debug2("\tRuntime regs: %p", hc->rt_regs);
220 usb_log_debug2("\tDoorbell array base: %p", hc->db_arry);
221
222 xhci_dump_cap_regs(hc->cap_regs);
223
224 hc->ac64 = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_AC64);
[7ec7b7e]225 hc->csz = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_CSZ);
[e4d7363]226 hc->max_slots = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SLOTS);
[94e9c29]227
228 struct timeval tv;
229 getuptime(&tv);
230 hc->wrap_time = tv.tv_sec * 1000000 + tv.tv_usec;
[665368c]231 hc->wrap_count = 0;
[94e9c29]232
[708d8fcd]233 unsigned ist = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_IST);
234 hc->ist = (ist & 0x10 >> 1) * (ist & 0xf);
[e4d7363]235
[a9fcd73]236 if ((err = xhci_rh_init(&hc->rh, hc)))
237 goto err_pio;
238
239 if ((err = hc_parse_ec(hc)))
240 goto err_rh;
[91ca111]241
[e4d7363]242 return EOK;
[a9fcd73]243
244err_rh:
245 xhci_rh_fini(&hc->rh);
246err_pio:
247 pio_disable(hc->reg_base, RNGSZ(hc->mmio_range));
248 return err;
[e4d7363]249}
250
[2c0564c]251static int event_worker(void *arg);
252
[eb928c4]253/**
254 * Initialize structures kept in allocated memory.
255 */
[0f6b50f]256int hc_init_memory(xhci_hc_t *hc, ddf_dev_t *device)
[e4d7363]257{
258 int err;
259
[b80c1ab]260 if (dma_buffer_alloc(&hc->dcbaa_dma, (1 + hc->max_slots) * sizeof(uint64_t)))
[e4d7363]261 return ENOMEM;
[b80c1ab]262 hc->dcbaa = hc->dcbaa_dma.virt;
[e4d7363]263
[9b2f69e]264 if ((err = xhci_event_ring_init(&hc->event_ring)))
[889146e]265 goto err_dcbaa;
[e4d7363]266
[b19131c5]267 if ((err = xhci_scratchpad_alloc(hc)))
[5a9ae994]268 goto err_event_ring;
[e4d7363]269
[aee352c]270 if ((err = xhci_init_commands(hc)))
[ee28ae66]271 goto err_scratch;
[aee352c]272
[2b61945]273 if ((err = xhci_bus_init(&hc->bus, hc)))
[6832245]274 goto err_cmd;
[e6b9182]275
[2c0564c]276 fid_t fid = fibril_create(&event_worker, hc);
277 if (!fid)
278 goto err_bus;
279
280 // TODO: completion_reset
281 hc->event_fibril_completion.active = true;
282 fibril_mutex_initialize(&hc->event_fibril_completion.guard);
283 fibril_condvar_initialize(&hc->event_fibril_completion.cv);
284
285 xhci_sw_ring_init(&hc->sw_ring, PAGE_SIZE / sizeof(xhci_trb_t));
286
287 fibril_add_ready(fid);
288
[e4d7363]289 return EOK;
290
[2c0564c]291err_bus:
292 xhci_bus_fini(&hc->bus);
[ee28ae66]293err_cmd:
[d271f78]294 xhci_fini_commands(hc);
[ee28ae66]295err_scratch:
296 xhci_scratchpad_free(hc);
[5a9ae994]297err_event_ring:
[e4d7363]298 xhci_event_ring_fini(&hc->event_ring);
299err_dcbaa:
[b80c1ab]300 hc->dcbaa = NULL;
301 dma_buffer_free(&hc->dcbaa_dma);
[e4d7363]302 return err;
303}
304
[ab5a0830]305/*
306 * Pseudocode:
307 * ip = read(intr[0].iman)
308 * if (ip) {
309 * status = read(usbsts)
310 * assert status
311 * assert ip
312 * accept (passing status)
313 * }
314 * decline
315 */
316static const irq_cmd_t irq_commands[] = {
317 {
318 .cmd = CMD_PIO_READ_32,
319 .dstarg = 3,
320 .addr = NULL /* intr[0].iman */
321 },
322 {
323 .cmd = CMD_AND,
324 .srcarg = 3,
325 .dstarg = 4,
326 .value = 0 /* host2xhci(32, 1) */
327 },
328 {
329 .cmd = CMD_PREDICATE,
330 .srcarg = 4,
331 .value = 5
332 },
333 {
334 .cmd = CMD_PIO_READ_32,
335 .dstarg = 1,
336 .addr = NULL /* usbsts */
337 },
338 {
339 .cmd = CMD_AND,
340 .srcarg = 1,
341 .dstarg = 2,
342 .value = 0 /* host2xhci(32, XHCI_STATUS_ACK_MASK) */
343 },
344 {
345 .cmd = CMD_PIO_WRITE_A_32,
346 .srcarg = 2,
347 .addr = NULL /* usbsts */
348 },
349 {
350 .cmd = CMD_PIO_WRITE_A_32,
[efe9463]351 .srcarg = 3,
[ab5a0830]352 .addr = NULL /* intr[0].iman */
353 },
354 {
355 .cmd = CMD_ACCEPT
356 },
357 {
358 .cmd = CMD_DECLINE
359 }
360};
361
[e4d7363]362
[cb89430]363/**
364 * Generates code to accept interrupts. The xHCI is designed primarily for
365 * MSI/MSI-X, but we use PCI Interrupt Pin. In this mode, all the Interrupters
366 * (except 0) are disabled.
367 */
[e4d7363]368int hc_irq_code_gen(irq_code_t *code, xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
[cb89430]369{
370 assert(code);
371 assert(hw_res);
372
[e4d7363]373 if (hw_res->irqs.count != 1) {
[cb89430]374 usb_log_info("Unexpected HW resources to enable interrupts.");
375 return EINVAL;
376 }
377
378 code->ranges = malloc(sizeof(irq_pio_range_t));
379 if (code->ranges == NULL)
380 return ENOMEM;
381
382 code->cmds = malloc(sizeof(irq_commands));
383 if (code->cmds == NULL) {
384 free(code->ranges);
385 return ENOMEM;
386 }
387
388 code->rangecount = 1;
389 code->ranges[0] = (irq_pio_range_t) {
[91ca111]390 .base = RNGABS(hc->mmio_range),
391 .size = RNGSZ(hc->mmio_range),
[cb89430]392 };
393
394 code->cmdcount = ARRAY_SIZE(irq_commands);
395 memcpy(code->cmds, irq_commands, sizeof(irq_commands));
396
[91ca111]397 void *intr0_iman = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF) + offsetof(xhci_rt_regs_t, ir[0]);
[ab5a0830]398 void *usbsts = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH) + offsetof(xhci_op_regs_t, usbsts);
[cb89430]399 code->cmds[0].addr = intr0_iman;
400 code->cmds[1].value = host2xhci(32, 1);
[ab5a0830]401 code->cmds[3].addr = usbsts;
402 code->cmds[4].value = host2xhci(32, XHCI_STATUS_ACK_MASK);
403 code->cmds[5].addr = usbsts;
404 code->cmds[6].addr = intr0_iman;
[cb89430]405
406 return hw_res->irqs.irqs[0];
407}
408
[eb928c4]409/**
410 * Claim xHC from BIOS. Implements handoff as per Section 4.22.1 of xHCI spec.
411 */
[e4d7363]412int hc_claim(xhci_hc_t *hc, ddf_dev_t *dev)
[cb89430]413{
[91ca111]414 /* No legacy support capability, the controller is solely for us */
415 if (!hc->legsup)
416 return EOK;
417
[0e7380f]418 if (xhci_reg_wait(&hc->op_regs->usbsts, XHCI_REG_MASK(XHCI_OP_CNR), 0))
419 return ETIMEOUT;
420
[e6b0dba]421 usb_log_debug2("LEGSUP: bios: %x, os: %x", hc->legsup->sem_bios, hc->legsup->sem_os);
[0e7380f]422 XHCI_REG_SET(hc->legsup, XHCI_LEGSUP_SEM_OS, 1);
[4d28d86]423 for (int i = 0; i <= (XHCI_LEGSUP_BIOS_TIMEOUT_US / XHCI_LEGSUP_POLLING_DELAY_1MS); i++) {
[e6b0dba]424 usb_log_debug2("LEGSUP: elapsed: %i ms, bios: %x, os: %x", i,
425 XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS),
426 XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS));
427 if (XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS) == 0) {
[0e7380f]428 return XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS) == 1 ? EOK : EIO;
[e6b0dba]429 }
[c9d905f]430 async_usleep(XHCI_LEGSUP_POLLING_DELAY_1MS);
[e6b0dba]431 }
[a1732929]432 usb_log_error("BIOS did not release XHCI legacy hold!");
[e6b0dba]433
[91ca111]434 return ENOTSUP;
[cb89430]435}
436
[eb928c4]437/**
[665368c]438 * Ask the xHC to reset its state. Implements sequence
[eb928c4]439 */
[cb89430]440static int hc_reset(xhci_hc_t *hc)
441{
[0e7380f]442 if (xhci_reg_wait(&hc->op_regs->usbsts, XHCI_REG_MASK(XHCI_OP_CNR), 0))
443 return ETIMEOUT;
444
[cb89430]445 /* Stop the HC: set R/S to 0 */
446 XHCI_REG_CLR(hc->op_regs, XHCI_OP_RS, 1);
447
[0e7380f]448 /* Wait until the HC is halted - it shall take at most 16 ms */
449 if (xhci_reg_wait(&hc->op_regs->usbsts, XHCI_REG_MASK(XHCI_OP_HCH), XHCI_REG_MASK(XHCI_OP_HCH)))
450 return ETIMEOUT;
[cb89430]451
452 /* Reset */
453 XHCI_REG_SET(hc->op_regs, XHCI_OP_HCRST, 1);
454
455 /* Wait until the reset is complete */
[0e7380f]456 if (xhci_reg_wait(&hc->op_regs->usbcmd, XHCI_REG_MASK(XHCI_OP_HCRST), 0))
457 return ETIMEOUT;
[cb89430]458
459 return EOK;
460}
461
462/**
463 * Initialize the HC: section 4.2
464 */
[e4d7363]465int hc_start(xhci_hc_t *hc, bool irq)
[cb89430]466{
467 int err;
468
469 if ((err = hc_reset(hc)))
470 return err;
471
[0e7380f]472 if (xhci_reg_wait(&hc->op_regs->usbsts, XHCI_REG_MASK(XHCI_OP_CNR), 0))
473 return ETIMEOUT;
[cb89430]474
[b80c1ab]475 uint64_t dcbaaptr = hc->dcbaa_dma.phys;
[cb89430]476 XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_LO, LOWER32(dcbaaptr));
477 XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_HI, UPPER32(dcbaaptr));
[15f8079]478 XHCI_REG_WR(hc->op_regs, XHCI_OP_MAX_SLOTS_EN, hc->max_slots);
[cb89430]479
[fb28cde]480 uintptr_t crcr;
481 xhci_trb_ring_reset_dequeue_state(&hc->cr.trb_ring, &crcr);
[4abb134]482 XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_LO, LOWER32(crcr));
483 XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, UPPER32(crcr));
[cb89430]484
[665368c]485 XHCI_REG_SET(hc->op_regs, XHCI_OP_EWE, 1);
486
[cb89430]487 xhci_interrupter_regs_t *intr0 = &hc->rt_regs->ir[0];
488 XHCI_REG_WR(intr0, XHCI_INTR_ERSTSZ, hc->event_ring.segment_count);
[b80c1ab]489 uint64_t erdp = hc->event_ring.dequeue_ptr;
[12fba858]490 XHCI_REG_WR(intr0, XHCI_INTR_ERDP_LO, LOWER32(erdp));
491 XHCI_REG_WR(intr0, XHCI_INTR_ERDP_HI, UPPER32(erdp));
[b80c1ab]492 uint64_t erstptr = hc->event_ring.erst.phys;
[cb89430]493 XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_LO, LOWER32(erstptr));
494 XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_HI, UPPER32(erstptr));
495
[665368c]496
[cb89430]497 if (irq) {
498 XHCI_REG_SET(intr0, XHCI_INTR_IE, 1);
499 XHCI_REG_SET(hc->op_regs, XHCI_OP_INTE, 1);
500 }
501
[503086d8]502 XHCI_REG_SET(hc->op_regs, XHCI_OP_HSEE, 1);
503
[cb89430]504 XHCI_REG_SET(hc->op_regs, XHCI_OP_RS, 1);
505
[05770666]506 xhci_rh_startup(&hc->rh);
[dcf0597]507
[cb89430]508 return EOK;
509}
510
[ab5a0830]511/**
512 * Used only when polling. Shall supplement the irq_commands.
513 */
[32fb6bce]514int hc_status(bus_t *bus, uint32_t *status)
[5cbccd4]515{
[32fb6bce]516 xhci_hc_t *hc = bus_to_hc(bus);
[ab5a0830]517 int ip = XHCI_REG_RD(hc->rt_regs->ir, XHCI_INTR_IP);
518 if (ip) {
519 *status = XHCI_REG_RD(hc->op_regs, XHCI_OP_STATUS);
520 XHCI_REG_WR(hc->op_regs, XHCI_OP_STATUS, *status & XHCI_STATUS_ACK_MASK);
521 XHCI_REG_WR(hc->rt_regs->ir, XHCI_INTR_IP, 1);
522
523 /* interrupt handler expects status from irq_commands, which is
524 * in xhci order. */
525 *status = host2xhci(32, *status);
526 }
[62ba2cbe]527
[598733c9]528 usb_log_debug2("Polled status: %x", *status);
[cb89430]529 return EOK;
530}
531
[665368c]532static int xhci_handle_mfindex_wrap_event(xhci_hc_t *hc, xhci_trb_t *trb)
533{
[94e9c29]534 struct timeval tv;
535 getuptime(&tv);
[598733c9]536 usb_log_debug2("Microframe index wrapped (@%lu.%li, %"PRIu64" total).", tv.tv_sec, tv.tv_usec, hc->wrap_count);
[94e9c29]537 hc->wrap_time = ((uint64_t) tv.tv_sec) * 1000000 + ((uint64_t) tv.tv_usec);
[665368c]538 ++hc->wrap_count;
539 return EOK;
540}
541
[fb154e13]542static int handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
543{
544 uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
545 usb_log_debug("Port status change event detected for port %u.", port_id);
546 xhci_rh_handle_port_change(&hc->rh, port_id);
547 return EOK;
548}
549
[472235a]550typedef int (*event_handler) (xhci_hc_t *, xhci_trb_t *trb);
551
[2c0564c]552/**
553 * These events are handled by separate event handling fibril.
554 */
[472235a]555static event_handler event_handlers [] = {
[fb154e13]556 [XHCI_TRB_TYPE_PORT_STATUS_CHANGE_EVENT] = &handle_port_status_change_event,
[e9e24f2]557 [XHCI_TRB_TYPE_TRANSFER_EVENT] = &xhci_handle_transfer_event,
[2c0564c]558};
559
560/**
561 * These events are handled directly in the interrupt handler, thus they must
562 * not block waiting for another interrupt.
563 */
564static event_handler event_handlers_fast [] = {
565 [XHCI_TRB_TYPE_COMMAND_COMPLETION_EVENT] = &xhci_handle_command_completion,
[665368c]566 [XHCI_TRB_TYPE_MFINDEX_WRAP_EVENT] = &xhci_handle_mfindex_wrap_event,
[472235a]567};
568
[2c0564c]569static int hc_handle_event(xhci_hc_t *hc, xhci_trb_t *trb)
570{
571 const unsigned type = TRB_TYPE(*trb);
572
573 if (type <= ARRAY_SIZE(event_handlers_fast) && event_handlers_fast[type])
574 return event_handlers_fast[type](hc, trb);
575
576 if (type <= ARRAY_SIZE(event_handlers) && event_handlers[type])
577 return xhci_sw_ring_enqueue(&hc->sw_ring, trb);
578
579 return ENOTSUP;
580}
581
582static int event_worker(void *arg)
[7ee5408]583{
[2c0564c]584 int err;
585 xhci_trb_t trb;
586 xhci_hc_t * const hc = arg;
587 assert(hc);
588
589 while (xhci_sw_ring_dequeue(&hc->sw_ring, &trb) != EINTR) {
590 const unsigned type = TRB_TYPE(trb);
[472235a]591
[2c0564c]592 if ((err = event_handlers[type](hc, &trb)))
593 usb_log_error("Failed to handle event: %s", str_error(err));
594 }
595
596 // TODO: completion_complete
597 fibril_mutex_lock(&hc->event_fibril_completion.guard);
598 hc->event_fibril_completion.active = false;
599 fibril_condvar_wait(&hc->event_fibril_completion.cv, &hc->event_fibril_completion.guard);
600 fibril_mutex_unlock(&hc->event_fibril_completion.guard);
601
602 return EOK;
[7ee5408]603}
604
[eb928c4]605/**
606 * Dequeue from event ring and handle dequeued events.
607 *
608 * As there can be events, that blocks on waiting for subsequent events,
609 * we solve this problem by first copying the event TRBs from the event ring,
610 * then asserting EHB and only after, handling the events.
611 *
612 * Whenever the event handling blocks, it switches fibril, and incoming
613 * IPC notification will create new event handling fibril for us.
614 */
[cb89430]615static void hc_run_event_ring(xhci_hc_t *hc, xhci_event_ring_t *event_ring, xhci_interrupter_regs_t *intr)
[62ba2cbe]616{
[cb89430]617 int err;
[472235a]618
[f3baab1]619 xhci_trb_t trb;
620 hc->event_handler = fibril_get_id();
[e50bdd92]621
[f3baab1]622 while ((err = xhci_event_ring_dequeue(event_ring, &trb)) != ENOENT) {
[2c0564c]623 if ((err = hc_handle_event(hc, &trb)) != EOK) {
624 usb_log_error("Failed to handle event in interrupt: %s", str_error(err));
[adb4e683]625 }
[f543804]626
627 uint64_t erdp = hc->event_ring.dequeue_ptr;
628 XHCI_REG_WR(intr, XHCI_INTR_ERDP_LO, LOWER32(erdp));
629 XHCI_REG_WR(intr, XHCI_INTR_ERDP_HI, UPPER32(erdp));
[cb89430]630 }
631
[f3baab1]632 hc->event_handler = 0;
633
[adb4e683]634 /* Update the ERDP to make room in the ring. */
[12fba858]635 uint64_t erdp = hc->event_ring.dequeue_ptr;
[f543804]636 erdp |= XHCI_REG_MASK(XHCI_INTR_ERDP_EHB);
[12fba858]637 XHCI_REG_WR(intr, XHCI_INTR_ERDP_LO, LOWER32(erdp));
638 XHCI_REG_WR(intr, XHCI_INTR_ERDP_HI, UPPER32(erdp));
[adb4e683]639
[472235a]640 usb_log_debug2("Event ring run finished.");
[cb89430]641}
642
[eb928c4]643/**
644 * Handle an interrupt request from xHC. Resolve all situations that trigger an
645 * interrupt separately.
646 *
647 * Note that all RW1C bits in USBSTS register are cleared at the time of
648 * handling the interrupt in irq_code. This method is the top-half.
649 *
650 * @param status contents of USBSTS register at the time of the interrupt.
651 */
[32fb6bce]652void hc_interrupt(bus_t *bus, uint32_t status)
[cb89430]653{
[32fb6bce]654 xhci_hc_t *hc = bus_to_hc(bus);
[ab5a0830]655 status = xhci2host(32, status);
[aee352c]656
[cb89430]657 if (status & XHCI_REG_MASK(XHCI_OP_HSE)) {
658 usb_log_error("Host controller error occured. Bad things gonna happen...");
[ab5a0830]659 status &= ~XHCI_REG_MASK(XHCI_OP_HSE);
[cb89430]660 }
661
662 if (status & XHCI_REG_MASK(XHCI_OP_EINT)) {
[472235a]663 usb_log_debug2("Event interrupt, running the event ring.");
[ab5a0830]664 hc_run_event_ring(hc, &hc->event_ring, &hc->rt_regs->ir[0]);
665 status &= ~XHCI_REG_MASK(XHCI_OP_EINT);
[cb89430]666 }
[275f529]667
[cb89430]668 if (status & XHCI_REG_MASK(XHCI_OP_SRE)) {
669 usb_log_error("Save/Restore error occured. WTF, S/R mechanism not implemented!");
[ab5a0830]670 status &= ~XHCI_REG_MASK(XHCI_OP_SRE);
671 }
672
[fb154e13]673 /* According to Note on p. 302, we may safely ignore the PCD bit. */
674 status &= ~XHCI_REG_MASK(XHCI_OP_PCD);
675
[ab5a0830]676 if (status) {
677 usb_log_error("Non-zero status after interrupt handling (%08x) - missing something?", status);
[cb89430]678 }
679}
680
[eb928c4]681/**
682 * Tear down all in-memory structures.
683 */
[e4d7363]684void hc_fini(xhci_hc_t *hc)
[cb89430]685{
[2c0564c]686 xhci_sw_ring_stop(&hc->sw_ring);
687
688 // TODO: completion_wait
689 fibril_mutex_lock(&hc->event_fibril_completion.guard);
690 while (hc->event_fibril_completion.active)
691 fibril_condvar_wait(&hc->event_fibril_completion.cv, &hc->event_fibril_completion.guard);
692 fibril_mutex_unlock(&hc->event_fibril_completion.guard);
693 xhci_sw_ring_fini(&hc->sw_ring);
694
[e6b9182]695 xhci_bus_fini(&hc->bus);
[cb89430]696 xhci_event_ring_fini(&hc->event_ring);
[b60944b]697 xhci_scratchpad_free(hc);
698 dma_buffer_free(&hc->dcbaa_dma);
[c46c356]699 xhci_fini_commands(hc);
[d32d51d]700 xhci_rh_fini(&hc->rh);
[20eaa82]701 pio_disable(hc->reg_base, RNGSZ(hc->mmio_range));
[837581fd]702 usb_log_info("Finalized.");
[62ba2cbe]703}
704
[51c1d500]705unsigned hc_speed_to_psiv(usb_speed_t speed)
706{
707 assert(speed < ARRAY_SIZE(usb_speed_to_psiv));
708 return usb_speed_to_psiv[speed];
709}
710
[eb928c4]711/**
712 * Ring a xHC Doorbell. Implements section 4.7.
713 */
[708d8fcd]714void hc_ring_doorbell(xhci_hc_t *hc, unsigned doorbell, unsigned target)
[a0be5d0]715{
716 assert(hc);
717 uint32_t v = host2xhci(32, target & BIT_RRANGE(uint32_t, 7));
718 pio_write_32(&hc->db_arry[doorbell], v);
[2896ff6]719 usb_log_debug2("Ringing doorbell %d (target: %d)", doorbell, target);
[a0be5d0]720}
[5cbccd4]721
[51c1d500]722/**
723 * Return an index to device context.
724 */
725static uint8_t endpoint_dci(xhci_endpoint_t *ep)
726{
727 return (2 * ep->base.endpoint) +
728 (ep->base.transfer_type == USB_TRANSFER_CONTROL
729 || ep->base.direction == USB_DIRECTION_IN);
730}
731
732void hc_ring_ep_doorbell(xhci_endpoint_t *ep, uint32_t stream_id)
733{
734 xhci_device_t * const dev = xhci_ep_to_dev(ep);
735 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
736 const uint8_t dci = endpoint_dci(ep);
737 const uint32_t target = (stream_id << 16) | (dci & 0x1ff);
738 hc_ring_doorbell(hc, dev->slot_id, target);
739}
740
[eb928c4]741/**
[7e5a12b]742 * Issue an Enable Slot command. Allocate memory for the slot and fill the
743 * DCBAA with the newly created slot.
[eb928c4]744 */
[7e5a12b]745int hc_enable_slot(xhci_device_t *dev)
[8ea7459]746{
747 int err;
[7e5a12b]748 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
749
750 /* Prepare memory for the context */
[7ec7b7e]751 if ((err = dma_buffer_alloc(&dev->dev_ctx, XHCI_DEVICE_CTX_SIZE(hc))))
[7e5a12b]752 return err;
[7ec7b7e]753 memset(dev->dev_ctx.virt, 0, XHCI_DEVICE_CTX_SIZE(hc));
[7e5a12b]754
755 /* Get the slot number */
[8ea7459]756 xhci_cmd_t cmd;
[c3d926f3]757 xhci_cmd_init(&cmd, XHCI_CMD_ENABLE_SLOT);
[8ea7459]758
[7e5a12b]759 err = xhci_cmd_sync(hc, &cmd);
[8ea7459]760
[7e5a12b]761 /* Link them together */
762 if (err == EOK) {
763 dev->slot_id = cmd.slot_id;
764 hc->dcbaa[dev->slot_id] = host2xhci(64, dev->dev_ctx.phys);
[c3d926f3]765 }
[8ea7459]766
767 xhci_cmd_fini(&cmd);
[abb5d08]768
769 if (err)
770 dma_buffer_free(&dev->dev_ctx);
771
[c3d926f3]772 return err;
[8ea7459]773}
774
[eb928c4]775/**
776 * Issue a Disable Slot command for a slot occupied by device.
[7e5a12b]777 * Frees the device context.
[eb928c4]778 */
[7e5a12b]779int hc_disable_slot(xhci_device_t *dev)
[f270ecb]780{
[9620a54]781 int err;
[7e5a12b]782 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
[9620a54]783
784 if ((err = xhci_cmd_sync_inline(hc, DISABLE_SLOT, .slot_id = dev->slot_id))) {
785 return err;
786 }
787
788 /* Free the device context. */
789 hc->dcbaa[dev->slot_id] = 0;
[b80c1ab]790 dma_buffer_free(&dev->dev_ctx);
[9620a54]791
792 /* Mark the slot as invalid. */
793 dev->slot_id = 0;
794
795 return EOK;
[f270ecb]796}
797
[eb928c4]798/**
799 * Prepare an empty Endpoint Input Context inside a dma buffer.
800 */
[a4e7e6e1]801static int create_configure_ep_input_ctx(xhci_device_t *dev, dma_buffer_t *dma_buf)
[b724494]802{
[7ec7b7e]803 const xhci_hc_t * hc = bus_to_hc(dev->base.bus);
804 const int err = dma_buffer_alloc(dma_buf, XHCI_INPUT_CTX_SIZE(hc));
[b80c1ab]805 if (err)
806 return err;
[b724494]807
[b80c1ab]808 xhci_input_ctx_t *ictx = dma_buf->virt;
[7ec7b7e]809 memset(ictx, 0, XHCI_INPUT_CTX_SIZE(hc));
[b724494]810
[e76c0ea]811 // Quoting sec. 4.6.5 and 4.6.6: A1, D0, D1 are down (already zeroed), A0 is up.
[7ec7b7e]812 XHCI_INPUT_CTRL_CTX_ADD_SET(*XHCI_GET_CTRL_CTX(ictx, hc), 0);
813 xhci_slot_ctx_t *slot_ctx = XHCI_GET_SLOT_CTX(XHCI_GET_DEVICE_CTX(ictx, hc), hc);
814 xhci_setup_slot_context(dev, slot_ctx);
[001778c]815
[b724494]816 return EOK;
817}
818
[eb928c4]819/**
820 * Initialize a device, assigning it an address. Implements section 4.3.4.
821 *
822 * @param dev Device to assing an address (unconfigured yet)
823 */
[51c1d500]824int hc_address_device(xhci_device_t *dev)
[b724494]825{
[0206d35]826 int err = ENOMEM;
[a4e7e6e1]827 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
[51c1d500]828 xhci_endpoint_t *ep0 = xhci_endpoint_get(dev->base.endpoints[0]);
[0206d35]829
[2cf28b9]830 /* Although we have the precise PSIV value on devices of tier 1,
831 * we have to rely on reverse mapping on others. */
[a75f9cbc]832 if (!usb_speed_to_psiv[dev->base.speed]) {
833 usb_log_error("Device reported an USB speed (%s) that cannot be mapped to HC port speed.", usb_str_speed(dev->base.speed));
[2cf28b9]834 return EINVAL;
835 }
836
[b724494]837 /* Issue configure endpoint command (sec 4.3.5). */
[b80c1ab]838 dma_buffer_t ictx_dma_buf;
[7e5a12b]839 if ((err = create_configure_ep_input_ctx(dev, &ictx_dma_buf)))
840 return err;
[b80c1ab]841 xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
[b724494]842
843 /* Copy endpoint 0 context and set A1 flag. */
[7ec7b7e]844 XHCI_INPUT_CTRL_CTX_ADD_SET(*XHCI_GET_CTRL_CTX(ictx, hc), 1);
[51c1d500]845 xhci_ep_ctx_t *ep_ctx = XHCI_GET_EP_CTX(XHCI_GET_DEVICE_CTX(ictx, hc), hc, 1);
[7ec7b7e]846 xhci_setup_endpoint_context(ep0, ep_ctx);
[51c1d500]847
[69b2dfee]848 /* Address device needs Ctx entries set to 1 only */
[7ec7b7e]849 xhci_slot_ctx_t *slot_ctx = XHCI_GET_SLOT_CTX(XHCI_GET_DEVICE_CTX(ictx, hc), hc);
[69b2dfee]850 XHCI_SLOT_CTX_ENTRIES_SET(*slot_ctx, 1);
851
[c3d926f3]852 /* Issue Address Device command. */
[7e5a12b]853 if ((err = xhci_cmd_sync_inline(hc, ADDRESS_DEVICE, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf)))
854 return err;
[b724494]855
[7ec7b7e]856 xhci_device_ctx_t *device_ctx = dev->dev_ctx.virt;
857 dev->base.address = XHCI_SLOT_DEVICE_ADDRESS(*XHCI_GET_SLOT_CTX(device_ctx, hc));
[a1732929]858 usb_log_debug2("Obtained USB address: %d.", dev->base.address);
[0206d35]859
[b724494]860 return EOK;
861}
862
[eb928c4]863/**
864 * Issue a Configure Device command for a device in slot.
865 *
866 * @param slot_id Slot ID assigned to the device.
867 */
[a4e7e6e1]868int hc_configure_device(xhci_device_t *dev)
[b724494]869{
[a4e7e6e1]870 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
871
[b724494]872 /* Issue configure endpoint command (sec 4.3.5). */
[b80c1ab]873 dma_buffer_t ictx_dma_buf;
[a4e7e6e1]874 const int err = create_configure_ep_input_ctx(dev, &ictx_dma_buf);
[928afc8d]875 if (err)
[c3d926f3]876 return err;
[b724494]877
[a4e7e6e1]878 return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf);
[b724494]879}
880
[eb928c4]881/**
882 * Issue a Deconfigure Device command for a device in slot.
883 *
[a4e7e6e1]884 * @param dev The owner of the device
[eb928c4]885 */
[a4e7e6e1]886int hc_deconfigure_device(xhci_device_t *dev)
[b724494]887{
[a4e7e6e1]888 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
889
[b724494]890 /* Issue configure endpoint command (sec 4.3.5) with the DC flag. */
[a4e7e6e1]891 return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = dev->slot_id, .deconfigure = true);
[b724494]892}
893
[eb928c4]894/**
895 * Instruct xHC to add an endpoint with supplied endpoint context.
896 *
[a4e7e6e1]897 * @param dev The owner of the device
898 * @param ep_idx Endpoint DCI in question
[eb928c4]899 * @param ep_ctx Endpoint context of the endpoint
900 */
[51c1d500]901int hc_add_endpoint(xhci_endpoint_t *ep)
[b724494]902{
[51c1d500]903 xhci_device_t * const dev = xhci_ep_to_dev(ep);
904 const unsigned dci = endpoint_dci(ep);
905
[b724494]906 /* Issue configure endpoint command (sec 4.3.5). */
[b80c1ab]907 dma_buffer_t ictx_dma_buf;
[a4e7e6e1]908 const int err = create_configure_ep_input_ctx(dev, &ictx_dma_buf);
[928afc8d]909 if (err)
[c3d926f3]910 return err;
[b724494]911
[b80c1ab]912 xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
[001778c]913
[a4e7e6e1]914 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
[51c1d500]915 XHCI_INPUT_CTRL_CTX_ADD_SET(*XHCI_GET_CTRL_CTX(ictx, hc), dci);
[7ec7b7e]916
[51c1d500]917 xhci_ep_ctx_t *ep_ctx = XHCI_GET_EP_CTX(XHCI_GET_DEVICE_CTX(ictx, hc), hc, dci);
918 xhci_setup_endpoint_context(ep, ep_ctx);
[7ec7b7e]919
[a4e7e6e1]920 return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf);
[b724494]921}
922
[eb928c4]923/**
924 * Instruct xHC to drop an endpoint.
925 *
[a4e7e6e1]926 * @param dev The owner of the endpoint
927 * @param ep_idx Endpoint DCI in question
[eb928c4]928 */
[51c1d500]929int hc_drop_endpoint(xhci_endpoint_t *ep)
[b724494]930{
[51c1d500]931 xhci_device_t * const dev = xhci_ep_to_dev(ep);
932 const unsigned dci = endpoint_dci(ep);
933
[b724494]934 /* Issue configure endpoint command (sec 4.3.5). */
[b80c1ab]935 dma_buffer_t ictx_dma_buf;
[a4e7e6e1]936 const int err = create_configure_ep_input_ctx(dev, &ictx_dma_buf);
[928afc8d]937 if (err)
[c3d926f3]938 return err;
[b724494]939
[7ec7b7e]940 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
[b80c1ab]941 xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
[51c1d500]942 XHCI_INPUT_CTRL_CTX_DROP_SET(*XHCI_GET_CTRL_CTX(ictx, hc), dci);
[b724494]943
[a4e7e6e1]944 return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf);
[b724494]945}
946
[eb928c4]947/**
948 * Instruct xHC to update information about an endpoint, using supplied
949 * endpoint context.
950 *
[a4e7e6e1]951 * @param dev The owner of the endpoint
952 * @param ep_idx Endpoint DCI in question
[eb928c4]953 * @param ep_ctx Endpoint context of the endpoint
954 */
[51c1d500]955int hc_update_endpoint(xhci_endpoint_t *ep)
[306a36d]956{
[51c1d500]957 xhci_device_t * const dev = xhci_ep_to_dev(ep);
958 const unsigned dci = endpoint_dci(ep);
959
[306a36d]960 dma_buffer_t ictx_dma_buf;
[7ec7b7e]961 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
962
963 const int err = dma_buffer_alloc(&ictx_dma_buf, XHCI_INPUT_CTX_SIZE(hc));
[306a36d]964 if (err)
965 return err;
966
967 xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
[7ec7b7e]968 memset(ictx, 0, XHCI_INPUT_CTX_SIZE(hc));
[306a36d]969
[51c1d500]970 XHCI_INPUT_CTRL_CTX_ADD_SET(*XHCI_GET_CTRL_CTX(ictx, hc), dci);
971 xhci_ep_ctx_t *ep_ctx = XHCI_GET_EP_CTX(XHCI_GET_DEVICE_CTX(ictx, hc), hc, dci);
972 xhci_setup_endpoint_context(ep, ep_ctx);
[306a36d]973
[a4e7e6e1]974 return xhci_cmd_sync_inline(hc, EVALUATE_CONTEXT, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf);
[306a36d]975}
976
[30fc56f]977/**
978 * Instruct xHC to stop running a transfer ring on an endpoint.
979 *
[a4e7e6e1]980 * @param dev The owner of the endpoint
981 * @param ep_idx Endpoint DCI in question
[30fc56f]982 */
[51c1d500]983int hc_stop_endpoint(xhci_endpoint_t *ep)
[30fc56f]984{
[51c1d500]985 xhci_device_t * const dev = xhci_ep_to_dev(ep);
986 const unsigned dci = endpoint_dci(ep);
[a4e7e6e1]987 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
[51c1d500]988 return xhci_cmd_sync_inline(hc, STOP_ENDPOINT, .slot_id = dev->slot_id, .endpoint_id = dci);
[30fc56f]989}
990
[feabe163]991/**
992 * Instruct xHC to reset halted endpoint.
993 *
[a4e7e6e1]994 * @param dev The owner of the endpoint
995 * @param ep_idx Endpoint DCI in question
[feabe163]996 */
[51c1d500]997int hc_reset_endpoint(xhci_endpoint_t *ep)
[feabe163]998{
[51c1d500]999 xhci_device_t * const dev = xhci_ep_to_dev(ep);
1000 const unsigned dci = endpoint_dci(ep);
[a4e7e6e1]1001 xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
[51c1d500]1002 return xhci_cmd_sync_inline(hc, RESET_ENDPOINT, .slot_id = dev->slot_id, .endpoint_id = dci);
1003}
1004
1005/**
1006 * Reset a ring position in both software and hardware.
1007 *
1008 * @param dev The owner of the endpoint
1009 */
1010int hc_reset_ring(xhci_endpoint_t *ep, uint32_t stream_id)
1011{
1012 xhci_device_t * const dev = xhci_ep_to_dev(ep);
1013 const unsigned dci = endpoint_dci(ep);
1014 uintptr_t addr;
1015
1016 xhci_trb_ring_t *ring = xhci_endpoint_get_ring(ep, stream_id);
1017 xhci_trb_ring_reset_dequeue_state(ring, &addr);
1018
1019 xhci_hc_t * const hc = bus_to_hc(endpoint_get_bus(&ep->base));
1020 return xhci_cmd_sync_inline(hc, SET_TR_DEQUEUE_POINTER,
1021 .slot_id = dev->slot_id,
1022 .endpoint_id = dci,
1023 .stream_id = stream_id,
1024 .dequeue_ptr = addr,
1025 );
[feabe163]1026}
1027
[5cbccd4]1028/**
1029 * @}
1030 */
Note: See TracBrowser for help on using the repository browser.