[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"
|
---|
| 43 | #include "hw_struct/trb.h"
|
---|
| 44 |
|
---|
[481af21e] | 45 | static inline int ring_doorbell(xhci_hc_t *hc, unsigned doorbell, unsigned target)
|
---|
[c9c0e41] | 46 | {
|
---|
| 47 | uint32_t v = host2xhci(32, target & BIT_RRANGE(uint32_t, 7));
|
---|
| 48 | pio_write_32(&hc->db_arry[doorbell], v);
|
---|
| 49 | return EOK;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[481af21e] | 52 | static inline int enqueue_trb(xhci_hc_t *hc, xhci_trb_t *trb,
|
---|
| 53 | unsigned doorbell, unsigned target)
|
---|
| 54 | {
|
---|
| 55 | xhci_trb_ring_enqueue(&hc->command_ring, trb);
|
---|
| 56 | ring_doorbell(hc, doorbell, target);
|
---|
| 57 |
|
---|
| 58 | xhci_dump_trb(trb);
|
---|
| 59 | usb_log_debug2("HC(%p): Sent TRB", hc);
|
---|
| 60 |
|
---|
| 61 | return EOK;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[c9c0e41] | 64 | int xhci_send_no_op_command(xhci_hc_t *hc)
|
---|
| 65 | {
|
---|
| 66 | xhci_trb_t trb;
|
---|
| 67 | memset(&trb, 0, sizeof(trb));
|
---|
| 68 |
|
---|
| 69 | trb.control = host2xhci(32, XHCI_TRB_TYPE_NO_OP_CMD << 10);
|
---|
| 70 |
|
---|
[481af21e] | 71 | return enqueue_trb(hc, &trb, 0, 0);
|
---|
[c9c0e41] | 72 | }
|
---|
| 73 |
|
---|
| 74 | int xhci_send_enable_slot_command(xhci_hc_t *hc)
|
---|
| 75 | {
|
---|
| 76 | xhci_trb_t trb;
|
---|
| 77 | memset(&trb, 0, sizeof(trb));
|
---|
| 78 |
|
---|
| 79 | trb.control = host2xhci(32, XHCI_TRB_TYPE_ENABLE_SLOT_CMD << 10);
|
---|
| 80 | trb.control |= host2xhci(32, XHCI_REG_RD(hc->xecp, XHCI_EC_SP_SLOT_TYPE) << 16);
|
---|
[481af21e] | 81 | trb.control |= host2xhci(32, hc->command_ring.pcs);
|
---|
[c9c0e41] | 82 |
|
---|
[5ac5eb1] | 83 | return enqueue_trb(hc, &trb, 0, 0);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | int xhci_send_disable_slot_command(xhci_hc_t *hc, uint32_t slot_id)
|
---|
| 87 | {
|
---|
| 88 | xhci_trb_t trb;
|
---|
| 89 | memset(&trb, 0, sizeof(trb));
|
---|
| 90 |
|
---|
| 91 | trb.control = host2xhci(32, XHCI_TRB_TYPE_DISABLE_SLOT_CMD << 10);
|
---|
| 92 | trb.control |= host2xhci(32, hc->command_ring.pcs);
|
---|
| 93 | trb.control |= host2xhci(32, slot_id << 24);
|
---|
[c9c0e41] | 94 |
|
---|
[481af21e] | 95 | return enqueue_trb(hc, &trb, 0, 0);
|
---|
[c9c0e41] | 96 | }
|
---|
| 97 |
|
---|
[f9e7fe8] | 98 | int xhci_handle_command_completion(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
| 99 | {
|
---|
[5ac5eb1] | 100 | usb_log_debug("HC(%p) Command completed.", hc);
|
---|
[f9e7fe8] | 101 | xhci_dump_trb(trb);
|
---|
| 102 |
|
---|
[5ac5eb1] | 103 | int code;
|
---|
| 104 | uint32_t slot_id;
|
---|
| 105 | xhci_trb_t *command;
|
---|
[f711f06] | 106 |
|
---|
[5ac5eb1] | 107 | code = XHCI_DWORD_EXTRACT(trb->status, 31, 24);
|
---|
| 108 | command = (xhci_trb_t *) XHCI_QWORD_EXTRACT(trb->parameter, 63, 4);
|
---|
| 109 | slot_id = XHCI_DWORD_EXTRACT(trb->control, 31, 24);
|
---|
| 110 | (void) slot_id;
|
---|
[c362127] | 111 |
|
---|
[5ac5eb1] | 112 | switch (TRB_TYPE(*command)) {
|
---|
[c362127] | 113 | case XHCI_TRB_TYPE_NO_OP_CMD:
|
---|
| 114 | assert(code == XHCI_TRBC_TRB_ERROR);
|
---|
| 115 | break;
|
---|
| 116 | case XHCI_TRB_TYPE_ENABLE_SLOT_CMD:
|
---|
| 117 | // TODO: Call a device addition callback once it's implemented.
|
---|
| 118 | break;
|
---|
[5ac5eb1] | 119 | case XHCI_TRB_TYPE_DISABLE_SLOT_CMD:
|
---|
| 120 | if (code == XHCI_TRBC_SLOT_NOT_ENABLED_ERROR)
|
---|
| 121 | usb_log_debug2("Slot ID to be disabled was not enabled.");
|
---|
| 122 | // TODO: Call a device removal callback that will deallocate associated
|
---|
| 123 | // data structures once it's implemented.
|
---|
| 124 | break;
|
---|
[c362127] | 125 | default:
|
---|
[5ac5eb1] | 126 | usb_log_debug2("Unsupported command trb.");
|
---|
| 127 | xhci_dump_trb(command);
|
---|
[c362127] | 128 | break;
|
---|
[f711f06] | 129 | }
|
---|
[f9e7fe8] | 130 |
|
---|
| 131 | return EOK;
|
---|
| 132 | }
|
---|
[c9c0e41] | 133 |
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * @}
|
---|
| 137 | */
|
---|