Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / platform / chrome / cros_ec_proto_test_util.c
blob65d328bcd6eb2d77ce4db845b3c6c39269d46622
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * CrOS Kunit tests utilities.
4 */
6 #include <kunit/test.h>
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>
13 #include "cros_ec.h"
14 #include "cros_ec_proto_test_util.h"
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;
21 static struct list_head cros_kunit_ec_xfer_mock_in;
22 static struct list_head cros_kunit_ec_xfer_mock_out;
24 int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
26 struct ec_xfer_mock *mock;
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;
34 list_del(&mock->list);
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);
43 msg->result = mock->result;
44 if (msg->insize)
45 memcpy(msg->data, mock->o_data, min(msg->insize, mock->o_data_len));
47 list_add_tail(&mock->list, &cros_kunit_ec_xfer_mock_out);
49 return mock->ret;
52 int cros_kunit_ec_cmd_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
54 ++cros_kunit_ec_cmd_xfer_mock_called;
55 return cros_kunit_ec_xfer_mock(ec_dev, msg);
58 int cros_kunit_ec_pkt_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
60 ++cros_kunit_ec_pkt_xfer_mock_called;
61 return cros_kunit_ec_xfer_mock(ec_dev, msg);
64 struct ec_xfer_mock *cros_kunit_ec_xfer_mock_add(struct kunit *test, size_t size)
66 return cros_kunit_ec_xfer_mock_addx(test, size, EC_RES_SUCCESS, size);
69 struct ec_xfer_mock *cros_kunit_ec_xfer_mock_addx(struct kunit *test,
70 int ret, int result, size_t size)
72 struct ec_xfer_mock *mock;
74 mock = kunit_kzalloc(test, sizeof(*mock), GFP_KERNEL);
75 if (!mock)
76 return NULL;
78 list_add_tail(&mock->list, &cros_kunit_ec_xfer_mock_in);
79 mock->test = test;
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;
88 return mock;
91 struct ec_xfer_mock *cros_kunit_ec_xfer_mock_next(void)
93 struct ec_xfer_mock *mock;
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);
99 return mock;
102 int cros_kunit_readmem_mock_offset;
103 u8 *cros_kunit_readmem_mock_data;
104 int cros_kunit_readmem_mock_ret;
106 int cros_kunit_readmem_mock(struct cros_ec_device *ec_dev, unsigned int offset,
107 unsigned int bytes, void *dest)
109 cros_kunit_readmem_mock_offset = offset;
111 memcpy(dest, cros_kunit_readmem_mock_data, bytes);
113 return cros_kunit_readmem_mock_ret;
116 void cros_kunit_mock_reset(void)
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);
125 cros_kunit_readmem_mock_offset = 0;
126 cros_kunit_readmem_mock_data = NULL;
127 cros_kunit_readmem_mock_ret = 0;