/* Minimal HID report-descriptor parser for mouse reports. * * See hid_report.h. The item walk keeps a global state (usage page, report * size/count, logical minimum and report ID), a small push/pop stack and the * local usages of the current main item. Input items advance one bit * position per report ID, so interleaved report IDs and non-byte-aligned * fields (12-bit axes, 5 buttons + 3 bits of padding, ...) decode correctly. */ #include "hid_report.h" #define MAX_REPORTS 8 #define MAX_USAGES 16 #define MAX_PUSH 4 /* Item type/tag values from the HID specification (6.2.2). */ #define IT_MAIN 0u #define IT_GLOBAL 1u #define IT_LOCAL 2u #define GT_USAGE_PAGE 0x0u #define GT_LOGICAL_MIN 0x1u #define GT_REPORT_SIZE 0x7u #define GT_REPORT_ID 0x8u #define GT_REPORT_COUNT 0x9u #define GT_PUSH 0xAu #define GT_POP 0xBu #define LT_USAGE 0x0u #define LT_USAGE_MIN 0x1u #define LT_USAGE_MAX 0x2u #define MT_INPUT 0x8u /* Generic Desktop usages. */ #define UD_X 0x30u #define UD_Y 0x31u #define UD_WHEEL 0x38u struct global { unsigned int usage_page; unsigned int report_size; unsigned int report_count; unsigned int report_id; int logical_min; }; struct report { unsigned int id; unsigned int bit; /* next free input bit */ unsigned int x_off, y_off, wheel_off; unsigned int x_size, y_size, wheel_size; unsigned int x_signed, y_signed, wheel_signed; unsigned int have_x, have_y, have_wheel; unsigned int btn_bit[8]; /* bit offset + 1, 0 = absent */ }; static unsigned int item_u(const unsigned char *d, unsigned int pos, unsigned int size) { unsigned int v = 0, k; for (k = 0; k < size; k++) v |= (unsigned int)d[pos + k] << (8u * k); return v; } static int item_s(const unsigned char *d, unsigned int pos, unsigned int size) { unsigned int v = item_u(d, pos, size); if (size && size < 4 && (v & (1u << (8u * size - 1u)))) v |= ~0u << (8u * size); return (int)v; } static unsigned int bits(const unsigned char *rep, unsigned int off, unsigned int size) { unsigned int v = 0, i; for (i = 0; i < size; i++) if (rep[(off + i) >> 3] & (1u << ((off + i) & 7u))) v |= 1u << i; return v; } static int sbits(const unsigned char *rep, unsigned int off, unsigned int size) { unsigned int v; if (!size) return 0; v = bits(rep, off, size); if (size < 32 && (v & (1u << (size - 1u)))) v |= ~0u << size; return (int)v; } static struct report *report_for(struct report *reps, unsigned int *count, unsigned int id) { unsigned int i; for (i = 0; i < *count; i++) { if (reps[i].id == id) return &reps[i]; } if (*count >= MAX_REPORTS) return 0; reps[*count].id = id; reps[*count].bit = 0; (*count)++; return &reps[*count - 1]; } /* Record one field of an Input item. `off` is the field's first bit and * `size` its width. Only the fields needed for a mouse are kept. */ static void add_field(struct report *r, unsigned int page, unsigned int usage, unsigned int off, unsigned int size, int signed_value) { if (off + size > HID_REPORT_MAX * 8u) return; if (page == 0x01u) { if (size > 32u) return; if (usage == UD_X && !r->have_x && size) { r->x_off = off; r->x_size = size; r->x_signed = signed_value != 0; r->have_x = 1; } else if (usage == UD_Y && !r->have_y && size) { r->y_off = off; r->y_size = size; r->y_signed = signed_value != 0; r->have_y = 1; } else if (usage == UD_WHEEL && !r->have_wheel && size) { r->wheel_off = off; r->wheel_size = size; r->wheel_signed = signed_value != 0; r->have_wheel = 1; } } else if (page == 0x09u && usage >= 1u && usage <= 8u && size == 1u) { if (!r->btn_bit[usage - 1u] && off < 255u) r->btn_bit[usage - 1u] = off + 1u; } } int hid_parse_mouse(const unsigned char *desc, unsigned int len, struct hid_mouse_layout *out) { struct global g, stack[MAX_PUSH]; struct report reps[MAX_REPORTS]; unsigned int local[MAX_USAGES]; unsigned int nlocal = 0; unsigned int usemin = 0, usemax = 0; unsigned int have_min = 0, have_max = 0; unsigned int nreps = 0, any_id = 0, depth = 0; unsigned int i = 0, k; if (!desc || !out || len < 2) return 0; for (i = 0; i < sizeof(*out); i++) ((unsigned char *)out)[i] = 0; for (i = 0; i < sizeof(reps); i++) ((unsigned char *)reps)[i] = 0; for (i = 0; i < sizeof(g); i++) ((unsigned char *)&g)[i] = 0; i = 0; while (i < len) { unsigned int item = desc[i++]; unsigned int size = item & 3u; unsigned int type = (item >> 2) & 3u; unsigned int tag = (item >> 4) & 0xFu; unsigned int pos = i; unsigned int data = 0; if (size == 3u) size = 4u; if (i + size > len) break; /* truncated tail */ if (size) data = item_u(desc, pos, size); i += size; if (type == IT_GLOBAL) { switch (tag) { case GT_USAGE_PAGE: g.usage_page = data; break; case GT_LOGICAL_MIN: g.logical_min = item_s(desc, pos, size); break; case GT_REPORT_SIZE: g.report_size = data; break; case GT_REPORT_ID: g.report_id = data & 0xFFu; any_id = 1; break; case GT_REPORT_COUNT: g.report_count = data; break; case GT_PUSH: if (depth < MAX_PUSH) stack[depth++] = g; break; case GT_POP: if (depth) g = stack[--depth]; break; default: break; } } else if (type == IT_LOCAL) { if (tag == LT_USAGE) { unsigned int page = g.usage_page; unsigned int usage = data; /* A four-byte Usage carries its own page. */ if (size == 4) { page = data >> 16; usage = data & 0xFFFFu; } if (nlocal < MAX_USAGES) local[nlocal++] = (page << 16) | usage; } else if (tag == LT_USAGE_MIN || tag == LT_USAGE_MAX) { unsigned int page = g.usage_page; unsigned int usage = data; if (size == 4) { page = data >> 16; usage = data & 0xFFFFu; } if (tag == LT_USAGE_MIN) { usemin = (page << 16) | usage; have_min = 1; } else { usemax = (page << 16) | usage; have_max = 1; } } } else if (type == IT_MAIN) { if (tag == MT_INPUT) { struct report *r = report_for(reps, &nreps, g.report_id); unsigned int variable = (data & 0x02u) != 0; unsigned int constant = (data & 0x01u) != 0; unsigned int fsize = g.report_size; unsigned int count = g.report_count; unsigned int total; if (fsize && count > 0xFFFFu / fsize) return 0; /* malformed count */ total = fsize * count; if (r && variable && !constant) { for (k = 0; k < count; k++) { unsigned int usage = 0; unsigned int page; int have = 0; if (k < nlocal) { usage = local[k]; have = 1; } else if (have_min && have_max) { unsigned int min_id = usemin & 0xFFFFu; unsigned int max_id = usemax & 0xFFFFu; unsigned int id = min_id + k; if ((usemin >> 16) == (usemax >> 16) && id <= max_id) { usage = (usemin & 0xFFFF0000u) | id; have = 1; } } if (!have) continue; page = usage >> 16; add_field(r, page, usage & 0xFFFFu, r->bit + k * fsize, fsize, g.logical_min < 0); } } if (r) r->bit += total; } /* Local items apply to one main item only. */ nlocal = 0; have_min = have_max = 0; } } /* Prefer a report that carries a wheel; otherwise the first one with * both axes is still a usable wheel-less mouse layout. */ { struct report *pick = 0; unsigned int end = 0; for (k = 0; k < nreps; k++) { struct report *r = &reps[k]; if (!r->have_x || !r->have_y) continue; if (!pick) pick = r; if (r->have_wheel) { pick = r; break; } } if (!pick) return 0; out->x_off = (unsigned short)pick->x_off; out->y_off = (unsigned short)pick->y_off; out->wheel_off = (unsigned short)pick->wheel_off; out->x_size = (unsigned char)pick->x_size; out->y_size = (unsigned char)pick->y_size; out->wheel_size = (unsigned char)pick->wheel_size; out->report_id = (unsigned char)pick->id; out->flags = HID_ML_VALID; if (any_id && pick->id) out->flags |= HID_ML_HAS_ID; if (pick->x_signed) out->flags |= HID_ML_X_SIGNED; if (pick->y_signed) out->flags |= HID_ML_Y_SIGNED; if (pick->wheel_signed) out->flags |= HID_ML_WHEEL_SIGNED; for (k = 0; k < 8; k++) out->btn_bit[k] = (unsigned char)pick->btn_bit[k]; end = pick->x_off + pick->x_size; if (pick->y_off + pick->y_size > end) end = pick->y_off + pick->y_size; if (pick->have_wheel && pick->wheel_off + pick->wheel_size > end) end = pick->wheel_off + pick->wheel_size; for (k = 0; k < 8; k++) { if (pick->btn_bit[k] && pick->btn_bit[k] > end) end = pick->btn_bit[k]; } out->report_bytes = (unsigned char)((end + 7u) / 8u); if (out->flags & HID_ML_HAS_ID) out->report_bytes++; if (out->report_bytes > HID_REPORT_MAX) return 0; } return 1; } int hid_mouse_decode(const struct hid_mouse_layout *l, const unsigned char *rep, unsigned int len, struct hid_mouse_frame *out) { unsigned int base = 0, i; if (!l || !rep || !out || !(l->flags & HID_ML_VALID)) return 0; if (len < l->report_bytes) return 0; if (l->flags & HID_ML_HAS_ID) { if (rep[0] != l->report_id) return 0; base = 8; } out->buttons = 0; for (i = 0; i < 8; i++) { unsigned int off; if (!l->btn_bit[i]) continue; off = base + l->btn_bit[i] - 1u; if (rep[off >> 3] & (1u << (off & 7u))) out->buttons |= 1u << i; } out->dx = (l->flags & HID_ML_X_SIGNED) ? sbits(rep, base + l->x_off, l->x_size) : (int)bits(rep, base + l->x_off, l->x_size); out->dy = (l->flags & HID_ML_Y_SIGNED) ? sbits(rep, base + l->y_off, l->y_size) : (int)bits(rep, base + l->y_off, l->y_size); out->wheel = 0; if (l->wheel_size) { out->wheel = (l->flags & HID_ML_WHEEL_SIGNED) ? sbits(rep, base + l->wheel_off, l->wheel_size) : (int)bits(rep, base + l->wheel_off, l->wheel_size); } return 1; }