usb_kbd.c 15 KB

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