From nobody Thu Apr 2 12:41:27 2026 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.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 31E6C34D4E4 for ; Wed, 11 Feb 2026 17:34:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770831269; cv=none; b=bwS3BfpbBVMH4pcL7Y3LNwTvBbZnKw0zFxTF4U7qiNurFG6EeVsEoxZ8iEvBg159AQkggpVgZ5l4vjeOISRPlKX3iGcvavBw3BGtswWT1G5swcoMyKSvOmhAQZnwJ1dOsUPd+WsisJl8T8vS7hnSWhjnBjm/jrhwx0jwUFRpYVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770831269; c=relaxed/simple; bh=BOtqzINSJkvVF3BJ+I9/SEpyPvXjE1wD/Pgxwmr4TOU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LMANWAXJSV4Xfbhw+jjXIiYg/w+Op8KnbHnopOYpq3PM2SDeYLGQ6aKdS6Uo3yJe0wNS8woLFndes7pukCcgpiuj+8XIDa63afaqd4GXpBJmq0sry/OoQaJNbspW909owEvhEMAsIj2o3P990aRkmZL8CWGW+TT0FNcqQTtrcVo= 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=K4cRNUhO; arc=none smtp.client-ip=209.85.128.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="K4cRNUhO" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4832701b9b7so31532015e9.2 for ; Wed, 11 Feb 2026 09:34:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1770831266; x=1771436066; 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=4NRnArFJ4FK2jOO4FMkxgZZsssccfLs2sPAF2qNLKg8=; b=K4cRNUhOknV6Whnku2Wpi8uY3TAaCiw4iHVT8Uwa/vlGNOKcdO/HH7R4v21mqGTioq tTTGGc3/13adKckDLv2RRUFyAJInIdPHCRPMBQ7SadRXphrVIeMW8CvYpipxkm4f88bm TuXA9jNF9WKkWzy5a7aGXQbhpF82zdyNInLJI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770831266; x=1771436066; 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=4NRnArFJ4FK2jOO4FMkxgZZsssccfLs2sPAF2qNLKg8=; b=ncdcbQIQ7xynMnI79UMWlMhMQ5cSyFTMh/Vd3ZvI0fFKLOqMFG1vMKwYYYUoxBgc9P F3IfCmr3xaWb5kZYe+D6qcnLOSVNDElA1HzX/8DmXkH6IjpZZSkwWw38h/b22W7hSHJ8 o+UxGkX/xkLVl4tOAVFlgEX51rE3I5aNcEzlAwxQQknAwTp2oMn2HceENmVz7nYy6Eec Rzeuq7cuh1p6nI9ANsM4U4mIg/Lqhi8YJ2eXzRuiru054fvMBX52CDYx4a6NqBxMzzRm a5s/8kXjs2ze/f4fkSP34a5U4XxIeB9A7vJiFASvgotDc4PZ3JCQrliEUSuRwA87NhM9 vACA== X-Forwarded-Encrypted: i=1; AJvYcCUt3FR9jEY+tHcr3B5g8Nnuk0JwYGI/BvmGPrEWKUU3OGSajJwxS2ZKa2KvL/r8K0Cutff3NxepgXVaN6A=@vger.kernel.org X-Gm-Message-State: AOJu0YwsGFK4EfVXzV/QoTbUfJhXOfYci5Azc2+YDJBH81+jfyVRTsxB SkOZ15IkZpu3qfUCQB5WL3aShUaouZq8xkiX722UyFj1NU0XqcUhpOasgX17hxZ78A== X-Gm-Gg: AZuq6aKklDjbN6BQzzmgeAsaHAUe3Lvce6JrBJxwT8kyXe5b8/0tiby4lWx8XJyrdU6 Eiy/XfMzg7Mlp5l9pOVHWj8C2IF9sVH9tfB8rYeuIze7VstLeCeR4rR4eusVd2qRtnnib7On9k7 2v+cDJ9M5gVqDwik/dcDr+kpvLpBpYP8k5dzbANzRSZAR2rnjTMag+8Waqt9MpwSYSuLUIiqRZ4 GkQ2rb7U0D4vN+8kttHcUZ7/tYbqr/r8MJwQJTP7jmZDS/IzOH7BNpsWPMB6fX7oYdtDQUSzv4d lAHiNcxfw1U7VMEICkVEKETGzg7ivkpOtAXG/ImulJ86F6cQjOYShZxpt7zeVKk3OB6V5+NFxox mZedZuD4LBksI9Ojb8czNqrQZDDRwtgIDDsaNbcorr3gCao4Id1rPlAQ84wEkEE4hqYZnCQjXNO tYJu0P4HnUZdvDOqVALxKxVrfXwfI= X-Received: by 2002:a05:600c:1389:b0:47e:e779:36d with SMTP id 5b1f17b1804b1-4835b92bd7dmr41041165e9.23.1770831266460; Wed, 11 Feb 2026 09:34:26 -0800 (PST) Received: from balto-ws ([37.228.206.31]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43783d50f24sm5885498f8f.14.2026.02.11.09.34.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Feb 2026 09:34:26 -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 v7 2/2] Input: cros_ec_keyb - add function key support Date: Wed, 11 Feb 2026 17:34:21 +0000 Message-ID: <20260211173421.1206478-3-fabiobaltieri@chromium.org> X-Mailer: git-send-email 2.53.0.239.g8d8fc8a987-goog In-Reply-To: <20260211173421.1206478-1-fabiobaltieri@chromium.org> References: <20260211173421.1206478-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 | 191 +++++++++++++++++++++++--- 1 file changed, 175 insertions(+), 16 deletions(-) diff --git a/drivers/input/keyboard/cros_ec_keyb.c b/drivers/input/keyboard= /cros_ec_keyb.c index 1c6b0461dc35..67022f92533d 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,103 @@ 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) { + int fn_pos; + unsigned int fn_code =3D cros_ec_keyb_fn_code(ckdev, row, col, &fn_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; + pos =3D fn_pos; + } 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; + pos =3D fn_pos; + } + } + + 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 +292,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 +675,63 @@ 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; +} + +/* + * Custom handler for the set keycode ioctl, calls the default handler and + * recomputes has_fn_map. + */ +static int cros_ec_keyb_setkeycode(struct input_dev *idev, + const struct input_keymap_entry *ke, + unsigned int *old_keycode) +{ + struct cros_ec_keyb *ckdev =3D input_get_drvdata(idev); + int ret; + + ret =3D input_default_setkeycode(idev, ke, old_keycode); + if (ret) + return ret; + + ckdev->has_fn_map =3D cros_ec_keyb_has_fn_map(ckdev); + + return 0; +} + /** * cros_ec_keyb_register_matrix - Register matrix keys * @@ -603,6 +753,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; @@ -631,11 +787,12 @@ static int cros_ec_keyb_register_matrix(struct cros_e= c_keyb *ckdev) idev->id.version =3D 1; idev->id.product =3D 0; idev->dev.parent =3D dev; + idev->setkeycode =3D cros_ec_keyb_setkeycode; =20 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 +807,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.239.g8d8fc8a987-goog