usb_kbd.c 17 KB

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