关于 exynos 4412 按键中断 异步通知
以下是驱动测试代码:
//内核的驱动代码
#include <linux/init.h>
#include <linux/module.h> //for module_init moudule_exit
#include <linux/fs.h> //for mkdev register_chrdev_region
#include <linux/cdev.h> //字符设备头文件
#include <linux/device.h>
#include <asm/io.h> //for ioremap
#include <linux/platform_device.h> //for 平台设备
#include <linux/of.h>
#include <linux/signal.h>
#include <linux/interrupt.h>
#define led_ma 500 //主设备号(那一类设备)
//某些主设备号已经静态地分配给了大部分公用设备。见documentation/devices.txt 。 这里我们常使用250
#define led_mi 2 //次设备号(这类设备当中的具体哪个设备)
//目标:把字符设备改造为平台设备
//1. 把硬件信息,分离出去,放到设备树
//#define led3con 0x11000c20
//#define led3dat 0x11000c24
struct resource *led_res_con;
struct resource *led_res_dat;
struct resource *key1_res; //devices source pointer
struct fasync_struct *async_queue; /* 异步结构体指针,用于读 */
struct cdev led; //定义一个字符设备
#define led_magic 'l' //幻数,类似加密,避免误操作
#define led_on _iow(led_magic, 1, int)
#define led_off _iow(led_magic,2,int)
unsigned int *led3_con;
unsigned int *led3_dat;
int led_open (struct inode *inode, struct file *file)
{
led3_con = ioremap((phys_addr_t)led_res_con,1); //把硬件的物理地址转换为内核的虚拟地址
if(led3_con == null){
printk("led3_con ioremap fail\n");
return -1;
}
led3_dat = ioremap((phys_addr_t)led_res_dat,1);
if(led3_dat == null){
printk("led3_dat ioremap fail\n");
return -1;
}
writel((readl(led3_con) & (~0xf)) | 1,led3_con);//led3 and key2 had use same register;
writel(1,led3_dat);
printk("led_open ok\n");
return 0;
}
int led_release(struct inode *inode, struct file *file)
{
free_irq(key1_res->start, null);
printk("remove ok\n");
printk("led_release ok\n");
return 0;
}
//实现自定义的个性化操作
long led_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
switch(cmd) //解析ioctl 命令
{
case led_on: //点亮灯
printk("led on cmd=%d\n",cmd);
writel(1,led3_dat);
break;
case led_off: //灭灯
printk("led off cmd=%d\n",cmd);
writel(0,led3_dat);
break;
default:
printk("ioctl cmd no found\n");
break;
}
printk("led_ioctl end\n");
return 0;
}
irqreturn_t key_interrupt(int irqno, void *devid)
{
kill_fasync(&async_queue, sigio, poll_in); //发送sigio 异步通知信号
printk("irqno = %d\n", irqno);
return irq_handled;
}
int key_fasync (int fd,struct file *filp,int mode)
{
return fasync_helper(fd,filp,mode,&async_queue);//处理标志的变更
}
//3.实现设备的文件操作
struct file_operations fops={ //设备的文件操作
.owner = this_module,
.open = led_open,
.release = led_release,
.unlocked_ioctl = led_ioctl,
.fasync = key_fasync,
};
int led_init(struct platform_device *pdev) //把找到的平台设备的信息传进来
{
int ret;
dev_t dev_id= mkdev(led_ma, led_mi); //把主次设备号合并生成设备id
//1.注册设备号
ret =register_chrdev_region(dev_id,1,"test_led");
if(ret<0){
printk("regiser led fail\n ");
return -1;
}
//2.初始化字符设备
cdev_init(&led,&fops); //字符设备初始化
ret =cdev_add(&led,dev_id,1); //添加字符设备到系统中
if(ret<0){
printk("cdev add led fail\n ");
return -1;
}
led_res_con = pdev->resource[0].start;
led_res_dat = pdev->resource[1].start;
printk("led init go 1\n");
/*****************************************************/
key1_res = platform_get_resource(pdev, ioresource_irq, 0);
if (key1_res == null) {
printk("no resource !\n");
return -enodev;
}
ret = request_irq(key1_res->start, //硬件中断号 对应exynos4412-test4412.dts中test4412-key interrupts = <1 2>中的1
key_interrupt, //中断处理函数
key1_res->flags, //中断标志 irqf_disabled 在表示中断处理时屏蔽掉其它所有中断
"key2", //中断名称 在cat /proc/interrupts 克看到
null);
if (ret < 0) {
printk("failed request irq: irqno = irq_res->start");
return ret;
}
printk("key irq init ok\n");
/*********************************************************/
return 0; //返回值为零表示运行成功 ,负值表示失败
}
void led_exit(void)
{
dev_t dev_id= mkdev(led_ma, led_mi);
cdev_del(&led); //删除设备
unregister_chrdev_region(dev_id, 1); //取消注册
printk("led exit go\n");
}
//2.把内核模块的入口,改为platform_driver的入口
//module_init(led_init);
//module_exit(led_exit);
static const struct of_device_id machled[] = {
{ .compatible = "test,led3"}, //必须和设备树里的设备名字一样
{},
};
module_device_table(of, machled);
struct platform_driver led_platform_driver = {
.driver = {
.name = "test_led",
.owner = this_module,
.of_match_table = of_match_ptr(machled),
},
.probe = led_init,
.remove = led_exit,
};
module_platform_driver(led_platform_driver); //声明led_platform_driver为平台设备驱动的加载入口
module_license("dual bsd/gpl");
接下来是测试代码:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#define led_magic 'l'
//#define led_on 1
//#define led_off 2
#define led_on _iow(led_magic, 1, int)
#define led_off _iow(led_magic,2,int)
int flag = 0;
void input_handler (int signum)
{
printf("receive a signal from globalfifo, signalnum : %d\n",signum);
flag = 1;
}
int main(int argc, char **argv)
{
int fd = open("/dev/led", o_rdwr);
if (fd < 0) {
printf("open /dev/led fail\n");
return -1;
}
int oflags;
signal(sigio, input_handler); //让input_handler()处理sigio信号
fcntl(fd, f_setown, getpid()); //设置设备文件的所有者为本进程
oflags = fcntl(fd, f_getfl); // 会调用 驱动中的 .fasync
fcntl(fd, f_setfl, oflags | fasync); //fasync 设置支持异步通知模式
while(1)
{
sleep(100);
if (flag)
{
ioctl(fd, led_off); //ioctl 实现设备的个性化操作(可自定义命令)
printf("led_off\n"); //注意要加\n 否则打印信息可能没有
flag = 0;
}
}
}
最后测试下来还是有一些小问题,后续再看;
下一篇: JS构造函数和原型