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 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 CONFIG_SYS_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 CONFIG_SYS_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 struct stdio_dev 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 */
167 if(dev
->devnum
!=-1) {
168 if(usb_kbd_probe(dev
,0)==1) { /* Ok, we found a keyboard */
169 /* check, if it is already registered */
170 USB_KBD_PRINTF("USB KBD found set up device.\n");
171 old_dev
= stdio_get_by_name(DEVNAME
);
173 /* ok, already registered, just return ok */
174 USB_KBD_PRINTF("USB KBD is already registered.\n");
177 /* register the keyboard */
178 USB_KBD_PRINTF("USB KBD register.\n");
179 memset (&usb_kbd_dev
, 0, sizeof(struct stdio_dev
));
180 strcpy(usb_kbd_dev
.name
, DEVNAME
);
181 usb_kbd_dev
.flags
= DEV_FLAGS_INPUT
| DEV_FLAGS_SYSTEM
;
182 usb_kbd_dev
.putc
= NULL
;
183 usb_kbd_dev
.puts
= NULL
;
184 usb_kbd_dev
.getc
= usb_kbd_getc
;
185 usb_kbd_dev
.tstc
= usb_kbd_testc
;
186 usb_kbd_dev
.priv
= (void *)dev
;
187 error
= stdio_register (&usb_kbd_dev
);
189 /* check if this is the standard input device */
190 if(strcmp(stdinname
,DEVNAME
)==0) {
191 /* reassign the console */
192 if(overwrite_console()) {
195 error
=console_assign(stdin
,DEVNAME
);
207 /* no USB Keyboard found */
212 /* deregistering the keyboard */
213 int usb_kbd_deregister(void)
215 #ifdef CONFIG_SYS_STDIO_DEREGISTER
216 return stdio_deregister(DEVNAME
);
222 /**************************************************************************
226 /* set the LEDs. Since this is used in the irq routine, the control job
227 is issued with a timeout of 0. This means, that the job is queued without
228 waiting for job completion */
230 static void usb_kbd_setled(struct usb_device
*dev
)
232 struct usb_interface
*iface
;
233 iface
= &dev
->config
.if_desc
[0];
243 usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
244 USB_REQ_SET_REPORT
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
245 0x200, iface
->desc
.bInterfaceNumber
, (void *)&leds
, 1, 0);
250 #define CAPITAL_MASK 0x20
251 /* Translate the scancode in ASCII */
252 static int usb_kbd_translate(unsigned char scancode
,unsigned char modifier
,int pressed
)
254 unsigned char keycode
;
263 if(repeat_delay
<REPEAT_DELAY
)
265 repeat_delay
=REPEAT_DELAY
;
268 if((scancode
>3) && (scancode
<=0x1d)) { /* alpha numeric values */
269 keycode
=scancode
-4 + 0x61;
271 keycode
&=~CAPITAL_MASK
; /* switch to capital Letters */
272 if(((modifier
&(1<<LEFT_SHIFT
))!=0)||((modifier
&(1<<RIGHT_SHIFT
))!=0)) {
273 if(keycode
& CAPITAL_MASK
)
274 keycode
&=~CAPITAL_MASK
; /* switch to capital Letters */
276 keycode
|=CAPITAL_MASK
; /* switch to non capital Letters */
279 if((scancode
>0x1d) && (scancode
<0x3A)) {
280 if(((modifier
&(1<<LEFT_SHIFT
))!=0)||((modifier
&(1<<RIGHT_SHIFT
))!=0)) /* shifted */
281 keycode
=usb_kbd_numkey_shifted
[scancode
-0x1e];
282 else /* non shifted */
283 keycode
=usb_kbd_numkey
[scancode
-0x1e];
287 keycode
= scancode
- 0x3;
290 if(scancode
==NUM_LOCK
) {
294 if(scancode
==CAPS_LOCK
) {
295 caps_lock
=~caps_lock
;
298 if(scancode
==SCROLL_LOCK
) {
299 scroll_lock
=~scroll_lock
;
304 USB_KBD_PRINTF("%c",keycode
);
305 usb_kbd_put_queue(keycode
);
310 /* Interrupt service routine */
311 static int usb_kbd_irq(struct usb_device
*dev
)
315 if((dev
->irq_status
!=0)||(dev
->irq_act_len
!=8))
317 USB_KBD_PRINTF("usb_keyboard Error %lX, len %d\n",dev
->irq_status
,dev
->irq_act_len
);
323 case 0x0: /* No combo key pressed */
326 case 0x01: /* Left Ctrl pressed */
327 case 0x10: /* Right Ctrl pressed */
332 for (i
= 2; i
< 8; i
++) {
333 if (old
[i
] > 3 && memscan(&new[2], old
[i
], 6) == &new[8]) {
334 res
|=usb_kbd_translate(old
[i
],new[0],0);
336 if (new[i
] > 3 && memscan(&old
[2], new[i
], 6) == &old
[8]) {
337 res
|=usb_kbd_translate(new[i
],new[0],1);
340 if((new[2]>3) && (old
[2]==new[2])) /* still pressed */
341 res
|=usb_kbd_translate(new[2],new[0],2);
344 memcpy(&old
[0],&new[0], 8);
345 return 1; /* install IRQ Handler again */
348 /* probes the USB device dev for keyboard type */
349 static int usb_kbd_probe(struct usb_device
*dev
, unsigned int ifnum
)
351 struct usb_interface
*iface
;
352 struct usb_endpoint_descriptor
*ep
;
355 if (dev
->descriptor
.bNumConfigurations
!= 1) return 0;
356 iface
= &dev
->config
.if_desc
[ifnum
];
358 if (iface
->desc
.bInterfaceClass
!= 3)
360 if (iface
->desc
.bInterfaceSubClass
!= 1)
362 if (iface
->desc
.bInterfaceProtocol
!= 1)
364 if (iface
->desc
.bNumEndpoints
!= 1)
367 ep
= &iface
->ep_desc
[0];
369 if (!(ep
->bEndpointAddress
& 0x80)) return 0;
370 if ((ep
->bmAttributes
& 3) != 3) return 0;
371 USB_KBD_PRINTF("USB KBD found set protocol...\n");
372 /* ok, we found a USB Keyboard, install it */
373 /* usb_kbd_get_hid_desc(dev); */
374 usb_set_protocol(dev
, iface
->desc
.bInterfaceNumber
, 0);
375 USB_KBD_PRINTF("USB KBD found set idle...\n");
376 usb_set_idle(dev
, iface
->desc
.bInterfaceNumber
, REPEAT_RATE
, 0);
377 memset(&new[0], 0, 8);
378 memset(&old
[0], 0, 8);
380 pipe
= usb_rcvintpipe(dev
, ep
->bEndpointAddress
);
381 maxp
= usb_maxpacket(dev
, pipe
);
382 dev
->irq_handle
=usb_kbd_irq
;
383 USB_KBD_PRINTF("USB KBD enable interrupt pipe...\n");
384 usb_submit_int_msg(dev
,pipe
,&new[0], maxp
> 8 ? 8 : maxp
,ep
->bInterval
);
390 struct usb_hid_descriptor
{
391 unsigned char bLength
;
392 unsigned char bDescriptorType
; /* 0x21 for HID */
393 unsigned short bcdHID
; /* release number */
394 unsigned char bCountryCode
;
395 unsigned char bNumDescriptors
;
396 unsigned char bReportDescriptorType
;
397 unsigned short wDescriptorLength
;
398 } __attribute__ ((packed
));
401 * We parse each description item into this structure. Short items data
402 * values are expanded to 32-bit signed int, long items contain a pointer
403 * into the data area.
407 unsigned char format
;
418 unsigned char *longdata
;
423 * HID report item format
426 #define HID_ITEM_FORMAT_SHORT 0
427 #define HID_ITEM_FORMAT_LONG 1
430 * Special tag indicating long items
433 #define HID_ITEM_TAG_LONG 15
436 static struct usb_hid_descriptor usb_kbd_hid_desc
;
438 void usb_kbd_display_hid(struct usb_hid_descriptor
*hid
)
440 printf("USB_HID_DESC:\n");
441 printf(" bLenght 0x%x\n",hid
->bLength
);
442 printf(" bcdHID 0x%x\n",hid
->bcdHID
);
443 printf(" bCountryCode %d\n",hid
->bCountryCode
);
444 printf(" bNumDescriptors 0x%x\n",hid
->bNumDescriptors
);
445 printf(" bReportDescriptorType 0x%x\n",hid
->bReportDescriptorType
);
446 printf(" wDescriptorLength 0x%x\n",hid
->wDescriptorLength
);
451 * Fetch a report description item from the data stream. We support long
452 * items, though they are not used yet.
455 static int fetch_item(unsigned char *start
,unsigned char *end
, struct hid_item
*item
)
457 if((end
- start
) > 0) {
458 unsigned char b
= *start
++;
459 item
->type
= (b
>> 2) & 3;
460 item
->tag
= (b
>> 4) & 15;
461 if (item
->tag
== HID_ITEM_TAG_LONG
) {
462 item
->format
= HID_ITEM_FORMAT_LONG
;
463 if ((end
- start
) >= 2) {
464 item
->size
= *start
++;
465 item
->tag
= *start
++;
466 if ((end
- start
) >= item
->size
) {
467 item
->data
.longdata
= start
;
473 item
->format
= HID_ITEM_FORMAT_SHORT
;
475 switch (item
->size
) {
479 if ((end
- start
) >= 1) {
480 item
->data
.u8
= *start
++;
485 if ((end
- start
) >= 2) {
486 item
->data
.u16
= le16_to_cpu((unsigned short *)start
);
492 if ((end
- start
) >= 4) {
493 item
->data
.u32
= le32_to_cpu((unsigned long *)start
);
504 * HID report descriptor item type (prefix bit 2,3)
507 #define HID_ITEM_TYPE_MAIN 0
508 #define HID_ITEM_TYPE_GLOBAL 1
509 #define HID_ITEM_TYPE_LOCAL 2
510 #define HID_ITEM_TYPE_RESERVED 3
512 * HID report descriptor main item tags
515 #define HID_MAIN_ITEM_TAG_INPUT 8
516 #define HID_MAIN_ITEM_TAG_OUTPUT 9
517 #define HID_MAIN_ITEM_TAG_FEATURE 11
518 #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION 10
519 #define HID_MAIN_ITEM_TAG_END_COLLECTION 12
521 * HID report descriptor main item contents
524 #define HID_MAIN_ITEM_CONSTANT 0x001
525 #define HID_MAIN_ITEM_VARIABLE 0x002
526 #define HID_MAIN_ITEM_RELATIVE 0x004
527 #define HID_MAIN_ITEM_WRAP 0x008
528 #define HID_MAIN_ITEM_NONLINEAR 0x010
529 #define HID_MAIN_ITEM_NO_PREFERRED 0x020
530 #define HID_MAIN_ITEM_NULL_STATE 0x040
531 #define HID_MAIN_ITEM_VOLATILE 0x080
532 #define HID_MAIN_ITEM_BUFFERED_BYTE 0x100
535 * HID report descriptor collection item types
538 #define HID_COLLECTION_PHYSICAL 0
539 #define HID_COLLECTION_APPLICATION 1
540 #define HID_COLLECTION_LOGICAL 2
542 * HID report descriptor global item tags
545 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0
546 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM 1
547 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM 2
548 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM 3
549 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM 4
550 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 5
551 #define HID_GLOBAL_ITEM_TAG_UNIT 6
552 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 7
553 #define HID_GLOBAL_ITEM_TAG_REPORT_ID 8
554 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 9
555 #define HID_GLOBAL_ITEM_TAG_PUSH 10
556 #define HID_GLOBAL_ITEM_TAG_POP 11
559 * HID report descriptor local item tags
562 #define HID_LOCAL_ITEM_TAG_USAGE 0
563 #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM 1
564 #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM 2
565 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 3
566 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM 4
567 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM 5
568 #define HID_LOCAL_ITEM_TAG_STRING_INDEX 7
569 #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM 8
570 #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM 9
571 #define HID_LOCAL_ITEM_TAG_DELIMITER 10
574 static void usb_kbd_show_item(struct hid_item
*item
)
577 case HID_ITEM_TYPE_MAIN
:
579 case HID_MAIN_ITEM_TAG_INPUT
:
580 printf("Main Input");
582 case HID_MAIN_ITEM_TAG_OUTPUT
:
583 printf("Main Output");
585 case HID_MAIN_ITEM_TAG_FEATURE
:
586 printf("Main Feature");
588 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION
:
589 printf("Main Begin Collection");
591 case HID_MAIN_ITEM_TAG_END_COLLECTION
:
592 printf("Main End Collection");
595 printf("Main reserved %d",item
->tag
);
599 case HID_ITEM_TYPE_GLOBAL
:
601 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE
:
602 printf("- Global Usage Page");
604 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM
:
605 printf("- Global Logical Minimum");
607 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM
:
608 printf("- Global Logical Maximum");
610 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM
:
611 printf("- Global physical Minimum");
613 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM
:
614 printf("- Global physical Maximum");
616 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT
:
617 printf("- Global Unit Exponent");
619 case HID_GLOBAL_ITEM_TAG_UNIT
:
620 printf("- Global Unit");
622 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE
:
623 printf("- Global Report Size");
625 case HID_GLOBAL_ITEM_TAG_REPORT_ID
:
626 printf("- Global Report ID");
628 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT
:
629 printf("- Global Report Count");
631 case HID_GLOBAL_ITEM_TAG_PUSH
:
632 printf("- Global Push");
634 case HID_GLOBAL_ITEM_TAG_POP
:
635 printf("- Global Pop");
638 printf("- Global reserved %d",item
->tag
);
642 case HID_ITEM_TYPE_LOCAL
:
644 case HID_LOCAL_ITEM_TAG_USAGE
:
645 printf("-- Local Usage");
647 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM
:
648 printf("-- Local Usage Minimum");
650 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM
:
651 printf("-- Local Usage Maximum");
653 case HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX
:
654 printf("-- Local Designator Index");
656 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM
:
657 printf("-- Local Designator Minimum");
659 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM
:
660 printf("-- Local Designator Maximum");
662 case HID_LOCAL_ITEM_TAG_STRING_INDEX
:
663 printf("-- Local String Index");
665 case HID_LOCAL_ITEM_TAG_STRING_MINIMUM
:
666 printf("-- Local String Minimum");
668 case HID_LOCAL_ITEM_TAG_STRING_MAXIMUM
:
669 printf("-- Local String Maximum");
671 case HID_LOCAL_ITEM_TAG_DELIMITER
:
672 printf("-- Local Delimiter");
675 printf("-- Local reserved %d",item
->tag
);
680 printf("--- reserved %d",item
->type
);
685 printf(" %d",item
->data
.u8
);
688 printf(" %d",item
->data
.u16
);
691 printf(" %ld",item
->data
.u32
);
698 static int usb_kbd_get_hid_desc(struct usb_device
*dev
)
700 unsigned char buffer
[256];
701 struct usb_descriptor_header
*head
;
702 struct usb_config_descriptor
*config
;
704 unsigned char *start
, *end
;
705 struct hid_item item
;
707 if(usb_get_configuration_no(dev
,&buffer
[0],0)==-1)
709 head
=(struct usb_descriptor_header
*)&buffer
[0];
710 if(head
->bDescriptorType
!=USB_DT_CONFIG
) {
711 printf(" ERROR: NOT USB_CONFIG_DESC %x\n",head
->bDescriptorType
);
715 config
=(struct usb_config_descriptor
*)&buffer
[0];
716 len
=le16_to_cpu(config
->wTotalLength
);
717 /* Ok the first entry must be a configuration entry, now process the others */
718 head
=(struct usb_descriptor_header
*)&buffer
[index
];
719 while(index
+1 < len
) {
720 if(head
->bDescriptorType
==USB_DT_HID
) {
721 printf("HID desc found\n");
722 memcpy(&usb_kbd_hid_desc
,&buffer
[index
],buffer
[index
]);
723 le16_to_cpus(&usb_kbd_hid_desc
.bcdHID
);
724 le16_to_cpus(&usb_kbd_hid_desc
.wDescriptorLength
);
725 usb_kbd_display_hid(&usb_kbd_hid_desc
);
729 index
+=head
->bLength
;
730 head
=(struct usb_descriptor_header
*)&buffer
[index
];
734 len
=usb_kbd_hid_desc
.wDescriptorLength
;
735 if((index
= usb_get_class_descriptor(dev
, 0, USB_DT_REPORT
, 0, &buffer
[0], len
)) < 0) {
736 printf("reading report descriptor failed\n");
739 printf(" report descriptor (size %u, read %d)\n", len
, index
);
744 index
=fetch_item(start
,end
,&item
);
748 usb_kbd_show_item(&item
);