1 /* $NetBSD: hid.c,v 1.15 2000/04/27 15:26:46 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/hid.c,v 1.11.2.6 2002/11/06 14:03:37 joe Exp $ */
3 /* $DragonFly: src/sys/bus/usb/hid.c,v 1.2 2003/06/17 04:28:32 dillon Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #if defined(__NetBSD__)
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbhid.h>
52 #include <dev/usb/hid.h>
55 #define DPRINTF(x) if (usbdebug) logprintf x
56 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
63 Static void hid_clear_local(struct hid_item *);
71 int32_t usages[MAXUSAGE];
80 hid_clear_local(struct hid_item *c)
85 c->designator_index = 0;
86 c->designator_minimum = 0;
87 c->designator_maximum = 0;
89 c->string_minimum = 0;
90 c->string_maximum = 0;
95 hid_start_parse(void *d, int len, int kindset)
99 s = malloc(sizeof *s, M_TEMP, M_WAITOK);
100 memset(s, 0, sizeof *s);
102 s->end = (char *)d + len;
103 s->kindset = kindset;
108 hid_end_parse(struct hid_data *s)
110 while (s->cur.next != NULL) {
111 struct hid_item *hi = s->cur.next->next;
112 free(s->cur.next, M_TEMP);
119 hid_get_item(struct hid_data *s, struct hid_item *h)
121 struct hid_item *c = &s->cur;
122 unsigned int bTag, bType, bSize;
131 if (s->multimax != 0) {
132 if (s->multi < s->multimax) {
133 c->usage = s->usages[min(s->multi, s->nu-1)];
136 c->loc.pos += c->loc.size;
140 c->loc.count = s->multimax;
159 bType = 0xff; /* XXX what should it be */
163 bType = (bSize >> 2) & 3;
165 if (bSize == 3) bSize = 4;
175 dval = (int8_t)*data++;
179 dval |= *data++ << 8;
180 dval = (int16_t)dval;
184 dval |= *data++ << 8;
185 dval |= *data++ << 16;
186 dval |= *data++ << 24;
189 printf("BAD LENGTH %d\n", bSize);
197 if (!(s->kindset & (1 << hid_input)))
202 if (c->flags & HIO_VARIABLE) {
203 s->multimax = c->loc.count;
207 for (i = c->usage_minimum;
208 i <= c->usage_maximum;
210 s->usages[s->nu] = i;
211 if (s->nu < MAXUSAGE-1)
221 c->loc.size * c->loc.count;
227 if (!(s->kindset & (1 << hid_output)))
229 c->kind = hid_output;
232 case 10: /* Collection */
233 c->kind = hid_collection;
234 c->collection = dval;
240 case 11: /* Feature */
241 if (!(s->kindset & (1 << hid_feature)))
243 c->kind = hid_feature;
246 case 12: /* End collection */
247 c->kind = hid_endcollection;
254 printf("Main bTag=%d\n", bTag);
261 c->_usage_page = dval << 16;
264 c->logical_minimum = dval;
267 c->logical_maximum = dval;
270 c->physical_maximum = dval;
273 c->physical_maximum = dval;
276 c->unit_exponent = dval;
291 hi = malloc(sizeof *hi, M_TEMP, M_WAITOK);
303 printf("Global bTag=%d\n", bTag);
311 dval = c->_usage_page | (dval&0xff);
313 dval = c->_usage_page | (dval&0xffff);
315 if (s->nu < MAXUSAGE)
316 s->usages[s->nu++] = dval;
322 dval = c->_usage_page | (dval&0xff);
324 dval = c->_usage_page | (dval&0xffff);
325 c->usage_minimum = dval;
329 dval = c->_usage_page | (dval&0xff);
331 dval = c->_usage_page | (dval&0xffff);
332 c->usage_maximum = dval;
335 c->designator_index = dval;
338 c->designator_minimum = dval;
341 c->designator_maximum = dval;
344 c->string_index = dval;
347 c->string_minimum = dval;
350 c->string_maximum = dval;
353 c->set_delimiter = dval;
356 printf("Local bTag=%d\n", bTag);
361 printf("default bType=%d\n", bType);
368 hid_report_size(void *buf, int len, enum hid_kind k, u_int8_t *idp)
375 for (d = hid_start_parse(buf, len, 1<<k); hid_get_item(d, &h); )
376 if (h.report_ID != 0)
382 *idp = id; /* XXX wrong */
385 return ((size + 7) / 8);
389 hid_locate(void *desc, int size, u_int32_t u, enum hid_kind k,
390 struct hid_location *loc, u_int32_t *flags)
395 for (d = hid_start_parse(desc, size, 1<<k); hid_get_item(d, &h); ) {
396 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
411 hid_get_data(u_char *buf, struct hid_location *loc)
413 u_int hpos = loc->pos;
414 u_int hsize = loc->size;
418 DPRINTFN(10, ("hid_get_data: loc %d/%d\n", hpos, hsize));
425 for (i = hpos; i < hpos+hsize; i += 8)
426 data |= buf[i / 8] << ((i / 8 - s) * 8);
428 data &= (1 << hsize) - 1;
431 data = ((int32_t)data << hsize) >> hsize;
432 DPRINTFN(10,("hid_get_data: loc %d/%d = %lu\n",
433 loc->pos, loc->size, (long)data));
438 hid_is_collection(void *desc, int size, u_int32_t usage)
444 hd = hid_start_parse(desc, size, hid_input);
448 err = hid_get_item(hd, &hi) &&
449 hi.kind == hid_collection &&