hidModule
Writes a HID driver in Lua: it matches devices by id_table, and can fix a
device’s report descriptor and rewrite its raw reports.
lunatik run -c softirq <script>, other contexts raise
runtime context mismatch, and the callbacks, which run under that runtime’s
lock, must not sleep. The driver stays registered until its stop or the end of
the runtime.
See examples/gesture and examples/xiaomi.
Usage
-- run with `lunatik run -c softirq <script>`
local hid = require("hid")
local function raw_event(driver, hdev, report, raw)
print(hdev.name, report.id, #raw)
end
hid.register({
name = "luahid_trace",
id_table = {{bus = 0x05, vendor = 0x2717, product = 0x5014}},
raw_event = raw_event,
})
Class hid_driver
| hid_driver:stop () | Unregisters the driver. |
hid
| register (opts) | Registers a new HID driver. |
Class hid_driver in detail
- hid_driver:stop ()
-
Unregisters the driver.
The devices it drives are released from it, and no callback of it runs again. Calling it
again does nothing, and a to-be-closed variable holding the driver stops it the same way.
In the script body, the call delivers
removefor each device the driver holds, on the calling task. A device it releases that another driver of the runtime matches gets no probe there: it stays without a driver until it is plugged again or the script stops. A stop from a finalizer at the runtime’s close delivers nothing.Returns
-
nil
Raises
“not allowed once the runtime is armed” past the script body: hid_unregister_driver sleeps, and an armed runtime runs under its spinlock; “not allowed inside a replay” from a callback hid.register or stop delivers inside the call, as hid.register raises itUsage
driver:stop()
hid in detail
- register (opts)
-
Registers a new HID driver.
The
optstable is the driver: each callback it carries receives it as its first argument, and one it does not carry does nothing.Parameters
- opts
table
driver options:
name(string),id_table(array of device ID tables, each with optional integer fieldsbusandgroup, from 0 to0xffff,vendorandproduct, from 0 to0xffffffff, anddriver_data, from 0), and the optional callbacks:-
probe(driver, hdev, id): a device matched;hdevis the device’s table, made here and handed to every later callback of the device, andidis the matching entry, withbus,group,vendor,productanddriver_data. Returning a negative errno,-errno.ENODEVsay, with linux.errno, fails the probe with it; an error, or a return that is neither nothing, zero nor an errno a user program is given, the kernel’s own fromERESTARTSYSon, logged asinvalid errno, fails it withECANCELED. -
report_fixup(driver, hdev, rdesc):rdescis a data over the report descriptor, edited in place, of fixed size and valid only during the call. -
raw_event(driver, hdev, report, raw):rawis a data over the report, edited in place and valid only during the call. Returning nothing or zero passes the report on and a negative errno drops it; an error, or any other return, drops it withECANCELED. -
remove(driver, hdev): the device left the driver, or the HID core failed its probe after probe returned, so each probe that returned gets oneremove; a device the driver still holds when its runtime stops gets none.
hdevcarriesbus,group,vendor,product,versionandname, and keeps what the callbacks store in it;reportcarriesid, type,size,applicationandmaxfield. Whatreport_fixupandremovereturn is ignored, and a callback’s error goes to the kernel log.A device on the bus that
id_tablematches and thathid-genericor no driver holds gets its callbacks inside the call, on the calling task, as one plugged in after the body returns would: probe, itsreport_fixup, andremovewhen the HID core fails the probe after probe returned. A script that tells the two apart uses a flag it clears once hid.register returns. This holds whatever asynchronous probing the kernel command line or the module’s parameters ask for. A device the HID core probes on another task while the body runs, one plugged in at that moment, is refused, and stays without a driver until it is plugged again or the script stops. -
Returns
-
hid_driver
the driver, which hid.register keeps for its runtime: dropping it stops
nothing, and the driver stays registered until stop or the end of the runtime
Raises
“not allowed once the runtime is armed” past the script body; from a percpu runtime; if required fields are missing, orid_tableis invalid or too long; the kernel’s errno if it refuses the driver,EBUSYfor a name another driver holds on the bus;bad field '<field>' (number expected, got <type>)if an entry’sbus,group,vendor,productordriver_datais present and not a number, andbad field '<field>' (out of bounds)if it is past its range;bad field '<field>' (function expected, got <type>)if probe,report_fixup,raw_eventorremoveis present and not a function;runtime context mismatchunless the runtime is softirq;not allowed while the runtime closesfrom a finalizer that runs at its close;not allowed inside a replayfrom a callback hid.register or stop delivers inside the call, or a coroutine it runs, since that callback holds the device lock a registration takes - opts
table
driver options: