Driver read and write routines implement I/O requests. You can make read and write routines execute synchronously or asynchronously. A synchronous read or write routine must complete an entire I/O request before returning to the Device Manager; an asynchronous read or write routine can begin an I/O transaction and then return to the Device Manager before the request is complete. In this case, the I/O request continues to be executed, typically when more data is available, by other routines such as interrupt handlers or completion routines. Handling Asynchronous I/O discusses how to complete an asynchronous read or write routine.
One rule you can always follow regarding synchronous and asynchronous operation is:
If your device driver can be called asynchronously and you call another device driver, you must call it asynchronously. And, if your device driver can be called asynchronously, always operate as if you are being called asynchronously.
In other words, you should not test to see whether an operation is synchronous or asynchronous, and do different things in each case.
Listing 8-7 shows a sample read routine.
Listing 8-7 Sample driver read routine
OSErr DoReadCommand (IOpb pb)
{
long numBytes;
short myErr;
numbytes = pb -> IORegCount;
{
/* do the read into pb -> iobuffer */
}
return(myErr);
}