/*
* Apple System Management Controller: Temperature Sensors
*/
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Taylor R. Campbell.
*
* 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
/* Create a sysmon_envsys record, but don't register it yet. */
sc->sc_sme = sysmon_envsys_create();
sc->sc_sme->sme_name = device_xname(self);
sc->sc_sme->sme_cookie = sc;
sc->sc_sme->sme_refresh = apple_smc_temp_refresh;
/* Find and attach all the sensors. */
error = apple_smc_temp_find_sensors(sc);
if (error) {
aprint_error_dev(self, "failed to find sensors: %d\n", error);
goto fail;
}
/* Sensors are all attached. Register with sysmon_envsys now. */
error = sysmon_envsys_register(sc->sc_sme);
if (error) {
aprint_error_dev(self, "failed to register with sysmon_envsys:"
" %d\n", error);
goto fail;
}
/* Release the keys and free the memory for sensor records. */
apple_smc_temp_release_keys(sc);
kmem_free(sc->sc_sensors,
(sizeof(sc->sc_sensors[0]) * sc->sc_nsensors));
sc->sc_sensors = NULL;
sc->sc_nsensors = 0;
}
/* Sanity-check the sensor number out of paranoia. */
if (edata->sensor >= sc->sc_nsensors) {
aprint_error_dev(sc->sc_dev, "unknown sensor %"PRIu32"\n",
edata->sensor);
return;
}
/* Read the raw temperature sensor value. */
key = sc->sc_sensors[edata->sensor].sensor_key;
KASSERT(key != NULL);
error = apple_smc_read_key_2(sc->sc_smc, key, &utemp16);
if (error) {
aprint_error_dev(sc->sc_dev,
"failed to read temperature sensor %"PRIu32" (%s): %d\n",
edata->sensor, apple_smc_key_name(key), error);
edata->state = ENVSYS_SINVALID;
return;
}
/* Sign-extend, in case we ever get below freezing... */
temp = (int16_t)utemp16;
/*
* Success guarantees that sc->sc_nsensors will be nonzero and
* sc->sc_sensors will be allocated.
*/
KASSERT(sc->sc_sensors != NULL);
KASSERT(sc->sc_nsensors > 0);
/* If we didn't find any sensors, bail. */
if (fss.fss_sensor == 0) {
kmem_free(sc->sc_sensors, sc->sc_nsensors);
sc->sc_sensors = NULL;
sc->sc_nsensors = 0;
return EIO;
}
/* Shrink the array if we overshot. */
if (fss.fss_sensor < sc->sc_nsensors) {
void *const sensors = kmem_alloc((fss.fss_sensor *
sizeof(sc->sc_sensors[0])), KM_SLEEP);
/* Record the maximum number of sensors we may have. */
fss->fss_sc->sc_nsensors = nsensors;
/* If we found a maximum of zero sensors, bail. */
if (nsensors == 0) {
fss->fss_sc->sc_sensors = NULL;
return EIO;
}
/*
* If there may be any sensors, optimistically allocate as many
* records for them as we may possibly need.
*/
fss->fss_sc->sc_sensors = kmem_alloc((nsensors *
sizeof(fss->fss_sc->sc_sensors[0])), KM_SLEEP);
/* Initialize the envsys_data record for this temperature sensor. */
edata->units = ENVSYS_STEMP;
edata->state = ENVSYS_SINVALID;
edata->flags = ENVSYS_FHAS_ENTROPY;
/*
* Use the SMC key name as the temperature sensor's name.
*
* XXX We ought to use a more meaningful name based on a table
* of known temperature sensors.
*/
CTASSERT(sizeof(edata->desc) >= 4);
(void)strlcpy(edata->desc, apple_smc_key_name(key), 4);
/* Attach this temperature sensor to sysmon_envsys. */
error = sysmon_envsys_sensor_attach(fss->fss_sc->sc_sme, edata);
if (error) {
aprint_error_dev(fss->fss_sc->sc_dev,
"failed to attach temperature sensor %s: %d\n",
apple_smc_key_name(key), error);
return;
}
/* Find [start, end) bounds on the temperature sensor key indices. */
error = apple_smc_bound_temp_sensors(smc, &tstart, &ustart);
if (error)
return error;
KASSERT(tstart <= ustart);
/* Inform the caller of the number of candidates. */
if (init != NULL) {
error = (*init)(smc, arg, (ustart - tstart));
if (error)
return error;
}
/* Take a closer look at all the candidates. */
for (i = tstart; i < ustart; i++) {
error = apple_smc_nth_key(smc, i, NULL, &key);
if (error)
continue;
/* Skip it if it's not a temperature sensor. */
if (!apple_smc_temp_sensor_p(key)) {
apple_smc_release_key(smc, key);
continue;
}
/* Scan it if it is one. */
(*scanner)(smc, arg, key);
}