Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / usb / hid.c
blobb9e2adbb07e177c96ebbda4085d4daf4dd9823af
1 /* $NetBSD: hid.c,v 1.27 2007/09/08 07:46:13 plunky Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/hid.c,v 1.11 1999/11/17 22:33:39 n_hibma Exp $ */
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: hid.c,v 1.27 2007/09/08 07:46:13 plunky Exp $");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #if defined(__NetBSD__)
40 #include <sys/kernel.h>
41 #endif
42 #include <sys/malloc.h>
44 #include <dev/usb/usb.h>
45 #include <dev/usb/usbhid.h>
47 #include <dev/usb/hid.h>
49 #ifdef UHIDEV_DEBUG
50 #define DPRINTF(x) if (uhidevdebug) logprintf x
51 #define DPRINTFN(n,x) if (uhidevdebug>(n)) logprintf x
52 extern int uhidevdebug;
53 #else
54 #define DPRINTF(x)
55 #define DPRINTFN(n,x)
56 #endif
58 Static void hid_clear_local(struct hid_item *);
60 #define MAXUSAGE 256
61 struct hid_data {
62 const u_char *start;
63 const u_char *end;
64 const u_char *p;
65 struct hid_item cur;
66 int32_t usages[MAXUSAGE];
67 int nu;
68 int minset;
69 int multi;
70 int multimax;
71 enum hid_kind kind;
74 Static void
75 hid_clear_local(struct hid_item *c)
78 DPRINTFN(5,("hid_clear_local\n"));
79 c->usage = 0;
80 c->usage_minimum = 0;
81 c->usage_maximum = 0;
82 c->designator_index = 0;
83 c->designator_minimum = 0;
84 c->designator_maximum = 0;
85 c->string_index = 0;
86 c->string_minimum = 0;
87 c->string_maximum = 0;
88 c->set_delimiter = 0;
91 struct hid_data *
92 hid_start_parse(const void *d, int len, enum hid_kind kind)
94 struct hid_data *s;
96 s = malloc(sizeof *s, M_TEMP, M_WAITOK|M_ZERO);
97 s->start = s->p = d;
98 s->end = (const char *)d + len;
99 s->kind = kind;
100 return (s);
103 void
104 hid_end_parse(struct hid_data *s)
107 while (s->cur.next != NULL) {
108 struct hid_item *hi = s->cur.next->next;
109 free(s->cur.next, M_TEMP);
110 s->cur.next = hi;
112 free(s, M_TEMP);
116 hid_get_item(struct hid_data *s, struct hid_item *h)
118 struct hid_item *c = &s->cur;
119 unsigned int bTag, bType, bSize;
120 u_int32_t oldpos;
121 const u_char *data;
122 int32_t dval;
123 const u_char *p;
124 struct hid_item *hi;
125 int i;
126 enum hid_kind retkind;
128 top:
129 DPRINTFN(5,("hid_get_item: multi=%d multimax=%d\n",
130 s->multi, s->multimax));
131 if (s->multimax != 0) {
132 if (s->multi < s->multimax) {
133 c->usage = s->usages[min(s->multi, s->nu-1)];
134 s->multi++;
135 *h = *c;
136 c->loc.pos += c->loc.size;
137 h->next = NULL;
138 DPRINTFN(5,("return multi\n"));
139 return (1);
140 } else {
141 c->loc.count = s->multimax;
142 s->multimax = 0;
143 s->nu = 0;
144 hid_clear_local(c);
147 for (;;) {
148 p = s->p;
149 if (p >= s->end)
150 return (0);
152 bSize = *p++;
153 if (bSize == 0xfe) {
154 /* long item */
155 bSize = *p++;
156 bSize |= *p++ << 8;
157 bTag = *p++;
158 data = p;
159 p += bSize;
160 bType = 0xff; /* XXX what should it be */
161 } else {
162 /* short item */
163 bTag = bSize >> 4;
164 bType = (bSize >> 2) & 3;
165 bSize &= 3;
166 if (bSize == 3) bSize = 4;
167 data = p;
168 p += bSize;
170 s->p = p;
171 switch(bSize) {
172 case 0:
173 dval = 0;
174 break;
175 case 1:
176 dval = /*(int8_t)*/ *data++;
177 break;
178 case 2:
179 dval = *data++;
180 dval |= *data++ << 8;
181 dval = /*(int16_t)*/ dval;
182 break;
183 case 4:
184 dval = *data++;
185 dval |= *data++ << 8;
186 dval |= *data++ << 16;
187 dval |= *data++ << 24;
188 break;
189 default:
190 printf("BAD LENGTH %d\n", bSize);
191 continue;
194 DPRINTFN(5,("hid_get_item: bType=%d bTag=%d dval=%d\n",
195 bType, bTag, dval));
196 switch (bType) {
197 case 0: /* Main */
198 switch (bTag) {
199 case 8: /* Input */
200 retkind = hid_input;
201 ret:
202 if (s->kind != retkind) {
203 s->minset = 0;
204 s->nu = 0;
205 hid_clear_local(c);
206 continue;
208 c->kind = retkind;
209 c->flags = dval;
210 if (c->flags & HIO_VARIABLE) {
211 s->multimax = c->loc.count;
212 s->multi = 0;
213 c->loc.count = 1;
214 if (s->minset) {
215 for (i = c->usage_minimum;
216 i <= c->usage_maximum;
217 i++) {
218 s->usages[s->nu] = i;
219 if (s->nu < MAXUSAGE-1)
220 s->nu++;
222 s->minset = 0;
224 goto top;
225 } else {
226 c->usage = c->_usage_page; /* XXX */
227 *h = *c;
228 h->next = NULL;
229 c->loc.pos +=
230 c->loc.size * c->loc.count;
231 s->minset = 0;
232 s->nu = 0;
233 hid_clear_local(c);
234 return (1);
236 case 9: /* Output */
237 retkind = hid_output;
238 goto ret;
239 case 10: /* Collection */
240 c->kind = hid_collection;
241 c->collection = dval;
242 c->collevel++;
243 *h = *c;
244 hid_clear_local(c);
245 s->nu = 0;
246 return (1);
247 case 11: /* Feature */
248 retkind = hid_feature;
249 goto ret;
250 case 12: /* End collection */
251 c->kind = hid_endcollection;
252 c->collevel--;
253 *h = *c;
254 s->nu = 0;
255 return (1);
256 default:
257 printf("Main bTag=%d\n", bTag);
258 break;
260 break;
261 case 1: /* Global */
262 switch (bTag) {
263 case 0:
264 c->_usage_page = dval << 16;
265 break;
266 case 1:
267 c->logical_minimum = dval;
268 break;
269 case 2:
270 c->logical_maximum = dval;
271 break;
272 case 3:
273 c->physical_maximum = dval;
274 break;
275 case 4:
276 c->physical_maximum = dval;
277 break;
278 case 5:
279 c->unit_exponent = dval;
280 break;
281 case 6:
282 c->unit = dval;
283 break;
284 case 7:
285 c->loc.size = dval;
286 break;
287 case 8:
288 c->report_ID = dval;
289 c->loc.pos = 0;
290 break;
291 case 9:
292 c->loc.count = dval;
293 break;
294 case 10: /* Push */
295 hi = malloc(sizeof *hi, M_TEMP, M_WAITOK);
296 *hi = *c;
297 c->next = hi;
298 break;
299 case 11: /* Pop */
300 hi = c->next;
301 oldpos = c->loc.pos;
302 *c = *hi;
303 c->loc.pos = oldpos;
304 free(hi, M_TEMP);
305 break;
306 default:
307 printf("Global bTag=%d\n", bTag);
308 break;
310 break;
311 case 2: /* Local */
312 switch (bTag) {
313 case 0:
314 if (bSize == 1)
315 dval = c->_usage_page | (dval&0xff);
316 else if (bSize == 2)
317 dval = c->_usage_page | (dval&0xffff);
318 c->usage = dval;
319 if (s->nu < MAXUSAGE)
320 s->usages[s->nu++] = dval;
321 /* else XXX */
322 break;
323 case 1:
324 s->minset = 1;
325 if (bSize == 1)
326 dval = c->_usage_page | (dval&0xff);
327 else if (bSize == 2)
328 dval = c->_usage_page | (dval&0xffff);
329 c->usage_minimum = dval;
330 break;
331 case 2:
332 if (bSize == 1)
333 dval = c->_usage_page | (dval&0xff);
334 else if (bSize == 2)
335 dval = c->_usage_page | (dval&0xffff);
336 c->usage_maximum = dval;
337 break;
338 case 3:
339 c->designator_index = dval;
340 break;
341 case 4:
342 c->designator_minimum = dval;
343 break;
344 case 5:
345 c->designator_maximum = dval;
346 break;
347 case 7:
348 c->string_index = dval;
349 break;
350 case 8:
351 c->string_minimum = dval;
352 break;
353 case 9:
354 c->string_maximum = dval;
355 break;
356 case 10:
357 c->set_delimiter = dval;
358 break;
359 default:
360 printf("Local bTag=%d\n", bTag);
361 break;
363 break;
364 default:
365 printf("default bType=%d\n", bType);
366 break;
372 hid_report_size(const void *buf, int len, enum hid_kind k, u_int8_t id)
374 struct hid_data *d;
375 struct hid_item h;
376 int lo, hi;
378 h.report_ID = 0;
379 lo = hi = -1;
380 DPRINTFN(2,("hid_report_size: kind=%d id=%d\n", k, id));
381 for (d = hid_start_parse(buf, len, k); hid_get_item(d, &h); ) {
382 DPRINTFN(2,("hid_report_size: item kind=%d id=%d pos=%d "
383 "size=%d count=%d\n",
384 h.kind, h.report_ID, h.loc.pos, h.loc.size,
385 h.loc.count));
386 if (h.report_ID == id && h.kind == k) {
387 if (lo < 0) {
388 lo = h.loc.pos;
389 #ifdef DIAGNOSTIC
390 if (lo != 0) {
391 printf("hid_report_size: lo != 0\n");
393 #endif
395 hi = h.loc.pos + h.loc.size * h.loc.count;
396 DPRINTFN(2,("hid_report_size: lo=%d hi=%d\n", lo, hi));
399 hid_end_parse(d);
400 return ((hi - lo + 7) / 8);
404 hid_locate(const void *desc, int size, u_int32_t u, u_int8_t id, enum hid_kind k,
405 struct hid_location *loc, u_int32_t *flags)
407 struct hid_data *d;
408 struct hid_item h;
410 h.report_ID = 0;
411 DPRINTFN(5,("hid_locate: enter usage=0x%x kind=%d id=%d\n", u, k, id));
412 for (d = hid_start_parse(desc, size, k); hid_get_item(d, &h); ) {
413 DPRINTFN(5,("hid_locate: usage=0x%x kind=%d id=%d flags=0x%x\n",
414 h.usage, h.kind, h.report_ID, h.flags));
415 if (h.kind == k && !(h.flags & HIO_CONST) &&
416 h.usage == u && h.report_ID == id) {
417 if (loc != NULL)
418 *loc = h.loc;
419 if (flags != NULL)
420 *flags = h.flags;
421 hid_end_parse(d);
422 return (1);
425 hid_end_parse(d);
426 loc->size = 0;
427 return (0);
430 u_long
431 hid_get_data(u_char *buf, struct hid_location *loc)
433 u_int hpos = loc->pos;
434 u_int hsize = loc->size;
435 u_int32_t data;
436 int i, s;
438 DPRINTFN(10, ("hid_get_data: loc %d/%d\n", hpos, hsize));
440 if (hsize == 0)
441 return (0);
443 data = 0;
444 s = hpos / 8;
445 for (i = hpos; i < hpos+hsize; i += 8)
446 data |= buf[i / 8] << ((i / 8 - s) * 8);
447 data >>= hpos % 8;
448 data &= (1 << hsize) - 1;
449 hsize = 32 - hsize;
450 /* Sign extend */
451 data = ((int32_t)data << hsize) >> hsize;
452 DPRINTFN(10,("hid_get_data: loc %d/%d = %lu\n",
453 loc->pos, loc->size, (long)data));
454 return (data);
458 * hid_is_collection(desc, size, id, usage)
460 * This function is broken in the following way.
462 * It is used to discover if the given 'id' is part of 'usage' collection
463 * in the descriptor in order to match report id against device type.
465 * The semantics of hid_start_parse() means though, that only a single
466 * kind of report is considered. The current HID code that uses this for
467 * matching is actually only looking for input reports, so this works
468 * for now.
470 * This function could try all report kinds (input, output and feature)
471 * consecutively if necessary, but it may be better to integrate the
472 * libusbhid code which can consider multiple report kinds simultaneously
474 * Needs some thought.
477 hid_is_collection(const void *desc, int size, u_int8_t id, u_int32_t usage)
479 struct hid_data *hd;
480 struct hid_item hi;
481 u_int32_t coll_usage = ~0;
483 hd = hid_start_parse(desc, size, hid_input);
484 if (hd == NULL)
485 return (0);
487 DPRINTFN(2,("hid_is_collection: id=%d usage=0x%x\n", id, usage));
488 while (hid_get_item(hd, &hi)) {
489 DPRINTFN(2,("hid_is_collection: kind=%d id=%d usage=0x%x"
490 "(0x%x)\n",
491 hi.kind, hi.report_ID, hi.usage, coll_usage));
493 if (hi.kind == hid_collection &&
494 hi.collection == HCOLL_APPLICATION)
495 coll_usage = hi.usage;
497 if (hi.kind == hid_endcollection)
498 coll_usage = ~0;
500 if (hi.kind == hid_input &&
501 coll_usage == usage &&
502 hi.report_ID == id) {
503 DPRINTFN(2,("hid_is_collection: found\n"));
504 hid_end_parse(hd);
505 return (1);
508 DPRINTFN(2,("hid_is_collection: not found\n"));
509 hid_end_parse(hd);
510 return (0);