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

Linux下的USB HUB驱动

[日期:2012-07-29] 来源:Linux社区  作者:zhengmeifu [字体:大 中 小]
4.2.1:usb_choose_configuration()函数分析
usb_choose_configuration()的代码如下:
//为usb device选择一个合适的配置
int usb_choose_configuration(struct usb_device *udev)
{
    int i;
    int num_configs;
    int insufficient_power = 0;
    struct usb_host_config *c, *best;
 
    best = NULL;
    //config数组
    c = udev->config;
    //config项数
    num_configs = udev->descriptor.bNumConfigurations;
    //遍历所有配置项
    for (i = 0; i < num_configs; (i++, c++)) {
        struct usb_interface_descriptor *desc = NULL;
 
        /* It's possible that a config has no interfaces! */
        //配置项的接口数目
        //取配置项的第一个接口
        if (c->desc.bNumInterfaces > 0)
            desc = &c->intf_cache[0]->altsetting->desc;
 
        /*
         * HP's USB bus-powered keyboard has only one configuration
         * and it claims to be self-powered; other devices may have
         * similar errors in their descriptors.  If the next test
         * were allowed to execute, such configurations would always
         * be rejected and the devices would not work as expected.
         * In the meantime, we run the risk of selecting a config
         * that requires external power at a time when that power
         * isn't available.  It seems to be the lesser of two evils.
         *
         * Bugzilla #6448 reports a device that appears to crash
         * when it receives a GET_DEVICE_STATUS request!  We don't
         * have any other way to tell whether a device is self-powered,
         * but since we don't use that information anywhere but here,
         * the call has been removed.
         *
         * Maybe the GET_DEVICE_STATUS call and the test below can
         * be reinstated when device firmwares become more reliable.
         * Don't hold your breath.
         */
#if 0
        /* Rule out self-powered configs for a bus-powered device */
        if (bus_powered && (c->desc.bmAttributes &
                    USB_CONFIG_ATT_SELFPOWER))
            continue;
#endif
 
        /*
         * The next test may not be as effective as it should be.
         * Some hubs have errors in their descriptor, claiming
         * to be self-powered when they are really bus-powered.
         * We will overestimate the amount of current such hubs
         * make available for each port.
         *
         * This is a fairly benign sort of failure.  It won't
         * cause us to reject configurations that we should have
         * accepted.
         */
 
        /* Rule out configs that draw too much bus current */
        //电源不足.配置描述符中的电力是所需电力的1/2
        if (c->desc.bMaxPower * 2 > udev->bus_mA) {
            insufficient_power++;
            continue;
        }
 
        /* When the first config's first interface is one of Microsoft's
         * pet nonstandard Ethernet-over-USB protocols, ignore it unless
         * this kernel has enabled the necessary host side driver.
         */
        if (i == 0 && desc && (is_rndis(desc) || is_activesync(desc))) {
#if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
            continue;
#else
            best = c;
#endif
        }
 
        /* From the remaining configs, choose the first one whose
         * first interface is for a non-vendor-specific class.
         * Reason: Linux is more likely to have a class driver
         * than a vendor-specific driver. */
         //选择一个不是USB_CLASS_VENDOR_SPEC的配置
        else if (udev->descriptor.bDeviceClass !=
                        USB_CLASS_VENDOR_SPEC &&
                (!desc || desc->bInterfaceClass !=
                        USB_CLASS_VENDOR_SPEC)) {
            best = c;
            break;
        }
 
        /* If all the remaining configs are vendor-specific,
         * choose the first one. */
        else if (!best)
            best = c;
    }
 
    if (insufficient_power > 0)
        dev_info(&udev->dev, "rejected %d configuration%s "
            "due to insufficient available bus power\n",
            insufficient_power, plural(insufficient_power));
    //如果选择好了配置,返回配置的序号,否则,返回-1
    if (best) {
        i = best->desc.bConfigurationValue;
        dev_info(&udev->dev,
            "configuration #%d chosen from %d choice%s\n",
            i, num_configs, plural(num_configs));
    } else {
        i = -1;
        dev_warn(&udev->dev,
            "no configuration chosen from %d choice%s\n",
            num_configs, plural(num_configs));
    }
    return i;
}
Linux按照自己的喜好选择好了配置之后,返回配置的序号.不过对于HUB来说,它有且仅有一个配置.
 
4.2.2:usb_set_configuration()函数分析
既然已经选好配置了,那就告诉设备选好的配置,这个过程是在usb_set_configuration()中完成的.它的代码如下:
int usb_set_configuration(struct usb_device *dev, int configuration)
{
    int i, ret;
    struct usb_host_config *cp = NULL;
    struct usb_interface **new_interfaces = NULL;
    int n, nintf;
 
    if (dev->authorized == 0 || configuration == -1)
        configuration = 0;
    else {
        for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
            if (dev->config[i].desc.bConfigurationValue ==
                    configuration) {
                cp = &dev->config[i];
                break;
            }
        }
    }
 
    if ((!cp && configuration != 0))
        return -EINVAL;
 
    /* The USB spec says configuration 0 means unconfigured.
     * But if a device includes a configuration numbered 0,
     * we will accept it as a correctly configured state.
     * Use -1 if you really want to unconfigure the device.
     */
    if (cp && configuration == 0)
        dev_warn(&dev->dev, "config 0 descriptor??\n");
首先,根据选择好的配置号找到相应的配置,在这里要注意了, dev->config[]数组中的配置并不是按照配置的序号来存放的,而是按照遍历到顺序来排序的.因为有些设备在发送配置描述符的时候,并不是按照配置序号来发送的,例如,配置2可能在第一次GET_CONFIGURATION就被发送了,而配置1可能是在第二次GET_CONFIGURATION才能发送.
取得配置描述信息之后,要对它进行有效性判断,注意一下本段代码的最后几行代码:usb2.0 spec上规定,0号配置是无效配置,但是可能有些厂商的设备并末按照这一约定,所以在linux中,遇到这种情况只是打印出警告信息,然后尝试使用这一配置.
    /* Allocate memory for new interfaces before doing anything else,
     * so that if we run out then nothing will have changed. */
 
    n = nintf = 0;
    if (cp) {
        //接口总数
        nintf = cp->desc.bNumInterfaces;
        //interface指针数组,
        new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
                GFP_KERNEL);
        if (!new_interfaces) {
            dev_err(&dev->dev, "Out of memory\n");
            return -ENOMEM;
        }
 
        for (; n < nintf; ++n) {
            new_interfaces[n] = kzalloc(
                    sizeof(struct usb_interface),
                    GFP_KERNEL);
            if (!new_interfaces[n]) {
                dev_err(&dev->dev, "Out of memory\n");
                ret = -ENOMEM;
free_interfaces:
                while (--n >= 0)
                    kfree(new_interfaces[n]);
                kfree(new_interfaces);
                return ret;
            }
        }
 
        //如果总电源小于所需电流,打印警告信息
        i = dev->bus_mA - cp->desc.bMaxPower * 2;
        if (i < 0)
            dev_warn(&dev->dev, "new config #%d exceeds power "
                    "limit by %dmA\n",
                    configuration, -i);
    }
在这里,注要是为new_interfaces分配空间,要这意的是, new_interfaces是一个二级指针,它的最终指向是struct usb_interface结构.特别的,如果总电流数要小于配置所需电流,则打印出警告消息.实际上,这种情况在usb_choose_configuration()中已经进行了过滤.
 
    /* Wake up the device so we can send it the Set-Config request */
    //要对设备进行配置了,先唤醒它
    ret = usb_autoresume_device(dev);
    if (ret)
        goto free_interfaces;
 
    /* if it's already configured, clear out old state first.
     * getting rid of old interfaces means unbinding their drivers.
     */
     //不是处于ADDRESS状态,先清除设备的状态
    if (dev->state != USB_STATE_ADDRESS)
        usb_disable_device(dev, 1); /* Skip ep0 */
 
    //发送控制消息,选取配置
    ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
                  USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
                  NULL, 0, USB_CTRL_SET_TIMEOUT);
    if (ret < 0) {
        /* All the old state is gone, so what else can we do?
         * The device is probably useless now anyway.
         */
        cp = NULL;
    }
 
    //dev->actconfig存放的是当前设备选取的配置
    dev->actconfig = cp;
    if (!cp) {
        usb_set_device_state(dev, USB_STATE_ADDRESS);
        usb_autosuspend_device(dev);
        goto free_interfaces;
    }
    //将状态设为CONFIGURED
    usb_set_device_state(dev, USB_STATE_CONFIGURED);linux
相关资讯       Linux驱动开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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