Writing Device Drivers

detach(9E)

The detach(9E) entry point is the inverse of attach(9E); it must free all resources that were allocated in attach(9E). If successful, the detach should call scsi_unprobe(9F). Example 13-3 shows a target driver detach(9E) routine.


Example 13-3 SCSI Target Driver detach(9E) Routine

static int
xxdetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	struct xxstate *xsp;
	switch (cmd) {
	case DDI_DETACH:
	  	normal detach(9E) operations, such as getting a 
		pointer to the state structure
	  	...
	  	scsi_free_consistent_buf(xsp->rqsbuf);
	  	scsi_destroy_pkt(xsp->rqs);
	  	xsp->sdp->sd_private = (caddr_t)NULL;
	  	xsp->sdp->sd_sense = NULL;
	  	scsi_unprobe(xsp->sdp);
	   	remove minor nodes
	   	free resources, such as the state structure and properties
	   	remove power managed components
 	  	return (DDI_SUCCESS);
	case DDI_SUSPEND:
	  	For information, see Chapter 8, Power Management	case DDI_PM_SUSPEND:
		For information, see Chapter 8, Power Management	default:
		return (DDI_FAILURE);
	}
}