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,
30 #ifdef CONFIG_USB_KEYBOARD
36 * if overwrite_console returns 1, the stdin, stderr and stdout
37 * are switched to the serial port, else the settings in the
38 * environment are used
40 #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
41 extern int overwrite_console (void);
43 int overwrite_console (void)
50 #define USB_KBD_PRINTF(fmt,args...) printf (fmt ,##args)
52 #define USB_KBD_PRINTF(fmt,args...)
56 #define REPEAT_RATE 40/4 /* 40msec -> 25cps */
57 #define REPEAT_DELAY 10 /* 10 x REAPEAT_RATE = 400msec */
60 #define CAPS_LOCK 0x39
61 #define SCROLL_LOCK 0x47
74 #define USB_KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
76 static volatile char usb_kbd_buffer[USB_KBD_BUFFER_LEN];
77 static volatile int usb_in_pointer = 0;
78 static volatile int usb_out_pointer = 0;
83 #define DEVNAME "usbkbd"
84 static unsigned char num_lock = 0;
85 static unsigned char caps_lock = 0;
86 static unsigned char scroll_lock = 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 if(usb_in_pointer==usb_out_pointer)
124 return(0); /* no data */
128 /* gets the character from the queue */
129 static int usb_kbd_getc(void)
132 while(usb_in_pointer==usb_out_pointer) {
133 #ifdef CFG_USB_EVENT_POLL
137 if((usb_out_pointer+1)==USB_KBD_BUFFER_LEN)
141 c=usb_kbd_buffer[usb_out_pointer];
146 /* forward decleration */
147 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum);
149 /* search for keyboard and register it if found */
150 int drv_usb_kbd_init(void)
153 device_t usb_kbd_dev,*old_dev;
154 struct usb_device *dev;
155 char *stdinname = getenv ("stdin");
159 /* scan all USB Devices */
160 for(i=0;i<USB_MAX_DEVICE;i++) {
161 dev=usb_get_dev_index(i); /* get device */
162 if(dev->devnum!=-1) {
163 if(usb_kbd_probe(dev,0)==1) { /* Ok, we found a keyboard */
164 /* check, if it is already registered */
165 USB_KBD_PRINTF("USB KBD found set up device.\n");
166 for (index=1; index<=ListNumItems(devlist); index++) {
167 old_dev = ListGetPtrToItem(devlist, index);
168 if(strcmp(old_dev->name,DEVNAME)==0) {
169 /* ok, already registered, just return ok */
170 USB_KBD_PRINTF("USB KBD is already registered.\n");
174 /* register the keyboard */
175 USB_KBD_PRINTF("USB KBD register.\n");
176 memset (&usb_kbd_dev, 0, sizeof(device_t));
177 strcpy(usb_kbd_dev.name, DEVNAME);
178 usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
179 usb_kbd_dev.putc = NULL;
180 usb_kbd_dev.puts = NULL;
181 usb_kbd_dev.getc = usb_kbd_getc;
182 usb_kbd_dev.tstc = usb_kbd_testc;
183 error = device_register (&usb_kbd_dev);
185 /* check if this is the standard input device */
186 if(strcmp(stdinname,DEVNAME)==0) {
187 /* reassign the console */
188 if(overwrite_console()) {
191 error=console_assign(stdin,DEVNAME);
203 /* no USB Keyboard found */
208 /* deregistering the keyboard */
209 int usb_kbd_deregister(void)
211 return device_deregister(DEVNAME);
214 /**************************************************************************
218 /* set the LEDs. Since this is used in the irq routine, the control job
219 is issued with a timeout of 0. This means, that the job is queued without
220 waiting for job completion */
222 static void usb_kbd_setled(struct usb_device *dev)
224 struct usb_interface_descriptor *iface;
225 iface = &dev->config.if_desc[0];
235 usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
236 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
237 0x200, iface->bInterfaceNumber,(void *)&leds, 1, 0);
242 #define CAPITAL_MASK 0x20
243 /* Translate the scancode in ASCII */
244 static int usb_kbd_translate(unsigned char scancode,unsigned char modifier,int pressed)
246 unsigned char keycode;
255 if(repeat_delay<REPEAT_DELAY)
257 repeat_delay=REPEAT_DELAY;
260 if((scancode>3) && (scancode<0x1d)) { /* alpha numeric values */
261 keycode=scancode-4 + 0x61;
263 keycode&=~CAPITAL_MASK; /* switch to capital Letters */
264 if(((modifier&(1<<LEFT_SHIFT))!=0)||((modifier&(1<<RIGHT_SHIFT))!=0)) {
265 if(keycode & CAPITAL_MASK)
266 keycode&=~CAPITAL_MASK; /* switch to capital Letters */
268 keycode|=CAPITAL_MASK; /* switch to non capital Letters */
271 if((scancode>0x1d) && (scancode<0x3A)) {
272 if(((modifier&(1<<LEFT_SHIFT))!=0)||((modifier&(1<<RIGHT_SHIFT))!=0)) /* shifted */
273 keycode=usb_kbd_numkey_shifted[scancode-0x1e];
274 else /* non shifted */
275 keycode=usb_kbd_numkey[scancode-0x1e];
278 if(scancode==NUM_LOCK) {
282 if(scancode==CAPS_LOCK) {
283 caps_lock=~caps_lock;
286 if(scancode==SCROLL_LOCK) {
287 scroll_lock=~scroll_lock;
292 USB_KBD_PRINTF("%c",keycode);
293 usb_kbd_put_queue(keycode);
298 /* Interrupt service routine */
299 static int usb_kbd_irq(struct usb_device *dev)
303 if((dev->irq_status!=0)||(dev->irq_act_len!=8))
305 USB_KBD_PRINTF("usb_keyboard Error %lX, len %d\n",dev->irq_status,dev->irq_act_len);
309 for (i = 2; i < 8; i++) {
310 if (old[i] > 3 && memscan(&new[2], old[i], 6) == &new[8]) {
311 res|=usb_kbd_translate(old[i],new[0],0);
313 if (new[i] > 3 && memscan(&old[2], new[i], 6) == &old[8]) {
314 res|=usb_kbd_translate(new[i],new[0],1);
317 if((new[2]>3) && (old[2]==new[2])) /* still pressed */
318 res|=usb_kbd_translate(new[2],new[0],2);
321 memcpy(&old[0],&new[0], 8);
322 return 1; /* install IRQ Handler again */
325 /* probes the USB device dev for keyboard type */
326 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
328 struct usb_interface_descriptor *iface;
329 struct usb_endpoint_descriptor *ep;
332 if (dev->descriptor.bNumConfigurations != 1) return 0;
333 iface = &dev->config.if_desc[ifnum];
335 if (iface->bInterfaceClass != 3) return 0;
336 if (iface->bInterfaceSubClass != 1) return 0;
337 if (iface->bInterfaceProtocol != 1) return 0;
338 if (iface->bNumEndpoints != 1) return 0;
340 ep = &iface->ep_desc[0];
342 if (!(ep->bEndpointAddress & 0x80)) return 0;
343 if ((ep->bmAttributes & 3) != 3) return 0;
344 USB_KBD_PRINTF("USB KBD found set protocol...\n");
345 /* ok, we found a USB Keyboard, install it */
346 /* usb_kbd_get_hid_desc(dev); */
347 usb_set_protocol(dev, iface->bInterfaceNumber, 0);
348 USB_KBD_PRINTF("USB KBD found set idle...\n");
349 usb_set_idle(dev, iface->bInterfaceNumber, REPEAT_RATE, 0);
350 memset(&new[0], 0, 8);
351 memset(&old[0], 0, 8);
353 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
354 maxp = usb_maxpacket(dev, pipe);
355 dev->irq_handle=usb_kbd_irq;
356 USB_KBD_PRINTF("USB KBD enable interrupt pipe...\n");
357 usb_submit_int_msg(dev,pipe,&new[0], maxp > 8 ? 8 : maxp,ep->bInterval);
363 struct usb_hid_descriptor {
364 unsigned char bLength;
365 unsigned char bDescriptorType; /* 0x21 for HID */
366 unsigned short bcdHID; /* release number */
367 unsigned char bCountryCode;
368 unsigned char bNumDescriptors;
369 unsigned char bReportDescriptorType;
370 unsigned short wDescriptorLength;
371 } __attribute__ ((packed));
374 * We parse each description item into this structure. Short items data
375 * values are expanded to 32-bit signed int, long items contain a pointer
376 * into the data area.
380 unsigned char format;
391 unsigned char *longdata;
396 * HID report item format
399 #define HID_ITEM_FORMAT_SHORT 0
400 #define HID_ITEM_FORMAT_LONG 1
403 * Special tag indicating long items
406 #define HID_ITEM_TAG_LONG 15
409 static struct usb_hid_descriptor usb_kbd_hid_desc;
411 void usb_kbd_display_hid(struct usb_hid_descriptor *hid)
413 printf("USB_HID_DESC:\n");
414 printf(" bLenght 0x%x\n",hid->bLength);
415 printf(" bcdHID 0x%x\n",hid->bcdHID);
416 printf(" bCountryCode %d\n",hid->bCountryCode);
417 printf(" bNumDescriptors 0x%x\n",hid->bNumDescriptors);
418 printf(" bReportDescriptorType 0x%x\n",hid->bReportDescriptorType);
419 printf(" wDescriptorLength 0x%x\n",hid->wDescriptorLength);
424 * Fetch a report description item from the data stream. We support long
425 * items, though they are not used yet.
428 static int fetch_item(unsigned char *start,unsigned char *end, struct hid_item *item)
430 if((end - start) > 0) {
431 unsigned char b = *start++;
432 item->type = (b >> 2) & 3;
433 item->tag = (b >> 4) & 15;
434 if (item->tag == HID_ITEM_TAG_LONG) {
435 item->format = HID_ITEM_FORMAT_LONG;
436 if ((end - start) >= 2) {
437 item->size = *start++;
438 item->tag = *start++;
439 if ((end - start) >= item->size) {
440 item->data.longdata = start;
446 item->format = HID_ITEM_FORMAT_SHORT;
448 switch (item->size) {
452 if ((end - start) >= 1) {
453 item->data.u8 = *start++;
458 if ((end - start) >= 2) {
459 item->data.u16 = swap_16((unsigned short *)start);
465 if ((end - start) >= 4) {
466 item->data.u32 = swap_32((unsigned long *)start);
477 * HID report descriptor item type (prefix bit 2,3)
480 #define HID_ITEM_TYPE_MAIN 0
481 #define HID_ITEM_TYPE_GLOBAL 1
482 #define HID_ITEM_TYPE_LOCAL 2
483 #define HID_ITEM_TYPE_RESERVED 3
485 * HID report descriptor main item tags
488 #define HID_MAIN_ITEM_TAG_INPUT 8
489 #define HID_MAIN_ITEM_TAG_OUTPUT 9
490 #define HID_MAIN_ITEM_TAG_FEATURE 11
491 #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION 10
492 #define HID_MAIN_ITEM_TAG_END_COLLECTION 12
494 * HID report descriptor main item contents
497 #define HID_MAIN_ITEM_CONSTANT 0x001
498 #define HID_MAIN_ITEM_VARIABLE 0x002
499 #define HID_MAIN_ITEM_RELATIVE 0x004
500 #define HID_MAIN_ITEM_WRAP 0x008
501 #define HID_MAIN_ITEM_NONLINEAR 0x010
502 #define HID_MAIN_ITEM_NO_PREFERRED 0x020
503 #define HID_MAIN_ITEM_NULL_STATE 0x040
504 #define HID_MAIN_ITEM_VOLATILE 0x080
505 #define HID_MAIN_ITEM_BUFFERED_BYTE 0x100
508 * HID report descriptor collection item types
511 #define HID_COLLECTION_PHYSICAL 0
512 #define HID_COLLECTION_APPLICATION 1
513 #define HID_COLLECTION_LOGICAL 2
515 * HID report descriptor global item tags
518 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0
519 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM 1
520 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM 2
521 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM 3
522 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM 4
523 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 5
524 #define HID_GLOBAL_ITEM_TAG_UNIT 6
525 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 7
526 #define HID_GLOBAL_ITEM_TAG_REPORT_ID 8
527 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 9
528 #define HID_GLOBAL_ITEM_TAG_PUSH 10
529 #define HID_GLOBAL_ITEM_TAG_POP 11
532 * HID report descriptor local item tags
535 #define HID_LOCAL_ITEM_TAG_USAGE 0
536 #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM 1
537 #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM 2
538 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 3
539 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM 4
540 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM 5
541 #define HID_LOCAL_ITEM_TAG_STRING_INDEX 7
542 #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM 8
543 #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM 9
544 #define HID_LOCAL_ITEM_TAG_DELIMITER 10
547 static void usb_kbd_show_item(struct hid_item *item)
550 case HID_ITEM_TYPE_MAIN:
552 case HID_MAIN_ITEM_TAG_INPUT:
553 printf("Main Input");
555 case HID_MAIN_ITEM_TAG_OUTPUT:
556 printf("Main Output");
558 case HID_MAIN_ITEM_TAG_FEATURE:
559 printf("Main Feature");
561 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
562 printf("Main Begin Collection");
564 case HID_MAIN_ITEM_TAG_END_COLLECTION:
565 printf("Main End Collection");
568 printf("Main reserved %d",item->tag);
572 case HID_ITEM_TYPE_GLOBAL:
574 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
575 printf("- Global Usage Page");
577 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
578 printf("- Global Logical Minimum");
580 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
581 printf("- Global Logical Maximum");
583 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
584 printf("- Global physical Minimum");
586 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
587 printf("- Global physical Maximum");
589 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
590 printf("- Global Unit Exponent");
592 case HID_GLOBAL_ITEM_TAG_UNIT:
593 printf("- Global Unit");
595 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
596 printf("- Global Report Size");
598 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
599 printf("- Global Report ID");
601 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
602 printf("- Global Report Count");
604 case HID_GLOBAL_ITEM_TAG_PUSH:
605 printf("- Global Push");
607 case HID_GLOBAL_ITEM_TAG_POP:
608 printf("- Global Pop");
611 printf("- Global reserved %d",item->tag);
615 case HID_ITEM_TYPE_LOCAL:
617 case HID_LOCAL_ITEM_TAG_USAGE:
618 printf("-- Local Usage");
620 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
621 printf("-- Local Usage Minimum");
623 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
624 printf("-- Local Usage Maximum");
626 case HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX:
627 printf("-- Local Designator Index");
629 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM:
630 printf("-- Local Designator Minimum");
632 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM:
633 printf("-- Local Designator Maximum");
635 case HID_LOCAL_ITEM_TAG_STRING_INDEX:
636 printf("-- Local String Index");
638 case HID_LOCAL_ITEM_TAG_STRING_MINIMUM:
639 printf("-- Local String Minimum");
641 case HID_LOCAL_ITEM_TAG_STRING_MAXIMUM:
642 printf("-- Local String Maximum");
644 case HID_LOCAL_ITEM_TAG_DELIMITER:
645 printf("-- Local Delimiter");
648 printf("-- Local reserved %d",item->tag);
653 printf("--- reserved %d",item->type);
658 printf(" %d",item->data.u8);
661 printf(" %d",item->data.u16);
664 printf(" %ld",item->data.u32);
671 static int usb_kbd_get_hid_desc(struct usb_device *dev)
673 unsigned char buffer[256];
674 struct usb_descriptor_header *head;
675 struct usb_config_descriptor *config;
677 unsigned char *start, *end;
678 struct hid_item item;
680 if(usb_get_configuration_no(dev,&buffer[0],0)==-1)
682 head =(struct usb_descriptor_header *)&buffer[0];
683 if(head->bDescriptorType!=USB_DT_CONFIG) {
684 printf(" ERROR: NOT USB_CONFIG_DESC %x\n",head->bDescriptorType);
688 config=(struct usb_config_descriptor *)&buffer[0];
689 len=swap_16(config->wTotalLength);
690 /* Ok the first entry must be a configuration entry, now process the others */
691 head=(struct usb_descriptor_header *)&buffer[index];
692 while(index+1 < len) {
693 if(head->bDescriptorType==USB_DT_HID) {
694 printf("HID desc found\n");
695 memcpy(&usb_kbd_hid_desc,&buffer[index],buffer[index]);
696 usb_kbd_hid_desc.bcdHID=swap_16(usb_kbd_hid_desc.bcdHID);
697 usb_kbd_hid_desc.wDescriptorLength=swap_16(usb_kbd_hid_desc.wDescriptorLength);
698 usb_kbd_display_hid(&usb_kbd_hid_desc);
702 index+=head->bLength;
703 head=(struct usb_descriptor_header *)&buffer[index];
707 len=usb_kbd_hid_desc.wDescriptorLength;
708 if((index = usb_get_class_descriptor(dev, 0, USB_DT_REPORT, 0, &buffer[0], len)) < 0) {
709 printf("reading report descriptor failed\n");
712 printf(" report descriptor (size %u, read %d)\n", len, index);
717 index=fetch_item(start,end,&item);
721 usb_kbd_show_item(&item);
732 #endif /* CONFIG_USB_KEYBOARD */