1*f63e15d8Smglocker /* $OpenBSD: hid.c,v 1.7 2024/05/10 10:49:10 mglocker Exp $ */
28a83145eSjcs /* $NetBSD: hid.c,v 1.23 2002/07/11 21:14:25 augustss Exp $ */
38a83145eSjcs /* $FreeBSD: src/sys/dev/usb/hid.c,v 1.11 1999/11/17 22:33:39 n_hibma Exp $ */
48a83145eSjcs
58a83145eSjcs /*
68a83145eSjcs * Copyright (c) 1998 The NetBSD Foundation, Inc.
78a83145eSjcs * All rights reserved.
88a83145eSjcs *
98a83145eSjcs * This code is derived from software contributed to The NetBSD Foundation
108a83145eSjcs * by Lennart Augustsson (lennart@augustsson.net) at
118a83145eSjcs * Carlstedt Research & Technology.
128a83145eSjcs *
138a83145eSjcs * Redistribution and use in source and binary forms, with or without
148a83145eSjcs * modification, are permitted provided that the following conditions
158a83145eSjcs * are met:
168a83145eSjcs * 1. Redistributions of source code must retain the above copyright
178a83145eSjcs * notice, this list of conditions and the following disclaimer.
188a83145eSjcs * 2. Redistributions in binary form must reproduce the above copyright
198a83145eSjcs * notice, this list of conditions and the following disclaimer in the
208a83145eSjcs * documentation and/or other materials provided with the distribution.
218a83145eSjcs *
228a83145eSjcs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
238a83145eSjcs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
248a83145eSjcs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
258a83145eSjcs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
268a83145eSjcs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
278a83145eSjcs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
288a83145eSjcs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
298a83145eSjcs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
308a83145eSjcs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
318a83145eSjcs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
328a83145eSjcs * POSSIBILITY OF SUCH DAMAGE.
338a83145eSjcs */
348a83145eSjcs
358a83145eSjcs #include <sys/param.h>
368a83145eSjcs #include <sys/systm.h>
378a83145eSjcs #include <sys/malloc.h>
388a83145eSjcs
398a83145eSjcs #include <dev/hid/hid.h>
408a83145eSjcs
418a83145eSjcs #ifdef USBHID_DEBUG
428a83145eSjcs #define DPRINTF(x...) do { printf(x); } while (0)
438a83145eSjcs #else
448a83145eSjcs #define DPRINTF(x...)
458a83145eSjcs #endif
468a83145eSjcs
478a83145eSjcs #define MAXUSAGE 64
488a83145eSjcs #define MAXPUSH 4
498a83145eSjcs #define MAXID 16
508a83145eSjcs
518a83145eSjcs struct hid_pos_data {
528a83145eSjcs int32_t rid;
538a83145eSjcs uint32_t pos;
548a83145eSjcs };
558a83145eSjcs
568a83145eSjcs struct hid_data {
578a83145eSjcs const uint8_t *start;
588a83145eSjcs const uint8_t *end;
598a83145eSjcs const uint8_t *p;
608a83145eSjcs struct hid_item cur[MAXPUSH];
618a83145eSjcs struct hid_pos_data last_pos[MAXID];
628a83145eSjcs int32_t usages_min[MAXUSAGE];
638a83145eSjcs int32_t usages_max[MAXUSAGE];
648a83145eSjcs int32_t usage_last; /* last seen usage */
658a83145eSjcs uint32_t loc_size; /* last seen size */
668a83145eSjcs uint32_t loc_count; /* last seen count */
678a83145eSjcs enum hid_kind kind;
688a83145eSjcs uint8_t pushlevel; /* current pushlevel */
698a83145eSjcs uint8_t ncount; /* end usage item count */
708a83145eSjcs uint8_t icount; /* current usage item count */
718a83145eSjcs uint8_t nusage; /* end "usages_min/max" index */
728a83145eSjcs uint8_t iusage; /* current "usages_min/max" index */
738a83145eSjcs uint8_t ousage; /* current "usages_min/max" offset */
748a83145eSjcs uint8_t susage; /* usage set flags */
758a83145eSjcs };
768a83145eSjcs
778a83145eSjcs static void
hid_clear_local(struct hid_item * c)788a83145eSjcs hid_clear_local(struct hid_item *c)
798a83145eSjcs {
808a83145eSjcs c->loc.count = 0;
818a83145eSjcs c->loc.size = 0;
828a83145eSjcs c->usage = 0;
838a83145eSjcs c->usage_minimum = 0;
848a83145eSjcs c->usage_maximum = 0;
858a83145eSjcs c->designator_index = 0;
868a83145eSjcs c->designator_minimum = 0;
878a83145eSjcs c->designator_maximum = 0;
888a83145eSjcs c->string_index = 0;
898a83145eSjcs c->string_minimum = 0;
908a83145eSjcs c->string_maximum = 0;
918a83145eSjcs c->set_delimiter = 0;
928a83145eSjcs }
938a83145eSjcs
948a83145eSjcs static void
hid_switch_rid(struct hid_data * s,struct hid_item * c,int32_t nextid)958a83145eSjcs hid_switch_rid(struct hid_data *s, struct hid_item *c, int32_t nextid)
968a83145eSjcs {
978a83145eSjcs uint8_t i;
988a83145eSjcs
998a83145eSjcs if (c->report_ID == nextid)
1008a83145eSjcs return;
1018a83145eSjcs
1028a83145eSjcs /* save current position for current rID */
1038a83145eSjcs if (c->report_ID == 0) {
1048a83145eSjcs i = 0;
1058a83145eSjcs } else {
1068a83145eSjcs for (i = 1; i != MAXID; i++) {
1078a83145eSjcs if (s->last_pos[i].rid == c->report_ID)
1088a83145eSjcs break;
1098a83145eSjcs if (s->last_pos[i].rid == 0)
1108a83145eSjcs break;
1118a83145eSjcs }
1128a83145eSjcs }
1138a83145eSjcs if (i != MAXID) {
1148a83145eSjcs s->last_pos[i].rid = c->report_ID;
1158a83145eSjcs s->last_pos[i].pos = c->loc.pos;
1168a83145eSjcs }
1178a83145eSjcs
1188a83145eSjcs /* store next report ID */
1198a83145eSjcs c->report_ID = nextid;
1208a83145eSjcs
1218a83145eSjcs /* lookup last position for next rID */
1228a83145eSjcs if (nextid == 0) {
1238a83145eSjcs i = 0;
1248a83145eSjcs } else {
1258a83145eSjcs for (i = 1; i != MAXID; i++) {
1268a83145eSjcs if (s->last_pos[i].rid == nextid)
1278a83145eSjcs break;
1288a83145eSjcs if (s->last_pos[i].rid == 0)
1298a83145eSjcs break;
1308a83145eSjcs }
1318a83145eSjcs }
1328a83145eSjcs if (i != MAXID) {
1338a83145eSjcs s->last_pos[i].rid = nextid;
1348a83145eSjcs c->loc.pos = s->last_pos[i].pos;
1358a83145eSjcs } else {
1368a83145eSjcs DPRINTF("Out of RID entries, position is set to zero!\n");
1378a83145eSjcs c->loc.pos = 0;
1388a83145eSjcs }
1398a83145eSjcs }
1408a83145eSjcs
1418a83145eSjcs struct hid_data *
hid_start_parse(const void * d,int len,enum hid_kind kind)1428a83145eSjcs hid_start_parse(const void *d, int len, enum hid_kind kind)
1438a83145eSjcs {
1448a83145eSjcs struct hid_data *s;
1458a83145eSjcs
1468a83145eSjcs s = malloc(sizeof(*s), M_TEMP, M_WAITOK | M_ZERO);
1478a83145eSjcs
1488a83145eSjcs s->start = s->p = d;
1498a83145eSjcs s->end = ((const uint8_t *)d) + len;
1508a83145eSjcs s->kind = kind;
1518a83145eSjcs return (s);
1528a83145eSjcs }
1538a83145eSjcs
1548a83145eSjcs void
hid_end_parse(struct hid_data * s)1558a83145eSjcs hid_end_parse(struct hid_data *s)
1568a83145eSjcs {
1578a83145eSjcs if (s == NULL)
1588a83145eSjcs return;
1598a83145eSjcs
1608a83145eSjcs free(s, M_TEMP, 0);
1618a83145eSjcs }
1628a83145eSjcs
1638a83145eSjcs static uint8_t
hid_get_byte(struct hid_data * s,const uint16_t wSize)1648a83145eSjcs hid_get_byte(struct hid_data *s, const uint16_t wSize)
1658a83145eSjcs {
1668a83145eSjcs const uint8_t *ptr;
1678a83145eSjcs uint8_t retval;
1688a83145eSjcs
1698a83145eSjcs ptr = s->p;
1708a83145eSjcs
1718a83145eSjcs /* check if end is reached */
1728a83145eSjcs if (ptr == s->end)
1738a83145eSjcs return (0);
1748a83145eSjcs
1758a83145eSjcs /* read out a byte */
1768a83145eSjcs retval = *ptr;
1778a83145eSjcs
1788a83145eSjcs /* check if data pointer can be advanced by "wSize" bytes */
1798a83145eSjcs if ((s->end - ptr) < wSize)
1808a83145eSjcs ptr = s->end;
1818a83145eSjcs else
1828a83145eSjcs ptr += wSize;
1838a83145eSjcs
1848a83145eSjcs /* update pointer */
1858a83145eSjcs s->p = ptr;
1868a83145eSjcs
1878a83145eSjcs return (retval);
1888a83145eSjcs }
1898a83145eSjcs
1908a83145eSjcs int
hid_get_item(struct hid_data * s,struct hid_item * h)1918a83145eSjcs hid_get_item(struct hid_data *s, struct hid_item *h)
1928a83145eSjcs {
1938a83145eSjcs struct hid_item *c;
1948a83145eSjcs unsigned int bTag, bType, bSize;
1958a83145eSjcs uint32_t oldpos;
1968a83145eSjcs int32_t mask;
1978a83145eSjcs int32_t dval;
1988a83145eSjcs
1998a83145eSjcs if (s == NULL)
2008a83145eSjcs return (0);
2018a83145eSjcs
2028cdb15fbSderaadt if (s->pushlevel >= MAXPUSH)
2038cdb15fbSderaadt return (0);
2048cdb15fbSderaadt
2058a83145eSjcs c = &s->cur[s->pushlevel];
2068a83145eSjcs
2078a83145eSjcs top:
2088a83145eSjcs /* check if there is an array of items */
2098a83145eSjcs DPRINTF("%s: icount=%d ncount=%d\n", __func__,
2108a83145eSjcs s->icount, s->ncount);
2118a83145eSjcs if (s->icount < s->ncount) {
2128a83145eSjcs /* get current usage */
2138a83145eSjcs if (s->iusage < s->nusage) {
2148a83145eSjcs dval = s->usages_min[s->iusage] + s->ousage;
2158a83145eSjcs c->usage = dval;
2168a83145eSjcs s->usage_last = dval;
2178a83145eSjcs if (dval == s->usages_max[s->iusage]) {
2188a83145eSjcs s->iusage ++;
2198a83145eSjcs s->ousage = 0;
2208a83145eSjcs } else {
2218a83145eSjcs s->ousage ++;
2228a83145eSjcs }
2238a83145eSjcs } else {
2248a83145eSjcs DPRINTF("Using last usage\n");
2258a83145eSjcs dval = s->usage_last;
2268a83145eSjcs }
2278a83145eSjcs s->icount ++;
2288a83145eSjcs /*
2298a83145eSjcs * Only copy HID item, increment position and return
2308a83145eSjcs * if correct kind!
2318a83145eSjcs */
232fdc486edSanton if (s->kind == hid_all || s->kind == c->kind) {
2338a83145eSjcs *h = *c;
2348a83145eSjcs DPRINTF("%u,%u,%u\n", h->loc.pos,
2358a83145eSjcs h->loc.size, h->loc.count);
2368a83145eSjcs c->loc.pos += c->loc.size * c->loc.count;
2378a83145eSjcs return (1);
2388a83145eSjcs }
2398a83145eSjcs }
2408a83145eSjcs
2418a83145eSjcs /* reset state variables */
2428a83145eSjcs s->icount = 0;
2438a83145eSjcs s->ncount = 0;
2448a83145eSjcs s->iusage = 0;
2458a83145eSjcs s->nusage = 0;
2468a83145eSjcs s->susage = 0;
2478a83145eSjcs s->ousage = 0;
2488a83145eSjcs hid_clear_local(c);
2498a83145eSjcs
2508a83145eSjcs /* get next item */
2518a83145eSjcs while (s->p != s->end) {
2528a83145eSjcs
2538a83145eSjcs bSize = hid_get_byte(s, 1);
2548a83145eSjcs if (bSize == 0xfe) {
2558a83145eSjcs /* long item */
2568a83145eSjcs bSize = hid_get_byte(s, 1);
2578a83145eSjcs bSize |= hid_get_byte(s, 1) << 8;
2588a83145eSjcs bTag = hid_get_byte(s, 1);
2598a83145eSjcs bType = 0xff; /* XXX what should it be */
2608a83145eSjcs } else {
2618a83145eSjcs /* short item */
2628a83145eSjcs bTag = bSize >> 4;
2638a83145eSjcs bType = (bSize >> 2) & 3;
2648a83145eSjcs bSize &= 3;
2658a83145eSjcs if (bSize == 3)
2668a83145eSjcs bSize = 4;
2678a83145eSjcs }
2688a83145eSjcs switch (bSize) {
2698a83145eSjcs case 0:
2708a83145eSjcs dval = 0;
2718a83145eSjcs mask = 0;
2728a83145eSjcs break;
2738a83145eSjcs case 1:
2748a83145eSjcs dval = hid_get_byte(s, 1);
2758a83145eSjcs mask = 0xFF;
2768a83145eSjcs break;
2778a83145eSjcs case 2:
2788a83145eSjcs dval = hid_get_byte(s, 1);
2798a83145eSjcs dval |= hid_get_byte(s, 1) << 8;
2808a83145eSjcs mask = 0xFFFF;
2818a83145eSjcs break;
2828a83145eSjcs case 4:
2838a83145eSjcs dval = hid_get_byte(s, 1);
2848a83145eSjcs dval |= hid_get_byte(s, 1) << 8;
2858a83145eSjcs dval |= hid_get_byte(s, 1) << 16;
2868a83145eSjcs dval |= hid_get_byte(s, 1) << 24;
2878a83145eSjcs mask = 0xFFFFFFFF;
2888a83145eSjcs break;
2898a83145eSjcs default:
2908a83145eSjcs dval = hid_get_byte(s, bSize);
2918a83145eSjcs DPRINTF("bad length %u (data=0x%02x)\n",
2928a83145eSjcs bSize, dval);
2938a83145eSjcs continue;
2948a83145eSjcs }
2958a83145eSjcs
2968a83145eSjcs DPRINTF("%s: bType=%d bTag=%d dval=%d\n", __func__,
2978a83145eSjcs bType, bTag, dval);
2988a83145eSjcs switch (bType) {
2998a83145eSjcs case 0: /* Main */
3008a83145eSjcs switch (bTag) {
3018a83145eSjcs case 8: /* Input */
3028a83145eSjcs c->kind = hid_input;
3038a83145eSjcs c->flags = dval;
3048a83145eSjcs ret:
3058a83145eSjcs c->loc.count = s->loc_count;
3068a83145eSjcs c->loc.size = s->loc_size;
3078a83145eSjcs
3088a83145eSjcs if (c->flags & HIO_VARIABLE) {
3098a83145eSjcs /* range check usage count */
3108a83145eSjcs if (c->loc.count > 255) {
3118a83145eSjcs DPRINTF("Number of "
3128a83145eSjcs "items truncated to 255\n");
3138a83145eSjcs s->ncount = 255;
3148a83145eSjcs } else
3158a83145eSjcs s->ncount = c->loc.count;
3168a83145eSjcs
3178a83145eSjcs /*
3188a83145eSjcs * The "top" loop will return
3198a83145eSjcs * one and one item:
3208a83145eSjcs */
3218a83145eSjcs c->loc.count = 1;
3228a83145eSjcs } else {
3238a83145eSjcs s->ncount = 1;
3248a83145eSjcs }
3258a83145eSjcs goto top;
3268a83145eSjcs
3278a83145eSjcs case 9: /* Output */
3288a83145eSjcs c->kind = hid_output;
3298a83145eSjcs c->flags = dval;
3308a83145eSjcs goto ret;
3318a83145eSjcs case 10: /* Collection */
3328a83145eSjcs c->kind = hid_collection;
3338a83145eSjcs c->collection = dval;
3348a83145eSjcs c->collevel++;
3358a83145eSjcs c->usage = s->usage_last;
3368a83145eSjcs *h = *c;
3378a83145eSjcs return (1);
3388a83145eSjcs case 11: /* Feature */
3398a83145eSjcs c->kind = hid_feature;
3408a83145eSjcs c->flags = dval;
3418a83145eSjcs goto ret;
3428a83145eSjcs case 12: /* End collection */
3438a83145eSjcs c->kind = hid_endcollection;
3448a83145eSjcs if (c->collevel == 0) {
3458a83145eSjcs DPRINTF("invalid end collection\n");
3468a83145eSjcs return (0);
3478a83145eSjcs }
3488a83145eSjcs c->collevel--;
3498a83145eSjcs *h = *c;
3508a83145eSjcs return (1);
3518a83145eSjcs default:
3528a83145eSjcs DPRINTF("Main bTag=%d\n", bTag);
3538a83145eSjcs break;
3548a83145eSjcs }
3558a83145eSjcs break;
3568a83145eSjcs case 1: /* Global */
3578a83145eSjcs switch (bTag) {
3588a83145eSjcs case 0:
3598a83145eSjcs c->_usage_page = dval << 16;
3608a83145eSjcs break;
3618a83145eSjcs case 1:
3628a83145eSjcs c->logical_minimum = dval;
3638a83145eSjcs break;
3648a83145eSjcs case 2:
3658a83145eSjcs c->logical_maximum = dval;
3668a83145eSjcs break;
3678a83145eSjcs case 3:
3688a83145eSjcs c->physical_minimum = dval;
3698a83145eSjcs break;
3708a83145eSjcs case 4:
3718a83145eSjcs c->physical_maximum = dval;
3728a83145eSjcs break;
3738a83145eSjcs case 5:
3748a83145eSjcs c->unit_exponent = dval;
3758a83145eSjcs break;
3768a83145eSjcs case 6:
3778a83145eSjcs c->unit = dval;
3788a83145eSjcs break;
3798a83145eSjcs case 7:
3808a83145eSjcs /* mask because value is unsigned */
3818a83145eSjcs s->loc_size = dval & mask;
3828a83145eSjcs break;
3838a83145eSjcs case 8:
3848a83145eSjcs hid_switch_rid(s, c, dval & mask);
3858a83145eSjcs break;
3868a83145eSjcs case 9:
3878a83145eSjcs /* mask because value is unsigned */
3888a83145eSjcs s->loc_count = dval & mask;
3898a83145eSjcs break;
3908a83145eSjcs case 10: /* Push */
3918cdb15fbSderaadt if (s->pushlevel < MAXPUSH - 1) {
3928a83145eSjcs s->pushlevel++;
3938a83145eSjcs s->cur[s->pushlevel] = *c;
3948a83145eSjcs /* store size and count */
3958a83145eSjcs c->loc.size = s->loc_size;
3968a83145eSjcs c->loc.count = s->loc_count;
3978a83145eSjcs /* update current item pointer */
3988a83145eSjcs c = &s->cur[s->pushlevel];
3998a83145eSjcs } else {
4008a83145eSjcs DPRINTF("Cannot push "
4018a83145eSjcs "item @ %d\n", s->pushlevel);
4028a83145eSjcs }
4038a83145eSjcs break;
4048a83145eSjcs case 11: /* Pop */
4058cdb15fbSderaadt if (s->pushlevel > 0) {
4068a83145eSjcs s->pushlevel--;
4078a83145eSjcs /* preserve position */
4088a83145eSjcs oldpos = c->loc.pos;
4098a83145eSjcs c = &s->cur[s->pushlevel];
4108a83145eSjcs /* restore size and count */
4118a83145eSjcs s->loc_size = c->loc.size;
4128a83145eSjcs s->loc_count = c->loc.count;
4138a83145eSjcs /* set default item location */
4148a83145eSjcs c->loc.pos = oldpos;
4158a83145eSjcs c->loc.size = 0;
4168a83145eSjcs c->loc.count = 0;
4178a83145eSjcs } else {
4188a83145eSjcs DPRINTF("Cannot pop "
4198a83145eSjcs "item @ %d\n", s->pushlevel);
4208a83145eSjcs }
4218a83145eSjcs break;
4228a83145eSjcs default:
4238a83145eSjcs DPRINTF("Global bTag=%d\n", bTag);
4248a83145eSjcs break;
4258a83145eSjcs }
4268a83145eSjcs break;
4278a83145eSjcs case 2: /* Local */
4288a83145eSjcs switch (bTag) {
4298a83145eSjcs case 0:
4308a83145eSjcs if (bSize != 4)
4318a83145eSjcs dval = (dval & mask) | c->_usage_page;
4328a83145eSjcs
4338a83145eSjcs /* set last usage, in case of a collection */
4348a83145eSjcs s->usage_last = dval;
4358a83145eSjcs
4368a83145eSjcs if (s->nusage < MAXUSAGE) {
4378a83145eSjcs s->usages_min[s->nusage] = dval;
4388a83145eSjcs s->usages_max[s->nusage] = dval;
4398a83145eSjcs s->nusage ++;
4408a83145eSjcs } else {
4418a83145eSjcs DPRINTF("max usage reached\n");
4428a83145eSjcs }
4438a83145eSjcs
4448a83145eSjcs /* clear any pending usage sets */
4458a83145eSjcs s->susage = 0;
4468a83145eSjcs break;
4478a83145eSjcs case 1:
4488a83145eSjcs s->susage |= 1;
4498a83145eSjcs
4508a83145eSjcs if (bSize != 4)
4518a83145eSjcs dval = (dval & mask) | c->_usage_page;
4528a83145eSjcs c->usage_minimum = dval;
4538a83145eSjcs
4548a83145eSjcs goto check_set;
4558a83145eSjcs case 2:
4568a83145eSjcs s->susage |= 2;
4578a83145eSjcs
4588a83145eSjcs if (bSize != 4)
4598a83145eSjcs dval = (dval & mask) | c->_usage_page;
4608a83145eSjcs c->usage_maximum = dval;
4618a83145eSjcs
4628a83145eSjcs check_set:
4638a83145eSjcs if (s->susage != 3)
4648a83145eSjcs break;
4658a83145eSjcs
4668a83145eSjcs /* sanity check */
4678a83145eSjcs if ((s->nusage < MAXUSAGE) &&
4688a83145eSjcs (c->usage_minimum <= c->usage_maximum)) {
4698a83145eSjcs /* add usage range */
4708a83145eSjcs s->usages_min[s->nusage] =
4718a83145eSjcs c->usage_minimum;
4728a83145eSjcs s->usages_max[s->nusage] =
4738a83145eSjcs c->usage_maximum;
4748a83145eSjcs s->nusage ++;
4758a83145eSjcs } else {
4768a83145eSjcs DPRINTF("Usage set dropped\n");
4778a83145eSjcs }
4788a83145eSjcs s->susage = 0;
4798a83145eSjcs break;
4808a83145eSjcs case 3:
4818a83145eSjcs c->designator_index = dval;
4828a83145eSjcs break;
4838a83145eSjcs case 4:
4848a83145eSjcs c->designator_minimum = dval;
4858a83145eSjcs break;
4868a83145eSjcs case 5:
4878a83145eSjcs c->designator_maximum = dval;
4888a83145eSjcs break;
4898a83145eSjcs case 7:
4908a83145eSjcs c->string_index = dval;
4918a83145eSjcs break;
4928a83145eSjcs case 8:
4938a83145eSjcs c->string_minimum = dval;
4948a83145eSjcs break;
4958a83145eSjcs case 9:
4968a83145eSjcs c->string_maximum = dval;
4978a83145eSjcs break;
4988a83145eSjcs case 10:
4998a83145eSjcs c->set_delimiter = dval;
5008a83145eSjcs break;
5018a83145eSjcs default:
5028a83145eSjcs DPRINTF("Local bTag=%d\n", bTag);
5038a83145eSjcs break;
5048a83145eSjcs }
5058a83145eSjcs break;
5068a83145eSjcs default:
5078a83145eSjcs DPRINTF("default bType=%d\n", bType);
5088a83145eSjcs break;
5098a83145eSjcs }
5108a83145eSjcs }
5118a83145eSjcs return (0);
5128a83145eSjcs }
5138a83145eSjcs
5148a83145eSjcs int
hid_report_size(const void * buf,int len,enum hid_kind k,u_int8_t id)5158a83145eSjcs hid_report_size(const void *buf, int len, enum hid_kind k, u_int8_t id)
5168a83145eSjcs {
5178a83145eSjcs struct hid_data *d;
5188a83145eSjcs struct hid_item h;
5198a83145eSjcs int lo, hi;
5208a83145eSjcs
5218a83145eSjcs h.report_ID = 0;
5228a83145eSjcs lo = hi = -1;
5238a83145eSjcs DPRINTF("hid_report_size: kind=%d id=%d\n", k, id);
5248a83145eSjcs for (d = hid_start_parse(buf, len, k); hid_get_item(d, &h); ) {
5258a83145eSjcs DPRINTF("hid_report_size: item kind=%d id=%d pos=%d "
5268a83145eSjcs "size=%d count=%d\n",
5278a83145eSjcs h.kind, h.report_ID, h.loc.pos, h.loc.size,
5288a83145eSjcs h.loc.count);
5298a83145eSjcs if (h.report_ID == id && h.kind == k) {
5308a83145eSjcs if (lo < 0) {
5318a83145eSjcs lo = h.loc.pos;
5328a83145eSjcs #ifdef DIAGNOSTIC
5338a83145eSjcs if (lo != 0) {
5348a83145eSjcs printf("hid_report_size: lo != 0\n");
5358a83145eSjcs }
5368a83145eSjcs #endif
5378a83145eSjcs }
5388a83145eSjcs hi = h.loc.pos + h.loc.size * h.loc.count;
5398a83145eSjcs DPRINTF("hid_report_size: lo=%d hi=%d\n", lo, hi);
5408a83145eSjcs
5418a83145eSjcs }
5428a83145eSjcs }
5438a83145eSjcs hid_end_parse(d);
5448a83145eSjcs return ((hi - lo + 7) / 8);
5458a83145eSjcs }
5468a83145eSjcs
5478a83145eSjcs int
hid_locate(const void * desc,int size,int32_t u,uint8_t id,enum hid_kind k,struct hid_location * loc,uint32_t * flags)5488a83145eSjcs hid_locate(const void *desc, int size, int32_t u, uint8_t id, enum hid_kind k,
5498a83145eSjcs struct hid_location *loc, uint32_t *flags)
5508a83145eSjcs {
5518a83145eSjcs struct hid_data *d;
5528a83145eSjcs struct hid_item h;
5538a83145eSjcs
5548a83145eSjcs h.report_ID = 0;
5558a83145eSjcs DPRINTF("hid_locate: enter usage=0x%x kind=%d id=%d\n", u, k, id);
5568a83145eSjcs for (d = hid_start_parse(desc, size, k); hid_get_item(d, &h); ) {
5578a83145eSjcs DPRINTF("hid_locate: usage=0x%x kind=%d id=%d flags=0x%x\n",
5588a83145eSjcs h.usage, h.kind, h.report_ID, h.flags);
5598a83145eSjcs if (h.kind == k && !(h.flags & HIO_CONST) &&
5608a83145eSjcs h.usage == u && h.report_ID == id) {
5618a83145eSjcs if (loc != NULL)
5628a83145eSjcs *loc = h.loc;
5638a83145eSjcs if (flags != NULL)
5648a83145eSjcs *flags = h.flags;
5658a83145eSjcs hid_end_parse(d);
5668a83145eSjcs return (1);
5678a83145eSjcs }
5688a83145eSjcs }
5698a83145eSjcs hid_end_parse(d);
5708a83145eSjcs if (loc != NULL)
5718a83145eSjcs loc->size = 0;
5728a83145eSjcs if (flags != NULL)
5738a83145eSjcs *flags = 0;
5748a83145eSjcs return (0);
5758a83145eSjcs }
5768a83145eSjcs
57727d6d6a4Sjcs uint32_t
hid_get_data_sub(const uint8_t * buf,int len,struct hid_location * loc,int is_signed)57827d6d6a4Sjcs hid_get_data_sub(const uint8_t *buf, int len, struct hid_location *loc,
57927d6d6a4Sjcs int is_signed)
5808a83145eSjcs {
5818a83145eSjcs uint32_t hpos = loc->pos;
5828a83145eSjcs uint32_t hsize = loc->size;
5838a83145eSjcs uint32_t data;
5848a83145eSjcs uint32_t rpos;
5858a83145eSjcs uint8_t n;
5868a83145eSjcs
58727d6d6a4Sjcs DPRINTF("hid_get_data_sub: loc %d/%d\n", hpos, hsize);
5888a83145eSjcs
5898a83145eSjcs /* Range check and limit */
5908a83145eSjcs if (hsize == 0)
5918a83145eSjcs return (0);
5928a83145eSjcs if (hsize > 32)
5938a83145eSjcs hsize = 32;
5948a83145eSjcs
5958a83145eSjcs /* Get data in a safe way */
5968a83145eSjcs data = 0;
5978a83145eSjcs rpos = (hpos / 8);
5988a83145eSjcs n = (hsize + 7) / 8;
5998a83145eSjcs rpos += n;
6008a83145eSjcs while (n--) {
6018a83145eSjcs rpos--;
6028a83145eSjcs if (rpos < len)
6038a83145eSjcs data |= buf[rpos] << (8 * n);
6048a83145eSjcs }
6058a83145eSjcs
6068a83145eSjcs /* Correctly shift down data */
6078a83145eSjcs data = (data >> (hpos % 8));
6088a83145eSjcs n = 32 - hsize;
6098a83145eSjcs
61027d6d6a4Sjcs /* Mask and sign extend in one */
61127d6d6a4Sjcs if (is_signed != 0)
6128a83145eSjcs data = (int32_t)((int32_t)data << n) >> n;
61327d6d6a4Sjcs else
61427d6d6a4Sjcs data = (uint32_t)((uint32_t)data << n) >> n;
6158a83145eSjcs
61627d6d6a4Sjcs DPRINTF("hid_get_data_sub: loc %d/%d = %lu\n",
6178a83145eSjcs loc->pos, loc->size, (long)data);
6188a83145eSjcs return (data);
6198a83145eSjcs }
6208a83145eSjcs
62127d6d6a4Sjcs int32_t
hid_get_data(const uint8_t * buf,int len,struct hid_location * loc)62227d6d6a4Sjcs hid_get_data(const uint8_t *buf, int len, struct hid_location *loc)
62327d6d6a4Sjcs {
62427d6d6a4Sjcs return (hid_get_data_sub(buf, len, loc, 1));
62527d6d6a4Sjcs }
62627d6d6a4Sjcs
62727d6d6a4Sjcs uint32_t
hid_get_udata(const uint8_t * buf,int len,struct hid_location * loc)62827d6d6a4Sjcs hid_get_udata(const uint8_t *buf, int len, struct hid_location *loc)
62927d6d6a4Sjcs {
63027d6d6a4Sjcs return (hid_get_data_sub(buf, len, loc, 0));
63127d6d6a4Sjcs }
63227d6d6a4Sjcs
6338a83145eSjcs int
hid_is_collection(const void * desc,int size,uint8_t id,int32_t usage)6348a83145eSjcs hid_is_collection(const void *desc, int size, uint8_t id, int32_t usage)
6358a83145eSjcs {
6368a83145eSjcs struct hid_data *hd;
6378a83145eSjcs struct hid_item hi;
6388a83145eSjcs uint32_t coll_usage = ~0;
6398a83145eSjcs
640fdc486edSanton hd = hid_start_parse(desc, size, hid_all);
6418a83145eSjcs
6428a83145eSjcs DPRINTF("%s: id=%d usage=0x%x\n", __func__, id, usage);
6438a83145eSjcs while (hid_get_item(hd, &hi)) {
6448a83145eSjcs DPRINTF("%s: kind=%d id=%d usage=0x%x(0x%x)\n", __func__,
6458a83145eSjcs hi.kind, hi.report_ID, hi.usage, coll_usage);
6468a83145eSjcs if (hi.kind == hid_collection &&
6478a83145eSjcs hi.collection == HCOLL_APPLICATION)
6488a83145eSjcs coll_usage = hi.usage;
6498a83145eSjcs if (hi.kind == hid_endcollection &&
6508a83145eSjcs coll_usage == usage && hi.report_ID == id) {
6518a83145eSjcs DPRINTF("%s: found\n", __func__);
6528a83145eSjcs hid_end_parse(hd);
6538a83145eSjcs return (1);
6548a83145eSjcs }
6558a83145eSjcs }
6568a83145eSjcs DPRINTF("%s: not found\n", __func__);
6578a83145eSjcs hid_end_parse(hd);
6588a83145eSjcs return (0);
6598a83145eSjcs }
660cdf25420Smiod
661cdf25420Smiod struct hid_data *
hid_get_collection_data(const void * desc,int size,int32_t usage,uint32_t collection)662cdf25420Smiod hid_get_collection_data(const void *desc, int size, int32_t usage,
663cdf25420Smiod uint32_t collection)
664cdf25420Smiod {
665cdf25420Smiod struct hid_data *hd;
666cdf25420Smiod struct hid_item hi;
667cdf25420Smiod
668cdf25420Smiod hd = hid_start_parse(desc, size, hid_all);
669cdf25420Smiod
670cdf25420Smiod DPRINTF("%s: usage=0x%x\n", __func__, usage);
671cdf25420Smiod while (hid_get_item(hd, &hi)) {
672cdf25420Smiod DPRINTF("%s: kind=%d id=%d usage=0x%x(0x%x)\n", __func__,
673cdf25420Smiod hi.kind, hi.report_ID, hi.usage, usage);
674cdf25420Smiod if (hi.kind == hid_collection &&
675cdf25420Smiod hi.collection == collection && hi.usage == usage) {
676cdf25420Smiod DPRINTF("%s: found\n", __func__);
677cdf25420Smiod return hd;
678cdf25420Smiod }
679cdf25420Smiod }
680cdf25420Smiod DPRINTF("%s: not found\n", __func__);
681cdf25420Smiod hid_end_parse(hd);
682cdf25420Smiod return NULL;
683cdf25420Smiod }
684cdf25420Smiod
685cdf25420Smiod int
hid_get_id_of_collection(const void * desc,int size,int32_t usage,uint32_t collection)686cdf25420Smiod hid_get_id_of_collection(const void *desc, int size, int32_t usage,
687cdf25420Smiod uint32_t collection)
688cdf25420Smiod {
689cdf25420Smiod struct hid_data *hd;
690cdf25420Smiod struct hid_item hi;
691cdf25420Smiod
692cdf25420Smiod hd = hid_start_parse(desc, size, hid_all);
693cdf25420Smiod
694*f63e15d8Smglocker DPRINTF("%s: usage=0x%x\n", __func__, usage);
695cdf25420Smiod while (hid_get_item(hd, &hi)) {
696cdf25420Smiod DPRINTF("%s: kind=%d id=%d usage=0x%x(0x%x)\n", __func__,
697cdf25420Smiod hi.kind, hi.report_ID, hi.usage, usage);
698cdf25420Smiod if (hi.kind == hid_collection &&
699cdf25420Smiod hi.collection == collection && hi.usage == usage) {
700cdf25420Smiod DPRINTF("%s: found\n", __func__);
701cdf25420Smiod hid_end_parse(hd);
702cdf25420Smiod return hi.report_ID;
703cdf25420Smiod }
704cdf25420Smiod }
705cdf25420Smiod DPRINTF("%s: not found\n", __func__);
706cdf25420Smiod hid_end_parse(hd);
707cdf25420Smiod return -1;
708cdf25420Smiod }
709