内核版本:linux2.6.22.6 硬件平台:JZ2440

驱动源码 atom_ipc_poll_key_int_drv.c :

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h> static struct class *key_int_class;
static struct class_device *key_int_class_device; volatile unsigned long *GPFCON=NULL;
volatile unsigned long *GPFDAT=NULL;
volatile unsigned long *GPGCON=NULL;
volatile unsigned long *GPGDAT=NULL; static struct fasync_struct fasync_key; //定义一个 fsync 结构体 struct pin_desc
{
unsigned int pin;
unsigned int key_val;
}; static unsigned char key_val;
static volatile int ev_press=; static struct pin_desc pin_desc_array[]={{S3C2410_GPF0,0x01},{S3C2410_GPF2,0X02},{S3C2410_GPG3,0x03},{S3C2410_GPG11,0x04}}; static DECLARE_WAIT_QUEUE_HEAD(wait_key); static atomic_t ready=ATOMIC_INIT(); static irqreturn_t key_handler(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 = 0x08 | pindesc->key_val;
else key_val = pindesc->key_val; kill_fasync(&fasync_key,SIGIO,POLL_IN); //发生中断后 向结构体里的PID进程 发送 SIGIO 信号 wake_up_interruptible(&wait_key);
ev_press = ;
return IRQ_RETVAL(IRQ_HANDLED);
} static int key_drv_open(struct inode *inode,struct file *file)
{
if(!atomic_dec_and_test(&ready))
{
atomic_inc(&ready);
return -EBUSY;
} request_irq(IRQ_EINT0, key_handler,IRQT_BOTHEDGE,"KEY1", &pin_desc_array[]);
request_irq(IRQ_EINT2, key_handler,IRQT_BOTHEDGE,"KEY2", &pin_desc_array[]);
request_irq(IRQ_EINT11, key_handler,IRQT_BOTHEDGE,"KEY3",&pin_desc_array[]);
request_irq(IRQ_EINT19, key_handler,IRQT_BOTHEDGE,"KEY4",&pin_desc_array[]); return ;
} ssize_t key_drv_read(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
{
if (count != )
return -EINVAL; wait_event_interruptible(wait_key,ev_press);
ev_press=;
copy_to_user(buf,&key_val,); return ;
} static int key_drv_close(struct inode *inode,struct file *file)
{
atomic_inc(&ready); free_irq(IRQ_EINT0, &pin_desc_array[]);
free_irq(IRQ_EINT2, &pin_desc_array[]);
free_irq(IRQ_EINT11, &pin_desc_array[]);
free_irq(IRQ_EINT19, &pin_desc_array[]); return ;
} unsigned int key_drv_poll(struct file *file,poll_table *wait)
{
unsigned int mask=;
poll_wait(file,&wait_key,wait); if(ev_press) mask |= POLLIN | POLLRDNORM; return mask;
} static int init_fasync(int fd,struct file *file,int on) // 初始化FASYNC 结构体
{
printk("init fasync struct...\n");
return fasync_helper(fd,file,on,&fasync_key);
} static struct file_operations key_drv_mode=
{
.owner = THIS_MODULE,
.open = key_drv_open,
.read = key_drv_read,
.release = key_drv_close,
.poll = key_drv_poll,
.fasync = init_fasync,
}; int major=;
static int key_drv_init(void)
{
major = register_chrdev(,"atom_ipc_poll_key",&key_drv_mode); // /proc/devices key_int_class = class_create(THIS_MODULE,"key_int_class");
key_int_class_device = class_device_create(key_int_class,NULL,MKDEV(major,),NULL,"atom_ipc_poll_key"); // /dev/key_int_drv GPFCON=(volatile unsigned long *)ioremap(0x56000050,);
GPFDAT=GPFCON+;
GPGCON=(volatile unsigned long *)ioremap(0x56000060,);
GPGDAT=GPGCON+; return ;
} static void key_drv_exit(void)
{
unregister_chrdev(major,"atom_ipc_poll_key"); class_device_unregister(key_int_class_device);
class_destroy(key_int_class);
iounmap(GPFCON);
iounmap(GPGCON); } module_init(key_drv_init);
module_exit(key_drv_exit);
MODULE_LICENSE("GPL");

测试应用程序 atom_ipc_poll_key_int_drv_test.c :

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> int fd=; void act_fun(void) // 捕获信号响应函数
{
unsigned char key_val=;
read(fd,&key_val,);
printf("key_val= %d\n",key_val);
} int main(int argc,char **argv)
{
unsigned char key_val=;
int oflags=; int ret;
struct pollfd fds[]; fd = open("/dev/atom_ipc_poll_key",O_RDWR); if(fd <)
{
printf("error: can't open device :/dev/atom_ipc_poll_key\n");
return -;
} signal(SIGIO,act_fun); //捕获信号 fcntl(fd,F_SETOWN,getpid()); //应用程 序用 fcntl 告诉fd驱动 当前应用程序的PID
oflags = fcntl(fd,F_GETFL); // 获取 fd驱动的 状态旗标
fcntl(fd,F_SETFL,oflags|FASYNC);//更新oflags while()
{
sleep();
}
return ;
}

Makefile文件:

KER_DIR=/work/systems/kernel/linux-/linux-2.6.22.6

all:
make -C $(KER_DIR) M=`pwd` modules clean:
make -C $(KER_DIR) M=`pwd` modules clean
rm -fr moudles.order obj-m +=atom_ipc_poll_key_int_drv.o

内核atom机制的更多相关文章

  1. 锁相关知识 & mutex怎么实现的 & spinlock怎么用的 & 怎样避免死锁 & 内核同步机制 & 读写锁

    spinlock在上一篇文章有提到:http://www.cnblogs.com/charlesblc/p/6254437.html  通过锁数据总线来实现. 而看了这篇文章说明:mutex内部也用到 ...

  2. [内核同步]浅析Linux内核同步机制

    转自:http://blog.csdn.net/fzubbsc/article/details/37736683?utm_source=tuicool&utm_medium=referral ...

  3. Linux内核同步机制--转发自蜗窝科技

    Linux内核同步机制之(一):原子操作 http://www.wowotech.net/linux_kenrel/atomic.html 一.源由 我们的程序逻辑经常遇到这样的操作序列: 1.读一个 ...

  4. Linux内核同步机制

    http://blog.csdn.net/bullbat/article/details/7376424 Linux内核同步控制方法有很多,信号量.锁.原子量.RCU等等,不同的实现方法应用于不同的环 ...

  5. Linux内核OOM机制的详细分析(转)

    Linux 内核 有个机制叫OOM killer(Out-Of-Memory killer),该机制会监控那些占用内存过大,尤其是瞬间很快消耗大量内存的进程,为了 防止内存耗尽而内核会把该进程杀掉.典 ...

  6. Linux 内核同步机制

        本文将就自己对内核同步机制的一些简要理解,做出一份自己的总结文档.     Linux内部,为了提供对共享资源的互斥访问,提供了一系列的方法,下面简要的一一介绍. Technorati 标签: ...

  7. Linux内核OOM机制的详细分析

    Linux 内核有个机制叫OOM killer(Out-Of-Memory killer),该机制会监控那些占用内存过大,尤其是瞬间很快消耗大量内存的进程,为了防止内存耗尽而内核会把该进程杀掉.典型的 ...

  8. Linux内核同步机制之(五):Read Write spin lock【转】

    一.为何会有rw spin lock? 在有了强大的spin lock之后,为何还会有rw spin lock呢?无他,仅仅是为了增加内核的并发,从而增加性能而已.spin lock严格的限制只有一个 ...

  9. 【内核】Linux内核Initrd机制解析,内核更新步骤,grub配置说明

    什么是Initrd initrd的英文含义是 boot loader initialized RAM disk,就是由boot loader初始化的内存盘.在 linux内核启动前, boot loa ...

随机推荐

  1. js中关于Blob对象的介绍与使用

    js中关于Blob对象的介绍与使用   blob对象介绍 一个 Blob对象表示一个不可变的, 原始数据的类似文件对象.Blob表示的数据不一定是一个JavaScript原生格式 blob对象本质上是 ...

  2. [Bayes] Understanding Bayes: A Look at the Likelihood

    From: https://alexanderetz.com/2015/04/15/understanding-bayes-a-look-at-the-likelihood/ Reading note ...

  3. [JS] Topic - Object.create vs new

    故事背景 Ref: 你不知道的javascript之Object.create 和new区别 var Base = function () {} (1) var o1 = new Base(); (2 ...

  4. 【python】关键网站

    https://pypi.org https://www.python.org/search/?q=pyhanlp&submit= https://www.lfd.uci.edu/~gohlk ...

  5. [Android] 基于 Linux 命令行构建 Android 应用(七):自动化构建

    本章将演示如何基于 Linux 命令行构建 Android 应用,在开始本章之前,希望你已经阅读之前几章内容. 本文环境为 RHEL Sandiego 32-bits,要基于 Linux CLI 构建 ...

  6. Springmvc的原理和业务处理

    要尽量弄懂这个springmvc的工作原理:DispatcherServle,HandlerMapping,HandlerAdapter和ViewResolver等对象协同工作,完成springmvc ...

  7. 时间模块和random模块

    时间模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time( ...

  8. DOM内容操作

    <table border="2"> <thead id="1" class="c1 c2"> <tr> ...

  9. 使用 PREPARE 的几个注意点

    简单的用set或者declare语句定义变量,然后直接作为sql的表名是不行的,mysql会把变量名当作表名.在其他的sql数据库中也是如此,mssql的解决方法是将整条sql语句作为变量,其中穿插变 ...

  10. ajax 200 4 parseerror 的错误

    这个问题也碰到几次: 最后在网上还是找到了点线索:1.一可能是data:中的json 不规范2.js语句不规范3.我碰到的是dataType: 'json',data:是数组,最后把json改为tex ...