xref: /openbmc/linux/drivers/platform/chrome/cros_ec_proto_test.c (revision b97d6790d03b763eca08847a9a5869a4291b9f9a)
1db681eafSTzung-Bi Shih // SPDX-License-Identifier: GPL-2.0
2db681eafSTzung-Bi Shih /*
3db681eafSTzung-Bi Shih  * Kunit tests for ChromeOS Embedded Controller protocol.
4db681eafSTzung-Bi Shih  */
5db681eafSTzung-Bi Shih 
6db681eafSTzung-Bi Shih #include <kunit/test.h>
7db681eafSTzung-Bi Shih 
8aaab5af4SAndy Shevchenko #include <asm/unaligned.h>
9aaab5af4SAndy Shevchenko 
10db681eafSTzung-Bi Shih #include <linux/platform_data/cros_ec_commands.h>
11db681eafSTzung-Bi Shih #include <linux/platform_data/cros_ec_proto.h>
12db681eafSTzung-Bi Shih 
13db681eafSTzung-Bi Shih #include "cros_ec.h"
14b99eb596STzung-Bi Shih #include "cros_kunit_util.h"
15db681eafSTzung-Bi Shih 
16db681eafSTzung-Bi Shih #define BUFSIZE 512
17db681eafSTzung-Bi Shih 
18db681eafSTzung-Bi Shih struct cros_ec_proto_test_priv {
19db681eafSTzung-Bi Shih 	struct cros_ec_device ec_dev;
20db681eafSTzung-Bi Shih 	u8 dout[BUFSIZE];
21db681eafSTzung-Bi Shih 	u8 din[BUFSIZE];
22db681eafSTzung-Bi Shih 	struct cros_ec_command *msg;
23db681eafSTzung-Bi Shih 	u8 _msg[BUFSIZE];
24db681eafSTzung-Bi Shih };
25db681eafSTzung-Bi Shih 
cros_ec_proto_test_prepare_tx_legacy_normal(struct kunit * test)26db681eafSTzung-Bi Shih static void cros_ec_proto_test_prepare_tx_legacy_normal(struct kunit *test)
27db681eafSTzung-Bi Shih {
28db681eafSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
29db681eafSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
30db681eafSTzung-Bi Shih 	struct cros_ec_command *msg = priv->msg;
31db681eafSTzung-Bi Shih 	int ret, i;
32db681eafSTzung-Bi Shih 	u8 csum;
33db681eafSTzung-Bi Shih 
34db681eafSTzung-Bi Shih 	ec_dev->proto_version = 2;
35db681eafSTzung-Bi Shih 
36db681eafSTzung-Bi Shih 	msg->command = EC_CMD_HELLO;
37db681eafSTzung-Bi Shih 	msg->outsize = EC_PROTO2_MAX_PARAM_SIZE;
38db681eafSTzung-Bi Shih 	msg->data[0] = 0xde;
39db681eafSTzung-Bi Shih 	msg->data[1] = 0xad;
40db681eafSTzung-Bi Shih 	msg->data[2] = 0xbe;
41db681eafSTzung-Bi Shih 	msg->data[3] = 0xef;
42db681eafSTzung-Bi Shih 
43db681eafSTzung-Bi Shih 	ret = cros_ec_prepare_tx(ec_dev, msg);
44db681eafSTzung-Bi Shih 
45db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, EC_MSG_TX_PROTO_BYTES + EC_PROTO2_MAX_PARAM_SIZE);
46db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[0], EC_CMD_VERSION0);
47db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[1], EC_CMD_HELLO);
48db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[2], EC_PROTO2_MAX_PARAM_SIZE);
49db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, EC_MSG_TX_HEADER_BYTES, 3);
50db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 0], 0xde);
51db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 1], 0xad);
52db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 2], 0xbe);
53db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + 3], 0xef);
54db681eafSTzung-Bi Shih 	for (i = 4; i < EC_PROTO2_MAX_PARAM_SIZE; ++i)
55db681eafSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->dout[EC_MSG_TX_HEADER_BYTES + i], 0);
56db681eafSTzung-Bi Shih 
57db681eafSTzung-Bi Shih 	csum = EC_CMD_VERSION0;
58db681eafSTzung-Bi Shih 	csum += EC_CMD_HELLO;
59db681eafSTzung-Bi Shih 	csum += EC_PROTO2_MAX_PARAM_SIZE;
60db681eafSTzung-Bi Shih 	csum += 0xde;
61db681eafSTzung-Bi Shih 	csum += 0xad;
62db681eafSTzung-Bi Shih 	csum += 0xbe;
63db681eafSTzung-Bi Shih 	csum += 0xef;
64db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test,
65db681eafSTzung-Bi Shih 			ec_dev->dout[EC_MSG_TX_HEADER_BYTES + EC_PROTO2_MAX_PARAM_SIZE],
66db681eafSTzung-Bi Shih 			csum);
67db681eafSTzung-Bi Shih }
68db681eafSTzung-Bi Shih 
cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize(struct kunit * test)69db681eafSTzung-Bi Shih static void cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize(struct kunit *test)
70db681eafSTzung-Bi Shih {
71db681eafSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
72db681eafSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
73db681eafSTzung-Bi Shih 	struct cros_ec_command *msg = priv->msg;
74db681eafSTzung-Bi Shih 	int ret;
75db681eafSTzung-Bi Shih 
76db681eafSTzung-Bi Shih 	ec_dev->proto_version = 2;
77db681eafSTzung-Bi Shih 
78db681eafSTzung-Bi Shih 	msg->outsize = EC_PROTO2_MAX_PARAM_SIZE + 1;
79db681eafSTzung-Bi Shih 
80db681eafSTzung-Bi Shih 	ret = cros_ec_prepare_tx(ec_dev, msg);
81db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EINVAL);
82db681eafSTzung-Bi Shih }
83db681eafSTzung-Bi Shih 
cros_ec_proto_test_prepare_tx_normal(struct kunit * test)84db681eafSTzung-Bi Shih static void cros_ec_proto_test_prepare_tx_normal(struct kunit *test)
85db681eafSTzung-Bi Shih {
86db681eafSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
87db681eafSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
88db681eafSTzung-Bi Shih 	struct cros_ec_command *msg = priv->msg;
89db681eafSTzung-Bi Shih 	struct ec_host_request *request = (struct ec_host_request *)ec_dev->dout;
90db681eafSTzung-Bi Shih 	int ret, i;
91db681eafSTzung-Bi Shih 	u8 csum;
92db681eafSTzung-Bi Shih 
93db681eafSTzung-Bi Shih 	msg->command = EC_CMD_HELLO;
94db681eafSTzung-Bi Shih 	msg->outsize = 0x88;
95db681eafSTzung-Bi Shih 	msg->data[0] = 0xde;
96db681eafSTzung-Bi Shih 	msg->data[1] = 0xad;
97db681eafSTzung-Bi Shih 	msg->data[2] = 0xbe;
98db681eafSTzung-Bi Shih 	msg->data[3] = 0xef;
99db681eafSTzung-Bi Shih 
100db681eafSTzung-Bi Shih 	ret = cros_ec_prepare_tx(ec_dev, msg);
101db681eafSTzung-Bi Shih 
102db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, sizeof(*request) + 0x88);
103db681eafSTzung-Bi Shih 
104db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, request->struct_version, EC_HOST_REQUEST_VERSION);
105db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, request->command, EC_CMD_HELLO);
106db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, request->command_version, 0);
107db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, request->data_len, 0x88);
108db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 0], 0xde);
109db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 1], 0xad);
110db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 2], 0xbe);
111db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + 3], 0xef);
112db681eafSTzung-Bi Shih 	for (i = 4; i < 0x88; ++i)
113db681eafSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->dout[sizeof(*request) + i], 0);
114db681eafSTzung-Bi Shih 
115db681eafSTzung-Bi Shih 	csum = EC_HOST_REQUEST_VERSION;
116db681eafSTzung-Bi Shih 	csum += EC_CMD_HELLO;
117db681eafSTzung-Bi Shih 	csum += 0x88;
118db681eafSTzung-Bi Shih 	csum += 0xde;
119db681eafSTzung-Bi Shih 	csum += 0xad;
120db681eafSTzung-Bi Shih 	csum += 0xbe;
121db681eafSTzung-Bi Shih 	csum += 0xef;
122db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, request->checksum, (u8)-csum);
123db681eafSTzung-Bi Shih }
124db681eafSTzung-Bi Shih 
cros_ec_proto_test_prepare_tx_bad_msg_outsize(struct kunit * test)125db681eafSTzung-Bi Shih static void cros_ec_proto_test_prepare_tx_bad_msg_outsize(struct kunit *test)
126db681eafSTzung-Bi Shih {
127db681eafSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
128db681eafSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
129db681eafSTzung-Bi Shih 	struct cros_ec_command *msg = priv->msg;
130db681eafSTzung-Bi Shih 	int ret;
131db681eafSTzung-Bi Shih 
132db681eafSTzung-Bi Shih 	msg->outsize = ec_dev->dout_size - sizeof(struct ec_host_request) + 1;
133db681eafSTzung-Bi Shih 
134db681eafSTzung-Bi Shih 	ret = cros_ec_prepare_tx(ec_dev, msg);
135db681eafSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EINVAL);
136db681eafSTzung-Bi Shih }
137db681eafSTzung-Bi Shih 
cros_ec_proto_test_check_result(struct kunit * test)1384319cbd4STzung-Bi Shih static void cros_ec_proto_test_check_result(struct kunit *test)
1394319cbd4STzung-Bi Shih {
1404319cbd4STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1414319cbd4STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1424319cbd4STzung-Bi Shih 	struct cros_ec_command *msg = priv->msg;
1434319cbd4STzung-Bi Shih 	int ret, i;
1444319cbd4STzung-Bi Shih 	static enum ec_status status[] = {
1454319cbd4STzung-Bi Shih 		EC_RES_SUCCESS,
1464319cbd4STzung-Bi Shih 		EC_RES_INVALID_COMMAND,
1474319cbd4STzung-Bi Shih 		EC_RES_ERROR,
1484319cbd4STzung-Bi Shih 		EC_RES_INVALID_PARAM,
1494319cbd4STzung-Bi Shih 		EC_RES_ACCESS_DENIED,
1504319cbd4STzung-Bi Shih 		EC_RES_INVALID_RESPONSE,
1514319cbd4STzung-Bi Shih 		EC_RES_INVALID_VERSION,
1524319cbd4STzung-Bi Shih 		EC_RES_INVALID_CHECKSUM,
1534319cbd4STzung-Bi Shih 		EC_RES_UNAVAILABLE,
1544319cbd4STzung-Bi Shih 		EC_RES_TIMEOUT,
1554319cbd4STzung-Bi Shih 		EC_RES_OVERFLOW,
1564319cbd4STzung-Bi Shih 		EC_RES_INVALID_HEADER,
1574319cbd4STzung-Bi Shih 		EC_RES_REQUEST_TRUNCATED,
1584319cbd4STzung-Bi Shih 		EC_RES_RESPONSE_TOO_BIG,
1594319cbd4STzung-Bi Shih 		EC_RES_BUS_ERROR,
1604319cbd4STzung-Bi Shih 		EC_RES_BUSY,
1614319cbd4STzung-Bi Shih 		EC_RES_INVALID_HEADER_VERSION,
1624319cbd4STzung-Bi Shih 		EC_RES_INVALID_HEADER_CRC,
1634319cbd4STzung-Bi Shih 		EC_RES_INVALID_DATA_CRC,
1644319cbd4STzung-Bi Shih 		EC_RES_DUP_UNAVAILABLE,
1654319cbd4STzung-Bi Shih 	};
1664319cbd4STzung-Bi Shih 
1674319cbd4STzung-Bi Shih 	for (i = 0; i < ARRAY_SIZE(status); ++i) {
1684319cbd4STzung-Bi Shih 		msg->result = status[i];
1694319cbd4STzung-Bi Shih 		ret = cros_ec_check_result(ec_dev, msg);
1704319cbd4STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ret, 0);
1714319cbd4STzung-Bi Shih 	}
1724319cbd4STzung-Bi Shih 
1734319cbd4STzung-Bi Shih 	msg->result = EC_RES_IN_PROGRESS;
1744319cbd4STzung-Bi Shih 	ret = cros_ec_check_result(ec_dev, msg);
1754319cbd4STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
1764319cbd4STzung-Bi Shih }
1774319cbd4STzung-Bi Shih 
cros_ec_proto_test_query_all_pretest(struct kunit * test)178b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_pretest(struct kunit *test)
179b99eb596STzung-Bi Shih {
180b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
181b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
182b99eb596STzung-Bi Shih 
183b99eb596STzung-Bi Shih 	/*
184b99eb596STzung-Bi Shih 	 * cros_ec_query_all() will free din and dout and allocate them again to fit the usage by
185b99eb596STzung-Bi Shih 	 * calling devm_kfree() and devm_kzalloc().  Set them to NULL as they aren't managed by
186b99eb596STzung-Bi Shih 	 * ec_dev->dev but allocated statically in struct cros_ec_proto_test_priv
187b99eb596STzung-Bi Shih 	 * (see cros_ec_proto_test_init()).
188b99eb596STzung-Bi Shih 	 */
189b99eb596STzung-Bi Shih 	ec_dev->din = NULL;
190b99eb596STzung-Bi Shih 	ec_dev->dout = NULL;
191b99eb596STzung-Bi Shih }
192b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_normal(struct kunit * test)193b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_normal(struct kunit *test)
194b99eb596STzung-Bi Shih {
195b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
196b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
197b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
198b99eb596STzung-Bi Shih 	int ret;
199b99eb596STzung-Bi Shih 
200b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
201b99eb596STzung-Bi Shih 	{
202b99eb596STzung-Bi Shih 		struct ec_response_get_protocol_info *data;
203b99eb596STzung-Bi Shih 
204b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
205b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
206b99eb596STzung-Bi Shih 
207b99eb596STzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
208b99eb596STzung-Bi Shih 		data->protocol_versions = BIT(3) | BIT(2);
209b99eb596STzung-Bi Shih 		data->max_request_packet_size = 0xbe;
210b99eb596STzung-Bi Shih 		data->max_response_packet_size = 0xef;
211b99eb596STzung-Bi Shih 	}
212b99eb596STzung-Bi Shih 
213b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
214b99eb596STzung-Bi Shih 	{
215b99eb596STzung-Bi Shih 		struct ec_response_get_protocol_info *data;
216b99eb596STzung-Bi Shih 
217b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
218b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
219b99eb596STzung-Bi Shih 
220b99eb596STzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
221b99eb596STzung-Bi Shih 		data->max_request_packet_size = 0xbf;
222b99eb596STzung-Bi Shih 	}
223b99eb596STzung-Bi Shih 
224b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
225b99eb596STzung-Bi Shih 	{
226b99eb596STzung-Bi Shih 		struct ec_response_get_cmd_versions *data;
227b99eb596STzung-Bi Shih 
228b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
229b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
230b99eb596STzung-Bi Shih 
231b99eb596STzung-Bi Shih 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
232b99eb596STzung-Bi Shih 		data->version_mask = BIT(6) | BIT(5);
233b99eb596STzung-Bi Shih 	}
234b99eb596STzung-Bi Shih 
235b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
236b99eb596STzung-Bi Shih 	{
237b99eb596STzung-Bi Shih 		struct ec_response_get_cmd_versions *data;
238b99eb596STzung-Bi Shih 
239b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
240b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
241b99eb596STzung-Bi Shih 
242b99eb596STzung-Bi Shih 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
243b99eb596STzung-Bi Shih 		data->version_mask = BIT(1);
244b99eb596STzung-Bi Shih 	}
245b99eb596STzung-Bi Shih 
246b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_event_wake_mask(). */
247b99eb596STzung-Bi Shih 	{
248b99eb596STzung-Bi Shih 		struct ec_response_host_event_mask *data;
249b99eb596STzung-Bi Shih 
250b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
251b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
252b99eb596STzung-Bi Shih 
253b99eb596STzung-Bi Shih 		data = (struct ec_response_host_event_mask *)mock->o_data;
254b99eb596STzung-Bi Shih 		data->mask = 0xbeef;
255b99eb596STzung-Bi Shih 	}
256b99eb596STzung-Bi Shih 
257b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
258b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
259b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
260b99eb596STzung-Bi Shih 
261b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
262b99eb596STzung-Bi Shih 	{
263b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
264b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
265b99eb596STzung-Bi Shih 
266b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
267b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
268b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
269b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
270b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
271b99eb596STzung-Bi Shih 
272b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, 0xbe - sizeof(struct ec_host_request));
273b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, 0xef - sizeof(struct ec_host_response));
274b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 3);
275b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, 0xef + EC_MAX_RESPONSE_OVERHEAD);
276b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, 0xbe + EC_MAX_REQUEST_OVERHEAD);
277b99eb596STzung-Bi Shih 	}
278b99eb596STzung-Bi Shih 
279b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
280b99eb596STzung-Bi Shih 	{
281b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
282b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
283b99eb596STzung-Bi Shih 
284b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
285b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
2863db0c9e5STzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
2873db0c9e5STzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
288b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
289b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
290b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
291b99eb596STzung-Bi Shih 
292b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0xbf - sizeof(struct ec_host_request));
293b99eb596STzung-Bi Shih 	}
294b99eb596STzung-Bi Shih 
295b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
296b99eb596STzung-Bi Shih 	{
297b99eb596STzung-Bi Shih 		struct ec_params_get_cmd_versions *data;
298b99eb596STzung-Bi Shih 
299b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
300b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
301b99eb596STzung-Bi Shih 
302b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
303b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
304b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
305b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
306b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
307b99eb596STzung-Bi Shih 
308b99eb596STzung-Bi Shih 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
309b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
310b99eb596STzung-Bi Shih 
311b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 7);
312b99eb596STzung-Bi Shih 	}
313b99eb596STzung-Bi Shih 
314b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
315b99eb596STzung-Bi Shih 	{
316b99eb596STzung-Bi Shih 		struct ec_params_get_cmd_versions *data;
317b99eb596STzung-Bi Shih 
318b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
319b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
320b99eb596STzung-Bi Shih 
321b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
322b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
323b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
324b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
325b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
326b99eb596STzung-Bi Shih 
327b99eb596STzung-Bi Shih 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
328b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_HOST_SLEEP_EVENT);
329b99eb596STzung-Bi Shih 
330b99eb596STzung-Bi Shih 		KUNIT_EXPECT_TRUE(test, ec_dev->host_sleep_v1);
331b99eb596STzung-Bi Shih 	}
332b99eb596STzung-Bi Shih 
333b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_event_wake_mask(). */
334b99eb596STzung-Bi Shih 	{
335b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
336b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
337b99eb596STzung-Bi Shih 
338b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
339b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
340b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
341b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
342b99eb596STzung-Bi Shih 
343b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->host_event_wake_mask, 0xbeef);
344b99eb596STzung-Bi Shih 	}
345b99eb596STzung-Bi Shih }
346b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_no_pd_return_error(struct kunit * test)347b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_no_pd_return_error(struct kunit *test)
348b99eb596STzung-Bi Shih {
349b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
350b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
351b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
352b99eb596STzung-Bi Shih 	int ret;
353b99eb596STzung-Bi Shih 
354b99eb596STzung-Bi Shih 	/* Set some garbage bytes. */
355b99eb596STzung-Bi Shih 	ec_dev->max_passthru = 0xbf;
356b99eb596STzung-Bi Shih 
357b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
358b99eb596STzung-Bi Shih 	{
359b99eb596STzung-Bi Shih 		struct ec_response_get_protocol_info *data;
360b99eb596STzung-Bi Shih 
361b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
362b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
363b99eb596STzung-Bi Shih 
364b99eb596STzung-Bi Shih 		/*
365b99eb596STzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
366b99eb596STzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
367b99eb596STzung-Bi Shih 		 */
368b99eb596STzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
369b99eb596STzung-Bi Shih 		data->max_request_packet_size = 0xbe;
370b99eb596STzung-Bi Shih 		data->max_response_packet_size = 0xef;
371b99eb596STzung-Bi Shih 	}
372b99eb596STzung-Bi Shih 
373b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
374b99eb596STzung-Bi Shih 	{
375b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
376b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
377b99eb596STzung-Bi Shih 	}
378b99eb596STzung-Bi Shih 
379b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
380b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
381b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
382b99eb596STzung-Bi Shih 
383b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
384b99eb596STzung-Bi Shih 	{
385b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
386b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
387b99eb596STzung-Bi Shih 
388b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
389b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
390b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
391b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
392b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
393b99eb596STzung-Bi Shih 	}
394b99eb596STzung-Bi Shih 
395b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
396b99eb596STzung-Bi Shih 	{
397b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
398b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
399b99eb596STzung-Bi Shih 
400b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
401b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
4023db0c9e5STzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
4033db0c9e5STzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
404b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
405b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
406b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
407b99eb596STzung-Bi Shih 
408b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
409b99eb596STzung-Bi Shih 	}
410b99eb596STzung-Bi Shih }
411b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_no_pd_return0(struct kunit * test)4123e97581eSTzung-Bi Shih static void cros_ec_proto_test_query_all_no_pd_return0(struct kunit *test)
4133e97581eSTzung-Bi Shih {
4143e97581eSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
4153e97581eSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
4163e97581eSTzung-Bi Shih 	struct ec_xfer_mock *mock;
4173e97581eSTzung-Bi Shih 	int ret;
4183e97581eSTzung-Bi Shih 
4193e97581eSTzung-Bi Shih 	/* Set some garbage bytes. */
4203e97581eSTzung-Bi Shih 	ec_dev->max_passthru = 0xbf;
4213e97581eSTzung-Bi Shih 
4223e97581eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
4233e97581eSTzung-Bi Shih 	{
4243e97581eSTzung-Bi Shih 		struct ec_response_get_protocol_info *data;
4253e97581eSTzung-Bi Shih 
4263e97581eSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
4273e97581eSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
4283e97581eSTzung-Bi Shih 
4293e97581eSTzung-Bi Shih 		/*
4303e97581eSTzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
4313e97581eSTzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
4323e97581eSTzung-Bi Shih 		 */
4333e97581eSTzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
4343e97581eSTzung-Bi Shih 		data->max_request_packet_size = 0xbe;
4353e97581eSTzung-Bi Shih 		data->max_response_packet_size = 0xef;
4363e97581eSTzung-Bi Shih 	}
4373e97581eSTzung-Bi Shih 
4383e97581eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
4393e97581eSTzung-Bi Shih 	{
4403e97581eSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
4413e97581eSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
4423e97581eSTzung-Bi Shih 	}
4433e97581eSTzung-Bi Shih 
4443e97581eSTzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
4453e97581eSTzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
4463e97581eSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
4473e97581eSTzung-Bi Shih 
4483e97581eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
4493e97581eSTzung-Bi Shih 	{
4503e97581eSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
4513e97581eSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
4523e97581eSTzung-Bi Shih 
4533e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
4543e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
4553e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
4563e97581eSTzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
4573e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
4583e97581eSTzung-Bi Shih 	}
4593e97581eSTzung-Bi Shih 
4603e97581eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
4613e97581eSTzung-Bi Shih 	{
4623e97581eSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
4633e97581eSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
4643e97581eSTzung-Bi Shih 
4653e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
4663e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
4673e97581eSTzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
4683e97581eSTzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
4693e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
4703e97581eSTzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
4713e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
4723e97581eSTzung-Bi Shih 
4733e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
4743e97581eSTzung-Bi Shih 	}
4753e97581eSTzung-Bi Shih }
4763e97581eSTzung-Bi Shih 
cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct kunit * test)477b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct kunit *test)
478b99eb596STzung-Bi Shih {
479b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
480b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
481b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
482b99eb596STzung-Bi Shih 	int ret;
483b99eb596STzung-Bi Shih 
484b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
485b99eb596STzung-Bi Shih 	{
486b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
487b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
488b99eb596STzung-Bi Shih 	}
489b99eb596STzung-Bi Shih 
490a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
491b99eb596STzung-Bi Shih 	{
492b99eb596STzung-Bi Shih 		struct ec_response_hello *data;
493b99eb596STzung-Bi Shih 
494b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
495b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
496b99eb596STzung-Bi Shih 
497b99eb596STzung-Bi Shih 		data = (struct ec_response_hello *)mock->o_data;
498b99eb596STzung-Bi Shih 		data->out_data = 0xa1b2c3d4;
499b99eb596STzung-Bi Shih 	}
500b99eb596STzung-Bi Shih 
501b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
502b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
503b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
504b99eb596STzung-Bi Shih 
505b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
506b99eb596STzung-Bi Shih 	{
507b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
508b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
509b99eb596STzung-Bi Shih 
510b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
511b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
512b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
513b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
514b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
515b99eb596STzung-Bi Shih 	}
516b99eb596STzung-Bi Shih 
517a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
518b99eb596STzung-Bi Shih 	{
519b99eb596STzung-Bi Shih 		struct ec_params_hello *data;
520b99eb596STzung-Bi Shih 
521b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
522b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
523b99eb596STzung-Bi Shih 
524b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
525b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
526b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
527b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
528b99eb596STzung-Bi Shih 
529b99eb596STzung-Bi Shih 		data = (struct ec_params_hello *)mock->i_data;
530b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->in_data, 0xa0b0c0d0);
531b99eb596STzung-Bi Shih 
532b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 2);
533b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, EC_PROTO2_MAX_PARAM_SIZE);
534b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, EC_PROTO2_MAX_PARAM_SIZE);
535b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
536b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_EQ(test, ec_dev->pkt_xfer, NULL);
537b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, EC_PROTO2_MSG_BYTES);
538b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, EC_PROTO2_MSG_BYTES);
539b99eb596STzung-Bi Shih 	}
540b99eb596STzung-Bi Shih }
541b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_legacy_normal_v3_return0(struct kunit * test)5423e97581eSTzung-Bi Shih static void cros_ec_proto_test_query_all_legacy_normal_v3_return0(struct kunit *test)
5433e97581eSTzung-Bi Shih {
5443e97581eSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
5453e97581eSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
5463e97581eSTzung-Bi Shih 	struct ec_xfer_mock *mock;
5473e97581eSTzung-Bi Shih 	int ret;
5483e97581eSTzung-Bi Shih 
5493e97581eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
5503e97581eSTzung-Bi Shih 	{
5513e97581eSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
5523e97581eSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
5533e97581eSTzung-Bi Shih 	}
5543e97581eSTzung-Bi Shih 
555a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
5563e97581eSTzung-Bi Shih 	{
5573e97581eSTzung-Bi Shih 		struct ec_response_hello *data;
5583e97581eSTzung-Bi Shih 
5593e97581eSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
5603e97581eSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
5613e97581eSTzung-Bi Shih 
5623e97581eSTzung-Bi Shih 		data = (struct ec_response_hello *)mock->o_data;
5633e97581eSTzung-Bi Shih 		data->out_data = 0xa1b2c3d4;
5643e97581eSTzung-Bi Shih 	}
5653e97581eSTzung-Bi Shih 
5663e97581eSTzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
5673e97581eSTzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
5683e97581eSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
5693e97581eSTzung-Bi Shih 
5703e97581eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
5713e97581eSTzung-Bi Shih 	{
5723e97581eSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
5733e97581eSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
5743e97581eSTzung-Bi Shih 
5753e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
5763e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
5773e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
5783e97581eSTzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
5793e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
5803e97581eSTzung-Bi Shih 	}
5813e97581eSTzung-Bi Shih 
582a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
5833e97581eSTzung-Bi Shih 	{
5843e97581eSTzung-Bi Shih 		struct ec_params_hello *data;
5853e97581eSTzung-Bi Shih 
5863e97581eSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
5873e97581eSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
5883e97581eSTzung-Bi Shih 
5893e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
5903e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
5913e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
5923e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
5933e97581eSTzung-Bi Shih 
5943e97581eSTzung-Bi Shih 		data = (struct ec_params_hello *)mock->i_data;
5953e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->in_data, 0xa0b0c0d0);
5963e97581eSTzung-Bi Shih 
5973e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 2);
5983e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_request, EC_PROTO2_MAX_PARAM_SIZE);
5993e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_response, EC_PROTO2_MAX_PARAM_SIZE);
6003e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0);
6013e97581eSTzung-Bi Shih 		KUNIT_EXPECT_PTR_EQ(test, ec_dev->pkt_xfer, NULL);
6023e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->din_size, EC_PROTO2_MSG_BYTES);
6033e97581eSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->dout_size, EC_PROTO2_MSG_BYTES);
6043e97581eSTzung-Bi Shih 	}
6053e97581eSTzung-Bi Shih }
6063e97581eSTzung-Bi Shih 
cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit * test)607b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit *test)
608b99eb596STzung-Bi Shih {
609b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
610b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
611b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
612b99eb596STzung-Bi Shih 	int ret;
613b99eb596STzung-Bi Shih 
614b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
615b99eb596STzung-Bi Shih 	{
616b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
617b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
618b99eb596STzung-Bi Shih 	}
619b99eb596STzung-Bi Shih 
620a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
621b99eb596STzung-Bi Shih 	{
622b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, -EIO, EC_RES_SUCCESS, 0);
623b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
624b99eb596STzung-Bi Shih 	}
625b99eb596STzung-Bi Shih 
626b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
627b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
628b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EIO);
629b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
630b99eb596STzung-Bi Shih 
631b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
632b99eb596STzung-Bi Shih 	{
633b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
634b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
635b99eb596STzung-Bi Shih 
636b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
637b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
638b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
639b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
640b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
641b99eb596STzung-Bi Shih 	}
642b99eb596STzung-Bi Shih 
643a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
644b99eb596STzung-Bi Shih 	{
645b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
646b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
647b99eb596STzung-Bi Shih 
648b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
649b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
650b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
651b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
652b99eb596STzung-Bi Shih 	}
653b99eb596STzung-Bi Shih }
654b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_legacy_return_error(struct kunit * test)655b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_legacy_return_error(struct kunit *test)
656b99eb596STzung-Bi Shih {
657b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
658b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
659b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
660b99eb596STzung-Bi Shih 	int ret;
661b99eb596STzung-Bi Shih 
662b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
663b99eb596STzung-Bi Shih 	{
664b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
665b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
666b99eb596STzung-Bi Shih 	}
667b99eb596STzung-Bi Shih 
668a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
669b99eb596STzung-Bi Shih 	{
670b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
671b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
672b99eb596STzung-Bi Shih 	}
673b99eb596STzung-Bi Shih 
674b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
675b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
676a88f7966STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP);
677b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
678b99eb596STzung-Bi Shih 
679b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
680b99eb596STzung-Bi Shih 	{
681b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
682b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
683b99eb596STzung-Bi Shih 
684b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
685b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
686b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
687b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
688b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
689b99eb596STzung-Bi Shih 	}
690b99eb596STzung-Bi Shih 
691a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
692b99eb596STzung-Bi Shih 	{
693b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
694b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
695b99eb596STzung-Bi Shih 
696b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
697b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
698b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
699b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
700b99eb596STzung-Bi Shih 	}
701b99eb596STzung-Bi Shih }
702b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_legacy_data_error(struct kunit * test)703b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_legacy_data_error(struct kunit *test)
704b99eb596STzung-Bi Shih {
705b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
706b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
707b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
708b99eb596STzung-Bi Shih 	int ret;
709b99eb596STzung-Bi Shih 
710b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
711b99eb596STzung-Bi Shih 	{
712b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
713b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
714b99eb596STzung-Bi Shih 	}
715b99eb596STzung-Bi Shih 
716a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
717b99eb596STzung-Bi Shih 	{
718b99eb596STzung-Bi Shih 		struct ec_response_hello *data;
719b99eb596STzung-Bi Shih 
720b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
721b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
722b99eb596STzung-Bi Shih 
723b99eb596STzung-Bi Shih 		data = (struct ec_response_hello *)mock->o_data;
724b99eb596STzung-Bi Shih 		data->out_data = 0xbeefbfbf;
725b99eb596STzung-Bi Shih 	}
726b99eb596STzung-Bi Shih 
727b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
728b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
729b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EBADMSG);
730b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
731b99eb596STzung-Bi Shih 
732b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
733b99eb596STzung-Bi Shih 	{
734b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
735b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
736b99eb596STzung-Bi Shih 
737b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
738b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
739b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
740b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
741b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
742b99eb596STzung-Bi Shih 	}
743b99eb596STzung-Bi Shih 
744a88f7966STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
745b99eb596STzung-Bi Shih 	{
746b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
747b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
748b99eb596STzung-Bi Shih 
749b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
750b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
751b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
752b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
753b99eb596STzung-Bi Shih 	}
754b99eb596STzung-Bi Shih }
755b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_legacy_return0(struct kunit * test)756cce5d551STzung-Bi Shih static void cros_ec_proto_test_query_all_legacy_return0(struct kunit *test)
757cce5d551STzung-Bi Shih {
758cce5d551STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
759cce5d551STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
760cce5d551STzung-Bi Shih 	struct ec_xfer_mock *mock;
761cce5d551STzung-Bi Shih 	int ret;
762cce5d551STzung-Bi Shih 
763cce5d551STzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
764cce5d551STzung-Bi Shih 	{
765cce5d551STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
766cce5d551STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
767cce5d551STzung-Bi Shih 	}
768cce5d551STzung-Bi Shih 
769cce5d551STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
770cce5d551STzung-Bi Shih 	{
771cce5d551STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
772cce5d551STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
773cce5d551STzung-Bi Shih 	}
774cce5d551STzung-Bi Shih 
775cce5d551STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
776cce5d551STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
777cce5d551STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
778cce5d551STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
779cce5d551STzung-Bi Shih 
780cce5d551STzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
781cce5d551STzung-Bi Shih 	{
782cce5d551STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
783cce5d551STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
784cce5d551STzung-Bi Shih 
785cce5d551STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
786cce5d551STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
787cce5d551STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
788cce5d551STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
789cce5d551STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
790cce5d551STzung-Bi Shih 	}
791cce5d551STzung-Bi Shih 
792cce5d551STzung-Bi Shih 	/* For cros_ec_get_proto_info_legacy(). */
793cce5d551STzung-Bi Shih 	{
794cce5d551STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
795cce5d551STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
796cce5d551STzung-Bi Shih 
797cce5d551STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
798cce5d551STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
799cce5d551STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello));
800cce5d551STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello));
801cce5d551STzung-Bi Shih 	}
802cce5d551STzung-Bi Shih }
803cce5d551STzung-Bi Shih 
cros_ec_proto_test_query_all_no_mkbp(struct kunit * test)804b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_no_mkbp(struct kunit *test)
805b99eb596STzung-Bi Shih {
806b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
807b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
808b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
809b99eb596STzung-Bi Shih 	int ret;
810b99eb596STzung-Bi Shih 
811b99eb596STzung-Bi Shih 	/* Set some garbage bytes. */
812b99eb596STzung-Bi Shih 	ec_dev->mkbp_event_supported = 0xbf;
813b99eb596STzung-Bi Shih 
814b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
815b99eb596STzung-Bi Shih 	{
816b99eb596STzung-Bi Shih 		struct ec_response_get_protocol_info *data;
817b99eb596STzung-Bi Shih 
818b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
819b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
820b99eb596STzung-Bi Shih 
821b99eb596STzung-Bi Shih 		/*
822b99eb596STzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
823b99eb596STzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
824b99eb596STzung-Bi Shih 		 */
825b99eb596STzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
826b99eb596STzung-Bi Shih 		data->max_request_packet_size = 0xbe;
827b99eb596STzung-Bi Shih 		data->max_response_packet_size = 0xef;
828b99eb596STzung-Bi Shih 	}
829b99eb596STzung-Bi Shih 
830b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
831b99eb596STzung-Bi Shih 	{
832b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
833b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
834b99eb596STzung-Bi Shih 	}
835b99eb596STzung-Bi Shih 
836b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
837b99eb596STzung-Bi Shih 	{
838b99eb596STzung-Bi Shih 		struct ec_response_get_cmd_versions *data;
839b99eb596STzung-Bi Shih 
840b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
841b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
842b99eb596STzung-Bi Shih 
843b99eb596STzung-Bi Shih 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
844b99eb596STzung-Bi Shih 		data->version_mask = 0;
845b99eb596STzung-Bi Shih 	}
846b99eb596STzung-Bi Shih 
847b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
848b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
849b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
850b99eb596STzung-Bi Shih 
851b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
852b99eb596STzung-Bi Shih 	{
853b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
854b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
855b99eb596STzung-Bi Shih 
856b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
857b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
858b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
859b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
860b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
861b99eb596STzung-Bi Shih 	}
862b99eb596STzung-Bi Shih 
863b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
864b99eb596STzung-Bi Shih 	{
865b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
866b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
867b99eb596STzung-Bi Shih 
868b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
869b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
8703db0c9e5STzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
8713db0c9e5STzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
872b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
873b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
874b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
875b99eb596STzung-Bi Shih 	}
876b99eb596STzung-Bi Shih 
877b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
878b99eb596STzung-Bi Shih 	{
879b99eb596STzung-Bi Shih 		struct ec_params_get_cmd_versions *data;
880b99eb596STzung-Bi Shih 
881b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
882b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
883b99eb596STzung-Bi Shih 
884b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
885b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
886b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
887b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
888b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
889b99eb596STzung-Bi Shih 
890b99eb596STzung-Bi Shih 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
891b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
892b99eb596STzung-Bi Shih 
893b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
894b99eb596STzung-Bi Shih 	}
895b99eb596STzung-Bi Shih }
896b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_no_mkbp_return_error(struct kunit * test)897a8f77c63STzung-Bi Shih static void cros_ec_proto_test_query_all_no_mkbp_return_error(struct kunit *test)
898a8f77c63STzung-Bi Shih {
899a8f77c63STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
900a8f77c63STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
901a8f77c63STzung-Bi Shih 	struct ec_xfer_mock *mock;
902a8f77c63STzung-Bi Shih 	int ret;
903a8f77c63STzung-Bi Shih 
904a8f77c63STzung-Bi Shih 	/* Set some garbage bytes. */
905a8f77c63STzung-Bi Shih 	ec_dev->mkbp_event_supported = 0xbf;
906a8f77c63STzung-Bi Shih 
907a8f77c63STzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
908a8f77c63STzung-Bi Shih 	{
909a8f77c63STzung-Bi Shih 		struct ec_response_get_protocol_info *data;
910a8f77c63STzung-Bi Shih 
911a8f77c63STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
912a8f77c63STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
913a8f77c63STzung-Bi Shih 
914a8f77c63STzung-Bi Shih 		/*
915a8f77c63STzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
916a8f77c63STzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
917a8f77c63STzung-Bi Shih 		 */
918a8f77c63STzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
919a8f77c63STzung-Bi Shih 		data->max_request_packet_size = 0xbe;
920a8f77c63STzung-Bi Shih 		data->max_response_packet_size = 0xef;
921a8f77c63STzung-Bi Shih 	}
922a8f77c63STzung-Bi Shih 
923a8f77c63STzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
924a8f77c63STzung-Bi Shih 	{
925a8f77c63STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
926a8f77c63STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
927a8f77c63STzung-Bi Shih 	}
928a8f77c63STzung-Bi Shih 
929a8f77c63STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
930a8f77c63STzung-Bi Shih 	{
931a8f77c63STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
932a8f77c63STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
933a8f77c63STzung-Bi Shih 	}
934a8f77c63STzung-Bi Shih 
935a8f77c63STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
936a8f77c63STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
937a8f77c63STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
938a8f77c63STzung-Bi Shih 
939a8f77c63STzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
940a8f77c63STzung-Bi Shih 	{
941a8f77c63STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
942a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
943a8f77c63STzung-Bi Shih 
944a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
945a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
946a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
947a8f77c63STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
948a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
949a8f77c63STzung-Bi Shih 	}
950a8f77c63STzung-Bi Shih 
951a8f77c63STzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
952a8f77c63STzung-Bi Shih 	{
953a8f77c63STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
954a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
955a8f77c63STzung-Bi Shih 
956a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
957a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
958a8f77c63STzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
959a8f77c63STzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
960a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
961a8f77c63STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
962a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
963a8f77c63STzung-Bi Shih 	}
964a8f77c63STzung-Bi Shih 
965a8f77c63STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
966a8f77c63STzung-Bi Shih 	{
967a8f77c63STzung-Bi Shih 		struct ec_params_get_cmd_versions *data;
968a8f77c63STzung-Bi Shih 
969a8f77c63STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
970a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
971a8f77c63STzung-Bi Shih 
972a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
973a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
974a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
975a8f77c63STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
976a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
977a8f77c63STzung-Bi Shih 
978a8f77c63STzung-Bi Shih 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
979a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
980a8f77c63STzung-Bi Shih 
981a8f77c63STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
982a8f77c63STzung-Bi Shih 	}
983a8f77c63STzung-Bi Shih }
984a8f77c63STzung-Bi Shih 
cros_ec_proto_test_query_all_no_mkbp_return0(struct kunit * test)9858120febaSTzung-Bi Shih static void cros_ec_proto_test_query_all_no_mkbp_return0(struct kunit *test)
9868120febaSTzung-Bi Shih {
9878120febaSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
9888120febaSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
9898120febaSTzung-Bi Shih 	struct ec_xfer_mock *mock;
9908120febaSTzung-Bi Shih 	int ret;
9918120febaSTzung-Bi Shih 
9928120febaSTzung-Bi Shih 	/* Set some garbage bytes. */
9938120febaSTzung-Bi Shih 	ec_dev->mkbp_event_supported = 0xbf;
9948120febaSTzung-Bi Shih 
9958120febaSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
9968120febaSTzung-Bi Shih 	{
9978120febaSTzung-Bi Shih 		struct ec_response_get_protocol_info *data;
9988120febaSTzung-Bi Shih 
9998120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
10008120febaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
10018120febaSTzung-Bi Shih 
10028120febaSTzung-Bi Shih 		/*
10038120febaSTzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
10048120febaSTzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
10058120febaSTzung-Bi Shih 		 */
10068120febaSTzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
10078120febaSTzung-Bi Shih 		data->max_request_packet_size = 0xbe;
10088120febaSTzung-Bi Shih 		data->max_response_packet_size = 0xef;
10098120febaSTzung-Bi Shih 	}
10108120febaSTzung-Bi Shih 
10118120febaSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
10128120febaSTzung-Bi Shih 	{
10138120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
10148120febaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
10158120febaSTzung-Bi Shih 	}
10168120febaSTzung-Bi Shih 
10178120febaSTzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
10188120febaSTzung-Bi Shih 	{
10198120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
10208120febaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
10218120febaSTzung-Bi Shih 	}
10228120febaSTzung-Bi Shih 
10238120febaSTzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
10248120febaSTzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
10258120febaSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
10268120febaSTzung-Bi Shih 
10278120febaSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
10288120febaSTzung-Bi Shih 	{
10298120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
10308120febaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
10318120febaSTzung-Bi Shih 
10328120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
10338120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
10348120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
10358120febaSTzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
10368120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
10378120febaSTzung-Bi Shih 	}
10388120febaSTzung-Bi Shih 
10398120febaSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
10408120febaSTzung-Bi Shih 	{
10418120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
10428120febaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
10438120febaSTzung-Bi Shih 
10448120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
10458120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
10468120febaSTzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
10478120febaSTzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
10488120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
10498120febaSTzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
10508120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
10518120febaSTzung-Bi Shih 	}
10528120febaSTzung-Bi Shih 
10538120febaSTzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
10548120febaSTzung-Bi Shih 	{
10558120febaSTzung-Bi Shih 		struct ec_params_get_cmd_versions *data;
10568120febaSTzung-Bi Shih 
10578120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
10588120febaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
10598120febaSTzung-Bi Shih 
10608120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
10618120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
10628120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
10638120febaSTzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
10648120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
10658120febaSTzung-Bi Shih 
10668120febaSTzung-Bi Shih 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
10678120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
10688120febaSTzung-Bi Shih 
10698120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
10708120febaSTzung-Bi Shih 	}
10718120febaSTzung-Bi Shih }
10728120febaSTzung-Bi Shih 
cros_ec_proto_test_query_all_no_host_sleep(struct kunit * test)1073b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_no_host_sleep(struct kunit *test)
1074b99eb596STzung-Bi Shih {
1075b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1076b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1077b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
1078b99eb596STzung-Bi Shih 	int ret;
1079b99eb596STzung-Bi Shih 
1080b99eb596STzung-Bi Shih 	/* Set some garbage bytes. */
1081b99eb596STzung-Bi Shih 	ec_dev->host_sleep_v1 = true;
1082b99eb596STzung-Bi Shih 
1083b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
1084b99eb596STzung-Bi Shih 	{
1085b99eb596STzung-Bi Shih 		struct ec_response_get_protocol_info *data;
1086b99eb596STzung-Bi Shih 
1087b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1088b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1089b99eb596STzung-Bi Shih 
1090b99eb596STzung-Bi Shih 		/*
1091b99eb596STzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
1092b99eb596STzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
1093b99eb596STzung-Bi Shih 		 */
1094b99eb596STzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
1095b99eb596STzung-Bi Shih 		data->max_request_packet_size = 0xbe;
1096b99eb596STzung-Bi Shih 		data->max_response_packet_size = 0xef;
1097b99eb596STzung-Bi Shih 	}
1098b99eb596STzung-Bi Shih 
1099b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
1100b99eb596STzung-Bi Shih 	{
1101b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1102b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1103b99eb596STzung-Bi Shih 	}
1104b99eb596STzung-Bi Shih 
1105b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1106b99eb596STzung-Bi Shih 	{
1107b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1108b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1109b99eb596STzung-Bi Shih 	}
1110b99eb596STzung-Bi Shih 
1111b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1112b99eb596STzung-Bi Shih 	{
1113b99eb596STzung-Bi Shih 		struct ec_response_get_cmd_versions *data;
1114b99eb596STzung-Bi Shih 
1115b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1116b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1117b99eb596STzung-Bi Shih 
1118b99eb596STzung-Bi Shih 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
1119b99eb596STzung-Bi Shih 		data->version_mask = 0;
1120b99eb596STzung-Bi Shih 	}
1121b99eb596STzung-Bi Shih 
1122b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
1123b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
1124b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
1125b99eb596STzung-Bi Shih 
1126b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
1127b99eb596STzung-Bi Shih 	{
1128b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1129b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1130b99eb596STzung-Bi Shih 
1131b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1132b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1133b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1134b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
1135b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1136b99eb596STzung-Bi Shih 	}
1137b99eb596STzung-Bi Shih 
1138b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
1139b99eb596STzung-Bi Shih 	{
1140b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1141b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1142b99eb596STzung-Bi Shih 
1143b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1144b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
11453db0c9e5STzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
11463db0c9e5STzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
1147b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1148b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
1149b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1150b99eb596STzung-Bi Shih 	}
1151b99eb596STzung-Bi Shih 
1152b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1153b99eb596STzung-Bi Shih 	{
1154b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1155b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1156b99eb596STzung-Bi Shih 
1157b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1158b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1159b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1160b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
1161b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1162b99eb596STzung-Bi Shih 	}
1163b99eb596STzung-Bi Shih 
1164b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1165b99eb596STzung-Bi Shih 	{
1166b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1167b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1168b99eb596STzung-Bi Shih 
1169b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1170b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1171b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1172b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
1173b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1174b99eb596STzung-Bi Shih 
1175b99eb596STzung-Bi Shih 		KUNIT_EXPECT_FALSE(test, ec_dev->host_sleep_v1);
1176b99eb596STzung-Bi Shih 	}
1177b99eb596STzung-Bi Shih }
1178b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_no_host_sleep_return0(struct kunit * test)11798120febaSTzung-Bi Shih static void cros_ec_proto_test_query_all_no_host_sleep_return0(struct kunit *test)
11808120febaSTzung-Bi Shih {
11818120febaSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
11828120febaSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
11838120febaSTzung-Bi Shih 	struct ec_xfer_mock *mock;
11848120febaSTzung-Bi Shih 	int ret;
11858120febaSTzung-Bi Shih 
11868120febaSTzung-Bi Shih 	/* Set some garbage bytes. */
11878120febaSTzung-Bi Shih 	ec_dev->host_sleep_v1 = true;
11888120febaSTzung-Bi Shih 
11898120febaSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
11908120febaSTzung-Bi Shih 	{
11918120febaSTzung-Bi Shih 		struct ec_response_get_protocol_info *data;
11928120febaSTzung-Bi Shih 
11938120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
11948120febaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
11958120febaSTzung-Bi Shih 
11968120febaSTzung-Bi Shih 		/*
11978120febaSTzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
11988120febaSTzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
11998120febaSTzung-Bi Shih 		 */
12008120febaSTzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
12018120febaSTzung-Bi Shih 		data->max_request_packet_size = 0xbe;
12028120febaSTzung-Bi Shih 		data->max_response_packet_size = 0xef;
12038120febaSTzung-Bi Shih 	}
12048120febaSTzung-Bi Shih 
12058120febaSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
12068120febaSTzung-Bi Shih 	{
12078120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
12088120febaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
12098120febaSTzung-Bi Shih 	}
12108120febaSTzung-Bi Shih 
12118120febaSTzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
12128120febaSTzung-Bi Shih 	{
12138120febaSTzung-Bi Shih 		struct ec_response_get_cmd_versions *data;
12148120febaSTzung-Bi Shih 
12158120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
12168120febaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
12178120febaSTzung-Bi Shih 
12188120febaSTzung-Bi Shih 		/* In order to pollute next cros_ec_get_host_command_version_mask(). */
12198120febaSTzung-Bi Shih 		data = (struct ec_response_get_cmd_versions *)mock->o_data;
12208120febaSTzung-Bi Shih 		data->version_mask = 0xbeef;
12218120febaSTzung-Bi Shih 	}
12228120febaSTzung-Bi Shih 
12238120febaSTzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
12248120febaSTzung-Bi Shih 	{
12258120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
12268120febaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
12278120febaSTzung-Bi Shih 	}
12288120febaSTzung-Bi Shih 
12298120febaSTzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
12308120febaSTzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
12318120febaSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
12328120febaSTzung-Bi Shih 
12338120febaSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
12348120febaSTzung-Bi Shih 	{
12358120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
12368120febaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
12378120febaSTzung-Bi Shih 
12388120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
12398120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
12408120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
12418120febaSTzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
12428120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
12438120febaSTzung-Bi Shih 	}
12448120febaSTzung-Bi Shih 
12458120febaSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
12468120febaSTzung-Bi Shih 	{
12478120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
12488120febaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
12498120febaSTzung-Bi Shih 
12508120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
12518120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
12528120febaSTzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
12538120febaSTzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
12548120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
12558120febaSTzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
12568120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
12578120febaSTzung-Bi Shih 	}
12588120febaSTzung-Bi Shih 
12598120febaSTzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
12608120febaSTzung-Bi Shih 	{
12618120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
12628120febaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
12638120febaSTzung-Bi Shih 
12648120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
12658120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
12668120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
12678120febaSTzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
12688120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
12698120febaSTzung-Bi Shih 	}
12708120febaSTzung-Bi Shih 
12718120febaSTzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
12728120febaSTzung-Bi Shih 	{
12738120febaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
12748120febaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
12758120febaSTzung-Bi Shih 
12768120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
12778120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
12788120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
12798120febaSTzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
12808120febaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
12818120febaSTzung-Bi Shih 
12828120febaSTzung-Bi Shih 		KUNIT_EXPECT_FALSE(test, ec_dev->host_sleep_v1);
12838120febaSTzung-Bi Shih 	}
12848120febaSTzung-Bi Shih }
12858120febaSTzung-Bi Shih 
cros_ec_proto_test_query_all_default_wake_mask_return_error(struct kunit * test)1286b99eb596STzung-Bi Shih static void cros_ec_proto_test_query_all_default_wake_mask_return_error(struct kunit *test)
1287b99eb596STzung-Bi Shih {
1288b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1289b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1290b99eb596STzung-Bi Shih 	struct ec_xfer_mock *mock;
1291b99eb596STzung-Bi Shih 	int ret;
1292b99eb596STzung-Bi Shih 
1293b99eb596STzung-Bi Shih 	/* Set some garbage bytes. */
1294b99eb596STzung-Bi Shih 	ec_dev->host_event_wake_mask = U32_MAX;
1295b99eb596STzung-Bi Shih 
1296b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
1297b99eb596STzung-Bi Shih 	{
1298b99eb596STzung-Bi Shih 		struct ec_response_get_protocol_info *data;
1299b99eb596STzung-Bi Shih 
1300b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1301b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1302b99eb596STzung-Bi Shih 
1303b99eb596STzung-Bi Shih 		/*
1304b99eb596STzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
1305b99eb596STzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
1306b99eb596STzung-Bi Shih 		 */
1307b99eb596STzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
1308b99eb596STzung-Bi Shih 		data->max_request_packet_size = 0xbe;
1309b99eb596STzung-Bi Shih 		data->max_response_packet_size = 0xef;
1310b99eb596STzung-Bi Shih 	}
1311b99eb596STzung-Bi Shih 
1312b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
1313b99eb596STzung-Bi Shih 	{
1314b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1315b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1316b99eb596STzung-Bi Shih 	}
1317b99eb596STzung-Bi Shih 
1318b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1319b99eb596STzung-Bi Shih 	{
1320b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1321b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1322b99eb596STzung-Bi Shih 	}
1323b99eb596STzung-Bi Shih 
1324b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1325b99eb596STzung-Bi Shih 	{
1326b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1327b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1328b99eb596STzung-Bi Shih 	}
1329b99eb596STzung-Bi Shih 
1330b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_event_wake_mask(). */
1331b99eb596STzung-Bi Shih 	{
1332b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1333b99eb596STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1334b99eb596STzung-Bi Shih 	}
1335b99eb596STzung-Bi Shih 
1336b99eb596STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
1337b99eb596STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
1338b99eb596STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
1339b99eb596STzung-Bi Shih 
1340b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
1341b99eb596STzung-Bi Shih 	{
1342b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1343b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1344b99eb596STzung-Bi Shih 
1345b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1346b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1347b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1348b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
1349b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1350b99eb596STzung-Bi Shih 	}
1351b99eb596STzung-Bi Shih 
1352b4d0836eSTzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
1353b99eb596STzung-Bi Shih 	{
1354b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1355b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1356b99eb596STzung-Bi Shih 
1357b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1358b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
13593db0c9e5STzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
13603db0c9e5STzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
1361b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1362b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
1363b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1364b99eb596STzung-Bi Shih 	}
1365b99eb596STzung-Bi Shih 
1366b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1367b99eb596STzung-Bi Shih 	{
1368b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1369b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1370b99eb596STzung-Bi Shih 
1371b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1372b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1373b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1374b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
1375b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1376b99eb596STzung-Bi Shih 	}
1377b99eb596STzung-Bi Shih 
1378b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1379b99eb596STzung-Bi Shih 	{
1380b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1381b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1382b99eb596STzung-Bi Shih 
1383b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1384b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1385b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1386b99eb596STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
1387b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1388b99eb596STzung-Bi Shih 	}
1389b99eb596STzung-Bi Shih 
1390b99eb596STzung-Bi Shih 	/* For cros_ec_get_host_event_wake_mask(). */
1391b99eb596STzung-Bi Shih 	{
1392b99eb596STzung-Bi Shih 		u32 mask;
1393b99eb596STzung-Bi Shih 
1394b99eb596STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1395b99eb596STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1396b99eb596STzung-Bi Shih 
1397b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1398b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
1399b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
1400b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1401b99eb596STzung-Bi Shih 
1402b99eb596STzung-Bi Shih 		mask = ec_dev->host_event_wake_mask;
1403b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), 0);
1404b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED), 0);
1405b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW), 0);
1406b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL), 0);
1407b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY), 0);
1408b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU), 0);
1409b99eb596STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS), 0);
1410b99eb596STzung-Bi Shih 	}
1411b99eb596STzung-Bi Shih }
1412b99eb596STzung-Bi Shih 
cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit * test)1413e4377229STzung-Bi Shih static void cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit *test)
1414e4377229STzung-Bi Shih {
1415e4377229STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1416e4377229STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1417e4377229STzung-Bi Shih 	struct ec_xfer_mock *mock;
1418e4377229STzung-Bi Shih 	int ret;
1419e4377229STzung-Bi Shih 
1420e4377229STzung-Bi Shih 	/* Set some garbage bytes. */
1421e4377229STzung-Bi Shih 	ec_dev->host_event_wake_mask = U32_MAX;
1422e4377229STzung-Bi Shih 
1423e4377229STzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
1424e4377229STzung-Bi Shih 	{
1425e4377229STzung-Bi Shih 		struct ec_response_get_protocol_info *data;
1426e4377229STzung-Bi Shih 
1427e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1428e4377229STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1429e4377229STzung-Bi Shih 
1430e4377229STzung-Bi Shih 		/*
1431e4377229STzung-Bi Shih 		 * Although it doesn't check the value, provides valid sizes so that
1432e4377229STzung-Bi Shih 		 * cros_ec_query_all() allocates din and dout correctly.
1433e4377229STzung-Bi Shih 		 */
1434e4377229STzung-Bi Shih 		data = (struct ec_response_get_protocol_info *)mock->o_data;
1435e4377229STzung-Bi Shih 		data->max_request_packet_size = 0xbe;
1436e4377229STzung-Bi Shih 		data->max_response_packet_size = 0xef;
1437e4377229STzung-Bi Shih 	}
1438e4377229STzung-Bi Shih 
1439e4377229STzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
1440e4377229STzung-Bi Shih 	{
1441e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1442e4377229STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1443e4377229STzung-Bi Shih 	}
1444e4377229STzung-Bi Shih 
1445e4377229STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1446e4377229STzung-Bi Shih 	{
1447e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1448e4377229STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1449e4377229STzung-Bi Shih 	}
1450e4377229STzung-Bi Shih 
1451e4377229STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1452e4377229STzung-Bi Shih 	{
1453e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1454e4377229STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1455e4377229STzung-Bi Shih 	}
1456e4377229STzung-Bi Shih 
1457e4377229STzung-Bi Shih 	/* For get_host_event_wake_mask(). */
1458e4377229STzung-Bi Shih 	{
1459e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
1460e4377229STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1461e4377229STzung-Bi Shih 	}
1462e4377229STzung-Bi Shih 
1463e4377229STzung-Bi Shih 	cros_ec_proto_test_query_all_pretest(test);
1464e4377229STzung-Bi Shih 	ret = cros_ec_query_all(ec_dev);
1465e4377229STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
1466e4377229STzung-Bi Shih 
1467e4377229STzung-Bi Shih 	/* For cros_ec_get_proto_info() without passthru. */
1468e4377229STzung-Bi Shih 	{
1469e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1470e4377229STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1471e4377229STzung-Bi Shih 
1472e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1473e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
1474e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1475e4377229STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
1476e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1477e4377229STzung-Bi Shih 	}
1478e4377229STzung-Bi Shih 
1479e4377229STzung-Bi Shih 	/* For cros_ec_get_proto_info() with passthru. */
1480e4377229STzung-Bi Shih 	{
1481e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1482e4377229STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1483e4377229STzung-Bi Shih 
1484e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1485e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command,
1486e4377229STzung-Bi Shih 				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
1487e4377229STzung-Bi Shih 				EC_CMD_GET_PROTOCOL_INFO);
1488e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1489e4377229STzung-Bi Shih 				sizeof(struct ec_response_get_protocol_info));
1490e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1491e4377229STzung-Bi Shih 	}
1492e4377229STzung-Bi Shih 
1493e4377229STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
1494e4377229STzung-Bi Shih 	{
1495e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1496e4377229STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1497e4377229STzung-Bi Shih 
1498e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1499e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1500e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1501e4377229STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
1502e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1503e4377229STzung-Bi Shih 	}
1504e4377229STzung-Bi Shih 
1505e4377229STzung-Bi Shih 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
1506e4377229STzung-Bi Shih 	{
1507e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1508e4377229STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1509e4377229STzung-Bi Shih 
1510e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1511e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
1512e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1513e4377229STzung-Bi Shih 				sizeof(struct ec_response_get_cmd_versions));
1514e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
1515e4377229STzung-Bi Shih 	}
1516e4377229STzung-Bi Shih 
1517e4377229STzung-Bi Shih 	/* For get_host_event_wake_mask(). */
1518e4377229STzung-Bi Shih 	{
1519e4377229STzung-Bi Shih 		u32 mask;
1520e4377229STzung-Bi Shih 
1521e4377229STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1522e4377229STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1523e4377229STzung-Bi Shih 
1524e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1525e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HOST_EVENT_GET_WAKE_MASK);
1526e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
1527e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1528e4377229STzung-Bi Shih 
1529e4377229STzung-Bi Shih 		mask = ec_dev->host_event_wake_mask;
1530e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), 0);
1531e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED), 0);
1532e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW), 0);
1533e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL), 0);
1534e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY), 0);
1535e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU), 0);
1536e4377229STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS), 0);
1537e4377229STzung-Bi Shih 	}
1538e4377229STzung-Bi Shih }
1539e4377229STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_normal(struct kunit * test)154082f4def2STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_normal(struct kunit *test)
154182f4def2STzung-Bi Shih {
154282f4def2STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
154382f4def2STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
154482f4def2STzung-Bi Shih 	struct ec_xfer_mock *mock;
154582f4def2STzung-Bi Shih 	int ret;
154682f4def2STzung-Bi Shih 	struct {
154782f4def2STzung-Bi Shih 		struct cros_ec_command msg;
154882f4def2STzung-Bi Shih 		u8 data[0x100];
154982f4def2STzung-Bi Shih 	} __packed buf;
155082f4def2STzung-Bi Shih 
155182f4def2STzung-Bi Shih 	ec_dev->max_request = 0xff;
155282f4def2STzung-Bi Shih 	ec_dev->max_response = 0xee;
155382f4def2STzung-Bi Shih 	ec_dev->max_passthru = 0xdd;
155482f4def2STzung-Bi Shih 
155582f4def2STzung-Bi Shih 	buf.msg.version = 0;
155682f4def2STzung-Bi Shih 	buf.msg.command = EC_CMD_HELLO;
155782f4def2STzung-Bi Shih 	buf.msg.insize = 4;
155882f4def2STzung-Bi Shih 	buf.msg.outsize = 2;
155982f4def2STzung-Bi Shih 	buf.data[0] = 0x55;
156082f4def2STzung-Bi Shih 	buf.data[1] = 0xaa;
156182f4def2STzung-Bi Shih 
156282f4def2STzung-Bi Shih 	{
156382f4def2STzung-Bi Shih 		u8 *data;
156482f4def2STzung-Bi Shih 
156582f4def2STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 4);
156682f4def2STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
156782f4def2STzung-Bi Shih 
156882f4def2STzung-Bi Shih 		data = (u8 *)mock->o_data;
156982f4def2STzung-Bi Shih 		data[0] = 0xaa;
157082f4def2STzung-Bi Shih 		data[1] = 0x55;
157182f4def2STzung-Bi Shih 		data[2] = 0xcc;
157282f4def2STzung-Bi Shih 		data[3] = 0x33;
157382f4def2STzung-Bi Shih 	}
157482f4def2STzung-Bi Shih 
157582f4def2STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
157682f4def2STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 4);
157782f4def2STzung-Bi Shih 
157882f4def2STzung-Bi Shih 	{
157982f4def2STzung-Bi Shih 		u8 *data;
158082f4def2STzung-Bi Shih 
158182f4def2STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
158282f4def2STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
158382f4def2STzung-Bi Shih 
158482f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
158582f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
158682f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, 4);
158782f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 2);
158882f4def2STzung-Bi Shih 
158982f4def2STzung-Bi Shih 		data = (u8 *)mock->i_data;
159082f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data[0], 0x55);
159182f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data[1], 0xaa);
159282f4def2STzung-Bi Shih 
159382f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, buf.data[0], 0xaa);
159482f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, buf.data[1], 0x55);
159582f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, buf.data[2], 0xcc);
159682f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, buf.data[3], 0x33);
159782f4def2STzung-Bi Shih 	}
159882f4def2STzung-Bi Shih }
159982f4def2STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_excess_msg_insize(struct kunit * test)160082f4def2STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_excess_msg_insize(struct kunit *test)
160182f4def2STzung-Bi Shih {
160282f4def2STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
160382f4def2STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
160482f4def2STzung-Bi Shih 	struct ec_xfer_mock *mock;
160582f4def2STzung-Bi Shih 	int ret;
160682f4def2STzung-Bi Shih 	struct {
160782f4def2STzung-Bi Shih 		struct cros_ec_command msg;
160882f4def2STzung-Bi Shih 		u8 data[0x100];
160982f4def2STzung-Bi Shih 	} __packed buf;
161082f4def2STzung-Bi Shih 
161182f4def2STzung-Bi Shih 	ec_dev->max_request = 0xff;
161282f4def2STzung-Bi Shih 	ec_dev->max_response = 0xee;
161382f4def2STzung-Bi Shih 	ec_dev->max_passthru = 0xdd;
161482f4def2STzung-Bi Shih 
161582f4def2STzung-Bi Shih 	buf.msg.version = 0;
161682f4def2STzung-Bi Shih 	buf.msg.command = EC_CMD_HELLO;
161782f4def2STzung-Bi Shih 	buf.msg.insize = 0xee + 1;
161882f4def2STzung-Bi Shih 	buf.msg.outsize = 2;
161982f4def2STzung-Bi Shih 
162082f4def2STzung-Bi Shih 	{
162182f4def2STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0xcc);
162282f4def2STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
162382f4def2STzung-Bi Shih 	}
162482f4def2STzung-Bi Shih 
162582f4def2STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
162682f4def2STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0xcc);
162782f4def2STzung-Bi Shih 
162882f4def2STzung-Bi Shih 	{
162982f4def2STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
163082f4def2STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
163182f4def2STzung-Bi Shih 
163282f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
163382f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO);
163482f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, 0xee);
163582f4def2STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 2);
163682f4def2STzung-Bi Shih 	}
163782f4def2STzung-Bi Shih }
163882f4def2STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru(struct kunit * test)163982f4def2STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru(struct kunit *test)
164082f4def2STzung-Bi Shih {
164182f4def2STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
164282f4def2STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
164382f4def2STzung-Bi Shih 	int ret;
164482f4def2STzung-Bi Shih 	struct {
164582f4def2STzung-Bi Shih 		struct cros_ec_command msg;
164682f4def2STzung-Bi Shih 		u8 data[0x100];
164782f4def2STzung-Bi Shih 	} __packed buf;
164882f4def2STzung-Bi Shih 
164982f4def2STzung-Bi Shih 	ec_dev->max_request = 0xff;
165082f4def2STzung-Bi Shih 	ec_dev->max_response = 0xee;
165182f4def2STzung-Bi Shih 	ec_dev->max_passthru = 0xdd;
165282f4def2STzung-Bi Shih 
165382f4def2STzung-Bi Shih 	buf.msg.version = 0;
165482f4def2STzung-Bi Shih 	buf.msg.command = EC_CMD_HELLO;
165582f4def2STzung-Bi Shih 	buf.msg.insize = 4;
165682f4def2STzung-Bi Shih 	buf.msg.outsize = 0xff + 1;
165782f4def2STzung-Bi Shih 
165882f4def2STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
165982f4def2STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EMSGSIZE);
166082f4def2STzung-Bi Shih }
166182f4def2STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru(struct kunit * test)166282f4def2STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru(struct kunit *test)
166382f4def2STzung-Bi Shih {
166482f4def2STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
166582f4def2STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
166682f4def2STzung-Bi Shih 	int ret;
166782f4def2STzung-Bi Shih 	struct {
166882f4def2STzung-Bi Shih 		struct cros_ec_command msg;
166982f4def2STzung-Bi Shih 		u8 data[0x100];
167082f4def2STzung-Bi Shih 	} __packed buf;
167182f4def2STzung-Bi Shih 
167282f4def2STzung-Bi Shih 	ec_dev->max_request = 0xff;
167382f4def2STzung-Bi Shih 	ec_dev->max_response = 0xee;
167482f4def2STzung-Bi Shih 	ec_dev->max_passthru = 0xdd;
167582f4def2STzung-Bi Shih 
167682f4def2STzung-Bi Shih 	buf.msg.version = 0;
167782f4def2STzung-Bi Shih 	buf.msg.command = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) + EC_CMD_HELLO;
167882f4def2STzung-Bi Shih 	buf.msg.insize = 4;
167982f4def2STzung-Bi Shih 	buf.msg.outsize = 0xdd + 1;
168082f4def2STzung-Bi Shih 
168182f4def2STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
168282f4def2STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EMSGSIZE);
168382f4def2STzung-Bi Shih }
168482f4def2STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_protocol_v3_normal(struct kunit * test)1685da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_protocol_v3_normal(struct kunit *test)
1686da95f691STzung-Bi Shih {
1687da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1688da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1689da95f691STzung-Bi Shih 	int ret;
1690da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1691da95f691STzung-Bi Shih 
1692da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1693da95f691STzung-Bi Shih 
1694da95f691STzung-Bi Shih 	ec_dev->proto_version = 3;
1695da95f691STzung-Bi Shih 	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
1696da95f691STzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1697da95f691STzung-Bi Shih 
1698da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1699da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
1700da95f691STzung-Bi Shih 
1701da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 0);
1702da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 1);
1703da95f691STzung-Bi Shih }
1704da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_protocol_v3_no_op(struct kunit * test)1705da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_protocol_v3_no_op(struct kunit *test)
1706da95f691STzung-Bi Shih {
1707da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1708da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1709da95f691STzung-Bi Shih 	int ret;
1710da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1711da95f691STzung-Bi Shih 
1712da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1713da95f691STzung-Bi Shih 
1714da95f691STzung-Bi Shih 	ec_dev->proto_version = 3;
1715da95f691STzung-Bi Shih 	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
1716da95f691STzung-Bi Shih 	ec_dev->pkt_xfer = NULL;
1717da95f691STzung-Bi Shih 
1718da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1719da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EIO);
1720da95f691STzung-Bi Shih }
1721da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_protocol_v2_normal(struct kunit * test)1722da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_protocol_v2_normal(struct kunit *test)
1723da95f691STzung-Bi Shih {
1724da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1725da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1726da95f691STzung-Bi Shih 	int ret;
1727da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1728da95f691STzung-Bi Shih 
1729da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1730da95f691STzung-Bi Shih 
1731da95f691STzung-Bi Shih 	ec_dev->proto_version = 2;
1732da95f691STzung-Bi Shih 	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
1733da95f691STzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1734da95f691STzung-Bi Shih 
1735da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1736da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
1737da95f691STzung-Bi Shih 
1738da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 1);
1739da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 0);
1740da95f691STzung-Bi Shih }
1741da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_protocol_v2_no_op(struct kunit * test)1742da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_protocol_v2_no_op(struct kunit *test)
1743da95f691STzung-Bi Shih {
1744da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1745da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1746da95f691STzung-Bi Shih 	int ret;
1747da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1748da95f691STzung-Bi Shih 
1749da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1750da95f691STzung-Bi Shih 
1751da95f691STzung-Bi Shih 	ec_dev->proto_version = 2;
1752da95f691STzung-Bi Shih 	ec_dev->cmd_xfer = NULL;
1753da95f691STzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1754da95f691STzung-Bi Shih 
1755da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1756da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EIO);
1757da95f691STzung-Bi Shih }
1758da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_in_progress_normal(struct kunit * test)1759da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_in_progress_normal(struct kunit *test)
1760da95f691STzung-Bi Shih {
1761da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1762da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1763da95f691STzung-Bi Shih 	struct ec_xfer_mock *mock;
1764da95f691STzung-Bi Shih 	int ret;
1765da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1766da95f691STzung-Bi Shih 
1767da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1768da95f691STzung-Bi Shih 
1769da95f691STzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1770da95f691STzung-Bi Shih 
1771da95f691STzung-Bi Shih 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1772da95f691STzung-Bi Shih 	{
1773da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1774da95f691STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1775da95f691STzung-Bi Shih 	}
1776da95f691STzung-Bi Shih 
1777da95f691STzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS. */
1778da95f691STzung-Bi Shih 	{
1779da95f691STzung-Bi Shih 		struct ec_response_get_comms_status *data;
1780da95f691STzung-Bi Shih 
1781da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1782da95f691STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1783da95f691STzung-Bi Shih 
1784da95f691STzung-Bi Shih 		data = (struct ec_response_get_comms_status *)mock->o_data;
1785da95f691STzung-Bi Shih 		data->flags = 0;
1786da95f691STzung-Bi Shih 	}
1787da95f691STzung-Bi Shih 
1788da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1789da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_comms_status));
1790da95f691STzung-Bi Shih 
1791da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, msg.result, EC_RES_SUCCESS);
1792da95f691STzung-Bi Shih 
1793da95f691STzung-Bi Shih 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1794da95f691STzung-Bi Shih 	{
1795da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1796da95f691STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1797da95f691STzung-Bi Shih 	}
1798da95f691STzung-Bi Shih 
1799da95f691STzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS. */
1800da95f691STzung-Bi Shih 	{
1801da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
1802da95f691STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
1803da95f691STzung-Bi Shih 
1804da95f691STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
1805da95f691STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_COMMS_STATUS);
1806da95f691STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
1807da95f691STzung-Bi Shih 				sizeof(struct ec_response_get_comms_status));
1808da95f691STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
1809da95f691STzung-Bi Shih 	}
1810da95f691STzung-Bi Shih 
1811da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
1812da95f691STzung-Bi Shih }
1813da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_in_progress_retries_eagain(struct kunit * test)1814da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_in_progress_retries_eagain(struct kunit *test)
1815da95f691STzung-Bi Shih {
1816da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1817da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1818da95f691STzung-Bi Shih 	struct ec_xfer_mock *mock;
1819da95f691STzung-Bi Shih 	int ret;
1820da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1821da95f691STzung-Bi Shih 
1822da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1823da95f691STzung-Bi Shih 
1824da95f691STzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1825da95f691STzung-Bi Shih 
1826da95f691STzung-Bi Shih 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1827da95f691STzung-Bi Shih 	{
1828da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1829da95f691STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1830da95f691STzung-Bi Shih 	}
1831da95f691STzung-Bi Shih 
1832da95f691STzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
1833da95f691STzung-Bi Shih 	cros_kunit_ec_xfer_mock_default_ret = -EAGAIN;
1834da95f691STzung-Bi Shih 
1835da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1836da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
1837da95f691STzung-Bi Shih 
1838da95f691STzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
1839da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 51);
1840da95f691STzung-Bi Shih }
1841da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing(struct kunit * test)1842da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing(struct kunit *test)
1843da95f691STzung-Bi Shih {
1844da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1845da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1846da95f691STzung-Bi Shih 	struct ec_xfer_mock *mock;
1847da95f691STzung-Bi Shih 	int ret;
1848da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1849da95f691STzung-Bi Shih 
1850da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1851da95f691STzung-Bi Shih 
1852da95f691STzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1853da95f691STzung-Bi Shih 
1854da95f691STzung-Bi Shih 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1855da95f691STzung-Bi Shih 	{
1856da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1857da95f691STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1858da95f691STzung-Bi Shih 	}
1859da95f691STzung-Bi Shih 
1860da95f691STzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
1861da95f691STzung-Bi Shih 	{
1862da95f691STzung-Bi Shih 		struct ec_response_get_comms_status *data;
1863da95f691STzung-Bi Shih 		int i;
1864da95f691STzung-Bi Shih 
1865da95f691STzung-Bi Shih 		for (i = 0; i < 50; ++i) {
1866da95f691STzung-Bi Shih 			mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
1867da95f691STzung-Bi Shih 			KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1868da95f691STzung-Bi Shih 
1869da95f691STzung-Bi Shih 			data = (struct ec_response_get_comms_status *)mock->o_data;
1870da95f691STzung-Bi Shih 			data->flags |= EC_COMMS_STATUS_PROCESSING;
1871da95f691STzung-Bi Shih 		}
1872da95f691STzung-Bi Shih 	}
1873da95f691STzung-Bi Shih 
1874da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
187500eb36d5STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
1876da95f691STzung-Bi Shih 
1877da95f691STzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
1878da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 51);
1879da95f691STzung-Bi Shih }
1880da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_in_progress_xfer_error(struct kunit * test)1881da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_in_progress_xfer_error(struct kunit *test)
1882da95f691STzung-Bi Shih {
1883da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1884da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1885da95f691STzung-Bi Shih 	struct ec_xfer_mock *mock;
1886da95f691STzung-Bi Shih 	int ret;
1887da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1888da95f691STzung-Bi Shih 
1889da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1890da95f691STzung-Bi Shih 
1891da95f691STzung-Bi Shih 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1892da95f691STzung-Bi Shih 	{
1893da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1894da95f691STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1895da95f691STzung-Bi Shih 	}
1896da95f691STzung-Bi Shih 
1897da95f691STzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS. */
1898da95f691STzung-Bi Shih 	{
1899da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, -EIO, EC_RES_SUCCESS, 0);
1900da95f691STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1901da95f691STzung-Bi Shih 	}
1902da95f691STzung-Bi Shih 
1903da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1904da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EIO);
1905da95f691STzung-Bi Shih }
1906da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_in_progress_return_error(struct kunit * test)1907da95f691STzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_in_progress_return_error(struct kunit *test)
1908da95f691STzung-Bi Shih {
1909da95f691STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
1910da95f691STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
1911da95f691STzung-Bi Shih 	struct ec_xfer_mock *mock;
1912da95f691STzung-Bi Shih 	int ret;
1913da95f691STzung-Bi Shih 	struct cros_ec_command msg;
1914da95f691STzung-Bi Shih 
1915da95f691STzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
1916da95f691STzung-Bi Shih 
1917da95f691STzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
1918da95f691STzung-Bi Shih 
1919da95f691STzung-Bi Shih 	/* For the first host command to return EC_RES_IN_PROGRESS. */
1920da95f691STzung-Bi Shih 	{
1921da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
1922da95f691STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1923da95f691STzung-Bi Shih 	}
1924da95f691STzung-Bi Shih 
1925da95f691STzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS. */
1926da95f691STzung-Bi Shih 	{
1927da95f691STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
1928da95f691STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
1929da95f691STzung-Bi Shih 	}
1930da95f691STzung-Bi Shih 
1931da95f691STzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
1932da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
1933da95f691STzung-Bi Shih 
1934da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, msg.result, EC_RES_INVALID_COMMAND);
1935da95f691STzung-Bi Shih 
1936da95f691STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
1937da95f691STzung-Bi Shih }
1938da95f691STzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_in_progress_return0(struct kunit * test)193982c9b7edSTzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_in_progress_return0(struct kunit *test)
194082c9b7edSTzung-Bi Shih {
194182c9b7edSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
194282c9b7edSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
194382c9b7edSTzung-Bi Shih 	struct ec_xfer_mock *mock;
194482c9b7edSTzung-Bi Shih 	int ret;
194582c9b7edSTzung-Bi Shih 	struct cros_ec_command msg;
194682c9b7edSTzung-Bi Shih 
194782c9b7edSTzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
194882c9b7edSTzung-Bi Shih 
194982c9b7edSTzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
195082c9b7edSTzung-Bi Shih 
195182c9b7edSTzung-Bi Shih 	/* For the first host command to return EC_RES_IN_PROGRESS. */
195282c9b7edSTzung-Bi Shih 	{
195382c9b7edSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
195482c9b7edSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
195582c9b7edSTzung-Bi Shih 	}
195682c9b7edSTzung-Bi Shih 
195782c9b7edSTzung-Bi Shih 	/* For EC_CMD_GET_COMMS_STATUS. */
195882c9b7edSTzung-Bi Shih 	{
195982c9b7edSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
196082c9b7edSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
196182c9b7edSTzung-Bi Shih 	}
196282c9b7edSTzung-Bi Shih 
196382c9b7edSTzung-Bi Shih 	ret = cros_ec_cmd_xfer(ec_dev, &msg);
196482c9b7edSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
196582c9b7edSTzung-Bi Shih 
196682c9b7edSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
196782c9b7edSTzung-Bi Shih }
196882c9b7edSTzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_status_normal(struct kunit * test)196974bed42fSTzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_status_normal(struct kunit *test)
197074bed42fSTzung-Bi Shih {
197174bed42fSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
197274bed42fSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
197374bed42fSTzung-Bi Shih 	struct ec_xfer_mock *mock;
197474bed42fSTzung-Bi Shih 	int ret;
197574bed42fSTzung-Bi Shih 	struct cros_ec_command msg;
197674bed42fSTzung-Bi Shih 
197774bed42fSTzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
197874bed42fSTzung-Bi Shih 
197974bed42fSTzung-Bi Shih 	/* For cros_ec_cmd_xfer(). */
198074bed42fSTzung-Bi Shih 	{
198174bed42fSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 0);
198274bed42fSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
198374bed42fSTzung-Bi Shih 	}
198474bed42fSTzung-Bi Shih 
198574bed42fSTzung-Bi Shih 	ret = cros_ec_cmd_xfer_status(ec_dev, &msg);
198674bed42fSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
198774bed42fSTzung-Bi Shih }
198874bed42fSTzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_status_xfer_error(struct kunit * test)198974bed42fSTzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_status_xfer_error(struct kunit *test)
199074bed42fSTzung-Bi Shih {
199174bed42fSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
199274bed42fSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
199374bed42fSTzung-Bi Shih 	struct ec_xfer_mock *mock;
199474bed42fSTzung-Bi Shih 	int ret;
199574bed42fSTzung-Bi Shih 	struct cros_ec_command msg;
199674bed42fSTzung-Bi Shih 
199774bed42fSTzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
199874bed42fSTzung-Bi Shih 
199974bed42fSTzung-Bi Shih 	/* For cros_ec_cmd_xfer(). */
200074bed42fSTzung-Bi Shih 	{
200174bed42fSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, -EPROTO, EC_RES_SUCCESS, 0);
200274bed42fSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
200374bed42fSTzung-Bi Shih 	}
200474bed42fSTzung-Bi Shih 
200574bed42fSTzung-Bi Shih 	ret = cros_ec_cmd_xfer_status(ec_dev, &msg);
200674bed42fSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
200774bed42fSTzung-Bi Shih }
200874bed42fSTzung-Bi Shih 
cros_ec_proto_test_cmd_xfer_status_return_error(struct kunit * test)20091242688fSTzung-Bi Shih static void cros_ec_proto_test_cmd_xfer_status_return_error(struct kunit *test)
20101242688fSTzung-Bi Shih {
20111242688fSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
20121242688fSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
20131242688fSTzung-Bi Shih 	struct ec_xfer_mock *mock;
20141242688fSTzung-Bi Shih 	int ret, i;
20151242688fSTzung-Bi Shih 	struct cros_ec_command msg;
20161242688fSTzung-Bi Shih 	static const int map[] = {
20171242688fSTzung-Bi Shih 		[EC_RES_SUCCESS] = 0,
20181242688fSTzung-Bi Shih 		[EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
20191242688fSTzung-Bi Shih 		[EC_RES_ERROR] = -EIO,
20201242688fSTzung-Bi Shih 		[EC_RES_INVALID_PARAM] = -EINVAL,
20211242688fSTzung-Bi Shih 		[EC_RES_ACCESS_DENIED] = -EACCES,
20221242688fSTzung-Bi Shih 		[EC_RES_INVALID_RESPONSE] = -EPROTO,
20231242688fSTzung-Bi Shih 		[EC_RES_INVALID_VERSION] = -ENOPROTOOPT,
20241242688fSTzung-Bi Shih 		[EC_RES_INVALID_CHECKSUM] = -EBADMSG,
20251242688fSTzung-Bi Shih 		/*
20261242688fSTzung-Bi Shih 		 * EC_RES_IN_PROGRESS is special because cros_ec_send_command() has extra logic to
20271242688fSTzung-Bi Shih 		 * handle it.  Note that default cros_kunit_ec_xfer_mock_default_ret == 0 thus
20281242688fSTzung-Bi Shih 		 * cros_ec_xfer_command() in cros_ec_wait_until_complete() returns 0.  As a result,
20291242688fSTzung-Bi Shih 		 * it returns -EPROTO without calling cros_ec_map_error().
20301242688fSTzung-Bi Shih 		 */
20311242688fSTzung-Bi Shih 		[EC_RES_IN_PROGRESS] = -EPROTO,
20321242688fSTzung-Bi Shih 		[EC_RES_UNAVAILABLE] = -ENODATA,
20331242688fSTzung-Bi Shih 		[EC_RES_TIMEOUT] = -ETIMEDOUT,
20341242688fSTzung-Bi Shih 		[EC_RES_OVERFLOW] = -EOVERFLOW,
20351242688fSTzung-Bi Shih 		[EC_RES_INVALID_HEADER] = -EBADR,
20361242688fSTzung-Bi Shih 		[EC_RES_REQUEST_TRUNCATED] = -EBADR,
20371242688fSTzung-Bi Shih 		[EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
20381242688fSTzung-Bi Shih 		[EC_RES_BUS_ERROR] = -EFAULT,
20391242688fSTzung-Bi Shih 		[EC_RES_BUSY] = -EBUSY,
20401242688fSTzung-Bi Shih 		[EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
20411242688fSTzung-Bi Shih 		[EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
20421242688fSTzung-Bi Shih 		[EC_RES_INVALID_DATA_CRC] = -EBADMSG,
20431242688fSTzung-Bi Shih 		[EC_RES_DUP_UNAVAILABLE] = -ENODATA,
20441242688fSTzung-Bi Shih 	};
20451242688fSTzung-Bi Shih 
20461242688fSTzung-Bi Shih 	memset(&msg, 0, sizeof(msg));
20471242688fSTzung-Bi Shih 
20481242688fSTzung-Bi Shih 	for (i = 0; i < ARRAY_SIZE(map); ++i) {
20491242688fSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, 0, i, 0);
20501242688fSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
20511242688fSTzung-Bi Shih 
20521242688fSTzung-Bi Shih 		ret = cros_ec_cmd_xfer_status(ec_dev, &msg);
20531242688fSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ret, map[i]);
20541242688fSTzung-Bi Shih 	}
20551242688fSTzung-Bi Shih }
20561242688fSTzung-Bi Shih 
cros_ec_proto_test_get_next_event_no_mkbp_event(struct kunit * test)20572b7ed927STzung-Bi Shih static void cros_ec_proto_test_get_next_event_no_mkbp_event(struct kunit *test)
20582b7ed927STzung-Bi Shih {
20592b7ed927STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
20602b7ed927STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
20612b7ed927STzung-Bi Shih 	struct ec_xfer_mock *mock;
20622b7ed927STzung-Bi Shih 	int ret;
20632b7ed927STzung-Bi Shih 	bool wake_event, more_events;
20642b7ed927STzung-Bi Shih 
20652b7ed927STzung-Bi Shih 	ec_dev->max_request = 0xff;
20662b7ed927STzung-Bi Shih 	ec_dev->max_response = 0xee;
20672b7ed927STzung-Bi Shih 	ec_dev->mkbp_event_supported = 0;
20682b7ed927STzung-Bi Shih 
20692b7ed927STzung-Bi Shih 	/* Set some garbage bytes. */
20702b7ed927STzung-Bi Shih 	wake_event = false;
20712b7ed927STzung-Bi Shih 	more_events = true;
20722b7ed927STzung-Bi Shih 
20732b7ed927STzung-Bi Shih 	/* For get_keyboard_state_event(). */
20742b7ed927STzung-Bi Shih 	{
20752b7ed927STzung-Bi Shih 		union ec_response_get_next_data_v1 *data;
20762b7ed927STzung-Bi Shih 
20772b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
20782b7ed927STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
20792b7ed927STzung-Bi Shih 
20802b7ed927STzung-Bi Shih 		data = (union ec_response_get_next_data_v1 *)mock->o_data;
20812b7ed927STzung-Bi Shih 		data->host_event = 0xbeef;
20822b7ed927STzung-Bi Shih 	}
20832b7ed927STzung-Bi Shih 
20842b7ed927STzung-Bi Shih 	ret = cros_ec_get_next_event(ec_dev, &wake_event, &more_events);
20852b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, sizeof(union ec_response_get_next_data_v1));
20862b7ed927STzung-Bi Shih 
20872b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_KEY_MATRIX);
20882b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.data.host_event, 0xbeef);
20892b7ed927STzung-Bi Shih 
20902b7ed927STzung-Bi Shih 	KUNIT_EXPECT_TRUE(test, wake_event);
20912b7ed927STzung-Bi Shih 	KUNIT_EXPECT_FALSE(test, more_events);
20922b7ed927STzung-Bi Shih 
20932b7ed927STzung-Bi Shih 	/* For get_keyboard_state_event(). */
20942b7ed927STzung-Bi Shih 	{
20952b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
20962b7ed927STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
20972b7ed927STzung-Bi Shih 
20982b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
20992b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_MKBP_STATE);
21002b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(union ec_response_get_next_data_v1));
21012b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
21022b7ed927STzung-Bi Shih 	}
21032b7ed927STzung-Bi Shih }
21042b7ed927STzung-Bi Shih 
cros_ec_proto_test_get_next_event_mkbp_event_ec_suspended(struct kunit * test)21052b7ed927STzung-Bi Shih static void cros_ec_proto_test_get_next_event_mkbp_event_ec_suspended(struct kunit *test)
21062b7ed927STzung-Bi Shih {
21072b7ed927STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
21082b7ed927STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
21092b7ed927STzung-Bi Shih 	int ret;
21102b7ed927STzung-Bi Shih 
21112b7ed927STzung-Bi Shih 	ec_dev->mkbp_event_supported = 1;
21122b7ed927STzung-Bi Shih 	ec_dev->suspended = true;
21132b7ed927STzung-Bi Shih 
21142b7ed927STzung-Bi Shih 	ret = cros_ec_get_next_event(ec_dev, NULL, NULL);
21152b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EHOSTDOWN);
21162b7ed927STzung-Bi Shih }
21172b7ed927STzung-Bi Shih 
cros_ec_proto_test_get_next_event_mkbp_event_version0(struct kunit * test)21182b7ed927STzung-Bi Shih static void cros_ec_proto_test_get_next_event_mkbp_event_version0(struct kunit *test)
21192b7ed927STzung-Bi Shih {
21202b7ed927STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
21212b7ed927STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
21222b7ed927STzung-Bi Shih 	struct ec_xfer_mock *mock;
21232b7ed927STzung-Bi Shih 	int ret;
21242b7ed927STzung-Bi Shih 	bool wake_event, more_events;
21252b7ed927STzung-Bi Shih 
21262b7ed927STzung-Bi Shih 	ec_dev->max_request = 0xff;
21272b7ed927STzung-Bi Shih 	ec_dev->max_response = 0xee;
21282b7ed927STzung-Bi Shih 	ec_dev->mkbp_event_supported = 1;
21292b7ed927STzung-Bi Shih 
21302b7ed927STzung-Bi Shih 	/* Set some garbage bytes. */
21312b7ed927STzung-Bi Shih 	wake_event = true;
21322b7ed927STzung-Bi Shih 	more_events = false;
21332b7ed927STzung-Bi Shih 
21342b7ed927STzung-Bi Shih 	/* For get_next_event_xfer(). */
21352b7ed927STzung-Bi Shih 	{
21362b7ed927STzung-Bi Shih 		struct ec_response_get_next_event *data;
21372b7ed927STzung-Bi Shih 
21382b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
21392b7ed927STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
21402b7ed927STzung-Bi Shih 
21412b7ed927STzung-Bi Shih 		data = (struct ec_response_get_next_event *)mock->o_data;
21422b7ed927STzung-Bi Shih 		data->event_type = EC_MKBP_EVENT_SENSOR_FIFO | EC_MKBP_HAS_MORE_EVENTS;
21432b7ed927STzung-Bi Shih 		data->data.sysrq = 0xbeef;
21442b7ed927STzung-Bi Shih 	}
21452b7ed927STzung-Bi Shih 
21462b7ed927STzung-Bi Shih 	ret = cros_ec_get_next_event(ec_dev, &wake_event, &more_events);
21472b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_next_event));
21482b7ed927STzung-Bi Shih 
21492b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_SENSOR_FIFO);
21502b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.data.sysrq, 0xbeef);
21512b7ed927STzung-Bi Shih 
21522b7ed927STzung-Bi Shih 	KUNIT_EXPECT_FALSE(test, wake_event);
21532b7ed927STzung-Bi Shih 	KUNIT_EXPECT_TRUE(test, more_events);
21542b7ed927STzung-Bi Shih 
21552b7ed927STzung-Bi Shih 	/* For get_next_event_xfer(). */
21562b7ed927STzung-Bi Shih 	{
21572b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
21582b7ed927STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
21592b7ed927STzung-Bi Shih 
21602b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
21612b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_NEXT_EVENT);
21622b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_get_next_event));
21632b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
21642b7ed927STzung-Bi Shih 	}
21652b7ed927STzung-Bi Shih }
21662b7ed927STzung-Bi Shih 
cros_ec_proto_test_get_next_event_mkbp_event_version2(struct kunit * test)21672b7ed927STzung-Bi Shih static void cros_ec_proto_test_get_next_event_mkbp_event_version2(struct kunit *test)
21682b7ed927STzung-Bi Shih {
21692b7ed927STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
21702b7ed927STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
21712b7ed927STzung-Bi Shih 	struct ec_xfer_mock *mock;
21722b7ed927STzung-Bi Shih 	int ret;
21732b7ed927STzung-Bi Shih 	bool wake_event, more_events;
21742b7ed927STzung-Bi Shih 
21752b7ed927STzung-Bi Shih 	ec_dev->max_request = 0xff;
21762b7ed927STzung-Bi Shih 	ec_dev->max_response = 0xee;
21772b7ed927STzung-Bi Shih 	ec_dev->mkbp_event_supported = 3;
21782b7ed927STzung-Bi Shih 
21792b7ed927STzung-Bi Shih 	/* Set some garbage bytes. */
21802b7ed927STzung-Bi Shih 	wake_event = false;
21812b7ed927STzung-Bi Shih 	more_events = true;
21822b7ed927STzung-Bi Shih 
21832b7ed927STzung-Bi Shih 	/* For get_next_event_xfer(). */
21842b7ed927STzung-Bi Shih 	{
21852b7ed927STzung-Bi Shih 		struct ec_response_get_next_event_v1 *data;
21862b7ed927STzung-Bi Shih 
21872b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
21882b7ed927STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
21892b7ed927STzung-Bi Shih 
21902b7ed927STzung-Bi Shih 		data = (struct ec_response_get_next_event_v1 *)mock->o_data;
21912b7ed927STzung-Bi Shih 		data->event_type = EC_MKBP_EVENT_FINGERPRINT;
21922b7ed927STzung-Bi Shih 		data->data.sysrq = 0xbeef;
21932b7ed927STzung-Bi Shih 	}
21942b7ed927STzung-Bi Shih 
21952b7ed927STzung-Bi Shih 	ret = cros_ec_get_next_event(ec_dev, &wake_event, &more_events);
21962b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_next_event_v1));
21972b7ed927STzung-Bi Shih 
21982b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_FINGERPRINT);
21992b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.data.sysrq, 0xbeef);
22002b7ed927STzung-Bi Shih 
22012b7ed927STzung-Bi Shih 	KUNIT_EXPECT_TRUE(test, wake_event);
22022b7ed927STzung-Bi Shih 	KUNIT_EXPECT_FALSE(test, more_events);
22032b7ed927STzung-Bi Shih 
22042b7ed927STzung-Bi Shih 	/* For get_next_event_xfer(). */
22052b7ed927STzung-Bi Shih 	{
22062b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
22072b7ed927STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
22082b7ed927STzung-Bi Shih 
22092b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 2);
22102b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_NEXT_EVENT);
22112b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
22122b7ed927STzung-Bi Shih 				sizeof(struct ec_response_get_next_event_v1));
22132b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
22142b7ed927STzung-Bi Shih 	}
22152b7ed927STzung-Bi Shih }
22162b7ed927STzung-Bi Shih 
cros_ec_proto_test_get_next_event_mkbp_event_host_event_rtc(struct kunit * test)22172b7ed927STzung-Bi Shih static void cros_ec_proto_test_get_next_event_mkbp_event_host_event_rtc(struct kunit *test)
22182b7ed927STzung-Bi Shih {
22192b7ed927STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
22202b7ed927STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
22212b7ed927STzung-Bi Shih 	struct ec_xfer_mock *mock;
22222b7ed927STzung-Bi Shih 	int ret;
22232b7ed927STzung-Bi Shih 	bool wake_event;
22242b7ed927STzung-Bi Shih 	struct ec_response_get_next_event_v1 *data;
22252b7ed927STzung-Bi Shih 
22262b7ed927STzung-Bi Shih 	ec_dev->max_request = 0xff;
22272b7ed927STzung-Bi Shih 	ec_dev->max_response = 0xee;
22282b7ed927STzung-Bi Shih 	ec_dev->mkbp_event_supported = 3;
22292b7ed927STzung-Bi Shih 	ec_dev->host_event_wake_mask = U32_MAX;
22302b7ed927STzung-Bi Shih 
22312b7ed927STzung-Bi Shih 	/* Set some garbage bytes. */
22322b7ed927STzung-Bi Shih 	wake_event = true;
22332b7ed927STzung-Bi Shih 
22342b7ed927STzung-Bi Shih 	/* For get_next_event_xfer(). */
22352b7ed927STzung-Bi Shih 	{
22362b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test,
22372b7ed927STzung-Bi Shih 						   sizeof(data->event_type) +
22382b7ed927STzung-Bi Shih 						   sizeof(data->data.host_event));
22392b7ed927STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
22402b7ed927STzung-Bi Shih 
22412b7ed927STzung-Bi Shih 		data = (struct ec_response_get_next_event_v1 *)mock->o_data;
22422b7ed927STzung-Bi Shih 		data->event_type = EC_MKBP_EVENT_HOST_EVENT;
22432b7ed927STzung-Bi Shih 		put_unaligned_le32(EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC), &data->data.host_event);
22442b7ed927STzung-Bi Shih 	}
22452b7ed927STzung-Bi Shih 
22462b7ed927STzung-Bi Shih 	ret = cros_ec_get_next_event(ec_dev, &wake_event, NULL);
22472b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, sizeof(data->event_type) + sizeof(data->data.host_event));
22482b7ed927STzung-Bi Shih 
22492b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_HOST_EVENT);
22502b7ed927STzung-Bi Shih 
22512b7ed927STzung-Bi Shih 	KUNIT_EXPECT_FALSE(test, wake_event);
22522b7ed927STzung-Bi Shih 
22532b7ed927STzung-Bi Shih 	/* For get_next_event_xfer(). */
22542b7ed927STzung-Bi Shih 	{
22552b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
22562b7ed927STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
22572b7ed927STzung-Bi Shih 
22582b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 2);
22592b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_NEXT_EVENT);
22602b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
22612b7ed927STzung-Bi Shih 				sizeof(struct ec_response_get_next_event_v1));
22622b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
22632b7ed927STzung-Bi Shih 	}
22642b7ed927STzung-Bi Shih }
22652b7ed927STzung-Bi Shih 
cros_ec_proto_test_get_next_event_mkbp_event_host_event_masked(struct kunit * test)22662b7ed927STzung-Bi Shih static void cros_ec_proto_test_get_next_event_mkbp_event_host_event_masked(struct kunit *test)
22672b7ed927STzung-Bi Shih {
22682b7ed927STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
22692b7ed927STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
22702b7ed927STzung-Bi Shih 	struct ec_xfer_mock *mock;
22712b7ed927STzung-Bi Shih 	int ret;
22722b7ed927STzung-Bi Shih 	bool wake_event;
22732b7ed927STzung-Bi Shih 	struct ec_response_get_next_event_v1 *data;
22742b7ed927STzung-Bi Shih 
22752b7ed927STzung-Bi Shih 	ec_dev->max_request = 0xff;
22762b7ed927STzung-Bi Shih 	ec_dev->max_response = 0xee;
22772b7ed927STzung-Bi Shih 	ec_dev->mkbp_event_supported = 3;
22782b7ed927STzung-Bi Shih 	ec_dev->host_event_wake_mask = U32_MAX & ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED);
22792b7ed927STzung-Bi Shih 
22802b7ed927STzung-Bi Shih 	/* Set some garbage bytes. */
22812b7ed927STzung-Bi Shih 	wake_event = true;
22822b7ed927STzung-Bi Shih 
22832b7ed927STzung-Bi Shih 	/* For get_next_event_xfer(). */
22842b7ed927STzung-Bi Shih 	{
22852b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test,
22862b7ed927STzung-Bi Shih 						   sizeof(data->event_type) +
22872b7ed927STzung-Bi Shih 						   sizeof(data->data.host_event));
22882b7ed927STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
22892b7ed927STzung-Bi Shih 
22902b7ed927STzung-Bi Shih 		data = (struct ec_response_get_next_event_v1 *)mock->o_data;
22912b7ed927STzung-Bi Shih 		data->event_type = EC_MKBP_EVENT_HOST_EVENT;
22922b7ed927STzung-Bi Shih 		put_unaligned_le32(EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED),
22932b7ed927STzung-Bi Shih 				   &data->data.host_event);
22942b7ed927STzung-Bi Shih 	}
22952b7ed927STzung-Bi Shih 
22962b7ed927STzung-Bi Shih 	ret = cros_ec_get_next_event(ec_dev, &wake_event, NULL);
22972b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, sizeof(data->event_type) + sizeof(data->data.host_event));
22982b7ed927STzung-Bi Shih 
22992b7ed927STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ec_dev->event_data.event_type, EC_MKBP_EVENT_HOST_EVENT);
23002b7ed927STzung-Bi Shih 
23012b7ed927STzung-Bi Shih 	KUNIT_EXPECT_FALSE(test, wake_event);
23022b7ed927STzung-Bi Shih 
23032b7ed927STzung-Bi Shih 	/* For get_next_event_xfer(). */
23042b7ed927STzung-Bi Shih 	{
23052b7ed927STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
23062b7ed927STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
23072b7ed927STzung-Bi Shih 
23082b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 2);
23092b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_NEXT_EVENT);
23102b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize,
23112b7ed927STzung-Bi Shih 				sizeof(struct ec_response_get_next_event_v1));
23122b7ed927STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
23132b7ed927STzung-Bi Shih 	}
23142b7ed927STzung-Bi Shih }
23152b7ed927STzung-Bi Shih 
cros_ec_proto_test_get_host_event_no_mkbp_event(struct kunit * test)23167cb1eb82STzung-Bi Shih static void cros_ec_proto_test_get_host_event_no_mkbp_event(struct kunit *test)
23177cb1eb82STzung-Bi Shih {
23187cb1eb82STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
23197cb1eb82STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
23207cb1eb82STzung-Bi Shih 	int ret;
23217cb1eb82STzung-Bi Shih 
23227cb1eb82STzung-Bi Shih 	ec_dev->mkbp_event_supported = 0;
23237cb1eb82STzung-Bi Shih 
23247cb1eb82STzung-Bi Shih 	ret = cros_ec_get_host_event(ec_dev);
23257cb1eb82STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
23267cb1eb82STzung-Bi Shih }
23277cb1eb82STzung-Bi Shih 
cros_ec_proto_test_get_host_event_not_host_event(struct kunit * test)23287cb1eb82STzung-Bi Shih static void cros_ec_proto_test_get_host_event_not_host_event(struct kunit *test)
23297cb1eb82STzung-Bi Shih {
23307cb1eb82STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
23317cb1eb82STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
23327cb1eb82STzung-Bi Shih 	int ret;
23337cb1eb82STzung-Bi Shih 
23347cb1eb82STzung-Bi Shih 	ec_dev->mkbp_event_supported = 1;
23357cb1eb82STzung-Bi Shih 	ec_dev->event_data.event_type = EC_MKBP_EVENT_FINGERPRINT;
23367cb1eb82STzung-Bi Shih 
23377cb1eb82STzung-Bi Shih 	ret = cros_ec_get_host_event(ec_dev);
23387cb1eb82STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
23397cb1eb82STzung-Bi Shih }
23407cb1eb82STzung-Bi Shih 
cros_ec_proto_test_get_host_event_wrong_event_size(struct kunit * test)23417cb1eb82STzung-Bi Shih static void cros_ec_proto_test_get_host_event_wrong_event_size(struct kunit *test)
23427cb1eb82STzung-Bi Shih {
23437cb1eb82STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
23447cb1eb82STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
23457cb1eb82STzung-Bi Shih 	int ret;
23467cb1eb82STzung-Bi Shih 
23477cb1eb82STzung-Bi Shih 	ec_dev->mkbp_event_supported = 1;
23487cb1eb82STzung-Bi Shih 	ec_dev->event_data.event_type = EC_MKBP_EVENT_HOST_EVENT;
23497cb1eb82STzung-Bi Shih 	ec_dev->event_size = 0xff;
23507cb1eb82STzung-Bi Shih 
23517cb1eb82STzung-Bi Shih 	ret = cros_ec_get_host_event(ec_dev);
23527cb1eb82STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0);
23537cb1eb82STzung-Bi Shih }
23547cb1eb82STzung-Bi Shih 
cros_ec_proto_test_get_host_event_normal(struct kunit * test)23557cb1eb82STzung-Bi Shih static void cros_ec_proto_test_get_host_event_normal(struct kunit *test)
23567cb1eb82STzung-Bi Shih {
23577cb1eb82STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
23587cb1eb82STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
23597cb1eb82STzung-Bi Shih 	int ret;
23607cb1eb82STzung-Bi Shih 
23617cb1eb82STzung-Bi Shih 	ec_dev->mkbp_event_supported = 1;
23627cb1eb82STzung-Bi Shih 	ec_dev->event_data.event_type = EC_MKBP_EVENT_HOST_EVENT;
23637cb1eb82STzung-Bi Shih 	ec_dev->event_size = sizeof(ec_dev->event_data.data.host_event);
23647cb1eb82STzung-Bi Shih 	put_unaligned_le32(EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC),
23657cb1eb82STzung-Bi Shih 			   &ec_dev->event_data.data.host_event);
23667cb1eb82STzung-Bi Shih 
23677cb1eb82STzung-Bi Shih 	ret = cros_ec_get_host_event(ec_dev);
23687cb1eb82STzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC));
23697cb1eb82STzung-Bi Shih }
23707cb1eb82STzung-Bi Shih 
cros_ec_proto_test_check_features_cached(struct kunit * test)237100238864STzung-Bi Shih static void cros_ec_proto_test_check_features_cached(struct kunit *test)
237200238864STzung-Bi Shih {
237300238864STzung-Bi Shih 	int ret, i;
23746514bac4STzung-Bi Shih 	static struct cros_ec_dev ec;
237500238864STzung-Bi Shih 
237600238864STzung-Bi Shih 	ec.features.flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FINGERPRINT);
237700238864STzung-Bi Shih 	ec.features.flags[1] = EC_FEATURE_MASK_0(EC_FEATURE_SCP);
237800238864STzung-Bi Shih 
237900238864STzung-Bi Shih 	for (i = 0; i < EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK; ++i) {
238000238864STzung-Bi Shih 		ret = cros_ec_check_features(&ec, i);
238100238864STzung-Bi Shih 		switch (i) {
238200238864STzung-Bi Shih 		case EC_FEATURE_FINGERPRINT:
238300238864STzung-Bi Shih 		case EC_FEATURE_SCP:
238400238864STzung-Bi Shih 			KUNIT_EXPECT_TRUE(test, ret);
238500238864STzung-Bi Shih 			break;
238600238864STzung-Bi Shih 		default:
238700238864STzung-Bi Shih 			KUNIT_EXPECT_FALSE(test, ret);
238800238864STzung-Bi Shih 			break;
238900238864STzung-Bi Shih 		}
239000238864STzung-Bi Shih 	}
239100238864STzung-Bi Shih }
239200238864STzung-Bi Shih 
cros_ec_proto_test_check_features_not_cached(struct kunit * test)239300238864STzung-Bi Shih static void cros_ec_proto_test_check_features_not_cached(struct kunit *test)
239400238864STzung-Bi Shih {
239500238864STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
239600238864STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
239700238864STzung-Bi Shih 	struct ec_xfer_mock *mock;
239800238864STzung-Bi Shih 	int ret, i;
23996514bac4STzung-Bi Shih 	static struct cros_ec_dev ec;
240000238864STzung-Bi Shih 
240100238864STzung-Bi Shih 	ec_dev->max_request = 0xff;
240200238864STzung-Bi Shih 	ec_dev->max_response = 0xee;
240300238864STzung-Bi Shih 	ec.ec_dev = ec_dev;
240400238864STzung-Bi Shih 	ec.dev = ec_dev->dev;
240500238864STzung-Bi Shih 	ec.cmd_offset = 0;
240600238864STzung-Bi Shih 	ec.features.flags[0] = -1;
240700238864STzung-Bi Shih 	ec.features.flags[1] = -1;
240800238864STzung-Bi Shih 
240900238864STzung-Bi Shih 	/* For EC_CMD_GET_FEATURES. */
241000238864STzung-Bi Shih 	{
241100238864STzung-Bi Shih 		struct ec_response_get_features *data;
241200238864STzung-Bi Shih 
241300238864STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
241400238864STzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
241500238864STzung-Bi Shih 
241600238864STzung-Bi Shih 		data = (struct ec_response_get_features *)mock->o_data;
241700238864STzung-Bi Shih 		data->flags[0] = EC_FEATURE_MASK_0(EC_FEATURE_FINGERPRINT);
241800238864STzung-Bi Shih 		data->flags[1] = EC_FEATURE_MASK_0(EC_FEATURE_SCP);
241900238864STzung-Bi Shih 	}
242000238864STzung-Bi Shih 
242100238864STzung-Bi Shih 	for (i = 0; i < EC_FEATURE_TYPEC_MUX_REQUIRE_AP_ACK; ++i) {
242200238864STzung-Bi Shih 		ret = cros_ec_check_features(&ec, i);
242300238864STzung-Bi Shih 		switch (i) {
242400238864STzung-Bi Shih 		case EC_FEATURE_FINGERPRINT:
242500238864STzung-Bi Shih 		case EC_FEATURE_SCP:
242600238864STzung-Bi Shih 			KUNIT_EXPECT_TRUE(test, ret);
242700238864STzung-Bi Shih 			break;
242800238864STzung-Bi Shih 		default:
242900238864STzung-Bi Shih 			KUNIT_EXPECT_FALSE(test, ret);
243000238864STzung-Bi Shih 			break;
243100238864STzung-Bi Shih 		}
243200238864STzung-Bi Shih 	}
243300238864STzung-Bi Shih 
243400238864STzung-Bi Shih 	/* For EC_CMD_GET_FEATURES. */
243500238864STzung-Bi Shih 	{
243600238864STzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
243700238864STzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
243800238864STzung-Bi Shih 
243900238864STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
244000238864STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_FEATURES);
244100238864STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_get_features));
244200238864STzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
244300238864STzung-Bi Shih 	}
244400238864STzung-Bi Shih }
244500238864STzung-Bi Shih 
cros_ec_proto_test_get_sensor_count_normal(struct kunit * test)244633f0fdbaSTzung-Bi Shih static void cros_ec_proto_test_get_sensor_count_normal(struct kunit *test)
244733f0fdbaSTzung-Bi Shih {
244833f0fdbaSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
244933f0fdbaSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
245033f0fdbaSTzung-Bi Shih 	struct ec_xfer_mock *mock;
245133f0fdbaSTzung-Bi Shih 	int ret;
24526514bac4STzung-Bi Shih 	static struct cros_ec_dev ec;
245333f0fdbaSTzung-Bi Shih 
245433f0fdbaSTzung-Bi Shih 	ec_dev->max_request = 0xff;
245533f0fdbaSTzung-Bi Shih 	ec_dev->max_response = 0xee;
245633f0fdbaSTzung-Bi Shih 	ec.ec_dev = ec_dev;
245733f0fdbaSTzung-Bi Shih 	ec.dev = ec_dev->dev;
245833f0fdbaSTzung-Bi Shih 	ec.cmd_offset = 0;
245933f0fdbaSTzung-Bi Shih 
246033f0fdbaSTzung-Bi Shih 	/* For EC_CMD_MOTION_SENSE_CMD. */
246133f0fdbaSTzung-Bi Shih 	{
246233f0fdbaSTzung-Bi Shih 		struct ec_response_motion_sense *data;
246333f0fdbaSTzung-Bi Shih 
246433f0fdbaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
246533f0fdbaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
246633f0fdbaSTzung-Bi Shih 
246733f0fdbaSTzung-Bi Shih 		data = (struct ec_response_motion_sense *)mock->o_data;
246833f0fdbaSTzung-Bi Shih 		data->dump.sensor_count = 0xbf;
246933f0fdbaSTzung-Bi Shih 	}
247033f0fdbaSTzung-Bi Shih 
247133f0fdbaSTzung-Bi Shih 	ret = cros_ec_get_sensor_count(&ec);
247233f0fdbaSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 0xbf);
247333f0fdbaSTzung-Bi Shih 
247433f0fdbaSTzung-Bi Shih 	/* For EC_CMD_MOTION_SENSE_CMD. */
247533f0fdbaSTzung-Bi Shih 	{
247633f0fdbaSTzung-Bi Shih 		struct ec_params_motion_sense *data;
247733f0fdbaSTzung-Bi Shih 
247833f0fdbaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
247933f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
248033f0fdbaSTzung-Bi Shih 
248133f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 1);
248233f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_MOTION_SENSE_CMD);
248333f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_motion_sense));
248433f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
248533f0fdbaSTzung-Bi Shih 
248633f0fdbaSTzung-Bi Shih 		data = (struct ec_params_motion_sense *)mock->i_data;
248733f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->cmd, MOTIONSENSE_CMD_DUMP);
248833f0fdbaSTzung-Bi Shih 	}
248933f0fdbaSTzung-Bi Shih }
249033f0fdbaSTzung-Bi Shih 
cros_ec_proto_test_get_sensor_count_xfer_error(struct kunit * test)249133f0fdbaSTzung-Bi Shih static void cros_ec_proto_test_get_sensor_count_xfer_error(struct kunit *test)
249233f0fdbaSTzung-Bi Shih {
249333f0fdbaSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
249433f0fdbaSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
249533f0fdbaSTzung-Bi Shih 	struct ec_xfer_mock *mock;
249633f0fdbaSTzung-Bi Shih 	int ret;
24976514bac4STzung-Bi Shih 	static struct cros_ec_dev ec;
249833f0fdbaSTzung-Bi Shih 
249933f0fdbaSTzung-Bi Shih 	ec_dev->max_request = 0xff;
250033f0fdbaSTzung-Bi Shih 	ec_dev->max_response = 0xee;
250133f0fdbaSTzung-Bi Shih 	ec.ec_dev = ec_dev;
250233f0fdbaSTzung-Bi Shih 	ec.dev = ec_dev->dev;
250333f0fdbaSTzung-Bi Shih 	ec.cmd_offset = 0;
250433f0fdbaSTzung-Bi Shih 
250533f0fdbaSTzung-Bi Shih 	/* For EC_CMD_MOTION_SENSE_CMD. */
250633f0fdbaSTzung-Bi Shih 	{
250733f0fdbaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_addx(test, -EPROTO, EC_RES_SUCCESS, 0);
250833f0fdbaSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
250933f0fdbaSTzung-Bi Shih 	}
251033f0fdbaSTzung-Bi Shih 
251133f0fdbaSTzung-Bi Shih 	ret = cros_ec_get_sensor_count(&ec);
251233f0fdbaSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
251333f0fdbaSTzung-Bi Shih 
251433f0fdbaSTzung-Bi Shih 	/* For EC_CMD_MOTION_SENSE_CMD. */
251533f0fdbaSTzung-Bi Shih 	{
251633f0fdbaSTzung-Bi Shih 		struct ec_params_motion_sense *data;
251733f0fdbaSTzung-Bi Shih 
251833f0fdbaSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
251933f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
252033f0fdbaSTzung-Bi Shih 
252133f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 1);
252233f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_MOTION_SENSE_CMD);
252333f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_motion_sense));
252433f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
252533f0fdbaSTzung-Bi Shih 
252633f0fdbaSTzung-Bi Shih 		data = (struct ec_params_motion_sense *)mock->i_data;
252733f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data->cmd, MOTIONSENSE_CMD_DUMP);
252833f0fdbaSTzung-Bi Shih 	}
252933f0fdbaSTzung-Bi Shih }
253033f0fdbaSTzung-Bi Shih 
cros_ec_proto_test_get_sensor_count_legacy(struct kunit * test)253133f0fdbaSTzung-Bi Shih static void cros_ec_proto_test_get_sensor_count_legacy(struct kunit *test)
253233f0fdbaSTzung-Bi Shih {
253333f0fdbaSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
253433f0fdbaSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
253533f0fdbaSTzung-Bi Shih 	struct ec_xfer_mock *mock;
253633f0fdbaSTzung-Bi Shih 	int ret, i;
25376514bac4STzung-Bi Shih 	static struct cros_ec_dev ec;
253833f0fdbaSTzung-Bi Shih 	struct {
253933f0fdbaSTzung-Bi Shih 		u8 readmem_data;
254033f0fdbaSTzung-Bi Shih 		int expected_result;
254133f0fdbaSTzung-Bi Shih 	} test_data[] = {
254233f0fdbaSTzung-Bi Shih 		{ 0, 0 },
254333f0fdbaSTzung-Bi Shih 		{ EC_MEMMAP_ACC_STATUS_PRESENCE_BIT, 2 },
254433f0fdbaSTzung-Bi Shih 	};
254533f0fdbaSTzung-Bi Shih 
254633f0fdbaSTzung-Bi Shih 	ec_dev->max_request = 0xff;
254733f0fdbaSTzung-Bi Shih 	ec_dev->max_response = 0xee;
254833f0fdbaSTzung-Bi Shih 	ec_dev->cmd_readmem = cros_kunit_readmem_mock;
254933f0fdbaSTzung-Bi Shih 	ec.ec_dev = ec_dev;
255033f0fdbaSTzung-Bi Shih 	ec.dev = ec_dev->dev;
255133f0fdbaSTzung-Bi Shih 	ec.cmd_offset = 0;
255233f0fdbaSTzung-Bi Shih 
255333f0fdbaSTzung-Bi Shih 	for (i = 0; i < ARRAY_SIZE(test_data); ++i) {
255433f0fdbaSTzung-Bi Shih 		/* For EC_CMD_MOTION_SENSE_CMD. */
255533f0fdbaSTzung-Bi Shih 		{
255633f0fdbaSTzung-Bi Shih 			mock = cros_kunit_ec_xfer_mock_addx(test, -EPROTO, EC_RES_SUCCESS, 0);
255733f0fdbaSTzung-Bi Shih 			KUNIT_ASSERT_PTR_NE(test, mock, NULL);
255833f0fdbaSTzung-Bi Shih 		}
255933f0fdbaSTzung-Bi Shih 
256033f0fdbaSTzung-Bi Shih 		/* For readmem. */
256133f0fdbaSTzung-Bi Shih 		{
256233f0fdbaSTzung-Bi Shih 			cros_kunit_readmem_mock_data = kunit_kzalloc(test, 1, GFP_KERNEL);
256333f0fdbaSTzung-Bi Shih 			KUNIT_ASSERT_PTR_NE(test, cros_kunit_readmem_mock_data, NULL);
256433f0fdbaSTzung-Bi Shih 			cros_kunit_readmem_mock_data[0] = test_data[i].readmem_data;
256533f0fdbaSTzung-Bi Shih 
256633f0fdbaSTzung-Bi Shih 			cros_kunit_ec_xfer_mock_default_ret = 1;
256733f0fdbaSTzung-Bi Shih 		}
256833f0fdbaSTzung-Bi Shih 
256933f0fdbaSTzung-Bi Shih 		ret = cros_ec_get_sensor_count(&ec);
257033f0fdbaSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, ret, test_data[i].expected_result);
257133f0fdbaSTzung-Bi Shih 
257233f0fdbaSTzung-Bi Shih 		/* For EC_CMD_MOTION_SENSE_CMD. */
257333f0fdbaSTzung-Bi Shih 		{
257433f0fdbaSTzung-Bi Shih 			struct ec_params_motion_sense *data;
257533f0fdbaSTzung-Bi Shih 
257633f0fdbaSTzung-Bi Shih 			mock = cros_kunit_ec_xfer_mock_next();
257733f0fdbaSTzung-Bi Shih 			KUNIT_EXPECT_PTR_NE(test, mock, NULL);
257833f0fdbaSTzung-Bi Shih 
257933f0fdbaSTzung-Bi Shih 			KUNIT_EXPECT_EQ(test, mock->msg.version, 1);
258033f0fdbaSTzung-Bi Shih 			KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_MOTION_SENSE_CMD);
258133f0fdbaSTzung-Bi Shih 			KUNIT_EXPECT_EQ(test, mock->msg.insize,
258233f0fdbaSTzung-Bi Shih 					sizeof(struct ec_response_motion_sense));
258333f0fdbaSTzung-Bi Shih 			KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
258433f0fdbaSTzung-Bi Shih 
258533f0fdbaSTzung-Bi Shih 			data = (struct ec_params_motion_sense *)mock->i_data;
258633f0fdbaSTzung-Bi Shih 			KUNIT_EXPECT_EQ(test, data->cmd, MOTIONSENSE_CMD_DUMP);
258733f0fdbaSTzung-Bi Shih 		}
258833f0fdbaSTzung-Bi Shih 
258933f0fdbaSTzung-Bi Shih 		/* For readmem. */
259033f0fdbaSTzung-Bi Shih 		{
259133f0fdbaSTzung-Bi Shih 			KUNIT_EXPECT_EQ(test, cros_kunit_readmem_mock_offset, EC_MEMMAP_ACC_STATUS);
259233f0fdbaSTzung-Bi Shih 		}
259333f0fdbaSTzung-Bi Shih 	}
259433f0fdbaSTzung-Bi Shih }
259533f0fdbaSTzung-Bi Shih 
cros_ec_proto_test_ec_cmd(struct kunit * test)25969399b2cbSTzung-Bi Shih static void cros_ec_proto_test_ec_cmd(struct kunit *test)
25979399b2cbSTzung-Bi Shih {
25989399b2cbSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
25999399b2cbSTzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
26009399b2cbSTzung-Bi Shih 	struct ec_xfer_mock *mock;
26019399b2cbSTzung-Bi Shih 	int ret;
26029399b2cbSTzung-Bi Shih 	u8 out[3], in[2];
26039399b2cbSTzung-Bi Shih 
26049399b2cbSTzung-Bi Shih 	ec_dev->max_request = 0xff;
26059399b2cbSTzung-Bi Shih 	ec_dev->max_response = 0xee;
26069399b2cbSTzung-Bi Shih 
26079399b2cbSTzung-Bi Shih 	out[0] = 0xdd;
26089399b2cbSTzung-Bi Shih 	out[1] = 0xcc;
26099399b2cbSTzung-Bi Shih 	out[2] = 0xbb;
26109399b2cbSTzung-Bi Shih 
26119399b2cbSTzung-Bi Shih 	{
26129399b2cbSTzung-Bi Shih 		u8 *data;
26139399b2cbSTzung-Bi Shih 
26149399b2cbSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_add(test, 2);
26159399b2cbSTzung-Bi Shih 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
26169399b2cbSTzung-Bi Shih 
26179399b2cbSTzung-Bi Shih 		data = (u8 *)mock->o_data;
26189399b2cbSTzung-Bi Shih 		data[0] = 0xaa;
26199399b2cbSTzung-Bi Shih 		data[1] = 0x99;
26209399b2cbSTzung-Bi Shih 	}
26219399b2cbSTzung-Bi Shih 
26229399b2cbSTzung-Bi Shih 	ret = cros_ec_cmd(ec_dev, 0x88, 0x77, out, ARRAY_SIZE(out), in, ARRAY_SIZE(in));
26239399b2cbSTzung-Bi Shih 	KUNIT_EXPECT_EQ(test, ret, 2);
26249399b2cbSTzung-Bi Shih 
26259399b2cbSTzung-Bi Shih 	{
26269399b2cbSTzung-Bi Shih 		u8 *data;
26279399b2cbSTzung-Bi Shih 
26289399b2cbSTzung-Bi Shih 		mock = cros_kunit_ec_xfer_mock_next();
26299399b2cbSTzung-Bi Shih 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
26309399b2cbSTzung-Bi Shih 
26319399b2cbSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.version, 0x88);
26329399b2cbSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.command, 0x77);
26339399b2cbSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.insize, ARRAY_SIZE(in));
26349399b2cbSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, ARRAY_SIZE(out));
26359399b2cbSTzung-Bi Shih 
26369399b2cbSTzung-Bi Shih 		data = (u8 *)mock->i_data;
26379399b2cbSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data[0], 0xdd);
26389399b2cbSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data[1], 0xcc);
26399399b2cbSTzung-Bi Shih 		KUNIT_EXPECT_EQ(test, data[2], 0xbb);
26409399b2cbSTzung-Bi Shih 	}
26419399b2cbSTzung-Bi Shih }
26429399b2cbSTzung-Bi Shih 
cros_ec_proto_test_release(struct device * dev)2643b99eb596STzung-Bi Shih static void cros_ec_proto_test_release(struct device *dev)
2644b99eb596STzung-Bi Shih {
2645b99eb596STzung-Bi Shih }
2646b99eb596STzung-Bi Shih 
cros_ec_proto_test_init(struct kunit * test)2647db681eafSTzung-Bi Shih static int cros_ec_proto_test_init(struct kunit *test)
2648db681eafSTzung-Bi Shih {
2649db681eafSTzung-Bi Shih 	struct cros_ec_proto_test_priv *priv;
2650db681eafSTzung-Bi Shih 	struct cros_ec_device *ec_dev;
2651db681eafSTzung-Bi Shih 
2652db681eafSTzung-Bi Shih 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
2653db681eafSTzung-Bi Shih 	if (!priv)
2654db681eafSTzung-Bi Shih 		return -ENOMEM;
2655db681eafSTzung-Bi Shih 	test->priv = priv;
2656db681eafSTzung-Bi Shih 
2657db681eafSTzung-Bi Shih 	ec_dev = &priv->ec_dev;
2658db681eafSTzung-Bi Shih 	ec_dev->dout = (u8 *)priv->dout;
2659db681eafSTzung-Bi Shih 	ec_dev->dout_size = ARRAY_SIZE(priv->dout);
2660db681eafSTzung-Bi Shih 	ec_dev->din = (u8 *)priv->din;
2661db681eafSTzung-Bi Shih 	ec_dev->din_size = ARRAY_SIZE(priv->din);
2662db681eafSTzung-Bi Shih 	ec_dev->proto_version = EC_HOST_REQUEST_VERSION;
2663b99eb596STzung-Bi Shih 	ec_dev->dev = kunit_kzalloc(test, sizeof(*ec_dev->dev), GFP_KERNEL);
2664b99eb596STzung-Bi Shih 	if (!ec_dev->dev)
2665b99eb596STzung-Bi Shih 		return -ENOMEM;
2666b99eb596STzung-Bi Shih 	device_initialize(ec_dev->dev);
2667b99eb596STzung-Bi Shih 	dev_set_name(ec_dev->dev, "cros_ec_proto_test");
2668b99eb596STzung-Bi Shih 	ec_dev->dev->release = cros_ec_proto_test_release;
2669b99eb596STzung-Bi Shih 	ec_dev->cmd_xfer = cros_kunit_ec_xfer_mock;
2670b99eb596STzung-Bi Shih 	ec_dev->pkt_xfer = cros_kunit_ec_xfer_mock;
2671*20bcab57STzung-Bi Shih 	mutex_init(&ec_dev->lock);
2672db681eafSTzung-Bi Shih 
2673db681eafSTzung-Bi Shih 	priv->msg = (struct cros_ec_command *)priv->_msg;
2674db681eafSTzung-Bi Shih 
2675b99eb596STzung-Bi Shih 	cros_kunit_mock_reset();
2676b99eb596STzung-Bi Shih 
2677db681eafSTzung-Bi Shih 	return 0;
2678db681eafSTzung-Bi Shih }
2679db681eafSTzung-Bi Shih 
cros_ec_proto_test_exit(struct kunit * test)2680b99eb596STzung-Bi Shih static void cros_ec_proto_test_exit(struct kunit *test)
2681b99eb596STzung-Bi Shih {
2682b99eb596STzung-Bi Shih 	struct cros_ec_proto_test_priv *priv = test->priv;
2683b99eb596STzung-Bi Shih 	struct cros_ec_device *ec_dev = &priv->ec_dev;
2684b99eb596STzung-Bi Shih 
2685b99eb596STzung-Bi Shih 	put_device(ec_dev->dev);
2686b99eb596STzung-Bi Shih }
2687b99eb596STzung-Bi Shih 
2688db681eafSTzung-Bi Shih static struct kunit_case cros_ec_proto_test_cases[] = {
2689db681eafSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_legacy_normal),
2690db681eafSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_legacy_bad_msg_outsize),
2691db681eafSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_normal),
2692db681eafSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_prepare_tx_bad_msg_outsize),
26934319cbd4STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_check_result),
2694b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_normal),
2695b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return_error),
26963e97581eSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return0),
2697b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return_error),
26983e97581eSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return0),
2699b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_xfer_error),
2700b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return_error),
2701b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_data_error),
2702cce5d551STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return0),
2703b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp),
2704a8f77c63STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp_return_error),
27058120febaSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp_return0),
2706b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep),
27078120febaSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep_return0),
2708b99eb596STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return_error),
2709e4377229STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return0),
271082f4def2STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_normal),
271182f4def2STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_insize),
271282f4def2STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru),
271382f4def2STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru),
2714da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v3_normal),
2715da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v3_no_op),
2716da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v2_normal),
2717da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v2_no_op),
2718da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_normal),
2719da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_retries_eagain),
2720da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing),
2721da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_xfer_error),
2722da95f691STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return_error),
272382c9b7edSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return0),
272474bed42fSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_status_normal),
272574bed42fSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_status_xfer_error),
27261242688fSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_status_return_error),
27272b7ed927STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_next_event_no_mkbp_event),
27282b7ed927STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_ec_suspended),
27292b7ed927STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_version0),
27302b7ed927STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_version2),
27312b7ed927STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_host_event_rtc),
27322b7ed927STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_next_event_mkbp_event_host_event_masked),
27337cb1eb82STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_host_event_no_mkbp_event),
27347cb1eb82STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_host_event_not_host_event),
27357cb1eb82STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_host_event_wrong_event_size),
27367cb1eb82STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_host_event_normal),
273700238864STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_check_features_cached),
273800238864STzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_check_features_not_cached),
273933f0fdbaSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_sensor_count_normal),
274033f0fdbaSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_sensor_count_xfer_error),
274133f0fdbaSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_get_sensor_count_legacy),
27429399b2cbSTzung-Bi Shih 	KUNIT_CASE(cros_ec_proto_test_ec_cmd),
2743db681eafSTzung-Bi Shih 	{}
2744db681eafSTzung-Bi Shih };
2745db681eafSTzung-Bi Shih 
2746db681eafSTzung-Bi Shih static struct kunit_suite cros_ec_proto_test_suite = {
2747db681eafSTzung-Bi Shih 	.name = "cros_ec_proto_test",
2748db681eafSTzung-Bi Shih 	.init = cros_ec_proto_test_init,
2749b99eb596STzung-Bi Shih 	.exit = cros_ec_proto_test_exit,
2750db681eafSTzung-Bi Shih 	.test_cases = cros_ec_proto_test_cases,
2751db681eafSTzung-Bi Shih };
2752db681eafSTzung-Bi Shih 
2753db681eafSTzung-Bi Shih kunit_test_suite(cros_ec_proto_test_suite);
2754db681eafSTzung-Bi Shih 
2755db681eafSTzung-Bi Shih MODULE_LICENSE("GPL");
2756