设备驱动模型第1节:平台-设备-驱动2

来源:互联网 发布:ntp服务器windows工具 编辑:程序博客网 时间:2024/06/01 11:59

设备:

(1)平台设备(说白了就是CPU内部的一个控制器,比如I2C控制器或是LCD控制器等)

struct platform_device

   const char     * name; //平台设备名字

   int                   id;     //平台设备的序列号,像UART有多个的话,就从0~开始

 struct device   dev;  //核心成员,也是linux驱动里面设备的基石

   u32           num_resources;  

    struct resource  * resource; //该平台设备的资源(物理寄存器内存地址、IO中断等)

   const struct platform_device_id *id_entry; 

   struct pdev_archdata   archdata; 

};

平台设备驱动

struct platform_driver {

       int(*probe)(struct platform_device *);

       int(*remove)(struct platform_device *);

       void(*shutdown)(struct platform_device *);

       int(*suspend)(struct platform_device *, pm_message_t state);

       int(*suspend_late)(struct platform_device *, pm_message_t state);

       int(*resume_early)(struct platform_device *);

       int(*resume)(struct platform_device *);

       struct device_driver driver;//核心成员,也是linux驱动里面驱动的基石

       structplatform_device_id *id_table;

};

 

platform_device_register(structplatform_device *);         /*注册一个平台设备*/

platform_device_unregister(structplatform_device *);    /*注销一个平台设备*/

platform_driver_register(struct platform_driver*drv)/*注册一个平台设备的驱动*/

platform_driver_unregister(structplatform_driver*drv)  /*注销一个平台设备的驱动*/

platform_get_resource(structplatform_device *, unsigned int, unsigned int);/*得到一个平台设备的资源*/

platform_get_irq(structplatform_device *, unsigned int);/*得到某个平台设备的中断号*/

platform_add_devices(structplatform_device **, int); /*向系统添加一系列设备的资源*/

 

(2)具体某一个设备(比如一个LCD液晶屏)

struct device {

struct device  * parent; //父设备,指向所该设备所属总线设备。

struct kobject kobj;     //代表自身
char init_name;           //设备的名称
struct bus_type * bus;  //该设备所属的总线
struct device_driver *driver; //该设备所匹配的驱动

void   *driver_data;    //指向驱动私有数据
void *platform_data; //由驱动定义并使用*/

///更多字段忽略了

};

注册设备:intdevice_register(sruct device *dev)

注销设备:voiddevice_unregister(struct device *dev);

(3)设备属性:

sysfs 中的设备入口可有属性. 相关的结构是:
struct device_attribute {
struct attribute attr;
ssize_t (*show)(struct device *dev, char *buf);
ssize_t (*store)(struct device *dev, const char *buf,
size_t count);
};
这些属性结构可在编译时建立, 使用这些宏:
DEVICE_ATTR(name, mode, show, store);
结果结构通过前缀dev_attr_ 到给定名子上来命名. 属性文件的实际管理使用通常的函数对来处理:
int device_create_file(struct device *device, struct device_attribute *attr);
void device_remove_file(struct device *dev, struct device_attribute *attr);
struct bus_type 的 dev_attrs 成员指向一个缺省的属性列表, 这些属性给添加到总线的每个设备创建.

(4)创建设备实例:

创建设备和创建总线基本一样这里只贴出示例程序:

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>


extern struct device       my_bus;
extern struct bus_type  my_bus_type;

static void my_dev_release(struct device *dev)
{
  printk("this is an driver sample !");
}

struct device my_dev = {
 .bus = &my_bus_type,   //与总线接上关系
 .parent = &my_bus,      //与总线设备接上关系
 .release = my_dev_release,
};


static ssize_t zhouxh_show(struct device *dev,struct device_attribute *attr,char *buf)
{
 return sprintf(buf, "%s\n","This is my device!");
}

static DEVICE_ATTR(dev, S_IRUGO, zhouxh_show, NULL);

static int __init my_device_init(void)
{
 int ret = 0;
       
  /* 初始化设备 */
 strncpy(my_dev.bus_id,"my_dev", BUS_ID_SIZE);   
  /*注册设备*/
 device_register(&my_dev);
  /*创建属性文件*/
 device_create_file(&my_dev,&dev_attr_dev);
 
 return ret;

}

static void my_device_exit(void)
{
 device_unregister(&my_dev);
}

module_init(my_device_init);
module_exit(my_device_exit);


MODULE_AUTHOR("zhouxh@163.com");
MODULE_LICENSE("Dual BSD/GPL");

0 0
原创粉丝点击