apple_smc.c revision 1.6.4.2 1 1.6.4.2 yamt /* $NetBSD: apple_smc.c,v 1.6.4.2 2014/05/22 11:40:21 yamt Exp $ */
2 1.6.4.2 yamt
3 1.6.4.2 yamt /*
4 1.6.4.2 yamt * Apple System Management Controller
5 1.6.4.2 yamt */
6 1.6.4.2 yamt
7 1.6.4.2 yamt /*-
8 1.6.4.2 yamt * Copyright (c) 2013 The NetBSD Foundation, Inc.
9 1.6.4.2 yamt * All rights reserved.
10 1.6.4.2 yamt *
11 1.6.4.2 yamt * This code is derived from software contributed to The NetBSD Foundation
12 1.6.4.2 yamt * by Taylor R. Campbell.
13 1.6.4.2 yamt *
14 1.6.4.2 yamt * Redistribution and use in source and binary forms, with or without
15 1.6.4.2 yamt * modification, are permitted provided that the following conditions
16 1.6.4.2 yamt * are met:
17 1.6.4.2 yamt * 1. Redistributions of source code must retain the above copyright
18 1.6.4.2 yamt * notice, this list of conditions and the following disclaimer.
19 1.6.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
20 1.6.4.2 yamt * notice, this list of conditions and the following disclaimer in the
21 1.6.4.2 yamt * documentation and/or other materials provided with the distribution.
22 1.6.4.2 yamt *
23 1.6.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.6.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.6.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.6.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.6.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.6.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.6.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.6.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.6.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.6.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.6.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
34 1.6.4.2 yamt */
35 1.6.4.2 yamt
36 1.6.4.2 yamt #include <sys/cdefs.h>
37 1.6.4.2 yamt __KERNEL_RCSID(0, "$NetBSD: apple_smc.c,v 1.6.4.2 2014/05/22 11:40:21 yamt Exp $");
38 1.6.4.2 yamt
39 1.6.4.2 yamt #include <sys/types.h>
40 1.6.4.2 yamt #include <sys/param.h>
41 1.6.4.2 yamt #include <sys/device.h>
42 1.6.4.2 yamt #include <sys/errno.h>
43 1.6.4.2 yamt #include <sys/kmem.h>
44 1.6.4.2 yamt #include <sys/module.h>
45 1.6.4.2 yamt #include <sys/mutex.h>
46 1.6.4.2 yamt #include <sys/rwlock.h>
47 1.6.4.2 yamt #if 0 /* XXX sysctl */
48 1.6.4.2 yamt #include <sys/sysctl.h>
49 1.6.4.2 yamt #endif
50 1.6.4.2 yamt #include <sys/systm.h>
51 1.6.4.2 yamt
52 1.6.4.2 yamt #include <dev/ic/apple_smc.h>
53 1.6.4.2 yamt #include <dev/ic/apple_smcreg.h>
54 1.6.4.2 yamt #include <dev/ic/apple_smcvar.h>
55 1.6.4.2 yamt
56 1.6.4.2 yamt /* Must match the config(5) name. */
57 1.6.4.2 yamt #define APPLE_SMC_BUS "applesmcbus"
58 1.6.4.2 yamt
59 1.6.4.2 yamt static int apple_smc_search(device_t, cfdata_t, const int *, void *);
60 1.6.4.2 yamt static uint8_t apple_smc_bus_read_1(struct apple_smc_tag *, bus_size_t);
61 1.6.4.2 yamt static void apple_smc_bus_write_1(struct apple_smc_tag *, bus_size_t,
62 1.6.4.2 yamt uint8_t);
63 1.6.4.2 yamt static int apple_smc_read_data(struct apple_smc_tag *, uint8_t *);
64 1.6.4.2 yamt static int apple_smc_write(struct apple_smc_tag *, bus_size_t, uint8_t);
65 1.6.4.2 yamt static int apple_smc_write_cmd(struct apple_smc_tag *, uint8_t);
66 1.6.4.2 yamt static int apple_smc_write_data(struct apple_smc_tag *, uint8_t);
67 1.6.4.2 yamt static int apple_smc_begin(struct apple_smc_tag *, uint8_t,
68 1.6.4.2 yamt const char *, uint8_t);
69 1.6.4.2 yamt static int apple_smc_input(struct apple_smc_tag *, uint8_t,
70 1.6.4.2 yamt const char *, void *, uint8_t);
71 1.6.4.2 yamt static int apple_smc_output(struct apple_smc_tag *, uint8_t,
72 1.6.4.2 yamt const char *, const void *, uint8_t);
73 1.6.4.2 yamt
74 1.6.4.2 yamt void
76 1.6.4.2 yamt apple_smc_attach(struct apple_smc_tag *smc)
77 1.6.4.2 yamt {
78 1.6.4.2 yamt
79 1.6.4.2 yamt mutex_init(&smc->smc_io_lock, MUTEX_DEFAULT, IPL_NONE);
80 1.6.4.2 yamt #if 0 /* XXX sysctl */
81 1.6.4.2 yamt apple_smc_sysctl_setup(smc);
82 1.6.4.2 yamt #endif
83 1.6.4.2 yamt
84 1.6.4.2 yamt /* Attach any children. */
85 1.6.4.2 yamt (void)apple_smc_rescan(smc, APPLE_SMC_BUS, NULL);
86 1.6.4.2 yamt }
87 1.6.4.2 yamt
88 1.6.4.2 yamt int
89 1.6.4.2 yamt apple_smc_detach(struct apple_smc_tag *smc, int flags)
90 1.6.4.2 yamt {
91 1.6.4.2 yamt int error;
92 1.6.4.2 yamt
93 1.6.4.2 yamt /* Fail if we can't detach all our children. */
94 1.6.4.2 yamt error = config_detach_children(smc->smc_dev, flags);
95 1.6.4.2 yamt if (error)
96 1.6.4.2 yamt return error;
97 1.6.4.2 yamt
98 1.6.4.2 yamt #if 0 /* XXX sysctl */
99 1.6.4.2 yamt sysctl_teardown(&smc->smc_log);
100 1.6.4.2 yamt #endif
101 1.6.4.2 yamt mutex_destroy(&smc->smc_io_lock);
102 1.6.4.2 yamt
103 1.6.4.2 yamt return 0;
104 1.6.4.2 yamt }
105 1.6.4.2 yamt
106 1.6.4.2 yamt int
107 1.6.4.2 yamt apple_smc_rescan(struct apple_smc_tag *smc, const char *ifattr,
108 1.6.4.2 yamt const int *locators)
109 1.6.4.2 yamt {
110 1.6.4.2 yamt
111 1.6.4.2 yamt /* Let autoconf(9) do the work of finding new children. */
112 1.6.4.2 yamt (void)config_search_loc(&apple_smc_search, smc->smc_dev, APPLE_SMC_BUS,
113 1.6.4.2 yamt locators, smc);
114 1.6.4.2 yamt return 0;
115 1.6.4.2 yamt }
116 1.6.4.2 yamt
117 1.6.4.2 yamt static int
118 1.6.4.2 yamt apple_smc_search(device_t parent, cfdata_t cf, const int *locators, void *aux)
119 1.6.4.2 yamt {
120 1.6.4.2 yamt struct apple_smc_tag *const smc = aux;
121 1.6.4.2 yamt static const struct apple_smc_attach_args zero_asa;
122 1.6.4.2 yamt struct apple_smc_attach_args asa = zero_asa;
123 1.6.4.2 yamt device_t dev;
124 1.6.4.2 yamt deviter_t di;
125 1.6.4.2 yamt bool attached = false;
126 1.6.4.2 yamt
127 1.6.4.2 yamt /*
128 1.6.4.2 yamt * If this device has already attached, don't attach it again.
129 1.6.4.2 yamt *
130 1.6.4.2 yamt * XXX This is a pretty silly way to query the children, but
131 1.6.4.2 yamt * struct device doesn't seem to list its children.
132 1.6.4.2 yamt */
133 1.6.4.2 yamt for (dev = deviter_first(&di, DEVITER_F_LEAVES_FIRST);
134 1.6.4.2 yamt dev != NULL;
135 1.6.4.2 yamt dev = deviter_next(&di)) {
136 1.6.4.2 yamt if (device_parent(dev) != parent)
137 1.6.4.2 yamt continue;
138 1.6.4.2 yamt if (!device_is_a(dev, cf->cf_name))
139 1.6.4.2 yamt continue;
140 1.6.4.2 yamt attached = true;
141 1.6.4.2 yamt break;
142 1.6.4.2 yamt }
143 1.6.4.2 yamt deviter_release(&di);
144 1.6.4.2 yamt if (attached)
145 1.6.4.2 yamt return 0;
146 1.6.4.2 yamt
147 1.6.4.2 yamt /* If this device doesn't match, don't attach it. */
148 1.6.4.2 yamt if (!config_match(parent, cf, aux))
149 1.6.4.2 yamt return 0;
150 1.6.4.2 yamt
151 1.6.4.2 yamt /* Looks hunky-dory. Attach. */
152 1.6.4.2 yamt asa.asa_smc = smc;
153 1.6.4.2 yamt (void)config_attach_loc(parent, cf, locators, &asa, NULL);
154 1.6.4.2 yamt return 0;
155 1.6.4.2 yamt }
156 1.6.4.2 yamt
157 1.6.4.2 yamt void
158 1.6.4.2 yamt apple_smc_child_detached(struct apple_smc_tag *smc __unused,
159 1.6.4.2 yamt device_t child __unused)
160 1.6.4.2 yamt {
161 1.6.4.2 yamt /* We keep no books about our children. */
162 1.6.4.2 yamt }
163 1.6.4.2 yamt
164 1.6.4.2 yamt static uint8_t
166 1.6.4.2 yamt apple_smc_bus_read_1(struct apple_smc_tag *smc, bus_size_t reg)
167 1.6.4.2 yamt {
168 1.6.4.2 yamt
169 1.6.4.2 yamt return bus_space_read_1(smc->smc_bst, smc->smc_bsh, reg);
170 1.6.4.2 yamt }
171 1.6.4.2 yamt
172 1.6.4.2 yamt static void
173 1.6.4.2 yamt apple_smc_bus_write_1(struct apple_smc_tag *smc, bus_size_t reg, uint8_t v)
174 1.6.4.2 yamt {
175 1.6.4.2 yamt
176 1.6.4.2 yamt bus_space_write_1(smc->smc_bst, smc->smc_bsh, reg, v);
177 1.6.4.2 yamt }
178 1.6.4.2 yamt
179 1.6.4.2 yamt /*
180 1.6.4.2 yamt * XXX These delays are pretty randomly chosen. Wait in 100 us
181 1.6.4.2 yamt * increments, up to a total of 1 ms.
182 1.6.4.2 yamt */
183 1.6.4.2 yamt
184 1.6.4.2 yamt static int
185 1.6.4.2 yamt apple_smc_read_data(struct apple_smc_tag *smc, uint8_t *byte)
186 1.6.4.2 yamt {
187 1.6.4.2 yamt uint8_t status;
188 1.6.4.2 yamt unsigned int i;
189 1.6.4.2 yamt
190 1.6.4.2 yamt KASSERT(mutex_owned(&smc->smc_io_lock));
191 1.6.4.2 yamt
192 1.6.4.2 yamt /*
193 1.6.4.2 yamt * Wait until the status register says there's data to read and
194 1.6.4.2 yamt * read it.
195 1.6.4.2 yamt */
196 1.6.4.2 yamt for (i = 0; i < 100; i++) {
197 1.6.4.2 yamt status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
198 1.6.4.2 yamt if (status & APPLE_SMC_STATUS_READ_READY) {
199 1.6.4.2 yamt *byte = apple_smc_bus_read_1(smc, APPLE_SMC_DATA);
200 1.6.4.2 yamt return 0;
201 1.6.4.2 yamt }
202 1.6.4.2 yamt DELAY(100);
203 1.6.4.2 yamt }
204 1.6.4.2 yamt
205 1.6.4.2 yamt return ETIMEDOUT;
206 1.6.4.2 yamt }
207 1.6.4.2 yamt
208 1.6.4.2 yamt static int
209 1.6.4.2 yamt apple_smc_write(struct apple_smc_tag *smc, bus_size_t reg, uint8_t byte)
210 1.6.4.2 yamt {
211 1.6.4.2 yamt uint8_t status;
212 1.6.4.2 yamt unsigned int i;
213 1.6.4.2 yamt
214 1.6.4.2 yamt KASSERT(mutex_owned(&smc->smc_io_lock));
215 1.6.4.2 yamt
216 1.6.4.2 yamt /*
217 1.6.4.2 yamt * Write the byte and then wait until the status register says
218 1.6.4.2 yamt * it has been accepted.
219 1.6.4.2 yamt */
220 1.6.4.2 yamt apple_smc_bus_write_1(smc, reg, byte);
221 1.6.4.2 yamt for (i = 0; i < 100; i++) {
222 1.6.4.2 yamt status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
223 1.6.4.2 yamt if (status & APPLE_SMC_STATUS_WRITE_ACCEPTED)
224 1.6.4.2 yamt return 0;
225 1.6.4.2 yamt DELAY(100);
226 1.6.4.2 yamt
227 1.6.4.2 yamt /* Write again if it hasn't been acknowledged at all. */
228 1.6.4.2 yamt if (!(status & APPLE_SMC_STATUS_WRITE_PENDING))
229 1.6.4.2 yamt apple_smc_bus_write_1(smc, reg, byte);
230 1.6.4.2 yamt }
231 1.6.4.2 yamt
232 1.6.4.2 yamt return ETIMEDOUT;
233 1.6.4.2 yamt }
234 1.6.4.2 yamt
235 1.6.4.2 yamt static int
236 1.6.4.2 yamt apple_smc_write_cmd(struct apple_smc_tag *smc, uint8_t cmd)
237 1.6.4.2 yamt {
238 1.6.4.2 yamt
239 1.6.4.2 yamt return apple_smc_write(smc, APPLE_SMC_CSR, cmd);
240 1.6.4.2 yamt }
241 1.6.4.2 yamt
242 1.6.4.2 yamt static int
243 1.6.4.2 yamt apple_smc_write_data(struct apple_smc_tag *smc, uint8_t data)
244 1.6.4.2 yamt {
245 1.6.4.2 yamt
246 1.6.4.2 yamt return apple_smc_write(smc, APPLE_SMC_DATA, data);
247 1.6.4.2 yamt }
248 1.6.4.2 yamt
249 1.6.4.2 yamt static int
251 1.6.4.2 yamt apple_smc_begin(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
252 1.6.4.2 yamt uint8_t size)
253 1.6.4.2 yamt {
254 1.6.4.2 yamt unsigned int i;
255 1.6.4.2 yamt int error;
256 1.6.4.2 yamt
257 1.6.4.2 yamt KASSERT(mutex_owned(&smc->smc_io_lock));
258 1.6.4.2 yamt
259 1.6.4.2 yamt /* Write the command first. */
260 1.6.4.2 yamt error = apple_smc_write_cmd(smc, cmd);
261 1.6.4.2 yamt if (error)
262 1.6.4.2 yamt return error;
263 1.6.4.2 yamt
264 1.6.4.2 yamt /* Write the key next. */
265 1.6.4.2 yamt for (i = 0; i < 4; i++) {
266 1.6.4.2 yamt error = apple_smc_write_data(smc, key[i]);
267 1.6.4.2 yamt if (error)
268 1.6.4.2 yamt return error;
269 1.6.4.2 yamt }
270 1.6.4.2 yamt
271 1.6.4.2 yamt /* Finally, report how many bytes of data we want to send/receive. */
272 1.6.4.2 yamt error = apple_smc_write_data(smc, size);
273 1.6.4.2 yamt if (error)
274 1.6.4.2 yamt return error;
275 1.6.4.2 yamt
276 1.6.4.2 yamt return 0;
277 1.6.4.2 yamt }
278 1.6.4.2 yamt
279 1.6.4.2 yamt static int
280 1.6.4.2 yamt apple_smc_input(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
281 1.6.4.2 yamt void *buffer, uint8_t size)
282 1.6.4.2 yamt {
283 1.6.4.2 yamt uint8_t *bytes = buffer;
284 1.6.4.2 yamt uint8_t i;
285 1.6.4.2 yamt int error;
286 1.6.4.2 yamt
287 1.6.4.2 yamt /* Grab the SMC I/O lock. */
288 1.6.4.2 yamt mutex_enter(&smc->smc_io_lock);
289 1.6.4.2 yamt
290 1.6.4.2 yamt /* Initiate the command with this key. */
291 1.6.4.2 yamt error = apple_smc_begin(smc, cmd, key, size);
292 1.6.4.2 yamt if (error)
293 1.6.4.2 yamt goto out;
294 1.6.4.2 yamt
295 1.6.4.2 yamt /* Read each byte of data in sequence. */
296 1.6.4.2 yamt for (i = 0; i < size; i++) {
297 1.6.4.2 yamt error = apple_smc_read_data(smc, &bytes[i]);
298 1.6.4.2 yamt if (error)
299 1.6.4.2 yamt goto out;
300 1.6.4.2 yamt }
301 1.6.4.2 yamt
302 1.6.4.2 yamt /* Success! */
303 1.6.4.2 yamt error = 0;
304 1.6.4.2 yamt
305 1.6.4.2 yamt out: mutex_exit(&smc->smc_io_lock);
306 1.6.4.2 yamt return error;
307 1.6.4.2 yamt }
308 1.6.4.2 yamt
309 1.6.4.2 yamt static int
310 1.6.4.2 yamt apple_smc_output(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
311 1.6.4.2 yamt const void *buffer, uint8_t size)
312 1.6.4.2 yamt {
313 1.6.4.2 yamt const uint8_t *bytes = buffer;
314 1.6.4.2 yamt uint8_t i;
315 1.6.4.2 yamt int error;
316 1.6.4.2 yamt
317 1.6.4.2 yamt /* Grab the SMC I/O lock. */
318 1.6.4.2 yamt mutex_enter(&smc->smc_io_lock);
319 1.6.4.2 yamt
320 1.6.4.2 yamt /* Initiate the command with this key. */
321 1.6.4.2 yamt error = apple_smc_begin(smc, cmd, key, size);
322 1.6.4.2 yamt if (error)
323 1.6.4.2 yamt goto out;
324 1.6.4.2 yamt
325 1.6.4.2 yamt /* Write each byte of data in sequence. */
326 1.6.4.2 yamt for (i = 0; i < size; i++) {
327 1.6.4.2 yamt error = apple_smc_write_data(smc, bytes[i]);
328 1.6.4.2 yamt if (error)
329 1.6.4.2 yamt goto out;
330 1.6.4.2 yamt }
331 1.6.4.2 yamt
332 1.6.4.2 yamt /* Success! */
333 1.6.4.2 yamt error = 0;
334 1.6.4.2 yamt
335 1.6.4.2 yamt out: mutex_exit(&smc->smc_io_lock);
336 1.6.4.2 yamt return error;
337 1.6.4.2 yamt }
338 1.6.4.2 yamt
339 1.6.4.2 yamt struct apple_smc_key {
341 1.6.4.2 yamt char ask_name[4 + 1];
342 1.6.4.2 yamt struct apple_smc_desc ask_desc;
343 1.6.4.2 yamt #ifdef DIAGNOSTIC
344 1.6.4.2 yamt struct apple_smc_tag *ask_smc;
345 1.6.4.2 yamt #endif
346 1.6.4.2 yamt };
347 1.6.4.2 yamt
348 1.6.4.2 yamt const char *
349 1.6.4.2 yamt apple_smc_key_name(const struct apple_smc_key *key)
350 1.6.4.2 yamt {
351 1.6.4.2 yamt
352 1.6.4.2 yamt return key->ask_name;
353 1.6.4.2 yamt }
354 1.6.4.2 yamt
355 1.6.4.2 yamt const struct apple_smc_desc *
356 1.6.4.2 yamt apple_smc_key_desc(const struct apple_smc_key *key)
357 1.6.4.2 yamt {
358 1.6.4.2 yamt
359 1.6.4.2 yamt return &key->ask_desc;
360 1.6.4.2 yamt }
361 1.6.4.2 yamt
362 1.6.4.2 yamt uint32_t
363 1.6.4.2 yamt apple_smc_nkeys(struct apple_smc_tag *smc)
364 1.6.4.2 yamt {
365 1.6.4.2 yamt
366 1.6.4.2 yamt return smc->smc_nkeys;
367 1.6.4.2 yamt }
368 1.6.4.2 yamt
369 1.6.4.2 yamt int
370 1.6.4.2 yamt apple_smc_nth_key(struct apple_smc_tag *smc, uint32_t index,
371 1.6.4.2 yamt const char type[4 + 1], struct apple_smc_key **keyp)
372 1.6.4.2 yamt {
373 1.6.4.2 yamt union { uint32_t u32; char name[4]; } index_be;
374 1.6.4.2 yamt struct apple_smc_key *key;
375 1.6.4.2 yamt int error;
376 1.6.4.2 yamt
377 1.6.4.2 yamt /* Paranoia: type must be NULL or 4 non-null characters long. */
378 1.6.4.2 yamt if ((type != NULL) && (strlen(type) != 4))
379 1.6.4.2 yamt return EINVAL;
380 1.6.4.2 yamt
381 1.6.4.2 yamt /* Create a new key. XXX Consider caching these. */
382 1.6.4.2 yamt key = kmem_alloc(sizeof(*key), KM_SLEEP);
383 1.6.4.2 yamt #ifdef DIAGNOSTIC
384 1.6.4.2 yamt key->ask_smc = smc;
385 1.6.4.2 yamt #endif
386 1.6.4.2 yamt
387 1.6.4.2 yamt /* Ask the SMC what the name of the key by this number is. */
388 1.6.4.2 yamt index_be.u32 = htobe32(index);
389 1.6.4.2 yamt error = apple_smc_input(smc, APPLE_SMC_CMD_NTH_KEY, index_be.name,
390 1.6.4.2 yamt key->ask_name, 4);
391 1.6.4.2 yamt if (error)
392 1.6.4.2 yamt goto fail;
393 1.6.4.2 yamt
394 1.6.4.2 yamt /* Null-terminate the name. */
395 1.6.4.2 yamt key->ask_name[4] = '\0';
396 1.6.4.2 yamt
397 1.6.4.2 yamt /* Ask the SMC for a description of this key by name. */
398 1.6.4.2 yamt CTASSERT(sizeof(key->ask_desc) == 6);
399 1.6.4.2 yamt error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
400 1.6.4.2 yamt &key->ask_desc, 6);
401 1.6.4.2 yamt if (error)
402 1.6.4.2 yamt goto fail;
403 1.6.4.2 yamt
404 1.6.4.2 yamt /* Fail with EINVAL if the types don't match. */
405 1.6.4.2 yamt if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
406 1.6.4.2 yamt error = EINVAL;
407 1.6.4.2 yamt goto fail;
408 1.6.4.2 yamt }
409 1.6.4.2 yamt
410 1.6.4.2 yamt /* Success! */
411 1.6.4.2 yamt *keyp = key;
412 1.6.4.2 yamt return 0;
413 1.6.4.2 yamt
414 1.6.4.2 yamt fail: kmem_free(key, sizeof(*key));
415 1.6.4.2 yamt return error;
416 1.6.4.2 yamt }
417 1.6.4.2 yamt
418 1.6.4.2 yamt int
419 1.6.4.2 yamt apple_smc_named_key(struct apple_smc_tag *smc, const char name[4 + 1],
420 1.6.4.2 yamt const char type[4 + 1], struct apple_smc_key **keyp)
421 1.6.4.2 yamt {
422 1.6.4.2 yamt struct apple_smc_key *key;
423 1.6.4.2 yamt int error;
424 1.6.4.2 yamt
425 1.6.4.2 yamt /* Paranoia: name must be 4 non-null characters long. */
426 1.6.4.2 yamt KASSERT(name != NULL);
427 1.6.4.2 yamt if (strlen(name) != 4)
428 1.6.4.2 yamt return EINVAL;
429 1.6.4.2 yamt
430 1.6.4.2 yamt /* Paranoia: type must be NULL or 4 non-null characters long. */
431 1.6.4.2 yamt if ((type != NULL) && (strlen(type) != 4))
432 1.6.4.2 yamt return EINVAL;
433 1.6.4.2 yamt
434 1.6.4.2 yamt /* Create a new key. XXX Consider caching these. */
435 1.6.4.2 yamt key = kmem_alloc(sizeof(*key), KM_SLEEP);
436 1.6.4.2 yamt #ifdef DIAGNOSTIC
437 1.6.4.2 yamt key->ask_smc = smc;
438 1.6.4.2 yamt #endif
439 1.6.4.2 yamt
440 1.6.4.2 yamt /* Use the specified name, and make sure it's null-terminated. */
441 1.6.4.2 yamt (void)memcpy(key->ask_name, name, 4);
442 1.6.4.2 yamt key->ask_name[4] = '\0';
443 1.6.4.2 yamt
444 1.6.4.2 yamt /* Ask the SMC for a description of this key by name. */
445 1.6.4.2 yamt CTASSERT(sizeof(key->ask_desc) == 6);
446 1.6.4.2 yamt error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
447 1.6.4.2 yamt &key->ask_desc, 6);
448 1.6.4.2 yamt if (error)
449 1.6.4.2 yamt goto fail;
450 1.6.4.2 yamt
451 1.6.4.2 yamt /* Fail with EINVAL if the types don't match. */
452 1.6.4.2 yamt if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
453 1.6.4.2 yamt error = EINVAL;
454 1.6.4.2 yamt goto fail;
455 1.6.4.2 yamt }
456 1.6.4.2 yamt
457 1.6.4.2 yamt /* Success! */
458 1.6.4.2 yamt *keyp = key;
459 1.6.4.2 yamt return 0;
460 1.6.4.2 yamt
461 1.6.4.2 yamt fail: kmem_free(key, sizeof(*key));
462 1.6.4.2 yamt return error;
463 1.6.4.2 yamt }
464 1.6.4.2 yamt
465 1.6.4.2 yamt void
466 1.6.4.2 yamt apple_smc_release_key(struct apple_smc_tag *smc, struct apple_smc_key *key)
467 1.6.4.2 yamt {
468 1.6.4.2 yamt
469 1.6.4.2 yamt #ifdef DIAGNOSTIC
470 1.6.4.2 yamt /* Make sure the caller didn't mix up SMC tags. */
471 1.6.4.2 yamt if (key->ask_smc != smc)
472 1.6.4.2 yamt aprint_error_dev(smc->smc_dev,
473 1.6.4.2 yamt "releasing key with wrong tag: %p != %p",
474 1.6.4.2 yamt smc, key->ask_smc);
475 1.6.4.2 yamt #endif
476 1.6.4.2 yamt
477 1.6.4.2 yamt /* Nothing to do but free the key's memory. */
478 1.6.4.2 yamt kmem_free(key, sizeof(*key));
479 1.6.4.2 yamt }
480 1.6.4.2 yamt
481 1.6.4.2 yamt int
482 1.6.4.2 yamt apple_smc_key_search(struct apple_smc_tag *smc, const char *name,
483 1.6.4.2 yamt uint32_t *result)
484 1.6.4.2 yamt {
485 1.6.4.2 yamt struct apple_smc_key *key;
486 1.6.4.2 yamt uint32_t start = 0, end = apple_smc_nkeys(smc), median;
487 1.6.4.2 yamt int cmp;
488 1.6.4.2 yamt int error;
489 1.6.4.2 yamt
490 1.6.4.2 yamt /* Do a binary search on the SMC's key space. */
491 1.6.4.2 yamt while (start < end) {
492 1.6.4.2 yamt median = (start + ((end - start) / 2));
493 1.6.4.2 yamt error = apple_smc_nth_key(smc, median, NULL, &key);
494 1.6.4.2 yamt if (error)
495 1.6.4.2 yamt return error;
496 1.6.4.2 yamt
497 1.6.4.2 yamt cmp = memcmp(name, apple_smc_key_name(key), 4);
498 1.6.4.2 yamt if (cmp < 0)
499 1.6.4.2 yamt end = median;
500 1.6.4.2 yamt else if (cmp > 0)
501 1.6.4.2 yamt start = (median + 1);
502 1.6.4.2 yamt else
503 1.6.4.2 yamt start = end = median; /* stop here */
504 1.6.4.2 yamt
505 1.6.4.2 yamt apple_smc_release_key(smc, key);
506 1.6.4.2 yamt }
507 1.6.4.2 yamt
508 1.6.4.2 yamt /* Success! */
509 1.6.4.2 yamt *result = start;
510 1.6.4.2 yamt return 0;
511 1.6.4.2 yamt }
512 1.6.4.2 yamt
513 1.6.4.2 yamt int
515 1.6.4.2 yamt apple_smc_read_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
516 1.6.4.2 yamt void *buffer, uint8_t size)
517 1.6.4.2 yamt {
518 1.6.4.2 yamt
519 1.6.4.2 yamt /* Refuse if software and hardware disagree on the key's size. */
520 1.6.4.2 yamt if (key->ask_desc.asd_size != size)
521 1.6.4.2 yamt return EINVAL;
522 1.6.4.2 yamt
523 1.6.4.2 yamt /* Refuse if the hardware doesn't want us to read it. */
524 1.6.4.2 yamt if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_READ))
525 1.6.4.2 yamt return EACCES;
526 1.6.4.2 yamt
527 1.6.4.2 yamt /* Looks good. Try reading it from the hardware. */
528 1.6.4.2 yamt return apple_smc_input(smc, APPLE_SMC_CMD_READ_KEY, key->ask_name,
529 1.6.4.2 yamt buffer, size);
530 1.6.4.2 yamt }
531 1.6.4.2 yamt
532 1.6.4.2 yamt int
533 1.6.4.2 yamt apple_smc_read_key_1(struct apple_smc_tag *smc,
534 1.6.4.2 yamt const struct apple_smc_key *key, uint8_t *p)
535 1.6.4.2 yamt {
536 1.6.4.2 yamt
537 1.6.4.2 yamt return apple_smc_read_key(smc, key, p, 1);
538 1.6.4.2 yamt }
539 1.6.4.2 yamt
540 1.6.4.2 yamt int
541 1.6.4.2 yamt apple_smc_read_key_2(struct apple_smc_tag *smc,
542 1.6.4.2 yamt const struct apple_smc_key *key, uint16_t *p)
543 1.6.4.2 yamt {
544 1.6.4.2 yamt uint16_t be;
545 1.6.4.2 yamt int error;
546 1.6.4.2 yamt
547 1.6.4.2 yamt /* Read a big-endian quantity from the hardware. */
548 1.6.4.2 yamt error = apple_smc_read_key(smc, key, &be, 2);
549 1.6.4.2 yamt if (error)
550 1.6.4.2 yamt return error;
551 1.6.4.2 yamt
552 1.6.4.2 yamt /* Convert it to host order. */
553 1.6.4.2 yamt *p = be16toh(be);
554 1.6.4.2 yamt
555 1.6.4.2 yamt /* Success! */
556 1.6.4.2 yamt return 0;
557 1.6.4.2 yamt }
558 1.6.4.2 yamt
559 1.6.4.2 yamt int
560 1.6.4.2 yamt apple_smc_read_key_4(struct apple_smc_tag *smc,
561 1.6.4.2 yamt const struct apple_smc_key *key, uint32_t *p)
562 1.6.4.2 yamt {
563 1.6.4.2 yamt uint32_t be;
564 1.6.4.2 yamt int error;
565 1.6.4.2 yamt
566 1.6.4.2 yamt /* Read a big-endian quantity from the hardware. */
567 1.6.4.2 yamt error = apple_smc_read_key(smc, key, &be, 4);
568 1.6.4.2 yamt if (error)
569 1.6.4.2 yamt return error;
570 1.6.4.2 yamt
571 1.6.4.2 yamt /* Convert it to host order. */
572 1.6.4.2 yamt *p = be32toh(be);
573 1.6.4.2 yamt
574 1.6.4.2 yamt /* Success! */
575 1.6.4.2 yamt return 0;
576 1.6.4.2 yamt }
577 1.6.4.2 yamt
578 1.6.4.2 yamt int
579 1.6.4.2 yamt apple_smc_write_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
580 1.6.4.2 yamt const void *buffer, uint8_t size)
581 1.6.4.2 yamt {
582 1.6.4.2 yamt
583 1.6.4.2 yamt /* Refuse if software and hardware disagree on the key's size. */
584 1.6.4.2 yamt if (key->ask_desc.asd_size != size)
585 1.6.4.2 yamt return EINVAL;
586 1.6.4.2 yamt
587 1.6.4.2 yamt /* Refuse if the hardware doesn't want us to write it. */
588 1.6.4.2 yamt if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_WRITE))
589 1.6.4.2 yamt return EACCES;
590 1.6.4.2 yamt
591 1.6.4.2 yamt /* Looks good. Try writing it to the hardware. */
592 1.6.4.2 yamt return apple_smc_output(smc, APPLE_SMC_CMD_WRITE_KEY, key->ask_name,
593 1.6.4.2 yamt buffer, size);
594 1.6.4.2 yamt }
595 1.6.4.2 yamt
596 1.6.4.2 yamt int
597 1.6.4.2 yamt apple_smc_write_key_1(struct apple_smc_tag *smc,
598 1.6.4.2 yamt const struct apple_smc_key *key, uint8_t v)
599 1.6.4.2 yamt {
600 1.6.4.2 yamt
601 1.6.4.2 yamt return apple_smc_write_key(smc, key, &v, 1);
602 1.6.4.2 yamt }
603 1.6.4.2 yamt
604 1.6.4.2 yamt int
605 1.6.4.2 yamt apple_smc_write_key_2(struct apple_smc_tag *smc,
606 1.6.4.2 yamt const struct apple_smc_key *key, uint16_t v)
607 1.6.4.2 yamt {
608 1.6.4.2 yamt /* Convert the quantity from host to big-endian byte order. */
609 1.6.4.2 yamt const uint16_t v_be = htobe16(v);
610 1.6.4.2 yamt
611 1.6.4.2 yamt /* Write the big-endian quantity to the hardware. */
612 1.6.4.2 yamt return apple_smc_write_key(smc, key, &v_be, 2);
613 1.6.4.2 yamt }
614 1.6.4.2 yamt
615 1.6.4.2 yamt int
616 1.6.4.2 yamt apple_smc_write_key_4(struct apple_smc_tag *smc,
617 1.6.4.2 yamt const struct apple_smc_key *key, uint32_t v)
618 1.6.4.2 yamt {
619 1.6.4.2 yamt /* Convert the quantity from host to big-endian byte order. */
620 1.6.4.2 yamt const uint32_t v_be = htobe32(v);
621 1.6.4.2 yamt
622 1.6.4.2 yamt /* Write the big-endian quantity to the hardware. */
623 1.6.4.2 yamt return apple_smc_write_key(smc, key, &v_be, 4);
624 1.6.4.2 yamt }
625 1.6.4.2 yamt
626 1.6.4.2 yamt MODULE(MODULE_CLASS_MISC, apple_smc, NULL)
628 1.6.4.2 yamt
629 1.6.4.2 yamt static int
630 1.6.4.2 yamt apple_smc_modcmd(modcmd_t cmd, void *data __unused)
631 1.6.4.2 yamt {
632 1.6.4.2 yamt
633 1.6.4.2 yamt /* Nothing to do for now to set up or tear down the module. */
634 1.6.4.2 yamt switch (cmd) {
635 1.6.4.2 yamt case MODULE_CMD_INIT:
636 1.6.4.2 yamt return 0;
637 1.6.4.2 yamt
638 1.6.4.2 yamt case MODULE_CMD_FINI:
639 return 0;
640
641 default:
642 return ENOTTY;
643 }
644 }
645