indent code coming from depthcharge and add missing acknowledgements

This commit is contained in:
Vladimir Serbinenko committed 2017-05-08 22:21:31 +02:00
1 parent 8af937d133
commit 3bf4907004
2 files changed
+79 -60

No files matched your search

+2
View File
@@ -4,6 +4,8 @@
* Copyright (C) 2012 Google Inc. * Copyright (C) 2012 Google Inc.
* Copyright (C) 2016 Free Software Foundation, Inc. * Copyright (C) 2016 Free Software Foundation, Inc.
* *
* This is based on depthcharge code.
*
* GRUB is free software: you can redistribute it and/or modify * GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
+77 -60
View File
@@ -4,6 +4,8 @@
* Copyright (C) 2012 Google Inc. * Copyright (C) 2012 Google Inc.
* Copyright (C) 2016 Free Software Foundation, Inc. * Copyright (C) 2016 Free Software Foundation, Inc.
* *
* This is based on depthcharge code.
*
* GRUB is free software: you can redistribute it and/or modify * GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
@@ -26,7 +28,7 @@
static grub_uint64_t static grub_uint64_t
grub_get_time_us (void) grub_get_time_us (void)
{ {
return 1000 * grub_get_time_ms(); return 1000 * grub_get_time_ms ();
} }
static void static void
@@ -102,25 +104,30 @@ static const uint8_t EcFramingByte = 0xec;
static uint64_t last_transfer; static uint64_t last_transfer;
static void stop_bus(void) static void
stop_bus (void)
{ {
spi_stop(); spi_stop ();
last_transfer = grub_get_time_us(); last_transfer = grub_get_time_us ();
} }
static int wait_for_frame(void) static int
wait_for_frame (void)
{ {
uint64_t start = grub_get_time_us(); uint64_t start = grub_get_time_us ();
uint8_t byte; uint8_t byte;
do { do
if (spi_read(&byte, 1)) {
if (spi_read (&byte, 1))
return -1; return -1;
if (byte != EcFramingByte && if (byte != EcFramingByte &&
grub_get_time_us() - start > FramingTimeoutUs) { grub_get_time_us () - start > FramingTimeoutUs)
grub_dprintf("cros", "Timeout waiting for framing byte.\n"); {
grub_dprintf ("cros", "Timeout waiting for framing byte.\n");
return -1; return -1;
} }
} while (byte != EcFramingByte); }
while (byte != EcFramingByte);
return 0; return 0;
} }
@@ -131,7 +138,8 @@ static int wait_for_frame(void)
* @param size Size of data block in bytes * @param size Size of data block in bytes
* @return checksum value (0 to 255) * @return checksum value (0 to 255)
*/ */
static uint8_t cros_ec_calc_checksum(const void *data, int size) static uint8_t
cros_ec_calc_checksum (const void *data, int size)
{ {
uint8_t csum; uint8_t csum;
const uint8_t *bytes = data; const uint8_t *bytes = data;
@@ -142,23 +150,24 @@ static uint8_t cros_ec_calc_checksum(const void *data, int size)
return csum & 0xff; return csum & 0xff;
} }
enum { enum
// response, arglen {
/* response, arglen */
CROS_EC_SPI_IN_HDR_SIZE = 2, CROS_EC_SPI_IN_HDR_SIZE = 2,
// version, cmd, arglen /* version, cmd, arglen */
CROS_EC_SPI_OUT_HDR_SIZE = 3 CROS_EC_SPI_OUT_HDR_SIZE = 3
}; };
static grub_uint8_t busbuf[256]; static grub_uint8_t busbuf[256];
#define MSG_BYTES ((int)sizeof (busbuf)) #define MSG_BYTES ((int)sizeof (busbuf))
static int ec_command(int cmd, int cmd_version, static int
const void *dout, int dout_len, ec_command (int cmd, int cmd_version,
void *din, int din_len) const void *dout, int dout_len, void *din, int din_len)
{ {
uint8_t *bytes; uint8_t *bytes;
// Header + data + checksum. /* Header + data + checksum. */
uint32_t out_bytes = CROS_EC_SPI_OUT_HDR_SIZE + dout_len + 1; uint32_t out_bytes = CROS_EC_SPI_OUT_HDR_SIZE + dout_len + 1;
uint32_t in_bytes = CROS_EC_SPI_IN_HDR_SIZE + din_len + 1; uint32_t in_bytes = CROS_EC_SPI_IN_HDR_SIZE + din_len + 1;
@@ -166,104 +175,112 @@ static int ec_command(int cmd, int cmd_version,
* Sanity-check I/O sizes given transaction overhead in internal * Sanity-check I/O sizes given transaction overhead in internal
* buffers. * buffers.
*/ */
if (out_bytes > MSG_BYTES) { if (out_bytes > MSG_BYTES)
grub_dprintf("cros", "Cannot send %d bytes\n", dout_len); {
grub_dprintf ("cros", "Cannot send %d bytes\n", dout_len);
return -1; return -1;
} }
if (in_bytes > MSG_BYTES) { if (in_bytes > MSG_BYTES)
grub_dprintf("cros", "Cannot receive %d bytes\n", din_len); {
grub_dprintf ("cros", "Cannot receive %d bytes\n", din_len);
return -1; return -1;
} }
// Prepare the output. /* Prepare the output. */
bytes = busbuf; bytes = busbuf;
*bytes++ = EC_CMD_VERSION0 + cmd_version; *bytes++ = EC_CMD_VERSION0 + cmd_version;
*bytes++ = cmd; *bytes++ = cmd;
*bytes++ = dout_len; *bytes++ = dout_len;
memcpy(bytes, dout, dout_len); memcpy (bytes, dout, dout_len);
bytes += dout_len; bytes += dout_len;
*bytes++ = cros_ec_calc_checksum(busbuf, *bytes++ = cros_ec_calc_checksum (busbuf,
CROS_EC_SPI_OUT_HDR_SIZE + dout_len); CROS_EC_SPI_OUT_HDR_SIZE + dout_len);
while (grub_get_time_us() - last_transfer < 200) while (grub_get_time_us () - last_transfer < 200)
; ;
if (spi_start()) if (spi_start ())
return -1; return -1;
// Allow EC to ramp up clock after being awoken. /* Allow EC to ramp up clock after being awoken. */
// See chrome-os-partner:32223 for more details. /* See chrome-os-partner:32223 for more details. */
grub_microsleep (100); grub_microsleep (100);
if (spi_send(busbuf, out_bytes)) { if (spi_send (busbuf, out_bytes))
stop_bus(); {
stop_bus ();
return -1; return -1;
} }
// Wait until the EC is ready. /* Wait until the EC is ready. */
if (wait_for_frame()) { if (wait_for_frame ())
stop_bus(); {
stop_bus ();
return -1; return -1;
} }
// Read the response code and the data length. /* Read the response code and the data length. */
bytes = busbuf; bytes = busbuf;
if (spi_read(bytes, 2)) { if (spi_read (bytes, 2))
stop_bus(); {
stop_bus ();
return -1; return -1;
} }
uint8_t result = *bytes++; uint8_t result = *bytes++;
uint8_t length = *bytes++; uint8_t length = *bytes++;
// Make sure there's enough room for the data. /* Make sure there's enough room for the data. */
if (CROS_EC_SPI_IN_HDR_SIZE + length + 1 > MSG_BYTES) { if (CROS_EC_SPI_IN_HDR_SIZE + length + 1 > MSG_BYTES)
grub_dprintf("cros", "Received length %#02x too large\n", {
length); grub_dprintf ("cros", "Received length %#02x too large\n", length);
stop_bus(); stop_bus ();
return -1; return -1;
} }
// Read the data and the checksum, and finish up. /* Read the data and the checksum, and finish up. */
if (spi_read(bytes, length + 1)) { if (spi_read (bytes, length + 1))
stop_bus(); {
stop_bus ();
return -1; return -1;
} }
bytes += length; bytes += length;
int expected = *bytes++; int expected = *bytes++;
stop_bus(); stop_bus ();
// Check the integrity of the response. /* Check the integrity of the response. */
if (result != 0) { if (result != 0)
grub_dprintf("cros", "Received bad result code %d\n", result); {
grub_dprintf ("cros", "Received bad result code %d\n", result);
return -result; return -result;
} }
int csum = cros_ec_calc_checksum(busbuf, int csum = cros_ec_calc_checksum (busbuf,
CROS_EC_SPI_IN_HDR_SIZE + length); CROS_EC_SPI_IN_HDR_SIZE + length);
if (csum != expected) { if (csum != expected)
grub_dprintf("cros", "Invalid checksum rx %#02x, calced %#02x\n", {
grub_dprintf ("cros", "Invalid checksum rx %#02x, calced %#02x\n",
expected, csum); expected, csum);
return -1; return -1;
} }
// If the caller wants the response, copy it out for them. /* If the caller wants the response, copy it out for them. */
if (length < din_len) if (length < din_len)
din_len = length; din_len = length;
if (din) { if (din)
memcpy(din, (uint8_t *)busbuf + CROS_EC_SPI_IN_HDR_SIZE, {
din_len); memcpy (din, (uint8_t *) busbuf + CROS_EC_SPI_IN_HDR_SIZE, din_len);
} }
return din_len; return din_len;
} }
int int
grub_cros_ec_scan_keyboard(struct grub_cros_ec_keyscan *scan) grub_cros_ec_scan_keyboard (struct grub_cros_ec_keyscan *scan)
{ {
if (ec_command(EC_CMD_MKBP_STATE, 0, NULL, 0, scan, if (ec_command (EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
sizeof(*scan)) < (int)sizeof(*scan)) sizeof (*scan)) < (int) sizeof (*scan))
return -1; return -1;
return 0; return 0;