usb_kbd.c 19 KB

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