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 <linux/platform_data/cros_ec_commands.h>
9 #include <linux/platform_data/cros_ec_proto.h>
10 
11 #include "cros_ec.h"
12 #include "cros_kunit_util.h"
13 
14 #define BUFSIZE 512
15 
16 struct cros_ec_proto_test_priv {
17 	struct cros_ec_device ec_dev;
18 	u8 dout[BUFSIZE];
19 	u8 din[BUFSIZE];
20 	struct cros_ec_command *msg;
21 	u8 _msg[BUFSIZE];
22 };
23 
24 static void cros_ec_proto_test_prepare_tx_legacy_normal(struct kunit *test)
25 {
26 	struct cros_ec_proto_test_priv *priv = test->priv;
27 	struct cros_ec_device *ec_dev = &priv->ec_dev;
28 	struct cros_ec_command *msg = priv->msg;
29 	int ret, i;
30 	u8 csum;
31 
32 	ec_dev->proto_version = 2;
33 
34 	msg->command = EC_CMD_HELLO;
35 	msg->outsize = EC_PROTO2_MAX_PARAM_SIZE;
36 	msg->data[0] = 0xde;
37 	msg->data[1] = 0xad;
38 	msg->data[2] = 0xbe;
39 	msg->data[3] = 0xef;
40 
41 	ret = cros_ec_prepare_tx(ec_dev, msg);
42 
43 	KUNIT_EXPECT_EQ(test, ret, EC_MSG_TX_PROTO_BYTES + EC_PROTO2_MAX_PARAM_SIZE);
44 	KUNIT_EXPECT_EQ(test, ec_dev->dout[0], EC_CMD_VERSION0);
45 	KUNIT_EXPECT_EQ(test, ec_dev->dout[1], EC_CMD_HELLO);
46 	KUNIT_EXPECT_EQ(test, ec_dev->dout[2], EC_PROTO2_MAX_PARAM_SIZE);
47 	KUNIT_EXPECT_EQ(test, EC_MSG_TX_HEADER_BYTES, 3);
48 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 0], 0xde);
49 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 1], 0xad);
50 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 2], 0xbe);
51 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 3], 0xef);
52 	for (i = 4; i < EC_PROTO2_MAX_PARAM_SIZE; ++i)
53 		KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + i], 0);
54 
55 	csum = EC_CMD_VERSION0;
56 	csum += EC_CMD_HELLO;
57 	csum += EC_PROTO2_MAX_PARAM_SIZE;
58 	csum += 0xde;
59 	csum += 0xad;
60 	csum += 0xbe;
61 	csum += 0xef;
62 	KUNIT_EXPECT_EQ(test,
63 			ec_dev->dout[EC_MSG_TX_HEADER_BYTES + EC_PROTO2_MAX_PARAM_SIZE],
64 			csum);
65 }
66 
67 static void cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize(struct kunit *test)
68 {
69 	struct cros_ec_proto_test_priv *priv = test->priv;
70 	struct cros_ec_device *ec_dev = &priv->ec_dev;
71 	struct cros_ec_command *msg = priv->msg;
72 	int ret;
73 
74 	ec_dev->proto_version = 2;
75 
76 	msg->outsize = EC_PROTO2_MAX_PARAM_SIZE + 1;
77 
78 	ret = cros_ec_prepare_tx(ec_dev, msg);
79 	KUNIT_EXPECT_EQ(test, ret, -EINVAL);
80 }
81 
82 static void cros_ec_proto_test_prepare_tx_normal(struct kunit *test)
83 {
84 	struct cros_ec_proto_test_priv *priv = test->priv;
85 	struct cros_ec_device *ec_dev = &priv->ec_dev;
86 	struct cros_ec_command *msg = priv->msg;
87 	struct ec_host_request *request = (struct ec_host_request *)ec_dev->dout;
88 	int ret, i;
89 	u8 csum;
90 
91 	msg->command = EC_CMD_HELLO;
92 	msg->outsize = 0x88;
93 	msg->data[0] = 0xde;
94 	msg->data[1] = 0xad;
95 	msg->data[2] = 0xbe;
96 	msg->data[3] = 0xef;
97 
98 	ret = cros_ec_prepare_tx(ec_dev, msg);
99 
100 	KUNIT_EXPECT_EQ(test, ret, sizeof(*request) + 0x88);
101 
102 	KUNIT_EXPECT_EQ(test, request->struct_version, EC_HOST_REQUEST_VERSION);
103 	KUNIT_EXPECT_EQ(test, request->command, EC_CMD_HELLO);
104 	KUNIT_EXPECT_EQ(test, request->command_version, 0);
105 	KUNIT_EXPECT_EQ(test, request->data_len, 0x88);
106 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 0], 0xde);
107 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 1], 0xad);
108 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 2], 0xbe);
109 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 3], 0xef);
110 	for (i = 4; i < 0x88; ++i)
111 		KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + i], 0);
112 
113 	csum = EC_HOST_REQUEST_VERSION;
114 	csum += EC_CMD_HELLO;
115 	csum += 0x88;
116 	csum += 0xde;
117 	csum += 0xad;
118 	csum += 0xbe;
119 	csum += 0xef;
120 	KUNIT_EXPECT_EQ(test, request->checksum, (u8)-csum);
121 }
122 
123 static void cros_ec_proto_test_prepare_tx_bad_msg_outsize(struct kunit *test)
124 {
125 	struct cros_ec_proto_test_priv *priv = test->priv;
126 	struct cros_ec_device *ec_dev = &priv->ec_dev;
127 	struct cros_ec_command *msg = priv->msg;
128 	int ret;
129 
130 	msg->outsize = ec_dev->dout_size - sizeof(struct ec_host_request) + 1;
131 
132 	ret = cros_ec_prepare_tx(ec_dev, msg);
133 	KUNIT_EXPECT_EQ(test, ret, -EINVAL);
134 }
135 
136 static void cros_ec_proto_test_check_result(struct kunit *test)
137 {
138 	struct cros_ec_proto_test_priv *priv = test->priv;
139 	struct cros_ec_device *ec_dev = &priv->ec_dev;
140 	struct cros_ec_command *msg = priv->msg;
141 	int ret, i;
142 	static enum ec_status status[] = {
143 		EC_RES_SUCCESS,
144 		EC_RES_INVALID_COMMAND,
145 		EC_RES_ERROR,
146 		EC_RES_INVALID_PARAM,
147 		EC_RES_ACCESS_DENIED,
148 		EC_RES_INVALID_RESPONSE,
149 		EC_RES_INVALID_VERSION,
150 		EC_RES_INVALID_CHECKSUM,
151 		EC_RES_UNAVAILABLE,
152 		EC_RES_TIMEOUT,
153 		EC_RES_OVERFLOW,
154 		EC_RES_INVALID_HEADER,
155 		EC_RES_REQUEST_TRUNCATED,
156 		EC_RES_RESPONSE_TOO_BIG,
157 		EC_RES_BUS_ERROR,
158 		EC_RES_BUSY,
159 		EC_RES_INVALID_HEADER_VERSION,
160 		EC_RES_INVALID_HEADER_CRC,
161 		EC_RES_INVALID_DATA_CRC,
162 		EC_RES_DUP_UNAVAILABLE,
163 	};
164 
165 	for (i = 0; i < ARRAY_SIZE(status); ++i) {
166 		msg->result = status[i];
167 		ret = cros_ec_check_result(ec_dev, msg);
168 		KUNIT_EXPECT_EQ(test, ret, 0);
169 	}
170 
171 	msg->result = EC_RES_IN_PROGRESS;
172 	ret = cros_ec_check_result(ec_dev, msg);
173 	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
174 }
175 
176 static void cros_ec_proto_test_query_all_pretest(struct kunit *test)
177 {
178 	struct cros_ec_proto_test_priv *priv = test->priv;
179 	struct cros_ec_device *ec_dev = &priv->ec_dev;
180 
181 	/*
182 	 * cros_ec_query_all() will free din and dout and allocate them again to fit the usage by
183 	 * calling devm_kfree() and devm_kzalloc().  Set them to NULL as they aren't managed by
184 	 * ec_dev->dev but allocated statically in struct cros_ec_proto_test_priv
185 	 * (see cros_ec_proto_test_init()).
186 	 */
187 	ec_dev->din = NULL;
188 	ec_dev->dout = NULL;
189 }
190 
191 static void cros_ec_proto_test_query_all_normal(struct kunit *test)
192 {
193 	struct cros_ec_proto_test_priv *priv = test->priv;
194 	struct cros_ec_device *ec_dev = &priv->ec_dev;
195 	struct ec_xfer_mock *mock;
196 	int ret;
197 
198 	/* For cros_ec_get_proto_info() without passthru. */
199 	{
200 		struct ec_response_get_protocol_info *data;
201 
202 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
203 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
204 
205 		data = (struct ec_response_get_protocol_info *)mock->o_data;
206 		data->protocol_versions = BIT(3) | BIT(2);
207 		data->max_request_packet_size = 0xbe;
208 		data->max_response_packet_size = 0xef;
209 	}
210 
211 	/* For cros_ec_get_proto_info() with passthru. */
212 	{
213 		struct ec_response_get_protocol_info *data;
214 
215 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
216 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
217 
218 		data = (struct ec_response_get_protocol_info *)mock->o_data;
219 		data->max_request_packet_size = 0xbf;
220 	}
221 
222 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
223 	{
224 		struct ec_response_get_cmd_versions *data;
225 
226 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
227 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
228 
229 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
230 		data->version_mask = BIT(6) | BIT(5);
231 	}
232 
233 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
234 	{
235 		struct ec_response_get_cmd_versions *data;
236 
237 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
238 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
239 
240 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
241 		data->version_mask = BIT(1);
242 	}
243 
244 	/* For cros_ec_get_host_event_wake_mask(). */
245 	{
246 		struct ec_response_host_event_mask *data;
247 
248 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
249 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
250 
251 		data = (struct ec_response_host_event_mask *)mock->o_data;
252 		data->mask = 0xbeef;
253 	}
254 
255 	cros_ec_proto_test_query_all_pretest(test);
256 	ret = cros_ec_query_all(ec_dev);
257 	KUNIT_EXPECT_EQ(test, ret, 0);
258 
259 	/* For cros_ec_get_proto_info() without passthru. */
260 	{
261 		mock = cros_kunit_ec_xfer_mock_next();
262 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
263 
264 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
265 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
266 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
267 				sizeof(struct ec_response_get_protocol_info));
268 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
269 
270 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, 0xbe - sizeof(struct ec_host_request));
271 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, 0xef - sizeof(struct ec_host_response));
272 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 3);
273 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, 0xef + EC_MAX_RESPONSE_OVERHEAD);
274 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, 0xbe + EC_MAX_REQUEST_OVERHEAD);
275 	}
276 
277 	/* For cros_ec_get_proto_info() with passthru. */
278 	{
279 		mock = cros_kunit_ec_xfer_mock_next();
280 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
281 
282 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
283 		KUNIT_EXPECT_EQ(test, mock->msg.command,
284 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
285 				EC_CMD_GET_PROTOCOL_INFO);
286 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
287 				sizeof(struct ec_response_get_protocol_info));
288 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
289 
290 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0xbf - sizeof(struct ec_host_request));
291 	}
292 
293 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
294 	{
295 		struct ec_params_get_cmd_versions *data;
296 
297 		mock = cros_kunit_ec_xfer_mock_next();
298 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
299 
300 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
301 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
302 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
303 				sizeof(struct ec_response_get_cmd_versions));
304 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
305 
306 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
307 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
308 
309 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 7);
310 	}
311 
312 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
313 	{
314 		struct ec_params_get_cmd_versions *data;
315 
316 		mock = cros_kunit_ec_xfer_mock_next();
317 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
318 
319 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
320 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
321 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
322 				sizeof(struct ec_response_get_cmd_versions));
323 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
324 
325 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
326 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_HOST_SLEEP_EVENT);
327 
328 		KUNIT_EXPECT_TRUE(test, ec_dev->host_sleep_v1);
329 	}
330 
331 	/* For cros_ec_get_host_event_wake_mask(). */
332 	{
333 		mock = cros_kunit_ec_xfer_mock_next();
334 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
335 
336 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
337 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
338 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
339 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
340 
341 		KUNIT_EXPECT_EQ(test, ec_dev->host_event_wake_mask, 0xbeef);
342 	}
343 }
344 
345 static void cros_ec_proto_test_query_all_no_pd_return_error(struct kunit *test)
346 {
347 	struct cros_ec_proto_test_priv *priv = test->priv;
348 	struct cros_ec_device *ec_dev = &priv->ec_dev;
349 	struct ec_xfer_mock *mock;
350 	int ret;
351 
352 	/* Set some garbage bytes. */
353 	ec_dev->max_passthru = 0xbf;
354 
355 	/* For cros_ec_get_proto_info() without passthru. */
356 	{
357 		struct ec_response_get_protocol_info *data;
358 
359 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
360 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
361 
362 		/*
363 		 * Although it doesn't check the value, provides valid sizes so that
364 		 * cros_ec_query_all() allocates din and dout correctly.
365 		 */
366 		data = (struct ec_response_get_protocol_info *)mock->o_data;
367 		data->max_request_packet_size = 0xbe;
368 		data->max_response_packet_size = 0xef;
369 	}
370 
371 	/* For cros_ec_get_proto_info() with passthru. */
372 	{
373 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
374 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
375 	}
376 
377 	cros_ec_proto_test_query_all_pretest(test);
378 	ret = cros_ec_query_all(ec_dev);
379 	KUNIT_EXPECT_EQ(test, ret, 0);
380 
381 	/* For cros_ec_get_proto_info() without passthru. */
382 	{
383 		mock = cros_kunit_ec_xfer_mock_next();
384 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
385 
386 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
387 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
388 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
389 				sizeof(struct ec_response_get_protocol_info));
390 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
391 	}
392 
393 	/* For cros_ec_get_proto_info() with passthru. */
394 	{
395 		mock = cros_kunit_ec_xfer_mock_next();
396 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
397 
398 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
399 		KUNIT_EXPECT_EQ(test, mock->msg.command,
400 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
401 				EC_CMD_GET_PROTOCOL_INFO);
402 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
403 				sizeof(struct ec_response_get_protocol_info));
404 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
405 
406 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
407 	}
408 }
409 
410 static void cros_ec_proto_test_query_all_no_pd_return0(struct kunit *test)
411 {
412 	struct cros_ec_proto_test_priv *priv = test->priv;
413 	struct cros_ec_device *ec_dev = &priv->ec_dev;
414 	struct ec_xfer_mock *mock;
415 	int ret;
416 
417 	/* Set some garbage bytes. */
418 	ec_dev->max_passthru = 0xbf;
419 
420 	/* For cros_ec_get_proto_info() without passthru. */
421 	{
422 		struct ec_response_get_protocol_info *data;
423 
424 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
425 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
426 
427 		/*
428 		 * Although it doesn't check the value, provides valid sizes so that
429 		 * cros_ec_query_all() allocates din and dout correctly.
430 		 */
431 		data = (struct ec_response_get_protocol_info *)mock->o_data;
432 		data->max_request_packet_size = 0xbe;
433 		data->max_response_packet_size = 0xef;
434 	}
435 
436 	/* For cros_ec_get_proto_info() with passthru. */
437 	{
438 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
439 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
440 	}
441 
442 	cros_ec_proto_test_query_all_pretest(test);
443 	ret = cros_ec_query_all(ec_dev);
444 	KUNIT_EXPECT_EQ(test, ret, 0);
445 
446 	/* For cros_ec_get_proto_info() without passthru. */
447 	{
448 		mock = cros_kunit_ec_xfer_mock_next();
449 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
450 
451 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
452 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
453 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
454 				sizeof(struct ec_response_get_protocol_info));
455 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
456 	}
457 
458 	/* For cros_ec_get_proto_info() with passthru. */
459 	{
460 		mock = cros_kunit_ec_xfer_mock_next();
461 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
462 
463 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
464 		KUNIT_EXPECT_EQ(test, mock->msg.command,
465 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
466 				EC_CMD_GET_PROTOCOL_INFO);
467 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
468 				sizeof(struct ec_response_get_protocol_info));
469 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
470 
471 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
472 	}
473 }
474 
475 static void cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct kunit *test)
476 {
477 	struct cros_ec_proto_test_priv *priv = test->priv;
478 	struct cros_ec_device *ec_dev = &priv->ec_dev;
479 	struct ec_xfer_mock *mock;
480 	int ret;
481 
482 	/* For cros_ec_get_proto_info() without passthru. */
483 	{
484 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
485 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
486 	}
487 
488 	/* For cros_ec_get_proto_info_legacy(). */
489 	{
490 		struct ec_response_hello *data;
491 
492 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
493 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
494 
495 		data = (struct ec_response_hello *)mock->o_data;
496 		data->out_data = 0xa1b2c3d4;
497 	}
498 
499 	cros_ec_proto_test_query_all_pretest(test);
500 	ret = cros_ec_query_all(ec_dev);
501 	KUNIT_EXPECT_EQ(test, ret, 0);
502 
503 	/* For cros_ec_get_proto_info() without passthru. */
504 	{
505 		mock = cros_kunit_ec_xfer_mock_next();
506 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
507 
508 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
509 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
510 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
511 				sizeof(struct ec_response_get_protocol_info));
512 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
513 	}
514 
515 	/* For cros_ec_get_proto_info_legacy(). */
516 	{
517 		struct ec_params_hello *data;
518 
519 		mock = cros_kunit_ec_xfer_mock_next();
520 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
521 
522 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
523 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
524 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
525 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
526 
527 		data = (struct ec_params_hello *)mock->i_data;
528 		KUNIT_EXPECT_EQ(test, data->in_data, 0xa0b0c0d0);
529 
530 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 2);
531 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, EC_PROTO2_MAX_PARAM_SIZE);
532 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, EC_PROTO2_MAX_PARAM_SIZE);
533 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
534 		KUNIT_EXPECT_PTR_EQ(test, ec_dev->pkt_xfer, NULL);
535 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, EC_PROTO2_MSG_BYTES);
536 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, EC_PROTO2_MSG_BYTES);
537 	}
538 }
539 
540 static void cros_ec_proto_test_query_all_legacy_normal_v3_return0(struct kunit *test)
541 {
542 	struct cros_ec_proto_test_priv *priv = test->priv;
543 	struct cros_ec_device *ec_dev = &priv->ec_dev;
544 	struct ec_xfer_mock *mock;
545 	int ret;
546 
547 	/* For cros_ec_get_proto_info() without passthru. */
548 	{
549 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
550 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
551 	}
552 
553 	/* For cros_ec_get_proto_info_legacy(). */
554 	{
555 		struct ec_response_hello *data;
556 
557 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
558 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
559 
560 		data = (struct ec_response_hello *)mock->o_data;
561 		data->out_data = 0xa1b2c3d4;
562 	}
563 
564 	cros_ec_proto_test_query_all_pretest(test);
565 	ret = cros_ec_query_all(ec_dev);
566 	KUNIT_EXPECT_EQ(test, ret, 0);
567 
568 	/* For cros_ec_get_proto_info() without passthru. */
569 	{
570 		mock = cros_kunit_ec_xfer_mock_next();
571 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
572 
573 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
574 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
575 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
576 				sizeof(struct ec_response_get_protocol_info));
577 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
578 	}
579 
580 	/* For cros_ec_get_proto_info_legacy(). */
581 	{
582 		struct ec_params_hello *data;
583 
584 		mock = cros_kunit_ec_xfer_mock_next();
585 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
586 
587 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
588 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
589 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
590 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
591 
592 		data = (struct ec_params_hello *)mock->i_data;
593 		KUNIT_EXPECT_EQ(test, data->in_data, 0xa0b0c0d0);
594 
595 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 2);
596 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, EC_PROTO2_MAX_PARAM_SIZE);
597 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, EC_PROTO2_MAX_PARAM_SIZE);
598 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
599 		KUNIT_EXPECT_PTR_EQ(test, ec_dev->pkt_xfer, NULL);
600 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, EC_PROTO2_MSG_BYTES);
601 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, EC_PROTO2_MSG_BYTES);
602 	}
603 }
604 
605 static void cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit *test)
606 {
607 	struct cros_ec_proto_test_priv *priv = test->priv;
608 	struct cros_ec_device *ec_dev = &priv->ec_dev;
609 	struct ec_xfer_mock *mock;
610 	int ret;
611 
612 	/* For cros_ec_get_proto_info() without passthru. */
613 	{
614 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
615 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
616 	}
617 
618 	/* For cros_ec_get_proto_info_legacy(). */
619 	{
620 		mock = cros_kunit_ec_xfer_mock_addx(test, -EIO, EC_RES_SUCCESS, 0);
621 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
622 	}
623 
624 	cros_ec_proto_test_query_all_pretest(test);
625 	ret = cros_ec_query_all(ec_dev);
626 	KUNIT_EXPECT_EQ(test, ret, -EIO);
627 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
628 
629 	/* For cros_ec_get_proto_info() without passthru. */
630 	{
631 		mock = cros_kunit_ec_xfer_mock_next();
632 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
633 
634 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
635 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
636 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
637 				sizeof(struct ec_response_get_protocol_info));
638 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
639 	}
640 
641 	/* For cros_ec_get_proto_info_legacy(). */
642 	{
643 		mock = cros_kunit_ec_xfer_mock_next();
644 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
645 
646 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
647 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
648 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
649 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
650 	}
651 }
652 
653 static void cros_ec_proto_test_query_all_legacy_return_error(struct kunit *test)
654 {
655 	struct cros_ec_proto_test_priv *priv = test->priv;
656 	struct cros_ec_device *ec_dev = &priv->ec_dev;
657 	struct ec_xfer_mock *mock;
658 	int ret;
659 
660 	/* For cros_ec_get_proto_info() without passthru. */
661 	{
662 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
663 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
664 	}
665 
666 	/* For cros_ec_get_proto_info_legacy(). */
667 	{
668 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
669 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
670 	}
671 
672 	cros_ec_proto_test_query_all_pretest(test);
673 	ret = cros_ec_query_all(ec_dev);
674 	KUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP);
675 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
676 
677 	/* For cros_ec_get_proto_info() without passthru. */
678 	{
679 		mock = cros_kunit_ec_xfer_mock_next();
680 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
681 
682 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
683 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
684 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
685 				sizeof(struct ec_response_get_protocol_info));
686 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
687 	}
688 
689 	/* For cros_ec_get_proto_info_legacy(). */
690 	{
691 		mock = cros_kunit_ec_xfer_mock_next();
692 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
693 
694 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
695 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
696 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
697 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
698 	}
699 }
700 
701 static void cros_ec_proto_test_query_all_legacy_data_error(struct kunit *test)
702 {
703 	struct cros_ec_proto_test_priv *priv = test->priv;
704 	struct cros_ec_device *ec_dev = &priv->ec_dev;
705 	struct ec_xfer_mock *mock;
706 	int ret;
707 
708 	/* For cros_ec_get_proto_info() without passthru. */
709 	{
710 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
711 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
712 	}
713 
714 	/* For cros_ec_get_proto_info_legacy(). */
715 	{
716 		struct ec_response_hello *data;
717 
718 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
719 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
720 
721 		data = (struct ec_response_hello *)mock->o_data;
722 		data->out_data = 0xbeefbfbf;
723 	}
724 
725 	cros_ec_proto_test_query_all_pretest(test);
726 	ret = cros_ec_query_all(ec_dev);
727 	KUNIT_EXPECT_EQ(test, ret, -EBADMSG);
728 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
729 
730 	/* For cros_ec_get_proto_info() without passthru. */
731 	{
732 		mock = cros_kunit_ec_xfer_mock_next();
733 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
734 
735 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
736 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
737 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
738 				sizeof(struct ec_response_get_protocol_info));
739 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
740 	}
741 
742 	/* For cros_ec_get_proto_info_legacy(). */
743 	{
744 		mock = cros_kunit_ec_xfer_mock_next();
745 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
746 
747 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
748 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
749 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
750 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
751 	}
752 }
753 
754 static void cros_ec_proto_test_query_all_no_mkbp(struct kunit *test)
755 {
756 	struct cros_ec_proto_test_priv *priv = test->priv;
757 	struct cros_ec_device *ec_dev = &priv->ec_dev;
758 	struct ec_xfer_mock *mock;
759 	int ret;
760 
761 	/* Set some garbage bytes. */
762 	ec_dev->mkbp_event_supported = 0xbf;
763 
764 	/* For cros_ec_get_proto_info() without passthru. */
765 	{
766 		struct ec_response_get_protocol_info *data;
767 
768 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
769 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
770 
771 		/*
772 		 * Although it doesn't check the value, provides valid sizes so that
773 		 * cros_ec_query_all() allocates din and dout correctly.
774 		 */
775 		data = (struct ec_response_get_protocol_info *)mock->o_data;
776 		data->max_request_packet_size = 0xbe;
777 		data->max_response_packet_size = 0xef;
778 	}
779 
780 	/* For cros_ec_get_proto_info() with passthru. */
781 	{
782 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
783 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
784 	}
785 
786 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
787 	{
788 		struct ec_response_get_cmd_versions *data;
789 
790 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
791 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
792 
793 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
794 		data->version_mask = 0;
795 	}
796 
797 	cros_ec_proto_test_query_all_pretest(test);
798 	ret = cros_ec_query_all(ec_dev);
799 	KUNIT_EXPECT_EQ(test, ret, 0);
800 
801 	/* For cros_ec_get_proto_info() without passthru. */
802 	{
803 		mock = cros_kunit_ec_xfer_mock_next();
804 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
805 
806 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
807 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
808 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
809 				sizeof(struct ec_response_get_protocol_info));
810 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
811 	}
812 
813 	/* For cros_ec_get_proto_info() with passthru. */
814 	{
815 		mock = cros_kunit_ec_xfer_mock_next();
816 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
817 
818 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
819 		KUNIT_EXPECT_EQ(test, mock->msg.command,
820 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
821 				EC_CMD_GET_PROTOCOL_INFO);
822 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
823 				sizeof(struct ec_response_get_protocol_info));
824 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
825 	}
826 
827 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
828 	{
829 		struct ec_params_get_cmd_versions *data;
830 
831 		mock = cros_kunit_ec_xfer_mock_next();
832 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
833 
834 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
835 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
836 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
837 				sizeof(struct ec_response_get_cmd_versions));
838 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
839 
840 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
841 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
842 
843 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
844 	}
845 }
846 
847 static void cros_ec_proto_test_query_all_no_host_sleep(struct kunit *test)
848 {
849 	struct cros_ec_proto_test_priv *priv = test->priv;
850 	struct cros_ec_device *ec_dev = &priv->ec_dev;
851 	struct ec_xfer_mock *mock;
852 	int ret;
853 
854 	/* Set some garbage bytes. */
855 	ec_dev->host_sleep_v1 = true;
856 
857 	/* For cros_ec_get_proto_info() without passthru. */
858 	{
859 		struct ec_response_get_protocol_info *data;
860 
861 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
862 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
863 
864 		/*
865 		 * Although it doesn't check the value, provides valid sizes so that
866 		 * cros_ec_query_all() allocates din and dout correctly.
867 		 */
868 		data = (struct ec_response_get_protocol_info *)mock->o_data;
869 		data->max_request_packet_size = 0xbe;
870 		data->max_response_packet_size = 0xef;
871 	}
872 
873 	/* For cros_ec_get_proto_info() with passthru. */
874 	{
875 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
876 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
877 	}
878 
879 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
880 	{
881 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
882 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
883 	}
884 
885 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
886 	{
887 		struct ec_response_get_cmd_versions *data;
888 
889 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
890 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
891 
892 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
893 		data->version_mask = 0;
894 	}
895 
896 	cros_ec_proto_test_query_all_pretest(test);
897 	ret = cros_ec_query_all(ec_dev);
898 	KUNIT_EXPECT_EQ(test, ret, 0);
899 
900 	/* For cros_ec_get_proto_info() without passthru. */
901 	{
902 		mock = cros_kunit_ec_xfer_mock_next();
903 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
904 
905 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
906 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
907 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
908 				sizeof(struct ec_response_get_protocol_info));
909 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
910 	}
911 
912 	/* For cros_ec_get_proto_info() with passthru. */
913 	{
914 		mock = cros_kunit_ec_xfer_mock_next();
915 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
916 
917 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
918 		KUNIT_EXPECT_EQ(test, mock->msg.command,
919 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
920 				EC_CMD_GET_PROTOCOL_INFO);
921 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
922 				sizeof(struct ec_response_get_protocol_info));
923 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
924 	}
925 
926 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
927 	{
928 		mock = cros_kunit_ec_xfer_mock_next();
929 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
930 
931 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
932 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
933 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
934 				sizeof(struct ec_response_get_cmd_versions));
935 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
936 	}
937 
938 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
939 	{
940 		mock = cros_kunit_ec_xfer_mock_next();
941 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
942 
943 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
944 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
945 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
946 				sizeof(struct ec_response_get_cmd_versions));
947 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
948 
949 		KUNIT_EXPECT_FALSE(test, ec_dev->host_sleep_v1);
950 	}
951 }
952 
953 static void cros_ec_proto_test_query_all_default_wake_mask_return_error(struct kunit *test)
954 {
955 	struct cros_ec_proto_test_priv *priv = test->priv;
956 	struct cros_ec_device *ec_dev = &priv->ec_dev;
957 	struct ec_xfer_mock *mock;
958 	int ret;
959 
960 	/* Set some garbage bytes. */
961 	ec_dev->host_event_wake_mask = U32_MAX;
962 
963 	/* For cros_ec_get_proto_info() without passthru. */
964 	{
965 		struct ec_response_get_protocol_info *data;
966 
967 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
968 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
969 
970 		/*
971 		 * Although it doesn't check the value, provides valid sizes so that
972 		 * cros_ec_query_all() allocates din and dout correctly.
973 		 */
974 		data = (struct ec_response_get_protocol_info *)mock->o_data;
975 		data->max_request_packet_size = 0xbe;
976 		data->max_response_packet_size = 0xef;
977 	}
978 
979 	/* For cros_ec_get_proto_info() with passthru. */
980 	{
981 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
982 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
983 	}
984 
985 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
986 	{
987 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
988 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
989 	}
990 
991 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
992 	{
993 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
994 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
995 	}
996 
997 	/* For cros_ec_get_host_event_wake_mask(). */
998 	{
999 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1000 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1001 	}
1002 
1003 	cros_ec_proto_test_query_all_pretest(test);
1004 	ret = cros_ec_query_all(ec_dev);
1005 	KUNIT_EXPECT_EQ(test, ret, 0);
1006 
1007 	/* For cros_ec_get_proto_info() without passthru. */
1008 	{
1009 		mock = cros_kunit_ec_xfer_mock_next();
1010 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1011 
1012 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1013 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1014 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1015 				sizeof(struct ec_response_get_protocol_info));
1016 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1017 	}
1018 
1019 	/* For cros_ec_get_proto_info() with passthru. */
1020 	{
1021 		mock = cros_kunit_ec_xfer_mock_next();
1022 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1023 
1024 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1025 		KUNIT_EXPECT_EQ(test, mock->msg.command,
1026 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
1027 				EC_CMD_GET_PROTOCOL_INFO);
1028 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1029 				sizeof(struct ec_response_get_protocol_info));
1030 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1031 	}
1032 
1033 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1034 	{
1035 		mock = cros_kunit_ec_xfer_mock_next();
1036 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1037 
1038 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1039 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1040 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1041 				sizeof(struct ec_response_get_cmd_versions));
1042 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1043 	}
1044 
1045 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1046 	{
1047 		mock = cros_kunit_ec_xfer_mock_next();
1048 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1049 
1050 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1051 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1052 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1053 				sizeof(struct ec_response_get_cmd_versions));
1054 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1055 	}
1056 
1057 	/* For cros_ec_get_host_event_wake_mask(). */
1058 	{
1059 		u32 mask;
1060 
1061 		mock = cros_kunit_ec_xfer_mock_next();
1062 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1063 
1064 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1065 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
1066 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
1067 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1068 
1069 		mask = ec_dev->host_event_wake_mask;
1070 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), 0);
1071 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED), 0);
1072 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW), 0);
1073 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL), 0);
1074 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY), 0);
1075 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU), 0);
1076 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS), 0);
1077 	}
1078 }
1079 
1080 static void cros_ec_proto_test_release(struct device *dev)
1081 {
1082 }
1083 
1084 static int cros_ec_proto_test_init(struct kunit *test)
1085 {
1086 	struct cros_ec_proto_test_priv *priv;
1087 	struct cros_ec_device *ec_dev;
1088 
1089 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
1090 	if (!priv)
1091 		return -ENOMEM;
1092 	test->priv = priv;
1093 
1094 	ec_dev = &priv->ec_dev;
1095 	ec_dev->dout = (u8 *)priv->dout;
1096 	ec_dev->dout_size = ARRAY_SIZE(priv->dout);
1097 	ec_dev->din = (u8 *)priv->din;
1098 	ec_dev->din_size = ARRAY_SIZE(priv->din);
1099 	ec_dev->proto_version = EC_HOST_REQUEST_VERSION;
1100 	ec_dev->dev = kunit_kzalloc(test, sizeof(*ec_dev->dev), GFP_KERNEL);
1101 	if (!ec_dev->dev)
1102 		return -ENOMEM;
1103 	device_initialize(ec_dev->dev);
1104 	dev_set_name(ec_dev->dev, "cros_ec_proto_test");
1105 	ec_dev->dev->release = cros_ec_proto_test_release;
1106 	ec_dev->cmd_xfer = cros_kunit_ec_xfer_mock;
1107 	ec_dev->pkt_xfer = cros_kunit_ec_xfer_mock;
1108 
1109 	priv->msg = (struct cros_ec_command *)priv->_msg;
1110 
1111 	cros_kunit_mock_reset();
1112 
1113 	return 0;
1114 }
1115 
1116 static void cros_ec_proto_test_exit(struct kunit *test)
1117 {
1118 	struct cros_ec_proto_test_priv *priv = test->priv;
1119 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1120 
1121 	put_device(ec_dev->dev);
1122 }
1123 
1124 static struct kunit_case cros_ec_proto_test_cases[] = {
1125 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_legacy_normal),
1126 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize),
1127 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_normal),
1128 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_bad_msg_outsize),
1129 	KUNIT_CASE(cros_ec_proto_test_check_result),
1130 	KUNIT_CASE(cros_ec_proto_test_query_all_normal),
1131 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return_error),
1132 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return0),
1133 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return_error),
1134 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return0),
1135 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_xfer_error),
1136 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return_error),
1137 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_data_error),
1138 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp),
1139 	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep),
1140 	KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return_error),
1141 	{}
1142 };
1143 
1144 static struct kunit_suite cros_ec_proto_test_suite = {
1145 	.name = "cros_ec_proto_test",
1146 	.init = cros_ec_proto_test_init,
1147 	.exit = cros_ec_proto_test_exit,
1148 	.test_cases = cros_ec_proto_test_cases,
1149 };
1150 
1151 kunit_test_suite(cros_ec_proto_test_suite);
1152 
1153 MODULE_LICENSE("GPL");
1154