1 /*****************************************************************************/
3 * names.c -- USB name database manipulation routines
5 * Copyright (C) 1999, 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /*****************************************************************************/
26 #include <sys/types.h>
43 #define usb_file gzFile
44 #define usb_fopen(path, mode) gzopen(path, mode)
45 #define usb_fgets(s, size, stream) gzgets(stream, s, size)
46 #define usb_close(f) gzclose(f)
48 #define usb_file FILE*
49 #define usb_fopen(path, mode) fopen(path, mode)
50 #define usb_fgets(s, size, stream) fgets(s, size, stream)
51 #define usb_close(f) fclose(f)
57 /* ---------------------------------------------------------------------- */
67 u_int16_t vendorid, productid;
78 struct subclass *next;
79 u_int8_t classid, subclassid;
84 struct protocol *next;
85 u_int8_t classid, subclassid, protocolid;
89 struct audioterminal {
90 struct audioterminal *next;
95 struct videoterminal {
96 struct videoterminal *next;
101 struct genericstrtable {
102 struct genericstrtable *next;
107 /* ---------------------------------------------------------------------- */
113 static unsigned int hashnum(unsigned int num)
115 unsigned int mask1 = HASH1 << 27, mask2 = HASH2 << 27;
117 for (; mask1 >= HASH1; mask1 >>= 1, mask2 >>= 1)
120 return num & (HASHSZ-1);
123 /* ---------------------------------------------------------------------- */
125 static struct vendor *vendors[HASHSZ] = { NULL, };
126 static struct product *products[HASHSZ] = { NULL, };
127 static struct class *classes[HASHSZ] = { NULL, };
128 static struct subclass *subclasses[HASHSZ] = { NULL, };
129 static struct protocol *protocols[HASHSZ] = { NULL, };
130 static struct audioterminal *audioterminals[HASHSZ] = { NULL, };
131 static struct videoterminal *videoterminals[HASHSZ] = { NULL, };
132 static struct genericstrtable *hiddescriptors[HASHSZ] = { NULL, };
133 static struct genericstrtable *reports[HASHSZ] = { NULL, };
134 static struct genericstrtable *huts[HASHSZ] = { NULL, };
135 static struct genericstrtable *biass[HASHSZ] = { NULL, };
136 static struct genericstrtable *physdess[HASHSZ] = { NULL, };
137 static struct genericstrtable *hutus[HASHSZ] = { NULL, };
138 static struct genericstrtable *langids[HASHSZ] = { NULL, };
139 static struct genericstrtable *countrycodes[HASHSZ] = { NULL, };
141 /* ---------------------------------------------------------------------- */
143 static const char *names_genericstrtable(struct genericstrtable *t[HASHSZ], unsigned int index)
145 struct genericstrtable *h;
147 for (h = t[hashnum(index)]; h; h = h->next)
153 const char *names_hid(u_int8_t hidd)
155 return names_genericstrtable(hiddescriptors, hidd);
158 const char *names_reporttag(u_int8_t rt)
160 return names_genericstrtable(reports, rt);
163 const char *names_huts(unsigned int data)
165 return names_genericstrtable(huts, data);
168 const char *names_hutus(unsigned int data)
170 return names_genericstrtable(hutus, data);
173 const char *names_langid(u_int16_t langid)
175 return names_genericstrtable(langids, langid);
178 const char *names_physdes(u_int8_t ph)
180 return names_genericstrtable(physdess, ph);
183 const char *names_bias(u_int8_t b)
185 return names_genericstrtable(biass, b);
188 const char *names_countrycode(unsigned int countrycode)
190 return names_genericstrtable(countrycodes, countrycode);
193 const char *names_vendor(u_int16_t vendorid)
197 v = vendors[hashnum(vendorid)];
198 for (; v; v = v->next)
199 if (v->vendorid == vendorid)
204 const char *names_product(u_int16_t vendorid, u_int16_t productid)
208 p = products[hashnum((vendorid << 16) | productid)];
209 for (; p; p = p->next)
210 if (p->vendorid == vendorid && p->productid == productid)
215 const char *names_class(u_int8_t classid)
219 c = classes[hashnum(classid)];
220 for (; c; c = c->next)
221 if (c->classid == classid)
226 const char *names_subclass(u_int8_t classid, u_int8_t subclassid)
230 s = subclasses[hashnum((classid << 8) | subclassid)];
231 for (; s; s = s->next)
232 if (s->classid == classid && s->subclassid == subclassid)
237 const char *names_protocol(u_int8_t classid, u_int8_t subclassid, u_int8_t protocolid)
241 p = protocols[hashnum((classid << 16) | (subclassid << 8) | protocolid)];
242 for (; p; p = p->next)
243 if (p->classid == classid && p->subclassid == subclassid && p->protocolid == protocolid)
248 const char *names_audioterminal(u_int16_t termt)
250 struct audioterminal *at;
252 at = audioterminals[hashnum(termt)];
253 for (; at; at = at->next)
254 if (at->termt == termt)
259 const char *names_videoterminal(u_int16_t termt)
261 struct videoterminal *vt;
263 vt = videoterminals[hashnum(termt)];
264 for (; vt; vt = vt->next)
265 if (vt->termt == termt)
270 /* ---------------------------------------------------------------------- */
272 static int new_vendor(const char *name, u_int16_t vendorid)
275 unsigned int h = hashnum(vendorid);
278 for (; v; v = v->next)
279 if (v->vendorid == vendorid)
281 v = malloc(sizeof(struct vendor) + strlen(name));
284 strcpy(v->name, name);
285 v->vendorid = vendorid;
286 v->next = vendors[h];
291 static int new_product(const char *name, u_int16_t vendorid, u_int16_t productid)
294 unsigned int h = hashnum((vendorid << 16) | productid);
297 for (; p; p = p->next)
298 if (p->vendorid == vendorid && p->productid == productid)
300 p = malloc(sizeof(struct product) + strlen(name));
303 strcpy(p->name, name);
304 p->vendorid = vendorid;
305 p->productid = productid;
306 p->next = products[h];
311 static int new_class(const char *name, u_int8_t classid)
314 unsigned int h = hashnum(classid);
317 for (; c; c = c->next)
318 if (c->classid == classid)
320 c = malloc(sizeof(struct class) + strlen(name));
323 strcpy(c->name, name);
324 c->classid = classid;
325 c->next = classes[h];
330 static int new_subclass(const char *name, u_int8_t classid, u_int8_t subclassid)
333 unsigned int h = hashnum((classid << 8) | subclassid);
336 for (; s; s = s->next)
337 if (s->classid == classid && s->subclassid == subclassid)
339 s = malloc(sizeof(struct subclass) + strlen(name));
342 strcpy(s->name, name);
343 s->classid = classid;
344 s->subclassid = subclassid;
345 s->next = subclasses[h];
350 static int new_protocol(const char *name, u_int8_t classid, u_int8_t subclassid, u_int8_t protocolid)
353 unsigned int h = hashnum((classid << 16) | (subclassid << 8) | protocolid);
356 for (; p; p = p->next)
357 if (p->classid == classid && p->subclassid == subclassid && p->protocolid == protocolid)
359 p = malloc(sizeof(struct protocol) + strlen(name));
362 strcpy(p->name, name);
363 p->classid = classid;
364 p->subclassid = subclassid;
365 p->protocolid = protocolid;
366 p->next = protocols[h];
371 static int new_audioterminal(const char *name, u_int16_t termt)
373 struct audioterminal *at;
374 unsigned int h = hashnum(termt);
376 at = audioterminals[h];
377 for (; at; at = at->next)
378 if (at->termt == termt)
380 at = malloc(sizeof(struct audioterminal) + strlen(name));
383 strcpy(at->name, name);
385 at->next = audioterminals[h];
386 audioterminals[h] = at;
390 static int new_videoterminal(const char *name, u_int16_t termt)
392 struct videoterminal *vt;
393 unsigned int h = hashnum(termt);
395 vt = videoterminals[h];
396 for (; vt; vt = vt->next)
397 if (vt->termt == termt)
399 vt = malloc(sizeof(struct videoterminal) + strlen(name));
402 strcpy(vt->name, name);
404 vt->next = videoterminals[h];
405 videoterminals[h] = vt;
409 static int new_genericstrtable(struct genericstrtable *t[HASHSZ], const char *name, unsigned int index)
411 struct genericstrtable *g;
412 unsigned int h = hashnum(index);
414 for (g = t[h]; g; g = g->next)
417 g = malloc(sizeof(struct genericstrtable) + strlen(name));
420 strcpy(g->name, name);
427 static int new_hid(const char *name, u_int8_t hidd)
429 return new_genericstrtable(hiddescriptors, name, hidd);
432 static int new_reporttag(const char *name, u_int8_t rt)
434 return new_genericstrtable(reports, name, rt);
437 static int new_huts(const char *name, unsigned int data)
439 return new_genericstrtable(huts, name, data);
442 static int new_hutus(const char *name, unsigned int data)
444 return new_genericstrtable(hutus, name, data);
447 static int new_langid(const char *name, u_int16_t langid)
449 return new_genericstrtable(langids, name, langid);
452 static int new_physdes(const char *name, u_int8_t ph)
454 return new_genericstrtable(physdess, name, ph);
456 static int new_bias(const char *name, u_int8_t b)
458 return new_genericstrtable(biass, name, b);
461 static int new_countrycode(const char *name, unsigned int countrycode)
463 return new_genericstrtable(countrycodes, name, countrycode);
466 /* ---------------------------------------------------------------------- */
470 static void parse(usb_file f)
473 unsigned int linectr = 0;
474 int lastvendor = -1, lastclass = -1, lastsubclass = -1, lasthut=-1, lastlang=-1;
477 while (usb_fgets(buf, sizeof(buf), f)) {
479 /* remove line ends */
480 if ((cp = strchr(buf, 13)))
482 if ((cp = strchr(buf, 10)))
484 if (buf[0] == '#' || !buf[0])
487 if (buf[0] == 'P' && buf[1] == 'H' && buf[2] == 'Y' && buf[3] == 'S' && buf[4] == 'D' &&
488 buf[5] == 'E' && buf[6] == 'S' && /*isspace(buf[7])*/ buf[7] == ' ') {
492 if (!isxdigit(*cp)) {
493 fprintf(stderr, "Invalid Physdes type at line %u\n", linectr);
496 u = strtoul(cp, &cp, 16);
500 fprintf(stderr, "Invalid Physdes type at line %u\n", linectr);
503 if (new_physdes(cp, u))
504 fprintf(stderr, "Duplicate Physdes type spec at line %u terminal type %04x %s\n", linectr, u, cp);
505 DBG(printf("line %5u physdes type %02x %s\n", linectr, u, cp));
509 if (buf[0] == 'P' && buf[1] == 'H' && buf[2] == 'Y' && /*isspace(buf[3])*/ buf[3] == ' ') {
513 if (!isxdigit(*cp)) {
514 fprintf(stderr, "Invalid PHY type at line %u\n", linectr);
517 u = strtoul(cp, &cp, 16);
521 fprintf(stderr, "Invalid PHY type at line %u\n", linectr);
524 if (new_physdes(cp, u))
525 fprintf(stderr, "Duplicate PHY type spec at line %u terminal type %04x %s\n", linectr, u, cp);
526 DBG(printf("line %5u PHY type %02x %s\n", linectr, u, cp));
530 if (buf[0] == 'B' && buf[1] == 'I' && buf[2] == 'A' && buf[3] == 'S' && /*isspace(buf[4])*/ buf[4] == ' ') {
534 if (!isxdigit(*cp)) {
535 fprintf(stderr, "Invalid BIAS type at line %u\n", linectr);
538 u = strtoul(cp, &cp, 16);
542 fprintf(stderr, "Invalid BIAS type at line %u\n", linectr);
546 fprintf(stderr, "Duplicate BIAS type spec at line %u terminal type %04x %s\n", linectr, u, cp);
547 DBG(printf("line %5u BIAS type %02x %s\n", linectr, u, cp));
551 if (buf[0] == 'L' && /*isspace(buf[1])*/ buf[1] == ' ') {
555 if (!isxdigit(*cp)) {
556 fprintf(stderr, "Invalid LANGID spec at line %u\n", linectr);
559 u = strtoul(cp, &cp, 16);
563 fprintf(stderr, "Invalid LANGID spec at line %u\n", linectr);
566 if (new_langid(cp, u))
567 fprintf(stderr, "Duplicate LANGID spec at line %u language-id %04x %s\n", linectr, u, cp);
568 DBG(printf("line %5u LANGID %02x %s\n", linectr, u, cp));
569 lasthut = lastclass = lastvendor = lastsubclass = -1;
573 if (buf[0] == 'C' && /*isspace(buf[1])*/ buf[1] == ' ') {
578 if (!isxdigit(*cp)) {
579 fprintf(stderr, "Invalid class spec at line %u\n", linectr);
582 u = strtoul(cp, &cp, 16);
586 fprintf(stderr, "Invalid class spec at line %u\n", linectr);
589 if (new_class(cp, u))
590 fprintf(stderr, "Duplicate class spec at line %u class %04x %s\n", linectr, u, cp);
591 DBG(printf("line %5u class %02x %s\n", linectr, u, cp));
592 lasthut = lastlang = lastvendor = lastsubclass = -1;
596 if (buf[0] == 'A' && buf[1] == 'T' && isspace(buf[2])) {
597 /* audio terminal type spec */
601 if (!isxdigit(*cp)) {
602 fprintf(stderr, "Invalid audio terminal type at line %u\n", linectr);
605 u = strtoul(cp, &cp, 16);
609 fprintf(stderr, "Invalid audio terminal type at line %u\n", linectr);
612 if (new_audioterminal(cp, u))
613 fprintf(stderr, "Duplicate audio terminal type spec at line %u terminal type %04x %s\n", linectr, u, cp);
614 DBG(printf("line %5u audio terminal type %02x %s\n", linectr, u, cp));
617 if (buf[0] == 'V' && buf[1] == 'T' && isspace(buf[2])) {
618 /* video terminal type spec */
622 if (!isxdigit(*cp)) {
623 fprintf(stderr, "Invalid video terminal type at line %u\n", linectr);
626 u = strtoul(cp, &cp, 16);
630 fprintf(stderr, "Invalid video terminal type at line %u\n", linectr);
633 if (new_videoterminal(cp, u))
634 fprintf(stderr, "Duplicate video terminal type spec at line %u terminal type %04x %s\n", linectr, u, cp);
635 DBG(printf("line %5u video terminal type %02x %s\n", linectr, u, cp));
638 if (buf[0] == 'H' && buf[1] == 'C' && buf[2] == 'C' && isspace(buf[3])) {
639 /* HID Descriptor bCountryCode */
643 if (!isxdigit(*cp)) {
644 fprintf(stderr, "Invalid HID country code at line %u\n", linectr);
647 u = strtoul(cp, &cp, 10);
651 fprintf(stderr, "Invalid HID country code at line %u\n", linectr);
654 if (new_countrycode(cp, u))
655 fprintf(stderr, "Duplicate HID country code at line %u country %02u %s\n", linectr, u, cp);
656 DBG(printf("line %5u keyboard country code %02u %s\n", linectr, u, cp));
661 u = strtoul(cp, &cp, 16);
665 fprintf(stderr, "Invalid vendor spec at line %u\n", linectr);
668 if (new_vendor(cp, u))
669 fprintf(stderr, "Duplicate vendor spec at line %u vendor %04x %s\n", linectr, u, cp);
670 DBG(printf("line %5u vendor %04x %s\n", linectr, u, cp));
672 lasthut = lastlang = lastclass = lastsubclass = -1;
675 if (buf[0] == '\t' && isxdigit(buf[1])) {
676 /* product or subclass spec */
677 u = strtoul(buf+1, &cp, 16);
681 fprintf(stderr, "Invalid product/subclass spec at line %u\n", linectr);
684 if (lastvendor != -1) {
685 if (new_product(cp, lastvendor, u))
686 fprintf(stderr, "Duplicate product spec at line %u product %04x:%04x %s\n", linectr, lastvendor, u, cp);
687 DBG(printf("line %5u product %04x:%04x %s\n", linectr, lastvendor, u, cp));
690 if (lastclass != -1) {
691 if (new_subclass(cp, lastclass, u))
692 fprintf(stderr, "Duplicate subclass spec at line %u class %02x:%02x %s\n", linectr, lastclass, u, cp);
693 DBG(printf("line %5u subclass %02x:%02x %s\n", linectr, lastclass, u, cp));
698 if (new_hutus(cp, (lasthut << 16)+u))
699 fprintf(stderr, "Duplicate HUT Usage Spec at line %u\n", linectr);
702 if (lastlang != -1) {
703 if (new_langid(cp, lastlang+(u<<10)))
704 fprintf(stderr, "Duplicate LANGID Usage Spec at line %u\n", linectr);
707 fprintf(stderr, "Product/Subclass spec without prior Vendor/Class spec at line %u\n", linectr);
710 if (buf[0] == '\t' && buf[1] == '\t' && isxdigit(buf[2])) {
712 u = strtoul(buf+2, &cp, 16);
716 fprintf(stderr, "Invalid protocol spec at line %u\n", linectr);
719 if (lastclass != -1 && lastsubclass != -1) {
720 if (new_protocol(cp, lastclass, lastsubclass, u))
721 fprintf(stderr, "Duplicate protocol spec at line %u class %02x:%02x:%02x %s\n", linectr, lastclass, lastsubclass, u, cp);
722 DBG(printf("line %5u protocol %02x:%02x:%02x %s\n", linectr, lastclass, lastsubclass, u, cp));
725 fprintf(stderr, "Protocol spec without prior Class and Subclass spec at line %u\n", linectr);
728 if (buf[0] == 'H' && buf[1] == 'I' && buf[2] == 'D' && /*isspace(buf[3])*/ buf[3] == ' ') {
732 if (!isxdigit(*cp)) {
733 fprintf(stderr, "Invalid HID type at line %u\n", linectr);
736 u = strtoul(cp, &cp, 16);
740 fprintf(stderr, "Invalid HID type at line %u\n", linectr);
744 fprintf(stderr, "Duplicate HID type spec at line %u terminal type %04x %s\n", linectr, u, cp);
745 DBG(printf("line %5u HID type %02x %s\n", linectr, u, cp));
749 if (buf[0] == 'H' && buf[1] == 'U' && buf[2] == 'T' && /*isspace(buf[3])*/ buf[3] == ' ') {
753 if (!isxdigit(*cp)) {
754 fprintf(stderr, "Invalid HUT type at line %u\n", linectr);
757 u = strtoul(cp, &cp, 16);
761 fprintf(stderr, "Invalid HUT type at line %u\n", linectr);
765 fprintf(stderr, "Duplicate HUT type spec at line %u terminal type %04x %s\n", linectr, u, cp);
766 lastlang = lastclass = lastvendor = lastsubclass = -1;
768 DBG(printf("line %5u HUT type %02x %s\n", linectr, u, cp));
772 if (buf[0] == 'R' && buf[1] == ' ') {
776 if (!isxdigit(*cp)) {
777 fprintf(stderr, "Invalid Report type at line %u\n", linectr);
780 u = strtoul(cp, &cp, 16);
784 fprintf(stderr, "Invalid Report type at line %u\n", linectr);
787 if (new_reporttag(cp, u))
788 fprintf(stderr, "Duplicate Report type spec at line %u terminal type %04x %s\n", linectr, u, cp);
789 DBG(printf("line %5u Report type %02x %s\n", linectr, u, cp));
793 fprintf(stderr, "Unknown line at line %u\n", linectr);
797 /* ---------------------------------------------------------------------- */
799 int names_init(char *n)
803 if (!(f = usb_fopen(n, "r"))) {