usb_kbd.c 14 KB

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