/* [cgw]: 设置evdev.c支持的事件类型 */
input->evbit[0] = BIT(EV_KEY);
/* [cgw]: 设置输入设备名,同platform_device的名字 */
input->name = pdev->name;
/* [cgw]: */
input->phys = "gpio-keys/input0";
/* [cgw]: 设置输入设备dev的父节点为platform_device->dev */
input->dev.parent = &pdev->dev;
/* [cgw]: 设置输入设备总线类型,供应商,产品,版本 */
input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100;
/* [cgw]: 为pdata->nbuttons个按键申请中断 */
for (i = 0; i < pdata->nbuttons; i++) {
struct gpio_keys_button *button = &pdata->buttons[i];
/* [cgw]: 获得gpio对应的中断号 */
int irq = gpio_to_irq(button->gpio);
/* [cgw]: 获得按键类型 */
unsigned int type = button->type ?: EV_KEY;
/* [cgw]: 设置中断类型为边沿中断 */
set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
/* [cgw]: 申请中断,设置中断服务程序 */
error = request_irq(irq, gpio_keys_isr, IRQF_SAMPLE_RANDOM,
button->desc ? button->desc : "gpio_keys",
pdev);
if (error) {
printk(KERN_ERR "gpio-keys: unable to claim irq %d; error %d\n",
irq, error);
goto fail;
}
/* [cgw]: 设置evdev.c支持的按键码 */
input_set_capability(input, type, button->code);
}
/* [cgw]: 注册一个输入设备 */
error = input_register_device(input);
/* [cgw]: 注册失败 */
if (error) {
printk(KERN_ERR "Unable to register gpio-keys input device\n");
goto fail;
}
return 0;
fail:
/* [cgw]: 释放中断 */
for (i = i - 1; i >= 0; i--)
free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
/* [cgw]: 释放输入设备占用的内存 */
input_free_device(input);
return error;
}
static int __devexit gpio_keys_remove(struct platform_device *pdev)
{
/* [cgw]: 在gpio_keys_probe()中调用了request_irq(..., pdev),
* 因此dev_id指向了platform_device *pdev,通过dev_id间接传递
* platform_device *指针
*/
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
/* [cgw]: probe()中已分配了一个struct input_dev,并通过platform_set_drvdata()
* 设置platform_device->dev->driver_data = input;
*/
struct input_dev *input = platform_get_drvdata(pdev);
int i;
/* [cgw]: 释放中断 */
for (i = 0; i < pdata->nbuttons; i++) {
int irq = gpio_to_irq(pdata->buttons[i].gpio);
free_irq(irq, pdev);
}
/* [cgw]: 注销输入设备 */
input_unregister_device(input);
return 0;
}
struct platform_driver gpio_keys_device_driver = {
.probe = gpio_keys_probe,
.remove = __devexit_p(gpio_keys_remove),
.driver = {
.name = "gpio-keys",
}
};
static int __init gpio_keys_init(void)
{
/* [cgw]: 注册gpio_keys_device_driver平台驱动 */
return platform_driver_register(&gpio_keys_device_driver);
}
static void __exit gpio_keys_exit(void)
{
/* [cgw]: 注销gpio_keys_device_driver平台驱动 */
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");
平台设备(platform_device):
keys_dev.c
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>