This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
135
Chapter 6
CHAPTER 6
Advanced Char Driver
Operations
In Chapter 3, we built a complete device driver that the user can write to and read
from. But a real device usually offers more functionality than synchronous read and
write. Now that we’re equipped with debugging tools should something go awry—
and a firm understanding of concurrency issues to help keep things from going
awry—we can safely go ahead and create a more advanced driver.
This chapter examines a few concepts that you need to understand to write fully fea-
tured char device drivers. We start with implementing the ioctl system call, which is
a common interface used for device control. Then we proceed to various ways of syn-
chronizing with user space; by the end of this chapter you have a good idea of how to
put processes to sleep (and wake them up), implement nonblocking I/O, and inform
user space when your devices are available for reading or writing. We finish with a
look at how to implement a few different device access policies within drivers.
The ideas discussed here are demonstrated by way of a couple of modified versions
of the scull driver. Once again, everything is implemented using in-memory virtual
devices, so you can try out the code yourself without needing to have any particular
hardware. By now, you may be wanting to get your hands dirty with real hardware,
but that will have to wait until Chapter 9.
ioctl
Most drivers need—in addition to the ability to read and write the device—the abil-
ity to perform various types of hardware control via the device driver. Most devices
can perform operations beyond simple data transfers; user space must often be able
to request, for example, that the device lock its door, eject its media, report error
information, change a baud rate, or self destruct. These operations are usually sup-
ported via the ioctl method, which implements the system call by the same name.
In user space, the ioctl system call has the following prototype:
int ioctl(int fd, unsigned long cmd, ...);
,ch06.8719 Page 135 Friday, January 21, 2005 10:44 AM
评论0