/*-
* Copyright (c) 2007 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2003 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Jason R. Thorpe for Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Power management framework for sysmon.
*
* We defer to a power management daemon running in userspace, since
* power management is largely a policy issue. This merely provides
* for power management event notification to that daemon.
*/
/*
* Singly linked list for dictionaries to be stored/sent.
*/
struct power_event_dictionary {
SIMPLEQ_ENTRY(power_event_dictionary) pev_dict_head;
prop_dictionary_t dict;
int flags;
};
struct power_event_description {
int type;
const char *desc;
};
/*
* Available events for power switches.
*/
static const struct power_event_description pswitch_event_desc[] = {
{ PSWITCH_EVENT_PRESSED, "pressed" },
{ PSWITCH_EVENT_RELEASED, "released" },
{ -1, NULL }
};
static power_event_t sysmon_power_event_queue[SYSMON_MAX_POWER_EVENTS];
static int sysmon_power_event_queue_head;
static int sysmon_power_event_queue_tail;
static int sysmon_power_event_queue_count;
/*
* sysmon_power_init:
*
* Initializes the mutexes and condition variables in the
* boot process via module initialization process.
*/
int
sysmon_power_init(void)
{
int error;
if (error == 0) {
rnd_detach_source(&sysmon_rndsource);
seldestroy(&sysmon_power_event_queue_selinfo);
cv_destroy(&sysmon_power_event_queue_cv);
mutex_destroy(&sysmon_power_event_queue_mtx);
}
return error;
}
/*
* sysmon_queue_power_event:
*
* Enqueue a power event for the power management daemon. Returns
* non-zero if we were able to enqueue a power event.
*/
static int
sysmon_queue_power_event(power_event_t *pev)
{
KASSERT(mutex_owned(&sysmon_power_event_queue_mtx));
if (sysmon_power_event_queue_count == SYSMON_MAX_POWER_EVENTS)
return 0;
/*
* sysmon_get_power_event:
*
* Get a power event from the queue. Returns non-zero if there
* is an event available.
*/
static int
sysmon_get_power_event(power_event_t *pev)
{
KASSERT(mutex_owned(&sysmon_power_event_queue_mtx));
if (sysmon_power_event_queue_count == 0)
return 0;
/*
* sysmon_power_daemon_task:
*
* Assign required power event members and sends a signal
* to the process to notify that an event was enqueued successfully.
*/
static int
sysmon_power_daemon_task(struct power_event_dictionary *ped,
void *pev_data, int event)
{
power_event_t pev;
int rv, error = 0;
if (!ped || !ped->dict || !pev_data)
return EINVAL;
memset(&pev, 0, sizeof(pev));
mutex_enter(&sysmon_power_event_queue_mtx);
switch (event) {
/*
* Power switch events.
*/
case PSWITCH_EVENT_PRESSED:
case PSWITCH_EVENT_RELEASED:
{
/*
* ENVSYS events.
*/
case PENVSYS_EVENT_NORMAL:
case PENVSYS_EVENT_CRITICAL:
case PENVSYS_EVENT_CRITUNDER:
case PENVSYS_EVENT_CRITOVER:
case PENVSYS_EVENT_WARNUNDER:
case PENVSYS_EVENT_WARNOVER:
case PENVSYS_EVENT_BATT_CRIT:
case PENVSYS_EVENT_BATT_WARN:
case PENVSYS_EVENT_BATT_HIGH:
case PENVSYS_EVENT_BATT_MAX:
case PENVSYS_EVENT_STATE_CHANGED:
case PENVSYS_EVENT_LOW_POWER:
{
struct penvsys_state *penvsys =
(struct penvsys_state *)pev_data;
case EVFILT_WRITE:
kn->kn_fop = &seltrue_filtops;
break;
default:
return EINVAL;
}
return 0;
}
/*
* sysmonioctl_power:
*
* Perform a power management control request.
*/
int
sysmonioctl_power(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
int error = 0;
switch (cmd) {
case POWER_IOC_GET_TYPE:
case POWER_IOC_GET_TYPE_WITH_LOSSAGE:
{
struct power_type *power_type = (void *) data;
/*
* Get the first dictionary enqueued and mark it
* as busy.
*/
mutex_enter(&sysmon_power_event_queue_mtx);
ped = SIMPLEQ_FIRST(&pev_dict_list);
if (!ped || !ped->dict) {
mutex_exit(&sysmon_power_event_queue_mtx);
error = ENOTSUP;
break;
}
/*
* Remove the dictionary now that we don't need it.
*/
mutex_enter(&sysmon_power_event_queue_mtx);
ped->flags &= ~SYSMON_POWER_DICTIONARY_BUSY;
ped->flags &= ~SYSMON_POWER_DICTIONARY_READY;
SIMPLEQ_REMOVE_HEAD(&pev_dict_list, pev_dict_head);
mutex_exit(&sysmon_power_event_queue_mtx);
sysmon_power_destroy_dictionary(ped);
break;
}
default:
error = ENOTTY;
}
return error;
}
/*
* sysmon_power_make_dictionary:
*
* Adds the properties for an event in a dictionary.
*/
int
sysmon_power_make_dictionary(prop_dictionary_t dict, void *power_data,
int event, int type)
{
int i;
/*
* sysmon_power_destroy_dictionary:
*
* Destroys a power_event_dictionary object and all its
* properties in the dictionary.
*/
static void
sysmon_power_destroy_dictionary(struct power_event_dictionary *ped)
{
prop_object_iterator_t iter;
prop_object_t obj;
/*
* sysmon_power_settype:
*
* Sets the back-end power management type. This information can
* be used by the power management daemon.
*/
void
sysmon_power_settype(const char *type)
{
/*
* Don't bother locking this; it's going to be set
* during autoconfiguration, and then only read from
* then on.
*/
(void)strlcpy(sysmon_power_type, type, sizeof(sysmon_power_type));
}
#define PENVSYS_SHOWSTATE(str) \
do { \
printf("%s: %s limit on '%s'\n", \
pes->pes_dvname, (str), pes->pes_sensname); \
} while (/* CONSTCOND */ 0)
/*
* sysmon_penvsys_event:
*
* Puts an event onto the sysmon power queue and sends the
* appropriate event if the daemon is running, otherwise a
* message is shown.
*/
void
sysmon_penvsys_event(struct penvsys_state *pes, int event)
{
struct power_event_dictionary *ped;
const char *mystr = NULL;
KASSERT(pes != NULL);
rnd_add_uint32(&sysmon_rndsource, pes->pes_type);
if (sysmon_power_daemon != NULL) {
/*
* Create a dictionary for the new event.
*/
ped = kmem_zalloc(sizeof(*ped), KM_NOSLEEP);
if (!ped)
return;
ped->dict = prop_dictionary_create();
if (sysmon_power_daemon_task(ped, pes, event) == 0)
return;
/* We failed */
prop_object_release(ped->dict);
kmem_free(ped, sizeof(*ped));
}
switch (pes->pes_type) {
case PENVSYS_TYPE_BATTERY:
switch (event) {
case PENVSYS_EVENT_LOW_POWER:
printf("sysmon: LOW POWER! SHUTTING DOWN.\n");
kern_reboot(RB_POWERDOWN, NULL);
break;
case PENVSYS_EVENT_STATE_CHANGED:
printf("%s: state changed on '%s' to '%s'\n",
pes->pes_dvname, pes->pes_sensname,
pes->pes_statedesc);
break;
case PENVSYS_EVENT_BATT_CRIT:
mystr = "critical capacity";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_BATT_WARN:
mystr = "warning capacity";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_BATT_HIGH:
mystr = "high capacity";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_BATT_MAX:
mystr = "maximum capacity";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_NORMAL:
printf("%s: normal capacity on '%s'\n",
pes->pes_dvname, pes->pes_sensname);
break;
}
break;
case PENVSYS_TYPE_FAN:
case PENVSYS_TYPE_INDICATOR:
case PENVSYS_TYPE_TEMP:
case PENVSYS_TYPE_POWER:
case PENVSYS_TYPE_RESISTANCE:
case PENVSYS_TYPE_VOLTAGE:
switch (event) {
case PENVSYS_EVENT_CRITICAL:
mystr = "critical";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_CRITOVER:
mystr = "critical over";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_CRITUNDER:
mystr = "critical under";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_WARNOVER:
mystr = "warning over";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_WARNUNDER:
mystr = "warning under";
PENVSYS_SHOWSTATE(mystr);
break;
case PENVSYS_EVENT_NORMAL:
printf("%s: normal state on '%s'\n",
pes->pes_dvname, pes->pes_sensname);
break;
default:
printf("%s: unknown event\n", __func__);
}
break;
case PENVSYS_TYPE_DRIVE:
switch (event) {
case PENVSYS_EVENT_STATE_CHANGED:
printf("%s: state changed on '%s' to '%s'\n",
pes->pes_dvname, pes->pes_sensname,
pes->pes_statedesc);
break;
case PENVSYS_EVENT_NORMAL:
printf("%s: normal state on '%s' (%s)\n",
pes->pes_dvname, pes->pes_sensname,
pes->pes_statedesc);
break;
}
break;
default:
printf("%s: unknown power type\n", __func__);
break;
}
}
/*
* sysmon_pswitch_register:
*
* Register a power switch device.
*/
int
sysmon_pswitch_register(struct sysmon_pswitch *smpsw)
{
(void)RUN_ONCE(&once_power, power_preinit);
return 0;
}
/*
* sysmon_pswitch_unregister:
*
* Unregister a power switch device.
*/
void
sysmon_pswitch_unregister(struct sysmon_pswitch *smpsw)
{
/* nada */
}
/*
* sysmon_pswitch_event:
*
* Register an event on a power switch device.
*/
void
sysmon_pswitch_event(struct sysmon_pswitch *smpsw, int event)
{
struct power_event_dictionary *ped = NULL;
KASSERT(smpsw != NULL);
/*
* For pnp specific events, we don't care if the power daemon
* is running or not
*/
if (smpsw->smpsw_type == PSWITCH_TYPE_LID) {
switch (event) {
case PSWITCH_EVENT_PRESSED:
pmf_event_inject(NULL, PMFE_CHASSIS_LID_CLOSE);
break;
case PSWITCH_EVENT_RELEASED:
pmf_event_inject(NULL, PMFE_CHASSIS_LID_OPEN);
break;
default:
break;
}
}
if (sysmon_power_daemon != NULL) {
/*
* Create a new dictionary for the event.
*/
ped = kmem_zalloc(sizeof(*ped), KM_NOSLEEP);
if (!ped)
return;
ped->dict = prop_dictionary_create();
if (sysmon_power_daemon_task(ped, smpsw, event) == 0)
return;
/* We failed */
prop_object_release(ped->dict);
kmem_free(ped, sizeof(*ped));
}
switch (smpsw->smpsw_type) {
case PSWITCH_TYPE_POWER:
if (event != PSWITCH_EVENT_PRESSED) {
/* just ignore it */
return;
}
/*
* Attempt a somewhat graceful shutdown of the system,
* as if the user has issued a reboot(2) call with
* RB_POWERDOWN.
*/
printf("%s: power button pressed, shutting down!\n",
smpsw->smpsw_name);
kern_reboot(RB_POWERDOWN, NULL);
break;
case PSWITCH_TYPE_RESET:
if (event != PSWITCH_EVENT_PRESSED) {
/* just ignore it */
return;
}
/*
* Attempt a somewhat graceful reboot of the system,
* as if the user had issued a reboot(2) call.
*/
printf("%s: reset button pressed, rebooting!\n",
smpsw->smpsw_name);
kern_reboot(0, NULL);
break;
case PSWITCH_TYPE_SLEEP:
if (event != PSWITCH_EVENT_PRESSED) {
/* just ignore it */
return;
}
/*
* Try to enter a "sleep" state.
*/
/* XXX */
printf("%s: sleep button pressed.\n", smpsw->smpsw_name);
break;
case PSWITCH_TYPE_HOTKEY:
/*
* Eat up the event, there's nothing we can do
*/
break;
case PSWITCH_TYPE_LID:
switch (event) {
case PSWITCH_EVENT_PRESSED:
/*
* Try to enter a "standby" state.
*/
/* XXX */
printf("%s: lid closed.\n", smpsw->smpsw_name);
break;
case PSWITCH_EVENT_RELEASED:
/*
* Come out of "standby" state.
*/
/* XXX */
printf("%s: lid opened.\n", smpsw->smpsw_name);
break;
case PSWITCH_TYPE_ACADAPTER:
switch (event) {
case PSWITCH_EVENT_PRESSED:
/*
* Come out of power-save state.
*/
aprint_normal("%s: AC adapter online.\n",
smpsw->smpsw_name);
break;
case PSWITCH_EVENT_RELEASED:
/*
* Try to enter a power-save state.
*/
aprint_normal("%s: AC adapter offline.\n",
smpsw->smpsw_name);
break;
}
break;
}
}
static int
sysmon_power_modcmd(modcmd_t cmd, void *arg)
{
int ret;
switch (cmd) {
case MODULE_CMD_INIT:
ret = sysmon_power_init();
break;
case MODULE_CMD_FINI:
ret = sysmon_power_fini();
break;
case MODULE_CMD_STAT:
default:
ret = ENOTTY;
}