hid_report.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Minimal HID report-descriptor parser for mouse reports.
  2. *
  3. * See hid_report.h. The item walk keeps a global state (usage page, report
  4. * size/count, logical minimum and report ID), a small push/pop stack and the
  5. * local usages of the current main item. Input items advance one bit
  6. * position per report ID, so interleaved report IDs and non-byte-aligned
  7. * fields (12-bit axes, 5 buttons + 3 bits of padding, ...) decode correctly.
  8. */
  9. #include "hid_report.h"
  10. #define MAX_REPORTS 8
  11. #define MAX_USAGES 16
  12. #define MAX_PUSH 4
  13. /* Item type/tag values from the HID specification (6.2.2). */
  14. #define IT_MAIN 0u
  15. #define IT_GLOBAL 1u
  16. #define IT_LOCAL 2u
  17. #define GT_USAGE_PAGE 0x0u
  18. #define GT_LOGICAL_MIN 0x1u
  19. #define GT_REPORT_SIZE 0x7u
  20. #define GT_REPORT_ID 0x8u
  21. #define GT_REPORT_COUNT 0x9u
  22. #define GT_PUSH 0xAu
  23. #define GT_POP 0xBu
  24. #define LT_USAGE 0x0u
  25. #define LT_USAGE_MIN 0x1u
  26. #define LT_USAGE_MAX 0x2u
  27. #define MT_INPUT 0x8u
  28. /* Generic Desktop usages. */
  29. #define UD_X 0x30u
  30. #define UD_Y 0x31u
  31. #define UD_WHEEL 0x38u
  32. struct global {
  33. unsigned int usage_page;
  34. unsigned int report_size;
  35. unsigned int report_count;
  36. unsigned int report_id;
  37. int logical_min;
  38. };
  39. struct report {
  40. unsigned int id;
  41. unsigned int bit; /* next free input bit */
  42. unsigned int x_off, y_off, wheel_off;
  43. unsigned int x_size, y_size, wheel_size;
  44. unsigned int x_signed, y_signed, wheel_signed;
  45. unsigned int have_x, have_y, have_wheel;
  46. unsigned int btn_bit[8]; /* bit offset + 1, 0 = absent */
  47. };
  48. static unsigned int item_u(const unsigned char *d, unsigned int pos,
  49. unsigned int size)
  50. {
  51. unsigned int v = 0, k;
  52. for (k = 0; k < size; k++)
  53. v |= (unsigned int)d[pos + k] << (8u * k);
  54. return v;
  55. }
  56. static int item_s(const unsigned char *d, unsigned int pos, unsigned int size)
  57. {
  58. unsigned int v = item_u(d, pos, size);
  59. if (size && size < 4 && (v & (1u << (8u * size - 1u))))
  60. v |= ~0u << (8u * size);
  61. return (int)v;
  62. }
  63. static unsigned int bits(const unsigned char *rep, unsigned int off,
  64. unsigned int size)
  65. {
  66. unsigned int v = 0, i;
  67. for (i = 0; i < size; i++)
  68. if (rep[(off + i) >> 3] & (1u << ((off + i) & 7u)))
  69. v |= 1u << i;
  70. return v;
  71. }
  72. static int sbits(const unsigned char *rep, unsigned int off, unsigned int size)
  73. {
  74. unsigned int v;
  75. if (!size)
  76. return 0;
  77. v = bits(rep, off, size);
  78. if (size < 32 && (v & (1u << (size - 1u))))
  79. v |= ~0u << size;
  80. return (int)v;
  81. }
  82. static struct report *report_for(struct report *reps, unsigned int *count,
  83. unsigned int id)
  84. {
  85. unsigned int i;
  86. for (i = 0; i < *count; i++) {
  87. if (reps[i].id == id)
  88. return &reps[i];
  89. }
  90. if (*count >= MAX_REPORTS)
  91. return 0;
  92. reps[*count].id = id;
  93. reps[*count].bit = 0;
  94. (*count)++;
  95. return &reps[*count - 1];
  96. }
  97. /* Record one field of an Input item. `off` is the field's first bit and
  98. * `size` its width. Only the fields needed for a mouse are kept. */
  99. static void add_field(struct report *r, unsigned int page,
  100. unsigned int usage, unsigned int off, unsigned int size,
  101. int signed_value)
  102. {
  103. if (off + size > HID_REPORT_MAX * 8u)
  104. return;
  105. if (page == 0x01u) {
  106. if (size > 32u)
  107. return;
  108. if (usage == UD_X && !r->have_x && size) {
  109. r->x_off = off;
  110. r->x_size = size;
  111. r->x_signed = signed_value != 0;
  112. r->have_x = 1;
  113. } else if (usage == UD_Y && !r->have_y && size) {
  114. r->y_off = off;
  115. r->y_size = size;
  116. r->y_signed = signed_value != 0;
  117. r->have_y = 1;
  118. } else if (usage == UD_WHEEL && !r->have_wheel && size) {
  119. r->wheel_off = off;
  120. r->wheel_size = size;
  121. r->wheel_signed = signed_value != 0;
  122. r->have_wheel = 1;
  123. }
  124. } else if (page == 0x09u && usage >= 1u && usage <= 8u && size == 1u) {
  125. if (!r->btn_bit[usage - 1u] && off < 255u)
  126. r->btn_bit[usage - 1u] = off + 1u;
  127. }
  128. }
  129. int hid_parse_mouse(const unsigned char *desc, unsigned int len,
  130. struct hid_mouse_layout *out)
  131. {
  132. struct global g, stack[MAX_PUSH];
  133. struct report reps[MAX_REPORTS];
  134. unsigned int local[MAX_USAGES];
  135. unsigned int nlocal = 0;
  136. unsigned int usemin = 0, usemax = 0;
  137. unsigned int have_min = 0, have_max = 0;
  138. unsigned int nreps = 0, any_id = 0, depth = 0;
  139. unsigned int i = 0, k;
  140. if (!desc || !out || len < 2)
  141. return 0;
  142. for (i = 0; i < sizeof(*out); i++)
  143. ((unsigned char *)out)[i] = 0;
  144. for (i = 0; i < sizeof(reps); i++)
  145. ((unsigned char *)reps)[i] = 0;
  146. for (i = 0; i < sizeof(g); i++)
  147. ((unsigned char *)&g)[i] = 0;
  148. i = 0;
  149. while (i < len) {
  150. unsigned int item = desc[i++];
  151. unsigned int size = item & 3u;
  152. unsigned int type = (item >> 2) & 3u;
  153. unsigned int tag = (item >> 4) & 0xFu;
  154. unsigned int pos = i;
  155. unsigned int data = 0;
  156. if (size == 3u)
  157. size = 4u;
  158. if (i + size > len)
  159. break; /* truncated tail */
  160. if (size)
  161. data = item_u(desc, pos, size);
  162. i += size;
  163. if (type == IT_GLOBAL) {
  164. switch (tag) {
  165. case GT_USAGE_PAGE:
  166. g.usage_page = data;
  167. break;
  168. case GT_LOGICAL_MIN:
  169. g.logical_min = item_s(desc, pos, size);
  170. break;
  171. case GT_REPORT_SIZE:
  172. g.report_size = data;
  173. break;
  174. case GT_REPORT_ID:
  175. g.report_id = data & 0xFFu;
  176. any_id = 1;
  177. break;
  178. case GT_REPORT_COUNT:
  179. g.report_count = data;
  180. break;
  181. case GT_PUSH:
  182. if (depth < MAX_PUSH)
  183. stack[depth++] = g;
  184. break;
  185. case GT_POP:
  186. if (depth)
  187. g = stack[--depth];
  188. break;
  189. default:
  190. break;
  191. }
  192. } else if (type == IT_LOCAL) {
  193. if (tag == LT_USAGE) {
  194. unsigned int page = g.usage_page;
  195. unsigned int usage = data;
  196. /* A four-byte Usage carries its own page. */
  197. if (size == 4) {
  198. page = data >> 16;
  199. usage = data & 0xFFFFu;
  200. }
  201. if (nlocal < MAX_USAGES)
  202. local[nlocal++] = (page << 16) | usage;
  203. } else if (tag == LT_USAGE_MIN || tag == LT_USAGE_MAX) {
  204. unsigned int page = g.usage_page;
  205. unsigned int usage = data;
  206. if (size == 4) {
  207. page = data >> 16;
  208. usage = data & 0xFFFFu;
  209. }
  210. if (tag == LT_USAGE_MIN) {
  211. usemin = (page << 16) | usage;
  212. have_min = 1;
  213. } else {
  214. usemax = (page << 16) | usage;
  215. have_max = 1;
  216. }
  217. }
  218. } else if (type == IT_MAIN) {
  219. if (tag == MT_INPUT) {
  220. struct report *r = report_for(reps, &nreps,
  221. g.report_id);
  222. unsigned int variable = (data & 0x02u) != 0;
  223. unsigned int constant = (data & 0x01u) != 0;
  224. unsigned int fsize = g.report_size;
  225. unsigned int count = g.report_count;
  226. unsigned int total;
  227. if (fsize && count > 0xFFFFu / fsize)
  228. return 0; /* malformed count */
  229. total = fsize * count;
  230. if (r && variable && !constant) {
  231. for (k = 0; k < count; k++) {
  232. unsigned int usage = 0;
  233. unsigned int page;
  234. int have = 0;
  235. if (k < nlocal) {
  236. usage = local[k];
  237. have = 1;
  238. } else if (have_min && have_max) {
  239. unsigned int min_id =
  240. usemin & 0xFFFFu;
  241. unsigned int max_id =
  242. usemax & 0xFFFFu;
  243. unsigned int id = min_id + k;
  244. if ((usemin >> 16) ==
  245. (usemax >> 16) &&
  246. id <= max_id) {
  247. usage = (usemin &
  248. 0xFFFF0000u) | id;
  249. have = 1;
  250. }
  251. }
  252. if (!have)
  253. continue;
  254. page = usage >> 16;
  255. add_field(r, page,
  256. usage & 0xFFFFu,
  257. r->bit + k * fsize,
  258. fsize,
  259. g.logical_min < 0);
  260. }
  261. }
  262. if (r)
  263. r->bit += total;
  264. }
  265. /* Local items apply to one main item only. */
  266. nlocal = 0;
  267. have_min = have_max = 0;
  268. }
  269. }
  270. /* Prefer a report that carries a wheel; otherwise the first one with
  271. * both axes is still a usable wheel-less mouse layout. */
  272. {
  273. struct report *pick = 0;
  274. unsigned int end = 0;
  275. for (k = 0; k < nreps; k++) {
  276. struct report *r = &reps[k];
  277. if (!r->have_x || !r->have_y)
  278. continue;
  279. if (!pick)
  280. pick = r;
  281. if (r->have_wheel) {
  282. pick = r;
  283. break;
  284. }
  285. }
  286. if (!pick)
  287. return 0;
  288. out->x_off = (unsigned short)pick->x_off;
  289. out->y_off = (unsigned short)pick->y_off;
  290. out->wheel_off = (unsigned short)pick->wheel_off;
  291. out->x_size = (unsigned char)pick->x_size;
  292. out->y_size = (unsigned char)pick->y_size;
  293. out->wheel_size = (unsigned char)pick->wheel_size;
  294. out->report_id = (unsigned char)pick->id;
  295. out->flags = HID_ML_VALID;
  296. if (any_id && pick->id)
  297. out->flags |= HID_ML_HAS_ID;
  298. if (pick->x_signed)
  299. out->flags |= HID_ML_X_SIGNED;
  300. if (pick->y_signed)
  301. out->flags |= HID_ML_Y_SIGNED;
  302. if (pick->wheel_signed)
  303. out->flags |= HID_ML_WHEEL_SIGNED;
  304. for (k = 0; k < 8; k++)
  305. out->btn_bit[k] = (unsigned char)pick->btn_bit[k];
  306. end = pick->x_off + pick->x_size;
  307. if (pick->y_off + pick->y_size > end)
  308. end = pick->y_off + pick->y_size;
  309. if (pick->have_wheel &&
  310. pick->wheel_off + pick->wheel_size > end)
  311. end = pick->wheel_off + pick->wheel_size;
  312. for (k = 0; k < 8; k++) {
  313. if (pick->btn_bit[k] &&
  314. pick->btn_bit[k] > end)
  315. end = pick->btn_bit[k];
  316. }
  317. out->report_bytes = (unsigned char)((end + 7u) / 8u);
  318. if (out->flags & HID_ML_HAS_ID)
  319. out->report_bytes++;
  320. if (out->report_bytes > HID_REPORT_MAX)
  321. return 0;
  322. }
  323. return 1;
  324. }
  325. int hid_mouse_decode(const struct hid_mouse_layout *l,
  326. const unsigned char *rep, unsigned int len,
  327. struct hid_mouse_frame *out)
  328. {
  329. unsigned int base = 0, i;
  330. if (!l || !rep || !out || !(l->flags & HID_ML_VALID))
  331. return 0;
  332. if (len < l->report_bytes)
  333. return 0;
  334. if (l->flags & HID_ML_HAS_ID) {
  335. if (rep[0] != l->report_id)
  336. return 0;
  337. base = 8;
  338. }
  339. out->buttons = 0;
  340. for (i = 0; i < 8; i++) {
  341. unsigned int off;
  342. if (!l->btn_bit[i])
  343. continue;
  344. off = base + l->btn_bit[i] - 1u;
  345. if (rep[off >> 3] & (1u << (off & 7u)))
  346. out->buttons |= 1u << i;
  347. }
  348. out->dx = (l->flags & HID_ML_X_SIGNED)
  349. ? sbits(rep, base + l->x_off, l->x_size)
  350. : (int)bits(rep, base + l->x_off, l->x_size);
  351. out->dy = (l->flags & HID_ML_Y_SIGNED)
  352. ? sbits(rep, base + l->y_off, l->y_size)
  353. : (int)bits(rep, base + l->y_off, l->y_size);
  354. out->wheel = 0;
  355. if (l->wheel_size) {
  356. out->wheel = (l->flags & HID_ML_WHEEL_SIGNED)
  357. ? sbits(rep, base + l->wheel_off, l->wheel_size)
  358. : (int)bits(rep, base + l->wheel_off, l->wheel_size);
  359. }
  360. return 1;
  361. }