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 model_str[64];
42 static char model_str_enc[256];
43 static char vendor_str_enc[256];
44 static char serial_str[UTIL_NAME_SIZE];
45 static char packed_if_str[UTIL_NAME_SIZE];
46 static char revision_str[64];
47 static char type_str[64];
48 static char instance_str[64];
50 static int use_usb_info;
51 static int use_num_info;
53 static void set_usb_iftype(char *to, int if_class_num, size_t len)
55 char *type = "generic";
57 switch (if_class_num) {
61 case 2: /* CDC-Control */
66 case 5: /* Physical */
80 case 0x0a: /* CDC-Data */
82 case 0x0b: /* Chip/Smart Card */
84 case 0x0d: /* Content Security */
89 case 0xdc: /* Diagnostic Device */
91 case 0xe0: /* Wireless Controller */
93 case 0xfe: /* Application-specific */
95 case 0xff: /* Vendor-specific */
100 strncpy(to, type, len);
104 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
108 char *type = "generic";
110 type_num = strtoul(from, &eptr, 0);
120 case 5: /* SFF-8070i */
123 case 1: /* RBC devices */
126 case 6: /* Transparent SPC-2 devices */
133 util_strlcpy(to, type, len);
137 static void set_scsi_type(char *to, const char *from, size_t len)
141 char *type = "generic";
143 type_num = strtoul(from, &eptr, 0);
165 util_strlcpy(to, type, len);
168 #define USB_DT_DEVICE 0x01
169 #define USB_DT_INTERFACE 0x04
171 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len)
173 char *filename = NULL;
176 unsigned char buf[18 + 65535];
177 unsigned int pos, strpos;
178 struct usb_interface_descriptor {
180 u_int8_t bDescriptorType;
181 u_int8_t bInterfaceNumber;
182 u_int8_t bAlternateSetting;
183 u_int8_t bNumEndpoints;
184 u_int8_t bInterfaceClass;
185 u_int8_t bInterfaceSubClass;
186 u_int8_t bInterfaceProtocol;
188 } __attribute__((packed));
191 if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0) {
195 fd = open(filename, O_RDONLY);
197 fprintf(stderr, "error opening USB device 'descriptors' file\n");
201 size = read(fd, buf, sizeof(buf));
203 if (size < 18 || size == sizeof(buf)) {
210 while (pos < sizeof(buf) && strpos+7 < len) {
211 struct usb_interface_descriptor *desc;
214 desc = (struct usb_interface_descriptor *) &buf[pos];
215 if (desc->bLength < 3)
217 pos += desc->bLength;
219 if (desc->bDescriptorType != USB_DT_INTERFACE)
222 if (snprintf(if_str, 8, "%02x%02x%02x:",
223 desc->bInterfaceClass,
224 desc->bInterfaceSubClass,
225 desc->bInterfaceProtocol) != 7)
228 if (strstr(ifs_str, if_str) != NULL)
231 memcpy(&ifs_str[strpos], if_str, 8),
240 * A unique USB identification is generated like this:
242 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
243 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
244 * use the SCSI vendor and model as USB-Vendor and USB-model.
245 * 3.) Otherwise use the USB manufacturer and product as
246 * USB-Vendor and USB-model. Any non-printable characters
247 * in those strings will be skipped; a slash '/' will be converted
248 * into a full stop '.'.
249 * 4.) If that fails, too, we will use idVendor and idProduct
250 * as USB-Vendor and USB-model.
251 * 5.) The USB identification is the USB-vendor and USB-model
252 * string concatenated with an underscore '_'.
253 * 6.) If the device supplies a serial number, this number
254 * is concatenated with the identification with an underscore '_'.
256 static int usb_id(struct udev_device *dev)
258 struct udev *udev = udev_device_get_udev(dev);
259 struct udev_device *dev_interface = NULL;
260 struct udev_device *dev_usb = NULL;
261 const char *if_class, *if_subclass;
265 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
267 /* shortcut, if we are called directly for a "usb_device" type */
268 if (udev_device_get_devtype(dev) != NULL && strcmp(udev_device_get_devtype(dev), "usb_device") == 0) {
269 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
274 /* usb interface directory */
275 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
276 if (dev_interface == NULL) {
277 info(udev, "unable to access usb_interface device of '%s'\n",
278 udev_device_get_syspath(dev));
282 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
284 info(udev, "%s: cannot get bInterfaceClass attribute\n",
285 udev_device_get_sysname(dev));
289 if_class_num = strtoul(if_class, NULL, 16);
290 if (if_class_num == 8) {
292 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
293 if (if_subclass != NULL)
294 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
296 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
299 info(udev, "%s: if_class %d protocol %d\n",
300 udev_device_get_syspath(dev_interface), if_class_num, protocol);
302 /* usb device directory */
303 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
305 info(udev, "unable to find parent 'usb' device of '%s'\n",
306 udev_device_get_syspath(dev));
310 /* all interfaces of the device in a single string */
311 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
313 /* mass storage : SCSI or ATAPI */
314 if ((protocol == 6 || protocol == 2) && !use_usb_info) {
315 struct udev_device *dev_scsi;
316 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
317 int host, bus, target, lun;
319 /* get scsi device */
320 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
321 if (dev_scsi == NULL) {
322 info(udev, "unable to find parent 'scsi' device of '%s'\n",
323 udev_device_get_syspath(dev));
326 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
327 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
331 /* Generic SPC-2 device */
332 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
334 info(udev, "%s: cannot get SCSI vendor attribute\n",
335 udev_device_get_sysname(dev_scsi));
338 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
339 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
340 udev_util_replace_chars(vendor_str, NULL);
342 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
344 info(udev, "%s: cannot get SCSI model attribute\n",
345 udev_device_get_sysname(dev_scsi));
348 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
349 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
350 udev_util_replace_chars(model_str, NULL);
352 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
354 info(udev, "%s: cannot get SCSI type attribute\n",
355 udev_device_get_sysname(dev_scsi));
358 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
360 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
362 info(udev, "%s: cannot get SCSI revision attribute\n",
363 udev_device_get_sysname(dev_scsi));
366 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
367 udev_util_replace_chars(revision_str, NULL);
370 * some broken devices have the same identifiers
371 * for all luns, export the target:lun number
373 sprintf(instance_str, "%d:%d", target, lun);
377 /* fallback to USB vendor & device */
378 if (vendor_str[0] == '\0') {
379 const char *usb_vendor = NULL;
382 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
385 usb_vendor = udev_device_get_sysattr_value(dev_usb, "idVendor");
388 info(udev, "No USB vendor information available\n");
391 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
392 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
393 udev_util_replace_chars(vendor_str, NULL);
396 if (model_str[0] == '\0') {
397 const char *usb_model = NULL;
400 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
403 usb_model = udev_device_get_sysattr_value(dev_usb, "idProduct");
406 dbg(udev, "No USB model information available\n");
409 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
410 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
411 udev_util_replace_chars(model_str, NULL);
414 if (revision_str[0] == '\0') {
417 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
419 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
420 udev_util_replace_chars(revision_str, NULL);
424 if (serial_str[0] == '\0') {
425 const char *usb_serial;
427 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
429 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
430 udev_util_replace_chars(serial_str, NULL);
436 int main(int argc, char **argv)
438 static const struct option options[] = {
439 { "usb-info", no_argument, NULL, 'u' },
440 { "num-info", no_argument, NULL, 'n' },
441 { "export", no_argument, NULL, 'x' },
442 { "debug", no_argument, NULL, 'd' },
443 { "help", no_argument, NULL, 'h' },
447 struct udev_device *dev = NULL;
448 char syspath[UTIL_PATH_SIZE];
457 logging_init("usb_id");
458 udev_set_log_fn(udev, log_fn);
463 option = getopt_long(argc, argv, "dnuxh", options, NULL);
470 if (udev_get_log_priority(udev) < LOG_INFO)
471 udev_set_log_priority(udev, LOG_INFO);
484 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
485 " --usb-info use usb strings instead\n"
486 " --num-info use numerical values\n"
487 " --export print values as environment keys\n"
488 " --help print this help text\n\n");
495 devpath = argv[optind];
496 if (devpath == NULL) {
497 fprintf(stderr, "No device specified\n");
502 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
503 util_strlcat(syspath, devpath, sizeof(syspath));
504 dev = udev_device_new_from_syspath(udev, syspath);
506 err(udev, "unable to access '%s'\n", devpath);
510 retval = usb_id(dev);
514 util_strlcpy(serial, vendor_str, sizeof(serial));
515 util_strlcat(serial, "_", sizeof(serial));
516 util_strlcat(serial, model_str, sizeof(serial));
517 if (serial_str[0] != '\0') {
518 util_strlcat(serial, "_", sizeof(serial));
519 util_strlcat(serial, serial_str, sizeof(serial));
521 if (instance_str[0] != '\0') {
522 util_strlcat(serial, "-", sizeof(serial));
523 util_strlcat(serial, instance_str, sizeof(serial));
527 printf("ID_VENDOR=%s\n", vendor_str);
528 printf("ID_VENDOR_ENC=%s\n", vendor_str_enc);
529 printf("ID_MODEL=%s\n", model_str);
530 printf("ID_MODEL_ENC=%s\n", model_str_enc);
531 printf("ID_REVISION=%s\n", revision_str);
532 printf("ID_SERIAL=%s\n", serial);
533 if (serial_str[0] != '\0')
534 printf("ID_SERIAL_SHORT=%s\n", serial_str);
535 if (type_str[0] != '\0')
536 printf("ID_TYPE=%s\n", type_str);
537 if (instance_str[0] != '\0')
538 printf("ID_INSTANCE=%s\n", instance_str);
539 printf("ID_BUS=usb\n");
540 if (packed_if_str[0] != '\0')
541 printf("ID_USB_INTERFACES=:%s\n", packed_if_str);
543 printf("%s\n", serial);
547 udev_device_unref(dev);