#define __NO_VERSION__

#include <linux/module.h>
#include <linux/config.h>
#include <linux/version.h>

#include <asm/uaccess.h>

#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>

unsigned int test_major = 0;

static ssize_t read_test(struct file *file,char *buf,size_t count,loff_t *f_pos)

{ int left;

if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT )
return -EFAULT;

for(left = count ; left > 0 ; left--)
{
__put_user(1,buf);
buf++;
}

return count;
}


static ssize_t write_test(struct file *file, const char *buf, size_t count, loff_t *f_pos)
{
return count;
}

static int open_test(struct inode *inode,struct file *file )

{
MOD_INC_USE_COUNT;
return 0;
}

static int release_test(struct inode *inode,struct file *file )
{
MOD_DEC_USE_COUNT;
return 0;
}


struct file_operations test_fops = {
read:read_test,
write:write_test,
open: open_test,
release:release_test
};

int init_module(void)
{
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)

{

unregister_chrdev(test_major, "test");

}


MODULE_LICENSE("GPL");
MODULE_AUTHOR("BECKHAM");


//===================================
这个程序存为test.c ,它的makefile:
CC=gcc
MODCFLAGS:=-Wall -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include
test.o:test.c
$(CC) $(MODCFLAGS) -c test.c

posted on 2005-04-12 19:36  default.aspx  阅读(524)  评论(0编辑  收藏  举报