]> git.sur5r.net Git - u-boot/blob - test/dm/usb.c
net: e1000: Mark _disable_wr() and _write_status() as __maybe_unused
[u-boot] / test / dm / usb.c
1 /*
2  * Copyright (C) 2015 Google, Inc
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <console.h>
9 #include <dm.h>
10 #include <usb.h>
11 #include <asm/io.h>
12 #include <asm/state.h>
13 #include <asm/test.h>
14 #include <dm/device-internal.h>
15 #include <dm/test.h>
16 #include <dm/uclass-internal.h>
17 #include <test/ut.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /* Test that sandbox USB works correctly */
22 static int dm_test_usb_base(struct unit_test_state *uts)
23 {
24         struct udevice *bus;
25
26         ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
27         ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
28         ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
29
30         return 0;
31 }
32 DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
33
34 /*
35  * Test that we can use the flash stick. This is more of a functional test. It
36  * covers scanning the bug, setting up a hub and a flash stick and reading
37  * data from the flash stick.
38  */
39 static int dm_test_usb_flash(struct unit_test_state *uts)
40 {
41         struct udevice *dev;
42         block_dev_desc_t *dev_desc;
43         char cmp[1024];
44
45         state_set_skip_delays(true);
46         ut_assertok(usb_init());
47         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
48         ut_assertok(get_device("usb", "0", &dev_desc));
49
50         /* Read a few blocks and look for the string we expect */
51         ut_asserteq(512, dev_desc->blksz);
52         memset(cmp, '\0', sizeof(cmp));
53         ut_asserteq(2, dev_desc->block_read(dev_desc->dev, 0, 2, cmp));
54         ut_assertok(strcmp(cmp, "this is a test"));
55
56         return 0;
57 }
58 DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
59
60 /* test that we can handle multiple storage devices */
61 static int dm_test_usb_multi(struct unit_test_state *uts)
62 {
63         struct udevice *dev;
64
65         state_set_skip_delays(true);
66         ut_assertok(usb_init());
67         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
68         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
69         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
70
71         return 0;
72 }
73 DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
74
75 static int count_usb_devices(void)
76 {
77         struct udevice *hub;
78         struct uclass *uc;
79         int count = 0;
80         int ret;
81
82         ret = uclass_get(UCLASS_USB_HUB, &uc);
83         if (ret)
84                 return ret;
85
86         uclass_foreach_dev(hub, uc) {
87                 struct udevice *dev;
88
89                 count++;
90                 for (device_find_first_child(hub, &dev);
91                      dev;
92                      device_find_next_child(&dev)) {
93                         count++;
94                 }
95         }
96
97         return count;
98 }
99
100 /* test that we can remove an emulated device and it is then not found */
101 static int dm_test_usb_remove(struct unit_test_state *uts)
102 {
103         struct udevice *dev, *emul;
104
105         /* Scan and check that all devices are present */
106         state_set_skip_delays(true);
107         ut_assertok(usb_init());
108         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
109         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
110         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
111         ut_asserteq(5, count_usb_devices());
112         ut_assertok(usb_stop());
113         ut_asserteq(5, count_usb_devices());
114
115         /* Remove the second emulation device */
116         ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
117                                                &dev));
118         ut_assertok(device_unbind(dev));
119
120         /* Rescan - only the first and third should be present */
121         ut_assertok(usb_init());
122         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
123         ut_assertok(usb_emul_find_for_dev(dev, &emul));
124         ut_asserteq_str("flash-stick@0", emul->name);
125         ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
126         ut_assertok(usb_emul_find_for_dev(dev, &emul));
127         ut_asserteq_str("flash-stick@2", emul->name);
128
129         ut_asserteq(-ENODEV, uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
130
131         ut_asserteq(4, count_usb_devices());
132         ut_assertok(usb_stop());
133         ut_asserteq(4, count_usb_devices());
134
135         return 0;
136 }
137 DM_TEST(dm_test_usb_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
138
139 const char usb_tree_base[] =
140 "  1  Hub (12 Mb/s, 100mA)\n"
141 "  |  sandbox hub 2345\n"
142 "  |\n"
143 "  |\b+-2  Mass Storage (12 Mb/s, 100mA)\n"
144 "  |    sandbox flash flash-stick@0\n"
145 "  |  \n"
146 "  |\b+-3  Mass Storage (12 Mb/s, 100mA)\n"
147 "  |    sandbox flash flash-stick@1\n"
148 "  |  \n"
149 "  |\b+-4  Mass Storage (12 Mb/s, 100mA)\n"
150 "       sandbox flash flash-stick@2\n"
151 "     \n";
152
153 /* test that the 'usb tree' command output looks correct */
154 static int dm_test_usb_tree(struct unit_test_state *uts)
155 {
156         char *data;
157         int len;
158
159         state_set_skip_delays(true);
160         ut_assertok(usb_init());
161         console_record_reset_enable();
162         usb_show_tree();
163         len = membuff_getraw(&gd->console_out, -1, true, &data);
164         if (len)
165                 data[len] = '\0';
166         ut_asserteq_str(usb_tree_base, data);
167         ut_assertok(usb_stop());
168
169         return 0;
170 }
171 DM_TEST(dm_test_usb_tree, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
172
173 const char usb_tree_remove[] =
174 "  1  Hub (12 Mb/s, 100mA)\n"
175 "  |  sandbox hub 2345\n"
176 "  |\n"
177 "  |\b+-2  Mass Storage (12 Mb/s, 100mA)\n"
178 "  |    sandbox flash flash-stick@0\n"
179 "  |  \n"
180 "  |\b+-3  Mass Storage (12 Mb/s, 100mA)\n"
181 "       sandbox flash flash-stick@2\n"
182 "     \n";
183
184 /*
185  * test that the 'usb tree' command output looks correct when we remove a
186  * device
187  */
188 static int dm_test_usb_tree_remove(struct unit_test_state *uts)
189 {
190         struct udevice *dev;
191         char *data;
192         int len;
193
194         /* Remove the second emulation device */
195         ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
196                                                &dev));
197         ut_assertok(device_unbind(dev));
198
199         state_set_skip_delays(true);
200         ut_assertok(usb_init());
201         console_record_reset_enable();
202         usb_show_tree();
203         len = membuff_getraw(&gd->console_out, -1, true, &data);
204         if (len)
205                 data[len] = '\0';
206         ut_asserteq_str(usb_tree_remove, data);
207         ut_assertok(usb_stop());
208
209         return 0;
210 }
211 DM_TEST(dm_test_usb_tree_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
212
213 const char usb_tree_reorder[] =
214 "  1  Hub (12 Mb/s, 100mA)\n"
215 "  |  sandbox hub 2345\n"
216 "  |\n"
217 "  |\b+-2  Mass Storage (12 Mb/s, 100mA)\n"
218 "  |    sandbox flash flash-stick@0\n"
219 "  |  \n"
220 "  |\b+-3  Mass Storage (12 Mb/s, 100mA)\n"
221 "  |    sandbox flash flash-stick@2\n"
222 "  |  \n"
223 "  |\b+-4  Mass Storage (12 Mb/s, 100mA)\n"
224 "       sandbox flash flash-stick@1\n"
225 "     \n";
226
227 /*
228  * test that the 'usb tree' command output looks correct when we reorder two
229  * devices.
230  */
231 static int dm_test_usb_tree_reorder(struct unit_test_state *uts)
232 {
233         struct udevice *dev, *parent;
234         char *data;
235         int len;
236
237         /* Remove the second emulation device */
238         ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
239                                                &dev));
240         parent = dev->parent;
241
242         /* Reorder the devices in the parent list and uclass list */
243         list_del(&dev->sibling_node);
244         list_add_tail(&dev->sibling_node, &parent->child_head);
245
246         list_del(&dev->uclass_node);
247         list_add_tail(&dev->uclass_node, &dev->uclass->dev_head);
248
249         state_set_skip_delays(true);
250         ut_assertok(usb_init());
251         console_record_reset_enable();
252         usb_show_tree();
253         len = membuff_getraw(&gd->console_out, -1, true, &data);
254         if (len)
255                 data[len] = '\0';
256         ut_asserteq_str(usb_tree_reorder, data);
257         ut_assertok(usb_stop());
258
259         return 0;
260 }
261 DM_TEST(dm_test_usb_tree_reorder, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
262
263 static int dm_test_usb_keyb(struct unit_test_state *uts)
264 {
265         struct udevice *dev;
266
267         state_set_skip_delays(true);
268         ut_assertok(usb_init());
269
270         /* Initially there should be no characters */
271         ut_asserteq(0, tstc());
272
273         ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
274                                               &dev));
275
276         /*
277          * Add a string to the USB keyboard buffer - it should appear in
278          * stdin
279          */
280         ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
281         ut_asserteq(1, tstc());
282         ut_asserteq('a', getc());
283         ut_asserteq(1, tstc());
284         ut_asserteq('b', getc());
285         ut_asserteq(0, tstc());
286
287         ut_assertok(usb_stop());
288
289         return 0;
290 }
291 DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);