/*
* hello-1.c - The simplest kernel module.
*/
#include /* Needed by all modules */
#include /* Needed for KERN_INFO */
#define DRIVER_AUTHOR "Peiter Jay Salzman "
#define DRIVER_DESC "A sample driver"
int init_module(void);
void cleanup_module(void);
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
/* You can use strings, like this:
* */
MODULE_LICENSE("GPL"); // Get rid of taint message by declaring code as GPL.
/* Or with defines, like this:
* */
MODULE_AUTHOR(DRIVER_AUTHOR); // Who wrote this module?
MODULE_DESCRIPTION(DRIVER_DESC); // What does this module do?
/* This module uses /dev/testdevice. The MODULE_SUPPORTED_DEVICE macro might be used in
* * the future to help automatic configuration of modules, but is currently unused other
* * than for documentation purposes.
* */
MODULE_SUPPORTED_DEVICE("testdevice");
and the associated Makefile
# linux sparc64 basic module makefile example
# rem for sparc64 by N. Lemouel
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -m64 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc-3.3
OBJS := ${patsubst %.c, %.o, ${wildcard *.c}}
all: ${OBJS}
.PHONY: all
clean:
rm -rf *.o
You can now test your basic module, as root execute the following commands :
spark:/home/stellae/devels/module/basic# insmod ./hello-1.o
spark:/home/stellae/devels/module/basic# rmmod hello-1
spark:/home/stellae/devels/module/basic# cat /var/log/messages | tail -n 5
Nov 3 00:46:13 localhost kernel: MII transceiver found at address 24, status 7809.
Nov 3 00:46:13 localhost kernel: Enabling bus-master transmits and whole-frame receives.
Nov 3 00:46:13 localhost kernel: 02:01.0: scatter/gather enabled. h/w checksums enabled
Nov 3 00:46:22 localhost kernel: Hello world 1.
Nov 3 00:46:33 localhost kernel: Goodbye world 1.
\\
Article by Stellae (N. Lemouël) //November, 2nd 2006//