summaryrefslogtreecommitdiff
path: root/common/host_command.c
blob: 93e66b68ad91bf3bd7a32a52eef0499c3ceecee7 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright (c) 2012 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.
 */

/* Host command module for Chrome EC */

#include "console.h"
#include "host_command.h"
#include "link_defs.h"
#include "lpc.h"
#include "ec_commands.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "util.h"

/* Console output macros */
#define CPUTS(outstr) cputs(CC_SYSTEM, outstr)
#define CPRINTF(format, args...) cprintf(CC_SYSTEM, format, ## args)

#define TASK_EVENT_SLOT(n) TASK_EVENT_CUSTOM(1 << n)

static int host_command[2];


void host_command_received(int slot, int command)
{
	/* TODO: should warn if we already think we're in a command */

	/* If this is the reboot command, reboot immediately.  This gives
	 * the host processor a way to unwedge the EC even if it's busy with
	 * some other command. */
	if (command == EC_CMD_REBOOT) {
		system_reset(1);
		/* Reset should never return; if it does, post an error */
		host_send_result(slot, EC_RES_ERROR);
		return;
	}

	/* Save the command */
	host_command[slot] = command;

	/* Wake up the task to handle the command for the slot */
	task_set_event(TASK_ID_HOSTCMD, TASK_EVENT_SLOT(slot), 0);
}


static int host_command_proto_version(uint8_t *data, int *resp_size)
{
	struct ec_response_proto_version *r =
		(struct ec_response_proto_version *)data;

	r->version = EC_PROTO_VERSION;

	*resp_size = sizeof(struct ec_response_proto_version);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_PROTO_VERSION, host_command_proto_version);


static int host_command_hello(uint8_t *data, int *resp_size)
{
	struct ec_params_hello *p = (struct ec_params_hello *)data;
	struct ec_response_hello *r = (struct ec_response_hello *)data;
	uint32_t d = p->in_data;

	r->out_data = d + 0x01020304;
	*resp_size = sizeof(struct ec_response_hello);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HELLO, host_command_hello);


static int host_command_read_test(uint8_t *data, int *resp_size)
{
	struct ec_params_read_test *p = (struct ec_params_read_test *)data;
	struct ec_response_read_test *r =
			(struct ec_response_read_test *)data;

	int offset = p->offset;
	int size = p->size / sizeof(uint32_t);
	int i;

	if (size > ARRAY_SIZE(r->data))
		return EC_RES_ERROR;

	for (i = 0; i < size; i++)
		r->data[i] = offset + i;

	*resp_size = sizeof(struct ec_response_read_test);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_READ_TEST, host_command_read_test);


#ifdef CONFIG_LPC
/* ACPI query event handler.  Note that the returned value is NOT actually
 * an EC_RES enum; it's 0 if no event was pending, or the 1-based
 * index of the lowest bit which was set. */
static int host_command_acpi_query_event(uint8_t *data, int *resp_size)
{
	uint32_t events = lpc_get_host_events();
	int i;

	for (i = 0; i < 32; i++) {
		if (events & (1 << i)) {
			lpc_clear_host_events(1 << i);
			return i + 1;
		}
	}

	/* No events pending */
	return 0;
}
DECLARE_HOST_COMMAND(EC_CMD_ACPI_QUERY_EVENT, host_command_acpi_query_event);
#endif


/* Finds a command by command number.  Returns the command structure, or NULL if
 * no match found. */
static const struct host_command *find_host_command(int command)
{
	const struct host_command *cmd;

	for (cmd = __hcmds; cmd < __hcmds_end; cmd++) {
		if (command == cmd->command)
			return cmd;
	}

	return NULL;
}


/* Handle a LPC command */
static void command_process(int slot)
{
	int command = host_command[slot];
	uint8_t *data = host_get_buffer(slot);
	const struct host_command *cmd = find_host_command(command);

	CPRINTF("[hostcmd%d 0x%02x]\n", slot, command);

	if (cmd) {
		int size = 0;
		int res = cmd->handler(data, &size);
		if ((res == EC_RES_SUCCESS) && size)
			host_send_response(slot, data, size);
		else
			host_send_result(slot, res);
	} else {
		host_send_result(slot, EC_RES_INVALID_COMMAND);
	}
}

/*****************************************************************************/
/* Initialization / task */

static int host_command_init(void)
{
	host_command[0] = host_command[1] = -1;

	return EC_SUCCESS;
}


void host_command_task(void)
{
	host_command_init();

	while (1) {
		/* wait for the next command event */
		int evt = task_wait_event(-1);
		/* process it */
		if (evt & TASK_EVENT_SLOT(0))
			command_process(0);
		if (evt & TASK_EVENT_SLOT(1))
			command_process(1);
	}
}