Writing Device Drivers

attach()

	int attach(dev_info_t *dip, ddi_attach_cmd_t cmd);

When power is restored to the system, each device with a reg property or with a pm-hardware-state property of value needs-suspend-resume has its attach(9E) entry point called with a command value of DDI_RESUME. If the system shutdown was aborted for some reason, each driver that was suspended is called to resume, even though the power has not been shut off. Consequently, the resume code in attach(9E) must make no assumptions about the state of the hardware; it may or may not have lost power.

The resume code must restore the hardware state from the saved image in memory (possibly including reloading firmware), reregister any necessary timeouts, and unblock any pending requests.

Example 8-5 shows an example of an attach(9E) routine with the DDI_RESUME command.


Example 8-5 attach(9E) Routine Showing the Use of DDI_RESUME

int
xxattach(devinfo_t *dip, ddi_attach_cmd_t cmd)
{
	   struct xxstate *xsp;
  	int	instance;

 	instance = ddi_get_instance(dip);
 	xsp = ddi_get_soft_state(statep, instance);

 	switch (cmd) {
 	case DDI_ATTACH:
	    	see chapter 5, Autoconfiguration, for discussion

 	case DDI_RESUME:
	  	   mutex_enter(&xsp->mu);
	     	if (!xsp->xx_pm_suspended) {
			    Restore device register contents from xsp->xx_device_state}
			    this section is optional, only needed if the driver maintains a running timeout
			    /* restart timeouts */
			    xsp->xx_timeout_id = timeout({...});
	    	}
	  	   xsp->xx_suspended = 0;	/* allow new operations */
	    	cv_broadcast(&xsp->cv);
	   	mutex_exit(&xsp->mu);
	   	return(DDI_SUCCESS);

 	case DDI_PM_RESUME:
	    	see Example 8-3	   default:
	    	return(DDI_FAILURE);
 	}
}


Note -

The detach(9E) and attach(9E) interfaces may also be used to resume a system that has been quiesced.