史上最最简单的Linux Input子系统入门Demo驱动使用定时器模拟键盘输入来构造一个Input设备

以下使用了一个定时器通过Linux 内核 Input子系统每隔500毫秒发送键盘0事件给上层。

代码如下:

#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/timer.h>

/**
 *
 * dts :
 my_platform_device003 {
 status = "okay";
 compatible ="my_platform_device_003";
 };

 how  to test:
 tree  /sys/input/
 sudo  hexdumop  /sys/input/eventX


 */

static struct input_dev *button_dev;

bool a = true;

static struct timer_list my_timer;

static void my_timer_func(unsigned long unused) {
	pr_info("timer expires , now  do sth 23333\n");
	a = !a;
	pr_info("input_report_key %d  \n", a);
	input_report_key(button_dev, BTN_0, a);
	input_sync(button_dev);
	pr_info("button_interrupt start \n");

	if (!timer_pending(&my_timer)) {
		mod_timer(&my_timer, jiffies + msecs_to_jiffies(500));
	}
}

int myprobe(struct platform_device *pdev) {
	int ret;
	pr_info("myplatformdriver myprobe \n");

	button_dev = input_allocate_device();
	if (!button_dev) {
		printk(KERN_ERR "button.c: Not enough memory\n");
		return -ENOMEM;
	}
	button_dev->name = "my simple key2";
	set_bit(EV_KEY, button_dev->evbit);
	set_bit(BTN_0, button_dev->keybit);
	button_dev->dev.parent = &pdev->dev;
	ret = input_register_device(button_dev);
	if (ret) {
		printk(KERN_ERR "button.c: Failed to register device\n");
		goto err_free_dev;
	}

	my_timer.function = my_timer_func;
	my_timer.expires = jiffies + msecs_to_jiffies(500);
	my_timer.data = 0;
	init_timer(&my_timer);
	add_timer(&my_timer);

	return 0;
	err_free_dev: input_free_device(button_dev);
	return ret;
}

int myremove(struct platform_device *pdev) {
	del_timer(&my_timer);
	input_unregister_device(button_dev);
	input_free_device(button_dev);
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver =
		{ .of_match_table = &my_of_match_table, .name = "my-platform-driver",
				.owner = THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");

目前还是使用了设备树的,请按照注释来添加…

**dts :
my_platform_device003 {
status = “okay”;
compatible =“my_platform_device_003”;
};

how to test:

tree /sys/input/
sudo hexdump /sys/input/eventX

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值