Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Adding a Linux device driver

On Linux systems, device drivers are typically distributed in one of three forms:

  • A patch against a specific kernel version

  • A loadable module

  • An installation script that applies appropriate patches

The most common of all these is the patch against a specific kernel version. These patches can in most cases be applied with the following procedure:2

# cd /usr/src/linux ; patch -p1 < patch_file

Diffs made against a different minor version of the kernel may fail, but the driver should still work. Here, we cover how to manually add a network "snarf" driver to the kernel. It's a very complicated and tedious process, especially when compared to other operating systems we've seen.

By convention, Linux kernel source resides in /usr/src/linux. Within the drivers subdirectory, you'll need to find the subdirectory that corresponds to the type of device you have. A directory listing of drivers looks like this:

% ls -F /usr/src/linux/drivers
Makefile	cdrom/	i2o/	nubus/	sbus/	telephony/
acorn/	char/	isdn/	parport/	scsi/	usb/
ap1000/	dio/	macintosh/	pci/	sgi/	video/
atm/	fc4/	misc/	pcmcia/	sound/	zorro/
block/	i2c/	net/	pnp/	tc/

The most common directories to which drivers are added are block, char, net, usb, sound, and scsi. These directories contain drivers for block devices (such as IDE disk drives), character devices (such as serial ports), network devices, USB devices, sound cards, and SCSI cards, respectively. Some of the other directories contain drivers for the buses themselves (e.g., pci, nubus, and zorro); it's unlikely that you will need to add drivers to these directories. Some directories contain platform-specific drivers, such as macintosh, acorn, and ap1000. Some directories contain specialty devices such as atm, isdn, and telephony.

Since our example device is a network-related device, we will add the driver to the directory drivers/net. We'll need to modify the following files:

  • drivers/net/Makefile, so that our driver will be compiled

  • drivers/net/Config.in, so that our device will appear in the config options

  • drivers/net/Space.c, so that the device will be probed on startup

After putting the .c and .h files for the driver in drivers/net, we'll add the driver to drivers/net/Makefile. The lines we'd add (near the end of the file) follow.

ifeq ($(CONFIG_SNARF),y)
	L_OBJS += snarf.o
	ifeq ($(CONFIG_SNARF),m)
		M_OBJS += snarf.o

This configuration adds the snarf driver so that it can be either configured as a module or built into the kernel.

After adding the device to the Makefile, we have to make sure we can configure the device when we configure the kernel. All network devices need to be listed in the file drivers/net/Config.in. To add the device so that it can be built either as a module or as part of the kernel (consistent with what we claimed in the Makefile), we add the following line:

tristate 'Snarf device support' CONFIG_SNARF 

The tristate keyword means you can build the device as a module. If the device cannot be built as a module, use the keyword bool instead of tristate. The next token is the string to display on the configuration screen. It can be any arbitrary text, but it should identify the device that is being configured. The final token is the configuration macro. This token needs to be the same as that tested for with the ifeq clause in the Makefile.

The last file we need to edit to add our device to the system is drivers/net/Space.c. Space.c contains references to the probe routines for the device driver, and it also controls the device probe order. Here, we'll have to edit the file in two different places. First we'll add a reference to the probe function, then we'll add the device to the list of devices to probe for.

At the top of the Space.c file are a bunch of references to other probe functions. We'll add the following line to that list:

extern int snarf_probe(struct device *); 

Next, to add the device to the actual probe list, we need to determine which list to add it to. A separate probe list is kept for each type of bus (PCI, EISA, SBus, MCA, ISA, parallel port, etc.). The snarf device is a PCI device, so we'll add it to the list called pci_probes. The line that says

struct devprobe pci_probes[] __initdata = { 

is followed by an ordered list of devices. The devices higher up in the list are probed first. Probe order does not usually matter for PCI devices, but some devices are sensitive. Just to be sure the snarf device is detected, we'll add it to the top of the list:

struct devprobe pci_probes[] __initdata = {
	snarf_probe, 0},

The device has now been added to the Linux kernel. When we next configure the kernel, the device should appear as a configuration option under "network devices."

  • + Share This
  • 🔖 Save To Your Account