如果支持电源管理,并且按示例 12–6 和 示例 12–7 中的方式使用 detach(9E) 和 attach(9E),则可以从用户上下文(例如,read(2)、write(2) 和 ioctl(2))访问该设备。
以下示例演示了该方法。该示例假定要执行的操作需要以电源级别 level 运行的组件 component。
mutex_enter(&xsp->mu); /* * Block command while device is suspended by DDI_SUSPEND */ while (xsp->xx_suspended) cv_wait(&xsp->xx_suspend_cv, &xsp->mu); /* * Mark component busy so xx_power() will reject attempt to lower power */ xsp->xx_busy[component]++; if (pm_busy_component(dip, component) != DDI_SUCCESS) { xsp->xx_busy[component]--; /* * Log error and abort */ } if (xsp->xx_power_level[component] < level) { mutex_exit(&xsp->mu); if (pm_raise_power(dip, component, level) != DDI_SUCCESS) { /* * Log error and abort */ } mutex_enter(&xsp->mu); }
当设备操作(例如,设备的中断处理程序执行的操作)完成时,可以使用以下示例中的代码段。
/* * For each command completion, decrement the busy count and unstack * the pm_busy_component() call by calling pm_idle_component(). This * will allow device power to be lowered when all commands complete * (all pm_busy_component() counts are unstacked) */ xsp->xx_busy[component]--; if (pm_idle_component(dip, component) != DDI_SUCCESS) { xsp->xx_busy[component]++; /* * Log error and abort */ } /* * If no more outstanding commands, wake up anyone (like DDI_SUSPEND) * waiting for all commands to be completed */