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