Chinaunix首页 | 论坛 | 博客
  • 博客访问: 216816
  • 博文数量: 32
  • 博客积分: 410
  • 博客等级: 一等列兵
  • 技术积分: 396
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-03 16:58
文章分类

全部博文(32)

文章存档

2013年(17)

2012年(15)

我的朋友

分类: LINUX

2013-04-16 11:47:05

usb hub 3 驱动的匹配:
只要是驱动,必然有一个标准的方式

按照驱动套路:
driver_register-->bus_add_driver-->driver_attach-->__driver_attach-->,(关键位置出来了,总线匹配)
-->driver_probe_device-->drv->probe==驱动(usb_probe_device)-->udriver->probe==通用驱动(generic_probe)。

驱动查找:

点击(此处)折叠或打开

  1. static int __driver_attach(struct device *dev, void *data)
  2. {
  3.     struct device_driver *drv = data;


  4. //这个时候就是总线找设备:usb_device_match,和
  5. //看之前的关键字关键字<usb_device_type,for_devices>

  6.     if (!driver_match_device(drv, dev))//总线匹配;见代码注释
  7.         return 0;

  8.     if (dev->parent) /* Needed for USB */
  9.         down(&dev->parent->sem);
  10.     down(&dev->sem);
  11.     if (!dev->driver)
  12.         driver_probe_device(drv, dev);//最终会调用的函数driver_probe_device;
  13.     up(&dev->sem);
  14.     if (dev->parent)
  15.         up(&dev->parent->sem);

  16.     return 0;
  17. }


//总线匹配:

点击(此处)折叠或打开

  1. static int usb_device_match(struct device *dev, struct device_driver *drv)
  2. {
  3.     /* devices and interfaces are handled separately */
  4.     if (is_usb_device(dev)) {//设备驱动的路口<usb_device_type>

  5.         /* interface drivers never match devices */
  6. //这里需要看usb.c里面的.
  7. //匹配到usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
  8.         if (!is_usb_device_driver(drv))//路口字<for_devices>
  9.             return 0;

  10.         /* TODO: Add real matching code */
  11.         return 1;//这里就告诉我们,这是usb_device_type,且for_devices==1;

  12.     } else if (is_usb_interface(dev)) {//接口设备的路口,后面再来弄;
  13.         struct usb_interface *intf;
  14.         struct usb_driver *usb_drv;
  15.         const struct usb_device_id *id;

  16.         /* device drivers never match interfaces */
  17.         if (is_usb_device_driver(drv))
  18.             return 0;

  19.         intf = to_usb_interface(dev);
  20.         usb_drv = to_usb_driver(drv);

  21.         id = usb_match_id(intf, usb_drv->id_table);
  22.         if (id)
  23.             return 1;

  24.         id = usb_match_dynamic_id(intf, usb_drv);
  25.         if (id)
  26.             return 1;
  27.     }

  28.     return 0;
  29. }

  30. //device的探测函数
  31. static int usb_probe_device(struct device *dev)
  32. {
  33.     struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  34.     struct usb_device *udev = to_usb_device(dev);
  35.     int error = -ENODEV;

  36.     dev_dbg(dev, "%s\n", __func__);

  37.     /* TODO: Add real matching code */

  38.     /* The device should always appear to be in use
  39.      * unless the driver suports autosuspend.
  40.      */
  41.     udev->pm_usage_cnt = !(udriver->supports_autosuspend);

  42.     error = udriver->probe(udev);//usb_driver,回到通用驱动usb_generic_driver
  43.     return error;
  44. }



//通用驱动探测


点击(此处)折叠或打开

  1. static int generic_probe(struct usb_device *udev)
  2. {
  3.     int err, c;

  4.     /* Choose and set the configuration. This registers the interfaces
  5.      * with the driver core and lets interface drivers bind to them.
  6.      */
  7.     if (usb_device_is_owned(udev))
  8.         ; /* Don't configure if the device is owned */
  9.     else if (udev->authorized == 0)
  10.         dev_err(&udev->dev, "Device is not authorized for usage\n");
  11.     else {
  12.         c = usb_choose_configuration(udev);
  13.         if (c >= 0) {
  14. // 既然已经选好配置了,那就告诉设备选好的配置,这个过程是在usb_set_configuration()中完成的
  15. //控制器和外设(hub)的通讯
  16.             err = usb_set_configuration(udev, c);//这里又会生仔(设备)哟;
  17.             if (err) {
  18.                 dev_err(&udev->dev, "can't set config #%d, error %d\n",c, err);
  19.                 /* This need not be fatal. The user can try to
  20.                  * set other configurations. */
  21.             }
  22.         }
  23.     }
  24.     /* USB device state == configured ... usable */
  25.     usb_notify_add_device(udev);

  26.     return 0;
  27. }

流程走完了;

这里完成后,整个驱动到了一个新的阶段,usb的枚举阶段的设备配置就完成;配置设备的含义==控制器讲:"你这个设备能够提供这么强大的力量,我选择一种"。
真正usb的功能驱动才刚刚开始,通常讲mass storage,webcam,包括hub的真正的驱动。

之前讲的那些驱动,都是些皮毛,还没有到肉,至少我们还没有弄明白,我们没有学会真正地控制usb;其实里面有好多的usb设备信息读取,我都漏掉,再整理的时候,找个时间再补充上每一章上去;



后面就要涉及到接口设备和驱动了;
阅读(2115) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~