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 serial_str[UTIL_NAME_SIZE];
42 static char revision_str[64];
43 static char type_str[64];
44 static char instance_str[64];
46 static int use_usb_info;
47 static int use_num_info;
49 static void set_usb_iftype(char *to, int if_class_num, size_t len)
51 char *type = "generic";
53 switch (if_class_num) {
66 case 2: /* CDC-Control */
67 case 5: /* Physical */
70 case 0x0a: /* CDC-Data */
71 case 0x0b: /* Chip/Smart Card */
72 case 0x0d: /* Content Security */
76 case 0xdc: /* Diagnostic Device */
77 case 0xe0: /* Wireless Controller */
78 case 0xf2: /* Application-specific */
79 case 0xff: /* Vendor-specific */
84 strncpy(to, type, len);
88 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
92 char *type = "generic";
94 type_num = strtoul(from, &eptr, 0);
104 case 5: /* SFF-8070i */
107 case 1: /* RBC devices */
110 case 6: /* Transparent SPC-2 devices */
117 util_strlcpy(to, type, len);
122 static void set_scsi_type(char *to, const char *from, size_t len)
126 char *type = "generic";
128 type_num = strtoul(from, &eptr, 0);
150 util_strlcpy(to, type, len);
154 * A unique USB identification is generated like this:
156 * 1.) Get the USB device type from DeviceClass, InterfaceClass
157 * and InterfaceSubClass
158 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
159 * use the SCSI vendor and model as USB-Vendor and USB-model.
160 * 3.) Otherwise use the USB manufacturer and product as
161 * USB-Vendor and USB-model. Any non-printable characters
162 * in those strings will be skipped; a slash '/' will be converted
163 * into a full stop '.'.
164 * 4.) If that fails, too, we will use idVendor and idProduct
165 * as USB-Vendor and USB-model.
166 * 5.) The USB identification is the USB-vendor and USB-model
167 * string concatenated with an underscore '_'.
168 * 6.) If the device supplies a serial number, this number
169 * is concatenated with the identification with an underscore '_'.
171 static int usb_id(struct udev_device *dev)
173 struct udev *udev = udev_device_get_udev(dev);
174 struct udev_device *dev_interface;
175 struct udev_device *dev_usb;
176 const char *if_class, *if_subclass;
180 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
182 /* usb interface directory */
183 dev_interface = udev_device_get_parent_with_subsystem(dev, "usb");
184 if (dev_interface == NULL) {
185 info(udev, "unable to access usb_interface device of '%s'\n",
186 udev_device_get_syspath(dev));
190 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
192 info(udev, "%s: cannot get bInterfaceClass attribute\n",
193 udev_device_get_sysname(dev));
196 if_class_num = strtoul(if_class, NULL, 16);
197 if (if_class_num == 8) {
198 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
199 if (if_subclass != NULL)
200 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
202 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
205 info(udev, "%s: if_class %d protocol %d\n",
206 udev_device_get_syspath(dev_interface), if_class_num, protocol);
208 /* usb device directory */
209 dev_usb = udev_device_get_parent_with_subsystem(dev_interface, "usb");
211 info(udev, "unable to find parent 'usb' device of '%s'\n",
212 udev_device_get_syspath(dev));
217 if (protocol == 6 && !use_usb_info) {
218 struct udev_device *dev_scsi;
219 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
220 int host, bus, target, lun;
222 /* get scsi device */
223 dev_scsi = udev_device_get_parent_with_subsystem(dev, "scsi");
224 if (dev_scsi == NULL) {
225 info(udev, "unable to find parent 'scsi' device of '%s'\n",
226 udev_device_get_syspath(dev));
229 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
230 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
234 /* Generic SPC-2 device */
235 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
237 info(udev, "%s: cannot get SCSI vendor attribute\n",
238 udev_device_get_sysname(dev_scsi));
241 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
243 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
245 info(udev, "%s: cannot get SCSI model attribute\n",
246 udev_device_get_sysname(dev_scsi));
249 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
251 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
253 info(udev, "%s: cannot get SCSI type attribute\n",
254 udev_device_get_sysname(dev_scsi));
257 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
259 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
261 info(udev, "%s: cannot get SCSI revision attribute\n",
262 udev_device_get_sysname(dev_scsi));
265 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
268 * some broken devices have the same identifiers
269 * for all luns, export the target:lun number
271 sprintf(instance_str, "%d:%d", target, lun);
275 /* fallback to USB vendor & device */
276 if (vendor_str[0] == '\0') {
277 const char *usb_vendor = NULL;
280 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
283 usb_vendor = udev_device_get_sysattr_value(dev_usb, "idVendor");
286 info(udev, "No USB vendor information available\n");
289 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
292 if (model_str[0] == '\0') {
293 const char *usb_model = NULL;
296 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
299 usb_model = udev_device_get_sysattr_value(dev_usb, "idProduct");
302 dbg(udev, "No USB model information available\n");
305 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
308 if (revision_str[0] == '\0') {
311 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
313 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
316 if (serial_str[0] == '\0') {
317 const char *usb_serial;
319 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
321 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
326 int main(int argc, char **argv)
328 static const struct option options[] = {
329 { "usb-info", no_argument, NULL, 'u' },
330 { "num-info", no_argument, NULL, 'n' },
331 { "export", no_argument, NULL, 'x' },
332 { "debug", no_argument, NULL, 'd' },
333 { "help", no_argument, NULL, 'h' },
337 struct udev_device *dev = NULL;
338 char syspath[UTIL_PATH_SIZE];
347 logging_init("usb_id");
348 udev_set_log_fn(udev, log_fn);
353 option = getopt_long(argc, argv, "dnuxh", options, NULL);
360 if (udev_get_log_priority(udev) < LOG_INFO)
361 udev_set_log_priority(udev, LOG_INFO);
374 printf("Usage: usb_id [--usb-info] [--num-info] [--export] [--help] <devpath>\n"
375 " --usb-info use usb strings instead\n"
376 " --num-info use numerical values\n"
377 " --export print values as environemt keys\n"
378 " --help print this help text\n\n");
385 devpath = getenv("DEVPATH");
387 devpath = argv[optind];
388 if (devpath == NULL) {
389 fprintf(stderr, "No device specified\n");
394 util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
395 util_strlcat(syspath, devpath, sizeof(syspath));
396 dev = udev_device_new_from_syspath(udev, syspath);
398 err(udev, "unable to access '%s'\n", devpath);
402 retval = usb_id(dev);
406 util_strlcpy(serial, vendor_str, sizeof(serial));
407 util_strlcat(serial, "_", sizeof(serial));
408 util_strlcat(serial, model_str, sizeof(serial));
409 if (serial_str[0] != '\0') {
410 util_strlcat(serial, "_", sizeof(serial));
411 util_strlcat(serial, serial_str, sizeof(serial));
413 if (instance_str[0] != '\0') {
414 util_strlcat(serial, "-", sizeof(serial));
415 util_strlcat(serial, instance_str, sizeof(serial));
419 printf("ID_VENDOR=%s\n", vendor_str);
420 printf("ID_MODEL=%s\n", model_str);
421 printf("ID_REVISION=%s\n", revision_str);
422 printf("ID_SERIAL=%s\n", serial);
423 if (serial_str[0] != '\0')
424 printf("ID_SERIAL_SHORT=%s\n", serial_str);
425 printf("ID_TYPE=%s\n", type_str);
426 if (instance_str[0] != '\0')
427 printf("ID_INSTANCE=%s\n", instance_str);
428 printf("ID_BUS=usb\n");
430 printf("%s\n", serial);
434 udev_device_unref(dev);