2 * Copyright (C) 2009 Lennart Poettering <lennart@poettering.net>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details:
24 #if defined(BUILD_FOR_USB)
25 # define DATABASE USB_DATABASE
26 # define SUBSYSTEM "usb"
27 # define DEVTYPE "usb_device"
28 # define VENDOR_ATTR "idVendor"
29 # define PRODUCT_ATTR "idProduct"
30 #elif defined(BUILD_FOR_PCI)
31 # define DATABASE PCI_DATABASE
32 # define SUBSYSTEM "pci"
34 # define VENDOR_ATTR "vendor"
35 # define PRODUCT_ATTR "device"
37 # error "Are you havin' a laugh?"
40 static int get_id_attr(
41 struct udev_device *parent,
48 if (!(t = udev_device_get_sysattr_value(parent, name))) {
49 fprintf(stderr, "%s lacks %s.\n", udev_device_get_syspath(parent), name);
53 if (!strncmp(t, "0x", 2))
56 if (sscanf(t, "%04x", &u) != 1 || u > 0xFFFFU) {
57 fprintf(stderr, "Failed to parse %s on %s.\n", name, udev_device_get_syspath(parent));
61 *value = (uint16_t) u;
65 static int get_vid_pid(
66 struct udev_device *parent,
70 if (get_id_attr(parent, VENDOR_ATTR, vid) < 0)
73 fprintf(stderr, "Invalid vendor id.\n");
77 if (get_id_attr(parent, PRODUCT_ATTR, pid) < 0)
83 static void rstrip(char *n) {
86 for (i = strlen(n); i > 0 && isspace(n[i-1]); i--)
90 #define HEXCHARS "0123456789abcdefABCDEF"
91 #define WHITESPACE " \t\n\r"
93 static int lookup_vid_pid(
101 int found_vendor = 0;
104 *vendor = *product = NULL;
106 if (!(f = fopen(DATABASE, "r"))) {
107 fprintf(stderr, "Failed to open database file "DATABASE": %s\n", strerror(errno));
119 if (getline(&line, &n, f) < 0)
124 if (line[0] == '#' || line[0] == 0)
127 if (strspn(line, HEXCHARS) == 4) {
133 if (sscanf(line, "%04x", &u) == 1 && u == vid) {
137 t += strspn(t, WHITESPACE);
139 if (!(*vendor = strdup(t))) {
140 fprintf(stderr, "Out of memory.\n");
150 if (found_vendor && line[0] == '\t' && strspn(line+1, HEXCHARS) == 4) {
153 if (sscanf(line+1, "%04x", &u) == 1 && u == pid) {
157 t += strspn(t, WHITESPACE);
159 if (!(*product = strdup(t))) {
160 fprintf(stderr, "Out of memory.\n");
179 *product = *vendor = NULL;
185 static struct udev_device *find_device(struct udev_device *dev, const char *subsys, const char *devtype)
189 str = udev_device_get_subsystem(dev);
192 if (strcmp(str, subsys) != 0)
195 if (devtype != NULL) {
196 str = udev_device_get_devtype(dev);
199 if (strcmp(str, devtype) != 0)
204 return udev_device_get_parent_with_subsystem_devtype(dev, SUBSYSTEM, DEVTYPE);
207 int main(int argc, char*argv[]) {
209 struct udev *udev = NULL;
212 struct udev_device *dev = NULL, *parent = NULL;
213 uint16_t vid = 0, pid = 0;
214 char *vendor = NULL, *product = NULL;
217 fprintf(stderr, "Need to pass sysfs path.\n");
221 if (!(udev = udev_new()))
224 if (asprintf(&sp, "%s%s", udev_get_sys_path(udev), argv[1]) < 0) {
225 fprintf(stderr, "Failed to allocate sysfs path.\n");
229 dev = udev_device_new_from_syspath(udev, sp);
233 fprintf(stderr, "Failed to access %s.\n", argv[1]);
237 parent = find_device(dev, SUBSYSTEM, DEVTYPE);
239 fprintf(stderr, "Failed to find device.\n");
243 if (get_vid_pid(parent, &vid, &pid) < 0)
246 if (lookup_vid_pid(vid, pid, &vendor, &product) < 0)
250 printf("ID_VENDOR_FROM_DATABASE=%s\n", vendor);
253 printf("ID_MODEL_FROM_DATABASE=%s\n", product);
258 udev_device_unref(dev);