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.
25 #include "../../udev.h"
27 #define MAX_PATH_LEN 512
28 #define MAX_SERIAL_LEN 256
29 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
32 void log_message(int priority
, const char *format
, ...)
35 static int udev_log
= -1;
40 value
= getenv("UDEV_LOG");
42 udev_log
= log_priority(value
);
47 if (priority
> udev_log
)
50 va_start(args
, format
);
51 vsyslog(priority
, format
, args
);
56 static char vendor_str
[64];
57 static char model_str
[64];
58 static char serial_str
[MAX_SERIAL_LEN
];
59 static char revision_str
[64];
60 static char type_str
[64];
61 static char instance_str
[64];
63 static int use_usb_info
;
64 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 strlcpy(to
, type
, len
);
173 static void set_scsi_type(char *to
, const char *from
, size_t len
)
177 char *type
= "generic";
179 type_num
= strtoul(from
, &eptr
, 0);
201 strlcpy(to
, type
, len
);
205 * A unique USB identification is generated like this:
207 * 1.) Get the USB device type from DeviceClass, InterfaceClass
208 * and InterfaceSubClass
209 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
210 * use the SCSI vendor and model as USB-Vendor and USB-model.
211 * 3.) Otherwise use the USB manufacturer and product as
212 * USB-Vendor and USB-model. Any non-printable characters
213 * in those strings will be skipped; a slash '/' will be converted
214 * into a full stop '.'.
215 * 4.) If that fails, too, we will use idVendor and idProduct
216 * as USB-Vendor and USB-model.
217 * 5.) The USB identification is the USB-vendor and USB-model
218 * string concatenated with an underscore '_'.
219 * 6.) If the device supplies a serial number, this number
220 * is concatenated with the identification with an underscore '_'.
222 static int usb_id(const char *devpath
)
224 struct sysfs_device
*dev
;
225 struct sysfs_device
*dev_interface
;
226 struct sysfs_device
*dev_usb
;
227 const char *scsi_model
, *scsi_vendor
, *scsi_type
, *scsi_rev
;
228 const char *usb_model
= NULL
, *usb_vendor
= NULL
, *usb_rev
, *usb_serial
;
229 const char *if_class
, *if_subclass
;
233 dbg("devpath %s\n", devpath
);
235 /* get all usb specific information: dev_interface, if_class, dev_usb */
236 dev
= sysfs_device_get(devpath
);
238 err("unable to access '%s'", devpath
);
242 /* usb interface directory */
243 dev_interface
= sysfs_device_get_parent_with_subsystem(dev
, "usb");
244 if (dev_interface
== NULL
) {
245 info("unable to access usb_interface device of '%s'", devpath
);
249 if_class
= sysfs_attr_get_value(dev_interface
->devpath
, "bInterfaceClass");
251 info("%s: cannot get bInterfaceClass attribute", dev_interface
->kernel
);
254 if_class_num
= strtoul(if_class
, NULL
, 16);
255 if (if_class_num
== 8) {
256 if_subclass
= sysfs_attr_get_value(dev_interface
->devpath
, "bInterfaceSubClass");
257 if (if_subclass
!= NULL
)
258 protocol
= set_usb_mass_storage_ifsubtype(type_str
, if_subclass
, sizeof(type_str
)-1);
260 set_usb_iftype(type_str
, if_class_num
, sizeof(type_str
)-1);
262 info("%s: if_class %d protocol %d\n", dev_interface
->devpath
, if_class_num
, protocol
);
264 /* usb device directory */
265 dev_usb
= sysfs_device_get_parent_with_subsystem(dev_interface
, "usb");
267 info("unable to find parent 'usb' device of '%s'", devpath
);
272 if (protocol
== 6 && !use_usb_info
) {
273 struct sysfs_device
*dev_scsi
;
274 int host
, bus
, target
, lun
;
276 /* get scsi device */
277 dev_scsi
= sysfs_device_get_parent_with_subsystem(dev
, "scsi");
278 if (dev_scsi
== NULL
) {
279 info("unable to find parent 'scsi' device of '%s'", devpath
);
282 if (sscanf(dev_scsi
->kernel
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4) {
283 info("invalid scsi device '%s'", dev_scsi
->kernel
);
287 /* Generic SPC-2 device */
288 scsi_vendor
= sysfs_attr_get_value(dev_scsi
->devpath
, "vendor");
290 info("%s: cannot get SCSI vendor attribute", dev_scsi
->kernel
);
293 set_str(vendor_str
, scsi_vendor
, sizeof(vendor_str
)-1);
295 scsi_model
= sysfs_attr_get_value(dev_scsi
->devpath
, "model");
297 info("%s: cannot get SCSI model attribute", dev_scsi
->kernel
);
300 set_str(model_str
, scsi_model
, sizeof(model_str
)-1);
302 scsi_type
= sysfs_attr_get_value(dev_scsi
->devpath
, "type");
304 info("%s: cannot get SCSI type attribute", dev_scsi
->kernel
);
307 set_scsi_type(type_str
, scsi_type
, sizeof(type_str
)-1);
309 scsi_rev
= sysfs_attr_get_value(dev_scsi
->devpath
, "rev");
311 info("%s: cannot get SCSI revision attribute", dev_scsi
->kernel
);
314 set_str(revision_str
, scsi_rev
, sizeof(revision_str
)-1);
317 * some broken devices have the same identifiers
318 * for all luns, export the target:lun number
320 sprintf(instance_str
, "%d:%d", target
, lun
);
324 /* Fallback to USB vendor & device */
325 if (vendor_str
[0] == '\0') {
327 if (!(usb_vendor
= sysfs_attr_get_value(dev_usb
->devpath
, "manufacturer")))
328 dbg("No USB vendor string found, using idVendor");
331 if (!(usb_vendor
= sysfs_attr_get_value(dev_usb
->devpath
, "idVendor"))) {
332 dbg("No USB vendor information available\n");
333 sprintf(vendor_str
,"0000");
336 set_str(vendor_str
,usb_vendor
, sizeof(vendor_str
) - 1);
339 if (model_str
[0] == '\0') {
341 if (!(usb_model
= sysfs_attr_get_value(dev_usb
->devpath
, "product")))
342 dbg("No USB model string found, using idProduct");
345 if (!(usb_model
= sysfs_attr_get_value(dev_usb
->devpath
, "idProduct")))
346 dbg("No USB model information available\n"); sprintf(model_str
,"0000");
348 set_str(model_str
, usb_model
, sizeof(model_str
) - 1);
351 if (revision_str
[0] == '\0') {
352 usb_rev
= sysfs_attr_get_value(dev_usb
->devpath
, "bcdDevice");
354 set_str(revision_str
, usb_rev
, sizeof(revision_str
)-1);
357 if (serial_str
[0] == '\0') {
358 usb_serial
= sysfs_attr_get_value(dev_usb
->devpath
, "serial");
360 set_str(serial_str
, usb_serial
, sizeof(serial_str
)-1);
365 int main(int argc
, char **argv
)
369 char devpath
[MAX_PATH_LEN
];
371 static const struct option options
[] = {
372 { "usb-info", 0, NULL
, 'u' },
373 { "num-info", 0, NULL
, 'n' },
374 { "export", 0, NULL
, 'x' },
375 { "help", 0, NULL
, 'h' },
379 logging_init("usb_id");
385 option
= getopt_long(argc
, argv
, "nuxh", options
, NULL
);
401 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
402 " --usb-info use usb strings instead\n"
403 " --num-info use numerical values\n"
404 " --export print values as environemt keys\n"
405 " --help print this help text\n\n");
412 env
= getenv("DEVPATH");
414 strlcpy(devpath
, env
, sizeof(devpath
));
416 if (argv
[optind
] == NULL
) {
417 fprintf(stderr
, "No device specified\n");
421 strlcpy(devpath
, argv
[optind
], sizeof(devpath
));
424 retval
= usb_id(devpath
);
429 strlcpy(serial
, vendor_str
, sizeof(serial
));
430 strlcat(serial
, "_", sizeof(serial
));
431 strlcat(serial
, model_str
, sizeof(serial
));
432 if (serial_str
[0] != '\0') {
433 strlcat(serial
, "_", sizeof(serial
));
434 strlcat(serial
, serial_str
, sizeof(serial
));
436 if (instance_str
[0] != '\0') {
437 strlcat(serial
, "-", sizeof(serial
));
438 strlcat(serial
, instance_str
, sizeof(serial
));
442 printf("ID_VENDOR=%s\n", vendor_str
);
443 printf("ID_MODEL=%s\n", model_str
);
444 printf("ID_REVISION=%s\n", revision_str
);
445 printf("ID_SERIAL=%s\n", serial
);
446 if (serial_str
[0] != '\0')
447 printf("ID_SERIAL_SHORT=%s\n", serial_str
);
448 printf("ID_TYPE=%s\n", type_str
);
449 if (instance_str
[0] != '\0')
450 printf("ID_INSTANCE=%s\n", instance_str
);
451 printf("ID_BUS=usb\n");
453 printf("%s\n", serial
);