Home | History | Annotate | Line # | Download | only in ic
apple_smc_fan.c revision 1.5
      1  1.5  pgoyette /*	$NetBSD: apple_smc_fan.c,v 1.5 2015/04/23 23:23:00 pgoyette Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Apple System Management Controller: Fans
      5  1.1  riastrad  */
      6  1.1  riastrad 
      7  1.1  riastrad /*-
      8  1.1  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      9  1.1  riastrad  * All rights reserved.
     10  1.1  riastrad  *
     11  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
     12  1.1  riastrad  * by Taylor R. Campbell.
     13  1.1  riastrad  *
     14  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     15  1.1  riastrad  * modification, are permitted provided that the following conditions
     16  1.1  riastrad  * are met:
     17  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     18  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     19  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     20  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     21  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     22  1.1  riastrad  *
     23  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  riastrad  */
     35  1.1  riastrad 
     36  1.1  riastrad #include <sys/cdefs.h>
     37  1.5  pgoyette __KERNEL_RCSID(0, "$NetBSD: apple_smc_fan.c,v 1.5 2015/04/23 23:23:00 pgoyette Exp $");
     38  1.1  riastrad 
     39  1.1  riastrad #include <sys/types.h>
     40  1.1  riastrad #include <sys/param.h>
     41  1.1  riastrad #include <sys/device.h>
     42  1.1  riastrad #include <sys/kmem.h>
     43  1.2  riastrad #include <sys/module.h>
     44  1.1  riastrad #if 0                           /* XXX sysctl */
     45  1.1  riastrad #include <sys/sysctl.h>
     46  1.1  riastrad #endif
     47  1.1  riastrad #include <sys/systm.h>
     48  1.1  riastrad 
     49  1.1  riastrad #include <dev/ic/apple_smc.h>
     50  1.1  riastrad 
     51  1.1  riastrad #include <dev/sysmon/sysmonvar.h>
     52  1.1  riastrad 
     53  1.1  riastrad #define	APPLE_SMC_NFANS_KEY		"FNum"
     54  1.1  riastrad 
     55  1.1  riastrad static const struct fan_sensor {
     56  1.1  riastrad 	const char *fs_name;
     57  1.1  riastrad 	const char *fs_key_suffix;
     58  1.1  riastrad } fan_sensors[] = {
     59  1.1  riastrad 	{ "actual",	"Ac" },
     60  1.1  riastrad 	{ "minimum",	"Mn" },
     61  1.1  riastrad 	{ "maximum",	"Mx" },
     62  1.1  riastrad 	{ "safe",	"Sf" },
     63  1.1  riastrad 	{ "target",	"Tg" },
     64  1.1  riastrad };
     65  1.1  riastrad 
     66  1.1  riastrad struct apple_smc_fan_softc {
     67  1.1  riastrad 	device_t		sc_dev;
     68  1.1  riastrad 	struct apple_smc_tag	*sc_smc;
     69  1.1  riastrad 	struct sysmon_envsys	*sc_sme;
     70  1.1  riastrad 	uint8_t			sc_nfans;
     71  1.1  riastrad 	struct {
     72  1.1  riastrad 		struct {
     73  1.1  riastrad 			struct apple_smc_key	*sensor_key;
     74  1.1  riastrad 			struct envsys_data	sensor_data;
     75  1.1  riastrad 		} sensors[__arraycount(fan_sensors)];
     76  1.1  riastrad 	}			*sc_fans;
     77  1.1  riastrad 
     78  1.1  riastrad #if 0				/* XXX sysctl */
     79  1.1  riastrad 	struct sysctllog	*sc_sysctl_log;
     80  1.1  riastrad 	const struct sysctlnode	*sc_sysctl_node;
     81  1.1  riastrad #endif
     82  1.1  riastrad };
     83  1.1  riastrad 
     84  1.1  riastrad struct fan_desc {
     85  1.1  riastrad 	uint8_t fd_type;
     86  1.1  riastrad 	uint8_t fd_zone;
     87  1.1  riastrad 	uint8_t fd_location;
     88  1.1  riastrad 	uint8_t fd_reserved0;
     89  1.1  riastrad 	char fd_name[12];
     90  1.1  riastrad } __packed;
     91  1.1  riastrad 
     92  1.1  riastrad static int	apple_smc_fan_match(device_t, cfdata_t, void *);
     93  1.1  riastrad static void	apple_smc_fan_attach(device_t, device_t, void *);
     94  1.1  riastrad static int	apple_smc_fan_detach(device_t, int);
     95  1.1  riastrad static int	apple_smc_fan_attach_sensors(struct apple_smc_fan_softc *);
     96  1.1  riastrad static void	apple_smc_fan_attach_sensor(struct apple_smc_fan_softc *,
     97  1.1  riastrad 		    uint8_t, const char *, uint8_t);
     98  1.1  riastrad static void	apple_smc_fan_refresh(struct sysmon_envsys *,
     99  1.1  riastrad 		    struct envsys_data *);
    100  1.1  riastrad static void	apple_smc_fan_release_keys(struct apple_smc_fan_softc *);
    101  1.1  riastrad #if 0				/* XXX sysctl */
    102  1.1  riastrad static int	apple_smc_fan_sysctl_setup(struct apple_smc_fan_softc *);
    103  1.1  riastrad static void	apple_smc_fan_sysctl_setup_1(struct apple_smc_tag *,
    104  1.1  riastrad 		    uint8_t);
    105  1.1  riastrad #endif
    106  1.1  riastrad 
    107  1.1  riastrad CFATTACH_DECL_NEW(apple_smc_fan, sizeof(struct apple_smc_fan_softc),
    108  1.1  riastrad     apple_smc_fan_match, apple_smc_fan_attach, apple_smc_fan_detach, NULL);
    109  1.1  riastrad 
    110  1.1  riastrad static int
    111  1.1  riastrad apple_smc_fan_match(device_t parent, cfdata_t match, void *aux)
    112  1.1  riastrad {
    113  1.1  riastrad 	const struct apple_smc_attach_args *asa = aux;
    114  1.1  riastrad 	struct apple_smc_key *nfans_key;
    115  1.1  riastrad 	uint8_t nfans;
    116  1.1  riastrad 	int rv = 0;
    117  1.1  riastrad 	int error;
    118  1.1  riastrad 
    119  1.4  riastrad 	/* Find how to find how many fans there are.  */
    120  1.1  riastrad 	error = apple_smc_named_key(asa->asa_smc, APPLE_SMC_NFANS_KEY,
    121  1.1  riastrad 	    APPLE_SMC_TYPE_UINT8, &nfans_key);
    122  1.1  riastrad 	if (error)
    123  1.1  riastrad 		goto out0;
    124  1.1  riastrad 
    125  1.4  riastrad 	/* Find how many fans there are.  */
    126  1.1  riastrad 	error = apple_smc_read_key_1(asa->asa_smc, nfans_key, &nfans);
    127  1.1  riastrad 	if (error)
    128  1.1  riastrad 		goto out1;
    129  1.1  riastrad 
    130  1.4  riastrad 	/* Attach only if there's at least one fan.  */
    131  1.1  riastrad 	if (nfans > 0)
    132  1.1  riastrad 		rv = 1;
    133  1.1  riastrad 
    134  1.1  riastrad out1:	apple_smc_release_key(asa->asa_smc, nfans_key);
    135  1.1  riastrad out0:	return rv;
    136  1.1  riastrad }
    137  1.1  riastrad 
    138  1.1  riastrad static void
    139  1.1  riastrad apple_smc_fan_attach(device_t parent, device_t self, void *aux)
    140  1.1  riastrad {
    141  1.1  riastrad 	struct apple_smc_fan_softc *sc = device_private(self);
    142  1.1  riastrad 	const struct apple_smc_attach_args *asa = aux;
    143  1.1  riastrad 	struct apple_smc_key *nfans_key;
    144  1.1  riastrad 	int error;
    145  1.1  riastrad 
    146  1.4  riastrad 	/* Identify ourselves.  */
    147  1.1  riastrad 	aprint_normal(": Apple SMC fan sensors\n");
    148  1.1  riastrad 
    149  1.4  riastrad 	/* Initialize the softc.  */
    150  1.1  riastrad 	sc->sc_dev = self;
    151  1.1  riastrad 	sc->sc_smc = asa->asa_smc;
    152  1.1  riastrad 
    153  1.4  riastrad 	/* Find how to find how many fans there are.  */
    154  1.1  riastrad 	error = apple_smc_named_key(sc->sc_smc, APPLE_SMC_NFANS_KEY,
    155  1.1  riastrad 	    APPLE_SMC_TYPE_UINT8, &nfans_key);
    156  1.1  riastrad 	if (error)
    157  1.1  riastrad 		goto out0;
    158  1.1  riastrad 
    159  1.4  riastrad 	/* Find how many fans there are.  */
    160  1.1  riastrad 	error = apple_smc_read_key_1(sc->sc_smc, nfans_key, &sc->sc_nfans);
    161  1.1  riastrad 	if (error)
    162  1.1  riastrad 		goto out1;
    163  1.1  riastrad 
    164  1.4  riastrad 	/*
    165  1.4  riastrad 	 * There should be at least one, but just in case the hardware
    166  1.4  riastrad 	 * changed its mind in the interim...
    167  1.4  riastrad 	 */
    168  1.4  riastrad 	if (sc->sc_nfans == 0) {
    169  1.1  riastrad 		aprint_error_dev(self, "no fans\n");
    170  1.1  riastrad 		goto out1;
    171  1.1  riastrad 	}
    172  1.1  riastrad 
    173  1.4  riastrad 	/*
    174  1.4  riastrad 	 * The number of fans must fit in a single decimal digit for
    175  1.4  riastrad 	 * the names of the fan keys; see the fan_sensor table above.
    176  1.4  riastrad 	 */
    177  1.1  riastrad 	if (sc->sc_nfans >= 10) {
    178  1.1  riastrad 		aprint_error_dev(self, "too many fans: %"PRIu8"\n",
    179  1.1  riastrad 		    sc->sc_nfans);
    180  1.1  riastrad 		sc->sc_nfans = 9;
    181  1.1  riastrad 	}
    182  1.1  riastrad 
    183  1.1  riastrad #if 0				/* XXX sysctl */
    184  1.4  riastrad 	/* Set up the sysctl tree for controlling the fans.  */
    185  1.1  riastrad 	error = apple_smc_fan_sysctl_setup(sc);
    186  1.1  riastrad 	if (error)
    187  1.1  riastrad 		goto fail0;
    188  1.1  riastrad #endif
    189  1.1  riastrad 
    190  1.4  riastrad 	/* Attach the sensors to sysmon_envsys.  */
    191  1.1  riastrad 	error = apple_smc_fan_attach_sensors(sc);
    192  1.1  riastrad 	if (error)
    193  1.1  riastrad 		goto fail1;
    194  1.1  riastrad 
    195  1.4  riastrad 	/* Success!  */
    196  1.1  riastrad 	goto out1;
    197  1.1  riastrad 
    198  1.1  riastrad #if 0
    199  1.1  riastrad fail2:
    200  1.1  riastrad 	apple_smc_fan_detach_sensors(sc);
    201  1.1  riastrad #endif
    202  1.1  riastrad 
    203  1.1  riastrad fail1:
    204  1.1  riastrad #if 0				/* XXX sysctl */
    205  1.1  riastrad 	sysctl_teardown(&sc->sc_sysctl_log);
    206  1.1  riastrad fail0:
    207  1.1  riastrad #endif
    208  1.1  riastrad 
    209  1.1  riastrad out1:	apple_smc_release_key(sc->sc_smc, nfans_key);
    210  1.1  riastrad out0:	return;
    211  1.1  riastrad }
    212  1.1  riastrad 
    213  1.1  riastrad static int
    214  1.1  riastrad apple_smc_fan_detach(device_t self, int flags)
    215  1.1  riastrad {
    216  1.1  riastrad 	struct apple_smc_fan_softc *sc = device_private(self);
    217  1.1  riastrad 
    218  1.4  riastrad 	/* If we registered with sysmon_envsys, unregister.  */
    219  1.1  riastrad 	if (sc->sc_sme != NULL) {
    220  1.1  riastrad 		sysmon_envsys_unregister(sc->sc_sme);
    221  1.1  riastrad 		sc->sc_sme = NULL;
    222  1.1  riastrad 
    223  1.1  riastrad 		KASSERT(sc->sc_fans != NULL);
    224  1.1  riastrad 		KASSERT(sc->sc_nfans > 0);
    225  1.1  riastrad 		KASSERT(sc->sc_nfans < 10);
    226  1.1  riastrad 
    227  1.4  riastrad 		/* Release the keys and free the memory for fan records. */
    228  1.1  riastrad 		apple_smc_fan_release_keys(sc);
    229  1.1  riastrad 		kmem_free(sc->sc_fans,
    230  1.1  riastrad 		    (sizeof(sc->sc_fans[0]) * sc->sc_nfans));
    231  1.1  riastrad 		sc->sc_fans = NULL;
    232  1.1  riastrad 		sc->sc_nfans = 0;
    233  1.1  riastrad 	}
    234  1.1  riastrad 
    235  1.1  riastrad #if 0				/* XXX sysctl */
    236  1.4  riastrad 	/* Tear down all the sysctl knobs we set up.  */
    237  1.1  riastrad 	sysctl_teardown(&sc->sc_sysctl_log);
    238  1.1  riastrad #endif
    239  1.1  riastrad 
    240  1.1  riastrad 	return 0;
    241  1.1  riastrad }
    242  1.1  riastrad 
    243  1.1  riastrad static int
    244  1.1  riastrad apple_smc_fan_attach_sensors(struct apple_smc_fan_softc *sc)
    245  1.1  riastrad {
    246  1.1  riastrad 	uint8_t fan, sensor;
    247  1.1  riastrad 	char fan_desc_key_name[4 + 1];
    248  1.1  riastrad 	struct apple_smc_key *fan_desc_key;
    249  1.1  riastrad 	struct fan_desc fan_desc;
    250  1.1  riastrad 	char name[sizeof(fan_desc.fd_name) + 1];
    251  1.1  riastrad 	int error;
    252  1.1  riastrad 
    253  1.4  riastrad 	/* Create a sysmon_envsys record, but don't register it yet.  */
    254  1.1  riastrad 	sc->sc_sme = sysmon_envsys_create();
    255  1.1  riastrad 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    256  1.1  riastrad 	sc->sc_sme->sme_cookie = sc;
    257  1.1  riastrad 	sc->sc_sme->sme_refresh = apple_smc_fan_refresh;
    258  1.1  riastrad 
    259  1.4  riastrad 	/* Create an array of fan sensor records.  */
    260  1.1  riastrad 	CTASSERT(10 <= (SIZE_MAX / sizeof(sc->sc_fans[0])));
    261  1.1  riastrad 	sc->sc_fans = kmem_zalloc((sizeof(sc->sc_fans[0]) * sc->sc_nfans),
    262  1.1  riastrad 	    KM_SLEEP);
    263  1.1  riastrad 
    264  1.4  riastrad 	/* Find all the fans.  */
    265  1.1  riastrad 	for (fan = 0; fan < sc->sc_nfans; fan++) {
    266  1.4  riastrad 
    267  1.4  riastrad 		/* Format the name of the key for the fan's description.  */
    268  1.4  riastrad 		(void)snprintf(fan_desc_key_name, sizeof(fan_desc_key_name),
    269  1.1  riastrad 		    "F%"PRIu8"ID", fan);
    270  1.1  riastrad 		KASSERT(4 == strlen(fan_desc_key_name));
    271  1.1  riastrad 
    272  1.4  riastrad 		/* Look up the key for this fan's description.  */
    273  1.1  riastrad 		error = apple_smc_named_key(sc->sc_smc, fan_desc_key_name,
    274  1.1  riastrad 		    APPLE_SMC_TYPE_FANDESC, &fan_desc_key);
    275  1.1  riastrad 		if (error) {
    276  1.1  riastrad 			aprint_error_dev(sc->sc_dev,
    277  1.1  riastrad 			    "error identifying fan %"PRIu8": %d\n",
    278  1.1  riastrad 			    fan, error);
    279  1.1  riastrad 			continue;
    280  1.1  riastrad 		}
    281  1.1  riastrad 
    282  1.4  riastrad 		/* Read the description of this fan.  */
    283  1.1  riastrad 		error = apple_smc_read_key(sc->sc_smc, fan_desc_key, &fan_desc,
    284  1.4  riastrad 		    sizeof(fan_desc));
    285  1.1  riastrad 		if (error) {
    286  1.1  riastrad 			aprint_error_dev(sc->sc_dev,
    287  1.1  riastrad 			    "error identifying fan %"PRIu8": %d\n",
    288  1.1  riastrad 			    fan, error);
    289  1.1  riastrad 			continue;
    290  1.1  riastrad 		}
    291  1.1  riastrad 
    292  1.1  riastrad 		/*
    293  1.1  riastrad 		 * XXX Do more with the fan description...
    294  1.1  riastrad 		 */
    295  1.1  riastrad 
    296  1.4  riastrad 		/* Make a null-terminated copy of this fan's description.  */
    297  1.1  riastrad 		(void)memcpy(name, fan_desc.fd_name, sizeof(fan_desc.fd_name));
    298  1.4  riastrad 		name[sizeof(fan_desc.fd_name)] = '\0';
    299  1.1  riastrad 
    300  1.4  riastrad 		/* Attach all the sensors for this fan.  */
    301  1.1  riastrad 		for (sensor = 0; sensor < __arraycount(fan_sensors); sensor++)
    302  1.1  riastrad 			apple_smc_fan_attach_sensor(sc, fan, name, sensor);
    303  1.1  riastrad 
    304  1.1  riastrad #if 0				/* XXX sysctl */
    305  1.4  riastrad 		/* Attach sysctl knobs to control this fan.  */
    306  1.1  riastrad 		apple_smc_fan_sysctl_setup_1(sc, fan);
    307  1.1  riastrad #endif
    308  1.1  riastrad 	}
    309  1.1  riastrad 
    310  1.4  riastrad 	/* Fan sensors are all attached.  Register with sysmon_envsys now.  */
    311  1.1  riastrad 	error = sysmon_envsys_register(sc->sc_sme);
    312  1.1  riastrad 	if (error)
    313  1.1  riastrad 		goto fail;
    314  1.1  riastrad 
    315  1.4  riastrad 	/* Success!  */
    316  1.1  riastrad 	error = 0;
    317  1.1  riastrad 	goto out;
    318  1.1  riastrad 
    319  1.1  riastrad fail:	sysmon_envsys_destroy(sc->sc_sme);
    320  1.1  riastrad 	sc->sc_sme = NULL;
    321  1.1  riastrad out:	return error;
    322  1.1  riastrad }
    323  1.1  riastrad 
    324  1.1  riastrad static void
    325  1.1  riastrad apple_smc_fan_attach_sensor(struct apple_smc_fan_softc *sc, uint8_t fan,
    326  1.1  riastrad     const char *name, uint8_t sensor)
    327  1.1  riastrad {
    328  1.1  riastrad 	char key_name[4 + 1];
    329  1.1  riastrad 	struct apple_smc_key **keyp;
    330  1.1  riastrad 	struct envsys_data *edata;
    331  1.1  riastrad 	int error;
    332  1.1  riastrad 
    333  1.1  riastrad 	KASSERT(fan < sc->sc_nfans);
    334  1.1  riastrad 	KASSERT(sensor < __arraycount(fan_sensors));
    335  1.1  riastrad 
    336  1.4  riastrad 	/* Format the name of the key for this fan sensor.   */
    337  1.4  riastrad 	(void)snprintf(key_name, sizeof(key_name), "F%d%s",
    338  1.1  riastrad 	    (int)sensor, fan_sensors[sensor].fs_key_suffix);
    339  1.1  riastrad 	KASSERT(strlen(key_name) == 4);
    340  1.4  riastrad 
    341  1.4  riastrad 	/* Look up the key for this fan sensor. */
    342  1.4  riastrad 	keyp = &sc->sc_fans[fan].sensors[sensor].sensor_key;
    343  1.1  riastrad 	error = apple_smc_named_key(sc->sc_smc, key_name, APPLE_SMC_TYPE_FPE2,
    344  1.1  riastrad 	    keyp);
    345  1.1  riastrad 	if (error)
    346  1.1  riastrad 		goto fail0;
    347  1.1  riastrad 
    348  1.4  riastrad 	/* Initialize the envsys_data record for this fan sensor.  */
    349  1.1  riastrad 	edata = &sc->sc_fans[fan].sensors[sensor].sensor_data;
    350  1.1  riastrad 	edata->units = ENVSYS_SFANRPM;
    351  1.1  riastrad 	edata->state = ENVSYS_SINVALID;
    352  1.1  riastrad 	edata->flags = ENVSYS_FHAS_ENTROPY;
    353  1.1  riastrad 	(void)snprintf(edata->desc, sizeof(edata->desc), "fan %s %s speed",
    354  1.1  riastrad 	    name, fan_sensors[sensor].fs_name);
    355  1.1  riastrad 
    356  1.4  riastrad 	/* Attach this fan sensor to sysmon_envsys.  */
    357  1.1  riastrad 	error = sysmon_envsys_sensor_attach(sc->sc_sme, edata);
    358  1.1  riastrad 	if (error)
    359  1.1  riastrad 		goto fail1;
    360  1.1  riastrad 
    361  1.4  riastrad 	/* Success!  */
    362  1.1  riastrad 	return;
    363  1.1  riastrad 
    364  1.1  riastrad fail1:	apple_smc_release_key(sc->sc_smc, *keyp);
    365  1.1  riastrad fail0:	*keyp = NULL;
    366  1.1  riastrad 	aprint_error_dev(sc->sc_dev,
    367  1.1  riastrad 	    "failed to attach fan %s %s speed sensor: %d\n",
    368  1.1  riastrad 	    name, fan_sensors[sensor].fs_name, error);
    369  1.1  riastrad }
    370  1.1  riastrad 
    371  1.1  riastrad static void
    372  1.1  riastrad apple_smc_fan_refresh(struct sysmon_envsys *sme, struct envsys_data *edata)
    373  1.1  riastrad {
    374  1.1  riastrad 	struct apple_smc_fan_softc *sc = sme->sme_cookie;
    375  1.1  riastrad 	uint8_t fan, sensor;
    376  1.1  riastrad 	struct apple_smc_key *key;
    377  1.1  riastrad 	uint16_t rpm;
    378  1.1  riastrad 	int error;
    379  1.1  riastrad 
    380  1.4  riastrad 	/* Sanity-check the sensor number out of paranoia.  */
    381  1.1  riastrad 	CTASSERT(10 <= (SIZE_MAX / __arraycount(fan_sensors)));
    382  1.1  riastrad 	KASSERT(sc->sc_nfans < 10);
    383  1.1  riastrad 	if (edata->sensor >= (sc->sc_nfans * __arraycount(fan_sensors))) {
    384  1.1  riastrad 		aprint_error_dev(sc->sc_dev, "unknown sensor %"PRIu32"\n",
    385  1.1  riastrad 		    edata->sensor);
    386  1.1  riastrad 		return;
    387  1.1  riastrad 	}
    388  1.1  riastrad 
    389  1.4  riastrad 	/* Pick apart the fan number and its sensor number.  */
    390  1.1  riastrad 	fan = (edata->sensor / __arraycount(fan_sensors));
    391  1.1  riastrad 	sensor = (edata->sensor % __arraycount(fan_sensors));
    392  1.1  riastrad 
    393  1.1  riastrad 	KASSERT(fan < sc->sc_nfans);
    394  1.1  riastrad 	KASSERT(sensor < __arraycount(fan_sensors));
    395  1.1  riastrad 	KASSERT(edata == &sc->sc_fans[fan].sensors[sensor].sensor_data);
    396  1.1  riastrad 
    397  1.1  riastrad 	/*
    398  1.1  riastrad 	 * If we're refreshing, this sensor got attached, so we ought
    399  1.4  riastrad 	 * to have a sensor key.  Grab it.
    400  1.1  riastrad 	 */
    401  1.1  riastrad 	key = sc->sc_fans[fan].sensors[sensor].sensor_key;
    402  1.1  riastrad 	KASSERT(key != NULL);
    403  1.1  riastrad 
    404  1.4  riastrad 	/* Read the fan sensor value, in rpm.  */
    405  1.1  riastrad 	error = apple_smc_read_key_2(sc->sc_smc, key, &rpm);
    406  1.1  riastrad 	if (error) {
    407  1.1  riastrad 		aprint_error_dev(sc->sc_dev,
    408  1.1  riastrad 		    "failed to read fan %d %s speed: %d\n",
    409  1.1  riastrad 		    fan, fan_sensors[sensor].fs_name, error);
    410  1.1  riastrad 		edata->state = ENVSYS_SINVALID;
    411  1.1  riastrad 		return;
    412  1.1  riastrad 	}
    413  1.1  riastrad 
    414  1.4  riastrad 	/* Success!  */
    415  1.1  riastrad 	edata->value_cur = rpm;
    416  1.1  riastrad 	edata->state = ENVSYS_SVALID;
    417  1.1  riastrad }
    418  1.1  riastrad 
    419  1.1  riastrad static void
    420  1.1  riastrad apple_smc_fan_release_keys(struct apple_smc_fan_softc *sc)
    421  1.1  riastrad {
    422  1.1  riastrad 	uint8_t fan, sensor;
    423  1.1  riastrad 
    424  1.1  riastrad 	for (fan = 0; fan < sc->sc_nfans; fan++) {
    425  1.1  riastrad 		for (sensor = 0;
    426  1.1  riastrad 		     sensor < __arraycount(fan_sensors);
    427  1.1  riastrad 		     sensor++) {
    428  1.1  riastrad 			struct apple_smc_key **const keyp =
    429  1.1  riastrad 			    &sc->sc_fans[fan].sensors[sensor].sensor_key;
    430  1.1  riastrad 			if (*keyp != NULL) {
    431  1.1  riastrad 				apple_smc_release_key(sc->sc_smc, *keyp);
    432  1.1  riastrad 				*keyp = NULL;
    433  1.1  riastrad 			}
    434  1.1  riastrad 		}
    435  1.1  riastrad 	}
    436  1.1  riastrad }
    437  1.1  riastrad 
    438  1.1  riastrad #if 0				/* XXX sysctl */
    439  1.1  riastrad static int
    440  1.1  riastrad apple_smc_fan_sysctl_setup(struct apple_smc_fan_softc *sc)
    441  1.1  riastrad {
    442  1.1  riastrad 	...
    443  1.1  riastrad }
    444  1.1  riastrad 
    445  1.1  riastrad static void
    446  1.1  riastrad apple_smc_fan_sysctl_setup_1(struct apple_smc_fan_softc *sc, uint8_t fan)
    447  1.1  riastrad {
    448  1.1  riastrad }
    449  1.1  riastrad #endif
    450  1.2  riastrad 
    451  1.5  pgoyette MODULE(MODULE_CLASS_DRIVER, apple_smc_fan, "apple_smc,sysmon_envsys");
    452  1.2  riastrad 
    453  1.2  riastrad #ifdef _MODULE
    454  1.2  riastrad #include "ioconf.c"
    455  1.2  riastrad #endif
    456  1.2  riastrad 
    457  1.2  riastrad static int
    458  1.2  riastrad apple_smc_fan_modcmd(modcmd_t cmd, void *arg __unused)
    459  1.2  riastrad {
    460  1.4  riastrad #ifdef _MODULE
    461  1.2  riastrad 	int error;
    462  1.4  riastrad #endif
    463  1.2  riastrad 
    464  1.2  riastrad 	switch (cmd) {
    465  1.2  riastrad 	case MODULE_CMD_INIT:
    466  1.2  riastrad #ifdef _MODULE
    467  1.2  riastrad 		error = config_init_component(cfdriver_ioconf_apple_smc_fan,
    468  1.2  riastrad 		    cfattach_ioconf_apple_smc_fan,
    469  1.2  riastrad 		    cfdata_ioconf_apple_smc_fan);
    470  1.3  riastrad 		if (error)
    471  1.2  riastrad 			return error;
    472  1.2  riastrad #endif
    473  1.2  riastrad 		return 0;
    474  1.2  riastrad 
    475  1.2  riastrad 	case MODULE_CMD_FINI:
    476  1.2  riastrad #ifdef _MODULE
    477  1.2  riastrad 		error = config_fini_component(cfdriver_ioconf_apple_smc_fan,
    478  1.2  riastrad 		    cfattach_ioconf_apple_smc_fan,
    479  1.2  riastrad 		    cfdata_ioconf_apple_smc_fan);
    480  1.2  riastrad 		if (error)
    481  1.2  riastrad 			return error;
    482  1.2  riastrad #endif
    483  1.2  riastrad 		return 0;
    484  1.2  riastrad 
    485  1.2  riastrad 	default:
    486  1.2  riastrad 		return ENOTTY;
    487  1.2  riastrad 	}
    488  1.2  riastrad }
    489