将globalfifo_platform改写成misc字符设备
由于linux 驱动倾向于分层设计,所以各个具体的设备都可以找到它归属的类型,从而套到它相应的架构里面去,并且只需要实现最底层的那一部分。但是,也有部分类似globalmem、globalfifo的字符设备,确实不知道它属于沈类型,我们一般推荐大家采用miscdevice框架结构。miscdevice本质上也是字符设备,只是在miscdevice核心层的misc_init()中,通过register_chrdev(MISC_MAJOR,“misc”,&misc_fops)注册了字符设备,而具体miscdevice 实列调用misc_register()的时候又自动完成了device_create()、获取动态次设备号的动作
上文摘抄自《Linux设备驱动开发详解:基于最新的Linux 4.0内核》
注意:
1.在misc中,设备的fops中的open函数,不能指定filp->private_data为&dev->miscdev
这样会导致
struct globalfifo_dev *dev = container_of(filp->private_data,struct globalfifo_dev,miscdev);
获取不到 ,引发死机
2. 同样需要在/dev下,根据驱动反馈的次设备号,创建设备节点
/*
* 验证阻塞与非阻塞操作
*此版本添加了互斥体
*进程间互斥访问共享资源(globalfifo)
*
*文件中,读fifo将唤醒写filo的进程(如果之前fifo正好是满的)
* 写fifo将唤醒读fifo的进程(如果之前fifo正好是空的)
*
*将globalfifo作为platform设备
*完成两个工作
* 1.将globalfifo移植为platform驱动
* 2.在板文件中添加globalfifo这个platform设备
*
*添加模块驱动作为misc
*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#define GLOBALFIFO_SIZE 0x1000
//#define MEM_CLEAR 0x1
#define GLOBALFIFO_MAGIC 'f'
#define MEM_CLEAR _IO(GLOBALFIFO_MAGIC,0)
#define GLOBALFIFO_MAJOR 231
#define DEVICE_NUM 1 //同主设备号,不同次设备号有1个
static int globalfifo_major = GLOBALFIFO_MAJOR;
module_param(globalfifo_major,int ,S_IRUGO);
struct globalfifo_dev
{
struct cdev cdev;
unsigned int current_len; //表示目前的FIFO中的有效长度,如果为0,表示FIFO为空,如果是GLOBALFIFO_SIZE,表示FIFO满了
unsigned char mem[GLOBALFIFO_SIZE];
struct mutex globalfifo_mutex;
wait_queue_head_t r_wait;
wait_queue_head_t w_wait;
struct miscdevice miscdev;
};
struct globalfifo_dev * globalfifo_devp;
static loff_t globalfifo_llseek(struct file *filp,loff_t offset, int orig)
{
loff_t ret = 0;
switch (orig)
{
case 0://从文件开头位置seek
if (offset < 0 || (unsigned int)offset > GLOBALFIFO_SIZE)
{
ret = -EINVAL;
break;
}
filp->f_pos = (unsigned int)offset;
ret = filp->f_pos;
break;
case 1://从文件当前位置seek
if ((filp->f_pos + offset) < 0 || (filp->f_pos + offset) > GLOBALFIFO_SIZE)
{
ret = -EINVAL;
break;
}
filp->f_pos = (unsigned int)offset;
ret = filp->f_pos;
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
ssize_t globalfifo_write(struct file *filp, const char __user *buf,
size_t size, loff_t *pos)
{
unsigned int count = size;
int ret = 0;
struct globalfifo_dev *dev = container_of(filp->private_data,struct globalfifo_dev,miscdev);
// if (p > GLOBALFIFO_SIZE)
// return 0;
// if (count > GLOBALFIFO_SIZE - p)
// count = GLOBALFIFO_SIZE - p;
DECLARE_WAITQUEUE(wait,current);
mutex_lock(&dev->globalfifo_mutex);
add_wait_queue(&dev->w_wait,&wait);
printk(KERN_INFO "globalfifo_devp->current_len is %d\n",globalfifo_devp->current_len);
while (dev->current_len == GLOBALFIFO_SIZE)//如果当前的fifo为满,则进程阻塞
{
if(filp->f_flags & O_NONBLOCK)//判断是否以非阻塞打开的设备文件(/dev/globalfifo)
{
//非阻塞就直接返回了
ret = -EAGAIN;
goto out;
}
//阻塞就让本函数休眠
__set_current_state(TASK_INTERRUPTIBLE);
mutex_unlock(&dev->globalfifo_mutex);
schedule();
if(signal_pending(current))//判断是否是信号唤醒的
{
//是信号唤醒的
ret = -ERESTARTSYS;
goto out2;
}
//不是信号唤醒的,可以操作了 ,此时再上锁dev->current_len != GLOBALFIFO_SIZE
mutex_lock(&dev->globalfifo_mutex);
}
if (dev->current_len + count > GLOBALFIFO_SIZE)
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 %u bytes(s) from %u \n",count,dev->current_len-count);
wake_up_interruptible(&dev->r_wait); //写入后再唤醒读(如果有读进程堵塞)
ret = count;
}
out:
mutex_unlock(&dev->globalfifo_mutex);
out2:
remove_wait_queue(&dev->w_wait,&wait);
set_current_state(TASK_RUNNING);
return ret;
}
ssize_t globalfifo_read(struct file *filp, char __user *buf,
size_t size, loff_t *pos)
{
unsigned int count = size;
int ret = 0;
struct globalfifo_dev *dev = container_of(filp->private_data,struct globalfifo_dev,miscdev);
DECLARE_WAITQUEUE(wait,current);
mutex_lock(&dev->globalfifo_mutex);
add_wait_queue(&dev->r_wait,&wait);
while (dev->current_len == 0)//如果当前的fifo为空,则进程阻塞
{
if(filp->f_flags & O_NONBLOCK)//判断是否以非阻塞打开的设备文件(/dev/globalfifo)
{
//非阻塞就直接返回了
ret = -EAGAIN;
goto out;
}
//阻塞就让本函数休眠
__set_current_state(TASK_INTERRUPTIBLE);
mutex_unlock(&dev->globalfifo_mutex);
schedule();
if(signal_pending(current))//判断是否是信号唤醒的
{
//是信号唤醒的
ret = -ERESTARTSYS;
goto out2;
}
//不是信号唤醒的,可以操作了 ,此时再上锁dev->current_len != 0
mutex_lock(&dev->globalfifo_mutex);
}
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);//将剩下的内容拷贝到前面去
dev->current_len -= count;
printk(KERN_INFO "read %u bytes(s) from %u \n",count,dev->current_len);
wake_up_interruptible(&dev->w_wait);//读出后后再唤醒写(如果有写进程堵塞)
ret = count;
}
out:
mutex_unlock(&dev->globalfifo_mutex);
out2:
remove_wait_queue(&dev->w_wait,&wait);
set_current_state(TASK_RUNNING);
return ret;
}
/*
在misc中,设备的fops中的open函数,不能指定filp->private_data为&dev->miscdev
这样会导致
struct globalfifo_dev *dev = container_of(filp->private_data,struct globalfifo_dev,miscdev);
获取不到 ,引发死机
*/
int globalfifo_open(struct inode *inode, struct file *filp)
{
//filp->private_data = globalfifo_devp;
//如果有多个设备,主设备号相同,只是次设备号不同,驱动这样写
//从inode->i_cdev中找到类型为struct globalfifo_dev的成员
// struct globalfifo_dev * dev = container_of(inode->i_cdev,struct globalfifo_dev,cdev); //container_of(ptr,type,member)
// filp->private_data = &dev->miscdev;
return 0;
}
int globalfifo_release(struct inode *inode, struct file *filp)
{
return 0;
}
long globalfifo_ioctl (struct file *filp, unsigned int cmd, unsigned long arg)
{
struct globalfifo_dev *dev = container_of(filp->private_data,struct globalfifo_dev,miscdev);
switch (cmd)
{
case MEM_CLEAR:
mutex_lock(&dev->globalfifo_mutex);
memset(dev->mem , 0 ,GLOBALFIFO_SIZE);
mutex_unlock(&dev->globalfifo_mutex);
printk(KERN_INFO "globalfifo is set to zero \n");
break;
default:
return -EINVAL;
}
return 0;
}
static const struct file_operations globalfifo_fops = {
.owner = THIS_MODULE,
.llseek = globalfifo_llseek,
.read = globalfifo_read,
.write = globalfifo_write,
.unlocked_ioctl = globalfifo_ioctl,
.open = globalfifo_open,
.release = globalfifo_release,
};
// static void globalfifo_setup_cdev(struct globalfifo_dev *dev,int index)
// {
// int err,devno = MKDEV(globalfifo_major,index);
// printk(KERN_INFO "index is %d\n",index); //添加打印代码是不想编译时这个代码被优化掉,导致次设备号的设备打不开
// cdev_init(&dev->cdev ,&globalfifo_fops);
// printk(KERN_INFO "dev->cdev is %p\n",&dev->cdev);
// dev->cdev.owner = THIS_MODULE;
// err = cdev_add(&dev->cdev ,devno, 1);
// if(err)
// printk(KERN_NOTICE "Error %d adding globalfifo%d",err,index);
// }
static int globalfifo_probe(struct platform_device *pdev)
{
int ret;
//将下面都注释掉,注册字符设备将由misc来完成
// dev_t devno = MKDEV(globalfifo_major,0);
// if (globalfifo_major)
// ret = register_chrdev_region(devno,DEVICE_NUM,"globalfifo");
// else{
// ret = alloc_chrdev_region(&devno,0,DEVICE_NUM,"globalfifo");
// globalfifo_major = MAJOR(devno);
// }
// if (ret < 0)
// return ret;
globalfifo_devp = kzalloc(sizeof(struct globalfifo_dev),GFP_KERNEL);
if (!globalfifo_devp)
{
ret = -ENOMEM;
goto fail_malloc;
}
globalfifo_devp->current_len = 0;
globalfifo_devp->miscdev.minor = MISC_DYNAMIC_MINOR;
globalfifo_devp->miscdev.name = "globalfifo_misc";
globalfifo_devp->miscdev.fops = &globalfifo_fops;
// for (i = 0;i < DEVICE_NUM;i++)
// {
// mutex_init(&(globalfifo_devp+i)->globalfifo_mutex);
// init_waitqueue_head(&(globalfifo_devp+i)->r_wait);
// init_waitqueue_head(&(globalfifo_devp+i)->w_wait);
// //globalfifo_setup_cdev(globalfifo_devp + i,i);
// }
mutex_init(&globalfifo_devp->globalfifo_mutex);
init_waitqueue_head(&globalfifo_devp->r_wait);
init_waitqueue_head(&globalfifo_devp->w_wait);
platform_set_drvdata(pdev,globalfifo_devp);
ret = misc_register(&globalfifo_devp->miscdev);//在这里完成了device_create(),cdev的创建,所以将globalfifo_setup_cdev()函数注释了
printk(KERN_INFO "misc minor is %d\n",globalfifo_devp->miscdev.minor);
if(ret < 0)
{
printk(KERN_INFO "misc_register failed ...\n");
goto fail_malloc;
}
return 0;
fail_malloc:
return ret;
}
static int globalfifo_remove(struct platform_device *pdev)
{
// int i = 0;
struct globalfifo_dev *globalfifo_devp = platform_get_drvdata(pdev);
// for (i = 0;i < DEVICE_NUM;i++)
// cdev_del(&(globalfifo_devp+i)->cdev);
misc_deregister(&globalfifo_devp->miscdev);
kfree(globalfifo_devp);
//unregister_chrdev_region(MKDEV(globalfifo_major,0),DEVICE_NUM);
return 0;
}
static struct platform_driver globalfifo_driver = {
.driver = {
.name = "globalfifo",
.owner = THIS_MODULE,
},
.probe = globalfifo_probe,
.remove = globalfifo_remove,
};
module_platform_driver(globalfifo_driver);
MODULE_AUTHOR("good man");
MODULE_LICENSE("GPL v2");