summaryrefslogtreecommitdiffstats
path: root/common/poweroff.c
blob: 32a78280d3a98aca93c2c8d5d0f79c06d32c247c (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
106
107
108
109
110
111
112
113
114
/*
 * Copyright (c) 2015 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#define pr_fmt(fmt) "poweroff: " fmt

#include <common.h>
#include <poweroff.h>
#include <malloc.h>
#include <of.h>

static LIST_HEAD(poweroff_handler_list);

/**
 * poweroff_handler_register() - register a handler for poweroffing the system
 * @rst:	The handler struct
 *
 * This adds @rst to the list of registered poweroff handlers.
 *
 * return: 0 for success or negative error code
 */
int poweroff_handler_register(struct poweroff_handler *handler)
{
	if (!handler->name)
		handler->name = POWEROFF_DEFAULT_NAME;
	if (!handler->priority)
		handler->priority = POWEROFF_DEFAULT_PRIORITY;

	list_add_tail(&handler->list, &poweroff_handler_list);

	pr_debug("registering poweroff handler \"%s\" with priority %d\n",
			handler->name, handler->priority);

	return 0;
}

/**
 * poweroff_handler_register_fn() - register a handler function
 * @poweroff_fn:	The poweroff function
 *
 * convenience wrapper for poweroff_handler_register() to register a handler
 * with given function and default values otherwise.
 *
 * return: 0 for success or negative error code
 */
int poweroff_handler_register_fn(void (*poweroff_fn)(struct poweroff_handler *))
{
	struct poweroff_handler *handler;
	int ret;

	handler = xzalloc(sizeof(*handler));

	handler->poweroff = poweroff_fn;

	ret = poweroff_handler_register(handler);

	if (ret)
		free(handler);

	return ret;
}

/**
 * poweroff_machine() - power off the machine
 */
void __noreturn poweroff_machine(void)
{
	struct poweroff_handler *handler = NULL, *tmp;
	unsigned int priority = 0;

	list_for_each_entry(tmp, &poweroff_handler_list, list) {
		if (tmp->priority > priority) {
			priority = tmp->priority;
			handler = tmp;
		}
	}

	if (handler) {
		pr_debug("using poweroff handler %s\n", handler->name);
		console_flush();
		handler->poweroff(handler);
	} else {
		pr_err("No poweroff handler found!\n");
	}

	hang();
}

/**
 * of_get_poweroff_priority() - get the desired poweroff priority from device tree
 * @node:	The device_node to read the property from
 *
 * return: The priority
 */
unsigned int of_get_poweroff_priority(struct device_node *node)
{
	unsigned int priority = POWEROFF_DEFAULT_PRIORITY;

	of_property_read_u32(node, "poweroff-priority", &priority);

	return priority;
}