Linux设备模型--驱动


linux中每个设备驱动由一个 struct  device_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.     int (*probe) (struct device *dev);  
  7.     int (*remove) (struct device *dev);  
  8.     void (*shutdown) (struct device *dev);  
  9.     int (*suspend) (struct device *dev, pm_message_t state);  
  10.     int (*resume) (struct device *dev);  
  11.     struct attribute_group **groups;  
  12.     struct dev_pm_ops *pm;  
  13.     struct driver_private *p;  
  14. };  
  15. struct driver_private {  
  16.     struct kobject kobj;  
  17.     struct klist klist_devices;               //该驱动所管理的设备链表头  
  18.     struct klist_node knode_bus;              //挂入总线链表中的指针  
  19.     struct module_kobject *mkobj;  
  20.     struct device_driver *driver;  
  21. };  
  22. struct driver_attribute {  
  23.     struct attribute attr;  
  24.     ssize_t (*show)(struct device_driver *driver, char *buf);  
  25.     ssize_t (*store)(struct device_driver *driver, const char *buf,  
  26.              size_t count);  
  27. };  

可以看出device结构体中也包含了一个kobject对象 和连接设备的链表。

当设备从系统总删除是remove被调用。

当系统关机的时候shutdown被调用。

注册一个设备驱动
int driver_register(struct d river  *drv)
删除一个设备驱动
void  d river _unregister(struct d river  *drv)


创建属性
int driver_create_file(struct driver *drv,struct driver_attribute *attr)
删除属性
int  d river _remove_file(struct d river  *drv,struct d river _attribute *attr)

struct device_driver中有个int  (*probe) ( struct  device *dev)函数,它什么时候调用呢?
device_driver中 name指向驱动的名字,前面的 struct  device 中也有一个名为bus_id的字符数组。查看一下,struct bus_type中有一个match函数,这个是干什么用的呢。设备有了驱动才可以工作,只有驱动没有设备也是不行,驱动和设备需要关联上,这就需要这个match函数。驱动和设备是通过name来管理的,所以在总线驱动match函数中要比较device的bus_id和driver中的name是否相等,什么时候比较?只要往总线添加设备或驱动时,总线都会把调用 match函数对新添加的设备或驱动中名字与总线中已经注册的驱动或设备的名字一一比较 。如果有相等,就说明驱动和设备互相找到了,这时device_driver中的probe函数就被调用。

添加设备驱动例子,添加到前面创建的my_bus总线上:
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>

MODULE_AUTHOR("David Xie");
MODULE_LICENSE("Dual BSD/GPL");

extern struct bus_type my_bus_type;

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

static int my_remove(struct device *dev)
{
    printk("Driver found device unpluged!\n");
    return 0;
}

struct device_driver my_driver = {
.name = "my_dev",
.bus = &my_bus_type,
.probe = my_probe,
        .remove = my_remove,
};

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

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

static int __init my_driver_init(void)
{
int ret = 0;
        
        /*注册驱动*/
driver_register(&my_driver);
/*创建属性文件*/
driver_create_file(&my_driver, &driver_attr_drv);
return ret;

}

static void my_driver_exit(void)
{
driver_unregister(&my_driver);
}

module_init(my_driver_init);
module_exit(my_driver_exit);

创建一个名为“bus_dev”的驱动,并将bus成员指向第一步创建的my_bus_type总线

测试结果:

加载驱动将会打印:Driver found device which my driver can handle!
查看 /sys/bus/my_bus/driver
ls  /sys/bus/my_bus/driver
my_dev



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值