Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7556247
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2012-07-30 14:13:27

  设备模型跟踪所有系统所知道的设备。进行跟踪的主要原因是让驱动程序协调与设备之间的关系。

先看驱动程序的结构体,仅仅贴出一些重要的成员:

点击(此处)折叠或打开

  1. /*linux/device.h*/
  2.  struct device_driver {
  3.      const char *name; //驱动函数的名字,在对应总线的driver目录下显示
  4.      struct bus_type *bus; //指定该驱动程序所操作的总线类型,必须设置,不然会注册失败
  5.     
  6.      struct module *owner;
  7.      const char *mod_name; /* used for built-in modules */

  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.      struct attribute_group **groups;
  14.     
  15.      struct dev_pm_ops *pm;

  16.      struct driver_private *p;
  17.  };


驱动注册/注销

int driver_register(struct device_driver *drv) 注册驱动

void driver_unregister(struct device_driver *drv) 注销驱动

驱动属性

驱动的属性使用struct driver_attribute 来描述:

struct driver_attribute {

struct attribute attr;

ssize_t (*show)(struct device_driver *drv,char *buf);

ssize_t (*store)(struct device_driver *drv,const char *buf, size_t count);

}

int driver_create_file(struct device_driver * drv,struct driver_attribute * attr) 创建属性

void driver_remove_file(struct device_driver * drv,struct driver_attribute * attr) 删除属性

 

源码实例:

点击(此处)折叠或打开

  1. #include <linux/device.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/string.h>

  6. extern struct bus_type my_bus_type;

  7. /*当驱动找到对应的设备时会执行该函数*/
  8. static int my_probe(struct device *dev)
  9. {
  10.     printk("Driver found device which my driver can handle!\n");
  11.     return 0;
  12. }

  13. static int my_remove(struct device *dev)
  14. {
  15.     printk("Driver found device unpluged!\n");
  16.     return 0;
  17. }

  18. struct device_driver my_driver = {
  19.         .name = "my_dev",
  20.         .bus = &my_bus_type,
  21.         .probe = my_probe,
  22.         .remove = my_remove,
  23. };

  24. static ssize_t mydriver_show(struct device_driver *driver, char *buf)
  25. {
  26.         return sprintf(buf, "%s\n", "This is my driver!");
  27. }

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

  29. static int __init my_driver_init(void)
  30. {
  31.         int ret = 0;

  32.         /*注册驱动*/
  33.         driver_register(&my_driver);
  34.         
  35.         /*创建属性文件*/
  36.         driver_create_file(&my_driver, &driver_attr_drv);

  37.         return ret;
  38. }

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

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

  45. MODULE_AUTHOR("David Xie");
  46. MODULE_LICENSE("GPL");


 

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