Hello Driver

6 篇文章 0 订阅

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<raulshao@gmail.com>");
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         // 卸载驱动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值