本文转载自:http://blog.csdn.net/angle_birds/article/details/8443695

platform_set_drvdata(struct platform_device *pdev, void *data)

platform_get_drvdata(const struct platform_device *pdev):
驱动中常用到platform_set_drvdata 和 platform_get_drvdata这两个函数,用于保存局部变量:
include/linux/platform_device.h中:
static inline void *platform_get_drvdata(const struct platform_device *pdev)
{
        return dev_get_drvdata(&pdev->dev);
}
static inline void platform_set_drvdata(struct platform_device *pdev, void *data)
{
        dev_set_drvdata(&pdev->dev, data);
}
 
static inline void
dev_set_drvdata (struct device *dev, void *data)
{
    dev->driver_data = data;
}
就是吧data赋值给dev->driver_data,pdev是平台总线设备,对于整个驱动是可见的,所以可以通过platform_get_drvdata来获取data。
 
marvell sd驱动eg:
chip是在probe函数中定义的局部变量,如果想在其他地方使用它怎么办呢? 这就需要把它保存起来。内核提供了这个方法,使用函数platform_set_drvdata()可以将chip保存成平台总线设备的私有
数据。以后再要使用它时只需调用platform_get_drvdata()就可以了。
static int sdhci_mv_probe(struct platform_device *pdev)
{
 struct sdhci_mv_chip *chip;
 struct sdhci_mv_slot *slot;
 chip = kzalloc(sizeof(struct sdhci_mv_chip), GFP_KERNEL);
 if (!chip) {
  ret = -ENOMEM;
  goto err;
 }
 chip->fixes = (sdhci_mv_get_interface(pdev) == INTERFACE_SDIO0)? &sdhci0_fixes : &sdhci1_fixes;
 if (chip->fixes)
     chip->quirks = chip->fixes->quirks;
 platform_set_drvdata(pdev, chip);
}
chip是局部变量,在驱动其他函数使用时,eg:
static int __devexit sdhci_mv_remove(struct platform_device *pdev)
{
 int i;
 struct sdhci_mv_chip *chip;
 chip = platform_get_drvdata(pdev);
 if (chip) {
  for (i = 0;i < chip->num_slots; i++)
   sdhci_mv_remove_slot(chip->slots[i]);
  platform_set_drvdata(pdev, NULL);
  kfree(chip);
 }
 return 0;
}
 
container_of(ptr, type, member)
问题:如何通过结构中的某个变量获取结构本身的指针???
container_of(ptr, type, member)宏的作用是 传入结构体类型type的域member的地址ptr,返回该结构体变量的首地址。
member是结构体类型type的成员,ptr是成员member的实例,返回ptr的入口地址;即通过结构体中一个成员的地址来得到此成员所在结构体的地址。
关于container_of见kernel.h中:
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr:     the pointer to the member.
* @type:     the type of the container struct this is embedded in.
* @member:     the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({             /
         const typeof( ((type *)0)->member ) *__mptr = (ptr);     /
         (type *)( (char *)__mptr - offsetof(type,member) );})
container_of在Linux Kernel中的应用非常广泛,它用于获得某结构中某成员的入口地址.
 
 
/* sprd keypad backlight */
struct sprd_lcd_led {
struct platform_device *pdev;
struct mutex mutex;
struct work_structwork;
spinlock_t value_lock;
enum led_brightness value;
struct led_classdev cdev;
int enabled;
int suspend;
struct early_suspend sprd_early_suspend_desc;
};
static void led_work(structwork_struct*work)
{                                       // 传入的参数是结构体类型sprd_lcd_led 的成员work的实例的地址
structsprd_lcd_led*led =container_of(work, struct sprd_lcd_led, work);
unsigned long flags;
mutex_lock(&led->mutex);
spin_lock_irqsave(&led->value_lock, flags);
if (led->value == LED_OFF || led->suspend) {
spin_unlock_irqrestore(&led->value_lock, flags);
sprd_led_disable(led);
goto out;
}
spin_unlock_irqrestore(&led->value_lock, flags);
sprd_led_enable(led);
out:
mutex_unlock(&led->mutex);
}
static void sprd_lcd_led_shutdown(structplatform_device*pdev) 
{                                          //传入的参数为platform_device 类型,所以使用platform_get_drvdata()来获取platform_device->device->driver_data

struct sprd_lcd_led*led =platform_get_drvdata(pdev);
mutex_lock(&led->mutex);
led->value = LED_OFF;
led->enabled = 1;
sprd_led_disable(led);
mutex_unlock(&led->mutex);
}

static int sprd_lcd_led_probe(structplatform_device*pdev)
{
struct sprd_lcd_led*led;//局部变量
int ret;
...............................................
led = kzalloc(sizeof(*led), GFP_KERNEL);
platform_set_drvdata(pdev, led);//局部变量保存到pdev->dev->driver_data      (platform_device->device->driver_data)
...............................................
}

platform_set_drvdata()/platform_get_drvdata()/container_of()【转】的更多相关文章

  1. platform_set_drvdata 和 platform_get_drvdata

    ndev是我们在probe函数中定义的局部变量,如果我想在其他地方使用它怎么办呢? 这就需要把它保存起来.内核提供了这个方法,使用函数platform_set_drvdata()可以将ndev保存成平 ...

  2. platform_set_drvdata和platform_get_drvdata用法【转】

    本文转载自:http://www.cnblogs.com/wangxianzhen/archive/2013/04/09/3009530.html 在用到Linux设备驱动的platform框架时,常 ...

  3. linux中offsetof与container_of宏定义

    linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->M ...

  4. 刨一刨内核container_of()的设计精髓

    新年第一帖,总得拿出点干货才行,虽然这篇水分还是有点大,大家可以晒干了温水冲服.这段时间一直在整理内核学习的基础知识点,期间又碰到了container_of()这个宏,当然还包括一个叫做offseto ...

  5. (转)offsetof与container_of宏[总结]

    1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...

  6. linux内核宏container_of

    首先来个简单版本 /* given a pointer @ptr to the field @member embedded into type (usually * struct) @type, r ...

  7. linux tricks 之 container_of.

    转载:http://blog.chinaunix.net/uid-20608849-id-3027972.html 由于内核中定义了很多复杂的数据结构,而它们的实例中的成员在作为函数参数传递的时,函数 ...

  8. (十)Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  9. typeof、offsetof、container_of的解释

    链表是内核最经典的数据结构之一,说到链表就不得不提及内核最经典(没有之一)的宏container_of. container_of似乎就是为链表而生的,它的主要作用是根据一个结构体变量中的一个域成员变 ...

随机推荐

  1. [转载][FPGA]Quartus代码保护-生成网表文件

    0. 简介 当项目过程中,不想给甲方源码时,该如何?我们可以用网表文件qxp或者vqm对资源进行保护. 下面讲解这两个文件的具体生成步骤: 1. 基本概念 QuartusII的qxp文件为Quartu ...

  2. SQLite数据库中rowid使用

    SQLite数据库中rowid使用   SQLite中每个表都默认包含一个隐藏列rowid,使用WITHOUT ROWID定义的表除外.通常情况下,rowid可以唯一的标记表中的每个记录.表中插入的第 ...

  3. Ubuntu 16.04下使用Wine安装PowerDesigner15

    说明: 1.关于没有.wine文件夹的解决方法:在命令行上运行winecfg: 2.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 下载: (链接 ...

  4. Linux内核源码分析--内核启动之zImage自解压过程

    参考: http://blog.chinaunix.net/uid-20543672-id-3018233.html Linux内核编译流程分析 linux2.6内核启动分析--李枝果(不看是你的损失 ...

  5. MySQL 查询某个列中同样值的数量统计

    数据如今是这种,我想确定出type列中的news和image....甚至以后有其它值,他们分别有多少个. SELECT type, count(1) AS counts FROM material G ...

  6. Java中HashMap遍历的两种方法(转)

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...

  7. Resharper 8.2的“安装”问题

    概述 完美解决Resharper 8.2的“安装”问题和VS2012写Javascript语句无法智能提示的问题: 目录 引言——Resharper 简介——安装——VS2012智能提示测试 引言 最 ...

  8. Solaris 系统启动与关闭

    忘掉root密码 更改内核参数后,重启进不了系统 复制---进入单用户模式----恢复文件 系统突然死机,如何尽量减少数据丢失 Sync 同步命令.将内存内容输入到硬盘,相当于保存文档.   Unix ...

  9. Google架构学习

    http://hideto.iteye.com/blog/130815 原文:Google Architecture Google是伸缩性的王者.Google一直的目标就是构建高性能高伸缩性的基础组织 ...

  10. C#给指定doc文件写入宏

    private void InsertMacro() { Word.Application oWord; Word.Document oDoc; VBIDE.VBComponent oModule; ...