1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/* Copyright 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Tests for keyboard MKBP protocol
*/
#include "common.h"
#include "console.h"
#include "ec_commands.h"
#include "gpio.h"
#include "host_command.h"
#include "keyboard_mkbp.h"
#include "keyboard_protocol.h"
#include "keyboard_scan.h"
#include "test_util.h"
#include "util.h"
static uint8_t state[KEYBOARD_COLS_MAX];
static int ec_int_level;
static const char *action[2] = { "release", "press" };
/*****************************************************************************/
/* Mock functions */
void host_send_response(struct host_cmd_handler_args *args)
{
/* Do nothing */
}
void gpio_set_level(enum gpio_signal signal, int level)
{
if (signal == GPIO_EC_INT_L)
ec_int_level = !!level;
}
int lid_is_open(void)
{
return 1;
}
/*****************************************************************************/
/* Test utilities */
#define FIFO_EMPTY() (ec_int_level == 1)
#define FIFO_NOT_EMPTY() (ec_int_level == 0)
void clear_state(void)
{
memset(state, 0xff, KEYBOARD_COLS_MAX);
}
void set_state(int c, int r, int pressed)
{
uint8_t mask = (1 << r);
if (pressed)
state[c] &= ~mask;
else
state[c] |= mask;
}
int press_key(int c, int r, int pressed)
{
ccprintf("Input %s (%d, %d)\n", action[pressed], c, r);
set_state(c, r, pressed);
return mkbp_keyboard_add(state);
}
int verify_key(int c, int r, int pressed)
{
struct host_cmd_handler_args args;
struct ec_response_get_next_event event;
int i;
args.version = 0;
args.command = EC_CMD_GET_NEXT_EVENT;
args.params = NULL;
args.params_size = 0;
args.response = &event;
args.response_max = sizeof(event);
args.response_size = 0;
if (c >= 0 && r >= 0) {
ccprintf("Verify %s (%d, %d)\n", action[pressed], c, r);
set_state(c, r, pressed);
if (host_command_process(&args) != EC_RES_SUCCESS)
return 0;
for (i = 0; i < KEYBOARD_COLS_MAX; ++i)
if (event.data.key_matrix[i] != state[i])
return 0;
} else {
ccprintf("Verify no events available\n");
if (host_command_process(&args) != EC_RES_UNAVAILABLE)
return 0;
}
return 1;
}
int verify_key_v2(int c, int r, int pressed, int expect_more)
{
struct host_cmd_handler_args args;
struct ec_response_get_next_event_v1 event;
int i;
args.version = 2;
args.command = EC_CMD_GET_NEXT_EVENT;
args.params = NULL;
args.params_size = 0;
args.response = &event;
args.response_max = sizeof(event);
args.response_size = 0;
if (c >= 0 && r >= 0) {
ccprintf("Verify %s (%d, %d). Expect %smore.\n",
action[pressed], c, r, expect_more ? "" : "no ");
set_state(c, r, pressed);
if (host_command_process(&args) != EC_RES_SUCCESS)
return 0;
if (!!(event.event_type & EC_MKBP_HAS_MORE_EVENTS) !=
expect_more) {
ccprintf("Incorrect more events!\n");
return 0;
}
for (i = 0; i < KEYBOARD_COLS_MAX; ++i)
if (event.data.key_matrix[i] != state[i])
return 0;
} else {
ccprintf("Verify no events available\n");
if (host_command_process(&args) != EC_RES_UNAVAILABLE)
return 0;
}
return 1;
}
int mkbp_config(struct ec_params_mkbp_set_config params)
{
struct host_cmd_handler_args args;
args.version = 0;
args.command = EC_CMD_MKBP_SET_CONFIG;
args.params = ¶ms;
args.params_size = sizeof(params);
args.response = NULL;
args.response_max = 0;
args.response_size = 0;
return host_command_process(&args) == EC_RES_SUCCESS;
}
int set_fifo_size(int sz)
{
struct ec_params_mkbp_set_config params;
params.config.valid_mask = EC_MKBP_VALID_FIFO_MAX_DEPTH;
params.config.valid_flags = 0;
params.config.fifo_max_depth = sz;
return mkbp_config(params);
}
int set_kb_scan_enabled(int enabled)
{
struct ec_params_mkbp_set_config params;
params.config.valid_mask = 0;
params.config.valid_flags = EC_MKBP_FLAGS_ENABLE;
params.config.flags = (enabled ? EC_MKBP_FLAGS_ENABLE : 0);
return mkbp_config(params);
}
void clear_mkbp_events(void)
{
struct host_cmd_handler_args args;
struct ec_response_get_next_event event;
args.version = 0;
args.command = EC_CMD_GET_NEXT_EVENT;
args.params = NULL;
args.params_size = 0;
args.response = &event;
args.response_max = sizeof(event);
args.response_size = 0;
/*
* We should return EC_RES_UNAVAILABLE if there are no MKBP events left.
*/
while (host_command_process(&args) != EC_RES_UNAVAILABLE)
;
}
/*****************************************************************************/
/* Tests */
int single_key_press(void)
{
keyboard_clear_buffer();
clear_state();
TEST_ASSERT(press_key(0, 0, 1) == EC_SUCCESS);
TEST_ASSERT(FIFO_NOT_EMPTY());
TEST_ASSERT(press_key(0, 0, 0) == EC_SUCCESS);
TEST_ASSERT(FIFO_NOT_EMPTY());
clear_state();
TEST_ASSERT(verify_key(0, 0, 1));
TEST_ASSERT(FIFO_NOT_EMPTY());
TEST_ASSERT(verify_key(0, 0, 0));
TEST_ASSERT(FIFO_EMPTY());
return EC_SUCCESS;
}
int single_key_press_v2(void)
{
keyboard_clear_buffer();
clear_state();
TEST_ASSERT(press_key(0, 0, 1) == EC_SUCCESS);
TEST_ASSERT(FIFO_NOT_EMPTY());
TEST_ASSERT(press_key(0, 0, 0) == EC_SUCCESS);
TEST_ASSERT(FIFO_NOT_EMPTY());
clear_state();
TEST_ASSERT(verify_key_v2(0, 0, 1, 1));
TEST_ASSERT(FIFO_NOT_EMPTY());
TEST_ASSERT(verify_key_v2(0, 0, 0, 0));
TEST_ASSERT(FIFO_EMPTY());
return EC_SUCCESS;
}
int test_fifo_size(void)
{
keyboard_clear_buffer();
clear_state();
TEST_ASSERT(set_fifo_size(1));
TEST_ASSERT(press_key(0, 0, 1) == EC_SUCCESS);
TEST_ASSERT(press_key(0, 0, 0) == EC_ERROR_OVERFLOW);
clear_state();
TEST_ASSERT(verify_key(0, 0, 1));
TEST_ASSERT(FIFO_EMPTY());
/* Restore FIFO size */
TEST_ASSERT(set_fifo_size(100));
return EC_SUCCESS;
}
int test_enable(void)
{
keyboard_clear_buffer();
clear_state();
TEST_ASSERT(set_kb_scan_enabled(0));
TEST_ASSERT(press_key(0, 0, 1) == EC_SUCCESS);
TEST_ASSERT(FIFO_EMPTY());
TEST_ASSERT(set_kb_scan_enabled(1));
TEST_ASSERT(press_key(0, 0, 1) == EC_SUCCESS);
TEST_ASSERT(FIFO_NOT_EMPTY());
TEST_ASSERT(verify_key(0, 0, 1));
return EC_SUCCESS;
}
int fifo_underrun(void)
{
keyboard_clear_buffer();
clear_state();
TEST_ASSERT(press_key(0, 0, 1) == EC_SUCCESS);
clear_state();
TEST_ASSERT(verify_key(0, 0, 1));
/* When FIFO under run, host command reutns last known state */
TEST_ASSERT(verify_key(-1, -1, -1));
return EC_SUCCESS;
}
void run_test(int argc, const char **argv)
{
ec_int_level = 1;
test_reset();
/* Clear any pending events such as lid open. */
clear_mkbp_events();
RUN_TEST(single_key_press);
RUN_TEST(single_key_press_v2);
RUN_TEST(test_fifo_size);
RUN_TEST(test_enable);
RUN_TEST(fifo_underrun);
test_print_result();
}
|