usb_kbd.c 19 KB

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