Android Qcom USB Driver学习(十四)

该系列文章总目录链接与各部分简介: Android Qcom USB Driver学习(零)

了解什么是dwc3 udc gadget hcd xhci, 差点被Chatgpt绕晕了,看的比较浅显就像知道这些都是什么作用的。

UDC-Gadget

UDC:(USB Device Controller)用于管理和控制USB设备与主机之间的通信。
Gadget:Android在此层实现了adb,mtp(Media Transfer Protocol 媒体传输协议),mass_storage等。

早期的USB只支持单一的gadget设备,使用场景较为简单,随后加入了composite framework,用来支持多个function的gadget设备,多个function的绑定在内核中完成,若需要修改,则需要修改内核,不灵活也不方便。Linux3.11版本引入了基于configfs的usb gadget configfs。
usb gadget configfs重新实现了复合设备层,可以在用户空间配置和组合内核的function,灵活的构成USB复合设备,极大了提高了工作效率。
system/core/rootdir/init.usb.configfs.rc

[persist.sys.usb.config]: [diag,adb]
[sys.usb.config]: [mtp,adb] 

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=mtp,adb && property:sys.usb.configfs=1
     write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "mtp_adb"
     write /config/usb_gadget/g1/idVendor 0x0c2e
     write /config/usb_gadget/g1/idProduct 0x0ba3
     symlink /config/usb_gadget/g1/functions/mtp.gs0 /config/usb_gadget/g1/configs/b.1/f1
     symlink /config/usb_gadget/g1/functions/ffs.adb /config/usb_gadget/g1/configs/b.1/f2
     write /config/usb_gadget/g1/UDC ${sys.usb.controller}
     setprop sys.usb.state ${sys.usb.config}

drivers/usb/gadget/configfs.c
drivers\usb\gadget\composite.c
drivers/usb/gadget/udc/core.c
drivers/usb/gadget/function/f_mtp.c
USB总线-Linux内核USB3.0设备控制器复合设备之USB gadget configfs分析

gadget -> composite -> function

make_group -> function_make -> usb_get_function_instance -> alloc_inst -> mtp_alloc_inst
每个function driver创建的时候会分配一个usb_function_instance

echo  4e00000.dwc3 >  configs/usb_gadget/g1/UDC 
usb_udc_attch_driver-> udc_bind_to_driver-> configfs_composite_bind 
                                         -> usb_add_function -> function->bind -> mtp_function_bind
		

HCD-XHCI

HCD:(Host Controller Driver)是主机控制器驱动,它是对主机控制器硬件的抽象。
XHCI:(eXtensible Host Controller Interface)也是主机控制器驱动,XHIC是基于USB3.0并且向下兼容,其中集成了两个roothub,一个对应usb2.0的(main_hcd),一个对应usb3.0的(shared_hcd)

struct xhci_hcd {
			   /* usb_hcd结构体:用于描述USB主机控制器驱动*/
				struct usb_hcd *main_hcd;
				struct usb_hcd *shared_hcd;
				/* glue to PCI and HCD framework */
				struct xhci_cap_regs __iomem *cap_regs;
				struct xhci_op_regs __iomem *op_regs;
				struct xhci_run_regs __iomem *run_regs;
				struct xhci_doorbell_array __iomem *dba;
				/* Our HCD's current interrupter register set */
				struct	xhci_intr_reg __iomem *ir_set;
		
				/* Cached register copies of read-only HC data */
				__u32		hcs_params1;
				...
		
				spinlock_t	lock;
		
				/* packed release number */
				u8		sbrn;
				u16		hci_version;
				...
				...
				...
			};

root hub设备注册(三)
drivers/usb/host/xhci-plat.c
drivers/usb/core/hcd.c
drivers/usb/host/xhci.c
drivers/usb/core/hub.c


dwc3_otg_start_host ->   dwc3_host_init -> platform_device_add(xhci) -> xhci_plat_probe

xhci->main_hcd = hcd;  
usb_add_hcd(hcd, irq, IRQF_SHARED); //roothub对应usb0
usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
	hcd->driver->start(hcd)  //hc_driver
		xhci_run(hcd)
			xhci_try_enable_msi(hcd)
				hci_setup_msix(xhci)
					xhci_irq(hcd)
					---------------------------------------------------------------------------------
				   |	xhci_handle_event(xhci)														|
				   |	case: TRB_PORT_STATUS                                                       |
				   |		handle_port_status(xhci, event);                                        |
				   |	case: TRB_TRANSFER                                                          |
				   |		handle_tx_event(xhci, &event->trans_event);                             |
				   |			usb_hcd_giveback_urb                                                |
				   |				tasklet_schedule(&bh->bh);                                      |
				   |					kick_hub_wq(hub);                                           |
				   |						queue_work(hub_wq, &hub->events)                        |
				   |							hub_event->port_event->hub_port_connect_change      |
				   |								->hub_port_connect -> usb_new_device(udev);     |
				    ----------------------------------------------------------------------------------		
	register_root_hub(hcd)
		usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE)
			usb_new_device (usb_dev)
				usb_enumerate_device(udev)
				announce_device(udev)/* 打印New USB device found*/
				device_add(&udev->dev)

DWC3

DWC3:(DesignWare USB 3.0 Controller) 在Android系统中,dwc3通常被用作USB3.0主机控制器,用于与XHCI驱动程序配合使用,控制USB3.0设备与主机之间的通信。同时,dwc3还可以与Gadget驱动程序配合使用,将linux设备转为USB设备,实现设备与主机间的通信。
dwc3控制器初始化的时候会将控制器设置为USB_DR_MODE_OTG模式,同时调用dwc3_host_init和dwc3_gadget_init函数初始化主机模式和设备模式所需的资源,控制器后续可以动态切换为主机模式和设备模式。

                  ->   host mode    xhci   dwc3_host_init()
dwc3 controller -
                  ->   device mode  udc    dwc3_gadget_init()

dual-role device (DRD) OTG State
enum dwc3_drd_state {
  	DRD_STATE_UNDEFINED = 0, //put controller and phy in suspend if no cable connected
							 //pm_runtime_putsync decrease usage_count
  	DRD_STATE_IDLE,	         
  	DRD_STATE_PERIPHERAL,
  	DRD_STATE_PERIPHERAL_SUSPEND,
  	DRD_STATE_HOST_IDLE,
  	DRD_STATE_HOST,
  };
dual-role usb control(DR) works on a particular mode
enum usb_dr_mode {
  	USB_DR_MODE_UNKNOWN,
  	USB_DR_MODE_HOST,
  	USB_DR_MODE_PERIPHERAL,
  	USB_DR_MODE_OTG,
  };

状态的切换通过dwc3_otg_sm_work -> dwc3_otg_start_host/dwc3_otg_start_peripheral

power/supply/qcom/qpnp-smb5.c devm_extcon_dev_register 发送广播

static const unsigned int smblib_extcon_cable[] = {
  	EXTCON_USB,
  	EXTCON_USB_HOST,
  	EXTCON_NONE,
};

smblib_notify_device_mode
	extcon_set_state_sync(chg->extcon, EXTCON_USB, enable);
smblib_notify_usb_host
	extcon_set_state_sync(chg->extcon, EXTCON_USB_HOST, enable);

usb/dwc3/dwc3-msm.c devm_extcon_register_notifier注册监听usb状态变化,接受广播

mdwc->extcon[idx].vbus_nb.notifier_call = dwc3_msm_vbus_notifier;
extcon_register_notifier(edev, EXTCON_USB, &mdwc->extcon[idx].vbus_nb);
extcon_register_notifier(edev, EXTCON_USB_HOST,&mdwc->extcon[idx].id_nb);
dwc3_msm_vbus_notifier(&mdwc->extcon[idx].vbus_nb,true, edev);
dwc3_msm_id_notifier(&mdwc->extcon[idx].id_nb,true, edev);

你可能感兴趣的:(Android_USB,android,adb,dwc3)