]> git.proxmox.com Git - mirror_qemu.git/blob - hw/misc/applesmc.c
Move QOM typedefs and add missing includes
[mirror_qemu.git] / hw / misc / applesmc.c
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 #define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC)
95
96 struct AppleSMCState {
97 ISADevice parent_obj;
98
99 MemoryRegion io_data;
100 MemoryRegion io_cmd;
101 MemoryRegion io_err;
102 uint32_t iobase;
103 uint8_t cmd;
104 uint8_t status;
105 uint8_t status_1e;
106 uint8_t last_ret;
107 char key[4];
108 uint8_t read_pos;
109 uint8_t data_len;
110 uint8_t data_pos;
111 uint8_t data[255];
112 char *osk;
113 QLIST_HEAD(, AppleSMCData) data_def;
114 };
115
116 static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val,
117 unsigned size)
118 {
119 AppleSMCState *s = opaque;
120 uint8_t status = s->status & 0x0f;
121
122 smc_debug("CMD received: 0x%02x\n", (uint8_t)val);
123 switch (val) {
124 case APPLESMC_READ_CMD:
125 /* did last command run through OK? */
126 if (status == APPLESMC_ST_CMD_DONE || status == APPLESMC_ST_NEW_CMD) {
127 s->cmd = val;
128 s->status = APPLESMC_ST_NEW_CMD | APPLESMC_ST_ACK;
129 } else {
130 smc_debug("ERROR: previous command interrupted!\n");
131 s->status = APPLESMC_ST_NEW_CMD;
132 s->status_1e = APPLESMC_ST_1E_CMD_INTRUPTED;
133 }
134 break;
135 default:
136 smc_debug("UNEXPECTED CMD 0x%02x\n", (uint8_t)val);
137 s->status = APPLESMC_ST_NEW_CMD;
138 s->status_1e = APPLESMC_ST_1E_BAD_CMD;
139 }
140 s->read_pos = 0;
141 s->data_pos = 0;
142 }
143
144 static struct AppleSMCData *applesmc_find_key(AppleSMCState *s)
145 {
146 struct AppleSMCData *d;
147
148 QLIST_FOREACH(d, &s->data_def, node) {
149 if (!memcmp(d->key, s->key, 4)) {
150 return d;
151 }
152 }
153 return NULL;
154 }
155
156 static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val,
157 unsigned size)
158 {
159 AppleSMCState *s = opaque;
160 struct AppleSMCData *d;
161
162 smc_debug("DATA received: 0x%02x\n", (uint8_t)val);
163 switch (s->cmd) {
164 case APPLESMC_READ_CMD:
165 if ((s->status & 0x0f) == APPLESMC_ST_CMD_DONE) {
166 break;
167 }
168 if (s->read_pos < 4) {
169 s->key[s->read_pos] = val;
170 s->status = APPLESMC_ST_ACK;
171 } else if (s->read_pos == 4) {
172 d = applesmc_find_key(s);
173 if (d != NULL) {
174 memcpy(s->data, d->data, d->len);
175 s->data_len = d->len;
176 s->data_pos = 0;
177 s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
178 s->status_1e = APPLESMC_ST_CMD_DONE; /* clear on valid key */
179 } else {
180 smc_debug("READ_CMD: key '%c%c%c%c' not found!\n",
181 s->key[0], s->key[1], s->key[2], s->key[3]);
182 s->status = APPLESMC_ST_CMD_DONE;
183 s->status_1e = APPLESMC_ST_1E_NOEXIST;
184 }
185 }
186 s->read_pos++;
187 break;
188 default:
189 s->status = APPLESMC_ST_CMD_DONE;
190 s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
191 }
192 }
193
194 static void applesmc_io_err_write(void *opaque, hwaddr addr, uint64_t val,
195 unsigned size)
196 {
197 smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val);
198 /* NOTE: writing to the error port not supported! */
199 }
200
201 static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr, unsigned size)
202 {
203 AppleSMCState *s = opaque;
204
205 switch (s->cmd) {
206 case APPLESMC_READ_CMD:
207 if (!(s->status & APPLESMC_ST_DATA_READY)) {
208 break;
209 }
210 if (s->data_pos < s->data_len) {
211 s->last_ret = s->data[s->data_pos];
212 smc_debug("READ '%c%c%c%c'[%d] = %02x\n",
213 s->key[0], s->key[1], s->key[2], s->key[3],
214 s->data_pos, s->last_ret);
215 s->data_pos++;
216 if (s->data_pos == s->data_len) {
217 s->status = APPLESMC_ST_CMD_DONE;
218 smc_debug("READ '%c%c%c%c' Len=%d complete!\n",
219 s->key[0], s->key[1], s->key[2], s->key[3],
220 s->data_len);
221 } else {
222 s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
223 }
224 }
225 break;
226 default:
227 s->status = APPLESMC_ST_CMD_DONE;
228 s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
229 }
230 smc_debug("DATA sent: 0x%02x\n", s->last_ret);
231
232 return s->last_ret;
233 }
234
235 static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr, unsigned size)
236 {
237 AppleSMCState *s = opaque;
238
239 smc_debug("CMD sent: 0x%02x\n", s->status);
240 return s->status;
241 }
242
243 static uint64_t applesmc_io_err_read(void *opaque, hwaddr addr, unsigned size)
244 {
245 AppleSMCState *s = opaque;
246
247 /* NOTE: read does not clear the 1e status */
248 smc_debug("ERR_CODE sent: 0x%02x\n", s->status_1e);
249 return s->status_1e;
250 }
251
252 static void applesmc_add_key(AppleSMCState *s, const char *key,
253 int len, const char *data)
254 {
255 struct AppleSMCData *def;
256
257 def = g_malloc0(sizeof(struct AppleSMCData));
258 def->key = key;
259 def->len = len;
260 def->data = data;
261
262 QLIST_INSERT_HEAD(&s->data_def, def, node);
263 }
264
265 static void qdev_applesmc_isa_reset(DeviceState *dev)
266 {
267 AppleSMCState *s = APPLE_SMC(dev);
268 struct AppleSMCData *d, *next;
269
270 /* Remove existing entries */
271 QLIST_FOREACH_SAFE(d, &s->data_def, node, next) {
272 QLIST_REMOVE(d, node);
273 }
274 s->status = 0x00;
275 s->status_1e = 0x00;
276 s->last_ret = 0x00;
277
278 applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
279 applesmc_add_key(s, "OSK0", 32, s->osk);
280 applesmc_add_key(s, "OSK1", 32, s->osk + 32);
281 applesmc_add_key(s, "NATJ", 1, "\0");
282 applesmc_add_key(s, "MSSP", 1, "\0");
283 applesmc_add_key(s, "MSSD", 1, "\0x3");
284 }
285
286 static const MemoryRegionOps applesmc_data_io_ops = {
287 .write = applesmc_io_data_write,
288 .read = applesmc_io_data_read,
289 .endianness = DEVICE_NATIVE_ENDIAN,
290 .impl = {
291 .min_access_size = 1,
292 .max_access_size = 1,
293 },
294 };
295
296 static const MemoryRegionOps applesmc_cmd_io_ops = {
297 .write = applesmc_io_cmd_write,
298 .read = applesmc_io_cmd_read,
299 .endianness = DEVICE_NATIVE_ENDIAN,
300 .impl = {
301 .min_access_size = 1,
302 .max_access_size = 1,
303 },
304 };
305
306 static const MemoryRegionOps applesmc_err_io_ops = {
307 .write = applesmc_io_err_write,
308 .read = applesmc_io_err_read,
309 .endianness = DEVICE_NATIVE_ENDIAN,
310 .impl = {
311 .min_access_size = 1,
312 .max_access_size = 1,
313 },
314 };
315
316 static void applesmc_isa_realize(DeviceState *dev, Error **errp)
317 {
318 AppleSMCState *s = APPLE_SMC(dev);
319
320 memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s,
321 "applesmc-data", 1);
322 isa_register_ioport(&s->parent_obj, &s->io_data,
323 s->iobase + APPLESMC_DATA_PORT);
324
325 memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s,
326 "applesmc-cmd", 1);
327 isa_register_ioport(&s->parent_obj, &s->io_cmd,
328 s->iobase + APPLESMC_CMD_PORT);
329
330 memory_region_init_io(&s->io_err, OBJECT(s), &applesmc_err_io_ops, s,
331 "applesmc-err", 1);
332 isa_register_ioport(&s->parent_obj, &s->io_err,
333 s->iobase + APPLESMC_ERR_PORT);
334
335 if (!s->osk || (strlen(s->osk) != 64)) {
336 warn_report("Using AppleSMC with invalid key");
337 s->osk = default_osk;
338 }
339
340 QLIST_INIT(&s->data_def);
341 qdev_applesmc_isa_reset(dev);
342 }
343
344 static Property applesmc_isa_properties[] = {
345 DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE, AppleSMCState, iobase,
346 APPLESMC_DEFAULT_IOBASE),
347 DEFINE_PROP_STRING("osk", AppleSMCState, osk),
348 DEFINE_PROP_END_OF_LIST(),
349 };
350
351 static void qdev_applesmc_class_init(ObjectClass *klass, void *data)
352 {
353 DeviceClass *dc = DEVICE_CLASS(klass);
354
355 dc->realize = applesmc_isa_realize;
356 dc->reset = qdev_applesmc_isa_reset;
357 device_class_set_props(dc, applesmc_isa_properties);
358 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
359 }
360
361 static const TypeInfo applesmc_isa_info = {
362 .name = TYPE_APPLE_SMC,
363 .parent = TYPE_ISA_DEVICE,
364 .instance_size = sizeof(AppleSMCState),
365 .class_init = qdev_applesmc_class_init,
366 };
367
368 static void applesmc_register_types(void)
369 {
370 type_register_static(&applesmc_isa_info);
371 }
372
373 type_init(applesmc_register_types)