Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / bus / usb / hid.c
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
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
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.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #if defined(__NetBSD__)
44 #include <sys/kernel.h>
45 #endif
46 #include <sys/malloc.h>
47  
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbhid.h>
50
51 #include <dev/usb/hid.h>
52
53 #ifdef USB_DEBUG
54 #define DPRINTF(x)      if (usbdebug) logprintf x
55 #define DPRINTFN(n,x)   if (usbdebug>(n)) logprintf x
56 extern int usbdebug;
57 #else
58 #define DPRINTF(x)
59 #define DPRINTFN(n,x)
60 #endif
61
62 Static void hid_clear_local(struct hid_item *);
63
64 #define MAXUSAGE 100
65 struct hid_data {
66         u_char *start;
67         u_char *end;
68         u_char *p;
69         struct hid_item cur;
70         int32_t usages[MAXUSAGE];
71         int nu;
72         int minset;
73         int multi;
74         int multimax;
75         int kindset;
76 };
77
78 Static void
79 hid_clear_local(struct hid_item *c)
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);
99         memset(s, 0, sizeof *s);
100         s->start = s->p = d;
101         s->end = (char *)d + len;
102         s->kindset = kindset;
103         return (s);
104 }
105
106 void
107 hid_end_parse(struct hid_data *s)
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 size, id;
372
373         id = 0;
374         for (d = hid_start_parse(buf, len, 1<<k); hid_get_item(d, &h); )
375                 if (h.report_ID != 0)
376                         id = h.report_ID;
377         hid_end_parse(d);
378         size = h.loc.pos;
379         if (id != 0) {
380                 size += 8;
381                 *idp = id;      /* XXX wrong */
382         } else
383                 *idp = 0;
384         return ((size + 7) / 8);
385 }
386
387 int
388 hid_locate(void *desc, int size, u_int32_t u, enum hid_kind k,
389            struct hid_location *loc, u_int32_t *flags)
390 {
391         struct hid_data *d;
392         struct hid_item h;
393
394         for (d = hid_start_parse(desc, size, 1<<k); hid_get_item(d, &h); ) {
395                 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
396                         if (loc != NULL)
397                                 *loc = h.loc;
398                         if (flags != NULL)
399                                 *flags = h.flags;
400                         hid_end_parse(d);
401                         return (1);
402                 }
403         }
404         hid_end_parse(d);
405         loc->size = 0;
406         return (0);
407 }
408
409 u_long
410 hid_get_data(u_char *buf, struct hid_location *loc)
411 {
412         u_int hpos = loc->pos;
413         u_int hsize = loc->size;
414         u_int32_t data;
415         int i, s;
416
417         DPRINTFN(10, ("hid_get_data: loc %d/%d\n", hpos, hsize));
418
419         if (hsize == 0)
420                 return (0);
421
422         data = 0;
423         s = hpos / 8; 
424         for (i = hpos; i < hpos+hsize; i += 8)
425                 data |= buf[i / 8] << ((i / 8 - s) * 8);
426         data >>= hpos % 8;
427         data &= (1 << hsize) - 1;
428         hsize = 32 - hsize;
429         /* Sign extend */
430         data = ((int32_t)data << hsize) >> hsize;
431         DPRINTFN(10,("hid_get_data: loc %d/%d = %lu\n", 
432                     loc->pos, loc->size, (long)data));
433         return (data);
434 }
435
436 int
437 hid_is_collection(void *desc, int size, u_int32_t usage)
438 {
439         struct hid_data *hd;
440         struct hid_item hi;
441         int err;
442
443         hd = hid_start_parse(desc, size, hid_input);
444         if (hd == NULL)
445                 return (0);
446
447         err = hid_get_item(hd, &hi) &&
448             hi.kind == hid_collection &&
449             hi.usage == usage;
450         hid_end_parse(hd);
451         return (err);
452 }