summaryrefslogtreecommitdiff
path: root/sys/arch/armv7/exynos/crosec.c
blob: 21178b485349e7a5de94a49b2d5db91bc4987de2 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* $OpenBSD: crosec.c,v 1.1 2015/01/26 02:48:24 bmercer Exp $ */
/*
 * Copyright (c) 2013 Patrick Wildt <patrick@blueri.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/sensors.h>
#include <sys/malloc.h>

#include <armv7/exynos/crosecvar.h>

#ifdef DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif

int	cros_ec_match(struct device *, void *, void *);
void	cros_ec_attach(struct device *, struct device *, void *);

int	cros_ec_send_command(struct cros_ec_softc *, uint8_t,
		int, const void *, int, uint8_t **, int);
int	cros_ec_command(struct cros_ec_softc *, uint8_t,
		int, const void *, int, void *, int);
int	cros_ec_command_inptr(struct cros_ec_softc *, uint8_t,
		int, const void *, int, uint8_t **, int);
int	cros_ec_i2c_command(struct cros_ec_softc *, uint8_t,
		int, const uint8_t *, int, uint8_t **, int);
int	cros_ec_calc_checksum(const uint8_t *, int);

struct cfattach crosec_ca = {
	sizeof(struct cros_ec_softc), cros_ec_match, cros_ec_attach
};

struct cfdriver crosec_cd = {
	NULL, "crosec", DV_DULL
};

int
cros_ec_match(struct device *parent, void *match, void *aux)
{
	struct i2c_attach_args *ia = aux;

	if (strcmp(ia->ia_name, "crosec") == 0)
		return 1;
	return 0;
}

void
cros_ec_attach(struct device *parent, struct device *self, void *aux)
{
	struct cros_ec_softc *sc = (struct cros_ec_softc *)self;
	struct i2c_attach_args *ia = aux;

	sc->sc_tag = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;

	printf("\n");

	if (cros_ec_check_version(sc)) {
		printf("%s: could not initialize ChromeOS EC\n", __func__);
		return;
	}

	if (cros_ec_init_keyboard(sc)) {
		printf("%s: could not initialize keyboard\n", __func__);
		return;
	}
}

int
cros_ec_check_version(struct cros_ec_softc *sc)
{
	struct ec_params_hello req;
	struct ec_response_hello *resp;

	sc->cmd_version_is_supported = 1;
	if (cros_ec_command_inptr(sc, EC_CMD_HELLO, 0, &req, sizeof(req),
				(uint8_t **)&resp, sizeof(*resp)) > 0) {
		/* new version supported */
		sc->cmd_version_is_supported = 1;
	} else {
		printf("%s: old EC interface not supported\n", __func__);
		return -1;
	}

	return 0;
}

int
cros_ec_i2c_command(struct cros_ec_softc *sc, uint8_t cmd, int cmd_version,
		const uint8_t *out, int out_len, uint8_t **in, int in_len)
{
	int out_bytes, in_bytes, ret;
	uint8_t *ptr = sc->out;
	uint8_t *inptr = sc->in;

	inptr += sizeof(uint64_t); /* returned data should be 64-bit aligned */
	if (!sc->cmd_version_is_supported) {
		/* old-style */
		*ptr++ = cmd;
		out_bytes = out_len + 1;
		in_bytes = in_len + 2;
		inptr--; /* status byte */
	} else {
		/* new-style */
		*ptr++ = EC_CMD_VERSION0 + cmd_version;
		*ptr++ = cmd;
		*ptr++ = out_len;
		out_bytes = out_len + 4;
		in_bytes = in_len + 3;
		inptr -= 2; /* status byte, length */
	}
	memcpy(ptr, out, out_len);
	ptr += out_len;

	if (sc->cmd_version_is_supported)
		*ptr++ = (uint8_t)
			 cros_ec_calc_checksum(sc->out, out_len + 3);

	iic_acquire_bus(sc->sc_tag, 0);
	ret = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
	    sc->sc_addr, NULL, 0, &sc->out, out_bytes, 0);
	if (ret) {
		DPRINTF(("%s: I2C write failed\n", __func__));
		iic_release_bus(sc->sc_tag, 0);
		return -1;
	}

	ret = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
	    sc->sc_addr, NULL, 0, inptr, in_bytes, 0);
	if (ret) {
		DPRINTF(("%s: I2C read failed\n", __func__));
		iic_release_bus(sc->sc_tag, 0);
		return -1;
	}

	iic_release_bus(sc->sc_tag, 0);

	if (*inptr != EC_RES_SUCCESS) {
		DPRINTF(("%s: bad result\n", __func__));
		return -(int)*inptr;
	}

	if (sc->cmd_version_is_supported) {
		int len, csum;

		len = inptr[1];
		if (len > sizeof(sc->in)) {
			DPRINTF(("%s: Received length too large\n", __func__));
			return -1;
		}
		csum = cros_ec_calc_checksum(inptr, 2 + len);
		if (csum != inptr[2 + len]) {
			DPRINTF(("%s: Invalid checksum\n", __func__));
			return -1;
		}
		in_len = min(in_len, len);
	}

	*in = sc->in + sizeof(uint64_t);

	return in_len;
}

int
cros_ec_send_command(struct cros_ec_softc *sc, uint8_t cmd, int cmd_version,
		const void *out, int out_len, uint8_t **in, int in_len)
{
	return cros_ec_i2c_command(sc, cmd, cmd_version,
				(const uint8_t *)out, out_len, in, in_len);
}

int
cros_ec_command(struct cros_ec_softc *sc, uint8_t cmd,
		int cmd_version, const void *out, int out_len,
		void *in, int in_len) {
	uint8_t *in_buffer;
	int len;

	len = cros_ec_command_inptr(sc, cmd, cmd_version, out, out_len,
			&in_buffer, in_len);

	if (len > 0) {
		if (in && in_buffer) {
			len = min(in_len, len);
			memmove(in, in_buffer, len);
		}
	}
	return len;
}

int
cros_ec_command_inptr(struct cros_ec_softc *sc, uint8_t cmd,
		int cmd_version, const void *out, int out_len,
		uint8_t **inp, int in_len) {
	uint8_t *in;
	int len;

	len = cros_ec_send_command(sc, cmd, cmd_version,
			(const uint8_t *)out, out_len, &in, in_len);

	/* Wait for the command to complete. */
	if (len == -EC_RES_IN_PROGRESS) {
		struct ec_response_get_comms_status *resp;

		do {
			int ret;

			delay(50000);
			cros_ec_send_command(sc, EC_CMD_GET_COMMS_STATUS, 0,
					NULL, 0,
					(uint8_t **)&resp, sizeof(*resp));
			if (ret < 0)
				return ret;

			//timeout CROS_EC_CMD_TIMEOUT_MS
			//return -EC_RES_TIMEOUT
		} while (resp->flags & EC_COMMS_STATUS_PROCESSING);

		/* Let's get the response. */
		len = cros_ec_send_command(sc, EC_CMD_RESEND_RESPONSE, 0,
				out, out_len, &in, in_len);
	}

	if (inp != NULL)
		*inp = in;

	return len;
}

int
cros_ec_calc_checksum(const uint8_t *data, int size)
{
	int csum, i;

	for (i = csum = 0; i < size; i++)
		csum += data[i];
	return csum & 0xff;
}

int
cros_ec_scan_keyboard(struct cros_ec_softc *sc, uint8_t *scan, int len)
{
	if (cros_ec_command(sc, EC_CMD_CROS_EC_STATE, 0, NULL, 0, scan,
			len) < len)
		return -1;

	return 0;
}

int
cros_ec_info(struct cros_ec_softc *sc, struct ec_response_cros_ec_info *info)
{
	if (cros_ec_command(sc, EC_CMD_CROS_EC_INFO, 0, NULL, 0, info,
				sizeof(*info)) < sizeof(*info))
		return -1;

	return 0;
}