Commit 45ceaf14 authored by Stephen Boyd's avatar Stephen Boyd Committed by Dmitry Torokhov

Input: extract ChromeOS vivaldi physmap show function

Let's introduce a common library file for the physmap show function
duplicated between three different keyboard drivers. This largely copies
the code from cros_ec_keyb.c which has the most recent version of the
show function, while using the vivaldi_data struct from the hid-vivaldi
driver. This saves a small amount of space in an allyesconfig build.

$ ./scripts/bloat-o-meter vmlinux.before vmlinux.after

add/remove: 3/0 grow/shrink: 2/3 up/down: 412/-720 (-308)
Function                                     old     new   delta
vivaldi_function_row_physmap_show              -     292    +292
_sub_I_65535_1                           1057564 1057616     +52
_sub_D_65535_0                           1057564 1057616     +52
e843419@49f2_00062737_9b04                     -       8      +8
e843419@20f6_0002a34d_35bc                     -       8      +8
atkbd_parse_fwnode_data                      480     472      -8
atkbd_do_show_function_row_physmap           316      76    -240
function_row_physmap_show                    620     148    -472
Total: Before=285581925, After=285581617, chg -0.00%
Signed-off-by: default avatarStephen Boyd <swboyd@chromium.org>
Tested-by: Stephen Boyd <swboyd@chromium.org> # coachz, wormdingler
Link: https://lore.kernel.org/r/20220228075446.466016-3-dmitry.torokhov@gmail.comSigned-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
parent d950db3f
...@@ -411,6 +411,7 @@ config HID_GOOGLE_HAMMER ...@@ -411,6 +411,7 @@ config HID_GOOGLE_HAMMER
config HID_VIVALDI config HID_VIVALDI
tristate "Vivaldi Keyboard" tristate "Vivaldi Keyboard"
select INPUT_VIVALDIFMAP
depends on HID depends on HID
help help
Say Y here if you want to enable support for Vivaldi keyboards. Say Y here if you want to enable support for Vivaldi keyboards.
......
...@@ -8,37 +8,24 @@ ...@@ -8,37 +8,24 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/hid.h> #include <linux/hid.h>
#include <linux/input/vivaldi-fmap.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/sysfs.h> #include <linux/sysfs.h>
#define MIN_FN_ROW_KEY 1 #define MIN_FN_ROW_KEY 1
#define MAX_FN_ROW_KEY 24 #define MAX_FN_ROW_KEY VIVALDI_MAX_FUNCTION_ROW_KEYS
#define HID_VD_FN_ROW_PHYSMAP 0x00000001 #define HID_VD_FN_ROW_PHYSMAP 0x00000001
#define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP) #define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP)
struct vivaldi_data {
u32 function_row_physmap[MAX_FN_ROW_KEY - MIN_FN_ROW_KEY + 1];
int max_function_row_key;
};
static ssize_t function_row_physmap_show(struct device *dev, static ssize_t function_row_physmap_show(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
char *buf) char *buf)
{ {
struct hid_device *hdev = to_hid_device(dev); struct hid_device *hdev = to_hid_device(dev);
struct vivaldi_data *drvdata = hid_get_drvdata(hdev); struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
ssize_t size = 0;
int i;
if (!drvdata->max_function_row_key)
return 0;
for (i = 0; i < drvdata->max_function_row_key; i++) return vivaldi_function_row_physmap_show(drvdata, buf);
size += sprintf(buf + size, "%02X ",
drvdata->function_row_physmap[i]);
size += sprintf(buf + size, "\n");
return size;
} }
static DEVICE_ATTR_RO(function_row_physmap); static DEVICE_ATTR_RO(function_row_physmap);
...@@ -85,11 +72,11 @@ static void vivaldi_feature_mapping(struct hid_device *hdev, ...@@ -85,11 +72,11 @@ static void vivaldi_feature_mapping(struct hid_device *hdev,
(usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL) (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
return; return;
fn_key = (usage->hid & HID_USAGE); fn_key = usage->hid & HID_USAGE;
if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY) if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
return; return;
if (fn_key > drvdata->max_function_row_key) if (fn_key > drvdata->num_function_row_keys)
drvdata->max_function_row_key = fn_key; drvdata->num_function_row_keys = fn_key;
report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL); report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL);
if (!report_data) if (!report_data)
......
...@@ -77,6 +77,13 @@ config INPUT_MATRIXKMAP ...@@ -77,6 +77,13 @@ config INPUT_MATRIXKMAP
To compile this driver as a module, choose M here: the To compile this driver as a module, choose M here: the
module will be called matrix-keymap. module will be called matrix-keymap.
config INPUT_VIVALDIFMAP
tristate
help
ChromeOS Vivaldi keymap support library. This is a hidden
option so that drivers can use common code to parse and
expose the vivaldi function row keymap.
comment "Userland interfaces" comment "Userland interfaces"
config INPUT_MOUSEDEV config INPUT_MOUSEDEV
......
...@@ -12,6 +12,7 @@ input-core-y += touchscreen.o ...@@ -12,6 +12,7 @@ input-core-y += touchscreen.o
obj-$(CONFIG_INPUT_FF_MEMLESS) += ff-memless.o obj-$(CONFIG_INPUT_FF_MEMLESS) += ff-memless.o
obj-$(CONFIG_INPUT_SPARSEKMAP) += sparse-keymap.o obj-$(CONFIG_INPUT_SPARSEKMAP) += sparse-keymap.o
obj-$(CONFIG_INPUT_MATRIXKMAP) += matrix-keymap.o obj-$(CONFIG_INPUT_MATRIXKMAP) += matrix-keymap.o
obj-$(CONFIG_INPUT_VIVALDIFMAP) += vivaldi-fmap.o
obj-$(CONFIG_INPUT_LEDS) += input-leds.o obj-$(CONFIG_INPUT_LEDS) += input-leds.o
obj-$(CONFIG_INPUT_MOUSEDEV) += mousedev.o obj-$(CONFIG_INPUT_MOUSEDEV) += mousedev.o
......
...@@ -103,6 +103,7 @@ config KEYBOARD_ATKBD ...@@ -103,6 +103,7 @@ config KEYBOARD_ATKBD
select SERIO_LIBPS2 select SERIO_LIBPS2
select SERIO_I8042 if ARCH_MIGHT_HAVE_PC_SERIO select SERIO_I8042 if ARCH_MIGHT_HAVE_PC_SERIO
select SERIO_GSCPS2 if GSC select SERIO_GSCPS2 if GSC
select INPUT_VIVALDIFMAP
help help
Say Y here if you want to use a standard AT or PS/2 keyboard. Usually Say Y here if you want to use a standard AT or PS/2 keyboard. Usually
you'll need this, unless you have a different type keyboard (USB, ADB you'll need this, unless you have a different type keyboard (USB, ADB
...@@ -749,6 +750,7 @@ config KEYBOARD_XTKBD ...@@ -749,6 +750,7 @@ config KEYBOARD_XTKBD
config KEYBOARD_CROS_EC config KEYBOARD_CROS_EC
tristate "ChromeOS EC keyboard" tristate "ChromeOS EC keyboard"
select INPUT_MATRIXKMAP select INPUT_MATRIXKMAP
select INPUT_VIVALDIFMAP
depends on CROS_EC depends on CROS_EC
help help
Say Y here to enable the matrix keyboard used by ChromeOS devices Say Y here to enable the matrix keyboard used by ChromeOS devices
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/input/vivaldi-fmap.h>
#include <linux/serio.h> #include <linux/serio.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <linux/libps2.h> #include <linux/libps2.h>
...@@ -64,8 +65,6 @@ static bool atkbd_terminal; ...@@ -64,8 +65,6 @@ static bool atkbd_terminal;
module_param_named(terminal, atkbd_terminal, bool, 0); module_param_named(terminal, atkbd_terminal, bool, 0);
MODULE_PARM_DESC(terminal, "Enable break codes on an IBM Terminal keyboard connected via AT/PS2"); MODULE_PARM_DESC(terminal, "Enable break codes on an IBM Terminal keyboard connected via AT/PS2");
#define MAX_FUNCTION_ROW_KEYS 24
#define SCANCODE(keymap) ((keymap >> 16) & 0xFFFF) #define SCANCODE(keymap) ((keymap >> 16) & 0xFFFF)
#define KEYCODE(keymap) (keymap & 0xFFFF) #define KEYCODE(keymap) (keymap & 0xFFFF)
...@@ -237,8 +236,7 @@ struct atkbd { ...@@ -237,8 +236,7 @@ struct atkbd {
/* Serializes reconnect(), attr->set() and event work */ /* Serializes reconnect(), attr->set() and event work */
struct mutex mutex; struct mutex mutex;
u32 function_row_physmap[MAX_FUNCTION_ROW_KEYS]; struct vivaldi_data vdata;
int num_function_row_keys;
}; };
/* /*
...@@ -308,17 +306,7 @@ static struct attribute *atkbd_attributes[] = { ...@@ -308,17 +306,7 @@ static struct attribute *atkbd_attributes[] = {
static ssize_t atkbd_show_function_row_physmap(struct atkbd *atkbd, char *buf) static ssize_t atkbd_show_function_row_physmap(struct atkbd *atkbd, char *buf)
{ {
ssize_t size = 0; return vivaldi_function_row_physmap_show(&atkbd->vdata, buf);
int i;
if (!atkbd->num_function_row_keys)
return 0;
for (i = 0; i < atkbd->num_function_row_keys; i++)
size += scnprintf(buf + size, PAGE_SIZE - size, "%02X ",
atkbd->function_row_physmap[i]);
size += scnprintf(buf + size, PAGE_SIZE - size, "\n");
return size;
} }
static umode_t atkbd_attr_is_visible(struct kobject *kobj, static umode_t atkbd_attr_is_visible(struct kobject *kobj,
...@@ -329,7 +317,7 @@ static umode_t atkbd_attr_is_visible(struct kobject *kobj, ...@@ -329,7 +317,7 @@ static umode_t atkbd_attr_is_visible(struct kobject *kobj,
struct atkbd *atkbd = serio_get_drvdata(serio); struct atkbd *atkbd = serio_get_drvdata(serio);
if (attr == &atkbd_attr_function_row_physmap.attr && if (attr == &atkbd_attr_function_row_physmap.attr &&
!atkbd->num_function_row_keys) !atkbd->vdata.num_function_row_keys)
return 0; return 0;
return attr->mode; return attr->mode;
...@@ -1206,10 +1194,11 @@ static void atkbd_parse_fwnode_data(struct serio *serio) ...@@ -1206,10 +1194,11 @@ static void atkbd_parse_fwnode_data(struct serio *serio)
/* Parse "function-row-physmap" property */ /* Parse "function-row-physmap" property */
n = device_property_count_u32(dev, "function-row-physmap"); n = device_property_count_u32(dev, "function-row-physmap");
if (n > 0 && n <= MAX_FUNCTION_ROW_KEYS && if (n > 0 && n <= VIVALDI_MAX_FUNCTION_ROW_KEYS &&
!device_property_read_u32_array(dev, "function-row-physmap", !device_property_read_u32_array(dev, "function-row-physmap",
atkbd->function_row_physmap, n)) { atkbd->vdata.function_row_physmap,
atkbd->num_function_row_keys = n; n)) {
atkbd->vdata.num_function_row_keys = n;
dev_dbg(dev, "FW reported %d function-row key locations\n", n); dev_dbg(dev, "FW reported %d function-row key locations\n", n);
} }
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/input/vivaldi-fmap.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/notifier.h> #include <linux/notifier.h>
...@@ -27,8 +28,6 @@ ...@@ -27,8 +28,6 @@
#include <asm/unaligned.h> #include <asm/unaligned.h>
#define MAX_NUM_TOP_ROW_KEYS 15
/** /**
* struct cros_ec_keyb - Structure representing EC keyboard device * struct cros_ec_keyb - Structure representing EC keyboard device
* *
...@@ -44,9 +43,7 @@ ...@@ -44,9 +43,7 @@
* @idev: The input device for the matrix keys. * @idev: The input device for the matrix keys.
* @bs_idev: The input device for non-matrix buttons and switches (or NULL). * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
* @notifier: interrupt event notifier for transport devices * @notifier: interrupt event notifier for transport devices
* @function_row_physmap: An array of the encoded rows/columns for the top * @vdata: vivaldi function row data
* row function keys, in an order from left to right
* @num_function_row_keys: The number of top row keys in a custom keyboard
*/ */
struct cros_ec_keyb { struct cros_ec_keyb {
unsigned int rows; unsigned int rows;
...@@ -64,8 +61,7 @@ struct cros_ec_keyb { ...@@ -64,8 +61,7 @@ struct cros_ec_keyb {
struct input_dev *bs_idev; struct input_dev *bs_idev;
struct notifier_block notifier; struct notifier_block notifier;
u16 function_row_physmap[MAX_NUM_TOP_ROW_KEYS]; struct vivaldi_data vdata;
size_t num_function_row_keys;
}; };
/** /**
...@@ -537,9 +533,9 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev) ...@@ -537,9 +533,9 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
int err; int err;
struct property *prop; struct property *prop;
const __be32 *p; const __be32 *p;
u16 *physmap; u32 *physmap;
u32 key_pos; u32 key_pos;
int row, col; unsigned int row, col, scancode, n_physmap;
err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols); err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
if (err) if (err)
...@@ -591,20 +587,21 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev) ...@@ -591,20 +587,21 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
ckdev->idev = idev; ckdev->idev = idev;
cros_ec_keyb_compute_valid_keys(ckdev); cros_ec_keyb_compute_valid_keys(ckdev);
physmap = ckdev->function_row_physmap; physmap = ckdev->vdata.function_row_physmap;
n_physmap = 0;
of_property_for_each_u32(dev->of_node, "function-row-physmap", of_property_for_each_u32(dev->of_node, "function-row-physmap",
prop, p, key_pos) { prop, p, key_pos) {
if (ckdev->num_function_row_keys == MAX_NUM_TOP_ROW_KEYS) { if (n_physmap == VIVALDI_MAX_FUNCTION_ROW_KEYS) {
dev_warn(dev, "Only support up to %d top row keys\n", dev_warn(dev, "Only support up to %d top row keys\n",
MAX_NUM_TOP_ROW_KEYS); VIVALDI_MAX_FUNCTION_ROW_KEYS);
break; break;
} }
row = KEY_ROW(key_pos); row = KEY_ROW(key_pos);
col = KEY_COL(key_pos); col = KEY_COL(key_pos);
*physmap = MATRIX_SCAN_CODE(row, col, ckdev->row_shift); scancode = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
physmap++; physmap[n_physmap++] = scancode;
ckdev->num_function_row_keys++;
} }
ckdev->vdata.num_function_row_keys = n_physmap;
err = input_register_device(ckdev->idev); err = input_register_device(ckdev->idev);
if (err) { if (err) {
...@@ -619,18 +616,10 @@ static ssize_t function_row_physmap_show(struct device *dev, ...@@ -619,18 +616,10 @@ static ssize_t function_row_physmap_show(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
char *buf) char *buf)
{ {
ssize_t size = 0; const struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
int i; const struct vivaldi_data *data = &ckdev->vdata;
struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
u16 *physmap = ckdev->function_row_physmap;
for (i = 0; i < ckdev->num_function_row_keys; i++)
size += scnprintf(buf + size, PAGE_SIZE - size,
"%s%02X", size ? " " : "", physmap[i]);
if (size)
size += scnprintf(buf + size, PAGE_SIZE - size, "\n");
return size; return vivaldi_function_row_physmap_show(data, buf);
} }
static DEVICE_ATTR_RO(function_row_physmap); static DEVICE_ATTR_RO(function_row_physmap);
...@@ -648,7 +637,7 @@ static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj, ...@@ -648,7 +637,7 @@ static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
struct cros_ec_keyb *ckdev = dev_get_drvdata(dev); struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
if (attr == &dev_attr_function_row_physmap.attr && if (attr == &dev_attr_function_row_physmap.attr &&
!ckdev->num_function_row_keys) !ckdev->vdata.num_function_row_keys)
return 0; return 0;
return attr->mode; return attr->mode;
......
// SPDX-License-Identifier: GPL-2.0
/*
* Helpers for ChromeOS Vivaldi keyboard function row mapping
*
* Copyright (C) 2022 Google, Inc
*/
#include <linux/export.h>
#include <linux/input/vivaldi-fmap.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
/**
* vivaldi_function_row_physmap_show - Print vivaldi function row physmap attribute
* @data: The vivaldi function row map
* @buf: Buffer to print the function row phsymap to
*/
ssize_t vivaldi_function_row_physmap_show(const struct vivaldi_data *data,
char *buf)
{
ssize_t size = 0;
int i;
const u32 *physmap = data->function_row_physmap;
if (!data->num_function_row_keys)
return 0;
for (i = 0; i < data->num_function_row_keys; i++)
size += scnprintf(buf + size, PAGE_SIZE - size,
"%s%02X", size ? " " : "", physmap[i]);
if (size)
size += scnprintf(buf + size, PAGE_SIZE - size, "\n");
return size;
}
EXPORT_SYMBOL_GPL(vivaldi_function_row_physmap_show);
MODULE_LICENSE("GPL");
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _VIVALDI_FMAP_H
#define _VIVALDI_FMAP_H
#include <linux/types.h>
#define VIVALDI_MAX_FUNCTION_ROW_KEYS 24
/**
* struct vivaldi_data - Function row map data for ChromeOS Vivaldi keyboards
* @function_row_physmap: An array of scancodes or their equivalent (HID usage
* codes, encoded rows/columns, etc) for the top
* row function keys, in an order from left to right
* @num_function_row_keys: The number of top row keys in a custom keyboard
*
* This structure is supposed to be used by ChromeOS keyboards using
* the Vivaldi keyboard function row design.
*/
struct vivaldi_data {
u32 function_row_physmap[VIVALDI_MAX_FUNCTION_ROW_KEYS];
unsigned int num_function_row_keys;
};
ssize_t vivaldi_function_row_physmap_show(const struct vivaldi_data *data,
char *buf);
#endif /* _VIVALDI_FMAP_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment