usb_kbd.c 17 KB

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