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

Linux下的USB HUB驱动

[日期:2012-07-29] 来源:Linux社区  作者:zhengmeifu [字体:大 中 小]
简单的说一下关于Vendor Specific class的设备,这个类型在USB Class Codes被定义如下:
This base class is defined for vendors to use as they please.  These class codes can be used in both Device and Interface Descriptors.
 

 
也就是说,这个类型是被厂商自定义的.它主要是靠SubClass和protocol来区分设备的.这些都是非标准定义的.
 
如果被这个match匹配成功的话,就会转入probe()了,由于bus中没有probe()函数,因此,流程转入到了与之匹配的驱动的probe函数.
记得在开篇的时候,分析过接口驱动,它的probe函数被设置为了usb_cache_string().代码如下:
static int usb_probe_interface(struct device *dev)
{
    struct usb_driver *driver = to_usb_driver(dev->driver);
    struct usb_interface *intf;
    struct usb_device *udev;
    const struct usb_device_id *id;
    int error = -ENODEV;
 
    dev_dbg(dev, "%s\n", __FUNCTION__);
    //如果是一个usb 设备,退出
    if (is_usb_device(dev))     /* Sanity check */
        return error;
 
    intf = to_usb_interface(dev);
    udev = interface_to_usbdev(intf);
 
    //如果所属设备的authorized 为0.错误,退出
    if (udev->authorized == 0) {
        dev_err(&intf->dev, "Device is not authorized for usage\n");
        return -ENODEV;
    }
 
    //再确认一下设备和驱动是否匹配
    id = usb_match_id(intf, driver->id_table);
    if (!id)
        id = usb_match_dynamic_id(intf, driver);
    //如果匹配
    if (id) {
        dev_dbg(dev, "%s - got id\n", __FUNCTION__);
 
        //使设备resume
        error = usb_autoresume_device(udev);
        if (error)
            return error;
 
        /* Interface "power state" doesn't correspond to any hardware
         * state whatsoever.  We use it to record when it's bound to
         * a driver that may start I/0:  it's not frozen/quiesced.
         */
         //将接口标志为active
        mark_active(intf);
        //intf->condition:接口的状态
        //USB_INTERFACE_BINDING:正在绑定
        intf->condition = USB_INTERFACE_BINDING;
 
        /* The interface should always appear to be in use
         * unless the driver suports autosuspend.
         */
         //intf->pm_usage_cnt:如果为1,接口不会autosuspend
        intf->pm_usage_cnt = !(driver->supports_autosuspend);
        //调用挂装结构的probe
        error = driver->probe(intf, id);
        //如果发生了错误,将接口标记成不活动的,接口状态设为USB_INTERFACE_UNBOUND
        if (error) {
            mark_quiesced(intf);
            intf->needs_remote_wakeup = 0;
            intf->condition = USB_INTERFACE_UNBOUND;
        }
        //如果成功,将接口状态设为USB_INTERFACE_BOUND
        else
            intf->condition = USB_INTERFACE_BOUND;
 
        //使设备suspend
        usb_autosuspend_device(udev);
    }
 
    return error;
}
至此为至,我们遇到了大量的usb_autoresume_device()/usb_autosuspend_device().先将它们放到一边,等以后再来详细分析他们的操作.
还记得,在usb_set_configuration()中,为每一个接口调用了mark_quiesced().所以在这个probe里,将正确匹配的接口调用mark_active(),将其标记为Active.
另外,我们来看一下,intf-> condition的几种情况,在代码中, condition属于enum usb_interface_condition结构.如下示:
enum usb_interface_condition {
    USB_INTERFACE_UNBOUND = 0,
    USB_INTERFACE_BINDING,
    USB_INTERFACE_BOUND,
    USB_INTERFACE_UNBINDING,
};
分别表示,没有绑定,正在绑定,已经绑定,正在解除绑定.
从上面的代码中可以看出.最后的流程会回溯到usb_driver的probe.
 
5.2:hub的接口驱动
经过上面的分析,我们知道,接口驱动和接口的匹配主要是根据接口的一些信息来判断的.那对于hub的接口,它的标志信息是什么呢?
在usb2.0 spec上,规定了hub的设备class和接口class都为0x9.也就是代码中定义的USB_CLASS_HUB.
同时,我们注意到,在usb_init()àusb_hub_init()中:
int usb_hub_init(void)
{
    if (usb_register(&hub_driver) < 0) {
        printk(KERN_ERR "%s: can't register hub driver\n",
            usbcore_name);
        return -1;
    }
 
    khubd_task = kthread_run(hub_thread, NULL, "khubd");
    if (!IS_ERR(khubd_task))
        return 0;
 
    /* Fall through if kernel_thread failed */
    usb_deregister(&hub_driver);
    printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
 
    return -1;
}
这个函数注册了hub_driver对应的接口驱动,还启动了一个内核线程.在这里,我们注意到, hub_driver的定义如下:
static struct usb_driver hub_driver = {
    .name =     "hub",
    .probe =    hub_probe,
    .disconnect =   hub_disconnect,
    .suspend =  hub_suspend,
    .resume =   hub_resume,
    .reset_resume = hub_reset_resume,
    .pre_reset =    hub_pre_reset,
    .post_reset =   hub_post_reset,
    .ioctl =    hub_ioctl,
    .id_table = hub_id_table,
    .supports_autosuspend = 1,
};
Hub_id_talbe被定义为:
static struct usb_device_id hub_id_table [] = {
    { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
      .bDeviceClass = USB_CLASS_HUB},
    { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
      .bInterfaceClass = USB_CLASS_HUB},
    { }                      /* Terminating entry */
};
看到了没,这些信息就是hub 的接口class信息.
也就是说, hub_driver就是为们要找的hub 接口的驱动.
根据上面的分析,流程最终会到hub_driver->probe中,对应的接口为hub_probe().

linux
相关资讯       Linux驱动开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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