手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux下的USB HUB驱动

[日期:2012-07-29] 来源:Linux社区  作者:zhengmeifu [字体:大 中 小]
五:hub接口驱动分析
5.1:接口驱动架构
是时候来分析接口驱动的架构了.
我们在上面看到了接口设备的注册.在开篇的时候分析了接口驱动的注册.我们首先来分析接口驱备和接口驱动的匹配.
代码还是在usb_bus_type->match().只不过是对应另外的一种情况了.将相关代码列出:
static int usb_device_match(struct device *dev, struct device_driver *drv)
{
    ……
    if (is_usb_device(dev)) {
        ……
}
    //interface的情况
    else {
        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;
}
经过前面的分析,因为在注册接口设备的时候,是将type设为usb_if_device_type,因此,这个函数第一个if是不会满足的.
首先,将struct device和struct device_driver转换为被封装的struct usb_interface和struct usb_driver.紧接着,我们看到了两个匹配,一个是usb_match_id().另外一个是usb_match_dynamic_id().后者只有在前者没有匹配成功的情况下才能调用.我们也可以看到, struct usb_driver中一个struct usb_device_id类型的数组(id_table字段)和一个dynids链表.哪id和dyname_id有什么区别呢?
一般来说,id_table是usb 接口驱动静态定义的可以和此驱动匹配设备的一些参数.而dyname_id是可以动态调整的.
你可以通过写/sys/bus/usb/drivers/XXX/new_id来设置驱动的dyname_id.其中,XXX表示驱动的名称.
这两个函数的逻辑都差不多,我们以usb_match_id()为例进行分析.代码如下:
const struct usb_device_id *usb_match_id(struct usb_interface *interface,
                     const struct usb_device_id *id)
{
    /* proc_connectinfo in devio.c may call us with id == NULL. */
    //如果driver中没有定义符合条件的id_talbe.则不能静态匹配所有设备
    if (id == NULL)
        return NULL;
 
    /* It is important to check that id->driver_info is nonzero,
       since an entry that is all zeroes except for a nonzero
       id->driver_info is the way to create an entry that
       indicates that the driver want to examine every
       device and interface. */
       //如果id_talbe中,有定义的匹配项,则调用usb_match_one_id()进行匹配测试
       //如果成功,则返回匹配成功的id_talbe 项数.否则,继续下一项
    for (; id->idVendor || id->idProduct || id->bDeviceClass ||
           id->bInterfaceClass || id->driver_info; id++) {
        if (usb_match_one_id(interface, id))
            return id;
    }
 
    //如果没有匹配成功,则返回NULL
    return NULL;
}
Id_talbe所属的结构如下示:
struct usb_device_id {
    /* which fields to match against? */
    __u16       match_flags;
 
    /* Used for product specific matches; range is inclusive */
    __u16       idVendor;
    __u16       idProduct;
    __u16       bcdDevice_lo;
    __u16       bcdDevice_hi;
 
    /* Used for device class matches */
    __u8        bDeviceClass;
    __u8        bDeviceSubClass;
    __u8        bDeviceProtocol;
 
    /* Used for interface class matches */
    __u8        bInterfaceClass;
    __u8        bInterfaceSubClass;
    __u8        bInterfaceProtocol;
 
    /* not matched against */
    kernel_ulong_t  driver_info;
};
match_flags是一个匹配标志,表示要匹配哪一项.而后面的成员,例如idVendor, idProduct.表示具体要匹配项的值.
usb_match_one_id()的代码如下:
int usb_match_one_id(struct usb_interface *interface,
             const struct usb_device_id *id)
{
    struct usb_host_interface *intf;
    struct usb_device *dev;
 
    /* proc_connectinfo in devio.c may call us with id == NULL. */
    //再次判断id是否为空
    if (id == NULL)
        return 0;
 
    //接口听当前匹配
    intf = interface->cur_altsetting;
    //转换为所属的usb_dev
    dev = interface_to_usbdev(interface);
 
    //关于设备参数的匹配.只有在设备参数符合的情况下,才会进行接口
    //参数的匹配.
    if (!usb_match_device(dev, id))
        return 0;
   
    //下面是关于接口参数的匹配
    /* The interface class, subclass, and protocol should never be
     * checked for a match if the device class is Vendor Specific,
     * unless the match record specifies the Vendor ID. */
 //当设备是厂商自定义类型时(Vendor Specific).除非定义了//USB_DEVICE_ID_MATCH_VENDOR
     //否则是不需要匹配的
    if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
            !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
            (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
                USB_DEVICE_ID_MATCH_INT_SUBCLASS |
                USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
        return 0;
    //如果要匹配interface class
    if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
        (id->bInterfaceClass != intf->desc.bInterfaceClass))
        return 0;
    //如果要匹配interface subclass
    if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
        (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
        return 0;
    //如果要匹配interface protocol
    if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
        (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
        return 0;
 
    return 1;
}
这个函数逻辑比较简单.它先检测所属设备参数的匹配项.再检查接口参数的匹配项.由于这个函数比较简单,就不对它多费舌去解释了.linux
相关资讯       Linux驱动开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款