linux设备驱动剖析,driver(驱动)原理与实例分析

1、 驱动描述

驱动程序由struct device_driver 描述 :

struct device_driver {

const char *name; /*驱动程序的名字( 体现在 sysfs 中 )*/

struct bus_type *bus; /*驱动程序所在的总线*/

struct module

*owner;

const char

*mod_name;

int (*probe) (struct device *dev);

int (*remove) (struct device *dev);

void (*shutdown) (struct device *dev);

int (*suspend) (struct device *dev, pm_message_t state);

int (*resume) (struct device *dev);

struct attribute_group **groups;

struct dev_pm_ops *pm;

struct driver_private *p;

}

2、 驱动注册/注册

1)int driver_register(struct device_driver *drv)

注册驱动

2)void driver_unregister(struct device_driver *drv)

注销驱动

3、 驱动属性

驱动的属性使用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);

}

1)int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)

创建属性

2)void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)

删除属性

4、 实例分析

driver.c源码

#include

#include

#include

#include

#include

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,

};

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);

5、 试验结果

eb2d92136dbdccf773fa8d18aac513cb.png0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值