抑郁症健康,内容丰富有趣,生活中的好帮手!
抑郁症健康 > linux按键驱动中的结构体 linux 驱动之input子系统(gpio-keys)实现

linux按键驱动中的结构体 linux 驱动之input子系统(gpio-keys)实现

时间:2023-08-04 08:17:44

相关推荐

1.概述

Gpio-keys 是基于input子系统实现的一个通用按键驱动,该驱动也符合linux驱动实现模型,即driver和device分离模型.一般按键驱动,都是基于gpio-keys进行开发的.

2. gpio-keys 代码分析(基于 linux 4.14.40)

(1)整体来说分为以下四步

static int gpio_keys_probe(struct platform_device *pdev)

{

...........

...........

/*第一,从设备树获取button,即gpio相关控制方式和属性*/

pdata = gpio_keys_get_devtree_pdata(dev);

...........

...........

/*第二,分配一个input 设备*/

input = devm_input_allocate_device(dev);

...........

...........

/*第三,注册gpio相关信息和分配资源*/

for (i = 0; i < pdata->nbuttons; i++) {

error = gpio_keys_setup_key(pdev, input, ddata,

button, i, child);

...........

...........

/*第四,注册input 设备到kernel*/

error = input_register_device(input);

...........

...........

return 0;

}

(2)从注册一个input设备来看,二,四步是一个通用的步骤,第三步和第一步是关键,先看第三步.

static int gpio_keys_setup_key(struct platform_device *pdev,

struct input_dev *input,

struct gpio_keys_drvdata *ddata,

const struct gpio_keys_button *button,

int idx,

struct fwnode_handle *child)

{

.............

.............

/*第一,请求gpio并获取irq 资源*/

error = devm_gpio_request_one(dev, button->gpio, flags, desc);

irq = gpiod_to_irq(bdata->gpiod);

.............

.............

/*第二,注册一个delayed work任务,这个任务的目的,当中断发生时,需要向

通过input子系统发消息,这个过程有可能会sleep或者schedule,所以必须通一个

delay work去完成这件事.

*/

INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);

isr = gpio_keys_gpio_isr;

irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;}

.............

.............

/*第三,注册中断*/

error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,

desc, bdata);

return 0;

}

(3)下面来看中断和delay work做了什么事

static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)

{

struct gpio_button_data *bdata = dev_id;

...............

...............

/*中断中,唯一做的事,就是把delay work加入到了system_wq队列中去.还做了一个延迟

这个延迟目的是为了去抖动.*/

mod_delayed_work(system_wq,

&bdata->work,

msecs_to_jiffies(bdata->software_debounce));

return IRQ_HANDLED;

}

static void gpio_keys_gpio_work_func(struct work_struct *work)

{

/*调用上报消息,接着向下看*/

...........

gpio_keys_gpio_report_event(bdata);

...........

}

static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)

{

............

/*第一步,获取gpio 状态.*/

state = gpiod_get_value_cansleep(bdata->gpiod);

............

/*发送消息到input*/

input_event(input, type, *bdata->code, state);

input_sync(input);

............

}

(4)下面请看第一步工作,以及设备树的配置

(红色为必要部分)

label : 一个key 的别名

linux,code : 键值,作为按键的唯一识别号

linux,input-type: input类型(EV_KEY(按键), EV_ABS(相对坐标), EV_REL(绝对坐标)...) 默认为EV_KEY

wakeup-source: 与pm相关,默认为disable

linux,can-disable:是否共享中断line ((默认)0:shared, 1: not shared)

debounce-interval : 去抖延时

gpios : gpio 的相关信息

/*用于gpio资源的申请及初始化,可列举很多*/

gpio_keys_pins_default: gpio_keys_pins_default {

pinctrl-single,pins = <

AM4372_IOPAD(0x9a0, PIN_INPUT | MUX_MODE9) /* (L23) mcasp0_aclkr.gpio0[18] */

>;

};

/*创建gpio-keys 设备描述*/

gpio_keys: gpio_keys {

compatible = "gpio-keys";

pinctrl-names = "default";

pinctrl-0 = ;

#address-cells = <1>;

#size-cells = <0>;

/*添加一个button, 用于做系统reset*/

button0 {

label = "reset";

linux,code = <0x198>; /*KEY_RESTART*/

gpios = ;

};

};

3. 内核模块选择配置

| with configuration data saying which GPIOs are used. |

| |

| To compile this driver as a module, choose M here: the |

| module will be called gpio_keys. |

| |

| Symbol: KEYBOARD_GPIO [=y] |

| Type : tristate |

| Prompt: GPIO Buttons |

| Location: |

| -> Device Drivers |

| -> Input device support |

| -> Generic input layer (needed for keyboard, mouse, ...) (INPUT [=y]) |

| -> Keyboards (INPUT_KEYBOARD [=y]) |

| Defined at drivers/input/keyboard/Kconfig:214 |

| Depends on: !UML && INPUT [=y] && INPUT_KEYBOARD [=y] && (GPIOLIB [=y] || COMPILE_TEST [=n])

4.cat /proc/bus/input/devices

通过这个可以看到接到input事件的handler是哪一个

$: 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=1000000 0 0 0 0 0 0 0 0 0 0 0 0

/下面的信息有助于你编写udev rules

$:udevadm info -a -p /sys//devices/platform/gpio_keys/input/input0

looking at device '/devices/platform/gpio_keys/input/input0':

KERNEL=="input0"

SUBSYSTEM=="input"

DRIVER==""

ATTR{name}=="gpio_keys"

ATTR{phys}=="gpio-keys/input0"

ATTR{properties}=="0"

ATTR{uniq}==""

looking at parent device '/devices/platform/gpio_keys':

KERNELS=="gpio_keys"

SUBSYSTEMS=="platform"

DRIVERS=="gpio-keys"

ATTRS{disabled_keys}==""

ATTRS{disabled_switches}==""

ATTRS{driver_override}=="(null)"

ATTRS{keys}=="408"

ATTRS{switches}==""

looking at parent device '/devices/platform':

KERNELS=="platform"

SUBSYSTEMS==""

DRIVERS==""

4.如何接收按键消息,以下例程供参考

#include #include #include #include #include #include #include #include #include #define KEY_DEVICE_FILE "/dev/input/event0"

struct input_event {

struct timeval time;

unsigned short type;

unsigned short code;

int value;

};

int main()

{

int fd_key = -1;

struct input_event key_evt;

int ret = 0;

fd_key= open(KEY_DEVICE_FILE, O_RDONLY);

if(fd_key< 0) {

return fd_key;

}

monitor_key:

ret = read(fd_key, (char*)&key_evt, sizeof(struct input_event));

if(ret < 0)

{

printf("read key event failed:%d\n", ret);

}else{

if(key_evt.code == 408 && key_evt.value == 1)

{

printf("The press of reset button is detected\n");

return 0;

}

#if 0

printf("%lld seconds %ld microseconds", key_evt.time.tv_sec,

key_evt.time.tv_usec);

printf("type:%d code:%d value:%d\n", key_evt.type, key_evt.code, key_evt.value);

#endif

}

goto monitor_key;

return 0;

}

如果觉得《linux按键驱动中的结构体 linux 驱动之input子系统(gpio-keys)实现》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。