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