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