1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kunit tests for ChromeOS Embedded Controller protocol.
4  */
5 
6 #include <kunit/test.h>
7 
8 #include <asm/unaligned.h>
9 
10 #include <linux/platform_data/cros_ec_commands.h>
11 #include <linux/platform_data/cros_ec_proto.h>
12 
13 #include "cros_ec.h"
14 #include "cros_kunit_util.h"
15 
16 #define BUFSIZE 512
17 
18 struct cros_ec_proto_test_priv {
19 	struct cros_ec_device ec_dev;
20 	u8 dout[BUFSIZE];
21 	u8 din[BUFSIZE];
22 	struct cros_ec_command *msg;
23 	u8 _msg[BUFSIZE];
24 };
25 
cros_ec_proto_test_prepare_tx_legacy_normal(struct kunit * test)26 static void cros_ec_proto_test_prepare_tx_legacy_normal(struct kunit *test)
27 {
28 	struct cros_ec_proto_test_priv *priv = test->priv;
29 	struct cros_ec_device *ec_dev = &priv->ec_dev;
30 	struct cros_ec_command *msg = priv->msg;
31 	int ret, i;
32 	u8 csum;
33 
34 	ec_dev->proto_version = 2;
35 
36 	msg->command = EC_CMD_HELLO;
37 	msg->outsize = EC_PROTO2_MAX_PARAM_SIZE;
38 	msg->data[0] = 0xde;
39 	msg->data[1] = 0xad;
40 	msg->data[2] = 0xbe;
41 	msg->data[3] = 0xef;
42 
43 	ret = cros_ec_prepare_tx(ec_dev, msg);
44 
45 	KUNIT_EXPECT_EQ(test, ret, EC_MSG_TX_PROTO_BYTES + EC_PROTO2_MAX_PARAM_SIZE);
46 	KUNIT_EXPECT_EQ(test, ec_dev->dout[0], EC_CMD_VERSION0);
47 	KUNIT_EXPECT_EQ(test, ec_dev->dout[1], EC_CMD_HELLO);
48 	KUNIT_EXPECT_EQ(test, ec_dev->dout[2], EC_PROTO2_MAX_PARAM_SIZE);
49 	KUNIT_EXPECT_EQ(test, EC_MSG_TX_HEADER_BYTES, 3);
50 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 0], 0xde);
51 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 1], 0xad);
52 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 2], 0xbe);
53 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 3], 0xef);
54 	for (i = 4; i < EC_PROTO2_MAX_PARAM_SIZE; ++i)
55 		KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + i], 0);
56 
57 	csum = EC_CMD_VERSION0;
58 	csum += EC_CMD_HELLO;
59 	csum += EC_PROTO2_MAX_PARAM_SIZE;
60 	csum += 0xde;
61 	csum += 0xad;
62 	csum += 0xbe;
63 	csum += 0xef;
64 	KUNIT_EXPECT_EQ(test,
65 			ec_dev->dout[EC_MSG_TX_HEADER_BYTES + EC_PROTO2_MAX_PARAM_SIZE],
66 			csum);
67 }
68 
cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize(struct kunit * test)69 static void cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize(struct kunit *test)
70 {
71 	struct cros_ec_proto_test_priv *priv = test->priv;
72 	struct cros_ec_device *ec_dev = &priv->ec_dev;
73 	struct cros_ec_command *msg = priv->msg;
74 	int ret;
75 
76 	ec_dev->proto_version = 2;
77 
78 	msg->outsize = EC_PROTO2_MAX_PARAM_SIZE + 1;
79 
80 	ret = cros_ec_prepare_tx(ec_dev, msg);
81 	KUNIT_EXPECT_EQ(test, ret, -EINVAL);
82 }
83 
cros_ec_proto_test_prepare_tx_normal(struct kunit * test)84 static void cros_ec_proto_test_prepare_tx_normal(struct kunit *test)
85 {
86 	struct cros_ec_proto_test_priv *priv = test->priv;
87 	struct cros_ec_device *ec_dev = &priv->ec_dev;
88 	struct cros_ec_command *msg = priv->msg;
89 	struct ec_host_request *request = (struct ec_host_request *)ec_dev->dout;
90 	int ret, i;
91 	u8 csum;
92 
93 	msg->command = EC_CMD_HELLO;
94 	msg->outsize = 0x88;
95 	msg->data[0] = 0xde;
96 	msg->data[1] = 0xad;
97 	msg->data[2] = 0xbe;
98 	msg->data[3] = 0xef;
99 
100 	ret = cros_ec_prepare_tx(ec_dev, msg);
101 
102 	KUNIT_EXPECT_EQ(test, ret, sizeof(*request) + 0x88);
103 
104 	KUNIT_EXPECT_EQ(test, request->struct_version, EC_HOST_REQUEST_VERSION);
105 	KUNIT_EXPECT_EQ(test, request->command, EC_CMD_HELLO);
106 	KUNIT_EXPECT_EQ(test, request->command_version, 0);
107 	KUNIT_EXPECT_EQ(test, request->data_len, 0x88);
108 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 0], 0xde);
109 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 1], 0xad);
110 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 2], 0xbe);
111 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 3], 0xef);
112 	for (i = 4; i < 0x88; ++i)
113 		KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + i], 0);
114 
115 	csum = EC_HOST_REQUEST_VERSION;
116 	csum += EC_CMD_HELLO;
117 	csum += 0x88;
118 	csum += 0xde;
119 	csum += 0xad;
120 	csum += 0xbe;
121 	csum += 0xef;
122 	KUNIT_EXPECT_EQ(test, request->checksum, (u8)-csum);
123 }
124 
cros_ec_proto_test_prepare_tx_bad_msg_outsize(struct kunit * test)125 static void cros_ec_proto_test_prepare_tx_bad_msg_outsize(struct kunit *test)
126 {
127 	struct cros_ec_proto_test_priv *priv = test->priv;
128 	struct cros_ec_device *ec_dev = &priv->ec_dev;
129 	struct cros_ec_command *msg = priv->msg;
130 	int ret;
131 
132 	msg->outsize = ec_dev->dout_size - sizeof(struct ec_host_request) + 1;
133 
134 	ret = cros_ec_prepare_tx(ec_dev, msg);
135 	KUNIT_EXPECT_EQ(test, ret, -EINVAL);
136 }
137 
cros_ec_proto_test_check_result(struct kunit * test)138 static void cros_ec_proto_test_check_result(struct kunit *test)
139 {
140 	struct cros_ec_proto_test_priv *priv = test->priv;
141 	struct cros_ec_device *ec_dev = &priv->ec_dev;
142 	struct cros_ec_command *msg = priv->msg;
143 	int ret, i;
144 	static enum ec_status status[] = {
145 		EC_RES_SUCCESS,
146 		EC_RES_INVALID_COMMAND,
147 		EC_RES_ERROR,
148 		EC_RES_INVALID_PARAM,
149 		EC_RES_ACCESS_DENIED,
150 		EC_RES_INVALID_RESPONSE,
151 		EC_RES_INVALID_VERSION,
152 		EC_RES_INVALID_CHECKSUM,
153 		EC_RES_UNAVAILABLE,
154 		EC_RES_TIMEOUT,
155 		EC_RES_OVERFLOW,
156 		EC_RES_INVALID_HEADER,
157 		EC_RES_REQUEST_TRUNCATED,
158 		EC_RES_RESPONSE_TOO_BIG,
159 		EC_RES_BUS_ERROR,
160 		EC_RES_BUSY,
161 		EC_RES_INVALID_HEADER_VERSION,
162 		EC_RES_INVALID_HEADER_CRC,
163 		EC_RES_INVALID_DATA_CRC,
164 		EC_RES_DUP_UNAVAILABLE,
165 	};
166 
167 	for (i = 0; i < ARRAY_SIZE(status); ++i) {
168 		msg->result = status[i];
169 		ret = cros_ec_check_result(ec_dev, msg);
170 		KUNIT_EXPECT_EQ(test, ret, 0);
171 	}
172 
173 	msg->result = EC_RES_IN_PROGRESS;
174 	ret = cros_ec_check_result(ec_dev, msg);
175 	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
176 }
177 
cros_ec_proto_test_query_all_pretest(struct kunit * test)178 static void cros_ec_proto_test_query_all_pretest(struct kunit *test)
179 {
180 	struct cros_ec_proto_test_priv *priv = test->priv;
181 	struct cros_ec_device *ec_dev = &priv->ec_dev;
182 
183 	/*
184 	 * cros_ec_query_all() will free din and dout and allocate them again to fit the usage by
185 	 * calling devm_kfree() and devm_kzalloc().  Set them to NULL as they aren't managed by
186 	 * ec_dev->dev but allocated statically in struct cros_ec_proto_test_priv
187 	 * (see cros_ec_proto_test_init()).
188 	 */
189 	ec_dev->din = NULL;
190 	ec_dev->dout = NULL;
191 }
192 
cros_ec_proto_test_query_all_normal(struct kunit * test)193 static void cros_ec_proto_test_query_all_normal(struct kunit *test)
194 {
195 	struct cros_ec_proto_test_priv *priv = test->priv;
196 	struct cros_ec_device *ec_dev = &priv->ec_dev;
197 	struct ec_xfer_mock *mock;
198 	int ret;
199 
200 	/* For cros_ec_get_proto_info() without passthru. */
201 	{
202 		struct ec_response_get_protocol_info *data;
203 
204 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
205 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
206 
207 		data = (struct ec_response_get_protocol_info *)mock->o_data;
208 		data->protocol_versions = BIT(3) | BIT(2);
209 		data->max_request_packet_size = 0xbe;
210 		data->max_response_packet_size = 0xef;
211 	}
212 
213 	/* For cros_ec_get_proto_info() with passthru. */
214 	{
215 		struct ec_response_get_protocol_info *data;
216 
217 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
218 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
219 
220 		data = (struct ec_response_get_protocol_info *)mock->o_data;
221 		data->max_request_packet_size = 0xbf;
222 	}
223 
224 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
225 	{
226 		struct ec_response_get_cmd_versions *data;
227 
228 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
229 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
230 
231 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
232 		data->version_mask = BIT(6) | BIT(5);
233 	}
234 
235 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
236 	{
237 		struct ec_response_get_cmd_versions *data;
238 
239 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
240 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
241 
242 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
243 		data->version_mask = BIT(1);
244 	}
245 
246 	/* For cros_ec_get_host_event_wake_mask(). */
247 	{
248 		struct ec_response_host_event_mask *data;
249 
250 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
251 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
252 
253 		data = (struct ec_response_host_event_mask *)mock->o_data;
254 		data->mask = 0xbeef;
255 	}
256 
257 	cros_ec_proto_test_query_all_pretest(test);
258 	ret = cros_ec_query_all(ec_dev);
259 	KUNIT_EXPECT_EQ(test, ret, 0);
260 
261 	/* For cros_ec_get_proto_info() without passthru. */
262 	{
263 		mock = cros_kunit_ec_xfer_mock_next();
264 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
265 
266 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
267 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
268 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
269 				sizeof(struct ec_response_get_protocol_info));
270 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
271 
272 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, 0xbe - sizeof(struct ec_host_request));
273 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, 0xef - sizeof(struct ec_host_response));
274 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 3);
275 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, 0xef + EC_MAX_RESPONSE_OVERHEAD);
276 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, 0xbe + EC_MAX_REQUEST_OVERHEAD);
277 	}
278 
279 	/* For cros_ec_get_proto_info() with passthru. */
280 	{
281 		mock = cros_kunit_ec_xfer_mock_next();
282 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
283 
284 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
285 		KUNIT_EXPECT_EQ(test, mock->msg.command,
286 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
287 				EC_CMD_GET_PROTOCOL_INFO);
288 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
289 				sizeof(struct ec_response_get_protocol_info));
290 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
291 
292 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0xbf - sizeof(struct ec_host_request));
293 	}
294 
295 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
296 	{
297 		struct ec_params_get_cmd_versions *data;
298 
299 		mock = cros_kunit_ec_xfer_mock_next();
300 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
301 
302 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
303 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
304 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
305 				sizeof(struct ec_response_get_cmd_versions));
306 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
307 
308 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
309 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
310 
311 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 7);
312 	}
313 
314 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
315 	{
316 		struct ec_params_get_cmd_versions *data;
317 
318 		mock = cros_kunit_ec_xfer_mock_next();
319 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
320 
321 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
322 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
323 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
324 				sizeof(struct ec_response_get_cmd_versions));
325 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
326 
327 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
328 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_HOST_SLEEP_EVENT);
329 
330 		KUNIT_EXPECT_TRUE(test, ec_dev->host_sleep_v1);
331 	}
332 
333 	/* For cros_ec_get_host_event_wake_mask(). */
334 	{
335 		mock = cros_kunit_ec_xfer_mock_next();
336 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
337 
338 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
339 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
340 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
341 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
342 
343 		KUNIT_EXPECT_EQ(test, ec_dev->host_event_wake_mask, 0xbeef);
344 	}
345 }
346 
cros_ec_proto_test_query_all_no_pd_return_error(struct kunit * test)347 static void cros_ec_proto_test_query_all_no_pd_return_error(struct kunit *test)
348 {
349 	struct cros_ec_proto_test_priv *priv = test->priv;
350 	struct cros_ec_device *ec_dev = &priv->ec_dev;
351 	struct ec_xfer_mock *mock;
352 	int ret;
353 
354 	/* Set some garbage bytes. */
355 	ec_dev->max_passthru = 0xbf;
356 
357 	/* For cros_ec_get_proto_info() without passthru. */
358 	{
359 		struct ec_response_get_protocol_info *data;
360 
361 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
362 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
363 
364 		/*
365 		 * Although it doesn't check the value, provides valid sizes so that
366 		 * cros_ec_query_all() allocates din and dout correctly.
367 		 */
368 		data = (struct ec_response_get_protocol_info *)mock->o_data;
369 		data->max_request_packet_size = 0xbe;
370 		data->max_response_packet_size = 0xef;
371 	}
372 
373 	/* For cros_ec_get_proto_info() with passthru. */
374 	{
375 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
376 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
377 	}
378 
379 	cros_ec_proto_test_query_all_pretest(test);
380 	ret = cros_ec_query_all(ec_dev);
381 	KUNIT_EXPECT_EQ(test, ret, 0);
382 
383 	/* For cros_ec_get_proto_info() without passthru. */
384 	{
385 		mock = cros_kunit_ec_xfer_mock_next();
386 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
387 
388 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
389 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
390 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
391 				sizeof(struct ec_response_get_protocol_info));
392 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
393 	}
394 
395 	/* For cros_ec_get_proto_info() with passthru. */
396 	{
397 		mock = cros_kunit_ec_xfer_mock_next();
398 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
399 
400 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
401 		KUNIT_EXPECT_EQ(test, mock->msg.command,
402 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
403 				EC_CMD_GET_PROTOCOL_INFO);
404 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
405 				sizeof(struct ec_response_get_protocol_info));
406 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
407 
408 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
409 	}
410 }
411 
cros_ec_proto_test_query_all_no_pd_return0(struct kunit * test)412 static void cros_ec_proto_test_query_all_no_pd_return0(struct kunit *test)
413 {
414 	struct cros_ec_proto_test_priv *priv = test->priv;
415 	struct cros_ec_device *ec_dev = &priv->ec_dev;
416 	struct ec_xfer_mock *mock;
417 	int ret;
418 
419 	/* Set some garbage bytes. */
420 	ec_dev->max_passthru = 0xbf;
421 
422 	/* For cros_ec_get_proto_info() without passthru. */
423 	{
424 		struct ec_response_get_protocol_info *data;
425 
426 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
427 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
428 
429 		/*
430 		 * Although it doesn't check the value, provides valid sizes so that
431 		 * cros_ec_query_all() allocates din and dout correctly.
432 		 */
433 		data = (struct ec_response_get_protocol_info *)mock->o_data;
434 		data->max_request_packet_size = 0xbe;
435 		data->max_response_packet_size = 0xef;
436 	}
437 
438 	/* For cros_ec_get_proto_info() with passthru. */
439 	{
440 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
441 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
442 	}
443 
444 	cros_ec_proto_test_query_all_pretest(test);
445 	ret = cros_ec_query_all(ec_dev);
446 	KUNIT_EXPECT_EQ(test, ret, 0);
447 
448 	/* For cros_ec_get_proto_info() without passthru. */
449 	{
450 		mock = cros_kunit_ec_xfer_mock_next();
451 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
452 
453 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
454 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
455 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
456 				sizeof(struct ec_response_get_protocol_info));
457 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
458 	}
459 
460 	/* For cros_ec_get_proto_info() with passthru. */
461 	{
462 		mock = cros_kunit_ec_xfer_mock_next();
463 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
464 
465 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
466 		KUNIT_EXPECT_EQ(test, mock->msg.command,
467 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
468 				EC_CMD_GET_PROTOCOL_INFO);
469 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
470 				sizeof(struct ec_response_get_protocol_info));
471 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
472 
473 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
474 	}
475 }
476 
cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct kunit * test)477 static void cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct kunit *test)
478 {
479 	struct cros_ec_proto_test_priv *priv = test->priv;
480 	struct cros_ec_device *ec_dev = &priv->ec_dev;
481 	struct ec_xfer_mock *mock;
482 	int ret;
483 
484 	/* For cros_ec_get_proto_info() without passthru. */
485 	{
486 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
487 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
488 	}
489 
490 	/* For cros_ec_get_proto_info_legacy(). */
491 	{
492 		struct ec_response_hello *data;
493 
494 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
495 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
496 
497 		data = (struct ec_response_hello *)mock->o_data;
498 		data->out_data = 0xa1b2c3d4;
499 	}
500 
501 	cros_ec_proto_test_query_all_pretest(test);
502 	ret = cros_ec_query_all(ec_dev);
503 	KUNIT_EXPECT_EQ(test, ret, 0);
504 
505 	/* For cros_ec_get_proto_info() without passthru. */
506 	{
507 		mock = cros_kunit_ec_xfer_mock_next();
508 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
509 
510 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
511 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
512 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
513 				sizeof(struct ec_response_get_protocol_info));
514 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
515 	}
516 
517 	/* For cros_ec_get_proto_info_legacy(). */
518 	{
519 		struct ec_params_hello *data;
520 
521 		mock = cros_kunit_ec_xfer_mock_next();
522 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
523 
524 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
525 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
526 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
527 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
528 
529 		data = (struct ec_params_hello *)mock->i_data;
530 		KUNIT_EXPECT_EQ(test, data->in_data, 0xa0b0c0d0);
531 
532 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 2);
533 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, EC_PROTO2_MAX_PARAM_SIZE);
534 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, EC_PROTO2_MAX_PARAM_SIZE);
535 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
536 		KUNIT_EXPECT_PTR_EQ(test, ec_dev->pkt_xfer, NULL);
537 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, EC_PROTO2_MSG_BYTES);
538 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, EC_PROTO2_MSG_BYTES);
539 	}
540 }
541 
cros_ec_proto_test_query_all_legacy_normal_v3_return0(struct kunit * test)542 static void cros_ec_proto_test_query_all_legacy_normal_v3_return0(struct kunit *test)
543 {
544 	struct cros_ec_proto_test_priv *priv = test->priv;
545 	struct cros_ec_device *ec_dev = &priv->ec_dev;
546 	struct ec_xfer_mock *mock;
547 	int ret;
548 
549 	/* For cros_ec_get_proto_info() without passthru. */
550 	{
551 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
552 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
553 	}
554 
555 	/* For cros_ec_get_proto_info_legacy(). */
556 	{
557 		struct ec_response_hello *data;
558 
559 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
560 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
561 
562 		data = (struct ec_response_hello *)mock->o_data;
563 		data->out_data = 0xa1b2c3d4;
564 	}
565 
566 	cros_ec_proto_test_query_all_pretest(test);
567 	ret = cros_ec_query_all(ec_dev);
568 	KUNIT_EXPECT_EQ(test, ret, 0);
569 
570 	/* For cros_ec_get_proto_info() without passthru. */
571 	{
572 		mock = cros_kunit_ec_xfer_mock_next();
573 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
574 
575 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
576 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
577 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
578 				sizeof(struct ec_response_get_protocol_info));
579 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
580 	}
581 
582 	/* For cros_ec_get_proto_info_legacy(). */
583 	{
584 		struct ec_params_hello *data;
585 
586 		mock = cros_kunit_ec_xfer_mock_next();
587 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
588 
589 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
590 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
591 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
592 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
593 
594 		data = (struct ec_params_hello *)mock->i_data;
595 		KUNIT_EXPECT_EQ(test, data->in_data, 0xa0b0c0d0);
596 
597 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 2);
598 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, EC_PROTO2_MAX_PARAM_SIZE);
599 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, EC_PROTO2_MAX_PARAM_SIZE);
600 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
601 		KUNIT_EXPECT_PTR_EQ(test, ec_dev->pkt_xfer, NULL);
602 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, EC_PROTO2_MSG_BYTES);
603 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, EC_PROTO2_MSG_BYTES);
604 	}
605 }
606 
cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit * test)607 static void cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit *test)
608 {
609 	struct cros_ec_proto_test_priv *priv = test->priv;
610 	struct cros_ec_device *ec_dev = &priv->ec_dev;
611 	struct ec_xfer_mock *mock;
612 	int ret;
613 
614 	/* For cros_ec_get_proto_info() without passthru. */
615 	{
616 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
617 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
618 	}
619 
620 	/* For cros_ec_get_proto_info_legacy(). */
621 	{
622 		mock = cros_kunit_ec_xfer_mock_addx(test, -EIO, EC_RES_SUCCESS, 0);
623 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
624 	}
625 
626 	cros_ec_proto_test_query_all_pretest(test);
627 	ret = cros_ec_query_all(ec_dev);
628 	KUNIT_EXPECT_EQ(test, ret, -EIO);
629 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
630 
631 	/* For cros_ec_get_proto_info() without passthru. */
632 	{
633 		mock = cros_kunit_ec_xfer_mock_next();
634 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
635 
636 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
637 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
638 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
639 				sizeof(struct ec_response_get_protocol_info));
640 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
641 	}
642 
643 	/* For cros_ec_get_proto_info_legacy(). */
644 	{
645 		mock = cros_kunit_ec_xfer_mock_next();
646 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
647 
648 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
649 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
650 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
651 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
652 	}
653 }
654 
cros_ec_proto_test_query_all_legacy_return_error(struct kunit * test)655 static void cros_ec_proto_test_query_all_legacy_return_error(struct kunit *test)
656 {
657 	struct cros_ec_proto_test_priv *priv = test->priv;
658 	struct cros_ec_device *ec_dev = &priv->ec_dev;
659 	struct ec_xfer_mock *mock;
660 	int ret;
661 
662 	/* For cros_ec_get_proto_info() without passthru. */
663 	{
664 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
665 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
666 	}
667 
668 	/* For cros_ec_get_proto_info_legacy(). */
669 	{
670 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
671 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
672 	}
673 
674 	cros_ec_proto_test_query_all_pretest(test);
675 	ret = cros_ec_query_all(ec_dev);
676 	KUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP);
677 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
678 
679 	/* For cros_ec_get_proto_info() without passthru. */
680 	{
681 		mock = cros_kunit_ec_xfer_mock_next();
682 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
683 
684 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
685 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
686 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
687 				sizeof(struct ec_response_get_protocol_info));
688 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
689 	}
690 
691 	/* For cros_ec_get_proto_info_legacy(). */
692 	{
693 		mock = cros_kunit_ec_xfer_mock_next();
694 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
695 
696 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
697 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
698 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
699 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
700 	}
701 }
702 
cros_ec_proto_test_query_all_legacy_data_error(struct kunit * test)703 static void cros_ec_proto_test_query_all_legacy_data_error(struct kunit *test)
704 {
705 	struct cros_ec_proto_test_priv *priv = test->priv;
706 	struct cros_ec_device *ec_dev = &priv->ec_dev;
707 	struct ec_xfer_mock *mock;
708 	int ret;
709 
710 	/* For cros_ec_get_proto_info() without passthru. */
711 	{
712 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
713 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
714 	}
715 
716 	/* For cros_ec_get_proto_info_legacy(). */
717 	{
718 		struct ec_response_hello *data;
719 
720 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
721 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
722 
723 		data = (struct ec_response_hello *)mock->o_data;
724 		data->out_data = 0xbeefbfbf;
725 	}
726 
727 	cros_ec_proto_test_query_all_pretest(test);
728 	ret = cros_ec_query_all(ec_dev);
729 	KUNIT_EXPECT_EQ(test, ret, -EBADMSG);
730 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
731 
732 	/* For cros_ec_get_proto_info() without passthru. */
733 	{
734 		mock = cros_kunit_ec_xfer_mock_next();
735 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
736 
737 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
738 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
739 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
740 				sizeof(struct ec_response_get_protocol_info));
741 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
742 	}
743 
744 	/* For cros_ec_get_proto_info_legacy(). */
745 	{
746 		mock = cros_kunit_ec_xfer_mock_next();
747 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
748 
749 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
750 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
751 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
752 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
753 	}
754 }
755 
cros_ec_proto_test_query_all_legacy_return0(struct kunit * test)756 static void cros_ec_proto_test_query_all_legacy_return0(struct kunit *test)
757 {
758 	struct cros_ec_proto_test_priv *priv = test->priv;
759 	struct cros_ec_device *ec_dev = &priv->ec_dev;
760 	struct ec_xfer_mock *mock;
761 	int ret;
762 
763 	/* For cros_ec_get_proto_info() without passthru. */
764 	{
765 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
766 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
767 	}
768 
769 	/* For cros_ec_get_proto_info_legacy(). */
770 	{
771 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
772 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
773 	}
774 
775 	cros_ec_proto_test_query_all_pretest(test);
776 	ret = cros_ec_query_all(ec_dev);
777 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
778 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
779 
780 	/* For cros_ec_get_proto_info() without passthru. */
781 	{
782 		mock = cros_kunit_ec_xfer_mock_next();
783 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
784 
785 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
786 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
787 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
788 				sizeof(struct ec_response_get_protocol_info));
789 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
790 	}
791 
792 	/* For cros_ec_get_proto_info_legacy(). */
793 	{
794 		mock = cros_kunit_ec_xfer_mock_next();
795 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
796 
797 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
798 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
799 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
800 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
801 	}
802 }
803 
cros_ec_proto_test_query_all_no_mkbp(struct kunit * test)804 static void cros_ec_proto_test_query_all_no_mkbp(struct kunit *test)
805 {
806 	struct cros_ec_proto_test_priv *priv = test->priv;
807 	struct cros_ec_device *ec_dev = &priv->ec_dev;
808 	struct ec_xfer_mock *mock;
809 	int ret;
810 
811 	/* Set some garbage bytes. */
812 	ec_dev->mkbp_event_supported = 0xbf;
813 
814 	/* For cros_ec_get_proto_info() without passthru. */
815 	{
816 		struct ec_response_get_protocol_info *data;
817 
818 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
819 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
820 
821 		/*
822 		 * Although it doesn't check the value, provides valid sizes so that
823 		 * cros_ec_query_all() allocates din and dout correctly.
824 		 */
825 		data = (struct ec_response_get_protocol_info *)mock->o_data;
826 		data->max_request_packet_size = 0xbe;
827 		data->max_response_packet_size = 0xef;
828 	}
829 
830 	/* For cros_ec_get_proto_info() with passthru. */
831 	{
832 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
833 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
834 	}
835 
836 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
837 	{
838 		struct ec_response_get_cmd_versions *data;
839 
840 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
841 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
842 
843 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
844 		data->version_mask = 0;
845 	}
846 
847 	cros_ec_proto_test_query_all_pretest(test);
848 	ret = cros_ec_query_all(ec_dev);
849 	KUNIT_EXPECT_EQ(test, ret, 0);
850 
851 	/* For cros_ec_get_proto_info() without passthru. */
852 	{
853 		mock = cros_kunit_ec_xfer_mock_next();
854 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
855 
856 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
857 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
858 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
859 				sizeof(struct ec_response_get_protocol_info));
860 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
861 	}
862 
863 	/* For cros_ec_get_proto_info() with passthru. */
864 	{
865 		mock = cros_kunit_ec_xfer_mock_next();
866 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
867 
868 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
869 		KUNIT_EXPECT_EQ(test, mock->msg.command,
870 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
871 				EC_CMD_GET_PROTOCOL_INFO);
872 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
873 				sizeof(struct ec_response_get_protocol_info));
874 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
875 	}
876 
877 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
878 	{
879 		struct ec_params_get_cmd_versions *data;
880 
881 		mock = cros_kunit_ec_xfer_mock_next();
882 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
883 
884 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
885 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
886 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
887 				sizeof(struct ec_response_get_cmd_versions));
888 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
889 
890 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
891 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
892 
893 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
894 	}
895 }
896 
cros_ec_proto_test_query_all_no_mkbp_return_error(struct kunit * test)897 static void cros_ec_proto_test_query_all_no_mkbp_return_error(struct kunit *test)
898 {
899 	struct cros_ec_proto_test_priv *priv = test->priv;
900 	struct cros_ec_device *ec_dev = &priv->ec_dev;
901 	struct ec_xfer_mock *mock;
902 	int ret;
903 
904 	/* Set some garbage bytes. */
905 	ec_dev->mkbp_event_supported = 0xbf;
906 
907 	/* For cros_ec_get_proto_info() without passthru. */
908 	{
909 		struct ec_response_get_protocol_info *data;
910 
911 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
912 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
913 
914 		/*
915 		 * Although it doesn't check the value, provides valid sizes so that
916 		 * cros_ec_query_all() allocates din and dout correctly.
917 		 */
918 		data = (struct ec_response_get_protocol_info *)mock->o_data;
919 		data->max_request_packet_size = 0xbe;
920 		data->max_response_packet_size = 0xef;
921 	}
922 
923 	/* For cros_ec_get_proto_info() with passthru. */
924 	{
925 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
926 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
927 	}
928 
929 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
930 	{
931 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
932 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
933 	}
934 
935 	cros_ec_proto_test_query_all_pretest(test);
936 	ret = cros_ec_query_all(ec_dev);
937 	KUNIT_EXPECT_EQ(test, ret, 0);
938 
939 	/* For cros_ec_get_proto_info() without passthru. */
940 	{
941 		mock = cros_kunit_ec_xfer_mock_next();
942 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
943 
944 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
945 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
946 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
947 				sizeof(struct ec_response_get_protocol_info));
948 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
949 	}
950 
951 	/* For cros_ec_get_proto_info() with passthru. */
952 	{
953 		mock = cros_kunit_ec_xfer_mock_next();
954 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
955 
956 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
957 		KUNIT_EXPECT_EQ(test, mock->msg.command,
958 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
959 				EC_CMD_GET_PROTOCOL_INFO);
960 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
961 				sizeof(struct ec_response_get_protocol_info));
962 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
963 	}
964 
965 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
966 	{
967 		struct ec_params_get_cmd_versions *data;
968 
969 		mock = cros_kunit_ec_xfer_mock_next();
970 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
971 
972 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
973 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
974 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
975 				sizeof(struct ec_response_get_cmd_versions));
976 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
977 
978 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
979 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
980 
981 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
982 	}
983 }
984 
cros_ec_proto_test_query_all_no_mkbp_return0(struct kunit * test)985 static void cros_ec_proto_test_query_all_no_mkbp_return0(struct kunit *test)
986 {
987 	struct cros_ec_proto_test_priv *priv = test->priv;
988 	struct cros_ec_device *ec_dev = &priv->ec_dev;
989 	struct ec_xfer_mock *mock;
990 	int ret;
991 
992 	/* Set some garbage bytes. */
993 	ec_dev->mkbp_event_supported = 0xbf;
994 
995 	/* For cros_ec_get_proto_info() without passthru. */
996 	{
997 		struct ec_response_get_protocol_info *data;
998 
999 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1000 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1001 
1002 		/*
1003 		 * Although it doesn't check the value, provides valid sizes so that
1004 		 * cros_ec_query_all() allocates din and dout correctly.
1005 		 */
1006 		data = (struct ec_response_get_protocol_info *)mock->o_data;
1007 		data->max_request_packet_size = 0xbe;
1008 		data->max_response_packet_size = 0xef;
1009 	}
1010 
1011 	/* For cros_ec_get_proto_info() with passthru. */
1012 	{
1013 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1014 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1015 	}
1016 
1017 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1018 	{
1019 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1020 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1021 	}
1022 
1023 	cros_ec_proto_test_query_all_pretest(test);
1024 	ret = cros_ec_query_all(ec_dev);
1025 	KUNIT_EXPECT_EQ(test, ret, 0);
1026 
1027 	/* For cros_ec_get_proto_info() without passthru. */
1028 	{
1029 		mock = cros_kunit_ec_xfer_mock_next();
1030 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1031 
1032 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1033 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1034 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1035 				sizeof(struct ec_response_get_protocol_info));
1036 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1037 	}
1038 
1039 	/* For cros_ec_get_proto_info() with passthru. */
1040 	{
1041 		mock = cros_kunit_ec_xfer_mock_next();
1042 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1043 
1044 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1045 		KUNIT_EXPECT_EQ(test, mock->msg.command,
1046 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
1047 				EC_CMD_GET_PROTOCOL_INFO);
1048 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1049 				sizeof(struct ec_response_get_protocol_info));
1050 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1051 	}
1052 
1053 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1054 	{
1055 		struct ec_params_get_cmd_versions *data;
1056 
1057 		mock = cros_kunit_ec_xfer_mock_next();
1058 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1059 
1060 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1061 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1062 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1063 				sizeof(struct ec_response_get_cmd_versions));
1064 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
1065 
1066 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
1067 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
1068 
1069 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
1070 	}
1071 }
1072 
cros_ec_proto_test_query_all_no_host_sleep(struct kunit * test)1073 static void cros_ec_proto_test_query_all_no_host_sleep(struct kunit *test)
1074 {
1075 	struct cros_ec_proto_test_priv *priv = test->priv;
1076 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1077 	struct ec_xfer_mock *mock;
1078 	int ret;
1079 
1080 	/* Set some garbage bytes. */
1081 	ec_dev->host_sleep_v1 = true;
1082 
1083 	/* For cros_ec_get_proto_info() without passthru. */
1084 	{
1085 		struct ec_response_get_protocol_info *data;
1086 
1087 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1088 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1089 
1090 		/*
1091 		 * Although it doesn't check the value, provides valid sizes so that
1092 		 * cros_ec_query_all() allocates din and dout correctly.
1093 		 */
1094 		data = (struct ec_response_get_protocol_info *)mock->o_data;
1095 		data->max_request_packet_size = 0xbe;
1096 		data->max_response_packet_size = 0xef;
1097 	}
1098 
1099 	/* For cros_ec_get_proto_info() with passthru. */
1100 	{
1101 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1102 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1103 	}
1104 
1105 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1106 	{
1107 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1108 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1109 	}
1110 
1111 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1112 	{
1113 		struct ec_response_get_cmd_versions *data;
1114 
1115 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1116 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1117 
1118 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
1119 		data->version_mask = 0;
1120 	}
1121 
1122 	cros_ec_proto_test_query_all_pretest(test);
1123 	ret = cros_ec_query_all(ec_dev);
1124 	KUNIT_EXPECT_EQ(test, ret, 0);
1125 
1126 	/* For cros_ec_get_proto_info() without passthru. */
1127 	{
1128 		mock = cros_kunit_ec_xfer_mock_next();
1129 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1130 
1131 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1132 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1133 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1134 				sizeof(struct ec_response_get_protocol_info));
1135 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1136 	}
1137 
1138 	/* For cros_ec_get_proto_info() with passthru. */
1139 	{
1140 		mock = cros_kunit_ec_xfer_mock_next();
1141 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1142 
1143 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1144 		KUNIT_EXPECT_EQ(test, mock->msg.command,
1145 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
1146 				EC_CMD_GET_PROTOCOL_INFO);
1147 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1148 				sizeof(struct ec_response_get_protocol_info));
1149 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1150 	}
1151 
1152 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1153 	{
1154 		mock = cros_kunit_ec_xfer_mock_next();
1155 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1156 
1157 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1158 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1159 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1160 				sizeof(struct ec_response_get_cmd_versions));
1161 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1162 	}
1163 
1164 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1165 	{
1166 		mock = cros_kunit_ec_xfer_mock_next();
1167 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1168 
1169 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1170 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1171 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1172 				sizeof(struct ec_response_get_cmd_versions));
1173 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1174 
1175 		KUNIT_EXPECT_FALSE(test, ec_dev->host_sleep_v1);
1176 	}
1177 }
1178 
cros_ec_proto_test_query_all_no_host_sleep_return0(struct kunit * test)1179 static void cros_ec_proto_test_query_all_no_host_sleep_return0(struct kunit *test)
1180 {
1181 	struct cros_ec_proto_test_priv *priv = test->priv;
1182 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1183 	struct ec_xfer_mock *mock;
1184 	int ret;
1185 
1186 	/* Set some garbage bytes. */
1187 	ec_dev->host_sleep_v1 = true;
1188 
1189 	/* For cros_ec_get_proto_info() without passthru. */
1190 	{
1191 		struct ec_response_get_protocol_info *data;
1192 
1193 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1194 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1195 
1196 		/*
1197 		 * Although it doesn't check the value, provides valid sizes so that
1198 		 * cros_ec_query_all() allocates din and dout correctly.
1199 		 */
1200 		data = (struct ec_response_get_protocol_info *)mock->o_data;
1201 		data->max_request_packet_size = 0xbe;
1202 		data->max_response_packet_size = 0xef;
1203 	}
1204 
1205 	/* For cros_ec_get_proto_info() with passthru. */
1206 	{
1207 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1208 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1209 	}
1210 
1211 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1212 	{
1213 		struct ec_response_get_cmd_versions *data;
1214 
1215 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1216 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1217 
1218 		/* In order to pollute next cros_ec_get_host_command_version_mask(). */
1219 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
1220 		data->version_mask = 0xbeef;
1221 	}
1222 
1223 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1224 	{
1225 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1226 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1227 	}
1228 
1229 	cros_ec_proto_test_query_all_pretest(test);
1230 	ret = cros_ec_query_all(ec_dev);
1231 	KUNIT_EXPECT_EQ(test, ret, 0);
1232 
1233 	/* For cros_ec_get_proto_info() without passthru. */
1234 	{
1235 		mock = cros_kunit_ec_xfer_mock_next();
1236 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1237 
1238 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1239 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1240 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1241 				sizeof(struct ec_response_get_protocol_info));
1242 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1243 	}
1244 
1245 	/* For cros_ec_get_proto_info() with passthru. */
1246 	{
1247 		mock = cros_kunit_ec_xfer_mock_next();
1248 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1249 
1250 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1251 		KUNIT_EXPECT_EQ(test, mock->msg.command,
1252 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
1253 				EC_CMD_GET_PROTOCOL_INFO);
1254 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1255 				sizeof(struct ec_response_get_protocol_info));
1256 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1257 	}
1258 
1259 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1260 	{
1261 		mock = cros_kunit_ec_xfer_mock_next();
1262 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1263 
1264 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1265 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1266 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1267 				sizeof(struct ec_response_get_cmd_versions));
1268 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1269 	}
1270 
1271 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1272 	{
1273 		mock = cros_kunit_ec_xfer_mock_next();
1274 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1275 
1276 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1277 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1278 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1279 				sizeof(struct ec_response_get_cmd_versions));
1280 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1281 
1282 		KUNIT_EXPECT_FALSE(test, ec_dev->host_sleep_v1);
1283 	}
1284 }
1285 
cros_ec_proto_test_query_all_default_wake_mask_return_error(struct kunit * test)1286 static void cros_ec_proto_test_query_all_default_wake_mask_return_error(struct kunit *test)
1287 {
1288 	struct cros_ec_proto_test_priv *priv = test->priv;
1289 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1290 	struct ec_xfer_mock *mock;
1291 	int ret;
1292 
1293 	/* Set some garbage bytes. */
1294 	ec_dev->host_event_wake_mask = U32_MAX;
1295 
1296 	/* For cros_ec_get_proto_info() without passthru. */
1297 	{
1298 		struct ec_response_get_protocol_info *data;
1299 
1300 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1301 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1302 
1303 		/*
1304 		 * Although it doesn't check the value, provides valid sizes so that
1305 		 * cros_ec_query_all() allocates din and dout correctly.
1306 		 */
1307 		data = (struct ec_response_get_protocol_info *)mock->o_data;
1308 		data->max_request_packet_size = 0xbe;
1309 		data->max_response_packet_size = 0xef;
1310 	}
1311 
1312 	/* For cros_ec_get_proto_info() with passthru. */
1313 	{
1314 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1315 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1316 	}
1317 
1318 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1319 	{
1320 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1321 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1322 	}
1323 
1324 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1325 	{
1326 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1327 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1328 	}
1329 
1330 	/* For cros_ec_get_host_event_wake_mask(). */
1331 	{
1332 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1333 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1334 	}
1335 
1336 	cros_ec_proto_test_query_all_pretest(test);
1337 	ret = cros_ec_query_all(ec_dev);
1338 	KUNIT_EXPECT_EQ(test, ret, 0);
1339 
1340 	/* For cros_ec_get_proto_info() without passthru. */
1341 	{
1342 		mock = cros_kunit_ec_xfer_mock_next();
1343 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1344 
1345 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1346 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1347 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1348 				sizeof(struct ec_response_get_protocol_info));
1349 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1350 	}
1351 
1352 	/* For cros_ec_get_proto_info() with passthru. */
1353 	{
1354 		mock = cros_kunit_ec_xfer_mock_next();
1355 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1356 
1357 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1358 		KUNIT_EXPECT_EQ(test, mock->msg.command,
1359 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
1360 				EC_CMD_GET_PROTOCOL_INFO);
1361 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1362 				sizeof(struct ec_response_get_protocol_info));
1363 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1364 	}
1365 
1366 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1367 	{
1368 		mock = cros_kunit_ec_xfer_mock_next();
1369 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1370 
1371 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1372 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1373 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1374 				sizeof(struct ec_response_get_cmd_versions));
1375 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1376 	}
1377 
1378 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1379 	{
1380 		mock = cros_kunit_ec_xfer_mock_next();
1381 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1382 
1383 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1384 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1385 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1386 				sizeof(struct ec_response_get_cmd_versions));
1387 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1388 	}
1389 
1390 	/* For cros_ec_get_host_event_wake_mask(). */
1391 	{
1392 		u32 mask;
1393 
1394 		mock = cros_kunit_ec_xfer_mock_next();
1395 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1396 
1397 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1398 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
1399 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
1400 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1401 
1402 		mask = ec_dev->host_event_wake_mask;
1403 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), 0);
1404 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED), 0);
1405 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW), 0);
1406 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL), 0);
1407 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY), 0);
1408 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU), 0);
1409 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS), 0);
1410 	}
1411 }
1412 
cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit * test)1413 static void cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit *test)
1414 {
1415 	struct cros_ec_proto_test_priv *priv = test->priv;
1416 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1417 	struct ec_xfer_mock *mock;
1418 	int ret;
1419 
1420 	/* Set some garbage bytes. */
1421 	ec_dev->host_event_wake_mask = U32_MAX;
1422 
1423 	/* For cros_ec_get_proto_info() without passthru. */
1424 	{
1425 		struct ec_response_get_protocol_info *data;
1426 
1427 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1428 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1429 
1430 		/*
1431 		 * Although it doesn't check the value, provides valid sizes so that
1432 		 * cros_ec_query_all() allocates din and dout correctly.
1433 		 */
1434 		data = (struct ec_response_get_protocol_info *)mock->o_data;
1435 		data->max_request_packet_size = 0xbe;
1436 		data->max_response_packet_size = 0xef;
1437 	}
1438 
1439 	/* For cros_ec_get_proto_info() with passthru. */
1440 	{
1441 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1442 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1443 	}
1444 
1445 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1446 	{
1447 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1448 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1449 	}
1450 
1451 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1452 	{
1453 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1454 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1455 	}
1456 
1457 	/* For get_host_event_wake_mask(). */
1458 	{
1459 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1460 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1461 	}
1462 
1463 	cros_ec_proto_test_query_all_pretest(test);
1464 	ret = cros_ec_query_all(ec_dev);
1465 	KUNIT_EXPECT_EQ(test, ret, 0);
1466 
1467 	/* For cros_ec_get_proto_info() without passthru. */
1468 	{
1469 		mock = cros_kunit_ec_xfer_mock_next();
1470 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1471 
1472 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1473 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1474 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1475 				sizeof(struct ec_response_get_protocol_info));
1476 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1477 	}
1478 
1479 	/* For cros_ec_get_proto_info() with passthru. */
1480 	{
1481 		mock = cros_kunit_ec_xfer_mock_next();
1482 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1483 
1484 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1485 		KUNIT_EXPECT_EQ(test, mock->msg.command,
1486 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
1487 				EC_CMD_GET_PROTOCOL_INFO);
1488 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1489 				sizeof(struct ec_response_get_protocol_info));
1490 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1491 	}
1492 
1493 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1494 	{
1495 		mock = cros_kunit_ec_xfer_mock_next();
1496 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1497 
1498 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1499 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1500 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1501 				sizeof(struct ec_response_get_cmd_versions));
1502 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1503 	}
1504 
1505 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1506 	{
1507 		mock = cros_kunit_ec_xfer_mock_next();
1508 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1509 
1510 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1511 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1512 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1513 				sizeof(struct ec_response_get_cmd_versions));
1514 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1515 	}
1516 
1517 	/* For get_host_event_wake_mask(). */
1518 	{
1519 		u32 mask;
1520 
1521 		mock = cros_kunit_ec_xfer_mock_next();
1522 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1523 
1524 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1525 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
1526 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
1527 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1528 
1529 		mask = ec_dev->host_event_wake_mask;
1530 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), 0);
1531 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED), 0);
1532 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW), 0);
1533 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL), 0);
1534 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY), 0);
1535 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU), 0);
1536 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS), 0);
1537 	}
1538 }
1539 
cros_ec_proto_test_cmd_xfer_normal(struct kunit * test)1540 static void cros_ec_proto_test_cmd_xfer_normal(struct kunit *test)
1541 {
1542 	struct cros_ec_proto_test_priv *priv = test->priv;
1543 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1544 	struct ec_xfer_mock *mock;
1545 	int ret;
1546 	struct {
1547 		struct cros_ec_command msg;
1548 		u8 data[0x100];
1549 	} __packed buf;
1550 
1551 	ec_dev->max_request = 0xff;
1552 	ec_dev->max_response = 0xee;
1553 	ec_dev->max_passthru = 0xdd;
1554 
1555 	buf.msg.version = 0;
1556 	buf.msg.command = EC_CMD_HELLO;
1557 	buf.msg.insize = 4;
1558 	buf.msg.outsize = 2;
1559 	buf.data[0] = 0x55;
1560 	buf.data[1] = 0xaa;
1561 
1562 	{
1563 		u8 *data;
1564 
1565 		mock = cros_kunit_ec_xfer_mock_add(test, 4);
1566 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1567 
1568 		data = (u8 *)mock->o_data;
1569 		data[0] = 0xaa;
1570 		data[1] = 0x55;
1571 		data[2] = 0xcc;
1572 		data[3] = 0x33;
1573 	}
1574 
1575 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
1576 	KUNIT_EXPECT_EQ(test, ret, 4);
1577 
1578 	{
1579 		u8 *data;
1580 
1581 		mock = cros_kunit_ec_xfer_mock_next();
1582 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1583 
1584 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1585 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
1586 		KUNIT_EXPECT_EQ(test, mock->msg.insize, 4);
1587 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 2);
1588 
1589 		data = (u8 *)mock->i_data;
1590 		KUNIT_EXPECT_EQ(test, data[0], 0x55);
1591 		KUNIT_EXPECT_EQ(test, data[1], 0xaa);
1592 
1593 		KUNIT_EXPECT_EQ(test, buf.data[0], 0xaa);
1594 		KUNIT_EXPECT_EQ(test, buf.data[1], 0x55);
1595 		KUNIT_EXPECT_EQ(test, buf.data[2], 0xcc);
1596 		KUNIT_EXPECT_EQ(test, buf.data[3], 0x33);
1597 	}
1598 }
1599 
cros_ec_proto_test_cmd_xfer_excess_msg_insize(struct kunit * test)1600 static void cros_ec_proto_test_cmd_xfer_excess_msg_insize(struct kunit *test)
1601 {
1602 	struct cros_ec_proto_test_priv *priv = test->priv;
1603 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1604 	struct ec_xfer_mock *mock;
1605 	int ret;
1606 	struct {
1607 		struct cros_ec_command msg;
1608 		u8 data[0x100];
1609 	} __packed buf;
1610 
1611 	ec_dev->max_request = 0xff;
1612 	ec_dev->max_response = 0xee;
1613 	ec_dev->max_passthru = 0xdd;
1614 
1615 	buf.msg.version = 0;
1616 	buf.msg.command = EC_CMD_HELLO;
1617 	buf.msg.insize = 0xee + 1;
1618 	buf.msg.outsize = 2;
1619 
1620 	{
1621 		mock = cros_kunit_ec_xfer_mock_add(test, 0xcc);
1622 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1623 	}
1624 
1625 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
1626 	KUNIT_EXPECT_EQ(test, ret, 0xcc);
1627 
1628 	{
1629 		mock = cros_kunit_ec_xfer_mock_next();
1630 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1631 
1632 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1633 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
1634 		KUNIT_EXPECT_EQ(test, mock->msg.insize, 0xee);
1635 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 2);
1636 	}
1637 }
1638 
cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru(struct kunit * test)1639 static void cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru(struct kunit *test)
1640 {
1641 	struct cros_ec_proto_test_priv *priv = test->priv;
1642 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1643 	int ret;
1644 	struct {
1645 		struct cros_ec_command msg;
1646 		u8 data[0x100];
1647 	} __packed buf;
1648 
1649 	ec_dev->max_request = 0xff;
1650 	ec_dev->max_response = 0xee;
1651 	ec_dev->max_passthru = 0xdd;
1652 
1653 	buf.msg.version = 0;
1654 	buf.msg.command = EC_CMD_HELLO;
1655 	buf.msg.insize = 4;
1656 	buf.msg.outsize = 0xff + 1;
1657 
1658 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
1659 	KUNIT_EXPECT_EQ(test, ret, -EMSGSIZE);
1660 }
1661 
cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru(struct kunit * test)1662 static void cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru(struct kunit *test)
1663 {
1664 	struct cros_ec_proto_test_priv *priv = test->priv;
1665 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1666 	int ret;
1667 	struct {
1668 		struct cros_ec_command msg;
1669 		u8 data[0x100];
1670 	} __packed buf;
1671 
1672 	ec_dev->max_request = 0xff;
1673 	ec_dev->max_response = 0xee;
1674 	ec_dev->max_passthru = 0xdd;
1675 
1676 	buf.msg.version = 0;
1677 	buf.msg.command = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) + EC_CMD_HELLO;
1678 	buf.msg.insize = 4;
1679 	buf.msg.outsize = 0xdd + 1;
1680 
1681 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
1682 	KUNIT_EXPECT_EQ(test, ret, -EMSGSIZE);
1683 }
1684 
cros_ec_proto_test_cmd_xfer_protocol_v3_normal(struct kunit * test)1685 static void cros_ec_proto_test_cmd_xfer_protocol_v3_normal(struct kunit *test)
1686 {
1687 	struct cros_ec_proto_test_priv *priv = test->priv;
1688 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1689 	int ret;
1690 	struct cros_ec_command msg;
1691 
1692 	memset(&msg, 0, sizeof(msg));
1693 
1694 	ec_dev->proto_version = 3;
1695 	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
1696 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1697 
1698 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1699 	KUNIT_EXPECT_EQ(test, ret, 0);
1700 
1701 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 0);
1702 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 1);
1703 }
1704 
cros_ec_proto_test_cmd_xfer_protocol_v3_no_op(struct kunit * test)1705 static void cros_ec_proto_test_cmd_xfer_protocol_v3_no_op(struct kunit *test)
1706 {
1707 	struct cros_ec_proto_test_priv *priv = test->priv;
1708 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1709 	int ret;
1710 	struct cros_ec_command msg;
1711 
1712 	memset(&msg, 0, sizeof(msg));
1713 
1714 	ec_dev->proto_version = 3;
1715 	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
1716 	ec_dev->pkt_xfer = NULL;
1717 
1718 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1719 	KUNIT_EXPECT_EQ(test, ret, -EIO);
1720 }
1721 
cros_ec_proto_test_cmd_xfer_protocol_v2_normal(struct kunit * test)1722 static void cros_ec_proto_test_cmd_xfer_protocol_v2_normal(struct kunit *test)
1723 {
1724 	struct cros_ec_proto_test_priv *priv = test->priv;
1725 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1726 	int ret;
1727 	struct cros_ec_command msg;
1728 
1729 	memset(&msg, 0, sizeof(msg));
1730 
1731 	ec_dev->proto_version = 2;
1732 	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
1733 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1734 
1735 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1736 	KUNIT_EXPECT_EQ(test, ret, 0);
1737 
1738 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 1);
1739 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 0);
1740 }
1741 
cros_ec_proto_test_cmd_xfer_protocol_v2_no_op(struct kunit * test)1742 static void cros_ec_proto_test_cmd_xfer_protocol_v2_no_op(struct kunit *test)
1743 {
1744 	struct cros_ec_proto_test_priv *priv = test->priv;
1745 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1746 	int ret;
1747 	struct cros_ec_command msg;
1748 
1749 	memset(&msg, 0, sizeof(msg));
1750 
1751 	ec_dev->proto_version = 2;
1752 	ec_dev->cmd_xfer = NULL;
1753 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1754 
1755 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1756 	KUNIT_EXPECT_EQ(test, ret, -EIO);
1757 }
1758 
cros_ec_proto_test_cmd_xfer_in_progress_normal(struct kunit * test)1759 static void cros_ec_proto_test_cmd_xfer_in_progress_normal(struct kunit *test)
1760 {
1761 	struct cros_ec_proto_test_priv *priv = test->priv;
1762 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1763 	struct ec_xfer_mock *mock;
1764 	int ret;
1765 	struct cros_ec_command msg;
1766 
1767 	memset(&msg, 0, sizeof(msg));
1768 
1769 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1770 
1771 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1772 	{
1773 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1774 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1775 	}
1776 
1777 	/* For EC_CMD_GET_COMMS_STATUS. */
1778 	{
1779 		struct ec_response_get_comms_status *data;
1780 
1781 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1782 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1783 
1784 		data = (struct ec_response_get_comms_status *)mock->o_data;
1785 		data->flags = 0;
1786 	}
1787 
1788 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1789 	KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_comms_status));
1790 
1791 	KUNIT_EXPECT_EQ(test, msg.result, EC_RES_SUCCESS);
1792 
1793 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1794 	{
1795 		mock = cros_kunit_ec_xfer_mock_next();
1796 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1797 	}
1798 
1799 	/* For EC_CMD_GET_COMMS_STATUS. */
1800 	{
1801 		mock = cros_kunit_ec_xfer_mock_next();
1802 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1803 
1804 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1805 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_COMMS_STATUS);
1806 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1807 				sizeof(struct ec_response_get_comms_status));
1808 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1809 	}
1810 
1811 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
1812 }
1813 
cros_ec_proto_test_cmd_xfer_in_progress_retries_eagain(struct kunit * test)1814 static void cros_ec_proto_test_cmd_xfer_in_progress_retries_eagain(struct kunit *test)
1815 {
1816 	struct cros_ec_proto_test_priv *priv = test->priv;
1817 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1818 	struct ec_xfer_mock *mock;
1819 	int ret;
1820 	struct cros_ec_command msg;
1821 
1822 	memset(&msg, 0, sizeof(msg));
1823 
1824 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1825 
1826 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1827 	{
1828 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1829 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1830 	}
1831 
1832 	/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
1833 	cros_kunit_ec_xfer_mock_default_ret = -EAGAIN;
1834 
1835 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1836 	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
1837 
1838 	/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
1839 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 51);
1840 }
1841 
cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing(struct kunit * test)1842 static void cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing(struct kunit *test)
1843 {
1844 	struct cros_ec_proto_test_priv *priv = test->priv;
1845 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1846 	struct ec_xfer_mock *mock;
1847 	int ret;
1848 	struct cros_ec_command msg;
1849 
1850 	memset(&msg, 0, sizeof(msg));
1851 
1852 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1853 
1854 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1855 	{
1856 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1857 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1858 	}
1859 
1860 	/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
1861 	{
1862 		struct ec_response_get_comms_status *data;
1863 		int i;
1864 
1865 		for (i = 0; i < 50; ++i) {
1866 			mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1867 			KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1868 
1869 			data = (struct ec_response_get_comms_status *)mock->o_data;
1870 			data->flags |= EC_COMMS_STATUS_PROCESSING;
1871 		}
1872 	}
1873 
1874 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1875 	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
1876 
1877 	/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
1878 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 51);
1879 }
1880 
cros_ec_proto_test_cmd_xfer_in_progress_xfer_error(struct kunit * test)1881 static void cros_ec_proto_test_cmd_xfer_in_progress_xfer_error(struct kunit *test)
1882 {
1883 	struct cros_ec_proto_test_priv *priv = test->priv;
1884 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1885 	struct ec_xfer_mock *mock;
1886 	int ret;
1887 	struct cros_ec_command msg;
1888 
1889 	memset(&msg, 0, sizeof(msg));
1890 
1891 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1892 	{
1893 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1894 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1895 	}
1896 
1897 	/* For EC_CMD_GET_COMMS_STATUS. */
1898 	{
1899 		mock = cros_kunit_ec_xfer_mock_addx(test, -EIO, EC_RES_SUCCESS, 0);
1900 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1901 	}
1902 
1903 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1904 	KUNIT_EXPECT_EQ(test, ret, -EIO);
1905 }
1906 
cros_ec_proto_test_cmd_xfer_in_progress_return_error(struct kunit * test)1907 static void cros_ec_proto_test_cmd_xfer_in_progress_return_error(struct kunit *test)
1908 {
1909 	struct cros_ec_proto_test_priv *priv = test->priv;
1910 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1911 	struct ec_xfer_mock *mock;
1912 	int ret;
1913 	struct cros_ec_command msg;
1914 
1915 	memset(&msg, 0, sizeof(msg));
1916 
1917 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1918 
1919 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1920 	{
1921 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1922 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1923 	}
1924 
1925 	/* For EC_CMD_GET_COMMS_STATUS. */
1926 	{
1927 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1928 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1929 	}
1930 
1931 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1932 	KUNIT_EXPECT_EQ(test, ret, 0);
1933 
1934 	KUNIT_EXPECT_EQ(test, msg.result, EC_RES_INVALID_COMMAND);
1935 
1936 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
1937 }
1938 
cros_ec_proto_test_cmd_xfer_in_progress_return0(struct kunit * test)1939 static void cros_ec_proto_test_cmd_xfer_in_progress_return0(struct kunit *test)
1940 {
1941 	struct cros_ec_proto_test_priv *priv = test->priv;
1942 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1943 	struct ec_xfer_mock *mock;
1944 	int ret;
1945 	struct cros_ec_command msg;
1946 
1947 	memset(&msg, 0, sizeof(msg));
1948 
1949 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1950 
1951 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1952 	{
1953 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1954 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1955 	}
1956 
1957 	/* For EC_CMD_GET_COMMS_STATUS. */
1958 	{
1959 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1960 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1961 	}
1962 
1963 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1964 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
1965 
1966 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
1967 }
1968 
cros_ec_proto_test_cmd_xfer_status_normal(struct kunit * test)1969 static void cros_ec_proto_test_cmd_xfer_status_normal(struct kunit *test)
1970 {
1971 	struct cros_ec_proto_test_priv *priv = test->priv;
1972 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1973 	struct ec_xfer_mock *mock;
1974 	int ret;
1975 	struct cros_ec_command msg;
1976 
1977 	memset(&msg, 0, sizeof(msg));
1978 
1979 	/* For cros_ec_cmd_xfer(). */
1980 	{
1981 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1982 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1983 	}
1984 
1985 	ret = cros_ec_cmd_xfer_status(ec_dev, &msg);
1986 	KUNIT_EXPECT_EQ(test, ret, 0);
1987 }
1988 
cros_ec_proto_test_cmd_xfer_status_xfer_error(struct kunit * test)1989 static void cros_ec_proto_test_cmd_xfer_status_xfer_error(struct kunit *test)
1990 {
1991 	struct cros_ec_proto_test_priv *priv = test->priv;
1992 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1993 	struct ec_xfer_mock *mock;
1994 	int ret;
1995 	struct cros_ec_command msg;
1996 
1997 	memset(&msg, 0, sizeof(msg));
1998 
1999 	/* For cros_ec_cmd_xfer(). */
2000 	{
2001 		mock = cros_kunit_ec_xfer_mock_addx(test, -EPROTO, EC_RES_SUCCESS, 0);
2002 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2003 	}
2004 
2005 	ret = cros_ec_cmd_xfer_status(ec_dev, &msg);
2006 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
2007 }
2008 
cros_ec_proto_test_cmd_xfer_status_return_error(struct kunit * test)2009 static void cros_ec_proto_test_cmd_xfer_status_return_error(struct kunit *test)
2010 {
2011 	struct cros_ec_proto_test_priv *priv = test->priv;
2012 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2013 	struct ec_xfer_mock *mock;
2014 	int ret, i;
2015 	struct cros_ec_command msg;
2016 	static const int map[] = {
2017 		[EC_RES_SUCCESS] = 0,
2018 		[EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
2019 		[EC_RES_ERROR] = -EIO,
2020 		[EC_RES_INVALID_PARAM] = -EINVAL,
2021 		[EC_RES_ACCESS_DENIED] = -EACCES,
2022 		[EC_RES_INVALID_RESPONSE] = -EPROTO,
2023 		[EC_RES_INVALID_VERSION] = -ENOPROTOOPT,
2024 		[EC_RES_INVALID_CHECKSUM] = -EBADMSG,
2025 		/*
2026 		 * EC_RES_IN_PROGRESS is special because cros_ec_send_command() has extra logic to
2027 		 * handle it.  Note that default cros_kunit_ec_xfer_mock_default_ret == 0 thus
2028 		 * cros_ec_xfer_command() in cros_ec_wait_until_complete() returns 0.  As a result,
2029 		 * it returns -EPROTO without calling cros_ec_map_error().
2030 		 */
2031 		[EC_RES_IN_PROGRESS] = -EPROTO,
2032 		[EC_RES_UNAVAILABLE] = -ENODATA,
2033 		[EC_RES_TIMEOUT] = -ETIMEDOUT,
2034 		[EC_RES_OVERFLOW] = -EOVERFLOW,
2035 		[EC_RES_INVALID_HEADER] = -EBADR,
2036 		[EC_RES_REQUEST_TRUNCATED] = -EBADR,
2037 		[EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
2038 		[EC_RES_BUS_ERROR] = -EFAULT,
2039 		[EC_RES_BUSY] = -EBUSY,
2040 		[EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
2041 		[EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
2042 		[EC_RES_INVALID_DATA_CRC] = -EBADMSG,
2043 		[EC_RES_DUP_UNAVAILABLE] = -ENODATA,
2044 	};
2045 
2046 	memset(&msg, 0, sizeof(msg));
2047 
2048 	for (i = 0; i < ARRAY_SIZE(map); ++i) {
2049 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, i, 0);
2050 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2051 
2052 		ret = cros_ec_cmd_xfer_status(ec_dev, &msg);
2053 		KUNIT_EXPECT_EQ(test, ret, map[i]);
2054 	}
2055 }
2056 
cros_ec_proto_test_get_next_event_no_mkbp_event(struct kunit * test)2057 static void cros_ec_proto_test_get_next_event_no_mkbp_event(struct kunit *test)
2058 {
2059 	struct cros_ec_proto_test_priv *priv = test->priv;
2060 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2061 	struct ec_xfer_mock *mock;
2062 	int ret;
2063 	bool wake_event, more_events;
2064 
2065 	ec_dev->max_request = 0xff;
2066 	ec_dev->max_response = 0xee;
2067 	ec_dev->mkbp_event_supported = 0;
2068 
2069 	/* Set some garbage bytes. */
2070 	wake_event = false;
2071 	more_events = true;
2072 
2073 	/* For get_keyboard_state_event(). */
2074 	{
2075 		union ec_response_get_next_data_v1 *data;
2076 
2077 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
2078 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2079 
2080 		data = (union ec_response_get_next_data_v1 *)mock->o_data;
2081 		data->host_event = 0xbeef;
2082 	}
2083 
2084 	ret = cros_ec_get_next_event(ec_dev, &wake_event, &more_events);
2085 	KUNIT_EXPECT_EQ(test, ret, sizeof(union ec_response_get_next_data_v1));
2086 
2087 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_KEY_MATRIX);
2088 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.data.host_event, 0xbeef);
2089 
2090 	KUNIT_EXPECT_TRUE(test, wake_event);
2091 	KUNIT_EXPECT_FALSE(test, more_events);
2092 
2093 	/* For get_keyboard_state_event(). */
2094 	{
2095 		mock = cros_kunit_ec_xfer_mock_next();
2096 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2097 
2098 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
2099 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_MKBP_STATE);
2100 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(union ec_response_get_next_data_v1));
2101 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
2102 	}
2103 }
2104 
cros_ec_proto_test_get_next_event_mkbp_event_ec_suspended(struct kunit * test)2105 static void cros_ec_proto_test_get_next_event_mkbp_event_ec_suspended(struct kunit *test)
2106 {
2107 	struct cros_ec_proto_test_priv *priv = test->priv;
2108 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2109 	int ret;
2110 
2111 	ec_dev->mkbp_event_supported = 1;
2112 	ec_dev->suspended = true;
2113 
2114 	ret = cros_ec_get_next_event(ec_dev, NULL, NULL);
2115 	KUNIT_EXPECT_EQ(test, ret, -EHOSTDOWN);
2116 }
2117 
cros_ec_proto_test_get_next_event_mkbp_event_version0(struct kunit * test)2118 static void cros_ec_proto_test_get_next_event_mkbp_event_version0(struct kunit *test)
2119 {
2120 	struct cros_ec_proto_test_priv *priv = test->priv;
2121 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2122 	struct ec_xfer_mock *mock;
2123 	int ret;
2124 	bool wake_event, more_events;
2125 
2126 	ec_dev->max_request = 0xff;
2127 	ec_dev->max_response = 0xee;
2128 	ec_dev->mkbp_event_supported = 1;
2129 
2130 	/* Set some garbage bytes. */
2131 	wake_event = true;
2132 	more_events = false;
2133 
2134 	/* For get_next_event_xfer(). */
2135 	{
2136 		struct ec_response_get_next_event *data;
2137 
2138 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
2139 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2140 
2141 		data = (struct ec_response_get_next_event *)mock->o_data;
2142 		data->event_type = EC_MKBP_EVENT_SENSOR_FIFO | EC_MKBP_HAS_MORE_EVENTS;
2143 		data->data.sysrq = 0xbeef;
2144 	}
2145 
2146 	ret = cros_ec_get_next_event(ec_dev, &wake_event, &more_events);
2147 	KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_next_event));
2148 
2149 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_SENSOR_FIFO);
2150 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.data.sysrq, 0xbeef);
2151 
2152 	KUNIT_EXPECT_FALSE(test, wake_event);
2153 	KUNIT_EXPECT_TRUE(test, more_events);
2154 
2155 	/* For get_next_event_xfer(). */
2156 	{
2157 		mock = cros_kunit_ec_xfer_mock_next();
2158 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2159 
2160 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
2161 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_NEXT_EVENT);
2162 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_get_next_event));
2163 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
2164 	}
2165 }
2166 
cros_ec_proto_test_get_next_event_mkbp_event_version2(struct kunit * test)2167 static void cros_ec_proto_test_get_next_event_mkbp_event_version2(struct kunit *test)
2168 {
2169 	struct cros_ec_proto_test_priv *priv = test->priv;
2170 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2171 	struct ec_xfer_mock *mock;
2172 	int ret;
2173 	bool wake_event, more_events;
2174 
2175 	ec_dev->max_request = 0xff;
2176 	ec_dev->max_response = 0xee;
2177 	ec_dev->mkbp_event_supported = 3;
2178 
2179 	/* Set some garbage bytes. */
2180 	wake_event = false;
2181 	more_events = true;
2182 
2183 	/* For get_next_event_xfer(). */
2184 	{
2185 		struct ec_response_get_next_event_v1 *data;
2186 
2187 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
2188 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2189 
2190 		data = (struct ec_response_get_next_event_v1 *)mock->o_data;
2191 		data->event_type = EC_MKBP_EVENT_FINGERPRINT;
2192 		data->data.sysrq = 0xbeef;
2193 	}
2194 
2195 	ret = cros_ec_get_next_event(ec_dev, &wake_event, &more_events);
2196 	KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_next_event_v1));
2197 
2198 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_FINGERPRINT);
2199 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.data.sysrq, 0xbeef);
2200 
2201 	KUNIT_EXPECT_TRUE(test, wake_event);
2202 	KUNIT_EXPECT_FALSE(test, more_events);
2203 
2204 	/* For get_next_event_xfer(). */
2205 	{
2206 		mock = cros_kunit_ec_xfer_mock_next();
2207 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2208 
2209 		KUNIT_EXPECT_EQ(test, mock->msg.version, 2);
2210 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_NEXT_EVENT);
2211 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
2212 				sizeof(struct ec_response_get_next_event_v1));
2213 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
2214 	}
2215 }
2216 
cros_ec_proto_test_get_next_event_mkbp_event_host_event_rtc(struct kunit * test)2217 static void cros_ec_proto_test_get_next_event_mkbp_event_host_event_rtc(struct kunit *test)
2218 {
2219 	struct cros_ec_proto_test_priv *priv = test->priv;
2220 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2221 	struct ec_xfer_mock *mock;
2222 	int ret;
2223 	bool wake_event;
2224 	struct ec_response_get_next_event_v1 *data;
2225 
2226 	ec_dev->max_request = 0xff;
2227 	ec_dev->max_response = 0xee;
2228 	ec_dev->mkbp_event_supported = 3;
2229 	ec_dev->host_event_wake_mask = U32_MAX;
2230 
2231 	/* Set some garbage bytes. */
2232 	wake_event = true;
2233 
2234 	/* For get_next_event_xfer(). */
2235 	{
2236 		mock = cros_kunit_ec_xfer_mock_add(test,
2237 						   sizeof(data->event_type) +
2238 						   sizeof(data->data.host_event));
2239 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2240 
2241 		data = (struct ec_response_get_next_event_v1 *)mock->o_data;
2242 		data->event_type = EC_MKBP_EVENT_HOST_EVENT;
2243 		put_unaligned_le32(EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC), &data->data.host_event);
2244 	}
2245 
2246 	ret = cros_ec_get_next_event(ec_dev, &wake_event, NULL);
2247 	KUNIT_EXPECT_EQ(test, ret, sizeof(data->event_type) + sizeof(data->data.host_event));
2248 
2249 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_HOST_EVENT);
2250 
2251 	KUNIT_EXPECT_FALSE(test, wake_event);
2252 
2253 	/* For get_next_event_xfer(). */
2254 	{
2255 		mock = cros_kunit_ec_xfer_mock_next();
2256 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2257 
2258 		KUNIT_EXPECT_EQ(test, mock->msg.version, 2);
2259 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_NEXT_EVENT);
2260 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
2261 				sizeof(struct ec_response_get_next_event_v1));
2262 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
2263 	}
2264 }
2265 
cros_ec_proto_test_get_next_event_mkbp_event_host_event_masked(struct kunit * test)2266 static void cros_ec_proto_test_get_next_event_mkbp_event_host_event_masked(struct kunit *test)
2267 {
2268 	struct cros_ec_proto_test_priv *priv = test->priv;
2269 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2270 	struct ec_xfer_mock *mock;
2271 	int ret;
2272 	bool wake_event;
2273 	struct ec_response_get_next_event_v1 *data;
2274 
2275 	ec_dev->max_request = 0xff;
2276 	ec_dev->max_response = 0xee;
2277 	ec_dev->mkbp_event_supported = 3;
2278 	ec_dev->host_event_wake_mask = U32_MAX & ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED);
2279 
2280 	/* Set some garbage bytes. */
2281 	wake_event = true;
2282 
2283 	/* For get_next_event_xfer(). */
2284 	{
2285 		mock = cros_kunit_ec_xfer_mock_add(test,
2286 						   sizeof(data->event_type) +
2287 						   sizeof(data->data.host_event));
2288 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2289 
2290 		data = (struct ec_response_get_next_event_v1 *)mock->o_data;
2291 		data->event_type = EC_MKBP_EVENT_HOST_EVENT;
2292 		put_unaligned_le32(EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED),
2293 				   &data->data.host_event);
2294 	}
2295 
2296 	ret = cros_ec_get_next_event(ec_dev, &wake_event, NULL);
2297 	KUNIT_EXPECT_EQ(test, ret, sizeof(data->event_type) + sizeof(data->data.host_event));
2298 
2299 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_HOST_EVENT);
2300 
2301 	KUNIT_EXPECT_FALSE(test, wake_event);
2302 
2303 	/* For get_next_event_xfer(). */
2304 	{
2305 		mock = cros_kunit_ec_xfer_mock_next();
2306 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2307 
2308 		KUNIT_EXPECT_EQ(test, mock->msg.version, 2);
2309 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_NEXT_EVENT);
2310 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
2311 				sizeof(struct ec_response_get_next_event_v1));
2312 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
2313 	}
2314 }
2315 
cros_ec_proto_test_get_host_event_no_mkbp_event(struct kunit * test)2316 static void cros_ec_proto_test_get_host_event_no_mkbp_event(struct kunit *test)
2317 {
2318 	struct cros_ec_proto_test_priv *priv = test->priv;
2319 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2320 	int ret;
2321 
2322 	ec_dev->mkbp_event_supported = 0;
2323 
2324 	ret = cros_ec_get_host_event(ec_dev);
2325 	KUNIT_EXPECT_EQ(test, ret, 0);
2326 }
2327 
cros_ec_proto_test_get_host_event_not_host_event(struct kunit * test)2328 static void cros_ec_proto_test_get_host_event_not_host_event(struct kunit *test)
2329 {
2330 	struct cros_ec_proto_test_priv *priv = test->priv;
2331 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2332 	int ret;
2333 
2334 	ec_dev->mkbp_event_supported = 1;
2335 	ec_dev->event_data.event_type = EC_MKBP_EVENT_FINGERPRINT;
2336 
2337 	ret = cros_ec_get_host_event(ec_dev);
2338 	KUNIT_EXPECT_EQ(test, ret, 0);
2339 }
2340 
cros_ec_proto_test_get_host_event_wrong_event_size(struct kunit * test)2341 static void cros_ec_proto_test_get_host_event_wrong_event_size(struct kunit *test)
2342 {
2343 	struct cros_ec_proto_test_priv *priv = test->priv;
2344 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2345 	int ret;
2346 
2347 	ec_dev->mkbp_event_supported = 1;
2348 	ec_dev->event_data.event_type = EC_MKBP_EVENT_HOST_EVENT;
2349 	ec_dev->event_size = 0xff;
2350 
2351 	ret = cros_ec_get_host_event(ec_dev);
2352 	KUNIT_EXPECT_EQ(test, ret, 0);
2353 }
2354 
cros_ec_proto_test_get_host_event_normal(struct kunit * test)2355 static void cros_ec_proto_test_get_host_event_normal(struct kunit *test)
2356 {
2357 	struct cros_ec_proto_test_priv *priv = test->priv;
2358 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2359 	int ret;
2360 
2361 	ec_dev->mkbp_event_supported = 1;
2362 	ec_dev->event_data.event_type = EC_MKBP_EVENT_HOST_EVENT;
2363 	ec_dev->event_size = sizeof(ec_dev->event_data.data.host_event);
2364 	put_unaligned_le32(EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC),
2365 			   &ec_dev->event_data.data.host_event);
2366 
2367 	ret = cros_ec_get_host_event(ec_dev);
2368 	KUNIT_EXPECT_EQ(test, ret, EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC));
2369 }
2370 
cros_ec_proto_test_check_features_cached(struct kunit * test)2371 static void cros_ec_proto_test_check_features_cached(struct kunit *test)
2372 {
2373 	int ret, i;
2374 	static struct cros_ec_dev ec;
2375 
2376 	ec.features.flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FINGERPRINT);
2377 	ec.features.flags[1] = EC_FEATURE_MASK_0(EC_FEATURE_SCP);
2378 
2379 	for (i = 0; i < EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK; ++i) {
2380 		ret = cros_ec_check_features(&ec, i);
2381 		switch (i) {
2382 		case EC_FEATURE_FINGERPRINT:
2383 		case EC_FEATURE_SCP:
2384 			KUNIT_EXPECT_TRUE(test, ret);
2385 			break;
2386 		default:
2387 			KUNIT_EXPECT_FALSE(test, ret);
2388 			break;
2389 		}
2390 	}
2391 }
2392 
cros_ec_proto_test_check_features_not_cached(struct kunit * test)2393 static void cros_ec_proto_test_check_features_not_cached(struct kunit *test)
2394 {
2395 	struct cros_ec_proto_test_priv *priv = test->priv;
2396 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2397 	struct ec_xfer_mock *mock;
2398 	int ret, i;
2399 	static struct cros_ec_dev ec;
2400 
2401 	ec_dev->max_request = 0xff;
2402 	ec_dev->max_response = 0xee;
2403 	ec.ec_dev = ec_dev;
2404 	ec.dev = ec_dev->dev;
2405 	ec.cmd_offset = 0;
2406 	ec.features.flags[0] = -1;
2407 	ec.features.flags[1] = -1;
2408 
2409 	/* For EC_CMD_GET_FEATURES. */
2410 	{
2411 		struct ec_response_get_features *data;
2412 
2413 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
2414 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2415 
2416 		data = (struct ec_response_get_features *)mock->o_data;
2417 		data->flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FINGERPRINT);
2418 		data->flags[1] = EC_FEATURE_MASK_0(EC_FEATURE_SCP);
2419 	}
2420 
2421 	for (i = 0; i < EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK; ++i) {
2422 		ret = cros_ec_check_features(&ec, i);
2423 		switch (i) {
2424 		case EC_FEATURE_FINGERPRINT:
2425 		case EC_FEATURE_SCP:
2426 			KUNIT_EXPECT_TRUE(test, ret);
2427 			break;
2428 		default:
2429 			KUNIT_EXPECT_FALSE(test, ret);
2430 			break;
2431 		}
2432 	}
2433 
2434 	/* For EC_CMD_GET_FEATURES. */
2435 	{
2436 		mock = cros_kunit_ec_xfer_mock_next();
2437 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2438 
2439 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
2440 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_FEATURES);
2441 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_get_features));
2442 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
2443 	}
2444 }
2445 
cros_ec_proto_test_get_sensor_count_normal(struct kunit * test)2446 static void cros_ec_proto_test_get_sensor_count_normal(struct kunit *test)
2447 {
2448 	struct cros_ec_proto_test_priv *priv = test->priv;
2449 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2450 	struct ec_xfer_mock *mock;
2451 	int ret;
2452 	static struct cros_ec_dev ec;
2453 
2454 	ec_dev->max_request = 0xff;
2455 	ec_dev->max_response = 0xee;
2456 	ec.ec_dev = ec_dev;
2457 	ec.dev = ec_dev->dev;
2458 	ec.cmd_offset = 0;
2459 
2460 	/* For EC_CMD_MOTION_SENSE_CMD. */
2461 	{
2462 		struct ec_response_motion_sense *data;
2463 
2464 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
2465 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2466 
2467 		data = (struct ec_response_motion_sense *)mock->o_data;
2468 		data->dump.sensor_count = 0xbf;
2469 	}
2470 
2471 	ret = cros_ec_get_sensor_count(&ec);
2472 	KUNIT_EXPECT_EQ(test, ret, 0xbf);
2473 
2474 	/* For EC_CMD_MOTION_SENSE_CMD. */
2475 	{
2476 		struct ec_params_motion_sense *data;
2477 
2478 		mock = cros_kunit_ec_xfer_mock_next();
2479 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2480 
2481 		KUNIT_EXPECT_EQ(test, mock->msg.version, 1);
2482 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_MOTION_SENSE_CMD);
2483 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_motion_sense));
2484 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
2485 
2486 		data = (struct ec_params_motion_sense *)mock->i_data;
2487 		KUNIT_EXPECT_EQ(test, data->cmd, MOTIONSENSE_CMD_DUMP);
2488 	}
2489 }
2490 
cros_ec_proto_test_get_sensor_count_xfer_error(struct kunit * test)2491 static void cros_ec_proto_test_get_sensor_count_xfer_error(struct kunit *test)
2492 {
2493 	struct cros_ec_proto_test_priv *priv = test->priv;
2494 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2495 	struct ec_xfer_mock *mock;
2496 	int ret;
2497 	static struct cros_ec_dev ec;
2498 
2499 	ec_dev->max_request = 0xff;
2500 	ec_dev->max_response = 0xee;
2501 	ec.ec_dev = ec_dev;
2502 	ec.dev = ec_dev->dev;
2503 	ec.cmd_offset = 0;
2504 
2505 	/* For EC_CMD_MOTION_SENSE_CMD. */
2506 	{
2507 		mock = cros_kunit_ec_xfer_mock_addx(test, -EPROTO, EC_RES_SUCCESS, 0);
2508 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2509 	}
2510 
2511 	ret = cros_ec_get_sensor_count(&ec);
2512 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
2513 
2514 	/* For EC_CMD_MOTION_SENSE_CMD. */
2515 	{
2516 		struct ec_params_motion_sense *data;
2517 
2518 		mock = cros_kunit_ec_xfer_mock_next();
2519 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2520 
2521 		KUNIT_EXPECT_EQ(test, mock->msg.version, 1);
2522 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_MOTION_SENSE_CMD);
2523 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_motion_sense));
2524 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
2525 
2526 		data = (struct ec_params_motion_sense *)mock->i_data;
2527 		KUNIT_EXPECT_EQ(test, data->cmd, MOTIONSENSE_CMD_DUMP);
2528 	}
2529 }
2530 
cros_ec_proto_test_get_sensor_count_legacy(struct kunit * test)2531 static void cros_ec_proto_test_get_sensor_count_legacy(struct kunit *test)
2532 {
2533 	struct cros_ec_proto_test_priv *priv = test->priv;
2534 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2535 	struct ec_xfer_mock *mock;
2536 	int ret, i;
2537 	static struct cros_ec_dev ec;
2538 	struct {
2539 		u8 readmem_data;
2540 		int expected_result;
2541 	} test_data[] = {
2542 		{ 0, 0 },
2543 		{ EC_MEMMAP_ACC_STATUS_PRESENCE_BIT, 2 },
2544 	};
2545 
2546 	ec_dev->max_request = 0xff;
2547 	ec_dev->max_response = 0xee;
2548 	ec_dev->cmd_readmem = cros_kunit_readmem_mock;
2549 	ec.ec_dev = ec_dev;
2550 	ec.dev = ec_dev->dev;
2551 	ec.cmd_offset = 0;
2552 
2553 	for (i = 0; i < ARRAY_SIZE(test_data); ++i) {
2554 		/* For EC_CMD_MOTION_SENSE_CMD. */
2555 		{
2556 			mock = cros_kunit_ec_xfer_mock_addx(test, -EPROTO, EC_RES_SUCCESS, 0);
2557 			KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2558 		}
2559 
2560 		/* For readmem. */
2561 		{
2562 			cros_kunit_readmem_mock_data = kunit_kzalloc(test, 1, GFP_KERNEL);
2563 			KUNIT_ASSERT_PTR_NE(test, cros_kunit_readmem_mock_data, NULL);
2564 			cros_kunit_readmem_mock_data[0] = test_data[i].readmem_data;
2565 
2566 			cros_kunit_ec_xfer_mock_default_ret = 1;
2567 		}
2568 
2569 		ret = cros_ec_get_sensor_count(&ec);
2570 		KUNIT_EXPECT_EQ(test, ret, test_data[i].expected_result);
2571 
2572 		/* For EC_CMD_MOTION_SENSE_CMD. */
2573 		{
2574 			struct ec_params_motion_sense *data;
2575 
2576 			mock = cros_kunit_ec_xfer_mock_next();
2577 			KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2578 
2579 			KUNIT_EXPECT_EQ(test, mock->msg.version, 1);
2580 			KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_MOTION_SENSE_CMD);
2581 			KUNIT_EXPECT_EQ(test, mock->msg.insize,
2582 					sizeof(struct ec_response_motion_sense));
2583 			KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
2584 
2585 			data = (struct ec_params_motion_sense *)mock->i_data;
2586 			KUNIT_EXPECT_EQ(test, data->cmd, MOTIONSENSE_CMD_DUMP);
2587 		}
2588 
2589 		/* For readmem. */
2590 		{
2591 			KUNIT_EXPECT_EQ(test, cros_kunit_readmem_mock_offset, EC_MEMMAP_ACC_STATUS);
2592 		}
2593 	}
2594 }
2595 
cros_ec_proto_test_ec_cmd(struct kunit * test)2596 static void cros_ec_proto_test_ec_cmd(struct kunit *test)
2597 {
2598 	struct cros_ec_proto_test_priv *priv = test->priv;
2599 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2600 	struct ec_xfer_mock *mock;
2601 	int ret;
2602 	u8 out[3], in[2];
2603 
2604 	ec_dev->max_request = 0xff;
2605 	ec_dev->max_response = 0xee;
2606 
2607 	out[0] = 0xdd;
2608 	out[1] = 0xcc;
2609 	out[2] = 0xbb;
2610 
2611 	{
2612 		u8 *data;
2613 
2614 		mock = cros_kunit_ec_xfer_mock_add(test, 2);
2615 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
2616 
2617 		data = (u8 *)mock->o_data;
2618 		data[0] = 0xaa;
2619 		data[1] = 0x99;
2620 	}
2621 
2622 	ret = cros_ec_cmd(ec_dev, 0x88, 0x77, out, ARRAY_SIZE(out), in, ARRAY_SIZE(in));
2623 	KUNIT_EXPECT_EQ(test, ret, 2);
2624 
2625 	{
2626 		u8 *data;
2627 
2628 		mock = cros_kunit_ec_xfer_mock_next();
2629 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
2630 
2631 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0x88);
2632 		KUNIT_EXPECT_EQ(test, mock->msg.command, 0x77);
2633 		KUNIT_EXPECT_EQ(test, mock->msg.insize, ARRAY_SIZE(in));
2634 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, ARRAY_SIZE(out));
2635 
2636 		data = (u8 *)mock->i_data;
2637 		KUNIT_EXPECT_EQ(test, data[0], 0xdd);
2638 		KUNIT_EXPECT_EQ(test, data[1], 0xcc);
2639 		KUNIT_EXPECT_EQ(test, data[2], 0xbb);
2640 	}
2641 }
2642 
cros_ec_proto_test_release(struct device * dev)2643 static void cros_ec_proto_test_release(struct device *dev)
2644 {
2645 }
2646 
cros_ec_proto_test_init(struct kunit * test)2647 static int cros_ec_proto_test_init(struct kunit *test)
2648 {
2649 	struct cros_ec_proto_test_priv *priv;
2650 	struct cros_ec_device *ec_dev;
2651 
2652 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
2653 	if (!priv)
2654 		return -ENOMEM;
2655 	test->priv = priv;
2656 
2657 	ec_dev = &priv->ec_dev;
2658 	ec_dev->dout = (u8 *)priv->dout;
2659 	ec_dev->dout_size = ARRAY_SIZE(priv->dout);
2660 	ec_dev->din = (u8 *)priv->din;
2661 	ec_dev->din_size = ARRAY_SIZE(priv->din);
2662 	ec_dev->proto_version = EC_HOST_REQUEST_VERSION;
2663 	ec_dev->dev = kunit_kzalloc(test, sizeof(*ec_dev->dev), GFP_KERNEL);
2664 	if (!ec_dev->dev)
2665 		return -ENOMEM;
2666 	device_initialize(ec_dev->dev);
2667 	dev_set_name(ec_dev->dev, "cros_ec_proto_test");
2668 	ec_dev->dev->release = cros_ec_proto_test_release;
2669 	ec_dev->cmd_xfer = cros_kunit_ec_xfer_mock;
2670 	ec_dev->pkt_xfer = cros_kunit_ec_xfer_mock;
2671 	mutex_init(&ec_dev->lock);
2672 
2673 	priv->msg = (struct cros_ec_command *)priv->_msg;
2674 
2675 	cros_kunit_mock_reset();
2676 
2677 	return 0;
2678 }
2679 
cros_ec_proto_test_exit(struct kunit * test)2680 static void cros_ec_proto_test_exit(struct kunit *test)
2681 {
2682 	struct cros_ec_proto_test_priv *priv = test->priv;
2683 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2684 
2685 	put_device(ec_dev->dev);
2686 }
2687 
2688 static struct kunit_case cros_ec_proto_test_cases[] = {
2689 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_legacy_normal),
2690 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize),
2691 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_normal),
2692 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_bad_msg_outsize),
2693 	KUNIT_CASE(cros_ec_proto_test_check_result),
2694 	KUNIT_CASE(cros_ec_proto_test_query_all_normal),
2695 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return_error),
2696 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return0),
2697 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return_error),
2698 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return0),
2699 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_xfer_error),
2700 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return_error),
2701 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_data_error),
2702 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return0),
2703 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp),
2704 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp_return_error),
2705 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp_return0),
2706 	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep),
2707 	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep_return0),
2708 	KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return_error),
2709 	KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return0),
2710 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_normal),
2711 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_insize),
2712 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru),
2713 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru),
2714 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v3_normal),
2715 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v3_no_op),
2716 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v2_normal),
2717 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v2_no_op),
2718 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_normal),
2719 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_retries_eagain),
2720 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing),
2721 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_xfer_error),
2722 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return_error),
2723 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return0),
2724 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_status_normal),
2725 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_status_xfer_error),
2726 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_status_return_error),
2727 	KUNIT_CASE(cros_ec_proto_test_get_next_event_no_mkbp_event),
2728 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_ec_suspended),
2729 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_version0),
2730 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_version2),
2731 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_host_event_rtc),
2732 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_host_event_masked),
2733 	KUNIT_CASE(cros_ec_proto_test_get_host_event_no_mkbp_event),
2734 	KUNIT_CASE(cros_ec_proto_test_get_host_event_not_host_event),
2735 	KUNIT_CASE(cros_ec_proto_test_get_host_event_wrong_event_size),
2736 	KUNIT_CASE(cros_ec_proto_test_get_host_event_normal),
2737 	KUNIT_CASE(cros_ec_proto_test_check_features_cached),
2738 	KUNIT_CASE(cros_ec_proto_test_check_features_not_cached),
2739 	KUNIT_CASE(cros_ec_proto_test_get_sensor_count_normal),
2740 	KUNIT_CASE(cros_ec_proto_test_get_sensor_count_xfer_error),
2741 	KUNIT_CASE(cros_ec_proto_test_get_sensor_count_legacy),
2742 	KUNIT_CASE(cros_ec_proto_test_ec_cmd),
2743 	{}
2744 };
2745 
2746 static struct kunit_suite cros_ec_proto_test_suite = {
2747 	.name = "cros_ec_proto_test",
2748 	.init = cros_ec_proto_test_init,
2749 	.exit = cros_ec_proto_test_exit,
2750 	.test_cases = cros_ec_proto_test_cases,
2751 };
2752 
2753 kunit_test_suite(cros_ec_proto_test_suite);
2754 
2755 MODULE_LICENSE("GPL");
2756