linux 模块驱动开发第一弹 helloworld

学习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!
至此,驱动开发第一弹完成。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值