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)); |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 53 | ut_asserteq(2, dev_desc->block_read(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")); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | 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] | 59 | |
| 60 | /* test that we can handle multiple storage devices */ |
| 61 | static int dm_test_usb_multi(struct unit_test_state *uts) |
| 62 | { |
| 63 | struct udevice *dev; |
| 64 | |
| 65 | state_set_skip_delays(true); |
| 66 | ut_assertok(usb_init()); |
| 67 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); |
| 68 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev)); |
| 69 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev)); |
| 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 | |
| 100 | /* test that we can remove an emulated device and it is then not found */ |
| 101 | static int dm_test_usb_remove(struct unit_test_state *uts) |
| 102 | { |
| 103 | struct udevice *dev, *emul; |
| 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 | f0bfd6d | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 111 | ut_asserteq(6, count_usb_devices()); |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 112 | ut_assertok(usb_stop()); |
Simon Glass | f0bfd6d | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 113 | ut_asserteq(6, count_usb_devices()); |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 114 | |
| 115 | /* Remove the second emulation device */ |
| 116 | ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1", |
| 117 | &dev)); |
| 118 | ut_assertok(device_unbind(dev)); |
| 119 | |
| 120 | /* Rescan - only the first and third should be present */ |
| 121 | ut_assertok(usb_init()); |
| 122 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev)); |
| 123 | ut_assertok(usb_emul_find_for_dev(dev, &emul)); |
| 124 | ut_asserteq_str("flash-stick@0", emul->name); |
| 125 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev)); |
| 126 | ut_assertok(usb_emul_find_for_dev(dev, &emul)); |
| 127 | ut_asserteq_str("flash-stick@2", emul->name); |
| 128 | |
| 129 | ut_asserteq(-ENODEV, uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev)); |
| 130 | |
Simon Glass | f0bfd6d | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 131 | ut_asserteq(5, count_usb_devices()); |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 132 | ut_assertok(usb_stop()); |
Simon Glass | f0bfd6d | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 133 | ut_asserteq(5, count_usb_devices()); |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | DM_TEST(dm_test_usb_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 138 | |
| 139 | const char usb_tree_base[] = |
| 140 | " 1 Hub (12 Mb/s, 100mA)\n" |
| 141 | " | sandbox hub 2345\n" |
| 142 | " |\n" |
| 143 | " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n" |
| 144 | " | sandbox flash flash-stick@0\n" |
| 145 | " | \n" |
| 146 | " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n" |
| 147 | " | sandbox flash flash-stick@1\n" |
| 148 | " | \n" |
| 149 | " |\b+-4 Mass Storage (12 Mb/s, 100mA)\n" |
Simon Glass | f0bfd6d | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 150 | " | sandbox flash flash-stick@2\n" |
| 151 | " | \n" |
| 152 | " |\b+-5 Human Interface (12 Mb/s, 100mA)\n" |
| 153 | " sandbox keyboard keyb@3\n" |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 154 | " \n"; |
| 155 | |
| 156 | /* test that the 'usb tree' command output looks correct */ |
| 157 | static int dm_test_usb_tree(struct unit_test_state *uts) |
| 158 | { |
| 159 | char *data; |
| 160 | int len; |
| 161 | |
| 162 | state_set_skip_delays(true); |
| 163 | ut_assertok(usb_init()); |
| 164 | console_record_reset_enable(); |
| 165 | usb_show_tree(); |
| 166 | len = membuff_getraw(&gd->console_out, -1, true, &data); |
| 167 | if (len) |
| 168 | data[len] = '\0'; |
| 169 | ut_asserteq_str(usb_tree_base, data); |
| 170 | ut_assertok(usb_stop()); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | DM_TEST(dm_test_usb_tree, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 175 | |
| 176 | const char usb_tree_remove[] = |
| 177 | " 1 Hub (12 Mb/s, 100mA)\n" |
| 178 | " | sandbox hub 2345\n" |
| 179 | " |\n" |
| 180 | " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n" |
| 181 | " | sandbox flash flash-stick@0\n" |
| 182 | " | \n" |
| 183 | " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n" |
Simon Glass | f0bfd6d | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 184 | " | sandbox flash flash-stick@2\n" |
| 185 | " | \n" |
| 186 | " |\b+-4 Human Interface (12 Mb/s, 100mA)\n" |
| 187 | " sandbox keyboard keyb@3\n" |
Simon Glass | 4700fe5 | 2015-11-08 23:48:01 -0700 | [diff] [blame] | 188 | " \n"; |
| 189 | |
| 190 | /* |
| 191 | * test that the 'usb tree' command output looks correct when we remove a |
| 192 | * device |
| 193 | */ |
| 194 | static int dm_test_usb_tree_remove(struct unit_test_state *uts) |
| 195 | { |
| 196 | struct udevice *dev; |
| 197 | char *data; |
| 198 | int len; |
| 199 | |
| 200 | /* Remove the second emulation device */ |
| 201 | ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1", |
| 202 | &dev)); |
| 203 | ut_assertok(device_unbind(dev)); |
| 204 | |
| 205 | state_set_skip_delays(true); |
| 206 | ut_assertok(usb_init()); |
| 207 | console_record_reset_enable(); |
| 208 | usb_show_tree(); |
| 209 | len = membuff_getraw(&gd->console_out, -1, true, &data); |
| 210 | if (len) |
| 211 | data[len] = '\0'; |
| 212 | ut_asserteq_str(usb_tree_remove, data); |
| 213 | ut_assertok(usb_stop()); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | DM_TEST(dm_test_usb_tree_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 846bb37 | 2015-11-08 23:48:02 -0700 | [diff] [blame] | 218 | |
| 219 | const char usb_tree_reorder[] = |
| 220 | " 1 Hub (12 Mb/s, 100mA)\n" |
| 221 | " | sandbox hub 2345\n" |
| 222 | " |\n" |
| 223 | " |\b+-2 Mass Storage (12 Mb/s, 100mA)\n" |
| 224 | " | sandbox flash flash-stick@0\n" |
| 225 | " | \n" |
| 226 | " |\b+-3 Mass Storage (12 Mb/s, 100mA)\n" |
| 227 | " | sandbox flash flash-stick@2\n" |
| 228 | " | \n" |
Simon Glass | f0bfd6d | 2016-01-07 10:23:42 -0700 | [diff] [blame] | 229 | " |\b+-4 Human Interface (12 Mb/s, 100mA)\n" |
| 230 | " | sandbox keyboard keyb@3\n" |
| 231 | " | \n" |
| 232 | " |\b+-5 Mass Storage (12 Mb/s, 100mA)\n" |
Simon Glass | 846bb37 | 2015-11-08 23:48:02 -0700 | [diff] [blame] | 233 | " sandbox flash flash-stick@1\n" |
| 234 | " \n"; |
| 235 | |
| 236 | /* |
| 237 | * test that the 'usb tree' command output looks correct when we reorder two |
| 238 | * devices. |
| 239 | */ |
| 240 | static int dm_test_usb_tree_reorder(struct unit_test_state *uts) |
| 241 | { |
| 242 | struct udevice *dev, *parent; |
| 243 | char *data; |
| 244 | int len; |
| 245 | |
| 246 | /* Remove the second emulation device */ |
| 247 | ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1", |
| 248 | &dev)); |
| 249 | parent = dev->parent; |
| 250 | |
| 251 | /* Reorder the devices in the parent list and uclass list */ |
| 252 | list_del(&dev->sibling_node); |
| 253 | list_add_tail(&dev->sibling_node, &parent->child_head); |
| 254 | |
| 255 | list_del(&dev->uclass_node); |
| 256 | list_add_tail(&dev->uclass_node, &dev->uclass->dev_head); |
| 257 | |
| 258 | state_set_skip_delays(true); |
| 259 | ut_assertok(usb_init()); |
| 260 | console_record_reset_enable(); |
| 261 | usb_show_tree(); |
| 262 | len = membuff_getraw(&gd->console_out, -1, true, &data); |
| 263 | if (len) |
| 264 | data[len] = '\0'; |
| 265 | ut_asserteq_str(usb_tree_reorder, data); |
| 266 | ut_assertok(usb_stop()); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | DM_TEST(dm_test_usb_tree_reorder, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | c0ccc72 | 2015-11-08 23:48:08 -0700 | [diff] [blame] | 271 | |
| 272 | static int dm_test_usb_keyb(struct unit_test_state *uts) |
| 273 | { |
| 274 | struct udevice *dev; |
| 275 | |
| 276 | state_set_skip_delays(true); |
| 277 | ut_assertok(usb_init()); |
| 278 | |
| 279 | /* Initially there should be no characters */ |
| 280 | ut_asserteq(0, tstc()); |
| 281 | |
| 282 | ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb", |
| 283 | &dev)); |
| 284 | |
| 285 | /* |
| 286 | * Add a string to the USB keyboard buffer - it should appear in |
| 287 | * stdin |
| 288 | */ |
| 289 | ut_assertok(sandbox_usb_keyb_add_string(dev, "ab")); |
| 290 | ut_asserteq(1, tstc()); |
| 291 | ut_asserteq('a', getc()); |
| 292 | ut_asserteq(1, tstc()); |
| 293 | ut_asserteq('b', getc()); |
| 294 | ut_asserteq(0, tstc()); |
| 295 | |
| 296 | ut_assertok(usb_stop()); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |