1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
12 #include <asm/state.h>
14 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
19 struct keyboard_test_data {
25 /* Test that sandbox USB works correctly */
26 static int dm_test_usb_base(struct unit_test_state *uts)
30 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
31 ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
32 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
36 DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
39 * Test that we can use the flash stick. This is more of a functional test. It
40 * covers scanning the bug, setting up a hub and a flash stick and reading
41 * data from the flash stick.
43 static int dm_test_usb_flash(struct unit_test_state *uts)
46 struct blk_desc *dev_desc;
49 state_set_skip_delays(true);
50 ut_assertok(usb_init());
51 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
52 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
54 /* Read a few blocks and look for the string we expect */
55 ut_asserteq(512, dev_desc->blksz);
56 memset(cmp, '\0', sizeof(cmp));
57 ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
58 ut_assertok(strcmp(cmp, "this is a test"));
59 ut_assertok(usb_stop());
63 DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
65 /* test that we can handle multiple storage devices */
66 static int dm_test_usb_multi(struct unit_test_state *uts)
70 state_set_skip_delays(true);
71 ut_assertok(usb_init());
72 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
73 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
74 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
75 ut_assertok(usb_stop());
79 DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
81 static int count_usb_devices(void)
88 ret = uclass_get(UCLASS_USB_HUB, &uc);
92 uclass_foreach_dev(hub, uc) {
96 for (device_find_first_child(hub, &dev);
98 device_find_next_child(&dev)) {
106 /* test that no USB devices are found after we stop the stack */
107 static int dm_test_usb_stop(struct unit_test_state *uts)
111 /* Scan and check that all devices are present */
112 state_set_skip_delays(true);
113 ut_assertok(usb_init());
114 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
115 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
116 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
117 ut_asserteq(6, count_usb_devices());
118 ut_assertok(usb_stop());
119 ut_asserteq(0, count_usb_devices());
123 DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
126 * dm_test_usb_keyb() - test USB keyboard driver
128 * This test copies USB keyboard scan codes into the key buffer of the USB
129 * keyboard emulation driver. These are picked up during emulated interrupts
130 * by the USB keyboard driver and converted to characters and escape sequences.
131 * The test then reads and verifies these characters and escape sequences from
132 * the standard input.
134 * TODO: The following features are not yet tested:
139 * * numerical pad keys
141 * TODO: The following features are not yet implemented by the USB keyboard
142 * driver and therefore not tested:
144 * * modifiers for non-alpha-numeric keys, e.g. <SHIFT><TAB> and <ALT><F4>
145 * * some special keys, e.g. <PRINT>
146 * * some modifiers, e.g. <ALT> and <META>
147 * * alternative keyboard layouts
149 * @uts: unit test state
150 * Return: 0 on success
152 static int dm_test_usb_keyb(struct unit_test_state *uts)
155 const struct keyboard_test_data *pos;
156 const struct keyboard_test_data kbd_test_data[] = {
210 /* <LEFT-SHIFT><A> */
212 /* <RIGHT-SHIFT><Z> */
215 /* <LEFT-CONTROL><A> */
216 {0x01, 0x04, "\x01"},
217 /* <RIGHT-CONTROL><Z> */
218 {0x10, 0x1d, "\x1a"},
241 /* <LEFT-SHIFT><1> */
243 /* <RIGHT-SHIFT><2> */
245 /* <LEFT-SHIFT><3> */
247 /* <RIGHT-SHIFT><4> */
249 /* <LEFT-SHIFT><5> */
251 /* <RIGHT-SHIFT><6> */
253 /* <LEFT-SHIFT><7> */
255 /* <RIGHT-SHIFT><8> */
257 /* <LEFT-SHIFT><9> */
259 /* <RIGHT-SHIFT><0> */
265 {0x00, 0x29, "\x1b"},
267 {0x00, 0x2a, "\x08"},
269 {0x00, 0x2b, "\x09"},
297 /* <LEFT-SHIFT><ENTER> */
299 /* <RIGHT-SHIFT><ESCAPE> */
300 {0x20, 0x29, "\x1b"},
301 /* <LEFT-SHIFT><BACKSPACE> */
302 {0x02, 0x2a, "\x08"},
303 /* <RIGHT-SHIFT><TAB> */
304 {0x20, 0x2b, "\x09"},
305 /* <LEFT-SHIFT><SPACE> */
309 /* <LEFT-SHIFT><EQUAL> */
311 /* <RIGHT-SHIFT><LEFT BRACE> */
313 /* <LEFT-SHIFT><RIGHT BRACE> */
315 /* <RIGHT-SHIFT><BACKSLASH> */
317 /* <LEFT-SHIFT><HASH-TILDE> */
319 /* <RIGHT-SHIFT><SEMICOLON> */
321 /* <LEFT-SHIFT><APOSTROPHE> */
323 /* <RIGHT-SHIFT><GRAVE> */
325 /* <LEFT-SHIFT><COMMA> */
327 /* <RIGHT-SHIFT><DOT> */
329 /* <LEFT-SHIFT><SLASH> */
331 #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
333 {0x00, 0x3a, "\x1bOP"},
335 {0x00, 0x3b, "\x1bOQ"},
337 {0x00, 0x3c, "\x1bOR"},
339 {0x00, 0x3d, "\x1bOS"},
341 {0x00, 0x3e, "\x1b[15~"},
343 {0x00, 0x3f, "\x1b[17~"},
345 {0x00, 0x40, "\x1b[18~"},
347 {0x00, 0x41, "\x1b[19~"},
349 {0x00, 0x42, "\x1b[20~"},
351 {0x00, 0x43, "\x1b[21~"},
353 {0x00, 0x44, "\x1b[23~"},
355 {0x00, 0x45, "\x1b[24~"},
357 {0x00, 0x49, "\x1b[2~"},
359 {0x00, 0x4a, "\x1b[H"},
361 {0x00, 0x4b, "\x1b[5~"},
363 {0x00, 0x4c, "\x1b[3~"},
365 {0x00, 0x4d, "\x1b[F"},
367 {0x00, 0x4e, "\x1b[6~"},
369 {0x00, 0x4f, "\x1b[C"},
371 {0x00, 0x50, "\x1b[D"},
373 {0x00, 0x51, "\x1b[B"},
375 {0x00, 0x52, "\x1b[A"},
376 #endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
383 state_set_skip_delays(true);
384 ut_assertok(usb_init());
386 /* Initially there should be no characters */
387 ut_asserteq(0, tstc());
389 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb@3",
393 * Add scan codes to the USB keyboard buffer. They should appear as
394 * corresponding characters and escape sequences in stdin.
396 for (pos = kbd_test_data; pos->scancode; ++pos) {
398 char scancodes[USB_KBD_BOOT_REPORT_SIZE] = {0};
400 scancodes[0] = pos->modifiers;
401 scancodes[2] = pos->scancode;
403 ut_assertok(sandbox_usb_keyb_add_string(dev, scancodes));
405 for (c = pos->result; *c; ++c) {
406 ut_asserteq(1, tstc());
407 ut_asserteq(*c, getc());
409 ut_asserteq(0, tstc());
411 ut_assertok(usb_stop());
415 DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);