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 | * Utility functions used to place TRBs onto the command ring.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef XHCI_COMMANDS_H
|
---|
37 | #define XHCI_COMMANDS_H
|
---|
38 |
|
---|
39 | #include <adt/list.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <fibril_synch.h>
|
---|
42 | #include "hw_struct/trb.h"
|
---|
43 |
|
---|
44 | #define XHCI_DEFAULT_TIMEOUT 1000000
|
---|
45 | #define XHCI_BLOCK_INDEFINITELY 0
|
---|
46 |
|
---|
47 | typedef struct xhci_hc xhci_hc_t;
|
---|
48 | typedef struct xhci_input_ctx xhci_input_ctx_t;
|
---|
49 | typedef struct xhci_port_bandwidth_ctx xhci_port_bandwidth_ctx_t;
|
---|
50 |
|
---|
51 | typedef enum xhci_cmd_type {
|
---|
52 | XHCI_CMD_ENABLE_SLOT,
|
---|
53 | XHCI_CMD_DISABLE_SLOT,
|
---|
54 | XHCI_CMD_ADDRESS_DEVICE,
|
---|
55 | XHCI_CMD_CONFIGURE_ENDPOINT,
|
---|
56 | XHCI_CMD_EVALUATE_CONTEXT,
|
---|
57 | XHCI_CMD_RESET_ENDPOINT,
|
---|
58 | XHCI_CMD_STOP_ENDPOINT,
|
---|
59 | XHCI_CMD_SET_TR_DEQUEUE_POINTER,
|
---|
60 | XHCI_CMD_RESET_DEVICE,
|
---|
61 | XHCI_CMD_FORCE_EVENT,
|
---|
62 | XHCI_CMD_NEGOTIATE_BANDWIDTH,
|
---|
63 | XHCI_CMD_SET_LATENCY_TOLERANCE_VALUE,
|
---|
64 | XHCI_CMD_GET_PORT_BANDWIDTH,
|
---|
65 | XHCI_CMD_FORCE_HEADER,
|
---|
66 | XHCI_CMD_NO_OP,
|
---|
67 | } xhci_cmd_type_t;
|
---|
68 |
|
---|
69 | typedef struct xhci_command {
|
---|
70 | /** Internal fields used for bookkeeping. Need not worry about these. */
|
---|
71 | struct {
|
---|
72 | link_t link;
|
---|
73 |
|
---|
74 | xhci_cmd_type_t cmd;
|
---|
75 | suseconds_t timeout;
|
---|
76 |
|
---|
77 | xhci_trb_t trb;
|
---|
78 | uintptr_t trb_phys;
|
---|
79 |
|
---|
80 | bool async;
|
---|
81 | bool completed;
|
---|
82 |
|
---|
83 | /* Will broadcast after command completes. */
|
---|
84 | fibril_mutex_t completed_mtx;
|
---|
85 | fibril_condvar_t completed_cv;
|
---|
86 | } _header;
|
---|
87 |
|
---|
88 | /** Below are arguments of all commands mixed together.
|
---|
89 | * Be sure to know which command accepts what arguments. */
|
---|
90 |
|
---|
91 | uint32_t slot_id;
|
---|
92 | uint32_t endpoint_id;
|
---|
93 | uint16_t stream_id;
|
---|
94 |
|
---|
95 | xhci_input_ctx_t *input_ctx;
|
---|
96 | xhci_port_bandwidth_ctx_t *bandwidth_ctx;
|
---|
97 | uintptr_t dequeue_ptr;
|
---|
98 |
|
---|
99 | uint8_t tcs;
|
---|
100 | uint8_t susp;
|
---|
101 | uint8_t device_speed;
|
---|
102 | uint32_t status;
|
---|
103 | bool deconfigure;
|
---|
104 | } xhci_cmd_t;
|
---|
105 |
|
---|
106 | /* Command handling control */
|
---|
107 | int xhci_init_commands(xhci_hc_t *);
|
---|
108 | void xhci_fini_commands(xhci_hc_t *);
|
---|
109 |
|
---|
110 | void xhci_stop_command_ring(xhci_hc_t *);
|
---|
111 | void xhci_abort_command_ring(xhci_hc_t *);
|
---|
112 | void xhci_start_command_ring(xhci_hc_t *);
|
---|
113 |
|
---|
114 | int xhci_handle_command_completion(xhci_hc_t *, xhci_trb_t *);
|
---|
115 |
|
---|
116 | /* Command lifecycle */
|
---|
117 | void xhci_cmd_init(xhci_cmd_t *, xhci_cmd_type_t);
|
---|
118 | void xhci_cmd_fini(xhci_cmd_t *);
|
---|
119 |
|
---|
120 | /* Issuing commands */
|
---|
121 | int xhci_cmd_sync(xhci_hc_t *, xhci_cmd_t *);
|
---|
122 | int xhci_cmd_sync_fini(xhci_hc_t *, xhci_cmd_t *);
|
---|
123 | int xhci_cmd_async_fini(xhci_hc_t *, xhci_cmd_t *);
|
---|
124 |
|
---|
125 | static inline int xhci_cmd_sync_inline_wrapper(xhci_hc_t *hc, xhci_cmd_t cmd)
|
---|
126 | {
|
---|
127 | /* Poor man's xhci_cmd_init (everything else is zeroed) */
|
---|
128 | link_initialize(&cmd._header.link);
|
---|
129 | fibril_mutex_initialize(&cmd._header.completed_mtx);
|
---|
130 | fibril_condvar_initialize(&cmd._header.completed_cv);
|
---|
131 |
|
---|
132 | if (!cmd._header.timeout) {
|
---|
133 | cmd._header.timeout = XHCI_DEFAULT_TIMEOUT;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /* Issue the command */
|
---|
137 | const int err = xhci_cmd_sync(hc, &cmd);
|
---|
138 | xhci_cmd_fini(&cmd);
|
---|
139 |
|
---|
140 | return err;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** The inline macro expects:
|
---|
144 | * - hc - HC to schedule command on (xhci_hc_t *).
|
---|
145 | * - command - Member of `xhci_cmd_type_t` without the "XHCI_CMD_" prefix.
|
---|
146 | * - VA_ARGS - (optional) Command arguments in struct initialization notation.
|
---|
147 | *
|
---|
148 | * The return code and semantics matches those of `xhci_cmd_sync_fini`.
|
---|
149 | *
|
---|
150 | * Example:
|
---|
151 | * int err = xhci_cmd_sync_inline(hc, DISABLE_SLOT, .slot_id = 42);
|
---|
152 | */
|
---|
153 |
|
---|
154 | #define xhci_cmd_sync_inline(hc, command, ...) \
|
---|
155 | xhci_cmd_sync_inline_wrapper(hc, \
|
---|
156 | (xhci_cmd_t) { ._header.cmd = XHCI_CMD_##command, ##__VA_ARGS__ })
|
---|
157 |
|
---|
158 | #endif
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * @}
|
---|
164 | */
|
---|