学习linux有一段时间了,按照传统还是把第一弹交给hellworld,笔者不知道打破这个传统意味着什么,
但笔者是个好孩子,考虑再三还是因循守旧比较稳妥~_~
hellworld.c
/*
* hello.c - The first kernel module programming
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
static int hello_init(void)
{
printk(KERN_INFO "Hello world!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_INFO "Goodbye cruel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("SXBG");
MODULE_VERSION("V1.0");
MODULE_DESCRIPTION("The first test program!");
MODULE_LICENSE("GPL");
Makefile
#
# This file is write for the first drivers
# Copyright (c) 2013 sxbg
obj-m := hello.o
#hello-objs := hello.o
ARM_LINUX_KERNEL := /home/sxbg/Downloads/linux
CURRENT_PATH := $(shell pwd)
all:
$(MAKE) -C $(ARM_LINUX_KERNEL) M=$(CURRENT_PATH) modules
clean:
$(MAKE) -C $(ARM_LINUX_KERNEL) M=$(CURRENT_PATH) clean
加载驱动模块
root@FZ:/mydriver# insmod hello.ko
Hello world!
卸载模块
root@FZ:/mydriver# rmmod hello
rmmod: chdir(/lib/modules): No such file or directory
查阅资料得知缺少指定的目录
root@FZ:mkdir -p lib/modules/2.6.32.2
root@FZ:/mydriver# rmmod hello
Goodbye cruel world!
rmmod: module 'hello' not found
出现
rmmod: module 'hello' not found看着挺碍眼的,查阅资料此是busybox的bug解决办法是:
在开辟板卸载模块的时辰可以卸载模块,不过会一向有如许一个提示:
rmmod: module ""×××"" not found
应用如下源码生成rmmod号令,就可以没有任何提示的卸载模块了
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
const char *modname = argv[1];
int ret = -1;
int maxtry = 10;
while (maxtry-- > 0) {
ret = _module(modname, O_NONBLOCK | O_EXCL);//体系调用sys__module
if (ret < 0 && errno == EAGAIN)
usleep(500000);
else
break;
}
if (ret != 0)
printf("Unable to unload driver module \"%s\": %s\n",
modname, strerror(errno));
}
#arm-linux-gcc rmmod.c -o rmmod
在S3C2440上
#mv /sbin/rmmod /sbin/rmmod2
#mv rmmod /sbin/rmmod
root@FZ:/mydriver# rmmod hello
Goodbye cruel world!
至此,驱动开发第一弹完成。
linux 模块驱动开发第一弹 helloworld
最新推荐文章于 2024-01-20 22:11:39 发布