上一节中,我们讲解了Linux input子系统的框架,到内核源码里详细分析了输入子系统的分离分层的框架等。
上一节文章链接:http://blog.csdn.net/lwj103862095/article/details/17733993
这一节,我们来以输入子系统的框架来写一个按键驱动。
问:怎么写符合输入子系统框架的驱动程序?
答:
1. 分配一个input_dev结构体
2. 设置
3. 注册
4. 硬件相关的代码,比如在中断服务程序里上报事件
问:如何分配input_dev结构体?
答:使用input_allocate_device函数
input_dev结构体的重要成员
-
struct input_dev {
-
const char *name;
-
const char *phys;
-
const char *uniq;
-
struct input_id id;
-
-
unsigned long evbit[NBITS(EV_MAX)];
-
unsigned long keybit[NBITS(KEY_MAX)];
-
unsigned long relbit[NBITS(REL_MAX)];
-
unsigned long absbit[NBITS(ABS_MAX)];
-
unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];
-
unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];
-
unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];
-
unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
-
...
-
}
问:第二步的设置,应该怎么设置,应该设置什么?
答:举例,在此按键驱动里
-
-
-
set_bit(EV_KEY,buttons_dev->evbit);
-
set_bit(EV_REP,buttons_dev->evbit);
-
-
-
set_bit(KEY_L,buttons_dev->keybit);
-
set_bit(KEY_S,buttons_dev->keybit);
-
set_bit(KEY_ENTER,buttons_dev->keybit);
-
set_bit(KEY_LEFTSHIFT,buttons_dev->keybit);
问:有哪些类呢?
答:在input.h里有以下类
-
#define EV_SYN 0x00 //同步类
-
#define EV_KEY 0x01 //按键类
-
#define EV_REL 0x02 //相对位移类
-
#define EV_ABS 0x03 //绝对位移类
-
#define EV_MSC 0x04
-
#define EV_SW 0x05
-
#define EV_LED 0x11
-
#define EV_SND 0x12 //声音类
-
#define EV_REP 0x14 //重复类
-
#define EV_FF 0x15
-
#define EV_PWR 0x16
-
#define EV_FF_STATUS 0x17
-
#define EV_MAX 0x1f
-
#define EV_CNT (EV_MAX+1)
问:如何注册?
答:使用input_register_device(struct input_dev *dev)函数来注册
问:此按键驱动的硬件操作包括哪些操作?
答:申请定时器、申请中断操作
驱动源码:
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include //class_create
-
#include //S3C2410_GPF1
-
-
#include
-
-
#include //wait_event_interruptible
-
#include //poll
-
#include
-
#include
-
-
static struct pin_desc{
-
int irq;
-
unsigned char *name;
-
unsigned int pin;
-
unsigned int key_val;
-
};
-
-
static struct pin_desc pins_desc[4] = {
-
{IRQ_EINT1,"K1",S3C2410_GPF1,KEY_L},
-
{IRQ_EINT4,"K2",S3C2410_GPF4,KEY_S},
-
{IRQ_EINT2,"K3",S3C2410_GPF2,KEY_ENTER},
-
{IRQ_EINT0,"K4",S3C2410_GPF0,KEY_LEFTSHIFT},
-
};
-
-
static struct pin_desc *irq_pd;
-
static struct input_dev *buttons_dev;
-
static struct timer_list buttons_timer;
-
-
-
static irqreturn_t buttons_irq(int irq, void *dev_id)
-
{
-
irq_pd = (struct pin_desc *)dev_id;
-
-
-
-
-
-
mod_timer(&buttons_timer, jiffies + (HZ /100));
-
return IRQ_HANDLED;
-
}
-
-
-
-
static void buttons_timer_function(unsigned long data)
-
{
-
struct pin_desc *pindesc = irq_pd;
-
unsigned int pinval;
-
pinval = s3c2410_gpio_getpin(pindesc->pin);
-
-
if(pinval)
-
{
-
-
input_event(buttons_dev,EV_KEY,pindesc->key_val,0);
-
input_sync(buttons_dev);
-
}
-
else
-
{
-
-
input_event(buttons_dev,EV_KEY,pindesc->key_val,1);
-
input_sync(buttons_dev);
-
}
-
}
-
-
-
static int buttons_input_init(void)
-
{
-
int i;
-
-
-
buttons_dev = input_allocate_device();
-
-
-
-
set_bit(EV_KEY,buttons_dev->evbit);
-
set_bit(EV_REP,buttons_dev->evbit);
-
-
-
set_bit(KEY_L,buttons_dev->keybit);
-
set_bit(KEY_S,buttons_dev->keybit);
-
set_bit(KEY_ENTER,buttons_dev->keybit);
-
set_bit(KEY_LEFTSHIFT,buttons_dev->keybit);
-
-
-
input_register_device(buttons_dev);
-
-
-
-
-
init_timer(&buttons_timer);
-
buttons_timer.function = buttons_timer_function;
-
add_timer(&buttons_timer);
-
-
-
for(i = 0;i < sizeof(pins_desc)/sizeof(pins_desc[0]);i++)
-
{
-
request_irq(pins_desc[i].irq, buttons_irq, IRQ_TYPE_EDGE_BOTH, pins_desc[i].name, &pins_desc[i]);
-
}
-
-
return 0;
-
}
-
-
-
static void buttons_input_exit(void)
-
{
-
int i;
-
for(i = 0;i < sizeof(pins_desc)/sizeof(pins_desc[0]);i++)
-
{
-
free_irq(pins_desc[i].irq, &pins_desc[i]);
-
}
-
del_timer(&buttons_timer);
-
input_unregister_device(buttons_dev);
-
input_free_device(buttons_dev);
-
}
-
-
module_init(buttons_input_init);
-
module_exit(buttons_input_exit);
-
-
MODULE_AUTHOR("LWJ");
-
MODULE_DESCRIPTION("Just for Demon");
-
MODULE_LICENSE("GPL");
测试步骤方法一:
-
[WJ2440]# ls
-
Qt first_test second_test
-
TQLedtest fourth_drv.ko sixth_drv.ko
-
app_test fourth_test sixth_test
-
bin home sixthdrvtest
-
buttons_all_drv.ko lib sys
-
buttons_all_test linuxrc third_drv.ko
-
buttons_input.ko mnt third_test
-
dev opt tmp
-
driver_test proc udisk
-
etc root usr
-
fifth_drv.ko sbin var
-
fifth_test sddisk web
-
first_drv.ko second_drv.ko
-
[WJ2440]# ls /dev/event* -l
-
crw-rw---- 1 root root 13, 64 Jan 2 06:04 /dev/event0
-
[WJ2440]# insmod buttons_input.ko
-
input: Unspecified device as /devices/virtual/input/input1
-
[WJ2440]# ls /dev/event* -l
-
crw-rw---- 1 root root 13, 64 Jan 2 06:04 /dev/event0
-
crw-rw---- 1 root root 13, 65 Jan 2 06:06 /dev/event1
-
[WJ2440]# cat /dev/tty1
-
[WJ2440]# cat /dev/tty1
-
ls
-
-
ls
输入cat /dev/tty1命令后,顺序按下K1,K2,K3则会显示ls
测试步骤方法二、
-
[WJ2440]# hexdump /dev/event1
-
0000000 b738 495d 8456 0007 0001 0026 0001 0000
-
0000010 b738 495d 846f 0007 0000 0000 0000 0000
-
0000020 b738 495d 2fb8 000a 0001 0026 0000 0000
-
0000030 b738 495d 2fc7 000a 0000 0000 0000 0000
分析:
hexdump /dev/event1 (open(/dev/event1), read(), )
秒 微秒 类 code value
0000000 0bb2 0000 0e48 000c 0001 0026 0001 0000
0000010 0bb2 0000 0e54 000c 0000 0000 0000 0000
0000020 0bb2 0000 5815 000e 0001 0026 0000 0000
0000030 0bb2 0000 581f 000e 0000 0000 0000 0000
-
struct input_event {
-
struct timeval time;
-
__u16 type;
-
__u16 code;
-
__s32 value;
-
};
-
-
struct timeval {
-
__kernel_time_t tv_sec;
-
__kernel_suseconds_t tv_usec;
-
};
疑问:在韦老师视频里,执行exec 0
我自己执行的时候,发现文件系统里并没有exec可执行文件,也就是busybox里没有移植有exec可执行文件,去找韦老师的文件系统时,也没有发现exec可执行文件,请教各位大虾,如果你有执行成功,请告之,感谢。
阅读(2031) | 评论(0) | 转发(0) |