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/context.h"
|
---|
44 | #include "hw_struct/trb.h"
|
---|
45 |
|
---|
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)
|
---|
48 | #define TRB_SET_EP(trb, ep) (trb).control |= host2xhci(32, ((ep) & 0x5) << 16)
|
---|
49 | #define TRB_SET_STREAM(trb, st) (trb).control |= host2xhci(32, ((st) & 0xFFFF) << 16)
|
---|
50 | #define TRB_SET_SUSP(trb, susp) (trb).control |= host2xhci(32, ((susp) & 0x1) << 23)
|
---|
51 | #define TRB_SET_SLOT(trb, slot) (trb).control |= host2xhci(32, (slot) << 24)
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * TODO: Not sure about SCT and DCS (see section 6.4.3.9).
|
---|
55 | */
|
---|
56 | #define TRB_SET_DEQUEUE_PTR(trb, dptr) (trb).parameter |= host2xhci(64, (dptr))
|
---|
57 | #define TRB_SET_ICTX(trb, phys) (trb).parameter |= host2xhci(64, phys_addr & (~0xF))
|
---|
58 |
|
---|
59 | #define TRB_GET_CODE(trb) XHCI_DWORD_EXTRACT((trb).status, 31, 24)
|
---|
60 | #define TRB_GET_SLOT(trb) XHCI_DWORD_EXTRACT((trb).control, 31, 24)
|
---|
61 | #define TRB_GET_PHYS(trb) (XHCI_QWORD_EXTRACT((trb).parameter, 63, 4) << 4)
|
---|
62 |
|
---|
63 | int xhci_init_commands(xhci_hc_t *hc)
|
---|
64 | {
|
---|
65 | assert(hc);
|
---|
66 |
|
---|
67 | list_initialize(&hc->commands);
|
---|
68 | return EOK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void xhci_fini_commands(xhci_hc_t *hc)
|
---|
72 | {
|
---|
73 | // Note: Untested.
|
---|
74 | assert(hc);
|
---|
75 | }
|
---|
76 |
|
---|
77 | int xhci_cmd_wait(xhci_cmd_t *cmd, suseconds_t timeout)
|
---|
78 | {
|
---|
79 | int rv = EOK;
|
---|
80 |
|
---|
81 | fibril_mutex_lock(&cmd->completed_mtx);
|
---|
82 | while (!cmd->completed) {
|
---|
83 | usb_log_debug2("Waiting for event completion: going to sleep.");
|
---|
84 | rv = fibril_condvar_wait_timeout(&cmd->completed_cv, &cmd->completed_mtx, timeout);
|
---|
85 |
|
---|
86 | usb_log_debug2("Waiting for event completion: woken: %s", str_error(rv));
|
---|
87 | if (rv == ETIMEOUT)
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | fibril_mutex_unlock(&cmd->completed_mtx);
|
---|
91 |
|
---|
92 | return rv;
|
---|
93 | }
|
---|
94 |
|
---|
95 | xhci_cmd_t *xhci_cmd_alloc(void)
|
---|
96 | {
|
---|
97 | xhci_cmd_t *cmd = malloc32(sizeof(xhci_cmd_t));
|
---|
98 | xhci_cmd_init(cmd);
|
---|
99 |
|
---|
100 | usb_log_debug2("Allocating cmd on the heap. Don't forget to deallocate it!");
|
---|
101 | return cmd;
|
---|
102 | }
|
---|
103 |
|
---|
104 | void xhci_cmd_init(xhci_cmd_t *cmd)
|
---|
105 | {
|
---|
106 | memset(cmd, 0, sizeof(*cmd));
|
---|
107 |
|
---|
108 | link_initialize(&cmd->link);
|
---|
109 |
|
---|
110 | fibril_mutex_initialize(&cmd->completed_mtx);
|
---|
111 | fibril_condvar_initialize(&cmd->completed_cv);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void xhci_cmd_fini(xhci_cmd_t *cmd)
|
---|
115 | {
|
---|
116 | list_remove(&cmd->link);
|
---|
117 | }
|
---|
118 |
|
---|
119 | void xhci_cmd_free(xhci_cmd_t *cmd)
|
---|
120 | {
|
---|
121 | xhci_cmd_fini(cmd);
|
---|
122 | free32(cmd);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static inline xhci_cmd_t *get_command(xhci_hc_t *hc, uint64_t phys)
|
---|
126 | {
|
---|
127 | link_t *cmd_link = list_first(&hc->commands);
|
---|
128 |
|
---|
129 |
|
---|
130 | usb_log_debug2("Searching TRB %lu...", phys);
|
---|
131 |
|
---|
132 | while (cmd_link != NULL) {
|
---|
133 | xhci_cmd_t *cmd = list_get_instance(cmd_link, xhci_cmd_t, link);
|
---|
134 |
|
---|
135 | if (cmd->trb_phys == phys)
|
---|
136 | break;
|
---|
137 |
|
---|
138 | cmd_link = list_next(cmd_link, &hc->commands);
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (cmd_link != NULL) {
|
---|
142 | list_remove(cmd_link);
|
---|
143 |
|
---|
144 | return list_get_instance(cmd_link, xhci_cmd_t, link);
|
---|
145 | }
|
---|
146 |
|
---|
147 | return NULL;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static inline int ring_doorbell(xhci_hc_t *hc, unsigned doorbell, unsigned target)
|
---|
151 | {
|
---|
152 | assert(hc);
|
---|
153 | uint32_t v = host2xhci(32, target & BIT_RRANGE(uint32_t, 7));
|
---|
154 | pio_write_32(&hc->db_arry[doorbell], v);
|
---|
155 | return EOK;
|
---|
156 | }
|
---|
157 |
|
---|
158 | static inline int enqueue_command(xhci_hc_t *hc, xhci_cmd_t *cmd, unsigned doorbell, unsigned target)
|
---|
159 | {
|
---|
160 | assert(hc);
|
---|
161 | assert(cmd);
|
---|
162 |
|
---|
163 | list_append(&cmd->link, &hc->commands);
|
---|
164 |
|
---|
165 | xhci_trb_ring_enqueue(&hc->command_ring, &cmd->trb, &cmd->trb_phys);
|
---|
166 | ring_doorbell(hc, doorbell, target);
|
---|
167 |
|
---|
168 | usb_log_debug2("HC(%p): Sent command:", hc);
|
---|
169 | xhci_dump_trb(&cmd->trb);
|
---|
170 |
|
---|
171 | return EOK;
|
---|
172 | }
|
---|
173 |
|
---|
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);
|
---|
201 | ring_doorbell(hc, 0, 0);
|
---|
202 | }
|
---|
203 |
|
---|
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 |
|
---|
255 | int xhci_send_no_op_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
|
---|
256 | {
|
---|
257 | assert(hc);
|
---|
258 |
|
---|
259 | xhci_trb_clean(&cmd->trb);
|
---|
260 |
|
---|
261 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_NO_OP_CMD);
|
---|
262 |
|
---|
263 | return enqueue_command(hc, cmd, 0, 0);
|
---|
264 | }
|
---|
265 |
|
---|
266 | int xhci_send_enable_slot_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
|
---|
267 | {
|
---|
268 | assert(hc);
|
---|
269 |
|
---|
270 | xhci_trb_clean(&cmd->trb);
|
---|
271 |
|
---|
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);
|
---|
274 |
|
---|
275 | return enqueue_command(hc, cmd, 0, 0);
|
---|
276 | }
|
---|
277 |
|
---|
278 | int xhci_send_disable_slot_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
|
---|
279 | {
|
---|
280 | assert(hc);
|
---|
281 | assert(cmd);
|
---|
282 |
|
---|
283 | xhci_trb_clean(&cmd->trb);
|
---|
284 |
|
---|
285 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_DISABLE_SLOT_CMD);
|
---|
286 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
---|
287 |
|
---|
288 | return enqueue_command(hc, cmd, 0, 0);
|
---|
289 | }
|
---|
290 |
|
---|
291 | int xhci_send_address_device_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
|
---|
292 | {
|
---|
293 | assert(hc);
|
---|
294 | assert(cmd);
|
---|
295 | assert(ictx);
|
---|
296 |
|
---|
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 | */
|
---|
303 |
|
---|
304 | xhci_trb_clean(&cmd->trb);
|
---|
305 |
|
---|
306 | uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
|
---|
307 | TRB_SET_ICTX(cmd->trb, phys_addr);
|
---|
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 | */
|
---|
316 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_ADDRESS_DEVICE_CMD);
|
---|
317 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
---|
318 |
|
---|
319 | return enqueue_command(hc, cmd, 0, 0);
|
---|
320 | }
|
---|
321 |
|
---|
322 | int xhci_send_configure_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
|
---|
323 | {
|
---|
324 | assert(hc);
|
---|
325 | assert(cmd);
|
---|
326 | assert(ictx);
|
---|
327 |
|
---|
328 | xhci_trb_clean(&cmd->trb);
|
---|
329 |
|
---|
330 | uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
|
---|
331 | TRB_SET_ICTX(cmd->trb, phys_addr);
|
---|
332 |
|
---|
333 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_CONFIGURE_ENDPOINT_CMD);
|
---|
334 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
---|
335 |
|
---|
336 | return enqueue_command(hc, cmd, 0, 0);
|
---|
337 | }
|
---|
338 |
|
---|
339 | int xhci_send_evaluate_context_command(xhci_hc_t *hc, xhci_cmd_t *cmd, xhci_input_ctx_t *ictx)
|
---|
340 | {
|
---|
341 | assert(hc);
|
---|
342 | assert(cmd);
|
---|
343 | assert(ictx);
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Note: All Drop Context flags of the input context shall be 0,
|
---|
347 | * all Add Context flags shall be initialize to indicate IDs
|
---|
348 | * of the contexts affected by the command.
|
---|
349 | * Refer to sections 6.2.2.3 and 6.3.3.3 for further info.
|
---|
350 | */
|
---|
351 | xhci_trb_clean(&cmd->trb);
|
---|
352 |
|
---|
353 | uint64_t phys_addr = (uint64_t) addr_to_phys(ictx);
|
---|
354 | TRB_SET_ICTX(cmd->trb, phys_addr);
|
---|
355 |
|
---|
356 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD);
|
---|
357 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
---|
358 |
|
---|
359 | return enqueue_command(hc, cmd, 0, 0);
|
---|
360 | }
|
---|
361 |
|
---|
362 | int xhci_send_reset_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, uint32_t ep_id, uint8_t tcs)
|
---|
363 | {
|
---|
364 | assert(hc);
|
---|
365 | assert(cmd);
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Note: TCS can have values 0 or 1. If it is set to 0, see sectuon 4.5.8 for
|
---|
369 | * information about this flag.
|
---|
370 | */
|
---|
371 | xhci_trb_clean(&cmd->trb);
|
---|
372 |
|
---|
373 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_RESET_ENDPOINT_CMD);
|
---|
374 | TRB_SET_TCS(cmd->trb, tcs);
|
---|
375 | TRB_SET_EP(cmd->trb, ep_id);
|
---|
376 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
---|
377 |
|
---|
378 | return enqueue_command(hc, cmd, 0, 0);
|
---|
379 | }
|
---|
380 |
|
---|
381 | int xhci_send_stop_endpoint_command(xhci_hc_t *hc, xhci_cmd_t *cmd, uint32_t ep_id, uint8_t susp)
|
---|
382 | {
|
---|
383 | assert(hc);
|
---|
384 | assert(cmd);
|
---|
385 |
|
---|
386 | xhci_trb_clean(&cmd->trb);
|
---|
387 |
|
---|
388 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_STOP_ENDPOINT_CMD);
|
---|
389 | TRB_SET_EP(cmd->trb, ep_id);
|
---|
390 | TRB_SET_SUSP(cmd->trb, susp);
|
---|
391 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
---|
392 |
|
---|
393 | return enqueue_command(hc, cmd, 0, 0);
|
---|
394 | }
|
---|
395 |
|
---|
396 | int xhci_send_set_dequeue_ptr_command(xhci_hc_t *hc, xhci_cmd_t *cmd,
|
---|
397 | uintptr_t dequeue_ptr, uint16_t stream_id,
|
---|
398 | uint32_t ep_id)
|
---|
399 | {
|
---|
400 | assert(hc);
|
---|
401 | assert(cmd);
|
---|
402 |
|
---|
403 | xhci_trb_clean(&cmd->trb);
|
---|
404 |
|
---|
405 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_SET_TR_DEQUEUE_POINTER_CMD);
|
---|
406 | TRB_SET_EP(cmd->trb, ep_id);
|
---|
407 | TRB_SET_STREAM(cmd->trb, stream_id);
|
---|
408 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
---|
409 | TRB_SET_DEQUEUE_PTR(cmd->trb, dequeue_ptr);
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * TODO: Set DCS (see section 4.6.10).
|
---|
413 | */
|
---|
414 |
|
---|
415 | return enqueue_command(hc, cmd, 0, 0);
|
---|
416 | }
|
---|
417 |
|
---|
418 | int xhci_send_reset_device_command(xhci_hc_t *hc, xhci_cmd_t *cmd)
|
---|
419 | {
|
---|
420 | assert(hc);
|
---|
421 | assert(cmd);
|
---|
422 |
|
---|
423 | xhci_trb_clean(&cmd->trb);
|
---|
424 |
|
---|
425 | TRB_SET_TYPE(cmd->trb, XHCI_TRB_TYPE_RESET_DEVICE_CMD);
|
---|
426 | TRB_SET_SLOT(cmd->trb, cmd->slot_id);
|
---|
427 |
|
---|
428 | return enqueue_command(hc, cmd, 0, 0);
|
---|
429 | }
|
---|
430 |
|
---|
431 | int xhci_handle_command_completion(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
432 | {
|
---|
433 | // TODO: Update dequeue ptrs.
|
---|
434 | assert(hc);
|
---|
435 | assert(trb);
|
---|
436 |
|
---|
437 | usb_log_debug("HC(%p) Command completed.", hc);
|
---|
438 |
|
---|
439 | int code;
|
---|
440 | uint64_t phys;
|
---|
441 | xhci_cmd_t *command;
|
---|
442 |
|
---|
443 | code = TRB_GET_CODE(*trb);
|
---|
444 | phys = TRB_GET_PHYS(*trb);;
|
---|
445 | command = get_command(hc, phys);
|
---|
446 | if (command == NULL) {
|
---|
447 | // TODO: STOP & ABORT may not have command structs in the list!
|
---|
448 | usb_log_debug("No command struct for this completion event found.");
|
---|
449 |
|
---|
450 | if (code != XHCI_TRBC_SUCCESS)
|
---|
451 | report_error(code);
|
---|
452 |
|
---|
453 | return EOK;
|
---|
454 | }
|
---|
455 |
|
---|
456 | command->status = code;
|
---|
457 | command->slot_id = TRB_GET_SLOT(*trb);
|
---|
458 |
|
---|
459 | usb_log_debug2("Completed command trb: %s", xhci_trb_str_type(TRB_TYPE(command->trb)));
|
---|
460 | if (TRB_TYPE(command->trb) != XHCI_TRB_TYPE_NO_OP_CMD) {
|
---|
461 | if (code != XHCI_TRBC_SUCCESS) {
|
---|
462 | report_error(code);
|
---|
463 | xhci_dump_trb(&command->trb);
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | switch (TRB_TYPE(command->trb)) {
|
---|
468 | case XHCI_TRB_TYPE_NO_OP_CMD:
|
---|
469 | assert(code == XHCI_TRBC_TRB_ERROR);
|
---|
470 | break;
|
---|
471 | case XHCI_TRB_TYPE_ENABLE_SLOT_CMD:
|
---|
472 | break;
|
---|
473 | case XHCI_TRB_TYPE_DISABLE_SLOT_CMD:
|
---|
474 | break;
|
---|
475 | case XHCI_TRB_TYPE_ADDRESS_DEVICE_CMD:
|
---|
476 | break;
|
---|
477 | case XHCI_TRB_TYPE_CONFIGURE_ENDPOINT_CMD:
|
---|
478 | break;
|
---|
479 | case XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD:
|
---|
480 | break;
|
---|
481 | case XHCI_TRB_TYPE_RESET_ENDPOINT_CMD:
|
---|
482 | break;
|
---|
483 | case XHCI_TRB_TYPE_STOP_ENDPOINT_CMD:
|
---|
484 | // Note: If the endpoint was in the middle of a transfer, then the xHC
|
---|
485 | // will add a Transfer TRB before the Event TRB, research that and
|
---|
486 | // handle it appropriately!
|
---|
487 | break;
|
---|
488 | case XHCI_TRB_TYPE_RESET_DEVICE_CMD:
|
---|
489 | break;
|
---|
490 | default:
|
---|
491 | usb_log_debug2("Unsupported command trb: %s", xhci_trb_str_type(TRB_TYPE(command->trb)));
|
---|
492 |
|
---|
493 | command->completed = true;
|
---|
494 | return ENAK;
|
---|
495 | }
|
---|
496 |
|
---|
497 | fibril_mutex_lock(&command->completed_mtx);
|
---|
498 | command->completed = true;
|
---|
499 | fibril_condvar_broadcast(&command->completed_cv);
|
---|
500 | fibril_mutex_unlock(&command->completed_mtx);
|
---|
501 |
|
---|
502 | return EOK;
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * @}
|
---|
508 | */
|
---|