usb_kbd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Denis Peter, MPL AG Switzerland
  5. *
  6. * Part of this source has been derived from the Linux USB
  7. * project.
  8. */
  9. #include <common.h>
  10. #include <console.h>
  11. #include <dm.h>
  12. #include <env.h>
  13. #include <errno.h>
  14. #include <log.h>
  15. #include <malloc.h>
  16. #include <memalign.h>
  17. #include <stdio_dev.h>
  18. #include <watchdog.h>
  19. #include <asm/byteorder.h>
  20. #ifdef CONFIG_SANDBOX
  21. #include <asm/state.h>
  22. #endif
  23. #include <usb.h>
  24. /*
  25. * If overwrite_console returns 1, the stdin, stderr and stdout
  26. * are switched to the serial port, else the settings in the
  27. * environment are used
  28. */
  29. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  30. extern int overwrite_console(void);
  31. #else
  32. int overwrite_console(void)
  33. {
  34. return 0;
  35. }
  36. #endif
  37. /* Keyboard sampling rate */
  38. #define REPEAT_RATE 40 /* 40msec -> 25cps */
  39. #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
  40. #define NUM_LOCK 0x53
  41. #define CAPS_LOCK 0x39
  42. #define SCROLL_LOCK 0x47
  43. /* Modifier bits */
  44. #define LEFT_CNTR (1 << 0)
  45. #define LEFT_SHIFT (1 << 1)
  46. #define LEFT_ALT (1 << 2)
  47. #define LEFT_GUI (1 << 3)
  48. #define RIGHT_CNTR (1 << 4)
  49. #define RIGHT_SHIFT (1 << 5)
  50. #define RIGHT_ALT (1 << 6)
  51. #define RIGHT_GUI (1 << 7)
  52. /* Size of the keyboard buffer */
  53. #define USB_KBD_BUFFER_LEN 0x20
  54. /* Device name */
  55. #define DEVNAME "usbkbd"
  56. /* Keyboard maps */
  57. static const unsigned char usb_kbd_numkey[] = {
  58. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  59. '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
  60. '\\', '#', ';', '\'', '`', ',', '.', '/'
  61. };
  62. static const unsigned char usb_kbd_numkey_shifted[] = {
  63. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  64. '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
  65. '|', '~', ':', '"', '~', '<', '>', '?'
  66. };
  67. static const unsigned char usb_kbd_num_keypad[] = {
  68. '/', '*', '-', '+', '\r',
  69. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  70. '.', 0, 0, 0, '='
  71. };
  72. static const u8 usb_special_keys[] = {
  73. #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
  74. '2', 'H', '5', '3', 'F', '6', 'C', 'D', 'B', 'A'
  75. #else
  76. 'C', 'D', 'B', 'A'
  77. #endif
  78. };
  79. /*
  80. * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
  81. * order. See usb_kbd_setled() function!
  82. */
  83. #define USB_KBD_NUMLOCK (1 << 0)
  84. #define USB_KBD_CAPSLOCK (1 << 1)
  85. #define USB_KBD_SCROLLLOCK (1 << 2)
  86. #define USB_KBD_CTRL (1 << 3)
  87. #define USB_KBD_LEDMASK \
  88. (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
  89. struct usb_kbd_pdata {
  90. unsigned long intpipe;
  91. int intpktsize;
  92. int intinterval;
  93. unsigned long last_report;
  94. struct int_queue *intq;
  95. uint32_t repeat_delay;
  96. uint32_t usb_in_pointer;
  97. uint32_t usb_out_pointer;
  98. uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
  99. uint8_t *new;
  100. uint8_t old[USB_KBD_BOOT_REPORT_SIZE];
  101. uint8_t flags;
  102. };
  103. extern int __maybe_unused net_busy_flag;
  104. /* The period of time between two calls of usb_kbd_testc(). */
  105. static unsigned long kbd_testc_tms;
  106. /* Puts character in the queue and sets up the in and out pointer. */
  107. static void usb_kbd_put_queue(struct usb_kbd_pdata *data, u8 c)
  108. {
  109. if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
  110. /* Check for buffer full. */
  111. if (data->usb_out_pointer == 0)
  112. return;
  113. data->usb_in_pointer = 0;
  114. } else {
  115. /* Check for buffer full. */
  116. if (data->usb_in_pointer == data->usb_out_pointer - 1)
  117. return;
  118. data->usb_in_pointer++;
  119. }
  120. data->usb_kbd_buffer[data->usb_in_pointer] = c;
  121. }
  122. /*
  123. * Set the LEDs. Since this is used in the irq routine, the control job is
  124. * issued with a timeout of 0. This means, that the job is queued without
  125. * waiting for job completion.
  126. */
  127. static void usb_kbd_setled(struct usb_device *dev)
  128. {
  129. struct usb_interface *iface = &dev->config.if_desc[0];
  130. struct usb_kbd_pdata *data = dev->privptr;
  131. ALLOC_ALIGN_BUFFER(uint32_t, leds, 1, USB_DMA_MINALIGN);
  132. *leds = data->flags & USB_KBD_LEDMASK;
  133. usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  134. USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  135. 0x200, iface->desc.bInterfaceNumber, leds, 1, 0);
  136. }
  137. #define CAPITAL_MASK 0x20
  138. /* Translate the scancode in ASCII */
  139. static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
  140. unsigned char modifier, int pressed)
  141. {
  142. uint8_t keycode = 0;
  143. /* Key released */
  144. if (pressed == 0) {
  145. data->repeat_delay = 0;
  146. return 0;
  147. }
  148. if (pressed == 2) {
  149. data->repeat_delay++;
  150. if (data->repeat_delay < REPEAT_DELAY)
  151. return 0;
  152. data->repeat_delay = REPEAT_DELAY;
  153. }
  154. /* Alphanumeric values */
  155. if ((scancode > 3) && (scancode <= 0x1d)) {
  156. keycode = scancode - 4 + 'a';
  157. if (data->flags & USB_KBD_CAPSLOCK)
  158. keycode &= ~CAPITAL_MASK;
  159. if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
  160. /* Handle CAPSLock + Shift pressed simultaneously */
  161. if (keycode & CAPITAL_MASK)
  162. keycode &= ~CAPITAL_MASK;
  163. else
  164. keycode |= CAPITAL_MASK;
  165. }
  166. }
  167. if ((scancode > 0x1d) && (scancode < 0x39)) {
  168. /* Shift pressed */
  169. if (modifier & (LEFT_SHIFT | RIGHT_SHIFT))
  170. keycode = usb_kbd_numkey_shifted[scancode - 0x1e];
  171. else
  172. keycode = usb_kbd_numkey[scancode - 0x1e];
  173. }
  174. /* Numeric keypad */
  175. if ((scancode >= 0x54) && (scancode <= 0x67))
  176. keycode = usb_kbd_num_keypad[scancode - 0x54];
  177. if (data->flags & USB_KBD_CTRL)
  178. keycode = scancode - 0x3;
  179. if (pressed == 1) {
  180. if (scancode == NUM_LOCK) {
  181. data->flags ^= USB_KBD_NUMLOCK;
  182. return 1;
  183. }
  184. if (scancode == CAPS_LOCK) {
  185. data->flags ^= USB_KBD_CAPSLOCK;
  186. return 1;
  187. }
  188. if (scancode == SCROLL_LOCK) {
  189. data->flags ^= USB_KBD_SCROLLLOCK;
  190. return 1;
  191. }
  192. }
  193. /* Report keycode if any */
  194. if (keycode) {
  195. debug("%c", keycode);
  196. usb_kbd_put_queue(data, keycode);
  197. return 0;
  198. }
  199. #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
  200. if (scancode < 0x3a || scancode > 0x52 ||
  201. scancode == 0x46 || scancode == 0x47)
  202. return 1;
  203. usb_kbd_put_queue(data, 0x1b);
  204. if (scancode < 0x3e) {
  205. /* F1 - F4 */
  206. usb_kbd_put_queue(data, 0x4f);
  207. usb_kbd_put_queue(data, scancode - 0x3a + 'P');
  208. return 0;
  209. }
  210. usb_kbd_put_queue(data, '[');
  211. if (scancode < 0x42) {
  212. /* F5 - F8 */
  213. usb_kbd_put_queue(data, '1');
  214. if (scancode == 0x3e)
  215. --scancode;
  216. keycode = scancode - 0x3f + '7';
  217. } else if (scancode < 0x49) {
  218. /* F9 - F12 */
  219. usb_kbd_put_queue(data, '2');
  220. if (scancode > 0x43)
  221. ++scancode;
  222. keycode = scancode - 0x42 + '0';
  223. } else {
  224. /*
  225. * INSERT, HOME, PAGE UP, DELETE, END, PAGE DOWN,
  226. * RIGHT, LEFT, DOWN, UP
  227. */
  228. keycode = usb_special_keys[scancode - 0x49];
  229. }
  230. usb_kbd_put_queue(data, keycode);
  231. if (scancode < 0x4f && scancode != 0x4a && scancode != 0x4d)
  232. usb_kbd_put_queue(data, '~');
  233. return 0;
  234. #else
  235. /* Left, Right, Up, Down */
  236. if (scancode > 0x4e && scancode < 0x53) {
  237. usb_kbd_put_queue(data, 0x1b);
  238. usb_kbd_put_queue(data, '[');
  239. usb_kbd_put_queue(data, usb_special_keys[scancode - 0x4f]);
  240. return 0;
  241. }
  242. return 1;
  243. #endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
  244. }
  245. static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
  246. {
  247. uint32_t res = 0;
  248. struct usb_kbd_pdata *data = dev->privptr;
  249. uint8_t *new;
  250. uint8_t *old;
  251. if (up) {
  252. new = data->old;
  253. old = data->new;
  254. } else {
  255. new = data->new;
  256. old = data->old;
  257. }
  258. if ((old[i] > 3) &&
  259. (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) ==
  260. new + USB_KBD_BOOT_REPORT_SIZE)) {
  261. res |= usb_kbd_translate(data, old[i], data->new[0], up);
  262. }
  263. return res;
  264. }
  265. /* Interrupt service routine */
  266. static int usb_kbd_irq_worker(struct usb_device *dev)
  267. {
  268. struct usb_kbd_pdata *data = dev->privptr;
  269. int i, res = 0;
  270. /* No combo key pressed */
  271. if (data->new[0] == 0x00)
  272. data->flags &= ~USB_KBD_CTRL;
  273. /* Left or Right Ctrl pressed */
  274. else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
  275. data->flags |= USB_KBD_CTRL;
  276. for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) {
  277. res |= usb_kbd_service_key(dev, i, 0);
  278. res |= usb_kbd_service_key(dev, i, 1);
  279. }
  280. /* Key is still pressed */
  281. if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
  282. res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
  283. if (res == 1)
  284. usb_kbd_setled(dev);
  285. memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE);
  286. return 1;
  287. }
  288. /* Keyboard interrupt handler */
  289. static int usb_kbd_irq(struct usb_device *dev)
  290. {
  291. if ((dev->irq_status != 0) ||
  292. (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) {
  293. debug("USB KBD: Error %lX, len %d\n",
  294. dev->irq_status, dev->irq_act_len);
  295. return 1;
  296. }
  297. return usb_kbd_irq_worker(dev);
  298. }
  299. /* Interrupt polling */
  300. static inline void usb_kbd_poll_for_event(struct usb_device *dev)
  301. {
  302. #if defined(CONFIG_SYS_USB_EVENT_POLL)
  303. struct usb_kbd_pdata *data = dev->privptr;
  304. /* Submit an interrupt transfer request */
  305. if (usb_int_msg(dev, data->intpipe, &data->new[0],
  306. data->intpktsize, data->intinterval, true) >= 0)
  307. usb_kbd_irq_worker(dev);
  308. #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) || \
  309. defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
  310. #if defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
  311. struct usb_interface *iface;
  312. struct usb_kbd_pdata *data = dev->privptr;
  313. iface = &dev->config.if_desc[0];
  314. usb_get_report(dev, iface->desc.bInterfaceNumber,
  315. 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE);
  316. if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE)) {
  317. usb_kbd_irq_worker(dev);
  318. #else
  319. struct usb_kbd_pdata *data = dev->privptr;
  320. if (poll_int_queue(dev, data->intq)) {
  321. usb_kbd_irq_worker(dev);
  322. /* We've consumed all queued int packets, create new */
  323. destroy_int_queue(dev, data->intq);
  324. data->intq = create_int_queue(dev, data->intpipe, 1,
  325. USB_KBD_BOOT_REPORT_SIZE, data->new,
  326. data->intinterval);
  327. #endif
  328. data->last_report = get_timer(0);
  329. /* Repeat last usb hid report every REPEAT_RATE ms for keyrepeat */
  330. } else if (data->last_report != -1 &&
  331. get_timer(data->last_report) > REPEAT_RATE) {
  332. usb_kbd_irq_worker(dev);
  333. data->last_report = get_timer(0);
  334. }
  335. #endif
  336. }
  337. /* test if a character is in the queue */
  338. static int usb_kbd_testc(struct stdio_dev *sdev)
  339. {
  340. struct stdio_dev *dev;
  341. struct usb_device *usb_kbd_dev;
  342. struct usb_kbd_pdata *data;
  343. /*
  344. * Polling the keyboard for an event can take dozens of milliseconds.
  345. * Add a delay between polls to avoid blocking activity which polls
  346. * rapidly, like the UEFI console timer.
  347. */
  348. unsigned long poll_delay = CONFIG_SYS_HZ / 50;
  349. #ifdef CONFIG_CMD_NET
  350. /*
  351. * If net_busy_flag is 1, NET transfer is running,
  352. * then we check key-pressed every second (first check may be
  353. * less than 1 second) to improve TFTP booting performance.
  354. */
  355. if (net_busy_flag)
  356. poll_delay = CONFIG_SYS_HZ;
  357. #endif
  358. #ifdef CONFIG_SANDBOX
  359. /*
  360. * Skip delaying polls if a test requests it.
  361. */
  362. if (state_get_skip_delays())
  363. poll_delay = 0;
  364. #endif
  365. dev = stdio_get_by_name(sdev->name);
  366. usb_kbd_dev = (struct usb_device *)dev->priv;
  367. data = usb_kbd_dev->privptr;
  368. if (get_timer(kbd_testc_tms) >= poll_delay) {
  369. usb_kbd_poll_for_event(usb_kbd_dev);
  370. kbd_testc_tms = get_timer(0);
  371. }
  372. return !(data->usb_in_pointer == data->usb_out_pointer);
  373. }
  374. /* gets the character from the queue */
  375. static int usb_kbd_getc(struct stdio_dev *sdev)
  376. {
  377. struct stdio_dev *dev;
  378. struct usb_device *usb_kbd_dev;
  379. struct usb_kbd_pdata *data;
  380. dev = stdio_get_by_name(sdev->name);
  381. usb_kbd_dev = (struct usb_device *)dev->priv;
  382. data = usb_kbd_dev->privptr;
  383. while (data->usb_in_pointer == data->usb_out_pointer) {
  384. WATCHDOG_RESET();
  385. usb_kbd_poll_for_event(usb_kbd_dev);
  386. }
  387. if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
  388. data->usb_out_pointer = 0;
  389. else
  390. data->usb_out_pointer++;
  391. return data->usb_kbd_buffer[data->usb_out_pointer];
  392. }
  393. /* probes the USB device dev for keyboard type. */
  394. static int usb_kbd_probe_dev(struct usb_device *dev, unsigned int ifnum)
  395. {
  396. struct usb_interface *iface;
  397. struct usb_endpoint_descriptor *ep;
  398. struct usb_kbd_pdata *data;
  399. int epNum;
  400. if (dev->descriptor.bNumConfigurations != 1)
  401. return 0;
  402. iface = &dev->config.if_desc[ifnum];
  403. if (iface->desc.bInterfaceClass != USB_CLASS_HID)
  404. return 0;
  405. if (iface->desc.bInterfaceSubClass != USB_SUB_HID_BOOT)
  406. return 0;
  407. if (iface->desc.bInterfaceProtocol != USB_PROT_HID_KEYBOARD)
  408. return 0;
  409. for (epNum = 0; epNum < iface->desc.bNumEndpoints; epNum++) {
  410. ep = &iface->ep_desc[epNum];
  411. /* Check if endpoint is interrupt IN endpoint */
  412. if ((ep->bmAttributes & 3) != 3)
  413. continue;
  414. if (ep->bEndpointAddress & 0x80)
  415. break;
  416. }
  417. if (epNum == iface->desc.bNumEndpoints)
  418. return 0;
  419. debug("USB KBD: found interrupt EP: 0x%x\n", ep->bEndpointAddress);
  420. data = malloc(sizeof(struct usb_kbd_pdata));
  421. if (!data) {
  422. printf("USB KBD: Error allocating private data\n");
  423. return 0;
  424. }
  425. /* Clear private data */
  426. memset(data, 0, sizeof(struct usb_kbd_pdata));
  427. /* allocate input buffer aligned and sized to USB DMA alignment */
  428. data->new = memalign(USB_DMA_MINALIGN,
  429. roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
  430. /* Insert private data into USB device structure */
  431. dev->privptr = data;
  432. /* Set IRQ handler */
  433. dev->irq_handle = usb_kbd_irq;
  434. data->intpipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  435. data->intpktsize = min(usb_maxpacket(dev, data->intpipe),
  436. USB_KBD_BOOT_REPORT_SIZE);
  437. data->intinterval = ep->bInterval;
  438. data->last_report = -1;
  439. /* We found a USB Keyboard, install it. */
  440. debug("USB KBD: set boot protocol\n");
  441. usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
  442. #if !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) && \
  443. !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
  444. debug("USB KBD: set idle interval...\n");
  445. usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE / 4, 0);
  446. #else
  447. debug("USB KBD: set idle interval=0...\n");
  448. usb_set_idle(dev, iface->desc.bInterfaceNumber, 0, 0);
  449. #endif
  450. debug("USB KBD: enable interrupt pipe...\n");
  451. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  452. data->intq = create_int_queue(dev, data->intpipe, 1,
  453. USB_KBD_BOOT_REPORT_SIZE, data->new,
  454. data->intinterval);
  455. if (!data->intq) {
  456. #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
  457. if (usb_get_report(dev, iface->desc.bInterfaceNumber,
  458. 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE) < 0) {
  459. #else
  460. if (usb_int_msg(dev, data->intpipe, data->new, data->intpktsize,
  461. data->intinterval, false) < 0) {
  462. #endif
  463. printf("Failed to get keyboard state from device %04x:%04x\n",
  464. dev->descriptor.idVendor, dev->descriptor.idProduct);
  465. /* Abort, we don't want to use that non-functional keyboard. */
  466. return 0;
  467. }
  468. /* Success. */
  469. return 1;
  470. }
  471. static int probe_usb_keyboard(struct usb_device *dev)
  472. {
  473. char *stdinname;
  474. struct stdio_dev usb_kbd_dev;
  475. int error;
  476. /* Try probing the keyboard */
  477. if (usb_kbd_probe_dev(dev, 0) != 1)
  478. return -ENOENT;
  479. /* Register the keyboard */
  480. debug("USB KBD: register.\n");
  481. memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
  482. strcpy(usb_kbd_dev.name, DEVNAME);
  483. usb_kbd_dev.flags = DEV_FLAGS_INPUT;
  484. usb_kbd_dev.getc = usb_kbd_getc;
  485. usb_kbd_dev.tstc = usb_kbd_testc;
  486. usb_kbd_dev.priv = (void *)dev;
  487. error = stdio_register(&usb_kbd_dev);
  488. if (error)
  489. return error;
  490. stdinname = env_get("stdin");
  491. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  492. error = iomux_doenv(stdin, stdinname);
  493. if (error)
  494. return error;
  495. #else
  496. /* Check if this is the standard input device. */
  497. if (strcmp(stdinname, DEVNAME))
  498. return 1;
  499. /* Reassign the console */
  500. if (overwrite_console())
  501. return 1;
  502. error = console_assign(stdin, DEVNAME);
  503. if (error)
  504. return error;
  505. #endif
  506. return 0;
  507. }
  508. #if !CONFIG_IS_ENABLED(DM_USB)
  509. /* Search for keyboard and register it if found. */
  510. int drv_usb_kbd_init(void)
  511. {
  512. int error, i;
  513. debug("%s: Probing for keyboard\n", __func__);
  514. /* Scan all USB Devices */
  515. for (i = 0; i < USB_MAX_DEVICE; i++) {
  516. struct usb_device *dev;
  517. /* Get USB device. */
  518. dev = usb_get_dev_index(i);
  519. if (!dev)
  520. break;
  521. if (dev->devnum == -1)
  522. continue;
  523. error = probe_usb_keyboard(dev);
  524. if (!error)
  525. return 1;
  526. if (error && error != -ENOENT)
  527. return error;
  528. }
  529. /* No USB Keyboard found */
  530. return -1;
  531. }
  532. /* Deregister the keyboard. */
  533. int usb_kbd_deregister(int force)
  534. {
  535. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  536. struct stdio_dev *dev;
  537. struct usb_device *usb_kbd_dev;
  538. struct usb_kbd_pdata *data;
  539. dev = stdio_get_by_name(DEVNAME);
  540. if (dev) {
  541. usb_kbd_dev = (struct usb_device *)dev->priv;
  542. data = usb_kbd_dev->privptr;
  543. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  544. if (iomux_replace_device(stdin, DEVNAME, force ? "nulldev" : ""))
  545. return 1;
  546. #endif
  547. if (stdio_deregister_dev(dev, force) != 0)
  548. return 1;
  549. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  550. destroy_int_queue(usb_kbd_dev, data->intq);
  551. #endif
  552. free(data->new);
  553. free(data);
  554. }
  555. return 0;
  556. #else
  557. return 1;
  558. #endif
  559. }
  560. #endif
  561. #if CONFIG_IS_ENABLED(DM_USB)
  562. static int usb_kbd_probe(struct udevice *dev)
  563. {
  564. struct usb_device *udev = dev_get_parent_priv(dev);
  565. return probe_usb_keyboard(udev);
  566. }
  567. static int usb_kbd_remove(struct udevice *dev)
  568. {
  569. struct usb_device *udev = dev_get_parent_priv(dev);
  570. struct usb_kbd_pdata *data;
  571. struct stdio_dev *sdev;
  572. int ret;
  573. sdev = stdio_get_by_name(DEVNAME);
  574. if (!sdev) {
  575. ret = -ENXIO;
  576. goto err;
  577. }
  578. data = udev->privptr;
  579. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  580. if (iomux_replace_device(stdin, DEVNAME, "nulldev")) {
  581. ret = -ENOLINK;
  582. goto err;
  583. }
  584. #endif
  585. if (stdio_deregister_dev(sdev, true)) {
  586. ret = -EPERM;
  587. goto err;
  588. }
  589. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  590. destroy_int_queue(udev, data->intq);
  591. #endif
  592. free(data->new);
  593. free(data);
  594. return 0;
  595. err:
  596. printf("%s: warning, ret=%d", __func__, ret);
  597. return ret;
  598. }
  599. static const struct udevice_id usb_kbd_ids[] = {
  600. { .compatible = "usb-keyboard" },
  601. { }
  602. };
  603. U_BOOT_DRIVER(usb_kbd) = {
  604. .name = "usb_kbd",
  605. .id = UCLASS_KEYBOARD,
  606. .of_match = usb_kbd_ids,
  607. .probe = usb_kbd_probe,
  608. .remove = usb_kbd_remove,
  609. };
  610. static const struct usb_device_id kbd_id_table[] = {
  611. {
  612. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
  613. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  614. USB_DEVICE_ID_MATCH_INT_PROTOCOL,
  615. .bInterfaceClass = USB_CLASS_HID,
  616. .bInterfaceSubClass = USB_SUB_HID_BOOT,
  617. .bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
  618. },
  619. { } /* Terminating entry */
  620. };
  621. U_BOOT_USB_DEVICE(usb_kbd, kbd_id_table);
  622. #endif