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>
17 #include <test/test.h>
20 struct keyboard_test_data {
26 /* Test that sandbox USB works correctly */
27 static int dm_test_usb_base(struct unit_test_state *uts)
31 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
32 ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
33 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
37 DM_TEST(dm_test_usb_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
40 * Test that we can use the flash stick. This is more of a functional test. It
41 * covers scanning the bug, setting up a hub and a flash stick and reading
42 * data from the flash stick.
44 static int dm_test_usb_flash(struct unit_test_state *uts)
46 struct blk_desc *dev_desc, *chk;
47 struct udevice *dev, *blk;
50 state_set_skip_delays(true);
51 ut_assertok(usb_init());
52 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
53 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
54 chk = blk_get_by_device(dev);
55 ut_asserteq_ptr(chk, dev_desc);
57 ut_assertok(device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk));
58 ut_asserteq_ptr(chk, blk_get_by_device(dev));
60 /* Read a few blocks and look for the string we expect */
61 ut_asserteq(512, dev_desc->blksz);
62 memset(cmp, '\0', sizeof(cmp));
63 ut_asserteq(2, blk_read(blk, 0, 2, cmp));
64 ut_asserteq_str("this is a test", cmp);
66 strcpy(cmp, "another test");
67 ut_asserteq(1, blk_write(blk, 1, 1, cmp));
69 memset(cmp, '\0', sizeof(cmp));
70 ut_asserteq(2, blk_read(blk, 0, 2, cmp));
71 ut_asserteq_str("this is a test", cmp);
72 ut_asserteq_str("another test", cmp + 512);
74 memset(cmp, '\0', sizeof(cmp));
75 ut_asserteq(1, blk_write(blk, 1, 1, cmp));
77 memset(cmp, '\0', sizeof(cmp));
78 ut_asserteq(2, blk_read(blk, 0, 2, cmp));
79 ut_asserteq_str("this is a test", cmp);
80 ut_asserteq_str("", cmp + 512);
82 ut_assertok(usb_stop());
86 DM_TEST(dm_test_usb_flash, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
88 /* test that we can handle multiple storage devices */
89 static int dm_test_usb_multi(struct unit_test_state *uts)
93 state_set_skip_delays(true);
94 ut_assertok(usb_init());
95 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
96 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
97 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
98 ut_assertok(usb_stop());
102 DM_TEST(dm_test_usb_multi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
104 /* test that we have an associated ofnode with the usb device */
105 static int dm_test_usb_fdt_node(struct unit_test_state *uts)
110 state_set_skip_delays(true);
111 ut_assertok(usb_init());
112 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
113 node = ofnode_path("/usb@1/hub/usbstor@1");
114 ut_asserteq(1, ofnode_equal(node, dev_ofnode(dev)));
115 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
116 ut_asserteq(1, ofnode_equal(ofnode_null(), dev_ofnode(dev)));
117 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
118 node = ofnode_path("/usb@1/hub/usbstor@3");
119 ut_asserteq(1, ofnode_equal(node, dev_ofnode(dev)));
120 ut_assertok(usb_stop());
124 DM_TEST(dm_test_usb_fdt_node, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
126 static int count_usb_devices(void)
133 ret = uclass_get(UCLASS_USB_HUB, &uc);
137 uclass_foreach_dev(hub, uc) {
141 for (device_find_first_child(hub, &dev);
143 device_find_next_child(&dev)) {
151 /* test that no USB devices are found after we stop the stack */
152 static int dm_test_usb_stop(struct unit_test_state *uts)
156 /* Scan and check that all devices are present */
157 state_set_skip_delays(true);
158 ut_assertok(usb_init());
159 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
160 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
161 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
162 ut_asserteq(6, count_usb_devices());
163 ut_assertok(usb_stop());
164 ut_asserteq(0, count_usb_devices());
168 DM_TEST(dm_test_usb_stop, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
171 * dm_test_usb_keyb() - test USB keyboard driver
173 * This test copies USB keyboard scan codes into the key buffer of the USB
174 * keyboard emulation driver. These are picked up during emulated interrupts
175 * by the USB keyboard driver and converted to characters and escape sequences.
176 * The test then reads and verifies these characters and escape sequences from
177 * the standard input.
179 * TODO: The following features are not yet tested:
184 * * numerical pad keys
186 * TODO: The following features are not yet implemented by the USB keyboard
187 * driver and therefore not tested:
189 * * modifiers for non-alpha-numeric keys, e.g. <SHIFT><TAB> and <ALT><F4>
190 * * some special keys, e.g. <PRINT>
191 * * some modifiers, e.g. <ALT> and <META>
192 * * alternative keyboard layouts
194 * @uts: unit test state
195 * Return: 0 on success
197 static int dm_test_usb_keyb(struct unit_test_state *uts)
200 const struct keyboard_test_data *pos;
201 const struct keyboard_test_data kbd_test_data[] = {
255 /* <LEFT-SHIFT><A> */
257 /* <RIGHT-SHIFT><Z> */
260 /* <LEFT-CONTROL><A> */
261 {0x01, 0x04, "\x01"},
262 /* <RIGHT-CONTROL><Z> */
263 {0x10, 0x1d, "\x1a"},
286 /* <LEFT-SHIFT><1> */
288 /* <RIGHT-SHIFT><2> */
290 /* <LEFT-SHIFT><3> */
292 /* <RIGHT-SHIFT><4> */
294 /* <LEFT-SHIFT><5> */
296 /* <RIGHT-SHIFT><6> */
298 /* <LEFT-SHIFT><7> */
300 /* <RIGHT-SHIFT><8> */
302 /* <LEFT-SHIFT><9> */
304 /* <RIGHT-SHIFT><0> */
310 {0x00, 0x29, "\x1b"},
312 {0x00, 0x2a, "\x08"},
314 {0x00, 0x2b, "\x09"},
342 /* <LEFT-SHIFT><ENTER> */
344 /* <RIGHT-SHIFT><ESCAPE> */
345 {0x20, 0x29, "\x1b"},
346 /* <LEFT-SHIFT><BACKSPACE> */
347 {0x02, 0x2a, "\x08"},
348 /* <RIGHT-SHIFT><TAB> */
349 {0x20, 0x2b, "\x09"},
350 /* <LEFT-SHIFT><SPACE> */
354 /* <LEFT-SHIFT><EQUAL> */
356 /* <RIGHT-SHIFT><LEFT BRACE> */
358 /* <LEFT-SHIFT><RIGHT BRACE> */
360 /* <RIGHT-SHIFT><BACKSLASH> */
362 /* <LEFT-SHIFT><HASH-TILDE> */
364 /* <RIGHT-SHIFT><SEMICOLON> */
366 /* <LEFT-SHIFT><APOSTROPHE> */
368 /* <RIGHT-SHIFT><GRAVE> */
370 /* <LEFT-SHIFT><COMMA> */
372 /* <RIGHT-SHIFT><DOT> */
374 /* <LEFT-SHIFT><SLASH> */
376 #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
378 {0x00, 0x3a, "\x1bOP"},
380 {0x00, 0x3b, "\x1bOQ"},
382 {0x00, 0x3c, "\x1bOR"},
384 {0x00, 0x3d, "\x1bOS"},
386 {0x00, 0x3e, "\x1b[15~"},
388 {0x00, 0x3f, "\x1b[17~"},
390 {0x00, 0x40, "\x1b[18~"},
392 {0x00, 0x41, "\x1b[19~"},
394 {0x00, 0x42, "\x1b[20~"},
396 {0x00, 0x43, "\x1b[21~"},
398 {0x00, 0x44, "\x1b[23~"},
400 {0x00, 0x45, "\x1b[24~"},
402 {0x00, 0x49, "\x1b[2~"},
404 {0x00, 0x4a, "\x1b[H"},
406 {0x00, 0x4b, "\x1b[5~"},
408 {0x00, 0x4c, "\x1b[3~"},
410 {0x00, 0x4d, "\x1b[F"},
412 {0x00, 0x4e, "\x1b[6~"},
414 {0x00, 0x4f, "\x1b[C"},
416 {0x00, 0x50, "\x1b[D"},
418 {0x00, 0x51, "\x1b[B"},
420 {0x00, 0x52, "\x1b[A"},
421 #endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
428 state_set_skip_delays(true);
429 ut_assertok(usb_init());
431 /* Initially there should be no characters */
432 ut_asserteq(0, tstc());
434 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb@3",
438 * Add scan codes to the USB keyboard buffer. They should appear as
439 * corresponding characters and escape sequences in stdin.
441 for (pos = kbd_test_data; pos->scancode; ++pos) {
443 char scancodes[USB_KBD_BOOT_REPORT_SIZE] = {0};
445 scancodes[0] = pos->modifiers;
446 scancodes[2] = pos->scancode;
448 ut_assertok(sandbox_usb_keyb_add_string(dev, scancodes));
450 for (c = pos->result; *c; ++c) {
451 ut_asserteq(1, tstc());
452 ut_asserteq(*c, getchar());
454 ut_asserteq(0, tstc());
456 ut_assertok(usb_stop());
460 DM_TEST(dm_test_usb_keyb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);