usb_kbd.c 19 KB

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