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 "../../udev/udev.h"
27 #define MAX_PATH_LEN 512
28 #define MAX_SERIAL_LEN 256
29 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
31 static void log_fn(struct udev *udev, int priority,
32 const char *file, int line, const char *fn,
33 const char *format, va_list args)
35 vsyslog(priority, format, args);
38 static char vendor_str[64];
39 static char model_str[64];
40 static char serial_str[MAX_SERIAL_LEN];
41 static char revision_str[64];
42 static char type_str[64];
43 static char instance_str[64];
45 static int use_usb_info;
46 static int use_num_info;
48 static void set_str(char *to, const char *from, size_t count)
52 /* strip trailing whitespace */
53 len = strnlen(from, count);
54 while (len && isspace(from[len-1]))
57 /* strip leading whitespace */
59 while (isspace(from[i]) && (i < len))
64 /* substitute multiple whitespace */
65 if (isspace(from[i])) {
66 while (isspace(from[i]))
70 /* Replace '/' with '.' */
76 /* skip non-printable chars */
77 if (!isalnum(from[i]) && !ispunct(from[i])) {
86 static void set_usb_iftype(char *to, int if_class_num, size_t len)
88 char *type = "generic";
90 switch (if_class_num) {
103 case 2: /* CDC-Control */
104 case 5: /* Physical */
107 case 0x0a: /* CDC-Data */
108 case 0x0b: /* Chip/Smart Card */
109 case 0x0d: /* Content Security */
110 case 0x0e: /* Video */
111 case 0xdc: /* Diagnostic Device */
112 case 0xe0: /* Wireless Controller */
113 case 0xf2: /* Application-specific */
114 case 0xff: /* Vendor-specific */
119 strncpy(to, type, len);
123 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
127 char *type = "generic";
129 type_num = strtoul(from, &eptr, 0);
139 case 5: /* SFF-8070i */
142 case 1: /* RBC devices */
143 case 6: /* Transparent SPC-2 devices */
150 strlcpy(to, type, len);
155 static void set_scsi_type(char *to, const char *from, size_t len)
159 char *type = "generic";
161 type_num = strtoul(from, &eptr, 0);
183 strlcpy(to, type, len);
187 * A unique USB identification is generated like this:
189 * 1.) Get the USB device type from DeviceClass, InterfaceClass
190 * and InterfaceSubClass
191 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
192 * use the SCSI vendor and model as USB-Vendor and USB-model.
193 * 3.) Otherwise use the USB manufacturer and product as
194 * USB-Vendor and USB-model. Any non-printable characters
195 * in those strings will be skipped; a slash '/' will be converted
196 * into a full stop '.'.
197 * 4.) If that fails, too, we will use idVendor and idProduct
198 * as USB-Vendor and USB-model.
199 * 5.) The USB identification is the USB-vendor and USB-model
200 * string concatenated with an underscore '_'.
201 * 6.) If the device supplies a serial number, this number
202 * is concatenated with the identification with an underscore '_'.
204 static int usb_id(struct udev *udev, const char *devpath)
206 struct sysfs_device *dev;
207 struct sysfs_device *dev_interface;
208 struct sysfs_device *dev_usb;
209 const char *if_class, *if_subclass;
213 dbg("devpath %s\n", devpath);
215 /* get all usb specific information: dev_interface, if_class, dev_usb */
216 dev = sysfs_device_get(udev, devpath);
218 err(udev, "unable to access '%s'\n", devpath);
222 /* usb interface directory */
223 dev_interface = sysfs_device_get_parent_with_subsystem(udev, dev, "usb");
224 if (dev_interface == NULL) {
225 info(udev, "unable to access usb_interface device of '%s'\n", devpath);
229 if_class = sysfs_attr_get_value(udev, dev_interface->devpath, "bInterfaceClass");
231 info(udev, "%s: cannot get bInterfaceClass attribute\n", dev_interface->kernel);
234 if_class_num = strtoul(if_class, NULL, 16);
235 if (if_class_num == 8) {
236 if_subclass = sysfs_attr_get_value(udev, dev_interface->devpath, "bInterfaceSubClass");
237 if (if_subclass != NULL)
238 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
240 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
242 info(udev, "%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
244 /* usb device directory */
245 dev_usb = sysfs_device_get_parent_with_subsystem(udev, dev_interface, "usb");
247 info(udev, "unable to find parent 'usb' device of '%s'\n", devpath);
252 if (protocol == 6 && !use_usb_info) {
253 struct sysfs_device *dev_scsi;
254 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
255 int host, bus, target, lun;
257 /* get scsi device */
258 dev_scsi = sysfs_device_get_parent_with_subsystem(udev, dev, "scsi");
259 if (dev_scsi == NULL) {
260 info(udev, "unable to find parent 'scsi' device of '%s'\n", devpath);
263 if (sscanf(dev_scsi->kernel, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
264 info(udev, "invalid scsi device '%s'\n", dev_scsi->kernel);
268 /* Generic SPC-2 device */
269 scsi_vendor = sysfs_attr_get_value(udev, dev_scsi->devpath, "vendor");
271 info(udev, "%s: cannot get SCSI vendor attribute\n", dev_scsi->kernel);
274 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
276 scsi_model = sysfs_attr_get_value(udev, dev_scsi->devpath, "model");
278 info(udev, "%s: cannot get SCSI model attribute\n", dev_scsi->kernel);
281 set_str(model_str, scsi_model, sizeof(model_str)-1);
283 scsi_type = sysfs_attr_get_value(udev, dev_scsi->devpath, "type");
285 info(udev, "%s: cannot get SCSI type attribute\n", dev_scsi->kernel);
288 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
290 scsi_rev = sysfs_attr_get_value(udev, dev_scsi->devpath, "rev");
292 info(udev, "%s: cannot get SCSI revision attribute\n", dev_scsi->kernel);
295 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
298 * some broken devices have the same identifiers
299 * for all luns, export the target:lun number
301 sprintf(instance_str, "%d:%d", target, lun);
305 /* fallback to USB vendor & device */
306 if (vendor_str[0] == '\0') {
307 const char *usb_vendor = NULL;
310 usb_vendor = sysfs_attr_get_value(udev, dev_usb->devpath, "manufacturer");
313 usb_vendor = sysfs_attr_get_value(udev, dev_usb->devpath, "idVendor");
316 info(udev, "No USB vendor information available\n");
319 set_str(vendor_str, usb_vendor, sizeof(vendor_str)-1);
322 if (model_str[0] == '\0') {
323 const char *usb_model = NULL;
326 usb_model = sysfs_attr_get_value(udev, dev_usb->devpath, "product");
329 usb_model = sysfs_attr_get_value(udev, dev_usb->devpath, "idProduct");
332 dbg("No USB model information available\n");
335 set_str(model_str, usb_model, sizeof(model_str)-1);
338 if (revision_str[0] == '\0') {
341 usb_rev = sysfs_attr_get_value(udev, dev_usb->devpath, "bcdDevice");
343 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
346 if (serial_str[0] == '\0') {
347 const char *usb_serial;
349 usb_serial = sysfs_attr_get_value(udev, dev_usb->devpath, "serial");
351 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
356 int main(int argc, char **argv)
361 char devpath[MAX_PATH_LEN];
363 static const struct option options[] = {
364 { "usb-info", 0, NULL, 'u' },
365 { "num-info", 0, NULL, 'n' },
366 { "export", 0, NULL, 'x' },
367 { "help", 0, NULL, 'h' },
375 logging_init("usb_id");
376 udev_set_log_fn(udev, log_fn);
382 option = getopt_long(argc, argv, "nuxh", options, NULL);
398 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
399 " --usb-info use usb strings instead\n"
400 " --num-info use numerical values\n"
401 " --export print values as environemt keys\n"
402 " --help print this help text\n\n");
409 env = getenv("DEVPATH");
411 strlcpy(devpath, env, sizeof(devpath));
413 if (argv[optind] == NULL) {
414 fprintf(stderr, "No device specified\n");
418 strlcpy(devpath, argv[optind], sizeof(devpath));
421 retval = usb_id(udev, devpath);
426 strlcpy(serial, vendor_str, sizeof(serial));
427 strlcat(serial, "_", sizeof(serial));
428 strlcat(serial, model_str, sizeof(serial));
429 if (serial_str[0] != '\0') {
430 strlcat(serial, "_", sizeof(serial));
431 strlcat(serial, serial_str, sizeof(serial));
433 if (instance_str[0] != '\0') {
434 strlcat(serial, "-", sizeof(serial));
435 strlcat(serial, instance_str, sizeof(serial));
439 printf("ID_VENDOR=%s\n", vendor_str);
440 printf("ID_MODEL=%s\n", model_str);
441 printf("ID_REVISION=%s\n", revision_str);
442 printf("ID_SERIAL=%s\n", serial);
443 if (serial_str[0] != '\0')
444 printf("ID_SERIAL_SHORT=%s\n", serial_str);
445 printf("ID_TYPE=%s\n", type_str);
446 if (instance_str[0] != '\0')
447 printf("ID_INSTANCE=%s\n", instance_str);
448 printf("ID_BUS=usb\n");
450 printf("%s\n", serial);