Device Driver Writing In Linux.pdf

(783 KB) Pobierz
Device Drivers
Device Drivers
The HyperNews Linux KHG Discussion Pages
Device Drivers
If you choose to write a device driver, you must take everything written here as a guide, and no more. I
cannot guarantee that this chapter will be free of errors, and I cannot guarantee that you will not damage your
computer, even if you follow these instructions exactly. It is highly unlikely that you will damage it, but I
cannot guarantee against it. There is only one ``infallible'' direction I can give you: Back up! Back up before
you test your new device driver, or you may regret it later.
What is this ``device driver'' stuff anyway? Here's a very short introduction to the concept.
It's not always necessary to write a ``real'' device driver. Sometimes you just need to know how to
write code that runs as a normal user process and still accesses hardware.
Assuming that you need to write a ``real'' device driver, there are some things that you need to know
regardless of what type of driver you are writing. In fact, you may need to learn what type of driver
you ought to write...
This section includes details specific to character device drivers, and assumes that you know
everything in the previous section.
TTY drivers
This section hasn't been written yet. TTY drivers are character devices that interface with the kernel's
generic TTY support, and they require more than just a standard character device interface. I'd
appreciate it if someone would write up how to attach a character device driver to the generic TTY
layer and submit it to me for inclusion in this guide.
This section includes details specific to block device drivers (suprise!)
This is a technical paper written by Rik Faith at the University of North Carolina.
Alan Cox gives an introduction to the network layer, including device drivers.
Many functions are useful to all sorts of drivers. Here is a summary of quite a few of them.
An edited version of a post of Linus Torvalds to the linux-kernel mailing list about how to correctly
deal with translating memory references when writing kernel source code such as device drivers.
An edited version of a post of Joerg Pommnitz to the linux-kernel mailing list about how the new
(Linux 2.1.8) exception mechanism works.
Other sources of information
Quite a few other references are also available on the topic of writing Linux device drivers by now. I put up
some (slightly outdated by now, but still worth reading, I think) notes for a talk I gave in May 1995 entitled
Writing Linux Device Drivers , which is specifically oriented at character devices implemented as kernel
runtime-loadable modules.
Linux Journal has had a long-running series of articles called Kernel Korner which, despite the wacky
http://www.linuxdoc.org/LDP/khg/HyperNews/get/devices/devices.html (1 of 3) [2002-03-13 2:58:49 PM]
189413319.002.png
Device Drivers
name, has had quite a bit of useful information on it. Some of the articles from that column may be available
on the web; most of them are available for purchase as back issues. One particularly useful series of articles,
which focussed in far more detail than my 30 minute talk on the subject of kernel runtime-loadable modules,
was in issues 23, 24, 25, 26, and 28. They were written by Alessandro Rubini and Georg v. Zezschwitz. Issue
29 is slated (as of this writing) to have an article on writing network device drivers, written by Alan Cox.
Issues 9, 10, and 11 have a series that I wrote on block device drivers.
Copyright (C) 1992, 1993, 1994, 1996 Michael K. Johnson, johnsonm@redhat.com.
Messages
22. DMA to user space by Marcel Boosten
20. memcpy error? by Edgar Vonk
15. What does mark_bh() do? by Erik Petersen
1. Untitled by Praveen Dwivedi
14. 3D Acceleration by jamesbat@innotts.co.uk
10. Hardware Interface I/O Access by Terry Moore
2. PCI Driver by Flavia Donno
http://www.linuxdoc.org/LDP/khg/HyperNews/get/devices/devices.html (2 of 3) [2002-03-13 2:58:49 PM]
189413319.003.png
Device Drivers
2. Transmit function by Joerg Schorr
1. Re: Transmit function by Paul Gortmaker
-> Skbuff by Joerg Schorr
http://www.linuxdoc.org/LDP/khg/HyperNews/get/devices/devices.html (3 of 3) [2002-03-13 2:58:49 PM]
189413319.004.png
What is a Device Driver?
The HyperNews Linux KHG Discussion Pages
What is a Device Driver?
Making hardware work is tedious. To write to a hard disk, for example, requires that you
write magic numbers in magic places, wait for the hard drive to say that it is ready to
receive data, and then feed it the data it wants, very carefully. To write to a floppy disk is
even harder, and requires that the program supervise the floppy disk drive almost
constantly while it is running.
Instead of putting code in each application you write to control each device, you share
the code between applications. To make sure that that code is not compromised, you
protect it from users and normal programs that use it. If you do it right, you will be able
to add and remove devices from your system without changing your applications at all.
Furthermore, you need to be able to load your program into memory and run it, which the
operating system also does. So an operating system is essentially a priviledged, general,
sharable library of low-level hardware and memory and process control functions and
routines.
All versions of Unix have an abstract way of reading and writing devices. By making the
devices act as much as possible like regular files, the same calls ( read() , write() ,
etc.) can be used for devices and files. Within the kernel, there are a set of functions,
registered with the filesystem, which are called to handle requests to do I/O on ``device
special files,'' which are those which represent devices. (See mknod(1,2) for an
explanation of how to make these files.)
All devices controlled by the same device driver are given the same major number , and
of those with the same major number, different devices are distinguished by different
minor numbers . (This is not strictly true, but it is close enough. If you understand where
it is not true, you don't need to read this section, and if you don't but want to learn, read
the code for the tty devices, which uses up 2 major numbers, and may use a third and
possibly fourth by the time you read this. Also, the ``misc'' major device supports many
minor devices that only need a few minor numbers; we'll get to that later.)
This chapter explains how to write any type of Linux device driver that you might need
to, including character, block, SCSI, and network drivers. It explains what functions you
need to write, how to initialize your drivers and obtain memory for them efficiently, and
what function are built in to Linux to make your job easier.
Creating device drivers for Linux is easier than you might think. It merely involves
writing a few functions and registering them with the Virtual Filesystem Switch (VFS),
so that when the proper device special files are accessed, the VFS can call your functions.
However, a word of warning is due here: Writing a device driver is writing a part of the
Linux kernel. This means that your driver runs with kernel permissions, and can do
anything it wants to: write to any memory, reformat your hard drive, damage your
monitor or video card, or even break your dishes, if your dishwasher is controlled by
your computer. Be careful.
http://www.linuxdoc.org/LDP/khg/HyperNews/get/devices/whatis.html (1 of 2) [2002-03-13 2:58:52 PM]
189413319.005.png
What is a Device Driver?
Also, your driver will run in kernel mode, and the Linux kernel, like most Unix kernels,
is non-pre-emptible. This means that if you driver takes a long time to work without
giving other programs a chance to work, your computer will appear to ``freeze'' when
your driver is running. Normal user-mode pre-emptive scheduling does not apply to your
driver.
Copyright (C) 1992, 1993, 1994, 1996 Michael K. Johnson, johnsonm@redhat.com.
Messages
1. Question ? by Rose Merone
http://www.linuxdoc.org/LDP/khg/HyperNews/get/devices/whatis.html (2 of 2) [2002-03-13 2:58:52 PM]
189413319.001.png
Zgłoś jeśli naruszono regulamin