blob: ef454b0ae581e6fceddc6a2803402f8c943e34e3 [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>
9#include <usb.h>
10#include <asm/io.h>
Simon Glass3884c982015-11-08 23:47:44 -070011#include <asm/state.h>
Simon Glassbff1a712015-11-08 23:48:08 -070012#include <asm/test.h>
Simon Glass3884c982015-11-08 23:47:44 -070013#include <dm/device-internal.h>
Simon Glasse00cb222015-03-25 12:23:05 -060014#include <dm/test.h>
Simon Glass431cbd62015-11-08 23:48:01 -070015#include <dm/uclass-internal.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050016#include <test/ut.h>
Simon Glasse00cb222015-03-25 12:23:05 -060017
18/* Test that sandbox USB works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -050019static int dm_test_usb_base(struct unit_test_state *uts)
Simon Glasse00cb222015-03-25 12:23:05 -060020{
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}
29DM_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 */
Joe Hershbergere721b882015-05-20 14:27:27 -050036static int dm_test_usb_flash(struct unit_test_state *uts)
Simon Glasse00cb222015-03-25 12:23:05 -060037{
38 struct udevice *dev;
Simon Glass4101f682016-02-29 15:25:34 -070039 struct blk_desc *dev_desc;
Simon Glasse00cb222015-03-25 12:23:05 -060040 char cmp[1024];
41
Simon Glass3884c982015-11-08 23:47:44 -070042 state_set_skip_delays(true);
Simon Glasse00cb222015-03-25 12:23:05 -060043 ut_assertok(usb_init());
44 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
Simon Glassebac37c2016-02-29 15:25:43 -070045 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
Simon Glasse00cb222015-03-25 12:23:05 -060046
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));
Simon Glass2a981dc2016-02-29 15:25:52 -070050 ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
Simon Glasse00cb222015-03-25 12:23:05 -060051 ut_assertok(strcmp(cmp, "this is a test"));
Simon Glass61ccd882016-02-29 15:26:02 -070052 ut_assertok(usb_stop());
Simon Glasse00cb222015-03-25 12:23:05 -060053
54 return 0;
55}
56DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass431cbd62015-11-08 23:48:01 -070057
58/* test that we can handle multiple storage devices */
59static 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));
Simon Glass61ccd882016-02-29 15:26:02 -070068 ut_assertok(usb_stop());
Simon Glass431cbd62015-11-08 23:48:01 -070069
70 return 0;
71}
72DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
73
74static 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
Bin Mengf4d4f7d2017-10-01 06:19:45 -070099/* test that no USB devices are found after we stop the stack */
100static int dm_test_usb_stop(struct unit_test_state *uts)
Simon Glass431cbd62015-11-08 23:48:01 -0700101{
Bin Mengf4d4f7d2017-10-01 06:19:45 -0700102 struct udevice *dev;
Simon Glass431cbd62015-11-08 23:48:01 -0700103
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));
Simon Glassa57a8172016-01-07 10:23:42 -0700110 ut_asserteq(6, count_usb_devices());
Simon Glass431cbd62015-11-08 23:48:01 -0700111 ut_assertok(usb_stop());
Bin Mengf4d4f7d2017-10-01 06:19:45 -0700112 ut_asserteq(0, count_usb_devices());
Simon Glass431cbd62015-11-08 23:48:01 -0700113
114 return 0;
115}
Bin Mengf4d4f7d2017-10-01 06:19:45 -0700116DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassbff1a712015-11-08 23:48:08 -0700117
118static 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
Peng Fan819ac502019-05-22 07:08:12 +0000128 ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb@3",
Simon Glassbff1a712015-11-08 23:48:08 -0700129 &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}
146DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);