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>
17 #include "alloc-util.h"
18 #include "device-util.h"
20 #include "libudev-util.h"
21 #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_close_
int fd
= -1;
142 unsigned char buf
[18 + 65535];
145 const char *filename
, *syspath
;
147 struct usb_interface_descriptor
{
149 uint8_t bDescriptorType
;
150 uint8_t bInterfaceNumber
;
151 uint8_t bAlternateSetting
;
152 uint8_t bNumEndpoints
;
153 uint8_t bInterfaceClass
;
154 uint8_t bInterfaceSubClass
;
155 uint8_t bInterfaceProtocol
;
159 r
= sd_device_get_syspath(dev
, &syspath
);
163 filename
= strjoina(syspath
, "/descriptors");
164 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
);
166 return log_device_debug_errno(dev
, errno
, "Failed to open \"%s\": %m", filename
);
168 size
= read(fd
, buf
, sizeof(buf
));
170 return log_device_warning_errno(dev
, SYNTHETIC_ERRNO(EIO
),
171 "Short read from \"%s\"", filename
);
172 assert((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 if (desc
->bLength
> size
- sizeof(struct usb_interface_descriptor
))
185 return log_device_debug_errno(dev
, SYNTHETIC_ERRNO(EIO
),
186 "Corrupt data read from \"%s\"", filename
);
187 pos
+= desc
->bLength
;
189 if (desc
->bDescriptorType
!= USB_DT_INTERFACE
)
192 if (snprintf(if_str
, 8, ":%02x%02x%02x",
193 desc
->bInterfaceClass
,
194 desc
->bInterfaceSubClass
,
195 desc
->bInterfaceProtocol
) != 7)
198 if (strstr(ifs_str
, if_str
))
201 memcpy(&ifs_str
[strpos
], if_str
, 8),
206 ifs_str
[strpos
++] = ':';
207 ifs_str
[strpos
++] = '\0';
214 * A unique USB identification is generated like this:
216 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
217 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC',
218 * use the SCSI vendor and model as USB-Vendor and USB-model.
219 * 3.) Otherwise, use the USB manufacturer and product as
220 * USB-Vendor and USB-model. Any non-printable characters
221 * in those strings will be skipped; a slash '/' will be converted
222 * into a full stop '.'.
223 * 4.) If that fails, too, we will use idVendor and idProduct
224 * as USB-Vendor and USB-model.
225 * 5.) The USB identification is the USB-vendor and USB-model
226 * string concatenated with an underscore '_'.
227 * 6.) If the device supplies a serial number, this number
228 * is concatenated with the identification with an underscore '_'.
230 static int builtin_usb_id(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
231 char vendor_str
[64] = "";
232 char vendor_str_enc
[256];
233 const char *vendor_id
;
234 char model_str
[64] = "";
235 char model_str_enc
[256];
236 const char *product_id
;
237 char serial_str
[UTIL_NAME_SIZE
] = "";
238 char packed_if_str
[UTIL_NAME_SIZE
] = "";
239 char revision_str
[64] = "";
240 char type_str
[64] = "";
241 char instance_str
[64] = "";
242 const char *ifnum
= NULL
;
243 const char *driver
= NULL
;
246 sd_device
*dev_interface
, *dev_usb
;
247 const char *if_class
, *if_subclass
;
253 const char *syspath
, *sysname
, *devtype
, *interface_syspath
;
258 r
= sd_device_get_syspath(dev
, &syspath
);
262 r
= sd_device_get_sysname(dev
, &sysname
);
266 /* shortcut, if we are called directly for a "usb_device" type */
267 if (sd_device_get_devtype(dev
, &devtype
) >= 0 && streq(devtype
, "usb_device")) {
268 dev_if_packed_info(dev
, packed_if_str
, sizeof(packed_if_str
));
273 /* usb interface directory */
274 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface", &dev_interface
);
276 return log_device_debug_errno(dev
, r
, "Failed to access usb_interface: %m");
278 r
= sd_device_get_syspath(dev_interface
, &interface_syspath
);
280 return log_device_debug_errno(dev_interface
, r
, "Failed to get syspath: %m");
281 (void) sd_device_get_sysattr_value(dev_interface
, "bInterfaceNumber", &ifnum
);
282 (void) sd_device_get_sysattr_value(dev_interface
, "driver", &driver
);
284 r
= sd_device_get_sysattr_value(dev_interface
, "bInterfaceClass", &if_class
);
286 return log_device_debug_errno(dev_interface
, r
, "Failed to get bInterfaceClass attribute: %m");
288 if_class_num
= strtoul(if_class
, NULL
, 16);
289 if (if_class_num
== 8) {
291 if (sd_device_get_sysattr_value(dev_interface
, "bInterfaceSubClass", &if_subclass
) >= 0)
292 protocol
= set_usb_mass_storage_ifsubtype(type_str
, if_subclass
, sizeof(type_str
)-1);
294 set_usb_iftype(type_str
, if_class_num
, sizeof(type_str
)-1);
296 log_device_debug(dev_interface
, "if_class:%d protocol:%d", if_class_num
, protocol
);
298 /* usb device directory */
299 r
= sd_device_get_parent_with_subsystem_devtype(dev_interface
, "usb", "usb_device", &dev_usb
);
301 return log_device_debug_errno(dev_interface
, r
, "Failed to find parent 'usb' device");
303 /* all interfaces of the device in a single string */
304 dev_if_packed_info(dev_usb
, packed_if_str
, sizeof(packed_if_str
));
306 /* mass storage : SCSI or ATAPI */
307 if (IN_SET(protocol
, 6, 2)) {
309 const char *scsi_sysname
, *scsi_model
, *scsi_vendor
, *scsi_type
, *scsi_rev
;
310 int host
, bus
, target
, lun
;
312 /* get scsi device */
313 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "scsi", "scsi_device", &dev_scsi
);
315 log_device_debug_errno(dev
, r
, "Unable to find parent SCSI device");
318 if (sd_device_get_sysname(dev_scsi
, &scsi_sysname
) < 0)
320 if (sscanf(scsi_sysname
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4) {
321 log_device_debug(dev_scsi
, "Invalid SCSI device");
325 /* Generic SPC-2 device */
326 r
= sd_device_get_sysattr_value(dev_scsi
, "vendor", &scsi_vendor
);
328 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI vendor attribute: %m");
331 udev_util_encode_string(scsi_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
332 util_replace_whitespace(scsi_vendor
, vendor_str
, sizeof(vendor_str
)-1);
333 util_replace_chars(vendor_str
, NULL
);
335 r
= sd_device_get_sysattr_value(dev_scsi
, "model", &scsi_model
);
337 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI model attribute: %m");
340 udev_util_encode_string(scsi_model
, model_str_enc
, sizeof(model_str_enc
));
341 util_replace_whitespace(scsi_model
, model_str
, sizeof(model_str
)-1);
342 util_replace_chars(model_str
, NULL
);
344 r
= sd_device_get_sysattr_value(dev_scsi
, "type", &scsi_type
);
346 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI type attribute: %m");
349 set_scsi_type(type_str
, scsi_type
, sizeof(type_str
)-1);
351 r
= sd_device_get_sysattr_value(dev_scsi
, "rev", &scsi_rev
);
353 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI revision attribute: %m");
356 util_replace_whitespace(scsi_rev
, revision_str
, sizeof(revision_str
)-1);
357 util_replace_chars(revision_str
, NULL
);
360 * some broken devices have the same identifiers
361 * for all luns, export the target:lun number
363 sprintf(instance_str
, "%d:%d", target
, lun
);
367 r
= sd_device_get_sysattr_value(dev_usb
, "idVendor", &vendor_id
);
369 return log_device_debug_errno(dev_usb
, r
, "Failed to get idVendor attribute: %m");
371 r
= sd_device_get_sysattr_value(dev_usb
, "idProduct", &product_id
);
373 return log_device_debug_errno(dev_usb
, r
, "Failed to get idProduct attribute: %m");
375 /* fallback to USB vendor & device */
376 if (vendor_str
[0] == '\0') {
377 const char *usb_vendor
;
379 if (sd_device_get_sysattr_value(dev_usb
, "manufacturer", &usb_vendor
) < 0)
380 usb_vendor
= vendor_id
;
381 udev_util_encode_string(usb_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
382 util_replace_whitespace(usb_vendor
, vendor_str
, sizeof(vendor_str
)-1);
383 util_replace_chars(vendor_str
, NULL
);
386 if (model_str
[0] == '\0') {
387 const char *usb_model
;
389 if (sd_device_get_sysattr_value(dev_usb
, "product", &usb_model
) < 0)
390 usb_model
= product_id
;
391 udev_util_encode_string(usb_model
, model_str_enc
, sizeof(model_str_enc
));
392 util_replace_whitespace(usb_model
, model_str
, sizeof(model_str
)-1);
393 util_replace_chars(model_str
, NULL
);
396 if (revision_str
[0] == '\0') {
399 if (sd_device_get_sysattr_value(dev_usb
, "bcdDevice", &usb_rev
) >= 0) {
400 util_replace_whitespace(usb_rev
, revision_str
, sizeof(revision_str
)-1);
401 util_replace_chars(revision_str
, NULL
);
405 if (serial_str
[0] == '\0') {
406 const char *usb_serial
;
408 if (sd_device_get_sysattr_value(dev_usb
, "serial", &usb_serial
) >= 0) {
409 const unsigned char *p
;
411 /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
412 for (p
= (unsigned char *) usb_serial
; *p
!= '\0'; p
++)
413 if (*p
< 0x20 || *p
> 0x7f || *p
== ',') {
419 util_replace_whitespace(usb_serial
, serial_str
, sizeof(serial_str
)-1);
420 util_replace_chars(serial_str
, NULL
);
426 l
= strpcpyl(&s
, sizeof(serial
), vendor_str
, "_", model_str
, NULL
);
427 if (!isempty(serial_str
))
428 l
= strpcpyl(&s
, l
, "_", serial_str
, NULL
);
430 if (!isempty(instance_str
))
431 strpcpyl(&s
, l
, "-", instance_str
, NULL
);
433 udev_builtin_add_property(dev
, test
, "ID_VENDOR", vendor_str
);
434 udev_builtin_add_property(dev
, test
, "ID_VENDOR_ENC", vendor_str_enc
);
435 udev_builtin_add_property(dev
, test
, "ID_VENDOR_ID", vendor_id
);
436 udev_builtin_add_property(dev
, test
, "ID_MODEL", model_str
);
437 udev_builtin_add_property(dev
, test
, "ID_MODEL_ENC", model_str_enc
);
438 udev_builtin_add_property(dev
, test
, "ID_MODEL_ID", product_id
);
439 udev_builtin_add_property(dev
, test
, "ID_REVISION", revision_str
);
440 udev_builtin_add_property(dev
, test
, "ID_SERIAL", serial
);
441 if (!isempty(serial_str
))
442 udev_builtin_add_property(dev
, test
, "ID_SERIAL_SHORT", serial_str
);
443 if (!isempty(type_str
))
444 udev_builtin_add_property(dev
, test
, "ID_TYPE", type_str
);
445 if (!isempty(instance_str
))
446 udev_builtin_add_property(dev
, test
, "ID_INSTANCE", instance_str
);
447 udev_builtin_add_property(dev
, test
, "ID_BUS", "usb");
448 if (!isempty(packed_if_str
))
449 udev_builtin_add_property(dev
, test
, "ID_USB_INTERFACES", packed_if_str
);
451 udev_builtin_add_property(dev
, test
, "ID_USB_INTERFACE_NUM", ifnum
);
453 udev_builtin_add_property(dev
, test
, "ID_USB_DRIVER", driver
);
457 const UdevBuiltin udev_builtin_usb_id
= {
459 .cmd
= builtin_usb_id
,
460 .help
= "USB device properties",