Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1658875
  • 博文数量: 174
  • 博客积分: 5493
  • 博客等级: 上校
  • 技术积分: 5802
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-05 15:13
个人简介

炼狱,是为追逐光芒

文章分类

全部博文(174)

文章存档

2017年(1)

2016年(3)

2015年(9)

2014年(5)

2013年(23)

2012年(56)

2011年(45)

2010年(32)

分类: LINUX

2011-12-29 09:37:01

随着技术的不断进步,系统拓扑接口越来越复杂,对智能电源管理、热插拔支持要求也越来越高,为适应这些需求,2.6内核提供了全新的内核设备模型。设备模型三元素:总线设备驱动。另:采用总线模型,极大的提高程序可移植性
1.总线
总线就是处理器与设备(包括SOC设备,比如SPI控制器)之间的通道,在设备模型中,所有的设备都通过总线相连,Linux的虚拟总线platform总线。在Linux设备模型中,总线由bus_type结构表示,在定义
  1. struct bus_type {
  2.     const char * name;/*总线类型名称*/
  3.     struct module * owner;/*指向模块的指针(如果有), 此模块负责操作这个总线*/

  4.     struct kset subsys;/*与该总线相关的子系统*/
  5.     struct kset drivers;/*总线驱动程序的kset*/
  6.     struct kset devices;/* 挂在该总线的所有设备的kset*/

  7.     struct klist klist_devices;/*与该总线相关的驱动程序链表*/
  8.     struct klist klist_drivers;/*挂接在该总线的设备链表*/

  9.     struct blocking_notifier_head bus_notifier;

  10.     struct bus_attribute * bus_attrs; /*总线属性*/
  11.     struct device_attribute * dev_attrs; /*设备属性,指向为每个加入总线的设备建立的默认属性链表*/
  12.     struct driver_attribute * drv_attrs; /*驱动程序属性*/
  13.     struct bus_attribute drivers_autoprobe_attr;/*驱动自动探测属性*/
  14.     struct bus_attribute drivers_probe_attr;/*驱动探测属性*/

  15.     int (*match)(struct device * dev, struct device_driver * drv);
  16.     int (*uevent)(struct device *dev, char **envp,
  17.                  int num_envp, char *buffer, int buffer_size);
  18.     int (*probe)(struct device * dev);
  19.     int (*remove)(struct device * dev);
  20.     void (*shutdown)(struct device * dev);

  21.     int (*suspend)(struct device * dev, pm_message_t state);
  22.     int (*suspend_late)(struct device * dev, pm_message_t state);
  23.     int (*resume_early)(struct device * dev);
  24.     nt (*resume)(struct device * dev);
  25. /*处理热插拔、电源管理、探测和移除等事件的方法*/
  26.     unsigned int drivers_autoprobe:1;
  27. };
在新版内核中,这个结构体更简洁,隐藏了无需驱动知道的一些成员:
  1. /*linux-3.1.6*/
  2. struct bus_type {
  3.     const char        *name;
  4.     struct bus_attribute    *bus_attrs;
  5.     struct device_attribute    *dev_attrs;
  6.     struct driver_attribute    *drv_attrs;

  7.     int (*match)(struct device *dev, struct device_driver *drv);
  8.     int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  9.     int (*probe)(struct device *dev);
  10.     int (*remove)(struct device *dev);
  11.     void (*shutdown)(struct device *dev);

  12.     int (*suspend)(struct device *dev, pm_message_t state);
  13.     int (*resume)(struct device *dev);

  14.     const struct dev_pm_ops *pm;

  15.     struct subsys_private *p;
  16. };
(1)总线注册过程:
申明和初始化 bus_type 结构体。只有很少的 bus_type 成员需要初始化,大部分都由设备模型核心控制。但必须为总线指定名字及一些必要的方法。例如
  1. struct bus_type my_bus_type = {
  2.     .name = "my_bus",
  3.     .match = my_match,
  4. };
调用bus_register函数注册总线
  1. int bus_register(struct bus_type * bus)
调用可能失败, 所以必须始终检查返回值。若成功,新的总线子系统将被添加进系统,并可在 sysfs 的 /sys/bus 下看到。之后可以向总线添加设备。
  1. ret = bus_register(&my_bus_type);
  2. if (ret)
  3.     return ret;
总线删除:
  1. void bus_unregister(struct bus_type *bus)
(2)总线方法:
在 bus_type 结构中定义了许多方法,它们允许总线核心作为设备核心和单独的驱动程序之间提供服务的中介,主要介绍以下两个方法:
  1. int (*match)(struct device * dev, struct device_driver * drv);
  2. /*当一个新设备或者驱动被添加到这个总线时,这个方法会被调用一次或多次,若指定的驱动程序能够处理指定的设备,则返回非零值。*/

  3. int (*uevent)(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
  4. /*在为用户空间产生热插拔事件之前,这个方法允许总线添加环境变量(参数和 kset 的uevent方法相同)*/
例如:
  1. static int my_match(struct device *dev, struct device_driver *driver)
  2. {
  3.     return !strncmp(dev->bus_id, driver->name, strlen(driver->name));
  4. }
(3)对设备和驱动的迭代
若要编写总线层代码, 可能不得不对所有已经注册到总线的设备或驱动进行一些操作,这可能需要仔细研究嵌入到 bus_type 结构中的其他数据结构, 但最好使用内核提供的辅助函数:
  1. int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, int (*fn)(struct device *, void *));
  2. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, void *data, int (*fn)(struct device_driver *, void *));

  3. /*这两个函数迭代总线上的每个设备或驱动程序, 将关联的 device 或 device_driver 传递给 fn, 同时传递 data 值。若 start 为 NULL, 则从第一个设备开始; 否则从 start 之后的第一个设备开始。若 fn 返回非零值, 迭代停止并且那个值从 bus_for_each_dev 或bus_for_each_drv 返回。*/
(4)总线属性
几乎 Linux 设备模型中的每一层都提供添加属性的函数, 总线层也不例外。bus_attribute 类型定义在 如下:
  1. struct bus_attribute {
  2.     struct attribute attr;
  3.     ssize_t (*show)(struct bus_type *, char * buf);
  4.     ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
  5. };
可以看出struct bus_attribute 和struct attribute 很相似,其实大部分在 kobject 级上的设备模型层都是以这种方式工作。
内核提供了一个宏在编译时创建和初始化 bus_attribute 结构:
  1. BUS_ATTR(_name,_mode,_show,_store)/*这个宏声明一个结构, 将 bus_attr_ 作为给定 _name 的前缀来创建总线的真正名称*/

  2. /*总线的属性必须显式调用 bus_create_file 来创建:*/
  3. int bus_create_file(struct bus_type *bus, struct bus_attribute *attr);

  4. /*删除总线的属性调用:*/
  5. void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr);
例如:
  1. /*
  2.  * Export a simple attribute.
  3.  */
  4. static ssize_t show_bus_version(struct bus_type *bus, char *buf)
  5. {
  6.     return snprintf(buf, PAGE_SIZE, "%s\n", Version);
  7. }

  8. static BUS_ATTR(version_file, S_IRUGO, show_bus_version, NULL);
  9. //将会在/sys/bus/my_bus/下显示version_file文件
  10. if (bus_create_file(&my_bus_type, &bus_attr_version_file))
  11. printk(KERN_NOTICE "Fail to create version attribute!\n");
2.设备
Linux 系统中的每个设备由一个 struct device 代表:
  1. struct device {
  2.     struct klist klist_children;
  3.     struct klist_node knode_parent; /* node in sibling list */
  4.     struct klist_node knode_driver;
  5.     struct klist_node knode_bus;
  6.     struct device *parent;/* 设备的 "父" 设备,该设备所属的设备,通常一个父设备是某种总线或者主控制器. 如果 parent 是 NULL, 则该设备是顶层设备,较少见 */

  7.     struct kobject kobj;/*代表该设备并将其连接到结构体系中的 kobject; 注意:作为通用的规则, device->kobj->parent 应等于 device->parent->kobj*/
  8.     char bus_id[BUS_ID_SIZE];/*在总线上唯一标识该设备的字符串;例如: PCI 设备使用标准的 PCI ID 格式, 包含:, 总线, 设备, 和功能号.*/
  9.     struct device_type *type;
  10.     unsigned is_registered:1;
  11.     unsigned uevent_suppress:1;
  12.     struct device_attribute uevent_attr;
  13.     struct device_attribute *devt_attr;

  14.     struct semaphore sem; /* semaphore to synchronize calls to its driver. */
  15.     struct bus_type * bus; /*标识该设备连接在何种类型的总线上*/
  16.     struct device_driver *driver; /*管理该设备的驱动程序*/
  17.     void *driver_data; /*该设备驱动使用的私有数据成员*/
  18.     void *platform_data; /* Platform specific data, device core doesn't touch it */
  19.     struct dev_pm_info power;

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

  29.     struct list_head dma_pools; /* dma pools (if dma'ble) */

  30.     struct dma_coherent_mem *dma_mem; /* internal for coherent mem override */
  31.     /* arch specific additions */
  32.     struct dev_archdata archdata;

  33.     spinlock_t devres_lock;
  34.     struct list_head devres_head;

  35.     /* class_device migration path */
  36.     struct list_head node;
  37.     struct class *class;
  38.     dev_t devt; /* dev_t, creates the sysfs "dev" */
  39.     struct attribute_group **groups; /* optional groups */

  40.     void (*release)(struct device * dev);/*当这个设备的最后引用被删除时,内核调用该方法; 它从被嵌入的 kobject 的 release 方法中调用。所有注册到核心的设备结构必须有一个 release 方法, 否则内核将打印错误信息*/
  41. };
  42. /*在注册 struct device 前,最少要设置parent, bus_id, bus, 和 release 成员*/
(1)设备注册
  1. int device_register(struct device *dev);
  2. void device_unregister(struct device *dev);
一个实际的总线也是一个设备,所以必须单独注册,例如
  1. static void my_bus_release(struct device *dev)
  2. {
  3. printk(KERN_DEBUG "my bus release\n");
  4. }
  5. struct device my_bus_dev = {
  6. .bus_id   = "my_bus0",
  7. .release  = my_bus_release
  8. };
  9. /*注册总线设备*/
  10.     ret = device_register(&my_bus_dev);
  11.     if (ret)
  12.         printk(KERN_NOTICE "Fail to register device:my_bus!\n");
(2)设备属性
sysfs 中的设备入口可有属性,相关的结构是:
  1. /* interface for exporting device attributes 这个结构体和《LDD3》中的不同,已经被更新过了,请特别注意!*/
  2. struct device_attribute {
  3.     struct attribute attr;
  4.     ssize_t (*show)(struct device *dev, struct device_attribute *attr,char *buf);
  5.     ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
  6. };

  7. /*设备属性结构可在编译时建立, 使用以下宏:*/
  8. DEVICE_ATTR(_name,_mode,_show,_store);
  9. /*这个宏声明一个结构, 将 dev_attr_ 作为给定 _name 的前缀来命名设备属性

  10. /*属性文件的实际处理使用以下函数:*/
  11.  int device_create_file(struct device *device, struct device_attribute * entry);
  12.  void device_remove_file(struct device * dev, struct device_attribute * attr);
(3)设备结构的嵌入
device 结构包含设备模型核心用来模拟系统的信息。但大部分子系统记录了关于它们又拥有的设备的额外信息,所以很少单纯用 device 结构代表设备,而是,通常将其嵌入一个设备的高层表示中。底层驱动几乎不知道 struct device。

lddbus 驱动创建了它自己的 device 类型,并期望每个设备驱动使用这个类型来注册它们的设备:

struct ldd_device {
 char *name;
 struct ldd_driver *driver;
 struct device dev; 
}; 
#define to_ldd_device(dev) container_of(dev, struct ldd_device, dev);

lddbus 导出的注册和注销接口如下:

/*
 * LDD devices.
 */


/*
 * For now, no references to LDDbus devices go out which are not
 * tracked via the module reference count, so we use a no-op
 * release function.
 */

static void ldd_dev_release(struct device *dev)
{ }

int register_ldd_device(struct ldd_device *ldddev)
{
    ldddev->dev.bus = &ldd_bus_type;
    ldddev->dev.parent = &ldd_bus;
    ldddev->dev.release = ldd_dev_release;
    strncpy(ldddev->dev.bus_id, ldddev->name, BUS_ID_SIZE);
    return device_register(&ldddev->dev);
}
EXPORT_SYMBOL(register_ldd_device);

void unregister_ldd_device(struct ldd_device *ldddev)
{
    device_unregister(&ldddev->dev);
}
EXPORT_SYMBOL(unregister_ldd_device);



3.设备驱动程序

(1)驱动定义
  1. /*定义在<linux/device.h>*/
  2. struct device_driver {
  3.     const char * name;/*驱动程序的名字( 在 sysfs 中出现 )*/
  4.     struct bus_type * bus;/*驱动程序所操作的总线类型*/

  5.     struct kobject kobj;/*内嵌的kobject对象*/
  6.     struct klist klist_devices;/*当前驱动程序能操作的设备链表*/
  7.     struct klist_node knode_bus;

  8.     struct module * owner;
  9.     const char * mod_name; /* used for built-in modules */
  10.     struct module_kobject * mkobj;

  11.     int (*probe) (struct device * dev);/*查询一个特定设备是否存在及驱动是否可以使用它的函数*/
  12.     int (*remove) (struct device * dev);/*将设备从系统中删除*/
  13.     void (*shutdown) (struct device * dev);/*关闭设备*/
  14.     int (*suspend) (struct device * dev, pm_message_t state);
  15.     int (*resume) (struct device * dev);
  16. };

  17. /*注册device_driver 结构的函数是:*/
  18. int driver_register(struct device_driver *drv);
  19. void driver_unregister(struct device_driver *drv);

  20. /*driver的属性结构在:*/
  21. struct driver_attribute {
  22.  struct attribute attr;
  23.  ssize_t (*show)(struct device_driver *drv, char *buf);
  24.  ssize_t (*store)(struct device_driver *drv, const char *buf, size_t count);
  25. };
  26. DRIVER_ATTR(_name,_mode,_show,_store)

  27. /*属性文件创建的方法:*/
  28. int driver_create_file(struct device_driver * drv, struct driver_attribute * attr);
  29. void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr);

  30. /*bus_type 结构含有一个成员( drv_attrs ) 指向一组为属于该总线的所有设备创建的默认属性*/
在新版内核中,这个结构体更简洁,隐藏了无需驱动知道的一些成员:
  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.     bool suppress_bind_attrs;    /* disables bind/unbind via sysfs */

  7.     const struct of_device_id    *of_match_table;

  8.     int (*probe) (struct device *dev);
  9.     int (*remove) (struct device *dev);
  10.     void (*shutdown) (struct device *dev);
  11.     int (*suspend) (struct device *dev, pm_message_t state);
  12.     int (*resume) (struct device *dev);
  13.     const struct attribute_group **groups;

  14.     const struct dev_pm_ops *pm;

  15.     struct driver_private *p;
  16. };
  17. struct driver_private {
  18. struct kobject kobj;
  19. struct klist klist_devices;
  20. struct klist_node knode_bus;
  21. struct module_kobject *mkobj;
  22. struct device_driver *driver;
  23. };
  24. #define to_driver(obj) container_of(obj, struct driver_private, kobj)
(2)驱动程序结构的嵌入

对大多数驱动程序核心结构, device_driver 结构通常被嵌入到一个更高层的、总线相关的结构中。

以lddbus 子系统为例,它定义了ldd_driver 结构:

struct ldd_driver {
 char *version;
 struct module *module;
 struct device_driver driver;
 struct driver_attribute version_attr; 
}; 
#define to_ldd_driver(drv) container_of(drv, struct ldd_driver, driver);

lddbus总线中相关的驱动注册和注销函数是:

/*
 * Crude driver interface.
 */

static ssize_t show_version(struct device_driver *driver, char *buf)
{
    struct ldd_driver *ldriver = to_ldd_driver(driver);
    sprintf(buf, "%s\n", ldriver->version);
    return strlen(buf);
}

int register_ldd_driver(struct ldd_driver *driver)
{
 int ret;
 driver->driver.bus = &ldd_bus_type;
 ret = driver_register(&driver->driver);/*注册底层的 device_driver 结构到核心*/
 if (ret)
 return ret;
 driver->version_attr.attr.name = "version";/* driver_attribute 结构必须手工填充*/
 driver->version_attr.attr.owner = driver->module;/*注意:设定 version 属性的拥有者为驱动模块, 不是 lddbus 模块!因为 show_version 函数是使用驱动模块所创建的 ldd_driver 结构,若 ldd_driver 结构在一个用户空间进程试图读取版本号时已经注销,就会出错*/
 driver->version_attr.attr.mode = S_IRUGO;
 driver->version_attr.show = show_version;
 driver->version_attr.store = NULL;
 return driver_create_file(&driver->driver, &driver->version_attr);/*建立版本属性,因为这个属性在运行时被创建,所以不能使用 DRIVER_ATTR 宏*/
}

void unregister_ldd_driver(struct ldd_driver *driver)
{
    driver_unregister(&driver->driver);
}
EXPORT_SYMBOL(register_ldd_driver);
EXPORT_SYMBOL(unregister_ldd_driver);

在sculld 中创建的 ldd_driver 结构如下:

/* Device model stuff */
static struct ldd_driver sculld_driver = {
    .version = "$Revision: 1.21 $",
    .module = THIS_MODULE,
    .driver = {
        .name = "sculld",
    },
};/*只要一个简单的 register_ldd_driver 调用就可添加它到系统中。一旦完成初始化, 驱动信息可在 sysfs 中显示*/



类 子系统

类是一个设备的高层视图, 它抽象出了底层的实现细节,从而允许用户空间使用设备所提供的功能, 而不用关心设备是如何连接和工作的。类成员通常由上层代码所控制, 而无需驱动的明确支持。但有些情况下驱动也需要直接处理类。

几乎所有的类都显示在 /sys/class 目录中。出于历史的原因,有一个例外:块设备显示在 /sys/block目录中。在许多情况, 类子系统是向用户空间导出信息的最好方法。当类子系统创建一个类时, 它将完全拥有这个类,根本不用担心哪个模块拥有那些属性,而且信息的表示也比较友好。

为了管理类,驱动程序核心导出了一些接口,其目的之一是提供包含设备号的属性以便自动创建设备节点,所以udev的使用离不开类。 类函数和结构与设备模型的其他部分遵循相同的模式,所以真正崭新的概念是很少的。

注意:class_simple 是老接口,在2.6.13中已被删除,这里不再研究。

管理类的接口

类由 struct class 的结构体来定义:

/*
 * device classes
 */

struct class {
    const char        * name;/*每个类需要一个唯一的名字, 它将显示在 /sys/class 中*/
    struct module        * owner;

    struct kset        subsys;
    struct list_head    children;
    struct list_head    devices;
    struct list_head    interfaces;
    struct kset        class_dirs;
    struct semaphore    sem;    /* locks both the children and interfaces lists */

    struct class_attribute        * class_attrs;/* 指向类属性的指针(以NULL结尾) */
    struct class_device_attribute    * class_dev_attrs;/* 指向类中每个设备的一组默认属性的指针 */
    struct device_attribute        * dev_attrs;

    int    (*uevent)(struct class_device *dev, char **envp,
             int num_envp, char *buffer, int buffer_size);/* 类热插拔产生时添加环境变量的函数 */
    int    (*dev_uevent)(struct device *dev, char **envp, int num_envp,
                char *buffer, int buffer_size);/* 类中的设备热插拔时添加环境变量的函数 */

    void    (*release)(struct class_device *dev);/* 把设备从类中删除的函数 */
    void    (*class_release)(struct class *class);/* 删除类本身的函数 */
    void    (*dev_release)(struct device *dev);

    int    (*suspend)(struct device *, pm_message_t state);
    int    (*resume)(struct device *);
};


/*类注册函数:*/
int class_register(struct class *cls);
void class_unregister(struct class *cls);

/*类属性的接口:*/
struct class_attribute {
 struct attribute attr;
 ssize_t (*show)(struct class *cls, char *buf);
 ssize_t (*store)(struct class *cls, const char *buf, size_t count); 
}; 
CLASS_ATTR(_name,_mode,_show,_store); 
int class_create_file(struct class *cls, const struct class_attribute *attr);
void class_remove_file(struct class *cls, const struct class_attribute *attr);


在更新的内核里,这个结构体变得简洁了,删除了一些成员:

/*in Linux 2.6.26.5*/

/*
 * device classes
 */

struct class {
    const char        *name;
    struct module        *owner;

    struct kset        subsys;
    struct list_head    devices;
    struct list_head    interfaces;
    struct kset        class_dirs;
    struct semaphore    sem; /* locks children, devices, interfaces */
    struct class_attribute        *class_attrs;
    struct device_attribute        *dev_attrs;

    int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);

    void (*class_release)(struct class *class);
    void (*dev_release)(struct device *dev);

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

类设备(在新内核中已被删除)

类存在的真正目的是给作为类成员的各个设备提供一个容器,成员由 struct class_device 来表示:

struct class_device {
    
struct list_head    node;/*for internal use by the driver core only*/
    
struct kobject        kobj;/*for internal use by the driver core only*/
    
struct class        * class;    /* 指向该设备所属的类,必须*/
    dev_t            devt
;        /* dev_t, creates the sysfs "dev" ,for internal use by the driver core only*/
    
struct class_device_attribute *devt_attr;/*for internal use by the driver core only*/
    
struct class_device_attribute uevent_attr;
    
struct device        * dev;        /* 指向此设备相关的 device 结构体,可选。若不为NULL,应是一个从类入口到/sys/devices 下相应入口的符号连接,以便用户空间查找设备入口*/
    
void            * class_data;    /* 私有数据指针 */
    
struct class_device    *parent;    /* parent of this child device, if there is one */
    
struct attribute_group ** groups;    /* optional groups */

    
void    (*release)(struct class_device *dev);
    
int    (*uevent)(struct class_device *dev, char **envp,
             
int num_envp, char *buffer, int buffer_size);
    
char    class_id[BUS_ID_SIZE];    /* 此类中的唯一的名字 */
};

/*类设备注册函数:*/
int class_device_register(struct class_device *cd);
void class_device_unregister(struct class_device *cd);

/*重命名一个已经注册的类设备入口:*/
int class_device_rename(struct class_device *cd, char *new_name); 

/*类设备入口属性:*/
struct class_device_attribute {
 
struct attribute attr;
 ssize_t 
(*show)(struct class_device *cls, char *buf);
 ssize_t 
(*store)(struct class_device *cls, const char *buf,
 
size_t count);
};

CLASS_DEVICE_ATTR
(_name, _mode, _show, _store); 

/*创建和删除除struct class中设备默认属性外的属性*/
int class_device_create_file(struct class_device *cls, const struct class_device_attribute *attr);
void class_device_remove_file(struct class_device *cls, const struct class_device_attribute *attr);

类接口

类子系统有一个 Linux 设备模型的其他部分找不到的附加概念,称为“接口”, 可将它理解为一种设备加入或离开类时获得信息的触发机制,结构体如下:

struct class_interface {
    struct list_head    node;
    struct class        *class;/* 指向该接口所属的类*/

    int (*add) (struct class_device *, struct class_interface *);

/*当一个类设备被加入到在 class_interface 结构中指定的类时, 将调用接口的 add 函数,进行一些设备需要的额外设置,通常是添加更多属性或其他的一些工作*/
    void (*remove)    (struct class_device *, struct class_interface *);/*一个接口的功能是简单明了的. 当设备从类中删除, 将调用remove 方法来进行必要的清理*/
    int (*add_dev)     (struct device *, struct class_interface *);
    void (*remove_dev) (struct device *, struct class_interface *);
};

/*注册或注销接口的函数:*/
int class_interface_register(struct class_interface *class_intf);
void class_interface_unregister(struct class_interface *class_intf);
/*一个类可注册多个接口*/


测试代码: device_module.rar   
这篇文章参考了,Tekkaman的博客,国嵌的代码,特别感谢国嵌的代码和视频,设备模型这一块直到最近看了国嵌的视频和代码,才真正理解。设备模型从一个更高的层次来看待内核驱动,现在再看以前的很多代码,有了更高层次的,更好的全局理解,此模块以后还要把LDD3多看几遍。
阅读(2307) | 评论(0) | 转发(5) |
给主人留下些什么吧!~~