平台信息:
内核:linux3.10
系统:android6.0
平台:RK3288
前言:本文主要实现的功能是在android系统中添加一个按键,在驱动层使用定时器,每隔1秒钟向上层发送按键实现,framework层继续上报按键事件。
驱动的源码:
key.c
1 /*1. 头文件 */ 2 #include <linux/module.h> 3 #include <linux/kernel.h> 4 #include <linux/init.h> 5 #include <linux/platform_device.h> 6 #include <linux/fb.h> 7 #include <linux/backlight.h> 8 #include <linux/err.h> 9 #include <linux/pwm.h> 10 #include <linux/slab.h> 11 #include <linux/miscdevice.h> 12 #include <linux/delay.h> 13 #include <linux/gpio.h> 14 #include <linux/timer.h> /*timer*/ 15 #include <asm/uaccess.h> /*jiffies*/ 16 #include <linux/delay.h> 17 #include <linux/interrupt.h> 18 #include <linux/workqueue.h> 19 #include <linux/input.h> 20 21 struct input_dev *input = NULL; 22 23 static struct timer_list timer1; 24 25 void timer1_function(unsigned long arg) 26 { 27 printk("timer1_function "); 28 29 mod_timer(&timer1,jiffies+HZ); 30 input_report_key(input, KEY_ZZB, 1); 31 input_sync(input); 32 input_report_key(input, KEY_ZZB, 0); 33 } 34 35 36 37 static int key_event_probe(struct platform_device *pdev) 38 { 39 int ret = -1; 40 struct device *dev = &pdev->dev; 41 char *str_temp; 42 43 printk("----------%s--------- ",__func__); 44 45 input = devm_input_allocate_device(dev); 46 if (!input){ 47 ret = -ENOMEM; 48 return ret; 49 } 50 51 input->name = "zzb-key"; /* pdev->name; */ 52 input->phys = "zzb-key"; 53 input->dev.parent = dev; 54 55 input->id.bustype = BUS_HOST; 56 input->id.vendor = 0x0001; 57 input->id.product = 0x0001; 58 input->id.version = 0x0100; 59 60 _set_bit(EV_KEY,input->keybit); 61 input_set_capability(input,EV_KEY,KEY_ZZB); 62 63 ret = input_register_device(input); 64 if (ret){ 65 printk("Error input_register_device "); 66 ret = -ENOMEM; 67 goto err; 68 } 69 70 init_timer(&timer1); 71 timer1.function = timer1_function; 72 add_timer(&timer1); 73 mod_timer(&timer1,jiffies+HZ); 74 75 return 0; 76 77 err: 78 return ret; 79 80 } 81 static int key_event_remove(struct platform_device *pdev) 82 { 83 printk("----------%s--------- ",__func__); 84 input_unregister_device(input); 85 del_timer(&timer1); 86 } 87 88 89 static const struct of_device_id rk_printer_event_match[] = { 90 { .compatible = "zzb,key", .data = NULL}, 91 {}, 92 }; 93 94 95 static struct platform_driver key_device_driver = { 96 .probe = key_event_probe, 97 .remove = key_event_remove, 98 .driver = { 99 .name = "zzb,key", 100 .owner = THIS_MODULE, 101 .of_match_table = rk_printer_event_match, 102 } 103 }; 104 105 106 module_platform_driver(key_device_driver); 107 108 /*5.模块的许可声明*/ 109 MODULE_LICENSE("GPL");
Makefile
1 ifeq ($(KERNELRELEASE),) 2 KERNELDIR ?=/home/zhuangzb/project/E800/rk3288-e810-android6.0/kernel #内核路径 3 PWD :=$(shell pwd) #当前路径 4 5 modules: 6 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 7 #cp hello.ko /opt/fs210/filesystem/drivers/ -f 8 modules_install: 9 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install 10 clean: 11 rm -rf *.o *~core *.ko *.mod.c .tmp_versions Module.symvers modules.order 12 else 13 obj-m :=key.o #生成hello.ko 14 endif
编译生成key.ko文件
装载驱动key.ko
驱动已经成功将键值上报上来了。
将键值在framework层上报。代码的改动如下:
1 diff --git a/device/rockchip/common/device.mk b/device/rockchip/common/device.mk 2 index d80f644..dc5c454 100755 3 --- a/device/rockchip/common/device.mk 4 +++ b/device/rockchip/common/device.mk 5 @@ -88,6 +88,7 @@ PRODUCT_COPY_FILES += 6 device/rockchip/common/media_profiles_default.xml:system/etc/media_profiles_default.xml 7 device/rockchip/common/rk29-keypad.kl:system/usr/keylayout/rk29-keypad.kl 8 device/rockchip/common/camera-input.kl:system/usr/keylayout/camera-input.kl 9 + device/rockchip/common/zzbkey-input.kl:system/usr/keylayout/zzbkey-input.kl 10 device/rockchip/common/printer-event.kl:system/usr/keylayout/printer-event.kl 11 device/rockchip/common/20050030_pwm.kl:system/usr/keylayout/20050030_pwm.kl 12 device/rockchip/common/ff680000_pwm.kl:system/usr/keylayout/ff680000_pwm.kl 13 diff --git a/frameworks/base/api/current.txt b/frameworks/base/api/current.txt 14 index 20e3b3e..313e909 100755 15 --- a/frameworks/base/api/current.txt 16 +++ b/frameworks/base/api/current.txt 17 @@ -35217,6 +35217,7 @@ package android.view { 18 field public static final int KEYCODE_CALL = 5; // 0x5 19 field public static final int KEYCODE_CAMERA = 27; // 0x1b 20 field public static final int KEYCODE_CAMERA_ESD = 295; // 0x127 21 + field public static final int KEYCODE_ZZB_KEY = 512; // 0x127 22 field public static final int KEYCODE_CAPS_LOCK = 115; // 0x73 23 field public static final int KEYCODE_CAPTIONS = 175; // 0xaf 24 field public static final int KEYCODE_CHANNEL_DOWN = 167; // 0xa7 25 diff --git a/frameworks/base/api/system-current.txt b/frameworks/base/api/system-current.txt 26 index 6d939df..8b3a438 100755 27 --- a/frameworks/base/api/system-current.txt 28 +++ b/frameworks/base/api/system-current.txt 29 @@ -37509,6 +37509,7 @@ package android.view { 30 field public static final int KEYCODE_CALL = 5; // 0x5 31 field public static final int KEYCODE_CAMERA = 27; // 0x1b 32 field public static final int KEYCODE_CAMERA_ESD = 295; // 0x127 33 + field public static final int KEYCODE_ZZB_KEY = 512; // 0x127 34 field public static final int KEYCODE_CAPS_LOCK = 115; // 0x73 35 field public static final int KEYCODE_CAPTIONS = 175; // 0xaf 36 field public static final int KEYCODE_CHANNEL_DOWN = 167; // 0xa7 37 diff --git a/frameworks/base/core/java/android/view/KeyEvent.java b/frameworks/base/core/java/android/view/KeyEvent.java 38 index 94f1e17..9689448 100755 39 --- a/frameworks/base/core/java/android/view/KeyEvent.java 40 +++ b/frameworks/base/core/java/android/view/KeyEvent.java 41 @@ -798,6 +798,7 @@ public class KeyEvent extends InputEvent implements Parcelable { 42 //$_rbox_$_modify_$ end 43 44 public static final int KEYCODE_CAMERA_ESD = 295; 45 + public static final int KEYCODE_ZZB_KEY = 512; 46 //private static final int LAST_KEYCODE = KEYCODE_CAMERA_ESD; 47 public static final int KEYCODE_PRN_COVEROPEN = 497; 48 public static final int KEYCODE_PRN_COVERCLOSE = 498; 49 @@ -1853,6 +1854,7 @@ public class KeyEvent extends InputEvent implements Parcelable { 50 case KeyEvent.KEYCODE_BRIGHTNESS_UP: 51 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: 52 case KeyEvent.KEYCODE_CAMERA_ESD: 53 + case KeyEvent.KEYCODE_ZZB_KEY: 54 case KeyEvent.KEYCODE_PRN_COVEROPEN: 55 case KeyEvent.KEYCODE_PRN_COVERCLOSE: 56 case KeyEvent.KEYCODE_PRN_TEMPOUT: 57 diff --git a/frameworks/base/core/res/res/values/attrs.xml b/frameworks/base/core/res/res/values/attrs.xml 58 index 0a685e8..4709151 100755 59 --- a/frameworks/base/core/res/res/values/attrs.xml 60 +++ b/frameworks/base/core/res/res/values/attrs.xml 61 @@ -1829,6 +1829,7 @@ i 62 <enum name="KEYCODE_TV_MEDIA_PAUSE" value="294" /> 63 <!--$_rbox_$_modify_$_end--> 64 <enum name="KEYCODE_CAMERA_ESD" value="295" /> 65 + <enum name="KEYCODE_ZZB_KEY" value="512" /> 66 <enum name="KEYCODE_PRN_COVEROPEN" value="497" /> 67 <enum name="KEYCODE_PRN_COVERCLOSE" value="498" /> 68 <enum name="KEYCODE_PRN_TEMPOUT" value="499" /> 69 diff --git a/frameworks/native/include/android/keycodes.h b/frameworks/native/include/android/keycodes.h 70 index 262de9f..5b5a3b5 100755 71 --- a/frameworks/native/include/android/keycodes.h 72 +++ b/frameworks/native/include/android/keycodes.h 73 @@ -730,6 +730,7 @@ enum { 74 AKEYCODE_TV_KEYMOUSE_DOWN = 283, 75 AKEYCODE_TV_KEYMOUSE_MODE_SWITCH = 284, 76 AKEYCODE_CAMERA_ESD = 295, 77 + AKEYCODE_ZZB_KEY = 512, 78 AKEYCODE_PRN_COVEROPEN = 497, 79 AKEYCODE_PRN_COVERCLOSE = 498, 80 AKEYCODE_PRN_TEMPOUT = 499, 81 diff --git a/frameworks/native/include/input/InputEventLabels.h b/frameworks/native/include/input/InputEventLabels.h 82 index 8a47e31..0ce216b 100755 83 --- a/frameworks/native/include/input/InputEventLabels.h 84 +++ b/frameworks/native/include/input/InputEventLabels.h 85 @@ -304,6 +304,7 @@ static const InputEventLabel KEYCODES[] = { 86 DEFINE_KEYCODE(TV_KEYMOUSE_DOWN), 87 DEFINE_KEYCODE(TV_KEYMOUSE_MODE_SWITCH), 88 DEFINE_KEYCODE(CAMERA_ESD), 89 + DEFINE_KEYCODE(ZZB_KEY), 90 DEFINE_KEYCODE(HELP), 91 DEFINE_KEYCODE(NAVIGATE_PREVIOUS), 92 DEFINE_KEYCODE(NAVIGATE_NEXT), 93 zhuangzb@UYFSanbu02:~/project/E800/rk3288-e810-android6.0$