2 * The PCI Library -- ID to Name Hash
4 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
20 struct id_bucket *next;
25 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
28 struct id_bucket *next;
31 #define BUCKET_ALIGNMENT sizeof(union id_align)
33 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
35 static void *id_alloc(struct pci_access *a, unsigned int size)
37 struct id_bucket *buck = a->current_id_bucket;
42 a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
43 memset(a->id_hash, 0, sizeof(struct id_entry *) * HASH_SIZE);
46 if (!buck || buck->full + size > BUCKET_SIZE)
48 buck = pci_malloc(a, BUCKET_SIZE);
49 buck->next = a->current_id_bucket;
50 a->current_id_bucket = buck;
51 buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
54 buck->full = BUCKET_ALIGN(buck->full + size);
55 return (byte *)buck + pos;
58 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
62 h = id12 ^ (id34 << 3) ^ (cat << 5);
67 pci_id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text, enum id_entry_src src)
69 u32 id12 = id_pair(id1, id2);
70 u32 id34 = id_pair(id3, id4);
71 unsigned int h = id_hash(cat, id12, id34);
72 struct id_entry *n = a->id_hash ? a->id_hash[h] : NULL;
73 int len = strlen(text);
75 while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
79 n = id_alloc(a, sizeof(struct id_entry) + len);
84 memcpy(n->name, text, len+1);
85 n->next = a->id_hash[h];
91 *pci_id_lookup(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4)
93 struct id_entry *n, *best;
97 if (!(flags & PCI_LOOKUP_SKIP_LOCAL))
100 const char *key = NULL;
101 struct udev *udev = udev_new();
102 struct udev_hwdb *hwdb = udev_hwdb_new(udev);
103 struct udev_list_entry *entry;
108 sprintf(modalias, "pci:v%08X*", id1);
109 key = "ID_VENDOR_FROM_DATABASE";
112 sprintf(modalias, "pci:v%08Xd%08X*", id1, id2);
113 key = "ID_MODEL_FROM_DATABASE";
116 sprintf(modalias, "pci:v%08Xd%08Xsv%08Xsd%08X*", id1, id2, id3, id4);
117 key = "ID_MODEL_FROM_DATABASE";
119 case ID_GEN_SUBSYSTEM:
120 sprintf(modalias, "pci:v*d*sv%08Xsd%08X*", id1, id2);
121 key = "ID_MODEL_FROM_DATABASE";
124 sprintf(modalias, "pci:v*d*sv*sd*bc%02X*", id1);
125 key = "ID_PCI_CLASS_FROM_DATABASE";
128 sprintf(modalias, "pci:v*d*sv*sd*bc%02Xsc%02X*", id1, id2);
129 key = "ID_PCI_SUBCLASS_FROM_DATABASE";
132 sprintf(modalias, "pci:v*d*sv*sd*bc%02Xsc%02Xi%02X*", id1, id2, id3);
133 key = "ID_PCI_INTERFACE_FROM_DATABASE";
138 udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0))
139 if (strcmp(udev_list_entry_get_name(entry), key) == 0)
140 return udev_list_entry_get_value(entry);
144 id12 = id_pair(id1, id2);
145 id34 = id_pair(id3, id4);
149 n = a->id_hash[id_hash(cat, id12, id34)];
153 if (n->id12 != id12 || n->id34 != id34 || n->cat != cat)
155 if (n->src == SRC_LOCAL && (flags & PCI_LOOKUP_SKIP_LOCAL))
157 if (n->src == SRC_NET && !(flags & PCI_LOOKUP_NETWORK))
159 if (n->src == SRC_CACHE && !(flags & PCI_LOOKUP_CACHE))
161 if (!best || best->src < n->src)
171 pci_id_hash_free(struct pci_access *a)
173 pci_mfree(a->id_hash);
175 while (a->current_id_bucket)
177 struct id_bucket *buck = a->current_id_bucket;
178 a->current_id_bucket = buck->next;