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 DeviceClass, InterfaceClass
243 * and InterfaceSubClass
244 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
245 * use the SCSI vendor and model as USB-Vendor and USB-model.
246 * 3.) Otherwise use the USB manufacturer and product as
247 * USB-Vendor and USB-model. Any non-printable characters
248 * in those strings will be skipped; a slash '/' will be converted
249 * into a full stop '.'.
250 * 4.) If that fails, too, we will use idVendor and idProduct
251 * as USB-Vendor and USB-model.
252 * 5.) The USB identification is the USB-vendor and USB-model
253 * string concatenated with an underscore '_'.
254 * 6.) If the device supplies a serial number, this number
255 * is concatenated with the identification with an underscore '_'.
257 static int usb_id(struct udev_device *dev)
259 struct udev *udev = udev_device_get_udev(dev);
260 struct udev_device *dev_interface;
261 struct udev_device *dev_usb;
262 const char *if_class, *if_subclass;
266 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
268 /* usb interface directory */
269 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
270 if (dev_interface == NULL) {
271 info(udev, "unable to access usb_interface device of '%s'\n",
272 udev_device_get_syspath(dev));
276 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
278 info(udev, "%s: cannot get bInterfaceClass attribute\n",
279 udev_device_get_sysname(dev));
283 if_class_num = strtoul(if_class, NULL, 16);
284 if (if_class_num == 8) {
286 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
287 if (if_subclass != NULL)
288 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
290 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
293 info(udev, "%s: if_class %d protocol %d\n",
294 udev_device_get_syspath(dev_interface), if_class_num, protocol);
296 /* usb device directory */
297 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
299 info(udev, "unable to find parent 'usb' device of '%s'\n",
300 udev_device_get_syspath(dev));
304 /* all interfaces of the device in a single string */
305 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
307 /* mass storage : SCSI or ATAPI */
308 if ((protocol == 6 || protocol == 2) && !use_usb_info) {
309 struct udev_device *dev_scsi;
310 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
311 int host, bus, target, lun;
313 /* get scsi device */
314 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
315 if (dev_scsi == NULL) {
316 info(udev, "unable to find parent 'scsi' device of '%s'\n",
317 udev_device_get_syspath(dev));
320 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
321 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
325 /* Generic SPC-2 device */
326 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
328 info(udev, "%s: cannot get SCSI vendor attribute\n",
329 udev_device_get_sysname(dev_scsi));
332 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
333 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
334 udev_util_replace_chars(vendor_str, NULL);
336 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
338 info(udev, "%s: cannot get SCSI model attribute\n",
339 udev_device_get_sysname(dev_scsi));
342 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
343 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
344 udev_util_replace_chars(model_str, NULL);
346 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
348 info(udev, "%s: cannot get SCSI type attribute\n",
349 udev_device_get_sysname(dev_scsi));
352 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
354 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
356 info(udev, "%s: cannot get SCSI revision attribute\n",
357 udev_device_get_sysname(dev_scsi));
360 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
361 udev_util_replace_chars(revision_str, NULL);
364 * some broken devices have the same identifiers
365 * for all luns, export the target:lun number
367 sprintf(instance_str, "%d:%d", target, lun);
371 /* fallback to USB vendor & device */
372 if (vendor_str[0] == '\0') {
373 const char *usb_vendor = NULL;
376 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
379 usb_vendor = udev_device_get_sysattr_value(dev_usb, "idVendor");
382 info(udev, "No USB vendor information available\n");
385 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
386 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
387 udev_util_replace_chars(vendor_str, NULL);
390 if (model_str[0] == '\0') {
391 const char *usb_model = NULL;
394 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
397 usb_model = udev_device_get_sysattr_value(dev_usb, "idProduct");
400 dbg(udev, "No USB model information available\n");
403 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
404 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
405 udev_util_replace_chars(model_str, NULL);
408 if (revision_str[0] == '\0') {
411 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
413 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
414 udev_util_replace_chars(revision_str, NULL);
418 if (serial_str[0] == '\0') {
419 const char *usb_serial;
421 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
423 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
424 udev_util_replace_chars(serial_str, NULL);
430 int main(int argc, char **argv)
432 static const struct option options[] = {
433 { "usb-info", no_argument, NULL, 'u' },
434 { "num-info", no_argument, NULL, 'n' },
435 { "export", no_argument, NULL, 'x' },
436 { "debug", no_argument, NULL, 'd' },
437 { "help", no_argument, NULL, 'h' },
441 struct udev_device *dev = NULL;
442 char syspath[UTIL_PATH_SIZE];
451 logging_init("usb_id");
452 udev_set_log_fn(udev, log_fn);
457 option = getopt_long(argc, argv, "dnuxh", options, NULL);
464 if (udev_get_log_priority(udev) < LOG_INFO)
465 udev_set_log_priority(udev, LOG_INFO);
478 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
479 " --usb-info use usb strings instead\n"
480 " --num-info use numerical values\n"
481 " --export print values as environment keys\n"
482 " --help print this help text\n\n");
489 devpath = argv[optind];
490 if (devpath == NULL) {
491 fprintf(stderr, "No device specified\n");
496 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
497 util_strlcat(syspath, devpath, sizeof(syspath));
498 dev = udev_device_new_from_syspath(udev, syspath);
500 err(udev, "unable to access '%s'\n", devpath);
504 retval = usb_id(dev);
508 util_strlcpy(serial, vendor_str, sizeof(serial));
509 util_strlcat(serial, "_", sizeof(serial));
510 util_strlcat(serial, model_str, sizeof(serial));
511 if (serial_str[0] != '\0') {
512 util_strlcat(serial, "_", sizeof(serial));
513 util_strlcat(serial, serial_str, sizeof(serial));
515 if (instance_str[0] != '\0') {
516 util_strlcat(serial, "-", sizeof(serial));
517 util_strlcat(serial, instance_str, sizeof(serial));
521 printf("ID_VENDOR=%s\n", vendor_str);
522 printf("ID_VENDOR_ENC=%s\n", vendor_str_enc);
523 printf("ID_MODEL=%s\n", model_str);
524 printf("ID_MODEL_ENC=%s\n", model_str_enc);
525 printf("ID_REVISION=%s\n", revision_str);
526 printf("ID_SERIAL=%s\n", serial);
527 if (serial_str[0] != '\0')
528 printf("ID_SERIAL_SHORT=%s\n", serial_str);
529 printf("ID_TYPE=%s\n", type_str);
530 if (instance_str[0] != '\0')
531 printf("ID_INSTANCE=%s\n", instance_str);
532 printf("ID_BUS=usb\n");
533 if (packed_if_str[0] != '\0')
534 printf("ID_USB_INTERFACES=:%s\n", packed_if_str);
536 printf("%s\n", serial);
540 udev_device_unref(dev);