【转】linux设备驱动模型 之driver…

来源:互联网 发布:linux ide开发工具 编辑:程序博客网 时间:2024/06/07 03:15

1、 驱动描述
      驱动程序由struct device_driver 描述:
struct device_driver{
const char *name;
struct bus_type *bus;
struct module
*owner;
const char
*mod_name;
int (*probe) (structdevice *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来描述:
structdriver_attribute {
struct attribute attr;
ssize_t (*show)(struct device_driver*drv,
char *buf);
ssize_t (*store)(structdevice_driver *drv,
const char *buf, size_tcount);
}
     1)int driver_create_file(struct device_driver * drv, structdriver_attribute * attr)
           创建属性
     2)void driver_remove_file(struct device_driver * drv, structdriver_attribute * attr)
           删除属性
4、 实例分析
      driver.c源码
#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");
    return0;
}

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

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、 试验结果

【转】linux设备驱动模型 之driver(驱动)原理与实例分析 - 阿 - 我的博客

 
原创粉丝点击