Hello Driver

1. linux-2.6.12目錄爲Linux的源代碼目錄

2. driver目錄下新建raulshao

3. driver目錄下面的Makefile最後一行添加

    obj-m    += raulshao/
4. raulshao目錄下新建hello.c, Makefile

 

// hello.c

#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif

#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/timer.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <asm/atomic.h>

 

MODULE_LICENSE("GPL");
MODULE_AUTHOR("RAUL.SIU<[email protected]>");
MODULE_DESCRIPTION("Hello Driver.");

 

 

//////////////////////////////////////////////////////////////////////////
// 指定接口
static struct file_operations dev_ops =
{
 owner: THIS_MODULE,
 write: FUN_WRITE,
 read: FUN_READ,
 open: FUN_OPEN,
 release:FUN_CLOSE,
};

 


int hello_init(void)
{

 register_chrdev(MAJOR_VER, DEV_NAME, &dev_ops);
 devfs_mk_cdev(MKDEV(MAJOR_VER, 0), S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP, DEV_NAME);

 printk(KERN_ALERT"hello_init!/n");
 return 0;
}


void hello_exit(void)
{
 printk(KERN_ALERT"hello_exit!/n");

 devfs_remove(DEV_NAME);


 unregister_chrdev(MAJOR_VER, DEV_NAME);
}


module_init(hello_init);
module_exit(hello_exit);

 

 

// Makefile

obj-m += hello.o

 

 

5. 在源代碼目錄下執行make

 

6. 測試

    insmode hello.ko  // 安裝驅動

    lsmod                   // 檢查驅動

    rmmod hello         // 卸載驅動

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章