3 * Denis Peter, MPL AG Switzerland
5 * Part of this source has been derived from the Linux USB
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #include <stdio_dev.h>
29 #include <asm/byteorder.h>
35 * If overwrite_console returns 1, the stdin, stderr and stdout
36 * are switched to the serial port, else the settings in the
37 * environment are used
39 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
40 extern int overwrite_console(void);
42 int overwrite_console(void)
49 #define USB_KBD_PRINTF(fmt, args...) printf(fmt, ##args)
51 #define USB_KBD_PRINTF(fmt, args...)
55 #define REPEAT_RATE (40/4) /* 40msec -> 25cps */
56 #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
59 #define CAPS_LOCK 0x39
60 #define SCROLL_LOCK 0x47
73 #define USB_KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
75 static char usb_kbd_buffer[USB_KBD_BUFFER_LEN];
76 static int usb_in_pointer;
77 static int usb_out_pointer;
82 #define DEVNAME "usbkbd"
83 static unsigned char num_lock;
84 static unsigned char caps_lock;
85 static unsigned char scroll_lock;
86 static unsigned char ctrl;
88 static unsigned char leds __attribute__((aligned(0x4)));
90 static unsigned char usb_kbd_numkey[] = {
91 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
92 '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
93 '\\', '#', ';', '\'', '`', ',', '.', '/'
95 static unsigned char usb_kbd_numkey_shifted[] = {
96 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
97 '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
98 '|', '~', ':', '"', '~', '<', '>', '?'
101 /******************************************************************
103 ******************************************************************/
104 /* puts character in the queue and sets up the in and out pointer */
105 static void usb_kbd_put_queue(char data)
107 if ((usb_in_pointer+1) == USB_KBD_BUFFER_LEN) {
108 if (usb_out_pointer == 0)
109 return; /* buffer full */
113 if ((usb_in_pointer+1) == usb_out_pointer)
114 return; /* buffer full */
117 usb_kbd_buffer[usb_in_pointer] = data;
121 /* test if a character is in the queue */
122 static int usb_kbd_testc(void)
124 #ifdef CONFIG_SYS_USB_EVENT_POLL
127 if (usb_in_pointer == usb_out_pointer)
128 return 0; /* no data */
132 /* gets the character from the queue */
133 static int usb_kbd_getc(void)
136 while (usb_in_pointer == usb_out_pointer) {
137 #ifdef CONFIG_SYS_USB_EVENT_POLL
141 if ((usb_out_pointer+1) == USB_KBD_BUFFER_LEN)
145 c = usb_kbd_buffer[usb_out_pointer];
150 /* forward decleration */
151 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum);
153 /* search for keyboard and register it if found */
154 int drv_usb_kbd_init(void)
157 struct stdio_dev usb_kbd_dev, *old_dev;
158 struct usb_device *dev;
159 char *stdinname = getenv("stdin");
163 /* scan all USB Devices */
164 for (i = 0 ; i < USB_MAX_DEVICE ; i++) {
165 dev = usb_get_dev_index(i); /* get device */
168 if (dev->devnum != -1) {
169 /* Ok, we found a keyboard */
170 if (usb_kbd_probe(dev, 0) == 1) {
171 /* check, if it is already registered */
172 USB_KBD_PRINTF("USB KBD found set up "
174 old_dev = stdio_get_by_name(DEVNAME);
176 /* already registered, just return ok */
177 USB_KBD_PRINTF("USB KBD is already "
181 /* register the keyboard */
182 USB_KBD_PRINTF("USB KBD register.\n");
183 memset(&usb_kbd_dev, 0,
184 sizeof(struct stdio_dev));
185 strcpy(usb_kbd_dev.name, DEVNAME);
186 usb_kbd_dev.flags = DEV_FLAGS_INPUT |
188 usb_kbd_dev.putc = NULL;
189 usb_kbd_dev.puts = NULL;
190 usb_kbd_dev.getc = usb_kbd_getc;
191 usb_kbd_dev.tstc = usb_kbd_testc;
192 usb_kbd_dev.priv = (void *)dev;
193 error = stdio_register(&usb_kbd_dev);
196 * check if this is the standard
199 if (strcmp(stdinname, DEVNAME) == 0) {
200 /* reassign the console */
201 if (overwrite_console())
203 error = console_assign(stdin,
216 /* no USB Keyboard found */
221 /* deregistering the keyboard */
222 int usb_kbd_deregister(void)
224 #ifdef CONFIG_SYS_STDIO_DEREGISTER
225 return stdio_deregister(DEVNAME);
231 /**************************************************************************
235 /* set the LEDs. Since this is used in the irq routine, the control job
236 is issued with a timeout of 0. This means, that the job is queued without
237 waiting for job completion */
239 static void usb_kbd_setled(struct usb_device *dev)
241 struct usb_interface *iface;
242 iface = &dev->config.if_desc[0];
244 if (scroll_lock != 0)
252 usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
253 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
254 0x200, iface->desc.bInterfaceNumber, (void *)&leds, 1, 0);
259 #define CAPITAL_MASK 0x20
260 /* Translate the scancode in ASCII */
261 static int usb_kbd_translate(unsigned char scancode, unsigned char modifier,
264 unsigned char keycode;
273 if (repeat_delay < REPEAT_DELAY)
275 repeat_delay = REPEAT_DELAY;
278 if ((scancode > 3) && (scancode <= 0x1d)) { /* alpha numeric values */
279 keycode = scancode - 4 + 0x61;
281 /* switch to capital Letters */
282 keycode &= ~CAPITAL_MASK;
283 if (((modifier&(1 << LEFT_SHIFT)) != 0) ||
284 ((modifier&(1 << RIGHT_SHIFT)) != 0)) {
285 if (keycode & CAPITAL_MASK)
286 /* switch to capital Letters */
287 keycode &= ~CAPITAL_MASK;
289 /* switch to non capital Letters */
290 keycode |= CAPITAL_MASK;
293 if ((scancode > 0x1d) && (scancode < 0x3A)) {
294 if (((modifier&(1 << LEFT_SHIFT)) != 0) ||
295 ((modifier&(1 << RIGHT_SHIFT)) != 0)) /* shifted */
296 keycode = usb_kbd_numkey_shifted[scancode-0x1e];
297 else /* non shifted */
298 keycode = usb_kbd_numkey[scancode-0x1e];
302 keycode = scancode - 0x3;
305 if (scancode == NUM_LOCK) {
306 num_lock = ~num_lock;
309 if (scancode == CAPS_LOCK) {
310 caps_lock = ~caps_lock;
313 if (scancode == SCROLL_LOCK) {
314 scroll_lock = ~scroll_lock;
319 USB_KBD_PRINTF("%c", keycode);
320 usb_kbd_put_queue(keycode);
325 /* Interrupt service routine */
326 static int usb_kbd_irq(struct usb_device *dev)
330 if ((dev->irq_status != 0) || (dev->irq_act_len != 8)) {
331 USB_KBD_PRINTF("usb_keyboard Error %lX, len %d\n",
332 dev->irq_status, dev->irq_act_len);
338 case 0x0: /* No combo key pressed */
341 case 0x01: /* Left Ctrl pressed */
342 case 0x10: /* Right Ctrl pressed */
347 for (i = 2; i < 8; i++) {
348 if (old[i] > 3 && memscan(&new[2], old[i], 6) == &new[8])
349 res |= usb_kbd_translate(old[i], new[0], 0);
351 if (new[i] > 3 && memscan(&old[2], new[i], 6) == &old[8])
352 res |= usb_kbd_translate(new[i], new[0], 1);
355 if ((new[2] > 3) && (old[2] == new[2])) /* still pressed */
356 res |= usb_kbd_translate(new[2], new[0], 2);
360 memcpy(&old[0], &new[0], 8);
362 return 1; /* install IRQ Handler again */
365 /* probes the USB device dev for keyboard type */
366 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
368 struct usb_interface *iface;
369 struct usb_endpoint_descriptor *ep;
372 if (dev->descriptor.bNumConfigurations != 1)
374 iface = &dev->config.if_desc[ifnum];
376 if (iface->desc.bInterfaceClass != 3)
378 if (iface->desc.bInterfaceSubClass != 1)
380 if (iface->desc.bInterfaceProtocol != 1)
382 if (iface->desc.bNumEndpoints != 1)
385 ep = &iface->ep_desc[0];
387 if (!(ep->bEndpointAddress & 0x80))
389 if ((ep->bmAttributes & 3) != 3)
391 USB_KBD_PRINTF("USB KBD found set protocol...\n");
392 /* ok, we found a USB Keyboard, install it */
393 /* usb_kbd_get_hid_desc(dev); */
394 usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
395 USB_KBD_PRINTF("USB KBD found set idle...\n");
396 usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
397 memset(&new[0], 0, 8);
398 memset(&old[0], 0, 8);
400 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
401 maxp = usb_maxpacket(dev, pipe);
402 dev->irq_handle = usb_kbd_irq;
403 USB_KBD_PRINTF("USB KBD enable interrupt pipe...\n");
404 usb_submit_int_msg(dev, pipe, &new[0], maxp > 8 ? 8 : maxp,
411 struct usb_hid_descriptor {
412 unsigned char bLength;
413 unsigned char bDescriptorType; /* 0x21 for HID */
414 unsigned short bcdHID; /* release number */
415 unsigned char bCountryCode;
416 unsigned char bNumDescriptors;
417 unsigned char bReportDescriptorType;
418 unsigned short wDescriptorLength;
422 * We parse each description item into this structure. Short items data
423 * values are expanded to 32-bit signed int, long items contain a pointer
424 * into the data area.
428 unsigned char format;
439 unsigned char *longdata;
444 * HID report item format
447 #define HID_ITEM_FORMAT_SHORT 0
448 #define HID_ITEM_FORMAT_LONG 1
451 * Special tag indicating long items
454 #define HID_ITEM_TAG_LONG 15
457 static struct usb_hid_descriptor usb_kbd_hid_desc;
459 void usb_kbd_display_hid(struct usb_hid_descriptor *hid)
461 printf("USB_HID_DESC:\n");
462 printf(" bLenght 0x%x\n", hid->bLength);
463 printf(" bcdHID 0x%x\n", hid->bcdHID);
464 printf(" bCountryCode %d\n", hid->bCountryCode);
465 printf(" bNumDescriptors 0x%x\n", hid->bNumDescriptors);
466 printf(" bReportDescriptorType 0x%x\n", hid->bReportDescriptorType);
467 printf(" wDescriptorLength 0x%x\n", hid->wDescriptorLength);
472 * Fetch a report description item from the data stream. We support long
473 * items, though they are not used yet.
476 static int fetch_item(unsigned char *start, unsigned char *end,
477 struct hid_item *item)
479 if ((end - start) > 0) {
480 unsigned char b = *start++;
481 item->type = (b >> 2) & 3;
482 item->tag = (b >> 4) & 15;
483 if (item->tag == HID_ITEM_TAG_LONG) {
484 item->format = HID_ITEM_FORMAT_LONG;
485 if ((end - start) >= 2) {
486 item->size = *start++;
487 item->tag = *start++;
488 if ((end - start) >= item->size) {
489 item->data.longdata = start;
495 item->format = HID_ITEM_FORMAT_SHORT;
497 switch (item->size) {
501 if ((end - start) >= 1) {
502 item->data.u8 = *start++;
507 if ((end - start) >= 2) {
508 item->data.u16 = le16_to_cpu(
509 (unsigned short *)start);
515 if ((end - start) >= 4) {
516 item->data.u32 = le32_to_cpu(
517 (unsigned long *)start);
528 * HID report descriptor item type (prefix bit 2, 3)
531 #define HID_ITEM_TYPE_MAIN 0
532 #define HID_ITEM_TYPE_GLOBAL 1
533 #define HID_ITEM_TYPE_LOCAL 2
534 #define HID_ITEM_TYPE_RESERVED 3
536 * HID report descriptor main item tags
539 #define HID_MAIN_ITEM_TAG_INPUT 8
540 #define HID_MAIN_ITEM_TAG_OUTPUT 9
541 #define HID_MAIN_ITEM_TAG_FEATURE 11
542 #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION 10
543 #define HID_MAIN_ITEM_TAG_END_COLLECTION 12
545 * HID report descriptor main item contents
548 #define HID_MAIN_ITEM_CONSTANT 0x001
549 #define HID_MAIN_ITEM_VARIABLE 0x002
550 #define HID_MAIN_ITEM_RELATIVE 0x004
551 #define HID_MAIN_ITEM_WRAP 0x008
552 #define HID_MAIN_ITEM_NONLINEAR 0x010
553 #define HID_MAIN_ITEM_NO_PREFERRED 0x020
554 #define HID_MAIN_ITEM_NULL_STATE 0x040
555 #define HID_MAIN_ITEM_VOLATILE 0x080
556 #define HID_MAIN_ITEM_BUFFERED_BYTE 0x100
559 * HID report descriptor collection item types
562 #define HID_COLLECTION_PHYSICAL 0
563 #define HID_COLLECTION_APPLICATION 1
564 #define HID_COLLECTION_LOGICAL 2
566 * HID report descriptor global item tags
569 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0
570 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM 1
571 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM 2
572 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM 3
573 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM 4
574 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 5
575 #define HID_GLOBAL_ITEM_TAG_UNIT 6
576 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 7
577 #define HID_GLOBAL_ITEM_TAG_REPORT_ID 8
578 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 9
579 #define HID_GLOBAL_ITEM_TAG_PUSH 10
580 #define HID_GLOBAL_ITEM_TAG_POP 11
583 * HID report descriptor local item tags
586 #define HID_LOCAL_ITEM_TAG_USAGE 0
587 #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM 1
588 #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM 2
589 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 3
590 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM 4
591 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM 5
592 #define HID_LOCAL_ITEM_TAG_STRING_INDEX 7
593 #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM 8
594 #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM 9
595 #define HID_LOCAL_ITEM_TAG_DELIMITER 10
598 static void usb_kbd_show_item(struct hid_item *item)
600 switch (item->type) {
601 case HID_ITEM_TYPE_MAIN:
603 case HID_MAIN_ITEM_TAG_INPUT:
604 printf("Main Input");
606 case HID_MAIN_ITEM_TAG_OUTPUT:
607 printf("Main Output");
609 case HID_MAIN_ITEM_TAG_FEATURE:
610 printf("Main Feature");
612 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
613 printf("Main Begin Collection");
615 case HID_MAIN_ITEM_TAG_END_COLLECTION:
616 printf("Main End Collection");
619 printf("Main reserved %d", item->tag);
623 case HID_ITEM_TYPE_GLOBAL:
625 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
626 printf("- Global Usage Page");
628 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
629 printf("- Global Logical Minimum");
631 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
632 printf("- Global Logical Maximum");
634 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
635 printf("- Global physical Minimum");
637 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
638 printf("- Global physical Maximum");
640 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
641 printf("- Global Unit Exponent");
643 case HID_GLOBAL_ITEM_TAG_UNIT:
644 printf("- Global Unit");
646 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
647 printf("- Global Report Size");
649 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
650 printf("- Global Report ID");
652 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
653 printf("- Global Report Count");
655 case HID_GLOBAL_ITEM_TAG_PUSH:
656 printf("- Global Push");
658 case HID_GLOBAL_ITEM_TAG_POP:
659 printf("- Global Pop");
662 printf("- Global reserved %d", item->tag);
666 case HID_ITEM_TYPE_LOCAL:
668 case HID_LOCAL_ITEM_TAG_USAGE:
669 printf("-- Local Usage");
671 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
672 printf("-- Local Usage Minimum");
674 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
675 printf("-- Local Usage Maximum");
677 case HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX:
678 printf("-- Local Designator Index");
680 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM:
681 printf("-- Local Designator Minimum");
683 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM:
684 printf("-- Local Designator Maximum");
686 case HID_LOCAL_ITEM_TAG_STRING_INDEX:
687 printf("-- Local String Index");
689 case HID_LOCAL_ITEM_TAG_STRING_MINIMUM:
690 printf("-- Local String Minimum");
692 case HID_LOCAL_ITEM_TAG_STRING_MAXIMUM:
693 printf("-- Local String Maximum");
695 case HID_LOCAL_ITEM_TAG_DELIMITER:
696 printf("-- Local Delimiter");
699 printf("-- Local reserved %d", item->tag);
704 printf("--- reserved %d", item->type);
707 switch (item->size) {
709 printf(" %d", item->data.u8);
712 printf(" %d", item->data.u16);
715 printf(" %ld", item->data.u32);
722 static int usb_kbd_get_hid_desc(struct usb_device *dev)
724 unsigned char buffer[256];
725 struct usb_descriptor_header *head;
726 struct usb_config_descriptor *config;
728 unsigned char *start, *end;
729 struct hid_item item;
731 if (usb_get_configuration_no(dev, &buffer[0], 0) == -1)
733 head = (struct usb_descriptor_header *)&buffer[0];
734 if (head->bDescriptorType != USB_DT_CONFIG) {
735 printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
736 head->bDescriptorType);
739 index = head->bLength;
740 config = (struct usb_config_descriptor *)&buffer[0];
741 len = le16_to_cpu(config->wTotalLength);
743 * Ok the first entry must be a configuration entry,
744 * now process the others
746 head = (struct usb_descriptor_header *)&buffer[index];
747 while (index+1 < len) {
748 if (head->bDescriptorType == USB_DT_HID) {
749 printf("HID desc found\n");
750 memcpy(&usb_kbd_hid_desc, &buffer[index],
752 le16_to_cpus(&usb_kbd_hid_desc.bcdHID);
753 le16_to_cpus(&usb_kbd_hid_desc.wDescriptorLength);
754 usb_kbd_display_hid(&usb_kbd_hid_desc);
758 index += head->bLength;
759 head = (struct usb_descriptor_header *)&buffer[index];
763 len = usb_kbd_hid_desc.wDescriptorLength;
764 index = usb_get_class_descriptor(dev, 0, USB_DT_REPORT, 0, &buffer[0],
767 printf("reading report descriptor failed\n");
770 printf(" report descriptor (size %u, read %d)\n", len, index);
775 index = fetch_item(start, end, &item);
779 usb_kbd_show_item(&item);
783 } while (index >= 0);