apm.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  20. * GNU GPL, version 2 or (at your option) any later version.
  21. */
  22. #include "apm.h"
  23. #include "hw.h"
  24. //#define DEBUG
  25. #ifdef DEBUG
  26. # define APM_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  27. #else
  28. # define APM_DPRINTF(format, ...) do { } while (0)
  29. #endif
  30. /* fixed I/O location */
  31. #define APM_CNT_IOPORT 0xb2
  32. #define APM_STS_IOPORT 0xb3
  33. static void apm_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  34. {
  35. APMState *apm = opaque;
  36. addr &= 1;
  37. APM_DPRINTF("apm_ioport_writeb addr=0x%x val=0x%02x\n", addr, val);
  38. if (addr == 0) {
  39. apm->apmc = val;
  40. if (apm->callback) {
  41. (apm->callback)(val, apm->arg);
  42. }
  43. } else {
  44. apm->apms = val;
  45. }
  46. }
  47. static uint32_t apm_ioport_readb(void *opaque, uint32_t addr)
  48. {
  49. APMState *apm = opaque;
  50. uint32_t val;
  51. addr &= 1;
  52. if (addr == 0) {
  53. val = apm->apmc;
  54. } else {
  55. val = apm->apms;
  56. }
  57. APM_DPRINTF("apm_ioport_readb addr=0x%x val=0x%02x\n", addr, val);
  58. return val;
  59. }
  60. const VMStateDescription vmstate_apm = {
  61. .name = "APM State",
  62. .version_id = 1,
  63. .minimum_version_id = 1,
  64. .minimum_version_id_old = 1,
  65. .fields = (VMStateField[]) {
  66. VMSTATE_UINT8(apmc, APMState),
  67. VMSTATE_UINT8(apms, APMState),
  68. VMSTATE_END_OF_LIST()
  69. }
  70. };
  71. void apm_init(APMState *apm, apm_ctrl_changed_t callback, void *arg)
  72. {
  73. apm->callback = callback;
  74. apm->arg = arg;
  75. /* ioport 0xb2, 0xb3 */
  76. register_ioport_write(APM_CNT_IOPORT, 2, 1, apm_ioport_writeb, apm);
  77. register_ioport_read(APM_CNT_IOPORT, 2, 1, apm_ioport_readb, apm);
  78. }