xHCI的TRB_PORT_STATUS:端口事件
前面知道,XHCI的中断分为4种事件,分别为
- TRB_TRANSFER: 数据传输完成了,去处理 URB。
- TRB_COMPLETION:发向XHCI的TRB完成了
- TRB_PORT_STATUS:端口状态变了,去枚举或移除设备。
- TRB_DEV_NOTE: 设备发来通知了,通常是远程唤醒。
TRB_PORT_STATUS 是传门用于处理端口变化的,函数名为handle_port_status
[硬件]
PORTSC 中 CSC/PEC/OCC/PLC/WRC/CEC 等变化
→ xHC 写 Port Status Change Event TRB(type=34)
→ USBSTS.EINT = 1
[硬中断]
xhci_irq() xhci-ring.c:3184
├─ spin_lock(&xhci->lock)
├─ status = readl(&xhci->op_regs->status)
├─ 检查 EINT / HCE / FATAL
├─ writel(status | STS_EINT, &xhci->op_regs->status)
├─ xhci_handle_events(xhci, interrupters[0], false)
│ │ xhci-ring.c:3093
│ ├─ xhci_clear_interrupt_pending(ir) xhci-ring.c:3075
│ ├─ while (unhandled_event_trb(ir->event_ring))
│ │ │ xhci-ring.c:135
│ │ ├─ xhci_handle_event_trb(xhci, ir, event)
│ │ │ │ xhci-ring.c:2993
│ │ │ └─ trb_type == TRB_PORT_STATUS(34)
│ │ │ └─ handle_port_status(xhci, event)
│ │ │ xhci-ring.c:1999
│ │ └─ inc_deq(xhci, ir->event_ring) xhci-ring.c:186
│ └─ xhci_update_erst_dequeue(xhci, ir, true) xhci-ring.c:3045
└─ spin_unlock(&xhci->lock)
return IRQ_HANDLED
Port Status Change Event TRB
Port Status Change Event TRB 是专门用于返回端口变化事件。
struct xhci_generic_trb {
__le32 field[4];
};
- Field[0]: PortId[24:31]
- Field[1]: reserved
- Field[2]: CompletionCode[24:31]
- Field[3]:
- C[0]
- TRB Type[10:15]
handle_port_status将中断事件分为两类:
- 第一类为XHCI特有的中断事件,这需要xHCI自行处理
- 第二类为标准的HUB事件,这里只需要构造相关的数据,完成HUB的中断端点Urb,交由HUB的中断端点处理完成函数即可。
xHCI特有的中断事件
HCD/根集线器处于 suspend
→ 调usb_hcd_resume_root_hub()唤醒根集线器,不是直接恢复某个设备。USB3 RESUME 事件
→ 设备远程唤醒;驱动置port_remote_wakeup,写PLS=U0。
→ 等后续链路到 U0 的 PLC 事件,complete(u3exit_done),再 ring doorbell。USB2 RESUME 事件
→ 设备远程唤醒时,K状态是设备自己驱动的,驱动只启动 20ms 计时。
→ 20ms 后由GetPortStatus写PLS=U0,并等rexit_done。
→ 主机主动 resume 时,才是软件写Resume驱动 K。另外两个 xHCI 特有处理
→ USB3 链路 Inactive:置VDEV_PORT_ERROR,拒绝新 URB;PORT_RC后清除。
→ 事件已在 xHCI 内部处理完:用bogus_port_status抑制,不再交给 hub core。
- USB3 是“写 U0 → 等 PLC → 完成”;
- USB2 是“等 20ms → 写 U0 → 等 rexit”;
- HCD suspend 只负责唤醒根集线器,设备 resume 交给后面的 hub 流程。
1.挂起/恢复相关
| 场景 | 触发条件 | handle_port_status 动作 | 目的 |
|---|---|---|---|
| HCD 处于 suspend | 任意端口事件,hcd->state == HC_STATE_SUSPENDED | usb_hcd_resume_root_hub(hcd) | 先唤醒根集线器,后续才能处理端口事件 |
| USB3 远程唤醒开始 | PORT_PLC && PLS == XDEV_RESUME 且 SuperSpeed | 置 port_remote_wakeup;清 PLC;usb_hcd_start_port_resume();写 XDEV_U0;标 bogus | 设备发 LFPS,xHC 进入 Resume;驱动主动把链路拉回 U0 |
| USB3 U3 退出完成 | PORT_PLC && SS && PLS == U0/U1/U2 | complete(&port->u3exit_done);xhci_ring_device();若是远程唤醒则清 PLC、usb_wakeup_notification();标 bogus | 通知等待者链路已回 U0,并重新 ring 所有端点 doorbell,恢复传输 |
| USB2 远程唤醒开始 | PORT_PLC && PLS == XDEV_RESUME 且非 SuperSpeed | 设置 resume_timestamp(20ms);置 resuming_ports;启动根集线器轮询定时器;usb_hcd_start_port_resume();标 bogus | USB2 设备驱动 K 状态;驱动等待 20ms 后在 GetPortStatus 中完成 U0 切换 |
| USB2 resume 完成通知 | hcd->speed < HCD_USB3 && port->rexit_active | complete(&port->rexit_done);清 rexit_active;标 bogus | 唤醒 xhci_handle_usb2_port_link_resume(),让它完成 U0 切换和 ring doorbell |
| USB2 清 PLC | hcd->speed < HCD_USB3 | xhci_test_and_clear_bit(PORT_PLC) | 避免 USB2 resume/link 事件重复上报 |
注意:USB2 的 U0 切换不在 handle_port_status() 里完成,而是在后续 GetPortStatus 时由 xhci_handle_usb2_port_link_resume() 完成;这里只负责启动 20ms 计时和唤醒等待者。
2.链路错误相关
| 场景 | 触发条件 | 动作 | 目的 | |
|---|---|---|---|---|
| USB3 链路 Inactive | PLS == XDEV_INACTIVE 且无 PORT_RESET | vdev->flags | = VDEV_PORT_ERROR | 标记端口错误,xhci_urb_enqueue() 会拒绝新 URB,避免向坏链路发数据 |
| Inactive 恢复 | PORT_RC 置位 | vdev->flags &= ~VDEV_PORT_ERROR | 端口复位完成后清除错误,允许新的传输 |
3.硬件 quirk 与事件抑制
| 场景 | 触发条件 | 动作 | 目的 |
|---|---|---|---|
| Cavium PHY quirk | XHCI_RESET_PLL_ON_DISCONNECT && PORT_CSC && !PORT_CONNECT | xhci_cavium_reset_phy_quirk() | ThunderX2 errata:USB2 设备断开后复位 PHY,避免下一次设备识别成 USB1 |
| 事件已内部消化 | bogus_port_status = true | 直接 return | 不再把 Resume/U3 exit 等内部事件重复上报给通用 hub 驱动 |
| 需要通用 hub 处理 | bogus_port_status = false | set_bit(HCD_FLAG_POLL_RH);usb_hcd_poll_rh_status() | 把 CSC/PEC/OCC/WRC/CEC/普通 PLC 交给通用 hub 驱动处理 |
RootHub通用中断
usb_hcd_poll_rh_status 函数用于处理通用中断。
void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
{
char buffer[6];
...
//端口 1 对应 bit1;
//端口 2 对应 bit2;
length = hcd->driver->hub_status_data(hcd, buffer);//xhci_hub_status_data
if (length > 0) {
...
urb->actual_length = length;
memcpy(urb->transfer_buffer, buffer, length);
//完成根HUB的URB,返回bitmap
usb_hcd_unlink_urb_from_ep(hcd, urb);
usb_hcd_giveback_urb(hcd, urb, status);
}
}
- xhci_get_port_status():把 PORTSC 翻译成标准 hub class 状态;
- hub_event():根据 USB_PORT_STAT_C 做插入、拔出、过流、warm reset 等处理。
xhci_hub_status_data读取了各个端口是否有变化,并存在buffer中,这样完成URB时,中断会根据那个端口是否有变化然后调用xhci_get_port_status来获取实际的变化。
usb_hcd_poll_rh_status()
→ xhci_hub_status_data() // 生成位图
→ 完成 hcd->status_urb
→ hub_irq()
→ 解析位图到 hub->event_bits
→ kick_hub_wq()
→ hub_event()
→ 对每个有变化的端口发 GetPortStatus
→ xhci_hub_control(GetPortStatus)
→ xhci_get_port_status()
→ xhci_get_usb3_port_status()
→ xhci_get_usb2_port_status()
本文链接为:http://www.usbzh.com/article/detail-1832.html ,欢迎转载,转载请附上本文链接。
XHCI 1.2b 规范





