platform_set_drvdata()/platform_get_drvdata()/container_of()【转】
本文转载自:http://blog.csdn.net/angle_birds/article/details/8443695
platform_set_drvdata(struct platform_device *pdev, void *data)
{
return dev_get_drvdata(&pdev->dev);
}
{
dev_set_drvdata(&pdev->dev, data);
}
dev_set_drvdata (struct device *dev, void *data)
{
dev->driver_data = data;
}
static int sdhci_mv_probe(struct platform_device *pdev)
{
struct sdhci_mv_chip *chip;
struct sdhci_mv_slot *slot;
if (!chip) {
ret = -ENOMEM;
goto err;
}
if (chip->fixes)
chip->quirks = chip->fixes->quirks;
}
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)宏的作用是 传入结构体类型type的域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中的应用非常广泛,它用于获得某结构中某成员的入口地址.
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;
};
{ // 传入的参数是结构体类型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);
}
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);
}
{
struct sprd_lcd_led*led;//局部变量
int ret;
led = kzalloc(sizeof(*led), GFP_KERNEL);
platform_set_drvdata()/platform_get_drvdata()/container_of()【转】的更多相关文章
- platform_set_drvdata 和 platform_get_drvdata
ndev是我们在probe函数中定义的局部变量,如果我想在其他地方使用它怎么办呢? 这就需要把它保存起来.内核提供了这个方法,使用函数platform_set_drvdata()可以将ndev保存成平 ...
- platform_set_drvdata和platform_get_drvdata用法【转】
本文转载自:http://www.cnblogs.com/wangxianzhen/archive/2013/04/09/3009530.html 在用到Linux设备驱动的platform框架时,常 ...
- linux中offsetof与container_of宏定义
linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->M ...
- 刨一刨内核container_of()的设计精髓
新年第一帖,总得拿出点干货才行,虽然这篇水分还是有点大,大家可以晒干了温水冲服.这段时间一直在整理内核学习的基础知识点,期间又碰到了container_of()这个宏,当然还包括一个叫做offseto ...
- (转)offsetof与container_of宏[总结]
1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...
- linux内核宏container_of
首先来个简单版本 /* given a pointer @ptr to the field @member embedded into type (usually * struct) @type, r ...
- linux tricks 之 container_of.
转载:http://blog.chinaunix.net/uid-20608849-id-3027972.html 由于内核中定义了很多复杂的数据结构,而它们的实例中的成员在作为函数参数传递的时,函数 ...
- (十)Linux内核中的常用宏container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...
- typeof、offsetof、container_of的解释
链表是内核最经典的数据结构之一,说到链表就不得不提及内核最经典(没有之一)的宏container_of. container_of似乎就是为链表而生的,它的主要作用是根据一个结构体变量中的一个域成员变 ...
随机推荐
- ubuntu允许mysql远程连接
ubuntu允许mysql远程连接 第一步: vim /etc/MySQL/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0 ...
- android 获取GPS定位
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- Hierarchical data in postgres
https://coderwall.com/p/whf3-a/hierarchical-data-in-postgres --------------------------------------- ...
- 1、CRM2011编程实战——清空指定页签以下的全部选项,并对页签以下的指定控件进行操作
需求:当页面载入时,"呼叫编号"保持不变,"任务号"自己主动更新."接报时间"和"发生日期"自己主动设置为当天日期和时间 ...
- 不错.net图片水印类
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Draw ...
- 笔记04 WPF对象引用
转自:http://www.fx114.net/qa-261-90254.aspx 我们应该都知道,XAML是一种声明式语言,XAML的标签声明的就是对象.一个XAML标签会对应着一个对象,这个对象一 ...
- 《好好说话》zz
最近,<奇葩说>闹出来了一些不愉快. 在半决赛中,姜思达惜败,愤怒的粉丝把矛头指向那场比赛的其他人.最终,马薇薇.黄执中和网友们吵起来了. 这件事本不算大事,毕竟娱乐业就是这个样子.刚刚好 ...
- 【ZZ】Visual C++ 6.0 精简安装版(支持VA、ICC 等等安装)
(2012-04-22 08:10:10) 标签: it 分类: 软件_Software Visual C++ 6.0 精简安装版(支持VA.ICC 等等安装) 2012-04-16 21:07 想找 ...
- java通过http方式下载文件
package com.qiyi; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...
- speechSynthesis,TTS语音合成。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...