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 void set_usb_iftype(char *to
, int if_class_num
, size_t len
)
36 char *type
= "generic";
38 switch (if_class_num
) {
42 case 2: /* CDC-Control */
47 case 5: /* Physical */
61 case 0x0a: /* CDC-Data */
63 case 0x0b: /* Chip/Smart Card */
65 case 0x0d: /* Content Security */
70 case 0xdc: /* Diagnostic Device */
72 case 0xe0: /* Wireless Controller */
74 case 0xfe: /* Application-specific */
76 case 0xff: /* Vendor-specific */
81 strncpy(to
, type
, len
);
85 static int set_usb_mass_storage_ifsubtype(char *to
, const char *from
, size_t len
)
89 char *type
= "generic";
91 type_num
= strtoul(from
, &eptr
, 0);
101 case 5: /* SFF-8070i */
104 case 1: /* RBC devices */
107 case 6: /* Transparent SPC-2 devices */
114 util_strscpy(to
, len
, type
);
118 static void set_scsi_type(char *to
, const char *from
, size_t len
)
122 char *type
= "generic";
124 type_num
= strtoul(from
, &eptr
, 0);
146 util_strscpy(to
, len
, type
);
149 #define USB_DT_DEVICE 0x01
150 #define USB_DT_INTERFACE 0x04
152 static int dev_if_packed_info(struct udev_device
*dev
, char *ifs_str
, size_t len
)
154 char *filename
= NULL
;
157 unsigned char buf
[18 + 65535];
158 unsigned int pos
, strpos
;
159 struct usb_interface_descriptor
{
161 u_int8_t bDescriptorType
;
162 u_int8_t bInterfaceNumber
;
163 u_int8_t bAlternateSetting
;
164 u_int8_t bNumEndpoints
;
165 u_int8_t bInterfaceClass
;
166 u_int8_t bInterfaceSubClass
;
167 u_int8_t bInterfaceProtocol
;
169 } __attribute__((packed
));
172 if (asprintf(&filename
, "%s/descriptors", udev_device_get_syspath(dev
)) < 0) {
176 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
);
178 fprintf(stderr
, "error opening USB device 'descriptors' file\n");
182 size
= read(fd
, buf
, sizeof(buf
));
184 if (size
< 18 || size
== sizeof(buf
)) {
192 while (pos
< sizeof(buf
) && strpos
+7 < len
-2) {
193 struct usb_interface_descriptor
*desc
;
196 desc
= (struct usb_interface_descriptor
*) &buf
[pos
];
197 if (desc
->bLength
< 3)
199 pos
+= desc
->bLength
;
201 if (desc
->bDescriptorType
!= USB_DT_INTERFACE
)
204 if (snprintf(if_str
, 8, ":%02x%02x%02x",
205 desc
->bInterfaceClass
,
206 desc
->bInterfaceSubClass
,
207 desc
->bInterfaceProtocol
) != 7)
210 if (strstr(ifs_str
, if_str
) != NULL
)
213 memcpy(&ifs_str
[strpos
], if_str
, 8),
217 ifs_str
[strpos
++] = ':';
218 ifs_str
[strpos
++] = '\0';
226 * A unique USB identification is generated like this:
228 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
229 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC'
230 * use the SCSI vendor and model as USB-Vendor and USB-model.
231 * 3.) Otherwise use the USB manufacturer and product as
232 * USB-Vendor and USB-model. Any non-printable characters
233 * in those strings will be skipped; a slash '/' will be converted
234 * into a full stop '.'.
235 * 4.) If that fails, too, we will use idVendor and idProduct
236 * as USB-Vendor and USB-model.
237 * 5.) The USB identification is the USB-vendor and USB-model
238 * string concatenated with an underscore '_'.
239 * 6.) If the device supplies a serial number, this number
240 * is concatenated with the identification with an underscore '_'.
242 static int builtin_usb_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
)
245 char vendor_str_enc
[256];
246 const char *vendor_id
;
248 char model_str_enc
[256];
249 const char *product_id
;
250 char serial_str
[UTIL_NAME_SIZE
];
251 char packed_if_str
[UTIL_NAME_SIZE
];
252 char revision_str
[64];
254 char instance_str
[64];
255 const char *ifnum
= NULL
;
256 const char *driver
= NULL
;
259 struct udev
*udev
= udev_device_get_udev(dev
);
260 struct udev_device
*dev_interface
= NULL
;
261 struct udev_device
*dev_usb
= NULL
;
262 const char *if_class
, *if_subclass
;
268 vendor_str
[0] = '\0';
270 serial_str
[0] = '\0';
271 packed_if_str
[0] = '\0';
272 revision_str
[0] = '\0';
274 instance_str
[0] = '\0';
276 dbg(udev
, "syspath %s\n", udev_device_get_syspath(dev
));
278 /* shortcut, if we are called directly for a "usb_device" type */
279 if (udev_device_get_devtype(dev
) != NULL
&& strcmp(udev_device_get_devtype(dev
), "usb_device") == 0) {
280 dev_if_packed_info(dev
, packed_if_str
, sizeof(packed_if_str
));
285 /* usb interface directory */
286 dev_interface
= udev_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface");
287 if (dev_interface
== NULL
) {
288 info(udev
, "unable to access usb_interface device of '%s'\n",
289 udev_device_get_syspath(dev
));
293 ifnum
= udev_device_get_sysattr_value(dev_interface
, "bInterfaceNumber");
294 driver
= udev_device_get_sysattr_value(dev_interface
, "driver");
296 if_class
= udev_device_get_sysattr_value(dev_interface
, "bInterfaceClass");
298 info(udev
, "%s: cannot get bInterfaceClass attribute\n",
299 udev_device_get_sysname(dev
));
303 if_class_num
= strtoul(if_class
, NULL
, 16);
304 if (if_class_num
== 8) {
306 if_subclass
= udev_device_get_sysattr_value(dev_interface
, "bInterfaceSubClass");
307 if (if_subclass
!= NULL
)
308 protocol
= set_usb_mass_storage_ifsubtype(type_str
, if_subclass
, sizeof(type_str
)-1);
310 set_usb_iftype(type_str
, if_class_num
, sizeof(type_str
)-1);
313 info(udev
, "%s: if_class %d protocol %d\n",
314 udev_device_get_syspath(dev_interface
), if_class_num
, protocol
);
316 /* usb device directory */
317 dev_usb
= udev_device_get_parent_with_subsystem_devtype(dev_interface
, "usb", "usb_device");
319 info(udev
, "unable to find parent 'usb' device of '%s'\n",
320 udev_device_get_syspath(dev
));
324 /* all interfaces of the device in a single string */
325 dev_if_packed_info(dev_usb
, packed_if_str
, sizeof(packed_if_str
));
327 /* mass storage : SCSI or ATAPI */
328 if ((protocol
== 6 || protocol
== 2)) {
329 struct udev_device
*dev_scsi
;
330 const char *scsi_model
, *scsi_vendor
, *scsi_type
, *scsi_rev
;
331 int host
, bus
, target
, lun
;
333 /* get scsi device */
334 dev_scsi
= udev_device_get_parent_with_subsystem_devtype(dev
, "scsi", "scsi_device");
335 if (dev_scsi
== NULL
) {
336 info(udev
, "unable to find parent 'scsi' device of '%s'\n",
337 udev_device_get_syspath(dev
));
340 if (sscanf(udev_device_get_sysname(dev_scsi
), "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4) {
341 info(udev
, "invalid scsi device '%s'\n", udev_device_get_sysname(dev_scsi
));
345 /* Generic SPC-2 device */
346 scsi_vendor
= udev_device_get_sysattr_value(dev_scsi
, "vendor");
348 info(udev
, "%s: cannot get SCSI vendor attribute\n",
349 udev_device_get_sysname(dev_scsi
));
352 udev_util_encode_string(scsi_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
353 util_replace_whitespace(scsi_vendor
, vendor_str
, sizeof(vendor_str
)-1);
354 util_replace_chars(vendor_str
, NULL
);
356 scsi_model
= udev_device_get_sysattr_value(dev_scsi
, "model");
358 info(udev
, "%s: cannot get SCSI model attribute\n",
359 udev_device_get_sysname(dev_scsi
));
362 udev_util_encode_string(scsi_model
, model_str_enc
, sizeof(model_str_enc
));
363 util_replace_whitespace(scsi_model
, model_str
, sizeof(model_str
)-1);
364 util_replace_chars(model_str
, NULL
);
366 scsi_type
= udev_device_get_sysattr_value(dev_scsi
, "type");
368 info(udev
, "%s: cannot get SCSI type attribute\n",
369 udev_device_get_sysname(dev_scsi
));
372 set_scsi_type(type_str
, scsi_type
, sizeof(type_str
)-1);
374 scsi_rev
= udev_device_get_sysattr_value(dev_scsi
, "rev");
376 info(udev
, "%s: cannot get SCSI revision attribute\n",
377 udev_device_get_sysname(dev_scsi
));
380 util_replace_whitespace(scsi_rev
, revision_str
, sizeof(revision_str
)-1);
381 util_replace_chars(revision_str
, NULL
);
384 * some broken devices have the same identifiers
385 * for all luns, export the target:lun number
387 sprintf(instance_str
, "%d:%d", target
, lun
);
391 vendor_id
= udev_device_get_sysattr_value(dev_usb
, "idVendor");
392 product_id
= udev_device_get_sysattr_value(dev_usb
, "idProduct");
394 /* fallback to USB vendor & device */
395 if (vendor_str
[0] == '\0') {
396 const char *usb_vendor
= NULL
;
398 usb_vendor
= udev_device_get_sysattr_value(dev_usb
, "manufacturer");
400 usb_vendor
= vendor_id
;
402 info(udev
, "No USB vendor information available\n");
405 udev_util_encode_string(usb_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
406 util_replace_whitespace(usb_vendor
, vendor_str
, sizeof(vendor_str
)-1);
407 util_replace_chars(vendor_str
, NULL
);
410 if (model_str
[0] == '\0') {
411 const char *usb_model
= NULL
;
413 usb_model
= udev_device_get_sysattr_value(dev_usb
, "product");
415 usb_model
= product_id
;
417 dbg(udev
, "No USB model information available\n");
420 udev_util_encode_string(usb_model
, model_str_enc
, sizeof(model_str_enc
));
421 util_replace_whitespace(usb_model
, model_str
, sizeof(model_str
)-1);
422 util_replace_chars(model_str
, NULL
);
425 if (revision_str
[0] == '\0') {
428 usb_rev
= udev_device_get_sysattr_value(dev_usb
, "bcdDevice");
430 util_replace_whitespace(usb_rev
, revision_str
, sizeof(revision_str
)-1);
431 util_replace_chars(revision_str
, NULL
);
435 if (serial_str
[0] == '\0') {
436 const char *usb_serial
;
438 usb_serial
= udev_device_get_sysattr_value(dev_usb
, "serial");
440 util_replace_whitespace(usb_serial
, serial_str
, sizeof(serial_str
)-1);
441 util_replace_chars(serial_str
, NULL
);
446 l
= util_strpcpyl(&s
, sizeof(serial
), vendor_str
, "_", model_str
, NULL
);
447 if (serial_str
[0] != '\0')
448 l
= util_strpcpyl(&s
, l
, "_", serial_str
, NULL
);
450 if (instance_str
[0] != '\0')
451 util_strpcpyl(&s
, l
, "-", instance_str
, NULL
);
453 udev_builtin_add_property(dev
, test
, "ID_VENDOR", vendor_str
);
454 udev_builtin_add_property(dev
, test
, "ID_VENDOR_ENC", vendor_str_enc
);
455 udev_builtin_add_property(dev
, test
, "ID_VENDOR_ID", vendor_id
);
456 udev_builtin_add_property(dev
, test
, "ID_MODEL", model_str
);
457 udev_builtin_add_property(dev
, test
, "ID_MODEL_ENC", model_str_enc
);
458 udev_builtin_add_property(dev
, test
, "ID_MODEL_ID", product_id
);
459 udev_builtin_add_property(dev
, test
, "ID_REVISION", revision_str
);
460 udev_builtin_add_property(dev
, test
, "ID_SERIAL", serial
);
461 if (serial_str
[0] != '\0')
462 udev_builtin_add_property(dev
, test
, "ID_SERIAL_SHORT", serial_str
);
463 if (type_str
[0] != '\0')
464 udev_builtin_add_property(dev
, test
, "ID_TYPE", type_str
);
465 if (instance_str
[0] != '\0')
466 udev_builtin_add_property(dev
, test
, "ID_INSTANCE", instance_str
);
467 udev_builtin_add_property(dev
, test
, "ID_BUS", "usb");
468 if (packed_if_str
[0] != '\0')
469 udev_builtin_add_property(dev
, test
, "ID_USB_INTERFACES", packed_if_str
);
471 udev_builtin_add_property(dev
, test
, "ID_USB_INTERFACE_NUM", ifnum
);
473 udev_builtin_add_property(dev
, test
, "ID_USB_DRIVER", driver
);
477 const struct udev_builtin udev_builtin_usb_id
= {
479 .cmd
= builtin_usb_id
,
480 .help
= "usb device properties",