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,
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 CFG_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 REAPEAT_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 volatile char usb_kbd_buffer[USB_KBD_BUFFER_LEN];
76 static volatile int usb_in_pointer = 0;
77 static volatile int usb_out_pointer = 0;
82 #define DEVNAME "usbkbd"
83 static unsigned char num_lock = 0;
84 static unsigned char caps_lock = 0;
85 static unsigned char scroll_lock = 0;
86 static unsigned char ctrl = 0;
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','\r',0x1b,'\b','\t',' ', '-',
92 '=', '[', ']','\\', '#', ';', '\'', '`', ',', '.', '/'
94 static unsigned char usb_kbd_numkey_shifted[] = {
95 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')','\r',0x1b,'\b','\t',' ', '_',
96 '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?'
99 /******************************************************************
101 ******************************************************************/
102 /* puts character in the queue and sets up the in and out pointer */
103 static void usb_kbd_put_queue(char data)
105 if((usb_in_pointer+1)==USB_KBD_BUFFER_LEN) {
106 if(usb_out_pointer==0) {
107 return; /* buffer full */
112 if((usb_in_pointer+1)==usb_out_pointer)
113 return; /* buffer full */
116 usb_kbd_buffer[usb_in_pointer]=data;
120 /* test if a character is in the queue */
121 static int usb_kbd_testc(void)
123 #ifdef CFG_USB_EVENT_POLL
126 if(usb_in_pointer==usb_out_pointer)
127 return(0); /* no data */
131 /* gets the character from the queue */
132 static int usb_kbd_getc(void)
135 while(usb_in_pointer==usb_out_pointer) {
136 #ifdef CFG_USB_EVENT_POLL
140 if((usb_out_pointer+1)==USB_KBD_BUFFER_LEN)
144 c=usb_kbd_buffer[usb_out_pointer];
149 /* forward decleration */
150 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum);
152 /* search for keyboard and register it if found */
153 int drv_usb_kbd_init(void)
156 device_t usb_kbd_dev,*old_dev;
157 struct usb_device *dev;
158 char *stdinname = getenv ("stdin");
162 /* scan all USB Devices */
163 for(i=0;i<USB_MAX_DEVICE;i++) {
164 dev=usb_get_dev_index(i); /* get device */
165 if(dev->devnum!=-1) {
166 if(usb_kbd_probe(dev,0)==1) { /* Ok, we found a keyboard */
167 /* check, if it is already registered */
168 USB_KBD_PRINTF("USB KBD found set up device.\n");
169 old_dev = device_get_by_name(DEVNAME);
171 /* ok, already registered, just return ok */
172 USB_KBD_PRINTF("USB KBD is already registered.\n");
175 /* register the keyboard */
176 USB_KBD_PRINTF("USB KBD register.\n");
177 memset (&usb_kbd_dev, 0, sizeof(device_t));
178 strcpy(usb_kbd_dev.name, DEVNAME);
179 usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
180 usb_kbd_dev.putc = NULL;
181 usb_kbd_dev.puts = NULL;
182 usb_kbd_dev.getc = usb_kbd_getc;
183 usb_kbd_dev.tstc = usb_kbd_testc;
184 error = device_register (&usb_kbd_dev);
186 /* check if this is the standard input device */
187 if(strcmp(stdinname,DEVNAME)==0) {
188 /* reassign the console */
189 if(overwrite_console()) {
192 error=console_assign(stdin,DEVNAME);
204 /* no USB Keyboard found */
209 /* deregistering the keyboard */
210 int usb_kbd_deregister(void)
212 return device_deregister(DEVNAME);
215 /**************************************************************************
219 /* set the LEDs. Since this is used in the irq routine, the control job
220 is issued with a timeout of 0. This means, that the job is queued without
221 waiting for job completion */
223 static void usb_kbd_setled(struct usb_device *dev)
225 struct usb_interface_descriptor *iface;
226 iface = &dev->config.if_desc[0];
236 usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
237 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
238 0x200, iface->bInterfaceNumber,(void *)&leds, 1, 0);
243 #define CAPITAL_MASK 0x20
244 /* Translate the scancode in ASCII */
245 static int usb_kbd_translate(unsigned char scancode,unsigned char modifier,int pressed)
247 unsigned char keycode;
256 if(repeat_delay<REPEAT_DELAY)
258 repeat_delay=REPEAT_DELAY;
261 if((scancode>3) && (scancode<=0x1d)) { /* alpha numeric values */
262 keycode=scancode-4 + 0x61;
264 keycode&=~CAPITAL_MASK; /* switch to capital Letters */
265 if(((modifier&(1<<LEFT_SHIFT))!=0)||((modifier&(1<<RIGHT_SHIFT))!=0)) {
266 if(keycode & CAPITAL_MASK)
267 keycode&=~CAPITAL_MASK; /* switch to capital Letters */
269 keycode|=CAPITAL_MASK; /* switch to non capital Letters */
272 if((scancode>0x1d) && (scancode<0x3A)) {
273 if(((modifier&(1<<LEFT_SHIFT))!=0)||((modifier&(1<<RIGHT_SHIFT))!=0)) /* shifted */
274 keycode=usb_kbd_numkey_shifted[scancode-0x1e];
275 else /* non shifted */
276 keycode=usb_kbd_numkey[scancode-0x1e];
280 keycode = scancode - 0x3;
283 if(scancode==NUM_LOCK) {
287 if(scancode==CAPS_LOCK) {
288 caps_lock=~caps_lock;
291 if(scancode==SCROLL_LOCK) {
292 scroll_lock=~scroll_lock;
297 USB_KBD_PRINTF("%c",keycode);
298 usb_kbd_put_queue(keycode);
303 /* Interrupt service routine */
304 static int usb_kbd_irq(struct usb_device *dev)
308 if((dev->irq_status!=0)||(dev->irq_act_len!=8))
310 USB_KBD_PRINTF("usb_keyboard Error %lX, len %d\n",dev->irq_status,dev->irq_act_len);
316 case 0x0: /* No combo key pressed */
319 case 0x01: /* Left Ctrl pressed */
320 case 0x10: /* Right Ctrl pressed */
325 for (i = 2; i < 8; i++) {
326 if (old[i] > 3 && memscan(&new[2], old[i], 6) == &new[8]) {
327 res|=usb_kbd_translate(old[i],new[0],0);
329 if (new[i] > 3 && memscan(&old[2], new[i], 6) == &old[8]) {
330 res|=usb_kbd_translate(new[i],new[0],1);
333 if((new[2]>3) && (old[2]==new[2])) /* still pressed */
334 res|=usb_kbd_translate(new[2],new[0],2);
337 memcpy(&old[0],&new[0], 8);
338 return 1; /* install IRQ Handler again */
341 /* probes the USB device dev for keyboard type */
342 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
344 struct usb_interface_descriptor *iface;
345 struct usb_endpoint_descriptor *ep;
348 if (dev->descriptor.bNumConfigurations != 1) return 0;
349 iface = &dev->config.if_desc[ifnum];
351 if (iface->bInterfaceClass != 3) return 0;
352 if (iface->bInterfaceSubClass != 1) return 0;
353 if (iface->bInterfaceProtocol != 1) return 0;
354 if (iface->bNumEndpoints != 1) return 0;
356 ep = &iface->ep_desc[0];
358 if (!(ep->bEndpointAddress & 0x80)) return 0;
359 if ((ep->bmAttributes & 3) != 3) return 0;
360 USB_KBD_PRINTF("USB KBD found set protocol...\n");
361 /* ok, we found a USB Keyboard, install it */
362 /* usb_kbd_get_hid_desc(dev); */
363 usb_set_protocol(dev, iface->bInterfaceNumber, 0);
364 USB_KBD_PRINTF("USB KBD found set idle...\n");
365 usb_set_idle(dev, iface->bInterfaceNumber, REPEAT_RATE, 0);
366 memset(&new[0], 0, 8);
367 memset(&old[0], 0, 8);
369 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
370 maxp = usb_maxpacket(dev, pipe);
371 dev->irq_handle=usb_kbd_irq;
372 USB_KBD_PRINTF("USB KBD enable interrupt pipe...\n");
373 usb_submit_int_msg(dev,pipe,&new[0], maxp > 8 ? 8 : maxp,ep->bInterval);
379 struct usb_hid_descriptor {
380 unsigned char bLength;
381 unsigned char bDescriptorType; /* 0x21 for HID */
382 unsigned short bcdHID; /* release number */
383 unsigned char bCountryCode;
384 unsigned char bNumDescriptors;
385 unsigned char bReportDescriptorType;
386 unsigned short wDescriptorLength;
387 } __attribute__ ((packed));
390 * We parse each description item into this structure. Short items data
391 * values are expanded to 32-bit signed int, long items contain a pointer
392 * into the data area.
396 unsigned char format;
407 unsigned char *longdata;
412 * HID report item format
415 #define HID_ITEM_FORMAT_SHORT 0
416 #define HID_ITEM_FORMAT_LONG 1
419 * Special tag indicating long items
422 #define HID_ITEM_TAG_LONG 15
425 static struct usb_hid_descriptor usb_kbd_hid_desc;
427 void usb_kbd_display_hid(struct usb_hid_descriptor *hid)
429 printf("USB_HID_DESC:\n");
430 printf(" bLenght 0x%x\n",hid->bLength);
431 printf(" bcdHID 0x%x\n",hid->bcdHID);
432 printf(" bCountryCode %d\n",hid->bCountryCode);
433 printf(" bNumDescriptors 0x%x\n",hid->bNumDescriptors);
434 printf(" bReportDescriptorType 0x%x\n",hid->bReportDescriptorType);
435 printf(" wDescriptorLength 0x%x\n",hid->wDescriptorLength);
440 * Fetch a report description item from the data stream. We support long
441 * items, though they are not used yet.
444 static int fetch_item(unsigned char *start,unsigned char *end, struct hid_item *item)
446 if((end - start) > 0) {
447 unsigned char b = *start++;
448 item->type = (b >> 2) & 3;
449 item->tag = (b >> 4) & 15;
450 if (item->tag == HID_ITEM_TAG_LONG) {
451 item->format = HID_ITEM_FORMAT_LONG;
452 if ((end - start) >= 2) {
453 item->size = *start++;
454 item->tag = *start++;
455 if ((end - start) >= item->size) {
456 item->data.longdata = start;
462 item->format = HID_ITEM_FORMAT_SHORT;
464 switch (item->size) {
468 if ((end - start) >= 1) {
469 item->data.u8 = *start++;
474 if ((end - start) >= 2) {
475 item->data.u16 = le16_to_cpu((unsigned short *)start);
481 if ((end - start) >= 4) {
482 item->data.u32 = le32_to_cpu((unsigned long *)start);
493 * HID report descriptor item type (prefix bit 2,3)
496 #define HID_ITEM_TYPE_MAIN 0
497 #define HID_ITEM_TYPE_GLOBAL 1
498 #define HID_ITEM_TYPE_LOCAL 2
499 #define HID_ITEM_TYPE_RESERVED 3
501 * HID report descriptor main item tags
504 #define HID_MAIN_ITEM_TAG_INPUT 8
505 #define HID_MAIN_ITEM_TAG_OUTPUT 9
506 #define HID_MAIN_ITEM_TAG_FEATURE 11
507 #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION 10
508 #define HID_MAIN_ITEM_TAG_END_COLLECTION 12
510 * HID report descriptor main item contents
513 #define HID_MAIN_ITEM_CONSTANT 0x001
514 #define HID_MAIN_ITEM_VARIABLE 0x002
515 #define HID_MAIN_ITEM_RELATIVE 0x004
516 #define HID_MAIN_ITEM_WRAP 0x008
517 #define HID_MAIN_ITEM_NONLINEAR 0x010
518 #define HID_MAIN_ITEM_NO_PREFERRED 0x020
519 #define HID_MAIN_ITEM_NULL_STATE 0x040
520 #define HID_MAIN_ITEM_VOLATILE 0x080
521 #define HID_MAIN_ITEM_BUFFERED_BYTE 0x100
524 * HID report descriptor collection item types
527 #define HID_COLLECTION_PHYSICAL 0
528 #define HID_COLLECTION_APPLICATION 1
529 #define HID_COLLECTION_LOGICAL 2
531 * HID report descriptor global item tags
534 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0
535 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM 1
536 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM 2
537 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM 3
538 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM 4
539 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 5
540 #define HID_GLOBAL_ITEM_TAG_UNIT 6
541 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 7
542 #define HID_GLOBAL_ITEM_TAG_REPORT_ID 8
543 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 9
544 #define HID_GLOBAL_ITEM_TAG_PUSH 10
545 #define HID_GLOBAL_ITEM_TAG_POP 11
548 * HID report descriptor local item tags
551 #define HID_LOCAL_ITEM_TAG_USAGE 0
552 #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM 1
553 #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM 2
554 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 3
555 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM 4
556 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM 5
557 #define HID_LOCAL_ITEM_TAG_STRING_INDEX 7
558 #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM 8
559 #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM 9
560 #define HID_LOCAL_ITEM_TAG_DELIMITER 10
563 static void usb_kbd_show_item(struct hid_item *item)
566 case HID_ITEM_TYPE_MAIN:
568 case HID_MAIN_ITEM_TAG_INPUT:
569 printf("Main Input");
571 case HID_MAIN_ITEM_TAG_OUTPUT:
572 printf("Main Output");
574 case HID_MAIN_ITEM_TAG_FEATURE:
575 printf("Main Feature");
577 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
578 printf("Main Begin Collection");
580 case HID_MAIN_ITEM_TAG_END_COLLECTION:
581 printf("Main End Collection");
584 printf("Main reserved %d",item->tag);
588 case HID_ITEM_TYPE_GLOBAL:
590 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
591 printf("- Global Usage Page");
593 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
594 printf("- Global Logical Minimum");
596 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
597 printf("- Global Logical Maximum");
599 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
600 printf("- Global physical Minimum");
602 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
603 printf("- Global physical Maximum");
605 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
606 printf("- Global Unit Exponent");
608 case HID_GLOBAL_ITEM_TAG_UNIT:
609 printf("- Global Unit");
611 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
612 printf("- Global Report Size");
614 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
615 printf("- Global Report ID");
617 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
618 printf("- Global Report Count");
620 case HID_GLOBAL_ITEM_TAG_PUSH:
621 printf("- Global Push");
623 case HID_GLOBAL_ITEM_TAG_POP:
624 printf("- Global Pop");
627 printf("- Global reserved %d",item->tag);
631 case HID_ITEM_TYPE_LOCAL:
633 case HID_LOCAL_ITEM_TAG_USAGE:
634 printf("-- Local Usage");
636 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
637 printf("-- Local Usage Minimum");
639 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
640 printf("-- Local Usage Maximum");
642 case HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX:
643 printf("-- Local Designator Index");
645 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM:
646 printf("-- Local Designator Minimum");
648 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM:
649 printf("-- Local Designator Maximum");
651 case HID_LOCAL_ITEM_TAG_STRING_INDEX:
652 printf("-- Local String Index");
654 case HID_LOCAL_ITEM_TAG_STRING_MINIMUM:
655 printf("-- Local String Minimum");
657 case HID_LOCAL_ITEM_TAG_STRING_MAXIMUM:
658 printf("-- Local String Maximum");
660 case HID_LOCAL_ITEM_TAG_DELIMITER:
661 printf("-- Local Delimiter");
664 printf("-- Local reserved %d",item->tag);
669 printf("--- reserved %d",item->type);
674 printf(" %d",item->data.u8);
677 printf(" %d",item->data.u16);
680 printf(" %ld",item->data.u32);
687 static int usb_kbd_get_hid_desc(struct usb_device *dev)
689 unsigned char buffer[256];
690 struct usb_descriptor_header *head;
691 struct usb_config_descriptor *config;
693 unsigned char *start, *end;
694 struct hid_item item;
696 if(usb_get_configuration_no(dev,&buffer[0],0)==-1)
698 head =(struct usb_descriptor_header *)&buffer[0];
699 if(head->bDescriptorType!=USB_DT_CONFIG) {
700 printf(" ERROR: NOT USB_CONFIG_DESC %x\n",head->bDescriptorType);
704 config=(struct usb_config_descriptor *)&buffer[0];
705 len=le16_to_cpu(config->wTotalLength);
706 /* Ok the first entry must be a configuration entry, now process the others */
707 head=(struct usb_descriptor_header *)&buffer[index];
708 while(index+1 < len) {
709 if(head->bDescriptorType==USB_DT_HID) {
710 printf("HID desc found\n");
711 memcpy(&usb_kbd_hid_desc,&buffer[index],buffer[index]);
712 le16_to_cpus(&usb_kbd_hid_desc.bcdHID);
713 le16_to_cpus(&usb_kbd_hid_desc.wDescriptorLength);
714 usb_kbd_display_hid(&usb_kbd_hid_desc);
718 index+=head->bLength;
719 head=(struct usb_descriptor_header *)&buffer[index];
723 len=usb_kbd_hid_desc.wDescriptorLength;
724 if((index = usb_get_class_descriptor(dev, 0, USB_DT_REPORT, 0, &buffer[0], len)) < 0) {
725 printf("reading report descriptor failed\n");
728 printf(" report descriptor (size %u, read %d)\n", len, index);
733 index=fetch_item(start,end,&item);
737 usb_kbd_show_item(&item);