/*
隻是寫了讀操作方面,寫操作還沒寫
阻塞型IO,在等待隊列上睡眠(初始化一個wait_queue 加入等待隊列頭的連結清單中)
非阻塞型IO ,判斷O_NONBLOCK标志
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/sched.h>
#define P_DEBUG(fmt, args...) printk("<kernel>[%s]:"fmt, __FUNCTION__, ##args)
#define DEVSIZE 512
//将裝置屬性封裝成一個結構體,表示一個字元裝置
struct test_dev_t {
char kbuf[DEVSIZE];
int curr_size;
int major;
int minor;
dev_t devno;
struct cdev test_cdev;
wait_queue_head_t queue;//定義等待隊列頭
}*dev;
/*
static dev_t devno;
static int major = 0;
static int minor = 0;
struct cdev test_cdev;
*/
//裝置驅動的操作方法
static int test_open(struct inode *,struct file *);
static int test_close(struct inode *,struct file *);
static ssize_t test_read(struct file *, char __user *, size_t , loff_t *);
static ssize_t test_write(struct file *, const char __user *, size_t, loff_t *);
struct file_operations test_fops = {
.open = test_open,
.release = test_close,
.read = test_read,
.write = test_write,
};
//打開裝置
static int test_open(struct inode *inode,struct file *filp)
{
P_DEBUG("dev open!\n");
struct test_dev_t *dev;
dev = container_of(inode->i_cdev,struct test_dev_t,test_cdev);
filp->private_data = dev;//利用裝置檔案的私有資料結構,儲存裝置相關的資料結構
return 0;
}
//釋放裝置
static int test_close(struct inode *inode,struct file *filp)
{
P_DEBUG("dev close!\n");
return 0;
}
//從裝置讀取
static ssize_t test_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
int ret;
struct test_dev_t *dev = filp->private_data;
if (*offset > DEVSIZE) {
return count ? -ENXIO : 0;
}
if (*offset + count > DEVSIZE) {
count = DEVSIZE - *offset;
}
//非阻塞,預設阻塞
filp->f_flags |= O_NONBLOCK;
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
P_DEBUG("wait read data....\n");
//阻塞,條件為假,讀程序睡眠
if (wait_event_interruptible(dev->queue,dev->curr_size > 0)) {
ret = -ERESTARTSYS;
goto RET;
}
if (copy_to_user(buf, dev->kbuf + *offset, count)) {
return -EFAULT;
}
else {
P_DEBUG("offset is [%d]\n", *offset);
ret = count;
dev->curr_size -= count;
*offset += count;
P_DEBUG("buf is [%s]\n", buf);
}
RET:
return ret;
}
//寫入裝置
static ssize_t test_write(struct file *filp, const char __user *buf, size_t count, loff_t *offset)
{
int ret;
struct test_dev_t *dev = filp->private_data;
P_DEBUG("dev->curr_size[%d]\n",dev->curr_size);
if (*offset > DEVSIZE) {
return count ? -ENXIO : 0;
}
if (*offset + count > DEVSIZE) {
count = DEVSIZE - *offset;
}
if (copy_from_user(dev->kbuf,buf,count)) {
return -EFAULT;
}
else {
ret = count;
dev->curr_size += count;
*offset += count;
P_DEBUG("dev->curr_size[%d]\n",dev->curr_size);
P_DEBUG("kbuf is [%s]\n", dev->kbuf);
}
wake_up_interruptible(&dev->queue);//喚醒睡眠的讀程序
return ret;
}
//初始化裝置屬性
static struct test_dev_t* dev_init(struct test_dev_t *dev)
{
dev = kmalloc(sizeof(struct test_dev_t),GFP_KERNEL);
memset(dev->kbuf,0,DEVSIZE);
dev->curr_size = 0;
dev->major = 0;
dev->minor = 0;
return dev;
}
static int __init test_init(void)
{
int ret;
dev = dev_init(dev);
//申請字元裝置号
if (dev->major) {
dev->devno = MKDEV(dev->major,dev->minor);
ret = register_chrdev_region(dev->devno,1,"test_dev");
}
else {//動态配置設定
ret = alloc_chrdev_region(&dev->devno,dev->minor,1,"test_dev");
dev->major = MAJOR(dev->devno);
dev->minor = MINOR(dev->devno);
}
if (ret < 0) {
P_DEBUG("register chrdev devno err!\n");
goto err_regDevno;
}
printk("major[%d] minor[%d]\n",dev->major,dev->minor);
cdev_init(&dev->test_cdev,&test_fops);
dev->test_cdev.owner = THIS_MODULE;
init_waitqueue_head(&dev->queue);//初始化等待隊列頭
ret = cdev_add(&dev->test_cdev,dev->devno,1);
if (ret < 0) {
P_DEBUG("add cdev error!\n");
goto err_addCdev;
}
return 0;
err_addCdev:
unregister_chrdev_region(dev->devno,1);
err_regDevno:
return ret;
}
static int __exit test_exit(void)
{
cdev_del(&dev->test_cdev);
unregister_chrdev_region(dev->devno,1);
return 0;
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("LL");
MODULE_VERSION("1.4");
測試
1.讀阻塞,等待寫資料的到來
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>
int main(void)
{
int fd ,ret;
char buf[10];
fd = open("/dev/test",O_RDWR);
if (fd <0) {
perror("open");
return -1;
}
ret = read(fd,buf,10);
if (ret == -1) {
perror("read");
printf("errno = %d\n",errno); //read 傳回的錯誤errno
}
else {
printf("buf is [%s]\n\n",buf);
}
close(fd);
return ret;
}
2. 寫資料,喚醒等待的讀
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
int main(void)
{
int fd;
char *buf = "1234567";
fd = open("/dev/test",O_RDWR);
if (fd < 0) {
perror("open");
return -1;
}
write(fd,buf,8);
close(fd);
return 0;
}