usb_kbd.c 19 KB

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