[c9c0e41] | 1 | /*
|
---|
[e0a5d4c] | 2 | * Copyright (c) 2018 Jaroslav Jindrak, Petr Manek, Ondrej Hlavaty, Jan Hrach
|
---|
[c9c0e41] | 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 | * Utility functions used to place TRBs onto the command ring.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef XHCI_COMMANDS_H
|
---|
| 37 | #define XHCI_COMMANDS_H
|
---|
| 38 |
|
---|
[110d795] | 39 | #include <adt/list.h>
|
---|
| 40 | #include <stdbool.h>
|
---|
[4688350b] | 41 | #include <fibril_synch.h>
|
---|
[3b60ea0] | 42 | #include <usb/dma_buffer.h>
|
---|
[548c123] | 43 | #include "hw_struct/trb.h"
|
---|
[889146e] | 44 | #include "trb_ring.h"
|
---|
[110d795] | 45 |
|
---|
[7d957b2] | 46 | #define XHCI_COMMAND_TIMEOUT 10000000
|
---|
| 47 | #define XHCI_CR_ABORT_TIMEOUT 5000000
|
---|
[913007f] | 48 |
|
---|
[c9c0e41] | 49 | typedef struct xhci_hc xhci_hc_t;
|
---|
[8db42f7] | 50 | typedef struct xhci_input_ctx xhci_input_ctx_t;
|
---|
[60af4cdb] | 51 | typedef struct xhci_port_bandwidth_ctx xhci_port_bandwidth_ctx_t;
|
---|
[c9c0e41] | 52 |
|
---|
[c3d926f3] | 53 | typedef enum xhci_cmd_type {
|
---|
| 54 | XHCI_CMD_ENABLE_SLOT,
|
---|
| 55 | XHCI_CMD_DISABLE_SLOT,
|
---|
| 56 | XHCI_CMD_ADDRESS_DEVICE,
|
---|
| 57 | XHCI_CMD_CONFIGURE_ENDPOINT,
|
---|
| 58 | XHCI_CMD_EVALUATE_CONTEXT,
|
---|
| 59 | XHCI_CMD_RESET_ENDPOINT,
|
---|
| 60 | XHCI_CMD_STOP_ENDPOINT,
|
---|
| 61 | XHCI_CMD_SET_TR_DEQUEUE_POINTER,
|
---|
| 62 | XHCI_CMD_RESET_DEVICE,
|
---|
| 63 | XHCI_CMD_FORCE_EVENT,
|
---|
| 64 | XHCI_CMD_NEGOTIATE_BANDWIDTH,
|
---|
| 65 | XHCI_CMD_SET_LATENCY_TOLERANCE_VALUE,
|
---|
| 66 | XHCI_CMD_GET_PORT_BANDWIDTH,
|
---|
| 67 | XHCI_CMD_FORCE_HEADER,
|
---|
| 68 | XHCI_CMD_NO_OP,
|
---|
| 69 | } xhci_cmd_type_t;
|
---|
| 70 |
|
---|
[889146e] | 71 | typedef enum {
|
---|
[ae3a941] | 72 | XHCI_CR_STATE_CLOSED, /**< Commands are rejected with ENAK. */
|
---|
| 73 | XHCI_CR_STATE_OPEN, /**< Commands are enqueued normally. */
|
---|
| 74 | XHCI_CR_STATE_CHANGING, /**< Commands wait until state changes. */
|
---|
| 75 | XHCI_CR_STATE_FULL, /**< Commands wait until something completes. */
|
---|
[d2c3dcd] | 76 | } xhci_cr_state_t;
|
---|
[889146e] | 77 |
|
---|
| 78 | typedef struct xhci_command_ring {
|
---|
| 79 | xhci_trb_ring_t trb_ring;
|
---|
| 80 |
|
---|
[904b1bc] | 81 | /** Guard access to this structure. */
|
---|
| 82 | fibril_mutex_t guard;
|
---|
[889146e] | 83 | list_t cmd_list;
|
---|
| 84 |
|
---|
[904b1bc] | 85 | /** Whether commands are allowed to be added. */
|
---|
| 86 | xhci_cr_state_t state;
|
---|
| 87 | /** For waiting on CR state change. */
|
---|
| 88 | fibril_condvar_t state_cv;
|
---|
[889146e] | 89 |
|
---|
[904b1bc] | 90 | /** For waiting on CR stopped event. */
|
---|
| 91 | fibril_condvar_t stopped_cv;
|
---|
[889146e] | 92 | } xhci_cmd_ring_t;
|
---|
| 93 |
|
---|
[110d795] | 94 | typedef struct xhci_command {
|
---|
[c3d926f3] | 95 | /** Internal fields used for bookkeeping. Need not worry about these. */
|
---|
| 96 | struct {
|
---|
| 97 | link_t link;
|
---|
| 98 |
|
---|
| 99 | xhci_cmd_type_t cmd;
|
---|
| 100 |
|
---|
| 101 | xhci_trb_t trb;
|
---|
| 102 | uintptr_t trb_phys;
|
---|
| 103 |
|
---|
| 104 | bool async;
|
---|
| 105 | bool completed;
|
---|
| 106 |
|
---|
| 107 | /* Will broadcast after command completes. */
|
---|
| 108 | fibril_mutex_t completed_mtx;
|
---|
| 109 | fibril_condvar_t completed_cv;
|
---|
| 110 | } _header;
|
---|
[110d795] | 111 |
|
---|
[c3d926f3] | 112 | /** Below are arguments of all commands mixed together.
|
---|
[904b1bc] | 113 | * Be sure to know which command accepts what arguments.
|
---|
| 114 | */
|
---|
[04df063] | 115 |
|
---|
[110d795] | 116 | uint32_t slot_id;
|
---|
[c3d926f3] | 117 | uint32_t endpoint_id;
|
---|
| 118 | uint16_t stream_id;
|
---|
[110d795] | 119 |
|
---|
[b80c1ab] | 120 | dma_buffer_t input_ctx, bandwidth_ctx;
|
---|
[c3d926f3] | 121 | uintptr_t dequeue_ptr;
|
---|
[4688350b] | 122 |
|
---|
[e7f21884] | 123 | bool tsp;
|
---|
[c3d926f3] | 124 | uint8_t susp;
|
---|
| 125 | uint8_t device_speed;
|
---|
| 126 | uint32_t status;
|
---|
| 127 | bool deconfigure;
|
---|
[110d795] | 128 | } xhci_cmd_t;
|
---|
| 129 |
|
---|
[c3d926f3] | 130 | /* Command handling control */
|
---|
[45457265] | 131 | extern errno_t xhci_init_commands(xhci_hc_t *);
|
---|
[3dd80f8] | 132 | extern void xhci_fini_commands(xhci_hc_t *);
|
---|
[04df063] | 133 |
|
---|
[19f0048] | 134 | extern void xhci_nuke_command_ring(xhci_hc_t *);
|
---|
[3dd80f8] | 135 | extern void xhci_stop_command_ring(xhci_hc_t *);
|
---|
| 136 | extern void xhci_abort_command_ring(xhci_hc_t *);
|
---|
| 137 | extern void xhci_start_command_ring(xhci_hc_t *);
|
---|
[3dc519f] | 138 |
|
---|
[45457265] | 139 | extern errno_t xhci_handle_command_completion(xhci_hc_t *, xhci_trb_t *);
|
---|
[f9e7fe8] | 140 |
|
---|
[c3d926f3] | 141 | /* Command lifecycle */
|
---|
[3dd80f8] | 142 | extern void xhci_cmd_init(xhci_cmd_t *, xhci_cmd_type_t);
|
---|
| 143 | extern void xhci_cmd_fini(xhci_cmd_t *);
|
---|
[c3d926f3] | 144 |
|
---|
| 145 | /* Issuing commands */
|
---|
[45457265] | 146 | extern errno_t xhci_cmd_sync(xhci_hc_t *, xhci_cmd_t *);
|
---|
| 147 | extern errno_t xhci_cmd_sync_fini(xhci_hc_t *, xhci_cmd_t *);
|
---|
| 148 | extern errno_t xhci_cmd_async_fini(xhci_hc_t *, xhci_cmd_t *);
|
---|
[c3d926f3] | 149 |
|
---|
[c9c0e41] | 150 | #endif
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * @}
|
---|
| 154 | */
|
---|