xref: /qemu/hw/misc/applesmc.c (revision 60efffa4)
1 /*
2  *  Apple SMC controller
3  *
4  *  Copyright (c) 2007 Alexander Graf
5  *
6  *  Authors: Alexander Graf <agraf@suse.de>
7  *           Susanne Graf <suse@csgraf.de>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21  *
22  * *****************************************************************
23  *
24  * In all Intel-based Apple hardware there is an SMC chip to control the
25  * backlight, fans and several other generic device parameters. It also
26  * contains the magic keys used to dongle Mac OS X to the device.
27  *
28  * This driver was mostly created by looking at the Linux AppleSMC driver
29  * implementation and does not support IRQ.
30  *
31  */
32 
33 #include "qemu/osdep.h"
34 #include "hw/isa/isa.h"
35 #include "hw/qdev-properties.h"
36 #include "ui/console.h"
37 #include "qemu/module.h"
38 #include "qemu/timer.h"
39 #include "qom/object.h"
40 
41 /* #define DEBUG_SMC */
42 
43 #define APPLESMC_DEFAULT_IOBASE        0x300
44 
45 enum {
46     APPLESMC_DATA_PORT               = 0x00,
47     APPLESMC_CMD_PORT                = 0x04,
48     APPLESMC_ERR_PORT                = 0x1e,
49     APPLESMC_NUM_PORTS               = 0x20,
50 };
51 
52 enum {
53     APPLESMC_READ_CMD                = 0x10,
54     APPLESMC_WRITE_CMD               = 0x11,
55     APPLESMC_GET_KEY_BY_INDEX_CMD    = 0x12,
56     APPLESMC_GET_KEY_TYPE_CMD        = 0x13,
57 };
58 
59 enum {
60     APPLESMC_ST_CMD_DONE             = 0x00,
61     APPLESMC_ST_DATA_READY           = 0x01,
62     APPLESMC_ST_BUSY                 = 0x02,
63     APPLESMC_ST_ACK                  = 0x04,
64     APPLESMC_ST_NEW_CMD              = 0x08,
65 };
66 
67 enum {
68     APPLESMC_ST_1E_CMD_INTRUPTED     = 0x80,
69     APPLESMC_ST_1E_STILL_BAD_CMD     = 0x81,
70     APPLESMC_ST_1E_BAD_CMD           = 0x82,
71     APPLESMC_ST_1E_NOEXIST           = 0x84,
72     APPLESMC_ST_1E_WRITEONLY         = 0x85,
73     APPLESMC_ST_1E_READONLY          = 0x86,
74     APPLESMC_ST_1E_BAD_INDEX         = 0xb8,
75 };
76 
77 #ifdef DEBUG_SMC
78 #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__)
79 #else
80 #define smc_debug(...) do { } while (0)
81 #endif
82 
83 static char default_osk[64] = "This is a dummy key. Enter the real key "
84                               "using the -osk parameter";
85 
86 struct AppleSMCData {
87     uint8_t len;
88     const char *key;
89     const char *data;
90     QLIST_ENTRY(AppleSMCData) node;
91 };
92 
93 typedef struct AppleSMCState AppleSMCState;
94 DECLARE_INSTANCE_CHECKER(AppleSMCState, APPLE_SMC,
95                          TYPE_APPLE_SMC)
96 
97 struct AppleSMCState {
98     ISADevice parent_obj;
99 
100     MemoryRegion io_data;
101     MemoryRegion io_cmd;
102     MemoryRegion io_err;
103     uint32_t iobase;
104     uint8_t cmd;
105     uint8_t status;
106     uint8_t status_1e;
107     uint8_t last_ret;
108     char key[4];
109     uint8_t read_pos;
110     uint8_t data_len;
111     uint8_t data_pos;
112     uint8_t data[255];
113     char *osk;
114     QLIST_HEAD(, AppleSMCData) data_def;
115 };
116 
117 static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val,
118                                   unsigned size)
119 {
120     AppleSMCState *s = opaque;
121     uint8_t status = s->status & 0x0f;
122 
123     smc_debug("CMD received: 0x%02x\n", (uint8_t)val);
124     switch (val) {
125     case APPLESMC_READ_CMD:
126         /* did last command run through OK? */
127         if (status == APPLESMC_ST_CMD_DONE || status == APPLESMC_ST_NEW_CMD) {
128             s->cmd = val;
129             s->status = APPLESMC_ST_NEW_CMD | APPLESMC_ST_ACK;
130         } else {
131             smc_debug("ERROR: previous command interrupted!\n");
132             s->status = APPLESMC_ST_NEW_CMD;
133             s->status_1e = APPLESMC_ST_1E_CMD_INTRUPTED;
134         }
135         break;
136     default:
137         smc_debug("UNEXPECTED CMD 0x%02x\n", (uint8_t)val);
138         s->status = APPLESMC_ST_NEW_CMD;
139         s->status_1e = APPLESMC_ST_1E_BAD_CMD;
140     }
141     s->read_pos = 0;
142     s->data_pos = 0;
143 }
144 
145 static struct AppleSMCData *applesmc_find_key(AppleSMCState *s)
146 {
147     struct AppleSMCData *d;
148 
149     QLIST_FOREACH(d, &s->data_def, node) {
150         if (!memcmp(d->key, s->key, 4)) {
151             return d;
152         }
153     }
154     return NULL;
155 }
156 
157 static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val,
158                                    unsigned size)
159 {
160     AppleSMCState *s = opaque;
161     struct AppleSMCData *d;
162 
163     smc_debug("DATA received: 0x%02x\n", (uint8_t)val);
164     switch (s->cmd) {
165     case APPLESMC_READ_CMD:
166         if ((s->status & 0x0f) == APPLESMC_ST_CMD_DONE) {
167             break;
168         }
169         if (s->read_pos < 4) {
170             s->key[s->read_pos] = val;
171             s->status = APPLESMC_ST_ACK;
172         } else if (s->read_pos == 4) {
173             d = applesmc_find_key(s);
174             if (d != NULL) {
175                 memcpy(s->data, d->data, d->len);
176                 s->data_len = d->len;
177                 s->data_pos = 0;
178                 s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
179                 s->status_1e = APPLESMC_ST_CMD_DONE;  /* clear on valid key */
180             } else {
181                 smc_debug("READ_CMD: key '%c%c%c%c' not found!\n",
182                           s->key[0], s->key[1], s->key[2], s->key[3]);
183                 s->status = APPLESMC_ST_CMD_DONE;
184                 s->status_1e = APPLESMC_ST_1E_NOEXIST;
185             }
186         }
187         s->read_pos++;
188         break;
189     default:
190         s->status = APPLESMC_ST_CMD_DONE;
191         s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
192     }
193 }
194 
195 static void applesmc_io_err_write(void *opaque, hwaddr addr, uint64_t val,
196                                   unsigned size)
197 {
198     smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val);
199     /* NOTE: writing to the error port not supported! */
200 }
201 
202 static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr, unsigned size)
203 {
204     AppleSMCState *s = opaque;
205 
206     switch (s->cmd) {
207     case APPLESMC_READ_CMD:
208         if (!(s->status & APPLESMC_ST_DATA_READY)) {
209             break;
210         }
211         if (s->data_pos < s->data_len) {
212             s->last_ret = s->data[s->data_pos];
213             smc_debug("READ '%c%c%c%c'[%d] = %02x\n",
214                       s->key[0], s->key[1], s->key[2], s->key[3],
215                       s->data_pos, s->last_ret);
216             s->data_pos++;
217             if (s->data_pos == s->data_len) {
218                 s->status = APPLESMC_ST_CMD_DONE;
219                 smc_debug("READ '%c%c%c%c' Len=%d complete!\n",
220                           s->key[0], s->key[1], s->key[2], s->key[3],
221                           s->data_len);
222             } else {
223                 s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
224             }
225         }
226         break;
227     default:
228         s->status = APPLESMC_ST_CMD_DONE;
229         s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
230     }
231     smc_debug("DATA sent: 0x%02x\n", s->last_ret);
232 
233     return s->last_ret;
234 }
235 
236 static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr, unsigned size)
237 {
238     AppleSMCState *s = opaque;
239 
240     smc_debug("CMD sent: 0x%02x\n", s->status);
241     return s->status;
242 }
243 
244 static uint64_t applesmc_io_err_read(void *opaque, hwaddr addr, unsigned size)
245 {
246     AppleSMCState *s = opaque;
247 
248     /* NOTE: read does not clear the 1e status */
249     smc_debug("ERR_CODE sent: 0x%02x\n", s->status_1e);
250     return s->status_1e;
251 }
252 
253 static void applesmc_add_key(AppleSMCState *s, const char *key,
254                              int len, const char *data)
255 {
256     struct AppleSMCData *def;
257 
258     def = g_malloc0(sizeof(struct AppleSMCData));
259     def->key = key;
260     def->len = len;
261     def->data = data;
262 
263     QLIST_INSERT_HEAD(&s->data_def, def, node);
264 }
265 
266 static void qdev_applesmc_isa_reset(DeviceState *dev)
267 {
268     AppleSMCState *s = APPLE_SMC(dev);
269     struct AppleSMCData *d, *next;
270 
271     /* Remove existing entries */
272     QLIST_FOREACH_SAFE(d, &s->data_def, node, next) {
273         QLIST_REMOVE(d, node);
274     }
275     s->status = 0x00;
276     s->status_1e = 0x00;
277     s->last_ret = 0x00;
278 
279     applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
280     applesmc_add_key(s, "OSK0", 32, s->osk);
281     applesmc_add_key(s, "OSK1", 32, s->osk + 32);
282     applesmc_add_key(s, "NATJ", 1, "\0");
283     applesmc_add_key(s, "MSSP", 1, "\0");
284     applesmc_add_key(s, "MSSD", 1, "\0x3");
285 }
286 
287 static const MemoryRegionOps applesmc_data_io_ops = {
288     .write = applesmc_io_data_write,
289     .read = applesmc_io_data_read,
290     .endianness = DEVICE_NATIVE_ENDIAN,
291     .impl = {
292         .min_access_size = 1,
293         .max_access_size = 1,
294     },
295 };
296 
297 static const MemoryRegionOps applesmc_cmd_io_ops = {
298     .write = applesmc_io_cmd_write,
299     .read = applesmc_io_cmd_read,
300     .endianness = DEVICE_NATIVE_ENDIAN,
301     .impl = {
302         .min_access_size = 1,
303         .max_access_size = 1,
304     },
305 };
306 
307 static const MemoryRegionOps applesmc_err_io_ops = {
308     .write = applesmc_io_err_write,
309     .read = applesmc_io_err_read,
310     .endianness = DEVICE_NATIVE_ENDIAN,
311     .impl = {
312         .min_access_size = 1,
313         .max_access_size = 1,
314     },
315 };
316 
317 static void applesmc_isa_realize(DeviceState *dev, Error **errp)
318 {
319     AppleSMCState *s = APPLE_SMC(dev);
320 
321     memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s,
322                           "applesmc-data", 1);
323     isa_register_ioport(&s->parent_obj, &s->io_data,
324                         s->iobase + APPLESMC_DATA_PORT);
325 
326     memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s,
327                           "applesmc-cmd", 1);
328     isa_register_ioport(&s->parent_obj, &s->io_cmd,
329                         s->iobase + APPLESMC_CMD_PORT);
330 
331     memory_region_init_io(&s->io_err, OBJECT(s), &applesmc_err_io_ops, s,
332                           "applesmc-err", 1);
333     isa_register_ioport(&s->parent_obj, &s->io_err,
334                         s->iobase + APPLESMC_ERR_PORT);
335 
336     if (!s->osk || (strlen(s->osk) != 64)) {
337         warn_report("Using AppleSMC with invalid key");
338         s->osk = default_osk;
339     }
340 
341     QLIST_INIT(&s->data_def);
342     qdev_applesmc_isa_reset(dev);
343 }
344 
345 static Property applesmc_isa_properties[] = {
346     DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE, AppleSMCState, iobase,
347                        APPLESMC_DEFAULT_IOBASE),
348     DEFINE_PROP_STRING("osk", AppleSMCState, osk),
349     DEFINE_PROP_END_OF_LIST(),
350 };
351 
352 static void qdev_applesmc_class_init(ObjectClass *klass, void *data)
353 {
354     DeviceClass *dc = DEVICE_CLASS(klass);
355 
356     dc->realize = applesmc_isa_realize;
357     dc->reset = qdev_applesmc_isa_reset;
358     device_class_set_props(dc, applesmc_isa_properties);
359     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
360 }
361 
362 static const TypeInfo applesmc_isa_info = {
363     .name          = TYPE_APPLE_SMC,
364     .parent        = TYPE_ISA_DEVICE,
365     .instance_size = sizeof(AppleSMCState),
366     .class_init    = qdev_applesmc_class_init,
367 };
368 
369 static void applesmc_register_types(void)
370 {
371     type_register_static(&applesmc_isa_info);
372 }
373 
374 type_init(applesmc_register_types)
375