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