1 // SPDX-License-Identifier: GPL-2.0
3 * CrOS Kunit tests utilities.
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>
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
);
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
));
38 mock
->i_data
= kunit_kzalloc(mock
->test
, msg
->outsize
, GFP_KERNEL
);
40 memcpy(mock
->i_data
, msg
->data
, msg
->outsize
);
43 msg
->result
= mock
->result
;
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
);
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
);
78 list_add_tail(&mock
->list
, &cros_kunit_ec_xfer_mock_in
);
82 mock
->result
= result
;
83 mock
->o_data
= kunit_kzalloc(test
, size
, GFP_KERNEL
);
86 mock
->o_data_len
= size
;
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
);
97 list_del(&mock
->list
);
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;