天天看点

支持异步通知的设备驱动和应用

驱动:

#include <linux/module.h>

#include <linux/types.h>

#include <linux/fs.h>

#include <linux/errno.h>

#include <linux/mm.h>

#include <linux/sched.h>

#include <linux/init.h>

#include <linux/cdev.h>

#include <asm/io.h>

#include <asm/system.h>

#include <asm/uaccess.h>

#include <linux/poll.h>

#define GLOBALFIFO_SIZE    0x1000   

#define FIFO_CLEAR 0x1 

#define GLOBALFIFO_MAJOR 253   

static int globalfifo_major = GLOBALFIFO_MAJOR;

struct globalfifo_dev                                    

{                                                       

  struct cdev cdev;                       

  unsigned int current_len;   

  unsigned char mem[GLOBALFIFO_SIZE];        

  struct semaphore sem;           

  wait_queue_head_t r_wait;     

  wait_queue_head_t w_wait;    

  struct fasync_struct *async_queue;

};

struct globalfifo_dev *globalfifo_devp;

int globalfifo_open(struct inode *inode, struct file *filp)

{

  filp->private_data = globalfifo_devp;

  return 0;

}

int globalfifo_release(struct inode *inode, struct file *filp)

{

  globalfifo_fasync( - 1, filp, 0);

  return 0;

}

static int globalfifo_ioctl(struct inode *inodep, struct file *filp, unsigned

  int cmd, unsigned long arg)

{

  struct globalfifo_dev *dev = filp->private_data;

  switch (cmd)

  {

    case FIFO_CLEAR:

        down(&dev->sem); //获得信号量       

      dev->current_len = 0;

      memset(dev->mem,0,GLOBALFIFO_SIZE);

      up(&dev->sem); //释放信号量

      printk(KERN_INFO "globalfifo is set to zero/n");     

      break;

    default:

      return  - EINVAL;

  }

  return 0;

}

static unsigned int globalfifo_poll(struct file *filp, poll_table *wait)

{

  unsigned int mask = 0;

  struct globalfifo_dev *dev = filp->private_data;

  down(&dev->sem);

  poll_wait(filp, &dev->r_wait, wait);

  poll_wait(filp, &dev->w_wait, wait); 

  if (dev->current_len != 0)

  {

    mask |= POLLIN | POLLRDNORM;

  }

  if (dev->current_len != GLOBALFIFO_SIZE)

  {

    mask |= POLLOUT | POLLWRNORM;

  }

  up(&dev->sem);

  return mask;

}

static int globalfifo_fasync(int fd, struct file *filp, int mode)

{

    struct globalfifo_dev *dev = filp->private_data;

    return fasync_helper(fd, filp, mode, &dev->async_queue);

}

static ssize_t globalfifo_read(struct file *filp, char __user *buf, size_t count,

  loff_t *ppos)

{

  int ret;

  struct globalfifo_dev *dev = filp->private_data; //获得设备结构体指针

  DECLARE_WAITQUEUE(wait, current); //定义等待队列

  down(&dev->sem); //获得信号量

  add_wait_queue(&dev->r_wait, &wait); //进入读等待队列头

  if (dev->current_len == 0)

  {

    if (filp->f_flags &O_NONBLOCK)

    {

      ret =  - EAGAIN;

      goto out;

    }

    __set_current_state(TASK_INTERRUPTIBLE); //改变进程状态为睡眠

    up(&dev->sem);

    schedule(); //调度其他进程执行

    if (signal_pending(current))

    //如果是因为信号唤醒

    {

      ret =  - ERESTARTSYS;

      goto out2;

    }

    down(&dev->sem);

  }

  if (count > dev->current_len)

    count = dev->current_len;

  if (copy_to_user(buf, dev->mem, count))

  {

    ret =  - EFAULT;

    goto out;

  }

  else

  {

    memcpy(dev->mem, dev->mem + count, dev->current_len - count); //fifo数据前移

    dev->current_len -= count; //有效数据长度减少

    printk(KERN_INFO "read %d bytes(s),current_len:%d/n", count, dev->current_len);

    wake_up_interruptible(&dev->w_wait); //唤醒写等待队列

    ret = count;

  }

  out: up(&dev->sem); //释放信号量

  out2:remove_wait_queue(&dev->w_wait, &wait); //从附属的等待队列头移除

  set_current_state(TASK_RUNNING);

  return ret;

}

static ssize_t globalfifo_write(struct file *filp, const char __user *buf,

  size_t count, loff_t *ppos)

{

  struct globalfifo_dev *dev = filp->private_data; //获得设备结构体指针

  int ret;

  DECLARE_WAITQUEUE(wait, current); //定义等待队列

  down(&dev->sem); //获取信号量

  add_wait_queue(&dev->w_wait, &wait); //进入写等待队列头

  if (dev->current_len == GLOBALFIFO_SIZE)

  {

    if (filp->f_flags &O_NONBLOCK)

    //如果是非阻塞访问

    {

      ret =  - EAGAIN;

      goto out;

    }

    __set_current_state(TASK_INTERRUPTIBLE); //改变进程状态为睡眠

    up(&dev->sem);

    schedule(); //调度其他进程执行

    if (signal_pending(current))

    //如果是因为信号唤醒

    {

      ret =  - ERESTARTSYS;

      goto out2;

    }

    down(&dev->sem); //获得信号量

  }

  if (count > GLOBALFIFO_SIZE - dev->current_len)

    count = GLOBALFIFO_SIZE - dev->current_len;

  if (copy_from_user(dev->mem + dev->current_len, buf, count))

  {

    ret =  - EFAULT;

    goto out;

  }

  else

  {

    dev->current_len += count;

    printk(KERN_INFO "written %d bytes(s),current_len:%d/n", count, dev

      ->current_len);

    wake_up_interruptible(&dev->r_wait); //唤醒读等待队列

    if (dev->async_queue)

       kill_fasync(&dev->async_queue, SIGIO, POLL_IN);

    ret = count;

  }

  out: up(&dev->sem); //释放信号量

  out2:remove_wait_queue(&dev->w_wait, &wait); //从附属的等待队列头移除

  set_current_state(TASK_RUNNING);

  return ret;

}

static const struct file_operations globalfifo_fops =

{

  .owner = THIS_MODULE,

  .read = globalfifo_read,

  .write = globalfifo_write,

  .ioctl = globalfifo_ioctl,

  .poll = globalfifo_poll,

  .open = globalfifo_open,

  .release = globalfifo_release,

  .fasync = globalfifo_fasync,

};

static void globalfifo_setup_cdev(struct globalfifo_dev *dev, int index)

{

  int err, devno = MKDEV(globalfifo_major, index);

  cdev_init(&dev->cdev, &globalfifo_fops);

  dev->cdev.owner = THIS_MODULE;

  dev->cdev.ops = &globalfifo_fops;

  err = cdev_add(&dev->cdev, devno, 1);

  if (err)

    printk(KERN_NOTICE "Error %d adding LED%d", err, index);

}

int globalfifo_init(void)

{

  int ret;

  dev_t devno = MKDEV(globalfifo_major, 0);

  if (globalfifo_major)

    ret = register_chrdev_region(devno, 1, "globalfifo");

  else 

  {

    ret = alloc_chrdev_region(&devno, 0, 1, "globalfifo");

    globalfifo_major = MAJOR(devno);

  }

  if (ret < 0)

    return ret;

  globalfifo_devp = kmalloc(sizeof(struct globalfifo_dev), GFP_KERNEL);

  if (!globalfifo_devp)   

  {

    ret =  - ENOMEM;

    goto fail_malloc;

  }

  memset(globalfifo_devp, 0, sizeof(struct globalfifo_dev));

  globalfifo_setup_cdev(globalfifo_devp, 0);

  init_MUTEX(&globalfifo_devp->sem);  

  init_waitqueue_head(&globalfifo_devp->r_wait);

  init_waitqueue_head(&globalfifo_devp->w_wait);

  return 0;

  fail_malloc: unregister_chrdev_region(devno, 1);

  return ret;

}

void globalfifo_exit(void)

{

  cdev_del(&globalfifo_devp->cdev);  

  kfree(globalfifo_devp);    

  unregister_chrdev_region(MKDEV(globalfifo_major, 0), 1);

}

MODULE_AUTHOR("Song Baohua");

MODULE_LICENSE("Dual BSD/GPL");

module_param(globalfifo_major, int, S_IRUGO);

module_init(globalfifo_init);

module_exit(globalfifo_exit);

应用:

#include <sys/types.h>

#include <sys/stat.h>

#include <stdio.h>

#include <fcntl.h>

#include <signal.h>

#include <unistd.h>

void input_handler(int signum)

{

  printf("receive a signal from globalfifo,signalnum:%d/n",signum);

}

main()

{

  int fd, oflags;

  fd = open("/dev/globalfifo", O_RDWR, S_IRUSR | S_IWUSR);

  if (fd !=  - 1)

  {

    //启动信号驱动机制

    signal(SIGIO, input_handler); //让input_handler()处理SIGIO信号

    fcntl(fd, F_SETOWN, getpid());

    oflags = fcntl(fd, F_GETFL);

    fcntl(fd, F_SETFL, oflags | FASYNC);

    while(1)

    {

        sleep(100);

    }

  }

  else

  {

    printf("device open failure/n");

  }

}

在板子上测试:

 $ insmod globalfifo_async.ko

$ cat /proc/devices

Character devices:

  1 mem

  2 pty

  3 ttyp

  4 /dev/vc/0

  4 tty

  5 /dev/tty

  5 /dev/console

  5 /dev/ptmx

  7 vcs

 10 misc

 13 input

 14 sound

 29 fb

 81 video4linux

 89 i2c

 90 mtd

128 ptm

136 pts

180 usb

188 ttyUSB

204 s3c2410_serial

253 globalfifo

Block devices:

$ mknod /dev/globalfifo c 253 1

$ ./asyncmonitor &

执行本人门4中的read, write进程,结果如下

 $ ./read &

/ $

********************

user read devices:

/ $ ./write &

/ $

user write devices:

written 1024 bytes(s),current_len:1024

receive a signal from globalfiread 1024 bytes(s),current_len:0

written 1024 bytes(s),current_len:1024

written 1024 bytes(s),current_len:2048

written 1024 bytes(s),current_len:3072

written 1024 bytes(s),current_len:4096

fo,signalnum:29

read count= :1024

write count is: 1024

user write devices:

receive a signal from globalfifo,signalnum:29

write count is: 1024

user write devices:

receive a signal from globalfifo,signalnum:29

write count is: 1024

user write devices:

receive a signal from globalfifo,signalnum:29

write count is: 1024

user write devices:

receive a signal from globalfifo,signalnum:29

write count is: 1024

user write devices:

********************

read 2048 bytes(s),current_len:2048

written 1024 bytes(s),current_len:3072

written 1024 bytes(s),current_len:4096

user read devices:

read count= :2048

write count is: 1024

user write devices:

receive a signal from globalfifo,signalnum:29

write count is: 1024

继续阅读