Skip to content

Instantly share code, notes, and snippets.

@classilla
Last active July 27, 2020 03:05
Show Gist options
  • Save classilla/0771ed3bddb11053338939fc60be70ed to your computer and use it in GitHub Desktop.
Save classilla/0771ed3bddb11053338939fc60be70ed to your computer and use it in GitHub Desktop.
Hack to enable automatic reboot after a power failure for NetBSD/macppc machines with pmu(4). Based on FreeBSD's. Updated for 9.0.
--- pmu.c.orig 2018-09-03 09:29:25.000000000 -0700
+++ pmu.c 2020-07-26 16:14:05.294338173 -0700
@@ -147,16 +147,17 @@
static int pmu_adb_set_handler(void *, void (*)(void *, int, uint8_t *), void *);
/* i2c stuff */
static int pmu_i2c_acquire_bus(void *, int);
static void pmu_i2c_release_bus(void *, int);
static int pmu_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
void *, size_t, int);
+static void pmu_server_mode(struct pmu_softc *);
static void pmu_attach_legacy_battery(struct pmu_softc *);
static void pmu_attach_smart_battery(struct pmu_softc *, int);
static int pmu_print(void *, const char *);
/* these values shows that number of data returned after 'send' cmd is sent */
static signed char pm_send_cmd_type[] = {
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
@@ -454,16 +455,19 @@
if (OF_getprop(pmnode, "prim-info", regs, sizeof(regs)) < 24)
goto bat_done;
nbat = regs[6] >> 16;
for (i = 0; i < nbat; i++)
pmu_attach_smart_battery(sc, i);
}
bat_done:
+ /* try to enable power fail mode */
+ pmu_server_mode(sc);
+
if (kthread_create(PRI_NONE, 0, NULL, pmu_thread, sc, &sc->sc_thread,
"%s", "pmu") != 0) {
aprint_error_dev(self, "unable to create event kthread\n");
}
sc->sc_lidswitch.smpsw_name = "Lid switch";
sc->sc_lidswitch.smpsw_type = PSWITCH_TYPE_LID;
if (sysmon_pswitch_register(&sc->sc_lidswitch) != 0)
@@ -1111,16 +1115,38 @@
static int
pmu_print(void *aux, const char *what)
{
return 0;
}
static void
+pmu_server_mode(struct pmu_softc *sc)
+{
+ uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
+ uint8_t setcmd[3];
+ uint8_t resp[3];
+ int len;
+
+ len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
+ if (len != 3) {
+ DPRINTF("pmu_server_mode query abnormal return length: %d\n",
+ len);
+ return;
+ }
+
+ setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
+ setcmd[1] = resp[1];
+ setcmd[2] = PMU_PWR_WAKEUP_AC_INSERT;
+
+ (void)pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
+}
+
+static void
pmu_attach_legacy_battery(struct pmu_softc *sc)
{
struct battery_attach_args baa;
baa.baa_type = BATTERY_TYPE_LEGACY;
baa.baa_pmu_ops = &sc->sc_pmu_ops;
config_found_ia(sc->sc_dev, "pmu_bus", &baa, pmu_print);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment