blob: 261ce257848b28bba1cf2fd93f037075b775a528 [file] [log] [blame]
David Hendricks42ad6522011-08-09 16:08:10 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2011 The Chromium OS Authors
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * power.c: power management routines
20 */
21
22#include <stdlib.h>
23#include <string.h>
24
25#include "flash.h" /* for msg_* */
26#include "power.h"
27
28enum pm_state {
29 PM_OFF,
30 PM_ON,
31};
32
33/* power management daemons used in Chromium OS */
34static struct chromiumos_pm {
35 enum pm_state powerd; /* power management running inside minijail */
36 enum pm_state powerm; /* power management running outside minijail */
37} pm_state_at_start;
38
39static int pm_disabled;
40
41/* disable power management and set pm_state_at_start as an output */
42int disable_power_management()
43{
44 if (pm_disabled) {
45 msg_pdbg("%s: Nothing to do.\n", __func__);
46 return 0;
47 }
48
49 msg_pdbg("%s: Disabling power management services.\n", __func__);
50
51 /* if the service terminates successfully, then it was running when
52 * flashrom was invoked */
53 if (system("stop powerd >/dev/null") == 0)
54 pm_state_at_start.powerd = PM_ON;
55 else
56 pm_state_at_start.powerd = PM_OFF;
57
58 if (system("stop powerm >/dev/null") == 0)
59 pm_state_at_start.powerm = PM_ON;
60 else
61 pm_state_at_start.powerm = PM_OFF;
62
63 pm_disabled = 1;
64 return 0;
65}
66
67/* (re-)enable power management */
68int restore_power_management()
69{
70 int rc = 0;
71
72 if (!pm_disabled) {
73 msg_pdbg("%s(): Power management enabled\n", __func__);
74 return 0;
75 }
76
77 msg_pdbg("%s: (Re-)Enabling power management services\n", __func__);
78
79 if (pm_state_at_start.powerd == PM_ON) {
80 if (system("start powerd >/dev/null") != 0) {
81 msg_perr("Cannot restart powerd service\n");
82 rc |= 1;
83 }
84 }
85
86 if (pm_state_at_start.powerm == PM_ON) {
87 if (system("start powerm >/dev/null") != 0) {
88 msg_perr("Cannot restart powerm service\n");
89 rc |= 1;
90 }
91 }
92
93 pm_disabled = 0;
94 return rc;
95}