source: mainline/uspace/drv/bus/usb/xhci/commands.h@ 61e27e80

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 61e27e80 was d2c3dcd, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

xhci commands: wait if the ring is full

  • Property mode set to 100644
File size: 5.3 KB
Line 
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 <usb/host/dma_buffer.h>
43#include "hw_struct/trb.h"
44#include "trb_ring.h"
45
46#define XHCI_COMMAND_TIMEOUT 10000000
47#define XHCI_CR_ABORT_TIMEOUT 5000000
48
49typedef struct xhci_hc xhci_hc_t;
50typedef struct xhci_input_ctx xhci_input_ctx_t;
51typedef struct xhci_port_bandwidth_ctx xhci_port_bandwidth_ctx_t;
52
53typedef 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
71typedef enum {
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. */
76} xhci_cr_state_t;
77
78typedef struct xhci_command_ring {
79 xhci_trb_ring_t trb_ring;
80
81 fibril_mutex_t guard; /**< Guard access to this structure. */
82 list_t cmd_list;
83
84 xhci_cr_state_t state; /**< Whether commands are allowed to be
85 added. */
86 fibril_condvar_t state_cv; /**< For waiting on CR state change. */
87
88 fibril_condvar_t stopped_cv; /**< For waiting on CR stopped event. */
89} xhci_cmd_ring_t;
90
91typedef struct xhci_command {
92 /** Internal fields used for bookkeeping. Need not worry about these. */
93 struct {
94 link_t link;
95
96 xhci_cmd_type_t cmd;
97
98 xhci_trb_t trb;
99 uintptr_t trb_phys;
100
101 bool async;
102 bool completed;
103
104 /* Will broadcast after command completes. */
105 fibril_mutex_t completed_mtx;
106 fibril_condvar_t completed_cv;
107 } _header;
108
109 /** Below are arguments of all commands mixed together.
110 * Be sure to know which command accepts what arguments. */
111
112 uint32_t slot_id;
113 uint32_t endpoint_id;
114 uint16_t stream_id;
115
116 dma_buffer_t input_ctx, bandwidth_ctx;
117 uintptr_t dequeue_ptr;
118
119 uint8_t tcs;
120 uint8_t susp;
121 uint8_t device_speed;
122 uint32_t status;
123 bool deconfigure;
124} xhci_cmd_t;
125
126/* Command handling control */
127int xhci_init_commands(xhci_hc_t *);
128void xhci_fini_commands(xhci_hc_t *);
129
130void xhci_stop_command_ring(xhci_hc_t *);
131void xhci_abort_command_ring(xhci_hc_t *);
132void xhci_start_command_ring(xhci_hc_t *);
133
134int xhci_handle_command_completion(xhci_hc_t *, xhci_trb_t *);
135
136/* Command lifecycle */
137void xhci_cmd_init(xhci_cmd_t *, xhci_cmd_type_t);
138void xhci_cmd_fini(xhci_cmd_t *);
139
140/* Issuing commands */
141int xhci_cmd_sync(xhci_hc_t *, xhci_cmd_t *);
142int xhci_cmd_sync_fini(xhci_hc_t *, xhci_cmd_t *);
143int xhci_cmd_async_fini(xhci_hc_t *, xhci_cmd_t *);
144
145static inline int xhci_cmd_sync_inline_wrapper(xhci_hc_t *hc, xhci_cmd_t cmd)
146{
147 /* Poor man's xhci_cmd_init (everything else is zeroed) */
148 link_initialize(&cmd._header.link);
149 fibril_mutex_initialize(&cmd._header.completed_mtx);
150 fibril_condvar_initialize(&cmd._header.completed_cv);
151
152 /* Issue the command */
153 const int err = xhci_cmd_sync(hc, &cmd);
154 xhci_cmd_fini(&cmd);
155
156 return err;
157}
158
159/** The inline macro expects:
160 * - hc - HC to schedule command on (xhci_hc_t *).
161 * - command - Member of `xhci_cmd_type_t` without the "XHCI_CMD_" prefix.
162 * - VA_ARGS - (optional) Command arguments in struct initialization notation.
163 *
164 * The return code and semantics matches those of `xhci_cmd_sync_fini`.
165 *
166 * Example:
167 * int err = xhci_cmd_sync_inline(hc, DISABLE_SLOT, .slot_id = 42);
168 */
169
170#define xhci_cmd_sync_inline(hc, command, ...) \
171 xhci_cmd_sync_inline_wrapper(hc, \
172 (xhci_cmd_t) { ._header.cmd = XHCI_CMD_##command, ##__VA_ARGS__ })
173
174#endif
175
176
177
178/**
179 * @}
180 */
Note: See TracBrowser for help on using the repository browser.