输入子系统2-作业

利用输入子系统驱动所有按键实验

判断通关:
实验现象截图,并上传代码

测试代码 simple_input_dev.c

/*
/*
设备树文件:
   key_int_node{
                compatible = "test_key";
                #address-cells = <1>;
                #size-cells = <1>;

                key_int@0 {
                        key_name = "key2_power_eint";
                        key_code = <116>;
                        gpio = <&gpx1 1 0>;
                        reg = <0x11000C20 0x18>;
                        interrupt-parent = <&gpx1>;
                        interrupts = <1 0>;
                };

                key_int@1 {
                        key_name = "key3_vup_eint";
                        key_code = <115>;
                        gpio = <&gpx1 2 0>;
                        reg = <0x11000C20 0x18>;
                        interrupt-parent = <&gpx1>;
                        interrupts = <2 0>;
                };

                key_int@2 {
                        key_name = "key4_vdown_eint";
                        key_code = <114>;
                        gpio = <&gpx3 2 0>;
                        reg = <0x11000C60 0x18>;
                        interrupt-parent = <&gpx3>;
                        interrupts = <2 0>;
                };
		};	
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>

#include <asm/io.h>

#define KEY_NUMS 3

//设计一个对象出来
struct key_desc{
	int irqno;
	void* reg_virt_base;
	const char* key_name;
	u32 code;
	int gpionum;
	struct device_node *cnp;	//方便随时获取节点各个信息
	
};

struct key_desc all_key[KEY_NUMS];	//数量设备树文件的子节点数一致即可

struct input_dev *pInputdev;

void get_all_child_from_node(void)
{
	struct device_node *cnp;
	struct device_node *prev = NULL;
	int i;
	
	//获取到设备树中的节点
	struct device_node *np = of_find_node_by_path("/key_int_node");
	
	if(np){
		printk("find node ok\n");
	}
	else{
		printk("find node failed\n");
		return;
	}

	i = 0;
	do{
		//获取子节点
		cnp = of_get_next_child(np, prev);
		
		if(cnp  != NULL)
		{
			all_key[i].cnp = cnp;
			i++;
		}
		
		prev = cnp;	//把当前子节点赋值给prev
	}while(of_get_next_child(np,prev) != NULL);
}




irqreturn_t input_key_irq_handler(int irqno, void *devid)
{
	struct key_desc* pdesc;
	int value;
	printk("--------------%s------------------\n",__FUNCTION__);
	
	//区分不同的按键
	pdesc = (struct key_desc* )devid;

	//直接通过gpio获取按键状态
	value = gpio_get_value(pdesc->gpionum);

	if(value){//抬起
		printk("__KERN__: %d Enter up\n",pdesc->code);	 
		//input_event(pInputdev,EV_KEY,KEY_POWER,0);	//上报数据
		input_report_key(pInputdev,pdesc->code,0);		//上报数据
		input_sync(pInputdev);		//上报数据结束
	}
	else{//按下
		printk("__KERN__: %d Enter down\n",pdesc->code);
		//input_event(pInputdev,EV_KEY,KEY_POWER,1);	//上报数据
		input_report_key(pInputdev, pdesc->code, 1);		//上报数据
		input_sync(pInputdev);	//上报数据结束
	}

	return IRQ_HANDLED;
}


static int __init simple_input_dev_init(void)
{
	/*
		1,分配一个input device对象
		2,初始化input  device对象
		3,注册input device对象
	*/
	int ret ,i;
	
	pInputdev = input_allocate_device();
	
	if(pInputdev == NULL)
		{
			printk(KERN_ERR "input_allocate_device error");
			return -ENOMEM;
		}
	
	get_all_child_from_node();

	//添加设备信息 -- /sys/class/input/eventX/device
	pInputdev->name = "simple input key";//随便取个名字 不然会报错
	pInputdev->phys = "key/input/input0";
	pInputdev->uniq = "simple key0 for 4412";
	pInputdev->id.bustype = BUS_HOST;	//控制模式 :GPIO
	pInputdev->id.vendor = 0x1234;		//厂商代码
	pInputdev->id.product = 0x6666;		//产品代码
	pInputdev->id.version = 0x0001;		//版本号
	
	//当前设备能够产生“按键”类型数据
	__set_bit(EV_KEY,pInputdev->evbit);
	//pInputdev->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
	printk("__KERN__:evbit = %lu\n",*(pInputdev->evbit));
	//当前设备能够产生power按键值
	__set_bit(KEY_POWER,pInputdev->keybit);	
	//pInputdev->keybit[BIT_WORD(KEY_POWER)] |= BIT_MASK(KEY_POWER);

	
	for(i=0; i<KEY_NUMS; i++)
	{	
		//设置Keybit,支持哪些按键
		//获取以整数形式存储于设备树中的<键值>--key_code
		of_property_read_u32(all_key[i].cnp, "key_code" , &all_key[i].code);	
		__set_bit(all_key[i].code, pInputdev->keybit);
		
		//通过节点去获取到中断号码
		all_key[i].irqno = irq_of_parse_and_map(all_key[i].cnp,0);

		//记录以字符串格式存储于设备树中的<名字>--key_name
		of_property_read_string(all_key[i].cnp, "key_name", &all_key[i].key_name);

		//记录节点gpio
		all_key[i].gpionum = of_get_named_gpio(all_key[i].cnp, "gpio", 0);
		//此处也可以写作 of_get_gpio(struct device_node * np, int index)
		//但是设备树中定义gpio时 必须写作 gpios = <&gpx1 1 0>; 
		//而非gpio = <&gpx1 1 0>; (差个s)

		

		printk("__KERN__:name = %s, code = %d, irq = %d\n",pInputdev->name,all_key[i].code,all_key[i].irqno);
		
		ret = request_irq(all_key[i].irqno, input_key_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, all_key[i].key_name, &all_key[i]);
		//&all_key[i] 作为 区分是哪个按键中断调用的中断函数 来使用
		
		if(ret != 0)
		{
			printk("request_irq ERROR!\n");
			goto err0;
			return 0;
		}
	}
	
	ret = input_register_device(pInputdev);		//注册只需注册一次即可。
	if(ret != 0)
	{
		printk(KERN_ERR "input_register_device error");
		goto err1;
		return 0;
	}
	
	return 0;
	
err1:
	for(i=0; i<KEY_NUMS; i++)
	{
		free_irq(all_key[i].irqno,&all_key[i]);
	}

err0:
	input_free_device(pInputdev);

	
	return ret;
}

static void __exit simple_input_dev_exit(void)
{
	int i;
	for(i=0; i<KEY_NUMS; i++)
	{
		free_irq(all_key[i].irqno,&all_key[i]);
	}
	input_unregister_device(pInputdev);
	input_free_device(pInputdev);
}

module_init(simple_input_dev_init);
module_exit(simple_input_dev_exit);
MODULE_LICENSE("GPL");

Makefile代码

ROOTFS_DIR = /home/linux/Level11

APP_NAME = simple_input_app

CROSS_COMPILE = arm-none-linux-gnueabi-
CC = $(CROSS_COMPILE)gcc

ifeq ($(KERNELRELEASE), )

KERNEL_DIR = /home/linux/kernel/linux-3.14-fs4412
CUR_DIR = $(shell pwd)

all :
		make -C  $(KERNEL_DIR) M=$(CUR_DIR) modules -j6 ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
		$(CC) $(APP_NAME).c -o $(APP_NAME)

clean :
		make -C  $(KERNEL_DIR) M=$(CUR_DIR) clean
		rm $(APP_NAME) ./1/$(APP_NAME)
		clear

install:
		cp -raf  *.ko  $(APP_NAME)   ./1/
		#cp -raf  *.ko  ./1/
		
else
obj-m += simple_input_dev.o
#obj-m += simple_input_app.o

endif

效果展示:

[root@farsight ]# insmod simple_input_dev.ko
[    5.820000] __KERN__:evbit = 2
[    5.825000] __KERN__:evbit = 2
[    5.825000] __KERN__:evbit = 2
[    5.830000] find node ok
[    5.830000] __KERN__:name = simple input key, code = 116, irq = 169
[    5.835000] --------------input_key_irq_handler------------------
[    5.835000] __KERN__: 116 Enter down
[    5.850000] input: simple input key as /devices/virtual/input/input1
[    5.855000] __KERN__:name = simple input key, code = 115, irq = 170
[    5.860000] input: simple input key as /devices/virtual/input/input2
[    5.865000] __KERN__:name = simple input key, code = 114, irq = 171
[    5.880000] input: simple input key as /devices/virtual/input/input3
[root@farsight ]# [   27.330000] --------------input_key_irq_handler------------------
[   27.330000] __KERN__: 114 Enter down
[   27.340000] --------------input_key_irq_handler------------------
[   27.340000] __KERN__: 114 Enter down
[   28.770000] --------------input_key_irq_handler------------------
[   28.770000] __KERN__: 116 Enter down
[   28.880000] --------------input_key_irq_handler------------------
[   28.880000] __KERN__: 116 Enter down
[   28.890000] --------------input_key_irq_handler------------------
[   28.890000] __KERN__: 116 Enter down
[   28.935000] --------------input_key_irq_handler------------------
[   28.935000] __KERN__: 116 Enter down
[   29.800000] --------------input_key_irq_handler------------------
[   29.800000] __KERN__: 115 Enter down
[   29.975000] --------------input_key_irq_handler------------------
[   29.975000] __KERN__: 115 Enter down

运行环境

内核:linux3.14