usb hub 3 驱动的匹配:
只要是驱动,必然有一个标准的方式
按照驱动套路:
driver_register-->bus_add_driver-->driver_attach-->__driver_attach-->,(关键位置出来了,总线匹配)
-->driver_probe_device-->drv->probe==驱动(usb_probe_device)-->udriver->probe==通用驱动(generic_probe)。
驱动查找:
-
static int __driver_attach(struct device *dev, void *data)
-
{
-
struct device_driver *drv = data;
-
-
-
//这个时候就是总线找设备:usb_device_match,和
-
//看之前的关键字关键字<usb_device_type,for_devices>
-
-
if (!driver_match_device(drv, dev))//总线匹配;见代码注释
-
return 0;
-
-
if (dev->parent) /* Needed for USB */
-
down(&dev->parent->sem);
-
down(&dev->sem);
-
if (!dev->driver)
-
driver_probe_device(drv, dev);//最终会调用的函数driver_probe_device;
-
up(&dev->sem);
-
if (dev->parent)
-
up(&dev->parent->sem);
-
-
return 0;
-
}
//总线匹配:
-
static int usb_device_match(struct device *dev, struct device_driver *drv)
-
{
-
/* devices and interfaces are handled separately */
-
if (is_usb_device(dev)) {//设备驱动的路口<usb_device_type>
-
-
/* interface drivers never match devices */
-
//这里需要看usb.c里面的.
-
//匹配到usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
-
if (!is_usb_device_driver(drv))//路口字<for_devices>
-
return 0;
-
-
/* TODO: Add real matching code */
-
return 1;//这里就告诉我们,这是usb_device_type,且for_devices==1;
-
-
} else if (is_usb_interface(dev)) {//接口设备的路口,后面再来弄;
-
struct usb_interface *intf;
-
struct usb_driver *usb_drv;
-
const struct usb_device_id *id;
-
-
/* device drivers never match interfaces */
-
if (is_usb_device_driver(drv))
-
return 0;
-
-
intf = to_usb_interface(dev);
-
usb_drv = to_usb_driver(drv);
-
-
id = usb_match_id(intf, usb_drv->id_table);
-
if (id)
-
return 1;
-
-
id = usb_match_dynamic_id(intf, usb_drv);
-
if (id)
-
return 1;
-
}
-
-
return 0;
-
}
-
-
//device的探测函数
-
static int usb_probe_device(struct device *dev)
-
{
-
struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
-
struct usb_device *udev = to_usb_device(dev);
-
int error = -ENODEV;
-
-
dev_dbg(dev, "%s\n", __func__);
-
-
/* TODO: Add real matching code */
-
-
/* The device should always appear to be in use
-
* unless the driver suports autosuspend.
-
*/
-
udev->pm_usage_cnt = !(udriver->supports_autosuspend);
-
-
error = udriver->probe(udev);//usb_driver,回到通用驱动usb_generic_driver
-
return error;
-
}
//通用驱动探测
-
static int generic_probe(struct usb_device *udev)
-
{
-
int err, c;
-
-
/* Choose and set the configuration. This registers the interfaces
-
* with the driver core and lets interface drivers bind to them.
-
*/
-
if (usb_device_is_owned(udev))
-
; /* Don't configure if the device is owned */
-
else if (udev->authorized == 0)
-
dev_err(&udev->dev, "Device is not authorized for usage\n");
-
else {
-
c = usb_choose_configuration(udev);
-
if (c >= 0) {
-
// 既然已经选好配置了,那就告诉设备选好的配置,这个过程是在usb_set_configuration()中完成的
-
//控制器和外设(hub)的通讯
-
err = usb_set_configuration(udev, c);//这里又会生仔(设备)哟;
-
if (err) {
-
dev_err(&udev->dev, "can't set config #%d, error %d\n",c, err);
-
/* This need not be fatal. The user can try to
-
* set other configurations. */
-
}
-
}
-
}
-
/* USB device state == configured ... usable */
-
usb_notify_add_device(udev);
-
-
return 0;
-
}
流程走完了;
这里完成后,整个驱动到了一个新的阶段,usb的枚举阶段的设备配置就完成;配置设备的含义==控制器讲:"你这个设备能够提供这么强大的力量,我选择一种"。
真正usb的功能驱动才刚刚开始,通常讲mass storage,webcam,包括hub的真正的驱动。
之前讲的那些驱动,都是些皮毛,还没有到肉,至少我们还没有弄明白,我们没有学会真正地控制usb;其实里面有好多的usb设备信息读取,我都漏掉,再整理的时候,找个时间再补充上每一章上去;
后面就要涉及到接口设备和驱动了;
阅读(2128) | 评论(0) | 转发(0) |