1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * USB device properties and persistent device path
5 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
6 * Author: Hannes Reinecke <hare@suse.de>
13 #include "device-nodes.h"
14 #include "device-util.h"
16 #include "parse-util.h"
17 #include "string-util.h"
19 #include "udev-builtin.h"
20 #include "udev-util.h"
22 static void set_usb_iftype(char *to
, int if_class_num
, size_t len
) {
23 const char *type
= "generic";
25 switch (if_class_num
) {
29 case 2: /* CDC-Control */
34 case 5: /* Physical */
48 case 0x0a: /* CDC-Data */
50 case 0x0b: /* Chip/Smart Card */
52 case 0x0d: /* Content Security */
57 case 0xdc: /* Diagnostic Device */
59 case 0xe0: /* Wireless Controller */
61 case 0xfe: /* Application-specific */
63 case 0xff: /* Vendor-specific */
66 strncpy(to
, type
, len
);
70 static int set_usb_mass_storage_ifsubtype(char *to
, const char *from
, size_t len
) {
72 const char *type
= "generic";
74 if (safe_atoi(from
, &type_num
) >= 0)
76 case 1: /* RBC devices */
88 case 6: /* Transparent SPC-2 devices */
93 strscpy(to
, len
, type
);
97 static void set_scsi_type(char *to
, const char *from
, size_t len
) {
99 const char *type
= "generic";
101 if (safe_atou(from
, &type_num
) >= 0)
120 strscpy(to
, len
, type
);
123 #define USB_DT_DEVICE 0x01
124 #define USB_DT_INTERFACE 0x04
126 static int dev_if_packed_info(sd_device
*dev
, char *ifs_str
, size_t len
) {
127 _cleanup_close_
int fd
= -EBADF
;
129 unsigned char buf
[18 + 65535];
132 const char *filename
, *syspath
;
134 struct usb_interface_descriptor
{
136 uint8_t bDescriptorType
;
137 uint8_t bInterfaceNumber
;
138 uint8_t bAlternateSetting
;
139 uint8_t bNumEndpoints
;
140 uint8_t bInterfaceClass
;
141 uint8_t bInterfaceSubClass
;
142 uint8_t bInterfaceProtocol
;
146 r
= sd_device_get_syspath(dev
, &syspath
);
150 filename
= strjoina(syspath
, "/descriptors");
151 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
153 return log_device_debug_errno(dev
, errno
, "Failed to open \"%s\": %m", filename
);
155 size
= read(fd
, buf
, sizeof(buf
));
157 return log_device_warning_errno(dev
, SYNTHETIC_ERRNO(EIO
),
158 "Short read from \"%s\"", filename
);
159 assert((size_t) size
<= sizeof buf
);
162 while (pos
+ sizeof(struct usb_interface_descriptor
) < (size_t) size
&&
163 strpos
+ 7 < len
- 2) {
165 struct usb_interface_descriptor
*desc
;
168 desc
= (struct usb_interface_descriptor
*) (buf
+ pos
);
169 if (desc
->bLength
< 3)
171 if (desc
->bLength
> size
- sizeof(struct usb_interface_descriptor
))
172 return log_device_debug_errno(dev
, SYNTHETIC_ERRNO(EIO
),
173 "Corrupt data read from \"%s\"", filename
);
174 pos
+= desc
->bLength
;
176 if (desc
->bDescriptorType
!= USB_DT_INTERFACE
)
179 if (snprintf(if_str
, 8, ":%02x%02x%02x",
180 desc
->bInterfaceClass
,
181 desc
->bInterfaceSubClass
,
182 desc
->bInterfaceProtocol
) != 7)
185 if (strstr(ifs_str
, if_str
))
188 memcpy(&ifs_str
[strpos
], if_str
, 8),
193 ifs_str
[strpos
++] = ':';
194 ifs_str
[strpos
++] = '\0';
201 * A unique USB identification is generated like this:
203 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
204 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC',
205 * use the SCSI vendor and model as USB-Vendor and USB-model.
206 * 3.) Otherwise, use the USB manufacturer and product as
207 * USB-Vendor and USB-model. Any non-printable characters
208 * in those strings will be skipped; a slash '/' will be converted
209 * into a full stop '.'.
210 * 4.) If that fails, too, we will use idVendor and idProduct
211 * as USB-Vendor and USB-model.
212 * 5.) The USB identification is the USB-vendor and USB-model
213 * string concatenated with an underscore '_'.
214 * 6.) If the device supplies a serial number, this number
215 * is concatenated with the identification with an underscore '_'.
217 static int builtin_usb_id(UdevEvent
*event
, int argc
, char *argv
[]) {
218 sd_device
*dev_interface
, *dev_usb
, *dev
= ASSERT_PTR(ASSERT_PTR(event
)->dev
);
219 const char *syspath
, *sysname
, *interface_syspath
, *vendor_id
, *product_id
,
220 *ifnum
= NULL
, *driver
= NULL
, *if_class
, *if_subclass
;
221 char *s
, model_str
[64] = "", model_str_enc
[256], serial_str
[UDEV_NAME_SIZE
] = "",
222 packed_if_str
[UDEV_NAME_SIZE
] = "", revision_str
[64] = "", type_str
[64] = "",
223 instance_str
[64] = "", serial
[256], vendor_str
[64] = "", vendor_str_enc
[256];
224 unsigned if_class_num
;
228 r
= sd_device_get_syspath(dev
, &syspath
);
232 r
= sd_device_get_sysname(dev
, &sysname
);
236 /* shortcut, if we are called directly for a "usb_device" type */
237 if (device_is_devtype(dev
, "usb_device")) {
238 dev_if_packed_info(dev
, packed_if_str
, sizeof(packed_if_str
));
243 /* usb interface directory */
244 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface", &dev_interface
);
246 return log_device_debug_errno(dev
, r
, "Failed to access usb_interface: %m");
248 r
= sd_device_get_syspath(dev_interface
, &interface_syspath
);
250 return log_device_debug_errno(dev_interface
, r
, "Failed to get syspath: %m");
251 (void) sd_device_get_sysattr_value(dev_interface
, "bInterfaceNumber", &ifnum
);
252 (void) sd_device_get_sysattr_value(dev_interface
, "driver", &driver
);
254 r
= sd_device_get_sysattr_value(dev_interface
, "bInterfaceClass", &if_class
);
256 return log_device_debug_errno(dev_interface
, r
, "Failed to get bInterfaceClass attribute: %m");
258 r
= safe_atou_full(if_class
, 16, &if_class_num
);
260 return log_device_debug_errno(dev_interface
, r
, "Failed to parse if_class: %m");
261 if (if_class_num
== 8) {
263 if (sd_device_get_sysattr_value(dev_interface
, "bInterfaceSubClass", &if_subclass
) >= 0)
264 protocol
= set_usb_mass_storage_ifsubtype(type_str
, if_subclass
, sizeof(type_str
)-1);
266 set_usb_iftype(type_str
, if_class_num
, sizeof(type_str
)-1);
268 log_device_debug(dev_interface
, "if_class:%u protocol:%i", if_class_num
, protocol
);
270 /* usb device directory */
271 r
= sd_device_get_parent_with_subsystem_devtype(dev_interface
, "usb", "usb_device", &dev_usb
);
273 return log_device_debug_errno(dev_interface
, r
, "Failed to find parent 'usb' device");
275 /* all interfaces of the device in a single string */
276 dev_if_packed_info(dev_usb
, packed_if_str
, sizeof(packed_if_str
));
278 /* mass storage : SCSI or ATAPI */
279 if (IN_SET(protocol
, 6, 2)) {
281 const char *scsi_sysname
, *scsi_model
, *scsi_vendor
, *scsi_type
, *scsi_rev
;
282 int host
, bus
, target
, lun
;
284 /* get scsi device */
285 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "scsi", "scsi_device", &dev_scsi
);
287 log_device_debug_errno(dev
, r
, "Unable to find parent SCSI device");
290 if (sd_device_get_sysname(dev_scsi
, &scsi_sysname
) < 0)
292 if (sscanf(scsi_sysname
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4) {
293 log_device_debug(dev_scsi
, "Invalid SCSI device");
297 /* Generic SPC-2 device */
298 r
= sd_device_get_sysattr_value(dev_scsi
, "vendor", &scsi_vendor
);
300 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI vendor attribute: %m");
303 encode_devnode_name(scsi_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
304 udev_replace_whitespace(scsi_vendor
, vendor_str
, sizeof(vendor_str
)-1);
305 udev_replace_chars(vendor_str
, NULL
);
307 r
= sd_device_get_sysattr_value(dev_scsi
, "model", &scsi_model
);
309 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI model attribute: %m");
312 encode_devnode_name(scsi_model
, model_str_enc
, sizeof(model_str_enc
));
313 udev_replace_whitespace(scsi_model
, model_str
, sizeof(model_str
)-1);
314 udev_replace_chars(model_str
, NULL
);
316 r
= sd_device_get_sysattr_value(dev_scsi
, "type", &scsi_type
);
318 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI type attribute: %m");
321 set_scsi_type(type_str
, scsi_type
, sizeof(type_str
)-1);
323 r
= sd_device_get_sysattr_value(dev_scsi
, "rev", &scsi_rev
);
325 log_device_debug_errno(dev_scsi
, r
, "Failed to get SCSI revision attribute: %m");
328 udev_replace_whitespace(scsi_rev
, revision_str
, sizeof(revision_str
)-1);
329 udev_replace_chars(revision_str
, NULL
);
332 * some broken devices have the same identifiers
333 * for all luns, export the target:lun number
335 sprintf(instance_str
, "%d:%d", target
, lun
);
339 r
= sd_device_get_sysattr_value(dev_usb
, "idVendor", &vendor_id
);
341 return log_device_debug_errno(dev_usb
, r
, "Failed to get idVendor attribute: %m");
343 r
= sd_device_get_sysattr_value(dev_usb
, "idProduct", &product_id
);
345 return log_device_debug_errno(dev_usb
, r
, "Failed to get idProduct attribute: %m");
347 /* fall back to USB vendor & device */
348 if (vendor_str
[0] == '\0') {
349 const char *usb_vendor
;
351 if (sd_device_get_sysattr_value(dev_usb
, "manufacturer", &usb_vendor
) < 0)
352 usb_vendor
= vendor_id
;
353 encode_devnode_name(usb_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
354 udev_replace_whitespace(usb_vendor
, vendor_str
, sizeof(vendor_str
)-1);
355 udev_replace_chars(vendor_str
, NULL
);
358 if (model_str
[0] == '\0') {
359 const char *usb_model
;
361 if (sd_device_get_sysattr_value(dev_usb
, "product", &usb_model
) < 0)
362 usb_model
= product_id
;
363 encode_devnode_name(usb_model
, model_str_enc
, sizeof(model_str_enc
));
364 udev_replace_whitespace(usb_model
, model_str
, sizeof(model_str
)-1);
365 udev_replace_chars(model_str
, NULL
);
368 if (revision_str
[0] == '\0') {
371 if (sd_device_get_sysattr_value(dev_usb
, "bcdDevice", &usb_rev
) >= 0) {
372 udev_replace_whitespace(usb_rev
, revision_str
, sizeof(revision_str
)-1);
373 udev_replace_chars(revision_str
, NULL
);
377 if (serial_str
[0] == '\0') {
378 const char *usb_serial
;
380 if (sd_device_get_sysattr_value(dev_usb
, "serial", &usb_serial
) >= 0) {
381 /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
382 for (const unsigned char *p
= (unsigned char*) usb_serial
; *p
!= '\0'; p
++)
383 if (*p
< 0x20 || *p
> 0x7f || *p
== ',') {
389 udev_replace_whitespace(usb_serial
, serial_str
, sizeof(serial_str
)-1);
390 udev_replace_chars(serial_str
, NULL
);
396 l
= strpcpyl(&s
, sizeof(serial
), vendor_str
, "_", model_str
, NULL
);
397 if (!isempty(serial_str
))
398 l
= strpcpyl(&s
, l
, "_", serial_str
, NULL
);
400 if (!isempty(instance_str
))
401 strpcpyl(&s
, l
, "-", instance_str
, NULL
);
403 if (sd_device_get_property_value(dev
, "ID_BUS", NULL
) >= 0)
404 log_device_debug(dev
, "ID_BUS property is already set, setting only properties prefixed with \"ID_USB_\".");
406 udev_builtin_add_property(event
, "ID_BUS", "usb");
408 udev_builtin_add_property(event
, "ID_MODEL", model_str
);
409 udev_builtin_add_property(event
, "ID_MODEL_ENC", model_str_enc
);
410 udev_builtin_add_property(event
, "ID_MODEL_ID", product_id
);
412 udev_builtin_add_property(event
, "ID_SERIAL", serial
);
413 if (!isempty(serial_str
))
414 udev_builtin_add_property(event
, "ID_SERIAL_SHORT", serial_str
);
416 udev_builtin_add_property(event
, "ID_VENDOR", vendor_str
);
417 udev_builtin_add_property(event
, "ID_VENDOR_ENC", vendor_str_enc
);
418 udev_builtin_add_property(event
, "ID_VENDOR_ID", vendor_id
);
420 udev_builtin_add_property(event
, "ID_REVISION", revision_str
);
422 if (!isempty(type_str
))
423 udev_builtin_add_property(event
, "ID_TYPE", type_str
);
425 if (!isempty(instance_str
))
426 udev_builtin_add_property(event
, "ID_INSTANCE", instance_str
);
429 /* Also export the same values in the above by prefixing ID_USB_. */
430 udev_builtin_add_property(event
, "ID_USB_MODEL", model_str
);
431 udev_builtin_add_property(event
, "ID_USB_MODEL_ENC", model_str_enc
);
432 udev_builtin_add_property(event
, "ID_USB_MODEL_ID", product_id
);
433 udev_builtin_add_property(event
, "ID_USB_SERIAL", serial
);
434 if (!isempty(serial_str
))
435 udev_builtin_add_property(event
, "ID_USB_SERIAL_SHORT", serial_str
);
437 udev_builtin_add_property(event
, "ID_USB_VENDOR", vendor_str
);
438 udev_builtin_add_property(event
, "ID_USB_VENDOR_ENC", vendor_str_enc
);
439 udev_builtin_add_property(event
, "ID_USB_VENDOR_ID", vendor_id
);
441 udev_builtin_add_property(event
, "ID_USB_REVISION", revision_str
);
443 if (!isempty(type_str
))
444 udev_builtin_add_property(event
, "ID_USB_TYPE", type_str
);
446 if (!isempty(instance_str
))
447 udev_builtin_add_property(event
, "ID_USB_INSTANCE", instance_str
);
449 if (!isempty(packed_if_str
))
450 udev_builtin_add_property(event
, "ID_USB_INTERFACES", packed_if_str
);
452 udev_builtin_add_property(event
, "ID_USB_INTERFACE_NUM", ifnum
);
454 udev_builtin_add_property(event
, "ID_USB_DRIVER", driver
);
458 const UdevBuiltin udev_builtin_usb_id
= {
460 .cmd
= builtin_usb_id
,
461 .help
= "USB device properties",