usb_kbd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 <stdio_dev.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. struct stdio_dev 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 = stdio_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(struct stdio_dev));
  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 = stdio_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_STDIO_DEREGISTER
  195. return stdio_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 *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->desc.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 *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->desc.bInterfaceClass != 3)
  322. return 0;
  323. if (iface->desc.bInterfaceSubClass != 1)
  324. return 0;
  325. if (iface->desc.bInterfaceProtocol != 1)
  326. return 0;
  327. if (iface->desc.bNumEndpoints != 1)
  328. return 0;
  329. ep = &iface->ep_desc[0];
  330. if (!(ep->bEndpointAddress & 0x80)) return 0;
  331. if ((ep->bmAttributes & 3) != 3) return 0;
  332. USB_KBD_PRINTF("USB KBD found set protocol...\n");
  333. /* ok, we found a USB Keyboard, install it */
  334. /* usb_kbd_get_hid_desc(dev); */
  335. usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
  336. USB_KBD_PRINTF("USB KBD found set idle...\n");
  337. usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
  338. memset(&new[0], 0, 8);
  339. memset(&old[0], 0, 8);
  340. repeat_delay=0;
  341. pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
  342. maxp = usb_maxpacket(dev, pipe);
  343. dev->irq_handle=usb_kbd_irq;
  344. USB_KBD_PRINTF("USB KBD enable interrupt pipe...\n");
  345. usb_submit_int_msg(dev,pipe,&new[0], maxp > 8 ? 8 : maxp,ep->bInterval);
  346. return 1;
  347. }
  348. #if 0
  349. struct usb_hid_descriptor {
  350. unsigned char bLength;
  351. unsigned char bDescriptorType; /* 0x21 for HID */
  352. unsigned short bcdHID; /* release number */
  353. unsigned char bCountryCode;
  354. unsigned char bNumDescriptors;
  355. unsigned char bReportDescriptorType;
  356. unsigned short wDescriptorLength;
  357. } __attribute__ ((packed));
  358. /*
  359. * We parse each description item into this structure. Short items data
  360. * values are expanded to 32-bit signed int, long items contain a pointer
  361. * into the data area.
  362. */
  363. struct hid_item {
  364. unsigned char format;
  365. unsigned char size;
  366. unsigned char type;
  367. unsigned char tag;
  368. union {
  369. unsigned char u8;
  370. char s8;
  371. unsigned short u16;
  372. short s16;
  373. unsigned long u32;
  374. long s32;
  375. unsigned char *longdata;
  376. } data;
  377. };
  378. /*
  379. * HID report item format
  380. */
  381. #define HID_ITEM_FORMAT_SHORT 0
  382. #define HID_ITEM_FORMAT_LONG 1
  383. /*
  384. * Special tag indicating long items
  385. */
  386. #define HID_ITEM_TAG_LONG 15
  387. static struct usb_hid_descriptor usb_kbd_hid_desc;
  388. void usb_kbd_display_hid(struct usb_hid_descriptor *hid)
  389. {
  390. printf("USB_HID_DESC:\n");
  391. printf(" bLenght 0x%x\n",hid->bLength);
  392. printf(" bcdHID 0x%x\n",hid->bcdHID);
  393. printf(" bCountryCode %d\n",hid->bCountryCode);
  394. printf(" bNumDescriptors 0x%x\n",hid->bNumDescriptors);
  395. printf(" bReportDescriptorType 0x%x\n",hid->bReportDescriptorType);
  396. printf(" wDescriptorLength 0x%x\n",hid->wDescriptorLength);
  397. }
  398. /*
  399. * Fetch a report description item from the data stream. We support long
  400. * items, though they are not used yet.
  401. */
  402. static int fetch_item(unsigned char *start,unsigned char *end, struct hid_item *item)
  403. {
  404. if((end - start) > 0) {
  405. unsigned char b = *start++;
  406. item->type = (b >> 2) & 3;
  407. item->tag = (b >> 4) & 15;
  408. if (item->tag == HID_ITEM_TAG_LONG) {
  409. item->format = HID_ITEM_FORMAT_LONG;
  410. if ((end - start) >= 2) {
  411. item->size = *start++;
  412. item->tag = *start++;
  413. if ((end - start) >= item->size) {
  414. item->data.longdata = start;
  415. start += item->size;
  416. return item->size;
  417. }
  418. }
  419. } else {
  420. item->format = HID_ITEM_FORMAT_SHORT;
  421. item->size = b & 3;
  422. switch (item->size) {
  423. case 0:
  424. return item->size;
  425. case 1:
  426. if ((end - start) >= 1) {
  427. item->data.u8 = *start++;
  428. return item->size;
  429. }
  430. break;
  431. case 2:
  432. if ((end - start) >= 2) {
  433. item->data.u16 = le16_to_cpu((unsigned short *)start);
  434. start+=2;
  435. return item->size;
  436. }
  437. case 3:
  438. item->size++;
  439. if ((end - start) >= 4) {
  440. item->data.u32 = le32_to_cpu((unsigned long *)start);
  441. start+=4;
  442. return item->size;
  443. }
  444. }
  445. }
  446. }
  447. return -1;
  448. }
  449. /*
  450. * HID report descriptor item type (prefix bit 2,3)
  451. */
  452. #define HID_ITEM_TYPE_MAIN 0
  453. #define HID_ITEM_TYPE_GLOBAL 1
  454. #define HID_ITEM_TYPE_LOCAL 2
  455. #define HID_ITEM_TYPE_RESERVED 3
  456. /*
  457. * HID report descriptor main item tags
  458. */
  459. #define HID_MAIN_ITEM_TAG_INPUT 8
  460. #define HID_MAIN_ITEM_TAG_OUTPUT 9
  461. #define HID_MAIN_ITEM_TAG_FEATURE 11
  462. #define HID_MAIN_ITEM_TAG_BEGIN_COLLECTION 10
  463. #define HID_MAIN_ITEM_TAG_END_COLLECTION 12
  464. /*
  465. * HID report descriptor main item contents
  466. */
  467. #define HID_MAIN_ITEM_CONSTANT 0x001
  468. #define HID_MAIN_ITEM_VARIABLE 0x002
  469. #define HID_MAIN_ITEM_RELATIVE 0x004
  470. #define HID_MAIN_ITEM_WRAP 0x008
  471. #define HID_MAIN_ITEM_NONLINEAR 0x010
  472. #define HID_MAIN_ITEM_NO_PREFERRED 0x020
  473. #define HID_MAIN_ITEM_NULL_STATE 0x040
  474. #define HID_MAIN_ITEM_VOLATILE 0x080
  475. #define HID_MAIN_ITEM_BUFFERED_BYTE 0x100
  476. /*
  477. * HID report descriptor collection item types
  478. */
  479. #define HID_COLLECTION_PHYSICAL 0
  480. #define HID_COLLECTION_APPLICATION 1
  481. #define HID_COLLECTION_LOGICAL 2
  482. /*
  483. * HID report descriptor global item tags
  484. */
  485. #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0
  486. #define HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM 1
  487. #define HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM 2
  488. #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM 3
  489. #define HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM 4
  490. #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 5
  491. #define HID_GLOBAL_ITEM_TAG_UNIT 6
  492. #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 7
  493. #define HID_GLOBAL_ITEM_TAG_REPORT_ID 8
  494. #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 9
  495. #define HID_GLOBAL_ITEM_TAG_PUSH 10
  496. #define HID_GLOBAL_ITEM_TAG_POP 11
  497. /*
  498. * HID report descriptor local item tags
  499. */
  500. #define HID_LOCAL_ITEM_TAG_USAGE 0
  501. #define HID_LOCAL_ITEM_TAG_USAGE_MINIMUM 1
  502. #define HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM 2
  503. #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 3
  504. #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM 4
  505. #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM 5
  506. #define HID_LOCAL_ITEM_TAG_STRING_INDEX 7
  507. #define HID_LOCAL_ITEM_TAG_STRING_MINIMUM 8
  508. #define HID_LOCAL_ITEM_TAG_STRING_MAXIMUM 9
  509. #define HID_LOCAL_ITEM_TAG_DELIMITER 10
  510. static void usb_kbd_show_item(struct hid_item *item)
  511. {
  512. switch(item->type) {
  513. case HID_ITEM_TYPE_MAIN:
  514. switch(item->tag) {
  515. case HID_MAIN_ITEM_TAG_INPUT:
  516. printf("Main Input");
  517. break;
  518. case HID_MAIN_ITEM_TAG_OUTPUT:
  519. printf("Main Output");
  520. break;
  521. case HID_MAIN_ITEM_TAG_FEATURE:
  522. printf("Main Feature");
  523. break;
  524. case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
  525. printf("Main Begin Collection");
  526. break;
  527. case HID_MAIN_ITEM_TAG_END_COLLECTION:
  528. printf("Main End Collection");
  529. break;
  530. default:
  531. printf("Main reserved %d",item->tag);
  532. break;
  533. }
  534. break;
  535. case HID_ITEM_TYPE_GLOBAL:
  536. switch(item->tag) {
  537. case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
  538. printf("- Global Usage Page");
  539. break;
  540. case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
  541. printf("- Global Logical Minimum");
  542. break;
  543. case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
  544. printf("- Global Logical Maximum");
  545. break;
  546. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
  547. printf("- Global physical Minimum");
  548. break;
  549. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
  550. printf("- Global physical Maximum");
  551. break;
  552. case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
  553. printf("- Global Unit Exponent");
  554. break;
  555. case HID_GLOBAL_ITEM_TAG_UNIT:
  556. printf("- Global Unit");
  557. break;
  558. case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
  559. printf("- Global Report Size");
  560. break;
  561. case HID_GLOBAL_ITEM_TAG_REPORT_ID:
  562. printf("- Global Report ID");
  563. break;
  564. case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
  565. printf("- Global Report Count");
  566. break;
  567. case HID_GLOBAL_ITEM_TAG_PUSH:
  568. printf("- Global Push");
  569. break;
  570. case HID_GLOBAL_ITEM_TAG_POP:
  571. printf("- Global Pop");
  572. break;
  573. default:
  574. printf("- Global reserved %d",item->tag);
  575. break;
  576. }
  577. break;
  578. case HID_ITEM_TYPE_LOCAL:
  579. switch(item->tag) {
  580. case HID_LOCAL_ITEM_TAG_USAGE:
  581. printf("-- Local Usage");
  582. break;
  583. case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
  584. printf("-- Local Usage Minimum");
  585. break;
  586. case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
  587. printf("-- Local Usage Maximum");
  588. break;
  589. case HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX:
  590. printf("-- Local Designator Index");
  591. break;
  592. case HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM:
  593. printf("-- Local Designator Minimum");
  594. break;
  595. case HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM:
  596. printf("-- Local Designator Maximum");
  597. break;
  598. case HID_LOCAL_ITEM_TAG_STRING_INDEX:
  599. printf("-- Local String Index");
  600. break;
  601. case HID_LOCAL_ITEM_TAG_STRING_MINIMUM:
  602. printf("-- Local String Minimum");
  603. break;
  604. case HID_LOCAL_ITEM_TAG_STRING_MAXIMUM:
  605. printf("-- Local String Maximum");
  606. break;
  607. case HID_LOCAL_ITEM_TAG_DELIMITER:
  608. printf("-- Local Delimiter");
  609. break;
  610. default:
  611. printf("-- Local reserved %d",item->tag);
  612. break;
  613. }
  614. break;
  615. default:
  616. printf("--- reserved %d",item->type);
  617. break;
  618. }
  619. switch(item->size) {
  620. case 1:
  621. printf(" %d",item->data.u8);
  622. break;
  623. case 2:
  624. printf(" %d",item->data.u16);
  625. break;
  626. case 4:
  627. printf(" %ld",item->data.u32);
  628. break;
  629. }
  630. printf("\n");
  631. }
  632. static int usb_kbd_get_hid_desc(struct usb_device *dev)
  633. {
  634. unsigned char buffer[256];
  635. struct usb_descriptor_header *head;
  636. struct usb_config_descriptor *config;
  637. int index,len,i;
  638. unsigned char *start, *end;
  639. struct hid_item item;
  640. if(usb_get_configuration_no(dev,&buffer[0],0)==-1)
  641. return -1;
  642. head =(struct usb_descriptor_header *)&buffer[0];
  643. if(head->bDescriptorType!=USB_DT_CONFIG) {
  644. printf(" ERROR: NOT USB_CONFIG_DESC %x\n",head->bDescriptorType);
  645. return -1;
  646. }
  647. index=head->bLength;
  648. config=(struct usb_config_descriptor *)&buffer[0];
  649. len=le16_to_cpu(config->wTotalLength);
  650. /* Ok the first entry must be a configuration entry, now process the others */
  651. head=(struct usb_descriptor_header *)&buffer[index];
  652. while(index+1 < len) {
  653. if(head->bDescriptorType==USB_DT_HID) {
  654. printf("HID desc found\n");
  655. memcpy(&usb_kbd_hid_desc,&buffer[index],buffer[index]);
  656. le16_to_cpus(&usb_kbd_hid_desc.bcdHID);
  657. le16_to_cpus(&usb_kbd_hid_desc.wDescriptorLength);
  658. usb_kbd_display_hid(&usb_kbd_hid_desc);
  659. len=0;
  660. break;
  661. }
  662. index+=head->bLength;
  663. head=(struct usb_descriptor_header *)&buffer[index];
  664. }
  665. if(len>0)
  666. return -1;
  667. len=usb_kbd_hid_desc.wDescriptorLength;
  668. if((index = usb_get_class_descriptor(dev, 0, USB_DT_REPORT, 0, &buffer[0], len)) < 0) {
  669. printf("reading report descriptor failed\n");
  670. return -1;
  671. }
  672. printf(" report descriptor (size %u, read %d)\n", len, index);
  673. start = &buffer[0];
  674. end = &buffer[len];
  675. i=0;
  676. do {
  677. index=fetch_item(start,end,&item);
  678. i+=index;
  679. i++;
  680. if(index>=0)
  681. usb_kbd_show_item(&item);
  682. start+=index;
  683. start++;
  684. } while(index>=0);
  685. }
  686. #endif