`
mmdev
  • 浏览: 12919185 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Linux设备驱动工程师之路——设备模型(下)上层模型

 
阅读更多

Linux设备驱动工程师之路——设备模型(下)上层模型

K-Style

转载请注明来自于衡阳师范学院08电2 Y-Kee http://blog.csdn.net/ayangke,QQ:843308498

一、重要知识点:

设备模型由总线、设备、驱动三要素组成。

底层模型决定上层模型,在总线,设备,驱动的结构体中你总是可以看到它们间接或者直接的包含了kobject结构或kset结构。

1.总线

总线是处理器和设备之间的通道,在设备模型中,所有设备都通过总线相连,甚至内部的虚拟“platform”总线。在linux设备模型中,总线由bus_type结构表示。定义在<linux/device>中。

总线描述

        struct bus_type {
         const char                  *name;//总线名称
         struct bus_attribute        *bus_attrs//总线属性
         struct device_attribute   *dev_attrs;//设备属性
         struct driver_attribute    *drv_attrs;//驱动属性
         int (*match)(struct device *dev, struct device_driver *drv);
         int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
         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 (*suspend_late)(struct device *dev, pm_message_t state);
         int (*resume_early)(struct device *dev);
         int (*resume)(struct device *dev);
         struct dev_pm_ops *pm;
         struct bus_type_private *p;
};

bus_type的bus_type_private成员

struct bus_type_private {
struct kset subsys;
struct kset *drivers_kset;
struct kset *devices_kset;
struct klist klist_devices;
struct klist klist_drivers;
struct blocking_notifier_head bus_notifier;
unsigned int drivers_autoprobe:1;
struct bus_type *bus;
};


可以看出该结构体中包含了有device和dirver的kset对象。

总线的注册:

int bus_register(structbus_type *bus);

如果成功,新的总线将被添加到系统,在/sys/bus目录下可以看到。

我们在深入看这个函数

int bus_register(struct bus_type *bus)
{
int retval;
struct bus_type_private *priv;
priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->bus = bus;
bus->p = priv;
BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
if (retval)
goto out;

priv->subsys.kobj.kset = bus_kset;
priv->subsys.kobj.ktype = &bus_ktype;
priv->drivers_autoprobe = 1;

retval = kset_register(&priv->subsys);
if (retval)
goto out;

retval = bus_create_file(bus, &bus_attr_uevent);
if (retval)
goto bus_uevent_fail;


priv->devices_kset = kset_create_and_add("devices", NULL,
&priv->subsys.kobj);
if (!priv->devices_kset) {
retval = -ENOMEM;
goto bus_devices_fail;
}


priv->drivers_kset = kset_create_and_add("drivers", NULL,
&priv->subsys.kobj);
if (!priv->drivers_kset) {
retval = -ENOMEM;
goto bus_drivers_fail;
}


klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
klist_init(&priv->klist_drivers, NULL, NULL);

retval = add_probe_files(bus);
if (retval)
goto bus_probe_files_fail;

retval = bus_add_attrs(bus);
if (retval)
goto bus_attrs_fail;

pr_debug("bus: '%s': registered\n", bus->name);
return 0;

bus_attrs_fail:
remove_probe_files(bus);
bus_probe_files_fail:
kset_unregister(bus->p->drivers_kset);
bus_drivers_fail:
kset_unregister(bus->p->devices_kset);
bus_devices_fail:
bus_remove_file(bus, &bus_attr_uevent);
bus_uevent_fail:
kset_unregister(&bus->p->subsys);
kfree(bus->p);
out:
return retval;
}


从这里

priv->subsys.kobj.kset = bus_kset;
priv->subsys.kobj.ktype = &bus_ktype;

可以看得出总线初始化了自己的kset对象。

总线的删除

void bus_unregister(struct bus_type *bus);

总线的match方法

int (*match)(struct device *dev, struct device_driver *drv);

当一个新设备或者驱动被添加到这个总线时,该方法被调用,用于判断指定的驱动程序是否能处理指定的设备。若可以,则返回非0值。下面的测试程序采用匹配设备的dev->bus_id字符串和驱动的drv->name字符串是否相同来判断驱动是否能处理该设备。

总线uevent方法

int (*uevent)(structdevice *dev, char **envp, int num_envp)

在为用户产生热插拔事件之前,这个方法允许总线添加环境变量。

总线属性由结构bus_attribute描述:

struct bus_attribute {

struct attribute attr;

ssize_t (*show)(struct bus_type *bus,char *buf);

ssize_t (*store)(struct bus_type *bus,const char *buf, size_t count);

};

BUS_ATTR(name,mode, show, store)

在编译时创建和初始化bus_attribute结构,它将bus_attr_作为给定前缀来创建总线的真正名称。

如:static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);

将创建一个bus_attr_version结构体对象。

int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)

创建属性文件

void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)

删除属性文件

2.设备

struct device {
struct klist klist_children;
struct klist_nodeknode_parent;/* node in sibling list */
struct klist_nodeknode_driver;
struct klist_nodeknode_bus;
struct device *parent;
struct kobject kobj;
char bus_id[BUS_ID_SIZE];/* position on parent bus */
unsigned uevent_suppress:1;
const char *init_name; /* initial name of the device */
struct device_type*type;

struct semaphoresem;/* semaphore to synchronize calls to
* its driver.
*/
struct bus_type*bus;/* type of bus device is on */
struct device_driver *driver;/* which driver has allocated this
  device */
void *driver_data;/* data private to the driver */
void *platform_data;/* Platform specific data, device
  core doesn't touch it */
struct dev_pm_infopower;


#ifdef CONFIG_NUMA
int numa_node;/* NUMA node this device is close to */
#endif
u64 *dma_mask;/* dma mask (if dma'able device) */
u64 coherent_dma_mask;/* Like dma_mask, but for
    alloc_coherent mappings as
    not all hardware supports
    64 bit addresses for consistent
    allocations such descriptors. */


struct device_dma_parameters *dma_parms;


struct list_headdma_pools;/* dma pools (if dma'ble) */


struct dma_coherent_mem*dma_mem; /* internal for coherent mem
    override */
/* arch specific additions */
struct dev_archdataarchdata;

dev_t devt;/* dev_t, creates the sysfs "dev" */

spinlock_t devres_lock;
struct list_headdevres_head;


struct klist_nodeknode_class;
struct class *class;
struct attribute_group**groups;/* optional groups */

void (*release)(struct device *dev);
};


可以看出device结构体中也包含了一个kobject对象

intdevice_register(struct device *dev);

注册设备

vioddevice_unregister(struct device *dev)

注销设备

设备属性由struct device_attribute描述

structdevice_attribute {

struct attribute attr;

ssize_t (*show)(struct device *dev,struct device_attribute *attr,

char *buf);

ssize_t (*store)(struct device *dev,struct device_attribute *attr,

const char *buf, size_t count);

};

int device_create_file(struct device *dev, struct device_attibute *entry)

创建属性文件

void device_remove_file(struct device *dev, struct device_attibute *entry)

删除属性文件

3.驱动

驱动程序由struct device_driver描述:

struct device_driver {
const char *name;
struct bus_type*bus;


struct module *owner;
const char *mod_name;/* used for built-in modules */


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


在看device_dirver的driver_private 对象

struct driver_private {
struct kobject kobj;
struct klist klist_devices;
struct klist_node knode_bus;
struct module_kobject *mkobj;
struct device_driver *driver;
};

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

当总线的match返回非0值,也就是总线找到与驱动相匹配的设备时,驱动的probe的函数将被调用。

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

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

int driver_register(structdevice_driver *drv)

在总线上注册驱动

void driver_unregister(struct device_driver *drv)

载总线上注销驱动

驱动的属性使用structdriver_attribute来描述

structdriver_attribute{

structattribue attr;

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

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

}

int dirver_create_file(struct device_driver *drv, struct driver_attribute *attr);

创建属性文件

void driver_remove_file struct device_driver *drv, struct driver_attribute *attr);

删除属性文件


二、测试模块

1.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");

static char *Version = "$Revision: 1.9 {1}quot;;

static int my_match(struct device *dev, struct device_driver *driver)
{
	return !strncmp(dev->bus_id, driver->name, strlen(driver->name));
}

static void my_bus_release(struct device *dev)
{
	printk(KERN_DEBUG "my bus release\n");
}
	
struct device my_bus = {
	.bus_id   = "my_bus0",
	.release  = my_bus_release
};


struct bus_type my_bus_type = {
	.name = "my_bus",
	.match = my_match,
};

EXPORT_SYMBOL(my_bus);
EXPORT_SYMBOL(my_bus_type);


/*
 * Export a simple attribute.
 */
static ssize_t show_bus_version(struct bus_type *bus, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%s\n", Version);
}

static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);


static int __init my_bus_init(void)
{
	int ret;
        
        /*注册总线*/
	ret = bus_register(&my_bus_type);
	if (ret)
		return ret;
		
	/*创建属性文件*/	
	if (bus_create_file(&my_bus_type, &bus_attr_version))
		printk(KERN_NOTICE "Fail to create version attribute!\n");
	
	/*注册总线设备*/
	ret = device_register(&my_bus);
	if (ret)
		printk(KERN_NOTICE "Fail to register device:my_bus!\n");
		
	return ret;
}

static void my_bus_exit(void)
{
	device_unregister(&my_bus);
	bus_unregister(&my_bus_type);
}

module_init(my_bus_init);
module_exit(my_bus_exit);

创建一条名为my_bus_type的总线和一个名为my_bus的总线设备,注意总线也是一个设备,也需要注册。

测试结果:



2.DEVICE

#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 device my_bus; 
extern struct bus_type my_bus_type;

/* Why need this ?*/
static void my_dev_release(struct device *dev)
{ 
	
}

struct device my_dev = {
	.bus = &my_bus_type,
	.parent = &my_bus,
	.release = my_dev_release,
};

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

static DEVICE_ATTR(dev, S_IRUGO, mydev_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);

注册一个bus_id即名字为my_dev的设备,该设备的bus成员指向上一步创建的my_bus_type总线,parent成员指向上一步创建的my_bus总线设备。

测试结果:


3.DRIVER

#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总线

测试结果:



分享到:
评论

相关推荐

    Linux设备驱动编程模型之上层容器篇.docx

    Linux设备驱动编程模型之上层容器篇.docx

    《Linux设备驱动开发详解》第18章、LCD设备驱动

    作者:华清远见嵌入式学院。...帧缓冲(Framebuffer)是Linux为显示设备提供的一个接口,它允许上层应用程序在图形模式下直接对显示缓冲区进行读写操作。本章主要讲解帧缓冲设备Linux驱动的架构及编程方法。

    linux设备模型.pdf

    接着, 第4, 5, 6章描述设备模型的基础概念, 驱动开发中基本上不会看到这些基础, 不过我们必须先理解它们, 才能更好的理解后续内容. 然后, 第7章简单介绍了下/sys/下的一些顶层目录是如何创建出来的, 可以浏览一下. ...

    电子科技大学,嵌入式linux设备驱动程序的开发

    电子科技大学,嵌入式linux设备驱动程序的开发 从linux内核 到上层软件开发

    从串口驱动到Linux驱动模型

    本文通过对Linux下串口驱动的分析。由最上层的C库。到操作系统系统调用层的封装,再到tty子系统的核心。再到一系列线路规程。再到最底层的硬件操作从串口驱动到Linux驱动模型,从一个模型入手,详细的分析了流程,...

    USB设备驱动程序与上层应用软件的IO操作设计

    USB设备驱动程序与上层应用软件的IO操作设计

    Linux设备模型之input子系统详解

    Input子系统是所有I/O设备驱动的中间层,为上层提供了一个统一的界面。例如,在终端系统中,我们不需要去管有多少个键盘,多少个鼠标。它只要从input子系统中去取对应的事件(按键,鼠标移位等)就可以了。

    8810音频驱动linux-android。完整资料.zip

    在音频设备驱动中ASoC被分为Machine、Platform和Codec三大部分。 Codec部分:负责音频解码,这部分代码全然无平台无关(设备原厂提供) Platform部分:包括了平台的音频DMA和音频接口的配置和控制(I2S,PCM。AC97...

    Linux设备模型(中)之上层容器.pdf

    Linux内核经典文献之《 Linux设备模型(中)之上层容器.pdf 》,适合深入Linux内核!

    Linux中LCD设备驱动

    帧缓冲(framebuffer)是Linux 系统为显示设备提供的一个接口,它将显示缓冲区抽象,屏蔽图像硬件的底层差异,允许上层应用程序在图形模式下直接对显示缓冲区进行读写操作。用户不必关心物理显示缓冲区的具体位置及...

    设备驱动程序.ppt

    在嵌入式操作系统下的设备驱动程序主要是控制和管理下层物理设备的硬件控制器,同时为上层应用提供统一的、与设备无关的系统调用服务,实现设备无关性。设备驱动程序通常包含中断处理程序和设备服务子程序2部分。 ...

    SGM31323数据手册及自己写的linux驱动代码

    此驱动是在MTK平台下写的,结合MTK下指示灯的控制接口,上层完全可以控制亮那盏灯,需要亮灭多久灭多久。里面算亮灭时间的算法自己写的,不是很精确,要求不在100ms以下的话,完全可以用。linux驱动接口都是差不多的...

    Linux下驱动的应用

    Linux驱动源码\1.8.1gdb的调试过程.c .............\10.3.3Linux USB内核的主要数据结构.c .............\11.3.10主机端设备驱动程序.c .............\11.3.3主机控制器驱动程序设计.c .............\12.2.13OTG...

    基于嵌入式Linux系统的DVB—CI设备驱动程序开发

    摘要:介绍了在嵌入式Linux系统中,使用设备驱动程序隐藏具体...上层程序提供抽象DVB—CI设备的方法,并以MontaVista的Hard Hat Linux系统、IBM的STB02500机 顶盒硬件平台为例,说明了DVB—CI设备驱动程序的实现方法。

    Linux2.6设备模型之Input子系统详解

    Input子系统是所有I/O设备驱动的中间层,为上层提供了一个统一的界面。例如,在终端系统中,我们不需要去管有多少个键盘,多少个鼠标。它只要从input子系统中去取对应的事件(按键,鼠标移位等)就可以了。本文对input...

    跟我学eCos嵌入式操作系统在SPCE3200上的应用——驱动开发篇V1.0.pdf

    eCos 的设备驱动程序支持分层结构,一个设备可以是另一个设备的上层设备,处于上层的设备 可以灵活地增加一些底层设备没有提供的功能和特性。 例如 Touch Panel 可以作为ADC 的上层设备, 它提供的功能是 ADC 不具备...

    Linux嵌入式系统的上层应用开发研究.pdf

    Linux嵌入式系统的上层应用开发研究.pdf

    Linuxbook\Linux嵌入式系统的上层应用开发研究

    Linuxbook\Linux嵌入式系统的上层应用开发研究.pdf

    嵌入式通信设备驱动程序设计标准化 (2005年)

    通过对windows和Linux环境下设备驱动程序设计模型的比较,结合通信领域嵌入式系统的特点,提出了嵌入式通信系统设备驱动程序设计标准化的构想;通过参考常用的设备驱动程序的设计思想和设计模型,制定了嵌入式通信...

Global site tag (gtag.js) - Google Analytics