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