2 * $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $
3 * $FreeBSD: src/sys/dev/usb/hid.c,v 1.23 2003/08/24 17:55:54 obrien Exp $
4 * $DragonFly: src/sys/bus/usb/hid.c,v 1.4 2003/12/30 01:01:44 dillon Exp $
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #if defined(__NetBSD__)
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
56 #define DPRINTF(x) if (usbdebug) logprintf x
57 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
64 Static void hid_clear_local(struct hid_item *);
72 int32_t usages[MAXUSAGE];
81 hid_clear_local(struct hid_item *c)
87 c->designator_index = 0;
88 c->designator_minimum = 0;
89 c->designator_maximum = 0;
91 c->string_minimum = 0;
92 c->string_maximum = 0;
97 hid_start_parse(void *d, int len, int kindset)
101 s = malloc(sizeof *s, M_TEMP, M_WAITOK|M_ZERO);
103 s->end = (char *)d + len;
104 s->kindset = kindset;
109 hid_end_parse(struct hid_data *s)
112 while (s->cur.next != NULL) {
113 struct hid_item *hi = s->cur.next->next;
114 free(s->cur.next, M_TEMP);
121 hid_get_item(struct hid_data *s, struct hid_item *h)
123 struct hid_item *c = &s->cur;
124 unsigned int bTag, bType, bSize;
133 if (s->multimax != 0) {
134 if (s->multi < s->multimax) {
135 c->usage = s->usages[min(s->multi, s->nu-1)];
138 c->loc.pos += c->loc.size;
142 c->loc.count = s->multimax;
161 bType = 0xff; /* XXX what should it be */
165 bType = (bSize >> 2) & 3;
167 if (bSize == 3) bSize = 4;
177 dval = (int8_t)*data++;
181 dval |= *data++ << 8;
182 dval = (int16_t)dval;
186 dval |= *data++ << 8;
187 dval |= *data++ << 16;
188 dval |= *data++ << 24;
191 printf("BAD LENGTH %d\n", bSize);
199 if (!(s->kindset & (1 << hid_input)))
204 if (c->flags & HIO_VARIABLE) {
205 s->multimax = c->loc.count;
209 for (i = c->usage_minimum;
210 i <= c->usage_maximum;
212 s->usages[s->nu] = i;
213 if (s->nu < MAXUSAGE-1)
223 c->loc.size * c->loc.count;
229 if (!(s->kindset & (1 << hid_output)))
231 c->kind = hid_output;
234 case 10: /* Collection */
235 c->kind = hid_collection;
236 c->collection = dval;
242 case 11: /* Feature */
243 if (!(s->kindset & (1 << hid_feature)))
245 c->kind = hid_feature;
248 case 12: /* End collection */
249 c->kind = hid_endcollection;
256 printf("Main bTag=%d\n", bTag);
263 c->_usage_page = dval << 16;
266 c->logical_minimum = dval;
269 c->logical_maximum = dval;
272 c->physical_maximum = dval;
275 c->physical_maximum = dval;
278 c->unit_exponent = dval;
293 hi = malloc(sizeof *hi, M_TEMP, M_WAITOK);
305 printf("Global bTag=%d\n", bTag);
313 dval = c->_usage_page | (dval&0xff);
315 dval = c->_usage_page | (dval&0xffff);
317 if (s->nu < MAXUSAGE)
318 s->usages[s->nu++] = dval;
324 dval = c->_usage_page | (dval&0xff);
326 dval = c->_usage_page | (dval&0xffff);
327 c->usage_minimum = dval;
331 dval = c->_usage_page | (dval&0xff);
333 dval = c->_usage_page | (dval&0xffff);
334 c->usage_maximum = dval;
337 c->designator_index = dval;
340 c->designator_minimum = dval;
343 c->designator_maximum = dval;
346 c->string_index = dval;
349 c->string_minimum = dval;
352 c->string_maximum = dval;
355 c->set_delimiter = dval;
358 printf("Local bTag=%d\n", bTag);
363 printf("default bType=%d\n", bType);
370 hid_report_size(void *buf, int len, enum hid_kind k, u_int8_t *idp)
377 for (d = hid_start_parse(buf, len, 1<<k); hid_get_item(d, &h); )
378 if (h.report_ID != 0)
384 *idp = id; /* XXX wrong */
387 return ((size + 7) / 8);
391 hid_locate(void *desc, int size, u_int32_t u, enum hid_kind k,
392 struct hid_location *loc, u_int32_t *flags)
397 for (d = hid_start_parse(desc, size, 1<<k); hid_get_item(d, &h); ) {
398 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
413 hid_get_data(u_char *buf, struct hid_location *loc)
415 u_int hpos = loc->pos;
416 u_int hsize = loc->size;
420 DPRINTFN(10, ("hid_get_data: loc %d/%d\n", hpos, hsize));
427 for (i = hpos; i < hpos+hsize; i += 8)
428 data |= buf[i / 8] << ((i / 8 - s) * 8);
430 data &= (1 << hsize) - 1;
433 data = ((int32_t)data << hsize) >> hsize;
434 DPRINTFN(10,("hid_get_data: loc %d/%d = %lu\n",
435 loc->pos, loc->size, (long)data));
440 hid_is_collection(void *desc, int size, u_int32_t usage)
446 hd = hid_start_parse(desc, size, hid_input);
450 err = hid_get_item(hd, &hi) &&
451 hi.kind == hid_collection &&