1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * USB device properties and persistent device path
5 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
6 * Author: Hannes Reinecke <hare@suse.de>
18 #include "alloc-util.h"
19 #include "device-util.h"
21 #include "libudev-private.h"
22 #include "string-util.h"
23 #include "udev-builtin.h"
25 static void set_usb_iftype(char *to
, int if_class_num
, size_t len
) {
26 const char *type
= "generic";
28 switch (if_class_num
) {
32 case 2: /* CDC-Control */
37 case 5: /* Physical */
51 case 0x0a: /* CDC-Data */
53 case 0x0b: /* Chip/Smart Card */
55 case 0x0d: /* Content Security */
60 case 0xdc: /* Diagnostic Device */
62 case 0xe0: /* Wireless Controller */
64 case 0xfe: /* Application-specific */
66 case 0xff: /* Vendor-specific */
71 strncpy(to
, type
, len
);
75 static int set_usb_mass_storage_ifsubtype(char *to
, const char *from
, size_t len
) {
78 const char *type
= "generic";
80 type_num
= strtoul(from
, &eptr
, 0);
83 case 1: /* RBC devices */
95 case 6: /* Transparent SPC-2 devices */
102 strscpy(to
, len
, type
);
106 static void set_scsi_type(char *to
, const char *from
, size_t len
) {
109 const char *type
= "generic";
111 type_num
= strtoul(from
, &eptr
, 0);
133 strscpy(to
, len
, type
);
136 #define USB_DT_DEVICE 0x01
137 #define USB_DT_INTERFACE 0x04
139 static int dev_if_packed_info(sd_device
*dev
, char *ifs_str
, size_t len
) {
140 _cleanup_free_
char *filename
= NULL
;
141 _cleanup_close_
int fd
= -1;
143 unsigned char buf
[18 + 65535];
148 struct usb_interface_descriptor
{
150 uint8_t bDescriptorType
;
151 uint8_t bInterfaceNumber
;
152 uint8_t bAlternateSetting
;
153 uint8_t bNumEndpoints
;
154 uint8_t bInterfaceClass
;
155 uint8_t bInterfaceSubClass
;
156 uint8_t bInterfaceProtocol
;
160 r
= sd_device_get_syspath(dev
, &syspath
);
163 if (asprintf(&filename
, "%s/descriptors", syspath
) < 0)
166 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
);
168 return log_device_debug_errno(dev
, errno
, "Failed to open USB device 'descriptors' file: %m");
170 size
= read(fd
, buf
, sizeof(buf
));
171 if (size
< 18 || (size_t) size
>= sizeof(buf
))
175 while (pos
+ sizeof(struct usb_interface_descriptor
) < (size_t) size
&&
176 strpos
+ 7 < len
- 2) {
178 struct usb_interface_descriptor
*desc
;
181 desc
= (struct usb_interface_descriptor
*) &buf
[pos
];
182 if (desc
->bLength
< 3)
184 pos
+= desc
->bLength
;
186 if (desc
->bDescriptorType
!= USB_DT_INTERFACE
)
189 if (snprintf(if_str
, 8, ":%02x%02x%02x",
190 desc
->bInterfaceClass
,
191 desc
->bInterfaceSubClass
,
192 desc
->bInterfaceProtocol
) != 7)
195 if (strstr(ifs_str
, if_str
) != NULL
)
198 memcpy(&ifs_str
[strpos
], if_str
, 8),
203 ifs_str
[strpos
++] = ':';
204 ifs_str
[strpos
++] = '\0';
211 * A unique USB identification is generated like this:
213 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
214 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC',
215 * use the SCSI vendor and model as USB-Vendor and USB-model.
216 * 3.) Otherwise, use the USB manufacturer and product as
217 * USB-Vendor and USB-model. Any non-printable characters
218 * in those strings will be skipped; a slash '/' will be converted
219 * into a full stop '.'.
220 * 4.) If that fails, too, we will use idVendor and idProduct
221 * as USB-Vendor and USB-model.
222 * 5.) The USB identification is the USB-vendor and USB-model
223 * string concatenated with an underscore '_'.
224 * 6.) If the device supplies a serial number, this number
225 * is concatenated with the identification with an underscore '_'.
227 static int builtin_usb_id(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
228 char vendor_str
[64] = "";
229 char vendor_str_enc
[256];
230 const char *vendor_id
;
231 char model_str
[64] = "";
232 char model_str_enc
[256];
233 const char *product_id
;
234 char serial_str
[UTIL_NAME_SIZE
] = "";
235 char packed_if_str
[UTIL_NAME_SIZE
] = "";
236 char revision_str
[64] = "";
237 char type_str
[64] = "";
238 char instance_str
[64] = "";
239 const char *ifnum
= NULL
;
240 const char *driver
= NULL
;
243 sd_device
*dev_interface
, *dev_usb
;
244 const char *if_class
, *if_subclass
;
250 const char *syspath
, *sysname
, *devtype
, *interface_syspath
;
255 r
= sd_device_get_syspath(dev
, &syspath
);
259 r
= sd_device_get_sysname(dev
, &sysname
);
263 /* shortcut, if we are called directly for a "usb_device" type */
264 if (sd_device_get_devtype(dev
, &devtype
) >= 0 && streq(devtype
, "usb_device")) {
265 dev_if_packed_info(dev
, packed_if_str
, sizeof(packed_if_str
));
270 /* usb interface directory */
271 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface", &dev_interface
);
273 return log_device_debug_errno(dev
, r
, "Failed to access usb_interface: %m");
275 r
= sd_device_get_syspath(dev_interface
, &interface_syspath
);
277 return log_device_debug_errno(dev_interface
, r
, "Failed to get syspath: %m");
278 (void) sd_device_get_sysattr_value(dev_interface
, "bInterfaceNumber", &ifnum
);
279 (void) sd_device_get_sysattr_value(dev_interface
, "driver", &driver
);
281 r
= sd_device_get_sysattr_value(dev_interface
, "bInterfaceClass", &if_class
);
283 return log_device_debug_errno(dev_interface
, r
, "Failed to get bInterfaceClass attribute: %m");
285 if_class_num
= strtoul(if_class
, NULL
, 16);
286 if (if_class_num
== 8) {
288 if (sd_device_get_sysattr_value(dev_interface
, "bInterfaceSubClass", &if_subclass
) >= 0)
289 protocol
= set_usb_mass_storage_ifsubtype(type_str
, if_subclass
, sizeof(type_str
)-1);
291 set_usb_iftype(type_str
, if_class_num
, sizeof(type_str
)-1);
293 log_device_debug(dev_interface
, "if_class:%d protocol:%d", if_class_num
, protocol
);
295 /* usb device directory */
296 r
= sd_device_get_parent_with_subsystem_devtype(dev_interface
, "usb", "usb_device", &dev_usb
);
298 return log_device_debug_errno(dev_interface
, r
, "Failed to find parent 'usb' device");
300 /* all interfaces of the device in a single string */
301 dev_if_packed_info(dev_usb
, packed_if_str
, sizeof(packed_if_str
));
303 /* mass storage : SCSI or ATAPI */
304 if (IN_SET(protocol
, 6, 2)) {
306 const char *scsi_sysname
, *scsi_model
, *scsi_vendor
, *scsi_type
, *scsi_rev
;
307 int host
, bus
, target
, lun
;
309 /* get scsi device */
310 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "scsi", "scsi_device", &dev_scsi
);
312 log_device_debug_errno(dev
, r
, "Unable to find parent SCSI device");
315 if (sd_device_get_sysname(dev_scsi
, &scsi_sysname
) < 0)
317 if (sscanf(scsi_sysname
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4) {
318 log_device_debug(dev_scsi
, "Invalid SCSI device");
322 /* Generic SPC-2 device */
323 r
= sd_device_get_sysattr_value(dev_scsi
, "vendor", &scsi_vendor
);
325 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI vendor attribute: %m");
328 udev_util_encode_string(scsi_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
329 util_replace_whitespace(scsi_vendor
, vendor_str
, sizeof(vendor_str
)-1);
330 util_replace_chars(vendor_str
, NULL
);
332 r
= sd_device_get_sysattr_value(dev_scsi
, "model", &scsi_model
);
334 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI model attribute: %m");
337 udev_util_encode_string(scsi_model
, model_str_enc
, sizeof(model_str_enc
));
338 util_replace_whitespace(scsi_model
, model_str
, sizeof(model_str
)-1);
339 util_replace_chars(model_str
, NULL
);
341 r
= sd_device_get_sysattr_value(dev_scsi
, "type", &scsi_type
);
343 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI type attribute: %m");
346 set_scsi_type(type_str
, scsi_type
, sizeof(type_str
)-1);
348 r
= sd_device_get_sysattr_value(dev_scsi
, "rev", &scsi_rev
);
350 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI revision attribute: %m");
353 util_replace_whitespace(scsi_rev
, revision_str
, sizeof(revision_str
)-1);
354 util_replace_chars(revision_str
, NULL
);
357 * some broken devices have the same identifiers
358 * for all luns, export the target:lun number
360 sprintf(instance_str
, "%d:%d", target
, lun
);
364 r
= sd_device_get_sysattr_value(dev_usb
, "idVendor", &vendor_id
);
366 return log_device_debug_errno(dev_usb
, r
, "Failed to get idVendor attribute: %m");
368 r
= sd_device_get_sysattr_value(dev_usb
, "idProduct", &product_id
);
370 return log_device_debug_errno(dev_usb
, r
, "Failed to get idProduct attribute: %m");
372 /* fallback to USB vendor & device */
373 if (vendor_str
[0] == '\0') {
374 const char *usb_vendor
;
376 if (sd_device_get_sysattr_value(dev_usb
, "manufacturer", &usb_vendor
) < 0)
377 usb_vendor
= vendor_id
;
378 udev_util_encode_string(usb_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
379 util_replace_whitespace(usb_vendor
, vendor_str
, sizeof(vendor_str
)-1);
380 util_replace_chars(vendor_str
, NULL
);
383 if (model_str
[0] == '\0') {
384 const char *usb_model
;
386 if (sd_device_get_sysattr_value(dev_usb
, "product", &usb_model
) < 0)
387 usb_model
= product_id
;
388 udev_util_encode_string(usb_model
, model_str_enc
, sizeof(model_str_enc
));
389 util_replace_whitespace(usb_model
, model_str
, sizeof(model_str
)-1);
390 util_replace_chars(model_str
, NULL
);
393 if (revision_str
[0] == '\0') {
396 if (sd_device_get_sysattr_value(dev_usb
, "bcdDevice", &usb_rev
) >= 0) {
397 util_replace_whitespace(usb_rev
, revision_str
, sizeof(revision_str
)-1);
398 util_replace_chars(revision_str
, NULL
);
402 if (serial_str
[0] == '\0') {
403 const char *usb_serial
;
405 if (sd_device_get_sysattr_value(dev_usb
, "serial", &usb_serial
) >= 0) {
406 const unsigned char *p
;
408 /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
409 for (p
= (unsigned char *) usb_serial
; *p
!= '\0'; p
++)
410 if (*p
< 0x20 || *p
> 0x7f || *p
== ',') {
416 util_replace_whitespace(usb_serial
, serial_str
, sizeof(serial_str
)-1);
417 util_replace_chars(serial_str
, NULL
);
423 l
= strpcpyl(&s
, sizeof(serial
), vendor_str
, "_", model_str
, NULL
);
424 if (!isempty(serial_str
))
425 l
= strpcpyl(&s
, l
, "_", serial_str
, NULL
);
427 if (!isempty(instance_str
))
428 strpcpyl(&s
, l
, "-", instance_str
, NULL
);
430 udev_builtin_add_property(dev
, test
, "ID_VENDOR", vendor_str
);
431 udev_builtin_add_property(dev
, test
, "ID_VENDOR_ENC", vendor_str_enc
);
432 udev_builtin_add_property(dev
, test
, "ID_VENDOR_ID", vendor_id
);
433 udev_builtin_add_property(dev
, test
, "ID_MODEL", model_str
);
434 udev_builtin_add_property(dev
, test
, "ID_MODEL_ENC", model_str_enc
);
435 udev_builtin_add_property(dev
, test
, "ID_MODEL_ID", product_id
);
436 udev_builtin_add_property(dev
, test
, "ID_REVISION", revision_str
);
437 udev_builtin_add_property(dev
, test
, "ID_SERIAL", serial
);
438 if (!isempty(serial_str
))
439 udev_builtin_add_property(dev
, test
, "ID_SERIAL_SHORT", serial_str
);
440 if (!isempty(type_str
))
441 udev_builtin_add_property(dev
, test
, "ID_TYPE", type_str
);
442 if (!isempty(instance_str
))
443 udev_builtin_add_property(dev
, test
, "ID_INSTANCE", instance_str
);
444 udev_builtin_add_property(dev
, test
, "ID_BUS", "usb");
445 if (!isempty(packed_if_str
))
446 udev_builtin_add_property(dev
, test
, "ID_USB_INTERFACES", packed_if_str
);
448 udev_builtin_add_property(dev
, test
, "ID_USB_INTERFACE_NUM", ifnum
);
450 udev_builtin_add_property(dev
, test
, "ID_USB_DRIVER", driver
);
454 const struct udev_builtin udev_builtin_usb_id
= {
456 .cmd
= builtin_usb_id
,
457 .help
= "USB device properties",