apple_smc.c revision 1.4 1 1.1 riastrad /* $NetBSD: apple_smc.c,v 1.4 2014/04/01 17:49:05 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.4 2014/04/01 17:49:05 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.4 riastrad
79 1.2 riastrad mutex_init(&smc->smc_io_lock, MUTEX_DEFAULT, IPL_NONE);
80 1.2 riastrad #if 0 /* XXX sysctl */
81 1.2 riastrad apple_smc_sysctl_setup(smc);
82 1.2 riastrad #endif
83 1.4 riastrad
84 1.3 riastrad /* Attach any children. */
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.4 riastrad
93 1.1 riastrad /* Fail if we can't detach all our children. */
94 1.1 riastrad error = config_detach_children(smc->smc_dev, flags);
95 1.1 riastrad if (error)
96 1.1 riastrad return error;
97 1.1 riastrad
98 1.1 riastrad #if 0 /* XXX sysctl */
99 1.1 riastrad sysctl_teardown(&smc->smc_log);
100 1.4 riastrad #endif
101 1.1 riastrad mutex_destroy(&smc->smc_io_lock);
102 1.1 riastrad
103 1.1 riastrad return 0;
104 1.1 riastrad }
105 1.2 riastrad
106 1.2 riastrad int
107 1.3 riastrad apple_smc_rescan(struct apple_smc_tag *smc, const char *ifattr,
108 1.2 riastrad const int *locators)
109 1.2 riastrad {
110 1.4 riastrad
111 1.3 riastrad /* Let autoconf(9) do the work of finding new children. */
112 1.3 riastrad (void)config_search_loc(&apple_smc_search, smc->smc_dev, APPLE_SMC_BUS,
113 1.3 riastrad locators, smc);
114 1.3 riastrad return 0;
115 1.2 riastrad }
116 1.3 riastrad
117 1.3 riastrad static int
118 1.3 riastrad apple_smc_search(device_t parent, cfdata_t cf, const int *locators, void *aux)
119 1.3 riastrad {
120 1.3 riastrad struct apple_smc_tag *const smc = aux;
121 1.3 riastrad static const struct apple_smc_attach_args zero_asa;
122 1.3 riastrad struct apple_smc_attach_args asa = zero_asa;
123 1.3 riastrad device_t dev;
124 1.3 riastrad deviter_t di;
125 1.2 riastrad bool attached = false;
126 1.2 riastrad
127 1.3 riastrad /*
128 1.2 riastrad * If this device has already attached, don't attach it again.
129 1.3 riastrad *
130 1.3 riastrad * XXX This is a pretty silly way to query the children, but
131 1.2 riastrad * struct device doesn't seem to list its children.
132 1.3 riastrad */
133 1.3 riastrad for (dev = deviter_first(&di, DEVITER_F_LEAVES_FIRST);
134 1.3 riastrad dev != NULL;
135 1.3 riastrad dev = deviter_next(&di)) {
136 1.2 riastrad if (device_parent(dev) != parent)
137 1.3 riastrad continue;
138 1.2 riastrad if (!device_is_a(dev, cf->cf_name))
139 1.3 riastrad continue;
140 1.3 riastrad attached = true;
141 1.3 riastrad break;
142 1.3 riastrad }
143 1.3 riastrad deviter_release(&di);
144 1.3 riastrad if (attached)
145 1.2 riastrad return 0;
146 1.3 riastrad
147 1.3 riastrad /* If this device doesn't match, don't attach it. */
148 1.3 riastrad if (!config_match(parent, cf, aux))
149 1.2 riastrad return 0;
150 1.3 riastrad
151 1.3 riastrad /* Looks hunky-dory. Attach. */
152 1.3 riastrad asa.asa_smc = smc;
153 1.3 riastrad (void)config_attach_loc(parent, cf, locators, &asa, NULL);
154 1.2 riastrad return 0;
155 1.2 riastrad }
156 1.2 riastrad
157 1.3 riastrad void
158 1.3 riastrad apple_smc_child_detached(struct apple_smc_tag *smc __unused,
159 1.2 riastrad device_t child __unused)
160 1.4 riastrad {
161 1.2 riastrad /* We keep no books about our children. */
162 1.2 riastrad }
163 1.1 riastrad
164 1.1 riastrad static uint8_t
166 1.1 riastrad apple_smc_bus_read_1(struct apple_smc_tag *smc, bus_size_t reg)
167 1.1 riastrad {
168 1.1 riastrad
169 1.1 riastrad return bus_space_read_1(smc->smc_bst, smc->smc_bsh, reg);
170 1.1 riastrad }
171 1.1 riastrad
172 1.1 riastrad static void
173 1.1 riastrad apple_smc_bus_write_1(struct apple_smc_tag *smc, bus_size_t reg, uint8_t v)
174 1.1 riastrad {
175 1.1 riastrad
176 1.1 riastrad bus_space_write_1(smc->smc_bst, smc->smc_bsh, reg, v);
177 1.1 riastrad }
178 1.1 riastrad
179 1.1 riastrad /*
180 1.1 riastrad * XXX These delays are pretty randomly chosen. Wait in 100 us
181 1.1 riastrad * increments, up to a total of 1 ms.
182 1.1 riastrad */
183 1.1 riastrad
184 1.1 riastrad static int
185 1.1 riastrad apple_smc_read_data(struct apple_smc_tag *smc, uint8_t *byte)
186 1.1 riastrad {
187 1.1 riastrad uint8_t status;
188 1.4 riastrad unsigned int i;
189 1.1 riastrad
190 1.4 riastrad KASSERT(mutex_owned(&smc->smc_io_lock));
191 1.4 riastrad
192 1.4 riastrad /*
193 1.4 riastrad * Wait until the status register says there's data to read and
194 1.1 riastrad * read it.
195 1.1 riastrad */
196 1.1 riastrad for (i = 0; i < 100; i++) {
197 1.1 riastrad status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
198 1.1 riastrad if (status & APPLE_SMC_STATUS_READ_READY) {
199 1.1 riastrad *byte = apple_smc_bus_read_1(smc, APPLE_SMC_DATA);
200 1.1 riastrad return 0;
201 1.1 riastrad }
202 1.1 riastrad DELAY(100);
203 1.1 riastrad }
204 1.1 riastrad
205 1.1 riastrad return ETIMEDOUT;
206 1.1 riastrad }
207 1.1 riastrad
208 1.1 riastrad static int
209 1.1 riastrad apple_smc_write(struct apple_smc_tag *smc, bus_size_t reg, uint8_t byte)
210 1.1 riastrad {
211 1.1 riastrad uint8_t status;
212 1.4 riastrad unsigned int i;
213 1.1 riastrad
214 1.4 riastrad KASSERT(mutex_owned(&smc->smc_io_lock));
215 1.4 riastrad
216 1.4 riastrad /*
217 1.4 riastrad * Write the byte and then wait until the status register says
218 1.1 riastrad * it has been accepted.
219 1.1 riastrad */
220 1.1 riastrad apple_smc_bus_write_1(smc, reg, byte);
221 1.1 riastrad for (i = 0; i < 100; i++) {
222 1.1 riastrad status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
223 1.1 riastrad if (status & APPLE_SMC_STATUS_WRITE_ACCEPTED)
224 1.4 riastrad return 0;
225 1.4 riastrad DELAY(100);
226 1.1 riastrad
227 1.1 riastrad /* Write again if it hasn't been acknowledged at all. */
228 1.1 riastrad if (!(status & APPLE_SMC_STATUS_WRITE_PENDING))
229 1.1 riastrad apple_smc_bus_write_1(smc, reg, byte);
230 1.1 riastrad }
231 1.1 riastrad
232 1.1 riastrad return ETIMEDOUT;
233 1.1 riastrad }
234 1.1 riastrad
235 1.1 riastrad static int
236 1.1 riastrad apple_smc_write_cmd(struct apple_smc_tag *smc, uint8_t cmd)
237 1.1 riastrad {
238 1.1 riastrad
239 1.1 riastrad return apple_smc_write(smc, APPLE_SMC_CSR, cmd);
240 1.1 riastrad }
241 1.1 riastrad
242 1.1 riastrad static int
243 1.1 riastrad apple_smc_write_data(struct apple_smc_tag *smc, uint8_t data)
244 1.1 riastrad {
245 1.1 riastrad
246 1.2 riastrad return apple_smc_write(smc, APPLE_SMC_DATA, data);
247 1.1 riastrad }
248 1.1 riastrad
249 1.1 riastrad static int
251 1.1 riastrad apple_smc_begin(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
252 1.1 riastrad uint8_t size)
253 1.1 riastrad {
254 1.4 riastrad unsigned int i;
255 1.1 riastrad int error;
256 1.4 riastrad
257 1.1 riastrad KASSERT(mutex_owned(&smc->smc_io_lock));
258 1.1 riastrad
259 1.1 riastrad /* Write the command first. */
260 1.1 riastrad error = apple_smc_write_cmd(smc, cmd);
261 1.4 riastrad if (error)
262 1.1 riastrad return error;
263 1.1 riastrad
264 1.1 riastrad /* Write the key next. */
265 1.1 riastrad for (i = 0; i < 4; i++) {
266 1.1 riastrad error = apple_smc_write_data(smc, key[i]);
267 1.1 riastrad if (error)
268 1.4 riastrad return error;
269 1.1 riastrad }
270 1.1 riastrad
271 1.1 riastrad /* Finally, report how many bytes of data we want to send/receive. */
272 1.1 riastrad error = apple_smc_write_data(smc, size);
273 1.1 riastrad if (error)
274 1.1 riastrad return error;
275 1.1 riastrad
276 1.1 riastrad return 0;
277 1.1 riastrad }
278 1.1 riastrad
279 1.1 riastrad static int
280 1.1 riastrad apple_smc_input(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
281 1.1 riastrad void *buffer, uint8_t size)
282 1.1 riastrad {
283 1.1 riastrad uint8_t *bytes = buffer;
284 1.4 riastrad uint8_t i;
285 1.4 riastrad int error;
286 1.4 riastrad
287 1.4 riastrad /* Grab the SMC I/O lock. */
288 1.1 riastrad mutex_enter(&smc->smc_io_lock);
289 1.1 riastrad
290 1.1 riastrad /* Initiate the command with this key. */
291 1.1 riastrad error = apple_smc_begin(smc, cmd, key, size);
292 1.4 riastrad if (error)
293 1.1 riastrad goto out;
294 1.1 riastrad
295 1.1 riastrad /* Read each byte of data in sequence. */
296 1.1 riastrad for (i = 0; i < size; i++) {
297 1.1 riastrad error = apple_smc_read_data(smc, &bytes[i]);
298 1.1 riastrad if (error)
299 1.1 riastrad goto out;
300 1.1 riastrad }
301 1.1 riastrad
302 1.4 riastrad /* Success! */
303 1.1 riastrad error = 0;
304 1.1 riastrad
305 1.1 riastrad out: mutex_exit(&smc->smc_io_lock);
306 1.1 riastrad return error;
307 1.1 riastrad }
308 1.1 riastrad
309 1.1 riastrad static int
310 1.1 riastrad apple_smc_output(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
311 1.1 riastrad const void *buffer, uint8_t size)
312 1.1 riastrad {
313 1.1 riastrad const uint8_t *bytes = buffer;
314 1.4 riastrad uint8_t i;
315 1.4 riastrad int error;
316 1.4 riastrad
317 1.4 riastrad /* Grab the SMC I/O lock. */
318 1.1 riastrad mutex_enter(&smc->smc_io_lock);
319 1.1 riastrad
320 1.1 riastrad /* Initiate the command with this key. */
321 1.1 riastrad error = apple_smc_begin(smc, cmd, key, size);
322 1.4 riastrad if (error)
323 1.1 riastrad goto out;
324 1.1 riastrad
325 1.1 riastrad /* Write each byte of data in sequence. */
326 1.1 riastrad for (i = 0; i < size; i++) {
327 1.1 riastrad error = apple_smc_write_data(smc, bytes[i]);
328 1.1 riastrad if (error)
329 1.4 riastrad goto out;
330 1.4 riastrad }
331 1.4 riastrad
332 1.4 riastrad /* Success! */
333 1.1 riastrad error = 0;
334 1.1 riastrad
335 1.2 riastrad out: mutex_exit(&smc->smc_io_lock);
336 1.2 riastrad return error;
337 1.2 riastrad }
338 1.2 riastrad
339 1.2 riastrad struct apple_smc_key {
341 1.2 riastrad char ask_name[4 + 1];
342 1.2 riastrad struct apple_smc_desc ask_desc;
343 1.1 riastrad #ifdef DIAGNOSTIC
344 1.1 riastrad struct apple_smc_tag *ask_smc;
345 1.1 riastrad #endif
346 1.1 riastrad };
347 1.1 riastrad
348 1.1 riastrad const char *
349 1.1 riastrad apple_smc_key_name(const struct apple_smc_key *key)
350 1.1 riastrad {
351 1.1 riastrad
352 1.1 riastrad return key->ask_name;
353 1.1 riastrad }
354 1.1 riastrad
355 1.1 riastrad const struct apple_smc_desc *
356 1.1 riastrad apple_smc_key_desc(const struct apple_smc_key *key)
357 1.1 riastrad {
358 1.1 riastrad
359 1.1 riastrad return &key->ask_desc;
360 1.1 riastrad }
361 1.1 riastrad
362 1.1 riastrad uint32_t
363 1.1 riastrad apple_smc_nkeys(struct apple_smc_tag *smc)
364 1.1 riastrad {
365 1.1 riastrad
366 1.1 riastrad return smc->smc_nkeys;
367 1.1 riastrad }
368 1.1 riastrad
369 1.1 riastrad int
370 1.1 riastrad apple_smc_nth_key(struct apple_smc_tag *smc, uint32_t index,
371 1.1 riastrad const char type[4 + 1], struct apple_smc_key **keyp)
372 1.1 riastrad {
373 1.4 riastrad union { uint32_t u32; char name[4]; } index_be;
374 1.1 riastrad struct apple_smc_key *key;
375 1.1 riastrad int error;
376 1.1 riastrad
377 1.4 riastrad /* Paranoia: type must be NULL or 4 non-null characters long. */
378 1.1 riastrad if ((type != NULL) && (strlen(type) != 4))
379 1.1 riastrad return EINVAL;
380 1.1 riastrad
381 1.1 riastrad /* Create a new key. XXX Consider caching these. */
382 1.1 riastrad key = kmem_alloc(sizeof(*key), KM_SLEEP);
383 1.4 riastrad #ifdef DIAGNOSTIC
384 1.1 riastrad key->ask_smc = smc;
385 1.1 riastrad #endif
386 1.1 riastrad
387 1.1 riastrad /* Ask the SMC what the name of the key by this number is. */
388 1.1 riastrad index_be.u32 = htobe32(index);
389 1.4 riastrad error = apple_smc_input(smc, APPLE_SMC_CMD_NTH_KEY, index_be.name,
390 1.4 riastrad key->ask_name, 4);
391 1.1 riastrad if (error)
392 1.1 riastrad goto fail;
393 1.4 riastrad
394 1.1 riastrad /* Null-terminate the name. */
395 1.1 riastrad key->ask_name[4] = '\0';
396 1.1 riastrad
397 1.1 riastrad /* Ask the SMC for a description of this key by name. */
398 1.1 riastrad CTASSERT(sizeof(key->ask_desc) == 6);
399 1.1 riastrad error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
400 1.4 riastrad &key->ask_desc, 6);
401 1.1 riastrad if (error)
402 1.1 riastrad goto fail;
403 1.1 riastrad
404 1.1 riastrad /* Fail with EINVAL if the types don't match. */
405 1.1 riastrad if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
406 1.1 riastrad error = EINVAL;
407 1.1 riastrad goto fail;
408 1.1 riastrad }
409 1.1 riastrad
410 1.4 riastrad /* Success! */
411 1.1 riastrad *keyp = key;
412 1.1 riastrad return 0;
413 1.1 riastrad
414 1.1 riastrad fail: kmem_free(key, sizeof(*key));
415 1.1 riastrad return error;
416 1.1 riastrad }
417 1.1 riastrad
418 1.1 riastrad int
419 1.1 riastrad apple_smc_named_key(struct apple_smc_tag *smc, const char name[4 + 1],
420 1.1 riastrad const char type[4 + 1], struct apple_smc_key **keyp)
421 1.4 riastrad {
422 1.1 riastrad struct apple_smc_key *key;
423 1.1 riastrad int error;
424 1.1 riastrad
425 1.1 riastrad /* Paranoia: name must be 4 non-null characters long. */
426 1.4 riastrad KASSERT(name != NULL);
427 1.1 riastrad if (strlen(name) != 4)
428 1.1 riastrad return EINVAL;
429 1.1 riastrad
430 1.4 riastrad /* Paranoia: type must be NULL or 4 non-null characters long. */
431 1.1 riastrad if ((type != NULL) && (strlen(type) != 4))
432 1.1 riastrad return EINVAL;
433 1.1 riastrad
434 1.1 riastrad /* Create a new key. XXX Consider caching these. */
435 1.4 riastrad key = kmem_alloc(sizeof(*key), KM_SLEEP);
436 1.4 riastrad #ifdef DIAGNOSTIC
437 1.1 riastrad key->ask_smc = smc;
438 1.1 riastrad #endif
439 1.1 riastrad
440 1.4 riastrad /* Use the specified name, and make sure it's null-terminated. */
441 1.1 riastrad (void)memcpy(key->ask_name, name, 4);
442 1.1 riastrad key->ask_name[4] = '\0';
443 1.1 riastrad
444 1.1 riastrad /* Ask the SMC for a description of this key by name. */
445 1.1 riastrad CTASSERT(sizeof(key->ask_desc) == 6);
446 1.1 riastrad error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
447 1.4 riastrad &key->ask_desc, 6);
448 1.1 riastrad if (error)
449 1.1 riastrad goto fail;
450 1.1 riastrad
451 1.1 riastrad /* Fail with EINVAL if the types don't match. */
452 1.1 riastrad if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
453 1.4 riastrad error = EINVAL;
454 1.1 riastrad goto fail;
455 1.1 riastrad }
456 1.1 riastrad
457 1.4 riastrad /* Success! */
458 1.1 riastrad *keyp = key;
459 1.1 riastrad return 0;
460 1.1 riastrad
461 1.1 riastrad fail: kmem_free(key, sizeof(*key));
462 1.1 riastrad return error;
463 1.1 riastrad }
464 1.1 riastrad
465 1.1 riastrad void
466 1.4 riastrad apple_smc_release_key(struct apple_smc_tag *smc, struct apple_smc_key *key)
467 1.1 riastrad {
468 1.1 riastrad
469 1.1 riastrad #ifdef DIAGNOSTIC
470 1.1 riastrad /* Make sure the caller didn't mix up SMC tags. */
471 1.1 riastrad if (key->ask_smc != smc)
472 1.4 riastrad aprint_error_dev(smc->smc_dev,
473 1.4 riastrad "releasing key with wrong tag: %p != %p",
474 1.1 riastrad smc, key->ask_smc);
475 1.1 riastrad #endif
476 1.1 riastrad
477 1.1 riastrad /* Nothing to do but free the key's memory. */
478 1.1 riastrad kmem_free(key, sizeof(*key));
479 1.1 riastrad }
480 1.1 riastrad
481 1.1 riastrad int
482 1.1 riastrad apple_smc_key_search(struct apple_smc_tag *smc, const char *name,
483 1.1 riastrad uint32_t *result)
484 1.1 riastrad {
485 1.4 riastrad struct apple_smc_key *key;
486 1.1 riastrad uint32_t start = 0, end = apple_smc_nkeys(smc), median;
487 1.1 riastrad int error;
488 1.1 riastrad
489 1.1 riastrad /* Do a binary search on the SMC's key space. */
490 1.1 riastrad while (start < end) {
491 1.1 riastrad median = (start + ((end - start) / 2));
492 1.1 riastrad error = apple_smc_nth_key(smc, median, NULL, &key);
493 1.1 riastrad if (error)
494 1.1 riastrad return error;
495 1.1 riastrad
496 1.1 riastrad if (memcmp(name, apple_smc_key_name(key), 4) < 0)
497 1.1 riastrad end = median;
498 1.1 riastrad else
499 1.4 riastrad start = (median + 1);
500 1.1 riastrad apple_smc_release_key(smc, key);
501 1.1 riastrad }
502 1.1 riastrad
503 1.2 riastrad /* Success! */
504 1.1 riastrad *result = start;
505 1.1 riastrad return 0;
506 1.1 riastrad }
507 1.1 riastrad
508 1.1 riastrad int
510 1.1 riastrad apple_smc_read_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
511 1.1 riastrad void *buffer, uint8_t size)
512 1.4 riastrad {
513 1.4 riastrad
514 1.1 riastrad /* Refuse if software and hardware disagree on the key's size. */
515 1.1 riastrad if (key->ask_desc.asd_size != size)
516 1.1 riastrad return EINVAL;
517 1.4 riastrad
518 1.1 riastrad /* Refuse if the hardware doesn't want us to read it. */
519 1.1 riastrad if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_READ))
520 1.1 riastrad return EACCES;
521 1.1 riastrad
522 1.1 riastrad /* Looks good. Try reading it from the hardware. */
523 1.1 riastrad return apple_smc_input(smc, APPLE_SMC_CMD_READ_KEY, key->ask_name,
524 1.1 riastrad buffer, size);
525 1.1 riastrad }
526 1.1 riastrad
527 1.1 riastrad int
528 1.1 riastrad apple_smc_read_key_1(struct apple_smc_tag *smc,
529 1.1 riastrad const struct apple_smc_key *key, uint8_t *p)
530 1.1 riastrad {
531 1.1 riastrad
532 1.1 riastrad return apple_smc_read_key(smc, key, p, 1);
533 1.1 riastrad }
534 1.1 riastrad
535 1.1 riastrad int
536 1.1 riastrad apple_smc_read_key_2(struct apple_smc_tag *smc,
537 1.4 riastrad const struct apple_smc_key *key, uint16_t *p)
538 1.1 riastrad {
539 1.1 riastrad uint16_t be;
540 1.1 riastrad int error;
541 1.1 riastrad
542 1.4 riastrad /* Read a big-endian quantity from the hardware. */
543 1.1 riastrad error = apple_smc_read_key(smc, key, &be, 2);
544 1.4 riastrad if (error)
545 1.4 riastrad return error;
546 1.1 riastrad
547 1.1 riastrad /* Convert it to host order. */
548 1.1 riastrad *p = be16toh(be);
549 1.1 riastrad
550 1.1 riastrad /* Success! */
551 1.1 riastrad return 0;
552 1.1 riastrad }
553 1.1 riastrad
554 1.1 riastrad int
555 1.1 riastrad apple_smc_read_key_4(struct apple_smc_tag *smc,
556 1.4 riastrad const struct apple_smc_key *key, uint32_t *p)
557 1.1 riastrad {
558 1.1 riastrad uint32_t be;
559 1.1 riastrad int error;
560 1.1 riastrad
561 1.4 riastrad /* Read a big-endian quantity from the hardware. */
562 1.1 riastrad error = apple_smc_read_key(smc, key, &be, 4);
563 1.4 riastrad if (error)
564 1.4 riastrad return error;
565 1.1 riastrad
566 1.1 riastrad /* Convert it to host order. */
567 1.1 riastrad *p = be32toh(be);
568 1.1 riastrad
569 1.1 riastrad /* Success! */
570 1.1 riastrad return 0;
571 1.1 riastrad }
572 1.1 riastrad
573 1.4 riastrad int
574 1.1 riastrad apple_smc_write_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
575 1.1 riastrad const void *buffer, uint8_t size)
576 1.4 riastrad {
577 1.4 riastrad
578 1.1 riastrad /* Refuse if software and hardware disagree on the key's size. */
579 1.1 riastrad if (key->ask_desc.asd_size != size)
580 1.1 riastrad return EINVAL;
581 1.4 riastrad
582 1.1 riastrad /* Refuse if the hardware doesn't want us to write it. */
583 1.1 riastrad if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_WRITE))
584 1.1 riastrad return EACCES;
585 1.1 riastrad
586 1.1 riastrad /* Looks good. Try writing it to the hardware. */
587 1.1 riastrad return apple_smc_output(smc, APPLE_SMC_CMD_WRITE_KEY, key->ask_name,
588 1.1 riastrad buffer, size);
589 1.1 riastrad }
590 1.1 riastrad
591 1.1 riastrad int
592 1.1 riastrad apple_smc_write_key_1(struct apple_smc_tag *smc,
593 1.1 riastrad const struct apple_smc_key *key, uint8_t v)
594 1.1 riastrad {
595 1.1 riastrad
596 1.1 riastrad return apple_smc_write_key(smc, key, &v, 1);
597 1.1 riastrad }
598 1.4 riastrad
599 1.1 riastrad int
600 1.1 riastrad apple_smc_write_key_2(struct apple_smc_tag *smc,
601 1.4 riastrad const struct apple_smc_key *key, uint16_t v)
602 1.1 riastrad {
603 1.1 riastrad /* Convert the quantity from host to big-endian byte order. */
604 1.1 riastrad const uint16_t v_be = htobe16(v);
605 1.1 riastrad
606 1.1 riastrad /* Write the big-endian quantity to the hardware. */
607 1.1 riastrad return apple_smc_write_key(smc, key, &v_be, 2);
608 1.1 riastrad }
609 1.4 riastrad
610 1.1 riastrad int
611 1.1 riastrad apple_smc_write_key_4(struct apple_smc_tag *smc,
612 1.4 riastrad const struct apple_smc_key *key, uint32_t v)
613 1.1 riastrad {
614 1.1 riastrad /* Convert the quantity from host to big-endian byte order. */
615 1.2 riastrad const uint16_t v_be = htobe32(v);
616 1.2 riastrad
617 1.2 riastrad /* Write the big-endian quantity to the hardware. */
618 1.2 riastrad return apple_smc_write_key(smc, key, &v_be, 4);
619 1.2 riastrad }
620 1.2 riastrad
621 1.2 riastrad MODULE(MODULE_CLASS_MISC, apple_smc, NULL)
623 1.2 riastrad
624 1.2 riastrad static int
625 1.3 riastrad apple_smc_modcmd(modcmd_t cmd, void *data __unused)
626 1.2 riastrad {
627 1.2 riastrad
628 1.3 riastrad /* Nothing to do for now to set up or tear down the module. */
629 1.2 riastrad switch (cmd) {
630 1.2 riastrad case MODULE_CMD_INIT:
631 1.2 riastrad return 0;
632 1.2 riastrad
633 1.2 riastrad case MODULE_CMD_FINI:
634 return 0;
635
636 default:
637 return ENOTTY;
638 }
639 }
640