From: Igor Putko <igorpetindev@gmail.com>
To: gregkh@linuxfoundation.org, marvin24@gmx.de
Cc: linux-staging@lists.linux.dev, linux-tegra@vger.kernel.org,
ac100@lists.launchpad.net, linux-kernel@vger.kernel.org,
Igor Putko <igorpetindev@gmail.com>
Subject: [PATCH] staging: nvec: kbd: replace global state with dynamic allocation
Date: Fri, 10 Jul 2026 22:12:21 +0300 [thread overview]
Message-ID: <20260710191221.2820-1-igorpetindev@gmail.com> (raw)
Remove the global static 'keys_dev' struct to allow multiple device
instances. Allocate 'nvec_keys' dynamically per-device using
devm_kzalloc() during probe, and fetch the context using
input_get_drvdata() and container_of().
Signed-off-by: Igor Putko <igorpetindev@gmail.com>
---
drivers/staging/nvec/nvec_kbd.c | 40 ++++++++++++++++++++-------------
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/drivers/staging/nvec/nvec_kbd.c b/drivers/staging/nvec/nvec_kbd.c
index d2b91318f..4b61aebca 100644
--- a/drivers/staging/nvec/nvec_kbd.c
+++ b/drivers/staging/nvec/nvec_kbd.c
@@ -35,19 +35,17 @@ struct nvec_keys {
bool caps_lock;
};
-static struct nvec_keys keys_dev;
-
-static void nvec_kbd_toggle_led(void)
+static void nvec_kbd_toggle_led(struct nvec_keys *keys)
{
char buf[] = { NVEC_KBD, SET_LEDS, 0 };
- keys_dev.caps_lock = !keys_dev.caps_lock;
+ keys->caps_lock = !keys->caps_lock;
- if (keys_dev.caps_lock)
+ if (keys->caps_lock)
/* should be BIT(0) only, firmware bug? */
buf[2] = BIT(0) | BIT(1) | BIT(2);
- nvec_write_async(keys_dev.nvec, buf, sizeof(buf));
+ nvec_write_async(keys->nvec, buf, sizeof(buf));
}
static int nvec_keys_notifier(struct notifier_block *nb,
@@ -55,6 +53,7 @@ static int nvec_keys_notifier(struct notifier_block *nb,
{
int code, state;
unsigned char *msg = data;
+ struct nvec_keys *keys = container_of(nb, struct nvec_keys, notifier);
if (event_type == NVEC_KB_EVT) {
int _size = (msg[0] & (3 << 5)) >> 5;
@@ -70,11 +69,11 @@ static int nvec_keys_notifier(struct notifier_block *nb,
state = msg[1] & 0x80;
if (code_tabs[_size][code] == KEY_CAPSLOCK && state)
- nvec_kbd_toggle_led();
+ nvec_kbd_toggle_led(keys);
- input_report_key(keys_dev.input, code_tabs[_size][code],
+ input_report_key(keys->input, code_tabs[_size][code],
!state);
- input_sync(keys_dev.input);
+ input_sync(keys->input);
return NOTIFY_STOP;
}
@@ -85,7 +84,7 @@ static int nvec_keys_notifier(struct notifier_block *nb,
static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
unsigned int code, int value)
{
- struct nvec_chip *nvec = keys_dev.nvec;
+ struct nvec_keys *keys = input_get_drvdata(dev);
char buf[] = { NVEC_KBD, SET_LEDS, 0 };
if (type == EV_REP)
@@ -98,7 +97,7 @@ static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
return -1;
buf[2] = !!value;
- nvec_write_async(nvec, buf, sizeof(buf));
+ nvec_write_async(keys->nvec, buf, sizeof(buf));
return 0;
}
@@ -107,6 +106,7 @@ static int nvec_kbd_probe(struct platform_device *pdev)
{
struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
int i, j, err;
+ struct nvec_keys *keys;
struct input_dev *idev;
char clear_leds[] = { NVEC_KBD, SET_LEDS, 0 },
enable_kbd[] = { NVEC_KBD, ENABLE_KBD },
@@ -115,6 +115,9 @@ static int nvec_kbd_probe(struct platform_device *pdev)
true };
j = 0;
+ keys = devm_kzalloc(&pdev->dev, sizeof(struct nvec_keys), GFP_KERNEL);
+ if (!keys)
+ return -ENOMEM;
for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
keycodes[j++] = code_tab_102us[i];
@@ -138,14 +141,18 @@ static int nvec_kbd_probe(struct platform_device *pdev)
set_bit(keycodes[i], idev->keybit);
clear_bit(0, idev->keybit);
+
+ input_set_drvdata(idev, keys);
+ platform_set_drvdata(pdev, keys);
+
err = input_register_device(idev);
if (err)
return err;
- keys_dev.input = idev;
- keys_dev.notifier.notifier_call = nvec_keys_notifier;
- keys_dev.nvec = nvec;
- nvec_register_notifier(nvec, &keys_dev.notifier, 0);
+ keys->input = idev;
+ keys->notifier.notifier_call = nvec_keys_notifier;
+ keys->nvec = nvec;
+ nvec_register_notifier(nvec, &keys->notifier, 0);
/* Enable keyboard */
nvec_write_sync(nvec, enable_kbd, 2, NULL);
@@ -165,12 +172,13 @@ static int nvec_kbd_probe(struct platform_device *pdev)
static void nvec_kbd_remove(struct platform_device *pdev)
{
struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
+ struct nvec_keys *keys = platform_get_drvdata(pdev);
char disable_kbd[] = { NVEC_KBD, DISABLE_KBD },
uncnfg_wake_key_reporting[] = { NVEC_KBD, CNFG_WAKE_KEY_REPORTING,
false };
nvec_write_async(nvec, uncnfg_wake_key_reporting, 3);
nvec_write_async(nvec, disable_kbd, 2);
- nvec_unregister_notifier(nvec, &keys_dev.notifier);
+ nvec_unregister_notifier(nvec, &keys->notifier);
}
static struct platform_driver nvec_kbd_driver = {
--
2.47.3
reply other threads:[~2026-07-10 19:12 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260710191221.2820-1-igorpetindev@gmail.com \
--to=igorpetindev@gmail.com \
--cc=ac100@lists.launchpad.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-staging@lists.linux.dev \
--cc=linux-tegra@vger.kernel.org \
--cc=marvin24@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox