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>
8 * Copyright (C) 2005-2011 Kay Sievers <kay@vrfy.org>
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "alloc-util.h"
35 #include "string-util.h"
38 static void set_usb_iftype(char *to
, int if_class_num
, size_t len
) {
39 const char *type
= "generic";
41 switch (if_class_num
) {
45 case 2: /* CDC-Control */
50 case 5: /* Physical */
64 case 0x0a: /* CDC-Data */
66 case 0x0b: /* Chip/Smart Card */
68 case 0x0d: /* Content Security */
73 case 0xdc: /* Diagnostic Device */
75 case 0xe0: /* Wireless Controller */
77 case 0xfe: /* Application-specific */
79 case 0xff: /* Vendor-specific */
84 strncpy(to
, type
, len
);
88 static int set_usb_mass_storage_ifsubtype(char *to
, const char *from
, size_t len
) {
91 const char *type
= "generic";
93 type_num
= strtoul(from
, &eptr
, 0);
96 case 1: /* RBC devices */
108 case 6: /* Transparent SPC-2 devices */
115 strscpy(to
, len
, type
);
119 static void set_scsi_type(char *to
, const char *from
, size_t len
) {
122 const char *type
= "generic";
124 type_num
= strtoul(from
, &eptr
, 0);
146 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
) {
153 _cleanup_free_
char *filename
= NULL
;
154 _cleanup_close_
int fd
= -1;
156 unsigned char buf
[18 + 65535];
159 struct usb_interface_descriptor
{
161 uint8_t bDescriptorType
;
162 uint8_t bInterfaceNumber
;
163 uint8_t bAlternateSetting
;
164 uint8_t bNumEndpoints
;
165 uint8_t bInterfaceClass
;
166 uint8_t bInterfaceSubClass
;
167 uint8_t bInterfaceProtocol
;
171 if (asprintf(&filename
, "%s/descriptors", udev_device_get_syspath(dev
)) < 0)
174 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
);
176 return log_debug_errno(errno
, "Error opening USB device 'descriptors' file: %m");
178 size
= read(fd
, buf
, sizeof(buf
));
179 if (size
< 18 || (size_t) size
>= sizeof(buf
))
183 while (pos
+ sizeof(struct usb_interface_descriptor
) < (size_t) size
&&
184 strpos
+ 7 < len
- 2) {
186 struct usb_interface_descriptor
*desc
;
189 desc
= (struct usb_interface_descriptor
*) &buf
[pos
];
190 if (desc
->bLength
< 3)
192 pos
+= desc
->bLength
;
194 if (desc
->bDescriptorType
!= USB_DT_INTERFACE
)
197 if (snprintf(if_str
, 8, ":%02x%02x%02x",
198 desc
->bInterfaceClass
,
199 desc
->bInterfaceSubClass
,
200 desc
->bInterfaceProtocol
) != 7)
203 if (strstr(ifs_str
, if_str
) != NULL
)
206 memcpy(&ifs_str
[strpos
], if_str
, 8),
211 ifs_str
[strpos
++] = ':';
212 ifs_str
[strpos
++] = '\0';
219 * A unique USB identification is generated like this:
221 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
222 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC',
223 * use the SCSI vendor and model as USB-Vendor and USB-model.
224 * 3.) Otherwise, use the USB manufacturer and product as
225 * USB-Vendor and USB-model. Any non-printable characters
226 * in those strings will be skipped; a slash '/' will be converted
227 * into a full stop '.'.
228 * 4.) If that fails, too, we will use idVendor and idProduct
229 * as USB-Vendor and USB-model.
230 * 5.) The USB identification is the USB-vendor and USB-model
231 * string concatenated with an underscore '_'.
232 * 6.) If the device supplies a serial number, this number
233 * is concatenated with the identification with an underscore '_'.
235 static int builtin_usb_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
236 char vendor_str
[64] = "";
237 char vendor_str_enc
[256];
238 const char *vendor_id
;
239 char model_str
[64] = "";
240 char model_str_enc
[256];
241 const char *product_id
;
242 char serial_str
[UTIL_NAME_SIZE
] = "";
243 char packed_if_str
[UTIL_NAME_SIZE
] = "";
244 char revision_str
[64] = "";
245 char type_str
[64] = "";
246 char instance_str
[64] = "";
247 const char *ifnum
= NULL
;
248 const char *driver
= NULL
;
251 struct udev_device
*dev_interface
= NULL
;
252 struct udev_device
*dev_usb
= NULL
;
253 const char *if_class
, *if_subclass
;
261 /* shortcut, if we are called directly for a "usb_device" type */
262 if (udev_device_get_devtype(dev
) != NULL
&& streq(udev_device_get_devtype(dev
), "usb_device")) {
263 dev_if_packed_info(dev
, packed_if_str
, sizeof(packed_if_str
));
268 /* usb interface directory */
269 dev_interface
= udev_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface");
270 if (dev_interface
== NULL
) {
271 log_debug("unable to access usb_interface device of '%s'",
272 udev_device_get_syspath(dev
));
276 ifnum
= udev_device_get_sysattr_value(dev_interface
, "bInterfaceNumber");
277 driver
= udev_device_get_sysattr_value(dev_interface
, "driver");
279 if_class
= udev_device_get_sysattr_value(dev_interface
, "bInterfaceClass");
281 log_debug("%s: cannot get bInterfaceClass attribute",
282 udev_device_get_sysname(dev
));
286 if_class_num
= strtoul(if_class
, NULL
, 16);
287 if (if_class_num
== 8) {
289 if_subclass
= udev_device_get_sysattr_value(dev_interface
, "bInterfaceSubClass");
290 if (if_subclass
!= NULL
)
291 protocol
= set_usb_mass_storage_ifsubtype(type_str
, if_subclass
, sizeof(type_str
)-1);
293 set_usb_iftype(type_str
, if_class_num
, sizeof(type_str
)-1);
296 log_debug("%s: if_class %d protocol %d",
297 udev_device_get_syspath(dev_interface
), if_class_num
, protocol
);
299 /* usb device directory */
300 dev_usb
= udev_device_get_parent_with_subsystem_devtype(dev_interface
, "usb", "usb_device");
302 log_debug("unable to find parent 'usb' device of '%s'",
303 udev_device_get_syspath(dev
));
307 /* all interfaces of the device in a single string */
308 dev_if_packed_info(dev_usb
, packed_if_str
, sizeof(packed_if_str
));
310 /* mass storage : SCSI or ATAPI */
311 if (IN_SET(protocol
, 6, 2)) {
312 struct udev_device
*dev_scsi
;
313 const char *scsi_model
, *scsi_vendor
, *scsi_type
, *scsi_rev
;
314 int host
, bus
, target
, lun
;
316 /* get scsi device */
317 dev_scsi
= udev_device_get_parent_with_subsystem_devtype(dev
, "scsi", "scsi_device");
318 if (dev_scsi
== NULL
) {
319 log_debug("unable to find parent 'scsi' device of '%s'",
320 udev_device_get_syspath(dev
));
323 if (sscanf(udev_device_get_sysname(dev_scsi
), "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4) {
324 log_debug("invalid scsi device '%s'", udev_device_get_sysname(dev_scsi
));
328 /* Generic SPC-2 device */
329 scsi_vendor
= udev_device_get_sysattr_value(dev_scsi
, "vendor");
331 log_debug("%s: cannot get SCSI vendor attribute",
332 udev_device_get_sysname(dev_scsi
));
335 udev_util_encode_string(scsi_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
336 util_replace_whitespace(scsi_vendor
, vendor_str
, sizeof(vendor_str
)-1);
337 util_replace_chars(vendor_str
, NULL
);
339 scsi_model
= udev_device_get_sysattr_value(dev_scsi
, "model");
341 log_debug("%s: cannot get SCSI model attribute",
342 udev_device_get_sysname(dev_scsi
));
345 udev_util_encode_string(scsi_model
, model_str_enc
, sizeof(model_str_enc
));
346 util_replace_whitespace(scsi_model
, model_str
, sizeof(model_str
)-1);
347 util_replace_chars(model_str
, NULL
);
349 scsi_type
= udev_device_get_sysattr_value(dev_scsi
, "type");
351 log_debug("%s: cannot get SCSI type attribute",
352 udev_device_get_sysname(dev_scsi
));
355 set_scsi_type(type_str
, scsi_type
, sizeof(type_str
)-1);
357 scsi_rev
= udev_device_get_sysattr_value(dev_scsi
, "rev");
359 log_debug("%s: cannot get SCSI revision attribute",
360 udev_device_get_sysname(dev_scsi
));
363 util_replace_whitespace(scsi_rev
, revision_str
, sizeof(revision_str
)-1);
364 util_replace_chars(revision_str
, NULL
);
367 * some broken devices have the same identifiers
368 * for all luns, export the target:lun number
370 sprintf(instance_str
, "%d:%d", target
, lun
);
374 vendor_id
= udev_device_get_sysattr_value(dev_usb
, "idVendor");
375 product_id
= udev_device_get_sysattr_value(dev_usb
, "idProduct");
377 /* fallback to USB vendor & device */
378 if (vendor_str
[0] == '\0') {
379 const char *usb_vendor
= NULL
;
381 usb_vendor
= udev_device_get_sysattr_value(dev_usb
, "manufacturer");
383 usb_vendor
= vendor_id
;
385 log_debug("No USB vendor information available");
388 udev_util_encode_string(usb_vendor
, vendor_str_enc
, sizeof(vendor_str_enc
));
389 util_replace_whitespace(usb_vendor
, vendor_str
, sizeof(vendor_str
)-1);
390 util_replace_chars(vendor_str
, NULL
);
393 if (model_str
[0] == '\0') {
394 const char *usb_model
= NULL
;
396 usb_model
= udev_device_get_sysattr_value(dev_usb
, "product");
398 usb_model
= product_id
;
401 udev_util_encode_string(usb_model
, model_str_enc
, sizeof(model_str_enc
));
402 util_replace_whitespace(usb_model
, model_str
, sizeof(model_str
)-1);
403 util_replace_chars(model_str
, NULL
);
406 if (revision_str
[0] == '\0') {
409 usb_rev
= udev_device_get_sysattr_value(dev_usb
, "bcdDevice");
411 util_replace_whitespace(usb_rev
, revision_str
, sizeof(revision_str
)-1);
412 util_replace_chars(revision_str
, NULL
);
416 if (serial_str
[0] == '\0') {
417 const char *usb_serial
;
419 usb_serial
= udev_device_get_sysattr_value(dev_usb
, "serial");
421 const unsigned char *p
;
423 /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
424 for (p
= (unsigned char *)usb_serial
; *p
!= '\0'; p
++)
425 if (*p
< 0x20 || *p
> 0x7f || *p
== ',') {
432 util_replace_whitespace(usb_serial
, serial_str
, sizeof(serial_str
)-1);
433 util_replace_chars(serial_str
, NULL
);
438 l
= strpcpyl(&s
, sizeof(serial
), vendor_str
, "_", model_str
, NULL
);
439 if (!isempty(serial_str
))
440 l
= strpcpyl(&s
, l
, "_", serial_str
, NULL
);
442 if (!isempty(instance_str
))
443 strpcpyl(&s
, l
, "-", instance_str
, NULL
);
445 udev_builtin_add_property(dev
, test
, "ID_VENDOR", vendor_str
);
446 udev_builtin_add_property(dev
, test
, "ID_VENDOR_ENC", vendor_str_enc
);
447 udev_builtin_add_property(dev
, test
, "ID_VENDOR_ID", vendor_id
);
448 udev_builtin_add_property(dev
, test
, "ID_MODEL", model_str
);
449 udev_builtin_add_property(dev
, test
, "ID_MODEL_ENC", model_str_enc
);
450 udev_builtin_add_property(dev
, test
, "ID_MODEL_ID", product_id
);
451 udev_builtin_add_property(dev
, test
, "ID_REVISION", revision_str
);
452 udev_builtin_add_property(dev
, test
, "ID_SERIAL", serial
);
453 if (!isempty(serial_str
))
454 udev_builtin_add_property(dev
, test
, "ID_SERIAL_SHORT", serial_str
);
455 if (!isempty(type_str
))
456 udev_builtin_add_property(dev
, test
, "ID_TYPE", type_str
);
457 if (!isempty(instance_str
))
458 udev_builtin_add_property(dev
, test
, "ID_INSTANCE", instance_str
);
459 udev_builtin_add_property(dev
, test
, "ID_BUS", "usb");
460 if (!isempty(packed_if_str
))
461 udev_builtin_add_property(dev
, test
, "ID_USB_INTERFACES", packed_if_str
);
463 udev_builtin_add_property(dev
, test
, "ID_USB_INTERFACE_NUM", ifnum
);
465 udev_builtin_add_property(dev
, test
, "ID_USB_DRIVER", driver
);
469 const struct udev_builtin udev_builtin_usb_id
= {
471 .cmd
= builtin_usb_id
,
472 .help
= "USB device properties",