130 likes | 262 Views
Implementation of Embedded OS. Lab3 Linux Kernel Modules. Goal. Learn how to write Linux kernel modules. * source: The Linux Device Drivers, 3rd. Environment. Host System Windows XP Build System VirtualBox + Ubuntu 8.04 Target System Creator XScale PXA270 Software
E N D
Implementation of Embedded OS Lab3 Linux Kernel Modules
Goal Learn how to write Linux kernel modules. / 13 * source: The Linux Device Drivers, 3rd
Environment • Host System • Windows XP • Build System • VirtualBox + Ubuntu 8.04 • Target System • Creator XScale PXA270 • Software • Drivers for Creator PXA270 LCD, 8-bit LED lamps, and 4-digit 7-segment LED. • You can download them from RSWiki IEOS Course Software / 13
First Step / 13 • Finish lab2-kernel first. • Please make sure you have modified the flash partition in Linux and flashed the correct file system (20MB).
Linux Device Drivers User Mode Application System Call Interface Virtual File System (VFS) Kernel Mode Buffer Cache Network Subsystem Character Device Driver Block Device Driver Network Device Driver Device Interface Physical Device Hardware / 13 • Three major types • Character device driver • Block device driver • Network device driver • Other types • USB driver • PCI driver • ……
A Simple Kernel Module #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_INFO */ #include <linux/init.h> /* Needed for the macros */ static int __init init_hello(void) { printk(KERN_INFO "Hello, world\n"); return 0; } static void __exit cleanup_hello(void) { printk(KERN_INFO "Goodbye, world\n"); } module_init(init_hello); module_exit(cleanup_hello); MODULE_LICENSE("GPL"); /* License type of this module */ MODULE_AUTHOR("DRIVER_AUTHOR"); /* Who wrote this module */ MODULE_DESCRIPTION("DRIVER_DESC"); /* What does this module do */ initial function cleanup function declaration of initial/cleanup functions / 13
Makefile for Kernel Modules obj-m += hello.o all: make -C <kernel path>M=$(PWD) modules clean: make -C <kernel path>M=$(PWD) clean / 13 • Suppose the filename of the module source is hello.c. • Write a Makefile like this: • Type “make” to compile the module. • Then transfer the output file hello.ko to the target board. • Type “insmod hello.ko” to load the module and “rmmod hello” to remove the module.
Module Parameters /* Needed for the macro module_param */ #include <linux/moduleparam.h> static int myint = 0; /* define a integer variable */ /* declare the variable as a module parameter */ module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); / 13 • You can define module parameters as follows. • You can pass the argument when loading the module as follows. • $ insmod hello.komyint=123 • Further information about module parameters can be found on the websites listed in References.
Virtual File System (VFS) (1/2) devno = MKDEV(MAJOR_NUM, MINOR_NUM); register_chrdev_region(devno, MAX_MINORS, MODULE_NAME)); cdev_init(pcdev, &fops); cdev_add(pcdev, devno, MAX_MINORS); / 13 • User processes can communicate with device drivers through several ways, and VFS is the most common one. • A device driver can define a set of file operations including open, read, write, ioctl, etc., and associate it with an device number (inode).
Virtual File System (VFS) (2/2) / 13 • A process can use ordinary file I/O functions to access the device. • First, create the corresponding file at /dev. • $ mknod /dev/demo c <MAJOR_NUM> <MINOR_NUM> • In lab2, the major number is 120 and the minor number is 0. • Then, open the file /dev/demo and use the function ioctl to control the device.
An Example of the User Programs #include <unistd.h> #include <sys/fcntl.h> #include <sys/ioctl.h> #include <stdio.h> #include "creator_pxa270_lcd.h" int main() { int fd; if((fd = open("/dev/demo", O_WRONLY)) < 0){ perror("couldn't open /dev/demo"); return 1; } ioctl(fd, _7SEG_IOCTL_ON, 0); close(fd); return 0; } / 13 Please use the cross compiler to compile this program.
Lab Requirements / 13 • Compile the kernel module. • Write a Makefile for the driver provided. • Compile the driver as a module and load it into Linux. • Implement a stopwatch app. • The resolution should be in 0.1 seconds. • The value of the counter should be display on the 4-digit 7-segment LED in decimal form. • It can be started, suspended, resumed, and reset by pressing the buttons of the keypad. • Please refer to the reference PDFs available on our course website if you have any questions about the driver.
References / 13 • Linux Device Drivers, Third Edition • http://lwn.net/Kernel/LDD3/ • The Linux Kernel Module Programming Guide • http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html