usb_kbd.c 19 KB

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