Merge git://www.denx.de/git/u-boot
[platform/kernel/u-boot.git] / common / usb_kbd.c
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 #ifdef CFG_USB_EVENT_POLL
134                 usb_event_poll();
135 #endif
136         }
137         if((usb_out_pointer+1)==USB_KBD_BUFFER_LEN)
138                 usb_out_pointer=0;
139         else
140                 usb_out_pointer++;
141         c=usb_kbd_buffer[usb_out_pointer];
142         return (int)c;
143
144 }
145
146 /* forward decleration */
147 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum);
148
149 /* search for keyboard and register it if found */
150 int drv_usb_kbd_init(void)
151 {
152         int error,i,index;
153         device_t usb_kbd_dev,*old_dev;
154         struct usb_device *dev;
155         char *stdinname  = getenv ("stdin");
156
157         usb_in_pointer=0;
158         usb_out_pointer=0;
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");
171                                                 return 1;
172                                         }
173                                 }
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);
184                                 if(error==0) {
185                                         /* check if this is the standard input device */
186                                         if(strcmp(stdinname,DEVNAME)==0) {
187                                                 /* reassign the console */
188                                                 if(overwrite_console()) {
189                                                         return 1;
190                                                 }
191                                                 error=console_assign(stdin,DEVNAME);
192                                                 if(error==0)
193                                                         return 1;
194                                                 else
195                                                         return error;
196                                         }
197                                         return 1;
198                                 }
199                                 return error;
200                         }
201                 }
202         }
203         /* no USB Keyboard found */
204         return -1;
205 }
206
207
208 /* deregistering the keyboard */
209 int usb_kbd_deregister(void)
210 {
211         return device_deregister(DEVNAME);
212 }
213
214 /**************************************************************************
215  * Low Level drivers
216  */
217
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 */
221
222 static void usb_kbd_setled(struct usb_device *dev)
223 {
224         struct usb_interface_descriptor *iface;
225         iface = &dev->config.if_desc[0];
226         leds=0;
227         if(scroll_lock!=0)
228                 leds|=1;
229         leds<<=1;
230         if(caps_lock!=0)
231                 leds|=1;
232         leds<<=1;
233         if(num_lock!=0)
234                 leds|=1;
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);
238
239 }
240
241
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)
245 {
246         unsigned char keycode;
247
248         if(pressed==0) {
249                 /* key released */
250                 repeat_delay=0;
251                 return 0;
252         }
253         if(pressed==2) {
254                 repeat_delay++;
255                 if(repeat_delay<REPEAT_DELAY)
256                         return 0;
257                 repeat_delay=REPEAT_DELAY;
258         }
259         keycode=0;
260         if((scancode>3) && (scancode<0x1d)) { /* alpha numeric values */
261                 keycode=scancode-4 + 0x61;
262                 if(caps_lock)
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 */
267                         else
268                                 keycode|=CAPITAL_MASK; /* switch to non capital Letters */
269                 }
270         }
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];
276         }
277         if(pressed==1) {
278                 if(scancode==NUM_LOCK) {
279                         num_lock=~num_lock;
280                         return 1;
281                 }
282                 if(scancode==CAPS_LOCK) {
283                         caps_lock=~caps_lock;
284                         return 1;
285                 }
286                 if(scancode==SCROLL_LOCK) {
287                         scroll_lock=~scroll_lock;
288                         return 1;
289                 }
290         }
291         if(keycode!=0) {
292                 USB_KBD_PRINTF("%c",keycode);
293                 usb_kbd_put_queue(keycode);
294         }
295         return 0;
296 }
297
298 /* Interrupt service routine */
299 static int usb_kbd_irq(struct usb_device *dev)
300 {
301         int i,res;
302
303         if((dev->irq_status!=0)||(dev->irq_act_len!=8))
304         {
305                 USB_KBD_PRINTF("usb_keyboard Error %lX, len %d\n",dev->irq_status,dev->irq_act_len);
306                 return 1;
307         }
308         res=0;
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);
312                 }
313                 if (new[i] > 3 && memscan(&old[2], new[i], 6) == &old[8]) {
314                         res|=usb_kbd_translate(new[i],new[0],1);
315                 }
316         }
317         if((new[2]>3) && (old[2]==new[2])) /* still pressed */
318                 res|=usb_kbd_translate(new[2],new[0],2);
319         if(res==1)
320                 usb_kbd_setled(dev);
321         memcpy(&old[0],&new[0], 8);
322         return 1; /* install IRQ Handler again */
323 }
324
325 /* probes the USB device dev for keyboard type */
326 static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
327 {
328         struct usb_interface_descriptor *iface;
329         struct usb_endpoint_descriptor *ep;
330         int pipe,maxp;
331
332         if (dev->descriptor.bNumConfigurations != 1) return 0;
333         iface = &dev->config.if_desc[ifnum];
334
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;
339
340         ep = &iface->ep_desc[0];
341
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);
352         repeat_delay=0;
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);
358         return 1;
359 }
360
361
362 #if 0
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));
372
373 /*
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.
377  */
378
379 struct hid_item {
380         unsigned char format;
381         unsigned char size;
382         unsigned char type;
383         unsigned char tag;
384         union {
385             unsigned char   u8;
386             char            s8;
387             unsigned short  u16;
388             short           s16;
389             unsigned long   u32;
390             long            s32;
391             unsigned char  *longdata;
392         } data;
393 };
394
395 /*
396  * HID report item format
397  */
398
399 #define HID_ITEM_FORMAT_SHORT   0
400 #define HID_ITEM_FORMAT_LONG    1
401
402 /*
403  * Special tag indicating long items
404  */
405
406 #define HID_ITEM_TAG_LONG       15
407
408
409 static struct usb_hid_descriptor usb_kbd_hid_desc;
410
411 void usb_kbd_display_hid(struct usb_hid_descriptor *hid)
412 {
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);
420 }
421
422
423 /*
424  * Fetch a report description item from the data stream. We support long
425  * items, though they are not used yet.
426  */
427
428 static int fetch_item(unsigned char *start,unsigned char *end, struct hid_item *item)
429 {
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;
441                                         start += item->size;
442                                         return item->size;
443                                 }
444                         }
445                 } else {
446                         item->format = HID_ITEM_FORMAT_SHORT;
447                         item->size = b & 3;
448                         switch (item->size) {
449                                 case 0:
450                                         return item->size;
451                                 case 1:
452                                         if ((end - start) >= 1) {
453                                                 item->data.u8 = *start++;
454                                                 return item->size;
455                                         }
456                                         break;
457                                 case 2:
458                                         if ((end - start) >= 2) {
459                                                 item->data.u16 = swap_16((unsigned short *)start);
460                                                 start+=2;
461                                                 return item->size;
462                                         }
463                                 case 3:
464                                         item->size++;
465                                         if ((end - start) >= 4) {
466                                                 item->data.u32 = swap_32((unsigned long *)start);
467                                                 start+=4;
468                                                 return item->size;
469                                         }
470                         }
471                 }
472         }
473         return -1;
474 }
475
476 /*
477  * HID report descriptor item type (prefix bit 2,3)
478  */
479
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
484 /*
485  * HID report descriptor main item tags
486  */
487
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
493 /*
494  * HID report descriptor main item contents
495  */
496
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
506
507 /*
508  * HID report descriptor collection item types
509  */
510
511 #define HID_COLLECTION_PHYSICAL         0
512 #define HID_COLLECTION_APPLICATION      1
513 #define HID_COLLECTION_LOGICAL          2
514 /*
515  * HID report descriptor global item tags
516  */
517
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
530
531 /*
532  * HID report descriptor local item tags
533  */
534
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
545
546
547 static void usb_kbd_show_item(struct hid_item *item)
548 {
549         switch(item->type) {
550                 case HID_ITEM_TYPE_MAIN:
551                         switch(item->tag) {
552                                 case HID_MAIN_ITEM_TAG_INPUT:
553                                         printf("Main Input");
554                                         break;
555                                 case HID_MAIN_ITEM_TAG_OUTPUT:
556                                         printf("Main Output");
557                                         break;
558                                 case HID_MAIN_ITEM_TAG_FEATURE:
559                                         printf("Main Feature");
560                                         break;
561                                 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
562                                         printf("Main Begin Collection");
563                                         break;
564                                 case HID_MAIN_ITEM_TAG_END_COLLECTION:
565                                         printf("Main End Collection");
566                                         break;
567                                 default:
568                                         printf("Main reserved %d",item->tag);
569                                         break;
570                         }
571                         break;
572                 case HID_ITEM_TYPE_GLOBAL:
573                         switch(item->tag) {
574                                 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
575                                         printf("- Global Usage Page");
576                                         break;
577                                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
578                                         printf("- Global Logical Minimum");
579                                         break;
580                                 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
581                                         printf("- Global Logical Maximum");
582                                         break;
583                                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
584                                         printf("- Global physical Minimum");
585                                         break;
586                                 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
587                                         printf("- Global physical Maximum");
588                                         break;
589                                 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
590                                         printf("- Global Unit Exponent");
591                                         break;
592                                 case HID_GLOBAL_ITEM_TAG_UNIT:
593                                         printf("- Global Unit");
594                                         break;
595                                 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
596                                         printf("- Global Report Size");
597                                         break;
598                                 case HID_GLOBAL_ITEM_TAG_REPORT_ID:
599                                         printf("- Global Report ID");
600                                         break;
601                                 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
602                                         printf("- Global Report Count");
603                                         break;
604                                 case HID_GLOBAL_ITEM_TAG_PUSH:
605                                         printf("- Global Push");
606                                         break;
607                                 case HID_GLOBAL_ITEM_TAG_POP:
608                                         printf("- Global Pop");
609                                         break;
610                                 default:
611                                         printf("- Global reserved %d",item->tag);
612                                         break;
613                         }
614                         break;
615                 case HID_ITEM_TYPE_LOCAL:
616                         switch(item->tag) {
617                                 case HID_LOCAL_ITEM_TAG_USAGE:
618                                         printf("-- Local Usage");
619                                         break;
620                                 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
621                                         printf("-- Local Usage Minimum");
622                                         break;
623                                 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
624                                         printf("-- Local Usage Maximum");
625                                         break;
626                                 case HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX:
627                                         printf("-- Local Designator Index");
628                                         break;
629                                 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM:
630                                         printf("-- Local Designator Minimum");
631                                         break;
632                                 case HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM:
633                                         printf("-- Local Designator Maximum");
634                                         break;
635                                 case HID_LOCAL_ITEM_TAG_STRING_INDEX:
636                                         printf("-- Local String Index");
637                                         break;
638                                 case HID_LOCAL_ITEM_TAG_STRING_MINIMUM:
639                                         printf("-- Local String Minimum");
640                                         break;
641                                 case HID_LOCAL_ITEM_TAG_STRING_MAXIMUM:
642                                         printf("-- Local String Maximum");
643                                         break;
644                                 case HID_LOCAL_ITEM_TAG_DELIMITER:
645                                         printf("-- Local Delimiter");
646                                         break;
647                                 default:
648                                         printf("-- Local reserved %d",item->tag);
649                                         break;
650                         }
651                         break;
652                 default:
653                         printf("--- reserved %d",item->type);
654                         break;
655         }
656         switch(item->size) {
657                 case 1:
658                         printf("  %d",item->data.u8);
659                         break;
660                 case 2:
661                         printf("  %d",item->data.u16);
662                         break;
663                 case 4:
664                         printf("  %ld",item->data.u32);
665                         break;
666         }
667         printf("\n");
668 }
669
670
671 static int usb_kbd_get_hid_desc(struct usb_device *dev)
672 {
673         unsigned char buffer[256];
674         struct usb_descriptor_header *head;
675         struct usb_config_descriptor *config;
676         int index,len,i;
677         unsigned char *start, *end;
678         struct hid_item item;
679
680         if(usb_get_configuration_no(dev,&buffer[0],0)==-1)
681                 return -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);
685                 return -1;
686         }
687         index=head->bLength;
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);
699                         len=0;
700                         break;
701                 }
702                 index+=head->bLength;
703                 head=(struct usb_descriptor_header *)&buffer[index];
704         }
705         if(len>0)
706                 return -1;
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");
710                 return -1;
711         }
712         printf(" report descriptor (size %u, read %d)\n", len, index);
713         start=&buffer[0];
714         end=&buffer[len];
715         i=0;
716         do {
717                 index=fetch_item(start,end,&item);
718                 i+=index;
719                 i++;
720                 if(index>=0)
721                         usb_kbd_show_item(&item);
722
723                 start+=index;
724                 start++;
725         } while(index>=0);
726
727 }
728
729
730 #endif
731
732 #endif /* CONFIG_USB_KEYBOARD */