t_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;
}
}
}
最后测试下来还是有一些小问题,后续再看;