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_NAME_LEN 72
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, *dev_interface, *dev_usb;
251 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
252 const char *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
253 const char *if_class, *if_subclass;
257 dbg("devpath %s\n", devpath);
259 dev = sysfs_device_get(devpath);
261 err("unable to access '%s'", devpath);
265 /* get scsi parent device */
266 dev_scsi = sysfs_device_get_parent(dev);
267 if (dev_scsi == NULL) {
268 err("unable to access parent device of '%s'", devpath);
271 /* allow only scsi devices */
272 if (strcmp(dev_scsi->subsystem, "scsi") != 0) {
273 info("%s is not a scsi device", devpath);
277 /* target directory */
278 dev_target = sysfs_device_get_parent(dev_scsi);
279 if (dev_target == NULL) {
280 err("unable to access parent device of '%s'", devpath);
285 dev_host = sysfs_device_get_parent(dev_target);
286 if (dev_host == NULL) {
287 err("unable to access parent device of '%s'", devpath);
291 /* usb interface directory */
292 dev_interface = sysfs_device_get_parent(dev_host);
293 if (dev_interface == NULL) {
294 err("unable to access parent device of '%s'", devpath);
298 /* usb device directory */
299 dev_usb = sysfs_device_get_parent(dev_interface);
300 if (dev_usb == NULL) {
301 err("unable to access parent device of '%s'", devpath);
304 if (strcmp(dev_interface->subsystem, "usb") != 0) {
305 info("%s is not an usb device", devpath);
309 if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
311 info("%s: cannot get bInterfaceClass attribute", dev_interface->kernel_name);
314 if_class_num = strtoul(if_class, NULL, 16);
315 if (if_class_num != 8) {
316 set_usb_iftype(type_str, if_class, sizeof(type_str)-1);
319 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
320 protocol = set_usb_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
323 if (!use_usb_info && protocol == 6) {
324 /* Generic SPC-2 device */
325 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
327 info("%s: cannot get SCSI vendor attribute", dev_scsi->kernel_name);
330 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
332 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
334 info("%s: cannot get SCSI model attribute", dev_scsi->kernel_name);
337 set_str(model_str, scsi_model, sizeof(model_str)-1);
339 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
341 info("%s: cannot get SCSI type attribute", dev_scsi->kernel_name);
344 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
346 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
348 info("%s: cannot get SCSI revision attribute", dev_scsi->kernel_name);
351 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
355 /* Fallback to USB vendor & device */
356 if (vendor_str[0] == '\0') {
358 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer")))
359 dbg("No USB vendor string found, using idVendor");
362 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor"))) {
363 dbg("No USB vendor information available\n");
364 sprintf(vendor_str,"0000");
367 set_str(vendor_str,usb_vendor, sizeof(vendor_str) - 1);
370 if (model_str[0] == '\0') {
372 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "product")))
373 dbg("No USB model string found, using idProduct");
376 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct")))
377 dbg("No USB model information available\n"); sprintf(model_str,"0000");
379 set_str(model_str, usb_model, sizeof(model_str) - 1);
382 if (revision_str[0] == '\0') {
383 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
385 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
388 if (serial_str[0] == '\0') {
389 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
391 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
396 int main(int argc, char **argv)
400 char devpath[MAX_NAME_LEN];
403 logging_init("usb_id");
406 dbg("argc is %d", argc);
408 /* sysfs path can be overridden for testing */
409 env = getenv("SYSFS_PATH");
411 strlcpy(sysfs_path, env, sizeof(sysfs_path));
412 remove_trailing_chars(sysfs_path, '/');
414 strcpy(sysfs_path, "/sys");
416 while ((option = getopt(argc, argv, "dnux")) != -1 ) {
418 dbg("option '%c' arg '%s'", option, optarg);
420 dbg("option '%c'", option);
437 info("Unknown or bad option '%c' (0x%x)", option, option);
443 env = getenv("DEVPATH");
445 strlcpy(devpath, env, sizeof(devpath));
447 if (optind == argc) {
448 fprintf(stderr, "No device specified\n");
452 strlcpy(devpath, argv[optind], sizeof(devpath));
455 retval = usb_id(devpath);
459 printf("ID_VENDOR=%s\n", vendor_str);
460 printf("ID_MODEL=%s\n", model_str);
461 printf("ID_REVISION=%s\n", revision_str);
462 if (serial_str[0] == '\0') {
463 printf("ID_SERIAL=%s_%s\n",
464 vendor_str, model_str);
466 printf("ID_SERIAL=%s_%s_%s\n",
467 vendor_str, model_str, serial_str);
469 printf("ID_TYPE=%s\n", type_str);
470 printf("ID_BUS=usb\n");
472 if (serial_str[0] == '\0') {
474 vendor_str, model_str);
477 vendor_str, model_str, serial_str);