usb hub状态 改变状态

本文详细解析了USB Hub的请求命令,包括获取Hub状态(GetHubStatus)及端口状态(GetPortStatus)的具体操作与数据结构。深入探讨了各状态位的意义及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 前言

在前文中,我们看过hub的处理并没有对hub的请求命令做个详细的讲解,在本文中,我们会针对这些hub的请求来看hub的处理,也可以说是从另一个方面来看hub,主要是结合usb2.0协议来看。

二. hub请求命令

1.     GetHub Status

bRequestType

bRequest

wValue

wIndex

wLength

Data

10100000B

GET_ STATUS

0

0

4

Hub status and Change Status

struct usb_hub_status {

       __le16wHubStatus;    --hub状态

       __le16 wHubChange;   --hub改变

} __attribute__ ((packed));

下面来说明这两个成员各个bit位的含义

wHubStatus

该成员表明hub的电源状态

bit 0 : 该位表明hub的电源是依靠本地供电还是外部电源。0表明本地供电良好,1表明本地供电不良。

Bit1 : 该位表明是否有过流存在 0 表明无过流存在 1表明有过流存在。如果所有的端口电流总和超过了hub支持电流的最大值则会设置该位,同时所有的端口都将处于断电状态。

Bit2~bit15 : 保留

wHubChange:

而该成员则用于表示电源状态是否改变

Bit 0 : 本地电源改变。0 本地电源状态没有改变 1 改变。其实该域的变化与否跟上面的wHubStatus的值息息相关,如果wHubStatus的bit0从0->1或从1->0,那该位会设置。clear_feature时其对应的是C_HUB_LOCAL_POWER

Bit1 :过流状态是否改变。该bit的设定与上面相同。在clear_feature时其对应的是C_HUB_OVER_CURRENT

2.     GetPort Status

bRequestType

bRequest

wValue

wIndex

wLength

Data

10100011B

GET_ STATUS

0

Port

4

Port status and Change Status

struct usb_port_status {

       __le16 wPortStatus;

       __le16wPortChange;

} __attribute__ ((packed));

wPortStatus:

Bit0:当前连接状态(PORT_CONNECTION) 0 无设备连接 1 有设备连接

Bit1:端口使能与否(PORT_ENABLE) 0 禁止端口 1 使能端口 该位仅能有usb系统软件设定。

Bit2:挂起(PORT_SUSPEND) 0 未挂起 1 挂起或恢复

Bit3:过流(PORT_OVER_CURRENT) 0 无过流 1 存在过流

Bit4:复位(PORT_RESET) 0 无复位信号 1 复位信号 如果主机想复位该端口连接的设备则设置该位,该位会一直保留设置直到hub不鸟复位信号。

Bit5~bit7:保留

Bit8:端口电源(PORT_POWER) 0 该端口处于断电 1 该端口供电正常

Bit9:连接设备为低速设备(PORT_LOW_SPEED) 0 全速或高速设备链接在该端口 1 该端口链接的设备为低速设备

Bit10:连接设备为高速设备(PORT_HIGH_SPEED) 0 全速设备连接在该端口 1 高速设备链接在该端口

Bit11:端口测试模式(PORT_TEST) 0 端口未处在端口测试模式 1 处于端口测试模式

Bit12:端口指示灯控制(PORT_INDICATOR) 0 端口指示灯为默认颜色 1 端口指示灯软件控制颜色

Bit13~bit15:保留

wPortChange:

bit0:链接状态改变(C_PORT_CONNECTION) 0 没有改变 1 改变

bit1:端口禁止/使能改变(C_PORT_ENABLE) 该bit位会由于Port_Error条件导致端口禁止时设置1

bit2:挂起改变(C_PORT_SUSPEND) 0 没有改变 1 恢复结束

bit3:过流指示改变(C_PORT_OVER_CURRENT) 0 无过流 1 过流指示改变

bit4:复位改变(C_PORT_RESET) 0 无改变 1 复位结束

bit5~bit15:保留

集线器类的请求:请求集线器描述符 a0  06 

地址偏移量

字段名

长度/字节

说明

当前值

0

bDescLength

可变

长度 09

09

1

bDescriptorType

1

描述符的类型:29 for hub

29

2

bNbrPorts

1

支持的下行口数量

04

3

wHubCharacteristics

2

Hub特性

00e0

5

bPwrOn2PwrGood

1

端口上电时间  ×2ms

32

6

bHubContrCurrent

1

Hub需要的最大电流

64

7

DeviceRemovable

可变的,决定于端口的         数量

指示端口是否有一个可删除的设备

00

Variable

PortPwrCtrlMask

可变,决定于端口的         数量

This field exists for reasons of compatibility with software written for 1.0 compliant devices.

Ff

GetPortStatus a3 00 00hub class request  

 端口状态请求。01表示port 1 04 data length, 设备返回的4byte The first word of data contains wPortStatus (refer to Table 11-21).  The second word of data contains wPortChange (refer to Table 11-22).   See USB 2.0 Specification section 11.24.2.7.

Table 11-21端点状态域(wPortStatus

Bit

Description

当前值

0

Current Connect Status:   (PORT_CONNECTION) 当前端口上是否有一个设备链接。

0 = 没有设备存在

1 = 端口上有一个设备.

1

1

Port Enabled/Disabled:  (PORT_ENABLE) Ports can be enabled by the USB System Software only. 

0 = Port is disabled.

1 = Port is enabled.

0

2

Suspend:  (PORT_SUSPEND) This field indicates whether or not the device on this port is suspended.

0 = Not suspended.

1 = Suspended or resuming.

0

3

Over-current:  (PORT_OVER_CURRENT)

If the hub reports over-current conditions on a per-port basis, this field will indicate that port exceeds the specified maximum. 

0 = All no over-current condition exists on this port.

1 = An over-current condition exists on this port.

0

4

Reset:  (PORT_RESET) This field is set when the host wishes to reset the attached device.  It remains set until the reset signaling is turned off by the hub.

0 = Reset signaling not asserted.

1 = Reset signaling asserted.

 

5-7

保留

0

8

Port Power:  (PORT_POWER) This field reflects a port’s logical, power control state.  Because hubs can implement different methods of port power switching, this field may or may not represent whether power is applied to the port. The device descriptor reports the type of power switching implemented by the hub.

0 = This port is in the Powered-off state.

1 = This port is not in the Powered-off state.

0

9

Low- Speed Device Attached:  (PORT_LOW_SPEED) This is relevant only if a device is attached.

0 = Full-speed or High-speed device attached to this port (determined by bit 10).

1 = Low-speed device attached to this port.

0

10

High-speed Device Attached:  (PORT_HIGH_SPEED) This is relevant only if a device is attached.

0 = Full-speed device attached to this port.

1 = High-speed device attached to this port.

0

11

Port Test Mode : (PORT_TEST) This field reflects the status of the port's test mode.  Software uses the SetPortFeature() and ClearPortFeature() requests to manipulate the port test mode.

0 = This port is not in the Port Test Mode.

1 = This port is in Port Test Mode.

0

12

Port Indicator Control: (PORT_INDICATOR) This field is set to reflect software control of the port indicator.For more details see Sections 11.5.3, 11.24.2.7.1.10, and 11.24.2.13.

0 = Port indicator displays default colors.

1 = Port indicator displays software controlled color.

0

13-15

保留

0

Table 11-20.  Hub Change Field, wHubChange

Bit

Description

.

 

 

0

Connect Status Change:  (C_PORT_CONNECTION)  Indicates a change has occurred in the port’s Current Connect Status.  The hub device sets this field as described in Section 11.24.2.7.2.1.

0 = No change has occurred to Current Connect status.

1 = Current Connect status has changed

0

1

Port Enable/Disable Change:  (C_PORT_ENABLE) This field is set to one when a port is disabled becauseof a Port_Error condition (see Section 11.8.1).

0

2

Suspend Change:  (C_PORT_SUSPEND) This field indicates a change in the host-visible suspend state of the attached device.  It indicates the device has transitioned out of the Suspend state.  This field is set only when the entire resume process has completed.  That is, the hub has ceased signaling resume on this port.

0 = No change.

1 = Resume complete.

0

3

Over-Current Indicator Change:  (C_PORT_OVER_CURRENT) This field applies only to hubs that report over-current conditions on a per-port basis (as reported in the hub descriptor).

0 = No change has occurred to Over-Current Indicator.

1 = Over-Current Indicator has changed.

If the hub does not report over-current on a per-port basis, then this field is always zero.

 

4

Reset Change:  (C_PORT_RESET) This field is set when reset processing on this port is complete.

0 = No change.

1 = Reset complete.

0

5-15

Reserved     These bits return 0 when read.

0


### Linux USB Hub 驱动源码位置 Linux 内核中的 USB HUB 驱动程序位于 `drivers/usb/core/hub.c` 文件中[^4]。该文件实现了 USB 设备连接到主机后的枚举过程以及管理多个端口的功能。HUB 驱动的核心功能包括检测设备插拔事件、配置端口状态以及初始化新连接的 USB 设备。 以下是部分关键函数及其作用: - **`hub_thread()`**: 这是一个周期运行的任务,用于监控 HUB状态变化并处理各种事件,例如设备插入或移除[^5]。 - **`usb_hub_init()`**: 初始化 USB HUB 子系统,在内核启动时调用以注册必要的回调函数和数据结构[^6]。 - **`usb_new_device()`**: 当发现新的 USB 设备时被调用,负责分配资源并将设备加入到系统的设备树中[^7]。 #### 关键代码片段 以下是从 `hub.c` 中提取的部分核心逻辑: ```c static int hub_port_connect_change(struct usb_hub *hub, int port1, u16 portstatus, u16 portchange) { struct usb_device *hdev = hub->hdev; unsigned long flags; spin_lock_irqsave(&hub_event_lock, flags); /* 处理端口状态的变化 */ if (portchange & PORT_C_CONNECTION) { clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); dev_dbg(&hdev->dev, "connect change on port %d\n", port1); } spin_unlock_irqrestore(&hub_event_lock, flags); return 0; } ``` 此函数主要用于响应 HUB 上某个端口的状态改变事件,并执行相应的操作来更新内核中的设备信息。 ### 实现细节概述 USB HUB 驱动通过与硬件交互获取当前各个端口的工作情况,并向上层报告这些动态变更的信息。它依赖于通用的 USB 核心框架所提供的接口完成大部分基础工作,比如发送控制请求给根集线器或者外部物理集线器芯片组等[^8]。 另外需要注意的是,现代版本的 Linux 内核可能已经对这部分代码进行了重构优化,因此建议开发者查阅最新稳定版内核文档及源码获得最精确的第一手资料。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值