summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Yung-Chieh Lo <yjlou@chromium.org>2012-06-01 16:33:58 +0800
committerGerrit <chrome-bot@google.com>2012-06-07 20:48:51 -0700
commit210ddfefcfe90944c70105463beb6f795513c5e6 (patch)
treed979799b7cdf940bfadaaeb5e258e24b0841c299
parentc4ac74a11dfa66d6ad725833e2aac6579b0c76ed (diff)
downloadchrome-ec-210ddfefcfe90944c70105463beb6f795513c5e6.tar.gz
Refactor the i8042 module to be thread-safe.
Any command/data coming from host will be placed in from_host queue, and the interrupt handler returns immediately. The i8042_command_task() will handle them later. Data reply to the host will be protected by the mutex. BUG=chrome-os-partner:10090 TEST=randomly play around on the link board. Change-Id: Ic19d5abd1abf8dc261ddaad4224cd9305c2f36a4 Signed-off-by: Louis Yung-Chieh Lo <yjlou@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/24299 Commit-Ready: Yung-Chieh Lo <yjlou%chromium.org@gtempaccount.com> Reviewed-by: Yung-Chieh Lo <yjlou%chromium.org@gtempaccount.com> Tested-by: Yung-Chieh Lo <yjlou%chromium.org@gtempaccount.com>
-rw-r--r--common/build.mk2
-rw-r--r--common/i8042.c145
-rw-r--r--common/queue.c53
-rw-r--r--include/queue.h40
4 files changed, 182 insertions, 58 deletions
diff --git a/common/build.mk b/common/build.mk
index f32e945113..5fdfabb061 100644
--- a/common/build.mk
+++ b/common/build.mk
@@ -8,7 +8,7 @@
common-y=main.o util.o console.o uart_buffering.o
common-y+=memory_commands.o shared_mem.o system_common.o hooks.o
-common-y+=gpio_commands.o version.o printf.o
+common-y+=gpio_commands.o version.o printf.o queue.o
common-$(CONFIG_BATTERY_ATL706486)+=battery_atl706486.o
common-$(CONFIG_CHARGER_BQ24725)+=charger_bq24725.o
common-$(CONFIG_PMU_TPS65090)+=pmu_tps65090.o
diff --git a/common/i8042.c b/common/i8042.c
index 8032e900cc..0174e84f0b 100644
--- a/common/i8042.c
+++ b/common/i8042.c
@@ -11,6 +11,7 @@
#include "i8042.h"
#include "keyboard.h"
#include "lpc.h"
+#include "queue.h"
#include "task.h"
#include "timer.h"
#include "util.h"
@@ -30,71 +31,69 @@
#define CPRINTF5(format, args...)
#endif
-#define MAX_QUEUED_KEY_PRESS 16
-/* Circular buffer to host.
- * head: next to dequeqe
- * tail: next to enqueue
- * head == tail: empty.
- * tail + 1 == head: full
- */
-static int head_to_buffer = 0;
-static int tail_to_buffer = 0;
-#define HOST_BUFFER_SIZE (16)
-static uint8_t to_host_buffer[HOST_BUFFER_SIZE];
+static int i8042_irq_enabled;
+
+
+static struct mutex to_host_mutex;
+static uint8_t to_host_buffer[16];
+static struct queue to_host = {
+ .buf_bytes = ARRAY_SIZE(to_host_buffer),
+ .unit_bytes = sizeof(uint8_t),
+ .buf = to_host_buffer,
+};
-static int i8042_irq_enabled = 0;
+/* Queue command/data from the host */
+enum {
+ HOST_COMMAND = 0,
+ HOST_DATA,
+};
+struct host_byte {
+ uint8_t type;
+ uint8_t byte;
+};
+/* 4 is big enough for all i8042 commands */
+static uint8_t from_host_buffer[4 * sizeof(struct host_byte)];
+static struct queue from_host = {
+ .buf_bytes = ARRAY_SIZE(from_host_buffer),
+ .unit_bytes = sizeof(struct host_byte),
+ .buf = from_host_buffer,
+};
/* Reset all i8042 buffer */
void i8042_flush_buffer()
{
- head_to_buffer = tail_to_buffer = 0;
+ queue_reset(&to_host);
lpc_keyboard_clear_buffer();
}
-/* Called by the chip-specific code when host sedns a byte to port 0x60. */
+/* Called by the chip-specific code when host sends a byte to port 0x60.
+ * Note that this is in the interrupt context.
+ */
void i8042_receives_data(int data)
{
- int ret_len;
- uint8_t output[MAX_SCAN_CODE_LEN];
- enum ec_error_list ret;
+ struct host_byte h;
- ret_len = handle_keyboard_data(data, output);
- ret = i8042_send_to_host(ret_len, output);
- ASSERT(ret == EC_SUCCESS);
+ h.type = HOST_DATA;
+ h.byte = data;
+ queue_add_units(&from_host, &h, 1);
+ task_wake(TASK_ID_I8042CMD);
}
-/* Called by the chip-specific code when host sedns a byte to port 0x64. */
+/* Called by the chip-specific code when host sends a byte to port 0x64.
+ * Note that this is in the interrupt context.
+ */
void i8042_receives_command(int cmd)
{
- int ret_len;
- uint8_t output[MAX_SCAN_CODE_LEN];
- enum ec_error_list ret;
-
- ret_len = handle_keyboard_command(cmd, output);
- ret = i8042_send_to_host(ret_len, output);
- ASSERT(ret == EC_SUCCESS);
-}
-
-
-/* Called by EC common code to send bytes to host via port 0x60. */
-static void enq_to_host(int len, const uint8_t *to_host)
-{
- int from, to;
+ struct host_byte h;
- /* Check if the buffer has enough space, then copy them to buffer. */
- if ((tail_to_buffer + len) <= (head_to_buffer + HOST_BUFFER_SIZE - 1)) {
- for (from = 0, to = tail_to_buffer; from < len;) {
- kblog_put('t', to);
- kblog_put('T', to_host[from]);
- to_host_buffer[to++] = to_host[from++];
- to %= HOST_BUFFER_SIZE;
- }
- tail_to_buffer = (tail_to_buffer + len) % HOST_BUFFER_SIZE;
- }
+ h.type = HOST_COMMAND;
+ h.byte = cmd;
+ queue_add_units(&from_host, &h, 1);
+ task_wake(TASK_ID_I8042CMD);
}
@@ -111,6 +110,24 @@ void i8042_disable_keyboard_irq(void) {
}
+static void i8042_handle_from_host(void)
+{
+ struct host_byte h;
+ int ret_len;
+ uint8_t output[MAX_SCAN_CODE_LEN];
+ enum ec_error_list ret;
+
+ while (queue_remove_unit(&from_host, &h)) {
+ if (h.type == HOST_COMMAND)
+ ret_len = handle_keyboard_command(h.byte, output);
+ else
+ ret_len = handle_keyboard_data(h.byte, output);
+
+ ret = i8042_send_to_host(ret_len, output);
+ ASSERT(ret == EC_SUCCESS);
+ }
+}
+
void i8042_command_task(void)
{
while (1) {
@@ -119,13 +136,13 @@ void i8042_command_task(void)
while (1) {
uint8_t chr;
- int empty = 0;
+
+ /* first handle command/data from host. */
+ i8042_handle_from_host();
/* Check if we have data in buffer to host. */
- if (head_to_buffer == tail_to_buffer) {
- empty = 1; /* nothing to host */
- }
- if (empty) break;
+ if (queue_is_empty(&to_host))
+ break; /* nothing to host */
/* if the host still didn't read that away,
try next time. */
@@ -137,11 +154,9 @@ void i8042_command_task(void)
}
/* Get a char from buffer. */
- chr = to_host_buffer[head_to_buffer];
- kblog_put('k', head_to_buffer);
+ kblog_put('k', to_host.head);
+ queue_remove_unit(&to_host, &chr);
kblog_put('K', chr);
- head_to_buffer =
- (head_to_buffer + 1) % HOST_BUFFER_SIZE;
/* Write to host. */
lpc_keyboard_put_char(chr, i8042_irq_enabled);
@@ -152,15 +167,31 @@ void i8042_command_task(void)
}
-enum ec_error_list i8042_send_to_host(int len, const uint8_t *to_host)
+static void enq_to_host(int len, const uint8_t *bytes)
+{
+ int i;
+
+ mutex_lock(&to_host_mutex);
+ /* Check if the buffer has enough space, then copy them to buffer. */
+ if (queue_has_space(&to_host, len)) {
+ for (i = 0; i < len; ++i) {
+ kblog_put('t', to_host.tail);
+ kblog_put('T', bytes[i]);
+ }
+ queue_add_units(&to_host, bytes, len);
+ }
+ mutex_unlock(&to_host_mutex);
+}
+
+enum ec_error_list i8042_send_to_host(int len, const uint8_t *bytes)
{
int i;
for (i = 0; i < len; i++)
- kblog_put('s', to_host[i]);
+ kblog_put('s', bytes[i]);
/* Put to queue in memory */
- enq_to_host(len, to_host);
+ enq_to_host(len, bytes);
/* Wake up the task to move from queue to the buffer to host. */
task_wake(TASK_ID_I8042CMD);
diff --git a/common/queue.c b/common/queue.c
new file mode 100644
index 0000000000..8018e4c0b0
--- /dev/null
+++ b/common/queue.c
@@ -0,0 +1,53 @@
+/* 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.
+ *
+ * Queue data structure implementation.
+ */
+#include "queue.h"
+#include "util.h"
+
+void queue_reset(struct queue *queue)
+{
+ queue->head = queue->tail = 0;
+}
+
+int queue_is_empty(const struct queue *q)
+{
+ return q->head == q->tail;
+}
+
+int queue_has_space(const struct queue *q, int unit_count)
+{
+ return (q->tail + unit_count * q->unit_bytes) <=
+ (q->head + q->buf_bytes - q->unit_bytes);
+}
+
+void queue_add_units(struct queue *q, const void *src, int unit_count)
+{
+ const uint8_t *s = (const uint8_t *)src;
+
+ if (!queue_has_space(q, unit_count))
+ return;
+
+ for (unit_count *= q->unit_bytes; unit_count; unit_count--) {
+ q->buf[q->tail++] = *(s++);
+ q->tail %= q->buf_bytes;
+ }
+}
+
+int queue_remove_unit(struct queue *q, void *dest)
+{
+ int count;
+ uint8_t *d = (uint8_t *)dest;
+
+ if (queue_is_empty(q))
+ return 0;
+
+ for (count = q->unit_bytes; count; count--) {
+ *(d++) = q->buf[q->head++];
+ q->head %= q->buf_bytes;
+ }
+
+ return 1;
+}
diff --git a/include/queue.h b/include/queue.h
new file mode 100644
index 0000000000..2c967ee4a8
--- /dev/null
+++ b/include/queue.h
@@ -0,0 +1,40 @@
+/* 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.
+ *
+ * Queue data structure.
+ */
+
+#include "common.h"
+
+/* Generic queue container.
+ *
+ * head: next to dequeqe
+ * tail: next to enqueue
+ *
+ * Empty:
+ * head == tail
+ * Full:
+ * ((tail + 1) % buf_bytes) == head
+ */
+struct queue {
+ int head, tail;
+ int buf_bytes; /* size of buffer (in byte) */
+ int unit_bytes; /* size of unit (in byte) */
+ uint8_t *buf;
+};
+
+/* Reset the queue to empty state. */
+void queue_reset(struct queue *queue);
+
+/* Return TRUE if the queue is empty. */
+int queue_is_empty(const struct queue *q);
+
+/* Return TRUE if the queue has at least one unit space. */
+int queue_has_space(const struct queue *q, int unit_count);
+
+/* Add multiple units into queue. */
+void queue_add_units(struct queue *q, const void *src, int unit_count);
+
+/* Remove one unit from the begin of the queue. */
+int queue_remove_unit(struct queue *q, void *dest);