1. 程式人生 > >字符設備驅動程序之異步通知(韋大仙)

字符設備驅動程序之異步通知(韋大仙)

std arc null 問題 發現 處理 函數 arm-linux tile

讀取按鍵的方法:

(1)查詢的方式:極度耗費資源

(2)中斷的方式:如果沒有按鍵按下,read函數會一直的等待

(3)poll機制的引入:可以指定超時時間

上述三種方式有一個共同點:應用程序主動的去查詢。

問題:有沒有一種方式當有按鍵按下時,驅動程序通知應用程序去讀取。這就是本節所說的異步通知,該方式用信號的方式來實現的。

進程間發信號,例如:

kill -9 PID

kill這個程序是發送者,進程號為PID的進程為接受者,9就是發送的信號。接下來引入signal函數:

signal 函數是幹嘛的?

signal函數用於向系統註冊某一信號的函數,說白了,就是告訴系統,當某個信號發生時,執行這個函數。其原型如下:

函數原型
void (*signal(int signum,void(* handler)(int)))(int);   

或者:typedef void(*sig_t) ( int );   sig_t signal(int signum,sig_t handler);

看上去挺復雜的,其實就是涉及到了一個函數指針的問題。不管是什麽指針,它充其量就是個地址。只不過函數指針,指向一個函數而已。

具體用法:看下面一個程序signal.c

#include <stdio.h>

#include <signal.h>

void my_signal(int signum)

{

  static int count=0;

  printf("signal =%d, %d times\n ",signum,count++);

}

int main(int argc,char **argv)

{

  signal(SIGUSR1,my_signal);// 把函數名賦給函數指針即可。

  while(1)

  {

    sleep(1000);

  }

  return 0;

}

arm-linux-gcc -o signal.o signal.c

將signal.o拷到根文件系統下,在後臺執行

./signal.o &

然後用ps命令查看一下,進程號。假設是833,那麽現在我們發信號:

kill -USR1 833

信號的要點:

(1)註冊一個信號處理函數;

(2)誰發;

(3)發給誰;

(4)怎麽發;

只要清楚以上4點,就可以寫出一個有信號的應用程序來。

對於驅動程序我們的目標是:

按下按鈕鍵時,驅動程序通知應用程序。也需要具備4點要素:

(1)應用程序:要註冊一個信號處理函數;

(2)誰發:驅動;

(3)發給誰:應用程序,同時應用程序要告訴驅動自己的PID;

(4)怎麽發:驅動程序肯定要調用某個函數kill_fasync;

驅動程序:

#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 *fifthdrv_class;
static struct class_device *fifthdrv_class_dev;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;

volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;


static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中斷事件標誌, 中斷服務程序將它置1,fifth_drv_read將它清0 */
static volatile int ev_press = 0;

static struct fasync_struct *button_async;


struct pin_desc{
unsigned int pin;
unsigned int key_val;
};


/* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04 */
/* 鍵值: 松開時, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;

struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};


/*
* 確定按鍵值
*/
static irqreturn_t buttons_irq(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 = 0x80 | pindesc->key_val;
  }
  else
  {
    /* 按下 */
    key_val = pindesc->key_val;
  }

  ev_press = 1; /* 表示中斷發生了 */
  wake_up_interruptible(&button_waitq); /* 喚醒休眠的進程 */


/*發給誰,這個誰肯定是在button_async這個結構中定義的。這個結構裏面做了什麽初始化,在什麽地方做了初始化,在fifth_drv_fasync函數中,而fifth_drv_fasync又是什麽時候被調用的呢?根據file_operations這個結構體中的.fasync = fifth_drv_fasync可知,當應用程序調用fasync這個系統調用時,就會調用fifth_drv_fasync這個函數。顯然,應用程序調用fasync來設置發給誰,在應用程序肯定有所設置。*/

/*為了使設備支持異步通信機制,驅動程序中涉及以下3項工作

1、支持SETOWN命令,能在這個控制命令處理中設置filp->f_owner為對應進程ID。不過此項工作已由內核完成,驅動程序無需處理。應用程序會調用fcntl(fd,F_SETOWN,pid),把進程的pid告訴驅動程序。然後應用程序通過函數oflags=fcntl(fd,FGETFL)還會讀出oflags;當應用程序調用fcntl(fd,FSETFL,oflags| FASYNC)時,就會調用系統的fasync這個函數。

2、支持F_SETFL命令的處理,每當FASYNC標誌改變時,驅動程序中的fasync函數得以執行。驅動中要實現fasync函數

3、在設備資源可獲得時,調用kill_fasync()函數激發相應的信號

*/
  kill_fasync (&button_async, SIGIO, POLL_IN);                  

  return IRQ_RETVAL(IRQ_HANDLED);
}

static int fifth_drv_open(struct inode *inode, struct file *file)
{
/* 配置GPF0,2為輸入引腳 */
/* 配置GPG3,11為輸入引腳 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);

return 0;
}

ssize_t fifth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
  if (size != 1)
  return -EINVAL;

  /* 如果沒有按鍵動作, 休眠 */
  wait_event_interruptible(button_waitq, ev_press);

  /* 如果有按鍵動作, 返回鍵值 */
  copy_to_user(buf, &key_val, 1);
  ev_press = 0;

  return 1;
}


int fifth_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}

static unsigned fifth_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;
}

static int fifth_drv_fasync (int fd, struct file *filp, int on)
{
  printk("driver: fifth_drv_fasync\n");
  return fasync_helper (fd, filp, on, &button_async);
}


static struct file_operations sencod_drv_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
.open = fifth_drv_open,
.read = fifth_drv_read,
.release = fifth_drv_close,
.poll = fifth_drv_poll,
.fasync = fifth_drv_fasync,
};


int major;
static int fifth_drv_init(void)
{
major = register_chrdev(0, "fifth_drv", &sencod_drv_fops);

fifthdrv_class = class_create(THIS_MODULE, "fifth_drv");

fifthdrv_class_dev = class_device_create(fifthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;

gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;

return 0;
}

static void fifth_drv_exit(void)
{
unregister_chrdev(major, "fifth_drv");
class_device_unregister(fifthdrv_class_dev);
class_destroy(fifthdrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}


module_init(fifth_drv_init);

module_exit(fifth_drv_exit);

MODULE_LICENSE("GPL");

應用程序:


#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>


/* fifthdrvtest
*/
int fd;

void my_signal_fun(int signum)
{
  unsigned char key_val;
  read(fd, &key_val, 1);
  printf("key_val: 0x%x\n", key_val);
}

int main(int argc, char **argv)
{
  unsigned char key_val;
  int ret;
  int Oflags;

//需要明確my_signal_fun在什麽時候被調用?會在驅動程序的中斷服務中,發現有按鍵按下時,利用kill_fasync (&button_async, SIGIO, POLL_IN)給應用程序發送一個信號。信號會觸發應用程序調用信號處理函數my_signal_fun

  signal(SIGIO, my_signal_fun);//應用程序註冊了信號處理函數

  fd = open("/dev/buttons", O_RDWR);
  if (fd < 0)
  {
    printf("can‘t open!\n");
  }

  fcntl(fd, F_SETOWN, getpid());//告訴內核發給誰

  Oflags = fcntl(fd, F_GETFL);

  fcntl(fd, F_SETFL, Oflags | FASYNC);//改變fasync標記,最終會調用驅動中的fasync函數。


  while (1)
  {
  sleep(1000);
  }

  return 0;
}

字符設備驅動程序之異步通知(韋大仙)