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.
25 * Copyright (C) 2005 Takahiro Hirofuchi
26 * - names_deinit() is added.
29 /*****************************************************************************/
31 #include <sys/types.h>
46 /* ---------------------------------------------------------------------- */
56 u_int16_t vendorid, productid;
67 struct subclass *next;
68 u_int8_t classid, subclassid;
73 struct protocol *next;
74 u_int8_t classid, subclassid, protocolid;
78 struct audioterminal {
79 struct audioterminal *next;
84 struct genericstrtable {
85 struct genericstrtable *next;
90 /* ---------------------------------------------------------------------- */
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)
103 return num & (HASHSZ-1);
106 /* ---------------------------------------------------------------------- */
108 static struct vendor *vendors[HASHSZ] = { NULL, };
109 static struct product *products[HASHSZ] = { NULL, };
110 static struct class *classes[HASHSZ] = { NULL, };
111 static struct subclass *subclasses[HASHSZ] = { NULL, };
112 static struct protocol *protocols[HASHSZ] = { NULL, };
113 static struct audioterminal *audioterminals[HASHSZ] = { NULL, };
114 static struct genericstrtable *hiddescriptors[HASHSZ] = { NULL, };
115 static struct genericstrtable *reports[HASHSZ] = { NULL, };
116 static struct genericstrtable *huts[HASHSZ] = { NULL, };
117 static struct genericstrtable *biass[HASHSZ] = { NULL, };
118 static struct genericstrtable *physdess[HASHSZ] = { NULL, };
119 static struct genericstrtable *hutus[HASHSZ] = { NULL, };
120 static struct genericstrtable *langids[HASHSZ] = { NULL, };
121 static struct genericstrtable *countrycodes[HASHSZ] = { NULL, };
123 /* ---------------------------------------------------------------------- */
125 static const char *names_genericstrtable(struct genericstrtable *t[HASHSZ], unsigned int index)
127 struct genericstrtable *h;
129 for (h = t[hashnum(index)]; h; h = h->next)
135 const char *names_hid(u_int8_t hidd)
137 return names_genericstrtable(hiddescriptors, hidd);
140 const char *names_reporttag(u_int8_t rt)
142 return names_genericstrtable(reports, rt);
145 const char *names_huts(unsigned int data)
147 return names_genericstrtable(huts, data);
150 const char *names_hutus(unsigned int data)
152 return names_genericstrtable(hutus, data);
155 const char *names_langid(u_int16_t langid)
157 return names_genericstrtable(langids, langid);
160 const char *names_physdes(u_int8_t ph)
162 return names_genericstrtable(physdess, ph);
165 const char *names_bias(u_int8_t b)
167 return names_genericstrtable(biass, b);
170 const char *names_countrycode(unsigned int countrycode)
172 return names_genericstrtable(countrycodes, countrycode);
175 const char *names_vendor(u_int16_t vendorid)
179 v = vendors[hashnum(vendorid)];
180 for (; v; v = v->next)
181 if (v->vendorid == vendorid)
186 const char *names_product(u_int16_t vendorid, u_int16_t productid)
190 p = products[hashnum((vendorid << 16) | productid)];
191 for (; p; p = p->next)
192 if (p->vendorid == vendorid && p->productid == productid)
197 const char *names_class(u_int8_t classid)
201 c = classes[hashnum(classid)];
202 for (; c; c = c->next)
203 if (c->classid == classid)
208 const char *names_subclass(u_int8_t classid, u_int8_t subclassid)
212 s = subclasses[hashnum((classid << 8) | subclassid)];
213 for (; s; s = s->next)
214 if (s->classid == classid && s->subclassid == subclassid)
219 const char *names_protocol(u_int8_t classid, u_int8_t subclassid, u_int8_t protocolid)
223 p = protocols[hashnum((classid << 16) | (subclassid << 8) | protocolid)];
224 for (; p; p = p->next)
225 if (p->classid == classid && p->subclassid == subclassid && p->protocolid == protocolid)
230 const char *names_audioterminal(u_int16_t termt)
232 struct audioterminal *at;
234 at = audioterminals[hashnum(termt)];
235 for (; at; at = at->next)
236 if (at->termt == termt)
241 /* ---------------------------------------------------------------------- */
242 /* add a cleanup function by takahiro */
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);
271 void names_free(void)
278 for (pool = pool_head; pool != NULL; ) {
290 /* ---------------------------------------------------------------------- */
292 static int new_vendor(const char *name, u_int16_t vendorid)
295 unsigned int h = hashnum(vendorid);
298 for (; v; v = v->next)
299 if (v->vendorid == vendorid)
301 v = my_malloc(sizeof(struct vendor) + strlen(name));
304 strcpy(v->name, name);
305 v->vendorid = vendorid;
306 v->next = vendors[h];
311 static int new_product(const char *name, u_int16_t vendorid, u_int16_t productid)
314 unsigned int h = hashnum((vendorid << 16) | productid);
317 for (; p; p = p->next)
318 if (p->vendorid == vendorid && p->productid == productid)
320 p = my_malloc(sizeof(struct product) + strlen(name));
323 strcpy(p->name, name);
324 p->vendorid = vendorid;
325 p->productid = productid;
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)
338 if (c->classid == classid)
340 c = my_malloc(sizeof(struct class) + strlen(name));
343 strcpy(c->name, name);
344 c->classid = classid;
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)
357 if (s->classid == classid && s->subclassid == subclassid)
359 s = my_malloc(sizeof(struct subclass) + strlen(name));
362 strcpy(s->name, name);
363 s->classid = classid;
364 s->subclassid = subclassid;
365 s->next = subclasses[h];
370 static int new_protocol(const char *name, u_int8_t classid, u_int8_t subclassid, u_int8_t protocolid)
373 unsigned int h = hashnum((classid << 16) | (subclassid << 8) | protocolid);
376 for (; p; p = p->next)
377 if (p->classid == classid && p->subclassid == subclassid && p->protocolid == protocolid)
379 p = my_malloc(sizeof(struct protocol) + strlen(name));
382 strcpy(p->name, name);
383 p->classid = classid;
384 p->subclassid = subclassid;
385 p->protocolid = protocolid;
386 p->next = protocols[h];
391 static int new_audioterminal(const char *name, u_int16_t termt)
393 struct audioterminal *at;
394 unsigned int h = hashnum(termt);
396 at = audioterminals[h];
397 for (; at; at = at->next)
398 if (at->termt == termt)
400 at = my_malloc(sizeof(struct audioterminal) + strlen(name));
403 strcpy(at->name, name);
405 at->next = audioterminals[h];
406 audioterminals[h] = at;
410 static int new_genericstrtable(struct genericstrtable *t[HASHSZ], const char *name, unsigned int index)
412 struct genericstrtable *g;
413 unsigned int h = hashnum(index);
415 for (g = t[h]; g; g = g->next)
418 g = my_malloc(sizeof(struct genericstrtable) + strlen(name));
421 strcpy(g->name, name);
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);
457 static int new_bias(const char *name, u_int8_t b)
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);
467 /* ---------------------------------------------------------------------- */
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)) {
480 /* remove line ends */
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' && /*isspace(buf[7])*/ buf[7] == ' ') {
493 if (!isxdigit(*cp)) {
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' && /*isspace(buf[3])*/ buf[3] == ' ') {
514 if (!isxdigit(*cp)) {
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' && /*isspace(buf[4])*/ buf[4] == ' ') {
535 if (!isxdigit(*cp)) {
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' && /*isspace(buf[1])*/ buf[1] == ' ') {
556 if (!isxdigit(*cp)) {
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' && /*isspace(buf[1])*/ buf[1] == ' ') {
579 if (!isxdigit(*cp)) {
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])) {
598 /* audio terminal type spec */
602 if (!isxdigit(*cp)) {
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])) {
619 /* HID Descriptor bCountryCode */
623 if (!isxdigit(*cp)) {
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])) {
656 /* product or subclass spec */
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' && /*isspace(buf[3])*/ buf[3] == ' ') {
712 if (!isxdigit(*cp)) {
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' && /*isspace(buf[3])*/ buf[3] == ' ') {
733 if (!isxdigit(*cp)) {
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] == ' ') {
756 if (!isxdigit(*cp)) {
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);
781 /* ---------------------------------------------------------------------- */
783 int names_init(char *n)
787 if (!(f = fopen(n, "r"))) {