usb_kbd.c 19 KB

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