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 <../../libsysfs/sysfs/libsysfs.h>
25 #include "../../udev_utils.h"
26 #include "../../logging.h"
28 #define MAX_NAME_LEN 72
29 #define MAX_SERIAL_LEN 256
30 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
33 void log_message(int priority, const char *format, ...)
36 static int udev_log = -1;
41 value = getenv("UDEV_LOG");
43 udev_log = log_priority(value);
48 if (priority > udev_log)
51 va_start(args, format);
52 vsyslog(priority, format, args);
57 char sysfs_mnt_path[SYSFS_PATH_MAX];
58 static char vendor_str[64];
59 static char model_str[64];
60 static char serial_str[MAX_SERIAL_LEN];
61 static char revision_str[16];
62 static char type_str[16];
64 static int use_usb_info;
65 static int use_num_info;
69 static void set_str(char *to, const char *from, size_t count)
73 /* strip trailing whitespace */
74 len = strnlen(from, count);
75 while (len && isspace(from[len-1]))
78 /* strip leading whitespace */
80 while (isspace(from[i]) && (i < len))
85 /* substitute multiple whitespace */
86 if (isspace(from[i])) {
87 while (isspace(from[i]))
91 /* Replace '/' with '.' */
97 /* skip non-printable chars */
98 if (!isalnum(from[i]) && !ispunct(from[i])) {
110 * Set the type based on the USB interface class
112 static void set_usb_iftype(char *to, const char *from, size_t len)
116 char *type = "generic";
118 type_num = strtoul(from, &eptr, 0);
133 case 2: /* CDC-Control */
134 case 5: /* Physical */
137 case 0x0a: /* CDC-Data */
138 case 0x0b: /* Chip/Smart Card */
139 case 0x0d: /* Content Security */
140 case 0x0e: /* Video */
141 case 0xdc: /* Diagnostic Device */
142 case 0xe0: /* Wireless Controller */
143 case 0xf2: /* Application-specific */
144 case 0xff: /* Vendor-specific */
149 strncpy(to, type, len);
156 * Set the type base on the interfaceSubClass.
157 * Valid for Mass-Storage devices (type 8) only.
159 static int set_usb_ifsubtype(char *to, const char *from, size_t len)
163 char *type = "generic";
165 type_num = strtoul(from, &eptr, 0);
175 case 5: /* SFF-8070i */
178 case 1: /* RBC devices */
179 case 6: /* Transparent SPC-2 devices */
186 strncpy(to, type, len);
192 static void set_scsi_type(char *to, const char *from, int count)
197 type_num = strtoul(from, &eptr, 0);
207 sprintf(to, "optical");
213 sprintf(to, "optical");
219 sprintf(to, "optical");
222 sprintf(to, "generic");
226 sprintf(to, "generic");
231 * A unique USB identification is generated like this:
233 * 1.) Get the USB device type from DeviceClass, InterfaceClass
234 * and InterfaceSubClass
235 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
236 * use the SCSI vendor and model as USB-Vendor and USB-model.
237 * 3.) Otherwise use the USB manufacturer and product as
238 * USB-Vendor and USB-model. Any non-printable characters
239 * in those strings will be skipped; a slash '/' will be converted
240 * into a full stop '.'.
241 * 4.) If that fails, too, we will use idVendor and idProduct
242 * as USB-Vendor and USB-model.
243 * 5.) The USB identification is the USB-vendor and USB-model
244 * string concatenated with an underscore '_'.
245 * 6.) If the device supplies a serial number, this number
246 * is concatenated with the identification with an underscore '_'.
248 static int usb_id(const char *target_path)
250 struct sysfs_class_device *class_dev; /* of target_path */
251 struct sysfs_class_device *class_dev_parent; /* for partitions */
252 struct sysfs_device *scsi_dev; /* the scsi_device */
253 struct sysfs_device *target_dev;
254 struct sysfs_device *host_dev, *interface_dev, *usb_dev;
255 struct sysfs_attribute *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
256 struct sysfs_attribute *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
257 struct sysfs_attribute *if_class, *if_subclass;
261 class_dev = sysfs_open_class_device_path(target_path);
263 info("open class %s failed: %s", target_path, strerror(errno));
266 class_dev_parent = sysfs_get_classdev_parent(class_dev);
267 if (class_dev_parent) {
268 scsi_dev = sysfs_get_classdev_device(class_dev_parent);
270 scsi_dev = sysfs_get_classdev_device(class_dev);
274 * The close of scsi_dev will close class_dev or class_dev_parent.
278 * We assume we are called after the device is completely ready,
279 * so we don't have to loop here like udev. (And we are usually
284 * errno is not set if we can't find the device link, so
285 * don't print it out here.
287 info("Cannot find sysfs device associated with %s", target_path);
292 * Allow only scsi devices.
294 * Other block devices can support SG IO, but only ide-cd does, so
295 * for now, don't bother with anything else.
297 if (strcmp(scsi_dev->bus, "scsi") != 0) {
298 info("%s is not a scsi device", target_path);
302 /* target directory */
303 target_dev = sysfs_get_device_parent(scsi_dev);
305 host_dev = sysfs_get_device_parent(target_dev);
306 /* usb interface directory */
307 interface_dev = sysfs_get_device_parent(host_dev);
308 /* usb device directory */
309 usb_dev = sysfs_get_device_parent(interface_dev);
311 if (strcmp(interface_dev->bus, "usb") != 0) {
312 info("%s is not an usb device", target_path);
316 if_class = sysfs_get_device_attr(interface_dev, "bInterfaceClass");
318 info("%s: cannot get bInterfaceClass attribute", interface_dev->name);
321 if_class_num = strtoul(if_class->value, NULL, 16);
322 if (if_class_num != 8) {
323 set_usb_iftype(type_str, if_class->value, sizeof(type_str) - 1);
326 if_subclass = sysfs_get_device_attr(interface_dev,
327 "bInterfaceSubClass");
328 protocol = set_usb_ifsubtype(type_str, if_subclass->value,
329 sizeof(type_str) -1 );
332 if (!use_usb_info && protocol == 6) {
333 /* Generic SPC-2 device */
334 scsi_vendor = sysfs_get_device_attr(scsi_dev, "vendor");
336 info("%s: cannot get SCSI vendor attribute", scsi_dev->name);
339 set_str(vendor_str, scsi_vendor->value, sizeof(vendor_str)-1);
341 scsi_model = sysfs_get_device_attr(scsi_dev, "model");
343 info("%s: cannot get SCSI model attribute", scsi_dev->name);
346 set_str(model_str, scsi_model->value, sizeof(model_str)-1);
348 scsi_type = sysfs_get_device_attr(scsi_dev, "type");
350 info("%s: cannot get SCSI type attribute", scsi_dev->name);
353 set_scsi_type(type_str, scsi_type->value, sizeof(type_str)-1);
355 scsi_rev = sysfs_get_device_attr(scsi_dev, "rev");
357 info("%s: cannot get SCSI revision attribute", scsi_dev->name);
360 set_str(revision_str, scsi_rev->value, sizeof(revision_str)-1);
364 /* Fallback to USB vendor & device */
365 if (vendor_str[0] == '\0') {
367 if (!(usb_vendor = sysfs_get_device_attr(usb_dev, "manufacturer")))
368 dbg("No USB vendor string found, using idVendor");
371 if (!(usb_vendor = sysfs_get_device_attr(usb_dev, "idVendor"))) {
372 dbg("No USB vendor information available\n");
373 sprintf(vendor_str,"0000");
376 set_str(vendor_str,usb_vendor->value, sizeof(vendor_str) - 1);
379 if (model_str[0] == '\0') {
381 if (!(usb_model = sysfs_get_device_attr(usb_dev, "product")))
382 dbg("No USB model string found, using idProduct");
385 if (!(usb_model = sysfs_get_device_attr(usb_dev, "idProduct"))) {
386 dbg("No USB model information available\n");
387 sprintf(model_str,"0000");
390 set_str(model_str, usb_model->value, sizeof(model_str) - 1);
393 if (revision_str[0] == '\0') {
394 usb_rev = sysfs_get_device_attr(usb_dev, "bcdDevice");
396 set_str(revision_str, usb_rev->value,
397 sizeof(revision_str) - 1);
401 if (serial_str[0] == '\0') {
402 usb_serial = sysfs_get_device_attr(usb_dev, "serial");
404 set_str(serial_str, usb_serial->value,
405 sizeof(serial_str) - 1);
411 int main(int argc, char **argv)
415 char target_path[MAX_NAME_LEN];
418 dbg("argc is %d", argc);
419 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
420 info("sysfs_get_mnt_path failed: %s",
425 while ((option = getopt(argc, argv, "dnux")) != -1 ) {
427 dbg("option '%c' arg '%s'", option, optarg);
429 dbg("option '%c'", option);
446 info("Unknown or bad option '%c' (0x%x)", option, option);
452 devpath = getenv("DEVPATH");
454 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
455 strncat(target_path, devpath, MAX_NAME_LEN);
457 if (optind == argc) {
458 fprintf(stderr, "No device specified\n");
461 devpath = argv[optind];
462 strncpy(target_path, devpath, MAX_NAME_LEN);
465 retval = usb_id(target_path);
469 printf("ID_VENDOR=%s\n", vendor_str);
470 printf("ID_MODEL=%s\n", model_str);
471 printf("ID_REVISION=%s\n", revision_str);
472 if (serial_str[0] == '\0') {
473 printf("ID_SERIAL=%s_%s\n",
474 vendor_str, model_str);
476 printf("ID_SERIAL=%s_%s_%s\n",
477 vendor_str, model_str, serial_str);
479 printf("ID_TYPE=%s\n", type_str);
480 printf("ID_BUS=usb\n");
482 if (serial_str[0] == '\0') {
484 vendor_str, model_str);
487 vendor_str, model_str, serial_str);