usb_kbd.c 17 KB

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