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.
25 #include "libudev-private.h"
29 static void log_fn(struct udev *udev, int priority,
30 const char *file, int line, const char *fn,
31 const char *format, va_list args)
34 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
35 vfprintf(stderr, format, args);
37 vsyslog(priority, format, args);
41 static char vendor_str[64];
42 static char vendor_str_enc[256];
43 static const char *vendor_id = "";
44 static char model_str[64];
45 static char model_str_enc[256];
46 static const char *product_id = "";
47 static char serial_str[UTIL_NAME_SIZE];
48 static char packed_if_str[UTIL_NAME_SIZE];
49 static char revision_str[64];
50 static char type_str[64];
51 static char instance_str[64];
52 static const char *ifnum;
53 static const char *driver;
55 static int use_usb_info;
56 static int use_num_info;
58 static void set_usb_iftype(char *to, int if_class_num, size_t len)
60 char *type = "generic";
62 switch (if_class_num) {
66 case 2: /* CDC-Control */
71 case 5: /* Physical */
85 case 0x0a: /* CDC-Data */
87 case 0x0b: /* Chip/Smart Card */
89 case 0x0d: /* Content Security */
94 case 0xdc: /* Diagnostic Device */
96 case 0xe0: /* Wireless Controller */
98 case 0xfe: /* Application-specific */
100 case 0xff: /* Vendor-specific */
105 strncpy(to, type, len);
109 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
113 char *type = "generic";
115 type_num = strtoul(from, &eptr, 0);
125 case 5: /* SFF-8070i */
128 case 1: /* RBC devices */
131 case 6: /* Transparent SPC-2 devices */
138 util_strscpy(to, len, type);
142 static void set_scsi_type(char *to, const char *from, size_t len)
146 char *type = "generic";
148 type_num = strtoul(from, &eptr, 0);
170 util_strscpy(to, len, type);
173 #define USB_DT_DEVICE 0x01
174 #define USB_DT_INTERFACE 0x04
176 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len)
178 char *filename = NULL;
181 unsigned char buf[18 + 65535];
182 unsigned int pos, strpos;
183 struct usb_interface_descriptor {
185 u_int8_t bDescriptorType;
186 u_int8_t bInterfaceNumber;
187 u_int8_t bAlternateSetting;
188 u_int8_t bNumEndpoints;
189 u_int8_t bInterfaceClass;
190 u_int8_t bInterfaceSubClass;
191 u_int8_t bInterfaceProtocol;
193 } __attribute__((packed));
196 if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0) {
200 fd = open(filename, O_RDONLY);
202 fprintf(stderr, "error opening USB device 'descriptors' file\n");
206 size = read(fd, buf, sizeof(buf));
208 if (size < 18 || size == sizeof(buf)) {
215 while (pos < sizeof(buf) && strpos+7 < len) {
216 struct usb_interface_descriptor *desc;
219 desc = (struct usb_interface_descriptor *) &buf[pos];
220 if (desc->bLength < 3)
222 pos += desc->bLength;
224 if (desc->bDescriptorType != USB_DT_INTERFACE)
227 if (snprintf(if_str, 8, "%02x%02x%02x:",
228 desc->bInterfaceClass,
229 desc->bInterfaceSubClass,
230 desc->bInterfaceProtocol) != 7)
233 if (strstr(ifs_str, if_str) != NULL)
236 memcpy(&ifs_str[strpos], if_str, 8),
245 * A unique USB identification is generated like this:
247 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
248 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
249 * use the SCSI vendor and model as USB-Vendor and USB-model.
250 * 3.) Otherwise use the USB manufacturer and product as
251 * USB-Vendor and USB-model. Any non-printable characters
252 * in those strings will be skipped; a slash '/' will be converted
253 * into a full stop '.'.
254 * 4.) If that fails, too, we will use idVendor and idProduct
255 * as USB-Vendor and USB-model.
256 * 5.) The USB identification is the USB-vendor and USB-model
257 * string concatenated with an underscore '_'.
258 * 6.) If the device supplies a serial number, this number
259 * is concatenated with the identification with an underscore '_'.
261 static int usb_id(struct udev_device *dev)
263 struct udev *udev = udev_device_get_udev(dev);
264 struct udev_device *dev_interface = NULL;
265 struct udev_device *dev_usb = NULL;
266 const char *if_class, *if_subclass;
270 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
272 /* shortcut, if we are called directly for a "usb_device" type */
273 if (udev_device_get_devtype(dev) != NULL && strcmp(udev_device_get_devtype(dev), "usb_device") == 0) {
274 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
279 /* usb interface directory */
280 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
281 if (dev_interface == NULL) {
282 info(udev, "unable to access usb_interface device of '%s'\n",
283 udev_device_get_syspath(dev));
287 ifnum = udev_device_get_sysattr_value(dev_interface, "bInterfaceNumber");
288 driver = udev_device_get_sysattr_value(dev_interface, "driver");
290 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
292 info(udev, "%s: cannot get bInterfaceClass attribute\n",
293 udev_device_get_sysname(dev));
297 if_class_num = strtoul(if_class, NULL, 16);
298 if (if_class_num == 8) {
300 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
301 if (if_subclass != NULL)
302 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
304 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
307 info(udev, "%s: if_class %d protocol %d\n",
308 udev_device_get_syspath(dev_interface), if_class_num, protocol);
310 /* usb device directory */
311 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
313 info(udev, "unable to find parent 'usb' device of '%s'\n",
314 udev_device_get_syspath(dev));
318 /* all interfaces of the device in a single string */
319 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
321 /* mass storage : SCSI or ATAPI */
322 if ((protocol == 6 || protocol == 2) && !use_usb_info) {
323 struct udev_device *dev_scsi;
324 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
325 int host, bus, target, lun;
327 /* get scsi device */
328 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
329 if (dev_scsi == NULL) {
330 info(udev, "unable to find parent 'scsi' device of '%s'\n",
331 udev_device_get_syspath(dev));
334 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
335 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
339 /* Generic SPC-2 device */
340 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
342 info(udev, "%s: cannot get SCSI vendor attribute\n",
343 udev_device_get_sysname(dev_scsi));
346 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
347 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
348 udev_util_replace_chars(vendor_str, NULL);
350 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
352 info(udev, "%s: cannot get SCSI model attribute\n",
353 udev_device_get_sysname(dev_scsi));
356 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
357 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
358 udev_util_replace_chars(model_str, NULL);
360 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
362 info(udev, "%s: cannot get SCSI type attribute\n",
363 udev_device_get_sysname(dev_scsi));
366 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
368 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
370 info(udev, "%s: cannot get SCSI revision attribute\n",
371 udev_device_get_sysname(dev_scsi));
374 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
375 udev_util_replace_chars(revision_str, NULL);
378 * some broken devices have the same identifiers
379 * for all luns, export the target:lun number
381 sprintf(instance_str, "%d:%d", target, lun);
385 vendor_id = udev_device_get_sysattr_value(dev_usb, "idVendor");
386 product_id = udev_device_get_sysattr_value(dev_usb, "idProduct");
388 /* fallback to USB vendor & device */
389 if (vendor_str[0] == '\0') {
390 const char *usb_vendor = NULL;
393 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
396 usb_vendor = vendor_id;
399 info(udev, "No USB vendor information available\n");
402 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
403 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
404 udev_util_replace_chars(vendor_str, NULL);
407 if (model_str[0] == '\0') {
408 const char *usb_model = NULL;
411 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
414 usb_model = product_id;
417 dbg(udev, "No USB model information available\n");
420 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
421 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
422 udev_util_replace_chars(model_str, NULL);
425 if (revision_str[0] == '\0') {
428 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
430 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
431 udev_util_replace_chars(revision_str, NULL);
435 if (serial_str[0] == '\0') {
436 const char *usb_serial;
438 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
440 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
441 udev_util_replace_chars(serial_str, NULL);
447 int main(int argc, char **argv)
449 static const struct option options[] = {
450 { "usb-info", no_argument, NULL, 'u' },
451 { "num-info", no_argument, NULL, 'n' },
452 { "export", no_argument, NULL, 'x' },
453 { "debug", no_argument, NULL, 'd' },
454 { "help", no_argument, NULL, 'h' },
458 struct udev_device *dev = NULL;
459 char syspath[UTIL_PATH_SIZE];
468 udev_log_init("usb_id");
469 udev_set_log_fn(udev, log_fn);
474 option = getopt_long(argc, argv, "dnuxh", options, NULL);
481 if (udev_get_log_priority(udev) < LOG_INFO)
482 udev_set_log_priority(udev, LOG_INFO);
495 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
496 " --usb-info use usb strings instead\n"
497 " --num-info use numerical values\n"
498 " --export print values as environment keys\n"
499 " --help print this help text\n\n");
506 devpath = argv[optind];
507 if (devpath == NULL) {
508 fprintf(stderr, "No device specified\n");
513 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
514 dev = udev_device_new_from_syspath(udev, syspath);
516 err(udev, "unable to access '%s'\n", devpath);
520 retval = usb_id(dev);
527 l = util_strpcpyl(&s, sizeof(serial), vendor_str, "_", model_str, NULL);
528 if (serial_str[0] != '\0')
529 l = util_strpcpyl(&s, l, "_", serial_str, NULL);
530 if (instance_str[0] != '\0')
531 util_strpcpyl(&s, l, "-", instance_str, NULL);
534 printf("ID_VENDOR=%s\n", vendor_str);
535 printf("ID_VENDOR_ENC=%s\n", vendor_str_enc);
536 printf("ID_VENDOR_ID=%s\n", vendor_id);
537 printf("ID_MODEL=%s\n", model_str);
538 printf("ID_MODEL_ENC=%s\n", model_str_enc);
539 printf("ID_MODEL_ID=%s\n", product_id);
540 printf("ID_REVISION=%s\n", revision_str);
541 printf("ID_SERIAL=%s\n", serial);
542 if (serial_str[0] != '\0')
543 printf("ID_SERIAL_SHORT=%s\n", serial_str);
544 if (type_str[0] != '\0')
545 printf("ID_TYPE=%s\n", type_str);
546 if (instance_str[0] != '\0')
547 printf("ID_INSTANCE=%s\n", instance_str);
548 printf("ID_BUS=usb\n");
549 if (packed_if_str[0] != '\0')
550 printf("ID_USB_INTERFACES=:%s\n", packed_if_str);
552 printf("ID_USB_INTERFACE_NUM=%s\n", ifnum);
554 printf("ID_USB_DRIVER=%s\n", driver);
556 printf("%s\n", serial);
560 udev_device_unref(dev);