usb_kbd.c 19 KB

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