]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/usb/hid.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / usb / hid.c
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$");
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                                         if (s->nu > 0)
198                                                 s->nu--;
199                                         continue;
200                                 }
201                                 c->kind = hid_input;
202                                 c->flags = dval;
203                         ret:
204                                 if (c->flags & HIO_VARIABLE) {
205                                         s->multimax = c->loc.count;
206                                         s->multi = 0;
207                                         c->loc.count = 1;
208                                         if (s->minset) {
209                                                 for (i = c->usage_minimum;
210                                                      i <= c->usage_maximum;
211                                                      i++) {
212                                                         s->usages[s->nu] = i;
213                                                         if (s->nu < MAXUSAGE-1)
214                                                                 s->nu++;
215                                                 }
216                                                 s->minset = 0;
217                                         }
218                                         goto top;
219                                 } else {
220                                         *h = *c;
221                                         h->next = 0;
222                                         c->loc.pos +=
223                                                 c->loc.size * c->loc.count;
224                                         hid_clear_local(c);
225                                         s->minset = 0;
226                                         return (1);
227                                 }
228                         case 9:         /* Output */
229                                 if (!(s->kindset & (1 << hid_output))) {
230                                         if (s->nu > 0)
231                                                 s->nu--;
232                                         continue;
233                                 }
234                                 c->kind = hid_output;
235                                 c->flags = dval;
236                                 goto ret;
237                         case 10:        /* Collection */
238                                 c->kind = hid_collection;
239                                 c->collection = dval;
240                                 c->collevel++;
241                                 *h = *c;
242                                 hid_clear_local(c);
243                                 s->nu = 0;
244                                 return (1);
245                         case 11:        /* Feature */
246                                 if (!(s->kindset & (1 << hid_feature))) {
247                                         if (s->nu > 0)
248                                                 s->nu--;
249                                         continue;
250                                 }
251                                 c->kind = hid_feature;
252                                 c->flags = dval;
253                                 goto ret;
254                         case 12:        /* End collection */
255                                 c->kind = hid_endcollection;
256                                 c->collevel--;
257                                 *h = *c;
258                                 hid_clear_local(c);
259                                 s->nu = 0;
260                                 return (1);
261                         default:
262                                 printf("Main bTag=%d\n", bTag);
263                                 break;
264                         }
265                         break;
266                 case 1:         /* Global */
267                         switch (bTag) {
268                         case 0:
269                                 c->_usage_page = dval << 16;
270                                 break;
271                         case 1:
272                                 c->logical_minimum = dval;
273                                 break;
274                         case 2:
275                                 c->logical_maximum = dval;
276                                 break;
277                         case 3:
278                                 c->physical_minimum = dval;
279                                 break;
280                         case 4:
281                                 c->physical_maximum = dval;
282                                 break;
283                         case 5:
284                                 c->unit_exponent = dval;
285                                 break;
286                         case 6:
287                                 c->unit = dval;
288                                 break;
289                         case 7:
290                                 c->loc.size = dval;
291                                 break;
292                         case 8:
293                                 c->report_ID = dval;
294                                 break;
295                         case 9:
296                                 c->loc.count = dval;
297                                 break;
298                         case 10: /* Push */
299                                 hi = malloc(sizeof *hi, M_TEMP, M_WAITOK);
300                                 *hi = s->cur;
301                                 c->next = hi;
302                                 break;
303                         case 11: /* Pop */
304                                 hi = c->next;
305                                 oldpos = c->loc.pos;
306                                 s->cur = *hi;
307                                 c->loc.pos = oldpos;
308                                 free(hi, M_TEMP);
309                                 break;
310                         default:
311                                 printf("Global bTag=%d\n", bTag);
312                                 break;
313                         }
314                         break;
315                 case 2:         /* Local */
316                         switch (bTag) {
317                         case 0:
318                                 if (bSize == 1)
319                                         dval = c->_usage_page | (dval&0xff);
320                                 else if (bSize == 2)
321                                         dval = c->_usage_page | (dval&0xffff);
322                                 c->usage = dval;
323                                 if (s->nu < MAXUSAGE)
324                                         s->usages[s->nu++] = dval;
325                                 /* else XXX */
326                                 break;
327                         case 1:
328                                 s->minset = 1;
329                                 if (bSize == 1)
330                                         dval = c->_usage_page | (dval&0xff);
331                                 else if (bSize == 2)
332                                         dval = c->_usage_page | (dval&0xffff);
333                                 c->usage_minimum = dval;
334                                 break;
335                         case 2:
336                                 if (bSize == 1)
337                                         dval = c->_usage_page | (dval&0xff);
338                                 else if (bSize == 2)
339                                         dval = c->_usage_page | (dval&0xffff);
340                                 c->usage_maximum = dval;
341                                 break;
342                         case 3:
343                                 c->designator_index = dval;
344                                 break;
345                         case 4:
346                                 c->designator_minimum = dval;
347                                 break;
348                         case 5:
349                                 c->designator_maximum = dval;
350                                 break;
351                         case 7:
352                                 c->string_index = dval;
353                                 break;
354                         case 8:
355                                 c->string_minimum = dval;
356                                 break;
357                         case 9:
358                                 c->string_maximum = dval;
359                                 break;
360                         case 10:
361                                 c->set_delimiter = dval;
362                                 break;
363                         default:
364                                 printf("Local bTag=%d\n", bTag);
365                                 break;
366                         }
367                         break;
368                 default:
369                         printf("default bType=%d\n", bType);
370                         break;
371                 }
372         }
373 }
374
375 int
376 hid_report_size(void *buf, int len, enum hid_kind k, u_int8_t *idp)
377 {
378         struct hid_data *d;
379         struct hid_item h;
380         int hi, lo, size, id;
381
382         id = 0;
383         hi = lo = -1;
384         for (d = hid_start_parse(buf, len, 1<<k); hid_get_item(d, &h); )
385                 if (h.kind == k) {
386                         if (h.report_ID != 0 && !id)
387                                 id = h.report_ID;
388                         if (h.report_ID == id) {
389                                 if (lo < 0)
390                                         lo = h.loc.pos;
391                                 hi = h.loc.pos + h.loc.size * h.loc.count;
392                         }
393                 }
394         hid_end_parse(d);
395         size = hi - lo;
396         if (id != 0) {
397                 size += 8;
398                 *idp = id;      /* XXX wrong */
399         } else
400                 *idp = 0;
401         return ((size + 7) / 8);
402 }
403
404 int
405 hid_locate(void *desc, int size, u_int32_t u, enum hid_kind k,
406            struct hid_location *loc, u_int32_t *flags)
407 {
408         struct hid_data *d;
409         struct hid_item h;
410
411         for (d = hid_start_parse(desc, size, 1<<k); hid_get_item(d, &h); ) {
412                 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
413                         if (loc != NULL)
414                                 *loc = h.loc;
415                         if (flags != NULL)
416                                 *flags = h.flags;
417                         hid_end_parse(d);
418                         return (1);
419                 }
420         }
421         hid_end_parse(d);
422         loc->size = 0;
423         return (0);
424 }
425
426 u_long
427 hid_get_data(u_char *buf, struct hid_location *loc)
428 {
429         u_int hpos = loc->pos;
430         u_int hsize = loc->size;
431         u_int32_t data;
432         int i, s;
433
434         DPRINTFN(10, ("hid_get_data: loc %d/%d\n", hpos, hsize));
435
436         if (hsize == 0)
437                 return (0);
438
439         data = 0;
440         s = hpos / 8;
441         for (i = hpos; i < hpos+hsize; i += 8)
442                 data |= buf[i / 8] << ((i / 8 - s) * 8);
443         data >>= hpos % 8;
444         data &= (1 << hsize) - 1;
445         hsize = 32 - hsize;
446         /* Sign extend */
447         data = ((int32_t)data << hsize) >> hsize;
448         DPRINTFN(10,("hid_get_data: loc %d/%d = %lu\n",
449                     loc->pos, loc->size, (long)data));
450         return (data);
451 }
452
453 int
454 hid_is_collection(void *desc, int size, u_int32_t usage)
455 {
456         struct hid_data *hd;
457         struct hid_item hi;
458         int err;
459
460         hd = hid_start_parse(desc, size, hid_input);
461         if (hd == NULL)
462                 return (0);
463
464         err = hid_get_item(hd, &hi) &&
465             hi.kind == hid_collection &&
466             hi.usage == usage;
467         hid_end_parse(hd);
468         return (err);
469 }