4 * Identify an USB (block) device
6 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
9 * Hannes Reinecke <hare@suse.de>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation version 2 of the License.
24 #include "../../udev.h"
26 #define MAX_PATH_LEN 512
27 #define MAX_SERIAL_LEN 256
28 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
31 void log_message(int priority, const char *format, ...)
34 static int udev_log = -1;
39 value = getenv("UDEV_LOG");
41 udev_log = log_priority(value);
46 if (priority > udev_log)
49 va_start(args, format);
50 vsyslog(priority, format, args);
55 static char vendor_str[64];
56 static char model_str[64];
57 static char serial_str[MAX_SERIAL_LEN];
58 static char revision_str[16];
59 static char type_str[16];
61 static int use_usb_info;
62 static int use_num_info;
66 static void set_str(char *to, const char *from, size_t count)
70 /* strip trailing whitespace */
71 len = strnlen(from, count);
72 while (len && isspace(from[len-1]))
75 /* strip leading whitespace */
77 while (isspace(from[i]) && (i < len))
82 /* substitute multiple whitespace */
83 if (isspace(from[i])) {
84 while (isspace(from[i]))
88 /* Replace '/' with '.' */
94 /* skip non-printable chars */
95 if (!isalnum(from[i]) && !ispunct(from[i])) {
107 * Set the type based on the USB interface class
109 static void set_usb_iftype(char *to, const char *from, size_t len)
113 char *type = "generic";
115 type_num = strtoul(from, &eptr, 0);
130 case 2: /* CDC-Control */
131 case 5: /* Physical */
134 case 0x0a: /* CDC-Data */
135 case 0x0b: /* Chip/Smart Card */
136 case 0x0d: /* Content Security */
137 case 0x0e: /* Video */
138 case 0xdc: /* Diagnostic Device */
139 case 0xe0: /* Wireless Controller */
140 case 0xf2: /* Application-specific */
141 case 0xff: /* Vendor-specific */
146 strncpy(to, type, len);
153 * Set the type base on the interfaceSubClass.
154 * Valid for Mass-Storage devices (type 8) only.
156 static int set_usb_ifsubtype(char *to, const char *from, size_t len)
160 char *type = "generic";
162 type_num = strtoul(from, &eptr, 0);
172 case 5: /* SFF-8070i */
175 case 1: /* RBC devices */
176 case 6: /* Transparent SPC-2 devices */
183 strncpy(to, type, len);
189 static void set_scsi_type(char *to, const char *from, int count)
194 type_num = strtoul(from, &eptr, 0);
204 sprintf(to, "optical");
210 sprintf(to, "optical");
216 sprintf(to, "optical");
219 sprintf(to, "generic");
223 sprintf(to, "generic");
228 * A unique USB identification is generated like this:
230 * 1.) Get the USB device type from DeviceClass, InterfaceClass
231 * and InterfaceSubClass
232 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
233 * use the SCSI vendor and model as USB-Vendor and USB-model.
234 * 3.) Otherwise use the USB manufacturer and product as
235 * USB-Vendor and USB-model. Any non-printable characters
236 * in those strings will be skipped; a slash '/' will be converted
237 * into a full stop '.'.
238 * 4.) If that fails, too, we will use idVendor and idProduct
239 * as USB-Vendor and USB-model.
240 * 5.) The USB identification is the USB-vendor and USB-model
241 * string concatenated with an underscore '_'.
242 * 6.) If the device supplies a serial number, this number
243 * is concatenated with the identification with an underscore '_'.
245 static int usb_id(const char *devpath)
247 struct sysfs_device *dev;
248 struct sysfs_device *dev_scsi;
249 struct sysfs_device *dev_target;
250 struct sysfs_device *dev_host;
251 struct sysfs_device *dev_interface;
252 struct sysfs_device *dev_usb;
253 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
254 const char *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
255 const char *if_class, *if_subclass;
259 dbg("devpath %s\n", devpath);
261 dev = sysfs_device_get(devpath);
263 err("unable to access '%s'", devpath);
267 /* get scsi parent device */
268 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
269 if (dev_scsi == NULL) {
270 err("unable to find parent 'scsi' device of '%s'", devpath);
274 /* target directory */
275 dev_target = sysfs_device_get_parent(dev_scsi);
276 if (dev_target == NULL) {
277 err("unable to access parent device of '%s'", devpath);
282 dev_host = sysfs_device_get_parent(dev_target);
283 if (dev_host == NULL) {
284 err("unable to access parent device of '%s'", devpath);
288 /* usb interface directory */
289 dev_interface = sysfs_device_get_parent_with_subsystem(dev_host, "usb");
290 if (dev_interface == NULL) {
291 err("unable to access parent device of '%s'", devpath);
295 /* usb device directory */
296 dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
297 if (dev_usb == NULL) {
298 err("unable to find parent 'usb' device of '%s'", devpath);
302 if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
304 info("%s: cannot get bInterfaceClass attribute", dev_interface->kernel_name);
307 if_class_num = strtoul(if_class, NULL, 16);
308 if (if_class_num != 8) {
309 set_usb_iftype(type_str, if_class, sizeof(type_str)-1);
312 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
313 protocol = set_usb_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
316 if (!use_usb_info && protocol == 6) {
317 /* Generic SPC-2 device */
318 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
320 info("%s: cannot get SCSI vendor attribute", dev_scsi->kernel_name);
323 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
325 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
327 info("%s: cannot get SCSI model attribute", dev_scsi->kernel_name);
330 set_str(model_str, scsi_model, sizeof(model_str)-1);
332 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
334 info("%s: cannot get SCSI type attribute", dev_scsi->kernel_name);
337 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
339 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
341 info("%s: cannot get SCSI revision attribute", dev_scsi->kernel_name);
344 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
348 /* Fallback to USB vendor & device */
349 if (vendor_str[0] == '\0') {
351 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer")))
352 dbg("No USB vendor string found, using idVendor");
355 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor"))) {
356 dbg("No USB vendor information available\n");
357 sprintf(vendor_str,"0000");
360 set_str(vendor_str,usb_vendor, sizeof(vendor_str) - 1);
363 if (model_str[0] == '\0') {
365 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "product")))
366 dbg("No USB model string found, using idProduct");
369 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct")))
370 dbg("No USB model information available\n"); sprintf(model_str,"0000");
372 set_str(model_str, usb_model, sizeof(model_str) - 1);
375 if (revision_str[0] == '\0') {
376 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
378 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
381 if (serial_str[0] == '\0') {
382 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
384 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
389 int main(int argc, char **argv)
393 char devpath[MAX_PATH_LEN];
396 logging_init("usb_id");
399 dbg("argc is %d", argc);
401 /* sysfs path can be overridden for testing */
402 env = getenv("SYSFS_PATH");
404 strlcpy(sysfs_path, env, sizeof(sysfs_path));
405 remove_trailing_chars(sysfs_path, '/');
407 strcpy(sysfs_path, "/sys");
409 while ((option = getopt(argc, argv, "dnux")) != -1 ) {
411 dbg("option '%c' arg '%s'", option, optarg);
413 dbg("option '%c'", option);
430 info("Unknown or bad option '%c' (0x%x)", option, option);
436 env = getenv("DEVPATH");
438 strlcpy(devpath, env, sizeof(devpath));
440 if (optind == argc) {
441 fprintf(stderr, "No device specified\n");
445 strlcpy(devpath, argv[optind], sizeof(devpath));
448 retval = usb_id(devpath);
452 printf("ID_VENDOR=%s\n", vendor_str);
453 printf("ID_MODEL=%s\n", model_str);
454 printf("ID_REVISION=%s\n", revision_str);
455 if (serial_str[0] == '\0') {
456 printf("ID_SERIAL=%s_%s\n",
457 vendor_str, model_str);
459 printf("ID_SERIAL=%s_%s_%s\n",
460 vendor_str, model_str, serial_str);
462 printf("ID_TYPE=%s\n", type_str);
463 printf("ID_BUS=usb\n");
465 if (serial_str[0] == '\0') {
467 vendor_str, model_str);
470 vendor_str, model_str, serial_str);