Loadable Modules & the Linux 2.6 Kernel 

by Daniele Paolo Scarpazza 





Example 1: 



module_param(my_integer_parameter, int, S_IRUSR | S_IWUSR ); 

MODULE_PARM_DESC(my_integer_parameter, "An integer parameter");

module_param(my_string_parameter, charp, S_IRUSR | 

                                  S_IWUSR | S_IRGRP | S_IROTH);

MODULE_PARM_DESC(my_string_parameter, "A character string parameter");



Example 2: 



# make

make -C /usr/src/linux-2.6.5-1.358 SUBDIRS=/root/sysfs_example modules

make[1]: Entering directory `/usr/src/linux-2.6.5-1.358'

  CC [M]  /root/sysfs_example/sysfs_example.o

  Building modules, stage 2.

  MODPOST

  CC      /root/sysfs_example/sysfs_example.mod.o

  LD [M]  /root/sysfs_example/sysfs_example.ko

make[1]: Leaving directory `/usr/src/linux-2.6.5-1.358'

# insmod sysfs_example.ko 

# cd /sys

# ls

block  bus  class  devices  firmware  power  sysfs_example

# cd sysfs_sample

# ls

hex  integer  string

# cat hex

0xdeadbeef

# cat integer 

123

# cat string

test

# echo 0xbabeface > hex

# echo 456 > integer

# echo hello > string 

# cat hex

0xbabeface

# cat integer 

456

# cat string 

hello

# cd ..

# rmmod sysfs_example

# ls

block  bus  class  devices  firmware  power





Listing One



#include <linux/module.h>

#include <linux/config.h>

#include <linux/init.h>



MODULE_LICENSE("GPL");

static int __init minimal_init(void) 

{

  return 0;  

}

static void __exit minimal_cleanup(void) 

{

}

module_init(minimal_init);

module_exit(minimal_cleanup);





Listing Two



obj-m   := your_module.o

KDIR    := /usr/src/linux-$(shell uname -r)

PWD := $(shell pwd)



default:

    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

install: default

    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install

clean:

    rm -rf *.o *.ko .*.cmd *.mod.c .tmp_versions *~ core







2



