FreeBSD DRIVER DEVELOPMENT

Cordula's Web cpghost at cordula.ws
Tue Dec 2 17:16:16 PST 2003


> > Dear Sir/Madam,
> > 
> > I have requirement to develop driver for the PCI based hardware interface
> > using FreeBSD Unix system. Would you please advise on the following:-
> > 
> > 1)    What tools are available to develop driver for x86 machine using C?

Most drivers in FreeBSD are written in C. They are compiled by gcc
(2.95.4 in -STABLE, 3.x in -CURRENT), just like any other part of
the system sources (see: /usr/src).

> > 2)    Suitable package including the operating system, tools and books etc?
> 
> Checking out the documentation on freebsd.org would probally be a good start.

Checking out some sample driver sources would be even better :)
For -STABLE (RELENG_4), [PCI-]drivers are located in the kernel source
tree, here:

  /usr/src/sys/pci     (PCI drivers)
  /usr/src/sys/dev     (mixed ISA and PCI drivers)

Reading the man page pci(4) [man 4 pci] won't hurt either.

As with every device driver, the recommended (or at least proved)
way is to take an existing, simple driver, and modify it to suit
your needs.

Kernel interfaces are documented in intro(9) and related section 9
man pages (ls /usr/share/man/man9)

A good introduction on writing device drivers for FreeBSD is in the
Documentation. Among others:

  * Developers' Handbook:
    http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/index.html

  * FreeBSD Architecture Handbook (esp. Section II: Device Drivers):
    http://www.freebsd.org/doc/en_US.ISO8859-1/books/arch-handbook/index.html

    (You may want to play with a KLD module, which exercises the PCI API:
     http://www.freebsd.org/doc/en_US.ISO8859-1/books/arch-handbook/pci.html)

  * PCI Special Interest Group (specs etc... for PCI bus)
    http://www.pcisig.com/   esp. this:
    http://www.pcisig.com/specifications/conventional

> > 3)    Can the driver developed for other unix operating system e.g. VxWorks
> > can be imported into the FreeBSD and compiled without major code change?
> 
> AFAIK this is something that is not really possible on any operating system.
> This is becuase changes to what header files and need to be made and ect. As to
> the extent of what changes are required, I guess that varies from what it is and
> whatever... but I never messed with this befor so I have no clue...

A device driver is always part of the kernel program. It runs in the
same address space as the kernel, and must interact with both the
kernel and the hardware in a very specific manner. A driver will
e.g. need to call some kernel functions to report data, or send
notifications. Or it may register a timer, etc... These kernel
functions (in FreeBSD, look at directory /usr/share/man/man9) differ
from OS to OS. FreeBSD's functions are completely different to, say,
Linux' functions or Win32s/Windows API calls. That's a reason why
writing device drivers is completely dependant upon a specific kernel.

Ah, and don't forget to use a development machine. Coding mistakes
in device drivers WILL almost always panic(9) the kernel, and under
unlucky circumstances trash the filesystem with it! [Same under Linux
or any other OS]

Good luck and happy hacking!

-- 
Cordula's Web. http://www.cordula.ws/



More information about the freebsd-questions mailing list