Device Driver Concept Intro
- Kernel Mode ==> Physical Address Space. Access to HW registers is possible
- User Mode ==> Virtual Addresses. Direct access to physical address space is
not possible
- But, the file system is a shared name space useful for
- Accessing shared system resources such as devices and shared memory
- Performing inter-process communication (named pipes)
- Naming of semaphores
- A file is an interface that exports:
- open(): convert public name space to local
name, established "proper" access to the file
- proper does not necessarily mean
exclusive, just that read, write operations perform properly
- read() returns bytes from the file/device
(advances file position)
- write(): appends bytes to the file/device (at current
position)
- close(): relinquish access to the device (invalidates
the local file descriptor)
- lseek(): move the position in the file
- A device can be hidden behind a file. Any call to open, read, write, close
will cause the associated device driver to perform appropriate actions on
the device and return appropriate results to the caller. This is basically
how processes can share memory, access hardware, etc. Users do not have the
power to install device drivers, so protection is maintained.
Example: cat
cat does this
(not showing a lot of stuff, like error checking, end of file, etc).
main(argc, argv) {
while
(1) {
s = read(stdin, buf, 1);
s = write(stdout, buf, 1);
}
'cat < /dev/console >/dev/console' is equivalent to 'cat'
both will simply echo what you type. /dev/console is special file in the
file system that has a "device driver" associated with it. When you
call write() on this file, you are really calling a function in the device driver
package associated with /dev/console. In this case, the device driver, running in kernel space, can send
characters to the user's display and get them from the keyboard by accessing
the physical memory space.
Device Drivers
- A set of functions that run in Kernel Mode that present file-like interface to user programs. The
device needs to act like a file in some way so that users can say
open("/dev/motor",...)...
- To use a device driver you must:
- Create a special file (node) on the disc and tell the OS which device driver
is associated with it. (mknod, insmod)
- Write the device driver, which is really just a collection of
functions that are allowed to run in kernel mode so that direct access
to the HW resources is possible. The key point is that you must define
open(), read(), write(), close(), etc. so that a user can treat your device like
a file. Not all operations must be defined for all devices. Some devices
are "write" only and some are "read" only. In this
case, open should fail (return -1) in the case that the user opens the
device in an unsupported mode.
- Install the device driver. This can be done either when you compile
the operating system or you can install a device driver when the OS is
already running (in Linux). When it is properly installed, the OS
passes the open("/dev/motor") system call to the open_motor()
function in your device driver (as an example).
Example
With the proper device driver, you should be able to get
sonar data from a sonar device using
cat < /dev/sonar
The device driver has to deal either with the sonar
directly, or maybe it communicates with an 8051 that is driving the sonar.
All of this is now hidden from the user.
Homework Assignment
Suppose you are building a robot with the Cerfboard as the central controller.
The robot has two wheels and two motors to drive them (right and left).
You can independently control the direction and speed of each motor. Specify a
device driver interface that you think would be appropriate for the motors. All
I want you do to is answer these questions:
- What
should the user program be responsible for?
- What should the device driver be
responsible for?
- Define functionality of open(), read(), write(), and
close() based on the decomposition that you proposed above. I'm talking a
sentence or two on each!
This week's lab assignment has a template for a device
driver.