button_drv.c驱动文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h>?
#include <asm/uaccess.h>?
#include <linux/device.h>?
#include <asm/arch/regs-gpio.h>?
#include <linux/irq.h>?
#include <asm-arm/irq.h>?
#include <linux/interrupt.h>?
#include <linux/delay.h>
#include <asm/hardware.h>
#include <linux/poll.h>
#define DRIVER_NAME "button_drv"
#define DEVICE_NAME "button_dev"
int major;
static DECLARE_MUTEX(button_lock); //定义互斥锁
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
struct class *button_class;
struct class_device *button_class_device;
unsigned char ev_press;
DECLARE_WAIT_QUEUE_HEAD(button_waitq);?
struct fasync_struct *button_fasync;?
unsigned char keyVal;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0,0x01},
{S3C2410_GPF2,0x02},courier; font-size: 18px;"> {S3C2410_GPG3,0x03},courier; font-size: 18px;"> {S3C2410_GPG11,0x04},courier; font-size: 18px;">};
irqreturn_t buttons_irq(int irq,void *dev_id)
{
unsigned int pin_val;
struct pin_desc *pin_desc = (struct pin_desc *)dev_id;
pin_val = s3c2410_gpio_getpin(pin_desc->pin);
if(pin_val)
{
keyVal = 0x80 | pin_desc->key_val;
}
else
keyVal = pin_desc->key_val;
wake_up_interruptible(&button_waitq);
ev_press = 1;
kill_fasync(&button_fasync,SIGIO,POLL_IN);?
return IRQ_HANDLED;
}
int button_drv_open(struct inode *inode,struct file *file)
int ret;
if(file->f_flags&O_NONBLOCK)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//非阻塞
if(down_trylock(&button_lock)) ?//获取信号量,失败返回非0
{
printk("failed 1 button_drv_open n");
return -EBUSY;
}
else //阻塞
down(&button_lock); ??//获取信号量,如果无法获取则休眠
}
ret = request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S1",&pins_desc[0]);
if(ret<0)
printk("failed 1 button_drv_open");
ret = request_irq(IRQ_EINT2,"S2",&pins_desc[1]);
printk("failed 2 button_drv_open");
ret = request_irq(IRQ_EINT11,"S3",&pins_desc[2]);
printk("failed 3 button_drv_open");
ret = request_irq(IRQ_EINT19,"S4",&pins_desc[3]);
printk("failed 4 button_drv_open");
return 0;
ssize_t button_drv_read(struct file *file,char __user *userbuf,size_t count,loff_t *off)
int ret;
if(file->f_flags&O_NONBLOCK)
if(ev_press!=1) //没有按键按下直接返回
printk("failed 1 button_drv_read n");
return -EAGAIN;
else //阻塞
wait_event_interruptible(button_waitq,ev_press); //如果按键没有动作则进入休眠
ret = copy_to_user(userbuf,&keyVal,1);
printk("failed 1 button_drv_read n");
return -1;
ev_press = 0;
return 1;
int button_drv_close(struct inode *inode,courier; font-size: 18px;"> free_irq(IRQ_EINT0,&pins_desc[0]);?
free_irq(IRQ_EINT2,courier; font-size: 18px;"> free_irq(IRQ_EINT11,courier; font-size: 18px;"> free_irq(IRQ_EINT19,courier; font-size: 18px;"> up(&button_lock); ??//释放互斥信号量
return 0;
unsigned int button_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;
int button_drv_fasync(int fd,struct file *file,int on)
ret = fasync_helper(fd,file,on,&button_fasync);
printk("failed 1 button_drv_fasync n");
return ret;
struct file_operations button_drv_fops = {
.owner = THIS_MODULE,courier; font-size: 18px;"> .open = button_drv_open,courier; font-size: 18px;"> .read = button_drv_read,courier; font-size: 18px;"> .release = button_drv_close,courier; font-size: 18px;"> .poll = button_drv_poll,courier; font-size: 18px;"> .fasync = button_drv_fasync,courier; font-size: 18px;">int __init button_drv_init(void)
major = register_chrdev(0,DRIVER_NAME,&button_drv_fops);
if(major<0)
printk("failed 1 button_drv_init n");
button_class = class_create(THIS_MODULE,DEVICE_NAME);
if(button_class<0)
printk("failed 2 button_drv_init n");
button_class_device = class_device_create(button_class,NULL,MKDEV(major,0),courier; font-size: 18px;"> if(button_class_device<0)
printk("failed 3 button_drv_init n");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,courier; font-size: 18px;"> gpgdat = gpgcon + 1;
void __exit button_drv_exit(void)
unregister_chrdev(major,courier; font-size: 18px;"> class_device_unregister(button_class_device);
class_destroy(button_class);
iounmap(gpfcon);
iounmap(gpgcon);
module_init(button_drv_init);
module_exit(button_drv_exit);
MODULE_LICENSE("GPL");
Makefile文件:
obj-m += timer_drv.o
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules?
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c
button_app_1.c文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
static int fd;
void button_signal(int signum)
unsigned char keyVal;
printf("signal = %d n",signum);
read(fd,courier; font-size: 18px;"> printf("keyVal = 0x%x n",keyVal);
int main(int argc,char **argv)
int oflags;
char *filename;
filename = argv[1];
fd = open(filename,O_RDWR); ? ??//阻塞
if(fd<0)
printf("can not open n");
signal(SIGIO,button_signal);?
fcntl(fd,F_SETOWN,getpid());?
oflags = fcntl(fd,F_GETFL);?
while(1)
sleep(1000);
}
button_app_2.c文件:
?
#include <signal.h>
?
static int fd;
?
}
?
//非阻塞
编译生成button_drv.ko和button_app_1、button_app_2文件,运行./button_app_1 /dev/button_dev或./button_app_2 /dev/button_dev