linux 字符驱动完整框架(poll,async,waitqueue,nonblock等)

一个linux内核驱动的完整框架,包含了能遇到的大部分内容,例如timer, poll, async, waitqueue, nonblock等等,不过基本上没啥大用,就是用来熟悉基础的,正常的驱动不太会这么简单。

后面还包含了makefile,直接在ubuntu上都可以编译运行。

另外还有应用程序在加上了,直接在ubuntu上gcc编译即可。


#include "linux/init.h"
#include "linux/kernel.h"
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kfifo.h>
#include <asm/io.h>
#include <asm/uaccess.h>
//#include <asm/mach/map.h>
#include "linux/poll.h"
#include "linux/timer.h"
#include "linux/fcntl.h"
#include "linux/fs.h"
#include "linux/syscalls.h"

#define VSER_DEV_NAME "vser"
//#define VSER_MAJOR 255
#define VSER_MAJOR 0
#define VSER_MINOR 0
#define VSER_DEV_CNT 1

struct vser_t
{
    struct cdev vsdev;
    dev_t dev;
    int major;
    int minor;
    struct device *device;
    struct class  *class;
    wait_queue_head_t rwqh;
    wait_queue_head_t wwqh;
    struct timer_list timer;
    struct fasync_struct *fasync;
}
g_vser_dev;

DEFINE_KFIFO(vsfifo,char,32);


static int vser_fasync(int fd, struct file *filp, int on)
{
    struct vser_t *pvser_dev=(struct vser_t*)filp->private_data;

    return fasync_helper(fd, filp, on, &pvser_dev->fasync);
}


static int vser_open(struct inode *inode,struct file *filp)
{
    printk("vser_open\n");

    filp->private_data=&g_vser_dev;

    return 0;
}


static int vser_release(struct inode *inode,struct file *filp)
{
    int ret;
   
    ret=vser_fasync(-1, filp, 0);
    printk("vser_release %d\n", ret);

    return ret;
}

static ssize_t vser_read(struct file *filp,char __user *buf,size_t count, loff_t *pos)
{
    unsigned int copied = 0;
    int ret;
    struct vser_t *pvser_dev=(struct vser_t*)filp->private_data;

    //non-blocking read process
    if(kfifo_is_empty(&vsfifo))
    {
        printk("kfifo is empty\n");
        if(filp->f_flags & O_NONBLOCK)
        {
            printk("nonblock return\n");
            return -EAGAIN;
        }  
        printk("blocking read\n");
    }

    if(wait_event_interruptible(pvser_dev->rwqh, !kfifo_is_empty(&vsfifo)))
    {
        printk("kfifo is empty, wait_queue interrupted by signal\n");
        return -ERESTARTSYS;
    }
    
    ret=kfifo_to_user(&vsfifo,buf,count,&copied);
    printk("real read %d %d\n", ret, copied);

    if(!kfifo_is_full(&vsfifo)) {
        printk("kfifo is write available, wakeup wwqh\n");
        wake_up_interruptible(&pvser_dev->wwqh);
    }

    return ret==0 ? copied: ret;
}

static ssize_t vser_write(struct file *filp,const char __user *buf,size_t count,loff_t *pos)
{
    unsigned int copied = 0;
    int ret;
    struct vser_t *pvser_dev=(struct vser_t*)filp->private_data;

    //nonblocking write process
    if(kfifo_is_full(&vsfifo))
    {
        printk("kfifo is full\n");
        if(filp->f_flags & O_NONBLOCK)
        {
            printk("nonblock write return\n");
            return -EAGAIN;
        }
        printk("blocking write\n");
    }

    if(wait_event_interruptible(pvser_dev->wwqh, !kfifo_is_full(&vsfifo)))
    {
        printk("kfifo wwqh interrupted by signal\n");
        return -ERESTARTSYS;
    }

    ret=kfifo_from_user(&vsfifo,buf,count,&copied);
    printk("real write %d %d\n", ret, copied);

    if(!kfifo_is_empty(&vsfifo))
    {
        printk("wakeup rwqh in write\n");
        wake_up_interruptible(&pvser_dev->rwqh);
    }

    if(!kfifo_is_empty(&vsfifo))
    {
        kill_fasync(&g_vser_dev.fasync, SIGIO, POLL_IN);
        printk("kill_fasync for read\n");
    }

    return ret==0?copied:ret;
}


static unsigned int vser_poll(struct file *filp, struct poll_table_struct *p)
{
    int mask=0;
    struct vser_t *pvser_dev=(struct vser_t*)filp->private_data;

    poll_wait(filp, &pvser_dev->rwqh, p);
    poll_wait(filp, &pvser_dev->wwqh, p);

    if(!kfifo_is_empty(&vsfifo))
    {
        mask|=POLLIN|POLLRDNORM;
    }
    if(!kfifo_is_full(&vsfifo))
    {
        mask|=POLLOUT|POLLWRNORM;
    }

    return mask;
}

static struct file_operations vser_ops = {
    .owner = THIS_MODULE,
    .open = vser_open,
    .release = vser_release,
    .read = vser_read,
    .write = vser_write,
    .poll = vser_poll,
    .fasync = vser_fasync,
};


static void timer_callback(struct timer_list *timer)
{
    /*
    struct vser_t *pvser=(struct vser_t*)arg;
    static int i=0;
    printk("timer:%d\n", i++);
    mod_timer(&pvser->timer, jiffies + msecs_to_jiffies(5000));*/

    static int i=0;
    printk("timer:%d\n", i++);
    mod_timer(timer, jiffies + msecs_to_jiffies(5000));
}

extern long sys_chmod(const char __user *filename, umode_t mode);
#define filename "/dev/"VSER_DEV_NAME

static int __init vser_init(void)
{
    int ret;
    //int major,minor;

    if(VSER_MAJOR == 0)
    {
        ret=alloc_chrdev_region(&g_vser_dev.dev, VSER_MINOR, 1, VSER_DEV_NAME);
        if(ret)
        goto reg_err;
    }
    else
    {
        g_vser_dev.dev = MKDEV(VSER_MAJOR,VSER_MINOR);
        ret = register_chrdev_region(g_vser_dev.dev,VSER_DEV_CNT,VSER_DEV_NAME);
        if (ret)
        goto reg_err;
    }

    //dev_t
    g_vser_dev.major=MAJOR(g_vser_dev.dev);
    g_vser_dev.minor=MINOR(g_vser_dev.dev);
    printk("major: %d minor: %d\n",g_vser_dev.major,g_vser_dev.minor);

    //cdev
    cdev_init(&g_vser_dev.vsdev,&vser_ops);
    g_vser_dev.vsdev.owner = THIS_MODULE;
    ret = cdev_add(&g_vser_dev.vsdev,g_vser_dev.dev,VSER_DEV_CNT);
    if (ret)
    goto add_err;

    //class
    g_vser_dev.class = class_create(THIS_MODULE, VSER_DEV_NAME); 
    if (IS_ERR(g_vser_dev.class)) { 
        ret=PTR_ERR(g_vser_dev.class); 
        goto cls_err;
    }

    //device
    g_vser_dev.device = device_create(g_vser_dev.class, NULL, g_vser_dev.dev, NULL, VSER_DEV_NAME); 
    if (IS_ERR(g_vser_dev.device)) { 
        ret=PTR_ERR(g_vser_dev.device);
        goto dev_err;
    }

    //ret=sys_chmod(filename, 0777);    
    //printk("chmod %s ret=%d\n", filename, ret);

    //wait_queue
    init_waitqueue_head(&g_vser_dev.rwqh);
    init_waitqueue_head(&g_vser_dev.wwqh);

    //timer
    /*
    init_timer(&g_vser_dev.timer);
    g_vser_dev.timer.function=timer_callback;
    g_vser_dev.timer.data=(unsigned long)&g_vser_dev;*/
    timer_setup(&g_vser_dev.timer, timer_callback, 0);
    mod_timer(&g_vser_dev.timer, jiffies + msecs_to_jiffies(5000));
    printk("start timer\n");

    printk("vser_init\n");
    return 0;

dev_err:
    class_destroy(g_vser_dev.class);
cls_err:
    cdev_del(&g_vser_dev.vsdev);
add_err:
    unregister_chrdev_region(g_vser_dev.dev,VSER_DEV_CNT);
reg_err:
    printk("vser_init failed\n");
    return ret;
}


static void __exit vser_exit(void)
{
    //dev_t dev;
    //dev = MKDEV(VSER_MAJOR,VSER_MINOR);
    int major,minor;
    major=MAJOR(g_vser_dev.dev);
    minor=MINOR(g_vser_dev.dev);
    printk("major: %d minor: %d\n",major,minor);

    cdev_del(&g_vser_dev.vsdev);
    unregister_chrdev_region(g_vser_dev.dev,VSER_DEV_CNT);

    device_destroy(g_vser_dev.class, g_vser_dev.dev); 
    class_destroy(g_vser_dev.class);

    del_timer_sync(&g_vser_dev.timer);

    printk("vser exit\n");
}


module_init(vser_init);
module_exit(vser_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("oushaojun");
MODULE_DESCRIPTION("A simple character device driver");
MODULE_ALIAS("virtual-serial");







makefile内容:


ifeq ($(KERNELRELEASE),)
	ifeq ($(ARCH),arm)
		KERNELDIR ?= /home/farsight/fs4412/linux-3.14.25-fs4412
		ROOTFS ?= /nfs/rootfs
	else
		KERNELDIR ?= /lib/modules/$(shell uname -r)/build
	endif
	PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) INSTALL_MOD_PATH=$(ROOTFS) modules_install

clean:
	rm -rf *.o *.ko.*.cmd *.mod.* modules.order Module.symvers.tmp_versi ons
else
	obj-m := vser.o
endif

应用程序,用来测试驱动是否工作正常:


#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "sys/mman.h"
#include "fcntl.h"
#include "errno.h"
#include "unistd.h"
#include "poll.h"
#include "sys/select.h"
#include "sys/time.h"
#include "linux/ioctl.h"
#include "signal.h"

static int fd=0;


static void sigio_signal_handler(int signum)
{
    int err=0;
    __uint8_t buffer[100]={0};

    err=read(fd, buffer, sizeof(buffer));
    if(err<0){
        perror("async read");
    }
    else{
        printf("async read success %d %s\n", err, (char*)buffer);
    }
}

int main(int argc, char **argv)
{
    int ret;
    int flags;
    char buffer[1024];

    fd=open("/dev/vser", O_RDWR/*|O_NONBLOCK*/);
    if(fd==-1)
    {
        perror("open");
        return -1;
    }
    else
    {
        printf("open ok\n");
    }

    //sigio callback
    signal(SIGIO, sigio_signal_handler);
    //set own
    fcntl(fd, F_SETOWN, getpid());
    //enable async
    flags=fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, flags|FASYNC);

    while(1) {
        sleep(1);
    }

    close(fd);
    printf("close\n");

    exit(0);
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值