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])) {
104 static void set_usb_iftype(char *to, int if_class_num, size_t len)
106 char *type = "generic";
108 switch (if_class_num) {
121 case 2: /* CDC-Control */
122 case 5: /* Physical */
125 case 0x0a: /* CDC-Data */
126 case 0x0b: /* Chip/Smart Card */
127 case 0x0d: /* Content Security */
128 case 0x0e: /* Video */
129 case 0xdc: /* Diagnostic Device */
130 case 0xe0: /* Wireless Controller */
131 case 0xf2: /* Application-specific */
132 case 0xff: /* Vendor-specific */
137 strncpy(to, type, len);
141 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
145 char *type = "generic";
147 type_num = strtoul(from, &eptr, 0);
157 case 5: /* SFF-8070i */
160 case 1: /* RBC devices */
161 case 6: /* Transparent SPC-2 devices */
168 strncpy(to, type, len);
174 static void set_scsi_type(char *to, const char *from, int count)
179 type_num = strtoul(from, &eptr, 0);
189 sprintf(to, "optical");
195 sprintf(to, "optical");
201 sprintf(to, "optical");
204 sprintf(to, "generic");
208 sprintf(to, "generic");
213 * A unique USB identification is generated like this:
215 * 1.) Get the USB device type from DeviceClass, InterfaceClass
216 * and InterfaceSubClass
217 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
218 * use the SCSI vendor and model as USB-Vendor and USB-model.
219 * 3.) Otherwise use the USB manufacturer and product as
220 * USB-Vendor and USB-model. Any non-printable characters
221 * in those strings will be skipped; a slash '/' will be converted
222 * into a full stop '.'.
223 * 4.) If that fails, too, we will use idVendor and idProduct
224 * as USB-Vendor and USB-model.
225 * 5.) The USB identification is the USB-vendor and USB-model
226 * string concatenated with an underscore '_'.
227 * 6.) If the device supplies a serial number, this number
228 * is concatenated with the identification with an underscore '_'.
230 static int usb_id(const char *devpath)
232 struct sysfs_device *dev;
233 struct sysfs_device *dev_interface;
234 struct sysfs_device *dev_usb;
235 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
236 const char *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
237 const char *if_class, *if_subclass;
241 dbg("devpath %s\n", devpath);
243 /* get all usb specific information: dev_interface, if_class, dev_usb */
244 dev = sysfs_device_get(devpath);
246 err("unable to access '%s'", devpath);
250 /* usb interface directory */
251 dev_interface = sysfs_device_get_parent_with_subsystem(dev, "usb");
252 if (dev_interface == NULL) {
253 info("unable to access usb_interface device of '%s'", devpath);
257 if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
259 info("%s: cannot get bInterfaceClass attribute", dev_interface->kernel_name);
262 if_class_num = strtoul(if_class, NULL, 16);
263 if (if_class_num == 8) {
264 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
265 if (if_subclass != NULL)
266 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
268 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
270 info("%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
272 /* usb device directory */
273 dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
275 info("unable to find parent 'usb' device of '%s'", devpath);
280 if (protocol == 6 && !use_usb_info) {
281 struct sysfs_device *dev_scsi;
283 /* get scsi device */
284 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
285 if (dev_scsi == NULL) {
286 info("unable to find parent 'scsi' device of '%s'", devpath);
290 /* Generic SPC-2 device */
291 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
293 info("%s: cannot get SCSI vendor attribute", dev_scsi->kernel_name);
296 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
298 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
300 info("%s: cannot get SCSI model attribute", dev_scsi->kernel_name);
303 set_str(model_str, scsi_model, sizeof(model_str)-1);
305 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
307 info("%s: cannot get SCSI type attribute", dev_scsi->kernel_name);
310 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
312 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
314 info("%s: cannot get SCSI revision attribute", dev_scsi->kernel_name);
317 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
321 /* Fallback to USB vendor & device */
322 if (vendor_str[0] == '\0') {
324 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer")))
325 dbg("No USB vendor string found, using idVendor");
328 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor"))) {
329 dbg("No USB vendor information available\n");
330 sprintf(vendor_str,"0000");
333 set_str(vendor_str,usb_vendor, sizeof(vendor_str) - 1);
336 if (model_str[0] == '\0') {
338 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "product")))
339 dbg("No USB model string found, using idProduct");
342 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct")))
343 dbg("No USB model information available\n"); sprintf(model_str,"0000");
345 set_str(model_str, usb_model, sizeof(model_str) - 1);
348 if (revision_str[0] == '\0') {
349 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
351 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
354 if (serial_str[0] == '\0') {
355 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
357 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
362 int main(int argc, char **argv)
366 char devpath[MAX_PATH_LEN];
369 logging_init("usb_id");
372 dbg("argc is %d", argc);
374 /* sysfs path can be overridden for testing */
375 env = getenv("SYSFS_PATH");
377 strlcpy(sysfs_path, env, sizeof(sysfs_path));
378 remove_trailing_chars(sysfs_path, '/');
380 strcpy(sysfs_path, "/sys");
382 while ((option = getopt(argc, argv, "dnux")) != -1 ) {
384 dbg("option '%c' arg '%s'", option, optarg);
386 dbg("option '%c'", option);
403 info("Unknown or bad option '%c' (0x%x)", option, option);
409 env = getenv("DEVPATH");
411 strlcpy(devpath, env, sizeof(devpath));
413 if (optind == argc) {
414 fprintf(stderr, "No device specified\n");
418 strlcpy(devpath, argv[optind], sizeof(devpath));
421 retval = usb_id(devpath);
425 printf("ID_VENDOR=%s\n", vendor_str);
426 printf("ID_MODEL=%s\n", model_str);
427 printf("ID_REVISION=%s\n", revision_str);
428 if (serial_str[0] == '\0') {
429 printf("ID_SERIAL=%s_%s\n",
430 vendor_str, model_str);
432 printf("ID_SERIAL=%s_%s_%s\n",
433 vendor_str, model_str, serial_str);
435 printf("ID_TYPE=%s\n", type_str);
436 printf("ID_BUS=usb\n");
438 if (serial_str[0] == '\0') {
440 vendor_str, model_str);
443 vendor_str, model_str, serial_str);