- Makefile
obj-m := test.o
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
- test.c
#include <linux/types.h> /*基本的类型定义*/
#include <linux/fs.h> /*文件系统使用相关的头文件*/
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/module.h>
unsigned int test_major = 0;
static int read_test(struct file *file, const char __user *buf,size_t count, loff_t *ppos){
printk(KERN_INFO "test read\n");
int left=0;
if (access_ok(VERIFY_WRITE, buf, count)){
for(left=count;left>0;left--){
__put_user('a', buf);
buf++;
}
}
return count;
}
static int write_test(struct file *file, const char __user *buf,size_t count, loff_t *ppos)//(struct inode *inode, struct file *file, const char *buf,int*count)
{ //33
printk(KERN_INFO "test write %d\n",count);
return count;
}
static int open_test(struct inode *inode, struct file *file ){ //36
//MOD_INC_USE_COUNT; /*模块计数加1,表示当前内核有个设备加载内核当中去*/
printk(KERN_INFO "test open\n");
try_module_get(THIS_MODULE);
return 0;
}
static void release_test(struct inode *inode, struct file *file ){ //41
//MOD_DEC_USE_COUNT;
printk(KERN_INFO "test release\n");
module_put(THIS_MODULE);
}
struct file_operations test_fops=
{
.owner = THIS_MODULE,
.read = read_test,
.write = write_test,
.open = open_test,
.release = release_test,
};
int init_module(void){
printk(KERN_INFO "test driver\n");
int result;
result = register_chrdev(0, "test", &test_fops);/*对设备操作的整个接口*/
if (result < 0){
printk(KERN_INFO "test: can't get major number\n");
return result;
}
if (test_major == 0)
test_major = result; /* dynamic */
return 0;
}
void cleanup_module(void){
printk(KERN_INFO "test driver 88\n");
unregister_chrdev(test_major, "test");
}
#include <linux/fs.h> /*文件系统使用相关的头文件*/
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/module.h>
unsigned int test_major = 0;
static int read_test(struct file *file, const char __user *buf,size_t count, loff_t *ppos){
printk(KERN_INFO "test read\n");
int left=0;
if (access_ok(VERIFY_WRITE, buf, count)){
for(left=count;left>0;left--){
__put_user('a', buf);
buf++;
}
}
return count;
}
static int write_test(struct file *file, const char __user *buf,size_t count, loff_t *ppos)//(struct inode *inode, struct file *file, const char *buf,int*count)
{ //33
printk(KERN_INFO "test write %d\n",count);
return count;
}
static int open_test(struct inode *inode, struct file *file ){ //36
//MOD_INC_USE_COUNT; /*模块计数加1,表示当前内核有个设备加载内核当中去*/
printk(KERN_INFO "test open\n");
try_module_get(THIS_MODULE);
return 0;
}
static void release_test(struct inode *inode, struct file *file ){ //41
//MOD_DEC_USE_COUNT;
printk(KERN_INFO "test release\n");
module_put(THIS_MODULE);
}
struct file_operations test_fops=
{
.owner = THIS_MODULE,
.read = read_test,
.write = write_test,
.open = open_test,
.release = release_test,
};
int init_module(void){
printk(KERN_INFO "test driver\n");
int result;
result = register_chrdev(0, "test", &test_fops);/*对设备操作的整个接口*/
if (result < 0){
printk(KERN_INFO "test: can't get major number\n");
return result;
}
if (test_major == 0)
test_major = result; /* dynamic */
return 0;
}
void cleanup_module(void){
printk(KERN_INFO "test driver 88\n");
unregister_chrdev(test_major, "test");
}
insmod -f test.ko
sudo insmod -f test.ko
cat /proc/devices (获得250的值)
sudo mknod /dev/test c 250 0
cat /proc/devices (获得250的值)
sudo mknod /dev/test c 250 0
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
void main ()
{
int sdc=open("/dev/test",O_RDWR);
char buf[512]={0};
int count=0;
count=read(sdc,buf,512);
printf("%d %s %d",sdc,buf,count);
// lseek(sdc,0 ,SEEK_SET);
count = write(sdc,buf,512);
printf("write %d %x\n",count,buf[0]);
close(sdc);}
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
void main ()
{
int sdc=open("/dev/test",O_RDWR);
char buf[512]={0};
int count=0;
count=read(sdc,buf,512);
printf("%d %s %d",sdc,buf,count);
// lseek(sdc,0 ,SEEK_SET);
count = write(sdc,buf,512);
printf("write %d %x\n",count,buf[0]);
close(sdc);}