summaryrefslogtreecommitdiff
path: root/common/device_state.c
blob: 650e2671ef7c0b58cbfab0f88ed5b007e18e0f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "console.h"
#include "device_state.h"
#include "hooks.h"

static int enabled = 1;

int device_get_state(enum device_type device)
{
	return device_states[device].state;
}

void device_set_state(enum device_type device, enum device_state state)
{
	if (device_states[device].state == state)
		return;

	device_states[device].state = state;
}

static void check_device_state(void)
{
	int i;

	if (!enabled)
		return;

	for (i = 0; i < DEVICE_COUNT; i++)
		board_update_device_state(i);
}
DECLARE_HOOK(HOOK_SECOND, check_device_state, HOOK_PRIO_DEFAULT);

static int device_has_interrupts(enum device_type device)
{
	return (device_states[device].deferred &&
		device_states[device].detect_on != GPIO_COUNT &&
		device_states[device].detect_off != GPIO_COUNT);
}

static void disable_interrupts(enum device_type device)
{
	if (!device_has_interrupts(device))
		return;

	/* Cancel any deferred callbacks */
	hook_call_deferred(device_states[device].deferred, -1);

	/* Disable gpio interrupts */
	gpio_disable_interrupt(device_states[device].detect_on);
	gpio_disable_interrupt(device_states[device].detect_off);
}

static void enable_interrupts(enum device_type device)
{
	if (!device_has_interrupts(device))
		return;

	/* Enable gpio interrupts */
	gpio_enable_interrupt(device_states[device].detect_on);
	gpio_enable_interrupt(device_states[device].detect_off);
}

void device_detect_state_enable(int enable)
{
	int i;

	enabled = enable;
	for (i = 0; i < DEVICE_COUNT; i++) {
		if (enabled) {
			enable_interrupts(i);
			board_update_device_state(i);
		} else {
			disable_interrupts(i);
			device_set_state(i, DEVICE_STATE_UNKNOWN);
		}
	}
}

static void print_state(const char *name, enum device_state state)
{
	ccprintf("%-9s %s\n", name, state == DEVICE_STATE_ON ? "on" :
		 state == DEVICE_STATE_OFF ? "off" : "unknown");
}

static int command_devices(int argc, char **argv)
{
	int i;

	if (!enabled)
		ccprintf("Device monitoring disabled\n");
	else
		for (i = 0; i < DEVICE_COUNT; i++)
			print_state(device_states[i].name,
				    device_states[i].state);

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(devices, command_devices,
	"",
	"Get the device states",
	NULL);