赞
踩
设备:struct device =(派生)=> struct usb_device =(包含)=> struct usb_interface(派生自device)
驱动:struct device_driver =(派生)=> struct usb_device_driver =(派生)=> struct usb_driver
(1) USB设备的注册:
usb_new_device(struct usb_device *udev) //用于hub枚举设备和注册roothub
device_add(&udev->dev); //struct usb_device *udev
如 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)
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; }
(2)USB驱动的注册:
== usb_init(void);
== usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
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)usb接口设备的注册:
device_add(&intf->dev); //struct usb_interface *intf;
如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)
(2)usb接口驱动的注册:
== module_usb_driver();
== usb_register_driver(struct usb_driver *new_driver, struct module *owner,
const char *mod_name);
//与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);
}
层次结构:dwc -> xhci -> root_hub -> udev
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
}
== 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); }
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; }
== xhci_plat_probe(struct platform_device *pdev)
== hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
dev_name(&pdev->dev), NULL);
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; }
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; }
== usb_add_hcd()
== usb_alloc_dev(NULL, &hcd->self, 0);
== dev->dev.parent = bus->controller; //rhdev->dev.parent = hcd->self->controller,即xhci->dev
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);
}
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];
}
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;
}
判断一个usb device
是否是root hub
的直连设备:
if (udev->parent && !udev->parent->parent &&
udev->dev.parent->parent == &dwc->xhci->dev)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。