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.
23 #include "../../udev.h"
25 #define MAX_PATH_LEN 512
26 #define MAX_SERIAL_LEN 256
27 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
30 void log_message(int priority, const char *format, ...)
33 static int udev_log = -1;
38 value = getenv("UDEV_LOG");
40 udev_log = log_priority(value);
45 if (priority > udev_log)
48 va_start(args, format);
49 vsyslog(priority, format, args);
54 static char vendor_str[64];
55 static char model_str[64];
56 static char serial_str[MAX_SERIAL_LEN];
57 static char revision_str[64];
58 static char type_str[64];
59 static char instance_str[64];
61 static int use_usb_info;
62 static int use_num_info;
64 static void set_str(char *to, const char *from, size_t count)
68 /* strip trailing whitespace */
69 len = strnlen(from, count);
70 while (len && isspace(from[len-1]))
73 /* strip leading whitespace */
75 while (isspace(from[i]) && (i < len))
80 /* substitute multiple whitespace */
81 if (isspace(from[i])) {
82 while (isspace(from[i]))
86 /* Replace '/' with '.' */
92 /* skip non-printable chars */
93 if (!isalnum(from[i]) && !ispunct(from[i])) {
102 static void set_usb_iftype(char *to, int if_class_num, size_t len)
104 char *type = "generic";
106 switch (if_class_num) {
119 case 2: /* CDC-Control */
120 case 5: /* Physical */
123 case 0x0a: /* CDC-Data */
124 case 0x0b: /* Chip/Smart Card */
125 case 0x0d: /* Content Security */
126 case 0x0e: /* Video */
127 case 0xdc: /* Diagnostic Device */
128 case 0xe0: /* Wireless Controller */
129 case 0xf2: /* Application-specific */
130 case 0xff: /* Vendor-specific */
135 strncpy(to, type, len);
139 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
143 char *type = "generic";
145 type_num = strtoul(from, &eptr, 0);
155 case 5: /* SFF-8070i */
158 case 1: /* RBC devices */
159 case 6: /* Transparent SPC-2 devices */
166 strlcpy(to, type, len);
171 static void set_scsi_type(char *to, const char *from, size_t len)
175 char *type = "generic";
177 type_num = strtoul(from, &eptr, 0);
199 strlcpy(to, type, len);
203 * A unique USB identification is generated like this:
205 * 1.) Get the USB device type from DeviceClass, InterfaceClass
206 * and InterfaceSubClass
207 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
208 * use the SCSI vendor and model as USB-Vendor and USB-model.
209 * 3.) Otherwise use the USB manufacturer and product as
210 * USB-Vendor and USB-model. Any non-printable characters
211 * in those strings will be skipped; a slash '/' will be converted
212 * into a full stop '.'.
213 * 4.) If that fails, too, we will use idVendor and idProduct
214 * as USB-Vendor and USB-model.
215 * 5.) The USB identification is the USB-vendor and USB-model
216 * string concatenated with an underscore '_'.
217 * 6.) If the device supplies a serial number, this number
218 * is concatenated with the identification with an underscore '_'.
220 static int usb_id(const char *devpath)
222 struct sysfs_device *dev;
223 struct sysfs_device *dev_interface;
224 struct sysfs_device *dev_usb;
225 const char *if_class, *if_subclass;
229 dbg("devpath %s\n", devpath);
231 /* get all usb specific information: dev_interface, if_class, dev_usb */
232 dev = sysfs_device_get(devpath);
234 err("unable to access '%s'\n", devpath);
238 /* usb interface directory */
239 dev_interface = sysfs_device_get_parent_with_subsystem(dev, "usb");
240 if (dev_interface == NULL) {
241 info("unable to access usb_interface device of '%s'\n", devpath);
245 if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
247 info("%s: cannot get bInterfaceClass attribute\n", dev_interface->kernel);
250 if_class_num = strtoul(if_class, NULL, 16);
251 if (if_class_num == 8) {
252 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
253 if (if_subclass != NULL)
254 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
256 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
258 info("%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
260 /* usb device directory */
261 dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
263 info("unable to find parent 'usb' device of '%s'\n", devpath);
268 if (protocol == 6 && !use_usb_info) {
269 struct sysfs_device *dev_scsi;
270 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
271 int host, bus, target, lun;
273 /* get scsi device */
274 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
275 if (dev_scsi == NULL) {
276 info("unable to find parent 'scsi' device of '%s'\n", devpath);
279 if (sscanf(dev_scsi->kernel, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
280 info("invalid scsi device '%s'\n", dev_scsi->kernel);
284 /* Generic SPC-2 device */
285 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
287 info("%s: cannot get SCSI vendor attribute\n", dev_scsi->kernel);
290 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
292 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
294 info("%s: cannot get SCSI model attribute\n", dev_scsi->kernel);
297 set_str(model_str, scsi_model, sizeof(model_str)-1);
299 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
301 info("%s: cannot get SCSI type attribute\n", dev_scsi->kernel);
304 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
306 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
308 info("%s: cannot get SCSI revision attribute\n", dev_scsi->kernel);
311 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
314 * some broken devices have the same identifiers
315 * for all luns, export the target:lun number
317 sprintf(instance_str, "%d:%d", target, lun);
321 /* fallback to USB vendor & device */
322 if (vendor_str[0] == '\0') {
323 const char *usb_vendor = NULL;
326 usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer");
329 usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor");
332 info("No USB vendor information available\n");
335 set_str(vendor_str, usb_vendor, sizeof(vendor_str)-1);
338 if (model_str[0] == '\0') {
339 const char *usb_model = NULL;
342 usb_model = sysfs_attr_get_value(dev_usb->devpath, "product");
345 usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct");
348 dbg("No USB model information available\n");
351 set_str(model_str, usb_model, sizeof(model_str)-1);
354 if (revision_str[0] == '\0') {
357 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
359 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
362 if (serial_str[0] == '\0') {
363 const char *usb_serial;
365 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
367 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
372 int main(int argc, char **argv)
376 char devpath[MAX_PATH_LEN];
378 static const struct option options[] = {
379 { "usb-info", 0, NULL, 'u' },
380 { "num-info", 0, NULL, 'n' },
381 { "export", 0, NULL, 'x' },
382 { "help", 0, NULL, 'h' },
386 logging_init("usb_id");
392 option = getopt_long(argc, argv, "nuxh", options, NULL);
408 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
409 " --usb-info use usb strings instead\n"
410 " --num-info use numerical values\n"
411 " --export print values as environemt keys\n"
412 " --help print this help text\n\n");
419 env = getenv("DEVPATH");
421 strlcpy(devpath, env, sizeof(devpath));
423 if (argv[optind] == NULL) {
424 fprintf(stderr, "No device specified\n");
428 strlcpy(devpath, argv[optind], sizeof(devpath));
431 retval = usb_id(devpath);
436 strlcpy(serial, vendor_str, sizeof(serial));
437 strlcat(serial, "_", sizeof(serial));
438 strlcat(serial, model_str, sizeof(serial));
439 if (serial_str[0] != '\0') {
440 strlcat(serial, "_", sizeof(serial));
441 strlcat(serial, serial_str, sizeof(serial));
443 if (instance_str[0] != '\0') {
444 strlcat(serial, "-", sizeof(serial));
445 strlcat(serial, instance_str, sizeof(serial));
449 printf("ID_VENDOR=%s\n", vendor_str);
450 printf("ID_MODEL=%s\n", model_str);
451 printf("ID_REVISION=%s\n", revision_str);
452 printf("ID_SERIAL=%s\n", serial);
453 if (serial_str[0] != '\0')
454 printf("ID_SERIAL_SHORT=%s\n", serial_str);
455 printf("ID_TYPE=%s\n", type_str);
456 if (instance_str[0] != '\0')
457 printf("ID_INSTANCE=%s\n", instance_str);
458 printf("ID_BUS=usb\n");
460 printf("%s\n", serial);