From nobody Mon Feb 9 05:20:48 2026 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 13531267B92 for ; Mon, 12 Jan 2026 09:33:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768210399; cv=none; b=koPSS5g+jcc8x2fnLEJe7leZF4sliOeWa4B8lhyHWUIj5yjZ8xhQcZ/tzHD1tawM76qYH2y+0uhJYZH+eRowGjNwQ/3aDvMvcI08ww01Jy7YFzJI7TMQuS2rTMzscrjwupfiYObwQSrQ6rYSJnjW/+lOy8y1Q2u4M1a7B5E7eyM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768210399; c=relaxed/simple; bh=h0BsuMOdFIZFpm8SL89CVB40l/jUixNjdg0sAkfN5/g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ev5g6xdWYxbefp49xQFguhRpzzI8coO2WhMwNhwFfdI3HBrSp1WNVSHuvUj5yvpqZsan8X4kjjvUJ3FrTuxRTxys4i6qVRk9/gWsCfiomrHXfb5de3LIa0Pw39Ckemxg7zikYCbX6OE/+oSh/uE8qoftKyVq9fIeysJFEhznkS4= 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=Rrxja8tx; arc=none smtp.client-ip=209.85.221.47 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="Rrxja8tx" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-42fb0fc5aa9so2779702f8f.1 for ; Mon, 12 Jan 2026 01:33:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1768210394; x=1768815194; 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=nl3ngVJNLpXQU2SZMuSDLRvJaqSe1mw8ao7+H3/RXRg=; b=Rrxja8txwWFb/Xu8Jl+UlzdqhvvwIPueUcqSV4RWZC/T9v4WPeXdQb7qC9ReNN6I/V IdQSV7GM+BHicybKNyihx8boZmzPcbbLRYOetOtEaPO19UX/+WtQZPx5ew0U47hyTwH6 PgEokJySeibbycqTsxIuDvmI2zpAtCCLrGrsA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768210394; x=1768815194; 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=nl3ngVJNLpXQU2SZMuSDLRvJaqSe1mw8ao7+H3/RXRg=; b=ech2OJpnShBN6c8Pbzu5IsA5UvUxbB6WAOUiNvAjhlGyxluR1PNNBVQs9WToN982PM OyWzPUamxv+7krVFFKLc1EsZlJlXFzwNINIg9o4dG+ohCafxEQXv1QCwkWK8XXyZnyYU CgBgolRN9dh1hnWcIu/7Gl2f9tSjobJh3vSzaWu2ns8KAGK31B60oyQns5y6UJ3a/2Mp TPyzXDX60CgOZne3PQvCeM5/SYlZI0s9O+DMENICQCCsfbb3iAuFsPYlU3OuNLV5Bg4L 5Li79bYRVp83nC4v8v7WvlSVb4aS9f5jgGUM925MDUEOcw9rBIP9pERMivYmsZJIMlqy ILlA== X-Forwarded-Encrypted: i=1; AJvYcCV2Qf3PnhdjjLkL5lCGiHLe3POrsGWFemJ1AcPa//P/OTG7B6GoPU0kQPxcRUZsuRLgPZRjoP5GBubkhuw=@vger.kernel.org X-Gm-Message-State: AOJu0Yzj2X9thrr095aTQf6S198CQW4DbK/zxdaMFVzG5setaL9AbFQK XNl130x6nlOfGQxVCGCJWaNUmYuQ9NFuWVQAxouxUGB8nbW90El2dZp11NNDRuePsoxnlyh7pG6 dnVk= X-Gm-Gg: AY/fxX41CxhHKL2m4QoyCTcV4ZK3hMIUeDfqulWqdHL54gv8cPWAQkby2go2lePwJuB W7nfl2FqMxT2l0Qucrx4BVxeJlm6nvMa7ILnJPnfyyN9TVHNt7+wZCCcPLy+r/vS43o85BAFwLJ 89IS8oW4KB03BW1O1vq7MAgy9LCtd9z/fnqlZgyl8jJLnD4tAci9xhC6JXsEEQR2nbagZ3+XDMl /fraAOZD7pQnXTK4OFKQPblaRgCjwDkiObkj1qG4zuo5l9bKZZ0q0DM1oMXz9YzP/VffSFZ+UYQ sFQ2cTAiL33Jk+mWl7r35ryEoHN9uBHXZ2qIHYWJaRqsAQVRJBgiYLy/+eEo353OV7sTLVtGt4t ACXmBUuH1Aol27UyGyJ4hECA8Wdv/pW3q/p7M8b2sz37AxpZDssMoy6mmnvwh37bAn3kSxoS9tW 84Z1CbBIbbV3dkLj4= X-Google-Smtp-Source: AGHT+IGHjojzoarfyqXZHU/dDA0FLz8AS1GIB0FZy7guqzhdsULMLXZSHc9FSdB6QWLMh3bSuBq0CA== X-Received: by 2002:a05:6000:420c:b0:431:67d:5390 with SMTP id ffacd0b85a97d-432c37757demr24039697f8f.54.1768210394411; Mon, 12 Jan 2026 01:33:14 -0800 (PST) Received: from balto-ws ([37.228.206.31]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-432bd0e16ffsm38608881f8f.12.2026.01.12.01.33.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jan 2026 01:33:13 -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 v5 1/1] Input: cros_ec_keyb - add function key support Date: Mon, 12 Jan 2026 09:33:09 +0000 Message-ID: <20260112093309.240905-2-fabiobaltieri@chromium.org> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog In-Reply-To: <20260112093309.240905-1-fabiobaltieri@chromium.org> References: <20260112093309.240905-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..93540f0c5a33 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 uses 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.457.g6b5491de43-goog