linux输入子系统:最简单的例子

#include <linux/module.h>
#include <linux/input.h>

/**
参考:

输入子系统的写法demo https://www.kernel.org/doc/html/latest/input/input-programming.html
输入子系统的函数api:https://www.kernel.org/doc/html/latest/driver-api/input.html

测试:cat /proc/bus/input/devices会发现注册的设备
**/
static struct input_dev *button_dev;

static int __init button_init(void)
{
        int error;
        button_dev = input_allocate_device();//分配输入设备内存,失败返回NULL
        if (!button_dev) {
                printk(KERN_ERR "button.c: Not enough memory\n");//如果分配失败,没有足够内存
                error = -ENOMEM;
  		return error;
        }
	//输入设备名字
	button_dev->name="I am simplest input subsystem";
	//输入设备时间类型为键盘事件
        button_dev->evbit[0] = BIT_MASK(EV_KEY);
	//输入设备的键盘掩码为:拥有方向上键
        button_dev->keybit[BIT_WORD(KEY_UP)] = BIT_MASK(KEY_UP);
	
	//设置产品id
	button_dev->id.bustype=BUS_I8042;
	button_dev->id.vendor=0x1111;
	button_dev->id.product=0x2222;
	button_dev->id.version=0x5555;


	//将设备注册进输入子系统
        error = input_register_device(button_dev);
        if (error) {
                printk(KERN_ERR "button.c: Failed to register device\n");//注册失败
		input_free_device(button_dev);//释放内存
		return error;
        }
	printk(KERN_DEBUG "OK,input subsystem inited!\n");
        return 0;
}

static void __exit button_exit(void)
{
        input_unregister_device(button_dev);
}

module_init(button_init);
module_exit(button_exit);

编译进内核烧进开发板或者编译成模块insmod后就会发现

cat /proc/bus/input/devices会发现注册的设备
I: Bus=0011 Vendor=1111 Product=2222 Version=5555                               
N: Name="I am simplest input subsystem"                                         
P: Phys=                                                                        
S: Sysfs=/devices/virtual/input/input0                                          
U: Uniq=                                                                        
H: Handlers=kbd event0                                                          
B: EV=3                                                                         
B: KEY=80 0 0 0 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值