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_host_command_proto_query() 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_host_command_proto_query() 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_host_command_proto_query() 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_host_command_proto_query() 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(1) | EC_CMD_GET_PROTOCOL_INFO);
285 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
286 				sizeof(struct ec_response_get_protocol_info));
287 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
288 
289 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0xbf - sizeof(struct ec_host_request));
290 	}
291 
292 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
293 	{
294 		struct ec_params_get_cmd_versions *data;
295 
296 		mock = cros_kunit_ec_xfer_mock_next();
297 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
298 
299 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
300 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
301 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
302 				sizeof(struct ec_response_get_cmd_versions));
303 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
304 
305 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
306 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
307 
308 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 7);
309 	}
310 
311 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
312 	{
313 		struct ec_params_get_cmd_versions *data;
314 
315 		mock = cros_kunit_ec_xfer_mock_next();
316 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
317 
318 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
319 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
320 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
321 				sizeof(struct ec_response_get_cmd_versions));
322 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
323 
324 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
325 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_HOST_SLEEP_EVENT);
326 
327 		KUNIT_EXPECT_TRUE(test, ec_dev->host_sleep_v1);
328 	}
329 
330 	/* For cros_ec_get_host_event_wake_mask(). */
331 	{
332 		mock = cros_kunit_ec_xfer_mock_next();
333 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
334 
335 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
336 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
337 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
338 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
339 
340 		KUNIT_EXPECT_EQ(test, ec_dev->host_event_wake_mask, 0xbeef);
341 	}
342 }
343 
344 static void cros_ec_proto_test_query_all_no_pd_return_error(struct kunit *test)
345 {
346 	struct cros_ec_proto_test_priv *priv = test->priv;
347 	struct cros_ec_device *ec_dev = &priv->ec_dev;
348 	struct ec_xfer_mock *mock;
349 	int ret;
350 
351 	/* Set some garbage bytes. */
352 	ec_dev->max_passthru = 0xbf;
353 
354 	/* For cros_ec_host_command_proto_query() without passthru. */
355 	{
356 		struct ec_response_get_protocol_info *data;
357 
358 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
359 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
360 
361 		/*
362 		 * Although it doesn't check the value, provides valid sizes so that
363 		 * cros_ec_query_all() allocates din and dout correctly.
364 		 */
365 		data = (struct ec_response_get_protocol_info *)mock->o_data;
366 		data->max_request_packet_size = 0xbe;
367 		data->max_response_packet_size = 0xef;
368 	}
369 
370 	/* For cros_ec_host_command_proto_query() with passthru. */
371 	{
372 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
373 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
374 	}
375 
376 	cros_ec_proto_test_query_all_pretest(test);
377 	ret = cros_ec_query_all(ec_dev);
378 	KUNIT_EXPECT_EQ(test, ret, 0);
379 
380 	/* For cros_ec_host_command_proto_query() without passthru. */
381 	{
382 		mock = cros_kunit_ec_xfer_mock_next();
383 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
384 
385 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
386 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
387 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
388 				sizeof(struct ec_response_get_protocol_info));
389 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
390 	}
391 
392 	/* For cros_ec_host_command_proto_query() with passthru. */
393 	{
394 		mock = cros_kunit_ec_xfer_mock_next();
395 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
396 
397 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
398 		KUNIT_EXPECT_EQ(test, mock->msg.command,
399 				EC_CMD_PASSTHRU_OFFSET(1) | EC_CMD_GET_PROTOCOL_INFO);
400 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
401 				sizeof(struct ec_response_get_protocol_info));
402 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
403 
404 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
405 	}
406 }
407 
408 static void cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct kunit *test)
409 {
410 	struct cros_ec_proto_test_priv *priv = test->priv;
411 	struct cros_ec_device *ec_dev = &priv->ec_dev;
412 	struct ec_xfer_mock *mock;
413 	int ret;
414 
415 	/* For cros_ec_host_command_proto_query() without passthru. */
416 	{
417 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
418 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
419 	}
420 
421 	/* For cros_ec_host_command_proto_query_v2(). */
422 	{
423 		struct ec_response_hello *data;
424 
425 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
426 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
427 
428 		data = (struct ec_response_hello *)mock->o_data;
429 		data->out_data = 0xa1b2c3d4;
430 	}
431 
432 	cros_ec_proto_test_query_all_pretest(test);
433 	ret = cros_ec_query_all(ec_dev);
434 	KUNIT_EXPECT_EQ(test, ret, 0);
435 
436 	/* For cros_ec_host_command_proto_query() without passthru. */
437 	{
438 		mock = cros_kunit_ec_xfer_mock_next();
439 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
440 
441 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
442 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
443 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
444 				sizeof(struct ec_response_get_protocol_info));
445 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
446 	}
447 
448 	/* For cros_ec_host_command_proto_query_v2(). */
449 	{
450 		struct ec_params_hello *data;
451 
452 		mock = cros_kunit_ec_xfer_mock_next();
453 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
454 
455 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
456 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
457 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
458 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
459 
460 		data = (struct ec_params_hello *)mock->i_data;
461 		KUNIT_EXPECT_EQ(test, data->in_data, 0xa0b0c0d0);
462 
463 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 2);
464 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, EC_PROTO2_MAX_PARAM_SIZE);
465 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, EC_PROTO2_MAX_PARAM_SIZE);
466 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
467 		KUNIT_EXPECT_PTR_EQ(test, ec_dev->pkt_xfer, NULL);
468 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, EC_PROTO2_MSG_BYTES);
469 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, EC_PROTO2_MSG_BYTES);
470 	}
471 }
472 
473 static void cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit *test)
474 {
475 	struct cros_ec_proto_test_priv *priv = test->priv;
476 	struct cros_ec_device *ec_dev = &priv->ec_dev;
477 	struct ec_xfer_mock *mock;
478 	int ret;
479 
480 	/* For cros_ec_host_command_proto_query() without passthru. */
481 	{
482 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
483 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
484 	}
485 
486 	/* For cros_ec_host_command_proto_query_v2(). */
487 	{
488 		mock = cros_kunit_ec_xfer_mock_addx(test, -EIO, EC_RES_SUCCESS, 0);
489 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
490 	}
491 
492 	cros_ec_proto_test_query_all_pretest(test);
493 	ret = cros_ec_query_all(ec_dev);
494 	KUNIT_EXPECT_EQ(test, ret, -EIO);
495 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
496 
497 	/* For cros_ec_host_command_proto_query() without passthru. */
498 	{
499 		mock = cros_kunit_ec_xfer_mock_next();
500 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
501 
502 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
503 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
504 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
505 				sizeof(struct ec_response_get_protocol_info));
506 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
507 	}
508 
509 	/* For cros_ec_host_command_proto_query_v2(). */
510 	{
511 		mock = cros_kunit_ec_xfer_mock_next();
512 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
513 
514 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
515 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
516 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
517 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
518 	}
519 }
520 
521 static void cros_ec_proto_test_query_all_legacy_return_error(struct kunit *test)
522 {
523 	struct cros_ec_proto_test_priv *priv = test->priv;
524 	struct cros_ec_device *ec_dev = &priv->ec_dev;
525 	struct ec_xfer_mock *mock;
526 	int ret;
527 
528 	/* For cros_ec_host_command_proto_query() without passthru. */
529 	{
530 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
531 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
532 	}
533 
534 	/* For cros_ec_host_command_proto_query_v2(). */
535 	{
536 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
537 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
538 	}
539 
540 	cros_ec_proto_test_query_all_pretest(test);
541 	ret = cros_ec_query_all(ec_dev);
542 	KUNIT_EXPECT_EQ(test, ret, EC_RES_INVALID_COMMAND);
543 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
544 
545 	/* For cros_ec_host_command_proto_query() without passthru. */
546 	{
547 		mock = cros_kunit_ec_xfer_mock_next();
548 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
549 
550 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
551 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
552 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
553 				sizeof(struct ec_response_get_protocol_info));
554 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
555 	}
556 
557 	/* For cros_ec_host_command_proto_query_v2(). */
558 	{
559 		mock = cros_kunit_ec_xfer_mock_next();
560 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
561 
562 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
563 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
564 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
565 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
566 	}
567 }
568 
569 static void cros_ec_proto_test_query_all_legacy_data_error(struct kunit *test)
570 {
571 	struct cros_ec_proto_test_priv *priv = test->priv;
572 	struct cros_ec_device *ec_dev = &priv->ec_dev;
573 	struct ec_xfer_mock *mock;
574 	int ret;
575 
576 	/* For cros_ec_host_command_proto_query() without passthru. */
577 	{
578 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
579 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
580 	}
581 
582 	/* For cros_ec_host_command_proto_query_v2(). */
583 	{
584 		struct ec_response_hello *data;
585 
586 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
587 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
588 
589 		data = (struct ec_response_hello *)mock->o_data;
590 		data->out_data = 0xbeefbfbf;
591 	}
592 
593 	cros_ec_proto_test_query_all_pretest(test);
594 	ret = cros_ec_query_all(ec_dev);
595 	KUNIT_EXPECT_EQ(test, ret, -EBADMSG);
596 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
597 
598 	/* For cros_ec_host_command_proto_query() without passthru. */
599 	{
600 		mock = cros_kunit_ec_xfer_mock_next();
601 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
602 
603 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
604 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
605 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
606 				sizeof(struct ec_response_get_protocol_info));
607 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
608 	}
609 
610 	/* For cros_ec_host_command_proto_query_v2(). */
611 	{
612 		mock = cros_kunit_ec_xfer_mock_next();
613 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
614 
615 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
616 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
617 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
618 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
619 	}
620 }
621 
622 static void cros_ec_proto_test_query_all_no_mkbp(struct kunit *test)
623 {
624 	struct cros_ec_proto_test_priv *priv = test->priv;
625 	struct cros_ec_device *ec_dev = &priv->ec_dev;
626 	struct ec_xfer_mock *mock;
627 	int ret;
628 
629 	/* Set some garbage bytes. */
630 	ec_dev->mkbp_event_supported = 0xbf;
631 
632 	/* For cros_ec_host_command_proto_query() without passthru. */
633 	{
634 		struct ec_response_get_protocol_info *data;
635 
636 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
637 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
638 
639 		/*
640 		 * Although it doesn't check the value, provides valid sizes so that
641 		 * cros_ec_query_all() allocates din and dout correctly.
642 		 */
643 		data = (struct ec_response_get_protocol_info *)mock->o_data;
644 		data->max_request_packet_size = 0xbe;
645 		data->max_response_packet_size = 0xef;
646 	}
647 
648 	/* For cros_ec_host_command_proto_query() with passthru. */
649 	{
650 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
651 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
652 	}
653 
654 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
655 	{
656 		struct ec_response_get_cmd_versions *data;
657 
658 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
659 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
660 
661 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
662 		data->version_mask = 0;
663 	}
664 
665 	cros_ec_proto_test_query_all_pretest(test);
666 	ret = cros_ec_query_all(ec_dev);
667 	KUNIT_EXPECT_EQ(test, ret, 0);
668 
669 	/* For cros_ec_host_command_proto_query() without passthru. */
670 	{
671 		mock = cros_kunit_ec_xfer_mock_next();
672 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
673 
674 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
675 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
676 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
677 				sizeof(struct ec_response_get_protocol_info));
678 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
679 	}
680 
681 	/* For cros_ec_host_command_proto_query() with passthru. */
682 	{
683 		mock = cros_kunit_ec_xfer_mock_next();
684 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
685 
686 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
687 		KUNIT_EXPECT_EQ(test, mock->msg.command,
688 				EC_CMD_PASSTHRU_OFFSET(1) | EC_CMD_GET_PROTOCOL_INFO);
689 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
690 				sizeof(struct ec_response_get_protocol_info));
691 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
692 	}
693 
694 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
695 	{
696 		struct ec_params_get_cmd_versions *data;
697 
698 		mock = cros_kunit_ec_xfer_mock_next();
699 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
700 
701 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
702 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
703 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
704 				sizeof(struct ec_response_get_cmd_versions));
705 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
706 
707 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
708 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
709 
710 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
711 	}
712 }
713 
714 static void cros_ec_proto_test_query_all_no_host_sleep(struct kunit *test)
715 {
716 	struct cros_ec_proto_test_priv *priv = test->priv;
717 	struct cros_ec_device *ec_dev = &priv->ec_dev;
718 	struct ec_xfer_mock *mock;
719 	int ret;
720 
721 	/* Set some garbage bytes. */
722 	ec_dev->host_sleep_v1 = true;
723 
724 	/* For cros_ec_host_command_proto_query() without passthru. */
725 	{
726 		struct ec_response_get_protocol_info *data;
727 
728 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
729 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
730 
731 		/*
732 		 * Although it doesn't check the value, provides valid sizes so that
733 		 * cros_ec_query_all() allocates din and dout correctly.
734 		 */
735 		data = (struct ec_response_get_protocol_info *)mock->o_data;
736 		data->max_request_packet_size = 0xbe;
737 		data->max_response_packet_size = 0xef;
738 	}
739 
740 	/* For cros_ec_host_command_proto_query() with passthru. */
741 	{
742 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
743 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
744 	}
745 
746 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
747 	{
748 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
749 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
750 	}
751 
752 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
753 	{
754 		struct ec_response_get_cmd_versions *data;
755 
756 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
757 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
758 
759 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
760 		data->version_mask = 0;
761 	}
762 
763 	cros_ec_proto_test_query_all_pretest(test);
764 	ret = cros_ec_query_all(ec_dev);
765 	KUNIT_EXPECT_EQ(test, ret, 0);
766 
767 	/* For cros_ec_host_command_proto_query() without passthru. */
768 	{
769 		mock = cros_kunit_ec_xfer_mock_next();
770 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
771 
772 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
773 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
774 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
775 				sizeof(struct ec_response_get_protocol_info));
776 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
777 	}
778 
779 	/* For cros_ec_host_command_proto_query() with passthru. */
780 	{
781 		mock = cros_kunit_ec_xfer_mock_next();
782 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
783 
784 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
785 		KUNIT_EXPECT_EQ(test, mock->msg.command,
786 				EC_CMD_PASSTHRU_OFFSET(1) | 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_host_command_version_mask() for MKBP. */
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_GET_CMD_VERSIONS);
799 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
800 				sizeof(struct ec_response_get_cmd_versions));
801 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
802 	}
803 
804 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
805 	{
806 		mock = cros_kunit_ec_xfer_mock_next();
807 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
808 
809 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
810 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
811 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
812 				sizeof(struct ec_response_get_cmd_versions));
813 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
814 
815 		KUNIT_EXPECT_FALSE(test, ec_dev->host_sleep_v1);
816 	}
817 }
818 
819 static void cros_ec_proto_test_query_all_default_wake_mask_return_error(struct kunit *test)
820 {
821 	struct cros_ec_proto_test_priv *priv = test->priv;
822 	struct cros_ec_device *ec_dev = &priv->ec_dev;
823 	struct ec_xfer_mock *mock;
824 	int ret;
825 
826 	/* Set some garbage bytes. */
827 	ec_dev->host_event_wake_mask = U32_MAX;
828 
829 	/* For cros_ec_host_command_proto_query() without passthru. */
830 	{
831 		struct ec_response_get_protocol_info *data;
832 
833 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
834 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
835 
836 		/*
837 		 * Although it doesn't check the value, provides valid sizes so that
838 		 * cros_ec_query_all() allocates din and dout correctly.
839 		 */
840 		data = (struct ec_response_get_protocol_info *)mock->o_data;
841 		data->max_request_packet_size = 0xbe;
842 		data->max_response_packet_size = 0xef;
843 	}
844 
845 	/* For cros_ec_host_command_proto_query() with passthru. */
846 	{
847 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
848 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
849 	}
850 
851 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
852 	{
853 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
854 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
855 	}
856 
857 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
858 	{
859 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
860 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
861 	}
862 
863 	/* For cros_ec_get_host_event_wake_mask(). */
864 	{
865 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
866 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
867 	}
868 
869 	cros_ec_proto_test_query_all_pretest(test);
870 	ret = cros_ec_query_all(ec_dev);
871 	KUNIT_EXPECT_EQ(test, ret, 0);
872 
873 	/* For cros_ec_host_command_proto_query() without passthru. */
874 	{
875 		mock = cros_kunit_ec_xfer_mock_next();
876 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
877 
878 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
879 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
880 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
881 				sizeof(struct ec_response_get_protocol_info));
882 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
883 	}
884 
885 	/* For cros_ec_host_command_proto_query() with passthru. */
886 	{
887 		mock = cros_kunit_ec_xfer_mock_next();
888 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
889 
890 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
891 		KUNIT_EXPECT_EQ(test, mock->msg.command,
892 				EC_CMD_PASSTHRU_OFFSET(1) | EC_CMD_GET_PROTOCOL_INFO);
893 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
894 				sizeof(struct ec_response_get_protocol_info));
895 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
896 	}
897 
898 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
899 	{
900 		mock = cros_kunit_ec_xfer_mock_next();
901 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
902 
903 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
904 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
905 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
906 				sizeof(struct ec_response_get_cmd_versions));
907 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
908 	}
909 
910 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
911 	{
912 		mock = cros_kunit_ec_xfer_mock_next();
913 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
914 
915 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
916 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
917 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
918 				sizeof(struct ec_response_get_cmd_versions));
919 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
920 	}
921 
922 	/* For cros_ec_get_host_event_wake_mask(). */
923 	{
924 		u32 mask;
925 
926 		mock = cros_kunit_ec_xfer_mock_next();
927 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
928 
929 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
930 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
931 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
932 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
933 
934 		mask = ec_dev->host_event_wake_mask;
935 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), 0);
936 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED), 0);
937 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW), 0);
938 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL), 0);
939 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY), 0);
940 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU), 0);
941 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS), 0);
942 	}
943 }
944 
945 static void cros_ec_proto_test_release(struct device *dev)
946 {
947 }
948 
949 static int cros_ec_proto_test_init(struct kunit *test)
950 {
951 	struct cros_ec_proto_test_priv *priv;
952 	struct cros_ec_device *ec_dev;
953 
954 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
955 	if (!priv)
956 		return -ENOMEM;
957 	test->priv = priv;
958 
959 	ec_dev = &priv->ec_dev;
960 	ec_dev->dout = (u8 *)priv->dout;
961 	ec_dev->dout_size = ARRAY_SIZE(priv->dout);
962 	ec_dev->din = (u8 *)priv->din;
963 	ec_dev->din_size = ARRAY_SIZE(priv->din);
964 	ec_dev->proto_version = EC_HOST_REQUEST_VERSION;
965 	ec_dev->dev = kunit_kzalloc(test, sizeof(*ec_dev->dev), GFP_KERNEL);
966 	if (!ec_dev->dev)
967 		return -ENOMEM;
968 	device_initialize(ec_dev->dev);
969 	dev_set_name(ec_dev->dev, "cros_ec_proto_test");
970 	ec_dev->dev->release = cros_ec_proto_test_release;
971 	ec_dev->cmd_xfer = cros_kunit_ec_xfer_mock;
972 	ec_dev->pkt_xfer = cros_kunit_ec_xfer_mock;
973 
974 	priv->msg = (struct cros_ec_command *)priv->_msg;
975 
976 	cros_kunit_mock_reset();
977 
978 	return 0;
979 }
980 
981 static void cros_ec_proto_test_exit(struct kunit *test)
982 {
983 	struct cros_ec_proto_test_priv *priv = test->priv;
984 	struct cros_ec_device *ec_dev = &priv->ec_dev;
985 
986 	put_device(ec_dev->dev);
987 }
988 
989 static struct kunit_case cros_ec_proto_test_cases[] = {
990 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_legacy_normal),
991 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize),
992 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_normal),
993 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_bad_msg_outsize),
994 	KUNIT_CASE(cros_ec_proto_test_check_result),
995 	KUNIT_CASE(cros_ec_proto_test_query_all_normal),
996 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return_error),
997 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return_error),
998 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_xfer_error),
999 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return_error),
1000 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_data_error),
1001 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp),
1002 	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep),
1003 	KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return_error),
1004 	{}
1005 };
1006 
1007 static struct kunit_suite cros_ec_proto_test_suite = {
1008 	.name = "cros_ec_proto_test",
1009 	.init = cros_ec_proto_test_init,
1010 	.exit = cros_ec_proto_test_exit,
1011 	.test_cases = cros_ec_proto_test_cases,
1012 };
1013 
1014 kunit_test_suite(cros_ec_proto_test_suite);
1015 
1016 MODULE_LICENSE("GPL");
1017