在上章分析了紅外platform_driver后,已經修改bug后,接下來我們自己創建一個紅外platform_device平台設備,其實寫一個平台設備很簡單.
創建紅外platform_device平台設備步驟為:
- 1) 創建一個platform_device設備,其中.name= "gpio-rc-recv",並注冊設備
- 2) 在drivers\media\rc\keymaps\里創建一個名字為rc-my-text.c鍵值映射文件
1.首先在include/media/rc-map.h添加rc-my-text.c鍵值映射文件的名字

2.由於我們板子上的紅外接收編碼是NEC格式,並且是GPIO類型,所以配置Make menuconfig:
->Device Drivers -> Multimedia support (MEDIA_SUPPORT [=y]) -> Remote controller decoders (RC_DECODERS [=y]) [*] Enable IR raw decoder for the NEC protocol //選擇NEC協議, ,使CONFIG_IR_NEC_DECODER=y ->Device Drivers -> Multimedia support (MEDIA_SUPPORT [=y]) -> Remote Controller devices (RC_DEVICES [=y]) [*] GPIO IR remote control //選擇GPIO接收類型,使CONFIG_IR_GPIO_CIR=y
3.寫ir_recv_test.c文件,來注冊platform_device
#include <linux/platform_device.h> #include <media/gpio-ir-recv.h> #include <media/rc-map.h> #include <linux/gpio.h> #include <linux/of_gpio.h>
static struct gpio_ir_recv_platform_data ir_recv_data = { .gpio_nr = GPIO_PD(5), .active_low = 1, .map_name = RC_MAP_MY_TEXT, //.map_name ="rc-my-text",用來匹配鍵映射表 .allowed_protos = 0, //允許支持所有編碼協議 }; static struct platform_device ir_recv_device = { .name = "gpio-rc-recv", .id = -1, .num_resources = 0, .dev = { .platform_data = &ir_recv_data, }, }; static int __init ir_recv_test_init(void) { platform_device_register(&ir_recv_device); return 0; } arch_initcall(ir_recv_test_init);
4.然后將ir_recv_test.c文件添加到Makefile中
obj-y += ir_recv_test.o
編譯內核后,便實現一個紅外驅動設備.
由於我們不知道遙控器具體鍵值對應的編碼,所以先測試,獲取編碼值后,再創建鍵值映射文件
5.編譯測試
如下圖所示,我們以上下左右確定5個按鍵為例:

注意:上圖顯示的僅僅是打印信息,並沒有上傳input按鍵值,所以需要創建鍵值映射文件
6.創建drivers\media\rc\keymaps\rc-my-text.c鍵值映射文件
一般上下左右按鍵都要實現重復功能(比如:按下一直調音量)
而確定按鍵一般不實現重復功能.
所以代碼如下:
#include <media/rc-map.h> #include <linux/module.h> static struct rc_map_table latte_key[] = { //所有支持的映射表 { 0x48ac40bf, KEY_UP}, { 0x48ac609f, KEY_DOWN}, { 0x48acc03f, KEY_LEFT}, { 0x48aca05f, KEY_RIGHT}, { 0x48ac20df, KEY_ENTER}, }; static struct rc_map_table repeat_key[] = { //支持重復按下的映射表 { 0x48ac40bf, KEY_UP}, { 0x48ac609f, KEY_DOWN}, { 0x48acc03f, KEY_LEFT}, { 0x48aca05f, KEY_RIGHT}, }; static struct rc_map_list latte_map = { .map = { .scan = latte_key, .size = ARRAY_SIZE(latte_key), .rc_type = RC_TYPE_NEC, //編碼類型為NEC .name = RC_MAP_MY_TEXT, //用來匹配platform_device .repeat_key = repeat_key, .repeat_size = ARRAY_SIZE(repeat_key), } }; static int __init init_rc_map_latte(void) { return rc_map_register(&latte_map); } static void __exit exit_rc_map_latte(void) { rc_map_unregister(&latte_map); } module_init(init_rc_map_latte) module_exit(exit_rc_map_latte) MODULE_LICENSE("GPL"); MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
然后修改drivers\media\rc\keymaps\Makefile,將該文件添加進去
7.編譯試驗
當一直按下上下左右任意鍵時,可以看到能實現重復功能:

通過getevent查看一直按下時,是否一直input上報事件:

當一直按下確定鍵時:

通過getevent查看一直按下確定鍵時,是否只上傳一次input上報事件:

