From 5e7b7f52adc133da90ef45303e4fc0eb4b95a489 Mon Sep 17 00:00:00 2001 From: qrsikno2 Date: Mon, 26 May 2025 22:35:30 +0800 Subject: [PATCH 1/7] feat(draft): start up from a skel --- Kbuild | 1 + Makefile | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Kbuild b/Kbuild index b13b9e7e3..13f9a863d 100644 --- a/Kbuild +++ b/Kbuild @@ -44,4 +44,5 @@ obj-$(CONFIG_LUNATIK_FIFO) += lib/luafifo.o obj-$(CONFIG_LUNATIK_XTABLE) += lib/luaxtable.o obj-$(CONFIG_LUNATIK_NETFILTER) += lib/luanetfilter.o obj-$(CONFIG_LUNATIK_COMPLETION) += lib/luacompletion.o +obj-$(CONFIG_LUNATIK_HID) += lib/luahid.o diff --git a/Makefile b/Makefile index 2dae95c3f..ceba7c414 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,8 @@ all: lunatik_sym.h CONFIG_LUNATIK_RCU=m CONFIG_LUNATIK_THREAD=m CONFIG_LUNATIK_FIB=m \ CONFIG_LUNATIK_DATA=m CONFIG_LUNATIK_PROBE=m CONFIG_LUNATIK_SYSCALL=m \ CONFIG_LUNATIK_XDP=m CONFIG_LUNATIK_FIFO=m CONFIG_LUNATIK_XTABLE=m \ - CONFIG_LUNATIK_NETFILTER=m CONFIG_LUNATIK_COMPLETION=m + CONFIG_LUNATIK_NETFILTER=m CONFIG_LUNATIK_COMPLETION=m \ + CONFIG_LUNATIK_HID=m clean: ${MAKE} -C ${MODULES_BUILD_PATH} M=${PWD} clean From 05c86acefe25cd343d0c85ab7ae901df8df660af Mon Sep 17 00:00:00 2001 From: qrsikno2 Date: Mon, 26 May 2025 22:35:55 +0800 Subject: [PATCH 2/7] start from a skel --- lib/luahid.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 lib/luahid.c diff --git a/lib/luahid.c b/lib/luahid.c new file mode 100644 index 000000000..3d8c694c1 --- /dev/null +++ b/lib/luahid.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * HID support for Linux + * + * Copyright (c) 1999 Andreas Gal + * Copyright (c) 2000-2005 Vojtech Pavlik + * Copyright (c) 2005 Michael Haboustak for Concept2, Inc + * Copyright (c) 2007-2008 Oliver Neukum + * Copyright (c) 2006-2012 Jiri Kosina + * Copyright (c) 2012 Henrik Rydberg + */ + +/* + */ + +#include +#include +#include +#include +#include + +#include + +static struct hid_driver hid_generic; + +static int __check_hid_generic(struct device_driver *drv, void *data) +{ + struct hid_driver *hdrv = to_hid_driver(drv); + struct hid_device *hdev = data; + + if (hdrv == &hid_generic) + return 0; + + return hid_match_device(hdev, hdrv) != NULL; +} + +static bool hid_generic_match(struct hid_device *hdev, + bool ignore_special_driver) +{ + if (ignore_special_driver) + return true; + + if (hdev->quirks & HID_QUIRK_IGNORE_SPECIAL_DRIVER) + return true; + + if (hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER) + return false; + + /* + * If any other driver wants the device, leave the device to this other + * driver. + */ + if (bus_for_each_drv(&hid_bus_type, NULL, hdev, __check_hid_generic)) + return false; + + return true; +} + +static int hid_generic_probe(struct hid_device *hdev, + const struct hid_device_id *id) +{ + int ret; + + hdev->quirks |= HID_QUIRK_INPUT_PER_APP; + + ret = hid_parse(hdev); + if (ret) + return ret; + + return hid_hw_start(hdev, HID_CONNECT_DEFAULT); +} + +static const struct hid_device_id hid_table[] = { + { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, HID_ANY_ID, HID_ANY_ID) }, + { } +}; +MODULE_DEVICE_TABLE(hid, hid_table); + +static struct hid_driver hid_generic = { + .name = "hid-generic-another", + .id_table = hid_table, + .match = hid_generic_match, + .probe = hid_generic_probe, +}; +module_hid_driver(hid_generic); + +MODULE_AUTHOR("Henrik Rydberg"); +MODULE_DESCRIPTION("HID generic driver"); +MODULE_LICENSE("GPL"); From 21d65f263bf401904feb5772387cfcacdcd3bdf0 Mon Sep 17 00:00:00 2001 From: qrsikno2 Date: Wed, 28 May 2025 22:08:22 +0800 Subject: [PATCH 3/7] inital version --- lib/luahid.c | 148 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 91 insertions(+), 57 deletions(-) mode change 100644 => 100755 lib/luahid.c diff --git a/lib/luahid.c b/lib/luahid.c old mode 100644 new mode 100755 index 3d8c694c1..2c9d97cba --- a/lib/luahid.c +++ b/lib/luahid.c @@ -1,61 +1,33 @@ -// SPDX-License-Identifier: GPL-2.0-or-later /* - * HID support for Linux - * - * Copyright (c) 1999 Andreas Gal - * Copyright (c) 2000-2005 Vojtech Pavlik - * Copyright (c) 2005 Michael Haboustak for Concept2, Inc - * Copyright (c) 2007-2008 Oliver Neukum - * Copyright (c) 2006-2012 Jiri Kosina - * Copyright (c) 2012 Henrik Rydberg - */ - -/* - */ +* SPDX-FileCopyrightText: (c) 2024 Jieming Zhou +* SPDX-License-Identifier: MIT OR GPL-2.0-only +*/ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include #include +#include #include -#include -#include +// #include +#include #include #include -static struct hid_driver hid_generic; +#include +#include +#include -static int __check_hid_generic(struct device_driver *drv, void *data) -{ - struct hid_driver *hdrv = to_hid_driver(drv); - struct hid_device *hdev = data; +#include - if (hdrv == &hid_generic) - return 0; - - return hid_match_device(hdev, hdrv) != NULL; -} - -static bool hid_generic_match(struct hid_device *hdev, - bool ignore_special_driver) -{ - if (ignore_special_driver) - return true; +#include "luanetfilter.h" - if (hdev->quirks & HID_QUIRK_IGNORE_SPECIAL_DRIVER) - return true; - - if (hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER) - return false; - - /* - * If any other driver wants the device, leave the device to this other - * driver. - */ - if (bus_for_each_drv(&hid_bus_type, NULL, hdev, __check_hid_generic)) - return false; - - return true; -} +typedef struct luahid_s { + lunatik_object_t *runtime; + struct hid_driver driver; +} luahid_t; +//kernel codes static int hid_generic_probe(struct hid_device *hdev, const struct hid_device_id *id) { @@ -69,21 +41,83 @@ static int hid_generic_probe(struct hid_device *hdev, return hid_hw_start(hdev, HID_CONNECT_DEFAULT); } - static const struct hid_device_id hid_table[] = { - { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, HID_ANY_ID, HID_ANY_ID) }, + { HID_USB_DEVICE(0x046D, 0xC542) }, { } }; MODULE_DEVICE_TABLE(hid, hid_table); -static struct hid_driver hid_generic = { - .name = "hid-generic-another", - .id_table = hid_table, - .match = hid_generic_match, - .probe = hid_generic_probe, + +static void luahid_release(void *private) +{ + luahid_t *hid = (luahid_t *)private; + if (hid) { + hid_unregister_driver(&hid->driver); + lunatik_putobject(hid->runtime); + } +} + +static int luahid_register(lua_State *L); + +static const luaL_Reg luahid_lib[] = { + {"register", luahid_register}, + {NULL, NULL} }; -module_hid_driver(hid_generic); -MODULE_AUTHOR("Henrik Rydberg"); -MODULE_DESCRIPTION("HID generic driver"); -MODULE_LICENSE("GPL"); +static const luaL_Reg luahid_mt[] = { + {"__gc", lunatik_deleteobject}, + {NULL, NULL} +}; + +static const lunatik_class_t luahid_class = { + .name = "hid", + .methods = luahid_mt, + .release = luahid_release, + .sleep = false, +}; + +static int luahid_register(lua_State *L) +{ + size_t len = 0; + luaL_checktype(L, 1, LUA_TTABLE); // assure that is a driver + + lunatik_object_t *object = lunatik_newobject(L, &luahid_class, sizeof(luahid_t)); + luahid_t *hid = (luahid_t *)object->private; + + //configure the driver's properties & callbacks + struct hid_driver *user_driver = &(hid->driver); + lua_tolstring(L, -1, &len); + user_driver -> name = lunatik_checkalloc(L, len); + lunatik_setstring(L, 1, user_driver, name, NAME_MAX); + user_driver -> id_table = hid_table; + user_driver -> probe = hid_generic_probe; + + lunatik_registerobject(L, 1, object); + + int ret = __hid_register_driver(user_driver, THIS_MODULE, KBUILD_MODNAME); + if (ret) { + lunatik_unregisterobject(L, object); + luaL_error(L, "failed to register hid driver: %s", user_driver->name); + } + lunatik_setruntime(L, hid, hid); + lunatik_getobject(hid->runtime); + // pass + return 1; /* object */ +} + +LUNATIK_NEWLIB(hid, luahid_lib, &luahid_class, NULL); + +static int __init luahid_init(void) +{ + return 0; +} + +static void __exit luahid_exit(void) +{ +} + +module_init(luahid_init); +module_exit(luahid_exit); +MODULE_LICENSE("Dual MIT/GPL"); +MODULE_AUTHOR("Jieming Zhou "); + From 2e0840acbf9e9faed07934699669dd91bd55a939 Mon Sep 17 00:00:00 2001 From: qrsikno2 Date: Thu, 29 May 2025 04:53:02 +0000 Subject: [PATCH 4/7] bug(hid): fix module problem --- bin/lunatik | 2 +- lib/luahid.c | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/bin/lunatik b/bin/lunatik index 696f79063..790ec8c2e 100755 --- a/bin/lunatik +++ b/bin/lunatik @@ -9,7 +9,7 @@ local lunatik = { device = "/dev/lunatik", modules = {"lunatik", "luadevice", "lualinux", "luanotifier", "luasocket", "luarcu", "luathread", "luafib", "luadata", "luaprobe", "luasyscall", "luaxdp", "luafifo", "luaxtable", - "luanetfilter", "luacompletion", "lunatik_run"} + "luanetfilter", "luacompletion", "lunatik_run", "luahid"} } function lunatik.prompt() diff --git a/lib/luahid.c b/lib/luahid.c index 2c9d97cba..ae7c88d75 100755 --- a/lib/luahid.c +++ b/lib/luahid.c @@ -8,7 +8,6 @@ #include #include #include -// #include #include #include @@ -73,12 +72,11 @@ static const lunatik_class_t luahid_class = { .name = "hid", .methods = luahid_mt, .release = luahid_release, - .sleep = false, + .sleep = true, }; static int luahid_register(lua_State *L) { - size_t len = 0; luaL_checktype(L, 1, LUA_TTABLE); // assure that is a driver lunatik_object_t *object = lunatik_newobject(L, &luahid_class, sizeof(luahid_t)); @@ -86,8 +84,7 @@ static int luahid_register(lua_State *L) //configure the driver's properties & callbacks struct hid_driver *user_driver = &(hid->driver); - lua_tolstring(L, -1, &len); - user_driver -> name = lunatik_checkalloc(L, len); + user_driver -> name = lunatik_checkalloc(L, NAME_MAX); lunatik_setstring(L, 1, user_driver, name, NAME_MAX); user_driver -> id_table = hid_table; user_driver -> probe = hid_generic_probe; From d3f229fd8249321d288e7260ee778856837cfdd4 Mon Sep 17 00:00:00 2001 From: qrsikno2 Date: Fri, 30 May 2025 15:07:41 +0000 Subject: [PATCH 5/7] refractor(headers): move lunatik_setstring from lib/luanetfilter.h to lunatik.h --- lib/luanetfilter.h | 11 ----------- lunatik.h | 11 +++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/luanetfilter.h b/lib/luanetfilter.h index d2ef7ad6b..e2c03d5f8 100644 --- a/lib/luanetfilter.h +++ b/lib/luanetfilter.h @@ -29,17 +29,6 @@ do { \ lua_pop(L, 1); \ } while (0) -#define lunatik_setstring(L, idx, hook, field, maxlen) \ -do { \ - size_t len; \ - lunatik_checkfield(L, idx, #field, LUA_TSTRING); \ - const char *str = lua_tolstring(L, -1, &len); \ - if (len > maxlen) \ - luaL_error(L, "'%s' is too long", #field); \ - strncpy((char *)hook->field, str, maxlen); \ - lua_pop(L, 1); \ -} while (0) - const lunatik_reg_t luanetfilter_family[] = { {"UNSPEC", NFPROTO_UNSPEC}, {"INET", NFPROTO_INET}, diff --git a/lunatik.h b/lunatik.h index 4103bdba0..a6bd8f8a3 100644 --- a/lunatik.h +++ b/lunatik.h @@ -283,6 +283,17 @@ static inline T checker(lua_State *L, int ix) \ #define lunatik_getregistry(L, key) lua_rawgetp((L), LUA_REGISTRYINDEX, (key)) +#define lunatik_setstring(L, idx, hook, field, maxlen) \ +do { \ + size_t len; \ + lunatik_checkfield(L, idx, #field, LUA_TSTRING); \ + const char *str = lua_tolstring(L, -1, &len); \ + if (len > maxlen) \ + luaL_error(L, "'%s' is too long", #field); \ + strncpy((char *)hook->field, str, maxlen); \ + lua_pop(L, 1); \ +} while (0) + static inline void lunatik_setregistry(lua_State *L, int ix, void *key) { lua_pushvalue(L, ix); From a6c2fc0ee294b1855880d58b2bf92d0ea819b4b9 Mon Sep 17 00:00:00 2001 From: qrsikno2 Date: Fri, 30 May 2025 15:08:23 +0000 Subject: [PATCH 6/7] bug(lunatik): add module name in lunatik script --- bin/lunatik | 2 +- lib/luahid.c | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/bin/lunatik b/bin/lunatik index 790ec8c2e..fe57eb49d 100755 --- a/bin/lunatik +++ b/bin/lunatik @@ -9,7 +9,7 @@ local lunatik = { device = "/dev/lunatik", modules = {"lunatik", "luadevice", "lualinux", "luanotifier", "luasocket", "luarcu", "luathread", "luafib", "luadata", "luaprobe", "luasyscall", "luaxdp", "luafifo", "luaxtable", - "luanetfilter", "luacompletion", "lunatik_run", "luahid"} + "luanetfilter", "luacompletion", "luahid", "lunatik_run"} } function lunatik.prompt() diff --git a/lib/luahid.c b/lib/luahid.c index ae7c88d75..dec8f47c6 100755 --- a/lib/luahid.c +++ b/lib/luahid.c @@ -19,8 +19,6 @@ #include -#include "luanetfilter.h" - typedef struct luahid_s { lunatik_object_t *runtime; struct hid_driver driver; From 3a7303499b2edef0a3a7b920d37e578075c18dd2 Mon Sep 17 00:00:00 2001 From: qrsikno2 Date: Fri, 30 May 2025 13:40:56 +0000 Subject: [PATCH 7/7] style(luahid): fix C++ comment style --- lib/luahid.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/luahid.c b/lib/luahid.c index dec8f47c6..24a4be666 100755 --- a/lib/luahid.c +++ b/lib/luahid.c @@ -1,5 +1,5 @@ /* -* SPDX-FileCopyrightText: (c) 2024 Jieming Zhou +* SPDX-FileCopyrightText: (c) 2025 Jieming Zhou * SPDX-License-Identifier: MIT OR GPL-2.0-only */ @@ -24,7 +24,10 @@ typedef struct luahid_s { struct hid_driver driver; } luahid_t; -//kernel codes +/* + * kernel codes copied from drivers/hid/hid-generic.c + * links: https://elixir.bootlin.com/linux/v6.13.7/source/drivers/hid/hid-generic.c + */ static int hid_generic_probe(struct hid_device *hdev, const struct hid_device_id *id) { @@ -75,12 +78,14 @@ static const lunatik_class_t luahid_class = { static int luahid_register(lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); // assure that is a driver + luaL_checktype(L, 1, LUA_TTABLE); /* assure that is a driver */ lunatik_object_t *object = lunatik_newobject(L, &luahid_class, sizeof(luahid_t)); luahid_t *hid = (luahid_t *)object->private; - //configure the driver's properties & callbacks + /* + * configure the driver's properties & callbacks + */ struct hid_driver *user_driver = &(hid->driver); user_driver -> name = lunatik_checkalloc(L, NAME_MAX); lunatik_setstring(L, 1, user_driver, name, NAME_MAX); @@ -96,7 +101,6 @@ static int luahid_register(lua_State *L) } lunatik_setruntime(L, hid, hid); lunatik_getobject(hid->runtime); - // pass return 1; /* object */ }