usb_kbd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. #ifdef USB_KBD_DEBUG
  33. #define USB_KBD_PRINTF(fmt, args...) printf(fmt, ##args)
  34. #else
  35. #define USB_KBD_PRINTF(fmt, args...)
  36. #endif
  37. /*
  38. * If overwrite_console returns 1, the stdin, stderr and stdout
  39. * are switched to the serial port, else the settings in the
  40. * environment are used
  41. */
  42. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  43. extern int overwrite_console(void);
  44. #else
  45. int overwrite_console(void)
  46. {
  47. return 0;
  48. }
  49. #endif
  50. /* Keyboard sampling rate */
  51. #define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */
  52. #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
  53. #define NUM_LOCK 0x53
  54. #define CAPS_LOCK 0x39
  55. #define SCROLL_LOCK 0x47
  56. /* Modifier bits */
  57. #define LEFT_CNTR (1 << 0)
  58. #define LEFT_SHIFT (1 << 1)
  59. #define LEFT_ALT (1 << 2)
  60. #define LEFT_GUI (1 << 3)
  61. #define RIGHT_CNTR (1 << 4)
  62. #define RIGHT_SHIFT (1 << 5)
  63. #define RIGHT_ALT (1 << 6)
  64. #define RIGHT_GUI (1 << 7)
  65. /* Size of the keyboard buffer */
  66. #define USB_KBD_BUFFER_LEN 0x20
  67. /* Device name */
  68. #define DEVNAME "usbkbd"
  69. /* Keyboard maps */
  70. static const unsigned char usb_kbd_numkey[] = {
  71. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  72. '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
  73. '\\', '#', ';', '\'', '`', ',', '.', '/'
  74. };
  75. static const unsigned char usb_kbd_numkey_shifted[] = {
  76. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  77. '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
  78. '|', '~', ':', '"', '~', '<', '>', '?'
  79. };
  80. static const unsigned char usb_kbd_num_keypad[] = {
  81. '/', '*', '-', '+', '\r',
  82. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  83. '.', 0, 0, 0, '='
  84. };
  85. /*
  86. * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
  87. * order. See usb_kbd_setled() function!
  88. */
  89. #define USB_KBD_NUMLOCK (1 << 0)
  90. #define USB_KBD_CAPSLOCK (1 << 1)
  91. #define USB_KBD_SCROLLLOCK (1 << 2)
  92. #define USB_KBD_CTRL (1 << 3)
  93. #define USB_KBD_LEDMASK \
  94. (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
  95. struct usb_kbd_pdata {
  96. uint32_t repeat_delay;
  97. uint32_t usb_in_pointer;
  98. uint32_t usb_out_pointer;
  99. uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
  100. uint8_t new[8];
  101. uint8_t old[8];
  102. uint8_t flags;
  103. };
  104. /* Generic keyboard event polling. */
  105. void usb_kbd_generic_poll(void)
  106. {
  107. struct stdio_dev *dev;
  108. struct usb_device *usb_kbd_dev;
  109. struct usb_kbd_pdata *data;
  110. struct usb_interface *iface;
  111. struct usb_endpoint_descriptor *ep;
  112. int pipe;
  113. int maxp;
  114. /* Get the pointer to USB Keyboard device pointer */
  115. dev = stdio_get_by_name(DEVNAME);
  116. usb_kbd_dev = (struct usb_device *)dev->priv;
  117. data = usb_kbd_dev->privptr;
  118. iface = &usb_kbd_dev->config.if_desc[0];
  119. ep = &iface->ep_desc[0];
  120. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  121. /* Submit a interrupt transfer request */
  122. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  123. usb_submit_int_msg(usb_kbd_dev, pipe, data->new,
  124. maxp > 8 ? 8 : maxp, ep->bInterval);
  125. }
  126. /* Puts character in the queue and sets up the in and out pointer. */
  127. static void usb_kbd_put_queue(struct usb_kbd_pdata *data, char c)
  128. {
  129. if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
  130. /* Check for buffer full. */
  131. if (data->usb_out_pointer == 0)
  132. return;
  133. data->usb_in_pointer = 0;
  134. } else {
  135. /* Check for buffer full. */
  136. if (data->usb_in_pointer == data->usb_out_pointer - 1)
  137. return;
  138. data->usb_in_pointer++;
  139. }
  140. data->usb_kbd_buffer[data->usb_in_pointer] = c;
  141. }
  142. /*
  143. * Set the LEDs. Since this is used in the irq routine, the control job is
  144. * issued with a timeout of 0. This means, that the job is queued without
  145. * waiting for job completion.
  146. */
  147. static void usb_kbd_setled(struct usb_device *dev)
  148. {
  149. struct usb_interface *iface = &dev->config.if_desc[0];
  150. struct usb_kbd_pdata *data = dev->privptr;
  151. uint32_t leds = data->flags & USB_KBD_LEDMASK;
  152. usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  153. USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  154. 0x200, iface->desc.bInterfaceNumber, (void *)&leds, 1, 0);
  155. }
  156. #define CAPITAL_MASK 0x20
  157. /* Translate the scancode in ASCII */
  158. static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
  159. unsigned char modifier, int pressed)
  160. {
  161. uint8_t keycode = 0;
  162. /* Key released */
  163. if (pressed == 0) {
  164. data->repeat_delay = 0;
  165. return 0;
  166. }
  167. if (pressed == 2) {
  168. data->repeat_delay++;
  169. if (data->repeat_delay < REPEAT_DELAY)
  170. return 0;
  171. data->repeat_delay = REPEAT_DELAY;
  172. }
  173. /* Alphanumeric values */
  174. if ((scancode > 3) && (scancode <= 0x1d)) {
  175. keycode = scancode - 4 + 'a';
  176. if (data->flags & USB_KBD_CAPSLOCK)
  177. keycode &= ~CAPITAL_MASK;
  178. if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
  179. /* Handle CAPSLock + Shift pressed simultaneously */
  180. if (keycode & CAPITAL_MASK)
  181. keycode &= ~CAPITAL_MASK;
  182. else
  183. keycode |= CAPITAL_MASK;
  184. }
  185. }
  186. if ((scancode > 0x1d) && (scancode < 0x3a)) {
  187. /* Shift pressed */
  188. if (modifier & (LEFT_SHIFT | RIGHT_SHIFT))
  189. keycode = usb_kbd_numkey_shifted[scancode - 0x1e];
  190. else
  191. keycode = usb_kbd_numkey[scancode - 0x1e];
  192. }
  193. /* Numeric keypad */
  194. if ((scancode >= 0x54) && (scancode <= 0x67))
  195. keycode = usb_kbd_num_keypad[scancode - 0x54];
  196. if (data->flags & USB_KBD_CTRL)
  197. keycode = scancode - 0x3;
  198. if (pressed == 1) {
  199. if (scancode == NUM_LOCK) {
  200. data->flags ^= USB_KBD_NUMLOCK;
  201. return 1;
  202. }
  203. if (scancode == CAPS_LOCK) {
  204. data->flags ^= USB_KBD_CAPSLOCK;
  205. return 1;
  206. }
  207. if (scancode == SCROLL_LOCK) {
  208. data->flags ^= USB_KBD_SCROLLLOCK;
  209. return 1;
  210. }
  211. }
  212. /* Report keycode if any */
  213. if (keycode) {
  214. USB_KBD_PRINTF("%c", keycode);
  215. usb_kbd_put_queue(data, keycode);
  216. }
  217. return 0;
  218. }
  219. static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
  220. {
  221. uint32_t res = 0;
  222. struct usb_kbd_pdata *data = dev->privptr;
  223. uint8_t *new;
  224. uint8_t *old;
  225. if (up) {
  226. new = data->old;
  227. old = data->new;
  228. } else {
  229. new = data->new;
  230. old = data->old;
  231. }
  232. if ((old[i] > 3) && (memscan(new + 2, old[i], 6) == new + 8))
  233. res |= usb_kbd_translate(data, old[i], data->new[0], up);
  234. return res;
  235. }
  236. /* Interrupt service routine */
  237. static int usb_kbd_irq_worker(struct usb_device *dev)
  238. {
  239. struct usb_kbd_pdata *data = dev->privptr;
  240. int i, res = 0;
  241. /* No combo key pressed */
  242. if (data->new[0] == 0x00)
  243. data->flags &= ~USB_KBD_CTRL;
  244. /* Left or Right Ctrl pressed */
  245. else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
  246. data->flags |= USB_KBD_CTRL;
  247. for (i = 2; i < 8; i++) {
  248. res |= usb_kbd_service_key(dev, i, 0);
  249. res |= usb_kbd_service_key(dev, i, 1);
  250. }
  251. /* Key is still pressed */
  252. if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
  253. res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
  254. if (res == 1)
  255. usb_kbd_setled(dev);
  256. memcpy(data->old, data->new, 8);
  257. return 1;
  258. }
  259. /* Keyboard interrupt handler */
  260. static int usb_kbd_irq(struct usb_device *dev)
  261. {
  262. if ((dev->irq_status != 0) || (dev->irq_act_len != 8)) {
  263. USB_KBD_PRINTF("USB KBD: Error %lX, len %d\n",
  264. dev->irq_status, dev->irq_act_len);
  265. return 1;
  266. }
  267. return usb_kbd_irq_worker(dev);
  268. }
  269. /* Interrupt polling */
  270. static inline void usb_kbd_poll_for_event(struct usb_device *dev)
  271. {
  272. #if defined(CONFIG_SYS_USB_EVENT_POLL)
  273. struct usb_interface *iface;
  274. struct usb_endpoint_descriptor *ep;
  275. struct usb_kbd_pdata *data;
  276. int pipe;
  277. int maxp;
  278. /* Get the pointer to USB Keyboard device pointer */
  279. data = dev->privptr;
  280. iface = &dev->config.if_desc[0];
  281. ep = &iface->ep_desc[0];
  282. pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  283. /* Submit a interrupt transfer request */
  284. maxp = usb_maxpacket(dev, pipe);
  285. usb_submit_int_msg(dev, pipe, &data->new[0],
  286. maxp > 8 ? 8 : maxp, ep->bInterval);
  287. usb_kbd_irq_worker(dev);
  288. #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
  289. struct usb_interface *iface;
  290. struct usb_kbd_pdata *data = dev->privptr;
  291. iface = &dev->config.if_desc[0];
  292. usb_get_report(dev, iface->desc.bInterfaceNumber,
  293. 1, 0, data->new, sizeof(data->new));
  294. if (memcmp(data->old, data->new, sizeof(data->new)))
  295. usb_kbd_irq_worker(dev);
  296. #endif
  297. }
  298. /* test if a character is in the queue */
  299. static int usb_kbd_testc(void)
  300. {
  301. struct stdio_dev *dev;
  302. struct usb_device *usb_kbd_dev;
  303. struct usb_kbd_pdata *data;
  304. dev = stdio_get_by_name(DEVNAME);
  305. usb_kbd_dev = (struct usb_device *)dev->priv;
  306. data = usb_kbd_dev->privptr;
  307. usb_kbd_poll_for_event(usb_kbd_dev);
  308. return !(data->usb_in_pointer == data->usb_out_pointer);
  309. }
  310. /* gets the character from the queue */
  311. static int usb_kbd_getc(void)
  312. {
  313. struct stdio_dev *dev;
  314. struct usb_device *usb_kbd_dev;
  315. struct usb_kbd_pdata *data;
  316. dev = stdio_get_by_name(DEVNAME);
  317. usb_kbd_dev = (struct usb_device *)dev->priv;
  318. data = usb_kbd_dev->privptr;
  319. while (data->usb_in_pointer == data->usb_out_pointer)
  320. usb_kbd_poll_for_event(usb_kbd_dev);
  321. if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
  322. data->usb_out_pointer = 0;
  323. else
  324. data->usb_out_pointer++;
  325. return data->usb_kbd_buffer[data->usb_out_pointer];
  326. }
  327. /* probes the USB device dev for keyboard type. */
  328. static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
  329. {
  330. struct usb_interface *iface;
  331. struct usb_endpoint_descriptor *ep;
  332. struct usb_kbd_pdata *data;
  333. int pipe, maxp;
  334. if (dev->descriptor.bNumConfigurations != 1)
  335. return 0;
  336. iface = &dev->config.if_desc[ifnum];
  337. if (iface->desc.bInterfaceClass != 3)
  338. return 0;
  339. if (iface->desc.bInterfaceSubClass != 1)
  340. return 0;
  341. if (iface->desc.bInterfaceProtocol != 1)
  342. return 0;
  343. if (iface->desc.bNumEndpoints != 1)
  344. return 0;
  345. ep = &iface->ep_desc[0];
  346. /* Check if endpoint 1 is interrupt endpoint */
  347. if (!(ep->bEndpointAddress & 0x80))
  348. return 0;
  349. if ((ep->bmAttributes & 3) != 3)
  350. return 0;
  351. USB_KBD_PRINTF("USB KBD: found set protocol...\n");
  352. data = malloc(sizeof(struct usb_kbd_pdata));
  353. if (!data) {
  354. printf("USB KBD: Error allocating private data\n");
  355. return 0;
  356. }
  357. /* Clear private data */
  358. memset(data, 0, sizeof(struct usb_kbd_pdata));
  359. /* Insert private data into USB device structure */
  360. dev->privptr = data;
  361. /* Set IRQ handler */
  362. dev->irq_handle = usb_kbd_irq;
  363. pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  364. maxp = usb_maxpacket(dev, pipe);
  365. /* We found a USB Keyboard, install it. */
  366. usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
  367. USB_KBD_PRINTF("USB KBD: found set idle...\n");
  368. usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
  369. USB_KBD_PRINTF("USB KBD: enable interrupt pipe...\n");
  370. usb_submit_int_msg(dev, pipe, data->new, maxp > 8 ? 8 : maxp,
  371. ep->bInterval);
  372. /* Success. */
  373. return 1;
  374. }
  375. /* Search for keyboard and register it if found. */
  376. int drv_usb_kbd_init(void)
  377. {
  378. struct stdio_dev usb_kbd_dev, *old_dev;
  379. struct usb_device *dev;
  380. char *stdinname = getenv("stdin");
  381. int error, i;
  382. /* Scan all USB Devices */
  383. for (i = 0; i < USB_MAX_DEVICE; i++) {
  384. /* Get USB device. */
  385. dev = usb_get_dev_index(i);
  386. if (!dev)
  387. return -1;
  388. if (dev->devnum == -1)
  389. continue;
  390. /* Try probing the keyboard */
  391. if (usb_kbd_probe(dev, 0) != 1)
  392. continue;
  393. /* We found a keyboard, check if it is already registered. */
  394. USB_KBD_PRINTF("USB KBD: found set up device.\n");
  395. old_dev = stdio_get_by_name(DEVNAME);
  396. if (old_dev) {
  397. /* Already registered, just return ok. */
  398. USB_KBD_PRINTF("USB KBD: is already registered.\n");
  399. return 1;
  400. }
  401. /* Register the keyboard */
  402. USB_KBD_PRINTF("USB KBD: register.\n");
  403. memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
  404. strcpy(usb_kbd_dev.name, DEVNAME);
  405. usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  406. usb_kbd_dev.putc = NULL;
  407. usb_kbd_dev.puts = NULL;
  408. usb_kbd_dev.getc = usb_kbd_getc;
  409. usb_kbd_dev.tstc = usb_kbd_testc;
  410. usb_kbd_dev.priv = (void *)dev;
  411. error = stdio_register(&usb_kbd_dev);
  412. if (error)
  413. return error;
  414. #ifdef CONFIG_CONSOLE_MUX
  415. error = iomux_doenv(stdin, stdinname);
  416. if (error)
  417. return error;
  418. #else
  419. /* Check if this is the standard input device. */
  420. if (strcmp(stdinname, DEVNAME))
  421. return 1;
  422. /* Reassign the console */
  423. if (overwrite_console())
  424. return 1;
  425. error = console_assign(stdin, DEVNAME);
  426. if (error)
  427. return error;
  428. #endif
  429. return 1;
  430. }
  431. /* No USB Keyboard found */
  432. return -1;
  433. }
  434. /* Deregister the keyboard. */
  435. int usb_kbd_deregister(void)
  436. {
  437. #ifdef CONFIG_SYS_STDIO_DEREGISTER
  438. return stdio_deregister(DEVNAME);
  439. #else
  440. return 1;
  441. #endif
  442. }