Skip to content

fix(usb): disable all interfaces of composite receivers - #760

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/eaglefrom
Resurgamz:fix/usb-receiver-disable-bug-377327
Sep 17, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/eaglefrom
Resurgamz:fix/usb-receiver-disable-bug-377327

Conversation

@Resurgamz

@Resurgamz Resurgamz commented Sep 16, 2026

Copy link
Copy Markdown

Summary

  • enumerate all USB interfaces belonging to the selected physical receiver
  • update every interface authorization state instead of forcing interface 0
  • reuse the same operation during boot, hotplug, and resume restoration
  • restore original interface states if a sysfs operation fails

Root Cause

Composite keyboard and mouse receivers expose separate interfaces. The existing code rewrote the selected mouse interface path from :1.1 to :1.0, which disabled the keyboard interface while leaving the mouse usable.

Verification

  • built the deepin-devicecontrol target successfully
  • ran focused USB authorization tests covering sibling enumeration, path validation, batch updates, and rollback
  • git diff --check passed

PMS: BUG-377327

Summary by Sourcery

Update USB receiver authorization so composite devices are handled consistently across control and restoration flows.

Bug Fixes:

  • Ensure composite USB receivers disable or enable all of their interfaces together, preventing sibling keyboard or mouse interfaces from remaining usable.
  • Restore prior USB authorization states when an interface update or device reset fails.

Enhancements:

  • Centralize USB interface discovery and authorization handling for device control and boot-time disabled-device restoration.
  • Validate USB sysfs paths and remove interface-path rewriting that incorrectly forced interface 0.

@sourcery-ai

sourcery-ai Bot commented Sep 16, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR centralizes USB authorization in a utility that validates interface paths, enumerates all sibling interfaces of the physical receiver, updates them consistently, and rolls back partial changes on failure. Existing control and boot-restoration flows now reuse this operation instead of rewriting paths to interface 0, while database state is updated only around successful authorization changes.

Sequence diagram for composite USB authorization

sequenceDiagram
    participant Control as ControlInterface
    participant Utils as UsbAuthorizationUtils
    participant Sysfs as USB sysfs
    participant DB as EnableSqlManager

    Control->>Utils: setInterfacesAuthorized(path, enable_device)
    Utils->>Utils: interfacePaths(path, sysRoot)
    Utils->>Sysfs: Read each sibling authorized state
    Utils->>Sysfs: Write authorization for every interface
    alt Any read or write fails
        Utils->>Sysfs: Restore previously changed states
        Utils-->>Control: false
    else All interfaces updated
        Utils-->>Control: true
        Control->>DB: Update authorization record
    end
Loading

Flow diagram for USB interface enumeration and validation

flowchart TD
    A[Selected interface path] --> B{Valid /devices path and interface format?}
    B -- No --> F[Return failure]
    B -- Yes --> C[Locate physical device directory]
    C --> D[Enumerate sibling interface directories]
    D --> E[Keep siblings with authorized files]
    E --> G{Interfaces found?}
    G -- No --> F
    G -- Yes --> H[Return all interface paths]
Loading

File-Level Changes

Change Details Files
Replace single-interface authorization with composite-receiver interface discovery and batch updates.
  • Validate that the requested path is a USB interface path under /devices.
  • Discover sibling interfaces for the same physical USB device and target those exposing authorized files.
  • Remove the old path rewrite that forced interface suffix .0.
  • Use the batch operation for normal enable/disable and boot-time disabled-device restoration.
deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.h
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/usbauthorizationutils.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/usbauthorizationutils.h
Make multi-interface authorization updates failure-safe.
  • Read and record each interface's original authorization state before changing it.
  • Roll back previously updated interfaces if a read or write fails.
  • Return failure when the path is invalid or no matching authorized interfaces are found, with diagnostic logging.
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/usbauthorizationutils.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/usbauthorizationutils.h
Preserve database bookkeeping around the centralized authorization operation.
  • Remove authorized-device records after successful enablement.
  • Insert or update records after successful disablement and retain the current resolved path during boot restoration.
deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/usbauthorizationutils.cpp" line_range="107-108" />
<code_context>
+            continue;
+        }
+
+        for (auto it = originalStates.crbegin(); it != originalStates.crend(); ++it)
+            writeAuthorized(sysRoot + it->first + "/authorized", it->second);
+        return false;
+    }
</code_context>
<issue_to_address>
**issue (broader_impact):** Rollback write failures are ignored, so `setInterfacesAuthorized` returns false even when one or more previously changed interfaces remain in the new authorization state instead of being restored.

**Triggers:** When a rollback write fails after a later interface operation fails.

**Suggested fix:** Check each rollback result, log the incomplete restoration, and expose the restoration failure to the caller.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@Resurgamz
Resurgamz force-pushed the fix/usb-receiver-disable-bug-377327 branch from ca10406 to e74a640 Compare September 16, 2026 07:45
Disable every interface belonging to the selected physical USB device.

禁用所选物理 USB 设备下的全部接口。

Log: 修复无线键鼠接收器仅禁用键盘的问题

PMS: BUG-377327

Influence: 复合 USB 接收器禁用、启用及持久化恢复行为。
@Resurgamz
Resurgamz force-pushed the fix/usb-receiver-disable-bug-377327 branch from e74a640 to 3bf3bab Compare September 17, 2026 02:56
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 97 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 97 分,大于 70 分通过阈值。本次提交修复了复合 USB 设备接口禁用问题,引入 UsbAuthorizationUtils 工具类统一管理所有接口的授权状态,代码结构清晰、安全防护完善、无安全漏洞。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 语法正确,逻辑清晰。physicalDevicePath() 函数正确解析 USB 设备路径和接口路径,setInterfacesAuthorized() 实现了完整的授权设置流程(读取原始状态→写入新状态→设备级 toggle→失败回滚),边界条件处理完善。


2. 代码质量 ✅

评价: 优秀 ✅ 通过

潜在问题:

  1. deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.h:1 - 版权年份格式不一致,'2019-2026 ~ 2023' 混合了 '-' 和 '~' 两种分隔格式

建议: 代码结构清晰,注释完整。建议修正 controlinterface.h 中的版权年份格式,统一为 '2019-2026 UnionTech Software Technology Co., Ltd.'。


3. 代码性能 ✅

评价: 优秀 ✅ 通过

潜在问题:

  1. deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/usbauthorizationutils.cpp:106 - physicalDevicePath() 在 setInterfacesAuthorized() 中被重复调用(直接调用一次,interfacePaths() 内部间接调用一次)

建议: 性能良好,资源使用合理。USB 设备接口数量通常少于 10 个,目录遍历开销可忽略。建议将 physicalDevicePath() 的结果传递给 interfacePaths() 以避免重复计算。


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: 存在0个安全漏洞。安全合规。physicalDevicePath() 包含路径遍历防护(检查 '..' 和 '/devices/' 前缀),authorized 文件值验证为 '0' 或 '1',文件写入操作检查返回值,回滚机制确保失败时恢复原始状态。


💡 改进建议代码示例

// 修正版权年份格式
// SPDX-FileCopyrightText: 2019-2026 UnionTech Software Technology Co., Ltd.

// 避免重复调用 physicalDevicePath()
bool UsbAuthorizationUtils::setInterfacesAuthorized(const QString &path, bool authorized, const QString &sysRoot)
{
    const QString devicePath = physicalDevicePath(path, sysRoot);
    if (devicePath.isEmpty()) {
        qWarning() << "No USB device found for" << path;
        return false;
    }
    // 直接使用 devicePath 枚举接口,避免在 interfacePaths() 中重复调用
    const QStringList interfaces = interfacePathsFromDevice(devicePath, sysRoot);
    // ...
}

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: max-lvs, Resurgamz

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Resurgamz

Copy link
Copy Markdown
Author

/merge

@deepin-bot
deepin-bot Bot merged commit d042319 into linuxdeepin:develop/eagle Sep 17, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants