/*======================================================================
A globalfifo driver as an example of char device drivers
This example is to introduce poll,blocking and non-blocking access
The initial developer of the original code is Baohua Song
<[email protected]>. All Rights Reserved.
======================================================================*/
#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>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#define GLOBALFIFO_SIZE 0x1000 /*全局fifo最大4K字節*/
#define FIFO_CLEAR 0x1 /*清0全局內存的長度*/
#define GLOBALFIFO_MAJOR 200 /*預設的globalfifo的主設備號*/
static int globalfifo_major = GLOBALFIFO_MAJOR;
module_param(globalfifo_major, int, S_IRUGO|S_IWUGO);
MODULE_PARM_DESC(globalfifo_major,"globalfifo major number");
/*globalfifo設備結構體*/
struct globalfifo_dev
{
struct cdev cdev; /*cdev結構體*/
unsigned int current_len; /*fifo有效數據長度*/
unsigned char mem[GLOBALFIFO_SIZE]; /*全局內存*/
struct semaphore sem; /*併發控制用的信號量*/
wait_queue_head_t r_wait; /*阻塞讀用的等待隊列頭*/
wait_queue_head_t w_wait; /*阻塞寫用的等待隊列頭*/
};
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)
{
return 0;
}
/* ioctl設備控制函數 */
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;
}
/*globalfifo讀函數*/
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, c urrent); //定義等待隊列
down(&dev->sem); //獲得信號量
add_wait_queue(&dev->r_wait, &wait); //增加等待隊列進入讀等待隊列頭
/* 等待FIFO非空 */
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;
}
/*globalfifo寫操作*/
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); //進入寫等待隊列頭
/* 等待FIFO非滿 */
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); //喚醒讀等待隊列
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,
.open = globalfifo_open,
.release = globalfifo_release,
};
/*初始化並註冊cdev*/
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); /*註銷cdev*/
kfree(globalfifo_devp); /*釋放設備結構體內存*/
unregister_chrdev_region(MKDEV(globalfifo_major, 0), 1); /*釋放設備號*/
}
module_init(globalfifo_init);
module_exit(globalfifo_exit);
MODULE_LICENSE("Dual BSD/GPL");