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/udev.h"
25 #define MAX_PATH_LEN 512
26 #define MAX_SERIAL_LEN 256
27 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
29 static void log_fn(struct udev *udev, int priority,
30 const char *file, int line, const char *fn,
31 const char *format, va_list args)
33 vsyslog(priority, format, args);
36 static char vendor_str[64];
37 static char model_str[64];
38 static char serial_str[MAX_SERIAL_LEN];
39 static char revision_str[64];
40 static char type_str[64];
41 static char instance_str[64];
43 static int use_usb_info;
44 static int use_num_info;
46 static void set_str(char *to, const char *from, size_t count)
50 /* strip trailing whitespace */
51 len = strnlen(from, count);
52 while (len && isspace(from[len-1]))
55 /* strip leading whitespace */
57 while (isspace(from[i]) && (i < len))
62 /* substitute multiple whitespace */
63 if (isspace(from[i])) {
64 while (isspace(from[i]))
68 /* Replace '/' with '.' */
74 /* skip non-printable chars */
75 if (!isalnum(from[i]) && !ispunct(from[i])) {
84 static void set_usb_iftype(char *to, int if_class_num, size_t len)
86 char *type = "generic";
88 switch (if_class_num) {
101 case 2: /* CDC-Control */
102 case 5: /* Physical */
105 case 0x0a: /* CDC-Data */
106 case 0x0b: /* Chip/Smart Card */
107 case 0x0d: /* Content Security */
108 case 0x0e: /* Video */
109 case 0xdc: /* Diagnostic Device */
110 case 0xe0: /* Wireless Controller */
111 case 0xf2: /* Application-specific */
112 case 0xff: /* Vendor-specific */
117 strncpy(to, type, len);
121 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
125 char *type = "generic";
127 type_num = strtoul(from, &eptr, 0);
137 case 5: /* SFF-8070i */
140 case 1: /* RBC devices */
141 case 6: /* Transparent SPC-2 devices */
148 util_strlcpy(to, type, len);
153 static void set_scsi_type(char *to, const char *from, size_t len)
157 char *type = "generic";
159 type_num = strtoul(from, &eptr, 0);
181 util_strlcpy(to, type, len);
185 * A unique USB identification is generated like this:
187 * 1.) Get the USB device type from DeviceClass, InterfaceClass
188 * and InterfaceSubClass
189 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
190 * use the SCSI vendor and model as USB-Vendor and USB-model.
191 * 3.) Otherwise use the USB manufacturer and product as
192 * USB-Vendor and USB-model. Any non-printable characters
193 * in those strings will be skipped; a slash '/' will be converted
194 * into a full stop '.'.
195 * 4.) If that fails, too, we will use idVendor and idProduct
196 * as USB-Vendor and USB-model.
197 * 5.) The USB identification is the USB-vendor and USB-model
198 * string concatenated with an underscore '_'.
199 * 6.) If the device supplies a serial number, this number
200 * is concatenated with the identification with an underscore '_'.
202 static int usb_id(struct udev *udev, const char *devpath)
204 struct sysfs_device *dev;
205 struct sysfs_device *dev_interface;
206 struct sysfs_device *dev_usb;
207 const char *if_class, *if_subclass;
211 dbg(udev, "devpath %s\n", devpath);
213 /* get all usb specific information: dev_interface, if_class, dev_usb */
214 dev = sysfs_device_get(udev, devpath);
216 err(udev, "unable to access '%s'\n", devpath);
220 /* usb interface directory */
221 dev_interface = sysfs_device_get_parent_with_subsystem(udev, dev, "usb");
222 if (dev_interface == NULL) {
223 info(udev, "unable to access usb_interface device of '%s'\n", devpath);
227 if_class = sysfs_attr_get_value(udev, dev_interface->devpath, "bInterfaceClass");
229 info(udev, "%s: cannot get bInterfaceClass attribute\n", dev_interface->kernel);
232 if_class_num = strtoul(if_class, NULL, 16);
233 if (if_class_num == 8) {
234 if_subclass = sysfs_attr_get_value(udev, dev_interface->devpath, "bInterfaceSubClass");
235 if (if_subclass != NULL)
236 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
238 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
240 info(udev, "%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
242 /* usb device directory */
243 dev_usb = sysfs_device_get_parent_with_subsystem(udev, dev_interface, "usb");
245 info(udev, "unable to find parent 'usb' device of '%s'\n", devpath);
250 if (protocol == 6 && !use_usb_info) {
251 struct sysfs_device *dev_scsi;
252 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
253 int host, bus, target, lun;
255 /* get scsi device */
256 dev_scsi = sysfs_device_get_parent_with_subsystem(udev, dev, "scsi");
257 if (dev_scsi == NULL) {
258 info(udev, "unable to find parent 'scsi' device of '%s'\n", devpath);
261 if (sscanf(dev_scsi->kernel, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
262 info(udev, "invalid scsi device '%s'\n", dev_scsi->kernel);
266 /* Generic SPC-2 device */
267 scsi_vendor = sysfs_attr_get_value(udev, dev_scsi->devpath, "vendor");
269 info(udev, "%s: cannot get SCSI vendor attribute\n", dev_scsi->kernel);
272 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
274 scsi_model = sysfs_attr_get_value(udev, dev_scsi->devpath, "model");
276 info(udev, "%s: cannot get SCSI model attribute\n", dev_scsi->kernel);
279 set_str(model_str, scsi_model, sizeof(model_str)-1);
281 scsi_type = sysfs_attr_get_value(udev, dev_scsi->devpath, "type");
283 info(udev, "%s: cannot get SCSI type attribute\n", dev_scsi->kernel);
286 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
288 scsi_rev = sysfs_attr_get_value(udev, dev_scsi->devpath, "rev");
290 info(udev, "%s: cannot get SCSI revision attribute\n", dev_scsi->kernel);
293 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
296 * some broken devices have the same identifiers
297 * for all luns, export the target:lun number
299 sprintf(instance_str, "%d:%d", target, lun);
303 /* fallback to USB vendor & device */
304 if (vendor_str[0] == '\0') {
305 const char *usb_vendor = NULL;
308 usb_vendor = sysfs_attr_get_value(udev, dev_usb->devpath, "manufacturer");
311 usb_vendor = sysfs_attr_get_value(udev, dev_usb->devpath, "idVendor");
314 info(udev, "No USB vendor information available\n");
317 set_str(vendor_str, usb_vendor, sizeof(vendor_str)-1);
320 if (model_str[0] == '\0') {
321 const char *usb_model = NULL;
324 usb_model = sysfs_attr_get_value(udev, dev_usb->devpath, "product");
327 usb_model = sysfs_attr_get_value(udev, dev_usb->devpath, "idProduct");
330 dbg(udev, "No USB model information available\n");
333 set_str(model_str, usb_model, sizeof(model_str)-1);
336 if (revision_str[0] == '\0') {
339 usb_rev = sysfs_attr_get_value(udev, dev_usb->devpath, "bcdDevice");
341 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
344 if (serial_str[0] == '\0') {
345 const char *usb_serial;
347 usb_serial = sysfs_attr_get_value(udev, dev_usb->devpath, "serial");
349 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
354 int main(int argc, char **argv)
359 char devpath[MAX_PATH_LEN];
361 static const struct option options[] = {
362 { "usb-info", no_argument, NULL, 'u' },
363 { "num-info", no_argument, NULL, 'n' },
364 { "export", no_argument, NULL, 'x' },
365 { "help", no_argument, NULL, 'h' },
373 logging_init("usb_id");
374 udev_set_log_fn(udev, log_fn);
380 option = getopt_long(argc, argv, "nuxh", options, NULL);
396 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
397 " --usb-info use usb strings instead\n"
398 " --num-info use numerical values\n"
399 " --export print values as environemt keys\n"
400 " --help print this help text\n\n");
407 env = getenv("DEVPATH");
409 util_strlcpy(devpath, env, sizeof(devpath));
411 if (argv[optind] == NULL) {
412 fprintf(stderr, "No device specified\n");
416 util_strlcpy(devpath, argv[optind], sizeof(devpath));
419 retval = usb_id(udev, devpath);
424 util_strlcpy(serial, vendor_str, sizeof(serial));
425 util_strlcat(serial, "_", sizeof(serial));
426 util_strlcat(serial, model_str, sizeof(serial));
427 if (serial_str[0] != '\0') {
428 util_strlcat(serial, "_", sizeof(serial));
429 util_strlcat(serial, serial_str, sizeof(serial));
431 if (instance_str[0] != '\0') {
432 util_strlcat(serial, "-", sizeof(serial));
433 util_strlcat(serial, instance_str, sizeof(serial));
437 printf("ID_VENDOR=%s\n", vendor_str);
438 printf("ID_MODEL=%s\n", model_str);
439 printf("ID_REVISION=%s\n", revision_str);
440 printf("ID_SERIAL=%s\n", serial);
441 if (serial_str[0] != '\0')
442 printf("ID_SERIAL_SHORT=%s\n", serial_str);
443 printf("ID_TYPE=%s\n", type_str);
444 if (instance_str[0] != '\0')
445 printf("ID_INSTANCE=%s\n", instance_str);
446 printf("ID_BUS=usb\n");
448 printf("%s\n", serial);