applesmc.c 7.9 KB

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