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_str(char *to, const char *from, size_t count)
53 /* strip trailing whitespace */
54 len = strnlen(from, count);
55 while (len && isspace(from[len-1]))
58 /* strip leading whitespace */
60 while (isspace(from[i]) && (i < len))
65 /* substitute multiple whitespace */
66 if (isspace(from[i])) {
67 while (isspace(from[i]))
71 /* Replace '/' with '.' */
77 /* skip non-printable chars */
78 if (!isalnum(from[i]) && !ispunct(from[i])) {
87 static void set_usb_iftype(char *to, int if_class_num, size_t len)
89 char *type = "generic";
91 switch (if_class_num) {
104 case 2: /* CDC-Control */
105 case 5: /* Physical */
108 case 0x0a: /* CDC-Data */
109 case 0x0b: /* Chip/Smart Card */
110 case 0x0d: /* Content Security */
111 case 0x0e: /* Video */
112 case 0xdc: /* Diagnostic Device */
113 case 0xe0: /* Wireless Controller */
114 case 0xf2: /* Application-specific */
115 case 0xff: /* Vendor-specific */
120 strncpy(to, type, len);
124 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
128 char *type = "generic";
130 type_num = strtoul(from, &eptr, 0);
140 case 5: /* SFF-8070i */
143 case 1: /* RBC devices */
144 case 6: /* Transparent SPC-2 devices */
151 util_strlcpy(to, type, len);
156 static void set_scsi_type(char *to, const char *from, size_t len)
160 char *type = "generic";
162 type_num = strtoul(from, &eptr, 0);
184 util_strlcpy(to, type, len);
188 * A unique USB identification is generated like this:
190 * 1.) Get the USB device type from DeviceClass, InterfaceClass
191 * and InterfaceSubClass
192 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
193 * use the SCSI vendor and model as USB-Vendor and USB-model.
194 * 3.) Otherwise use the USB manufacturer and product as
195 * USB-Vendor and USB-model. Any non-printable characters
196 * in those strings will be skipped; a slash '/' will be converted
197 * into a full stop '.'.
198 * 4.) If that fails, too, we will use idVendor and idProduct
199 * as USB-Vendor and USB-model.
200 * 5.) The USB identification is the USB-vendor and USB-model
201 * string concatenated with an underscore '_'.
202 * 6.) If the device supplies a serial number, this number
203 * is concatenated with the identification with an underscore '_'.
205 static int usb_id(struct udev_device *dev)
207 struct udev *udev = udev_device_get_udev(dev);
208 struct udev_device *dev_interface;
209 struct udev_device *dev_usb;
210 const char *if_class, *if_subclass;
214 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
216 /* usb interface directory */
217 dev_interface = udev_device_get_parent_with_subsystem(dev, "usb");
218 if (dev_interface == NULL) {
219 info(udev, "unable to access usb_interface device of '%s'\n",
220 udev_device_get_syspath(dev));
224 if_class = udev_device_get_attr_value(dev_interface, "bInterfaceClass");
226 info(udev, "%s: cannot get bInterfaceClass attribute\n",
227 udev_device_get_sysname(dev));
230 if_class_num = strtoul(if_class, NULL, 16);
231 if (if_class_num == 8) {
232 if_subclass = udev_device_get_attr_value(dev_interface, "bInterfaceSubClass");
233 if (if_subclass != NULL)
234 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
236 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
239 info(udev, "%s: if_class %d protocol %d\n",
240 udev_device_get_syspath(dev_interface), if_class_num, protocol);
242 /* usb device directory */
243 dev_usb = udev_device_get_parent_with_subsystem(dev_interface, "usb");
245 info(udev, "unable to find parent 'usb' device of '%s'\n",
246 udev_device_get_syspath(dev));
251 if (protocol == 6 && !use_usb_info) {
252 struct udev_device *dev_scsi;
253 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
254 int host, bus, target, lun;
256 /* get scsi device */
257 dev_scsi = udev_device_get_parent_with_subsystem(dev, "scsi");
258 if (dev_scsi == NULL) {
259 info(udev, "unable to find parent 'scsi' device of '%s'\n",
260 udev_device_get_syspath(dev));
263 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
264 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
268 /* Generic SPC-2 device */
269 scsi_vendor = udev_device_get_attr_value(dev_scsi, "vendor");
271 info(udev, "%s: cannot get SCSI vendor attribute\n",
272 udev_device_get_sysname(dev_scsi));
275 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
277 scsi_model = udev_device_get_attr_value(dev_scsi, "model");
279 info(udev, "%s: cannot get SCSI model attribute\n",
280 udev_device_get_sysname(dev_scsi));
283 set_str(model_str, scsi_model, sizeof(model_str)-1);
285 scsi_type = udev_device_get_attr_value(dev_scsi, "type");
287 info(udev, "%s: cannot get SCSI type attribute\n",
288 udev_device_get_sysname(dev_scsi));
291 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
293 scsi_rev = udev_device_get_attr_value(dev_scsi, "rev");
295 info(udev, "%s: cannot get SCSI revision attribute\n",
296 udev_device_get_sysname(dev_scsi));
299 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
302 * some broken devices have the same identifiers
303 * for all luns, export the target:lun number
305 sprintf(instance_str, "%d:%d", target, lun);
309 /* fallback to USB vendor & device */
310 if (vendor_str[0] == '\0') {
311 const char *usb_vendor = NULL;
314 usb_vendor = udev_device_get_attr_value(dev_usb, "manufacturer");
317 usb_vendor = udev_device_get_attr_value(dev_usb, "idVendor");
320 info(udev, "No USB vendor information available\n");
323 set_str(vendor_str, usb_vendor, sizeof(vendor_str)-1);
326 if (model_str[0] == '\0') {
327 const char *usb_model = NULL;
330 usb_model = udev_device_get_attr_value(dev_usb, "product");
333 usb_model = udev_device_get_attr_value(dev_usb, "idProduct");
336 dbg(udev, "No USB model information available\n");
339 set_str(model_str, usb_model, sizeof(model_str)-1);
342 if (revision_str[0] == '\0') {
345 usb_rev = udev_device_get_attr_value(dev_usb, "bcdDevice");
347 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
350 if (serial_str[0] == '\0') {
351 const char *usb_serial;
353 usb_serial = udev_device_get_attr_value(dev_usb, "serial");
355 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
360 int main(int argc, char **argv)
362 static const struct option options[] = {
363 { "usb-info", no_argument, NULL, 'u' },
364 { "num-info", no_argument, NULL, 'n' },
365 { "export", no_argument, NULL, 'x' },
366 { "debug", no_argument, NULL, 'd' },
367 { "help", no_argument, NULL, 'h' },
371 struct udev_device *dev;
372 char syspath[UTIL_PATH_SIZE];
381 logging_init("usb_id");
382 udev_set_log_fn(udev, log_fn);
387 option = getopt_long(argc, argv, "dnuxh", options, NULL);
394 if (udev_get_log_priority(udev) < LOG_INFO)
395 udev_set_log_priority(udev, LOG_INFO);
408 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
409 " --usb-info use usb strings instead\n"
410 " --num-info use numerical values\n"
411 " --export print values as environemt keys\n"
412 " --help print this help text\n\n");
419 devpath = getenv("DEVPATH");
421 devpath = argv[optind];
422 if (devpath == NULL) {
423 fprintf(stderr, "No device specified\n");
428 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
429 util_strlcat(syspath, devpath, sizeof(syspath));
430 dev = udev_device_new_from_syspath(udev, syspath);
432 err(udev, "unable to access '%s'\n", devpath);
436 retval = usb_id(dev);
440 util_strlcpy(serial, vendor_str, sizeof(serial));
441 util_strlcat(serial, "_", sizeof(serial));
442 util_strlcat(serial, model_str, sizeof(serial));
443 if (serial_str[0] != '\0') {
444 util_strlcat(serial, "_", sizeof(serial));
445 util_strlcat(serial, serial_str, sizeof(serial));
447 if (instance_str[0] != '\0') {
448 util_strlcat(serial, "-", sizeof(serial));
449 util_strlcat(serial, instance_str, sizeof(serial));
453 printf("ID_VENDOR=%s\n", vendor_str);
454 printf("ID_MODEL=%s\n", model_str);
455 printf("ID_REVISION=%s\n", revision_str);
456 printf("ID_SERIAL=%s\n", serial);
457 if (serial_str[0] != '\0')
458 printf("ID_SERIAL_SHORT=%s\n", serial_str);
459 printf("ID_TYPE=%s\n", type_str);
460 if (instance_str[0] != '\0')
461 printf("ID_INSTANCE=%s\n", instance_str);
462 printf("ID_BUS=usb\n");
464 printf("%s\n", serial);
468 udev_device_unref(dev);