ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/src/trunk/sys/dev/usb/hid.c
Revision: 2227
Committed: Sat Nov 29 16:07:31 2008 UTC (15 years, 5 months ago) by laffer1
Content type: text/plain
File size: 10174 byte(s)
Log Message:
merge enhancements

File Contents

# Content
1 /* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
2
3
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/sys/dev/usb/hid.c,v 1.29 2007/06/20 05:10:52 imp Exp $");
6 /*-
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
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.
29 *
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.
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbhid.h>
49
50 #include <dev/usb/hid.h>
51
52 #ifdef USB_DEBUG
53 #define DPRINTF(x) if (usbdebug) printf x
54 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x
55 extern int usbdebug;
56 #else
57 #define DPRINTF(x)
58 #define DPRINTFN(n,x)
59 #endif
60
61 static void hid_clear_local(struct hid_item *);
62
63 #define MAXUSAGE 100
64 struct hid_data {
65 u_char *start;
66 u_char *end;
67 u_char *p;
68 struct hid_item cur;
69 int32_t usages[MAXUSAGE];
70 int nu;
71 int minset;
72 int multi;
73 int multimax;
74 int kindset;
75 };
76
77 static void
78 hid_clear_local(struct hid_item *c)
79 {
80
81 c->usage = 0;
82 c->usage_minimum = 0;
83 c->usage_maximum = 0;
84 c->designator_index = 0;
85 c->designator_minimum = 0;
86 c->designator_maximum = 0;
87 c->string_index = 0;
88 c->string_minimum = 0;
89 c->string_maximum = 0;
90 c->set_delimiter = 0;
91 }
92
93 struct hid_data *
94 hid_start_parse(void *d, int len, int kindset)
95 {
96 struct hid_data *s;
97
98 s = malloc(sizeof *s, M_TEMP, M_WAITOK|M_ZERO);
99 s->start = s->p = d;
100 s->end = (char *)d + len;
101 s->kindset = kindset;
102 return (s);
103 }
104
105 void
106 hid_end_parse(struct hid_data *s)
107 {
108
109 while (s->cur.next != NULL) {
110 struct hid_item *hi = s->cur.next->next;
111 free(s->cur.next, M_TEMP);
112 s->cur.next = hi;
113 }
114 free(s, M_TEMP);
115 }
116
117 int
118 hid_get_item(struct hid_data *s, struct hid_item *h)
119 {
120 struct hid_item *c = &s->cur;
121 unsigned int bTag, bType, bSize;
122 u_int32_t oldpos;
123 u_char *data;
124 int32_t dval;
125 u_char *p;
126 struct hid_item *hi;
127 int i;
128
129 top:
130 if (s->multimax != 0) {
131 if (s->multi < s->multimax) {
132 c->usage = s->usages[min(s->multi, s->nu-1)];
133 s->multi++;
134 *h = *c;
135 c->loc.pos += c->loc.size;
136 h->next = 0;
137 return (1);
138 } else {
139 c->loc.count = s->multimax;
140 s->multimax = 0;
141 s->nu = 0;
142 hid_clear_local(c);
143 }
144 }
145 for (;;) {
146 p = s->p;
147 if (p >= s->end)
148 return (0);
149
150 bSize = *p++;
151 if (bSize == 0xfe) {
152 /* long item */
153 bSize = *p++;
154 bSize |= *p++ << 8;
155 bTag = *p++;
156 data = p;
157 p += bSize;
158 bType = 0xff; /* XXX what should it be */
159 } else {
160 /* short item */
161 bTag = bSize >> 4;
162 bType = (bSize >> 2) & 3;
163 bSize &= 3;
164 if (bSize == 3) bSize = 4;
165 data = p;
166 p += bSize;
167 }
168 s->p = p;
169 switch(bSize) {
170 case 0:
171 dval = 0;
172 break;
173 case 1:
174 dval = (int8_t)*data++;
175 break;
176 case 2:
177 dval = *data++;
178 dval |= *data++ << 8;
179 dval = (int16_t)dval;
180 break;
181 case 4:
182 dval = *data++;
183 dval |= *data++ << 8;
184 dval |= *data++ << 16;
185 dval |= *data++ << 24;
186 break;
187 default:
188 printf("BAD LENGTH %d\n", bSize);
189 continue;
190 }
191
192 switch (bType) {
193 case 0: /* Main */
194 switch (bTag) {
195 case 8: /* Input */
196 if (!(s->kindset & (1 << hid_input)))
197 continue;
198 c->kind = hid_input;
199 c->flags = dval;
200 ret:
201 if (c->flags & HIO_VARIABLE) {
202 s->multimax = c->loc.count;
203 s->multi = 0;
204 c->loc.count = 1;
205 if (s->minset) {
206 for (i = c->usage_minimum;
207 i <= c->usage_maximum;
208 i++) {
209 s->usages[s->nu] = i;
210 if (s->nu < MAXUSAGE-1)
211 s->nu++;
212 }
213 s->minset = 0;
214 }
215 goto top;
216 } else {
217 *h = *c;
218 h->next = 0;
219 c->loc.pos +=
220 c->loc.size * c->loc.count;
221 hid_clear_local(c);
222 s->minset = 0;
223 return (1);
224 }
225 case 9: /* Output */
226 if (!(s->kindset & (1 << hid_output)))
227 continue;
228 c->kind = hid_output;
229 c->flags = dval;
230 goto ret;
231 case 10: /* Collection */
232 c->kind = hid_collection;
233 c->collection = dval;
234 c->collevel++;
235 *h = *c;
236 hid_clear_local(c);
237 s->nu = 0;
238 return (1);
239 case 11: /* Feature */
240 if (!(s->kindset & (1 << hid_feature)))
241 continue;
242 c->kind = hid_feature;
243 c->flags = dval;
244 goto ret;
245 case 12: /* End collection */
246 c->kind = hid_endcollection;
247 c->collevel--;
248 *h = *c;
249 hid_clear_local(c);
250 s->nu = 0;
251 return (1);
252 default:
253 printf("Main bTag=%d\n", bTag);
254 break;
255 }
256 break;
257 case 1: /* Global */
258 switch (bTag) {
259 case 0:
260 c->_usage_page = dval << 16;
261 break;
262 case 1:
263 c->logical_minimum = dval;
264 break;
265 case 2:
266 c->logical_maximum = dval;
267 break;
268 case 3:
269 c->physical_maximum = dval;
270 break;
271 case 4:
272 c->physical_maximum = dval;
273 break;
274 case 5:
275 c->unit_exponent = dval;
276 break;
277 case 6:
278 c->unit = dval;
279 break;
280 case 7:
281 c->loc.size = dval;
282 break;
283 case 8:
284 c->report_ID = dval;
285 break;
286 case 9:
287 c->loc.count = dval;
288 break;
289 case 10: /* Push */
290 hi = malloc(sizeof *hi, M_TEMP, M_WAITOK);
291 *hi = s->cur;
292 c->next = hi;
293 break;
294 case 11: /* Pop */
295 hi = c->next;
296 oldpos = c->loc.pos;
297 s->cur = *hi;
298 c->loc.pos = oldpos;
299 free(hi, M_TEMP);
300 break;
301 default:
302 printf("Global bTag=%d\n", bTag);
303 break;
304 }
305 break;
306 case 2: /* Local */
307 switch (bTag) {
308 case 0:
309 if (bSize == 1)
310 dval = c->_usage_page | (dval&0xff);
311 else if (bSize == 2)
312 dval = c->_usage_page | (dval&0xffff);
313 c->usage = dval;
314 if (s->nu < MAXUSAGE)
315 s->usages[s->nu++] = dval;
316 /* else XXX */
317 break;
318 case 1:
319 s->minset = 1;
320 if (bSize == 1)
321 dval = c->_usage_page | (dval&0xff);
322 else if (bSize == 2)
323 dval = c->_usage_page | (dval&0xffff);
324 c->usage_minimum = dval;
325 break;
326 case 2:
327 if (bSize == 1)
328 dval = c->_usage_page | (dval&0xff);
329 else if (bSize == 2)
330 dval = c->_usage_page | (dval&0xffff);
331 c->usage_maximum = dval;
332 break;
333 case 3:
334 c->designator_index = dval;
335 break;
336 case 4:
337 c->designator_minimum = dval;
338 break;
339 case 5:
340 c->designator_maximum = dval;
341 break;
342 case 7:
343 c->string_index = dval;
344 break;
345 case 8:
346 c->string_minimum = dval;
347 break;
348 case 9:
349 c->string_maximum = dval;
350 break;
351 case 10:
352 c->set_delimiter = dval;
353 break;
354 default:
355 printf("Local bTag=%d\n", bTag);
356 break;
357 }
358 break;
359 default:
360 printf("default bType=%d\n", bType);
361 break;
362 }
363 }
364 }
365
366 int
367 hid_report_size(void *buf, int len, enum hid_kind k, u_int8_t *idp)
368 {
369 struct hid_data *d;
370 struct hid_item h;
371 int hi, lo, size, id;
372
373 id = 0;
374 hi = lo = -1;
375 for (d = hid_start_parse(buf, len, 1<<k); hid_get_item(d, &h); )
376 if (h.kind == k) {
377 if (h.report_ID != 0 && !id)
378 id = h.report_ID;
379 if (h.report_ID == id) {
380 if (lo < 0)
381 lo = h.loc.pos;
382 hi = h.loc.pos + h.loc.size * h.loc.count;
383 }
384 }
385 hid_end_parse(d);
386 size = hi - lo;
387 if (id != 0) {
388 size += 8;
389 *idp = id; /* XXX wrong */
390 } else
391 *idp = 0;
392 return ((size + 7) / 8);
393 }
394
395 int
396 hid_locate(void *desc, int size, u_int32_t u, enum hid_kind k,
397 struct hid_location *loc, u_int32_t *flags)
398 {
399 struct hid_data *d;
400 struct hid_item h;
401
402 for (d = hid_start_parse(desc, size, 1<<k); hid_get_item(d, &h); ) {
403 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
404 if (loc != NULL)
405 *loc = h.loc;
406 if (flags != NULL)
407 *flags = h.flags;
408 hid_end_parse(d);
409 return (1);
410 }
411 }
412 hid_end_parse(d);
413 loc->size = 0;
414 return (0);
415 }
416
417 u_long
418 hid_get_data(u_char *buf, struct hid_location *loc)
419 {
420 u_int hpos = loc->pos;
421 u_int hsize = loc->size;
422 u_int32_t data;
423 int i, s;
424
425 DPRINTFN(10, ("hid_get_data: loc %d/%d\n", hpos, hsize));
426
427 if (hsize == 0)
428 return (0);
429
430 data = 0;
431 s = hpos / 8;
432 for (i = hpos; i < hpos+hsize; i += 8)
433 data |= buf[i / 8] << ((i / 8 - s) * 8);
434 data >>= hpos % 8;
435 data &= (1 << hsize) - 1;
436 hsize = 32 - hsize;
437 /* Sign extend */
438 data = ((int32_t)data << hsize) >> hsize;
439 DPRINTFN(10,("hid_get_data: loc %d/%d = %lu\n",
440 loc->pos, loc->size, (long)data));
441 return (data);
442 }
443
444 int
445 hid_is_collection(void *desc, int size, u_int32_t usage)
446 {
447 struct hid_data *hd;
448 struct hid_item hi;
449 int err;
450
451 hd = hid_start_parse(desc, size, hid_input);
452 if (hd == NULL)
453 return (0);
454
455 err = hid_get_item(hd, &hi) &&
456 hi.kind == hid_collection &&
457 hi.usage == usage;
458 hid_end_parse(hd);
459 return (err);
460 }

Properties

Name Value
cvs2svn:cvs-rev 1.3