我自己定义了一个需求
1、建立1个字符设备,其中有100字节的存储区
2、能通过open,read,write实现,往其中写入数据和读出数据
- #ifndef SCULL_MAJOR
-
#define SCULL_MAJOR 0
-
#endif
-
-
#include<linux/module.h>
-
#include<linux/init.h>
-
#include<linux/kernel.h>
-
-
#include<linux/types.h>
-
#include<linux/fs.h>
-
#include<linux/cdev.h>
-
#include<asm/uaccess.h>
-
-
MODULE_AUTHOR("AW");
-
MODULE_LICENSE("GPL");
-
-
typedef struct scull_dev
-
{
-
int num;
-
unsigned char data[100];
-
unsigned char *first;
-
struct cdev cdev;
-
}scull_dev;
-
-
//定义操作的函数
-
int scull_open(struct inode *inode,struct file *flip);
-
ssize_t scull_read(struct file *filp,const __user *buf,size_t len,loff_t *offp);
-
ssize_t scull_write(struct file *filp,const char __user *buf,size_t len,loff_t *offp);
-
int scull_ioctl(struct inode *inode,struct file *filp,unsigned long num );
-
-
int scull_open(struct inode *inode ,struct file *filp)
-
{
-
printk("<1>scull is opend \n");
-
struct scull_dev *dev=container_of(inode->i_cdev,scull_dev,cdev);
-
filp->private_data=dev->data;
-
memset(dev->data,1,100);
-
return 0;
-
}
-
-
ssize_t scull_read(struct file *filp,const __user *buf,size_t len,loff_t *offp)
-
{
-
printk("<0>------------------------read!\n");
-
copy_to_user(buf,filp->private_data,20);
-
return 0;
-
}
-
-
ssize_t scull_write(struct file *filp,const char __user *buf,size_t len,loff_t *offp)
-
{
-
printk("<0>!!!!!!!!!!!!!!!!!!!!!!!write!\n");
-
copy_from_user(filp->private_data,buf,20);
-
return 0;
-
}
-
-
int scull_ioctl(struct inode *inode ,struct file *filp,unsigned long num)
-
{
-
return 0;
-
}
-
-
static struct file_operations my_fops=
-
{
-
.owner=THIS_MODULE,
-
.open=scull_open,
-
.read=scull_read,
-
.write=scull_write,
-
.ioctl=scull_ioctl,
-
//.close=scull_close,
-
};
-
-
//int register_chrdev(unsigned int major,const char *name,struct file_oprations *fops)//2.4中的方法
-
//在此函数中,注册设备并初始化
-
int scull_init(void)
-
{
-
int result;
-
int dev_num=0;
-
int scull_major=SCULL_MAJOR;
-
int scull_minor=0;
-
scull_dev *myscull=(scull_dev *)kmalloc(sizeof(scull_dev),GFP_KERNEL);
-
memset(myscull,0,sizeof(scull_dev));
-
-
//设定设备号
-
if(scull_major)
-
{
-
dev_num=MKDEV(scull_major,scull_minor);
-
printk("<0>the devnum is %d",dev_num);
-
result=register_chrdev_region(dev_num,1,"scull");
-
}
-
else
-
{
-
result=alloc_chrdev_region(&dev_num,scull_minor,1,"scull");
-
printk("the result is %d",result);
-
}
-
if(result<0)
-
{
-
printk("<1>scull:can't get major %d\n",scull_major);
-
return result;
-
}
-
scull_major=MAJOR(dev_num);
-
printk("<0>the MAJOR is %d",scull_major);
-
-
//初始化cdev设备scull,确定相应的操作和结构
-
cdev_init(&myscull->cdev,&my_fops);
-
myscull->cdev.owner=THIS_MODULE;
-
//向系统注册,绑定刚刚的设备号
-
result=cdev_add(&myscull->cdev,dev_num,1);
-
if(result <0)
-
{
-
printk("<0>it doesn't registered!\n");
-
}
-
printk("<1>the module scull is insert !\n");
-
return 0;
-
}
-
-
void scull_exit(void)
-
{
-
printk("<1>leave this module!\n");
-
}
-
-
module_init(scull_init);
-
module_exit(scull_exit);
下面是测试函数
- #include<stdio.h>
-
#include<stdlib.h>
-
#include<fcntl.h>
-
#include<string.h>
-
-
int main()
-
{
-
int fd=open("/dev/scull",O_RDWR);
-
if(fd==-1)
-
{
-
printf("the fd can't opened\n");
-
return -1;
-
}
-
printf("the fd is %d\n",fd);
-
unsigned char data[100];
-
memset(data,0,100);
-
read(fd,data,20);
-
int i;
-
for(i=0;i<20;i++)
-
{
-
printf("%d",data[i]);
-
}
-
-
printf("\n");
-
unsigned char data1[21]={1,2,3,4,5,6,7,8,8,1,2,3,4,5,5,6,6,4};
-
write(fd,data1,20);
-
-
read(fd,data,20);
-
for(i=0;i<20;i++)
-
{
-
printf("%d",data[i]);
-
}
-
printf("\n");
-
}
其中的模块的Makefile文件
- obj-m:=scull.o
-
KERNELDIR:=/lib/modules/2.6.32-30-generic/build
-
PWD:=$(shell pwd)
-
modules:
-
$(MAKE) -C $(KERNELDIR) M=$(PWD)
-
clean:
能够正确得到结果
阅读(702) | 评论(0) | 转发(0) |