当前位置:   article > 正文

usb host 驱动之设备层次_usb20_host _set_vbus

usb20_host _set_vbus

1、device 层次架构

设备:struct device 		   	=(派生)=> struct usb_device 			=(包含)=> struct usb_interface(派生自device)
驱动:struct device_driver 	=(派生)=> struct usb_device_driver 	=(派生)=> struct usb_driver
  • 1
  • 2

1.1 usb 设备驱动

(1) USB设备的注册:

usb_new_device(struct usb_device *udev)	 	//用于hub枚举设备和注册roothub
	device_add(&udev->dev);			 		//struct usb_device *udev
  • 1
  • 2

如 hub 枚举连接的设备:

== hub_port_connect()
	== udev = usb_alloc_dev(hdev, hdev->bus, port1);		//构建usb设备 
	== hub_port_init()
	== usb_new_device()
		== usb_enumerate_device()
		== device_add(udev)			//注册usb设备
== usb_generic_driver_probe()		//(\drivers\usb\core\generic.c)
	== usb_choose_configuration()
	== usb_set_configuration()
		== device_add(intf)
== usb function driver(usb-storage)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
struct usb_device *usb_alloc_dev(struct usb_device *parent,
				 struct usb_bus *bus, unsigned port1)
{
	struct usb_device *dev;
	struct usb_hcd *usb_hcd = bus_to_hcd(bus);
	unsigned root_hub = 0;
	unsigned raw_port = port1;

	device_initialize(&dev->dev);
	dev->dev.bus = &usb_bus_type;			//device层级结构,设备的bus 类型
	dev->dev.type = &usb_device_type;
	dev->dev.groups = usb_device_groups;
	set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
	dev->state = USB_STATE_ATTACHED;
	dev->lpm_disable_count = 1;
	atomic_set(&dev->urbnum, 0);

	/* ep0 maxpacket comes later, from device descriptor */
	usb_enable_endpoint(dev, &dev->ep0, false);

	if (unlikely(!parent)) {			//该usb device 为root hub
		dev->dev.parent = bus->controller;
		dev_set_name(&dev->dev, "usb%d", bus->busnum);
		root_hub = 1;
	} else {							//该usb device 为generic device
		dev->dev.parent = &parent->dev;
		dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
	}

	dev->portnum = port1;
	dev->bus = bus;				//usb层级结构 usb_bus,dev->bus = hcd->self;	
	dev->parent = parent;		//usb层级结构 usb_device

	return dev;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

(2)USB驱动的注册:

== usb_init(void);
	== usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
  • 1
  • 2
int usb_register_device_driver(struct usb_device_driver *new_udriver,
		struct module *owner)
{
	new_udriver->drvwrap.for_devices = 1;
	new_udriver->drvwrap.driver.name = new_udriver->name;
	new_udriver->drvwrap.driver.bus = &usb_bus_type;			//driver层级结构,bus 类型
	new_udriver->drvwrap.driver.probe = usb_probe_device;		//match过程先执行该probe(device_driver的),再调用usb_device_driver的probe:usb_generic_driver_probe

	retval = driver_register(&new_udriver->drvwrap.driver);		//注册device_driver

	if (!retval) {
		bus_for_each_dev(&usb_bus_type, NULL, new_udriver,
				 __usb_bus_reprobe_drivers);					//寻找与之匹配的device
	} 

	return retval;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

1.2 usb 接口驱动

(1)usb接口设备的注册:

device_add(&intf->dev);		//struct usb_interface *intf;
  • 1

如generic 驱动去注册usb device 的各个interface:

== usb_generic_driver_probe()			//(\drivers\usb\core\generic.c)
	== usb_choose_configuration()
	== usb_set_configuration()
		== device_add(intf)				//注册interface
== usb function driver(usb-storage)
  • 1
  • 2
  • 3
  • 4
  • 5

(2)usb接口驱动的注册:

== module_usb_driver();
	== usb_register_driver(struct usb_driver *new_driver, struct module *owner,
			const char *mod_name);
  • 1
  • 2
  • 3
//与usb_register_device_driver 一样,只不过传参变成了usb_driver
int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
			const char *mod_name)		
{
	new_driver->drvwrap.for_devices = 0;
	new_driver->drvwrap.driver.name = new_driver->name;
	new_driver->drvwrap.driver.bus = &usb_bus_type;
	new_driver->drvwrap.driver.probe = usb_probe_interface;
	new_driver->drvwrap.driver.remove = usb_unbind_interface;
	new_driver->drvwrap.driver.owner = owner;
	new_driver->drvwrap.driver.mod_name = mod_name;
	new_driver->drvwrap.driver.dev_groups = new_driver->dev_groups;

	retval = driver_register(&new_driver->drvwrap.driver);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2、host 层级

层次结构:dwc -> xhci -> root_hub -> udev

2.1 dwc

dwc 作为一个platform device,设备信息由设备树解析,与驱动(\drivers\usb\dwc3\core.c)匹配后执行dwc3_probe

static int dwc3_probe(struct platform_device *pdev)
{
	struct device		*dev = &pdev->dev;
	struct resource		*res, dwc_res;
	struct dwc3		*dwc;
	
	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
	if (!dwc)
		return -ENOMEM;

	dwc->dev = dev;		//dwc->dev = pdev->dev;即dwc和pdev派生自同一个dev
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.1.1 dwc -> xhci

== dwc3_probe(struct platform_device *pdev)== dwc3_core_init_mode(dwc);
		== dwc3_host_init(dwc);
			== xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
			== platform_device_add(xhci);

int dwc3_host_init(struct dwc3 *dwc)
{
	struct platform_device	*xhci;

	xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
	xhci->dev.parent	= dwc->dev;		//xhci 是dwc 的child

	dwc->xhci = xhci;
	
	ret = platform_device_add(xhci);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2.2 xhci

xhci 作为一个platform device 注册之后,与驱动(\drivers\usb\host\xhci-plat.c)匹配后执行xhci_plat_probe

static int xhci_plat_probe(struct platform_device *pdev)
{
	const struct xhci_plat_priv *priv_match;
	const struct hc_driver	*driver;
	struct device		*sysdev, *tmpdev;
	struct xhci_hcd		*xhci;
	struct resource         *res;
	struct usb_hcd		*hcd;

	driver = &xhci_plat_hc_driver;

	hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
			       dev_name(&pdev->dev), NULL);		//创建一个hcd,依赖于xhci

	xhci = hcd_to_xhci(hcd);

	xhci->main_hcd = hcd;					//usb2.0 hcd
	xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
			dev_name(&pdev->dev), hcd);		//usb3.0 hcd

	ret = usb_add_hcd(hcd, irq, IRQF_SHARED);

	ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);

	return ret;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

2.2.1 hcd

== xhci_plat_probe(struct platform_device *pdev)
	== hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
			       dev_name(&pdev->dev), NULL);
  • 1
  • 2
  • 3
struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver,
		struct device *sysdev, struct device *dev, const char *bus_name,
		struct usb_hcd *primary_hcd)
{
	struct usb_hcd *hcd;

	hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
	
	usb_bus_init(&hcd->self);		// 初始化usb_bus,所有的 usb_device 都挂在这条总线下
	hcd->self.controller = dev;		// hcd->self.controller = xhci->dev; 与xhci 绑定
	hcd->self.sysdev = sysdev;
	hcd->self.bus_name = bus_name;

	hcd->driver = driver;
	hcd->speed = driver->flags & HCD_MASK;
	hcd->product_desc = (driver->product_desc) ? driver->product_desc :
			"USB Host Controller";
	return hcd;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2.2.2 xhci->rhdev

int usb_add_hcd(struct usb_hcd *hcd,
		unsigned int irqnum, unsigned long irqflags)
{
	int retval;
	struct usb_device *rhdev;

	retval = usb_register_bus(&hcd->self);		//注册usb_bus 总线

	rhdev = usb_alloc_dev(NULL, &hcd->self, 0);		//创建rhdev

	hcd->self.root_hub = rhdev;					//rhdev 与hcd 绑定

	retval = register_root_hub(hcd);	//注册rhdev,调用usb_new_device

	if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
		usb_hcd_poll_rh_status(hcd);		//轮询rh 的状态

	return retval;			
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2.3 rhdev

== usb_add_hcd()
	== usb_alloc_dev(NULL, &hcd->self, 0);
		== dev->dev.parent = bus->controller;		//rhdev->dev.parent = hcd->self->controller,即xhci->dev
  • 1
  • 2
  • 3

2.3.1 rhdev->hub

static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_host_interface *desc;
	struct usb_device *hdev;		//此处为rhdev
	struct usb_hub *hub;	

	desc = intf->cur_altsetting;
	hdev = interface_to_usbdev(intf);

	hub->intfdev = &intf->dev;
	hub->hdev = hdev;				//构建hub

	usb_set_intfdata(intf, hub);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2.3.2 hub->port_dev

static int hub_configure(struct usb_hub *hub,
	struct usb_endpoint_descriptor *endpoint)
{
	for (i = 0; i < hdev->maxchild; i++) {
		struct usb_port *port_dev = hub->ports[i];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.3.3 port_dev->udev

static void port_event(struct usb_hub *hub, int port1)
		__must_hold(&port_dev->status_lock)
{
	int connect_change;
	struct usb_port *port_dev = hub->ports[port1 - 1];
	struct usb_device *udev = port_dev->child;
	struct usb_device *hdev = hub->hdev;
	u16 portstatus, portchange;

	...usb_alloc_dev(hdev, hdev->bus, port1);	//udev->dev.parent = &hdev->dev;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

判断一个usb device是否是root hub的直连设备:

if (udev->parent && !udev->parent->parent &&
		udev->dev.parent->parent == &dwc->xhci->dev) 
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/246945
推荐阅读
相关标签
  

闽ICP备14008679号