usb_kbd.c 21 KB

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