Module Programming

cancel
Showing results for 
Search instead for 
Did you mean: 

Module Programming

Module Programming

 

 

If you will write driver, try out the hello world module example in the (must have) book "Linux Device Driver 3rd ed.".

Add driver source

In your kernel dir, eg, nios2-linux/linux-2.6, (NOT in the uClinux-dist dir)

save the file to drivers/misc/hello.c

#include <linux/init.h>

#include <linux/module.h>

 

MODULE_LICENSE("Dual BSD/GPL");

 

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);

Add these lines to drivers/misc/Kconfig, before the "#endif" line.

config HELLO

   tristate "example hello module"

   help

     Enable example hello module.

Add the line to drivers/misc/Makefile

obj-$(CONFIG_HELLO) += hello.o

Device node

If you want to use a new device node, you can add it to uClinux-dist/vendors/Altera/nios2/romfs_list

 

Platform devices

Platform devices are created by platform-specific code in nios2-linux/linux-2.6/arch/nios2/kernel/config.c

Unless this file is modified to create a platform device, probing your module will fail.

Save your works with git

It is suggested that you create a local branch using git in advance. Then add these files, and save them with commits.

Please refer to git tutorial. eg, assume you are at the official "test-nios2" branch.

 

git branch

* test-nios2

 

# rename the branch as your own experimental branch

git branch -M test-nios2 test-hello

 

git branch

* test-hello

 

# add/edit files with text editor

 

# save changes with git

git add drivers/misc/hello.c

git add drivers/misc/Kconfig

git add drivers/misc/Makefile

git commit -s -m "hello: example kernel driver"

 

# take a look!

git log

# Or

gitk

 

# You may pull updates to this branch. And save your future works with this experimental branch.

git pull origin test-nios2

 

# You can also create a new official "test-nios2" which was gone with previous renaming.

git branch --track test-nios2 origin/test-nios2

 

git branch

* test-hello

 test-nios2

 

# You can switch between branches

git checkout test-nios2

# Or

git checkout test-hello

Compile-in the driver

 

Go back to uClinux-dist, (NOT in the kernel source tree linux-2.6)

 

cd nios2-linux/uClinux-dist

 

Enable your driver in kernel using "make menuconfig", the driver will be "compile-in" -- run directly at boot.

Device Drivers -->

Misc devices --->

<*> example hello module

Save, make and boot. Then you should see the message "Hello, world" displayed.

Build loadable module

If you want to be able to load the module after kernel boot,

then enable module support in kernel and build the hello.ko module.

 

Still in uClinux-dist, (NOT in the kernel source tree linux-2.6)

 

In kernel selection,

Loadable module support -->

[*] Enable loadable module support

[*] Module unloading

 

Device Drivers -->

Misc devices --->

<M> example hello module

In apps selection, (this should be selected already with default config)

Busybox -->

[*] insmod

[*] insmod: lsmod

[*] insmod: modprobe

[*] insmod: rmmod

[ ] insmod: Pre 2.1 kernel modules

[ ] insmod: 2.1 - 2.4 kernel modules

[*] insmod: 2.6 and above kernel modules

[ ] insmod: Model version checks

[ ] insmod: Support tainted module checking with new kernels

 

Then, in ~/uClinux-dist dir, rebuild with,

make user/busybox_clean

make

The modules objects, (eg hello.ko) will be installed in romfs/lib/modules tree.

Boot nios2 uClinux, and load module

modprobe hello

There is an (outdated) example driver in Chinese from Alex.liu, http://www.icwin.net/ShowArtitle.ASP?art_id=8452&cat_id=52 

Build module alone (not recommended for newbie)

You can compile the hello module alone, without building the whole kernel.

In the module dir, linux-2.6/drivers/misc, compile it with,

make ARCH=nios2 CROSS_COMPILE=nios2-linux-uclibc- -C ~/nios2-linux/uClinux-dist/linux-2.6.x M=`pwd` modules

nios2-linux-uclibc-strip -R .comment -R .note -g --strip-unneeded hello.ko

Then you can transfer the module file, hello.ko, to your board with ftp or nfs mount if you have ethernet port, and try out with "insmod hello.ko" and "rmmod hello". Or you may replace the old module at /lib/modules/2.6.27-rc1/kernel/drivers/misc , then you can use "modprobe hello".

You can save the time to rebuild the whole kernel, download and reboot.

 

Or if you want to build module outside the kernel tree, ie external module, create a Makefile in your module dir, with only one line,

obj-m := hello.o

Then build it with the same commands above.

 

For details, please read,

linux-2.6/Documentation/kbuild/modules.txt

linux-2.6/Documentation/kbuild/makefiles.txt 

Version history
Last update:
‎12-19-2022 11:42 AM
Updated by: