2 * usb_id - identify an USB device
4 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
7 * Hannes Reinecke <hare@suse.de>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
23 #include "../../udev/udev.h"
27 static void log_fn(struct udev *udev, int priority,
28 const char *file, int line, const char *fn,
29 const char *format, va_list args)
32 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
33 vfprintf(stderr, format, args);
35 vsyslog(priority, format, args);
39 static char vendor_str[64];
40 static char model_str[64];
41 static char model_str_enc[256];
42 static char vendor_str_enc[256];
43 static char serial_str[UTIL_NAME_SIZE];
44 static char revision_str[64];
45 static char type_str[64];
46 static char instance_str[64];
48 static int use_usb_info;
49 static int use_num_info;
51 static void set_usb_iftype(char *to, int if_class_num, size_t len)
53 char *type = "generic";
55 switch (if_class_num) {
59 case 2: /* CDC-Control */
64 case 5: /* Physical */
78 case 0x0a: /* CDC-Data */
80 case 0x0b: /* Chip/Smart Card */
82 case 0x0d: /* Content Security */
87 case 0xdc: /* Diagnostic Device */
89 case 0xe0: /* Wireless Controller */
91 case 0xfe: /* Application-specific */
93 case 0xff: /* Vendor-specific */
98 strncpy(to, type, len);
102 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
106 char *type = "generic";
108 type_num = strtoul(from, &eptr, 0);
118 case 5: /* SFF-8070i */
121 case 1: /* RBC devices */
124 case 6: /* Transparent SPC-2 devices */
131 util_strlcpy(to, type, len);
135 static void set_scsi_type(char *to, const char *from, size_t len)
139 char *type = "generic";
141 type_num = strtoul(from, &eptr, 0);
163 util_strlcpy(to, type, len);
167 * A unique USB identification is generated like this:
169 * 1.) Get the USB device type from DeviceClass, InterfaceClass
170 * and InterfaceSubClass
171 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
172 * use the SCSI vendor and model as USB-Vendor and USB-model.
173 * 3.) Otherwise use the USB manufacturer and product as
174 * USB-Vendor and USB-model. Any non-printable characters
175 * in those strings will be skipped; a slash '/' will be converted
176 * into a full stop '.'.
177 * 4.) If that fails, too, we will use idVendor and idProduct
178 * as USB-Vendor and USB-model.
179 * 5.) The USB identification is the USB-vendor and USB-model
180 * string concatenated with an underscore '_'.
181 * 6.) If the device supplies a serial number, this number
182 * is concatenated with the identification with an underscore '_'.
184 static int usb_id(struct udev_device *dev)
186 struct udev *udev = udev_device_get_udev(dev);
187 struct udev_device *dev_interface;
188 struct udev_device *dev_usb;
189 const char *if_class, *if_subclass;
193 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
195 /* usb interface directory */
196 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
197 if (dev_interface == NULL) {
198 info(udev, "unable to access usb_interface device of '%s'\n",
199 udev_device_get_syspath(dev));
203 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
205 info(udev, "%s: cannot get bInterfaceClass attribute\n",
206 udev_device_get_sysname(dev));
210 if_class_num = strtoul(if_class, NULL, 16);
211 if (if_class_num == 8) {
213 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
214 if (if_subclass != NULL)
215 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
217 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
220 info(udev, "%s: if_class %d protocol %d\n",
221 udev_device_get_syspath(dev_interface), if_class_num, protocol);
223 /* usb device directory */
224 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
226 info(udev, "unable to find parent 'usb' device of '%s'\n",
227 udev_device_get_syspath(dev));
231 /* mass storage : SCSI or ATAPI */
232 if ((protocol == 6 || protocol == 2) && !use_usb_info) {
233 struct udev_device *dev_scsi;
234 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
235 int host, bus, target, lun;
237 /* get scsi device */
238 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
239 if (dev_scsi == NULL) {
240 info(udev, "unable to find parent 'scsi' device of '%s'\n",
241 udev_device_get_syspath(dev));
244 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
245 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
249 /* Generic SPC-2 device */
250 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
252 info(udev, "%s: cannot get SCSI vendor attribute\n",
253 udev_device_get_sysname(dev_scsi));
256 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
257 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
258 udev_util_replace_chars(vendor_str, NULL);
260 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
262 info(udev, "%s: cannot get SCSI model attribute\n",
263 udev_device_get_sysname(dev_scsi));
266 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
267 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
268 udev_util_replace_chars(model_str, NULL);
270 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
272 info(udev, "%s: cannot get SCSI type attribute\n",
273 udev_device_get_sysname(dev_scsi));
276 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
278 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
280 info(udev, "%s: cannot get SCSI revision attribute\n",
281 udev_device_get_sysname(dev_scsi));
284 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
285 udev_util_replace_chars(revision_str, NULL);
288 * some broken devices have the same identifiers
289 * for all luns, export the target:lun number
291 sprintf(instance_str, "%d:%d", target, lun);
295 /* fallback to USB vendor & device */
296 if (vendor_str[0] == '\0') {
297 const char *usb_vendor = NULL;
300 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
303 usb_vendor = udev_device_get_sysattr_value(dev_usb, "idVendor");
306 info(udev, "No USB vendor information available\n");
309 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
310 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
311 udev_util_replace_chars(vendor_str, NULL);
314 if (model_str[0] == '\0') {
315 const char *usb_model = NULL;
318 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
321 usb_model = udev_device_get_sysattr_value(dev_usb, "idProduct");
324 dbg(udev, "No USB model information available\n");
327 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
328 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
329 udev_util_replace_chars(model_str, NULL);
332 if (revision_str[0] == '\0') {
335 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
337 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
338 udev_util_replace_chars(revision_str, NULL);
342 if (serial_str[0] == '\0') {
343 const char *usb_serial;
345 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
347 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
348 udev_util_replace_chars(serial_str, NULL);
354 int main(int argc, char **argv)
356 static const struct option options[] = {
357 { "usb-info", no_argument, NULL, 'u' },
358 { "num-info", no_argument, NULL, 'n' },
359 { "export", no_argument, NULL, 'x' },
360 { "debug", no_argument, NULL, 'd' },
361 { "help", no_argument, NULL, 'h' },
365 struct udev_device *dev = NULL;
366 char syspath[UTIL_PATH_SIZE];
375 logging_init("usb_id");
376 udev_set_log_fn(udev, log_fn);
381 option = getopt_long(argc, argv, "dnuxh", options, NULL);
388 if (udev_get_log_priority(udev) < LOG_INFO)
389 udev_set_log_priority(udev, LOG_INFO);
402 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
403 " --usb-info use usb strings instead\n"
404 " --num-info use numerical values\n"
405 " --export print values as environment keys\n"
406 " --help print this help text\n\n");
413 devpath = getenv("DEVPATH");
415 devpath = argv[optind];
416 if (devpath == NULL) {
417 fprintf(stderr, "No device specified\n");
422 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
423 util_strlcat(syspath, devpath, sizeof(syspath));
424 dev = udev_device_new_from_syspath(udev, syspath);
426 err(udev, "unable to access '%s'\n", devpath);
430 retval = usb_id(dev);
434 util_strlcpy(serial, vendor_str, sizeof(serial));
435 util_strlcat(serial, "_", sizeof(serial));
436 util_strlcat(serial, model_str, sizeof(serial));
437 if (serial_str[0] != '\0') {
438 util_strlcat(serial, "_", sizeof(serial));
439 util_strlcat(serial, serial_str, sizeof(serial));
441 if (instance_str[0] != '\0') {
442 util_strlcat(serial, "-", sizeof(serial));
443 util_strlcat(serial, instance_str, sizeof(serial));
447 printf("ID_VENDOR=%s\n", vendor_str);
448 printf("ID_VENDOR_ENC=%s\n", vendor_str_enc);
449 printf("ID_MODEL=%s\n", model_str);
450 printf("ID_MODEL_ENC=%s\n", model_str_enc);
451 printf("ID_REVISION=%s\n", revision_str);
452 printf("ID_SERIAL=%s\n", serial);
453 if (serial_str[0] != '\0')
454 printf("ID_SERIAL_SHORT=%s\n", serial_str);
455 printf("ID_TYPE=%s\n", type_str);
456 if (instance_str[0] != '\0')
457 printf("ID_INSTANCE=%s\n", instance_str);
458 printf("ID_BUS=usb\n");
460 printf("%s\n", serial);
464 udev_device_unref(dev);