Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 7 | #include <console.h> |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <usb.h> |
| 10 | #include <asm/io.h> |
Simon Glass | 3884c98 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 11 | #include <asm/state.h> |
Simon Glass | bff1a71 | 2015-11-08 23:48:08 -0700 | [diff] [blame] | 12 | #include <asm/test.h> |
Simon Glass | 3884c98 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 14 | #include <dm/test.h> |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 15 | #include <dm/uclass-internal.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | #include <test/ut.h> |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 17 | |
| 18 | /* Test that sandbox USB works correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | static int dm_test_usb_base(struct unit_test_state *uts) |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 20 | { |
| 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 | } |
| 29 | DM_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 Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 36 | static int dm_test_usb_flash(struct unit_test_state *uts) |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 37 | { |
| 38 | struct udevice *dev; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 39 | struct blk_desc *dev_desc; |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 40 | char cmp[1024]; |
| 41 | |
Simon Glass | 3884c98 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 42 | state_set_skip_delays(true); |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 43 | ut_assertok(usb_init()); |
| 44 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); |
Simon Glass | ebac37c | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 45 | ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc)); |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 46 | |
| 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 Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 50 | ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp)); |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 51 | ut_assertok(strcmp(cmp, "this is a test")); |
Simon Glass | 61ccd88 | 2016-02-29 15:26:02 -0700 | [diff] [blame] | 52 | ut_assertok(usb_stop()); |
Simon Glass | e00cb22 | 2015-03-25 12:23:05 -0600 | [diff] [blame] | 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | 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] | 57 | |
| 58 | /* test that we can handle multiple storage devices */ |
| 59 | static 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 Glass | 61ccd88 | 2016-02-29 15:26:02 -0700 | [diff] [blame] | 68 | ut_assertok(usb_stop()); |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 73 | |
| 74 | static 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 Meng | f4d4f7d | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 99 | /* test that no USB devices are found after we stop the stack */ |
| 100 | static int dm_test_usb_stop(struct unit_test_state *uts) |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 101 | { |
Bin Meng | f4d4f7d | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 102 | struct udevice *dev; |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 103 | |
| 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 Glass | a57a817 | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 110 | ut_asserteq(6, count_usb_devices()); |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 111 | ut_assertok(usb_stop()); |
Bin Meng | f4d4f7d | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 112 | ut_asserteq(0, count_usb_devices()); |
Simon Glass | 431cbd6 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
Bin Meng | f4d4f7d | 2017-10-01 06:19:45 -0700 | [diff] [blame] | 116 | 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] | 117 | |
| 118 | static 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 | |
| 128 | ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb", |
| 129 | &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 | } |
| 146 | DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |