linux更新驱动脚本,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 /* 定义了一些相关的宏 */

#include /* 定义了模块需要的*/

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值