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.sievers@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 char vendor_str[64];
35 static char vendor_str_enc[256];
36 static const char *vendor_id = "";
37 static char model_str[64];
38 static char model_str_enc[256];
39 static const char *product_id = "";
40 static char serial_str[UTIL_NAME_SIZE];
41 static char packed_if_str[UTIL_NAME_SIZE];
42 static char revision_str[64];
43 static char type_str[64];
44 static char instance_str[64];
45 static const char *ifnum;
46 static const char *driver;
48 static void set_usb_iftype(char *to, int if_class_num, size_t len)
50 char *type = "generic";
52 switch (if_class_num) {
56 case 2: /* CDC-Control */
61 case 5: /* Physical */
75 case 0x0a: /* CDC-Data */
77 case 0x0b: /* Chip/Smart Card */
79 case 0x0d: /* Content Security */
84 case 0xdc: /* Diagnostic Device */
86 case 0xe0: /* Wireless Controller */
88 case 0xfe: /* Application-specific */
90 case 0xff: /* Vendor-specific */
95 strncpy(to, type, len);
99 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len)
103 char *type = "generic";
105 type_num = strtoul(from, &eptr, 0);
115 case 5: /* SFF-8070i */
118 case 1: /* RBC devices */
121 case 6: /* Transparent SPC-2 devices */
128 util_strscpy(to, len, type);
132 static void set_scsi_type(char *to, const char *from, size_t len)
136 char *type = "generic";
138 type_num = strtoul(from, &eptr, 0);
160 util_strscpy(to, len, type);
163 #define USB_DT_DEVICE 0x01
164 #define USB_DT_INTERFACE 0x04
166 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len)
168 char *filename = NULL;
171 unsigned char buf[18 + 65535];
172 unsigned int pos, strpos;
173 struct usb_interface_descriptor {
175 u_int8_t bDescriptorType;
176 u_int8_t bInterfaceNumber;
177 u_int8_t bAlternateSetting;
178 u_int8_t bNumEndpoints;
179 u_int8_t bInterfaceClass;
180 u_int8_t bInterfaceSubClass;
181 u_int8_t bInterfaceProtocol;
183 } __attribute__((packed));
186 if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0) {
190 fd = open(filename, O_RDONLY|O_CLOEXEC);
192 fprintf(stderr, "error opening USB device 'descriptors' file\n");
196 size = read(fd, buf, sizeof(buf));
198 if (size < 18 || size == sizeof(buf)) {
206 while (pos < sizeof(buf) && strpos+7 < len-2) {
207 struct usb_interface_descriptor *desc;
210 desc = (struct usb_interface_descriptor *) &buf[pos];
211 if (desc->bLength < 3)
213 pos += desc->bLength;
215 if (desc->bDescriptorType != USB_DT_INTERFACE)
218 if (snprintf(if_str, 8, ":%02x%02x%02x",
219 desc->bInterfaceClass,
220 desc->bInterfaceSubClass,
221 desc->bInterfaceProtocol) != 7)
224 if (strstr(ifs_str, if_str) != NULL)
227 memcpy(&ifs_str[strpos], if_str, 8),
231 ifs_str[strpos++] = ':';
232 ifs_str[strpos++] = '\0';
240 * A unique USB identification is generated like this:
242 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
243 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
244 * use the SCSI vendor and model as USB-Vendor and USB-model.
245 * 3.) Otherwise use the USB manufacturer and product as
246 * USB-Vendor and USB-model. Any non-printable characters
247 * in those strings will be skipped; a slash '/' will be converted
248 * into a full stop '.'.
249 * 4.) If that fails, too, we will use idVendor and idProduct
250 * as USB-Vendor and USB-model.
251 * 5.) The USB identification is the USB-vendor and USB-model
252 * string concatenated with an underscore '_'.
253 * 6.) If the device supplies a serial number, this number
254 * is concatenated with the identification with an underscore '_'.
256 static int usb_id(struct udev_device *dev)
258 struct udev *udev = udev_device_get_udev(dev);
259 struct udev_device *dev_interface = NULL;
260 struct udev_device *dev_usb = NULL;
261 const char *if_class, *if_subclass;
265 dbg(udev, "syspath %s\n", udev_device_get_syspath(dev));
267 /* shortcut, if we are called directly for a "usb_device" type */
268 if (udev_device_get_devtype(dev) != NULL && strcmp(udev_device_get_devtype(dev), "usb_device") == 0) {
269 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
274 /* usb interface directory */
275 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
276 if (dev_interface == NULL) {
277 info(udev, "unable to access usb_interface device of '%s'\n",
278 udev_device_get_syspath(dev));
282 ifnum = udev_device_get_sysattr_value(dev_interface, "bInterfaceNumber");
283 driver = udev_device_get_sysattr_value(dev_interface, "driver");
285 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
287 info(udev, "%s: cannot get bInterfaceClass attribute\n",
288 udev_device_get_sysname(dev));
292 if_class_num = strtoul(if_class, NULL, 16);
293 if (if_class_num == 8) {
295 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
296 if (if_subclass != NULL)
297 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
299 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
302 info(udev, "%s: if_class %d protocol %d\n",
303 udev_device_get_syspath(dev_interface), if_class_num, protocol);
305 /* usb device directory */
306 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
308 info(udev, "unable to find parent 'usb' device of '%s'\n",
309 udev_device_get_syspath(dev));
313 /* all interfaces of the device in a single string */
314 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
316 /* mass storage : SCSI or ATAPI */
317 if ((protocol == 6 || protocol == 2)) {
318 struct udev_device *dev_scsi;
319 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
320 int host, bus, target, lun;
322 /* get scsi device */
323 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
324 if (dev_scsi == NULL) {
325 info(udev, "unable to find parent 'scsi' device of '%s'\n",
326 udev_device_get_syspath(dev));
329 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
330 info(udev, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi));
334 /* Generic SPC-2 device */
335 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
337 info(udev, "%s: cannot get SCSI vendor attribute\n",
338 udev_device_get_sysname(dev_scsi));
341 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
342 udev_util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
343 udev_util_replace_chars(vendor_str, NULL);
345 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
347 info(udev, "%s: cannot get SCSI model attribute\n",
348 udev_device_get_sysname(dev_scsi));
351 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
352 udev_util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
353 udev_util_replace_chars(model_str, NULL);
355 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
357 info(udev, "%s: cannot get SCSI type attribute\n",
358 udev_device_get_sysname(dev_scsi));
361 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
363 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
365 info(udev, "%s: cannot get SCSI revision attribute\n",
366 udev_device_get_sysname(dev_scsi));
369 udev_util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
370 udev_util_replace_chars(revision_str, NULL);
373 * some broken devices have the same identifiers
374 * for all luns, export the target:lun number
376 sprintf(instance_str, "%d:%d", target, lun);
380 vendor_id = udev_device_get_sysattr_value(dev_usb, "idVendor");
381 product_id = udev_device_get_sysattr_value(dev_usb, "idProduct");
383 /* fallback to USB vendor & device */
384 if (vendor_str[0] == '\0') {
385 const char *usb_vendor = NULL;
387 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
389 usb_vendor = vendor_id;
391 info(udev, "No USB vendor information available\n");
394 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
395 udev_util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
396 udev_util_replace_chars(vendor_str, NULL);
399 if (model_str[0] == '\0') {
400 const char *usb_model = NULL;
402 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
404 usb_model = product_id;
406 dbg(udev, "No USB model information available\n");
409 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
410 udev_util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
411 udev_util_replace_chars(model_str, NULL);
414 if (revision_str[0] == '\0') {
417 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
419 udev_util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
420 udev_util_replace_chars(revision_str, NULL);
424 if (serial_str[0] == '\0') {
425 const char *usb_serial;
427 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
429 udev_util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
430 udev_util_replace_chars(serial_str, NULL);
436 static int builtin_usb_id(struct udev_device *dev, bool test)
448 l = util_strpcpyl(&s, sizeof(serial), vendor_str, "_", model_str, NULL);
449 if (serial_str[0] != '\0')
450 l = util_strpcpyl(&s, l, "_", serial_str, NULL);
451 if (instance_str[0] != '\0')
452 util_strpcpyl(&s, l, "-", instance_str, NULL);
454 udev_builtin_add_property(dev, test, "ID_VENDOR", vendor_str);
455 udev_builtin_add_property(dev, test, "ID_VENDOR_ENC", vendor_str_enc);
456 udev_builtin_add_property(dev, test, "ID_VENDOR_ID", vendor_id);
457 udev_builtin_add_property(dev, test, "ID_MODEL", model_str);
458 udev_builtin_add_property(dev, test, "ID_MODEL_ENC", model_str_enc);
459 udev_builtin_add_property(dev, test, "ID_MODEL_ID", product_id);
460 udev_builtin_add_property(dev, test, "ID_REVISION", revision_str);
461 udev_builtin_add_property(dev, test, "ID_SERIAL", serial);
462 if (serial_str[0] != '\0')
463 udev_builtin_add_property(dev, test, "ID_SERIAL_SHORT", serial_str);
464 if (type_str[0] != '\0')
465 udev_builtin_add_property(dev, test, "ID_TYPE", type_str);
466 if (instance_str[0] != '\0')
467 udev_builtin_add_property(dev, test, "ID_INSTANCE", instance_str);
468 udev_builtin_add_property(dev, test, "ID_BUS", "usb");
469 if (packed_if_str[0] != '\0')
470 udev_builtin_add_property(dev, test, "ID_USB_INTERFACES", packed_if_str);
472 udev_builtin_add_property(dev, test, "ID_USB_INTERFACE_NUM", ifnum);
474 udev_builtin_add_property(dev, test, "ID_USB_DRIVER", driver);
478 const struct udev_builtin udev_builtin_usb_id = {
480 .cmd = builtin_usb_id,
481 .help = "usb device properties",