1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CrOS Kunit tests utilities.
4  */
5 
6 #include <kunit/test.h>
7 
8 #include <linux/list.h>
9 #include <linux/minmax.h>
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 int cros_kunit_ec_xfer_mock_default_result;
17 int cros_kunit_ec_xfer_mock_default_ret;
18 int cros_kunit_ec_cmd_xfer_mock_called;
19 int cros_kunit_ec_pkt_xfer_mock_called;
20 
21 static struct list_head cros_kunit_ec_xfer_mock_in;
22 static struct list_head cros_kunit_ec_xfer_mock_out;
23 
cros_kunit_ec_xfer_mock(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)24 int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
25 {
26 	struct ec_xfer_mock *mock;
27 
28 	mock = list_first_entry_or_null(&cros_kunit_ec_xfer_mock_in, struct ec_xfer_mock, list);
29 	if (!mock) {
30 		msg->result = cros_kunit_ec_xfer_mock_default_result;
31 		return cros_kunit_ec_xfer_mock_default_ret;
32 	}
33 
34 	list_del(&mock->list);
35 
36 	memcpy(&mock->msg, msg, sizeof(*msg));
37 	if (msg->outsize) {
38 		mock->i_data = kunit_kzalloc(mock->test, msg->outsize, GFP_KERNEL);
39 		if (mock->i_data)
40 			memcpy(mock->i_data, msg->data, msg->outsize);
41 	}
42 
43 	msg->result = mock->result;
44 	if (msg->insize)
45 		memcpy(msg->data, mock->o_data, min(msg->insize, mock->o_data_len));
46 
47 	list_add_tail(&mock->list, &cros_kunit_ec_xfer_mock_out);
48 
49 	return mock->ret;
50 }
51 
cros_kunit_ec_cmd_xfer_mock(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)52 int cros_kunit_ec_cmd_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
53 {
54 	++cros_kunit_ec_cmd_xfer_mock_called;
55 	return cros_kunit_ec_xfer_mock(ec_dev, msg);
56 }
57 
cros_kunit_ec_pkt_xfer_mock(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)58 int cros_kunit_ec_pkt_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
59 {
60 	++cros_kunit_ec_pkt_xfer_mock_called;
61 	return cros_kunit_ec_xfer_mock(ec_dev, msg);
62 }
63 
cros_kunit_ec_xfer_mock_add(struct kunit * test,size_t size)64 struct ec_xfer_mock *cros_kunit_ec_xfer_mock_add(struct kunit *test, size_t size)
65 {
66 	return cros_kunit_ec_xfer_mock_addx(test, size, EC_RES_SUCCESS, size);
67 }
68 
cros_kunit_ec_xfer_mock_addx(struct kunit * test,int ret,int result,size_t size)69 struct ec_xfer_mock *cros_kunit_ec_xfer_mock_addx(struct kunit *test,
70 						  int ret, int result, size_t size)
71 {
72 	struct ec_xfer_mock *mock;
73 
74 	mock = kunit_kzalloc(test, sizeof(*mock), GFP_KERNEL);
75 	if (!mock)
76 		return NULL;
77 
78 	list_add_tail(&mock->list, &cros_kunit_ec_xfer_mock_in);
79 	mock->test = test;
80 
81 	mock->ret = ret;
82 	mock->result = result;
83 	mock->o_data = kunit_kzalloc(test, size, GFP_KERNEL);
84 	if (!mock->o_data)
85 		return NULL;
86 	mock->o_data_len = size;
87 
88 	return mock;
89 }
90 
cros_kunit_ec_xfer_mock_next(void)91 struct ec_xfer_mock *cros_kunit_ec_xfer_mock_next(void)
92 {
93 	struct ec_xfer_mock *mock;
94 
95 	mock = list_first_entry_or_null(&cros_kunit_ec_xfer_mock_out, struct ec_xfer_mock, list);
96 	if (mock)
97 		list_del(&mock->list);
98 
99 	return mock;
100 }
101 
102 int cros_kunit_readmem_mock_offset;
103 u8 *cros_kunit_readmem_mock_data;
104 int cros_kunit_readmem_mock_ret;
105 
cros_kunit_readmem_mock(struct cros_ec_device * ec_dev,unsigned int offset,unsigned int bytes,void * dest)106 int cros_kunit_readmem_mock(struct cros_ec_device *ec_dev, unsigned int offset,
107 			    unsigned int bytes, void *dest)
108 {
109 	cros_kunit_readmem_mock_offset = offset;
110 
111 	memcpy(dest, cros_kunit_readmem_mock_data, bytes);
112 
113 	return cros_kunit_readmem_mock_ret;
114 }
115 
cros_kunit_mock_reset(void)116 void cros_kunit_mock_reset(void)
117 {
118 	cros_kunit_ec_xfer_mock_default_result = 0;
119 	cros_kunit_ec_xfer_mock_default_ret = 0;
120 	cros_kunit_ec_cmd_xfer_mock_called = 0;
121 	cros_kunit_ec_pkt_xfer_mock_called = 0;
122 	INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_in);
123 	INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_out);
124 
125 	cros_kunit_readmem_mock_offset = 0;
126 	cros_kunit_readmem_mock_data = NULL;
127 	cros_kunit_readmem_mock_ret = 0;
128 }
129