Friday 27 March 2015

Writing your first device driver on Fedora19

Steps:
1. Create a C program.
2. Compile C program to get .o file
3. Convert .o file to .ko (kernel object) file
4. Insert .ko file
5. Check the existence of .ko file
6. Remove .ko file

ofd.c source code

/* ofd.c – Our First Driver code */
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>

static int __init ofd_init(void) /* Constructor */
{
    printk(KERN_INFO "Hello : ofd registered");
    return 0;
}

static void __exit ofd_exit(void) /* Destructor */
{
    printk(KERN_INFO "Goodbye : ofd unregistered");
}

module_init(ofd_init);
module_exit(ofd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("VIIT Comp Dept");
MODULE_DESCRIPTION("Our First Driver");

Contents of MakeFile

 # Makefile – makefile of our first driver
 
# if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq (${KERNELRELEASE},)
    obj-m := ofd.o
# Otherwise we were called directly from the command line.
# Invoke the kernel build system.
else
KERNEL_SOURCE := /usr/src/kernels/3.18.7-100.fc20.x86_64
PWD := $(shell pwd)
default:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules
 
clean:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif


Execution snapshot on Fedora 19 machine


1 comment: