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