Simon Glass | e00cb22 | 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 | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 8 | #include <console.h> |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <usb.h> |
| 11 | #include <asm/io.h> |
Simon Glass | 3884c98 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 12 | #include <asm/state.h> |
Simon Glass | bff1a71 | 2015-11-08 23:48:08 -0700 | [diff] [blame] | 13 | #include <asm/test.h> |
Simon Glass | 3884c98 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 15 | #include <dm/test.h> |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 16 | #include <dm/uclass-internal.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | #include <test/ut.h> |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 18 | |
| 19 | /* Test that sandbox USB works correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | static int dm_test_usb_base(struct unit_test_state *uts) |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 21 | { |
| 22 | struct udevice *bus; |
| 23 | |
| 24 | ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus)); |
| 25 | ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus)); |
| 26 | ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus)); |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 31 | |
| 32 | /* |
| 33 | * Test that we can use the flash stick. This is more of a functional test. It |
| 34 | * covers scanning the bug, setting up a hub and a flash stick and reading |
| 35 | * data from the flash stick. |
| 36 | */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 37 | static int dm_test_usb_flash(struct unit_test_state *uts) |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 38 | { |
| 39 | struct udevice *dev; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 40 | struct blk_desc *dev_desc; |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 41 | char cmp[1024]; |
| 42 | |
Simon Glass | 3884c98 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 43 | state_set_skip_delays(true); |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 44 | ut_assertok(usb_init()); |
| 45 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); |
Simon Glass | ebac37c | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 46 | ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc)); |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 47 | |
| 48 | /* Read a few blocks and look for the string we expect */ |
| 49 | ut_asserteq(512, dev_desc->blksz); |
| 50 | memset(cmp, '\0', sizeof(cmp)); |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 51 | ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp)); |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 52 | ut_assertok(strcmp(cmp, "this is a test")); |
Simon Glass | 61ccd88 | 2016-02-29 15:26:02 -0700 | [diff] [blame] | 53 | ut_assertok(usb_stop()); |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 58 | |
| 59 | /* test that we can handle multiple storage devices */ |
| 60 | static int dm_test_usb_multi(struct unit_test_state *uts) |
| 61 | { |
| 62 | struct udevice *dev; |
| 63 | |
| 64 | state_set_skip_delays(true); |
| 65 | ut_assertok(usb_init()); |
| 66 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); |
| 67 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev)); |
| 68 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev)); |
Simon Glass | 61ccd88 | 2016-02-29 15:26:02 -0700 | [diff] [blame] | 69 | ut_assertok(usb_stop()); |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 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 | |
Bin Meng | f4d4f7d | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 100 | /* test that no USB devices are found after we stop the stack */ |
| 101 | static int dm_test_usb_stop(struct unit_test_state *uts) |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 102 | { |
Bin Meng | f4d4f7d | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 103 | struct udevice *dev; |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 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)); |
Simon Glass | a57a817 | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 111 | ut_asserteq(6, count_usb_devices()); |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 112 | ut_assertok(usb_stop()); |
Bin Meng | f4d4f7d | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 113 | ut_asserteq(0, count_usb_devices()); |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
Bin Meng | f4d4f7d | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 117 | DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | bff1a71 | 2015-11-08 23:48:08 -0700 | [diff] [blame] | 118 | |
| 119 | static int dm_test_usb_keyb(struct unit_test_state *uts) |
| 120 | { |
| 121 | struct udevice *dev; |
| 122 | |
| 123 | state_set_skip_delays(true); |
| 124 | ut_assertok(usb_init()); |
| 125 | |
| 126 | /* Initially there should be no characters */ |
| 127 | ut_asserteq(0, tstc()); |
| 128 | |
| 129 | ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb", |
| 130 | &dev)); |
| 131 | |
| 132 | /* |
| 133 | * Add a string to the USB keyboard buffer - it should appear in |
| 134 | * stdin |
| 135 | */ |
| 136 | ut_assertok(sandbox_usb_keyb_add_string(dev, "ab")); |
| 137 | ut_asserteq(1, tstc()); |
| 138 | ut_asserteq('a', getc()); |
| 139 | ut_asserteq(1, tstc()); |
| 140 | ut_asserteq('b', getc()); |
| 141 | ut_asserteq(0, tstc()); |
| 142 | |
| 143 | ut_assertok(usb_stop()); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |