![]() |
|
|
|
|
|
Writing a Linux device driver
By Aditya Kulkarni <aditya@freeos.com>
What do I need to know about writing drivers? Basic knowledge of kernel compilation, a good deal of programming The first thing a programmer must know before attempting to write a Choosing the device type a) Block drivers A block device is something that can host a filesystem such as a disk. A b) Character drivers A character device is one that can be accessed like a file, and a char Device drivers in Linux are known as modules and can be loaded dynamically eg: A simple module #define MODULE int init_module (void) /* Loads a module in the kernel */ void cleanup_module(void) /* Removes module from kernel */ Compiling the module # gcc -c hello.c The output is Hello kernel # rmmod hello.o GoodBye Kernel init_module loads the relocated module image into kernel space and runs the module's init function. The module image begins with a module structure and is followed by code The module structure is defined as follows: struct module
Return Values On success, zero is returned. On error, -1 is returned Errors EPERM The user is not the superuser. ENOENT No module by that name exists. EINVAL Some image slot filled in incorrectly, image->name EBUSY The module's initialization routine failed. EFAULT name or image is outside the program's accessible cleanup_module attempts to remove an unused loadable module entry. If name is NULL, all unused modules marked auto clean will be removed. Return Values On success, zero is returned. On error, -1 is returned and errno is Errors EPERM The user is not the superuser. ENOENT No module by that name exists. EINVAL name was the empty string. EBUSY The module is in use. EFAULT name is outside the program's accessible address General flags used for compiling any driver are -D__KERNEL__ _DMODULE -O -Wall -I$(INCLUDEDIR) Note: The INCLUDEDIR should contain the header files of the kernel source. Module code has to be recompiled for each version of the kernel that it
Other articles by Aditya Kulkarni
Current Rating: [ 5.32 / 10 ]
Number of Times Rated: [ 1810 ]
|
|
|
© 1998-2004 FreeOS Technologies (I) Pvt. Ltd. All rights reserved.
[Privacy Policy]
![]() |