TestACPISleep.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <acpi.h>
  2. #include <accommon.h>
  3. #include <acnamesp.h>
  4. void
  5. fail(char *msg)
  6. {
  7. fprint(2, "%s\n", msg);
  8. exits("FAIL");
  9. }
  10. UINT32
  11. shutdownhandler(void *ctxt)
  12. {
  13. ACPI_STATUS status;
  14. syslog(1, "acpi", "acpi event: power button");
  15. status = AcpiEnterSleepStatePrep(5);
  16. if (ACPI_FAILURE(status))
  17. fail("failed to prep for sleep");
  18. status = AcpiEnterSleepState(5);
  19. if (ACPI_FAILURE(status))
  20. fail("failed to go to sleep");
  21. return 0;
  22. }
  23. UINT32
  24. timerhandler(void *ctxt)
  25. {
  26. syslog(1, "acpi", "acpi event: pm timer");
  27. return 0;
  28. }
  29. unsigned int
  30. AcpiIntrWait(int afd, unsigned int *info);
  31. ACPI_STATUS AcpiRunInterrupt(void);
  32. /* The order of initialization comes from the ACPICA Programmer's Reference */
  33. void main(int argc, char *argv[])
  34. {
  35. ACPI_STATUS status;
  36. extern UINT32 AcpiDbgLevel;
  37. unsigned int info;
  38. int fd = open("/dev/acpiintr", OWRITE);
  39. if (fd < 0)
  40. exits("can't open /dev/acpiintr");
  41. status = AcpiInitializeSubsystem();
  42. if (ACPI_FAILURE(status))
  43. fail("failed to init subsystem");
  44. status = AcpiInitializeTables(NULL, 4, 0);
  45. if (ACPI_FAILURE(status))
  46. fail("failed to init tables");
  47. status = AcpiLoadTables();
  48. if (ACPI_FAILURE(status))
  49. fail("failed to load tables");
  50. status = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
  51. if (ACPI_FAILURE(status))
  52. fail("failed to enable subsystem");
  53. /* ACPI Handlers should be installed at this time */
  54. status = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
  55. if (ACPI_FAILURE(status))
  56. fail("failed to initialize objects");
  57. /* Other ACPI initialization can occur here including:
  58. * Enumerating devices using _HID method
  59. * Loading, configuring, installing device drivers
  60. * Device Driver install handlers for other address spaces
  61. * Enumerating PCI devices and loading PCIConfig handlers
  62. */
  63. //AcpiDbgLevel |= ACPI_LV_VERBOSITY1 | ACPI_LV_FUNCTIONS;
  64. //status = AcpiInstallGlobalEventHandler(shutdownhandler, nil);
  65. status = AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, shutdownhandler, nil);
  66. status = AcpiInstallFixedEventHandler(ACPI_EVENT_PMTIMER, timerhandler, nil);
  67. if (ACPI_FAILURE(status))
  68. fail("failed to install fixed event handler");
  69. print("Fixed Event Handler installed!\n");
  70. status = AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0);
  71. if (ACPI_FAILURE(status))
  72. fail("failed to enable power button");
  73. status = AcpiEnableEvent(ACPI_EVENT_PMTIMER, 0);
  74. if (ACPI_FAILURE(status))
  75. fail("failed to enable pmtimer");
  76. while (AcpiIntrWait(fd, &info) > sizeof(info)) {
  77. AcpiRunInterrupt();
  78. }
  79. status = AcpiTerminate();
  80. if (ACPI_FAILURE(status))
  81. fail("failed to terminate acpi");
  82. exits(nil);
  83. }