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 "hw/hw.h" 34 #include "hw/isa/isa.h" 35 #include "ui/console.h" 36 #include "qemu/timer.h" 37 38 /* #define DEBUG_SMC */ 39 40 #define APPLESMC_DEFAULT_IOBASE 0x300 41 /* data port used by Apple SMC */ 42 #define APPLESMC_DATA_PORT 0x0 43 /* command/status port used by Apple SMC */ 44 #define APPLESMC_CMD_PORT 0x4 45 #define APPLESMC_NR_PORTS 32 46 #define APPLESMC_MAX_DATA_LENGTH 32 47 48 #define APPLESMC_READ_CMD 0x10 49 #define APPLESMC_WRITE_CMD 0x11 50 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12 51 #define APPLESMC_GET_KEY_TYPE_CMD 0x13 52 53 #ifdef DEBUG_SMC 54 #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__) 55 #else 56 #define smc_debug(...) do { } while(0) 57 #endif 58 59 static char default_osk[64] = "This is a dummy key. Enter the real key " 60 "using the -osk parameter"; 61 62 struct AppleSMCData { 63 uint8_t len; 64 const char *key; 65 const char *data; 66 QLIST_ENTRY(AppleSMCData) node; 67 }; 68 69 #define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC) 70 71 typedef struct AppleSMCState AppleSMCState; 72 struct AppleSMCState { 73 ISADevice parent_obj; 74 75 MemoryRegion io_data; 76 MemoryRegion io_cmd; 77 uint32_t iobase; 78 uint8_t cmd; 79 uint8_t status; 80 uint8_t key[4]; 81 uint8_t read_pos; 82 uint8_t data_len; 83 uint8_t data_pos; 84 uint8_t data[255]; 85 uint8_t charactic[4]; 86 char *osk; 87 QLIST_HEAD(, AppleSMCData) data_def; 88 }; 89 90 static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val, 91 unsigned size) 92 { 93 AppleSMCState *s = opaque; 94 95 smc_debug("CMD Write B: %#x = %#x\n", addr, val); 96 switch(val) { 97 case APPLESMC_READ_CMD: 98 s->status = 0x0c; 99 break; 100 } 101 s->cmd = val; 102 s->read_pos = 0; 103 s->data_pos = 0; 104 } 105 106 static void applesmc_fill_data(AppleSMCState *s) 107 { 108 struct AppleSMCData *d; 109 110 QLIST_FOREACH(d, &s->data_def, node) { 111 if (!memcmp(d->key, s->key, 4)) { 112 smc_debug("Key matched (%s Len=%d Data=%s)\n", d->key, 113 d->len, d->data); 114 memcpy(s->data, d->data, d->len); 115 return; 116 } 117 } 118 } 119 120 static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val, 121 unsigned size) 122 { 123 AppleSMCState *s = opaque; 124 125 smc_debug("DATA Write B: %#x = %#x\n", addr, val); 126 switch(s->cmd) { 127 case APPLESMC_READ_CMD: 128 if(s->read_pos < 4) { 129 s->key[s->read_pos] = val; 130 s->status = 0x04; 131 } else if(s->read_pos == 4) { 132 s->data_len = val; 133 s->status = 0x05; 134 s->data_pos = 0; 135 smc_debug("Key = %c%c%c%c Len = %d\n", s->key[0], 136 s->key[1], s->key[2], s->key[3], val); 137 applesmc_fill_data(s); 138 } 139 s->read_pos++; 140 break; 141 } 142 } 143 144 static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr1, 145 unsigned size) 146 { 147 AppleSMCState *s = opaque; 148 uint8_t retval = 0; 149 150 switch(s->cmd) { 151 case APPLESMC_READ_CMD: 152 if(s->data_pos < s->data_len) { 153 retval = s->data[s->data_pos]; 154 smc_debug("READ_DATA[%d] = %#hhx\n", s->data_pos, 155 retval); 156 s->data_pos++; 157 if(s->data_pos == s->data_len) { 158 s->status = 0x00; 159 smc_debug("EOF\n"); 160 } else 161 s->status = 0x05; 162 } 163 } 164 smc_debug("DATA Read b: %#x = %#x\n", addr1, retval); 165 166 return retval; 167 } 168 169 static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr1, unsigned size) 170 { 171 AppleSMCState *s = opaque; 172 173 smc_debug("CMD Read B: %#x\n", addr1); 174 return s->status; 175 } 176 177 static void applesmc_add_key(AppleSMCState *s, const char *key, 178 int len, const char *data) 179 { 180 struct AppleSMCData *def; 181 182 def = g_malloc0(sizeof(struct AppleSMCData)); 183 def->key = key; 184 def->len = len; 185 def->data = data; 186 187 QLIST_INSERT_HEAD(&s->data_def, def, node); 188 } 189 190 static void qdev_applesmc_isa_reset(DeviceState *dev) 191 { 192 AppleSMCState *s = APPLE_SMC(dev); 193 struct AppleSMCData *d, *next; 194 195 /* Remove existing entries */ 196 QLIST_FOREACH_SAFE(d, &s->data_def, node, next) { 197 QLIST_REMOVE(d, node); 198 } 199 200 applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03"); 201 applesmc_add_key(s, "OSK0", 32, s->osk); 202 applesmc_add_key(s, "OSK1", 32, s->osk + 32); 203 applesmc_add_key(s, "NATJ", 1, "\0"); 204 applesmc_add_key(s, "MSSP", 1, "\0"); 205 applesmc_add_key(s, "MSSD", 1, "\0x3"); 206 } 207 208 static const MemoryRegionOps applesmc_data_io_ops = { 209 .write = applesmc_io_data_write, 210 .read = applesmc_io_data_read, 211 .endianness = DEVICE_NATIVE_ENDIAN, 212 .impl = { 213 .min_access_size = 1, 214 .max_access_size = 1, 215 }, 216 }; 217 218 static const MemoryRegionOps applesmc_cmd_io_ops = { 219 .write = applesmc_io_cmd_write, 220 .read = applesmc_io_cmd_read, 221 .endianness = DEVICE_NATIVE_ENDIAN, 222 .impl = { 223 .min_access_size = 1, 224 .max_access_size = 1, 225 }, 226 }; 227 228 static void applesmc_isa_realize(DeviceState *dev, Error **errp) 229 { 230 AppleSMCState *s = APPLE_SMC(dev); 231 232 memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s, 233 "applesmc-data", 4); 234 isa_register_ioport(&s->parent_obj, &s->io_data, 235 s->iobase + APPLESMC_DATA_PORT); 236 237 memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s, 238 "applesmc-cmd", 4); 239 isa_register_ioport(&s->parent_obj, &s->io_cmd, 240 s->iobase + APPLESMC_CMD_PORT); 241 242 if (!s->osk || (strlen(s->osk) != 64)) { 243 fprintf(stderr, "WARNING: Using AppleSMC with invalid key\n"); 244 s->osk = default_osk; 245 } 246 247 QLIST_INIT(&s->data_def); 248 qdev_applesmc_isa_reset(dev); 249 } 250 251 static Property applesmc_isa_properties[] = { 252 DEFINE_PROP_HEX32("iobase", AppleSMCState, iobase, 253 APPLESMC_DEFAULT_IOBASE), 254 DEFINE_PROP_STRING("osk", AppleSMCState, osk), 255 DEFINE_PROP_END_OF_LIST(), 256 }; 257 258 static void qdev_applesmc_class_init(ObjectClass *klass, void *data) 259 { 260 DeviceClass *dc = DEVICE_CLASS(klass); 261 262 dc->realize = applesmc_isa_realize; 263 dc->reset = qdev_applesmc_isa_reset; 264 dc->props = applesmc_isa_properties; 265 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 266 } 267 268 static const TypeInfo applesmc_isa_info = { 269 .name = TYPE_APPLE_SMC, 270 .parent = TYPE_ISA_DEVICE, 271 .instance_size = sizeof(AppleSMCState), 272 .class_init = qdev_applesmc_class_init, 273 }; 274 275 static void applesmc_register_types(void) 276 { 277 type_register_static(&applesmc_isa_info); 278 } 279 280 type_init(applesmc_register_types) 281