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.
24 #include "../../udev/udev.h"
28 static void log_fn(struct udev *udev, int priority,
29 const char *file, int line, const char *fn,
30 const char *format, va_list args)
33 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
34 vfprintf(stderr, format, args);
36 vsyslog(priority, format, args);
40 static char vendor_str[64];
41 static char vendor_str_enc[256];
42 static const char *vendor_id = "";
43 static char model_str[64];
44 static char model_str_enc[256];
45 static const char *product_id = "";
46 static char serial_str[UTIL_NAME_SIZE];
47 static char packed_if_str[UTIL_NAME_SIZE];
48 static char revision_str[64];
49 static char type_str[64];
50 static char instance_str[64];
51 static const char *ifnum;
52 static const char *driver;
54 static int use_usb_info;
55 static int use_num_info;
57 static void set_usb_iftype(char *to, int if_class_num, size_t len)
59 char *type = "generic";
61 switch (if_class_num) {
65 case 2: /* CDC-Control */
70 case 5: /* Physical */
84 case 0x0a: /* CDC-Data */
86 case 0x0b: /* Chip/Smart Card */
88 case 0x0d: /* Content Security */
93 case 0xdc: /* Diagnostic Device */
95 case 0xe0: /* Wireless Controller */
97 case 0xfe: /* Application-specific */
99 case 0xff: /* Vendor-specific */
104 strncpy(to, type, len);
108 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
112 char *type = "generic";
114 type_num = strtoul(from, &eptr, 0);
124 case 5: /* SFF-8070i */
127 case 1: /* RBC devices */
130 case 6: /* Transparent SPC-2 devices */
137 util_strlcpy(to, type, len);
141 static void set_scsi_type(char *to, const char *from, size_t len)
145 char *type = "generic";
147 type_num = strtoul(from, &eptr, 0);
169 util_strlcpy(to, type, len);
172 #define USB_DT_DEVICE 0x01
173 #define USB_DT_INTERFACE 0x04
175 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len)
177 char *filename = NULL;
180 unsigned char buf[18 + 65535];
181 unsigned int pos, strpos;
182 struct usb_interface_descriptor {
184 u_int8_t bDescriptorType;
185 u_int8_t bInterfaceNumber;
186 u_int8_t bAlternateSetting;
187 u_int8_t bNumEndpoints;
188 u_int8_t bInterfaceClass;
189 u_int8_t bInterfaceSubClass;
190 u_int8_t bInterfaceProtocol;
192 } __attribute__((packed));
195 if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0) {
199 fd = open(filename, O_RDONLY);
201 fprintf(stderr, "error opening USB device 'descriptors' file\n");
205 size = read(fd, buf, sizeof(buf));
207 if (size < 18 || size == sizeof(buf)) {
214 while (pos < sizeof(buf) && strpos+7 < len) {
215 struct usb_interface_descriptor *desc;
218 desc = (struct usb_interface_descriptor *) &buf[pos];
219 if (desc->bLength < 3)
221 pos += desc->bLength;
223 if (desc->bDescriptorType != USB_DT_INTERFACE)
226 if (snprintf(if_str, 8, "%02x%02x%02x:",
227 desc->bInterfaceClass,
228 desc->bInterfaceSubClass,
229 desc->bInterfaceProtocol) != 7)
232 if (strstr(ifs_str, if_str) != NULL)
235 memcpy(&ifs_str[strpos], if_str, 8),
244 * A unique USB identification is generated like this:
246 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
247 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
248 * use the SCSI vendor and model as USB-Vendor and USB-model.
249 * 3.) Otherwise use the USB manufacturer and product as
250 * USB-Vendor and USB-model. Any non-printable characters
251 * in those strings will be skipped; a slash '/' will be converted
252 * into a full stop '.'.
253 * 4.) If that fails, too, we will use idVendor and idProduct
254 * as USB-Vendor and USB-model.
255 * 5.) The USB identification is the USB-vendor and USB-model
256 * string concatenated with an underscore '_'.
257 * 6.) If the device supplies a serial number, this number
258 * is concatenated with the identification with an underscore '_'.
260 static int usb_id(struct udev_device *dev)
262 struct udev *udev = udev_device_get_udev(dev);
263 struct udev_device *dev_interface = NULL;
264 struct udev_device *dev_usb = NULL;
265 const char *if_class, *if_subclass;
269 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
271 /* shortcut, if we are called directly for a "usb_device" type */
272 if (udev_device_get_devtype(dev) != NULL && strcmp(udev_device_get_devtype(dev), "usb_device") == 0) {
273 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
278 /* usb interface directory */
279 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
280 if (dev_interface == NULL) {
281 info(udev, "unable to access usb_interface device of '%s'\n",
282 udev_device_get_syspath(dev));
286 ifnum = udev_device_get_sysattr_value(dev_interface, "bInterfaceNumber");
287 driver = udev_device_get_sysattr_value(dev_interface, "driver");
289 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
291 info(udev, "%s: cannot get bInterfaceClass attribute\n",
292 udev_device_get_sysname(dev));
296 if_class_num = strtoul(if_class, NULL, 16);
297 if (if_class_num == 8) {
299 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
300 if (if_subclass != NULL)
301 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
303 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
306 info(udev, "%s: if_class %d protocol %d\n",
307 udev_device_get_syspath(dev_interface), if_class_num, protocol);
309 /* usb device directory */
310 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
312 info(udev, "unable to find parent 'usb' device of '%s'\n",
313 udev_device_get_syspath(dev));
317 /* all interfaces of the device in a single string */
318 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
320 /* mass storage : SCSI or ATAPI */
321 if ((protocol == 6 || protocol == 2) && !use_usb_info) {
322 struct udev_device *dev_scsi;
323 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
324 int host, bus, target, lun;
326 /* get scsi device */
327 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
328 if (dev_scsi == NULL) {
329 info(udev, "unable to find parent 'scsi' device of '%s'\n",
330 udev_device_get_syspath(dev));
333 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
334 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
338 /* Generic SPC-2 device */
339 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
341 info(udev, "%s: cannot get SCSI vendor attribute\n",
342 udev_device_get_sysname(dev_scsi));
345 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
346 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
347 udev_util_replace_chars(vendor_str, NULL);
349 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
351 info(udev, "%s: cannot get SCSI model attribute\n",
352 udev_device_get_sysname(dev_scsi));
355 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
356 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
357 udev_util_replace_chars(model_str, NULL);
359 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
361 info(udev, "%s: cannot get SCSI type attribute\n",
362 udev_device_get_sysname(dev_scsi));
365 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
367 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
369 info(udev, "%s: cannot get SCSI revision attribute\n",
370 udev_device_get_sysname(dev_scsi));
373 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
374 udev_util_replace_chars(revision_str, NULL);
377 * some broken devices have the same identifiers
378 * for all luns, export the target:lun number
380 sprintf(instance_str, "%d:%d", target, lun);
384 vendor_id = udev_device_get_sysattr_value(dev_usb, "idVendor");
385 product_id = udev_device_get_sysattr_value(dev_usb, "idProduct");
387 /* fallback to USB vendor & device */
388 if (vendor_str[0] == '\0') {
389 const char *usb_vendor = NULL;
392 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
395 usb_vendor = vendor_id;
398 info(udev, "No USB vendor information available\n");
401 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
402 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
403 udev_util_replace_chars(vendor_str, NULL);
406 if (model_str[0] == '\0') {
407 const char *usb_model = NULL;
410 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
413 usb_model = product_id;
416 dbg(udev, "No USB model information available\n");
419 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
420 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
421 udev_util_replace_chars(model_str, NULL);
424 if (revision_str[0] == '\0') {
427 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
429 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
430 udev_util_replace_chars(revision_str, NULL);
434 if (serial_str[0] == '\0') {
435 const char *usb_serial;
437 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
439 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
440 udev_util_replace_chars(serial_str, NULL);
446 int main(int argc, char **argv)
448 static const struct option options[] = {
449 { "usb-info", no_argument, NULL, 'u' },
450 { "num-info", no_argument, NULL, 'n' },
451 { "export", no_argument, NULL, 'x' },
452 { "debug", no_argument, NULL, 'd' },
453 { "help", no_argument, NULL, 'h' },
457 struct udev_device *dev = NULL;
458 char syspath[UTIL_PATH_SIZE];
467 logging_init("usb_id");
468 udev_set_log_fn(udev, log_fn);
473 option = getopt_long(argc, argv, "dnuxh", options, NULL);
480 if (udev_get_log_priority(udev) < LOG_INFO)
481 udev_set_log_priority(udev, LOG_INFO);
494 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
495 " --usb-info use usb strings instead\n"
496 " --num-info use numerical values\n"
497 " --export print values as environment keys\n"
498 " --help print this help text\n\n");
505 devpath = argv[optind];
506 if (devpath == NULL) {
507 fprintf(stderr, "No device specified\n");
512 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
513 util_strlcat(syspath, devpath, sizeof(syspath));
514 dev = udev_device_new_from_syspath(udev, syspath);
516 err(udev, "unable to access '%s'\n", devpath);
520 retval = usb_id(dev);
524 util_strlcpy(serial, vendor_str, sizeof(serial));
525 util_strlcat(serial, "_", sizeof(serial));
526 util_strlcat(serial, model_str, sizeof(serial));
527 if (serial_str[0] != '\0') {
528 util_strlcat(serial, "_", sizeof(serial));
529 util_strlcat(serial, serial_str, sizeof(serial));
531 if (instance_str[0] != '\0') {
532 util_strlcat(serial, "-", sizeof(serial));
533 util_strlcat(serial, instance_str, sizeof(serial));
537 printf("ID_VENDOR=%s\n", vendor_str);
538 printf("ID_VENDOR_ENC=%s\n", vendor_str_enc);
539 printf("ID_VENDOR_ID=%s\n", vendor_id);
540 printf("ID_MODEL=%s\n", model_str);
541 printf("ID_MODEL_ENC=%s\n", model_str_enc);
542 printf("ID_MODEL_ID=%s\n", product_id);
543 printf("ID_REVISION=%s\n", revision_str);
544 printf("ID_SERIAL=%s\n", serial);
545 if (serial_str[0] != '\0')
546 printf("ID_SERIAL_SHORT=%s\n", serial_str);
547 if (type_str[0] != '\0')
548 printf("ID_TYPE=%s\n", type_str);
549 if (instance_str[0] != '\0')
550 printf("ID_INSTANCE=%s\n", instance_str);
551 printf("ID_BUS=usb\n");
552 if (packed_if_str[0] != '\0')
553 printf("ID_USB_INTERFACES=:%s\n", packed_if_str);
555 printf("ID_USB_INTERFACE_NUM=%s\n", ifnum);
557 printf("ID_USB_DRIVER=%s\n", driver);
559 printf("%s\n", serial);
563 udev_device_unref(dev);