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;
65 static void set_str(char *to, const char *from, size_t count)
69 /* strip trailing whitespace */
70 len = strnlen(from, count);
71 while (len && isspace(from[len-1]))
74 /* strip leading whitespace */
76 while (isspace(from[i]) && (i < len))
81 /* substitute multiple whitespace */
82 if (isspace(from[i])) {
83 while (isspace(from[i]))
87 /* Replace '/' with '.' */
93 /* skip non-printable chars */
94 if (!isalnum(from[i]) && !ispunct(from[i])) {
103 static void set_usb_iftype(char *to, int if_class_num, size_t len)
105 char *type = "generic";
107 switch (if_class_num) {
120 case 2: /* CDC-Control */
121 case 5: /* Physical */
124 case 0x0a: /* CDC-Data */
125 case 0x0b: /* Chip/Smart Card */
126 case 0x0d: /* Content Security */
127 case 0x0e: /* Video */
128 case 0xdc: /* Diagnostic Device */
129 case 0xe0: /* Wireless Controller */
130 case 0xf2: /* Application-specific */
131 case 0xff: /* Vendor-specific */
136 strncpy(to, type, len);
140 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
144 char *type = "generic";
146 type_num = strtoul(from, &eptr, 0);
156 case 5: /* SFF-8070i */
159 case 1: /* RBC devices */
160 case 6: /* Transparent SPC-2 devices */
167 strncpy(to, type, len);
173 static void set_scsi_type(char *to, const char *from, int count)
178 type_num = strtoul(from, &eptr, 0);
188 sprintf(to, "optical");
194 sprintf(to, "optical");
200 sprintf(to, "optical");
203 sprintf(to, "generic");
207 sprintf(to, "generic");
212 * A unique USB identification is generated like this:
214 * 1.) Get the USB device type from DeviceClass, InterfaceClass
215 * and InterfaceSubClass
216 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
217 * use the SCSI vendor and model as USB-Vendor and USB-model.
218 * 3.) Otherwise use the USB manufacturer and product as
219 * USB-Vendor and USB-model. Any non-printable characters
220 * in those strings will be skipped; a slash '/' will be converted
221 * into a full stop '.'.
222 * 4.) If that fails, too, we will use idVendor and idProduct
223 * as USB-Vendor and USB-model.
224 * 5.) The USB identification is the USB-vendor and USB-model
225 * string concatenated with an underscore '_'.
226 * 6.) If the device supplies a serial number, this number
227 * is concatenated with the identification with an underscore '_'.
229 static int usb_id(const char *devpath)
231 struct sysfs_device *dev;
232 struct sysfs_device *dev_interface;
233 struct sysfs_device *dev_usb;
234 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
235 const char *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
236 const char *if_class, *if_subclass;
240 dbg("devpath %s\n", devpath);
242 /* get all usb specific information: dev_interface, if_class, dev_usb */
243 dev = sysfs_device_get(devpath);
245 err("unable to access '%s'", devpath);
249 /* usb interface directory */
250 dev_interface = sysfs_device_get_parent_with_subsystem(dev, "usb");
251 if (dev_interface == NULL) {
252 info("unable to access usb_interface device of '%s'", devpath);
256 if_class = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceClass");
258 info("%s: cannot get bInterfaceClass attribute", dev_interface->kernel_name);
261 if_class_num = strtoul(if_class, NULL, 16);
262 if (if_class_num == 8) {
263 if_subclass = sysfs_attr_get_value(dev_interface->devpath, "bInterfaceSubClass");
264 if (if_subclass != NULL)
265 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
267 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
269 info("%s: if_class %d protocol %d\n", dev_interface->devpath, if_class_num, protocol);
271 /* usb device directory */
272 dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
274 info("unable to find parent 'usb' device of '%s'", devpath);
279 if (protocol == 6 && !use_usb_info) {
280 struct sysfs_device *dev_scsi;
282 /* get scsi device */
283 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
284 if (dev_scsi == NULL) {
285 info("unable to find parent 'scsi' device of '%s'", devpath);
289 /* Generic SPC-2 device */
290 scsi_vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
292 info("%s: cannot get SCSI vendor attribute", dev_scsi->kernel_name);
295 set_str(vendor_str, scsi_vendor, sizeof(vendor_str)-1);
297 scsi_model = sysfs_attr_get_value(dev_scsi->devpath, "model");
299 info("%s: cannot get SCSI model attribute", dev_scsi->kernel_name);
302 set_str(model_str, scsi_model, sizeof(model_str)-1);
304 scsi_type = sysfs_attr_get_value(dev_scsi->devpath, "type");
306 info("%s: cannot get SCSI type attribute", dev_scsi->kernel_name);
309 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
311 scsi_rev = sysfs_attr_get_value(dev_scsi->devpath, "rev");
313 info("%s: cannot get SCSI revision attribute", dev_scsi->kernel_name);
316 set_str(revision_str, scsi_rev, sizeof(revision_str)-1);
320 /* Fallback to USB vendor & device */
321 if (vendor_str[0] == '\0') {
323 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "manufacturer")))
324 dbg("No USB vendor string found, using idVendor");
327 if (!(usb_vendor = sysfs_attr_get_value(dev_usb->devpath, "idVendor"))) {
328 dbg("No USB vendor information available\n");
329 sprintf(vendor_str,"0000");
332 set_str(vendor_str,usb_vendor, sizeof(vendor_str) - 1);
335 if (model_str[0] == '\0') {
337 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "product")))
338 dbg("No USB model string found, using idProduct");
341 if (!(usb_model = sysfs_attr_get_value(dev_usb->devpath, "idProduct")))
342 dbg("No USB model information available\n"); sprintf(model_str,"0000");
344 set_str(model_str, usb_model, sizeof(model_str) - 1);
347 if (revision_str[0] == '\0') {
348 usb_rev = sysfs_attr_get_value(dev_usb->devpath, "bcdDevice");
350 set_str(revision_str, usb_rev, sizeof(revision_str)-1);
353 if (serial_str[0] == '\0') {
354 usb_serial = sysfs_attr_get_value(dev_usb->devpath, "serial");
356 set_str(serial_str, usb_serial, sizeof(serial_str)-1);
361 int main(int argc, char **argv)
365 char devpath[MAX_PATH_LEN];
368 logging_init("usb_id");
371 dbg("argc is %d", argc);
373 while ((option = getopt(argc, argv, "dnux")) != -1 ) {
375 dbg("option '%c' arg '%s'", option, optarg);
377 dbg("option '%c'", option);
391 info("Unknown or bad option '%c' (0x%x)", option, option);
397 env = getenv("DEVPATH");
399 strlcpy(devpath, env, sizeof(devpath));
401 if (optind == argc) {
402 fprintf(stderr, "No device specified\n");
406 strlcpy(devpath, argv[optind], sizeof(devpath));
409 retval = usb_id(devpath);
413 printf("ID_VENDOR=%s\n", vendor_str);
414 printf("ID_MODEL=%s\n", model_str);
415 printf("ID_REVISION=%s\n", revision_str);
416 if (serial_str[0] == '\0') {
417 printf("ID_SERIAL=%s_%s\n",
418 vendor_str, model_str);
420 printf("ID_SERIAL=%s_%s_%s\n",
421 vendor_str, model_str, serial_str);
423 printf("ID_TYPE=%s\n", type_str);
424 printf("ID_BUS=usb\n");
426 if (serial_str[0] == '\0') {
428 vendor_str, model_str);
431 vendor_str, model_str, serial_str);