2 * usb_id - identify an USB device
4 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
7 * Hannes Reinecke <hare@suse.de>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
23 #include "../../udev/udev.h"
27 static void log_fn(struct udev *udev, int priority,
28 const char *file, int line, const char *fn,
29 const char *format, va_list args)
32 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
33 vfprintf(stderr, format, args);
35 vsyslog(priority, format, args);
39 static char vendor_str[64];
40 static char model_str[64];
41 static char serial_str[UTIL_NAME_SIZE];
42 static char revision_str[64];
43 static char type_str[64];
44 static char instance_str[64];
46 static int use_usb_info;
47 static int use_num_info;
49 static void set_usb_iftype(char *to, int if_class_num, size_t len)
51 char *type = "generic";
53 switch (if_class_num) {
66 case 2: /* CDC-Control */
68 case 5: /* Physical */
74 case 0x0a: /* CDC-Data */
76 case 0x0b: /* Chip/Smart Card */
78 case 0x0d: /* Content Security */
83 case 0xdc: /* Diagnostic Device */
85 case 0xe0: /* Wireless Controller */
87 case 0xf2: /* Application-specific */
89 case 0xff: /* Vendor-specific */
94 strncpy(to, type, len);
98 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
102 char *type = "generic";
104 type_num = strtoul(from, &eptr, 0);
114 case 5: /* SFF-8070i */
117 case 1: /* RBC devices */
120 case 6: /* Transparent SPC-2 devices */
127 util_strlcpy(to, type, len);
132 static void set_scsi_type(char *to, const char *from, size_t len)
136 char *type = "generic";
138 type_num = strtoul(from, &eptr, 0);
160 util_strlcpy(to, type, len);
164 * A unique USB identification is generated like this:
166 * 1.) Get the USB device type from DeviceClass, InterfaceClass
167 * and InterfaceSubClass
168 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
169 * use the SCSI vendor and model as USB-Vendor and USB-model.
170 * 3.) Otherwise use the USB manufacturer and product as
171 * USB-Vendor and USB-model. Any non-printable characters
172 * in those strings will be skipped; a slash '/' will be converted
173 * into a full stop '.'.
174 * 4.) If that fails, too, we will use idVendor and idProduct
175 * as USB-Vendor and USB-model.
176 * 5.) The USB identification is the USB-vendor and USB-model
177 * string concatenated with an underscore '_'.
178 * 6.) If the device supplies a serial number, this number
179 * is concatenated with the identification with an underscore '_'.
181 static int usb_id(struct udev_device *dev)
183 struct udev *udev = udev_device_get_udev(dev);
184 struct udev_device *dev_interface;
185 struct udev_device *dev_usb;
186 const char *if_class, *if_subclass;
190 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
192 /* usb interface directory */
193 dev_interface = udev_device_get_parent_with_subsystem(dev, "usb");
194 if (dev_interface == NULL) {
195 info(udev, "unable to access usb_interface device of '%s'\n",
196 udev_device_get_syspath(dev));
200 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
202 info(udev, "%s: cannot get bInterfaceClass attribute\n",
203 udev_device_get_sysname(dev));
206 if_class_num = strtoul(if_class, NULL, 16);
207 if (if_class_num == 8) {
208 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
209 if (if_subclass != NULL)
210 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
212 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
215 info(udev, "%s: if_class %d protocol %d\n",
216 udev_device_get_syspath(dev_interface), if_class_num, protocol);
218 /* usb device directory */
219 dev_usb = udev_device_get_parent_with_subsystem(dev_interface, "usb");
221 info(udev, "unable to find parent 'usb' device of '%s'\n",
222 udev_device_get_syspath(dev));
227 if (protocol == 6 && !use_usb_info) {
228 struct udev_device *dev_scsi;
229 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
230 int host, bus, target, lun;
232 /* get scsi device */
233 dev_scsi = udev_device_get_parent_with_subsystem(dev, "scsi");
234 if (dev_scsi == NULL) {
235 info(udev, "unable to find parent 'scsi' device of '%s'\n",
236 udev_device_get_syspath(dev));
239 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
240 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
244 /* Generic SPC-2 device */
245 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
247 info(udev, "%s: cannot get SCSI vendor attribute\n",
248 udev_device_get_sysname(dev_scsi));
251 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
252 udev_util_replace_chars(vendor_str, NULL);
254 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
256 info(udev, "%s: cannot get SCSI model attribute\n",
257 udev_device_get_sysname(dev_scsi));
260 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
261 udev_util_replace_chars(model_str, NULL);
263 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
265 info(udev, "%s: cannot get SCSI type attribute\n",
266 udev_device_get_sysname(dev_scsi));
269 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
271 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
273 info(udev, "%s: cannot get SCSI revision attribute\n",
274 udev_device_get_sysname(dev_scsi));
277 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
278 udev_util_replace_chars(revision_str, NULL);
281 * some broken devices have the same identifiers
282 * for all luns, export the target:lun number
284 sprintf(instance_str, "%d:%d", target, lun);
288 /* fallback to USB vendor & device */
289 if (vendor_str[0] == '\0') {
290 const char *usb_vendor = NULL;
293 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
296 usb_vendor = udev_device_get_sysattr_value(dev_usb, "idVendor");
299 info(udev, "No USB vendor information available\n");
302 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
303 udev_util_replace_chars(vendor_str, NULL);
306 if (model_str[0] == '\0') {
307 const char *usb_model = NULL;
310 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
313 usb_model = udev_device_get_sysattr_value(dev_usb, "idProduct");
316 dbg(udev, "No USB model information available\n");
319 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
320 udev_util_replace_chars(model_str, NULL);
323 if (revision_str[0] == '\0') {
326 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
328 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
329 udev_util_replace_chars(revision_str, NULL);
333 if (serial_str[0] == '\0') {
334 const char *usb_serial;
336 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
338 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
339 udev_util_replace_chars(serial_str, NULL);
345 int main(int argc, char **argv)
347 static const struct option options[] = {
348 { "usb-info", no_argument, NULL, 'u' },
349 { "num-info", no_argument, NULL, 'n' },
350 { "export", no_argument, NULL, 'x' },
351 { "debug", no_argument, NULL, 'd' },
352 { "help", no_argument, NULL, 'h' },
356 struct udev_device *dev = NULL;
357 char syspath[UTIL_PATH_SIZE];
366 logging_init("usb_id");
367 udev_set_log_fn(udev, log_fn);
372 option = getopt_long(argc, argv, "dnuxh", options, NULL);
379 if (udev_get_log_priority(udev) < LOG_INFO)
380 udev_set_log_priority(udev, LOG_INFO);
393 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
394 " --usb-info use usb strings instead\n"
395 " --num-info use numerical values\n"
396 " --export print values as environemt keys\n"
397 " --help print this help text\n\n");
404 devpath = getenv("DEVPATH");
406 devpath = argv[optind];
407 if (devpath == NULL) {
408 fprintf(stderr, "No device specified\n");
413 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
414 util_strlcat(syspath, devpath, sizeof(syspath));
415 dev = udev_device_new_from_syspath(udev, syspath);
417 err(udev, "unable to access '%s'\n", devpath);
421 retval = usb_id(dev);
425 util_strlcpy(serial, vendor_str, sizeof(serial));
426 util_strlcat(serial, "_", sizeof(serial));
427 util_strlcat(serial, model_str, sizeof(serial));
428 if (serial_str[0] != '\0') {
429 util_strlcat(serial, "_", sizeof(serial));
430 util_strlcat(serial, serial_str, sizeof(serial));
432 if (instance_str[0] != '\0') {
433 util_strlcat(serial, "-", sizeof(serial));
434 util_strlcat(serial, instance_str, sizeof(serial));
438 printf("ID_VENDOR=%s\n", vendor_str);
439 printf("ID_MODEL=%s\n", model_str);
440 printf("ID_REVISION=%s\n", revision_str);
441 printf("ID_SERIAL=%s\n", serial);
442 if (serial_str[0] != '\0')
443 printf("ID_SERIAL_SHORT=%s\n", serial_str);
444 printf("ID_TYPE=%s\n", type_str);
445 if (instance_str[0] != '\0')
446 printf("ID_INSTANCE=%s\n", instance_str);
447 printf("ID_BUS=usb\n");
449 printf("%s\n", serial);
453 udev_device_unref(dev);