From 58b7872185b471f4bb8c3dad56dabed92b00f8a0 Mon Sep 17 00:00:00 2001 From: Piotr Bartman-Szwarc Date: Sat, 18 May 2024 21:11:26 +0200 Subject: [PATCH] q-dev: keep description as in `lspci/lsusb` --- qubes/device_protocol.py | 23 ++++++++++++++--------- qubes/ext/pci.py | 2 +- qubes/tests/devices_pci.py | 4 ++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/qubes/device_protocol.py b/qubes/device_protocol.py index f9833854b..3d8b9aaa6 100644 --- a/qubes/device_protocol.py +++ b/qubes/device_protocol.py @@ -370,12 +370,17 @@ def __str__(self): if self.devclass == "block": return "Block device" if self.devclass in ("usb", "pci"): + # try subclass first as in `lspci` result = self._load_classes(self.devclass).get( - self._interface_encoding[1:], None) - if result is None: + self._interface_encoding[1:-2] + '**', None) + if (result is None or result.lower() + in ('none', 'no subclass', 'unused', 'undefined')): + # if not, try interface result = self._load_classes(self.devclass).get( - self._interface_encoding[1:-2] + '**', None) - if result is None: + self._interface_encoding[1:], None) + if (result is None or result.lower() + in ('none', 'no subclass', 'unused', 'undefined')): + # if not, try class result = self._load_classes(self.devclass).get( self._interface_encoding[1:-4] + '****', None) if result is None: @@ -399,21 +404,20 @@ def _load_classes(bus: str): encoding='utf-8', errors='ignore') as pciids: # for `class_name` and `subclass_name` # pylint: disable=used-before-assignment - # pylint: disable=possibly-used-before-assignment class_id = None subclass_id = None for line in pciids.readlines(): line = line.rstrip() if line.startswith('\t\t') \ - and class_id is not None and subclass_id is not None: + and class_id is not None and subclass_id is not None: (progif_id, _, progif_name) = line[2:].split(' ', 2) result[class_id + subclass_id + progif_id] = \ - f"{class_name}: {subclass_name} ({progif_name})" + progif_name elif line.startswith('\t') and class_id: (subclass_id, _, subclass_name) = line[1:].split(' ', 2) # store both prog-if specific entry and generic one result[class_id + subclass_id + '**'] = \ - f"{class_name}: {subclass_name}" + subclass_name elif line.startswith('C '): (_, class_id, _, class_name) = line.split(' ', 3) result[class_id + '****'] = class_name @@ -548,7 +552,8 @@ def description(self) -> str: else: vendor = "unknown vendor" - return f"{prod} ({vendor})" + main_interface = str(self.interfaces[0]) + return f"{main_interface}: {vendor} {prod}" @property def interfaces(self) -> List[DeviceInterface]: diff --git a/qubes/ext/pci.py b/qubes/ext/pci.py index 9915ffd1a..1535a7f9d 100644 --- a/qubes/ext/pci.py +++ b/qubes/ext/pci.py @@ -145,7 +145,7 @@ def attached_devices(app): def _device_desc(hostdev_xml): - return '{devclass}: {product} ({vendor})'.format( + return '{devclass}: {vendor} {product}'.format( devclass=pcidev_class(hostdev_xml), vendor=hostdev_xml.findtext('capability/vendor'), product=hostdev_xml.findtext('capability/product'), diff --git a/qubes/tests/devices_pci.py b/qubes/tests/devices_pci.py index 072877221..01011cadd 100644 --- a/qubes/tests/devices_pci.py +++ b/qubes/tests/devices_pci.py @@ -151,6 +151,6 @@ def test_000_unsupported_device(self): self.assertEqual(devices[0].parent_device, None) self.assertEqual(devices[0].libvirt_name, "pci_0000_00_14_0") self.assertEqual(devices[0].description, - "USB controller: 9 Series Chipset Family " - "USB xHCI Controller (Intel Corporation)") + "USB controller: Intel Corporation 9 Series " + "Chipset Family USB xHCI Controller") self.assertEqual(devices[0].self_identity, "0x8086:0x8cb1::p0c0330")