usb_kbd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. *
  26. */
  27. #include <common.h>
  28. #include <malloc.h>
  29. #include <stdio_dev.h>
  30. #include <asm/byteorder.h>
  31. #include <usb.h>
  32. /*
  33. * If overwrite_console returns 1, the stdin, stderr and stdout
  34. * are switched to the serial port, else the settings in the
  35. * environment are used
  36. */
  37. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  38. extern int overwrite_console(void);
  39. #else
  40. int overwrite_console(void)
  41. {
  42. return 0;
  43. }
  44. #endif
  45. /* Keyboard sampling rate */
  46. #define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */
  47. #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
  48. #define NUM_LOCK 0x53
  49. #define CAPS_LOCK 0x39
  50. #define SCROLL_LOCK 0x47
  51. /* Modifier bits */
  52. #define LEFT_CNTR (1 << 0)
  53. #define LEFT_SHIFT (1 << 1)
  54. #define LEFT_ALT (1 << 2)
  55. #define LEFT_GUI (1 << 3)
  56. #define RIGHT_CNTR (1 << 4)
  57. #define RIGHT_SHIFT (1 << 5)
  58. #define RIGHT_ALT (1 << 6)
  59. #define RIGHT_GUI (1 << 7)
  60. /* Size of the keyboard buffer */
  61. #define USB_KBD_BUFFER_LEN 0x20
  62. /* Device name */
  63. #define DEVNAME "usbkbd"
  64. /* Keyboard maps */
  65. static const unsigned char usb_kbd_numkey[] = {
  66. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  67. '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
  68. '\\', '#', ';', '\'', '`', ',', '.', '/'
  69. };
  70. static const unsigned char usb_kbd_numkey_shifted[] = {
  71. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  72. '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
  73. '|', '~', ':', '"', '~', '<', '>', '?'
  74. };
  75. static const unsigned char usb_kbd_num_keypad[] = {
  76. '/', '*', '-', '+', '\r',
  77. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  78. '.', 0, 0, 0, '='
  79. };
  80. /*
  81. * map arrow keys to ^F/^B ^N/^P, can't really use the proper
  82. * ANSI sequence for arrow keys because the queuing code breaks
  83. * when a single keypress expands to 3 queue elements
  84. */
  85. static const unsigned char usb_kbd_arrow[] = {
  86. 0x6, 0x2, 0xe, 0x10
  87. };
  88. /*
  89. * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
  90. * order. See usb_kbd_setled() function!
  91. */
  92. #define USB_KBD_NUMLOCK (1 << 0)
  93. #define USB_KBD_CAPSLOCK (1 << 1)
  94. #define USB_KBD_SCROLLLOCK (1 << 2)
  95. #define USB_KBD_CTRL (1 << 3)
  96. #define USB_KBD_LEDMASK \
  97. (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
  98. struct usb_kbd_pdata {
  99. uint32_t repeat_delay;
  100. uint32_t usb_in_pointer;
  101. uint32_t usb_out_pointer;
  102. uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
  103. uint8_t *new;
  104. uint8_t old[8];
  105. uint8_t flags;
  106. };
  107. /* Generic keyboard event polling. */
  108. void usb_kbd_generic_poll(void)
  109. {
  110. struct stdio_dev *dev;
  111. struct usb_device *usb_kbd_dev;
  112. struct usb_kbd_pdata *data;
  113. struct usb_interface *iface;
  114. struct usb_endpoint_descriptor *ep;
  115. int pipe;
  116. int maxp;
  117. /* Get the pointer to USB Keyboard device pointer */
  118. dev = stdio_get_by_name(DEVNAME);
  119. usb_kbd_dev = (struct usb_device *)dev->priv;
  120. data = usb_kbd_dev->privptr;
  121. iface = &usb_kbd_dev->config.if_desc[0];
  122. ep = &iface->ep_desc[0];
  123. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  124. /* Submit a interrupt transfer request */
  125. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  126. usb_submit_int_msg(usb_kbd_dev, pipe, data->new,
  127. maxp > 8 ? 8 : maxp, ep->bInterval);
  128. }
  129. /* Puts character in the queue and sets up the in and out pointer. */
  130. static void usb_kbd_put_queue(struct usb_kbd_pdata *data, char c)
  131. {
  132. if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
  133. /* Check for buffer full. */
  134. if (data->usb_out_pointer == 0)
  135. return;
  136. data->usb_in_pointer = 0;
  137. } else {
  138. /* Check for buffer full. */
  139. if (data->usb_in_pointer == data->usb_out_pointer - 1)
  140. return;
  141. data->usb_in_pointer++;
  142. }
  143. data->usb_kbd_buffer[data->usb_in_pointer] = c;
  144. }
  145. /*
  146. * Set the LEDs. Since this is used in the irq routine, the control job is
  147. * issued with a timeout of 0. This means, that the job is queued without
  148. * waiting for job completion.
  149. */
  150. static void usb_kbd_setled(struct usb_device *dev)
  151. {
  152. struct usb_interface *iface = &dev->config.if_desc[0];
  153. struct usb_kbd_pdata *data = dev->privptr;
  154. uint32_t leds = data->flags & USB_KBD_LEDMASK;
  155. usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  156. USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  157. 0x200, iface->desc.bInterfaceNumber, (void *)&leds, 1, 0);
  158. }
  159. #define CAPITAL_MASK 0x20
  160. /* Translate the scancode in ASCII */
  161. static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
  162. unsigned char modifier, int pressed)
  163. {
  164. uint8_t keycode = 0;
  165. /* Key released */
  166. if (pressed == 0) {
  167. data->repeat_delay = 0;
  168. return 0;
  169. }
  170. if (pressed == 2) {
  171. data->repeat_delay++;
  172. if (data->repeat_delay < REPEAT_DELAY)
  173. return 0;
  174. data->repeat_delay = REPEAT_DELAY;
  175. }
  176. /* Alphanumeric values */
  177. if ((scancode > 3) && (scancode <= 0x1d)) {
  178. keycode = scancode - 4 + 'a';
  179. if (data->flags & USB_KBD_CAPSLOCK)
  180. keycode &= ~CAPITAL_MASK;
  181. if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
  182. /* Handle CAPSLock + Shift pressed simultaneously */
  183. if (keycode & CAPITAL_MASK)
  184. keycode &= ~CAPITAL_MASK;
  185. else
  186. keycode |= CAPITAL_MASK;
  187. }
  188. }
  189. if ((scancode > 0x1d) && (scancode < 0x3a)) {
  190. /* Shift pressed */
  191. if (modifier & (LEFT_SHIFT | RIGHT_SHIFT))
  192. keycode = usb_kbd_numkey_shifted[scancode - 0x1e];
  193. else
  194. keycode = usb_kbd_numkey[scancode - 0x1e];
  195. }
  196. /* Arrow keys */
  197. if ((scancode >= 0x4f) && (scancode <= 0x52))
  198. keycode = usb_kbd_arrow[scancode - 0x4f];
  199. /* Numeric keypad */
  200. if ((scancode >= 0x54) && (scancode <= 0x67))
  201. keycode = usb_kbd_num_keypad[scancode - 0x54];
  202. if (data->flags & USB_KBD_CTRL)
  203. keycode = scancode - 0x3;
  204. if (pressed == 1) {
  205. if (scancode == NUM_LOCK) {
  206. data->flags ^= USB_KBD_NUMLOCK;
  207. return 1;
  208. }
  209. if (scancode == CAPS_LOCK) {
  210. data->flags ^= USB_KBD_CAPSLOCK;
  211. return 1;
  212. }
  213. if (scancode == SCROLL_LOCK) {
  214. data->flags ^= USB_KBD_SCROLLLOCK;
  215. return 1;
  216. }
  217. }
  218. /* Report keycode if any */
  219. if (keycode) {
  220. debug("%c", keycode);
  221. usb_kbd_put_queue(data, keycode);
  222. }
  223. return 0;
  224. }
  225. static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
  226. {
  227. uint32_t res = 0;
  228. struct usb_kbd_pdata *data = dev->privptr;
  229. uint8_t *new;
  230. uint8_t *old;
  231. if (up) {
  232. new = data->old;
  233. old = data->new;
  234. } else {
  235. new = data->new;
  236. old = data->old;
  237. }
  238. if ((old[i] > 3) && (memscan(new + 2, old[i], 6) == new + 8))
  239. res |= usb_kbd_translate(data, old[i], data->new[0], up);
  240. return res;
  241. }
  242. /* Interrupt service routine */
  243. static int usb_kbd_irq_worker(struct usb_device *dev)
  244. {
  245. struct usb_kbd_pdata *data = dev->privptr;
  246. int i, res = 0;
  247. /* No combo key pressed */
  248. if (data->new[0] == 0x00)
  249. data->flags &= ~USB_KBD_CTRL;
  250. /* Left or Right Ctrl pressed */
  251. else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
  252. data->flags |= USB_KBD_CTRL;
  253. for (i = 2; i < 8; i++) {
  254. res |= usb_kbd_service_key(dev, i, 0);
  255. res |= usb_kbd_service_key(dev, i, 1);
  256. }
  257. /* Key is still pressed */
  258. if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
  259. res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
  260. if (res == 1)
  261. usb_kbd_setled(dev);
  262. memcpy(data->old, data->new, 8);
  263. return 1;
  264. }
  265. /* Keyboard interrupt handler */
  266. static int usb_kbd_irq(struct usb_device *dev)
  267. {
  268. if ((dev->irq_status != 0) || (dev->irq_act_len != 8)) {
  269. debug("USB KBD: Error %lX, len %d\n",
  270. dev->irq_status, dev->irq_act_len);
  271. return 1;
  272. }
  273. return usb_kbd_irq_worker(dev);
  274. }
  275. /* Interrupt polling */
  276. static inline void usb_kbd_poll_for_event(struct usb_device *dev)
  277. {
  278. #if defined(CONFIG_SYS_USB_EVENT_POLL)
  279. struct usb_interface *iface;
  280. struct usb_endpoint_descriptor *ep;
  281. struct usb_kbd_pdata *data;
  282. int pipe;
  283. int maxp;
  284. /* Get the pointer to USB Keyboard device pointer */
  285. data = dev->privptr;
  286. iface = &dev->config.if_desc[0];
  287. ep = &iface->ep_desc[0];
  288. pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  289. /* Submit a interrupt transfer request */
  290. maxp = usb_maxpacket(dev, pipe);
  291. usb_submit_int_msg(dev, pipe, &data->new[0],
  292. maxp > 8 ? 8 : maxp, ep->bInterval);
  293. usb_kbd_irq_worker(dev);
  294. #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
  295. struct usb_interface *iface;
  296. struct usb_kbd_pdata *data = dev->privptr;
  297. iface = &dev->config.if_desc[0];
  298. usb_get_report(dev, iface->desc.bInterfaceNumber,
  299. 1, 0, data->new, sizeof(data->new));
  300. if (memcmp(data->old, data->new, sizeof(data->new)))
  301. usb_kbd_irq_worker(dev);
  302. #endif
  303. }
  304. /* test if a character is in the queue */
  305. static int usb_kbd_testc(void)
  306. {
  307. struct stdio_dev *dev;
  308. struct usb_device *usb_kbd_dev;
  309. struct usb_kbd_pdata *data;
  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(void)
  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. int pipe, maxp;
  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, roundup(8, 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. pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  372. maxp = usb_maxpacket(dev, pipe);
  373. /* We found a USB Keyboard, install it. */
  374. usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
  375. debug("USB KBD: found set idle...\n");
  376. usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
  377. debug("USB KBD: enable interrupt pipe...\n");
  378. if (usb_submit_int_msg(dev, pipe, data->new, maxp > 8 ? 8 : maxp,
  379. ep->bInterval) < 0) {
  380. printf("Failed to get keyboard state from device %04x:%04x\n",
  381. dev->descriptor.idVendor, dev->descriptor.idProduct);
  382. /* Abort, we don't want to use that non-functional keyboard. */
  383. return 0;
  384. }
  385. /* Success. */
  386. return 1;
  387. }
  388. /* Search for keyboard and register it if found. */
  389. int drv_usb_kbd_init(void)
  390. {
  391. struct stdio_dev usb_kbd_dev, *old_dev;
  392. struct usb_device *dev;
  393. char *stdinname = getenv("stdin");
  394. int error, i;
  395. /* Scan all USB Devices */
  396. for (i = 0; i < USB_MAX_DEVICE; i++) {
  397. /* Get USB device. */
  398. dev = usb_get_dev_index(i);
  399. if (!dev)
  400. return -1;
  401. if (dev->devnum == -1)
  402. continue;
  403. /* Try probing the keyboard */
  404. if (usb_kbd_probe(dev, 0) != 1)
  405. continue;
  406. /* We found a keyboard, check if it is already registered. */
  407. debug("USB KBD: found set up device.\n");
  408. old_dev = stdio_get_by_name(DEVNAME);
  409. if (old_dev) {
  410. /* Already registered, just return ok. */
  411. debug("USB KBD: is already registered.\n");
  412. usb_kbd_deregister();
  413. return 1;
  414. }
  415. /* Register the keyboard */
  416. debug("USB KBD: register.\n");
  417. memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
  418. strcpy(usb_kbd_dev.name, DEVNAME);
  419. usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  420. usb_kbd_dev.putc = NULL;
  421. usb_kbd_dev.puts = NULL;
  422. usb_kbd_dev.getc = usb_kbd_getc;
  423. usb_kbd_dev.tstc = usb_kbd_testc;
  424. usb_kbd_dev.priv = (void *)dev;
  425. error = stdio_register(&usb_kbd_dev);
  426. if (error)
  427. return error;
  428. #ifdef CONFIG_CONSOLE_MUX
  429. error = iomux_doenv(stdin, stdinname);
  430. if (error)
  431. return error;
  432. #else
  433. /* Check if this is the standard input device. */
  434. if (strcmp(stdinname, DEVNAME))
  435. return 1;
  436. /* Reassign the console */
  437. if (overwrite_console())
  438. return 1;
  439. error = console_assign(stdin, DEVNAME);
  440. if (error)
  441. return error;
  442. #endif
  443. return 1;
  444. }
  445. /* No USB Keyboard found */
  446. return -1;
  447. }
  448. /* Deregister the keyboard. */
  449. int usb_kbd_deregister(void)
  450. {
  451. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  452. return stdio_deregister(DEVNAME);
  453. #else
  454. return 1;
  455. #endif
  456. }