GPIO-KEY
Openwrt 使用的按键驱动为 gpio-button-hotplug,源码为:package/kernel/gpio-button-hotplug/src/gpio-button-hotplug.c
menuconfig 配置如下:
Kernel modules --->
Other modules --->
<*> kmod-gpio-button-hotplug
在 target/linux/ramips/dts/FIREWRT.dts 配置了按键
这里配置了一个按键其名称为 WPS,POWER,对应的 GPIO 为 gpio18、23,键值为 0x198、116(对应内核头文件 include/uapi/linux/input.h
中的 KEY_WPS,KEY_POWER)。
在开发板上执行如下操作,监测该按键的操作结果
hi 表示高电平, lo 表示低电平
[GPIO] FireWRT-GPIO电源按键----2
在按键驱动 gpio-button-hotplug.c 中对按键的键值和名称进行了映射,如下所示:
比如 电源键 按键,键值为 116,对应的名称为“power”,当按下或释放该按键时,驱动会向用户层发送事件,最终执行
/etc/rc.button/目录下所对应的脚本 power,其内容如下:
- #!/bin/sh
- mnt="/mnt"
- kill_all() {
- PIDS=`fuser -m $1`
- if [ ! -z "$PIDS" ];then
- for line in $PIDS
- do
- if [ "$line" -eq "1" ]; then
- break
- fi
- kill -9 $line
- done
- fi
-
- }
- foreachd(){
- for file in $mnt/*
- do
- if [ -d $file ]
- then
- kill_all $file 1>/dev/null 2>&1
- umount_device_ret=$(umount $file 2>&1)
- umount_device_ret_erro=`echo $umount_device_ret | grep "can't"`
- fi
-
- if [ -z "$umount_device_ret_erro" ];then
- rm -r $file 1>/dev/null 2>&1
- echo "Success"
- else
- echo "Error umout"
- fi
- done
- }
- countfile=0
-
- for file in $mnt/*
- do
- if [ -d $file ]
- then
- countfile=$countfile+1
- break;
- fi
- done
-
- if [ $countfile -eq 0 ]#如果没有设备挂在就直接关机
- then
- echo 0 > /sys/class/gpio/gpio24/value
- else#卸载设备再休眠1s关机
- foreachd $mnt
- sleep 1
- echo 0 > /sys/class/gpio/gpio24/value
- fi
复制代码
在此前必须先执行
- GPIO24=24
- echo $GPIO24 > /sys/class/gpio/export
- echo out > /sys/class/gpio/gpio${GPIO24}/direction
- echo 1 > /sys/class/gpio/gpio${GPIO24}/value
复制代码
原因时GPIO 24时控制电源
所以我们让上面的这个脚本自动启动
- #!/bin/sh /etc/rc.common
- # Copyright (C) 2006 OpenWrt.org
-
- START=65
- start() {
- /sbin/mkpowerio.sh
- }
复制代码