2 * usb-db, pci-db - lookup vendor/product database
4 * Copyright (C) 2009 Lennart Poettering <lennart@poettering.net>
5 * Copyright (C) 2011 Kay Sievers <kay.sievers@vrfy.org>
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, see <http://www.gnu.org/licenses/>.
30 static int get_id_attr(
31 struct udev_device *parent,
38 if (!(t = udev_device_get_sysattr_value(parent, name))) {
39 fprintf(stderr, "%s lacks %s.\n", udev_device_get_syspath(parent), name);
43 if (startswith(t, "0x"))
46 if (sscanf(t, "%04x", &u) != 1 || u > 0xFFFFU) {
47 fprintf(stderr, "Failed to parse %s on %s.\n", name, udev_device_get_syspath(parent));
51 *value = (uint16_t) u;
55 static int get_vid_pid(
56 struct udev_device *parent,
57 const char *vendor_attr,
58 const char *product_attr,
62 if (get_id_attr(parent, vendor_attr, vid) < 0)
65 fprintf(stderr, "Invalid vendor id.\n");
69 if (get_id_attr(parent, product_attr, pid) < 0)
75 static void rstrip(char *n) {
78 for (i = strlen(n); i > 0 && isspace(n[i-1]); i--)
82 #define HEXCHARS "0123456789abcdefABCDEF"
83 #define WHITESPACE " \t\n\r"
84 static int lookup_vid_pid(const char *database,
85 uint16_t vid, uint16_t pid,
86 char **vendor, char **product)
94 *vendor = *product = NULL;
96 if (!(f = fopen(database, "rme"))) {
97 fprintf(stderr, "Failed to open database file '%s': %s\n", database, strerror(errno));
104 if (getline(&line, &n, f) < 0)
109 if (line[0] == '#' || line[0] == 0)
112 if (strspn(line, HEXCHARS) == 4) {
118 if (sscanf(line, "%04x", &u) == 1 && u == vid) {
122 t += strspn(t, WHITESPACE);
124 if (!(*vendor = strdup(t))) {
125 fprintf(stderr, "Out of memory.\n");
135 if (found_vendor && line[0] == '\t' && strspn(line+1, HEXCHARS) == 4) {
138 if (sscanf(line+1, "%04x", &u) == 1 && u == pid) {
142 t += strspn(t, WHITESPACE);
144 if (!(*product = strdup(t))) {
145 fprintf(stderr, "Out of memory.\n");
164 *product = *vendor = NULL;
170 static struct udev_device *find_device(struct udev_device *dev, const char *subsys, const char *devtype)
174 str = udev_device_get_subsystem(dev);
177 if (strcmp(str, subsys) != 0)
180 if (devtype != NULL) {
181 str = udev_device_get_devtype(dev);
184 if (strcmp(str, devtype) != 0)
189 return udev_device_get_parent_with_subsystem_devtype(dev, subsys, devtype);
193 static int builtin_db(struct udev_device *dev, bool test,
194 const char *database,
195 const char *vendor_attr, const char *product_attr,
196 const char *subsys, const char *devtype)
198 struct udev_device *parent;
199 uint16_t vid = 0, pid = 0;
200 char *vendor = NULL, *product = NULL;
202 parent = find_device(dev, subsys, devtype);
204 fprintf(stderr, "Failed to find device.\n");
208 if (get_vid_pid(parent, vendor_attr, product_attr, &vid, &pid) < 0)
211 if (lookup_vid_pid(database, vid, pid, &vendor, &product) < 0)
215 udev_builtin_add_property(dev, test, "ID_VENDOR_FROM_DATABASE", vendor);
217 udev_builtin_add_property(dev, test, "ID_MODEL_FROM_DATABASE", product);
225 static int builtin_usb_db(struct udev_device *dev, int argc, char *argv[], bool test)
227 return builtin_db(dev, test, USB_DATABASE, "idVendor", "idProduct", "usb", "usb_device");
230 static int builtin_pci_db(struct udev_device *dev, int argc, char *argv[], bool test)
232 return builtin_db(dev, test, PCI_DATABASE, "vendor", "device", "pci", NULL);
235 const struct udev_builtin udev_builtin_usb_db = {
237 .cmd = builtin_usb_db,
238 .help = "USB vendor/product database",
242 const struct udev_builtin udev_builtin_pci_db = {
244 .cmd = builtin_pci_db,
245 .help = "PCI vendor/product database",