Specifications

D
evice Driver Programming
10-14
The Read Routine 10
Specification
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/cred.h>
#include <sys/ddi.h>
int xxread (dev, uio_p, crp)
dev_t dev;
uio_t *uio_p;
cred_t *crp;
Return Values
0 if the device read is successful
A nonzero value if the read fails. The number is returned to the user in
errno; it should be an error number as defined in <sys/errno.h>.
The device driver’s read(D2) routine is called when the read(2) system call is made
to read data from the device. This entry point is optional. It is valid only for character
device drivers.
The dev argument specifies the device major and minor number.
The uio_p argument is a pointer to a uio structure that describes the location and layout
of the user’s I/0 buffers. This structure is defined in <sys/uio.h>; it is described in
Chapter 9, “The iovec and uio Structures” on page 9-7.
NOTE
A driver compiled as a large offset driver is passed (and expects to
get) the large offset version of the uio structure for the
read(D2) interface it supplies. The same applies for small off-
set drivers. For details on how this is done, see Chapter 9, the sec-
tion “Small vs. Large Offset Drivers” on page 9-21.
The crp argument is a pointer to the cred structure associated with the user process.
The read activity is used to initiate and in some cases complete a read activity when a user
process makes a read() system call. Data are passed directly to the process’s address
space if it is available. This happens, for example, when the device has transferred data
beforehand between the device and the system memory upon receiving an interrupt or on a
DMA operation. In this case, the kernel has the data ready to be read and stored in some
kernel or driver memory pool. The purpose of the read entry point is to transfer this data
between the driver’s kernel address space and the user-level process’s address space.