Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519374
  • 博文数量: 398
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-21 16:02
个人简介

嵌入式屌丝

文章分类

全部博文(398)

文章存档

2013年(398)

我的朋友

分类: LINUX

2013-08-23 09:44:03

    如果你了解了前面总线、设备模型,分析总线设备驱动模型的driver相对来说会轻松很多。开始也是看看其数据结构。

点击(此处)折叠或打开

  1. struct device_driver {
  2.     const char        *name;                 //驱动的名字
  3.     struct bus_type        *bus;             //驱动呈现属于的总线类型

  4.     struct module        *owner;
  5.     const char        *mod_name;    /* used for built-in modules */

  6.     bool suppress_bind_attrs;    /* disables bind/unbind via sysfs */

  7.     const struct of_device_id    *of_match_table;

  8.     int (*probe) (struct device *dev);     //驱动挂载的时候调用
  9.     int (*remove) (struct device *dev);    //卸载的时候调用
  10.     void (*shutdown) (struct device *dev);
  11.     int (*suspend) (struct device *dev, pm_message_t state);
  12.     int (*resume) (struct device *dev);
  13.     const struct attribute_group **groups;

  14.     const struct dev_pm_ops *pm;

  15.     struct driver_private *p;
  16. };
与device类型相似,其中有一个指向driver_private的指针p,一些与其他的组件相关的联系都被移到这个结构变量中。

点击(此处)折叠或打开

  1. struct driver_private {
  2.     struct kobject kobj;      //在sysfs中代表目录本身
  3.     struct klist klist_devices;//驱动链表
  4.     struct klist_node knode_bus; //挂载在总线的驱动链表的节点
  5.     struct module_kobject *mkobj;//driver与相关的module之间的联系
  6.     struct device_driver *driver;
  7. };
  8. #define to_driver(obj) container_of(obj, struct driver_private, kobj)
由上面可以看出driver指针最后也有driver_private回到了device_driver之中,下面也来看看驱动的属性文件的表示方法

点击(此处)折叠或打开

  1. struct driver_attribute {
  2.     struct attribute attr;
  3.     ssize_t (*show)(struct device_driver *driver, char *buf);
  4.     ssize_t (*store)(struct device_driver *driver, const char *buf,
  5.              size_t count);
  6. };

  7. #define DRIVER_ATTR(_name, _mode, _show, _store)    \
  8. struct driver_attribute driver_attr_##_name =        \
  9.     __ATTR(_name, _mode, _show, _store)
上面只是有两个读写函数,看完了关于驱动的一些重要的数据结构,那么开始重要的,如何向内核注册一个drv呢?我们使用driver_register

点击(此处)折叠或打开

  1. int driver_register(struct device_driver *drv)
  2. {
  3.     int ret;
  4.     struct device_driver *other;

  5.     BUG_ON(!drv->bus->p);

  6.     if ((drv->bus->probe && drv->probe) ||
  7.      (drv->bus->remove && drv->remove) ||
  8.      (drv->bus->shutdown && drv->shutdown))
  9.         printk(KERN_WARNING "Driver '%s' needs updating - please use "
  10.             "bus_type methods\n", drv->name);

  11.     other = driver_find(drv->name, drv->bus);
  12.     if (other) {
  13.         put_driver(other);
  14.         printk(KERN_ERR "Error: Driver '%s' is already registered, "
  15.             "aborting...\n", drv->name);
  16.         return -EBUSY;
  17.     }

  18.     ret = bus_add_driver(drv);
  19.     if (ret)
  20.         return ret;
  21.     ret = driver_add_groups(drv, drv->groups);
  22.     if (ret)
  23.         bus_remove_driver(drv);
  24.     return ret;
  25. }
从函数可以看出,首先drv->bus一定要预先设置。在使用driver_find从bus的驱动链表中特定名字的driver,那么就进入这个函数的重点的东西bus_add_driver,几乎注册所有的工作都是由它来完成。

点击(此处)折叠或打开

  1. int bus_add_driver(struct device_driver *drv)
  2. {
  3.     struct bus_type *bus;
  4.     struct driver_private *priv;
  5.     int error = 0;

  6.     bus = bus_get(drv->bus);//增加对bus的引用
  7.     if (!bus)
  8.         return -EINVAL;

  9.     pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);

  10.     priv = kzalloc(sizeof(*priv), GFP_KERNEL);//分配初始化一个drv->p,也就是上面的driver_private结构
  11.     if (!priv) {
  12.         error = -ENOMEM;
  13.         goto out_put_bus;
  14.     }
  15.     klist_init(&priv->klist_devices, NULL, NULL);
  16.     priv->driver = drv;
  17.     drv->p = priv;
  18.     priv->kobj.kset = bus->p->drivers_kset;
  19.     error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  20.                  "%s", drv->name);//将drv加入sysfs
  21.     if (error)
  22.         goto out_unregister;

  23.     if (drv->bus->p->drivers_autoprobe) {
  24.         error = driver_attach(drv);//如果总线可以自动的probe,就会调用匹配函数
  25.         if (error)
  26.             goto out_unregister;
  27.     }
  28.     klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);//将drv挂入到总线的链表中
  29.     module_add_driver(drv->owner, drv);//创建driver相关的模块

  30.     error = driver_create_file(drv, &driver_attr_uevent);//在drv目录下创建event属性文件
  31.     if (error) {
  32.         printk(KERN_ERR "%s: uevent attr (%s) failed\n",
  33.             __func__, drv->name);
  34.     }
  35.     error = driver_add_attrs(bus, drv);//添加属性
  36.     if (error) {
  37.         /* How the hell do we get out of this pickle? Give up */
  38.         printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
  39.             __func__, drv->name);
  40.     }

  41.     if (!drv->suppress_bind_attrs) {
  42.         error = add_bind_files(drv);
  43.         if (error) {
  44.             /* Ditto */
  45.             printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  46.                 __func__, drv->name);
  47.         }
  48.     }

  49.     kobject_uevent(&priv->kobj, KOBJ_ADD);//向用户空间发布kobj_add消息
  50.     return 0;

  51. out_unregister:
  52.     kobject_put(&priv->kobj);
  53.     kfree(drv->p);
  54.     drv->p = NULL;
  55. out_put_bus:
  56.     bus_put(bus);
  57.     return error;
  58. }
其实上面的处理过程相对于设备来说,会简单很多,下面主要对当驱动挂接的时候,怎么去匹配进行分析。

点击(此处)折叠或打开

  1. int driver_attach(struct device_driver *drv)
  2. {
  3.     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  4. }

点击(此处)折叠或打开

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

  4.     /*
  5.      * Lock device and try to bind to it. We drop the error
  6.      * here and always return 0, because we need to keep trying
  7.      * to bind to devices and some drivers will return an error
  8.      * simply if it didn't support the device.
  9.      *
  10.      * driver_probe_device() will spit a warning if there
  11.      * is an error.
  12.      */

  13.     if (!driver_match_device(drv, dev))
  14.         return 0;

  15.     if (dev->parent)    /* Needed for USB */
  16.         device_lock(dev->parent);
  17.     device_lock(dev);
  18.     if (!dev->driver)
  19.         driver_probe_device(drv, dev);
  20.     device_unlock(dev);
  21.     if (dev->parent)
  22.         device_unlock(dev->parent);

  23.     return 0;

点击(此处)折叠或打开

  1. static inline int driver_match_device(struct device_driver *drv,
  2.                  struct device *dev)
  3. {
  4.     return drv->bus->match ? drv->bus->match(dev, drv) : 1;
  5. }
最终也是调用总线的match函数来完成设备与驱动的匹配的过程。
以上分析了总线、设备、驱动三种类型,主要是在注册上,其主要是在sysfs中创建目录和属性文件。在设备或者驱动注册到总线上,总线是如何为其找到对应的驱动的过程,下面一个图能很好的说明这一过程。
由图可以清楚的看出,bus的作用是向内核注册一条总线,并将drv一一加入到总线的drv链表,dev一一加入到总线的dev链表。当有设备或驱动注册的时候,在驱动或者设备链表一一取出,调用总线的match函数来完成匹配,匹配成功后调用总线的probe函数。
回顾下driver_register的作用,首先会将drv放入到bus得drv链表,从bus的dev链表取出每一个dev,用总线的match函数来判断能否支持drv
device_register的作用和driver一样,将dev放入到bus得dev链表,从bus的drv链表取出每一个drv,用总线的match函数来判断能否支持dev。

点击(此处)折叠或打开

  1. extern struct bus_type my_bus_type;

  2. static int my_probe(struct device *dev)
  3. {
  4.     printk("Driver found device which my driver can handle!\n");
  5.     return 0;
  6. }

  7. static int my_remove(struct device *dev)
  8. {
  9.     printk("Driver found device unpluged!\n");
  10.     return 0;
  11. }

  12. struct device_driver my_driver = {
  13.     .name = "my_dev",
  14.     .bus = &my_bus_type,
  15.     .probe = my_probe,
  16.         .remove    = my_remove,
  17. };

  18. /*
  19.  * Export a simple attribute.
  20.  */
  21. static ssize_t mydriver_show(struct device_driver *driver, char *buf)
  22. {
  23.     return sprintf(buf, "%s\n", "This is my driver!");
  24. }

  25. static DRIVER_ATTR(drv, S_IRUGO, mydriver_show, NULL);

  26. static int __init my_driver_init(void)
  27. {
  28.     int ret = 0;
  29.         
  30.         /*注册驱动*/
  31.     driver_register(&my_driver);
  32.         
  33.     /*创建属性文件*/
  34.     driver_create_file(&my_driver, &driver_attr_drv);
  35.     
  36.     return ret;    

  37. }

  38. static void my_driver_exit(void)
  39. {
  40.     driver_unregister(&my_driver);
  41. }

  42. module_init(my_driver_init);
  43. module_exit(my_driver_exit);

阅读(250) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~