usb_kbd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. if (dev->descriptor.bNumConfigurations != 1)
  382. return 0;
  383. iface = &dev->config.if_desc[ifnum];
  384. if (iface->desc.bInterfaceClass != USB_CLASS_HID)
  385. return 0;
  386. if (iface->desc.bInterfaceSubClass != USB_SUB_HID_BOOT)
  387. return 0;
  388. if (iface->desc.bInterfaceProtocol != USB_PROT_HID_KEYBOARD)
  389. return 0;
  390. if (iface->desc.bNumEndpoints != 1)
  391. return 0;
  392. ep = &iface->ep_desc[0];
  393. /* Check if endpoint 1 is interrupt endpoint */
  394. if (!(ep->bEndpointAddress & 0x80))
  395. return 0;
  396. if ((ep->bmAttributes & 3) != 3)
  397. return 0;
  398. debug("USB KBD: found set protocol...\n");
  399. data = malloc(sizeof(struct usb_kbd_pdata));
  400. if (!data) {
  401. printf("USB KBD: Error allocating private data\n");
  402. return 0;
  403. }
  404. /* Clear private data */
  405. memset(data, 0, sizeof(struct usb_kbd_pdata));
  406. /* allocate input buffer aligned and sized to USB DMA alignment */
  407. data->new = memalign(USB_DMA_MINALIGN,
  408. roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
  409. /* Insert private data into USB device structure */
  410. dev->privptr = data;
  411. /* Set IRQ handler */
  412. dev->irq_handle = usb_kbd_irq;
  413. data->intpipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  414. data->intpktsize = min(usb_maxpacket(dev, data->intpipe),
  415. USB_KBD_BOOT_REPORT_SIZE);
  416. data->intinterval = ep->bInterval;
  417. data->last_report = -1;
  418. /* We found a USB Keyboard, install it. */
  419. usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
  420. debug("USB KBD: found set idle...\n");
  421. #if !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) && \
  422. !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
  423. usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE / 4, 0);
  424. #else
  425. usb_set_idle(dev, iface->desc.bInterfaceNumber, 0, 0);
  426. #endif
  427. debug("USB KBD: enable interrupt pipe...\n");
  428. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  429. data->intq = create_int_queue(dev, data->intpipe, 1,
  430. USB_KBD_BOOT_REPORT_SIZE, data->new,
  431. data->intinterval);
  432. if (!data->intq) {
  433. #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
  434. if (usb_get_report(dev, iface->desc.bInterfaceNumber,
  435. 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE) < 0) {
  436. #else
  437. if (usb_int_msg(dev, data->intpipe, data->new, data->intpktsize,
  438. data->intinterval, false) < 0) {
  439. #endif
  440. printf("Failed to get keyboard state from device %04x:%04x\n",
  441. dev->descriptor.idVendor, dev->descriptor.idProduct);
  442. /* Abort, we don't want to use that non-functional keyboard. */
  443. return 0;
  444. }
  445. /* Success. */
  446. return 1;
  447. }
  448. static int probe_usb_keyboard(struct usb_device *dev)
  449. {
  450. char *stdinname;
  451. struct stdio_dev usb_kbd_dev;
  452. int error;
  453. /* Try probing the keyboard */
  454. if (usb_kbd_probe_dev(dev, 0) != 1)
  455. return -ENOENT;
  456. /* Register the keyboard */
  457. debug("USB KBD: register.\n");
  458. memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
  459. strcpy(usb_kbd_dev.name, DEVNAME);
  460. usb_kbd_dev.flags = DEV_FLAGS_INPUT;
  461. usb_kbd_dev.getc = usb_kbd_getc;
  462. usb_kbd_dev.tstc = usb_kbd_testc;
  463. usb_kbd_dev.priv = (void *)dev;
  464. error = stdio_register(&usb_kbd_dev);
  465. if (error)
  466. return error;
  467. stdinname = env_get("stdin");
  468. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  469. error = iomux_doenv(stdin, stdinname);
  470. if (error)
  471. return error;
  472. #else
  473. /* Check if this is the standard input device. */
  474. if (strcmp(stdinname, DEVNAME))
  475. return 1;
  476. /* Reassign the console */
  477. if (overwrite_console())
  478. return 1;
  479. error = console_assign(stdin, DEVNAME);
  480. if (error)
  481. return error;
  482. #endif
  483. return 0;
  484. }
  485. #if !CONFIG_IS_ENABLED(DM_USB)
  486. /* Search for keyboard and register it if found. */
  487. int drv_usb_kbd_init(void)
  488. {
  489. int error, i;
  490. debug("%s: Probing for keyboard\n", __func__);
  491. /* Scan all USB Devices */
  492. for (i = 0; i < USB_MAX_DEVICE; i++) {
  493. struct usb_device *dev;
  494. /* Get USB device. */
  495. dev = usb_get_dev_index(i);
  496. if (!dev)
  497. break;
  498. if (dev->devnum == -1)
  499. continue;
  500. error = probe_usb_keyboard(dev);
  501. if (!error)
  502. return 1;
  503. if (error && error != -ENOENT)
  504. return error;
  505. }
  506. /* No USB Keyboard found */
  507. return -1;
  508. }
  509. /* Deregister the keyboard. */
  510. int usb_kbd_deregister(int force)
  511. {
  512. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  513. struct stdio_dev *dev;
  514. struct usb_device *usb_kbd_dev;
  515. struct usb_kbd_pdata *data;
  516. dev = stdio_get_by_name(DEVNAME);
  517. if (dev) {
  518. usb_kbd_dev = (struct usb_device *)dev->priv;
  519. data = usb_kbd_dev->privptr;
  520. if (stdio_deregister_dev(dev, force) != 0)
  521. return 1;
  522. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  523. if (iomux_doenv(stdin, env_get("stdin")) != 0)
  524. return 1;
  525. #endif
  526. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  527. destroy_int_queue(usb_kbd_dev, data->intq);
  528. #endif
  529. free(data->new);
  530. free(data);
  531. }
  532. return 0;
  533. #else
  534. return 1;
  535. #endif
  536. }
  537. #endif
  538. #if CONFIG_IS_ENABLED(DM_USB)
  539. static int usb_kbd_probe(struct udevice *dev)
  540. {
  541. struct usb_device *udev = dev_get_parent_priv(dev);
  542. return probe_usb_keyboard(udev);
  543. }
  544. static int usb_kbd_remove(struct udevice *dev)
  545. {
  546. struct usb_device *udev = dev_get_parent_priv(dev);
  547. struct usb_kbd_pdata *data;
  548. struct stdio_dev *sdev;
  549. int ret;
  550. sdev = stdio_get_by_name(DEVNAME);
  551. if (!sdev) {
  552. ret = -ENXIO;
  553. goto err;
  554. }
  555. data = udev->privptr;
  556. if (stdio_deregister_dev(sdev, true)) {
  557. ret = -EPERM;
  558. goto err;
  559. }
  560. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  561. if (iomux_doenv(stdin, env_get("stdin"))) {
  562. ret = -ENOLINK;
  563. goto err;
  564. }
  565. #endif
  566. #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
  567. destroy_int_queue(udev, data->intq);
  568. #endif
  569. free(data->new);
  570. free(data);
  571. return 0;
  572. err:
  573. printf("%s: warning, ret=%d", __func__, ret);
  574. return ret;
  575. }
  576. static const struct udevice_id usb_kbd_ids[] = {
  577. { .compatible = "usb-keyboard" },
  578. { }
  579. };
  580. U_BOOT_DRIVER(usb_kbd) = {
  581. .name = "usb_kbd",
  582. .id = UCLASS_KEYBOARD,
  583. .of_match = usb_kbd_ids,
  584. .probe = usb_kbd_probe,
  585. .remove = usb_kbd_remove,
  586. };
  587. static const struct usb_device_id kbd_id_table[] = {
  588. {
  589. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
  590. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  591. USB_DEVICE_ID_MATCH_INT_PROTOCOL,
  592. .bInterfaceClass = USB_CLASS_HID,
  593. .bInterfaceSubClass = USB_SUB_HID_BOOT,
  594. .bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
  595. },
  596. { } /* Terminating entry */
  597. };
  598. U_BOOT_USB_DEVICE(usb_kbd, kbd_id_table);
  599. #endif