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_assertok(strcmp(cmp, "this is a test"));
65 ut_assertok(usb_stop());
69 DM_TEST(dm_test_usb_flash, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
71 /* test that we can handle multiple storage devices */
72 static int dm_test_usb_multi(struct unit_test_state *uts)
76 state_set_skip_delays(true);
77 ut_assertok(usb_init());
78 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
79 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
80 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
81 ut_assertok(usb_stop());
85 DM_TEST(dm_test_usb_multi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
87 /* test that we have an associated ofnode with the usb device */
88 static int dm_test_usb_fdt_node(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 node = ofnode_path("/usb@1/hub/usbstor@1");
97 ut_asserteq(1, ofnode_equal(node, dev_ofnode(dev)));
98 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
99 ut_asserteq(1, ofnode_equal(ofnode_null(), dev_ofnode(dev)));
100 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
101 node = ofnode_path("/usb@1/hub/usbstor@3");
102 ut_asserteq(1, ofnode_equal(node, dev_ofnode(dev)));
103 ut_assertok(usb_stop());
107 DM_TEST(dm_test_usb_fdt_node, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
109 static int count_usb_devices(void)
116 ret = uclass_get(UCLASS_USB_HUB, &uc);
120 uclass_foreach_dev(hub, uc) {
124 for (device_find_first_child(hub, &dev);
126 device_find_next_child(&dev)) {
134 /* test that no USB devices are found after we stop the stack */
135 static int dm_test_usb_stop(struct unit_test_state *uts)
139 /* Scan and check that all devices are present */
140 state_set_skip_delays(true);
141 ut_assertok(usb_init());
142 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
143 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
144 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
145 ut_asserteq(6, count_usb_devices());
146 ut_assertok(usb_stop());
147 ut_asserteq(0, count_usb_devices());
151 DM_TEST(dm_test_usb_stop, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
154 * dm_test_usb_keyb() - test USB keyboard driver
156 * This test copies USB keyboard scan codes into the key buffer of the USB
157 * keyboard emulation driver. These are picked up during emulated interrupts
158 * by the USB keyboard driver and converted to characters and escape sequences.
159 * The test then reads and verifies these characters and escape sequences from
160 * the standard input.
162 * TODO: The following features are not yet tested:
167 * * numerical pad keys
169 * TODO: The following features are not yet implemented by the USB keyboard
170 * driver and therefore not tested:
172 * * modifiers for non-alpha-numeric keys, e.g. <SHIFT><TAB> and <ALT><F4>
173 * * some special keys, e.g. <PRINT>
174 * * some modifiers, e.g. <ALT> and <META>
175 * * alternative keyboard layouts
177 * @uts: unit test state
178 * Return: 0 on success
180 static int dm_test_usb_keyb(struct unit_test_state *uts)
183 const struct keyboard_test_data *pos;
184 const struct keyboard_test_data kbd_test_data[] = {
238 /* <LEFT-SHIFT><A> */
240 /* <RIGHT-SHIFT><Z> */
243 /* <LEFT-CONTROL><A> */
244 {0x01, 0x04, "\x01"},
245 /* <RIGHT-CONTROL><Z> */
246 {0x10, 0x1d, "\x1a"},
269 /* <LEFT-SHIFT><1> */
271 /* <RIGHT-SHIFT><2> */
273 /* <LEFT-SHIFT><3> */
275 /* <RIGHT-SHIFT><4> */
277 /* <LEFT-SHIFT><5> */
279 /* <RIGHT-SHIFT><6> */
281 /* <LEFT-SHIFT><7> */
283 /* <RIGHT-SHIFT><8> */
285 /* <LEFT-SHIFT><9> */
287 /* <RIGHT-SHIFT><0> */
293 {0x00, 0x29, "\x1b"},
295 {0x00, 0x2a, "\x08"},
297 {0x00, 0x2b, "\x09"},
325 /* <LEFT-SHIFT><ENTER> */
327 /* <RIGHT-SHIFT><ESCAPE> */
328 {0x20, 0x29, "\x1b"},
329 /* <LEFT-SHIFT><BACKSPACE> */
330 {0x02, 0x2a, "\x08"},
331 /* <RIGHT-SHIFT><TAB> */
332 {0x20, 0x2b, "\x09"},
333 /* <LEFT-SHIFT><SPACE> */
337 /* <LEFT-SHIFT><EQUAL> */
339 /* <RIGHT-SHIFT><LEFT BRACE> */
341 /* <LEFT-SHIFT><RIGHT BRACE> */
343 /* <RIGHT-SHIFT><BACKSLASH> */
345 /* <LEFT-SHIFT><HASH-TILDE> */
347 /* <RIGHT-SHIFT><SEMICOLON> */
349 /* <LEFT-SHIFT><APOSTROPHE> */
351 /* <RIGHT-SHIFT><GRAVE> */
353 /* <LEFT-SHIFT><COMMA> */
355 /* <RIGHT-SHIFT><DOT> */
357 /* <LEFT-SHIFT><SLASH> */
359 #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
361 {0x00, 0x3a, "\x1bOP"},
363 {0x00, 0x3b, "\x1bOQ"},
365 {0x00, 0x3c, "\x1bOR"},
367 {0x00, 0x3d, "\x1bOS"},
369 {0x00, 0x3e, "\x1b[15~"},
371 {0x00, 0x3f, "\x1b[17~"},
373 {0x00, 0x40, "\x1b[18~"},
375 {0x00, 0x41, "\x1b[19~"},
377 {0x00, 0x42, "\x1b[20~"},
379 {0x00, 0x43, "\x1b[21~"},
381 {0x00, 0x44, "\x1b[23~"},
383 {0x00, 0x45, "\x1b[24~"},
385 {0x00, 0x49, "\x1b[2~"},
387 {0x00, 0x4a, "\x1b[H"},
389 {0x00, 0x4b, "\x1b[5~"},
391 {0x00, 0x4c, "\x1b[3~"},
393 {0x00, 0x4d, "\x1b[F"},
395 {0x00, 0x4e, "\x1b[6~"},
397 {0x00, 0x4f, "\x1b[C"},
399 {0x00, 0x50, "\x1b[D"},
401 {0x00, 0x51, "\x1b[B"},
403 {0x00, 0x52, "\x1b[A"},
404 #endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
411 state_set_skip_delays(true);
412 ut_assertok(usb_init());
414 /* Initially there should be no characters */
415 ut_asserteq(0, tstc());
417 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb@3",
421 * Add scan codes to the USB keyboard buffer. They should appear as
422 * corresponding characters and escape sequences in stdin.
424 for (pos = kbd_test_data; pos->scancode; ++pos) {
426 char scancodes[USB_KBD_BOOT_REPORT_SIZE] = {0};
428 scancodes[0] = pos->modifiers;
429 scancodes[2] = pos->scancode;
431 ut_assertok(sandbox_usb_keyb_add_string(dev, scancodes));
433 for (c = pos->result; *c; ++c) {
434 ut_asserteq(1, tstc());
435 ut_asserteq(*c, getchar());
437 ut_asserteq(0, tstc());
439 ut_assertok(usb_stop());
443 DM_TEST(dm_test_usb_keyb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);