usb_kbd.c 19 KB

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