summaryrefslogtreecommitdiff
path: root/zephyr/include/emul/tcpc/emul_tcpci_partner_src.h
blob: c919db24c7db27fcc85bf529290128512cb9ba9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/**
 * @file
 *
 * @brief Backend API for USB-C source device emulator
 */

#ifndef __EMUL_TCPCI_PARTNER_SRC_H
#define __EMUL_TCPCI_PARTNER_SRC_H

#include <zephyr/drivers/emul.h>
#include "emul/tcpc/emul_tcpci_partner_common.h"
#include "emul/tcpc/emul_tcpci.h"
#include "usb_pd.h"

/**
 * @brief USB-C source device emulator backend API
 * @defgroup tcpci_src_emul USB-C source device emulator
 * @{
 *
 * USB-C source device emulator can be attached to TCPCI emulator. It is able to
 * respond to some TCPM messages. It always attach as source and present
 * source capabilities constructed from given PDOs.
 */

/** Structure describing source device emulator data */
struct tcpci_src_emul_data {
	/** Power data objects returned in source capabilities message */
	uint32_t pdo[PDO_MAX_OBJECTS];
	/** Pointer to common TCPCI partner data */
	struct tcpci_partner_data *common_data;
	/** Delayed work which is executed on SourceCapability timeout */
	struct k_work_delayable source_capability_timeout;
};

/** Structure describing standalone source device emulator */
struct tcpci_src_emul {
	/** Common TCPCI partner data */
	struct tcpci_partner_data common_data;
	/** Operations used by TCPCI emulator */
	struct tcpci_emul_partner_ops ops;
	/** Source emulator data */
	struct tcpci_src_emul_data data;
};

/** Return values of @ref tcpci_src_emul_check_pdos function */
enum check_pdos_res {
	TCPCI_SRC_EMUL_CHECK_PDO_OK = 0,
	TCPCI_SRC_EMUL_FIRST_PDO_NO_FIXED_5V,
	TCPCI_SRC_EMUL_FIXED_VOLT_REPEATED,
	TCPCI_SRC_EMUL_FIXED_VOLT_NOT_IN_ORDER,
	TCPCI_SRC_EMUL_NON_FIRST_PDO_FIXED_FLAGS,
	TCPCI_SRC_EMUL_BATT_VOLT_REPEATED,
	TCPCI_SRC_EMUL_BATT_VOLT_NOT_IN_ORDER,
	TCPCI_SRC_EMUL_VAR_VOLT_REPEATED,
	TCPCI_SRC_EMUL_VAR_VOLT_NOT_IN_ORDER,
	TCPCI_SRC_EMUL_PDO_AFTER_ZERO,
};

/**
 * @brief Initialise USB-C source device emulator. Need to be called before
 *        any other function that use @p emul or it's components.
 *
 * @param emul Pointer to USB-C source device emulator
 * @param rev The USB-PD revision this port partner supports
 */
void tcpci_src_emul_init(struct tcpci_src_emul *emul, enum pd_rev_type rev);

/**
 * @brief Initialise USB-C source device data structure. Single PDO 5V@3A is
 *        created with fixed unconstrained flag.
 *
 * @param data Pointer to USB-C source device emulator data
 * @param common_data Pointer to USB-C device emulator common data
 */
void tcpci_src_emul_init_data(struct tcpci_src_emul_data *data,
			      struct tcpci_partner_data *common_data);

/**
 * @brief Connect emulated device to TCPCI
 *
 * @param data Pointer to USB-C source device emulator data
 * @param common_data Pointer to USB-C device emulator common data
 * @param ops Pointer to USB-C device emulato operations
 * @param tcpci_emul Poinetr to TCPCI emulator to connect
 *
 * @return 0 on success
 * @return negative on TCPCI connect error or send source capabilities error
 */
int tcpci_src_emul_connect_to_tcpci(struct tcpci_src_emul_data *data,
				    struct tcpci_partner_data *common_data,
				    const struct tcpci_emul_partner_ops *ops,
				    const struct emul *tcpci_emul);

/**
 * @brief Check if PDOs of given source device emulator are in correct order
 *
 * @param data Pointer to USB-C source device emulator
 *
 * @return TCPCI_SRC_EMUL_CHECK_PDO_OK if PDOs are correct
 * @return TCPCI_SRC_EMUL_FIRST_PDO_NO_FIXED_5V if first PDO is not
 *         fixed type 5V
 * @return TCPCI_SRC_EMUL_FIXED_VOLT_REPEATED if two or more fixed type PDOs
 *         have the same voltage
 * @return TCPCI_SRC_EMUL_FIXED_VOLT_NOT_IN_ORDER if fixed PDO with higher
 *         voltage is before the one with lower voltage
 * @return TCPCI_SRC_EMUL_NON_FIRST_PDO_FIXED_FLAGS if PDO different than first
 *         has some flags set
 * @return TCPCI_SRC_EMUL_BATT_VOLT_REPEATED if two or more battery type PDOs
 *         have the same min and max voltage
 * @return TCPCI_SRC_EMUL_BATT_VOLT_NOT_IN_ORDER if battery PDO with higher
 *         voltage is before the one with lower voltage
 * @return TCPCI_SRC_EMUL_VAR_VOLT_REPEATED if two or more variable type PDOs
 *         have the same min and max voltage
 * @return TCPCI_SRC_EMUL_VAR_VOLT_NOT_IN_ORDER if variable PDO with higher
 *         voltage is before the one with lower voltage
 * @return TCPCI_SRC_EMUL_PDO_AFTER_ZERO if PDOs of different types are not in
 *         correct order (fixed, battery, variable) or non-zero PDO is placed
 *         after zero PDO
 */
enum check_pdos_res tcpci_src_emul_check_pdos(struct tcpci_src_emul_data *data);

/**
 * @brief Send capability message constructed from source device emulator PDOs
 *
 * @param data Pointer to USB-C source device emulator data
 * @param common_data Pointer to common TCPCI partner data
 * @param delay Optional delay
 *
 * @return TCPCI_EMUL_TX_SUCCESS on success
 * @return TCPCI_EMUL_TX_FAILED when TCPCI is configured to not handle
 *                              messages of this type
 * @return -ENOMEM when there is no free memory for message
 * @return -EINVAL on TCPCI emulator add RX message error
 */
int tcpci_src_emul_send_capability_msg(struct tcpci_src_emul_data *data,
				       struct tcpci_partner_data *common_data,
				       uint64_t delay);

/**
 * @brief Send capability message constructed from source device emulator PDOs.
 *        SourceCapability timer is started when message wasn't send
 *        successfully. Emulator will try to send source capability message
 *        again on timeout. Otherwise SenderResponse timer is started and
 *        emulator will wait for Request message.
 *
 * @param data Pointer to USB-C source device emulator data
 * @param common_data Pointer to common TCPCI partner data
 * @param delay Optional delay
 *
 * @return TCPCI_EMUL_TX_SUCCESS on success
 * @return -ENOMEM when there is no free memory for message
 * @return -EINVAL on TCPCI emulator add RX message error
 */
int tcpci_src_emul_send_capability_msg_with_timer(
	struct tcpci_src_emul_data *data,
	struct tcpci_partner_data *common_data,
	uint64_t delay);

/**
 * @brief Handle SOP messages as TCPCI source device. It handles request,
 *        get source cap and soft reset messages.
 *
 * @param data Pointer to USB-C source device emulator data
 * @param common_data Pointer to common TCPCI partner data
 * @param msg Pointer to received message
 *
 * @param TCPCI_PARTNER_COMMON_MSG_HANDLED Message was handled
 * @param TCPCI_PARTNER_COMMON_MSG_NOT_HANDLED Message wasn't handled
 */
enum tcpci_partner_handler_res tcpci_src_emul_handle_sop_msg(
	struct tcpci_src_emul_data *data,
	struct tcpci_partner_data *common_data,
	const struct tcpci_emul_msg *msg);

/**
 * @brief Perform action required by source device on hard reset. Schedule
 *        source capabilities message in 15 ms after hard reset.
 *
 * @param data Pointer to USB-C source device emulator data
 */
void tcpci_src_emul_hard_reset(void *data);

/**
 * @brief Disable source capabilities timer on disconnect
 *
 * @param data Pointer to USB-C source device emulator data
 */
void tcpci_src_emul_disconnect(struct tcpci_src_emul_data *data);

/**
 * @}
 */

#endif /* __EMUL_TCPCI_PARTNER_SRC_H */