ok6410 android driver(3)
This article discusses the Makefile and how to port the module to different platform (localhost and arm).
1、localhost
My localhost system is debian wheezy.
If you would like to set up the environment for user-mode, you can follow the website :
http://www.cnblogs.com/plinx/p/3149216.html
Acturely, we could also just use the $(shell uname -r) kernel version in our system.
The different between these two is which command we need to enter for 'make'.
//user-mode $ make //$(shell uname -r) $ sudo make
Here is the Makefile :
obj-m := word_count1.o obj-m += word_count2.o KDIR := /lib/modules/$(shell uname -r)/build #KDIR := /home/linx/Android/forlinx/android2.3_kernel_v1.01 PWD := $(shell pwd) #ARCH := arm #CROSS := arm-linux- modules: $(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS) -C $(KDIR) M=$(PWD) modules allclean: rm -f *.o *.ko *.mod.c *.mod.o clean: rm -f *.o *.mod.c *.mod.o .PHONY : allclean .PHONY : clean
If you would like to use the kernel you compile for device driver, just correct the $(shell uname -r) to your directory.
In the word_count2.c(in previous article), we create a miscdevice(a character divice).
Device driver provides a mechanism rather than policy.
To the word_count2.c, we know we could do 'write and read' on the miscdevice 'wordcount2'.
Two ways to test the device driver :
(1) shell communicate
I write the command in a test2.sh file :
#!/bin/sh devpath="/dev/wordcount2" echo ">> Insmoding word_count2.ko" sudo insmod word_count1.ko if [ -c "$devpath" ]; then echo ">> Writting to wordcount..." echo ">> The world count program." > $devpath echo "" echo ">> Reading the wordcount..." sudo cat /dev/wordcount2 echo "------------------------" sudo rmmod word_count2 echo ">> Rmmoding word_count2.ko" echo "" echo "----/var/log/syslog-----" sudo dmesg | grep word | tail -n 5 echo "------------------------" echo "" else echo ">> The word count modules doesn't exits." fi
(2) user program communicate
The following is the ftest.c :
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { int testdev; int ret; int num = 0; unsigned char buf[4]; testdev = open("/dev/wordcount2", O_RDWR); if(testdev == -1) { printf("Can not open file.\n"); return 0; } if(argc > 1) { printf("Strings : %s.\n", argv[1]); write(testdev, argv[1], strlen(argv[1])); } read(testdev, buf, 4); num = ((int)buf[0]) << 24 | \ ((int)buf[1]) << 16 | \ ((int)buf[2]) << 8 | \ ((int)buf[3]); printf("Word Byte : 0x%d%d%d%dH\n", buf[0], buf[1], buf[2], buf[3]); printf("word count: %d\n", num); close(testdev); return 0; }
I also write the command in a test3.sh file too :
#!/bin/sh devpath="/dev/wordcount2" echo ">> Insmod the wordcount2..." sudo insmod word_count2.ko if [ -c "$devpath" ]; then echo "" echo ">> ./ftest \"test string 1 2 3\"" echo "" ./ftest wordcount2 "test string 1 2 3" sudo rmmod word_count2 echo ">> Rmmoding word_count2.ko" echo "" echo "----- /var/log/syslog ------" sudo dmesg | grep word | tail -n 5 echo "----------------------------" else echo ">> $devpath doesn't exits" fi
2、ok6410-android
If we want to compile the device drive into an ARM platform, we have to get this :
(1) cross compiler
arm-linux- (the compiler support on the cd of forlinx)
or arm-eabi- (the compiler support on the google android source code)
(2) source code compiled on arm platform
Here I got two piece of kernel-source code :
Android-2.3_kernel (support by forlinx)
goldfish-2.6.29_kernel (support by google)
Then we just need to correct the Makefile path.
After tested, we know :
(1) the module works properly with two cross compiler and Android-2.3_kernel.
(2) the module doesn't work on the goldfish-2.6.29_kernel.
TIPS :
When rmmod, there may wanning : rmmod : chdir(/lib/modules): No such file or directory
[Fix]
Copy the file to /lib/modules/$(uname -r)/
If there is not this directory, create it.
3、goldfish emulator
The same with ok6410 except two different :
(1) No need to create the directory /lib/modules/$(uname -r)/
we can insmod and rmmod in any directory
(2) When we configure the kernel, we should use the goldfish_armv7_defconfig and enable 'Enable loadable module support' and his subproject.