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) {
57 case 2: /* CDC-Control */
62 case 5: /* Physical */
76 case 0x0a: /* CDC-Data */
78 case 0x0b: /* Chip/Smart Card */
80 case 0x0d: /* Content Security */
85 case 0xdc: /* Diagnostic Device */
87 case 0xe0: /* Wireless Controller */
89 case 0xfe: /* Application-specific */
91 case 0xff: /* Vendor-specific */
96 strncpy(to, type, len);
100 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
104 char *type = "generic";
106 type_num = strtoul(from, &eptr, 0);
116 case 5: /* SFF-8070i */
119 case 1: /* RBC devices */
122 case 6: /* Transparent SPC-2 devices */
129 util_strlcpy(to, type, len);
133 static void set_scsi_type(char *to, const char *from, size_t len)
137 char *type = "generic";
139 type_num = strtoul(from, &eptr, 0);
161 util_strlcpy(to, type, len);
165 * A unique USB identification is generated like this:
167 * 1.) Get the USB device type from DeviceClass, InterfaceClass
168 * and InterfaceSubClass
169 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
170 * use the SCSI vendor and model as USB-Vendor and USB-model.
171 * 3.) Otherwise use the USB manufacturer and product as
172 * USB-Vendor and USB-model. Any non-printable characters
173 * in those strings will be skipped; a slash '/' will be converted
174 * into a full stop '.'.
175 * 4.) If that fails, too, we will use idVendor and idProduct
176 * as USB-Vendor and USB-model.
177 * 5.) The USB identification is the USB-vendor and USB-model
178 * string concatenated with an underscore '_'.
179 * 6.) If the device supplies a serial number, this number
180 * is concatenated with the identification with an underscore '_'.
182 static int usb_id(struct udev_device *dev)
184 struct udev *udev = udev_device_get_udev(dev);
185 struct udev_device *dev_interface;
186 struct udev_device *dev_usb;
187 const char *if_class, *if_subclass;
191 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
193 /* usb interface directory */
194 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
195 if (dev_interface == NULL) {
196 info(udev, "unable to access usb_interface device of '%s'\n",
197 udev_device_get_syspath(dev));
201 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
203 info(udev, "%s: cannot get bInterfaceClass attribute\n",
204 udev_device_get_sysname(dev));
208 if_class_num = strtoul(if_class, NULL, 16);
209 if (if_class_num == 8) {
211 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
212 if (if_subclass != NULL)
213 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
215 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
218 info(udev, "%s: if_class %d protocol %d\n",
219 udev_device_get_syspath(dev_interface), if_class_num, protocol);
221 /* usb device directory */
222 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
224 info(udev, "unable to find parent 'usb' device of '%s'\n",
225 udev_device_get_syspath(dev));
229 /* mass storage : SCSI or ATAPI */
230 if ((protocol == 6 || protocol == 2) && !use_usb_info) {
231 struct udev_device *dev_scsi;
232 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
233 int host, bus, target, lun;
235 /* get scsi device */
236 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
237 if (dev_scsi == NULL) {
238 info(udev, "unable to find parent 'scsi' device of '%s'\n",
239 udev_device_get_syspath(dev));
242 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
243 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
247 /* Generic SPC-2 device */
248 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
250 info(udev, "%s: cannot get SCSI vendor attribute\n",
251 udev_device_get_sysname(dev_scsi));
254 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
255 udev_util_replace_chars(vendor_str, NULL);
257 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
259 info(udev, "%s: cannot get SCSI model attribute\n",
260 udev_device_get_sysname(dev_scsi));
263 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
264 udev_util_replace_chars(model_str, NULL);
266 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
268 info(udev, "%s: cannot get SCSI type attribute\n",
269 udev_device_get_sysname(dev_scsi));
272 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
274 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
276 info(udev, "%s: cannot get SCSI revision attribute\n",
277 udev_device_get_sysname(dev_scsi));
280 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
281 udev_util_replace_chars(revision_str, NULL);
284 * some broken devices have the same identifiers
285 * for all luns, export the target:lun number
287 sprintf(instance_str, "%d:%d", target, lun);
291 /* fallback to USB vendor & device */
292 if (vendor_str[0] == '\0') {
293 const char *usb_vendor = NULL;
296 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
299 usb_vendor = udev_device_get_sysattr_value(dev_usb, "idVendor");
302 info(udev, "No USB vendor information available\n");
305 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
306 udev_util_replace_chars(vendor_str, NULL);
309 if (model_str[0] == '\0') {
310 const char *usb_model = NULL;
313 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
316 usb_model = udev_device_get_sysattr_value(dev_usb, "idProduct");
319 dbg(udev, "No USB model information available\n");
322 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
323 udev_util_replace_chars(model_str, NULL);
326 if (revision_str[0] == '\0') {
329 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
331 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
332 udev_util_replace_chars(revision_str, NULL);
336 if (serial_str[0] == '\0') {
337 const char *usb_serial;
339 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
341 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
342 udev_util_replace_chars(serial_str, NULL);
348 int main(int argc, char **argv)
350 static const struct option options[] = {
351 { "usb-info", no_argument, NULL, 'u' },
352 { "num-info", no_argument, NULL, 'n' },
353 { "export", no_argument, NULL, 'x' },
354 { "debug", no_argument, NULL, 'd' },
355 { "help", no_argument, NULL, 'h' },
359 struct udev_device *dev = NULL;
360 char syspath[UTIL_PATH_SIZE];
369 logging_init("usb_id");
370 udev_set_log_fn(udev, log_fn);
375 option = getopt_long(argc, argv, "dnuxh", options, NULL);
382 if (udev_get_log_priority(udev) < LOG_INFO)
383 udev_set_log_priority(udev, LOG_INFO);
396 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
397 " --usb-info use usb strings instead\n"
398 " --num-info use numerical values\n"
399 " --export print values as environment keys\n"
400 " --help print this help text\n\n");
407 devpath = getenv("DEVPATH");
409 devpath = argv[optind];
410 if (devpath == NULL) {
411 fprintf(stderr, "No device specified\n");
416 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
417 util_strlcat(syspath, devpath, sizeof(syspath));
418 dev = udev_device_new_from_syspath(udev, syspath);
420 err(udev, "unable to access '%s'\n", devpath);
424 retval = usb_id(dev);
428 util_strlcpy(serial, vendor_str, sizeof(serial));
429 util_strlcat(serial, "_", sizeof(serial));
430 util_strlcat(serial, model_str, sizeof(serial));
431 if (serial_str[0] != '\0') {
432 util_strlcat(serial, "_", sizeof(serial));
433 util_strlcat(serial, serial_str, sizeof(serial));
435 if (instance_str[0] != '\0') {
436 util_strlcat(serial, "-", sizeof(serial));
437 util_strlcat(serial, instance_str, sizeof(serial));
441 printf("ID_VENDOR=%s\n", vendor_str);
442 printf("ID_MODEL=%s\n", model_str);
443 printf("ID_REVISION=%s\n", revision_str);
444 printf("ID_SERIAL=%s\n", serial);
445 if (serial_str[0] != '\0')
446 printf("ID_SERIAL_SHORT=%s\n", serial_str);
447 printf("ID_TYPE=%s\n", type_str);
448 if (instance_str[0] != '\0')
449 printf("ID_INSTANCE=%s\n", instance_str);
450 printf("ID_BUS=usb\n");
452 printf("%s\n", serial);
456 udev_device_unref(dev);