Forum Discussion
Altera_Forum
Honored Contributor
21 years agoThis message will describe building a hello_world driver for the kernel. The driver will be listed as a miscellaneous driver inside the kernel configuration tool.
(1) Create the code for the driver (hello_world.c):#include <linux/init.h># include <linux/module.h># include <linux/kernel.h>
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);
MODULE_LICENSE("Dual BSD/GPL"); (2) Create the Makefile for the driver: ifneq ($(KERNELRELEASE),)
# This is the section of the Makefile that builds the module inside the kernel
obj-$(CONFIG_HELLO_WORLD) := hello_world.o
else
# This section is for building the module outside of the source # $(ECLIPSE_WORKSPACE) is assumed to be defined externally by the environment
# please reference the articles on http://www.lwn.net regarding# kernel module development for more information about this Makefile
KDIR := $(KERNEL_PLUGIN)/linux-2.6.x
KERNEL_PROJECT := eval_kit_0
BUILDDIR := $(ECLIPSE_WORKSPACE)/$(KERNEL_PROJECT)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) O=$(BUILDDIR) SUBDIRS=$(PWD) modules
endif (3) Copy the two files into the source tree using the following few commands: cd <wherever the above files live>
mkdir $(KERNEL_PLUGIN)/linux-2.6.x/misc/hello_world
cp * $(KERNEL_PLUGIN)/linux-2.6.x/drivers/misc/hello_world (4) Update the Makefile and Kconfig file in the $(KERNEL_PLUGIN)/linux-2.6.x/drivers/misc directory... ## Makefile for misc devices that really don't fit anywhere else.#
obj- := misc.o # Dummy rule to force built-in.o to be made
obj-$(CONFIG_IBM_ASM) += ibmasm/
# add the following line
obj-$(CONFIG_HELLO_WORLD) += hello_world/ ## Misc strange devices#
menu "Misc devices"
config IBM_ASM
tristate "Device driver for IBM RSA service processor"
depends on X86
default n
---help---
This option enables device driver support for in-band access to the
IBM RSA (Condor) service processor in eServer xSeries systems.
The ibmasm device driver allows user space application to access
ASM (Advanced Systems Management) functions on the service
processor. The driver is meant to be used in conjunction with
a user space API.
The ibmasm driver also enables the OS to use the UART on the
service processor board as a regular serial port.
If unsure, say N.
# add the following few lines
config HELLO_WORLD
tristate "Hello_world example"
default n
endmenu After saving all the modified files, you can now configure a kernel with the above driver. It is recommended that the driver be compiled into the kernel for testing purposes since that should help with the debugging effort. The kernel option will appear in Device Drivers -> Miscellaneous -> Hello_world example. Once you have configured, built, and uploaded your kernel, the kernel should start and display: "Hello, World!" while it is starting up. Much of the above code was taken from LWN.net's Device driver series of articles. Hope this helps... Ken. BTW, debugging is discussed within the reference manual I believe... I may post some additional instructions up here later on if people have trouble with debugging the kernel.