¼ÓÔØÇý¶¯Ê§°Ü!j¼±!!!
ÎÒдÁËÒ»¸ö²âÊÔʹÓõÄ×Ö·ûÇý¶¯ÈçÏ£º###################################################hello.c
#include <linux/version.h>
#include <linux/config.h>
#if CONFIG_MODVERSIONS == 1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/ioctl.h>
#include "hello.h"
struct file_operations hello_fops =
{
.owner = THIS_MODULE,
.llseek = hello_llseek,
.open = hello_open,
.release = hello_release,
};
loff_t hello_llseek(struct file * filp, loff_t off, int whence)
{
return 0 ;
}
/*Open·½·¨*/
int hello_open(struct inode *inode, struct file *filp)
{
printk("hello: open /dev/hello!!!\n") ;
try_module_get(THIS_MODULE) ;
return 0 ;
}
/*Release ·½·¨*/
int hello_release(struct inode *inode, struct file *filp)
{
printk("hello: close /dev/hello!!!\n") ;
module_put(THIS_MODULE) ;
return 0 ;
}
static int __init my_init_function(void)
{
int result, err ;
/*×¢²áÇý¶¯É豸Ö÷,´ÎÉ豸ºÅ*/
if(hello_major)
{
/*Ö¸¶¨Ö÷É豸ºÅ*/
hello_dev = MKDEV(hello_major, hello_minor) ;
result = register_chrdev_region(hello_dev, 1, "Hello") ;
}
else
{
/*¶¯Ì¬·ÖÅäÖ÷É豸ºÅ*/
result = alloc_chrdev_region(&hello_dev, hello_minor, 1, "Hello") ;
hello_major = MAJOR(hello_dev) ;
}
if(result < 0)
{
printk(KERN_ERR "hello: can't get major %d\n", hello_major) ;
return -1 ;
}
/*×¢²á×Ö·ûÉ豸Çý¶¯³ÌÐò*/
phello_cdev = cdev_alloc() ;
if(phello_cdev != NULL)
{
cdev_init(phello_cdev, &hello_fops) ;
phello_cdev->ops = &hello_fops ;
phello_cdev->owner = THIS_MODULE ;
err = cdev_add(phello_cdev, hello_dev, 1) ;
if(err)
{
printk(KERN_NOTICE "Error %d adding hello_cdev", err);
}
else
{
printk("Success adding hello_cdev!\n") ;
}
}
else
{
printk(KERN_ERR "hello: Register char hello_dev error\n") ;
return -1 ;
}
return 0 ;
}
static void __exit my_cleanup_function(void)
{
unregister_chrdev_region(hello_dev, 1) ;
}
module_init(my_init_function) ;
module_exit(my_cleanup_function) ;
MODULE_AUTHOR("CHENHUI") ;
MODULE_DESCRIPTION("Test") ;
MODULE_LICENSE("GPL") ;
###########################################################hello.h
#ifndef _HELLO_H_
#define _HELLO_H_
#define MYDRIVER_NAME "Hello"
#undef MYDRIVER_MAJOR
#ifndef MYDRIVER_MAJOR
#define MYDRIVER_MAJOR 0 /* dynamic major by default */
#endif
loff_t hello_llseek (struct file *filp, loff_t off, int whence);
int hello_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
int hello_open(struct inode *inode, struct file *filp);
int hello_release(struct inode *inode, struct file *filp);
extern struct file_operations hello_fops;
struct cdev *phello_cdev ; //×Ö·ûÉ豸µÄ½Úµã
dev_t hello_dev ; //Çý¶¯É豸ºÅµÄ½Úµã
/*¸øÖ÷,´ÎÉ豸ºÅ¸³Öµ*/
int hello_major = MYDRIVER_MAJOR;
int hello_minor = 0 ;
#endif
*******************************************************************************
ÎÒµÄÎÊÌâÈçÏÂ:
1.ÎÒ±àÒëµÄʱºòʹÓõÄÊÇmake -C /usr/src/linux SUBDIRS=$PWD modules,µÚÒ»´ÎÔËÐÐÕâ¸öÃüÁîµÄʱºò£¬ÎªÊ²Ã´»á±àÒëÄںˣ¿ÎÒ°´ÕÕ¡¶linux É豸Çý¶¯ µÚÈý°æ¡·ÉϵÄд·¨make -C /usr/src/linux M ='pwd' modules ½á¹ûÖ»»á±àÒëÄںˣ¬ÎÒµÄĿ¼Ï¸ù±¾Ã»ÓÐÈκÎеÄÎļþ£¨*.ko *.mod.oµÈµÈ£©Éú³É£¿
2.µ±ÎÒʹÓÃmake -C /usr/src/linux SUBDIRS=$PWD modules±àÒë³É¹¦ºó£¬ÔËÐÐ#insmod hello.koºó£¬ÎÒʹÓÃdmesg¿´µ½ÏµÍ³Ìáʾ£º¡°no version for"struct_module" found:kernel tainted¡±,ÇëÎÊÕâÊÇÔõô»ØÊ£¿
3.×îºóÒ»¸öÈÃÎÒºÜÓôÃÆµÄÎÊÌ⣺µÚÒ»´ÎÔËÐÐ#insmod hello.koºó£¬²ì¿´/proc/devicesÀïÃæ»¹ÊÇÓÐÎÒµÄÉ豸µÄ£¬É豸ºÅÊÇ252£¬È»ºóÎÒÔËÐУº#rmmod hello.ko.È»ºóÔÙ´ÎÔËÐÐ:#insmod hello.ko,½á¹ûϵͳ¾Í±§´íÁË£ºÎÒ¶ÔÕÕ´úÂë¡£ÊÇÎÒµÄcdev_addº¯Êý³ö´íÁË£¬´íÎóºÅÊÇ-17£¬ÇëÎÊÔõô½â¾ö£¿
лл´ó¼Ò£¡Çë´ó¼ÒÖ¸µãһϡ£Ê®·Ö¸Ð¼¤£¡£¡£¡£¡
Ò³:
[1]