| [c9c0e41] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Jaroslav Jindrak
|
|---|
| 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 Command sending functions.
|
|---|
| 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 "commands.h"
|
|---|
| 41 | #include "debug.h"
|
|---|
| 42 | #include "hc.h"
|
|---|
| [8db42f7] | 43 | #include "hw_struct/context.h"
|
|---|
| [c9c0e41] | 44 | #include "hw_struct/trb.h"
|
|---|
| 45 |
|
|---|
| [1b78a7c1] | 46 | #define TRB_SET_TCS(trb, tcs) (trb).control |= host2xhci(32, ((tcs &0x1) << 9))
|
|---|
| 47 | #define TRB_SET_TYPE(trb, type) (trb).control |= host2xhci(32, (type) << 10)
|
|---|
| [b724494] | 48 | #define TRB_SET_DC(trb, dc) (trb).control |= host2xhci(32, (dc) << 9)
|
|---|
| [1b78a7c1] | 49 | #define TRB_SET_EP(trb, ep) (trb).control |= host2xhci(32, ((ep) & 0x5) << 16)
|
|---|
| [0cabd10] | 50 | #define TRB_SET_STREAM(trb, st) (trb).control |= host2xhci(32, ((st) & 0xFFFF) << 16)
|
|---|
| [1b78a7c1] | 51 | #define TRB_SET_SUSP(trb, susp) (trb).control |= host2xhci(32, ((susp) & 0x1) << 23)
|
|---|
| 52 | #define TRB_SET_SLOT(trb, slot) (trb).control |= host2xhci(32, (slot) << 24)
|
|---|
| [60af4cdb] | 53 | #define TRB_SET_DEV_SPEED(trb, speed) (trb).control |= host2xhci(32, (speed & 0xF) << 16)
|
|---|
| [1b78a7c1] | 54 |
|
|---|
| [0cabd10] | 55 | /**
|
|---|
| 56 | * TODO: Not sure about SCT and DCS (see section 6.4.3.9).
|
|---|
| 57 | */
|
|---|
| 58 | #define TRB_SET_DEQUEUE_PTR(trb, dptr) (trb).parameter |= host2xhci(64, (dptr))
|
|---|
| [548c123] | 59 | #define TRB_SET_ICTX(trb, phys) (trb).parameter |= host2xhci(64, phys_addr & (~0xF))
|
|---|
| [1b78a7c1] | 60 |
|
|---|
| 61 | #define TRB_GET_CODE(trb) XHCI_DWORD_EXTRACT((trb).status, 31, 24)
|
|---|
| 62 | #define TRB_GET_SLOT(trb) XHCI_DWORD_EXTRACT((trb).control, 31, 24)
|
|---|
| 63 | #define TRB_GET_PHYS(trb) (XHCI_QWORD_EXTRACT((trb).parameter, 63, 4) << 4)
|
|---|
| 64 |
|
|---|
| [110d795] | 65 | int xhci_init_commands(xhci_hc_t *hc)
|
|---|
| 66 | {
|
|---|
| 67 | assert(hc);
|
|---|
| 68 |
|
|---|
| 69 | list_initialize(&hc->commands);
|
|---|
| [74b852b] | 70 |
|
|---|
| 71 | fibril_mutex_initialize(&hc->commands_mtx);
|
|---|
| 72 |
|
|---|
| [110d795] | 73 | return EOK;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| [c46c356] | 76 | void xhci_fini_commands(xhci_hc_t *hc)
|
|---|
| 77 | {
|
|---|
| 78 | // Note: Untested.
|
|---|
| 79 | assert(hc);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| [913007f] | 82 | int xhci_cmd_wait(xhci_cmd_t *cmd, suseconds_t timeout)
|
|---|
| [110d795] | 83 | {
|
|---|
| [4688350b] | 84 | int rv = EOK;
|
|---|
| 85 |
|
|---|
| 86 | fibril_mutex_lock(&cmd->completed_mtx);
|
|---|
| [110d795] | 87 | while (!cmd->completed) {
|
|---|
| [4688350b] | 88 | usb_log_debug2("Waiting for event completion: going to sleep.");
|
|---|
| 89 | rv = fibril_condvar_wait_timeout(&cmd->completed_cv, &cmd->completed_mtx, timeout);
|
|---|
| [110d795] | 90 |
|
|---|
| [4688350b] | 91 | usb_log_debug2("Waiting for event completion: woken: %s", str_error(rv));
|
|---|
| 92 | if (rv == ETIMEOUT)
|
|---|
| 93 | break;
|
|---|
| [110d795] | 94 | }
|
|---|
| [1f76b7d] | 95 | fibril_mutex_unlock(&cmd->completed_mtx);
|
|---|
| [110d795] | 96 |
|
|---|
| [4688350b] | 97 | return rv;
|
|---|
| [110d795] | 98 | }
|
|---|
| 99 |
|
|---|
| [04df063] | 100 | xhci_cmd_t *xhci_cmd_alloc(void)
|
|---|
| [110d795] | 101 | {
|
|---|
| [078e0e6] | 102 | xhci_cmd_t *cmd = malloc(sizeof(xhci_cmd_t));
|
|---|
| [4688350b] | 103 | xhci_cmd_init(cmd);
|
|---|
| [04df063] | 104 |
|
|---|
| 105 | usb_log_debug2("Allocating cmd on the heap. Don't forget to deallocate it!");
|
|---|
| [4688350b] | 106 | return cmd;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void xhci_cmd_init(xhci_cmd_t *cmd)
|
|---|
| 110 | {
|
|---|
| 111 | memset(cmd, 0, sizeof(*cmd));
|
|---|
| [110d795] | 112 |
|
|---|
| 113 | link_initialize(&cmd->link);
|
|---|
| 114 |
|
|---|
| [4688350b] | 115 | fibril_mutex_initialize(&cmd->completed_mtx);
|
|---|
| 116 | fibril_condvar_initialize(&cmd->completed_cv);
|
|---|
| [110d795] | 117 | }
|
|---|
| 118 |
|
|---|
| [04df063] | 119 | void xhci_cmd_fini(xhci_cmd_t *cmd)
|
|---|
| [110d795] | 120 | {
|
|---|
| [9304b66] | 121 | list_remove(&cmd->link);
|
|---|
| [04df063] | 122 | }
|
|---|
| [9304b66] | 123 |
|
|---|
| [04df063] | 124 | void xhci_cmd_free(xhci_cmd_t *cmd)
|
|---|
| 125 | {
|
|---|
| 126 | xhci_cmd_fini(cmd);
|
|---|
| [078e0e6] | 127 | free(cmd);
|
|---|
| [110d795] | 128 | }
|
|---|
| 129 |
|
|---|
| [2fa43d1] | 130 | static inline xhci_cmd_t *get_command(xhci_hc_t *hc, uint64_t phys)
|
|---|
| [110d795] | 131 | {
|
|---|
| [74b852b] | 132 | fibril_mutex_lock(&hc->commands_mtx);
|
|---|
| 133 |
|
|---|
| [110d795] | 134 | link_t *cmd_link = list_first(&hc->commands);
|
|---|
| 135 |
|
|---|
| [2fa43d1] | 136 | while (cmd_link != NULL) {
|
|---|
| 137 | xhci_cmd_t *cmd = list_get_instance(cmd_link, xhci_cmd_t, link);
|
|---|
| 138 |
|
|---|
| [548c123] | 139 | if (cmd->trb_phys == phys)
|
|---|
| [2fa43d1] | 140 | break;
|
|---|
| 141 |
|
|---|
| 142 | cmd_link = list_next(cmd_link, &hc->commands);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| [110d795] | 145 | if (cmd_link != NULL) {
|
|---|
| 146 | list_remove(cmd_link);
|
|---|
| [74b852b] | 147 | fibril_mutex_unlock(&hc->commands_mtx);
|
|---|
| [9f5b613] | 148 |
|
|---|
| [110d795] | 149 | return list_get_instance(cmd_link, xhci_cmd_t, link);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| [74b852b] | 152 | fibril_mutex_unlock(&hc->commands_mtx);
|
|---|
| [110d795] | 153 | return NULL;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| [548c123] | 156 | static inline int enqueue_command(xhci_hc_t *hc, xhci_cmd_t *cmd, unsigned doorbell, unsigned target)
|
|---|
| [481af21e] | 157 | {
|
|---|
| [c058a388] | 158 | assert(hc);
|
|---|
| [548c123] | 159 | assert(cmd);
|
|---|
| 160 |
|
|---|
| [74b852b] | 161 | fibril_mutex_lock(&hc->commands_mtx);
|
|---|
| [548c123] | 162 | list_append(&cmd->link, &hc->commands);
|
|---|
| [74b852b] | 163 | fibril_mutex_unlock(&hc->commands_mtx);
|
|---|
| [c058a388] | 164 |
|
|---|
| [548c123] | 165 | xhci_trb_ring_enqueue(&hc->command_ring, &cmd->trb, &cmd->trb_phys);
|
|---|
| [a0be5d0] | 166 | hc_ring_doorbell(hc, doorbell, target);
|
|---|
| [481af21e] | 167 |
|
|---|
| [548c123] | 168 | usb_log_debug2("HC(%p): Sent command:", hc);
|
|---|
| 169 | xhci_dump_trb(&cmd->trb);
|
|---|
| [481af21e] | 170 |
|
|---|
| 171 | return EOK;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| [3dc519f] | 174 | void xhci_stop_command_ring(xhci_hc_t *hc)
|
|---|
| 175 | {
|
|---|
| 176 | assert(hc);
|
|---|
| 177 |
|
|---|
| 178 | XHCI_REG_SET(hc->op_regs, XHCI_OP_CS, 1);
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Note: There is a bug in qemu that checks CS only when CRCR_HI
|
|---|
| 182 | * is written, this (and the read/write in abort) ensures
|
|---|
| 183 | * the command rings stops.
|
|---|
| 184 | */
|
|---|
| 185 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, XHCI_REG_RD(hc->op_regs, XHCI_OP_CRCR_HI));
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | void xhci_abort_command_ring(xhci_hc_t *hc)
|
|---|
| 189 | {
|
|---|
| 190 | assert(hc);
|
|---|
| 191 |
|
|---|
| 192 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CA, 1);
|
|---|
| 193 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, XHCI_REG_RD(hc->op_regs, XHCI_OP_CRCR_HI));
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | void xhci_start_command_ring(xhci_hc_t *hc)
|
|---|
| 197 | {
|
|---|
| 198 | assert(hc);
|
|---|
| 199 |
|
|---|
| 200 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRR, 1);
|
|---|
| [a0be5d0] | 201 | hc_ring_doorbell(hc, 0, 0);
|
|---|
| [3dc519f] | 202 | }
|
|---|
| 203 |
|
|---|
| [4fa5342] | 204 | static const char *trb_codes [] = {
|
|---|
| 205 | #define TRBC(t) [XHCI_TRBC_##t] = #t
|
|---|
| 206 | TRBC(INVALID),
|
|---|
| 207 | TRBC(SUCCESS),
|
|---|
| 208 | TRBC(DATA_BUFFER_ERROR),
|
|---|
| 209 | TRBC(BABBLE_DETECTED_ERROR),
|
|---|
| 210 | TRBC(USB_TRANSACTION_ERROR),
|
|---|
| 211 | TRBC(TRB_ERROR),
|
|---|
| 212 | TRBC(STALL_ERROR),
|
|---|
| 213 | TRBC(RESOURCE_ERROR),
|
|---|
| 214 | TRBC(BANDWIDTH_ERROR),
|
|---|
| 215 | TRBC(NO_SLOTS_ERROR),
|
|---|
| 216 | TRBC(INVALID_STREAM_ERROR),
|
|---|
| 217 | TRBC(SLOT_NOT_ENABLED_ERROR),
|
|---|
| 218 | TRBC(EP_NOT_ENABLED_ERROR),
|
|---|
| 219 | TRBC(SHORT_PACKET),
|
|---|
| 220 | TRBC(RING_UNDERRUN),
|
|---|
| 221 | TRBC(RING_OVERRUN),
|
|---|
| 222 | TRBC(VF_EVENT_RING_FULL),
|
|---|
| 223 | TRBC(PARAMETER_ERROR),
|
|---|
| 224 | TRBC(BANDWIDTH_OVERRUN_ERROR),
|
|---|
| 225 | TRBC(CONTEXT_STATE_ERROR),
|
|---|
| 226 | TRBC(NO_PING_RESPONSE_ERROR),
|
|---|
| 227 | TRBC(EVENT_RING_FULL_ERROR),
|
|---|
| 228 | TRBC(INCOMPATIBLE_DEVICE_ERROR),
|
|---|
| 229 | TRBC(MISSED_SERVICE_ERROR),
|
|---|
| 230 | TRBC(COMMAND_RING_STOPPED),
|
|---|
| 231 | TRBC(COMMAND_ABORTED),
|
|---|
| 232 | TRBC(STOPPED),
|
|---|
| 233 | TRBC(STOPPED_LENGTH_INVALID),
|
|---|
| 234 | TRBC(STOPPED_SHORT_PACKET),
|
|---|
| 235 | TRBC(MAX_EXIT_LATENCY_TOO_LARGE_ERROR),
|
|---|
| 236 | [30] = "<reserved>",
|
|---|
| 237 | TRBC(ISOCH_BUFFER_OVERRUN),
|
|---|
| 238 | TRBC(EVENT_LOST_ERROR),
|
|---|
| 239 | TRBC(UNDEFINED_ERROR),
|
|---|
| 240 | TRBC(INVALID_STREAM_ID_ERROR),
|
|---|
| 241 | TRBC(SECONDARY_BANDWIDTH_ERROR),
|
|---|
| 242 | TRBC(SPLIT_TRANSACTION_ERROR),
|
|---|
| 243 | [XHCI_TRBC_MAX] = NULL
|
|---|
| 244 | #undef TRBC
|
|---|
| 245 | };
|
|---|
| 246 |
|
|---|
| 247 | static void report_error(int code)
|
|---|
| 248 | {
|
|---|
| 249 | if (code < XHCI_TRBC_MAX && trb_codes[code] != NULL)
|
|---|
| 250 | usb_log_error("Command resulted in error: %s.", trb_codes[code]);
|
|---|
| 251 | else
|
|---|
| 252 | usb_log_error("Command resulted in reserved or vendor specific error.");
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| [110d795] | 255 | int xhci_send_no_op_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
|
|---|
| [c9c0e41] | 256 | {
|
|---|
| [c058a388] | 257 | assert(hc);
|
|---|
| 258 |
|
|---|
| [548c123] | 259 | xhci_trb_clean(&cmd->trb);
|
|---|
| [c9c0e41] | 260 |
|
|---|
| [548c123] | 261 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_NO_OP_CMD);
|
|---|
| [110d795] | 262 |
|
|---|
| [548c123] | 263 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [c9c0e41] | 264 | }
|
|---|
| 265 |
|
|---|
| [110d795] | 266 | int xhci_send_enable_slot_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
|
|---|
| [c9c0e41] | 267 | {
|
|---|
| [c058a388] | 268 | assert(hc);
|
|---|
| 269 |
|
|---|
| [548c123] | 270 | xhci_trb_clean(&cmd->trb);
|
|---|
| [c9c0e41] | 271 |
|
|---|
| [548c123] | 272 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_ENABLE_SLOT_CMD);
|
|---|
| 273 | cmd->trb.control |= host2xhci(32, XHCI_REG_RD(hc->xecp, XHCI_EC_SP_SLOT_TYPE) << 16);
|
|---|
| [110d795] | 274 |
|
|---|
| [548c123] | 275 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [5ac5eb1] | 276 | }
|
|---|
| 277 |
|
|---|
| [110d795] | 278 | int xhci_send_disable_slot_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
|
|---|
| [5ac5eb1] | 279 | {
|
|---|
| [c058a388] | 280 | assert(hc);
|
|---|
| [110d795] | 281 | assert(cmd);
|
|---|
| [c058a388] | 282 |
|
|---|
| [548c123] | 283 | xhci_trb_clean(&cmd->trb);
|
|---|
| [5ac5eb1] | 284 |
|
|---|
| [548c123] | 285 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_DISABLE_SLOT_CMD);
|
|---|
| 286 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| [110d795] | 287 |
|
|---|
| [548c123] | 288 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [c9c0e41] | 289 | }
|
|---|
| 290 |
|
|---|
| [04df063] | 291 | int xhci_send_address_device_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
|
|---|
| [8db42f7] | 292 | {
|
|---|
| [c058a388] | 293 | assert(hc);
|
|---|
| [110d795] | 294 | assert(cmd);
|
|---|
| [04df063] | 295 | assert(ictx);
|
|---|
| [c058a388] | 296 |
|
|---|
| [8db42f7] | 297 | /**
|
|---|
| 298 | * TODO: Requirements for this command:
|
|---|
| 299 | * dcbaa[slot_id] is properly sized and initialized
|
|---|
| 300 | * ictx has valids slot context and endpoint 0, all
|
|---|
| 301 | * other should be ignored at this point (see section 4.6.5).
|
|---|
| 302 | */
|
|---|
| [04df063] | 303 |
|
|---|
| [548c123] | 304 | xhci_trb_clean(&cmd->trb);
|
|---|
| [8db42f7] | 305 |
|
|---|
| [04df063] | 306 | uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
|
|---|
| [548c123] | 307 | TRB_SET_ICTX(cmd->trb, phys_addr);
|
|---|
| [8db42f7] | 308 |
|
|---|
| 309 | /**
|
|---|
| 310 | * Note: According to section 6.4.3.4, we can set the 9th bit
|
|---|
| 311 | * of the control field of the trb (BSR) to 1 and then the xHC
|
|---|
| 312 | * will not issue the SET_ADDRESS request to the USB device.
|
|---|
| 313 | * This can be used to provide compatibility with legacy USB devices
|
|---|
| 314 | * that require their device descriptor to be read before such request.
|
|---|
| 315 | */
|
|---|
| [548c123] | 316 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_ADDRESS_DEVICE_CMD);
|
|---|
| 317 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| [8db42f7] | 318 |
|
|---|
| [548c123] | 319 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [8db42f7] | 320 | }
|
|---|
| 321 |
|
|---|
| [04df063] | 322 | int xhci_send_configure_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
|
|---|
| [665bf3c] | 323 | {
|
|---|
| [c058a388] | 324 | assert(hc);
|
|---|
| [110d795] | 325 | assert(cmd);
|
|---|
| [c058a388] | 326 |
|
|---|
| [548c123] | 327 | xhci_trb_clean(&cmd->trb);
|
|---|
| [665bf3c] | 328 |
|
|---|
| [b724494] | 329 | if (!cmd->deconfigure) {
|
|---|
| 330 | /* If the DC flag is on, input context is not evaluated. */
|
|---|
| 331 | assert(ictx);
|
|---|
| 332 |
|
|---|
| 333 | uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
|
|---|
| 334 | TRB_SET_ICTX(cmd->trb, phys_addr);
|
|---|
| 335 | }
|
|---|
| [110d795] | 336 |
|
|---|
| [548c123] | 337 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_CONFIGURE_ENDPOINT_CMD);
|
|---|
| 338 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| [b724494] | 339 | TRB_SET_DC(cmd->trb, cmd->deconfigure);
|
|---|
| [665bf3c] | 340 |
|
|---|
| [548c123] | 341 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [665bf3c] | 342 | }
|
|---|
| 343 |
|
|---|
| [04df063] | 344 | int xhci_send_evaluate_context_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
|
|---|
| [c9ce62ae] | 345 | {
|
|---|
| [c058a388] | 346 | assert(hc);
|
|---|
| [110d795] | 347 | assert(cmd);
|
|---|
| [04df063] | 348 | assert(ictx);
|
|---|
| [c058a388] | 349 |
|
|---|
| [c9ce62ae] | 350 | /**
|
|---|
| 351 | * Note: All Drop Context flags of the input context shall be 0,
|
|---|
| 352 | * all Add Context flags shall be initialize to indicate IDs
|
|---|
| 353 | * of the contexts affected by the command.
|
|---|
| 354 | * Refer to sections 6.2.2.3 and 6.3.3.3 for further info.
|
|---|
| 355 | */
|
|---|
| [548c123] | 356 | xhci_trb_clean(&cmd->trb);
|
|---|
| [c9ce62ae] | 357 |
|
|---|
| [04df063] | 358 | uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
|
|---|
| [548c123] | 359 | TRB_SET_ICTX(cmd->trb, phys_addr);
|
|---|
| [c9ce62ae] | 360 |
|
|---|
| [548c123] | 361 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD);
|
|---|
| 362 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| [110d795] | 363 |
|
|---|
| [548c123] | 364 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [c9ce62ae] | 365 | }
|
|---|
| 366 |
|
|---|
| [110d795] | 367 | int xhci_send_reset_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, uint32_t ep_id, uint8_t tcs)
|
|---|
| [05aeee0e] | 368 | {
|
|---|
| [c058a388] | 369 | assert(hc);
|
|---|
| [110d795] | 370 | assert(cmd);
|
|---|
| [c058a388] | 371 |
|
|---|
| [05aeee0e] | 372 | /**
|
|---|
| 373 | * Note: TCS can have values 0 or 1. If it is set to 0, see sectuon 4.5.8 for
|
|---|
| 374 | * information about this flag.
|
|---|
| 375 | */
|
|---|
| [548c123] | 376 | xhci_trb_clean(&cmd->trb);
|
|---|
| [05aeee0e] | 377 |
|
|---|
| [548c123] | 378 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_RESET_ENDPOINT_CMD);
|
|---|
| 379 | TRB_SET_TCS(cmd->trb, tcs);
|
|---|
| 380 | TRB_SET_EP(cmd->trb, ep_id);
|
|---|
| 381 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| [c9bec1c] | 382 |
|
|---|
| [548c123] | 383 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [05aeee0e] | 384 | }
|
|---|
| 385 |
|
|---|
| [110d795] | 386 | int xhci_send_stop_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, uint32_t ep_id, uint8_t susp)
|
|---|
| [05aeee0e] | 387 | {
|
|---|
| [c058a388] | 388 | assert(hc);
|
|---|
| [110d795] | 389 | assert(cmd);
|
|---|
| [c058a388] | 390 |
|
|---|
| [548c123] | 391 | xhci_trb_clean(&cmd->trb);
|
|---|
| [110d795] | 392 |
|
|---|
| [548c123] | 393 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_STOP_ENDPOINT_CMD);
|
|---|
| 394 | TRB_SET_EP(cmd->trb, ep_id);
|
|---|
| 395 | TRB_SET_SUSP(cmd->trb, susp);
|
|---|
| 396 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| [05aeee0e] | 397 |
|
|---|
| [548c123] | 398 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [c058a388] | 399 | }
|
|---|
| [05aeee0e] | 400 |
|
|---|
| [0cabd10] | 401 | int xhci_send_set_dequeue_ptr_command(xhci_hc_t *hc, xhci_cmd_t *cmd,
|
|---|
| 402 | uintptr_t dequeue_ptr, uint16_t stream_id,
|
|---|
| 403 | uint32_t ep_id)
|
|---|
| 404 | {
|
|---|
| 405 | assert(hc);
|
|---|
| 406 | assert(cmd);
|
|---|
| 407 |
|
|---|
| [548c123] | 408 | xhci_trb_clean(&cmd->trb);
|
|---|
| [0cabd10] | 409 |
|
|---|
| [548c123] | 410 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_SET_TR_DEQUEUE_POINTER_CMD);
|
|---|
| 411 | TRB_SET_EP(cmd->trb, ep_id);
|
|---|
| 412 | TRB_SET_STREAM(cmd->trb, stream_id);
|
|---|
| 413 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| 414 | TRB_SET_DEQUEUE_PTR(cmd->trb, dequeue_ptr);
|
|---|
| [0cabd10] | 415 |
|
|---|
| 416 | /**
|
|---|
| 417 | * TODO: Set DCS (see section 4.6.10).
|
|---|
| 418 | */
|
|---|
| 419 |
|
|---|
| [548c123] | 420 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [0cabd10] | 421 | }
|
|---|
| 422 |
|
|---|
| [110d795] | 423 | int xhci_send_reset_device_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
|
|---|
| [c058a388] | 424 | {
|
|---|
| 425 | assert(hc);
|
|---|
| [110d795] | 426 | assert(cmd);
|
|---|
| [c058a388] | 427 |
|
|---|
| [548c123] | 428 | xhci_trb_clean(&cmd->trb);
|
|---|
| [c058a388] | 429 |
|
|---|
| [548c123] | 430 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_RESET_DEVICE_CMD);
|
|---|
| 431 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| [c9bec1c] | 432 |
|
|---|
| [548c123] | 433 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| [05aeee0e] | 434 | }
|
|---|
| 435 |
|
|---|
| [60af4cdb] | 436 | int xhci_get_port_bandwidth_command(xhci_hc_t *hc, xhci_cmd_t *cmd,
|
|---|
| 437 | xhci_port_bandwidth_ctx_t *ctx, uint8_t device_speed)
|
|---|
| 438 | {
|
|---|
| 439 | assert(hc);
|
|---|
| 440 | assert(cmd);
|
|---|
| 441 |
|
|---|
| 442 | xhci_trb_clean(&cmd->trb);
|
|---|
| 443 |
|
|---|
| 444 | uint64_t phys_addr = (uint64_t) addr_to_phys(ctx);
|
|---|
| 445 | TRB_SET_ICTX(cmd->trb, phys_addr);
|
|---|
| 446 |
|
|---|
| 447 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_GET_PORT_BANDWIDTH_CMD);
|
|---|
| 448 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
|---|
| 449 | TRB_SET_DEV_SPEED(cmd->trb, device_speed);
|
|---|
| 450 |
|
|---|
| 451 | return enqueue_command(hc, cmd, 0, 0);
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| [f9e7fe8] | 454 | int xhci_handle_command_completion(xhci_hc_t *hc, xhci_trb_t *trb)
|
|---|
| 455 | {
|
|---|
| [110d795] | 456 | // TODO: Update dequeue ptrs.
|
|---|
| [c058a388] | 457 | assert(hc);
|
|---|
| 458 | assert(trb);
|
|---|
| 459 |
|
|---|
| [d1d7a92] | 460 | usb_log_debug2("HC(%p) Command completed.", hc);
|
|---|
| [f9e7fe8] | 461 |
|
|---|
| [5ac5eb1] | 462 | int code;
|
|---|
| [2fa43d1] | 463 | uint64_t phys;
|
|---|
| [110d795] | 464 | xhci_cmd_t *command;
|
|---|
| [f711f06] | 465 |
|
|---|
| [1b78a7c1] | 466 | code = TRB_GET_CODE(*trb);
|
|---|
| 467 | phys = TRB_GET_PHYS(*trb);;
|
|---|
| [2fa43d1] | 468 | command = get_command(hc, phys);
|
|---|
| 469 | if (command == NULL) {
|
|---|
| 470 | // TODO: STOP & ABORT may not have command structs in the list!
|
|---|
| [d1d7a92] | 471 | usb_log_warning("No command struct for this completion event found.");
|
|---|
| [2fa43d1] | 472 |
|
|---|
| 473 | if (code != XHCI_TRBC_SUCCESS)
|
|---|
| 474 | report_error(code);
|
|---|
| 475 |
|
|---|
| 476 | return EOK;
|
|---|
| 477 | }
|
|---|
| [110d795] | 478 |
|
|---|
| 479 | command->status = code;
|
|---|
| [1b78a7c1] | 480 | command->slot_id = TRB_GET_SLOT(*trb);
|
|---|
| [110d795] | 481 |
|
|---|
| [548c123] | 482 | usb_log_debug2("Completed command trb: %s", xhci_trb_str_type(TRB_TYPE(command->trb)));
|
|---|
| 483 | if (TRB_TYPE(command->trb) != XHCI_TRB_TYPE_NO_OP_CMD) {
|
|---|
| [665bf3c] | 484 | if (code != XHCI_TRBC_SUCCESS) {
|
|---|
| [4fa5342] | 485 | report_error(code);
|
|---|
| [548c123] | 486 | xhci_dump_trb(&command->trb);
|
|---|
| [665bf3c] | 487 | }
|
|---|
| 488 | }
|
|---|
| [c362127] | 489 |
|
|---|
| [548c123] | 490 | switch (TRB_TYPE(command->trb)) {
|
|---|
| [c362127] | 491 | case XHCI_TRB_TYPE_NO_OP_CMD:
|
|---|
| [9f5b613] | 492 | assert(code == XHCI_TRBC_TRB_ERROR);
|
|---|
| [110d795] | 493 | break;
|
|---|
| [c362127] | 494 | case XHCI_TRB_TYPE_ENABLE_SLOT_CMD:
|
|---|
| [110d795] | 495 | break;
|
|---|
| [5ac5eb1] | 496 | case XHCI_TRB_TYPE_DISABLE_SLOT_CMD:
|
|---|
| [110d795] | 497 | break;
|
|---|
| [8db42f7] | 498 | case XHCI_TRB_TYPE_ADDRESS_DEVICE_CMD:
|
|---|
| [110d795] | 499 | break;
|
|---|
| [665bf3c] | 500 | case XHCI_TRB_TYPE_CONFIGURE_ENDPOINT_CMD:
|
|---|
| [110d795] | 501 | break;
|
|---|
| [c9ce62ae] | 502 | case XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD:
|
|---|
| [110d795] | 503 | break;
|
|---|
| [05aeee0e] | 504 | case XHCI_TRB_TYPE_RESET_ENDPOINT_CMD:
|
|---|
| [110d795] | 505 | break;
|
|---|
| [05aeee0e] | 506 | case XHCI_TRB_TYPE_STOP_ENDPOINT_CMD:
|
|---|
| 507 | // Note: If the endpoint was in the middle of a transfer, then the xHC
|
|---|
| 508 | // will add a Transfer TRB before the Event TRB, research that and
|
|---|
| 509 | // handle it appropriately!
|
|---|
| [110d795] | 510 | break;
|
|---|
| [c058a388] | 511 | case XHCI_TRB_TYPE_RESET_DEVICE_CMD:
|
|---|
| [110d795] | 512 | break;
|
|---|
| [c362127] | 513 | default:
|
|---|
| [548c123] | 514 | usb_log_debug2("Unsupported command trb: %s", xhci_trb_str_type(TRB_TYPE(command->trb)));
|
|---|
| [110d795] | 515 |
|
|---|
| 516 | command->completed = true;
|
|---|
| [665bf3c] | 517 | return ENAK;
|
|---|
| [f711f06] | 518 | }
|
|---|
| [110d795] | 519 |
|
|---|
| [4688350b] | 520 | fibril_mutex_lock(&command->completed_mtx);
|
|---|
| [110d795] | 521 | command->completed = true;
|
|---|
| [4688350b] | 522 | fibril_condvar_broadcast(&command->completed_cv);
|
|---|
| 523 | fibril_mutex_unlock(&command->completed_mtx);
|
|---|
| 524 |
|
|---|
| [110d795] | 525 | return EOK;
|
|---|
| [f9e7fe8] | 526 | }
|
|---|
| [c9c0e41] | 527 |
|
|---|
| 528 |
|
|---|
| 529 | /**
|
|---|
| 530 | * @}
|
|---|
| 531 | */
|
|---|