Android USB电源管理

The USB peripheral detects the lack of 3 consecutive SOF packets as a suspend request from the USB host.

1 驱动shutdown顺序
系统关机或重启的过程中,会调用设备驱动的shutdown函数来完成设备的关闭操作,有需要的设备可以在驱动中定义该函数。其调用流程如下:
kernel_restart
kernel_restart_prepare
device_shutdown // 逆向遍历devices_kset->list所有device
dev->driver->shutdown

由此可见,各个驱动shutdown的顺序由设备在链表中的位置决定,后添加的先调用。设备添加到链表中的流程如下:

device_initialize
device_add
kobject_add
kobj_kset_join
list_add_tail(&kobj->entry, &kobj->kset->list)

由此可见,设备注册时,会把节点添加到devices_kset->list末尾。因此驱动的shutdown顺序是设备注册的逆序,而在通过dts创建设备的系统中,设备的注册顺序是设备节点在dts中的前后顺序。

2 USB主机PORTSC寄存器
2.1 EHCI
Connected:
PORTSC1.CCS (bit 0) - Current Connection Status
PORTSC1.CSC (bit 1) - Connection Status Change

Reset - Great Than 10ms:
PORTSC1.PR (bit 8) - Port Reset
PORTSC1.PE (bit 2) - Port Enable

Disconnected:
PORTSC1.CCS (bit 0) 
PORTSC1.CSC (bit 1)
PORTSC1.PEC(bit 3) - Port Enable Change

2.2 xHCI
Connected:
PORTSC.CCS (bit0 ) - Current Connection Status

Reset - Great Than 10ms:
PORTSC.PR (bit4) - Port Reset
PORTSC.PED (bit1) - Port Enabled/Disabled

echo "file xhci-hub.c +p" > /d/dynamic_debug/control
dmesg -n4
wPortStatus & 0x03表示复位成功

3 EHCI电源管理
3.1 休眠唤醒流程
Android USB电源管理_第1张图片
suspend usb_device时,先suspend usb interface,再挂起整个设备,也就是调用generic.c(类似于Windows CCGP)驱动的generic_suspend();
resume usb_device时,先唤醒整个设备,也就是调用generic.c(类似于Windows CCGP)驱动的generic_resume(),再resume usb interface。

3.2 LS/FS/HS Early Suspend Interrupt
DWC2 databook indicates if the core sets "ErlySusp" bit, an idle state has been detected on the USB for 3 ms. This situation can be occurred when waiting a request from user daemon. So, we should keep the connection between udc and gadget even though this interrupt is occurred.

3.3 External Hub reset-resume Issue
hub_port_connect_change()
usb_detect_quirks(), which will set“udev->persist_enabled = 1”
hub_activate(), which checks“udev->persist_enabled == 1”,
then sets “udev->reset_resume = 1; ”the“reset_resume”flag will cause EHCI stack calls
usb_port_resume() to reset the external hub when the system exits suspend-to-RAM.

3.4 HS Device Suspend和Resume
Suspend:
1)高速设备在收到挂起信号(3ms空闲)后,应在0.125ms内切换到全速状态,也就是说要把下拉终端电阻45Ω移除,并在D+数据线上重新挂上1.5k上拉电阻。
2)设备在随后的100-875μs内检测数据线上的状态。如果该状态是一个Full speed J,那么说明host发下来的是一个挂起信号;如果此时该状态是SE0,说明是host drive数据线D+/D-到0,这是一个复位信号(复位信号会持续至少10ms时间)。
Resume:
因为设备挂起时处于全速状态,当host需要设备退出suspend状态时,先发送一个持续时间超过20ms的Fulll Speed K状态,设备看到K状态结束的1.3us(持续2个 LS bit位时间)内醒过来,而host需要在3ms内发送uSOF信号以维持正常的高速信号模式,否则设备又将进入suspend。

4 xHCI电源管理

Android USB电源管理_第2张图片

4.1 Gadget Runtime-PM APIs
@ include/linux/usb/gadget.h
static inline int usb_gadget_autopm_get(struct usb_gadget *gadget);
static inline void usb_gadget_autopm_put(struct usb_gadget *gadget);

4.2 HS LPM
L0:means ON
L1:means SLEEP
L2:traditional USB 2.0 suspend
L3:means OFF

4.3 SS LPM
U0: normal work
U1/U2: 一般都不支持的,Linux DT直接禁止了,因为绝大部分的USB3的U1/U2都是有问题的。
U3: suspend

4.4 USB3 Resume
1)LTSSM处于U3状态;
2)PHY收到LFPS WAKEUP信号,通知Link层LTSSM,LTSSM指导PHY也发送LFPS给对端设备;
3)LTSSM进入Recovery状态,包括TS1,TS2,IDLE训练;
4)持续发送TS1直到成功接收到对端发送的8个TS1,然后进入TS2阶段;在TS2阶段,需要接收到对端发送的至少8个TS2,并且自己给对端至少也发送了16个TS2,此时TS1,TS2训练成功;
5)确认TS1和TS2训练是否成功,如果TS1和TS2都成功,转入下一步;否则,如果TS1或者TS2训练失败,转入SS.Inactive;
6)LTSSM指导PHY进行IDLE训练,接收到对端发送的至少8个空闲符号时,确保自己也同时至少发送了16个IDLE符号给对端;
7)确认上一步是否成功,如果成功,转入U0;否则转入SS.Inactive。
注意:
LFPS和IDLE都是PIPE(PHY Interface for PCI Express)接口PHY实施的;
而TS1,TS2训练序列是LTSSM生成的数据;
LGOOD_0 to LGOOD_7的发送表示Link层已成功收到对端发送的数据(CRC校验正确);
LCRD_A to LCRD_D(CRD means Credit)的发送表示Link层已成功将接收的数据push入protocol层,说明Link层LCRD_X对应的buffer可用了(USB3协议实施时,Link层最多可缓存接收到的4个packets)。

4.5 全局禁止运行时autosuspend
echo -1 >/sys/module/usbcore/parameters/autosuspend

4.6 autosuspend
Intel APL (Gen9, A39X0) 8-port MPH xHCI会注册2个host,一个是usb1(LS/FS/HS),另一个是usb2(SS)。
禁止usb1电源管理:
echo on > /sys/bus/usb/devices/usb1/power/control
禁止usb2电源管理:
echo on > /sys/bus/usb/devices/usb2/power/control

5 Abbreviations
ARC:Argonant RISC Core
CCGP:Windows USB Common Class Generic Parent,Linux内核类似的驱动就是usb_generic_driver(generic.c)
CCGP MI_:Common Class Generic Parent Multi Interface
DWC2:Design Ware Controller 2,Apple的嵌入式设备,包括iPad和iPhone都是使用的DWC2
ISP1161:Philips' Integrated host Solution Pairs 1161,“Firms introduce USB host controllers”,https://www.eetimes.com/document.asp?doc_id=1290054
MDATA:More DATA,USB双缓冲(ep_kind配置使能)切换机制对应到DATA0和DATA1
Quirks:the attributes of a device that are considered to be noncompliant with expected operation
SL811HS:Cypress/ScanLogic 811 Host/Slave,性能上与ISP1161(Integrated host Solution Pairs 1161)相当
TDI:TransDimension Inc.,该公司首先发明了将TT集成到EHCI RootHub中的方法,这样对于嵌入式系统来说,就省去了OHCI/UHCI的硬件,同时降低了成本,作为对该公司的纪念,Linux内核定义了宏ehci_is_TDI(ehci);产品UHC124表示USB Host Controller;收购了ARC USB技术;现已被chipidea收购,chipidea又被mips收购
TT:Transaction Translator(事务转换器,将USB2.0的包转换成USB1.1的包)
USB BH reset:Bigger Hammer or Brad Hosler,表示warm reset;you may be confused why the USB 3.0 spec calls the same type of reset "warm reset" in some places and "BH reset" in other places. "BH" reset is supposed to stand for "Big Hammer" reset, but it also stands for "Brad Hosler". Brad died shortly after the USB 3.0 bus specification was started, and they decided to name the reset after him. The suggestion was made shortly before the spec was finalized, so the wording is a bit inconsistent.
USB Host枚举时-71错误码可能原因:DP和DM走线太长导致眼图差;DP和DM接反了
USB KVM:KVM是键盘(Keyboard)、显示器(Video)、鼠标(Mouse)的缩写;KVM端口是25-pin,包含VGA接口和USB接口
uSOF:micro Start of Frame,125us

你可能感兴趣的:(USB,Android,USB电源管理)