apm.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * QEMU PC APM controller Emulation
  3. * This is split out from acpi.c
  4. *
  5. * Copyright (c) 2006 Fabrice Bellard
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License version 2 as published by the Free Software Foundation.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  18. */
  19. #include "apm.h"
  20. #include "hw.h"
  21. //#define DEBUG
  22. #ifdef DEBUG
  23. # define APM_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  24. #else
  25. # define APM_DPRINTF(format, ...) do { } while (0)
  26. #endif
  27. /* fixed I/O location */
  28. #define APM_CNT_IOPORT 0xb2
  29. #define APM_STS_IOPORT 0xb3
  30. static void apm_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  31. {
  32. APMState *apm = opaque;
  33. addr &= 1;
  34. APM_DPRINTF("apm_ioport_writeb addr=0x%x val=0x%02x\n", addr, val);
  35. if (addr == 0) {
  36. apm->apmc = val;
  37. if (apm->callback) {
  38. (apm->callback)(val, apm->arg);
  39. }
  40. } else {
  41. apm->apms = val;
  42. }
  43. }
  44. static uint32_t apm_ioport_readb(void *opaque, uint32_t addr)
  45. {
  46. APMState *apm = opaque;
  47. uint32_t val;
  48. addr &= 1;
  49. if (addr == 0) {
  50. val = apm->apmc;
  51. } else {
  52. val = apm->apms;
  53. }
  54. APM_DPRINTF("apm_ioport_readb addr=0x%x val=0x%02x\n", addr, val);
  55. return val;
  56. }
  57. const VMStateDescription vmstate_apm = {
  58. .name = "APM State",
  59. .version_id = 1,
  60. .minimum_version_id = 1,
  61. .minimum_version_id_old = 1,
  62. .fields = (VMStateField[]) {
  63. VMSTATE_UINT8(apmc, APMState),
  64. VMSTATE_UINT8(apms, APMState),
  65. VMSTATE_END_OF_LIST()
  66. }
  67. };
  68. void apm_init(APMState *apm, apm_ctrl_changed_t callback, void *arg)
  69. {
  70. apm->callback = callback;
  71. apm->arg = arg;
  72. /* ioport 0xb2, 0xb3 */
  73. register_ioport_write(APM_CNT_IOPORT, 2, 1, apm_ioport_writeb, apm);
  74. register_ioport_read(APM_CNT_IOPORT, 2, 1, apm_ioport_readb, apm);
  75. }