summaryrefslogtreecommitdiff
path: root/zephyr/emul/tcpc/emul_tcpci_partner_drp.c
blob: 659faad1fb8d24bcbd16081d1de528eeccbc7bfe (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* Copyright 2022 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.
 */

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(tcpci_drp_emul, CONFIG_TCPCI_EMUL_LOG_LEVEL);

#include <zephyr/sys/byteorder.h>
#include <zephyr/zephyr.h>

#include "common.h"
#include "emul/tcpc/emul_tcpci.h"
#include "emul/tcpc/emul_tcpci_partner_common.h"
#include "emul/tcpc/emul_tcpci_partner_drp.h"
#include "emul/tcpc/emul_tcpci_partner_snk.h"
#include "emul/tcpc/emul_tcpci_partner_src.h"
#include "tcpm/tcpci.h"
#include "usb_pd.h"

/** Check description in emul_tcpci_partner_drp.h */
enum tcpci_partner_handler_res tcpci_drp_emul_handle_sop_msg(
	struct tcpci_drp_emul_data *data,
	struct tcpci_src_emul_data *src_data,
	struct tcpci_snk_emul_data *snk_data,
	struct tcpci_partner_data *common_data,
	const struct tcpci_emul_partner_ops *ops,
	const struct tcpci_emul_msg *msg)
{
	uint16_t pwr_status;
	uint16_t header;

	header = sys_get_le16(msg->buf);

	if (PD_HEADER_CNT(header)) {
		/* Handle data message */
		switch (PD_HEADER_TYPE(header)) {
		case PD_DATA_REQUEST:
			if (data->sink) {
				/* As sink we shouldn't accept request */
				tcpci_partner_send_control_msg(common_data,
							       PD_CTRL_REJECT,
							       0);
				return TCPCI_PARTNER_COMMON_MSG_HANDLED;
			}
			/* As source, let source handler to handle this */
			return TCPCI_PARTNER_COMMON_MSG_NOT_HANDLED;
		case PD_DATA_SOURCE_CAP:
			if (!data->sink) {
				/* As source we shouldn't respond */
				return TCPCI_PARTNER_COMMON_MSG_HANDLED;
			}
			/* As sink, let sink handler to handle this */
			return TCPCI_PARTNER_COMMON_MSG_NOT_HANDLED;
		}
	} else {
		/* Handle control message */
		switch (PD_HEADER_TYPE(header)) {
		case PD_CTRL_PR_SWAP:
			tcpci_partner_send_control_msg(common_data,
						       PD_CTRL_ACCEPT,
						       0);
			data->in_pwr_swap = true;
			return TCPCI_PARTNER_COMMON_MSG_HANDLED;
		case PD_CTRL_PS_RDY:
			if (!data->in_pwr_swap) {
				return TCPCI_PARTNER_COMMON_MSG_NOT_HANDLED;
			}
			data->in_pwr_swap = false;

			/* Reset counters */
			common_data->msg_id = 0;
			common_data->recv_msg_id = -1;

			/* Perform power role swap */
			if (!data->sink) {
				/* Disable VBUS if emulator was source */
				tcpci_emul_get_reg(common_data->tcpci_emul,
						   TCPC_REG_POWER_STATUS,
						   &pwr_status);
				pwr_status &= ~TCPC_REG_POWER_STATUS_VBUS_PRES;
				tcpci_emul_set_reg(common_data->tcpci_emul,
						   TCPC_REG_POWER_STATUS,
						   pwr_status);
				/* Reconnect as sink */
				data->sink = true;
				common_data->power_role = PD_ROLE_SINK;
			} else {
				/* Reconnect as source */
				data->sink = false;
				common_data->power_role = PD_ROLE_SOURCE;
			}
			tcpci_partner_send_control_msg(common_data,
						       PD_CTRL_PS_RDY, 0);
			/* Reconnect to TCPCI emulator */
			tcpci_drp_emul_connect_to_tcpci(
					data, src_data, snk_data, common_data,
					ops, common_data->tcpci_emul);

			return TCPCI_PARTNER_COMMON_MSG_HANDLED;
		}
	}

	return TCPCI_PARTNER_COMMON_MSG_NOT_HANDLED;
}

/** Check description in emul_tcpci_partner_drp.h */
void tcpci_drp_emul_hard_reset(void *emul)
{
	struct tcpci_drp_emul *drp_emul = emul;

	if (drp_emul->data.sink) {
		tcpci_snk_emul_hard_reset(&drp_emul->snk_data);
	} else {
		tcpci_src_emul_hard_reset(&drp_emul->src_data);
	}
}

/**
 * @brief Function called when TCPM wants to transmit message. Accept received
 *        message and generate response.
 *
 * @param emul Pointer to TCPCI emulator
 * @param ops Pointer to partner operations structure
 * @param tx_msg Pointer to TX message buffer
 * @param type Type of message
 * @param retry Count of retries
 */
static void tcpci_drp_emul_transmit_op(const struct emul *emul,
				       const struct tcpci_emul_partner_ops *ops,
				       const struct tcpci_emul_msg *tx_msg,
				       enum tcpci_msg_type type,
				       int retry)
{
	struct tcpci_drp_emul *drp_emul =
		CONTAINER_OF(ops, struct tcpci_drp_emul, ops);
	enum tcpci_partner_handler_res processed;
	uint16_t header;
	int ret;

	ret = k_mutex_lock(&drp_emul->common_data.transmit_mutex, K_FOREVER);
	if (ret) {
		LOG_ERR("Failed to get DRP mutex");
		/* Inform TCPM that message send failed */
		tcpci_partner_common_msg_handler(&drp_emul->common_data,
						 tx_msg, type,
						 TCPCI_EMUL_TX_FAILED);
		return;
	}

	header = sys_get_le16(tx_msg->buf);

	/* Call common handler */
	processed = tcpci_partner_common_msg_handler(&drp_emul->common_data,
						     tx_msg, type,
						     TCPCI_EMUL_TX_SUCCESS);
	switch (processed) {
	case TCPCI_PARTNER_COMMON_MSG_HARD_RESET:
		k_mutex_unlock(&drp_emul->common_data.transmit_mutex);
		return;
	case TCPCI_PARTNER_COMMON_MSG_HANDLED:
		if (!drp_emul->data.sink && PD_HEADER_CNT(header) == 0 &&
		    PD_HEADER_TYPE(header) == PD_CTRL_SOFT_RESET) {
			/* As source, advertise capabilities after soft reset */
			tcpci_src_emul_send_capability_msg_with_timer(
							&drp_emul->src_data,
							&drp_emul->common_data,
							0);
		}
		/* Message handled nothing to do */
		k_mutex_unlock(&drp_emul->common_data.transmit_mutex);
		return;
	case TCPCI_PARTNER_COMMON_MSG_NOT_HANDLED:
	default:
		/* Continue */
		break;
	}

	/* Handle only SOP messages */
	if (type != TCPCI_MSG_SOP) {
		k_mutex_unlock(&drp_emul->common_data.transmit_mutex);
		return;
	}

	/* Call drp specific handler */
	processed = tcpci_drp_emul_handle_sop_msg(&drp_emul->data,
						  &drp_emul->src_data,
						  &drp_emul->snk_data,
						  &drp_emul->common_data,
						  ops, tx_msg);
	if (processed == TCPCI_PARTNER_COMMON_MSG_HANDLED) {
		k_mutex_unlock(&drp_emul->common_data.transmit_mutex);
		return;
	}

	/* Call source specific handler */
	processed = tcpci_src_emul_handle_sop_msg(&drp_emul->src_data,
						  &drp_emul->common_data,
						  tx_msg);
	if (processed == TCPCI_PARTNER_COMMON_MSG_HANDLED) {
		k_mutex_unlock(&drp_emul->common_data.transmit_mutex);
		return;
	}

	/* Call sink specific handler */
	processed = tcpci_snk_emul_handle_sop_msg(&drp_emul->snk_data,
						  &drp_emul->common_data,
						  tx_msg);
	if (processed == TCPCI_PARTNER_COMMON_MSG_HANDLED) {
		k_mutex_unlock(&drp_emul->common_data.transmit_mutex);
		return;
	}

	/* Send reject for not handled messages (PD rev 2.0) */
	tcpci_partner_send_control_msg(&drp_emul->common_data,
				       PD_CTRL_REJECT, 0);
	k_mutex_unlock(&drp_emul->common_data.transmit_mutex);
}

/**
 * @brief Function called when TCPM consumes message. Free message that is no
 *        longer needed.
 *
 * @param emul Pointer to TCPCI emulator
 * @param ops Pointer to partner operations structure
 * @param rx_msg Message that was consumed by TCPM
 */
static void tcpci_drp_emul_rx_consumed_op(
		const struct emul *emul,
		const struct tcpci_emul_partner_ops *ops,
		const struct tcpci_emul_msg *rx_msg)
{
	struct tcpci_partner_msg *msg = CONTAINER_OF(rx_msg,
						     struct tcpci_partner_msg,
						     msg);

	tcpci_partner_free_msg(msg);
}

/**
 * @brief Function called when emulator is disconnected from TCPCI
 *
 * @param emul Pointer to TCPCI emulator
 * @param ops Pointer to partner operations structure
 */
static void tcpci_drp_emul_disconnect_op(
		const struct emul *emul,
		const struct tcpci_emul_partner_ops *ops)
{
	struct tcpci_drp_emul *drp_emul =
		CONTAINER_OF(ops, struct tcpci_drp_emul, ops);

	tcpci_partner_common_disconnect(&drp_emul->common_data);
	tcpci_src_emul_disconnect(&drp_emul->src_data);
}

/** Check description in emul_tcpci_partner_drp.h */
int tcpci_drp_emul_connect_to_tcpci(struct tcpci_drp_emul_data *data,
				    struct tcpci_src_emul_data *src_data,
				    struct tcpci_snk_emul_data *snk_data,
				    struct tcpci_partner_data *common_data,
				    const struct tcpci_emul_partner_ops *ops,
				    const struct emul *tcpci_emul)
{
	if (data->sink) {
		return tcpci_snk_emul_connect_to_tcpci(snk_data, common_data,
						       ops, tcpci_emul);
	}

	return tcpci_src_emul_connect_to_tcpci(src_data, common_data,
					       ops, tcpci_emul);
}

/** Check description in emul_tcpci_partner_drp.h */
void tcpci_drp_emul_init(struct tcpci_drp_emul *emul, enum pd_rev_type rev)
{
	/* By default init as sink */
	tcpci_drp_emul_init_with_pd_role(emul, rev, PD_ROLE_SINK);
}

/** Check description in emul_tcpci_partner_drp.h */
void tcpci_drp_emul_init_with_pd_role(struct tcpci_drp_emul *emul,
				      enum pd_rev_type rev,
				      enum pd_power_role power_role)
{
	tcpci_partner_init(&emul->common_data, tcpci_drp_emul_hard_reset, emul);

	emul->common_data.power_role = power_role;
	emul->common_data.data_role =
		(power_role == PD_ROLE_SINK) ? PD_ROLE_UFP : PD_ROLE_DFP;

	emul->common_data.rev = rev;

	emul->ops.transmit = tcpci_drp_emul_transmit_op;
	emul->ops.rx_consumed = tcpci_drp_emul_rx_consumed_op;
	emul->ops.control_change = NULL;
	emul->ops.disconnect = tcpci_drp_emul_disconnect_op;

	emul->data.sink = power_role == PD_ROLE_SINK;
	emul->data.in_pwr_swap = false;
	tcpci_src_emul_init_data(&emul->src_data, &emul->common_data);
	tcpci_snk_emul_init_data(&emul->snk_data);

	/* Add dual role bit to sink and source PDOs */
	emul->src_data.pdo[0] |= PDO_FIXED_DUAL_ROLE;
	emul->snk_data.pdo[0] |= PDO_FIXED_DUAL_ROLE;
}