Specifications

Writing a User-Level Device Drive
r
17-25
Return Value
The driver’s open routine returns EUD_NOERROR if the device is successfully opened.
Otherwise, it returns a user-level device driver error code (see “Overview of the Device
Configuration Program” on page 17-14 for a listing of the error codes as defined in
<userdriv.h>).
Example specification and pseudo code for a user-level driver’s open routine are
presented as follows:
int
dev_open(dev_desc, pathname, flags, arg)
int *dev_desc;
char *pathname;
int flags, arg;
{
Allocate a device descriptor.
Attach the driver’s shared memory and device register region.
Synchronize access to the device driver (Should it be exclusive?)
Perform any necessary device initialization.
Return a device descriptor to the user.
Return success status.
}
The Asynchronous I/O Support Routines 17
To obtain good throughput to a device, it is recommended that you design the user-level
driver to allow a user process to initiate multiple asynchronous I/O operations. If you do,
the I/O completion routine can schedule the next I/O request as soon as the previous
request has been completed. The number of pending I/O requests that is allowed is
determined by the writer of the driver. When that number is exceeded, the driver should
return the EUD_IOREQ error. If the driver allows only one I/O request to be initiated at a
time, it should also return the EUD_IOREQ error when a user process initiates a second
request.
In most cases, you should avoid intermediate buffering of data in a user-level driver.
Buffering requires copying of data, and copying adds more overhead than is considered
acceptable. The type of device for which you are writing a driver determines whether or
not you must buffer data. Some devices provide data to be read only on user request (the
DR11W emulator, for example); others provide unsolicited data (a serial line controller,
for example). The first type does not require buffering of data; the second does.
For a DMA device that does no intermediate buffering, the driver’s read and write inter-
faces require the user to supply a description of the physical location to which the DMA
transfer is to be directed. The user provides this description by supplying the location of a
udbuf_t structure on a call to the driver’s aread or awrite routine (see “User I/O
Buffer Descriptor” on page 17-7 for a description of this structure).