xref: /openbmc/u-boot/test/dm/usb.c (revision 894c3ad2)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <console.h>
8 #include <dm.h>
9 #include <usb.h>
10 #include <asm/io.h>
11 #include <asm/state.h>
12 #include <asm/test.h>
13 #include <dm/device-internal.h>
14 #include <dm/test.h>
15 #include <dm/uclass-internal.h>
16 #include <test/ut.h>
17 
18 /* Test that sandbox USB works correctly */
19 static int dm_test_usb_base(struct unit_test_state *uts)
20 {
21 	struct udevice *bus;
22 
23 	ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
24 	ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
25 	ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
26 
27 	return 0;
28 }
29 DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
30 
31 /*
32  * Test that we can use the flash stick. This is more of a functional test. It
33  * covers scanning the bug, setting up a hub and a flash stick and reading
34  * data from the flash stick.
35  */
36 static int dm_test_usb_flash(struct unit_test_state *uts)
37 {
38 	struct udevice *dev;
39 	struct blk_desc *dev_desc;
40 	char cmp[1024];
41 
42 	state_set_skip_delays(true);
43 	ut_assertok(usb_init());
44 	ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
45 	ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
46 
47 	/* Read a few blocks and look for the string we expect */
48 	ut_asserteq(512, dev_desc->blksz);
49 	memset(cmp, '\0', sizeof(cmp));
50 	ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
51 	ut_assertok(strcmp(cmp, "this is a test"));
52 	ut_assertok(usb_stop());
53 
54 	return 0;
55 }
56 DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
57 
58 /* test that we can handle multiple storage devices */
59 static int dm_test_usb_multi(struct unit_test_state *uts)
60 {
61 	struct udevice *dev;
62 
63 	state_set_skip_delays(true);
64 	ut_assertok(usb_init());
65 	ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
66 	ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
67 	ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
68 	ut_assertok(usb_stop());
69 
70 	return 0;
71 }
72 DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
73 
74 static int count_usb_devices(void)
75 {
76 	struct udevice *hub;
77 	struct uclass *uc;
78 	int count = 0;
79 	int ret;
80 
81 	ret = uclass_get(UCLASS_USB_HUB, &uc);
82 	if (ret)
83 		return ret;
84 
85 	uclass_foreach_dev(hub, uc) {
86 		struct udevice *dev;
87 
88 		count++;
89 		for (device_find_first_child(hub, &dev);
90 		     dev;
91 		     device_find_next_child(&dev)) {
92 			count++;
93 		}
94 	}
95 
96 	return count;
97 }
98 
99 /* test that no USB devices are found after we stop the stack */
100 static int dm_test_usb_stop(struct unit_test_state *uts)
101 {
102 	struct udevice *dev;
103 
104 	/* Scan and check that all devices are present */
105 	state_set_skip_delays(true);
106 	ut_assertok(usb_init());
107 	ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
108 	ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
109 	ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
110 	ut_asserteq(6, count_usb_devices());
111 	ut_assertok(usb_stop());
112 	ut_asserteq(0, count_usb_devices());
113 
114 	return 0;
115 }
116 DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
117 
118 static int dm_test_usb_keyb(struct unit_test_state *uts)
119 {
120 	struct udevice *dev;
121 
122 	state_set_skip_delays(true);
123 	ut_assertok(usb_init());
124 
125 	/* Initially there should be no characters */
126 	ut_asserteq(0, tstc());
127 
128 	ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
129 					      &dev));
130 
131 	/*
132 	 * Add a string to the USB keyboard buffer - it should appear in
133 	 * stdin
134 	 */
135 	ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
136 	ut_asserteq(1, tstc());
137 	ut_asserteq('a', getc());
138 	ut_asserteq(1, tstc());
139 	ut_asserteq('b', getc());
140 	ut_asserteq(0, tstc());
141 
142 	ut_assertok(usb_stop());
143 
144 	return 0;
145 }
146 DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
147