在上一节中,我们讲解了如何自动创建设备节点,实现一个中断方式的按键驱动。虽然中断式的驱动,效率是蛮高的,但是大家有没有发现,应用程序的死循环里的读函数是一直在读的;在实际的应用场所里,有没有那么一种情况,偶尔有数据、偶尔没有数据,答案当然是有的。我们理想当然的就会想到,当有数据的时候,我们才去读它,没数据的时候我们读它干啥?岂不浪费劳动力?
上一节文章链接:http://blog.csdn.net/lwj103862095/article/details/17511867
这一节里,我们在中断的基础上添加poll机制来实现有数据的时候就去读,没数据的时候,自己规定一个时间,如果还没有数据,就表示超时时间。
poll机制总结:(韦老师总结的,不是我总结的哦!)
1. poll > sys_poll > do_sys_poll >poll_initwait,poll_initwait函数注册一下回调函数__pollwait,它就是我们的驱动程序执行poll_wait时,真正被调用的函数。
2. 接下来执行file->f_op->poll,即我们驱动程序里自己实现的poll函数
3. 如果设备未就绪,do_sys_poll里会让进程休眠一定时间
4. 进程被唤醒的条件有2:一是上面说的“一定时间”到了,二是被驱动程序唤醒。驱动程序发现条件就绪时,就把“某个队列”上挂着的进程唤醒,这个队列,就是前面通过poll_wait把本进程挂过去的队列。
5. 如果驱动程序没有去唤醒进程,那么chedule_timeout(__timeou)超时后,会重复2、3动作,直到应用程序的poll调用传入的时间到达。
问:这一节与上一节的在驱动里添加了哪些内容?
答:仅仅添加了poll函数,该函数如下:
- static
unsigned int fourth_drv_poll(struct file *file, poll_table *wait) - {
-
unsigned int mask = 0; -
-
-
poll_wait(file, &button_waitq, wait); -
-
-
if(ev_press) -
{ -
mask |= POLLIN | POLLRDNORM; -
} -
-
-
return mask; - }
详细请参考驱动源码:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
//class_create - #include
//S3C2410_GPF1 - //#include
- #include
- //#include
- #include
//wait_event_interruptible - #include
//poll -
-
-
- static
DECLARE_WAIT_QUEUE_HEAD(button_waitq); -
-
- static
struct class *fourthdrv_class; - static
struct device *fourthdrv_device; -
- static
struct pin_desc{ -
unsigned int pin; -
unsigned int key_val; - };
-
- static
struct pin_desc pins_desc[4] = { -
{S3C2410_GPF1,0x01}, -
{S3C2410_GPF4,0x02}, -
{S3C2410_GPF2,0x03}, -
{S3C2410_GPF0,0x04}, - };
-
- static
int ev_press = 0; -
-
-
- static
unsigned char key_val; - int
major; -
-
- static
irqreturn_t buttons_irq(int irq, void *dev_id) - {
-
struct pin_desc *pindesc = (struct pin_desc *)dev_id; -
unsigned int pinval; -
pinval = s3c2410_gpio_getpin(pindesc->pin); -
-
if(pinval) -
{ -
-
key_val = 0x80 | (pindesc->key_val); -
} -
else -
{ -
-
key_val = pindesc->key_val; -
} -
-
ev_press = 1; -
wake_up_interruptible(&button_waitq); -
return IRQ_HANDLED; - }
- static
int fourth_drv_open(struct inode * inode, struct file * filp) - {
-
-
request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K1",&pins_desc[0]); -
request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K2",&pins_desc[1]); -
request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K3",&pins_desc[2]); -
request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K4",&pins_desc[3]); -
return 0; - }
-
- static
ssize_t fourth_drv_read(struct file *file, char __user *user, size_t size,loff_t *ppos) - {
-
if (size != 1) -
return -EINVAL; -
-
-
wait_event_interruptible(button_waitq, ev_press); -
copy_to_user(user, &key_val, 1); -
-
-
ev_press = 0; -
return 1; - }
-
- static
int fourth_drv_close(struct inode *inode, struct file *file) - {
-
free_irq(IRQ_EINT1,&pins_desc[0]); -
free_irq(IRQ_EINT4,&pins_desc[1]); -
free_irq(IRQ_EINT2,&pins_desc[2]); -
free_irq(IRQ_EINT0,&pins_desc[3]); -
return 0; - }
-
- static
unsigned int fourth_drv_poll(struct file *file, poll_table *wait) - {
-
unsigned int mask = 0; -
-
-
poll_wait(file, &button_waitq, wait); -
-
-
if(ev_press) -
{ -
mask |= POLLIN | POLLRDNORM; -
} -
-
-
return mask; - }
-
-
-
- static
const struct file_operations fourth_drv_fops = { -
.owner = THIS_MODULE, -
.open = fourth_drv_open, -
.read = fourth_drv_read, -
.release = fourth_drv_close, -
.poll = fourth_drv_poll, - };
-
-
-
- static
int fourth_drv_init(void) - {
-
-
major = register_chrdev(0, "fourth_drv", &fourth_drv_fops); -
-
-
fourthdrv_class = class_create(THIS_MODULE, "fourthdrv"); -
-
-
fourthdrv_device = device_create(fourthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); -
-
return 0; - }
-
-
- static
void fourth_drv_exit(void) - {
-
unregister_chrdev(major, "fourth_drv"); -
device_unregister(fourthdrv_device); //卸载类下的设备 -
class_destroy(fourthdrv_class); //卸载类 - }
-
- module_init(fourth_drv_init);
//用于修饰入口函数 - module_exit(fourth_drv_exit);
//用于修饰出口函数 -
- MODULE_AUTHOR("LWJ");
- MODULE_DESCRIPTION("Just
for Demon"); - MODULE_LICENSE("GPL");
//遵循GPL协议
应用测试源码:
- #include
- #include
- #include
- #include
- #include
- #include
-
-
- int
main(int argc ,char *argv[]) -
- {
-
int fd; -
unsigned char key_val; -
struct pollfd fds; -
int ret; -
-
fd = open("/dev/buttons",O_RDWR); -
if (fd < 0) -
{ -
printf("open error\n"); -
} -
fds.fd = fd; -
fds.events = POLLIN; -
while(1) -
{ -
-
ret = poll(&fds,1,5000); -
if(ret == 0) -
{ -
printf("time out\n"); -
} -
else -
{ -
read(fd,&key_val,1); -
printf("key_val = 0x%x\n",key_val); -
} -
} -
return 0; - }
测试步骤:
- [WJ2440]#
ls - Qt
etc lib sbin third_test - TQLedtest
first_drv.ko linuxrc sddisk tmp - app_test
first_test mnt second_drv.ko udisk - bin
fourth_drv.ko opt second_test usr - dev
fourth_test proc sys var - driver_test
home root third_drv.ko web - [WJ2440]#
insmod fourth_drv.ko - [WJ2440]#
lsmod - fourth_drv
3164 0 - Live 0xbf003000 - [WJ2440]#
ls /dev/buttons -l - crw-rw----
1 root root 252, 0 Jan 2 03:00 /dev/buttons - [WJ2440]#
./fourth_test - time
out - time
out - key_val
= 0x1 - key_val
= 0x81 - key_val
= 0x4 - key_val
= 0x84 - key_val
= 0x3 - key_val
= 0x83 - key_val
= 0x2 - key_val
= 0x82 - ^C
- [WJ2440]#
./fourth_test & - [WJ2440]#
time out - time
out - time
out - [WJ2440]#top
-
- Mem:
10076K used, 50088K free, 0K shrd, 0K buff, 7224K cached - CPU:
0.1% usr 0.7% sys 0.0% nic 99.0% idle 0.0% io 0.0% irq 0.0% sirq - Load
average: 0.00 0.00 0.00 1/23 637 -
PID PPID USER STAT VSZ %MEM CPU %CPU COMMAND -
637 589 root R 2092 3.4 0 0.7 top -
589 1 root S 2092 3.4 0 0.0 -/bin/sh -
1 0 root S 2088 3.4 0 0.0 init -
590 1 root S 2088 3.4 0 0.0 /usr/sbin/telnetd -l /bin/login -
587 1 root S 1508 2.5 0 0.0 EmbedSky_wdg -
636 589 root S 1432 2.3 0 0.0 ./fourth_test -
573 2 root SW< 0 0.0 0 0.0 [rpciod/0] -
5 2 root SW< 0 0.0 0 0.0 [khelper] -
329 2 root SW< 0 0.0 0 0.0 [nfsiod] -
2 0 root SW< 0 0.0 0 0.0 [kthreadd] -
3 2 root SW< 0 0.0 0 0.0 [ksoftirqd/0] -
4 2 root SW< 0 0.0 0 0.0 [events/0] -
11 2 root SW< 0 0.0 0 0.0 [async/mgr] -
237 2 root SW< 0 0.0 0 0.0 [kblockd/0] -
247 2 root SW< 0 0.0 0 0.0 [khubd] -
254 2 root SW< 0 0.0 0 0.0 [kmmcd] -
278 2 root SW 0 0.0 0 0.0 [pdflush] -
279 2 root SW 0 0.0 0 0.0 [pdflush] -
280 2 root SW< 0 0.0 0 0.0 [kswapd0] -
325 2 root SW< 0 0.0 0 0.0 [aio/0]
由测试结果可以看出,当按键没有被按下时,5秒之后,会显示出time out,表示时间已到,在这5秒时间里,没有按键被按下,即没有数据可读,当按键按下时,立即打印出按下的按键;同时,fourth_test进程,也几乎不占用CPU的利用率。