source: mainline/uspace/drv/bus/usb/xhci/commands.h@ 6d91888

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6d91888 was b80c1ab, checked in by Aearsis <Hlavaty.Ondrej@…>, 8 years ago

xhci: use dma_buffers instead of malloc32 util

A bit of refactoring was needed to adapt scratchpad buffers.

  • Property mode set to 100644
File size: 4.7 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
45#define XHCI_DEFAULT_TIMEOUT 1000000
46#define XHCI_BLOCK_INDEFINITELY 0
47
48typedef struct xhci_hc xhci_hc_t;
49typedef struct xhci_input_ctx xhci_input_ctx_t;
50typedef struct xhci_port_bandwidth_ctx xhci_port_bandwidth_ctx_t;
51
52typedef enum xhci_cmd_type {
53 XHCI_CMD_ENABLE_SLOT,
54 XHCI_CMD_DISABLE_SLOT,
55 XHCI_CMD_ADDRESS_DEVICE,
56 XHCI_CMD_CONFIGURE_ENDPOINT,
57 XHCI_CMD_EVALUATE_CONTEXT,
58 XHCI_CMD_RESET_ENDPOINT,
59 XHCI_CMD_STOP_ENDPOINT,
60 XHCI_CMD_SET_TR_DEQUEUE_POINTER,
61 XHCI_CMD_RESET_DEVICE,
62 XHCI_CMD_FORCE_EVENT,
63 XHCI_CMD_NEGOTIATE_BANDWIDTH,
64 XHCI_CMD_SET_LATENCY_TOLERANCE_VALUE,
65 XHCI_CMD_GET_PORT_BANDWIDTH,
66 XHCI_CMD_FORCE_HEADER,
67 XHCI_CMD_NO_OP,
68} xhci_cmd_type_t;
69
70typedef struct xhci_command {
71 /** Internal fields used for bookkeeping. Need not worry about these. */
72 struct {
73 link_t link;
74
75 xhci_cmd_type_t cmd;
76 suseconds_t timeout;
77
78 xhci_trb_t trb;
79 uintptr_t trb_phys;
80
81 bool async;
82 bool completed;
83
84 /* Will broadcast after command completes. */
85 fibril_mutex_t completed_mtx;
86 fibril_condvar_t completed_cv;
87 } _header;
88
89 /** Below are arguments of all commands mixed together.
90 * Be sure to know which command accepts what arguments. */
91
92 uint32_t slot_id;
93 uint32_t endpoint_id;
94 uint16_t stream_id;
95
96 dma_buffer_t input_ctx, 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 */
107int xhci_init_commands(xhci_hc_t *);
108void xhci_fini_commands(xhci_hc_t *);
109
110void xhci_stop_command_ring(xhci_hc_t *);
111void xhci_abort_command_ring(xhci_hc_t *);
112void xhci_start_command_ring(xhci_hc_t *);
113
114int xhci_handle_command_completion(xhci_hc_t *, xhci_trb_t *);
115
116/* Command lifecycle */
117void xhci_cmd_init(xhci_cmd_t *, xhci_cmd_type_t);
118void xhci_cmd_fini(xhci_cmd_t *);
119
120/* Issuing commands */
121int xhci_cmd_sync(xhci_hc_t *, xhci_cmd_t *);
122int xhci_cmd_sync_fini(xhci_hc_t *, xhci_cmd_t *);
123int xhci_cmd_async_fini(xhci_hc_t *, xhci_cmd_t *);
124
125static 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 */
Note: See TracBrowser for help on using the repository browser.