[coreboot-gerrit] New patch to review for coreboot: libpayload: Add mouse cursor driver

Patrick Rudolph (siro@das-labor.org) gerrit at coreboot.org
Mon Mar 6 18:51:52 CET 2017


Patrick Rudolph (siro at das-labor.org) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/18593

-gerrit

commit 4be2a8e5f683d931a9309f3879aeda70021b82cb
Author: Patrick Rudolph <siro at das-labor.org>
Date:   Mon Feb 6 15:02:25 2017 +0100

    libpayload: Add mouse cursor driver
    
    Add a driver to handle multiple low level mouse drivers
    and provide basic cursor acceleration support.
    
    Tested on Lenovo T500.
    
    Change-Id: Ib7cec736631b8acf81a14d28daa29ff720777b10
    Signed-off-by: Patrick Rudolph <siro at das-labor.org>
---
 payloads/libpayload/Kconfig                |  10 ++
 payloads/libpayload/drivers/Makefile.inc   |   2 +
 payloads/libpayload/drivers/mouse_cursor.c | 186 +++++++++++++++++++++++++++++
 payloads/libpayload/include/libpayload.h   |  37 ++++++
 4 files changed, 235 insertions(+)

diff --git a/payloads/libpayload/Kconfig b/payloads/libpayload/Kconfig
index dcb1f4e..02f5977 100644
--- a/payloads/libpayload/Kconfig
+++ b/payloads/libpayload/Kconfig
@@ -342,6 +342,16 @@ config NVRAM
 	depends on ARCH_X86 # for now
 	default y
 
+config MOUSE_CURSOR
+	bool "Support for mouse cursor handling"
+	default n
+	help
+	  Provides a common interface for various mouse cursor drivers.
+	  Supports up to 32 buttons.
+	  Supports 3 axis mice.
+	  Applies simple cursor acceleration.
+	  Allows to set cursor acceleration and cursor speed.
+
 config RTC_PORT_EXTENDED_VIA
 	bool "Extended RTC ports are 0x74/0x75"
 	default n
diff --git a/payloads/libpayload/drivers/Makefile.inc b/payloads/libpayload/drivers/Makefile.inc
index 1fa0ec3..c6f6575 100644
--- a/payloads/libpayload/drivers/Makefile.inc
+++ b/payloads/libpayload/drivers/Makefile.inc
@@ -42,6 +42,8 @@ libc-$(CONFIG_LP_PC_KEYBOARD) += keyboard.c
 
 libc-$(CONFIG_LP_CBMEM_CONSOLE) += cbmem_console.c
 
+libc-$(CONFIG_LP_MOUSE_CURSOR) += mouse_cursor.c
+
 libc-$(CONFIG_LP_NVRAM) += nvram.c
 libc-$(CONFIG_LP_NVRAM) += options.c
 
diff --git a/payloads/libpayload/drivers/mouse_cursor.c b/payloads/libpayload/drivers/mouse_cursor.c
new file mode 100644
index 0000000..73985ff
--- /dev/null
+++ b/payloads/libpayload/drivers/mouse_cursor.c
@@ -0,0 +1,186 @@
+/*
+ * This file is part of the libpayload project.
+ *
+ * Copyright (C) 2017 Patrick Rudolph <siro at das-labor.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <libpayload-config.h>
+#include <libpayload.h>
+#include <usb/usb.h>
+
+struct mouse_cursor_input_driver *cursor_in;
+
+static int mouse_cursor_driver_exists(struct mouse_cursor_input_driver *in)
+{
+	struct mouse_cursor_input_driver *head = cursor_in;
+
+	while (head) {
+		if (head == in)
+			return 1;
+		head = head->next;
+	}
+
+	return 0;
+}
+
+/** Add new mouse cursor input driver */
+void mouse_cursor_add_input_driver(struct mouse_cursor_input_driver *in)
+{
+	/* Check if this driver was already added to the console list */
+	if (mouse_cursor_driver_exists(in))
+		return;
+	in->next = cursor_in;
+	cursor_in = in;
+}
+
+/** Init enabled mouse cursor drivers */
+void mouse_cursor_init(void)
+{
+/* FIXME */
+}
+
+static u32 mouse_buttons;
+static u8 mouse_acceleration = 0x10;
+/* Fixed-point 1/256 units */
+static int mouse_rel_x;
+static int mouse_rel_y;
+static int mouse_rel_z;
+static u32 mouse_speed = 0x299;
+
+/** Test for fast moving cursor */
+static u8 mouse_cusor_is_fast(int x, int y)
+{
+	return (x * x + y * y) > (mouse_acceleration * mouse_acceleration);
+}
+
+/** Poll for mouse data.
+ * Polls all drivers for new mouse data.
+ * Applies accelerations to relative movement.
+ * Logical ors all buttons states.
+ * Call often to prevent driver's queue overrun !
+ */
+void mouse_cursor_poll(void)
+{
+	struct mouse_cursor_input_driver *in;
+	int rel_x, rel_y, rel_z;
+	u32 buttons;
+
+	mouse_buttons = 0;
+	/* Iterate over all connected drivers */
+	for (in = cursor_in; in != 0; in = in->next)
+		if (in->get_state) {
+			in->get_state(&rel_x, &rel_y, &rel_z, &buttons);
+
+			/* Accumulate relative movement */
+			if (mouse_cusor_is_fast(rel_x, rel_y)) {
+				/* Apply simple cursor acceleration. Default: x2.6 */
+				mouse_rel_x += rel_x * mouse_speed;
+				mouse_rel_y += rel_y * mouse_speed;
+				mouse_rel_z += rel_z * mouse_speed;
+			} else {
+				mouse_rel_x += rel_x * 256;
+				mouse_rel_y += rel_y * 256;
+				mouse_rel_z += rel_z * 256;
+			}
+
+			/* Logic or all buttons */
+			mouse_buttons |= buttons;
+		}
+}
+
+/** Get relative mouse movement.
+ * Returns relative mouse movement with acceleration
+ * applied. The internal state will be cleared and stays
+ * clear until one of the drivers provide motion input again.
+ */
+void mouse_cursor_get_rel(int *x, int *y, int *z)
+{
+	mouse_cursor_poll();
+
+	if (x) {
+		*x = mouse_rel_x / 256;
+		mouse_rel_x = 0;
+	}
+	if (y) {
+		*y = mouse_rel_y / 256;
+		mouse_rel_y = 0;
+	}
+	if (z) {
+		*z = mouse_rel_z / 256;
+		mouse_rel_z = 0;
+	}
+}
+
+/** Get mouse button state.
+ * Returns the current button states.
+ * There are up to 32 possible buttons,
+ * but most device only implement three buttons.
+ */
+void mouse_cursor_get_buttons(u32 *buttons)
+{
+	mouse_poll();
+
+	if (buttons)
+		*buttons = mouse_buttons;
+}
+
+/** Set cursor speed.
+ * Sets the mouse cursor speed coefficient.
+ * It is used in case the cursor is moving faster
+ * than the mouse accaleration coefficient.
+ */
+void mouse_cursor_set_speed(u32 val)
+{
+	mouse_speed = val;
+}
+
+/** Get cursor speed.
+ * Returns the internal mouse cursor speed in
+ * 1/256th units.
+ */
+u32 mouse_cursor_get_speed(void)
+{
+	return mouse_speed;
+}
+
+/** Set cursor acceleration.
+ * Sets the mouse cursor acceleration coefficient.
+ * It is used the compare the raw relative cursor
+ * movement against.
+ */
+void mouse_cursor_set_acceleration(u8 val)
+{
+	mouse_acceleration = val;
+}
+
+/** Get cursor acceleration.
+ * Returns the current cursor acceleration coefficient.
+ */
+u8 mouse_cursor_get_acceleration(void)
+{
+	return mouse_acceleration;
+}
+
diff --git a/payloads/libpayload/include/libpayload.h b/payloads/libpayload/include/libpayload.h
index 97d5944..3858caa 100644
--- a/payloads/libpayload/include/libpayload.h
+++ b/payloads/libpayload/include/libpayload.h
@@ -164,6 +164,20 @@ int keyboard_set_layout(char *country);
 /** @} */
 
 /**
+ * @defgroup mouse Mouse cursor functions
+ * @ingroup input
+ * @{
+ */
+void mouse_cursor_poll(void);
+void mouse_cursor_get_rel(int *x, int *y, int *z);
+void mouse_cursor_get_buttons(u32 *buttons);
+void mouse_cursor_set_speed(u32 val);
+u32 mouse_cursor_get_speed(void);
+void mouse_cursor_set_acceleration(u8 val);
+u8 mouse_cursor_get_acceleration(void);
+/** @} */
+
+/**
  * @defgroup serial Serial functions
  * @ingroup input
  * @{
@@ -304,6 +318,29 @@ int console_remove_output_driver(void *function);
 #define havechar havekey
 /** @} */
 
+/**
+ * @defgroup mouse_cursor Mouse cursor functions
+ * @{
+ */
+typedef enum {
+	CURSOR_INPUT_TYPE_UNKNOWN = 0,
+	CURSOR_INPUT_TYPE_USB,
+	CURSOR_INPUT_TYPE_PS2,
+} cursor_input_type;
+
+void mouse_cursor_init(void);
+
+struct mouse_cursor_input_driver;
+struct mouse_cursor_input_driver {
+	struct mouse_cursor_input_driver *next;
+	/* X,Y,Z axis and buttons */
+	void (*get_state)(int *, int *, int *, u32 *);
+	cursor_input_type input_type;
+};
+
+void mouse_cursor_add_input_driver(struct mouse_cursor_input_driver *in);
+
+/** @} */
 
 /**
  * @defgroup exec Execution functions



More information about the coreboot-gerrit mailing list