usb_kbd.c 15 KB

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