usb.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <console.h>
  7. #include <dm.h>
  8. #include <usb.h>
  9. #include <asm/io.h>
  10. #include <asm/state.h>
  11. #include <asm/test.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/test.h>
  14. #include <dm/uclass-internal.h>
  15. #include <test/ut.h>
  16. struct keyboard_test_data {
  17. const char modifiers;
  18. const char scancode;
  19. const char result[6];
  20. };
  21. /* Test that sandbox USB works correctly */
  22. static int dm_test_usb_base(struct unit_test_state *uts)
  23. {
  24. struct udevice *bus;
  25. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
  26. ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
  27. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
  28. return 0;
  29. }
  30. DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  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. */
  36. static int dm_test_usb_flash(struct unit_test_state *uts)
  37. {
  38. struct udevice *dev;
  39. struct blk_desc *dev_desc;
  40. char cmp[1024];
  41. state_set_skip_delays(true);
  42. ut_assertok(usb_init());
  43. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  44. ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
  45. /* Read a few blocks and look for the string we expect */
  46. ut_asserteq(512, dev_desc->blksz);
  47. memset(cmp, '\0', sizeof(cmp));
  48. ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
  49. ut_assertok(strcmp(cmp, "this is a test"));
  50. ut_assertok(usb_stop());
  51. return 0;
  52. }
  53. DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  54. /* test that we can handle multiple storage devices */
  55. static int dm_test_usb_multi(struct unit_test_state *uts)
  56. {
  57. struct udevice *dev;
  58. state_set_skip_delays(true);
  59. ut_assertok(usb_init());
  60. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  61. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  62. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  63. ut_assertok(usb_stop());
  64. return 0;
  65. }
  66. DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  67. static int count_usb_devices(void)
  68. {
  69. struct udevice *hub;
  70. struct uclass *uc;
  71. int count = 0;
  72. int ret;
  73. ret = uclass_get(UCLASS_USB_HUB, &uc);
  74. if (ret)
  75. return ret;
  76. uclass_foreach_dev(hub, uc) {
  77. struct udevice *dev;
  78. count++;
  79. for (device_find_first_child(hub, &dev);
  80. dev;
  81. device_find_next_child(&dev)) {
  82. count++;
  83. }
  84. }
  85. return count;
  86. }
  87. /* test that no USB devices are found after we stop the stack */
  88. static int dm_test_usb_stop(struct unit_test_state *uts)
  89. {
  90. struct udevice *dev;
  91. /* Scan and check that all devices are present */
  92. state_set_skip_delays(true);
  93. ut_assertok(usb_init());
  94. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
  95. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
  96. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
  97. ut_asserteq(6, count_usb_devices());
  98. ut_assertok(usb_stop());
  99. ut_asserteq(0, count_usb_devices());
  100. return 0;
  101. }
  102. DM_TEST(dm_test_usb_stop, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  103. /**
  104. * dm_test_usb_keyb() - test USB keyboard driver
  105. *
  106. * This test copies USB keyboard scan codes into the key buffer of the USB
  107. * keyboard emulation driver. These are picked up during emulated interrupts
  108. * by the USB keyboard driver and converted to characters and escape sequences.
  109. * The test then reads and verifies these characters and escape sequences from
  110. * the standard input.
  111. *
  112. * TODO: The following features are not yet tested:
  113. *
  114. * * LED status
  115. * * caps-lock
  116. * * num-lock
  117. * * numerical pad keys
  118. *
  119. * TODO: The following features are not yet implemented by the USB keyboard
  120. * driver and therefore not tested:
  121. *
  122. * * modifiers for non-alpha-numeric keys, e.g. <SHIFT><TAB> and <ALT><F4>
  123. * * some special keys, e.g. <PRINT>
  124. * * some modifiers, e.g. <ALT> and <META>
  125. * * alternative keyboard layouts
  126. *
  127. * @uts: unit test state
  128. * Return: 0 on success
  129. */
  130. static int dm_test_usb_keyb(struct unit_test_state *uts)
  131. {
  132. struct udevice *dev;
  133. const struct keyboard_test_data *pos;
  134. const struct keyboard_test_data kbd_test_data[] = {
  135. /* <A> */
  136. {0x00, 0x04, "a"},
  137. /* <B> */
  138. {0x00, 0x05, "b"},
  139. /* <C> */
  140. {0x00, 0x06, "c"},
  141. /* <D> */
  142. {0x00, 0x07, "d"},
  143. /* <E> */
  144. {0x00, 0x08, "e"},
  145. /* <F> */
  146. {0x00, 0x09, "f"},
  147. /* <G> */
  148. {0x00, 0x0a, "g"},
  149. /* <H> */
  150. {0x00, 0x0b, "h"},
  151. /* <I> */
  152. {0x00, 0x0c, "i"},
  153. /* <J> */
  154. {0x00, 0x0d, "j"},
  155. /* <K> */
  156. {0x00, 0x0e, "k"},
  157. /* <L> */
  158. {0x00, 0x0f, "l"},
  159. /* <M> */
  160. {0x00, 0x10, "m"},
  161. /* <N> */
  162. {0x00, 0x11, "n"},
  163. /* <O> */
  164. {0x00, 0x12, "o"},
  165. /* <P> */
  166. {0x00, 0x13, "p"},
  167. /* <Q> */
  168. {0x00, 0x14, "q"},
  169. /* <R> */
  170. {0x00, 0x15, "r"},
  171. /* <S> */
  172. {0x00, 0x16, "s"},
  173. /* <T> */
  174. {0x00, 0x17, "t"},
  175. /* <U> */
  176. {0x00, 0x18, "u"},
  177. /* <V> */
  178. {0x00, 0x19, "v"},
  179. /* <W> */
  180. {0x00, 0x1a, "w"},
  181. /* <X> */
  182. {0x00, 0x1b, "x"},
  183. /* <Y> */
  184. {0x00, 0x1c, "y"},
  185. /* <Z> */
  186. {0x00, 0x1d, "z"},
  187. /* <LEFT-SHIFT><A> */
  188. {0x02, 0x04, "A"},
  189. /* <RIGHT-SHIFT><Z> */
  190. {0x20, 0x1d, "Z"},
  191. /* <LEFT-CONTROL><A> */
  192. {0x01, 0x04, "\x01"},
  193. /* <RIGHT-CONTROL><Z> */
  194. {0x10, 0x1d, "\x1a"},
  195. /* <1> */
  196. {0x00, 0x1e, "1"},
  197. /* <2> */
  198. {0x00, 0x1f, "2"},
  199. /* <3> */
  200. {0x00, 0x20, "3"},
  201. /* <4> */
  202. {0x00, 0x21, "4"},
  203. /* <5> */
  204. {0x00, 0x22, "5"},
  205. /* <6> */
  206. {0x00, 0x23, "6"},
  207. /* <7> */
  208. {0x00, 0x24, "7"},
  209. /* <8> */
  210. {0x00, 0x25, "8"},
  211. /* <9> */
  212. {0x00, 0x26, "9"},
  213. /* <0> */
  214. {0x00, 0x27, "0"},
  215. /* <LEFT-SHIFT><1> */
  216. {0x02, 0x1e, "!"},
  217. /* <RIGHT-SHIFT><2> */
  218. {0x20, 0x1f, "@"},
  219. /* <LEFT-SHIFT><3> */
  220. {0x02, 0x20, "#"},
  221. /* <RIGHT-SHIFT><4> */
  222. {0x20, 0x21, "$"},
  223. /* <LEFT-SHIFT><5> */
  224. {0x02, 0x22, "%"},
  225. /* <RIGHT-SHIFT><6> */
  226. {0x20, 0x23, "^"},
  227. /* <LEFT-SHIFT><7> */
  228. {0x02, 0x24, "&"},
  229. /* <RIGHT-SHIFT><8> */
  230. {0x20, 0x25, "*"},
  231. /* <LEFT-SHIFT><9> */
  232. {0x02, 0x26, "("},
  233. /* <RIGHT-SHIFT><0> */
  234. {0x20, 0x27, ")"},
  235. /* <ENTER> */
  236. {0x00, 0x28, "\r"},
  237. /* <ESCAPE> */
  238. {0x00, 0x29, "\x1b"},
  239. /* <BACKSPACE> */
  240. {0x00, 0x2a, "\x08"},
  241. /* <TAB> */
  242. {0x00, 0x2b, "\x09"},
  243. /* <SPACE> */
  244. {0x00, 0x2c, " "},
  245. /* <MINUS> */
  246. {0x00, 0x2d, "-"},
  247. /* <EQUAL> */
  248. {0x00, 0x2e, "="},
  249. /* <LEFT BRACE> */
  250. {0x00, 0x2f, "["},
  251. /* <RIGHT BRACE> */
  252. {0x00, 0x30, "]"},
  253. /* <BACKSLASH> */
  254. {0x00, 0x31, "\\"},
  255. /* <HASH-TILDE> */
  256. {0x00, 0x32, "#"},
  257. /* <SEMICOLON> */
  258. {0x00, 0x33, ";"},
  259. /* <APOSTROPHE> */
  260. {0x00, 0x34, "'"},
  261. /* <GRAVE> */
  262. {0x00, 0x35, "`"},
  263. /* <COMMA> */
  264. {0x00, 0x36, ","},
  265. /* <DOT> */
  266. {0x00, 0x37, "."},
  267. /* <SLASH> */
  268. {0x00, 0x38, "/"},
  269. /* <LEFT-SHIFT><ENTER> */
  270. {0x02, 0x28, "\r"},
  271. /* <RIGHT-SHIFT><ESCAPE> */
  272. {0x20, 0x29, "\x1b"},
  273. /* <LEFT-SHIFT><BACKSPACE> */
  274. {0x02, 0x2a, "\x08"},
  275. /* <RIGHT-SHIFT><TAB> */
  276. {0x20, 0x2b, "\x09"},
  277. /* <LEFT-SHIFT><SPACE> */
  278. {0x02, 0x2c, " "},
  279. /* <MINUS> */
  280. {0x20, 0x2d, "_"},
  281. /* <LEFT-SHIFT><EQUAL> */
  282. {0x02, 0x2e, "+"},
  283. /* <RIGHT-SHIFT><LEFT BRACE> */
  284. {0x20, 0x2f, "{"},
  285. /* <LEFT-SHIFT><RIGHT BRACE> */
  286. {0x02, 0x30, "}"},
  287. /* <RIGHT-SHIFT><BACKSLASH> */
  288. {0x20, 0x31, "|"},
  289. /* <LEFT-SHIFT><HASH-TILDE> */
  290. {0x02, 0x32, "~"},
  291. /* <RIGHT-SHIFT><SEMICOLON> */
  292. {0x20, 0x33, ":"},
  293. /* <LEFT-SHIFT><APOSTROPHE> */
  294. {0x02, 0x34, "\""},
  295. /* <RIGHT-SHIFT><GRAVE> */
  296. {0x20, 0x35, "~"},
  297. /* <LEFT-SHIFT><COMMA> */
  298. {0x02, 0x36, "<"},
  299. /* <RIGHT-SHIFT><DOT> */
  300. {0x20, 0x37, ">"},
  301. /* <LEFT-SHIFT><SLASH> */
  302. {0x02, 0x38, "?"},
  303. #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
  304. /* <F1> */
  305. {0x00, 0x3a, "\x1bOP"},
  306. /* <F2> */
  307. {0x00, 0x3b, "\x1bOQ"},
  308. /* <F3> */
  309. {0x00, 0x3c, "\x1bOR"},
  310. /* <F4> */
  311. {0x00, 0x3d, "\x1bOS"},
  312. /* <F5> */
  313. {0x00, 0x3e, "\x1b[15~"},
  314. /* <F6> */
  315. {0x00, 0x3f, "\x1b[17~"},
  316. /* <F7> */
  317. {0x00, 0x40, "\x1b[18~"},
  318. /* <F8> */
  319. {0x00, 0x41, "\x1b[19~"},
  320. /* <F9> */
  321. {0x00, 0x42, "\x1b[20~"},
  322. /* <F10> */
  323. {0x00, 0x43, "\x1b[21~"},
  324. /* <F11> */
  325. {0x00, 0x44, "\x1b[23~"},
  326. /* <F12> */
  327. {0x00, 0x45, "\x1b[24~"},
  328. /* <INSERT> */
  329. {0x00, 0x49, "\x1b[2~"},
  330. /* <HOME> */
  331. {0x00, 0x4a, "\x1b[H"},
  332. /* <PAGE UP> */
  333. {0x00, 0x4b, "\x1b[5~"},
  334. /* <DELETE> */
  335. {0x00, 0x4c, "\x1b[3~"},
  336. /* <END> */
  337. {0x00, 0x4d, "\x1b[F"},
  338. /* <PAGE DOWN> */
  339. {0x00, 0x4e, "\x1b[6~"},
  340. /* <RIGHT> */
  341. {0x00, 0x4f, "\x1b[C"},
  342. /* <LEFT> */
  343. {0x00, 0x50, "\x1b[D"},
  344. /* <DOWN> */
  345. {0x00, 0x51, "\x1b[B"},
  346. /* <UP> */
  347. {0x00, 0x52, "\x1b[A"},
  348. #endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
  349. /* End of list */
  350. {0x00, 0x00, "\0"}
  351. };
  352. state_set_skip_delays(true);
  353. ut_assertok(usb_init());
  354. /* Initially there should be no characters */
  355. ut_asserteq(0, tstc());
  356. ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb@3",
  357. &dev));
  358. /*
  359. * Add scan codes to the USB keyboard buffer. They should appear as
  360. * corresponding characters and escape sequences in stdin.
  361. */
  362. for (pos = kbd_test_data; pos->scancode; ++pos) {
  363. const char *c;
  364. char scancodes[USB_KBD_BOOT_REPORT_SIZE] = {0};
  365. scancodes[0] = pos->modifiers;
  366. scancodes[2] = pos->scancode;
  367. ut_assertok(sandbox_usb_keyb_add_string(dev, scancodes));
  368. for (c = pos->result; *c; ++c) {
  369. ut_asserteq(1, tstc());
  370. ut_asserteq(*c, getc());
  371. }
  372. ut_asserteq(0, tstc());
  373. }
  374. ut_assertok(usb_stop());
  375. return 0;
  376. }
  377. DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);