usb_kbd.c 19 KB

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