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