summaryrefslogtreecommitdiff
path: root/common/usbc/usb_pd_host.c
blob: 06da8a711c10138cccea579762d8b8942ed5d8be (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
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Host commands for TCPMv2 USB PD module
 */

#include "console.h"
#include "ec_commands.h"
#include "host_command.h"
#include "usb_mux.h"
#include "usb_pd.h"
#include "usb_pd_dpm_sm.h"
#include "usb_pd_tcpm.h"
#include "util.h"

#include <string.h>

#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ##args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ##args)

/* Retrieve all discovery results for the given port and transmit type */
static enum ec_status hc_typec_discovery(struct host_cmd_handler_args *args)
{
	const struct ec_params_typec_discovery *p = args->params;
	struct ec_response_typec_discovery *r = args->response;
	const struct pd_discovery *disc;
	enum tcpci_msg_type type;

	/* Confirm the number of HC VDOs matches our stored VDOs */
	BUILD_ASSERT(sizeof(r->discovery_vdo) == sizeof(union disc_ident_ack));

	if (p->port >= board_get_usb_pd_port_count())
		return EC_RES_INVALID_PARAM;

	if (p->partner_type > TYPEC_PARTNER_SOP_PRIME)
		return EC_RES_INVALID_PARAM;

	type = p->partner_type == TYPEC_PARTNER_SOP ? TCPCI_MSG_SOP :
						      TCPCI_MSG_SOP_PRIME;

	/*
	 * Clear out access mask so we can track if tasks have touched data
	 * since read started.
	 */
	pd_discovery_access_clear(p->port, type);

	disc = pd_get_am_discovery_and_notify_access(p->port, type);

	/* Initialize return size to that of discovery with no SVIDs */
	args->response_size = sizeof(*r);

	if (pd_get_identity_discovery(p->port, type) == PD_DISC_COMPLETE) {
		r->identity_count = disc->identity_cnt;
		memcpy(r->discovery_vdo,
		       pd_get_identity_response(p->port, type)->raw_value,
		       sizeof(r->discovery_vdo));
	} else {
		r->identity_count = 0;
		return EC_RES_SUCCESS;
	}

	if (pd_get_modes_discovery(p->port, type) == PD_DISC_COMPLETE) {
		int svid_i;
		int max_resp_svids =
			(args->response_max - args->response_size) /
			sizeof(struct svid_mode_info);

		if (disc->svid_cnt > max_resp_svids) {
			CPRINTS("Warn: SVIDS exceeded HC response");
			r->svid_count = max_resp_svids;
		} else {
			r->svid_count = disc->svid_cnt;
		}

		for (svid_i = 0; svid_i < r->svid_count; svid_i++) {
			r->svids[svid_i].svid = disc->svids[svid_i].svid;
			r->svids[svid_i].mode_count =
				disc->svids[svid_i].mode_cnt;
			memcpy(r->svids[svid_i].mode_vdo,
			       disc->svids[svid_i].mode_vdo,
			       sizeof(r->svids[svid_i].mode_vdo));
			args->response_size += sizeof(struct svid_mode_info);
		}
	} else {
		r->svid_count = 0;
	}

	/*
	 * Verify that another task did not access this data during the duration
	 * of the copy.  If the data was accessed, return BUSY so the AP will
	 * try retrieving again and get the updated data.
	 */
	if (!pd_discovery_access_validate(p->port, type)) {
		CPRINTS("[C%d] %s returns EC_RES_BUSY!!\n", p->port, __func__);
		return EC_RES_BUSY;
	}

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_TYPEC_DISCOVERY, hc_typec_discovery,
		     EC_VER_MASK(0));

/* Default to feature unavailable, with boards supporting it overriding */
__overridable enum ec_status
board_set_tbt_ufp_reply(int port, enum typec_tbt_ufp_reply reply)
{
	return EC_RES_UNAVAILABLE;
}

static enum ec_status hc_typec_control(struct host_cmd_handler_args *args)
{
	const struct ec_params_typec_control *p = args->params;
	mux_state_t mode;
	uint32_t data[VDO_MAX_SIZE];
	enum tcpci_msg_type tx_type;

	if (p->port >= board_get_usb_pd_port_count())
		return EC_RES_INVALID_PARAM;

	switch (p->command) {
	case TYPEC_CONTROL_COMMAND_EXIT_MODES:
		pd_dpm_request(p->port, DPM_REQUEST_EXIT_MODES);
		break;
	case TYPEC_CONTROL_COMMAND_CLEAR_EVENTS:
		pd_clear_events(p->port, p->clear_events_mask);
		break;
	case TYPEC_CONTROL_COMMAND_ENTER_MODE:
		return pd_request_enter_mode(p->port, p->mode_to_enter);
	case TYPEC_CONTROL_COMMAND_TBT_UFP_REPLY:
		return board_set_tbt_ufp_reply(p->port, p->tbt_ufp_reply);
	case TYPEC_CONTROL_COMMAND_USB_MUX_SET:
		/* The EC will fill in polarity, so filter flip out */
		mode = p->mux_params.mux_flags & ~USB_PD_MUX_POLARITY_INVERTED;

		if (!IS_ENABLED(CONFIG_USB_MUX_AP_CONTROL))
			return EC_RES_INVALID_PARAM;

		usb_mux_set_single(p->port, p->mux_params.mux_index, mode,
				   USB_SWITCH_CONNECT,
				   polarity_rm_dts(pd_get_polarity(p->port)));
		return EC_RES_SUCCESS;
	case TYPEC_CONTROL_COMMAND_BIST_SHARE_MODE:
		return pd_set_bist_share_mode(p->bist_share_mode);
	case TYPEC_CONTROL_COMMAND_SEND_VDM_REQ:
		if (!IS_ENABLED(CONFIG_USB_PD_VDM_AP_CONTROL))
			return EC_RES_INVALID_PARAM;

		if (p->vdm_req_params.vdm_data_objects <= 0 ||
		    p->vdm_req_params.vdm_data_objects > VDO_MAX_SIZE)
			return EC_RES_INVALID_PARAM;

		memcpy(data, p->vdm_req_params.vdm_data,
		       sizeof(uint32_t) * p->vdm_req_params.vdm_data_objects);

		switch (p->vdm_req_params.partner_type) {
		case TYPEC_PARTNER_SOP:
			tx_type = TCPCI_MSG_SOP;
			break;
		case TYPEC_PARTNER_SOP_PRIME:
			tx_type = TCPCI_MSG_SOP_PRIME;
			break;
		case TYPEC_PARTNER_SOP_PRIME_PRIME:
			tx_type = TCPCI_MSG_SOP_PRIME_PRIME;
			break;
		default:
			return EC_RES_INVALID_PARAM;
		}

		return pd_request_vdm(p->port, data,
				      p->vdm_req_params.vdm_data_objects,
				      tx_type);
	default:
		return EC_RES_INVALID_PARAM;
	}

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_TYPEC_CONTROL, hc_typec_control, EC_VER_MASK(0));

/*
 * Validate ec_response_typec_status_v0's binary compatibility with
 * ec_response_typec_status, which is being deprecated.
 */
BUILD_ASSERT(offsetof(struct ec_response_typec_status_v0,
		      typec_status.sop_prime_revision) ==
	     offsetof(struct ec_response_typec_status, sop_prime_revision));
BUILD_ASSERT(offsetof(struct ec_response_typec_status_v0, source_cap_pdos) ==
	     offsetof(struct ec_response_typec_status, source_cap_pdos));
BUILD_ASSERT(sizeof(struct ec_response_typec_status_v0) ==
	     sizeof(struct ec_response_typec_status));

/*
 * Validate ec_response_typec_status_v0's binary compatibility with
 * ec_response_typec_status_v1 with respect to typec_status.
 */
BUILD_ASSERT(offsetof(struct ec_response_typec_status_v0,
		      typec_status.pd_enabled) ==
	     offsetof(struct ec_response_typec_status_v1,
		      typec_status.pd_enabled));
BUILD_ASSERT(offsetof(struct ec_response_typec_status_v0,
		      typec_status.sop_prime_revision) ==
	     offsetof(struct ec_response_typec_status_v1,
		      typec_status.sop_prime_revision));

static enum ec_status hc_typec_status(struct host_cmd_handler_args *args)
{
	const struct ec_params_typec_status *p = args->params;
	struct ec_response_typec_status_v1 *r1 = args->response;
	struct ec_response_typec_status_v0 *r0 = args->response;
	struct cros_ec_typec_status *cs = &r1->typec_status;
	const char *tc_state_name;

	if (p->port >= board_get_usb_pd_port_count())
		return EC_RES_INVALID_PARAM;

	args->response_size = args->version == 0 ? sizeof(*r0) : sizeof(*r1);

	if (args->response_max < args->response_size)
		return EC_RES_RESPONSE_TOO_BIG;

	cs->pd_enabled = pd_comm_is_enabled(p->port);
	cs->dev_connected = pd_is_connected(p->port);
	cs->sop_connected = pd_capable(p->port);

	cs->power_role = pd_get_power_role(p->port);
	cs->data_role = pd_get_data_role(p->port);
	cs->vconn_role = pd_get_vconn_state(p->port) ? PD_ROLE_VCONN_SRC :
						       PD_ROLE_VCONN_OFF;
	cs->polarity = pd_get_polarity(p->port);
	cs->cc_state = pd_get_task_cc_state(p->port);
	cs->dp_pin = get_dp_pin_mode(p->port);
	cs->mux_state = usb_mux_get(p->port);

	tc_state_name = pd_get_task_state_name(p->port);
	strzcpy(cs->tc_state, tc_state_name, sizeof(cs->tc_state));

	cs->events = pd_get_events(p->port);

	if (pd_get_partner_rmdo(p->port).major_rev != 0) {
		cs->sop_revision =
			PD_STATUS_RMDO_REV_SET_MAJOR(
				pd_get_partner_rmdo(p->port).major_rev) |
			PD_STATUS_RMDO_REV_SET_MINOR(
				pd_get_partner_rmdo(p->port).minor_rev) |
			PD_STATUS_RMDO_VER_SET_MAJOR(
				pd_get_partner_rmdo(p->port).major_ver) |
			PD_STATUS_RMDO_VER_SET_MINOR(
				pd_get_partner_rmdo(p->port).minor_ver);
	} else if (cs->sop_connected) {
		cs->sop_revision = PD_STATUS_REV_SET_MAJOR(
			pd_get_rev(p->port, TCPCI_MSG_SOP));
	} else {
		cs->sop_revision = 0;
	}

	cs->sop_prime_revision =
		pd_get_identity_discovery(p->port, TCPCI_MSG_SOP_PRIME) ==
				PD_DISC_COMPLETE ?
			PD_STATUS_REV_SET_MAJOR(
				pd_get_rev(p->port, TCPCI_MSG_SOP_PRIME)) :
			0;

	if (args->version == 0) {
		cs->source_cap_count = MIN(pd_get_src_cap_cnt(p->port),
					   ARRAY_SIZE(r0->source_cap_pdos));
		memcpy(r0->source_cap_pdos, pd_get_src_caps(p->port),
		       cs->source_cap_count * sizeof(uint32_t));
		cs->sink_cap_count = MIN(pd_get_snk_cap_cnt(p->port),
					 ARRAY_SIZE(r0->sink_cap_pdos));
		memcpy(r0->sink_cap_pdos, pd_get_snk_caps(p->port),
		       cs->sink_cap_count * sizeof(uint32_t));
	} else {
		cs->source_cap_count = MIN(pd_get_src_cap_cnt(p->port),
					   ARRAY_SIZE(r1->source_cap_pdos));
		memcpy(r1->source_cap_pdos, pd_get_src_caps(p->port),
		       cs->source_cap_count * sizeof(uint32_t));
		cs->sink_cap_count = MIN(pd_get_snk_cap_cnt(p->port),
					 ARRAY_SIZE(r1->sink_cap_pdos));
		memcpy(r1->sink_cap_pdos, pd_get_snk_caps(p->port),
		       cs->sink_cap_count * sizeof(uint32_t));
	}

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_TYPEC_STATUS, hc_typec_status,
		     EC_VER_MASK(0) | EC_VER_MASK(1));