Linux 第一个驱动程序编写

环境

 Linux

内核升级

  • 下载标准内核源码
  • 链接选择合适的版本,我选择的是4.3.1。
  • 创建一个文件夹 放入其中,解压。进入第二层目录。
  • 执行make menuconfig,进入 Processor type and feature选项,回车进入Processor family选项,选择 Generic-x86-64保存退出。
  • make
  • make modules
  • make modules_install
  • make install
  • reboot
  • 重启后uname -r查看新内核版本。

开始编写Hello World驱动程序

hello.c

#include <linux/init.h>             /* 定义了一些相关的宏 */
#include <linux/module.h>           /* 定义了模块需要的*/

static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");    /* 打印hello World */
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, world\n");  /* 打印Goodbye,world */
}

module_init(hello_init);   /* 指定模块加载函数 */
module_exit(hello_exit);   /* 指定模块卸载函数 */
MODULE_LICENSE("Dual BSD/GPL");

Makefile

ifeq ($(KERNELRELEASE),)

    # Assume the source tree is where the running kernel was built
    # You should set KERNELDIR in the environment if it's elsewhere
    KERNELDIR ?= /linux-2.6.29.4/linux-2.6.29.4
    # The current directory is passed to sub-makes as argument
    PWD := $(shell pwd)

modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
    # called from kernel build system: just declare what our modules are
    obj-m := hello.o 
endif

修改下KERNELDIR 路径就可以了。

insmod加载模块

  • insmod hello.ko

以上就是全部过程了=_=

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我们说的Linux其实指的就是 内核(kernel)而已。这个内核控制你主机的所有硬件并提供系统所有的功能,所以它...
    Zhang21阅读 7,464评论 0 18
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,218评论 2 33
  • 1. 简介 本文档包含DPDK软件安装和配置的相关说明。旨在帮助用户快速启动和运行软件。文档主要描述了在Linux...
    半天妖阅读 17,990评论 0 22
  • make menuconfig过程解析作者 codercjg 在 28 九月 2015, 5:27 下午 make...
    codercjg阅读 992评论 0 1
  • 定价调整偏见:谈判报价时,一定要做好市场调研,摸清对方的底线,锁定理想价格,谈判成功率高。 权利有限策略:谈判时学...
    小笨鱼王月阅读 131评论 0 0