summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/host_cmd/src/host_event_commands.c
blob: c2f7e720451596add4685008aefc59b3b98495da (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
/* 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/ztest.h>
#include "include/lpc.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"

struct host_cmd_host_event_commands_fixture {
	struct host_events_ctx ctx;
};

static void *host_cmd_host_event_commands_setup(void)
{
	static struct host_cmd_host_event_commands_fixture fixture = { 0 };

	return &fixture;
}

static void host_cmd_host_event_commands_before(void *fixture)
{
	struct host_cmd_host_event_commands_fixture *f = fixture;

	host_events_save(&f->ctx);
}

static void host_cmd_host_event_commands_after(void *fixture)
{
	struct host_cmd_host_event_commands_fixture *f = fixture;

	host_events_restore(&f->ctx);
}

ZTEST_SUITE(host_cmd_host_event_commands, drivers_predicate_post_main,
	    host_cmd_host_event_commands_setup,
	    host_cmd_host_event_commands_before,
	    host_cmd_host_event_commands_after, NULL);

/**
 * @brief TestPurpose: Verify EC_CMD_HOST_EVENT invalid host command.
 */
ZTEST_USER(host_cmd_host_event_commands, test_host_event_invalid_cmd)
{
	enum ec_status ret_val;
	struct ec_response_host_event result = { 0 };

	ret_val = host_cmd_host_event(0xFF, 0, &result);

	zassert_equal(ret_val, EC_RES_INVALID_PARAM, "Expected=%d, returned=%d",
		      EC_RES_INVALID_PARAM, ret_val);
}

/**
 * @brief TestPurpose: Verify EC_CMD_HOST_EVENT get host command.
 */
ZTEST_USER(host_cmd_host_event_commands, test_host_event_get_cmd)
{
	enum ec_status ret_val;
	struct ec_response_host_event result = { 0 };
	struct {
		uint8_t mask;
		enum ec_status result;
	} event_get[] = {
		{ EC_HOST_EVENT_MAIN, EC_RES_ACCESS_DENIED },
		{ EC_HOST_EVENT_B, EC_RES_SUCCESS },
#ifdef CONFIG_HOSTCMD_X86
		{ EC_HOST_EVENT_SCI_MASK, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_SMI_MASK, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_ALWAYS_REPORT_MASK, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_ACTIVE_WAKE_MASK, EC_RES_SUCCESS },
#ifdef CONFIG_POWER_S0IX
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX, EC_RES_SUCCESS },
#endif /* CONFIG_POWER_S0IX */
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S3, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S5, EC_RES_SUCCESS },
#endif /* CONFIG_HOSTCMD_X86 */
		{ 0xFF, EC_RES_INVALID_PARAM },
	};

	for (int i = 0; i < ARRAY_SIZE(event_get); i++) {
		ret_val = host_cmd_host_event(EC_HOST_EVENT_GET,
					      event_get[i].mask, &result);
		zassert_equal(ret_val, event_get[i].result,
			      "[%d] Expected=%d, returned=%d", i,
			      event_get[i].result, ret_val);
	}
}

/**
 * @brief TestPurpose: Verify EC_CMD_HOST_EVENT set host command.
 */
ZTEST_USER(host_cmd_host_event_commands, test_host_event_set_cmd)
{
	enum ec_status ret_val;
	struct ec_response_host_event result = { 0 };
	struct {
		uint8_t mask;
		enum ec_status result;
	} event_set[] = {
		{ EC_HOST_EVENT_MAIN, EC_RES_ACCESS_DENIED },
		{ EC_HOST_EVENT_B, EC_RES_ACCESS_DENIED },
#ifdef CONFIG_HOSTCMD_X86
		{ EC_HOST_EVENT_SCI_MASK, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_SMI_MASK, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_ALWAYS_REPORT_MASK, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_ACTIVE_WAKE_MASK, EC_RES_SUCCESS },
#ifdef CONFIG_POWER_S0IX
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX, EC_RES_SUCCESS },
#endif /* CONFIG_POWER_S0IX */
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S3, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S5, EC_RES_SUCCESS },
#endif /* CONFIG_HOSTCMD_X86 */
		{ 0xFF, EC_RES_INVALID_PARAM },
	};

	for (int i = 0; i < ARRAY_SIZE(event_set); i++) {
		ret_val = host_cmd_host_event(EC_HOST_EVENT_SET,
					      event_set[i].mask, &result);
		zassert_equal(ret_val, event_set[i].result,
			      "[%d] Expected=%d, returned=%d", i,
			      event_set[i].result, ret_val);
	}
}

/**
 * @brief TestPurpose: Verify EC_CMD_HOST_EVENT clear host command.
 */
ZTEST_USER(host_cmd_host_event_commands, test_host_event_clear_cmd)
{
	enum ec_status ret_val;
	struct ec_response_host_event result = { 0 };
	struct {
		uint8_t mask;
		enum ec_status result;
	} event_set[] = {
		{ EC_HOST_EVENT_MAIN, EC_RES_SUCCESS },
		{ EC_HOST_EVENT_B, EC_RES_SUCCESS },
#ifdef CONFIG_HOSTCMD_X86
		{ EC_HOST_EVENT_SCI_MASK, EC_RES_ACCESS_DENIED },
		{ EC_HOST_EVENT_SMI_MASK, EC_RES_ACCESS_DENIED },
		{ EC_HOST_EVENT_ALWAYS_REPORT_MASK, EC_RES_ACCESS_DENIED },
		{ EC_HOST_EVENT_ACTIVE_WAKE_MASK, EC_RES_ACCESS_DENIED },
#ifdef CONFIG_POWER_S0IX
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX, EC_RES_ACCESS_DENIED },
#endif /* CONFIG_POWER_S0IX */
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S3, EC_RES_ACCESS_DENIED },
		{ EC_HOST_EVENT_LAZY_WAKE_MASK_S5, EC_RES_ACCESS_DENIED },
#endif /* CONFIG_HOSTCMD_X86 */
		{ 0xFF, EC_RES_INVALID_PARAM },
	};

	for (int i = 0; i < ARRAY_SIZE(event_set); i++) {
		ret_val = host_cmd_host_event(EC_HOST_EVENT_CLEAR,
					      event_set[i].mask, &result);
		zassert_equal(ret_val, event_set[i].result,
			      "Expected [%d] result=%d, returned=%d", i,
			      event_set[i].result, ret_val);
	}
}

enum ec_status host_event_mask_cmd_helper(uint32_t command, uint32_t mask,
					  struct ec_response_host_event_mask *r)
{
	enum ec_status ret_val;

	struct ec_params_host_event_mask params = {
		.mask = mask,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(command, 0, *r, params);

	ret_val = host_command_process(&args);

	return ret_val;
}

/**
 * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_CLEAR clear host command.
 */
ZTEST_USER(host_cmd_host_event_commands, test_host_event_clear__cmd)
{
	enum ec_status ret_val;
	host_event_t events;
	host_event_t mask = EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY);
	host_event_t lpc_event_mask;
	struct ec_response_host_event_mask response = { 0 };

	lpc_event_mask = lpc_get_host_event_mask(LPC_HOST_EVENT_SMI);
	lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, lpc_event_mask | mask);

	host_set_single_event(EC_HOST_EVENT_KEYBOARD_RECOVERY);
	events = host_get_events();

	zassert_true(events & mask, "events=0x%X", events);

	ret_val = host_event_mask_cmd_helper(EC_CMD_HOST_EVENT_CLEAR, mask,
					     &response);

	zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d",
		      EC_RES_SUCCESS, ret_val);

	events = host_get_events();
	zassert_false(events & mask, "events=0x%X", events);
}

/**
 * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_CLEAR_B clear host command.
 */
ZTEST_USER(host_cmd_host_event_commands, test_host_event_clear_b_cmd)
{
	enum ec_status ret_val;
	host_event_t events_b;
	host_event_t mask = EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY);
	host_event_t lpc_event_mask;
	struct ec_response_host_event_mask response = { 0 };
	struct ec_response_host_event result = { 0 };

	lpc_event_mask = lpc_get_host_event_mask(LPC_HOST_EVENT_SMI);
	lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, lpc_event_mask | mask);

	host_set_single_event(EC_HOST_EVENT_KEYBOARD_RECOVERY);

	host_cmd_host_event(EC_HOST_EVENT_GET, EC_HOST_EVENT_B, &result);
	events_b = result.value;
	zassert_true(events_b & mask, "events_b=0x%X", events_b);

	ret_val = host_event_mask_cmd_helper(EC_CMD_HOST_EVENT_CLEAR_B, mask,
					     &response);

	zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d",
		      EC_RES_SUCCESS, ret_val);

	host_cmd_host_event(EC_HOST_EVENT_GET, EC_HOST_EVENT_B, &result);
	events_b = result.value;
	zassert_false(events_b & mask, "events_b=0x%X", events_b);
}