<!-- @page { margin: 0.79in } TD P { margin-bottom: 0in } P { margin-bottom: 0.08in } -->
LDD3 driver example run on ARM platform (1)
平台: i.mx31 3-stack (arm11)
内核版本 : Linux-2.6.31
代码:
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 ?= /home/mx31/codes/linux-2.6-imx/
# 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 hellop.o
endif |
注: KERNELDIR指向你的平台所使用的内核树目录。
hello.c
/*
* $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $
*/
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world/n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world/n");
}
module_init(hello_init);
module_exit(hello_exit); |
hellop.c
/*
* $Id: hellop.c,v 1.4 2004/09/26 07:02:43 gregkh Exp $
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
MODULE_LICENSE("Dual BSD/GPL");
/*
* These lines, although not shown in the book,
* are needed to make hello.c run properly even when
* your kernel has version support enabled
*/
/*
* A couple of parameters that can be passed in: how many times we say
* hello, and to whom.
*/
///内核参数定义及声明 static char *whom = "world";
static int howmany = 1;
module_param(howmany, int, S_IRUGO); ///设置参数 howmany,使其可以通过命令行传递 module_param(whom, charp, S_IRUGO); ///S_IRUGO表示访问 sysfs中该模块参数的权限是只读
static int hello_init(void)
{
int i;
for (i = 0; i < howmany; i++)
printk(KERN_ALERT "(%d) Hello, %s/n", i, whom);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world/n");
}
module_init(hello_init);
module_exit(hello_exit); |
编译命令:
make ARCH=arm CROSS_COMPILE=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-
运行结果:
$ /home$ insmod hello.ko
Hello, world
$ /home$ insmod hellop.ko whom="Michael" howmany=3
(0) Hello, Michael
(1) Hello, Michael
(2) Hello, Michael
$ /home$ cat /sys/module/hellop/parameters/howmany
3
$ /home$ cat /sys/module/hellop/parameters/whom
Michael
|