// SPDX-License-Identifier: GPL-2.0 // ChromeOS EC keyboard driver // // Copyright (C) 2012 Google, Inc. // // This driver uses the ChromeOS EC byte-level message-based protocol for // communicating the keyboard state (which keys are pressed) from a keyboard EC // to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing, // but everything else (including deghosting) is done here. The main // motivation for this is to keep the EC firmware as simple as possible, since // it cannot be easily upgraded and EC flash/IRAM space is relatively // expensive. #include <linux/module.h> #include <linux/acpi.h> #include <linux/bitops.h> #include <linux/i2c.h> #include <linux/input.h> #include <linux/input/vivaldi-fmap.h> #include <linux/interrupt.h> #include <linux/kernel.h> #include <linux/notifier.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/sysrq.h> #include <linux/input/matrix_keypad.h> #include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> #include <linux/unaligned.h> /** * struct cros_ec_keyb - Structure representing EC keyboard device * * @rows: Number of rows in the keypad * @cols: Number of columns in the keypad * @row_shift: log2 or number of rows, rounded up * @ghost_filter: true to enable the matrix key-ghosting filter * @valid_keys: bitmap of existing keys for each matrix column * @old_kb_state: bitmap of keys pressed last scan * @dev: Device pointer * @ec: Top level ChromeOS device to use to talk to EC * @idev: The input device for the matrix keys. * @bs_idev: The input device for non-matrix buttons and switches (or NULL). * @notifier: interrupt event notifier for transport devices * @vdata: vivaldi function row data */ struct cros_ec_keyb { … }; /** * struct cros_ec_bs_map - Mapping between Linux keycodes and EC button/switch * bitmap #defines * * @ev_type: The type of the input event to generate (e.g., EV_KEY). * @code: A linux keycode * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN * @inverted: If the #define and EV_SW have opposite meanings, this is true. * Only applicable to switches. */ struct cros_ec_bs_map { … }; /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */ static const struct cros_ec_bs_map cros_ec_keyb_bs[] = …; /* * Returns true when there is at least one combination of pressed keys that * results in ghosting. */ static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf) { … } /* * Compares the new keyboard state to the old one and produces key * press/release events accordingly. The keyboard state is 13 bytes (one byte * per column) */ static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev, uint8_t *kb_state, int len) { … } /** * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches * * This takes a bitmap of buttons or switches from the EC and reports events, * syncing at the end. * * @ckdev: The keyboard device. * @ev_type: The input event type (e.g., EV_KEY). * @mask: A bitmap of buttons from the EC. */ static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev, unsigned int ev_type, u32 mask) { … } static int cros_ec_keyb_work(struct notifier_block *nb, unsigned long queued_during_suspend, void *_notify) { … } /* * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by * ghosting logic to ignore NULL or virtual keys. */ static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev) { … } /** * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO * * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and * unmarshalling and different version nonsense into something simple. * * @ec_dev: The EC device * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT. * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH. Actually * in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or * EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver. * @result: Where we'll store the result; a union * @result_size: The size of the result. Expected to be the size of one of * the elements in the union. * * Returns 0 if no error or -error upon error. */ static int cros_ec_keyb_info(struct cros_ec_device *ec_dev, enum ec_mkbp_info_type info_type, enum ec_mkbp_event event_type, union ec_response_get_next_data *result, size_t result_size) { … } /** * cros_ec_keyb_query_switches - Query the state of switches and report * * This will ask the EC about the current state of switches and report to the * kernel. Note that we don't query for buttons because they are more * transitory and we'll get an update on the next release / press. * * @ckdev: The keyboard device * * Returns 0 if no error or -error upon error. */ static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev) { … } /** * cros_ec_keyb_resume - Resume the keyboard * * We use the resume notification as a chance to query the EC for switches. * * @dev: The keyboard device * * Returns 0 if no error or -error upon error. */ static int cros_ec_keyb_resume(struct device *dev) { … } /** * cros_ec_keyb_register_bs - Register non-matrix buttons/switches * * Handles all the bits of the keyboard driver related to non-matrix buttons * and switches, including asking the EC about which are present and telling * the kernel to expect them. * * If this device has no support for buttons and switches we'll return no error * but the ckdev->bs_idev will remain NULL when this function exits. * * @ckdev: The keyboard device * @expect_buttons_switches: Indicates that EC must report button and/or * switch events * * Returns 0 if no error or -error upon error. */ static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev, bool expect_buttons_switches) { … } static void cros_ec_keyb_parse_vivaldi_physmap(struct cros_ec_keyb *ckdev) { … } /** * cros_ec_keyb_register_matrix - Register matrix keys * * Handles all the bits of the keyboard driver related to matrix keys. * * @ckdev: The keyboard device * * Returns 0 if no error or -error upon error. */ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev) { … } static ssize_t function_row_physmap_show(struct device *dev, struct device_attribute *attr, char *buf) { … } static DEVICE_ATTR_RO(function_row_physmap); static struct attribute *cros_ec_keyb_attrs[] = …; static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n) { … } static const struct attribute_group cros_ec_keyb_group = …; __ATTRIBUTE_GROUPS(…); static int cros_ec_keyb_probe(struct platform_device *pdev) { … } static void cros_ec_keyb_remove(struct platform_device *pdev) { … } #ifdef CONFIG_ACPI static const struct acpi_device_id cros_ec_keyb_acpi_match[] = …; MODULE_DEVICE_TABLE(acpi, cros_ec_keyb_acpi_match); #endif #ifdef CONFIG_OF static const struct of_device_id cros_ec_keyb_of_match[] = …; MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match); #endif static DEFINE_SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume); static struct platform_driver cros_ec_keyb_driver = …; module_platform_driver(…) …; MODULE_LICENSE(…) …; MODULE_DESCRIPTION(…) …; MODULE_ALIAS(…) …;