usb_kbd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 <errno.h>
  13. #include <malloc.h>
  14. #include <memalign.h>
  15. #include <stdio_dev.h>
  16. #include <asm/byteorder.h>
  17. #include <usb.h>
  18. /*
  19. * If overwrite_console returns 1, the stdin, stderr and stdout
  20. * are switched to the serial port, else the settings in the
  21. * environment are used
  22. */
  23. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  24. extern int overwrite_console(void);
  25. #else
  26. int overwrite_console(void)
  27. {
  28. return 0;
  29. }
  30. #endif
  31. /* Keyboard sampling rate */
  32. #define REPEAT_RATE 40 /* 40msec -> 25cps */
  33. #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
  34. #define NUM_LOCK 0x53
  35. #define CAPS_LOCK 0x39
  36. #define SCROLL_LOCK 0x47
  37. /* Modifier bits */
  38. #define LEFT_CNTR (1 << 0)
  39. #define LEFT_SHIFT (1 << 1)
  40. #define LEFT_ALT (1 << 2)
  41. #define LEFT_GUI (1 << 3)
  42. #define RIGHT_CNTR (1 << 4)
  43. #define RIGHT_SHIFT (1 << 5)
  44. #define RIGHT_ALT (1 << 6)
  45. #define RIGHT_GUI (1 << 7)
  46. /* Size of the keyboard buffer */
  47. #define USB_KBD_BUFFER_LEN 0x20
  48. /* Device name */
  49. #define DEVNAME "usbkbd"
  50. /* Keyboard maps */
  51. static const unsigned char usb_kbd_numkey[] = {
  52. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  53. '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
  54. '\\', '#', ';', '\'', '`', ',', '.', '/'
  55. };
  56. static const unsigned char usb_kbd_numkey_shifted[] = {
  57. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  58. '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
  59. '|', '~', ':', '"', '~', '<', '>', '?'
  60. };
  61. static const unsigned char usb_kbd_num_keypad[] = {
  62. '/', '*', '-', '+', '\r',
  63. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  64. '.', 0, 0, 0, '='
  65. };
  66. /*
  67. * map arrow keys to ^F/^B ^N/^P, can't really use the proper
  68. * ANSI sequence for arrow keys because the queuing code breaks
  69. * when a single keypress expands to 3 queue elements
  70. */
  71. static const unsigned char usb_kbd_arrow[] = {
  72. 0x6, 0x2, 0xe, 0x10
  73. };
  74. /*
  75. * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
  76. * order. See usb_kbd_setled() function!
  77. */
  78. #define USB_KBD_NUMLOCK (1 << 0)
  79. #define USB_KBD_CAPSLOCK (1 << 1)
  80. #define USB_KBD_SCROLLLOCK (1 << 2)
  81. #define USB_KBD_CTRL (1 << 3)
  82. #define USB_KBD_LEDMASK \
  83. (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
  84. /*
  85. * USB Keyboard reports are 8 bytes in boot protocol.
  86. * Appendix B of HID Device Class Definition 1.11
  87. */
  88. #define USB_KBD_BOOT_REPORT_SIZE 8
  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 __maybe_unused 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, char 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. /* Arrow keys */
  175. if ((scancode >= 0x4f) && (scancode <= 0x52))
  176. keycode = usb_kbd_arrow[scancode - 0x4f];
  177. /* Numeric keypad */
  178. if ((scancode >= 0x54) && (scancode <= 0x67))
  179. keycode = usb_kbd_num_keypad[scancode - 0x54];
  180. if (data->flags & USB_KBD_CTRL)
  181. keycode = scancode - 0x3;
  182. if (pressed == 1) {
  183. if (scancode == NUM_LOCK) {
  184. data->flags ^= USB_KBD_NUMLOCK;
  185. return 1;
  186. }
  187. if (scancode == CAPS_LOCK) {
  188. data->flags ^= USB_KBD_CAPSLOCK;
  189. return 1;
  190. }
  191. if (scancode == SCROLL_LOCK) {
  192. data->flags ^= USB_KBD_SCROLLLOCK;
  193. return 1;
  194. }
  195. }
  196. /* Report keycode if any */
  197. if (keycode) {
  198. debug("%c", keycode);
  199. usb_kbd_put_queue(data, keycode);
  200. }
  201. return 0;
  202. }
  203. static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
  204. {
  205. uint32_t res = 0;
  206. struct usb_kbd_pdata *data = dev->privptr;
  207. uint8_t *new;
  208. uint8_t *old;
  209. if (up) {
  210. new = data->old;
  211. old = data->new;
  212. } else {
  213. new = data->new;
  214. old = data->old;
  215. }
  216. if ((old[i] > 3) &&
  217. (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) ==
  218. new + USB_KBD_BOOT_REPORT_SIZE)) {
  219. res |= usb_kbd_translate(data, old[i], data->new[0], up);
  220. }
  221. return res;
  222. }
  223. /* Interrupt service routine */
  224. static int usb_kbd_irq_worker(struct usb_device *dev)
  225. {
  226. struct usb_kbd_pdata *data = dev->privptr;
  227. int i, res = 0;
  228. /* No combo key pressed */
  229. if (data->new[0] == 0x00)
  230. data->flags &= ~USB_KBD_CTRL;
  231. /* Left or Right Ctrl pressed */
  232. else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
  233. data->flags |= USB_KBD_CTRL;
  234. for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) {
  235. res |= usb_kbd_service_key(dev, i, 0);
  236. res |= usb_kbd_service_key(dev, i, 1);
  237. }
  238. /* Key is still pressed */
  239. if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
  240. res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
  241. if (res == 1)
  242. usb_kbd_setled(dev);
  243. memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE);
  244. return 1;
  245. }
  246. /* Keyboard interrupt handler */
  247. static int usb_kbd_irq(struct usb_device *dev)
  248. {
  249. if ((dev->irq_status != 0) ||
  250. (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) {
  251. debug("USB KBD: Error %lX, len %d\n",
  252. dev->irq_status, dev->irq_act_len);
  253. return 1;
  254. }
  255. return usb_kbd_irq_worker(dev);
  256. }
  257. /* Interrupt polling */
  258. static inline void usb_kbd_poll_for_event(struct usb_device *dev)
  259. {
  260. #if defined(CONFIG_SYS_USB_EVENT_POLL)
  261. struct usb_kbd_pdata *data = dev->privptr;
  262. /* Submit a interrupt transfer request */
  263. usb_submit_int_msg(dev, data->intpipe, &data->new[0], data->intpktsize,
  264. data->intinterval);
  265. usb_kbd_irq_worker(dev);
  266. #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) || \
  267. defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
  268. #if defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
  269. struct usb_interface *iface;
  270. struct usb_kbd_pdata *data = dev->privptr;
  271. iface = &dev->config.if_desc[0];
  272. usb_get_report(dev, iface->desc.bInterfaceNumber,
  273. 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE);
  274. if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE)) {
  275. usb_kbd_irq_worker(dev);
  276. #else
  277. struct usb_kbd_pdata *data = dev->privptr;
  278. if (poll_int_queue(dev, data->intq)) {
  279. usb_kbd_irq_worker(dev);
  280. /* We've consumed all queued int packets, create new */
  281. destroy_int_queue(dev, data->intq);
  282. data->intq = create_int_queue(dev, data->intpipe, 1,
  283. USB_KBD_BOOT_REPORT_SIZE, data->new,
  284. data->intinterval);
  285. #endif
  286. data->last_report = get_timer(0);
  287. /* Repeat last usb hid report every REPEAT_RATE ms for keyrepeat */
  288. } else if (data->last_report != -1 &&
  289. get_timer(data->last_report) > REPEAT_RATE) {
  290. usb_kbd_irq_worker(dev);
  291. data->last_report = get_timer(0);
  292. }
  293. #endif
  294. }
  295. /* test if a character is in the queue */
  296. static int usb_kbd_testc(struct stdio_dev *sdev)
  297. {
  298. struct stdio_dev *dev;
  299. struct usb_device *usb_kbd_dev;
  300. struct usb_kbd_pdata *data;
  301. #ifdef CONFIG_CMD_NET
  302. /*
  303. * If net_busy_flag is 1, NET transfer is running,
  304. * then we check key-pressed every second (first check may be
  305. * less than 1 second) to improve TFTP booting performance.
  306. */
  307. if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
  308. return 0;
  309. kbd_testc_tms = get_timer(0);
  310. #endif
  311. dev = stdio_get_by_name(DEVNAME);
  312. usb_kbd_dev = (struct usb_device *)dev->priv;
  313. data = usb_kbd_dev->privptr;
  314. usb_kbd_poll_for_event(usb_kbd_dev);
  315. return !(data->usb_in_pointer == data->usb_out_pointer);
  316. }
  317. /* gets the character from the queue */
  318. static int usb_kbd_getc(struct stdio_dev *sdev)
  319. {
  320. struct stdio_dev *dev;
  321. struct usb_device *usb_kbd_dev;
  322. struct usb_kbd_pdata *data;
  323. dev = stdio_get_by_name(DEVNAME);
  324. usb_kbd_dev = (struct usb_device *)dev->priv;
  325. data = usb_kbd_dev->privptr;
  326. while (data->usb_in_pointer == data->usb_out_pointer)
  327. usb_kbd_poll_for_event(usb_kbd_dev);
  328. if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
  329. data->usb_out_pointer = 0;
  330. else
  331. data->usb_out_pointer++;
  332. return data->usb_kbd_buffer[data->usb_out_pointer];
  333. }
  334. /* probes the USB device dev for keyboard type. */
  335. static int usb_kbd_probe_dev(struct usb_device *dev, unsigned int ifnum)
  336. {
  337. struct usb_interface *iface;
  338. struct usb_endpoint_descriptor *ep;
  339. struct usb_kbd_pdata *data;
  340. if (dev->descriptor.bNumConfigurations != 1)
  341. return 0;
  342. iface = &dev->config.if_desc[ifnum];
  343. if (iface->desc.bInterfaceClass != USB_CLASS_HID)
  344. return 0;
  345. if (iface->desc.bInterfaceSubClass != USB_SUB_HID_BOOT)
  346. return 0;
  347. if (iface->desc.bInterfaceProtocol != USB_PROT_HID_KEYBOARD)
  348. return 0;
  349. if (iface->desc.bNumEndpoints != 1)
  350. return 0;
  351. ep = &iface->ep_desc[0];
  352. /* Check if endpoint 1 is interrupt endpoint */
  353. if (!(ep->bEndpointAddress & 0x80))
  354. return 0;
  355. if ((ep->bmAttributes & 3) != 3)
  356. return 0;
  357. debug("USB KBD: found set protocol...\n");
  358. data = malloc(sizeof(struct usb_kbd_pdata));
  359. if (!data) {
  360. printf("USB KBD: Error allocating private data\n");
  361. return 0;
  362. }
  363. /* Clear private data */
  364. memset(data, 0, sizeof(struct usb_kbd_pdata));
  365. /* allocate input buffer aligned and sized to USB DMA alignment */
  366. data->new = memalign(USB_DMA_MINALIGN,
  367. roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
  368. /* Insert private data into USB device structure */
  369. dev->privptr = data;
  370. /* Set IRQ handler */
  371. dev->irq_handle = usb_kbd_irq;
  372. data->intpipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  373. data->intpktsize = min(usb_maxpacket(dev, data->intpipe),
  374. USB_KBD_BOOT_REPORT_SIZE);
  375. data->intinterval = ep->bInterval;
  376. data->last_report = -1;
  377. /* We found a USB Keyboard, install it. */
  378. usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
  379. debug("USB KBD: found set idle...\n");
  380. #if !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) && \
  381. !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
  382. usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE / 4, 0);
  383. #else
  384. usb_set_idle(dev, iface->desc.bInterfaceNumber, 0, 0);
  385. #endif
  386. debug("USB KBD: enable interrupt pipe...\n");
  387. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  388. data->intq = create_int_queue(dev, data->intpipe, 1,
  389. USB_KBD_BOOT_REPORT_SIZE, data->new,
  390. data->intinterval);
  391. if (!data->intq) {
  392. #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
  393. if (usb_get_report(dev, iface->desc.bInterfaceNumber,
  394. 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE) < 0) {
  395. #else
  396. if (usb_submit_int_msg(dev, data->intpipe, data->new, data->intpktsize,
  397. data->intinterval) < 0) {
  398. #endif
  399. printf("Failed to get keyboard state from device %04x:%04x\n",
  400. dev->descriptor.idVendor, dev->descriptor.idProduct);
  401. /* Abort, we don't want to use that non-functional keyboard. */
  402. return 0;
  403. }
  404. /* Success. */
  405. return 1;
  406. }
  407. static int probe_usb_keyboard(struct usb_device *dev)
  408. {
  409. char *stdinname;
  410. struct stdio_dev usb_kbd_dev;
  411. int error;
  412. /* Try probing the keyboard */
  413. if (usb_kbd_probe_dev(dev, 0) != 1)
  414. return -ENOENT;
  415. /* Register the keyboard */
  416. debug("USB KBD: register.\n");
  417. memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
  418. strcpy(usb_kbd_dev.name, DEVNAME);
  419. usb_kbd_dev.flags = DEV_FLAGS_INPUT;
  420. usb_kbd_dev.getc = usb_kbd_getc;
  421. usb_kbd_dev.tstc = usb_kbd_testc;
  422. usb_kbd_dev.priv = (void *)dev;
  423. error = stdio_register(&usb_kbd_dev);
  424. if (error)
  425. return error;
  426. stdinname = env_get("stdin");
  427. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  428. error = iomux_doenv(stdin, stdinname);
  429. if (error)
  430. return error;
  431. #else
  432. /* Check if this is the standard input device. */
  433. if (strcmp(stdinname, DEVNAME))
  434. return 1;
  435. /* Reassign the console */
  436. if (overwrite_console())
  437. return 1;
  438. error = console_assign(stdin, DEVNAME);
  439. if (error)
  440. return error;
  441. #endif
  442. return 0;
  443. }
  444. #ifndef CONFIG_DM_USB
  445. /* Search for keyboard and register it if found. */
  446. int drv_usb_kbd_init(void)
  447. {
  448. int error, i;
  449. debug("%s: Probing for keyboard\n", __func__);
  450. /* Scan all USB Devices */
  451. for (i = 0; i < USB_MAX_DEVICE; i++) {
  452. struct usb_device *dev;
  453. /* Get USB device. */
  454. dev = usb_get_dev_index(i);
  455. if (!dev)
  456. break;
  457. if (dev->devnum == -1)
  458. continue;
  459. error = probe_usb_keyboard(dev);
  460. if (!error)
  461. return 1;
  462. if (error && error != -ENOENT)
  463. return error;
  464. }
  465. /* No USB Keyboard found */
  466. return -1;
  467. }
  468. /* Deregister the keyboard. */
  469. int usb_kbd_deregister(int force)
  470. {
  471. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  472. struct stdio_dev *dev;
  473. struct usb_device *usb_kbd_dev;
  474. struct usb_kbd_pdata *data;
  475. dev = stdio_get_by_name(DEVNAME);
  476. if (dev) {
  477. usb_kbd_dev = (struct usb_device *)dev->priv;
  478. data = usb_kbd_dev->privptr;
  479. if (stdio_deregister_dev(dev, force) != 0)
  480. return 1;
  481. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  482. if (iomux_doenv(stdin, env_get("stdin")) != 0)
  483. return 1;
  484. #endif
  485. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  486. destroy_int_queue(usb_kbd_dev, data->intq);
  487. #endif
  488. free(data->new);
  489. free(data);
  490. }
  491. return 0;
  492. #else
  493. return 1;
  494. #endif
  495. }
  496. #endif
  497. #ifdef CONFIG_DM_USB
  498. static int usb_kbd_probe(struct udevice *dev)
  499. {
  500. struct usb_device *udev = dev_get_parent_priv(dev);
  501. return probe_usb_keyboard(udev);
  502. }
  503. static int usb_kbd_remove(struct udevice *dev)
  504. {
  505. struct usb_device *udev = dev_get_parent_priv(dev);
  506. struct usb_kbd_pdata *data;
  507. struct stdio_dev *sdev;
  508. int ret;
  509. sdev = stdio_get_by_name(DEVNAME);
  510. if (!sdev) {
  511. ret = -ENXIO;
  512. goto err;
  513. }
  514. data = udev->privptr;
  515. if (stdio_deregister_dev(sdev, true)) {
  516. ret = -EPERM;
  517. goto err;
  518. }
  519. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  520. if (iomux_doenv(stdin, env_get("stdin"))) {
  521. ret = -ENOLINK;
  522. goto err;
  523. }
  524. #endif
  525. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  526. destroy_int_queue(udev, data->intq);
  527. #endif
  528. free(data->new);
  529. free(data);
  530. return 0;
  531. err:
  532. printf("%s: warning, ret=%d", __func__, ret);
  533. return ret;
  534. }
  535. static const struct udevice_id usb_kbd_ids[] = {
  536. { .compatible = "usb-keyboard" },
  537. { }
  538. };
  539. U_BOOT_DRIVER(usb_kbd) = {
  540. .name = "usb_kbd",
  541. .id = UCLASS_KEYBOARD,
  542. .of_match = usb_kbd_ids,
  543. .probe = usb_kbd_probe,
  544. .remove = usb_kbd_remove,
  545. };
  546. static const struct usb_device_id kbd_id_table[] = {
  547. {
  548. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
  549. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  550. USB_DEVICE_ID_MATCH_INT_PROTOCOL,
  551. .bInterfaceClass = USB_CLASS_HID,
  552. .bInterfaceSubClass = USB_SUB_HID_BOOT,
  553. .bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
  554. },
  555. { } /* Terminating entry */
  556. };
  557. U_BOOT_USB_DEVICE(usb_kbd, kbd_id_table);
  558. #endif