hid_report.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Minimal HID report-descriptor parser for mouse reports.
  2. *
  3. * usbhid.c uses this to find a wheel (Generic Desktop usage 0x38) and the
  4. * exact bit offsets of the mouse fields. A USB boot mouse has no wheel
  5. * field in its boot report, so a mouse whose report descriptor advertises
  6. * one is switched to report protocol and decoded through this layout. When
  7. * the descriptor has no wheel, or the parser cannot make sense of it, the
  8. * caller keeps the fixed boot-protocol decoder.
  9. *
  10. * Only the Input items a mouse needs are interpreted (buttons, X, Y, Wheel);
  11. * the rest of the descriptor is skipped by its item size. The parser is
  12. * freestanding and has a host test (make test-hid).
  13. */
  14. #ifndef HID_REPORT_H
  15. #define HID_REPORT_H
  16. /* A report longer than this cannot be received through the interrupt-IN
  17. * path (USB_INT_MAX in usb_hcd.h), so the layout is rejected. */
  18. #define HID_REPORT_MAX 32
  19. /* Mouse layout flags. */
  20. #define HID_ML_VALID 0x01u /* X and Y found; layout usable */
  21. #define HID_ML_HAS_ID 0x02u /* reports start with report_id */
  22. #define HID_ML_X_SIGNED 0x04u
  23. #define HID_ML_Y_SIGNED 0x08u
  24. #define HID_ML_WHEEL_SIGNED 0x10u
  25. struct hid_mouse_layout {
  26. unsigned short x_off, y_off, wheel_off; /* bits, after the ID byte */
  27. unsigned char x_size, y_size, wheel_size;
  28. unsigned char flags;
  29. unsigned char report_id;
  30. unsigned char report_bytes; /* including the ID byte */
  31. unsigned char btn_bit[8]; /* bit offset + 1, 0 = absent */
  32. };
  33. struct hid_mouse_frame {
  34. unsigned int buttons; /* bit0 left, bit1 right, bit2 middle, ... */
  35. int dx, dy; /* HID report values: +dy is down */
  36. int wheel; /* notches, +1 = away from the user */
  37. };
  38. /* Parse a report descriptor. Returns 1 and fills `out` when a mouse report
  39. * with X and Y was found; a layout with wheel_size != 0 also has a wheel.
  40. * Returns 0 for a descriptor that is not a usable mouse layout. */
  41. int hid_parse_mouse(const unsigned char *desc, unsigned int len,
  42. struct hid_mouse_layout *out);
  43. /* Decode one report-protocol report through `layout`. Returns 1 when the
  44. * report belongs to the mouse report ID and carries every field the layout
  45. * needs, 0 otherwise. */
  46. int hid_mouse_decode(const struct hid_mouse_layout *layout,
  47. const unsigned char *rep, unsigned int len,
  48. struct hid_mouse_frame *out);
  49. #endif