Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 8 | #include <console.h> |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <usb.h> |
| 11 | #include <asm/io.h> |
Simon Glass | 2d7c499 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 12 | #include <asm/state.h> |
Simon Glass | c0ccc72 | 2015-11-08 23:48:08 -0700 | [diff] [blame] | 13 | #include <asm/test.h> |
Simon Glass | 2d7c499 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 15 | #include <dm/test.h> |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 16 | #include <dm/uclass-internal.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | #include <test/ut.h> |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 18 | |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 21 | /* Test that sandbox USB works correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 22 | static int dm_test_usb_base(struct unit_test_state *uts) |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 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 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 39 | static int dm_test_usb_flash(struct unit_test_state *uts) |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 40 | { |
| 41 | struct udevice *dev; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 42 | struct blk_desc *dev_desc; |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 43 | char cmp[1024]; |
| 44 | |
Simon Glass | 2d7c499 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 45 | state_set_skip_delays(true); |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 46 | ut_assertok(usb_init()); |
| 47 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); |
Simon Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 48 | ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc)); |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 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)); |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 53 | ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp)); |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 54 | ut_assertok(strcmp(cmp, "this is a test")); |
Simon Glass | 582b76e | 2016-02-29 15:26:02 -0700 | [diff] [blame] | 55 | ut_assertok(usb_stop()); |
Simon Glass | 3168048 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 60 | |
| 61 | /* test that we can handle multiple storage devices */ |
| 62 | static int dm_test_usb_multi(struct unit_test_state *uts) |
| 63 | { |
| 64 | struct udevice *dev; |
| 65 | |
| 66 | state_set_skip_delays(true); |
| 67 | ut_assertok(usb_init()); |
| 68 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); |
| 69 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev)); |
| 70 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev)); |
Simon Glass | 582b76e | 2016-02-29 15:26:02 -0700 | [diff] [blame] | 71 | ut_assertok(usb_stop()); |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 76 | |
| 77 | static int count_usb_devices(void) |
| 78 | { |
| 79 | struct udevice *hub; |
| 80 | struct uclass *uc; |
| 81 | int count = 0; |
| 82 | int ret; |
| 83 | |
| 84 | ret = uclass_get(UCLASS_USB_HUB, &uc); |
| 85 | if (ret) |
| 86 | return ret; |
| 87 | |
| 88 | uclass_foreach_dev(hub, uc) { |
| 89 | struct udevice *dev; |
| 90 | |
| 91 | count++; |
| 92 | for (device_find_first_child(hub, &dev); |
| 93 | dev; |
| 94 | device_find_next_child(&dev)) { |
| 95 | count++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return count; |
| 100 | } |
| 101 | |
Bin Meng | b6189f2 | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 102 | /* test that no USB devices are found after we stop the stack */ |
| 103 | static int dm_test_usb_stop(struct unit_test_state *uts) |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 104 | { |
Bin Meng | b6189f2 | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 105 | struct udevice *dev; |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 106 | |
| 107 | /* Scan and check that all devices are present */ |
| 108 | state_set_skip_delays(true); |
| 109 | ut_assertok(usb_init()); |
| 110 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); |
| 111 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev)); |
| 112 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev)); |
Simon Glass | f0bfd6d | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 113 | ut_asserteq(6, count_usb_devices()); |
Simon Glass | 846bb37 | 2015-11-08 23:48:02 -0700 | [diff] [blame] | 114 | ut_assertok(usb_stop()); |
Bin Meng | b6189f2 | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 115 | ut_asserteq(0, count_usb_devices()); |
Simon Glass | 846bb37 | 2015-11-08 23:48:02 -0700 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |
Bin Meng | b6189f2 | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 119 | DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | c0ccc72 | 2015-11-08 23:48:08 -0700 | [diff] [blame] | 120 | |
| 121 | static int dm_test_usb_keyb(struct unit_test_state *uts) |
| 122 | { |
| 123 | struct udevice *dev; |
| 124 | |
| 125 | state_set_skip_delays(true); |
| 126 | ut_assertok(usb_init()); |
| 127 | |
| 128 | /* Initially there should be no characters */ |
| 129 | ut_asserteq(0, tstc()); |
| 130 | |
| 131 | ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb", |
| 132 | &dev)); |
| 133 | |
| 134 | /* |
| 135 | * Add a string to the USB keyboard buffer - it should appear in |
| 136 | * stdin |
| 137 | */ |
| 138 | ut_assertok(sandbox_usb_keyb_add_string(dev, "ab")); |
| 139 | ut_asserteq(1, tstc()); |
| 140 | ut_asserteq('a', getc()); |
| 141 | ut_asserteq(1, tstc()); |
| 142 | ut_asserteq('b', getc()); |
| 143 | ut_asserteq(0, tstc()); |
| 144 | |
| 145 | ut_assertok(usb_stop()); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |