31 #include <sys/types.h>
96 static unsigned int hashnum(
unsigned int num)
98 unsigned int mask1 =
HASH1 << 27, mask2 =
HASH2 << 27;
100 for (; mask1 >=
HASH1; mask1 >>= 1, mask2 >>= 1)
129 for (h =
t[hashnum(index)];
h; h = h->
next)
137 return names_genericstrtable(hiddescriptors, hidd);
142 return names_genericstrtable(reports, rt);
147 return names_genericstrtable(huts, data);
152 return names_genericstrtable(hutus, data);
157 return names_genericstrtable(langids, langid);
162 return names_genericstrtable(physdess, ph);
167 return names_genericstrtable(biass, b);
172 return names_genericstrtable(countrycodes, countrycode);
179 v = vendors[hashnum(vendorid)];
180 for (;
v; v = v->
next)
190 p = products[hashnum((vendorid << 16) | productid)];
191 for (;
p; p = p->
next)
201 c = classes[hashnum(classid)];
202 for (;
c; c = c->
next)
212 s = subclasses[hashnum((classid << 8) | subclassid)];
213 for (;
s; s = s->
next)
223 p = protocols[hashnum((classid << 16) | (subclassid << 8) | protocolid)];
224 for (;
p; p = p->
next)
234 at = audioterminals[hashnum(termt)];
236 if (at->
termt == termt)
249 static struct pool *pool_head =
NULL;
251 static void *my_malloc(
size_t size)
255 p = calloc(1,
sizeof(
struct pool));
261 p->
mem = calloc(1, size);
278 for (pool = pool_head; pool !=
NULL; ) {
295 unsigned int h = hashnum(vendorid);
298 for (;
v; v = v->
next)
306 v->
next = vendors[
h];
314 unsigned int h = hashnum((vendorid << 16) | productid);
317 for (;
p; p = p->
next)
326 p->
next = products[
h];
331 static int new_class(
const char *name,
u_int8_t classid)
334 unsigned int h = hashnum(classid);
337 for (;
c; c = c->
next)
340 c = my_malloc(
sizeof(
struct class) +
strlen(name));
345 c->
next = classes[
h];
350 static int new_subclass(
const char *name,
u_int8_t classid,
u_int8_t subclassid)
353 unsigned int h = hashnum((classid << 8) | subclassid);
356 for (;
s; s = s->
next)
365 s->
next = subclasses[
h];
373 unsigned int h = hashnum((classid << 16) | (subclassid << 8) | protocolid);
376 for (;
p; p = p->
next)
386 p->
next = protocols[
h];
391 static int new_audioterminal(
const char *name,
u_int16_t termt)
394 unsigned int h = hashnum(termt);
396 at = audioterminals[
h];
398 if (at->
termt == termt)
405 at->
next = audioterminals[
h];
406 audioterminals[
h] =
at;
410 static int new_genericstrtable(
struct genericstrtable *
t[HASHSZ],
const char *name,
unsigned int index)
413 unsigned int h = hashnum(index);
415 for (g =
t[h]; g; g = g->
next)
428 static int new_hid(
const char *name,
u_int8_t hidd)
430 return new_genericstrtable(hiddescriptors, name, hidd);
433 static int new_reporttag(
const char *name,
u_int8_t rt)
435 return new_genericstrtable(reports, name, rt);
438 static int new_huts(
const char *name,
unsigned int data)
440 return new_genericstrtable(huts, name, data);
443 static int new_hutus(
const char *name,
unsigned int data)
445 return new_genericstrtable(hutus, name, data);
448 static int new_langid(
const char *name,
u_int16_t langid)
450 return new_genericstrtable(langids, name, langid);
453 static int new_physdes(
const char *name,
u_int8_t ph)
455 return new_genericstrtable(physdess, name, ph);
459 return new_genericstrtable(biass, name, b);
462 static int new_countrycode(
const char *name,
unsigned int countrycode)
464 return new_genericstrtable(countrycodes, name, countrycode);
471 static void parse(FILE *
f)
474 unsigned int linectr = 0;
475 int lastvendor = -1, lastclass = -1, lastsubclass = -1, lasthut=-1, lastlang=-1;
478 while (fgets(buf,
sizeof(buf), f)) {
481 if ((cp =
strchr(buf, 13)))
483 if ((cp =
strchr(buf, 10)))
485 if (buf[0] ==
'#' || !buf[0])
488 if (buf[0] ==
'P' && buf[1] ==
'H' && buf[2] ==
'Y' && buf[3] ==
'S' && buf[4] ==
'D' &&
489 buf[5] ==
'E' && buf[6] ==
'S' && buf[7] ==
' ') {
494 fprintf(stderr,
"Invalid Physdes type at line %u\n", linectr);
497 u = strtoul(cp, &cp, 16);
501 fprintf(stderr,
"Invalid Physdes type at line %u\n", linectr);
504 if (new_physdes(cp, u))
505 fprintf(stderr,
"Duplicate Physdes type spec at line %u terminal type %04x %s\n", linectr, u, cp);
506 DBG(
printf(
"line %5u physdes type %02x %s\n", linectr, u, cp));
510 if (buf[0] ==
'P' && buf[1] ==
'H' && buf[2] ==
'Y' && buf[3] ==
' ') {
515 fprintf(stderr,
"Invalid PHY type at line %u\n", linectr);
518 u = strtoul(cp, &cp, 16);
522 fprintf(stderr,
"Invalid PHY type at line %u\n", linectr);
525 if (new_physdes(cp, u))
526 fprintf(stderr,
"Duplicate PHY type spec at line %u terminal type %04x %s\n", linectr, u, cp);
527 DBG(
printf(
"line %5u PHY type %02x %s\n", linectr, u, cp));
531 if (buf[0] ==
'B' && buf[1] ==
'I' && buf[2] ==
'A' && buf[3] ==
'S' && buf[4] ==
' ') {
536 fprintf(stderr,
"Invalid BIAS type at line %u\n", linectr);
539 u = strtoul(cp, &cp, 16);
543 fprintf(stderr,
"Invalid BIAS type at line %u\n", linectr);
547 fprintf(stderr,
"Duplicate BIAS type spec at line %u terminal type %04x %s\n", linectr, u, cp);
548 DBG(
printf(
"line %5u BIAS type %02x %s\n", linectr, u, cp));
552 if (buf[0] ==
'L' && buf[1] ==
' ') {
557 fprintf(stderr,
"Invalid LANGID spec at line %u\n", linectr);
560 u = strtoul(cp, &cp, 16);
564 fprintf(stderr,
"Invalid LANGID spec at line %u\n", linectr);
567 if (new_langid(cp, u))
568 fprintf(stderr,
"Duplicate LANGID spec at line %u language-id %04x %s\n", linectr, u, cp);
569 DBG(
printf(
"line %5u LANGID %02x %s\n", linectr, u, cp));
570 lasthut = lastclass = lastvendor = lastsubclass = -1;
574 if (buf[0] ==
'C' && buf[1] ==
' ') {
580 fprintf(stderr,
"Invalid class spec at line %u\n", linectr);
583 u = strtoul(cp, &cp, 16);
587 fprintf(stderr,
"Invalid class spec at line %u\n", linectr);
590 if (new_class(cp, u))
591 fprintf(stderr,
"Duplicate class spec at line %u class %04x %s\n", linectr, u, cp);
592 DBG(
printf(
"line %5u class %02x %s\n", linectr, u, cp));
593 lasthut = lastlang = lastvendor = lastsubclass = -1;
597 if (buf[0] ==
'A' && buf[1] ==
'T' &&
isspace(buf[2])) {
603 fprintf(stderr,
"Invalid audio terminal type at line %u\n", linectr);
606 u = strtoul(cp, &cp, 16);
610 fprintf(stderr,
"Invalid audio terminal type at line %u\n", linectr);
613 if (new_audioterminal(cp, u))
614 fprintf(stderr,
"Duplicate audio terminal type spec at line %u terminal type %04x %s\n", linectr, u, cp);
615 DBG(
printf(
"line %5u audio terminal type %02x %s\n", linectr, u, cp));
618 if (buf[0] ==
'H' && buf[1] ==
'C' && buf[2] ==
'C' &&
isspace(buf[3])) {
624 fprintf(stderr,
"Invalid HID country code at line %u\n", linectr);
627 u = strtoul(cp, &cp, 10);
631 fprintf(stderr,
"Invalid HID country code at line %u\n", linectr);
634 if (new_countrycode(cp, u))
635 fprintf(stderr,
"Duplicate HID country code at line %u country %02u %s\n", linectr, u, cp);
636 DBG(
printf(
"line %5u keyboard country code %02u %s\n", linectr, u, cp));
641 u = strtoul(cp, &cp, 16);
645 fprintf(stderr,
"Invalid vendor spec at line %u\n", linectr);
648 if (new_vendor(cp, u))
649 fprintf(stderr,
"Duplicate vendor spec at line %u vendor %04x %s\n", linectr, u, cp);
650 DBG(
printf(
"line %5u vendor %04x %s\n", linectr, u, cp));
652 lasthut = lastlang = lastclass = lastsubclass = -1;
655 if (buf[0] ==
'\t' &&
isxdigit(buf[1])) {
657 u = strtoul(buf+1, &cp, 16);
661 fprintf(stderr,
"Invalid product/subclass spec at line %u\n", linectr);
664 if (lastvendor != -1) {
665 if (new_product(cp, lastvendor, u))
666 fprintf(stderr,
"Duplicate product spec at line %u product %04x:%04x %s\n", linectr, lastvendor, u, cp);
667 DBG(
printf(
"line %5u product %04x:%04x %s\n", linectr, lastvendor, u, cp));
670 if (lastclass != -1) {
671 if (new_subclass(cp, lastclass, u))
672 fprintf(stderr,
"Duplicate subclass spec at line %u class %02x:%02x %s\n", linectr, lastclass, u, cp);
673 DBG(
printf(
"line %5u subclass %02x:%02x %s\n", linectr, lastclass, u, cp));
678 if (new_hutus(cp, (lasthut << 16)+u))
679 fprintf(stderr,
"Duplicate HUT Usage Spec at line %u\n", linectr);
682 if (lastlang != -1) {
683 if (new_langid(cp, lastlang+(u<<10)))
684 fprintf(stderr,
"Duplicate LANGID Usage Spec at line %u\n", linectr);
687 fprintf(stderr,
"Product/Subclass spec without prior Vendor/Class spec at line %u\n", linectr);
690 if (buf[0] ==
'\t' && buf[1] ==
'\t' &&
isxdigit(buf[2])) {
692 u = strtoul(buf+2, &cp, 16);
696 fprintf(stderr,
"Invalid protocol spec at line %u\n", linectr);
699 if (lastclass != -1 && lastsubclass != -1) {
700 if (new_protocol(cp, lastclass, lastsubclass, u))
701 fprintf(stderr,
"Duplicate protocol spec at line %u class %02x:%02x:%02x %s\n", linectr, lastclass, lastsubclass, u, cp);
702 DBG(
printf(
"line %5u protocol %02x:%02x:%02x %s\n", linectr, lastclass, lastsubclass, u, cp));
705 fprintf(stderr,
"Protocol spec without prior Class and Subclass spec at line %u\n", linectr);
708 if (buf[0] ==
'H' && buf[1] ==
'I' && buf[2] ==
'D' && buf[3] ==
' ') {
713 fprintf(stderr,
"Invalid HID type at line %u\n", linectr);
716 u = strtoul(cp, &cp, 16);
720 fprintf(stderr,
"Invalid HID type at line %u\n", linectr);
724 fprintf(stderr,
"Duplicate HID type spec at line %u terminal type %04x %s\n", linectr, u, cp);
725 DBG(
printf(
"line %5u HID type %02x %s\n", linectr, u, cp));
729 if (buf[0] ==
'H' && buf[1] ==
'U' && buf[2] ==
'T' && buf[3] ==
' ') {
734 fprintf(stderr,
"Invalid HUT type at line %u\n", linectr);
737 u = strtoul(cp, &cp, 16);
741 fprintf(stderr,
"Invalid HUT type at line %u\n", linectr);
745 fprintf(stderr,
"Duplicate HUT type spec at line %u terminal type %04x %s\n", linectr, u, cp);
746 lastlang = lastclass = lastvendor = lastsubclass = -1;
748 DBG(
printf(
"line %5u HUT type %02x %s\n", linectr, u, cp));
752 if (buf[0] ==
'R' && buf[1] ==
' ') {
757 fprintf(stderr,
"Invalid Report type at line %u\n", linectr);
760 u = strtoul(cp, &cp, 16);
764 fprintf(stderr,
"Invalid Report type at line %u\n", linectr);
767 if (new_reporttag(cp, u))
768 fprintf(stderr,
"Duplicate Report type spec at line %u terminal type %04x %s\n", linectr, u, cp);
769 DBG(
printf(
"line %5u Report type %02x %s\n", linectr, u, cp));
773 if (buf[0] ==
'V' && buf[1] ==
'T') {
777 fprintf(stderr,
"Unknown line at line %u\n", linectr);
787 if (!(f = fopen(n,
"r"))) {