一、實驗說明
異步通知提供一種類似中斷的機制,當驅動程序可以訪問的時候主動告訴應用程序,類似中斷。“信號”爲此應運而生,信號類似於我們硬件上使用的“中斷”,
只不過信號是軟件層次上的。算是在軟件層次上對中斷的一種模擬,驅動可以通過主動向應用程序發送信號的方式來報告自己可以訪問了,應用程序獲取到信號以後就可以從驅動設備中讀取或者寫入數據了。整個過程就相當於應用程序收到了驅動發送過來了的一箇中斷,然後應用程序去響應這個中斷,在整個處理過程中應用程序並沒有去查詢驅動設備是否可以訪問,一切都是由驅動設備自己告訴給應用程序的。
阻塞、非阻塞、異步通知,這三種是針對不同的場合提出來的不同的解決方法,沒有優劣之分,在實際的工作和學習中,根據自己的實際需求選擇合適的處理方法即可。
異步通知的核心就是信號,在 arch/xtensa/include/uapi/asm/signal.h 文件中定義了 Linux 所支持的所有信號,這些信號如下所示:
示例代碼 Linux 信號
#define SIGHUP 1 /* 終端掛起或控制進程終止 */
#define SIGINT 2 /* 終端中斷(Ctrl+C 組合鍵) */
#define SIGQUIT 3 /* 終端退出(Ctrl+\組合鍵) */
#define SIGILL 4 /* 非法指令 */
#define SIGTRAP 5 /* debug 使用,有斷點指令產生 */
#define SIGABRT 6 /* 由 abort(3)發出的退出指令 */
#define SIGIOT 6 /* IOT 指令 */
#define SIGBUS 7 /* 總線錯誤 */
#define SIGFPE 8 /* 浮點運算錯誤 */
#define SIGKILL 9 /* 殺死、終止進程 */
#define SIGUSR1 10 /* 用戶自定義信號 1 */
#define SIGSEGV 11 /* 段違例(無效的內存段) */
#define SIGUSR2 12 /* 用戶自定義信號 2 */
#define SIGPIPE 13 /* 向非讀管道寫入數據 */
#define SIGALRM 14 /* 鬧鐘 */
#define SIGTERM 15 /* 軟件終止 */
#define SIGSTKFLT 16 /* 棧異常 */
#define SIGCHLD 17 /* 子進程結束 */
#define SIGCONT 18 /* 進程繼續 */
#define SIGSTOP 19 /* 停止進程的執行,只是暫停 */
#define SIGTSTP 20 /* 停止進程的運行(Ctrl+Z 組合鍵) */
#define SIGTTIN 21 /* 後臺進程需要從終端讀取數據 */
#define SIGTTOU 22 /* 後臺進程需要向終端寫數據 */
#define SIGURG 23 /* 有"緊急"數據 */
#define SIGXCPU 24 /* 超過 CPU 資源限制 */
#define SIGXFSZ 25 /* 文件大小超額 */
#define SIGVTALRM 26 /* 虛擬時鐘信號 */
#define SIGPROF 27 /* 時鐘信號描述 */
#define SIGWINCH 28 /* 窗口大小改變 */
#define SIGIO 29 /* 可以進行輸入/輸出操作 */
#define SIGPOLL SIGIO
/* #define SIGLOS 29 */
#define SIGPWR 30 /* 斷點重啓 */
#define SIGSYS 31 /* 非法的系統調用 */
#define SIGUNUSED 31 /* 未使用信號 */
在示例代碼 53.1.1.1 中的這些信號中,除了 SIGKILL(9)和 SIGSTOP(19)這兩個信號不能被忽略外,其他的信號都可以忽略。這些信號就相當於中斷號,不同的中斷號代表了不同的中斷,不同的中斷所做的處理不同,因此,驅動程序可以通過嚮應用程序發送不同的信號來實現不同的功能。(重點->重寫APP接收到信號的處理函數)
二、APP重寫信號處理函數舉例
/*把SIGINT信號的處理函數重定義爲sigint_handler函數*/
/*ctrl+c組合鍵將向當前控制檯執行的程序發送SIGINT信號*/
#include "stdlib.h"
#include "stdio.h"
#include "signal.h"
void sigint_handler(int num)
{
printf("\r\nSIGINT signal!\r\n");
exit(0);/*退出當前程序*/
}
int main(void)
{
signal(SIGINT, sigint_handler);
while(1);
return 0;
}
三、原理圖
四、設備樹
五、驅動程序
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/string.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/ide.h>
#define DeviceName "devicetree-key0-fasync" //設備驅動名稱
#define DeviceNodes 1 //設備節點個數
#define KEY_NUM 0X01
#define KEY0VALUE 0X01
#define INVAKEY 0XFF
struct irq_keydesc{
int gpio_num; /* io編號 */
int irq_num; /* 中斷號 */
unsigned char value; /* 鍵值 */
char name[25]; /* 名字 */
irqreturn_t (*handler) (int, void *); /* 中斷處理函數 */
};
/* 定義一個設備結構體 */
struct mod_struct {
dev_t devid; /* 設備號 */
int major; /* 主設備號 */
int minor; /* 次設備號 */
struct cdev cdev; /* cdev */
struct class *class; /* 類 */
struct device *device; /* 設備 */
struct device_node *nd; /* 設備節點 */
struct irq_keydesc irqkey0; /* 按鍵中斷 */
struct timer_list timer; /* 內核定時器 */
atomic_t keyvalue; /* 原子變量1->整形*/
atomic_t releasekey; /* 原子變量2->整形*/
wait_queue_head_t r_wait; /* 讀(讀函數中使用)等待隊列頭 */
struct fasync_struct *fasync_queue; /* 異步相關結構體 */
};
struct mod_struct mod_device; /* 定義一個設備 */
/*
* @description : 打開設備
* @param - inode : 傳遞給驅動的inode
* @param - filp : 設備文件,file結構體有個叫做private_data的成員變量
* 一般在open的時候將private_data指向設備結構體。
* @return : 0 成功;其他 失敗
*/
static int key0_interrupt_open(struct inode *inode, struct file *filp)
{
filp->private_data = &mod_device; /* 設置私有數據 */
return 0;
}
/*
* @description : 從設備讀取數據
* @param - filp : 要打開的設備文件(文件描述符)
* @param - buf : 返回給用戶空間的數據緩衝區
* @param - cnt : 要讀取的數據長度
* @param - offt : 相對於文件首地址的偏移
* @return : 讀取的字節數,如果爲負值,表示讀取失敗
*/
static ssize_t key0_interrupt_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char keyvalue;
unsigned char releasekey;
struct mod_struct *dev = filp->private_data;
keyvalue = atomic_read(&dev->keyvalue);
releasekey = atomic_read(&dev->releasekey);
if(releasekey) { /* 有效按鍵 */
if(keyvalue & 0x80) {
keyvalue &= ~0x80;
ret = copy_to_user(buf, &keyvalue, sizeof(keyvalue));
} else {
ret = -EINVAL;
goto data_error;
}
atomic_set(&dev->releasekey, 0); /* 按下標誌清零 */
} else {
ret = -EINVAL;
goto data_error;
}
data_error:
return ret;
}
/*
* @description : 向設備寫數據
* @param - filp : 設備文件,表示打開的文件描述符
* @param - buf : 要寫給設備寫入的數據
* @param - cnt : 要寫入的數據長度
* @param - offt : 相對於文件首地址的偏移
* @return : 寫入的字節數,如果爲負值,表示寫入失敗
*/
static ssize_t key0_interrupt_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
return 0;
}
static int key0_interrupt_fasync(int fd,struct file *filp,int on)
{
struct mod_struct *dev = filp->private_data;
return fasync_helper(fd,filp,on,&dev->fasync_queue);
}
/*
* @description : 關閉/釋放設備
* @param - filp : 要關閉的設備文件(文件描述符)
* @return : 0 成功;其他 失敗
*/
static int key0_interrupt_release(struct inode *inode, struct file *filp)
{
key0_interrupt_fasync(-1,filp,0);/*刪除異步通知*/
return 0;
}
/* 設備操作函數 */
static struct file_operations beep_fops = {
.owner = THIS_MODULE,
.open = key0_interrupt_open,
.read = key0_interrupt_read,
.write = key0_interrupt_write,
.fasync = key0_interrupt_fasync,
.release = key0_interrupt_release,
};
/* 按鍵中斷處理函數 */
static irqreturn_t key0_handler(int irq, void *dev_id)
{
struct mod_struct *dev = dev_id;
dev->timer.data = (volatile long)dev_id;
mod_timer(&dev->timer, jiffies + msecs_to_jiffies(20)); /* 20ms定時 */
return IRQ_HANDLED;
}
/* 定時器處理函數 */
static void timer_func(unsigned long arg) {
int value = 0;
struct mod_struct *dev = (struct mod_struct*)arg;
value = gpio_get_value(dev->irqkey0.gpio_num);
if(value == 0) { /* 按下 */
atomic_set(&dev->keyvalue, dev->irqkey0.value);/*=0x01*/
} else if(value == 1) { /* 釋放 */
atomic_set(&dev->keyvalue, 0X80 | (dev->irqkey0.value));
atomic_set(&dev->releasekey, 1); /* 完成的按鍵過程 */
}
if(atomic_read(&dev->releasekey)) {/* 按鍵有效 */
/*發送SIGIO信號*/
kill_fasync(&dev->fasync_queue,SIGIO,POLL_IN);
}
}
/*
* @description : 按鍵初始化函數
* @param : dev
* @return : 無
*/
static int key_initx(struct mod_struct *dev)
{
int ret = 0;
/* 1、獲取設備節點:/devicetree-leds-pincrl */
dev->nd = of_find_node_by_path("/devicetree-key0-interrupt");
if(dev->nd == NULL) {
printk("devicetree-key0-interrupt node not find!\r\n");
ret = -EINVAL;
goto fail_fd;
} else {
printk("devicetree-key0-interrupt node find!\r\n");
}
/* 2、從設備節點下的led-gpio屬性裏獲取gpio編號 */
dev->irqkey0.gpio_num = of_get_named_gpio(dev->nd,"key-gpio", 0);
if(dev->irqkey0.gpio_num < 0) {
printk("can't get key-gpio\r\n");
ret = -EINVAL;
goto fail_gpio;
}
printk("key-gpio num = %d\r\n", dev->irqkey0.gpio_num);
/*3、申請gpio(檢查是否被佔用)*/
sprintf(dev->irqkey0.name,"%s","key0-gpio-interrupt");
ret = gpio_request(dev->irqkey0.gpio_num,dev->irqkey0.name);
if(ret)
{
ret = -EBUSY;
printk("IO %d busy,can't request!\r\n",dev->irqkey0.gpio_num);
goto fail_ioreq;
}
/*4、設置key0爲輸入 */
ret = gpio_direction_input(dev->irqkey0.gpio_num);
if(ret < 0) {
printk("can't set gpio!\r\n");
goto fail_ioset;
}
/*5、根據IO編號獲取其中斷號*/
dev->irqkey0.irq_num=gpio_to_irq(dev->irqkey0.gpio_num);
#if 0
dev->irqkey0.irq_num = irq_of_parse_and_map(dev->nd,0);
#endif
/*6、設置中斷函數*/
dev->irqkey0.handler = key0_handler;
dev->irqkey0.value = KEY0VALUE;
/*7、註冊中斷*/
ret = request_irq(dev->irqkey0.irq_num,
dev->irqkey0.handler,
IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,
dev->irqkey0.name,
&mod_device);
if(ret){
printk("irq %d request failed!\r\n", dev->irqkey0.irq_num);
goto fail_irq;
}
/*8、初始化定時器*/
init_timer(&mod_device.timer);
mod_device.timer.function = timer_func;
return 0;
fail_irq:
fail_ioset:
gpio_free(dev->irqkey0.gpio_num);
fail_ioreq:
fail_gpio:
fail_fd:
return ret;
}
/*
* @description : 驅動出口函數
* @param : 無
* @return : 無
*/
static int __init key0_interrupt_init(void)
{
int ret = 0;
/* 註冊字符設備驅動 */
/* 1、創建設備號 */
if (mod_device.major)
{
mod_device.devid = MKDEV(mod_device.major,0);//設備號起始點(major,0)
//設備號起始點開始,申請DeviceNodes個設備號
ret = register_chrdev_region(mod_device.devid,DeviceNodes,DeviceName);
}
else {
ret = alloc_chrdev_region(&mod_device.devid,0,DeviceNodes,DeviceName); /* 申請設備號 */
mod_device.major = MAJOR(mod_device.devid); /* 獲取分配號的主設備號 */
mod_device.minor = MINOR(mod_device.devid); /* 獲取分配號的次設備號 */
}
if(ret < 0){ printk("設備號申請失敗\r\n");goto fail_chrdev; }
printk("mod_device major=%d,minor=%d\r\n",mod_device.major,mod_device.minor);
/* 2、初始化cdev並添加cdev */
mod_device.cdev.owner = THIS_MODULE;
cdev_init(&mod_device.cdev,&beep_fops);
ret = cdev_add(&mod_device.cdev,mod_device.devid,DeviceNodes);
if(ret < 0){ printk("添加cdev失敗!\r\n");goto fail_cdev; }
/* 3、創建類 */
mod_device.class = class_create(THIS_MODULE,DeviceName);
if (IS_ERR(mod_device.class)) { ret = PTR_ERR(mod_device.class);goto fail_class; }
/* 4、創建設備節點*/
mod_device.device = device_create(mod_device.class,NULL,mod_device.devid,NULL,DeviceName);/*和設備設爲一個名字*/
if (IS_ERR(mod_device.device)) { ret = PTR_ERR(mod_device.device);goto fail_device; }
/* 5、初始化IO */
ret = key_initx(&mod_device);
if(ret < 0) {
goto fail_keyinit;
}
/* 6、初始化原子變量 */
atomic_set(&mod_device.keyvalue, INVAKEY);/*mod_device.keyvalue=INVAKEY*/
atomic_set(&mod_device.releasekey, 0); /*mod_device.releasekey=0*/
/* 7、初始化等待隊列頭 */
init_waitqueue_head(&mod_device.r_wait);
/*X、模塊加載失敗處理部分*/
fail_keyinit:
device_destroy(mod_device.class,mod_device.devid);
fail_device:
class_destroy(mod_device.class);
fail_class:
cdev_del(&mod_device.cdev);
fail_cdev:
unregister_chrdev_region(mod_device.devid,DeviceNodes);
fail_chrdev:
return ret;
}
/*
* @description : 驅動出口函數
* @param : 無
* @return : 無
*/
static void __exit key0_interrupt_exit(void)/*按照相反順序註銷*/
{
/*1、釋放中斷*/
free_irq(mod_device.irqkey0.irq_num, &mod_device);
/*2、內核定定時器註銷部分*/
del_timer_sync(&mod_device.timer);/* 刪除timer */
/*3、LED設備註銷部分*/
gpio_free(mod_device.irqkey0.gpio_num);/*註銷gpio*/
/*4、設備驅動註銷部分*/
device_destroy(mod_device.class,mod_device.devid);/*註銷設備節點*/
class_destroy(mod_device.class);/*註銷類*/
cdev_del(&mod_device.cdev);/*註銷cdev*/
unregister_chrdev_region(mod_device.devid,DeviceNodes);/*註銷chrdev*/
}
module_init(key0_interrupt_init);
module_exit(key0_interrupt_exit);
MODULE_LICENSE("GPL");
六、APP測試程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <signal.h>
/*
*argc:應用程序參數個數
*argv[]:具體的參數內容,字符串形式
*/
int fd;
static void sigio_signal_func(int num)
{
int err;
unsigned int keyvalue = 0;
printf("num = %d \r\n",num);
err = read(fd,&keyvalue,sizeof(keyvalue));
if(err < 0){
printf("error = %d \r\n",err);
}
else
{
printf("sigio singal! keyvalue = %d \r\n",keyvalue);
}
}
int main(int argc, char *argv[])
{
int flags = 0;
char *filename;
if(argc != 2) {
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename,O_RDWR);/*open函數默認是阻塞方式訪問設備*/
if(fd < 0) {
printf("file %s open failed!\r\n", filename);
return -1;
}
signal(SIGIO,sigio_signal_func);/*重新定義SIGIO信號處理函數*/
fcntl(fd,F_SETOWN,getpid()); /*設置當前進程可接收到fd的SINIO信號*/
flags = fcntl(fd, F_GETFL); /*獲取文件描述符fd的狀態*/
fcntl(fd,F_SETFL,flags|FASYNC); /*設置文件描述符fd的狀態(開啓異步通知)*/
while(1) {
sleep(2);
}
close(fd);
return 0;
}