blob: 5d6ceefce0bb2671155d810bec63ade352316101 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse00cb222015-03-25 12:23:05 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glasse00cb222015-03-25 12:23:05 -06004 */
5
6#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -07007#include <console.h>
Simon Glasse00cb222015-03-25 12:23:05 -06008#include <dm.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06009#include <part.h>
Simon Glasse00cb222015-03-25 12:23:05 -060010#include <usb.h>
11#include <asm/io.h>
Simon Glass3884c982015-11-08 23:47:44 -070012#include <asm/state.h>
Simon Glassbff1a712015-11-08 23:48:08 -070013#include <asm/test.h>
Simon Glass3884c982015-11-08 23:47:44 -070014#include <dm/device-internal.h>
Simon Glasse00cb222015-03-25 12:23:05 -060015#include <dm/test.h>
Simon Glass431cbd62015-11-08 23:48:01 -070016#include <dm/uclass-internal.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060017#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050018#include <test/ut.h>
Simon Glasse00cb222015-03-25 12:23:05 -060019
Heinrich Schuchardt12f1e792019-11-23 18:15:23 +010020struct keyboard_test_data {
21 const char modifiers;
22 const char scancode;
23 const char result[6];
24};
25
Simon Glasse00cb222015-03-25 12:23:05 -060026/* Test that sandbox USB works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -050027static int dm_test_usb_base(struct unit_test_state *uts)
Simon Glasse00cb222015-03-25 12:23:05 -060028{
29 struct udevice *bus;
30
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));
34
35 return 0;
36}
Simon Glasse180c2b2020-07-28 19:41:12 -060037DM_TEST(dm_test_usb_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasse00cb222015-03-25 12:23:05 -060038
39/*
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.
43 */
Joe Hershbergere721b882015-05-20 14:27:27 -050044static int dm_test_usb_flash(struct unit_test_state *uts)
Simon Glasse00cb222015-03-25 12:23:05 -060045{
46 struct udevice *dev;
Simon Glass4101f682016-02-29 15:25:34 -070047 struct blk_desc *dev_desc;
Simon Glasse00cb222015-03-25 12:23:05 -060048 char cmp[1024];
49
Simon Glass3884c982015-11-08 23:47:44 -070050 state_set_skip_delays(true);
Simon Glasse00cb222015-03-25 12:23:05 -060051 ut_assertok(usb_init());
52 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
Simon Glassebac37c2016-02-29 15:25:43 -070053 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
Simon Glasse00cb222015-03-25 12:23:05 -060054
55 /* Read a few blocks and look for the string we expect */
56 ut_asserteq(512, dev_desc->blksz);
57 memset(cmp, '\0', sizeof(cmp));
Simon Glass2a981dc2016-02-29 15:25:52 -070058 ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
Simon Glasse00cb222015-03-25 12:23:05 -060059 ut_assertok(strcmp(cmp, "this is a test"));
Simon Glass61ccd882016-02-29 15:26:02 -070060 ut_assertok(usb_stop());
Simon Glasse00cb222015-03-25 12:23:05 -060061
62 return 0;
63}
Simon Glasse180c2b2020-07-28 19:41:12 -060064DM_TEST(dm_test_usb_flash, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass431cbd62015-11-08 23:48:01 -070065
66/* test that we can handle multiple storage devices */
67static int dm_test_usb_multi(struct unit_test_state *uts)
68{
69 struct udevice *dev;
70
71 state_set_skip_delays(true);
72 ut_assertok(usb_init());
73 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
74 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
75 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
Simon Glass61ccd882016-02-29 15:26:02 -070076 ut_assertok(usb_stop());
Simon Glass431cbd62015-11-08 23:48:01 -070077
78 return 0;
79}
Simon Glasse180c2b2020-07-28 19:41:12 -060080DM_TEST(dm_test_usb_multi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass431cbd62015-11-08 23:48:01 -070081
Michael Wallec03b7612020-06-02 01:47:07 +020082/* test that we have an associated ofnode with the usb device */
83static int dm_test_usb_fdt_node(struct unit_test_state *uts)
84{
85 struct udevice *dev;
86 ofnode node;
87
88 state_set_skip_delays(true);
89 ut_assertok(usb_init());
90 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
91 node = ofnode_path("/usb@1/hub/usbstor@1");
92 ut_asserteq(1, ofnode_equal(node, dev_ofnode(dev)));
93 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
94 ut_asserteq(1, ofnode_equal(ofnode_null(), dev_ofnode(dev)));
95 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
96 node = ofnode_path("/usb@1/hub/usbstor@3");
97 ut_asserteq(1, ofnode_equal(node, dev_ofnode(dev)));
98 ut_assertok(usb_stop());
99
100 return 0;
101}
Simon Glasse180c2b2020-07-28 19:41:12 -0600102DM_TEST(dm_test_usb_fdt_node, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Michael Wallec03b7612020-06-02 01:47:07 +0200103
Simon Glass431cbd62015-11-08 23:48:01 -0700104static int count_usb_devices(void)
105{
106 struct udevice *hub;
107 struct uclass *uc;
108 int count = 0;
109 int ret;
110
111 ret = uclass_get(UCLASS_USB_HUB, &uc);
112 if (ret)
113 return ret;
114
115 uclass_foreach_dev(hub, uc) {
116 struct udevice *dev;
117
118 count++;
119 for (device_find_first_child(hub, &dev);
120 dev;
121 device_find_next_child(&dev)) {
122 count++;
123 }
124 }
125
126 return count;
127}
128
Bin Mengf4d4f7d2017-10-01 06:19:45 -0700129/* test that no USB devices are found after we stop the stack */
130static int dm_test_usb_stop(struct unit_test_state *uts)
Simon Glass431cbd62015-11-08 23:48:01 -0700131{
Bin Mengf4d4f7d2017-10-01 06:19:45 -0700132 struct udevice *dev;
Simon Glass431cbd62015-11-08 23:48:01 -0700133
134 /* Scan and check that all devices are present */
135 state_set_skip_delays(true);
136 ut_assertok(usb_init());
137 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
138 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
139 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
Simon Glassa57a8172016-01-07 10:23:42 -0700140 ut_asserteq(6, count_usb_devices());
Simon Glass431cbd62015-11-08 23:48:01 -0700141 ut_assertok(usb_stop());
Bin Mengf4d4f7d2017-10-01 06:19:45 -0700142 ut_asserteq(0, count_usb_devices());
Simon Glass431cbd62015-11-08 23:48:01 -0700143
144 return 0;
145}
Simon Glasse180c2b2020-07-28 19:41:12 -0600146DM_TEST(dm_test_usb_stop, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassbff1a712015-11-08 23:48:08 -0700147
Heinrich Schuchardt12f1e792019-11-23 18:15:23 +0100148/**
149 * dm_test_usb_keyb() - test USB keyboard driver
150 *
151 * This test copies USB keyboard scan codes into the key buffer of the USB
152 * keyboard emulation driver. These are picked up during emulated interrupts
153 * by the USB keyboard driver and converted to characters and escape sequences.
154 * The test then reads and verifies these characters and escape sequences from
155 * the standard input.
156 *
157 * TODO: The following features are not yet tested:
158 *
159 * * LED status
160 * * caps-lock
161 * * num-lock
162 * * numerical pad keys
163 *
164 * TODO: The following features are not yet implemented by the USB keyboard
165 * driver and therefore not tested:
166 *
167 * * modifiers for non-alpha-numeric keys, e.g. <SHIFT><TAB> and <ALT><F4>
168 * * some special keys, e.g. <PRINT>
169 * * some modifiers, e.g. <ALT> and <META>
170 * * alternative keyboard layouts
171 *
172 * @uts: unit test state
173 * Return: 0 on success
174 */
Simon Glassbff1a712015-11-08 23:48:08 -0700175static int dm_test_usb_keyb(struct unit_test_state *uts)
176{
177 struct udevice *dev;
Heinrich Schuchardt12f1e792019-11-23 18:15:23 +0100178 const struct keyboard_test_data *pos;
179 const struct keyboard_test_data kbd_test_data[] = {
180 /* <A> */
181 {0x00, 0x04, "a"},
182 /* <B> */
183 {0x00, 0x05, "b"},
184 /* <C> */
185 {0x00, 0x06, "c"},
186 /* <D> */
187 {0x00, 0x07, "d"},
188 /* <E> */
189 {0x00, 0x08, "e"},
190 /* <F> */
191 {0x00, 0x09, "f"},
192 /* <G> */
193 {0x00, 0x0a, "g"},
194 /* <H> */
195 {0x00, 0x0b, "h"},
196 /* <I> */
197 {0x00, 0x0c, "i"},
198 /* <J> */
199 {0x00, 0x0d, "j"},
200 /* <K> */
201 {0x00, 0x0e, "k"},
202 /* <L> */
203 {0x00, 0x0f, "l"},
204 /* <M> */
205 {0x00, 0x10, "m"},
206 /* <N> */
207 {0x00, 0x11, "n"},
208 /* <O> */
209 {0x00, 0x12, "o"},
210 /* <P> */
211 {0x00, 0x13, "p"},
212 /* <Q> */
213 {0x00, 0x14, "q"},
214 /* <R> */
215 {0x00, 0x15, "r"},
216 /* <S> */
217 {0x00, 0x16, "s"},
218 /* <T> */
219 {0x00, 0x17, "t"},
220 /* <U> */
221 {0x00, 0x18, "u"},
222 /* <V> */
223 {0x00, 0x19, "v"},
224 /* <W> */
225 {0x00, 0x1a, "w"},
226 /* <X> */
227 {0x00, 0x1b, "x"},
228 /* <Y> */
229 {0x00, 0x1c, "y"},
230 /* <Z> */
231 {0x00, 0x1d, "z"},
232
233 /* <LEFT-SHIFT><A> */
234 {0x02, 0x04, "A"},
235 /* <RIGHT-SHIFT><Z> */
236 {0x20, 0x1d, "Z"},
237
238 /* <LEFT-CONTROL><A> */
239 {0x01, 0x04, "\x01"},
240 /* <RIGHT-CONTROL><Z> */
241 {0x10, 0x1d, "\x1a"},
242
243 /* <1> */
244 {0x00, 0x1e, "1"},
245 /* <2> */
246 {0x00, 0x1f, "2"},
247 /* <3> */
248 {0x00, 0x20, "3"},
249 /* <4> */
250 {0x00, 0x21, "4"},
251 /* <5> */
252 {0x00, 0x22, "5"},
253 /* <6> */
254 {0x00, 0x23, "6"},
255 /* <7> */
256 {0x00, 0x24, "7"},
257 /* <8> */
258 {0x00, 0x25, "8"},
259 /* <9> */
260 {0x00, 0x26, "9"},
261 /* <0> */
262 {0x00, 0x27, "0"},
263
264 /* <LEFT-SHIFT><1> */
265 {0x02, 0x1e, "!"},
266 /* <RIGHT-SHIFT><2> */
267 {0x20, 0x1f, "@"},
268 /* <LEFT-SHIFT><3> */
269 {0x02, 0x20, "#"},
270 /* <RIGHT-SHIFT><4> */
271 {0x20, 0x21, "$"},
272 /* <LEFT-SHIFT><5> */
273 {0x02, 0x22, "%"},
274 /* <RIGHT-SHIFT><6> */
275 {0x20, 0x23, "^"},
276 /* <LEFT-SHIFT><7> */
277 {0x02, 0x24, "&"},
278 /* <RIGHT-SHIFT><8> */
279 {0x20, 0x25, "*"},
280 /* <LEFT-SHIFT><9> */
281 {0x02, 0x26, "("},
282 /* <RIGHT-SHIFT><0> */
283 {0x20, 0x27, ")"},
284
285 /* <ENTER> */
286 {0x00, 0x28, "\r"},
287 /* <ESCAPE> */
288 {0x00, 0x29, "\x1b"},
289 /* <BACKSPACE> */
290 {0x00, 0x2a, "\x08"},
291 /* <TAB> */
292 {0x00, 0x2b, "\x09"},
293 /* <SPACE> */
294 {0x00, 0x2c, " "},
295 /* <MINUS> */
296 {0x00, 0x2d, "-"},
297 /* <EQUAL> */
298 {0x00, 0x2e, "="},
299 /* <LEFT BRACE> */
300 {0x00, 0x2f, "["},
301 /* <RIGHT BRACE> */
302 {0x00, 0x30, "]"},
303 /* <BACKSLASH> */
304 {0x00, 0x31, "\\"},
305 /* <HASH-TILDE> */
306 {0x00, 0x32, "#"},
307 /* <SEMICOLON> */
308 {0x00, 0x33, ";"},
309 /* <APOSTROPHE> */
310 {0x00, 0x34, "'"},
311 /* <GRAVE> */
312 {0x00, 0x35, "`"},
313 /* <COMMA> */
314 {0x00, 0x36, ","},
315 /* <DOT> */
316 {0x00, 0x37, "."},
317 /* <SLASH> */
318 {0x00, 0x38, "/"},
319
320 /* <LEFT-SHIFT><ENTER> */
321 {0x02, 0x28, "\r"},
322 /* <RIGHT-SHIFT><ESCAPE> */
323 {0x20, 0x29, "\x1b"},
324 /* <LEFT-SHIFT><BACKSPACE> */
325 {0x02, 0x2a, "\x08"},
326 /* <RIGHT-SHIFT><TAB> */
327 {0x20, 0x2b, "\x09"},
328 /* <LEFT-SHIFT><SPACE> */
329 {0x02, 0x2c, " "},
330 /* <MINUS> */
331 {0x20, 0x2d, "_"},
332 /* <LEFT-SHIFT><EQUAL> */
333 {0x02, 0x2e, "+"},
334 /* <RIGHT-SHIFT><LEFT BRACE> */
335 {0x20, 0x2f, "{"},
336 /* <LEFT-SHIFT><RIGHT BRACE> */
337 {0x02, 0x30, "}"},
338 /* <RIGHT-SHIFT><BACKSLASH> */
339 {0x20, 0x31, "|"},
340 /* <LEFT-SHIFT><HASH-TILDE> */
341 {0x02, 0x32, "~"},
342 /* <RIGHT-SHIFT><SEMICOLON> */
343 {0x20, 0x33, ":"},
344 /* <LEFT-SHIFT><APOSTROPHE> */
345 {0x02, 0x34, "\""},
346 /* <RIGHT-SHIFT><GRAVE> */
347 {0x20, 0x35, "~"},
348 /* <LEFT-SHIFT><COMMA> */
349 {0x02, 0x36, "<"},
350 /* <RIGHT-SHIFT><DOT> */
351 {0x20, 0x37, ">"},
352 /* <LEFT-SHIFT><SLASH> */
353 {0x02, 0x38, "?"},
354#ifdef CONFIG_USB_KEYBOARD_FN_KEYS
355 /* <F1> */
356 {0x00, 0x3a, "\x1bOP"},
357 /* <F2> */
358 {0x00, 0x3b, "\x1bOQ"},
359 /* <F3> */
360 {0x00, 0x3c, "\x1bOR"},
361 /* <F4> */
362 {0x00, 0x3d, "\x1bOS"},
363 /* <F5> */
364 {0x00, 0x3e, "\x1b[15~"},
365 /* <F6> */
366 {0x00, 0x3f, "\x1b[17~"},
367 /* <F7> */
368 {0x00, 0x40, "\x1b[18~"},
369 /* <F8> */
370 {0x00, 0x41, "\x1b[19~"},
371 /* <F9> */
372 {0x00, 0x42, "\x1b[20~"},
373 /* <F10> */
374 {0x00, 0x43, "\x1b[21~"},
375 /* <F11> */
376 {0x00, 0x44, "\x1b[23~"},
377 /* <F12> */
378 {0x00, 0x45, "\x1b[24~"},
379 /* <INSERT> */
380 {0x00, 0x49, "\x1b[2~"},
381 /* <HOME> */
382 {0x00, 0x4a, "\x1b[H"},
383 /* <PAGE UP> */
384 {0x00, 0x4b, "\x1b[5~"},
385 /* <DELETE> */
386 {0x00, 0x4c, "\x1b[3~"},
387 /* <END> */
388 {0x00, 0x4d, "\x1b[F"},
389 /* <PAGE DOWN> */
390 {0x00, 0x4e, "\x1b[6~"},
391 /* <RIGHT> */
392 {0x00, 0x4f, "\x1b[C"},
393 /* <LEFT> */
394 {0x00, 0x50, "\x1b[D"},
395 /* <DOWN> */
396 {0x00, 0x51, "\x1b[B"},
397 /* <UP> */
398 {0x00, 0x52, "\x1b[A"},
399#endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
400
401 /* End of list */
402 {0x00, 0x00, "\0"}
403 };
404
Simon Glassbff1a712015-11-08 23:48:08 -0700405
406 state_set_skip_delays(true);
407 ut_assertok(usb_init());
408
409 /* Initially there should be no characters */
410 ut_asserteq(0, tstc());
411
Peng Fan819ac502019-05-22 07:08:12 +0000412 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb@3",
Simon Glassbff1a712015-11-08 23:48:08 -0700413 &dev));
414
415 /*
Heinrich Schuchardt12f1e792019-11-23 18:15:23 +0100416 * Add scan codes to the USB keyboard buffer. They should appear as
417 * corresponding characters and escape sequences in stdin.
Simon Glassbff1a712015-11-08 23:48:08 -0700418 */
Heinrich Schuchardt12f1e792019-11-23 18:15:23 +0100419 for (pos = kbd_test_data; pos->scancode; ++pos) {
420 const char *c;
421 char scancodes[USB_KBD_BOOT_REPORT_SIZE] = {0};
Simon Glassbff1a712015-11-08 23:48:08 -0700422
Heinrich Schuchardt12f1e792019-11-23 18:15:23 +0100423 scancodes[0] = pos->modifiers;
424 scancodes[2] = pos->scancode;
425
426 ut_assertok(sandbox_usb_keyb_add_string(dev, scancodes));
427
428 for (c = pos->result; *c; ++c) {
429 ut_asserteq(1, tstc());
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200430 ut_asserteq(*c, getchar());
Heinrich Schuchardt12f1e792019-11-23 18:15:23 +0100431 }
432 ut_asserteq(0, tstc());
433 }
Simon Glassbff1a712015-11-08 23:48:08 -0700434 ut_assertok(usb_stop());
435
436 return 0;
437}
Simon Glasse180c2b2020-07-28 19:41:12 -0600438DM_TEST(dm_test_usb_keyb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);