summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/console_cmd/usb_pd_console.c
blob: 21056056d412fb935365d7ec4b01408221e75c5b (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/shell/shell.h>
#include <zephyr/ztest.h>

#include "console.h"
#include "ec_commands.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"
#include "usb_prl_sm.h"

static void console_cmd_usb_pd_after(void *fixture)
{
	ARG_UNUSED(fixture);

	/* TODO (b/230059737) */
	test_set_chipset_to_g3();
	k_sleep(K_SECONDS(1));
	test_set_chipset_to_s0();
	k_sleep(K_SECONDS(10));

	/* Keep port used by testsuite enabled (default state) */
	pd_comm_enable(0, 1);
	pd_set_suspend(0, 0);
}

ZTEST_SUITE(console_cmd_usb_pd, drivers_predicate_post_main, NULL, NULL,
	    console_cmd_usb_pd_after, NULL);

ZTEST_USER(console_cmd_usb_pd, test_too_few_args)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd");
	zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d",
		      EC_ERROR_PARAM_COUNT, rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0");
	zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d",
		      EC_ERROR_PARAM_COUNT, rv);
}

ZTEST_USER(console_cmd_usb_pd, test_dump)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd dump 0");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd dump 4");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd dump -4");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd dump x");
	zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d",
		      EC_ERROR_PARAM2, rv);
}

ZTEST_USER(console_cmd_usb_pd, test_trysrc)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd trysrc 0");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd trysrc 2");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd trysrc 5");
	zassert_equal(rv, EC_ERROR_PARAM3, "Expected %d, but got %d",
		      EC_ERROR_PARAM3, rv);
}

ZTEST_USER(console_cmd_usb_pd, test_version)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd version");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_bad_port)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 5");
	zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d",
		      EC_ERROR_PARAM_COUNT, rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 5 tx");
	zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d",
		      EC_ERROR_PARAM2, rv);
}

ZTEST_USER(console_cmd_usb_pd, test_tx)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 tx");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_charger)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 charger");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_dev)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dev");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dev 20");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dev x");
	zassert_equal(rv, EC_ERROR_PARAM3, "Expected %d, but got %d",
		      EC_ERROR_PARAM3, rv);
}

ZTEST_USER(console_cmd_usb_pd, test_disable)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 disable");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_enable)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 enable");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_suspend)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 suspend");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_resume)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 resume");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_hard)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 hard");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_soft)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 soft");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_swap)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 swap");
	zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d",
		      EC_ERROR_PARAM_COUNT, rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 swap power");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 swap data");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 swap vconn");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 swap x");
	zassert_equal(rv, EC_ERROR_PARAM3, "Expected %d, but got %d",
		      EC_ERROR_PARAM3, rv);
}

ZTEST_USER(console_cmd_usb_pd, test_dualrole)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dualrole");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dualrole on");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dualrole off");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dualrole freeze");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dualrole sink");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dualrole source");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 dualrole x");
	zassert_equal(rv, EC_ERROR_PARAM4, "Expected %d, but got %d",
		      EC_ERROR_PARAM4, rv);
}

ZTEST_USER(console_cmd_usb_pd, test_state)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 state");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_srccaps)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 srccaps");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

ZTEST_USER(console_cmd_usb_pd, test_timer)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pd 0 timer");
	zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS,
		      rv);
}

static void set_device_vdo(int port, enum tcpci_msg_type type)
{
	union tbt_mode_resp_device device_resp;
	struct pd_discovery *dev_disc;

	dev_disc = pd_get_am_discovery_and_notify_access(port, type);
	dev_disc->svid_cnt = 1;
	dev_disc->svids[0].svid = USB_VID_INTEL;
	dev_disc->svids[0].discovery = PD_DISC_COMPLETE;
	dev_disc->svids[0].mode_cnt = 1;
	device_resp.tbt_alt_mode = TBT_ALTERNATE_MODE;
	device_resp.tbt_adapter = TBT_ADAPTER_TBT3;
	device_resp.intel_spec_b0 = VENDOR_SPECIFIC_NOT_SUPPORTED;
	device_resp.vendor_spec_b0 = VENDOR_SPECIFIC_NOT_SUPPORTED;
	device_resp.vendor_spec_b1 = VENDOR_SPECIFIC_NOT_SUPPORTED;
	dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value;
}

static void set_active_cable_type(int port, enum tcpci_msg_type type,
				  enum idh_ptype ptype)
{
	struct pd_discovery *dev_disc;

	dev_disc = pd_get_am_discovery_and_notify_access(port, type);
	dev_disc->identity.idh.product_type = ptype;
	prl_set_rev(port, type, PD_REV30);
}

ZTEST_USER(console_cmd_usb_pd, test_pe)
{
	int rv;

	pd_set_identity_discovery(0, TCPCI_MSG_SOP, PD_DISC_COMPLETE);

	rv = shell_execute_cmd(get_ec_shell(), "pe 0 dump");
	zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv);

	set_device_vdo(0, TCPCI_MSG_SOP);
	rv = shell_execute_cmd(get_ec_shell(), "pe 0 dump");
	zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv);

	/* Handle error scenarios */
	rv = shell_execute_cmd(get_ec_shell(), "pe 0");
	zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d",
		      EC_ERROR_PARAM_COUNT, rv);

	rv = shell_execute_cmd(get_ec_shell(), "pe x dump");
	zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d",
		      EC_ERROR_PARAM2, rv);
}

ZTEST_USER(console_cmd_usb_pd, test_pdcable)
{
	int rv;

	rv = shell_execute_cmd(get_ec_shell(), "pdcable 0");
	zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv);

	set_device_vdo(0, TCPCI_MSG_SOP_PRIME);

	/* Set active cable type IDH_PTYPE_ACABLE */
	set_active_cable_type(0, TCPCI_MSG_SOP_PRIME, IDH_PTYPE_ACABLE);
	rv = shell_execute_cmd(get_ec_shell(), "pdcable 0");
	zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv);

	/* Set active cable type IDH_PTYPE_PCABLE */
	set_active_cable_type(0, TCPCI_MSG_SOP_PRIME, IDH_PTYPE_PCABLE);
	rv = shell_execute_cmd(get_ec_shell(), "pdcable 0");
	zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv);

	/* Handle error scenarios */
	rv = shell_execute_cmd(get_ec_shell(), "pdcable");
	zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d",
		      EC_ERROR_PARAM_COUNT, rv);

	rv = shell_execute_cmd(get_ec_shell(), "pdcable t");
	zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d",
		      EC_ERROR_PARAM2, rv);
}