2 * usb_id - identify an USB device
4 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
6 * Author: Hannes Reinecke <hare@suse.de>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "libudev-private.h"
37 static void log_fn(struct udev *udev, int priority,
38 const char *file, int line, const char *fn,
39 const char *format, va_list args)
42 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
43 vfprintf(stderr, format, args);
45 vsyslog(priority, format, args);
49 static char vendor_str[64];
50 static char vendor_str_enc[256];
51 static const char *vendor_id = "";
52 static char model_str[64];
53 static char model_str_enc[256];
54 static const char *product_id = "";
55 static char serial_str[UTIL_NAME_SIZE];
56 static char packed_if_str[UTIL_NAME_SIZE];
57 static char revision_str[64];
58 static char type_str[64];
59 static char instance_str[64];
60 static const char *ifnum;
61 static const char *driver;
63 static int use_usb_info;
64 static int use_num_info;
66 static void set_usb_iftype(char *to, int if_class_num, size_t len)
68 char *type = "generic";
70 switch (if_class_num) {
74 case 2: /* CDC-Control */
79 case 5: /* Physical */
93 case 0x0a: /* CDC-Data */
95 case 0x0b: /* Chip/Smart Card */
97 case 0x0d: /* Content Security */
102 case 0xdc: /* Diagnostic Device */
104 case 0xe0: /* Wireless Controller */
106 case 0xfe: /* Application-specific */
108 case 0xff: /* Vendor-specific */
113 strncpy(to, type, len);
117 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
121 char *type = "generic";
123 type_num = strtoul(from, &eptr, 0);
133 case 5: /* SFF-8070i */
136 case 1: /* RBC devices */
139 case 6: /* Transparent SPC-2 devices */
146 util_strscpy(to, len, type);
150 static void set_scsi_type(char *to, const char *from, size_t len)
154 char *type = "generic";
156 type_num = strtoul(from, &eptr, 0);
178 util_strscpy(to, len, type);
181 #define USB_DT_DEVICE 0x01
182 #define USB_DT_INTERFACE 0x04
184 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len)
186 char *filename = NULL;
189 unsigned char buf[18 + 65535];
190 unsigned int pos, strpos;
191 struct usb_interface_descriptor {
193 u_int8_t bDescriptorType;
194 u_int8_t bInterfaceNumber;
195 u_int8_t bAlternateSetting;
196 u_int8_t bNumEndpoints;
197 u_int8_t bInterfaceClass;
198 u_int8_t bInterfaceSubClass;
199 u_int8_t bInterfaceProtocol;
201 } __attribute__((packed));
204 if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0) {
208 fd = open(filename, O_RDONLY);
210 fprintf(stderr, "error opening USB device 'descriptors' file\n");
214 size = read(fd, buf, sizeof(buf));
216 if (size < 18 || size == sizeof(buf)) {
223 while (pos < sizeof(buf) && strpos+7 < len) {
224 struct usb_interface_descriptor *desc;
227 desc = (struct usb_interface_descriptor *) &buf[pos];
228 if (desc->bLength < 3)
230 pos += desc->bLength;
232 if (desc->bDescriptorType != USB_DT_INTERFACE)
235 if (snprintf(if_str, 8, "%02x%02x%02x:",
236 desc->bInterfaceClass,
237 desc->bInterfaceSubClass,
238 desc->bInterfaceProtocol) != 7)
241 if (strstr(ifs_str, if_str) != NULL)
244 memcpy(&ifs_str[strpos], if_str, 8),
253 * A unique USB identification is generated like this:
255 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
256 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
257 * use the SCSI vendor and model as USB-Vendor and USB-model.
258 * 3.) Otherwise use the USB manufacturer and product as
259 * USB-Vendor and USB-model. Any non-printable characters
260 * in those strings will be skipped; a slash '/' will be converted
261 * into a full stop '.'.
262 * 4.) If that fails, too, we will use idVendor and idProduct
263 * as USB-Vendor and USB-model.
264 * 5.) The USB identification is the USB-vendor and USB-model
265 * string concatenated with an underscore '_'.
266 * 6.) If the device supplies a serial number, this number
267 * is concatenated with the identification with an underscore '_'.
269 static int usb_id(struct udev_device *dev)
271 struct udev *udev = udev_device_get_udev(dev);
272 struct udev_device *dev_interface = NULL;
273 struct udev_device *dev_usb = NULL;
274 const char *if_class, *if_subclass;
278 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
280 /* shortcut, if we are called directly for a "usb_device" type */
281 if (udev_device_get_devtype(dev) != NULL && strcmp(udev_device_get_devtype(dev), "usb_device") == 0) {
282 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
287 /* usb interface directory */
288 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
289 if (dev_interface == NULL) {
290 info(udev, "unable to access usb_interface device of '%s'\n",
291 udev_device_get_syspath(dev));
295 ifnum = udev_device_get_sysattr_value(dev_interface, "bInterfaceNumber");
296 driver = udev_device_get_sysattr_value(dev_interface, "driver");
298 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
300 info(udev, "%s: cannot get bInterfaceClass attribute\n",
301 udev_device_get_sysname(dev));
305 if_class_num = strtoul(if_class, NULL, 16);
306 if (if_class_num == 8) {
308 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
309 if (if_subclass != NULL)
310 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
312 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
315 info(udev, "%s: if_class %d protocol %d\n",
316 udev_device_get_syspath(dev_interface), if_class_num, protocol);
318 /* usb device directory */
319 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
321 info(udev, "unable to find parent 'usb' device of '%s'\n",
322 udev_device_get_syspath(dev));
326 /* all interfaces of the device in a single string */
327 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
329 /* mass storage : SCSI or ATAPI */
330 if ((protocol == 6 || protocol == 2) && !use_usb_info) {
331 struct udev_device *dev_scsi;
332 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
333 int host, bus, target, lun;
335 /* get scsi device */
336 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
337 if (dev_scsi == NULL) {
338 info(udev, "unable to find parent 'scsi' device of '%s'\n",
339 udev_device_get_syspath(dev));
342 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
343 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
347 /* Generic SPC-2 device */
348 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
350 info(udev, "%s: cannot get SCSI vendor attribute\n",
351 udev_device_get_sysname(dev_scsi));
354 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
355 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
356 udev_util_replace_chars(vendor_str, NULL);
358 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
360 info(udev, "%s: cannot get SCSI model attribute\n",
361 udev_device_get_sysname(dev_scsi));
364 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
365 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
366 udev_util_replace_chars(model_str, NULL);
368 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
370 info(udev, "%s: cannot get SCSI type attribute\n",
371 udev_device_get_sysname(dev_scsi));
374 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
376 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
378 info(udev, "%s: cannot get SCSI revision attribute\n",
379 udev_device_get_sysname(dev_scsi));
382 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
383 udev_util_replace_chars(revision_str, NULL);
386 * some broken devices have the same identifiers
387 * for all luns, export the target:lun number
389 sprintf(instance_str, "%d:%d", target, lun);
393 vendor_id = udev_device_get_sysattr_value(dev_usb, "idVendor");
394 product_id = udev_device_get_sysattr_value(dev_usb, "idProduct");
396 /* fallback to USB vendor & device */
397 if (vendor_str[0] == '\0') {
398 const char *usb_vendor = NULL;
401 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
404 usb_vendor = vendor_id;
407 info(udev, "No USB vendor information available\n");
410 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
411 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
412 udev_util_replace_chars(vendor_str, NULL);
415 if (model_str[0] == '\0') {
416 const char *usb_model = NULL;
419 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
422 usb_model = product_id;
425 dbg(udev, "No USB model information available\n");
428 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
429 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
430 udev_util_replace_chars(model_str, NULL);
433 if (revision_str[0] == '\0') {
436 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
438 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
439 udev_util_replace_chars(revision_str, NULL);
443 if (serial_str[0] == '\0') {
444 const char *usb_serial;
446 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
448 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
449 udev_util_replace_chars(serial_str, NULL);
455 int main(int argc, char **argv)
457 static const struct option options[] = {
458 { "usb-info", no_argument, NULL, 'u' },
459 { "num-info", no_argument, NULL, 'n' },
460 { "export", no_argument, NULL, 'x' },
461 { "debug", no_argument, NULL, 'd' },
462 { "help", no_argument, NULL, 'h' },
466 struct udev_device *dev = NULL;
474 udev_log_init("usb_id");
475 udev_set_log_fn(udev, log_fn);
480 option = getopt_long(argc, argv, "dnuxh", options, NULL);
487 if (udev_get_log_priority(udev) < LOG_INFO)
488 udev_set_log_priority(udev, LOG_INFO);
501 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] [<devpath>]\n"
502 " --usb-info use usb strings instead\n"
503 " --num-info use numerical values\n"
504 " --export print values as environment keys\n"
505 " --help print this help text\n\n");
512 dev = udev_device_new_from_environment(udev);
514 char syspath[UTIL_PATH_SIZE];
517 devpath = argv[optind];
518 if (devpath == NULL) {
519 fprintf(stderr, "missing device\n");
524 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
525 dev = udev_device_new_from_syspath(udev, syspath);
527 err(udev, "unable to access '%s'\n", devpath);
534 retval = usb_id(dev);
541 l = util_strpcpyl(&s, sizeof(serial), vendor_str, "_", model_str, NULL);
542 if (serial_str[0] != '\0')
543 l = util_strpcpyl(&s, l, "_", serial_str, NULL);
544 if (instance_str[0] != '\0')
545 util_strpcpyl(&s, l, "-", instance_str, NULL);
548 printf("ID_VENDOR=%s\n", vendor_str);
549 printf("ID_VENDOR_ENC=%s\n", vendor_str_enc);
550 printf("ID_VENDOR_ID=%s\n", vendor_id);
551 printf("ID_MODEL=%s\n", model_str);
552 printf("ID_MODEL_ENC=%s\n", model_str_enc);
553 printf("ID_MODEL_ID=%s\n", product_id);
554 printf("ID_REVISION=%s\n", revision_str);
555 printf("ID_SERIAL=%s\n", serial);
556 if (serial_str[0] != '\0')
557 printf("ID_SERIAL_SHORT=%s\n", serial_str);
558 if (type_str[0] != '\0')
559 printf("ID_TYPE=%s\n", type_str);
560 if (instance_str[0] != '\0')
561 printf("ID_INSTANCE=%s\n", instance_str);
562 printf("ID_BUS=usb\n");
563 if (packed_if_str[0] != '\0')
564 printf("ID_USB_INTERFACES=:%s\n", packed_if_str);
566 printf("ID_USB_INTERFACE_NUM=%s\n", ifnum);
568 printf("ID_USB_DRIVER=%s\n", driver);
570 printf("%s\n", serial);
574 udev_device_unref(dev);