xref: /openbmc/u-boot/common/usb_kbd.c (revision c8c845cf)
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 <devices.h>
29 #include <asm/byteorder.h>
30 
31 #ifdef CONFIG_USB_KEYBOARD
32 
33 #include <usb.h>
34 
35 #undef USB_KBD_DEBUG
36 /*
37  * if overwrite_console returns 1, the stdin, stderr and stdout
38  * are switched to the serial port, else the settings in the
39  * environment are used
40  */
41 #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
42 extern int overwrite_console (void);
43 #else
44 int overwrite_console (void)
45 {
46 	return (0);
47 }
48 #endif
49 
50 #ifdef	USB_KBD_DEBUG
51 #define	USB_KBD_PRINTF(fmt,args...)	printf (fmt ,##args)
52 #else
53 #define USB_KBD_PRINTF(fmt,args...)
54 #endif
55 
56 
57 #define REPEAT_RATE  40/4 /* 40msec -> 25cps */
58 #define REPEAT_DELAY 10 /* 10 x REAPEAT_RATE = 400msec */
59 
60 #define NUM_LOCK	0x53
61 #define CAPS_LOCK 0x39
62 #define SCROLL_LOCK 0x47
63 
64 
65 /* Modifier bits */
66 #define LEFT_CNTR		0
67 #define LEFT_SHIFT	1
68 #define LEFT_ALT		2
69 #define LEFT_GUI		3
70 #define RIGHT_CNTR	4
71 #define RIGHT_SHIFT	5
72 #define RIGHT_ALT		6
73 #define RIGHT_GUI		7
74 
75 #define USB_KBD_BUFFER_LEN 0x20  /* size of the keyboardbuffer */
76 
77 static volatile char usb_kbd_buffer[USB_KBD_BUFFER_LEN];
78 static volatile int usb_in_pointer = 0;
79 static volatile int usb_out_pointer = 0;
80 
81 unsigned char new[8];
82 unsigned char old[8];
83 int repeat_delay;
84 #define DEVNAME "usbkbd"
85 static unsigned char num_lock = 0;
86 static unsigned char caps_lock = 0;
87 static unsigned char scroll_lock = 0;
88 static unsigned char ctrl = 0;
89 
90 static unsigned char leds __attribute__ ((aligned (0x4)));
91 
92 static unsigned char usb_kbd_numkey[] = {
93 	 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0','\r',0x1b,'\b','\t',' ', '-',
94 	 '=', '[', ']','\\', '#', ';', '\'', '`', ',', '.', '/'
95 };
96 static unsigned char usb_kbd_numkey_shifted[] = {
97 	 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')','\r',0x1b,'\b','\t',' ', '_',
98 	 '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?'
99 };
100 
101 /******************************************************************
102  * Queue handling
103  ******************************************************************/
104 /* puts character in the queue and sets up the in and out pointer */
105 static void usb_kbd_put_queue(char data)
106 {
107 	if((usb_in_pointer+1)==USB_KBD_BUFFER_LEN) {
108 		if(usb_out_pointer==0) {
109 			return; /* buffer full */
110 		} else{
111 			usb_in_pointer=0;
112 		}
113 	} else {
114 		if((usb_in_pointer+1)==usb_out_pointer)
115 			return; /* buffer full */
116 		usb_in_pointer++;
117 	}
118 	usb_kbd_buffer[usb_in_pointer]=data;
119 	return;
120 }
121 
122 /* test if a character is in the queue */
123 static int usb_kbd_testc(void)
124 {
125 #ifdef CFG_USB_EVENT_POLL
126 	usb_event_poll();
127 #endif
128 	if(usb_in_pointer==usb_out_pointer)
129 		return(0); /* no data */
130 	else
131 		return(1);
132 }
133 /* gets the character from the queue */
134 static int usb_kbd_getc(void)
135 {
136 	char c;
137 	while(usb_in_pointer==usb_out_pointer) {
138 #ifdef CFG_USB_EVENT_POLL
139 		usb_event_poll();
140 #endif
141 	}
142 	if((usb_out_pointer+1)==USB_KBD_BUFFER_LEN)
143 		usb_out_pointer=0;
144 	else
145 		usb_out_pointer++;
146 	c=usb_kbd_buffer[usb_out_pointer];
147 	return (int)c;
148 
149 }
150 
151 /* forward decleration */
152 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum);
153 
154 /* search for keyboard and register it if found */
155 int drv_usb_kbd_init(void)
156 {
157 	int error,i,index;
158 	device_t usb_kbd_dev,*old_dev;
159 	struct usb_device *dev;
160 	char *stdinname  = getenv ("stdin");
161 
162 	usb_in_pointer=0;
163 	usb_out_pointer=0;
164 	/* scan all USB Devices */
165 	for(i=0;i<USB_MAX_DEVICE;i++) {
166 		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 				for (index=1; index<=ListNumItems(devlist); index++) {
172 					old_dev = ListGetPtrToItem(devlist, index);
173 					if(strcmp(old_dev->name,DEVNAME)==0) {
174 						/* ok, already registered, just return ok */
175 						USB_KBD_PRINTF("USB KBD is already registered.\n");
176 						return 1;
177 					}
178 				}
179 				/* register the keyboard */
180 				USB_KBD_PRINTF("USB KBD register.\n");
181 				memset (&usb_kbd_dev, 0, sizeof(device_t));
182 				strcpy(usb_kbd_dev.name, DEVNAME);
183 				usb_kbd_dev.flags =  DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
184 				usb_kbd_dev.putc = NULL;
185 				usb_kbd_dev.puts = NULL;
186 				usb_kbd_dev.getc = usb_kbd_getc;
187 				usb_kbd_dev.tstc = usb_kbd_testc;
188 				error = device_register (&usb_kbd_dev);
189 				if(error==0) {
190 					/* check if this is the standard input device */
191 					if(strcmp(stdinname,DEVNAME)==0) {
192 						/* reassign the console */
193 						if(overwrite_console()) {
194 							return 1;
195 						}
196 						error=console_assign(stdin,DEVNAME);
197 						if(error==0)
198 							return 1;
199 						else
200 							return error;
201 					}
202 					return 1;
203 				}
204 				return error;
205 			}
206 		}
207 	}
208 	/* no USB Keyboard found */
209 	return -1;
210 }
211 
212 
213 /* deregistering the keyboard */
214 int usb_kbd_deregister(void)
215 {
216 	return device_deregister(DEVNAME);
217 }
218 
219 /**************************************************************************
220  * Low Level drivers
221  */
222 
223 /* set the LEDs. Since this is used in the irq routine, the control job
224    is issued with a timeout of 0. This means, that the job is queued without
225    waiting for job completion */
226 
227 static void usb_kbd_setled(struct usb_device *dev)
228 {
229 	struct usb_interface_descriptor *iface;
230 	iface = &dev->config.if_desc[0];
231 	leds=0;
232 	if(scroll_lock!=0)
233 		leds|=1;
234 	leds<<=1;
235 	if(caps_lock!=0)
236 		leds|=1;
237 	leds<<=1;
238 	if(num_lock!=0)
239 		leds|=1;
240 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
241 		USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
242 		0x200, iface->bInterfaceNumber,(void *)&leds, 1, 0);
243 
244 }
245 
246 
247 #define CAPITAL_MASK 0x20
248 /* Translate the scancode in ASCII */
249 static int usb_kbd_translate(unsigned char scancode,unsigned char modifier,int pressed)
250 {
251 	unsigned char keycode;
252 
253 	if(pressed==0) {
254 		/* key released */
255 		repeat_delay=0;
256 		return 0;
257 	}
258 	if(pressed==2) {
259 		repeat_delay++;
260 		if(repeat_delay<REPEAT_DELAY)
261 			return 0;
262 		repeat_delay=REPEAT_DELAY;
263 	}
264 	keycode=0;
265 	if((scancode>3) && (scancode<=0x1d)) { /* alpha numeric values */
266 		keycode=scancode-4 + 0x61;
267 		if(caps_lock)
268 			keycode&=~CAPITAL_MASK; /* switch to capital Letters */
269 		if(((modifier&(1<<LEFT_SHIFT))!=0)||((modifier&(1<<RIGHT_SHIFT))!=0)) {
270 			if(keycode & CAPITAL_MASK)
271 				keycode&=~CAPITAL_MASK; /* switch to capital Letters */
272 			else
273 				keycode|=CAPITAL_MASK; /* switch to non capital Letters */
274 		}
275 	}
276 	if((scancode>0x1d) && (scancode<0x3A)) {
277 		if(((modifier&(1<<LEFT_SHIFT))!=0)||((modifier&(1<<RIGHT_SHIFT))!=0))  /* shifted */
278 			keycode=usb_kbd_numkey_shifted[scancode-0x1e];
279 		else /* non shifted */
280 			keycode=usb_kbd_numkey[scancode-0x1e];
281 	}
282 
283 	if (ctrl)
284 		keycode = scancode - 0x3;
285 
286 	if(pressed==1) {
287 		if(scancode==NUM_LOCK) {
288 			num_lock=~num_lock;
289 			return 1;
290 		}
291 		if(scancode==CAPS_LOCK) {
292 			caps_lock=~caps_lock;
293 			return 1;
294 		}
295 		if(scancode==SCROLL_LOCK) {
296 			scroll_lock=~scroll_lock;
297 			return 1;
298 		}
299 	}
300 	if(keycode!=0) {
301 		USB_KBD_PRINTF("%c",keycode);
302 		usb_kbd_put_queue(keycode);
303 	}
304 	return 0;
305 }
306 
307 /* Interrupt service routine */
308 static int usb_kbd_irq(struct usb_device *dev)
309 {
310 	int i,res;
311 
312 	if((dev->irq_status!=0)||(dev->irq_act_len!=8))
313 	{
314 		USB_KBD_PRINTF("usb_keyboard Error %lX, len %d\n",dev->irq_status,dev->irq_act_len);
315 		return 1;
316 	}
317 	res=0;
318 
319 	switch (new[0]) {
320 	case 0x0:	/* No combo key pressed */
321 		ctrl = 0;
322 		break;
323 	case 0x01:	/* Left Ctrl pressed */
324 	case 0x10:	/* Right Ctrl pressed */
325 		ctrl = 1;
326 		break;
327 	}
328 
329 	for (i = 2; i < 8; i++) {
330 		if (old[i] > 3 && memscan(&new[2], old[i], 6) == &new[8]) {
331 			res|=usb_kbd_translate(old[i],new[0],0);
332 		}
333 		if (new[i] > 3 && memscan(&old[2], new[i], 6) == &old[8]) {
334 			res|=usb_kbd_translate(new[i],new[0],1);
335 		}
336 	}
337 	if((new[2]>3) && (old[2]==new[2])) /* still pressed */
338 		res|=usb_kbd_translate(new[2],new[0],2);
339 	if(res==1)
340 		usb_kbd_setled(dev);
341 	memcpy(&old[0],&new[0], 8);
342 	return 1; /* install IRQ Handler again */
343 }
344 
345 /* probes the USB device dev for keyboard type */
346 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
347 {
348 	struct usb_interface_descriptor *iface;
349 	struct usb_endpoint_descriptor *ep;
350 	int pipe,maxp;
351 
352 	if (dev->descriptor.bNumConfigurations != 1) return 0;
353 	iface = &dev->config.if_desc[ifnum];
354 
355 	if (iface->bInterfaceClass != 3) return 0;
356 	if (iface->bInterfaceSubClass != 1) return 0;
357 	if (iface->bInterfaceProtocol != 1) return 0;
358 	if (iface->bNumEndpoints != 1) return 0;
359 
360 	ep = &iface->ep_desc[0];
361 
362 	if (!(ep->bEndpointAddress & 0x80)) return 0;
363 	if ((ep->bmAttributes & 3) != 3) return 0;
364 	USB_KBD_PRINTF("USB KBD found set protocol...\n");
365 	/* ok, we found a USB Keyboard, install it */
366 	/* usb_kbd_get_hid_desc(dev); */
367 	usb_set_protocol(dev, iface->bInterfaceNumber, 0);
368 	USB_KBD_PRINTF("USB KBD found set idle...\n");
369 	usb_set_idle(dev, iface->bInterfaceNumber, REPEAT_RATE, 0);
370 	memset(&new[0], 0, 8);
371 	memset(&old[0], 0, 8);
372 	repeat_delay=0;
373 	pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
374 	maxp = usb_maxpacket(dev, pipe);
375 	dev->irq_handle=usb_kbd_irq;
376 	USB_KBD_PRINTF("USB KBD enable interrupt pipe...\n");
377 	usb_submit_int_msg(dev,pipe,&new[0], maxp > 8 ? 8 : maxp,ep->bInterval);
378 	return 1;
379 }
380 
381 
382 #if 0
383 struct usb_hid_descriptor {
384 	unsigned char  bLength;
385 	unsigned char  bDescriptorType; /* 0x21 for HID */
386 	unsigned short bcdHID; /* release number */
387 	unsigned char  bCountryCode;
388 	unsigned char  bNumDescriptors;
389 	unsigned char  bReportDescriptorType;
390 	unsigned short wDescriptorLength;
391 } __attribute__ ((packed));
392 
393 /*
394  * We parse each description item into this structure. Short items data
395  * values are expanded to 32-bit signed int, long items contain a pointer
396  * into the data area.
397  */
398 
399 struct hid_item {
400 	unsigned char format;
401 	unsigned char size;
402 	unsigned char type;
403 	unsigned char tag;
404 	union {
405 	    unsigned char   u8;
406 	    char            s8;
407 	    unsigned short  u16;
408 	    short           s16;
409 	    unsigned long   u32;
410 	    long            s32;
411 	    unsigned char  *longdata;
412 	} data;
413 };
414 
415 /*
416  * HID report item format
417  */
418 
419 #define HID_ITEM_FORMAT_SHORT	0
420 #define HID_ITEM_FORMAT_LONG	1
421 
422 /*
423  * Special tag indicating long items
424  */
425 
426 #define HID_ITEM_TAG_LONG	15
427 
428 
429 static struct usb_hid_descriptor usb_kbd_hid_desc;
430 
431 void usb_kbd_display_hid(struct usb_hid_descriptor *hid)
432 {
433 	printf("USB_HID_DESC:\n");
434 	printf("  bLenght               0x%x\n",hid->bLength);
435 	printf("  bcdHID                0x%x\n",hid->bcdHID);
436 	printf("  bCountryCode          %d\n",hid->bCountryCode);
437 	printf("  bNumDescriptors       0x%x\n",hid->bNumDescriptors);
438 	printf("  bReportDescriptorType 0x%x\n",hid->bReportDescriptorType);
439 	printf("  wDescriptorLength     0x%x\n",hid->wDescriptorLength);
440 }
441 
442 
443 /*
444  * Fetch a report description item from the data stream. We support long
445  * items, though they are not used yet.
446  */
447 
448 static int fetch_item(unsigned char *start,unsigned char *end, struct hid_item *item)
449 {
450 	if((end - start) > 0) {
451 		unsigned char b = *start++;
452 		item->type = (b >> 2) & 3;
453 		item->tag  = (b >> 4) & 15;
454 		if (item->tag == HID_ITEM_TAG_LONG) {
455 			item->format = HID_ITEM_FORMAT_LONG;
456 			if ((end - start) >= 2) {
457 				item->size = *start++;
458 				item->tag  = *start++;
459 				if ((end - start) >= item->size) {
460 					item->data.longdata = start;
461 					start += item->size;
462 					return item->size;
463 				}
464 			}
465 		} else {
466 			item->format = HID_ITEM_FORMAT_SHORT;
467 			item->size = b & 3;
468 			switch (item->size) {
469 				case 0:
470 					return item->size;
471 				case 1:
472 					if ((end - start) >= 1) {
473 						item->data.u8 = *start++;
474 						return item->size;
475 					}
476 					break;
477 				case 2:
478 					if ((end - start) >= 2) {
479 						item->data.u16 = le16_to_cpu((unsigned short *)start);
480 						start+=2;
481 						return item->size;
482 					}
483 				case 3:
484 					item->size++;
485 					if ((end - start) >= 4) {
486 						item->data.u32 = le32_to_cpu((unsigned long *)start);
487 						start+=4;
488 						return item->size;
489 					}
490 			}
491 		}
492 	}
493 	return -1;
494 }
495 
496 /*
497  * HID report descriptor item type (prefix bit 2,3)
498  */
499 
500 #define HID_ITEM_TYPE_MAIN		0
501 #define HID_ITEM_TYPE_GLOBAL		1
502 #define HID_ITEM_TYPE_LOCAL		2
503 #define HID_ITEM_TYPE_RESERVED		3
504 /*
505  * HID report descriptor main item tags
506  */
507 
508 #define HID_MAIN_ITEM_TAG_INPUT			8
509 #define HID_MAIN_ITEM_TAG_OUTPUT		9
510 #define HID_MAIN_ITEM_TAG_FEATURE		11
511 #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION	10
512 #define HID_MAIN_ITEM_TAG_END_COLLECTION	12
513 /*
514  * HID report descriptor main item contents
515  */
516 
517 #define HID_MAIN_ITEM_CONSTANT		0x001
518 #define HID_MAIN_ITEM_VARIABLE		0x002
519 #define HID_MAIN_ITEM_RELATIVE		0x004
520 #define HID_MAIN_ITEM_WRAP		0x008
521 #define HID_MAIN_ITEM_NONLINEAR		0x010
522 #define HID_MAIN_ITEM_NO_PREFERRED	0x020
523 #define HID_MAIN_ITEM_NULL_STATE	0x040
524 #define HID_MAIN_ITEM_VOLATILE		0x080
525 #define HID_MAIN_ITEM_BUFFERED_BYTE	0x100
526 
527 /*
528  * HID report descriptor collection item types
529  */
530 
531 #define HID_COLLECTION_PHYSICAL		0
532 #define HID_COLLECTION_APPLICATION	1
533 #define HID_COLLECTION_LOGICAL		2
534 /*
535  * HID report descriptor global item tags
536  */
537 
538 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE		0
539 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM	1
540 #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM	2
541 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM	3
542 #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM	4
543 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT	5
544 #define HID_GLOBAL_ITEM_TAG_UNIT		6
545 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE		7
546 #define HID_GLOBAL_ITEM_TAG_REPORT_ID		8
547 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT	9
548 #define HID_GLOBAL_ITEM_TAG_PUSH		10
549 #define HID_GLOBAL_ITEM_TAG_POP			11
550 
551 /*
552  * HID report descriptor local item tags
553  */
554 
555 #define HID_LOCAL_ITEM_TAG_USAGE		0
556 #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM	1
557 #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM	2
558 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX	3
559 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM	4
560 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM	5
561 #define HID_LOCAL_ITEM_TAG_STRING_INDEX		7
562 #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM	8
563 #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM	9
564 #define HID_LOCAL_ITEM_TAG_DELIMITER		10
565 
566 
567 static void usb_kbd_show_item(struct hid_item *item)
568 {
569 	switch(item->type) {
570 		case HID_ITEM_TYPE_MAIN:
571 			switch(item->tag) {
572 				case HID_MAIN_ITEM_TAG_INPUT:
573 					printf("Main Input");
574 					break;
575 				case HID_MAIN_ITEM_TAG_OUTPUT:
576 					printf("Main Output");
577 					break;
578 				case HID_MAIN_ITEM_TAG_FEATURE:
579 					printf("Main Feature");
580 					break;
581 				case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
582 					printf("Main Begin Collection");
583 					break;
584 				case HID_MAIN_ITEM_TAG_END_COLLECTION:
585 					printf("Main End Collection");
586 					break;
587 				default:
588 					printf("Main reserved %d",item->tag);
589 					break;
590 			}
591 			break;
592 		case HID_ITEM_TYPE_GLOBAL:
593 			switch(item->tag) {
594 				case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
595 					printf("- Global Usage Page");
596 					break;
597 				case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
598 					printf("- Global Logical Minimum");
599 					break;
600 				case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
601 					printf("- Global Logical Maximum");
602 					break;
603 				case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
604 					printf("- Global physical Minimum");
605 					break;
606 				case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
607 					printf("- Global physical Maximum");
608 					break;
609 				case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
610 					printf("- Global Unit Exponent");
611 					break;
612 				case HID_GLOBAL_ITEM_TAG_UNIT:
613 					printf("- Global Unit");
614 					break;
615 				case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
616 					printf("- Global Report Size");
617 					break;
618 				case HID_GLOBAL_ITEM_TAG_REPORT_ID:
619 					printf("- Global Report ID");
620 					break;
621 				case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
622 					printf("- Global Report Count");
623 					break;
624 				case HID_GLOBAL_ITEM_TAG_PUSH:
625 					printf("- Global Push");
626 					break;
627 				case HID_GLOBAL_ITEM_TAG_POP:
628 					printf("- Global Pop");
629 					break;
630 				default:
631 					printf("- Global reserved %d",item->tag);
632 					break;
633 			}
634 			break;
635 		case HID_ITEM_TYPE_LOCAL:
636 			switch(item->tag) {
637 				case HID_LOCAL_ITEM_TAG_USAGE:
638 					printf("-- Local Usage");
639 					break;
640 				case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
641 					printf("-- Local Usage Minimum");
642 					break;
643 				case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
644 					printf("-- Local Usage Maximum");
645 					break;
646 				case HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX:
647 					printf("-- Local Designator Index");
648 					break;
649 				case HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM:
650 					printf("-- Local Designator Minimum");
651 					break;
652 				case HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM:
653 					printf("-- Local Designator Maximum");
654 					break;
655 				case HID_LOCAL_ITEM_TAG_STRING_INDEX:
656 					printf("-- Local String Index");
657 					break;
658 				case HID_LOCAL_ITEM_TAG_STRING_MINIMUM:
659 					printf("-- Local String Minimum");
660 					break;
661 				case HID_LOCAL_ITEM_TAG_STRING_MAXIMUM:
662 					printf("-- Local String Maximum");
663 					break;
664 				case HID_LOCAL_ITEM_TAG_DELIMITER:
665 					printf("-- Local Delimiter");
666 					break;
667 				default:
668 					printf("-- Local reserved %d",item->tag);
669 					break;
670 			}
671 			break;
672 		default:
673 			printf("--- reserved %d",item->type);
674 			break;
675 	}
676 	switch(item->size) {
677 		case 1:
678 			printf("  %d",item->data.u8);
679 			break;
680 		case 2:
681 			printf("  %d",item->data.u16);
682 			break;
683 		case 4:
684 			printf("  %ld",item->data.u32);
685 			break;
686 	}
687 	printf("\n");
688 }
689 
690 
691 static int usb_kbd_get_hid_desc(struct usb_device *dev)
692 {
693 	unsigned char buffer[256];
694 	struct usb_descriptor_header *head;
695 	struct usb_config_descriptor *config;
696 	int index,len,i;
697 	unsigned char *start, *end;
698 	struct hid_item item;
699 
700 	if(usb_get_configuration_no(dev,&buffer[0],0)==-1)
701 		return -1;
702 	head =(struct usb_descriptor_header *)&buffer[0];
703 	if(head->bDescriptorType!=USB_DT_CONFIG) {
704 		printf(" ERROR: NOT USB_CONFIG_DESC %x\n",head->bDescriptorType);
705 		return -1;
706 	}
707 	index=head->bLength;
708 	config=(struct usb_config_descriptor *)&buffer[0];
709 	len=le16_to_cpu(config->wTotalLength);
710 	/* Ok the first entry must be a configuration entry, now process the others */
711 	head=(struct usb_descriptor_header *)&buffer[index];
712 	while(index+1 < len) {
713 		if(head->bDescriptorType==USB_DT_HID) {
714 			printf("HID desc found\n");
715 			memcpy(&usb_kbd_hid_desc,&buffer[index],buffer[index]);
716 			le16_to_cpus(&usb_kbd_hid_desc.bcdHID);
717 			le16_to_cpus(&usb_kbd_hid_desc.wDescriptorLength);
718 			usb_kbd_display_hid(&usb_kbd_hid_desc);
719 			len=0;
720 			break;
721 		}
722 		index+=head->bLength;
723 		head=(struct usb_descriptor_header *)&buffer[index];
724 	}
725 	if(len>0)
726 		return -1;
727 	len=usb_kbd_hid_desc.wDescriptorLength;
728 	if((index = usb_get_class_descriptor(dev, 0, USB_DT_REPORT, 0, &buffer[0], len)) < 0) {
729 		printf("reading report descriptor failed\n");
730 		return -1;
731 	}
732 	printf(" report descriptor (size %u, read %d)\n", len, index);
733 	start = &buffer[0];
734 	end = &buffer[len];
735 	i=0;
736 	do {
737 		index=fetch_item(start,end,&item);
738 		i+=index;
739 		i++;
740 		if(index>=0)
741 			usb_kbd_show_item(&item);
742 
743 		start+=index;
744 		start++;
745 	} while(index>=0);
746 
747 }
748 
749 
750 #endif
751 
752 #endif /* CONFIG_USB_KEYBOARD */
753