Writing a Linux Driver
The task of integrating the driver into the kernel includes several steps:
Insert kernel calls to the new driver.
Add the driver to the list of drivers.
Modify compilation scripts.
Re-compile the driver.
The insertion of the OS call to mrv4_init() is done in the /usr/src/linux/kernel/mem.c file. The other driver function calls (open, read, write, ioctl, release, etc.) are user transparent. They are carried out through the file_operations structure. A driver major number must be added to the list located at /usr/include/linux/major.h. Search for a free driver number; for example, if number 62 is free, you must add one or both of the following lines to the file, depending on the Linux release:
/dev/mrv4 62 #define MRV4_MAJOR 62
Each device is referenced by one major and one minor number. The major number represents the number of the driver. The minor number distinguishes between several devices which are controlled by the same device (e.g., several hard disks controlled by the same IDE driver: hd0, hd1, hd2).
The next step is to create a logical device to access the driver. You must use the command mknod in this way:
mknod -m og+rw /dev/mrv4 c 62 0
where 62 is the major, 0 the minor (only one physical device) and c indicates a character device. Set the permissions as necessary, although you can modify them later with the command chmod. For example, enable rw if you want to allow all users to access the device:
crw-rw-rw-2 bin bin 62, 0 Mar 12 1997 /dev/mrv4
To allow driver compilation within the kernel, the following lines must be added to the script file /usr/src/linux/arch/i386/config.in:
comment 'MRV 4' bool 'MRV 4 card support' CONFIG_MRV4
and the following lines to /usr/src/linux/drivers/char/Makefile:
ifdef CONFIG_MRV4 L_OBJS += mrv4.o endifIt is recommended that the driver be compiled alone, before linking the kernel. This method will save time testing syntax errors:
cd /usr/src/linux/drivers/char gcc -c mrv4.c -Wall -D__KERNEL__And when all is well, delete the object file:
rm -f mrv4.oNext, configure the kernel by typing:
cd /usr/src/linux make configAnswer yes when the script asks you about installing the MRV-4 driver (this sets the constant CONFIG_MRV4). Finally, insert an empty floppy disk and re-build the kernel by typing the following commands:
make zdisk # generate a bootable # floppy disk dev -R /dev/fd0 1 # disable writes to<\n> # floppyOnce you are sure that the kernel works, you can overwrite the file vmlinuz with the new kernel. To test the new kernel, restart the system (type reboot) and ... good luck! There are no debuggers available.
If the kernel seems to work, you might test the driver by writing one or more user programs, i.e., mrv4test.c which call the driver functions:
fid = open("/dev/mrv4", ...); read(fid, ...); write(fid, ...); ioctl(fid, ...); close(fid);
You can obtain privileged documentation at sunsite.unc.edu (see Resources). But of course, you will never be able to write your own driver using only the general guidelines of this article. To facilitate this task, we supply the source files for a dummy driver for Linux 2.0.24, which is a model for character driver development. It simulates the equation y = a x and includes an example of interrupt management (which does not work since it is not associated with any hardware). Its name is foo, since Linux already has a driver called dummy. These files are:
README: summary of instructions to install it
foo.c: driver source file
foo.h: driver header file
footest.c: program for driver testing
You can obtain these files via anonymous FTP at ftp://ftp.linuxjournal.com/pub/lj/listings/issue48/2476.tgz.
Fernando Matía is an Associate Professor at the Universidad Politecnica de Madrid (UPM). He was born in Madrid, Spain, in 1966. He became an Industrial Engineer at UPM in 1990 and received his Ph.D. degree at UPM in 1994 in the area of Control Engineering. He works at the Systems and Automatic Control Engineering Division (DISAM). His main activities are Intelligent Control, Fuzzy Control, Robotics and Computer Sciences. He can be reached at email@example.com.
- Give new life to old phones and tablets with these tips!
- Readers' Choice Awards 2014 Poll
- Memory Ordering in Modern Microprocessors, Part I
- Source Code Scanners for Better Code
- Tech Tip: Really Simple HTTP Server with Python
- Linux Systems Administrator
- Senior Perl Developer
- Putlocker!! Watch Begin Again Online 2014 Streaming Full Movie