usb_kbd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland
  4. *
  5. * Part of this source has been derived from the Linux USB
  6. * project.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <malloc.h>
  12. #include <stdio_dev.h>
  13. #include <asm/byteorder.h>
  14. #include <usb.h>
  15. /*
  16. * If overwrite_console returns 1, the stdin, stderr and stdout
  17. * are switched to the serial port, else the settings in the
  18. * environment are used
  19. */
  20. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  21. extern int overwrite_console(void);
  22. #else
  23. int overwrite_console(void)
  24. {
  25. return 0;
  26. }
  27. #endif
  28. /* Keyboard sampling rate */
  29. #define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */
  30. #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
  31. #define NUM_LOCK 0x53
  32. #define CAPS_LOCK 0x39
  33. #define SCROLL_LOCK 0x47
  34. /* Modifier bits */
  35. #define LEFT_CNTR (1 << 0)
  36. #define LEFT_SHIFT (1 << 1)
  37. #define LEFT_ALT (1 << 2)
  38. #define LEFT_GUI (1 << 3)
  39. #define RIGHT_CNTR (1 << 4)
  40. #define RIGHT_SHIFT (1 << 5)
  41. #define RIGHT_ALT (1 << 6)
  42. #define RIGHT_GUI (1 << 7)
  43. /* Size of the keyboard buffer */
  44. #define USB_KBD_BUFFER_LEN 0x20
  45. /* Device name */
  46. #define DEVNAME "usbkbd"
  47. /* Keyboard maps */
  48. static const unsigned char usb_kbd_numkey[] = {
  49. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  50. '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
  51. '\\', '#', ';', '\'', '`', ',', '.', '/'
  52. };
  53. static const unsigned char usb_kbd_numkey_shifted[] = {
  54. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  55. '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
  56. '|', '~', ':', '"', '~', '<', '>', '?'
  57. };
  58. static const unsigned char usb_kbd_num_keypad[] = {
  59. '/', '*', '-', '+', '\r',
  60. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  61. '.', 0, 0, 0, '='
  62. };
  63. /*
  64. * map arrow keys to ^F/^B ^N/^P, can't really use the proper
  65. * ANSI sequence for arrow keys because the queuing code breaks
  66. * when a single keypress expands to 3 queue elements
  67. */
  68. static const unsigned char usb_kbd_arrow[] = {
  69. 0x6, 0x2, 0xe, 0x10
  70. };
  71. /*
  72. * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
  73. * order. See usb_kbd_setled() function!
  74. */
  75. #define USB_KBD_NUMLOCK (1 << 0)
  76. #define USB_KBD_CAPSLOCK (1 << 1)
  77. #define USB_KBD_SCROLLLOCK (1 << 2)
  78. #define USB_KBD_CTRL (1 << 3)
  79. #define USB_KBD_LEDMASK \
  80. (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
  81. /*
  82. * USB Keyboard reports are 8 bytes in boot protocol.
  83. * Appendix B of HID Device Class Definition 1.11
  84. */
  85. #define USB_KBD_BOOT_REPORT_SIZE 8
  86. struct usb_kbd_pdata {
  87. uint32_t repeat_delay;
  88. uint32_t usb_in_pointer;
  89. uint32_t usb_out_pointer;
  90. uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
  91. uint8_t *new;
  92. uint8_t old[USB_KBD_BOOT_REPORT_SIZE];
  93. uint8_t flags;
  94. };
  95. extern int __maybe_unused net_busy_flag;
  96. /* The period of time between two calls of usb_kbd_testc(). */
  97. static unsigned long __maybe_unused kbd_testc_tms;
  98. /* Generic keyboard event polling. */
  99. void usb_kbd_generic_poll(void)
  100. {
  101. struct stdio_dev *dev;
  102. struct usb_device *usb_kbd_dev;
  103. struct usb_kbd_pdata *data;
  104. struct usb_interface *iface;
  105. struct usb_endpoint_descriptor *ep;
  106. int pipe;
  107. int maxp;
  108. /* Get the pointer to USB Keyboard device pointer */
  109. dev = stdio_get_by_name(DEVNAME);
  110. usb_kbd_dev = (struct usb_device *)dev->priv;
  111. data = usb_kbd_dev->privptr;
  112. iface = &usb_kbd_dev->config.if_desc[0];
  113. ep = &iface->ep_desc[0];
  114. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  115. /* Submit a interrupt transfer request */
  116. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  117. usb_submit_int_msg(usb_kbd_dev, pipe, data->new,
  118. min(maxp, USB_KBD_BOOT_REPORT_SIZE),
  119. ep->bInterval);
  120. }
  121. /* Puts character in the queue and sets up the in and out pointer. */
  122. static void usb_kbd_put_queue(struct usb_kbd_pdata *data, char c)
  123. {
  124. if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
  125. /* Check for buffer full. */
  126. if (data->usb_out_pointer == 0)
  127. return;
  128. data->usb_in_pointer = 0;
  129. } else {
  130. /* Check for buffer full. */
  131. if (data->usb_in_pointer == data->usb_out_pointer - 1)
  132. return;
  133. data->usb_in_pointer++;
  134. }
  135. data->usb_kbd_buffer[data->usb_in_pointer] = c;
  136. }
  137. /*
  138. * Set the LEDs. Since this is used in the irq routine, the control job is
  139. * issued with a timeout of 0. This means, that the job is queued without
  140. * waiting for job completion.
  141. */
  142. static void usb_kbd_setled(struct usb_device *dev)
  143. {
  144. struct usb_interface *iface = &dev->config.if_desc[0];
  145. struct usb_kbd_pdata *data = dev->privptr;
  146. uint32_t leds = data->flags & USB_KBD_LEDMASK;
  147. usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  148. USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  149. 0x200, iface->desc.bInterfaceNumber, (void *)&leds, 1, 0);
  150. }
  151. #define CAPITAL_MASK 0x20
  152. /* Translate the scancode in ASCII */
  153. static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
  154. unsigned char modifier, int pressed)
  155. {
  156. uint8_t keycode = 0;
  157. /* Key released */
  158. if (pressed == 0) {
  159. data->repeat_delay = 0;
  160. return 0;
  161. }
  162. if (pressed == 2) {
  163. data->repeat_delay++;
  164. if (data->repeat_delay < REPEAT_DELAY)
  165. return 0;
  166. data->repeat_delay = REPEAT_DELAY;
  167. }
  168. /* Alphanumeric values */
  169. if ((scancode > 3) && (scancode <= 0x1d)) {
  170. keycode = scancode - 4 + 'a';
  171. if (data->flags & USB_KBD_CAPSLOCK)
  172. keycode &= ~CAPITAL_MASK;
  173. if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
  174. /* Handle CAPSLock + Shift pressed simultaneously */
  175. if (keycode & CAPITAL_MASK)
  176. keycode &= ~CAPITAL_MASK;
  177. else
  178. keycode |= CAPITAL_MASK;
  179. }
  180. }
  181. if ((scancode > 0x1d) && (scancode < 0x3a)) {
  182. /* Shift pressed */
  183. if (modifier & (LEFT_SHIFT | RIGHT_SHIFT))
  184. keycode = usb_kbd_numkey_shifted[scancode - 0x1e];
  185. else
  186. keycode = usb_kbd_numkey[scancode - 0x1e];
  187. }
  188. /* Arrow keys */
  189. if ((scancode >= 0x4f) && (scancode <= 0x52))
  190. keycode = usb_kbd_arrow[scancode - 0x4f];
  191. /* Numeric keypad */
  192. if ((scancode >= 0x54) && (scancode <= 0x67))
  193. keycode = usb_kbd_num_keypad[scancode - 0x54];
  194. if (data->flags & USB_KBD_CTRL)
  195. keycode = scancode - 0x3;
  196. if (pressed == 1) {
  197. if (scancode == NUM_LOCK) {
  198. data->flags ^= USB_KBD_NUMLOCK;
  199. return 1;
  200. }
  201. if (scancode == CAPS_LOCK) {
  202. data->flags ^= USB_KBD_CAPSLOCK;
  203. return 1;
  204. }
  205. if (scancode == SCROLL_LOCK) {
  206. data->flags ^= USB_KBD_SCROLLLOCK;
  207. return 1;
  208. }
  209. }
  210. /* Report keycode if any */
  211. if (keycode) {
  212. debug("%c", keycode);
  213. usb_kbd_put_queue(data, keycode);
  214. }
  215. return 0;
  216. }
  217. static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
  218. {
  219. uint32_t res = 0;
  220. struct usb_kbd_pdata *data = dev->privptr;
  221. uint8_t *new;
  222. uint8_t *old;
  223. if (up) {
  224. new = data->old;
  225. old = data->new;
  226. } else {
  227. new = data->new;
  228. old = data->old;
  229. }
  230. if ((old[i] > 3) &&
  231. (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) ==
  232. new + USB_KBD_BOOT_REPORT_SIZE)) {
  233. res |= usb_kbd_translate(data, old[i], data->new[0], up);
  234. }
  235. return res;
  236. }
  237. /* Interrupt service routine */
  238. static int usb_kbd_irq_worker(struct usb_device *dev)
  239. {
  240. struct usb_kbd_pdata *data = dev->privptr;
  241. int i, res = 0;
  242. /* No combo key pressed */
  243. if (data->new[0] == 0x00)
  244. data->flags &= ~USB_KBD_CTRL;
  245. /* Left or Right Ctrl pressed */
  246. else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
  247. data->flags |= USB_KBD_CTRL;
  248. for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) {
  249. res |= usb_kbd_service_key(dev, i, 0);
  250. res |= usb_kbd_service_key(dev, i, 1);
  251. }
  252. /* Key is still pressed */
  253. if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
  254. res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
  255. if (res == 1)
  256. usb_kbd_setled(dev);
  257. memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE);
  258. return 1;
  259. }
  260. /* Keyboard interrupt handler */
  261. static int usb_kbd_irq(struct usb_device *dev)
  262. {
  263. if ((dev->irq_status != 0) ||
  264. (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) {
  265. debug("USB KBD: Error %lX, len %d\n",
  266. dev->irq_status, dev->irq_act_len);
  267. return 1;
  268. }
  269. return usb_kbd_irq_worker(dev);
  270. }
  271. /* Interrupt polling */
  272. static inline void usb_kbd_poll_for_event(struct usb_device *dev)
  273. {
  274. #if defined(CONFIG_SYS_USB_EVENT_POLL)
  275. struct usb_interface *iface;
  276. struct usb_endpoint_descriptor *ep;
  277. struct usb_kbd_pdata *data;
  278. int pipe;
  279. int maxp;
  280. /* Get the pointer to USB Keyboard device pointer */
  281. data = dev->privptr;
  282. iface = &dev->config.if_desc[0];
  283. ep = &iface->ep_desc[0];
  284. pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  285. /* Submit a interrupt transfer request */
  286. maxp = usb_maxpacket(dev, pipe);
  287. usb_submit_int_msg(dev, pipe, &data->new[0],
  288. min(maxp, USB_KBD_BOOT_REPORT_SIZE),
  289. ep->bInterval);
  290. usb_kbd_irq_worker(dev);
  291. #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
  292. struct usb_interface *iface;
  293. struct usb_kbd_pdata *data = dev->privptr;
  294. iface = &dev->config.if_desc[0];
  295. usb_get_report(dev, iface->desc.bInterfaceNumber,
  296. 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE);
  297. if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE))
  298. usb_kbd_irq_worker(dev);
  299. #endif
  300. }
  301. /* test if a character is in the queue */
  302. static int usb_kbd_testc(struct stdio_dev *sdev)
  303. {
  304. struct stdio_dev *dev;
  305. struct usb_device *usb_kbd_dev;
  306. struct usb_kbd_pdata *data;
  307. #ifdef CONFIG_CMD_NET
  308. /*
  309. * If net_busy_flag is 1, NET transfer is running,
  310. * then we check key-pressed every second (first check may be
  311. * less than 1 second) to improve TFTP booting performance.
  312. */
  313. if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
  314. return 0;
  315. kbd_testc_tms = get_timer(0);
  316. #endif
  317. dev = stdio_get_by_name(DEVNAME);
  318. usb_kbd_dev = (struct usb_device *)dev->priv;
  319. data = usb_kbd_dev->privptr;
  320. usb_kbd_poll_for_event(usb_kbd_dev);
  321. return !(data->usb_in_pointer == data->usb_out_pointer);
  322. }
  323. /* gets the character from the queue */
  324. static int usb_kbd_getc(struct stdio_dev *sdev)
  325. {
  326. struct stdio_dev *dev;
  327. struct usb_device *usb_kbd_dev;
  328. struct usb_kbd_pdata *data;
  329. dev = stdio_get_by_name(DEVNAME);
  330. usb_kbd_dev = (struct usb_device *)dev->priv;
  331. data = usb_kbd_dev->privptr;
  332. while (data->usb_in_pointer == data->usb_out_pointer)
  333. usb_kbd_poll_for_event(usb_kbd_dev);
  334. if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
  335. data->usb_out_pointer = 0;
  336. else
  337. data->usb_out_pointer++;
  338. return data->usb_kbd_buffer[data->usb_out_pointer];
  339. }
  340. /* probes the USB device dev for keyboard type. */
  341. static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
  342. {
  343. struct usb_interface *iface;
  344. struct usb_endpoint_descriptor *ep;
  345. struct usb_kbd_pdata *data;
  346. int pipe, maxp;
  347. if (dev->descriptor.bNumConfigurations != 1)
  348. return 0;
  349. iface = &dev->config.if_desc[ifnum];
  350. if (iface->desc.bInterfaceClass != 3)
  351. return 0;
  352. if (iface->desc.bInterfaceSubClass != 1)
  353. return 0;
  354. if (iface->desc.bInterfaceProtocol != 1)
  355. return 0;
  356. if (iface->desc.bNumEndpoints != 1)
  357. return 0;
  358. ep = &iface->ep_desc[0];
  359. /* Check if endpoint 1 is interrupt endpoint */
  360. if (!(ep->bEndpointAddress & 0x80))
  361. return 0;
  362. if ((ep->bmAttributes & 3) != 3)
  363. return 0;
  364. debug("USB KBD: found set protocol...\n");
  365. data = malloc(sizeof(struct usb_kbd_pdata));
  366. if (!data) {
  367. printf("USB KBD: Error allocating private data\n");
  368. return 0;
  369. }
  370. /* Clear private data */
  371. memset(data, 0, sizeof(struct usb_kbd_pdata));
  372. /* allocate input buffer aligned and sized to USB DMA alignment */
  373. data->new = memalign(USB_DMA_MINALIGN,
  374. roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
  375. /* Insert private data into USB device structure */
  376. dev->privptr = data;
  377. /* Set IRQ handler */
  378. dev->irq_handle = usb_kbd_irq;
  379. pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  380. maxp = usb_maxpacket(dev, pipe);
  381. /* We found a USB Keyboard, install it. */
  382. usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
  383. debug("USB KBD: found set idle...\n");
  384. usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
  385. debug("USB KBD: enable interrupt pipe...\n");
  386. if (usb_submit_int_msg(dev, pipe, data->new,
  387. min(maxp, USB_KBD_BOOT_REPORT_SIZE),
  388. ep->bInterval) < 0) {
  389. printf("Failed to get keyboard state from device %04x:%04x\n",
  390. dev->descriptor.idVendor, dev->descriptor.idProduct);
  391. /* Abort, we don't want to use that non-functional keyboard. */
  392. return 0;
  393. }
  394. /* Success. */
  395. return 1;
  396. }
  397. /* Search for keyboard and register it if found. */
  398. int drv_usb_kbd_init(void)
  399. {
  400. struct stdio_dev usb_kbd_dev, *old_dev;
  401. struct usb_device *dev;
  402. char *stdinname = getenv("stdin");
  403. int error, i;
  404. /* Scan all USB Devices */
  405. for (i = 0; i < USB_MAX_DEVICE; i++) {
  406. /* Get USB device. */
  407. dev = usb_get_dev_index(i);
  408. if (!dev)
  409. return -1;
  410. if (dev->devnum == -1)
  411. continue;
  412. /* Try probing the keyboard */
  413. if (usb_kbd_probe(dev, 0) != 1)
  414. continue;
  415. /* We found a keyboard, check if it is already registered. */
  416. debug("USB KBD: found set up device.\n");
  417. old_dev = stdio_get_by_name(DEVNAME);
  418. if (old_dev) {
  419. /* Already registered, just return ok. */
  420. debug("USB KBD: is already registered.\n");
  421. usb_kbd_deregister();
  422. return 1;
  423. }
  424. /* Register the keyboard */
  425. debug("USB KBD: register.\n");
  426. memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
  427. strcpy(usb_kbd_dev.name, DEVNAME);
  428. usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  429. usb_kbd_dev.getc = usb_kbd_getc;
  430. usb_kbd_dev.tstc = usb_kbd_testc;
  431. usb_kbd_dev.priv = (void *)dev;
  432. error = stdio_register(&usb_kbd_dev);
  433. if (error)
  434. return error;
  435. #ifdef CONFIG_CONSOLE_MUX
  436. error = iomux_doenv(stdin, stdinname);
  437. if (error)
  438. return error;
  439. #else
  440. /* Check if this is the standard input device. */
  441. if (strcmp(stdinname, DEVNAME))
  442. return 1;
  443. /* Reassign the console */
  444. if (overwrite_console())
  445. return 1;
  446. error = console_assign(stdin, DEVNAME);
  447. if (error)
  448. return error;
  449. #endif
  450. return 1;
  451. }
  452. /* No USB Keyboard found */
  453. return -1;
  454. }
  455. /* Deregister the keyboard. */
  456. int usb_kbd_deregister(void)
  457. {
  458. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  459. return stdio_deregister(DEVNAME);
  460. #else
  461. return 1;
  462. #endif
  463. }