linux 字符驱动模板

 linux 字符驱动模板 2.4以下的内核适用。

#include <linux/module.h>
#include <linux/config.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <asm/uaccess.h> 

#define MAJOR_NUM 125
#define DEVICE_NAME "emptychr"

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

 return count;
}
static ssize_t test_write(struct file *file, const char *buf, size_t count, loff_t *f_pos)
{
 return count;
}
static int test_open(struct inode *inode,struct file *file )
{
 MOD_INC_USE_COUNT;
 return 0;
}
static int test_release(struct inode *inode,struct file *file )
{
 MOD_DEC_USE_COUNT;
 return 0;
}
static int test_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
 return 0;
}
struct file_operations test_fops = {
 read:test_read,
 write:test_write,
 open: test_open,
 release:test_release,
 ioctl:test_ioctl
};
int test_init(void)
{
 int result;
 result = register_chrdev(MAJOR_NUM, DEVICE_NAME, &test_fops);
 if (result < 0) {
  printk(KERN_INFO "test: can't get major number/n");
  return result;
 }
 
 printk("init module/n");
 return 0;

}
void test_exit(void)
{
 unregister_chrdev(MAJOR_NUM,DEVICE_NAME);
 printk("cleanup_module/n");
}


module_init(test_init);
module_exit(test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("huangxb");

你可能感兴趣的:(linux 字符驱动模板)