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 /* test that we have an associated ofnode with the usb device */
82 static int dm_test_usb_fdt_node(struct unit_test_state *uts)
87 state_set_skip_delays(true);
88 ut_assertok(usb_init());
89 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
90 node = ofnode_path("/usb@1/hub/usbstor@1");
91 ut_asserteq(1, ofnode_equal(node, dev_ofnode(dev)));
92 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
93 ut_asserteq(1, ofnode_equal(ofnode_null(), dev_ofnode(dev)));
94 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
95 node = ofnode_path("/usb@1/hub/usbstor@3");
96 ut_asserteq(1, ofnode_equal(node, dev_ofnode(dev)));
97 ut_assertok(usb_stop());
101 DM_TEST(dm_test_usb_fdt_node, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
103 static int count_usb_devices(void)
110 ret = uclass_get(UCLASS_USB_HUB, &uc);
114 uclass_foreach_dev(hub, uc) {
118 for (device_find_first_child(hub, &dev);
120 device_find_next_child(&dev)) {
128 /* test that no USB devices are found after we stop the stack */
129 static int dm_test_usb_stop(struct unit_test_state *uts)
133 /* Scan and check that all devices are present */
134 state_set_skip_delays(true);
135 ut_assertok(usb_init());
136 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
137 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
138 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
139 ut_asserteq(6, count_usb_devices());
140 ut_assertok(usb_stop());
141 ut_asserteq(0, count_usb_devices());
145 DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
148 * dm_test_usb_keyb() - test USB keyboard driver
150 * This test copies USB keyboard scan codes into the key buffer of the USB
151 * keyboard emulation driver. These are picked up during emulated interrupts
152 * by the USB keyboard driver and converted to characters and escape sequences.
153 * The test then reads and verifies these characters and escape sequences from
154 * the standard input.
156 * TODO: The following features are not yet tested:
161 * * numerical pad keys
163 * TODO: The following features are not yet implemented by the USB keyboard
164 * driver and therefore not tested:
166 * * modifiers for non-alpha-numeric keys, e.g. <SHIFT><TAB> and <ALT><F4>
167 * * some special keys, e.g. <PRINT>
168 * * some modifiers, e.g. <ALT> and <META>
169 * * alternative keyboard layouts
171 * @uts: unit test state
172 * Return: 0 on success
174 static int dm_test_usb_keyb(struct unit_test_state *uts)
177 const struct keyboard_test_data *pos;
178 const struct keyboard_test_data kbd_test_data[] = {
232 /* <LEFT-SHIFT><A> */
234 /* <RIGHT-SHIFT><Z> */
237 /* <LEFT-CONTROL><A> */
238 {0x01, 0x04, "\x01"},
239 /* <RIGHT-CONTROL><Z> */
240 {0x10, 0x1d, "\x1a"},
263 /* <LEFT-SHIFT><1> */
265 /* <RIGHT-SHIFT><2> */
267 /* <LEFT-SHIFT><3> */
269 /* <RIGHT-SHIFT><4> */
271 /* <LEFT-SHIFT><5> */
273 /* <RIGHT-SHIFT><6> */
275 /* <LEFT-SHIFT><7> */
277 /* <RIGHT-SHIFT><8> */
279 /* <LEFT-SHIFT><9> */
281 /* <RIGHT-SHIFT><0> */
287 {0x00, 0x29, "\x1b"},
289 {0x00, 0x2a, "\x08"},
291 {0x00, 0x2b, "\x09"},
319 /* <LEFT-SHIFT><ENTER> */
321 /* <RIGHT-SHIFT><ESCAPE> */
322 {0x20, 0x29, "\x1b"},
323 /* <LEFT-SHIFT><BACKSPACE> */
324 {0x02, 0x2a, "\x08"},
325 /* <RIGHT-SHIFT><TAB> */
326 {0x20, 0x2b, "\x09"},
327 /* <LEFT-SHIFT><SPACE> */
331 /* <LEFT-SHIFT><EQUAL> */
333 /* <RIGHT-SHIFT><LEFT BRACE> */
335 /* <LEFT-SHIFT><RIGHT BRACE> */
337 /* <RIGHT-SHIFT><BACKSLASH> */
339 /* <LEFT-SHIFT><HASH-TILDE> */
341 /* <RIGHT-SHIFT><SEMICOLON> */
343 /* <LEFT-SHIFT><APOSTROPHE> */
345 /* <RIGHT-SHIFT><GRAVE> */
347 /* <LEFT-SHIFT><COMMA> */
349 /* <RIGHT-SHIFT><DOT> */
351 /* <LEFT-SHIFT><SLASH> */
353 #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
355 {0x00, 0x3a, "\x1bOP"},
357 {0x00, 0x3b, "\x1bOQ"},
359 {0x00, 0x3c, "\x1bOR"},
361 {0x00, 0x3d, "\x1bOS"},
363 {0x00, 0x3e, "\x1b[15~"},
365 {0x00, 0x3f, "\x1b[17~"},
367 {0x00, 0x40, "\x1b[18~"},
369 {0x00, 0x41, "\x1b[19~"},
371 {0x00, 0x42, "\x1b[20~"},
373 {0x00, 0x43, "\x1b[21~"},
375 {0x00, 0x44, "\x1b[23~"},
377 {0x00, 0x45, "\x1b[24~"},
379 {0x00, 0x49, "\x1b[2~"},
381 {0x00, 0x4a, "\x1b[H"},
383 {0x00, 0x4b, "\x1b[5~"},
385 {0x00, 0x4c, "\x1b[3~"},
387 {0x00, 0x4d, "\x1b[F"},
389 {0x00, 0x4e, "\x1b[6~"},
391 {0x00, 0x4f, "\x1b[C"},
393 {0x00, 0x50, "\x1b[D"},
395 {0x00, 0x51, "\x1b[B"},
397 {0x00, 0x52, "\x1b[A"},
398 #endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
405 state_set_skip_delays(true);
406 ut_assertok(usb_init());
408 /* Initially there should be no characters */
409 ut_asserteq(0, tstc());
411 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb@3",
415 * Add scan codes to the USB keyboard buffer. They should appear as
416 * corresponding characters and escape sequences in stdin.
418 for (pos = kbd_test_data; pos->scancode; ++pos) {
420 char scancodes[USB_KBD_BOOT_REPORT_SIZE] = {0};
422 scancodes[0] = pos->modifiers;
423 scancodes[2] = pos->scancode;
425 ut_assertok(sandbox_usb_keyb_add_string(dev, scancodes));
427 for (c = pos->result; *c; ++c) {
428 ut_asserteq(1, tstc());
429 ut_asserteq(*c, getc());
431 ut_asserteq(0, tstc());
433 ut_assertok(usb_stop());
437 DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);