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 |
|
---|
45 | static inline int ring_doorbell(xhci_hc_t *hc, unsigned doorbell, unsigned target)
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
71 | return enqueue_trb(hc, &trb, 0, 0);
|
---|
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);
|
---|
81 | trb.control |= host2xhci(32, hc->command_ring.pcs);
|
---|
82 |
|
---|
83 | // TODO: Setup input control context.
|
---|
84 |
|
---|
85 | return enqueue_trb(hc, &trb, 0, 0);
|
---|
86 | }
|
---|
87 |
|
---|
88 | int xhci_handle_command_completion(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
89 | {
|
---|
90 | usb_log_debug2("HC(%p) Command completed.", hc);
|
---|
91 | xhci_dump_trb(trb);
|
---|
92 |
|
---|
93 | int code = XHCI_DWORD_EXTRACT(trb->status, 31, 24);
|
---|
94 | if (code != XHCI_TRBC_SUCCESS)
|
---|
95 | return EINVAL; // TODO: Find a better error code/handler.
|
---|
96 |
|
---|
97 | xhci_trb_t *command = (xhci_trb_t *) XHCI_QWORD_EXTRACT(trb->parameter, 63, 4);
|
---|
98 | switch(TRB_TYPE(*command)) {
|
---|
99 | default:
|
---|
100 | // TODO:
|
---|
101 | break;
|
---|
102 | }
|
---|
103 |
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * @}
|
---|
110 | */
|
---|