/****************************************************************************
* I.MX6 gpio-keys driver hacking
* 说明:
* 1. 本文解读gpio-keys驱动是如何注册,最终处理函数在哪里。
* 2. 从最后生成的设备节点来看,我们直接可以通过操作该设备节点来来让系统
* 进行相关操作,譬如关机、挂起等操作。
*
* 2016-3-17 深圳 南山平山村 曾剑锋
***************************************************************************/ static struct platform_driver gpio_keys_device_driver = { <----+
.probe = gpio_keys_probe, ---------*-------+
.remove = __devexit_p(gpio_keys_remove), | |
.driver = { | |
.name = "gpio-keys", | |
.owner = THIS_MODULE, | |
#ifdef CONFIG_PM | |
.pm = &gpio_keys_pm_ops, | |
#endif | |
} | |
}; | |
| |
static int __init gpio_keys_init(void) <------------+ | |
{ | | |
return platform_driver_register(&gpio_keys_device_driver); | --+ |
} | |
| |
static void __exit gpio_keys_exit(void) | |
{ | |
platform_driver_unregister(&gpio_keys_device_driver); | |
} | |
| |
module_init(gpio_keys_init); -------------+ |
module_exit(gpio_keys_exit); |
|
MODULE_LICENSE("GPL"); |
MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>"); |
MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs"); |
MODULE_ALIAS("platform:gpio-keys"); |
|
static int __devinit gpio_keys_probe(struct platform_device *pdev) <-----+
{
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
struct gpio_keys_drvdata *ddata;
struct device *dev = &pdev->dev;
struct input_dev *input;
int i, error;
int wakeup = ; ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
pdata->nbuttons * sizeof(struct gpio_button_data),
GFP_KERNEL);
input = input_allocate_device();
if (!ddata || !input) {
dev_err(dev, "failed to allocate state\n");
error = -ENOMEM;
goto fail1;
} ddata->input = input;
ddata->n_buttons = pdata->nbuttons;
ddata->enable = pdata->enable;
ddata->disable = pdata->disable;
mutex_init(&ddata->disable_lock); platform_set_drvdata(pdev, ddata);
input_set_drvdata(input, ddata); input->name = pdata->name ? : pdev->name;
input->phys = "gpio-keys/input0";
input->dev.parent = &pdev->dev;
input->open = gpio_keys_open;
input->close = gpio_keys_close; input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100; /* Enable auto repeat feature of Linux input subsystem */
if (pdata->rep)
__set_bit(EV_REP, input->evbit); for (i = ; i < pdata->nbuttons; i++) {
struct gpio_keys_button *button = &pdata->buttons[i];
struct gpio_button_data *bdata = &ddata->data[i];
unsigned int type = button->type ?: EV_KEY; bdata->input = input;
bdata->button = button; error = gpio_keys_setup_key(pdev, bdata, button); -------+
if (error) |
goto fail2; |
|
if (button->wakeup) |
wakeup = ; |
|
input_set_capability(input, type, button->code); |
} |
|
error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group); |
if (error) { |
dev_err(dev, "Unable to export keys/switches, error: %d\n", |
error); |
goto fail2; |
} |
|
error = input_register_device(input); |
if (error) { |
dev_err(dev, "Unable to register input device, error: %d\n", |
error); |
goto fail3; |
} |
|
/* get current state of buttons */ |
for (i = ; i < pdata->nbuttons; i++) |
gpio_keys_report_event(&ddata->data[i]); |
input_sync(input); |
|
device_init_wakeup(&pdev->dev, wakeup); |
|
return ; |
|
fail3: |
sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group); |
fail2: |
while (--i >= ) { |
free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]); |
if (ddata->data[i].timer_debounce) |
del_timer_sync(&ddata->data[i].timer); |
cancel_work_sync(&ddata->data[i].work); |
gpio_free(pdata->buttons[i].gpio); |
} |
|
platform_set_drvdata(pdev, NULL); |
fail1: |
input_free_device(input); |
kfree(ddata); |
+---------------------------------------+
return error; |
} |
V
static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
struct gpio_button_data *bdata,
struct gpio_keys_button *button)
{
const char *desc = button->desc ? button->desc : "gpio_keys";
struct device *dev = &pdev->dev;
unsigned long irqflags;
int irq, error; setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
INIT_WORK(&bdata->work, gpio_keys_work_func); --------------------+
|
error = gpio_request(button->gpio, desc); |
if (error < ) { |
dev_err(dev, "failed to request GPIO %d, error %d\n", |
button->gpio, error); |
goto fail2; |
} |
|
error = gpio_direction_input(button->gpio); |
if (error < ) { |
dev_err(dev, "failed to configure" |
" direction for GPIO %d, error %d\n", |
button->gpio, error); |
goto fail3; |
} |
|
if (button->debounce_interval) { |
error = gpio_set_debounce(button->gpio, |
button->debounce_interval * ); |
/* use timer if gpiolib doesn't provide debounce */ |
if (error < ) |
bdata->timer_debounce = button->debounce_interval; |
} |
|
irq = gpio_to_irq(button->gpio); |
if (irq < ) { |
error = irq; |
dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n", |
button->gpio, error); |
goto fail3; |
} |
|
irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING; |
/* |
* If platform has specified that the button can be disabled, |
* we don't want it to share the interrupt line. |
*/ |
if (!button->can_disable) |
irqflags |= IRQF_SHARED; |
/* |
* Resume power key early during syscore instead of at device |
* resume time. |
* Some platform like Android need to konw the power key is pressed |
* then to reume the other devcies |
*/ |
if (button->wakeup) |
irqflags |= IRQF_NO_SUSPEND | IRQF_EARLY_RESUME; |
|
error = request_any_context_irq(irq, gpio_keys_isr, irqflags, desc, bdata); |
if (error < ) { |
dev_err(dev, "Unable to claim irq %d; error %d\n", |
irq, error); |
goto fail3; |
} |
|
return ; |
|
fail3: |
gpio_free(button->gpio); |
fail2: |
return error; |
} |
|
static void gpio_keys_work_func(struct work_struct *work) <-------------+
{
struct gpio_button_data *bdata =
container_of(work, struct gpio_button_data, work); gpio_keys_report_event(bdata); -----------+
} |
|
static void gpio_keys_report_event(struct gpio_button_data *bdata) <---------+
{
struct gpio_keys_button *button = bdata->button;
struct input_dev *input = bdata->input;
unsigned int type = button->type ?: EV_KEY;
int state = (gpio_get_value_cansleep(button->gpio) ? : ) ^ button->active_low;
printk("zengjf check gpio-keys positon: %s in line %d.\n", __func__, _LINE__); if (type == EV_ABS) {
if (state)
input_event(input, type, button->code, button->value);
} else {
input_event(input, type, button->code, !!state);
}
input_sync(input);
} /**
* root@android:/ # cat /proc/bus/input/devices
* I: Bus=0019 Vendor=0001 Product=0001 Version=0100
* N: Name="gpio-keys"
* P: Phys=gpio-keys/input0
* S: Sysfs=/devices/platform/gpio-keys/input/input0
* U: Uniq=
* H: Handlers=event0
* B: PROP=0
* B: EV=3
* B: KEY=100000 0 0 0
* ......
*/

I.MX6 gpio-keys driver hacking的更多相关文章

  1. I.MX6 PWM buzzer driver hacking with Demo test

    /***************************************************************************** * I.MX6 PWM buzzer dr ...

  2. I.MX6 ar1020 SPI device driver hacking

    /************************************************************************************ * I.MX6 ar1020 ...

  3. I.MX6 AD7606-4 device driver registe hacking

    /********************************************************************** * I.MX6 AD7606-4 device driv ...

  4. I.MX6 bq27441 driver hacking

    /************************************************************************* * I.MX6 bq27441 driver ha ...

  5. I.MX6 Linux I2C device& driver hacking

    /******************************************************************************************* * I.MX6 ...

  6. OK335xS LAN8710 phy driver hacking

    /******************************************************************** * OK335xS LAN8710 phy driver h ...

  7. I.MX6 Ar8031 device register hacking

    /***************************************************************************** * I.MX6 Ar8031 device ...

  8. OK335xS knob driver hacking

    /************************************************************************* * OK335xS knob driver hac ...

  9. I.MX6 Power off register hacking

    /*********************************************************************** * I.MX6 Power off register ...

随机推荐

  1. Catalyst揭秘 Day6 Physical plan解析

    Catalyst揭秘 Day6 Physical plan解析 物理计划是Spark和Sparksql相对比而言的,因为SparkSql是在Spark core上的一个抽象,物理化就是变成RDD,是S ...

  2. linux设备驱动模型(kobject与kset)

    Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...

  3. 【Python笔记】异常处理

    1 什么是异常 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行.一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误. 当Pytho ...

  4. linux C语言getopt()函数的使用

    getopt被用来解析命令行选项参数. #include <unistd.h> 函数及参数介绍 extern char *optarg; //选项的参数指针,如果选项字符串里的字母后接着冒 ...

  5. 2014年辛星完全解读Javascript第三节

    经过第一节的入门和第二节的运算符,那么接下来我们就可以学习Javascript的函数了,当然了,不管大家之前学习的是什么编程语言,都会有函数的概念,如果大家学的是Pascal,还会有”过程“的概念,但 ...

  6. Oracle监听器—动态注册

    注册就是将数据库作为一个服务注册到监听程序.客户端不需要知道数据库名和实例名,只需要知道该数据库对外提供的服务名就可以申请连接到数据库.这个服务名可能与实例名一样,也有可能不一样. 注册分: 1. 静 ...

  7. oracle新建用户

    说明:以下命令在PLSQL中运行 一.以管理员身份登录PLSQL scott/root as sysdba 二.创建新用户 create user extjsTest1 identified by r ...

  8. COUNT(*)与COUNT(列名)的区别(转)

    COUNT(*)与COUNT(列名)的区别       以前一直没有留意到COUNT(*)与COUNT(列名)的区别,昨天晚上无意中看到数据库系统工程师教程里面的一句话."如果null参与聚 ...

  9. 现代浏览器原生js获取id号方法

    <div id="tests" class="a b c" style="color:#f00">123</div> ...

  10. firefly笔记一之http模块

    原地址:http://www.9miao.com/question-15-54380.html Firefly是免费开源的游戏服务器端框架,开发语言是python,基于twisted框架开发,作为一名 ...