applesmc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.h"
  33. #include "isa.h"
  34. #include "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. struct AppleSMCStatus {
  62. ISADevice dev;
  63. uint32_t iobase;
  64. uint8_t cmd;
  65. uint8_t status;
  66. uint8_t key[4];
  67. uint8_t read_pos;
  68. uint8_t data_len;
  69. uint8_t data_pos;
  70. uint8_t data[255];
  71. uint8_t charactic[4];
  72. char *osk;
  73. QLIST_HEAD(, AppleSMCData) data_def;
  74. };
  75. static void applesmc_io_cmd_writeb(void *opaque, uint32_t addr, uint32_t val)
  76. {
  77. struct AppleSMCStatus *s = opaque;
  78. smc_debug("CMD Write B: %#x = %#x\n", addr, val);
  79. switch(val) {
  80. case APPLESMC_READ_CMD:
  81. s->status = 0x0c;
  82. break;
  83. }
  84. s->cmd = val;
  85. s->read_pos = 0;
  86. s->data_pos = 0;
  87. }
  88. static void applesmc_fill_data(struct AppleSMCStatus *s)
  89. {
  90. struct AppleSMCData *d;
  91. QLIST_FOREACH(d, &s->data_def, node) {
  92. if (!memcmp(d->key, s->key, 4)) {
  93. smc_debug("Key matched (%s Len=%d Data=%s)\n", d->key,
  94. d->len, d->data);
  95. memcpy(s->data, d->data, d->len);
  96. return;
  97. }
  98. }
  99. }
  100. static void applesmc_io_data_writeb(void *opaque, uint32_t addr, uint32_t val)
  101. {
  102. struct AppleSMCStatus *s = opaque;
  103. smc_debug("DATA Write B: %#x = %#x\n", addr, val);
  104. switch(s->cmd) {
  105. case APPLESMC_READ_CMD:
  106. if(s->read_pos < 4) {
  107. s->key[s->read_pos] = val;
  108. s->status = 0x04;
  109. } else if(s->read_pos == 4) {
  110. s->data_len = val;
  111. s->status = 0x05;
  112. s->data_pos = 0;
  113. smc_debug("Key = %c%c%c%c Len = %d\n", s->key[0],
  114. s->key[1], s->key[2], s->key[3], val);
  115. applesmc_fill_data(s);
  116. }
  117. s->read_pos++;
  118. break;
  119. }
  120. }
  121. static uint32_t applesmc_io_data_readb(void *opaque, uint32_t addr1)
  122. {
  123. struct AppleSMCStatus *s = opaque;
  124. uint8_t retval = 0;
  125. switch(s->cmd) {
  126. case APPLESMC_READ_CMD:
  127. if(s->data_pos < s->data_len) {
  128. retval = s->data[s->data_pos];
  129. smc_debug("READ_DATA[%d] = %#hhx\n", s->data_pos,
  130. retval);
  131. s->data_pos++;
  132. if(s->data_pos == s->data_len) {
  133. s->status = 0x00;
  134. smc_debug("EOF\n");
  135. } else
  136. s->status = 0x05;
  137. }
  138. }
  139. smc_debug("DATA Read b: %#x = %#x\n", addr1, retval);
  140. return retval;
  141. }
  142. static uint32_t applesmc_io_cmd_readb(void *opaque, uint32_t addr1)
  143. {
  144. struct AppleSMCStatus *s = opaque;
  145. smc_debug("CMD Read B: %#x\n", addr1);
  146. return s->status;
  147. }
  148. static void applesmc_add_key(struct AppleSMCStatus *s, const char *key,
  149. int len, const char *data)
  150. {
  151. struct AppleSMCData *def;
  152. def = g_malloc0(sizeof(struct AppleSMCData));
  153. def->key = key;
  154. def->len = len;
  155. def->data = data;
  156. QLIST_INSERT_HEAD(&s->data_def, def, node);
  157. }
  158. static void qdev_applesmc_isa_reset(DeviceState *dev)
  159. {
  160. struct AppleSMCStatus *s = DO_UPCAST(struct AppleSMCStatus, dev.qdev, dev);
  161. struct AppleSMCData *d, *next;
  162. /* Remove existing entries */
  163. QLIST_FOREACH_SAFE(d, &s->data_def, node, next) {
  164. QLIST_REMOVE(d, node);
  165. }
  166. applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
  167. applesmc_add_key(s, "OSK0", 32, s->osk);
  168. applesmc_add_key(s, "OSK1", 32, s->osk + 32);
  169. applesmc_add_key(s, "NATJ", 1, "\0");
  170. applesmc_add_key(s, "MSSP", 1, "\0");
  171. applesmc_add_key(s, "MSSD", 1, "\0x3");
  172. }
  173. static int applesmc_isa_init(ISADevice *dev)
  174. {
  175. struct AppleSMCStatus *s = DO_UPCAST(struct AppleSMCStatus, dev, dev);
  176. register_ioport_read(s->iobase + APPLESMC_DATA_PORT, 4, 1,
  177. applesmc_io_data_readb, s);
  178. register_ioport_read(s->iobase + APPLESMC_CMD_PORT, 4, 1,
  179. applesmc_io_cmd_readb, s);
  180. register_ioport_write(s->iobase + APPLESMC_DATA_PORT, 4, 1,
  181. applesmc_io_data_writeb, s);
  182. register_ioport_write(s->iobase + APPLESMC_CMD_PORT, 4, 1,
  183. applesmc_io_cmd_writeb, s);
  184. if (!s->osk || (strlen(s->osk) != 64)) {
  185. fprintf(stderr, "WARNING: Using AppleSMC with invalid key\n");
  186. s->osk = default_osk;
  187. }
  188. QLIST_INIT(&s->data_def);
  189. qdev_applesmc_isa_reset(&dev->qdev);
  190. return 0;
  191. }
  192. static Property applesmc_isa_properties[] = {
  193. DEFINE_PROP_HEX32("iobase", struct AppleSMCStatus, iobase,
  194. APPLESMC_DEFAULT_IOBASE),
  195. DEFINE_PROP_STRING("osk", struct AppleSMCStatus, osk),
  196. DEFINE_PROP_END_OF_LIST(),
  197. };
  198. static void qdev_applesmc_class_init(ObjectClass *klass, void *data)
  199. {
  200. DeviceClass *dc = DEVICE_CLASS(klass);
  201. ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
  202. ic->init = applesmc_isa_init;
  203. dc->reset = qdev_applesmc_isa_reset;
  204. dc->props = applesmc_isa_properties;
  205. }
  206. static TypeInfo applesmc_isa_info = {
  207. .name = "isa-applesmc",
  208. .parent = TYPE_ISA_DEVICE,
  209. .instance_size = sizeof(struct AppleSMCStatus),
  210. .class_init = qdev_applesmc_class_init,
  211. };
  212. static void applesmc_register_types(void)
  213. {
  214. type_register_static(&applesmc_isa_info);
  215. }
  216. type_init(applesmc_register_types)