Closing Devices
Streaming I/O and Device Drivers 7-41
7.14 Closing Devices
A device is closed by calling SIO_delete, which in turn calls Dxx_idle and
Dxx_close. Dxx_close closes the device after Dxx_idle returns the device to
its initial state, which is the state of the device immediately after it was
opened. This is shown in Example 7-31.
Example 7-31. Closing a Device
/*
* ======== Dxx_idle ========
*/
Int Dxx_idle(DEV_Handle device, Bool flush)
{
Dxx_Handle objptr = (Dxx_Handle) device->object;
Uns post_count;
/*
* The only time we will wait for all pending data
* is when the device is in output mode, and flush
* was not requested.
*/
if ((device->mode == DEV_OUTPUT) && !flush)
{
/* first, make sure device is started */
if ( `device is not started` &&
`device has received data` ) {
`start the device`
}
/*
* wait for all output buffers to be consumed by the
* output HWI. We need to maintain a count of how many
* buffers are returned so we can set the semaphore later.
*/
post_count = 0;
while (!QUE_empty(device->todevice)) {
SEM_pend(objptr->sync, SYS_FOREVER);
post_count++;
}
if (`there is a buffer currently in use by the HWI` ) {
SEM_pend(objptr->sync, SYS_FOREVER);
post_count++;
}
`stop the device`