/*************************************************************************
> File Name: cdev.c
> Author: zxn
> Created Time: Fri 22 May 2015 10:22:05 AM CST
************************************************************************/
#include"mycdev.h"
//define an instance
struct mychar_dev *p_mychar_dev;
//open func of the struct file_operation
//the inode is no use here because we use p_mychar_dev to simulate the file
int mychar_open(struct inode *p_inode,struct file *p_file)
{
p_file->private_data = p_mychar_dev;
printk(KERN_INFO "mychar has opened\n");
if(p_file->private_data == NULL){
printk(KERN_INFO "pointer is null\n");
}
return 0;
}
int mychar_release(struct inode *p_inode, struct file * p_file)
{
p_file->private_data = 0;
return 0;
}
//read
ssize_t mychar_read(struct file *p_file, char __user *buf, size_t size,loff_t *p_pos)
{
unsigned long pos = *p_pos;
long count = size;
long ret = 0;
struct mychar_dev *p_cdev = p_file->private_data;
printk(KERN_INFO "enter mychar read\n");
if(size > MYCHAR_MEM_SIZE){
return 0;
}
if(pos + count > MYCHAR_MEM_SIZE){
count = MYCHAR_MEM_SIZE - pos;
}
if(copy_to_user(buf,(char*)(p_cdev->mem + pos),count)== count){
ret = -EFAULT;
}
else {
*p_pos = pos + count;
ret = count;
printk(KERN_INFO "read %lu bytes from %lu\n",count,pos);
printk(KERN_INFO "mem0 = %d mem1 = %d mem3 = %d\n",p_cdev->mem[0],p_cdev->mem[1],p_cdev->mem[3]);
printk(KERN_INFO "buf0 = %d buf1 = %d buf3 = %d\n",*(buf+0),*(buf+1),*(buf+3));
}
return ret;
}
//write
ssize_t mychar_write(struct file *p_file,const char __user *buf,size_t size,loff_t *p_pos)
{
unsigned long pos = *p_pos;
long count = size;
long ret = 0;
struct mychar_dev *p_cdev = p_file->private_data;
printk(KERN_INFO "enter mychar write\n");
if(size > MYCHAR_MEM_SIZE){
return 0;
}
if(pos + count > MYCHAR_MEM_SIZE){
count = MYCHAR_MEM_SIZE - pos;
}
if(copy_from_user((void *)(p_cdev->mem + pos),buf,count)){
ret = -EFAULT;
}
else{
*p_pos = pos + count;
ret = count;
printk(KERN_INFO"written %lu bytes from %lu\n",count,pos);
printk(KERN_INFO "mem0 = %d mem1 = %d mem3 = %d \n",p_cdev->mem[0],p_cdev->mem[1],p_cdev->mem[3]);
}
return ret;
}
//llseek 设置文件偏移
static loff_t mychar_llseek(struct file *p_file,loff_t offset,int orig)
{
loff_t ret = 0;
printk(KERN_INFO "enter llseek,f_pos = %d offset = %d\n",p_file->f_pos,offset);
switch(orig){
case 0: //offset from the file beginning
if(offset < 0){
ret = -EINVAL; //EINVAL invalid argument
break;
}
if(offset > MYCHAR_MEM_SIZE){
ret = -EINVAL;
break;
}
p_file->f_pos = (unsigned int) offset;
ret = offset;
break;
case 1: //offset from current pos
if((p_file->f_pos + offset) > MYCHAR_MEM_SIZE || \
(p_file->f_pos +offset) < 0){
ret = -EINVAL;
break;
}
p_file->f_pos += offset;
ret = p_file->f_pos;
break;
default :
ret = -EINVAL;
break;
}
printk(KERN_INFO "ret = %d, f_pos= %d\n",ret,p_file->f_pos);
return ret;
}
// one operation of ioctl: memery clear
int mychar_ioctl(struct inode *p_inode,struct file *p_file,unsigned int cmd,unsigned long arg)
{
struct mychar_dev *dev = (struct mychar_dev *)(p_file->private_data);
switch(cmd)
{
case MYCHAR_MEM_CLR:
memset(dev,0,MYCHAR_MEM_SIZE);
printk(KERN_INFO"mychar memery is set to zero\n");
break;
default:
return -EINVAL;
}
return 0;
}
//文件操作结构体
static const struct file_operations mychar_fops = {
.owner = THIS_MODULE,
.llseek = mychar_llseek,
.read = mychar_read,
.write = mychar_write,
.unlocked_ioctl = mychar_ioctl,
.open = mychar_open,
.release = mychar_release,
};
//cdev 结构初始化
static void mychar_setup_cdev(struct mychar_dev *dev,int index)
{
int err;
int devno = MKDEV(mychar_major,index);
cdev_init(&dev->cdev,&mychar_fops);
dev->cdev.owner = THIS_MODULE;
err = cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE "Eroor %d adding mychar %d\n",err,index);
}
//init
static int __init mychar_init(void)
{
int result;
dev_t devno = MKDEV(mychar_major,0);
if(mychar_major)
result = register_chrdev_region(devno,1,"mychar");
else{
result = alloc_chrdev_region(&devno,0,1,"mychar");
mychar_major = MAJOR(devno);
}
if(result < 0)
return result;
p_mychar_dev = kmalloc(sizeof(struct mychar_dev),GFP_KERNEL);
if(p_mychar_dev == NULL){
result = -ENOMEM;
goto fall_malloc;
}
memset(p_mychar_dev,0,sizeof(struct mychar_dev));
mychar_setup_cdev(p_mychar_dev,0);
printk(KERN_INFO "Hello,word!this is my cdev\n");
return 0;
fall_malloc:
unregister_chrdev_region(devno,1);
return result;
}
//exit
static void __exit mychar_exit(void)
{
cdev_del(&(p_mychar_dev->cdev));
kfree(p_mychar_dev);
unregister_chrdev_region(MKDEV(mychar_major,0),1);
printk(KERN_INFO "Bye word!\n");
}
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("zxn");
module_init(mychar_init);
module_exit(mychar_exit);