summaryrefslogtreecommitdiff
path: root/test/kb_8042.c
blob: 9705b506fe3609fb1d6cd5f5ac5380024bc4025a (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* 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 "i8042_protocol.h"
#include "keyboard_8042.h"
#include "keyboard_protocol.h"
#include "keyboard_scan.h"
#include "lpc.h"
#include "power_button.h"
#include "system.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"

static const char *action[2] = {"release", "press"};

#define BUF_SIZE 16
static char lpc_char_buf[BUF_SIZE];
static unsigned int lpc_char_cnt;

/*****************************************************************************/
/* Mock functions */

int lid_is_open(void)
{
	return 1;
}

void lpc_keyboard_put_char(uint8_t chr, int send_irq)
{
	lpc_char_buf[lpc_char_cnt++] = chr;
}

/*****************************************************************************/
/* Test utilities */

static void press_key(int c, int r, int pressed)
{
	ccprintf("Input %s (%d, %d)\n", action[pressed], c, r);
	keyboard_state_changed(r, c, pressed);
}

static void enable_keystroke(int enabled)
{
	uint8_t data = enabled ? I8042_CMD_ENABLE : I8042_CMD_RESET_DIS;
	keyboard_host_write(data, 0);
	msleep(30);
}

static void reset_8042(void)
{
	keyboard_host_write(I8042_CMD_RESET_DEF, 0);
	msleep(30);
}

static void set_typematic(uint8_t val)
{
	keyboard_host_write(I8042_CMD_SETREP, 0);
	msleep(30);
	keyboard_host_write(val, 0);
	msleep(30);
}

static void set_scancode(uint8_t s)
{
	keyboard_host_write(I8042_CMD_SSCANSET, 0);
	msleep(30);
	keyboard_host_write(s, 0);
	msleep(30);
}

static void write_cmd_byte(uint8_t val)
{
	keyboard_host_write(I8042_WRITE_CMD_BYTE, 1);
	msleep(30);
	keyboard_host_write(val, 0);
	msleep(30);
}

static uint8_t read_cmd_byte(void)
{
	lpc_char_cnt = 0;
	keyboard_host_write(I8042_READ_CMD_BYTE, 1);
	msleep(30);
	return lpc_char_buf[0];
}

static int __verify_lpc_char(char *arr, unsigned int sz, int delay_ms)
{
	int i;

	lpc_char_cnt = 0;
	for (i = 0; i < sz; ++i)
		lpc_char_buf[i] = 0;
	msleep(delay_ms);
	TEST_ASSERT_ARRAY_EQ(arr, lpc_char_buf, sz);
	return EC_SUCCESS;
}

#define VERIFY_LPC_CHAR(s) \
	TEST_ASSERT(__verify_lpc_char(s, strlen(s), 30) == EC_SUCCESS)
#define VERIFY_LPC_CHAR_DELAY(s, t) \
	TEST_ASSERT(__verify_lpc_char(s, strlen(s), t) == EC_SUCCESS)

static int __verify_no_char(void)
{
	lpc_char_cnt = 0;
	msleep(30);
	TEST_CHECK(lpc_char_cnt == 0);
}

#define VERIFY_NO_CHAR() TEST_ASSERT(__verify_no_char() == EC_SUCCESS)

/*****************************************************************************/
/* Tests */

static int test_single_key_press(void)
{
	enable_keystroke(1);
	press_key(1, 1, 1);
	VERIFY_LPC_CHAR("\x01");
	press_key(1, 1, 0);
	VERIFY_LPC_CHAR("\x81");

	press_key(12, 6, 1);
	VERIFY_LPC_CHAR("\xe0\x4d");
	press_key(12, 6, 0);
	VERIFY_LPC_CHAR("\xe0\xcd");

	return EC_SUCCESS;
}

static int test_disable_keystroke(void)
{
	enable_keystroke(0);
	press_key(1, 1, 1);
	VERIFY_NO_CHAR();
	press_key(1, 1, 0);
	VERIFY_NO_CHAR();

	return EC_SUCCESS;
}

static int test_typematic(void)
{
	enable_keystroke(1);

	/*
	 * 250ms delay, 8 chars / sec.
	 */
	set_typematic(0xf);

	press_key(1, 1, 1);
	VERIFY_LPC_CHAR_DELAY("\x01\x01\x01\x01\x01", 650);
	press_key(1, 1, 0);
	VERIFY_LPC_CHAR_DELAY("\x81", 300);

	/*
	 * 500ms delay, 10.9 chars / sec.
	 */
	reset_8042();

	press_key(1, 1, 1);
	VERIFY_LPC_CHAR_DELAY("\x01\x01\x01", 650);
	press_key(1, 1, 0);
	VERIFY_LPC_CHAR_DELAY("\x81", 200);

	return EC_SUCCESS;
}

static int test_scancode_set2(void)
{
	set_scancode(2);

	write_cmd_byte(read_cmd_byte() | I8042_XLATE);
	press_key(1, 1, 1);
	VERIFY_LPC_CHAR("\x01");
	press_key(1, 1, 0);
	VERIFY_LPC_CHAR("\x81");

	write_cmd_byte(read_cmd_byte() & ~I8042_XLATE);
	press_key(1, 1, 1);
	VERIFY_LPC_CHAR("\x76");
	press_key(1, 1, 0);
	VERIFY_LPC_CHAR("\xf0\x76");

	return EC_SUCCESS;
}

static int test_power_button(void)
{
	gpio_set_level(GPIO_POWER_BUTTON_L, 1);
	set_scancode(1);
	test_chipset_on();

	gpio_set_level(GPIO_POWER_BUTTON_L, 0);
	VERIFY_LPC_CHAR_DELAY("\xe0\x5e", 100);

	gpio_set_level(GPIO_POWER_BUTTON_L, 1);
	VERIFY_LPC_CHAR_DELAY("\xe0\xde", 100);

	set_scancode(2);
	write_cmd_byte(read_cmd_byte() & ~I8042_XLATE);

	gpio_set_level(GPIO_POWER_BUTTON_L, 0);
	VERIFY_LPC_CHAR_DELAY("\xe0\x37", 100);

	gpio_set_level(GPIO_POWER_BUTTON_L, 1);
	VERIFY_LPC_CHAR_DELAY("\xe0\xf0\x37", 100);

	test_chipset_off();

	gpio_set_level(GPIO_POWER_BUTTON_L, 0);
	VERIFY_NO_CHAR();

	gpio_set_level(GPIO_POWER_BUTTON_L, 1);
	VERIFY_NO_CHAR();

	return EC_SUCCESS;
}

static int test_sysjump(void)
{
	set_scancode(2);
	enable_keystroke(1);

	system_run_image_copy(EC_IMAGE_RW);

	/* Shouldn't reach here */
	return EC_ERROR_UNKNOWN;
}

static int test_sysjump_cont(void)
{
	write_cmd_byte(read_cmd_byte() | I8042_XLATE);
	press_key(1, 1, 1);
	VERIFY_LPC_CHAR("\x01");
	press_key(1, 1, 0);
	VERIFY_LPC_CHAR("\x81");

	write_cmd_byte(read_cmd_byte() & ~I8042_XLATE);
	press_key(1, 1, 1);
	VERIFY_LPC_CHAR("\x76");
	press_key(1, 1, 0);
	VERIFY_LPC_CHAR("\xf0\x76");

	return EC_SUCCESS;
}

static const struct ec_response_keybd_config keybd_config = {
	.num_top_row_keys = 13,
	.action_keys = {
		TK_BACK,		/* T1 */
		TK_REFRESH,		/* T2 */
		TK_FULLSCREEN,		/* T3 */
		TK_OVERVIEW,		/* T4 */
		TK_SNAPSHOT,		/* T5 */
		TK_BRIGHTNESS_DOWN,	/* T6 */
		TK_BRIGHTNESS_UP,	/* T7 */
		TK_KBD_BKLIGHT_DOWN,	/* T8 */
		TK_KBD_BKLIGHT_UP,	/* T9 */
		TK_PLAY_PAUSE,		/* T10 */
		TK_VOL_MUTE,		/* T11 */
		TK_VOL_DOWN,		/* T12 */
		TK_VOL_UP,		/* T13 */
	},
};

__override const struct ec_response_keybd_config
*board_vivaldi_keybd_config(void)
{
	return &keybd_config;
}

static int test_ec_cmd_get_keybd_config(void)
{
	struct ec_response_keybd_config resp;
	int rv;

	rv = test_send_host_command(EC_CMD_GET_KEYBD_CONFIG, 0, NULL, 0,
				    &resp, sizeof(resp));
	if (rv != EC_RES_SUCCESS) {
		ccprintf("Error: EC_CMD_GET_KEYBD_CONFIG cmd returns %d\n", rv);
		return EC_ERROR_INVAL;
	}

	if (memcmp(&resp, &keybd_config, sizeof(resp))) {
		ccprintf("Error: EC_CMD_GET_KEYBD_CONFIG returned bad cfg\n");
		return EC_ERROR_INVAL;
	}

	ccprintf("EC_CMD_GET_KEYBD_CONFIG response is good\n");
	return EC_SUCCESS;
}

static int test_vivaldi_top_keys(void)
{
	set_scancode(2);

	/* Test REFRESH key */
	write_cmd_byte(read_cmd_byte() | I8042_XLATE);
	press_key(2, 3, 1);		/* Press T2 */
	VERIFY_LPC_CHAR("\xe0\x67");	/* Check REFRESH scancode in set-1 */

	/* Test SNAPSHOT key */
	write_cmd_byte(read_cmd_byte() | I8042_XLATE);
	press_key(4, 3, 1);		/* Press T2 */
	VERIFY_LPC_CHAR("\xe0\x13");	/* Check SNAPSHOT scancode in set-1 */

	/* Test VOL_UP key */
	write_cmd_byte(read_cmd_byte() | I8042_XLATE);
	press_key(5, 3, 1);		/* Press T2 */
	VERIFY_LPC_CHAR("\xe0\x30");	/* Check VOL_UP scancode in set-1 */

	return EC_SUCCESS;
}

void run_test(int argc, char **argv)
{
	test_reset();
	wait_for_task_started();

	if (system_get_image_copy() == EC_IMAGE_RO) {
		RUN_TEST(test_single_key_press);
		RUN_TEST(test_disable_keystroke);
		RUN_TEST(test_typematic);
		RUN_TEST(test_scancode_set2);
		RUN_TEST(test_power_button);
		RUN_TEST(test_ec_cmd_get_keybd_config);
		RUN_TEST(test_vivaldi_top_keys);
		RUN_TEST(test_sysjump);
	} else {
		RUN_TEST(test_sysjump_cont);
	}

	test_print_result();
}