字符设备驱动

#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>


#define MEM_SIZE 0x1000 /*全局内存最大4KB*/
#define MEM_CLEAR 0x1
#define MEM_MAJOR 0 /*预设的mem的主设备号*/


static mem_major = MEM_MAJOR;


struct mem_dev_t /*mem设备结构体*/
{
struct cdev cdev;
unsigned char mem[MEM_SIZE];
}
struct mem_dev_t *mem_dev;


int mem_open(struct inode *inode, struct file *filp)
{
struct mem_dev_t *dev;
/*获取设备结构体*/
dev = container_of(inode->i_cdev,struct mem_dev_t,cdev);
/*将设备结构体指针赋值给文件私有数据指针*/
filp->private_data = dev;
return 0;
}
int mem_close(struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t mem_read(struct file *filp, char _ _user *buf,size_t size,loff_t *ppos)
{
struct mem_dev_t *dev = filp->private_data;
unsigned int p = *ppos;
unsigned int count = size;
int ret = 0;
if(p > MEM_SIZE) /*分析和获取有效的写长度*/
return count ? -ENXIO:0;
if(count > MEM_SIZE - p)
count = MEM_SIZE - p; 
if(copy_to_user(buf,(void*)(dev->mem + p),count)) /*内核空间→用户空间*/
{
ret = -EFAULT;
}
else
{
*ppos += count;
ret = count;
}
return ret;
}
static ssize_t mem_write(struct file *filp, const char __user *buf,size_t size, loff_t *ppos)
{
struct mem_dev_t *dev = filp->private_data;
unsigned int p = *ppos;
unsigned int count = size;
int ret = 0;
if(p > MEM_SIZE) /*分析和获取有效的写长度*/
return count ? -ENXIO:0;
if(count > MEM_SIZE - p)
count = MEM_SIZE - p; 
if(copy_from_user(dev->mem + p,buf,count)) /*用户空间→内核空间*/
{
ret = -EFAULT;
}
else
{
*ppos += count;
ret = count;
}
return ret;


}
static int mem_ioctl(struct inode *inodep, struct file *filp,unsigned int cmd, unsigned long arg)
{
struct mem_dev_t *dev = filp->private_data;
switch(cmd)
{
case MEM_CLEAR:
memset(dev->mem,0,MEM_SIZE);
printk(KERN_INFO "globalmem is set to zero\n");
break;
default:
return -EINVAL;
}
return 0;
}
/* seek文件定位函数*/
static loff_t mem_lseek(struct file *filp, loff_t offset,int orig)
{
loff_t ret = 0;
switch(orig)
{
case 0: /*相对文件开始位置偏移*/
if(offset < 0)
{
ret = -EINVAL;
break;
}
if((unsigned int)offset > MEM_SIZE)
{
ret = -EINVAL;
break;
}
filp->f_ops = (unsigned int)offset;
return filp->f_ops;
break;
case 1: /*相对文件当前位置偏移*/
if((filp->f_pos + offset) < 0)
{
ret = -EINVAL;
break;
}
if((filp->f_pos + offset) > MEM_SIZE)
{
ret = -EINVAL;
break;
}
filp->f_ops += (unsigned int)offset;
return filp->f_ops;
break;
default:
ret = - EINVAL;
break;
}
return ret;
}
//文件操作结构体
struct file_operations fops={
.owner = THIS_MODULE,
.open = mem_open,
.release = mem_close,
.read = mem_read,
.write = mem_write,
.ioctl = mem_ioctl,
.llseek = mem_lseek,
};
//初始化,添加字符设备
static void mem_dev_setup(struct mem_dev_t *dev,int index)
{
int err,devno = MKDEV(mem_major,index);
cdev_init(&dev->cdev,&fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &fops;
err = cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE "Error %d adding mem%d",err,index);
}
//初始化 申请设备号,和内存
static int mem_init(void)
{
int ret;
dev_t devno = MKDEV(mem_major,0);
if(mem_major) //mem_major=0为命令行设置设备号所用
{
ret = register_chrdev_region(dev,2,"mem");
}
else
{
ret = alloc_chrdev_region(&devno,0,2,"mem");
mem_major = MAJOR(devno);
}
mem_dev = kmalloc(sizeof(struct mem_dev_t)*2,GFP_KERNEL);
if(!mem_dev)
{
ret = -ENOMEM;
goto fail_malloc;
}
memset(mem_dev,0,sizeof(struct mem_dev_t)*2);

mem_dev_setup(&mem_dev[0],0);
mem_dev_setup(&mem_dev[1],1);
if(ret < 0)
return ret;

return 0;

fail_malloc:unregister_chrdev_region(MKDEV(mem_major,0),2);
return ret;
}
//删除设备号和字符设备,释放内存
static void mem_exit(void)
{
unregister_chrdev_region(MKDEV(mem_major,0),2);
cdev_del(&(mem_dev[0].cdev));
cdev_del(&(mem_dev[0].cdev));
kfree(mem_dev);
}
MODULE_AUTHOR("CC");
MODULE_LICENSE("Dual BSD/GPL");


module_param(mem_major,int,S_IRUGO); /*通过命令行设置设备号*/


module_init(mem_init);
module_exit(mem_exit);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值