From nobody Mon Feb 9 07:44:08 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 93E97326927 for ; Tue, 6 Jan 2026 11:29:58 +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=1767699001; cv=none; b=LHQVzfvor9kgcpDWVghMsyUxmvY+0cKaCloW9PtbhzX32uMpfWfNYqlBk3mzHdcNzy0IFssCt1fvRspcrgBomKFSAKW76hBBG+EmU6vgw3tKa72exrnpcVZg4ye7JujTfqGdQV8qv70BWYLHqnBEm2epUEhZD922voYSjeBE0cw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767699001; c=relaxed/simple; bh=S7yjyKlCDH8q87cleFtLMuz9lywljZCqKLq+HLZ63IA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ml+n4r2Lv+5eCSj4uQHUfO3y28oOaSnOwd03S2Ba6GWKy/yYu7EgC/ArpyPolKS6nseCFE3BLOK0OgOps0i9+8OccDHMfEi4mOFK8pxXCsWlO0d2U21eXDAtEuVfpkoMiiwFMoagcubmWmPrjwZ3I7ozzDA1AurKYZvLugSegp0= 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=jxRbd+e5; 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="jxRbd+e5" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-47d63594f7eso5257735e9.0 for ; Tue, 06 Jan 2026 03:29:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1767698997; x=1768303797; 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=+hmZHVE8WQaJfR5ykW+zRucqvzo9/FyMacfU4JQkTmc=; b=jxRbd+e5BDi4td2ylGyQlhRXiwp6eAhfgc4oWh5C0V7e2NYkNbyAWCes7fD2JFwrA3 BPNeS5hZWa82XsT0+kAhYj9hr2y6h2+dCNivkc7lFad8WEzxaH1B4RYout58emqdPwwB Dmjj/waHKH55DYGUVWxQn6HHeP4Gs6EvBDkdw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767698997; x=1768303797; 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=+hmZHVE8WQaJfR5ykW+zRucqvzo9/FyMacfU4JQkTmc=; b=GV1++xTeoqMVwCBJDIMUZeMOGNuFq6gShBLgMqvJXP4DyaQsA8EQpSDB8RJ3j6/v7z /VAqb5+javdYI4bG4NX5iwKVG6FoHCHhYZc/9voyvY13K3UtkbyaGHktat6dqAMLhn8H 5DCB0oRu4ra4b8WjqppAAD40LKTpT9KBcplq89VxEUQiRYkYR92iRK1gmY8tRq/cwYhH FyPC0oAWu5EaA1bUap9OgmeP5TWCJ6MJJcxAR80q4FlPKwibX5dFE62zMc9CQKEsDgTO eo4or7daLONiEZnql+iqql3hpRTb+6mZzZh26E+R1T66sB3Epyy8UPpF4y/TlTC33KNF makA== X-Forwarded-Encrypted: i=1; AJvYcCUukS3oj4EaC+U+Q1Sp5Xp1RMEjML5SYXiaDxYwZiZdiMcEa8Q1e3vuqEMGts3bAYRIdSZltvSw+zACIOM=@vger.kernel.org X-Gm-Message-State: AOJu0YxzJIznwJHqaF4P1ktzN93sdcJcaW+9DY/q9cR+UPaBdQmGn9Ro n4ad04hbRQeDijzPpPSnVEpFTGSOu1u+mn8/+t7fEy3KqlWCD5bqnLrYzV05CAm7MA== X-Gm-Gg: AY/fxX4Y8MPCE+0INhP1mvz0wt0Y0PPPZAJ8lh5IE7gxWuUHsZrvyjed8j7CMXHBBus 6UpMSzGyuDGSgGBrPcFKf2JBJQQWOqDpdHq0403CTy19ZFLqCcQrnGRdVFx3UA0u3DFfTwxEHGv aIQHwfgN7ZIpL4YCinofUqhN98j50XTUsYqq8nfQ9CjCdM1FlN5jM/GcUn4HeHGglqE7SWBkmCT TASefPZ/9gbnzNfCDRBWcxov6MMw+Ua+Q4rPt2cY3k4AfyAPnTodRqXXkWUySg3ct0bKVgPZjVX +iQ/4OZnSpjeqJW+Ya7zsL02NoBjxt0VYQq5YpkwslNgjIQ0fbiRcRc5ugb+bn20SJWA1D5QJ+x djfl9+lCWkT0qaVTeNmoN5aa9a5ZEJOLa4tl1HlfoPuD0xzuczupCrPmClBVYOVKpZA7++cabuL myyB4/vPGZZcik8TxJq6/LsK5fZg== X-Google-Smtp-Source: AGHT+IGwdYiIO3f0W4orc8af84j7Uhz0ms1JygNr19XpIC2gifN7PrRb3cyEXCnsY9AusmwuehfLGg== X-Received: by 2002:a05:600c:3115:b0:477:9f34:17b8 with SMTP id 5b1f17b1804b1-47d7f06cfd2mr33884765e9.1.1767698996863; Tue, 06 Jan 2026 03:29:56 -0800 (PST) Received: from balto-ws ([37.228.206.31]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-432bd5ee243sm3757482f8f.31.2026.01.06.03.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jan 2026 03:29:56 -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 v4 1/1] Input: cros_ec_keyb - add function key support Date: Tue, 6 Jan 2026 11:29:53 +0000 Message-ID: <20260106112953.692304-2-fabiobaltieri@chromium.org> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog In-Reply-To: <20260106112953.692304-1-fabiobaltieri@chromium.org> References: <20260106112953.692304-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 | 174 +++++++++++++++++++++++--- 1 file changed, 158 insertions(+), 16 deletions(-) diff --git a/drivers/input/keyboard/cros_ec_keyb.c b/drivers/input/keyboard= /cros_ec_keyb.c index 1c6b0461dc35..74ac1700b104 100644 --- a/drivers/input/keyboard/cros_ec_keyb.c +++ b/drivers/input/keyboard/cros_ec_keyb.c @@ -29,6 +29,11 @@ =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 +49,11 @@ * @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 use an fn function map layer + * @normal_key_status: active normal keys map + * @fn_key_status: active function keys map + * @fn_key_pressed: tracks the function key status + * @fn_key_triggered: tracks where any function key fired */ struct cros_ec_keyb { unsigned int rows; @@ -61,6 +71,12 @@ struct cros_ec_keyb { struct notifier_block notifier; =20 struct vivaldi_data vdata; + + bool has_fn_map; + u8 normal_key_status[CROS_EC_KEYBOARD_COLS_MAX]; + u8 fn_key_status[CROS_EC_KEYBOARD_COLS_MAX]; + bool fn_key_pressed; + bool fn_key_triggered; }; =20 /** @@ -166,16 +182,104 @@ 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_key_triggered =3D false; + } else if (!ckdev->fn_key_triggered) { + /* + * 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) { + if (code =3D=3D KEY_FN) + return cros_ec_keyb_process_fn_key(ckdev, row, col, state); + + if (!state) { + if (ckdev->fn_key_status[col] & BIT(row)) { + code =3D cros_ec_keyb_fn_code(ckdev, row, col, &pos); + + ckdev->fn_key_status[col] &=3D ~BIT(row); + } else if (ckdev->normal_key_status[col] & BIT(row)) { + ckdev->normal_key_status[col] &=3D ~BIT(row); + } else { + /* Discard, key press code was not sent */ + return; + } + } else if (ckdev->fn_key_pressed) { + code =3D cros_ec_keyb_fn_code(ckdev, row, col, &pos); + + ckdev->fn_key_triggered =3D true; + + if (!code) + return; + + ckdev->fn_key_status[col] |=3D BIT(row); + } else { + ckdev->normal_key_status[col] |=3D BIT(row); + } + } + + 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 +296,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 +679,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 +737,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 +775,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 +790,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.52.0.351.gbe84eed79e-goog