usb_kbd.c 16 KB

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