From nobody Tue Feb 10 14:32:13 2026 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0516D37FF7A for ; Mon, 9 Feb 2026 15:51:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770652293; cv=none; b=iMFrInm/DeKgEsLR8nP5wtYcbcYnQxcZ8CmKYVYV5NJYvHtwxWfnVN78eD/t+XioLEQuqR216ozUrXAs2UTj2/9bDWcBN5dMnN9eNsKsIpRm9h5hTO12hCqmUZSZ96gzr8yqi423ObuBGjqvKN22L0RxzC2T6TKYqBr1bgt1SoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770652293; c=relaxed/simple; bh=ZO9cUQryCpH6PJYir0nq665tykkoM1zKVnJWClAzMcs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q42zfpiN1C1GK0AO/+c181Rdtxqq/NWcc8Y0DAgwjarMI7MjyO+vvALowVr2kYyhWaYG/9B/DQmLKkBlQ/f1vHapPo9RLCo6lWMm/Z/f4TlKCXFBpBb/UmtDnpaqn2KMWhI+mrkevh3XhjVXWoGbRVSDkJ1/qmtWFTiZyCSnrkA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=hVAwcwXB; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="hVAwcwXB" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-483487335c2so5584455e9.2 for ; Mon, 09 Feb 2026 07:51:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1770652291; x=1771257091; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+icTVFVT5ydEiDM14pFQOsLqB2MXILYZm42sz+o1Ids=; b=hVAwcwXBHbHf4iphPcJsQN1q3A6fG991ojPeitgcP6bKVTj1+qnkqiXfl2jd7a6PMb JWBSVYDUEj5aNfRq+GJeQ6/TzCXDOqrNRzy7tbHV4thEatMlIB+bZkpg83Fyy+quNq+1 oUfSvPlAJLtMmTicElTaa5tvpdczLHRwwfoxc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770652291; x=1771257091; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=+icTVFVT5ydEiDM14pFQOsLqB2MXILYZm42sz+o1Ids=; b=kYYp0NGW/Y5O3kxI9eKEcS+CXsF7gNpdGvH6gSKsqVuQIe2zFTyDAXeSEX51sYFbEB NuFwfI/nBQlAnKqBE8FWWrkvdhxW5uasey/Sl9ozVmx9HNHCRlI/7nHXu4rekBNnoUYT fPaACAJjBwH05y6tr+I5rovoGdmq8qRal5kx4fiGKCrPIKmnSWrLAicB14MLs2myER9d ay8rbKKbH2+GWPZaQgn5D+lC9owH4xb/OLOvARsl9C7KLu397oxSf9dCC0PFpYDgHbjI AZq/FEUPXQAlDXOJJO6Cr9Y1buEUZG2qG7BTdTM7xbRJR0mur7u2VrHDdgCCU5jgcMvY 0K/Q== X-Forwarded-Encrypted: i=1; AJvYcCWtHMYzc+USqYc1BmLvGtC0KquWj/4ar7axya+2VRzBYnMsTzyg/t60b1gSVOkIkyBnFgcNePpb0th04H0=@vger.kernel.org X-Gm-Message-State: AOJu0YxXyVgUBOY48szKCVvCQBFPIXulvr85Ihampwbmk2TqHJ1YnQ9r TTTaO+nHvIsi04dU7caGkAQhthosOSpQ+ON0YR8OROqsDhtpAzPn0bdLUEMjQ8Fk8A== X-Gm-Gg: AZuq6aJclNZTIDT9b+K4g8Rsvbas3M1oHPkUzOff1+P1KXV7htkFLVCndET6GnX/+qy 3NZ6BiWDt8riGwL8kw9vjyVgRCKGBhlcdxGPQF1YBijbJdN6+RUbtSKBNO9pwnZiOnBkUjnsyi3 AeBCWShM3xIWhblk6SeAdF8iIug0KnGXheBHwODVu9Y2H8I8Jxm5Eh2l0QopAwtf/RWPgsLsdtV nmWkgHO0EHR+Yg+cKoRgydqZe60o/lb++nEeCs5aMY4uA/17yrtGUbCg/q2EvyE1TSGx4ql8D3P w6lfUGXcsTdDM2HffLGbpqzfwnjyf9OiD+BM85xnCFjSLe04MDnGwcAVvIadYzG1DGxiul8j5ka Bs1zOlETHxNfE3ZCh8FoYbI9ZPqy3yLe6FmH97L6GbzpktEqhZmnZIvp6pN/T8aU9yDi8nDjH02 qeqGOucu4uaWHE+8Y= X-Received: by 2002:a05:600c:1388:b0:483:a21:774c with SMTP id 5b1f17b1804b1-4832097808dmr166958595e9.25.1770652291271; Mon, 09 Feb 2026 07:51:31 -0800 (PST) Received: from balto-ws ([37.228.206.31]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48317d7a924sm501929835e9.10.2026.02.09.07.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Feb 2026 07:51:30 -0800 (PST) From: Fabio Baltieri To: Dmitry Torokhov , Benson Leung , Guenter Roeck Cc: Fabio Baltieri , Tzung-Bi Shih , Simon Glass , linux-input@vger.kernel.org, chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v6 1/1] Input: cros_ec_keyb - add function key support Date: Mon, 9 Feb 2026 15:51:27 +0000 Message-ID: <20260209155127.158813-2-fabiobaltieri@chromium.org> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog In-Reply-To: <20260209155127.158813-1-fabiobaltieri@chromium.org> References: <20260209155127.158813-1-fabiobaltieri@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for handling an Fn button and sending separate keycodes for a subset of keys in the matrix defined in the upper half of the keymap. Signed-off-by: Fabio Baltieri Reviewed-by: Simon Glass --- drivers/input/keyboard/cros_ec_keyb.c | 167 +++++++++++++++++++++++--- 1 file changed, 151 insertions(+), 16 deletions(-) diff --git a/drivers/input/keyboard/cros_ec_keyb.c b/drivers/input/keyboard= /cros_ec_keyb.c index 1c6b0461dc35..cda56270cac8 100644 --- a/drivers/input/keyboard/cros_ec_keyb.c +++ b/drivers/input/keyboard/cros_ec_keyb.c @@ -29,6 +29,12 @@ =20 #include =20 +/* + * Maximum size of the normal key matrix, this is limited by the host comm= and + * key_matrix field defined in ec_response_get_next_data_v3 + */ +#define CROS_EC_KEYBOARD_COLS_MAX 18 + /** * struct cros_ec_keyb - Structure representing EC keyboard device * @@ -44,6 +50,9 @@ * @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 + * @has_fn_map: whether the driver uses an fn function-map layer + * @fn_key_pressed: tracks the function key status + * @fn_event_pending: tracks where any function key fired */ struct cros_ec_keyb { unsigned int rows; @@ -61,6 +70,10 @@ struct cros_ec_keyb { struct notifier_block notifier; =20 struct vivaldi_data vdata; + + bool has_fn_map; + bool fn_key_pressed; + bool fn_event_pending; }; =20 /** @@ -166,16 +179,100 @@ static bool cros_ec_keyb_has_ghosting(struct cros_ec= _keyb *ckdev, uint8_t *buf) return false; } =20 +/* + * Process a function key state change, send an event report if appropriat= e. + */ +static void cros_ec_keyb_process_fn_key(struct cros_ec_keyb *ckdev, + int row, int col, bool state) +{ + struct input_dev *idev =3D ckdev->idev; + int pos =3D MATRIX_SCAN_CODE(row, col, ckdev->row_shift); + + ckdev->fn_key_pressed =3D state; + + if (state) { + ckdev->fn_event_pending =3D false; + } else if (!ckdev->fn_event_pending) { + /* + * Send the original code if nothing else has been pressed + * together with Fn. + */ + input_event(idev, EV_MSC, MSC_SCAN, pos); + input_report_key(idev, KEY_FN, true); + input_sync(idev); + + input_event(idev, EV_MSC, MSC_SCAN, pos); + input_report_key(idev, KEY_FN, false); + } +} + +/* + * Return the Fn code for a normal key row, col combination, optionally se= t a + * position code too. + */ +static unsigned int cros_ec_keyb_fn_code(struct cros_ec_keyb *ckdev, + int row, int col, int *pos) +{ + struct input_dev *idev =3D ckdev->idev; + const unsigned short *keycodes =3D idev->keycode; + int fn_pos =3D MATRIX_SCAN_CODE(row + ckdev->rows, col, ckdev->row_shift); + + if (pos) + *pos =3D fn_pos; + + return keycodes[fn_pos]; +} + +/* + * Process the new state for a single key. + */ +static void cros_ec_keyb_process_one(struct cros_ec_keyb *ckdev, + int row, int col, bool state) +{ + struct input_dev *idev =3D ckdev->idev; + const unsigned short *keycodes =3D idev->keycode; + int pos =3D MATRIX_SCAN_CODE(row, col, ckdev->row_shift); + unsigned int code =3D keycodes[pos]; + + dev_dbg(ckdev->dev, "changed: [r%d c%d]: byte %02x\n", row, col, state); + + if (ckdev->has_fn_map) { + unsigned int fn_code =3D cros_ec_keyb_fn_code(ckdev, row, col, &pos); + + if (code =3D=3D KEY_FN) + return cros_ec_keyb_process_fn_key(ckdev, row, col, state); + + if (!state) { + if (test_bit(fn_code, idev->key)) { + code =3D fn_code; + } else if (test_bit(code, idev->key)) { + /* nothing to do */ + } else { + /* Discard, key press code was not sent */ + return; + } + } else if (ckdev->fn_key_pressed) { + ckdev->fn_event_pending =3D true; + + if (!fn_code) + return; + + code =3D fn_code; + } + } + + input_event(idev, EV_MSC, MSC_SCAN, pos); + input_report_key(idev, code, state); +} =20 /* * 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) + * press/release events accordingly. The keyboard state is one byte + * per column. */ static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev, uint8_t *kb_state, int len) { - struct input_dev *idev =3D ckdev->idev; int col, row; int new_state; int old_state; @@ -192,20 +289,13 @@ static void cros_ec_keyb_process(struct cros_ec_keyb = *ckdev, =20 for (col =3D 0; col < ckdev->cols; col++) { for (row =3D 0; row < ckdev->rows; row++) { - int pos =3D MATRIX_SCAN_CODE(row, col, ckdev->row_shift); - const unsigned short *keycodes =3D idev->keycode; - new_state =3D kb_state[col] & (1 << row); old_state =3D ckdev->old_kb_state[col] & (1 << row); - if (new_state !=3D old_state) { - dev_dbg(ckdev->dev, - "changed: [r%d c%d]: byte %02x\n", - row, col, new_state); - - input_event(idev, EV_MSC, MSC_SCAN, pos); - input_report_key(idev, keycodes[pos], - new_state); - } + + if (new_state =3D=3D old_state) + continue; + + cros_ec_keyb_process_one(ckdev, row, col, new_state); } ckdev->old_kb_state[col] =3D kb_state[col]; } @@ -582,6 +672,43 @@ static void cros_ec_keyb_parse_vivaldi_physmap(struct = cros_ec_keyb *ckdev) ckdev->vdata.num_function_row_keys =3D n_physmap; } =20 +/* Returns true if there is a KEY_FN code defined in the normal keymap */ +static bool cros_ec_keyb_has_fn_key(struct cros_ec_keyb *ckdev) +{ + struct input_dev *idev =3D ckdev->idev; + const unsigned short *keycodes =3D idev->keycode; + + for (int row =3D 0; row < ckdev->rows; row++) { + for (int col =3D 0; col < ckdev->cols; col++) { + int pos =3D MATRIX_SCAN_CODE(row, col, ckdev->row_shift); + + if (keycodes[pos] =3D=3D KEY_FN) + return true; + } + } + + return false; +} + +/* + * Returns true if there is a KEY_FN defined and at least one key in the fn + * layer keymap + */ +static bool cros_ec_keyb_has_fn_map(struct cros_ec_keyb *ckdev) +{ + if (!cros_ec_keyb_has_fn_key(ckdev)) + return false; + + for (int row =3D 0; row < ckdev->rows; row++) { + for (int col =3D 0; col < ckdev->cols; col++) { + if (cros_ec_keyb_fn_code(ckdev, row, col, NULL) !=3D 0) + return true; + } + } + + return false; +} + /** * cros_ec_keyb_register_matrix - Register matrix keys * @@ -603,6 +730,12 @@ static int cros_ec_keyb_register_matrix(struct cros_ec= _keyb *ckdev) if (err) return err; =20 + if (ckdev->cols > CROS_EC_KEYBOARD_COLS_MAX) { + dev_err(dev, "keypad,num-columns too large: %d (max: %d)\n", + ckdev->cols, CROS_EC_KEYBOARD_COLS_MAX); + return -EINVAL; + } + ckdev->valid_keys =3D devm_kzalloc(dev, ckdev->cols, GFP_KERNEL); if (!ckdev->valid_keys) return -ENOMEM; @@ -635,7 +768,7 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_= keyb *ckdev) ckdev->ghost_filter =3D device_property_read_bool(dev, "google,needs-ghost-filter"); =20 - err =3D matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols, + err =3D matrix_keypad_build_keymap(NULL, NULL, ckdev->rows * 2, ckdev->co= ls, NULL, idev); if (err) { dev_err(dev, "cannot build key matrix\n"); @@ -650,6 +783,8 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_= keyb *ckdev) cros_ec_keyb_compute_valid_keys(ckdev); cros_ec_keyb_parse_vivaldi_physmap(ckdev); =20 + ckdev->has_fn_map =3D cros_ec_keyb_has_fn_map(ckdev); + err =3D input_register_device(ckdev->idev); if (err) { dev_err(dev, "cannot register input device\n"); --=20 2.53.0.rc2.204.g2597b5adb4-goog