apple_smc.c revision 1.1       1  1.1  riastrad /*	$NetBSD: apple_smc.c,v 1.1 2014/04/01 17:47:36 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Apple System Management Controller
      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.1  riastrad __KERNEL_RCSID(0, "$NetBSD: apple_smc.c,v 1.1 2014/04/01 17:47:36 riastradh 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/errno.h>
     43  1.1  riastrad #include <sys/kmem.h>
     44  1.1  riastrad #include <sys/mutex.h>
     45  1.1  riastrad #if 0                           /* XXX sysctl */
     46  1.1  riastrad #include <sys/sysctl.h>
     47  1.1  riastrad #endif
     48  1.1  riastrad #include <sys/systm.h>
     49  1.1  riastrad 
     50  1.1  riastrad #include <dev/ic/apple_smc.h>
     51  1.1  riastrad #include <dev/ic/apple_smcreg.h>
     52  1.1  riastrad #include <dev/ic/apple_smcvar.h>
     53  1.1  riastrad 
     54  1.1  riastrad static void	apple_smc_rescan1(struct apple_smc_tag *,
     55  1.1  riastrad 		    struct apple_smc_attach_args *,
     56  1.1  riastrad 		    const char *, const char *, device_t *);
     57  1.1  riastrad static uint8_t	apple_smc_bus_read_1(struct apple_smc_tag *, bus_size_t);
     58  1.1  riastrad static void	apple_smc_bus_write_1(struct apple_smc_tag *, bus_size_t,
     59  1.1  riastrad 		    uint8_t);
     60  1.1  riastrad static int	apple_smc_read_data(struct apple_smc_tag *, uint8_t *);
     61  1.1  riastrad static int	apple_smc_write(struct apple_smc_tag *, bus_size_t, uint8_t);
     62  1.1  riastrad static int	apple_smc_write_cmd(struct apple_smc_tag *, uint8_t);
     63  1.1  riastrad static int	apple_smc_write_data(struct apple_smc_tag *, uint8_t);
     64  1.1  riastrad static int	apple_smc_begin(struct apple_smc_tag *, uint8_t,
     65  1.1  riastrad 		    const char *, uint8_t);
     66  1.1  riastrad static int	apple_smc_input(struct apple_smc_tag *, uint8_t,
     67  1.1  riastrad 		    const char *, void *, uint8_t);
     68  1.1  riastrad static int	apple_smc_output(struct apple_smc_tag *, uint8_t,
     69  1.1  riastrad 		    const char *, const void *, uint8_t);
     70  1.1  riastrad 
     71  1.1  riastrad void
     72  1.1  riastrad apple_smc_attach(struct apple_smc_tag *smc)
     73  1.1  riastrad {
     74  1.1  riastrad 
     75  1.1  riastrad 	mutex_init(&smc->smc_lock, MUTEX_DEFAULT, IPL_NONE);
     76  1.1  riastrad 
     77  1.1  riastrad #if 0				/* XXX sysctl */
     78  1.1  riastrad 	apple_smc_sysctl_setup(smc);
     79  1.1  riastrad #endif
     80  1.1  riastrad 
     81  1.1  riastrad         (void)apple_smc_rescan(smc, NULL, NULL);
     82  1.1  riastrad }
     83  1.1  riastrad 
     84  1.1  riastrad int
     85  1.1  riastrad apple_smc_rescan(struct apple_smc_tag *smc, const char *ifattr,
     86  1.1  riastrad     const int *locators)
     87  1.1  riastrad {
     88  1.1  riastrad 	struct apple_smc_attach_args asa;
     89  1.1  riastrad 
     90  1.1  riastrad 	(void)locators;		/* ignore */
     91  1.1  riastrad 
     92  1.1  riastrad 	(void)memset(&asa, 0, sizeof asa);
     93  1.1  riastrad 	asa.asa_smc = smc;
     94  1.1  riastrad #if 0				/* XXX sysctl */
     95  1.1  riastrad 	asa.asa_sysctlnode = smc->smc_sysctlnode;
     96  1.1  riastrad #endif
     97  1.1  riastrad 
     98  1.1  riastrad 	apple_smc_rescan1(smc, &asa, ifattr, "applesmcfan", &smc->smc_fan);
     99  1.1  riastrad 	apple_smc_rescan1(smc, &asa, ifattr, "applesmctemp", &smc->smc_temp);
    100  1.1  riastrad #if 0				/* XXX accelerometer */
    101  1.1  riastrad 	apple_smc_rescan1(smc, &asa, ifattr, "applesmcaccel",
    102  1.1  riastrad 	    &smc->smc_accelfan);
    103  1.1  riastrad #endif
    104  1.1  riastrad 
    105  1.1  riastrad         return 0;
    106  1.1  riastrad }
    107  1.1  riastrad 
    108  1.1  riastrad static void
    109  1.1  riastrad apple_smc_rescan1(struct apple_smc_tag *smc, struct apple_smc_attach_args *asa,
    110  1.1  riastrad     const char *ifattr, const char *ifa_conf, device_t *devp)
    111  1.1  riastrad {
    112  1.1  riastrad 
    113  1.1  riastrad 	if (ifattr_match(ifattr, ifa_conf) && *devp == NULL)
    114  1.1  riastrad 		*devp = config_found_ia(smc->smc_dev, ifa_conf, asa, NULL);
    115  1.1  riastrad }
    116  1.1  riastrad 
    117  1.1  riastrad int
    118  1.1  riastrad apple_smc_detach(struct apple_smc_tag *smc, int flags)
    119  1.1  riastrad {
    120  1.1  riastrad 	int error;
    121  1.1  riastrad 
    122  1.1  riastrad 	error = config_detach_children(smc->smc_dev, flags);
    123  1.1  riastrad 	if (error)
    124  1.1  riastrad 		return error;
    125  1.1  riastrad 
    126  1.1  riastrad #if 0				/* XXX sysctl */
    127  1.1  riastrad 	sysctl_teardown(&smc->smc_log);
    128  1.1  riastrad #endif
    129  1.1  riastrad 
    130  1.1  riastrad 	return 0;
    131  1.1  riastrad }
    132  1.1  riastrad 
    133  1.1  riastrad static uint8_t
    134  1.1  riastrad apple_smc_bus_read_1(struct apple_smc_tag *smc, bus_size_t reg)
    135  1.1  riastrad {
    136  1.1  riastrad 
    137  1.1  riastrad 	return bus_space_read_1(smc->smc_bst, smc->smc_bsh, reg);
    138  1.1  riastrad }
    139  1.1  riastrad 
    140  1.1  riastrad static void
    141  1.1  riastrad apple_smc_bus_write_1(struct apple_smc_tag *smc, bus_size_t reg, uint8_t v)
    142  1.1  riastrad {
    143  1.1  riastrad 
    144  1.1  riastrad 	bus_space_write_1(smc->smc_bst, smc->smc_bsh, reg, v);
    145  1.1  riastrad }
    146  1.1  riastrad 
    147  1.1  riastrad /*
    148  1.1  riastrad  * XXX These delays are pretty randomly chosen.  Wait in 100 us
    149  1.1  riastrad  * increments, up to a total of 1 ms.
    150  1.1  riastrad  */
    151  1.1  riastrad 
    152  1.1  riastrad static int
    153  1.1  riastrad apple_smc_read_data(struct apple_smc_tag *smc, uint8_t *byte)
    154  1.1  riastrad {
    155  1.1  riastrad 	uint8_t status;
    156  1.1  riastrad 	unsigned int i;
    157  1.1  riastrad 
    158  1.1  riastrad 	KASSERT(mutex_owned(&smc->smc_lock));
    159  1.1  riastrad 
    160  1.1  riastrad 	for (i = 0; i < 100; i++) {
    161  1.1  riastrad 		status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
    162  1.1  riastrad 		if (status & APPLE_SMC_STATUS_READ_READY) {
    163  1.1  riastrad 			*byte = apple_smc_bus_read_1(smc, APPLE_SMC_DATA);
    164  1.1  riastrad 			return 0;
    165  1.1  riastrad 		}
    166  1.1  riastrad 		DELAY(100);
    167  1.1  riastrad 	}
    168  1.1  riastrad 
    169  1.1  riastrad 	return ETIMEDOUT;
    170  1.1  riastrad }
    171  1.1  riastrad 
    172  1.1  riastrad static int
    173  1.1  riastrad apple_smc_write(struct apple_smc_tag *smc, bus_size_t reg, uint8_t byte)
    174  1.1  riastrad {
    175  1.1  riastrad 	uint8_t status;
    176  1.1  riastrad 	unsigned int i;
    177  1.1  riastrad 
    178  1.1  riastrad 	KASSERT(mutex_owned(&smc->smc_lock));
    179  1.1  riastrad 
    180  1.1  riastrad 	apple_smc_bus_write_1(smc, reg, byte);
    181  1.1  riastrad 	for (i = 0; i < 100; i++) {
    182  1.1  riastrad 		status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
    183  1.1  riastrad 		if (status & APPLE_SMC_STATUS_WRITE_ACCEPTED)
    184  1.1  riastrad 			return 0;
    185  1.1  riastrad 		DELAY(100);
    186  1.1  riastrad 		if (!(status & APPLE_SMC_STATUS_WRITE_PENDING))
    187  1.1  riastrad 			apple_smc_bus_write_1(smc, reg, byte);
    188  1.1  riastrad 	}
    189  1.1  riastrad 
    190  1.1  riastrad 	return ETIMEDOUT;
    191  1.1  riastrad }
    192  1.1  riastrad 
    193  1.1  riastrad static int
    194  1.1  riastrad apple_smc_write_cmd(struct apple_smc_tag *smc, uint8_t cmd)
    195  1.1  riastrad {
    196  1.1  riastrad 
    197  1.1  riastrad 	return apple_smc_write(smc, APPLE_SMC_CSR, cmd);
    198  1.1  riastrad }
    199  1.1  riastrad 
    200  1.1  riastrad static int
    201  1.1  riastrad apple_smc_write_data(struct apple_smc_tag *smc, uint8_t data)
    202  1.1  riastrad {
    203  1.1  riastrad 
    204  1.1  riastrad 	return apple_smc_write(smc, APPLE_SMC_DATA, data);
    205  1.1  riastrad }
    206  1.1  riastrad 
    207  1.1  riastrad struct apple_smc_key {
    208  1.1  riastrad 	char ask_name[4 + 1];
    209  1.1  riastrad 	struct apple_smc_desc ask_desc;
    210  1.1  riastrad #ifdef DIAGNOSTIC
    211  1.1  riastrad 	struct apple_smc_tag *ask_smc;
    212  1.1  riastrad #endif
    213  1.1  riastrad };
    214  1.1  riastrad 
    215  1.1  riastrad static int
    216  1.1  riastrad apple_smc_begin(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
    217  1.1  riastrad     uint8_t size)
    218  1.1  riastrad {
    219  1.1  riastrad 	unsigned int i;
    220  1.1  riastrad 	int error;
    221  1.1  riastrad 
    222  1.1  riastrad 	KASSERT(mutex_owned(&smc->smc_lock));
    223  1.1  riastrad 
    224  1.1  riastrad 	error = apple_smc_write_cmd(smc, cmd);
    225  1.1  riastrad 	if (error)
    226  1.1  riastrad 		return error;
    227  1.1  riastrad 
    228  1.1  riastrad 	for (i = 0; i < 4; i++) {
    229  1.1  riastrad 		error = apple_smc_write_data(smc, key[i]);
    230  1.1  riastrad 		if (error)
    231  1.1  riastrad 			return error;
    232  1.1  riastrad 	}
    233  1.1  riastrad 
    234  1.1  riastrad 	error = apple_smc_write_data(smc, size);
    235  1.1  riastrad 	if (error)
    236  1.1  riastrad 		return error;
    237  1.1  riastrad 
    238  1.1  riastrad 	return 0;
    239  1.1  riastrad }
    240  1.1  riastrad 
    241  1.1  riastrad static int
    242  1.1  riastrad apple_smc_input(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
    243  1.1  riastrad     void *buffer, uint8_t size)
    244  1.1  riastrad {
    245  1.1  riastrad 	uint8_t *bytes = buffer;
    246  1.1  riastrad 	uint8_t i;
    247  1.1  riastrad 	int error;
    248  1.1  riastrad 
    249  1.1  riastrad 	mutex_enter(&smc->smc_lock);
    250  1.1  riastrad 	error = apple_smc_begin(smc, cmd, key, size);
    251  1.1  riastrad 	if (error)
    252  1.1  riastrad 		goto out;
    253  1.1  riastrad 
    254  1.1  riastrad 	for (i = 0; i < size; i++) {
    255  1.1  riastrad 		error = apple_smc_read_data(smc, &bytes[i]);
    256  1.1  riastrad 		if (error)
    257  1.1  riastrad 			goto out;
    258  1.1  riastrad 	}
    259  1.1  riastrad 
    260  1.1  riastrad 	/* Success!  */
    261  1.1  riastrad 	error = 0;
    262  1.1  riastrad 
    263  1.1  riastrad out:
    264  1.1  riastrad 	mutex_exit(&smc->smc_lock);
    265  1.1  riastrad 	return error;
    266  1.1  riastrad }
    267  1.1  riastrad 
    268  1.1  riastrad static int
    269  1.1  riastrad apple_smc_output(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
    270  1.1  riastrad     const void *buffer, uint8_t size)
    271  1.1  riastrad {
    272  1.1  riastrad 	const uint8_t *bytes = buffer;
    273  1.1  riastrad 	uint8_t i;
    274  1.1  riastrad 	int error;
    275  1.1  riastrad 
    276  1.1  riastrad 	mutex_enter(&smc->smc_lock);
    277  1.1  riastrad 	error = apple_smc_begin(smc, cmd, key, size);
    278  1.1  riastrad 	if (error)
    279  1.1  riastrad 		goto out;
    280  1.1  riastrad 
    281  1.1  riastrad 	for (i = 0; i < size; i++) {
    282  1.1  riastrad 		error = apple_smc_write_data(smc, bytes[i]);
    283  1.1  riastrad 		if (error)
    284  1.1  riastrad 			goto out;
    285  1.1  riastrad 	}
    286  1.1  riastrad 
    287  1.1  riastrad out:
    288  1.1  riastrad 	mutex_exit(&smc->smc_lock);
    289  1.1  riastrad 	return error;
    290  1.1  riastrad }
    291  1.1  riastrad 
    292  1.1  riastrad const char *
    293  1.1  riastrad apple_smc_key_name(const struct apple_smc_key *key)
    294  1.1  riastrad {
    295  1.1  riastrad 
    296  1.1  riastrad 	return key->ask_name;
    297  1.1  riastrad }
    298  1.1  riastrad 
    299  1.1  riastrad const struct apple_smc_desc *
    300  1.1  riastrad apple_smc_key_desc(const struct apple_smc_key *key)
    301  1.1  riastrad {
    302  1.1  riastrad 
    303  1.1  riastrad 	return &key->ask_desc;
    304  1.1  riastrad }
    305  1.1  riastrad 
    306  1.1  riastrad uint32_t
    307  1.1  riastrad apple_smc_nkeys(struct apple_smc_tag *smc)
    308  1.1  riastrad {
    309  1.1  riastrad 
    310  1.1  riastrad 	return smc->smc_nkeys;
    311  1.1  riastrad }
    312  1.1  riastrad 
    313  1.1  riastrad int
    314  1.1  riastrad apple_smc_nth_key(struct apple_smc_tag *smc, uint32_t index,
    315  1.1  riastrad     const char type[4 + 1], struct apple_smc_key **keyp)
    316  1.1  riastrad {
    317  1.1  riastrad 	union { uint32_t u32; char name[4]; } index_be;
    318  1.1  riastrad 	struct apple_smc_key *key;
    319  1.1  riastrad 	int error;
    320  1.1  riastrad 
    321  1.1  riastrad 	if ((type != NULL) && (strlen(type) != 4))
    322  1.1  riastrad 		return EINVAL;
    323  1.1  riastrad 
    324  1.1  riastrad 	key = kmem_alloc(sizeof(*key), KM_SLEEP);
    325  1.1  riastrad #ifdef DIAGNOSTIC
    326  1.1  riastrad 	key->ask_smc = smc;
    327  1.1  riastrad #endif
    328  1.1  riastrad 
    329  1.1  riastrad 	index_be.u32 = htobe32(index);
    330  1.1  riastrad 	error = apple_smc_input(smc, APPLE_SMC_CMD_NTH_KEY, index_be.name,
    331  1.1  riastrad 	    key->ask_name, 4);
    332  1.1  riastrad 	if (error)
    333  1.1  riastrad 		goto fail;
    334  1.1  riastrad 	key->ask_name[4] = '\0';
    335  1.1  riastrad 
    336  1.1  riastrad 	CTASSERT(sizeof(key->ask_desc) == 6);
    337  1.1  riastrad 	error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
    338  1.1  riastrad 	    &key->ask_desc, 6);
    339  1.1  riastrad 	if (error)
    340  1.1  riastrad 		goto fail;
    341  1.1  riastrad 
    342  1.1  riastrad 	if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
    343  1.1  riastrad 		error = EINVAL;
    344  1.1  riastrad 		goto fail;
    345  1.1  riastrad 	}
    346  1.1  riastrad 
    347  1.1  riastrad 	/* Success!  */
    348  1.1  riastrad 	*keyp = key;
    349  1.1  riastrad 	return 0;
    350  1.1  riastrad 
    351  1.1  riastrad fail:
    352  1.1  riastrad 	kmem_free(key, sizeof(*key));
    353  1.1  riastrad 	return error;
    354  1.1  riastrad }
    355  1.1  riastrad 
    356  1.1  riastrad int
    357  1.1  riastrad apple_smc_named_key(struct apple_smc_tag *smc, const char name[4 + 1],
    358  1.1  riastrad     const char type[4 + 1], struct apple_smc_key **keyp)
    359  1.1  riastrad {
    360  1.1  riastrad 	struct apple_smc_key *key;
    361  1.1  riastrad 	int error;
    362  1.1  riastrad 
    363  1.1  riastrad 	KASSERT(name != NULL);
    364  1.1  riastrad 	if (strlen(name) != 4)
    365  1.1  riastrad 		return EINVAL;
    366  1.1  riastrad 
    367  1.1  riastrad 	if ((type != NULL) && (strlen(type) != 4))
    368  1.1  riastrad 		return EINVAL;
    369  1.1  riastrad 
    370  1.1  riastrad 	key = kmem_alloc(sizeof(*key), KM_SLEEP);
    371  1.1  riastrad #ifdef DIAGNOSTIC
    372  1.1  riastrad 	key->ask_smc = smc;
    373  1.1  riastrad #endif
    374  1.1  riastrad 	(void)memcpy(key->ask_name, name, 4);
    375  1.1  riastrad 	key->ask_name[4] = '\0';
    376  1.1  riastrad 
    377  1.1  riastrad 	CTASSERT(sizeof(key->ask_desc) == 6);
    378  1.1  riastrad 	error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
    379  1.1  riastrad 	    &key->ask_desc, 6);
    380  1.1  riastrad 	if (error)
    381  1.1  riastrad 		goto fail;
    382  1.1  riastrad 
    383  1.1  riastrad 	if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
    384  1.1  riastrad 		error = EINVAL;
    385  1.1  riastrad 		goto fail;
    386  1.1  riastrad 	}
    387  1.1  riastrad 
    388  1.1  riastrad 	*keyp = key;
    389  1.1  riastrad 	return 0;
    390  1.1  riastrad 
    391  1.1  riastrad fail:
    392  1.1  riastrad 	kmem_free(key, sizeof(*key));
    393  1.1  riastrad 	return error;
    394  1.1  riastrad }
    395  1.1  riastrad 
    396  1.1  riastrad void
    397  1.1  riastrad apple_smc_release_key(struct apple_smc_tag *smc, struct apple_smc_key *key)
    398  1.1  riastrad {
    399  1.1  riastrad 
    400  1.1  riastrad #ifdef DIAGNOSTIC
    401  1.1  riastrad 	if (key->ask_smc != smc)
    402  1.1  riastrad 		aprint_error_dev(smc->smc_dev,
    403  1.1  riastrad 		    "releasing key with wrong tag: %p != %p",
    404  1.1  riastrad 		    smc, key->ask_smc);
    405  1.1  riastrad #endif
    406  1.1  riastrad 	kmem_free(key, sizeof(*key));
    407  1.1  riastrad }
    408  1.1  riastrad 
    409  1.1  riastrad int
    410  1.1  riastrad apple_smc_key_search(struct apple_smc_tag *smc, const char *name,
    411  1.1  riastrad     uint32_t *result)
    412  1.1  riastrad {
    413  1.1  riastrad 	struct apple_smc_key *key;
    414  1.1  riastrad 	uint32_t start = 0, end = apple_smc_nkeys(smc), median;
    415  1.1  riastrad 	int error;
    416  1.1  riastrad 
    417  1.1  riastrad 	while (start < end) {
    418  1.1  riastrad 		median = (start + ((end - start) / 2));
    419  1.1  riastrad 		error = apple_smc_nth_key(smc, median, NULL, &key);
    420  1.1  riastrad 		if (error)
    421  1.1  riastrad 			return error;
    422  1.1  riastrad 
    423  1.1  riastrad 		if (memcmp(name, apple_smc_key_name(key), 4) < 0)
    424  1.1  riastrad 			end = median;
    425  1.1  riastrad 		else
    426  1.1  riastrad 			start = (median + 1);
    427  1.1  riastrad 		apple_smc_release_key(smc, key);
    428  1.1  riastrad 	}
    429  1.1  riastrad 
    430  1.1  riastrad 	*result = start;
    431  1.1  riastrad 	return 0;
    432  1.1  riastrad }
    433  1.1  riastrad 
    434  1.1  riastrad int
    435  1.1  riastrad apple_smc_read_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
    436  1.1  riastrad     void *buffer, uint8_t size)
    437  1.1  riastrad {
    438  1.1  riastrad 
    439  1.1  riastrad 	if (key->ask_desc.asd_size != size)
    440  1.1  riastrad 		return EINVAL;
    441  1.1  riastrad 	if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_READ))
    442  1.1  riastrad 		return EACCES;
    443  1.1  riastrad 
    444  1.1  riastrad 	return apple_smc_input(smc, APPLE_SMC_CMD_READ_KEY, key->ask_name,
    445  1.1  riastrad 	    buffer, size);
    446  1.1  riastrad }
    447  1.1  riastrad 
    448  1.1  riastrad int
    449  1.1  riastrad apple_smc_read_key_1(struct apple_smc_tag *smc,
    450  1.1  riastrad     const struct apple_smc_key *key, uint8_t *p)
    451  1.1  riastrad {
    452  1.1  riastrad 
    453  1.1  riastrad 	return apple_smc_read_key(smc, key, p, 1);
    454  1.1  riastrad }
    455  1.1  riastrad 
    456  1.1  riastrad int
    457  1.1  riastrad apple_smc_read_key_2(struct apple_smc_tag *smc,
    458  1.1  riastrad     const struct apple_smc_key *key, uint16_t *p)
    459  1.1  riastrad {
    460  1.1  riastrad 	uint16_t be;
    461  1.1  riastrad 	int error;
    462  1.1  riastrad 
    463  1.1  riastrad 	error = apple_smc_read_key(smc, key, &be, 2);
    464  1.1  riastrad 	if (error)
    465  1.1  riastrad 		return error;
    466  1.1  riastrad 
    467  1.1  riastrad 	*p = be16toh(be);
    468  1.1  riastrad 	return 0;
    469  1.1  riastrad }
    470  1.1  riastrad 
    471  1.1  riastrad int
    472  1.1  riastrad apple_smc_read_key_4(struct apple_smc_tag *smc,
    473  1.1  riastrad     const struct apple_smc_key *key, uint32_t *p)
    474  1.1  riastrad {
    475  1.1  riastrad 	uint32_t be;
    476  1.1  riastrad 	int error;
    477  1.1  riastrad 
    478  1.1  riastrad 	error = apple_smc_read_key(smc, key, &be, 4);
    479  1.1  riastrad 	if (error)
    480  1.1  riastrad 		return error;
    481  1.1  riastrad 
    482  1.1  riastrad 	*p = be32toh(be);
    483  1.1  riastrad 	return 0;
    484  1.1  riastrad }
    485  1.1  riastrad 
    486  1.1  riastrad int
    487  1.1  riastrad apple_smc_write_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
    488  1.1  riastrad     const void *buffer, uint8_t size)
    489  1.1  riastrad {
    490  1.1  riastrad 
    491  1.1  riastrad 	if (key->ask_desc.asd_size != size)
    492  1.1  riastrad 		return EINVAL;
    493  1.1  riastrad 	if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_WRITE))
    494  1.1  riastrad 		return EACCES;
    495  1.1  riastrad 
    496  1.1  riastrad 	return apple_smc_output(smc, APPLE_SMC_CMD_WRITE_KEY, key->ask_name,
    497  1.1  riastrad 	    buffer, size);
    498  1.1  riastrad }
    499  1.1  riastrad 
    500  1.1  riastrad int
    501  1.1  riastrad apple_smc_write_key_1(struct apple_smc_tag *smc,
    502  1.1  riastrad     const struct apple_smc_key *key, uint8_t v)
    503  1.1  riastrad {
    504  1.1  riastrad 
    505  1.1  riastrad 	return apple_smc_write_key(smc, key, &v, 1);
    506  1.1  riastrad }
    507  1.1  riastrad 
    508  1.1  riastrad int
    509  1.1  riastrad apple_smc_write_key_2(struct apple_smc_tag *smc,
    510  1.1  riastrad     const struct apple_smc_key *key, uint16_t v)
    511  1.1  riastrad {
    512  1.1  riastrad 	const uint16_t v_be = htobe16(v);
    513  1.1  riastrad 
    514  1.1  riastrad 	return apple_smc_write_key(smc, key, &v_be, 2);
    515  1.1  riastrad }
    516  1.1  riastrad 
    517  1.1  riastrad int
    518  1.1  riastrad apple_smc_write_key_4(struct apple_smc_tag *smc,
    519  1.1  riastrad     const struct apple_smc_key *key, uint32_t v)
    520  1.1  riastrad {
    521  1.1  riastrad 	const uint16_t v_be = htobe32(v);
    522  1.1  riastrad 
    523  1.1  riastrad 	return apple_smc_write_key(smc, key, &v_be, 4);
    524  1.1  riastrad }
    525