1b99eb596STzung-Bi Shih // SPDX-License-Identifier: GPL-2.0
2b99eb596STzung-Bi Shih /*
3b99eb596STzung-Bi Shih * CrOS Kunit tests utilities.
4b99eb596STzung-Bi Shih */
5b99eb596STzung-Bi Shih
6b99eb596STzung-Bi Shih #include <kunit/test.h>
7b99eb596STzung-Bi Shih
8b99eb596STzung-Bi Shih #include <linux/list.h>
9b99eb596STzung-Bi Shih #include <linux/minmax.h>
10b99eb596STzung-Bi Shih #include <linux/platform_data/cros_ec_commands.h>
11b99eb596STzung-Bi Shih #include <linux/platform_data/cros_ec_proto.h>
12b99eb596STzung-Bi Shih
13b99eb596STzung-Bi Shih #include "cros_ec.h"
14b99eb596STzung-Bi Shih #include "cros_kunit_util.h"
15b99eb596STzung-Bi Shih
16*afef1e1aSTzung-Bi Shih int cros_kunit_ec_xfer_mock_default_result;
17b99eb596STzung-Bi Shih int cros_kunit_ec_xfer_mock_default_ret;
18da95f691STzung-Bi Shih int cros_kunit_ec_cmd_xfer_mock_called;
19da95f691STzung-Bi Shih int cros_kunit_ec_pkt_xfer_mock_called;
20b99eb596STzung-Bi Shih
21b99eb596STzung-Bi Shih static struct list_head cros_kunit_ec_xfer_mock_in;
22b99eb596STzung-Bi Shih static struct list_head cros_kunit_ec_xfer_mock_out;
23b99eb596STzung-Bi Shih
cros_kunit_ec_xfer_mock(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)24b99eb596STzung-Bi Shih int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
25b99eb596STzung-Bi Shih {
26b99eb596STzung-Bi Shih struct ec_xfer_mock *mock;
27b99eb596STzung-Bi Shih
28b99eb596STzung-Bi Shih mock = list_first_entry_or_null(&cros_kunit_ec_xfer_mock_in, struct ec_xfer_mock, list);
29*afef1e1aSTzung-Bi Shih if (!mock) {
30*afef1e1aSTzung-Bi Shih msg->result = cros_kunit_ec_xfer_mock_default_result;
31b99eb596STzung-Bi Shih return cros_kunit_ec_xfer_mock_default_ret;
32*afef1e1aSTzung-Bi Shih }
33b99eb596STzung-Bi Shih
34b99eb596STzung-Bi Shih list_del(&mock->list);
35b99eb596STzung-Bi Shih
36b99eb596STzung-Bi Shih memcpy(&mock->msg, msg, sizeof(*msg));
37b99eb596STzung-Bi Shih if (msg->outsize) {
38b99eb596STzung-Bi Shih mock->i_data = kunit_kzalloc(mock->test, msg->outsize, GFP_KERNEL);
39b99eb596STzung-Bi Shih if (mock->i_data)
40b99eb596STzung-Bi Shih memcpy(mock->i_data, msg->data, msg->outsize);
41b99eb596STzung-Bi Shih }
42b99eb596STzung-Bi Shih
43b99eb596STzung-Bi Shih msg->result = mock->result;
44b99eb596STzung-Bi Shih if (msg->insize)
45b99eb596STzung-Bi Shih memcpy(msg->data, mock->o_data, min(msg->insize, mock->o_data_len));
46b99eb596STzung-Bi Shih
47b99eb596STzung-Bi Shih list_add_tail(&mock->list, &cros_kunit_ec_xfer_mock_out);
48b99eb596STzung-Bi Shih
49b99eb596STzung-Bi Shih return mock->ret;
50b99eb596STzung-Bi Shih }
51b99eb596STzung-Bi Shih
cros_kunit_ec_cmd_xfer_mock(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)52da95f691STzung-Bi Shih int cros_kunit_ec_cmd_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
53da95f691STzung-Bi Shih {
54da95f691STzung-Bi Shih ++cros_kunit_ec_cmd_xfer_mock_called;
55da95f691STzung-Bi Shih return cros_kunit_ec_xfer_mock(ec_dev, msg);
56da95f691STzung-Bi Shih }
57da95f691STzung-Bi Shih
cros_kunit_ec_pkt_xfer_mock(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)58da95f691STzung-Bi Shih int cros_kunit_ec_pkt_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
59da95f691STzung-Bi Shih {
60da95f691STzung-Bi Shih ++cros_kunit_ec_pkt_xfer_mock_called;
61da95f691STzung-Bi Shih return cros_kunit_ec_xfer_mock(ec_dev, msg);
62da95f691STzung-Bi Shih }
63da95f691STzung-Bi Shih
cros_kunit_ec_xfer_mock_add(struct kunit * test,size_t size)64b99eb596STzung-Bi Shih struct ec_xfer_mock *cros_kunit_ec_xfer_mock_add(struct kunit *test, size_t size)
65b99eb596STzung-Bi Shih {
66b99eb596STzung-Bi Shih return cros_kunit_ec_xfer_mock_addx(test, size, EC_RES_SUCCESS, size);
67b99eb596STzung-Bi Shih }
68b99eb596STzung-Bi Shih
cros_kunit_ec_xfer_mock_addx(struct kunit * test,int ret,int result,size_t size)69b99eb596STzung-Bi Shih struct ec_xfer_mock *cros_kunit_ec_xfer_mock_addx(struct kunit *test,
70b99eb596STzung-Bi Shih int ret, int result, size_t size)
71b99eb596STzung-Bi Shih {
72b99eb596STzung-Bi Shih struct ec_xfer_mock *mock;
73b99eb596STzung-Bi Shih
74b99eb596STzung-Bi Shih mock = kunit_kzalloc(test, sizeof(*mock), GFP_KERNEL);
75b99eb596STzung-Bi Shih if (!mock)
76b99eb596STzung-Bi Shih return NULL;
77b99eb596STzung-Bi Shih
78b99eb596STzung-Bi Shih list_add_tail(&mock->list, &cros_kunit_ec_xfer_mock_in);
79b99eb596STzung-Bi Shih mock->test = test;
80b99eb596STzung-Bi Shih
81b99eb596STzung-Bi Shih mock->ret = ret;
82b99eb596STzung-Bi Shih mock->result = result;
83b99eb596STzung-Bi Shih mock->o_data = kunit_kzalloc(test, size, GFP_KERNEL);
84b99eb596STzung-Bi Shih if (!mock->o_data)
85b99eb596STzung-Bi Shih return NULL;
86b99eb596STzung-Bi Shih mock->o_data_len = size;
87b99eb596STzung-Bi Shih
88b99eb596STzung-Bi Shih return mock;
89b99eb596STzung-Bi Shih }
90b99eb596STzung-Bi Shih
cros_kunit_ec_xfer_mock_next(void)91b99eb596STzung-Bi Shih struct ec_xfer_mock *cros_kunit_ec_xfer_mock_next(void)
92b99eb596STzung-Bi Shih {
93b99eb596STzung-Bi Shih struct ec_xfer_mock *mock;
94b99eb596STzung-Bi Shih
95b99eb596STzung-Bi Shih mock = list_first_entry_or_null(&cros_kunit_ec_xfer_mock_out, struct ec_xfer_mock, list);
96b99eb596STzung-Bi Shih if (mock)
97b99eb596STzung-Bi Shih list_del(&mock->list);
98b99eb596STzung-Bi Shih
99b99eb596STzung-Bi Shih return mock;
100b99eb596STzung-Bi Shih }
101b99eb596STzung-Bi Shih
10233f0fdbaSTzung-Bi Shih int cros_kunit_readmem_mock_offset;
10333f0fdbaSTzung-Bi Shih u8 *cros_kunit_readmem_mock_data;
10433f0fdbaSTzung-Bi Shih int cros_kunit_readmem_mock_ret;
10533f0fdbaSTzung-Bi Shih
cros_kunit_readmem_mock(struct cros_ec_device * ec_dev,unsigned int offset,unsigned int bytes,void * dest)10633f0fdbaSTzung-Bi Shih int cros_kunit_readmem_mock(struct cros_ec_device *ec_dev, unsigned int offset,
10733f0fdbaSTzung-Bi Shih unsigned int bytes, void *dest)
10833f0fdbaSTzung-Bi Shih {
10933f0fdbaSTzung-Bi Shih cros_kunit_readmem_mock_offset = offset;
11033f0fdbaSTzung-Bi Shih
11133f0fdbaSTzung-Bi Shih memcpy(dest, cros_kunit_readmem_mock_data, bytes);
11233f0fdbaSTzung-Bi Shih
11333f0fdbaSTzung-Bi Shih return cros_kunit_readmem_mock_ret;
11433f0fdbaSTzung-Bi Shih }
11533f0fdbaSTzung-Bi Shih
cros_kunit_mock_reset(void)116b99eb596STzung-Bi Shih void cros_kunit_mock_reset(void)
117b99eb596STzung-Bi Shih {
118*afef1e1aSTzung-Bi Shih cros_kunit_ec_xfer_mock_default_result = 0;
119b99eb596STzung-Bi Shih cros_kunit_ec_xfer_mock_default_ret = 0;
120da95f691STzung-Bi Shih cros_kunit_ec_cmd_xfer_mock_called = 0;
121da95f691STzung-Bi Shih cros_kunit_ec_pkt_xfer_mock_called = 0;
122b99eb596STzung-Bi Shih INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_in);
123b99eb596STzung-Bi Shih INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_out);
12433f0fdbaSTzung-Bi Shih
12533f0fdbaSTzung-Bi Shih cros_kunit_readmem_mock_offset = 0;
12633f0fdbaSTzung-Bi Shih cros_kunit_readmem_mock_data = NULL;
12733f0fdbaSTzung-Bi Shih cros_kunit_readmem_mock_ret = 0;
128b99eb596STzung-Bi Shih }
129b99eb596STzung-Bi Shih
130b99eb596STzung-Bi Shih MODULE_LICENSE("GPL");
131