usb_kbd.c 14 KB

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