2 * USB device properties and persistent device path
4 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
5 * Author: Hannes Reinecke <hare@suse.de>
7 * Copyright (C) 2005-2011 Kay Sievers <kay@vrfy.org>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 static void set_usb_iftype(char *to, int if_class_num, size_t len) {
35 const char *type = "generic";
37 switch (if_class_num) {
41 case 2: /* CDC-Control */
46 case 5: /* Physical */
60 case 0x0a: /* CDC-Data */
62 case 0x0b: /* Chip/Smart Card */
64 case 0x0d: /* Content Security */
69 case 0xdc: /* Diagnostic Device */
71 case 0xe0: /* Wireless Controller */
73 case 0xfe: /* Application-specific */
75 case 0xff: /* Vendor-specific */
80 strncpy(to, type, len);
84 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len) {
87 const char *type = "generic";
89 type_num = strtoul(from, &eptr, 0);
92 case 1: /* RBC devices */
104 case 6: /* Transparent SPC-2 devices */
111 strscpy(to, len, type);
115 static void set_scsi_type(char *to, const char *from, size_t len) {
118 const char *type = "generic";
120 type_num = strtoul(from, &eptr, 0);
142 strscpy(to, len, type);
145 #define USB_DT_DEVICE 0x01
146 #define USB_DT_INTERFACE 0x04
148 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len) {
149 _cleanup_free_ char *filename = NULL;
150 _cleanup_close_ int fd = -1;
152 unsigned char buf[18 + 65535];
155 struct usb_interface_descriptor {
157 uint8_t bDescriptorType;
158 uint8_t bInterfaceNumber;
159 uint8_t bAlternateSetting;
160 uint8_t bNumEndpoints;
161 uint8_t bInterfaceClass;
162 uint8_t bInterfaceSubClass;
163 uint8_t bInterfaceProtocol;
167 if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0)
170 fd = open(filename, O_RDONLY|O_CLOEXEC);
172 fprintf(stderr, "error opening USB device 'descriptors' file\n");
176 size = read(fd, buf, sizeof(buf));
177 if (size < 18 || size == sizeof(buf))
181 while (pos + sizeof(struct usb_interface_descriptor) < (size_t) size &&
182 strpos + 7 < len - 2) {
184 struct usb_interface_descriptor *desc;
187 desc = (struct usb_interface_descriptor *) &buf[pos];
188 if (desc->bLength < 3)
190 pos += desc->bLength;
192 if (desc->bDescriptorType != USB_DT_INTERFACE)
195 if (snprintf(if_str, 8, ":%02x%02x%02x",
196 desc->bInterfaceClass,
197 desc->bInterfaceSubClass,
198 desc->bInterfaceProtocol) != 7)
201 if (strstr(ifs_str, if_str) != NULL)
204 memcpy(&ifs_str[strpos], if_str, 8),
209 ifs_str[strpos++] = ':';
210 ifs_str[strpos++] = '\0';
217 * A unique USB identification is generated like this:
219 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
220 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC',
221 * use the SCSI vendor and model as USB-Vendor and USB-model.
222 * 3.) Otherwise, use the USB manufacturer and product as
223 * USB-Vendor and USB-model. Any non-printable characters
224 * in those strings will be skipped; a slash '/' will be converted
225 * into a full stop '.'.
226 * 4.) If that fails, too, we will use idVendor and idProduct
227 * as USB-Vendor and USB-model.
228 * 5.) The USB identification is the USB-vendor and USB-model
229 * string concatenated with an underscore '_'.
230 * 6.) If the device supplies a serial number, this number
231 * is concatenated with the identification with an underscore '_'.
233 static int builtin_usb_id(struct udev_device *dev, int argc, char *argv[], bool test) {
235 char vendor_str_enc[256];
236 const char *vendor_id;
238 char model_str_enc[256];
239 const char *product_id;
240 char serial_str[UTIL_NAME_SIZE];
241 char packed_if_str[UTIL_NAME_SIZE];
242 char revision_str[64];
244 char instance_str[64];
245 const char *ifnum = NULL;
246 const char *driver = NULL;
249 struct udev_device *dev_interface = NULL;
250 struct udev_device *dev_usb = NULL;
251 const char *if_class, *if_subclass;
257 vendor_str[0] = '\0';
259 serial_str[0] = '\0';
260 packed_if_str[0] = '\0';
261 revision_str[0] = '\0';
263 instance_str[0] = '\0';
265 /* shortcut, if we are called directly for a "usb_device" type */
266 if (udev_device_get_devtype(dev) != NULL && streq(udev_device_get_devtype(dev), "usb_device")) {
267 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
272 /* usb interface directory */
273 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
274 if (dev_interface == NULL) {
275 log_debug("unable to access usb_interface device of '%s'",
276 udev_device_get_syspath(dev));
280 ifnum = udev_device_get_sysattr_value(dev_interface, "bInterfaceNumber");
281 driver = udev_device_get_sysattr_value(dev_interface, "driver");
283 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
285 log_debug("%s: cannot get bInterfaceClass attribute",
286 udev_device_get_sysname(dev));
290 if_class_num = strtoul(if_class, NULL, 16);
291 if (if_class_num == 8) {
293 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
294 if (if_subclass != NULL)
295 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
297 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
300 log_debug("%s: if_class %d protocol %d",
301 udev_device_get_syspath(dev_interface), if_class_num, protocol);
303 /* usb device directory */
304 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
306 log_debug("unable to find parent 'usb' device of '%s'",
307 udev_device_get_syspath(dev));
311 /* all interfaces of the device in a single string */
312 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
314 /* mass storage : SCSI or ATAPI */
315 if ((protocol == 6 || protocol == 2)) {
316 struct udev_device *dev_scsi;
317 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
318 int host, bus, target, lun;
320 /* get scsi device */
321 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
322 if (dev_scsi == NULL) {
323 log_debug("unable to find parent 'scsi' device of '%s'",
324 udev_device_get_syspath(dev));
327 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
328 log_debug("invalid scsi device '%s'", udev_device_get_sysname(dev_scsi));
332 /* Generic SPC-2 device */
333 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
335 log_debug("%s: cannot get SCSI vendor attribute",
336 udev_device_get_sysname(dev_scsi));
339 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
340 util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
341 util_replace_chars(vendor_str, NULL);
343 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
345 log_debug("%s: cannot get SCSI model attribute",
346 udev_device_get_sysname(dev_scsi));
349 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
350 util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
351 util_replace_chars(model_str, NULL);
353 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
355 log_debug("%s: cannot get SCSI type attribute",
356 udev_device_get_sysname(dev_scsi));
359 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
361 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
363 log_debug("%s: cannot get SCSI revision attribute",
364 udev_device_get_sysname(dev_scsi));
367 util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
368 util_replace_chars(revision_str, NULL);
371 * some broken devices have the same identifiers
372 * for all luns, export the target:lun number
374 sprintf(instance_str, "%d:%d", target, lun);
378 vendor_id = udev_device_get_sysattr_value(dev_usb, "idVendor");
379 product_id = udev_device_get_sysattr_value(dev_usb, "idProduct");
381 /* fallback to USB vendor & device */
382 if (vendor_str[0] == '\0') {
383 const char *usb_vendor = NULL;
385 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
387 usb_vendor = vendor_id;
389 log_debug("No USB vendor information available");
392 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
393 util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
394 util_replace_chars(vendor_str, NULL);
397 if (model_str[0] == '\0') {
398 const char *usb_model = NULL;
400 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
402 usb_model = product_id;
405 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
406 util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
407 util_replace_chars(model_str, NULL);
410 if (revision_str[0] == '\0') {
413 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
415 util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
416 util_replace_chars(revision_str, NULL);
420 if (serial_str[0] == '\0') {
421 const char *usb_serial;
423 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
425 const unsigned char *p;
427 /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
428 for (p = (unsigned char *)usb_serial; *p != '\0'; p++)
429 if (*p < 0x20 || *p > 0x7f || *p == ',') {
436 util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
437 util_replace_chars(serial_str, NULL);
442 l = strpcpyl(&s, sizeof(serial), vendor_str, "_", model_str, NULL);
443 if (serial_str[0] != '\0')
444 l = strpcpyl(&s, l, "_", serial_str, NULL);
446 if (instance_str[0] != '\0')
447 strpcpyl(&s, l, "-", instance_str, NULL);
449 udev_builtin_add_property(dev, test, "ID_VENDOR", vendor_str);
450 udev_builtin_add_property(dev, test, "ID_VENDOR_ENC", vendor_str_enc);
451 udev_builtin_add_property(dev, test, "ID_VENDOR_ID", vendor_id);
452 udev_builtin_add_property(dev, test, "ID_MODEL", model_str);
453 udev_builtin_add_property(dev, test, "ID_MODEL_ENC", model_str_enc);
454 udev_builtin_add_property(dev, test, "ID_MODEL_ID", product_id);
455 udev_builtin_add_property(dev, test, "ID_REVISION", revision_str);
456 udev_builtin_add_property(dev, test, "ID_SERIAL", serial);
457 if (serial_str[0] != '\0')
458 udev_builtin_add_property(dev, test, "ID_SERIAL_SHORT", serial_str);
459 if (type_str[0] != '\0')
460 udev_builtin_add_property(dev, test, "ID_TYPE", type_str);
461 if (instance_str[0] != '\0')
462 udev_builtin_add_property(dev, test, "ID_INSTANCE", instance_str);
463 udev_builtin_add_property(dev, test, "ID_BUS", "usb");
464 if (packed_if_str[0] != '\0')
465 udev_builtin_add_property(dev, test, "ID_USB_INTERFACES", packed_if_str);
467 udev_builtin_add_property(dev, test, "ID_USB_INTERFACE_NUM", ifnum);
469 udev_builtin_add_property(dev, test, "ID_USB_DRIVER", driver);
473 const struct udev_builtin udev_builtin_usb_id = {
475 .cmd = builtin_usb_id,
476 .help = "USB device properties",