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 matia@disam.upm.es.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- RSS Feeds
- New Products
- Using Salt Stack and Vagrant for Drupal Development
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- New Products
- Tech Tip: Really Simple HTTP Server with Python
- Ahh, the Koolaid.
3 hours 23 min ago - git-annex assistant
9 hours 23 min ago - direct cable connection
9 hours 45 min ago - Agreed on AirDroid. With my
9 hours 55 min ago - I just learned this
9 hours 59 min ago - enterprise
10 hours 30 min ago - not living upto the mobile revolution
13 hours 21 min ago - Deceptive Advertising and
13 hours 56 min ago - Let\'s declare that you have
13 hours 57 min ago - Alterations in Contest Due
13 hours 58 min ago




Comments
I like this one
Good introduction, I think, for those who is not familiar with Linux drivers at all.
Thanks a lot to the author. I enjoyed the article.
This article twice uses the
This article twice uses the term "protected mode" where it should be using the term "supervisor mode".
Protected mode is a mode of the Intel x86 processor which provides various protection features, such as memory protection and the ability to disable privileged instructions. Under Linux, all software runs in protected mode, but user applications run at a different privilege level to the kernel.
Of course this article is quite out of date (though surprisingly much of it is still relevant) but on this point it was wrong even back when it was written.
sidebar & port table ?
where is the sidebar & port table that were discussed in this article..?
links and sidebars
This is not the magazine so there are no "sidebars" Check the links in the articles they have what you are looking for.
"I have always wished that my computer would be as easy to use as my telephone.
My wish has come true. I no longer know how to use my telephone."
-- Bjarne Stroustrup