ubuntu11.10编译添加helloworld驱动
系统 ubuntu11.10 desk
在系统目录/usr/src/ 有linux版本的文件夹但是没有代码在里面,可以直接将
helloworld的驱动代码放到drivers目录下进行编译安装,这样就不必要搞个开发板或是更新kernel来测试了。
hello world 驱动代码如下:
hello_printk.c
/* * "Hello, world!" minimal kernel module * * Valerie Henson <val@nmt.edu> * */ /* * The below are header files provided by the kernel which are * required for all modules. They include things like the definition * of the module_init() macro. */ #include <linux/init.h> #include <linux/module.h> /* * This is the init function, which is run when the module is first * loaded. The __init keyword tells the kernel that this code will * only be run once, when the module is loaded. */ static int __init hello_init(void) { printk("Hello, world!\n"); return 0; } /* * The below macro informs the kernel as to which function to use as * the init function. */ module_init(hello_init); /* * Similary, the exit function is run once, upon module unloading, and * the module_exit() macro identifies which function is the exit * function. */ static void __exit hello_exit(void) { printk("Goodbye, world!\n"); } module_exit(hello_exit); /* * MODULE_LICENSE() informs the kernel what license the module source * code is under, which affects which symbols it may access in the * main kernel. Certain module licenses will "taint" the kernel, * indicating that non-open or untrusted code has been loaded. * Modules licensed under GPLv2 do not taint the kernel and can access * all symbols, but declaring it so is a legal statement that the * source code to this module is licensed under GPLv2, and so you must * provide the source code if you ship a binary version of the module. */ MODULE_LICENSE("GPL"); MODULE_AUTHOR("Valerie Henson <val@nmt.edu>"); MODULE_DESCRIPTION("\"Hello, world!\" minimal module"); MODULE_VERSION("printk");
Makefile文件
# obj-m is a list of what kernel modules to build. The .o and other # objects will be automatically built from the corresponding .c file - # no need to list the source files explicitly. obj-m := hello_printk.o # KDIR is the location of the kernel source. The current standard is # to link to the associated source tree from the directory containing # the compiled modules. KDIR := /lib/modules/$(shell uname -r)/build # PWD is the current working directory and the location of our module # source files. PWD := $(shell pwd) # default is the default make target. The rule here says to run make # with a working directory of the directory containing the kernel # source and compile only the modules in the PWD (local) directory. default: $(MAKE) -C $(KDIR) M=$(PWD) modules
测试helloworld驱动
- 将helloworld文件copy到 drivers/hello_printk 目录下。
warren@ubuntu:~$ cd /usr/src/
warren@ubuntu:/usr/src$ ls
linux-headers-3.0.0-12 linux-headers-3.0.0-22
linux-headers-3.0.0-12-generic linux-headers-3.0.0-22-generic-pae
warren@ubuntu:/usr/src$ cd linux-headers-3.0.0-22-generic-pae/drivers
warren@ubuntu:/usr/src/linux-headers-3.0.0-22-generic-pae/drivers$ ls
accessibility cpuidle ide mfd ps3 telephony
acpi crypto idle misc ptp thermal
amba dca ieee802154 mmc rapidio tty
ata dio infiniband mtd regulator uio
atm dma input net rtc usb
auxdisplay edac isdn nfc s390 uwb
base eisa Kconfig nubus sbus vhost
bcma firewire leds of scsi video
block firmware lguest parisc sfi virtio
bluetooth gpio macintosh parport sh vlynq
cdrom gpu Makefile pci sn w1
char hello_printk mca pcmcia spi watchdog
clk hid md platform ssb xen
clocksource hwmon media pnp staging zorro
connector hwspinlock memstick power target
cpufreq i2c message pps tc
2.编译helloworld
进入hello_printk 目录 直接运行 make,
warren@ubuntu:/usr/src/linux-headers-3.0.0-22-generic-pae/drivers/hello_printk$ sudo make
[sudo] password for warren:
make -C /lib/modules/3.0.0-22-generic-pae/build M=/usr/src/linux-headers-3.0.0-22/drivers/hello_printk modules
make[1]: Entering directory `/usr/src/linux-headers-3.0.0-22-generic-pae'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-22-generic-pae'
warren@ubuntu:/usr/src/linux-headers-3.0.0-22-generic-pae/drivers/hello_printk$ ls
hello_printk.c hello_printk.ko hello_printk.mod.c hello_printk.mod.o hello_printk.o Makefile modules.order Module.symvers
hello_printk.ko 即是我们需要的文件
3.安装hello_printk.ko
通过insmod命令加载ko文件
warren@ubuntu:/usr/src/linux-headers-3.0.0-22-generic-pae/drivers/hello_printk$ sudo insmod hello_printk.ko
通过lsmod查看是否加载成功
warren@ubuntu:/usr/src/linux-headers-3.0.0-22-generic-pae/drivers/hello_printk$ lsmod
Module Size Used by
hello_printk 12393 0
rfcomm 38408 0
bnep 17923 2
bluetooth 148869 10 rfcomm,bnep
parport_pc 32114 0
ppdev 12849 0
通过dmesg命令可以查看ko文件安装时产生的log
warren@ubuntu:/usr/src/linux-headers-3.0.0-22-generic-pae/drivers/hello_printk$ dmesg
。。。。。。。。。。。。。
[ 92.995971] wlan0: authenticated
[ 92.997230] wlan0: associate with 00:25:86:3c:30:08 (try 1)
[ 93.000434] wlan0: RX AssocResp from 00:25:86:3c:30:08 (capab=0x431 status=0 aid=9)
[ 93.000442] wlan0: associated
[ 93.001095] ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
[ 103.301232] wlan0: no IPv6 routers present
[ 3458.696379] Hello, world!
在log最后我们看到了 我们想要的结果,这就表明驱动安装成功了。
4.卸载hello_printk.ko
rmmod命令卸载
warren@ubuntu:/usr/src/linux-headers-3.0.0-22-generic-pae/drivers/hello_printk$ sudo rmmod hello_printk
再用lsmod查看时看不到hello_printk的
warren@ubuntu:/usr/src/linux-headers-3.0.0-22-generic-pae/drivers/hello_printk$ dmesg
[ 93.001095] ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
[ 103.301232] wlan0: no IPv6 routers present
[ 3458.696379] Hello, world!
[ 3936.923792] Goodbye, world!
查看log信息可以看到Goodbye, world! 表明已经成功卸载了文件。
浙公网安备 33010602011771号