applesmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include "qemu/osdep.h"
  33. #include "hw/isa/isa.h"
  34. #include "hw/qdev-properties.h"
  35. #include "ui/console.h"
  36. #include "qemu/module.h"
  37. #include "qemu/timer.h"
  38. /* #define DEBUG_SMC */
  39. #define APPLESMC_DEFAULT_IOBASE 0x300
  40. enum {
  41. APPLESMC_DATA_PORT = 0x00,
  42. APPLESMC_CMD_PORT = 0x04,
  43. APPLESMC_ERR_PORT = 0x1e,
  44. APPLESMC_NUM_PORTS = 0x20,
  45. };
  46. enum {
  47. APPLESMC_READ_CMD = 0x10,
  48. APPLESMC_WRITE_CMD = 0x11,
  49. APPLESMC_GET_KEY_BY_INDEX_CMD = 0x12,
  50. APPLESMC_GET_KEY_TYPE_CMD = 0x13,
  51. };
  52. enum {
  53. APPLESMC_ST_CMD_DONE = 0x00,
  54. APPLESMC_ST_DATA_READY = 0x01,
  55. APPLESMC_ST_BUSY = 0x02,
  56. APPLESMC_ST_ACK = 0x04,
  57. APPLESMC_ST_NEW_CMD = 0x08,
  58. };
  59. enum {
  60. APPLESMC_ST_1E_CMD_INTRUPTED = 0x80,
  61. APPLESMC_ST_1E_STILL_BAD_CMD = 0x81,
  62. APPLESMC_ST_1E_BAD_CMD = 0x82,
  63. APPLESMC_ST_1E_NOEXIST = 0x84,
  64. APPLESMC_ST_1E_WRITEONLY = 0x85,
  65. APPLESMC_ST_1E_READONLY = 0x86,
  66. APPLESMC_ST_1E_BAD_INDEX = 0xb8,
  67. };
  68. #ifdef DEBUG_SMC
  69. #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__)
  70. #else
  71. #define smc_debug(...) do { } while (0)
  72. #endif
  73. static char default_osk[64] = "This is a dummy key. Enter the real key "
  74. "using the -osk parameter";
  75. struct AppleSMCData {
  76. uint8_t len;
  77. const char *key;
  78. const char *data;
  79. QLIST_ENTRY(AppleSMCData) node;
  80. };
  81. #define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC)
  82. typedef struct AppleSMCState AppleSMCState;
  83. struct AppleSMCState {
  84. ISADevice parent_obj;
  85. MemoryRegion io_data;
  86. MemoryRegion io_cmd;
  87. MemoryRegion io_err;
  88. uint32_t iobase;
  89. uint8_t cmd;
  90. uint8_t status;
  91. uint8_t status_1e;
  92. uint8_t last_ret;
  93. char key[4];
  94. uint8_t read_pos;
  95. uint8_t data_len;
  96. uint8_t data_pos;
  97. uint8_t data[255];
  98. char *osk;
  99. QLIST_HEAD(, AppleSMCData) data_def;
  100. };
  101. static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val,
  102. unsigned size)
  103. {
  104. AppleSMCState *s = opaque;
  105. uint8_t status = s->status & 0x0f;
  106. smc_debug("CMD received: 0x%02x\n", (uint8_t)val);
  107. switch (val) {
  108. case APPLESMC_READ_CMD:
  109. /* did last command run through OK? */
  110. if (status == APPLESMC_ST_CMD_DONE || status == APPLESMC_ST_NEW_CMD) {
  111. s->cmd = val;
  112. s->status = APPLESMC_ST_NEW_CMD | APPLESMC_ST_ACK;
  113. } else {
  114. smc_debug("ERROR: previous command interrupted!\n");
  115. s->status = APPLESMC_ST_NEW_CMD;
  116. s->status_1e = APPLESMC_ST_1E_CMD_INTRUPTED;
  117. }
  118. break;
  119. default:
  120. smc_debug("UNEXPECTED CMD 0x%02x\n", (uint8_t)val);
  121. s->status = APPLESMC_ST_NEW_CMD;
  122. s->status_1e = APPLESMC_ST_1E_BAD_CMD;
  123. }
  124. s->read_pos = 0;
  125. s->data_pos = 0;
  126. }
  127. static struct AppleSMCData *applesmc_find_key(AppleSMCState *s)
  128. {
  129. struct AppleSMCData *d;
  130. QLIST_FOREACH(d, &s->data_def, node) {
  131. if (!memcmp(d->key, s->key, 4)) {
  132. return d;
  133. }
  134. }
  135. return NULL;
  136. }
  137. static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val,
  138. unsigned size)
  139. {
  140. AppleSMCState *s = opaque;
  141. struct AppleSMCData *d;
  142. smc_debug("DATA received: 0x%02x\n", (uint8_t)val);
  143. switch (s->cmd) {
  144. case APPLESMC_READ_CMD:
  145. if ((s->status & 0x0f) == APPLESMC_ST_CMD_DONE) {
  146. break;
  147. }
  148. if (s->read_pos < 4) {
  149. s->key[s->read_pos] = val;
  150. s->status = APPLESMC_ST_ACK;
  151. } else if (s->read_pos == 4) {
  152. d = applesmc_find_key(s);
  153. if (d != NULL) {
  154. memcpy(s->data, d->data, d->len);
  155. s->data_len = d->len;
  156. s->data_pos = 0;
  157. s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
  158. s->status_1e = APPLESMC_ST_CMD_DONE; /* clear on valid key */
  159. } else {
  160. smc_debug("READ_CMD: key '%c%c%c%c' not found!\n",
  161. s->key[0], s->key[1], s->key[2], s->key[3]);
  162. s->status = APPLESMC_ST_CMD_DONE;
  163. s->status_1e = APPLESMC_ST_1E_NOEXIST;
  164. }
  165. }
  166. s->read_pos++;
  167. break;
  168. default:
  169. s->status = APPLESMC_ST_CMD_DONE;
  170. s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
  171. }
  172. }
  173. static void applesmc_io_err_write(void *opaque, hwaddr addr, uint64_t val,
  174. unsigned size)
  175. {
  176. smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val);
  177. /* NOTE: writing to the error port not supported! */
  178. }
  179. static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr, unsigned size)
  180. {
  181. AppleSMCState *s = opaque;
  182. switch (s->cmd) {
  183. case APPLESMC_READ_CMD:
  184. if (!(s->status & APPLESMC_ST_DATA_READY)) {
  185. break;
  186. }
  187. if (s->data_pos < s->data_len) {
  188. s->last_ret = s->data[s->data_pos];
  189. smc_debug("READ '%c%c%c%c'[%d] = %02x\n",
  190. s->key[0], s->key[1], s->key[2], s->key[3],
  191. s->data_pos, s->last_ret);
  192. s->data_pos++;
  193. if (s->data_pos == s->data_len) {
  194. s->status = APPLESMC_ST_CMD_DONE;
  195. smc_debug("READ '%c%c%c%c' Len=%d complete!\n",
  196. s->key[0], s->key[1], s->key[2], s->key[3],
  197. s->data_len);
  198. } else {
  199. s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
  200. }
  201. }
  202. break;
  203. default:
  204. s->status = APPLESMC_ST_CMD_DONE;
  205. s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
  206. }
  207. smc_debug("DATA sent: 0x%02x\n", s->last_ret);
  208. return s->last_ret;
  209. }
  210. static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr, unsigned size)
  211. {
  212. AppleSMCState *s = opaque;
  213. smc_debug("CMD sent: 0x%02x\n", s->status);
  214. return s->status;
  215. }
  216. static uint64_t applesmc_io_err_read(void *opaque, hwaddr addr, unsigned size)
  217. {
  218. AppleSMCState *s = opaque;
  219. /* NOTE: read does not clear the 1e status */
  220. smc_debug("ERR_CODE sent: 0x%02x\n", s->status_1e);
  221. return s->status_1e;
  222. }
  223. static void applesmc_add_key(AppleSMCState *s, const char *key,
  224. int len, const char *data)
  225. {
  226. struct AppleSMCData *def;
  227. def = g_malloc0(sizeof(struct AppleSMCData));
  228. def->key = key;
  229. def->len = len;
  230. def->data = data;
  231. QLIST_INSERT_HEAD(&s->data_def, def, node);
  232. }
  233. static void qdev_applesmc_isa_reset(DeviceState *dev)
  234. {
  235. AppleSMCState *s = APPLE_SMC(dev);
  236. struct AppleSMCData *d, *next;
  237. /* Remove existing entries */
  238. QLIST_FOREACH_SAFE(d, &s->data_def, node, next) {
  239. QLIST_REMOVE(d, node);
  240. }
  241. s->status = 0x00;
  242. s->status_1e = 0x00;
  243. s->last_ret = 0x00;
  244. applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
  245. applesmc_add_key(s, "OSK0", 32, s->osk);
  246. applesmc_add_key(s, "OSK1", 32, s->osk + 32);
  247. applesmc_add_key(s, "NATJ", 1, "\0");
  248. applesmc_add_key(s, "MSSP", 1, "\0");
  249. applesmc_add_key(s, "MSSD", 1, "\0x3");
  250. }
  251. static const MemoryRegionOps applesmc_data_io_ops = {
  252. .write = applesmc_io_data_write,
  253. .read = applesmc_io_data_read,
  254. .endianness = DEVICE_NATIVE_ENDIAN,
  255. .impl = {
  256. .min_access_size = 1,
  257. .max_access_size = 1,
  258. },
  259. };
  260. static const MemoryRegionOps applesmc_cmd_io_ops = {
  261. .write = applesmc_io_cmd_write,
  262. .read = applesmc_io_cmd_read,
  263. .endianness = DEVICE_NATIVE_ENDIAN,
  264. .impl = {
  265. .min_access_size = 1,
  266. .max_access_size = 1,
  267. },
  268. };
  269. static const MemoryRegionOps applesmc_err_io_ops = {
  270. .write = applesmc_io_err_write,
  271. .read = applesmc_io_err_read,
  272. .endianness = DEVICE_NATIVE_ENDIAN,
  273. .impl = {
  274. .min_access_size = 1,
  275. .max_access_size = 1,
  276. },
  277. };
  278. static void applesmc_isa_realize(DeviceState *dev, Error **errp)
  279. {
  280. AppleSMCState *s = APPLE_SMC(dev);
  281. memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s,
  282. "applesmc-data", 1);
  283. isa_register_ioport(&s->parent_obj, &s->io_data,
  284. s->iobase + APPLESMC_DATA_PORT);
  285. memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s,
  286. "applesmc-cmd", 1);
  287. isa_register_ioport(&s->parent_obj, &s->io_cmd,
  288. s->iobase + APPLESMC_CMD_PORT);
  289. memory_region_init_io(&s->io_err, OBJECT(s), &applesmc_err_io_ops, s,
  290. "applesmc-err", 1);
  291. isa_register_ioport(&s->parent_obj, &s->io_err,
  292. s->iobase + APPLESMC_ERR_PORT);
  293. if (!s->osk || (strlen(s->osk) != 64)) {
  294. warn_report("Using AppleSMC with invalid key");
  295. s->osk = default_osk;
  296. }
  297. QLIST_INIT(&s->data_def);
  298. qdev_applesmc_isa_reset(dev);
  299. }
  300. static Property applesmc_isa_properties[] = {
  301. DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE, AppleSMCState, iobase,
  302. APPLESMC_DEFAULT_IOBASE),
  303. DEFINE_PROP_STRING("osk", AppleSMCState, osk),
  304. DEFINE_PROP_END_OF_LIST(),
  305. };
  306. static void qdev_applesmc_class_init(ObjectClass *klass, void *data)
  307. {
  308. DeviceClass *dc = DEVICE_CLASS(klass);
  309. dc->realize = applesmc_isa_realize;
  310. dc->reset = qdev_applesmc_isa_reset;
  311. dc->props = applesmc_isa_properties;
  312. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  313. }
  314. static const TypeInfo applesmc_isa_info = {
  315. .name = TYPE_APPLE_SMC,
  316. .parent = TYPE_ISA_DEVICE,
  317. .instance_size = sizeof(AppleSMCState),
  318. .class_init = qdev_applesmc_class_init,
  319. };
  320. static void applesmc_register_types(void)
  321. {
  322. type_register_static(&applesmc_isa_info);
  323. }
  324. type_init(applesmc_register_types)