HID: asus: add support for ROG Ally handhelds
Denis Benato
maintainerv11 cover HID: asus: add support for ROG Ally handhelds
Hi all,
This patch series adds support for ROG Ally handhelds in the HID driver for ASUS devices.
The changes include reinitialization of the device after exiting a sleep state, gamepad configuration, vibration strength configuration, joysticks and triggers range configuration, anti-deadzone configuration, response curve support, force feedback support, gamepad mode support, turbo buttons support, and button remapping support.
This driver is based on the original Luke's work, which was a great starting point and I have reworked it to be upstreamable, while keeping the original functionality intact and extending hid-asus to avoid regressions in functionality such as the backlight control.
The ally support has been a surprisingly long task, spanning multiple years and being used in Steam OS and well as other distributions for quite a while, and I am happy to finally be submitting it.
At this time LEDs are not part of the patch series as I have work in progress to unify LEDs handling across all ASUS devices, and I will submit that in a separate patch series when the userspace interface will be ready to express capabilities of these devices, but that will take some more time.
This driver is the culmination of a lot of work, from many different people, and I want to thank Luke for his original work, as well as the many people that have helped me test and provide feedback on the driver: Khamunetri, Jonathan, Derek, Matthew and others that have helped during these years.
Best regards,
Denis Benato
Link v1: https://lore.kernel.org/all/[email protected]/
Link v2: https://lore.kernel.org/all/[email protected]/
Link v3: https://lore.kernel.org/all/[email protected]/
Link v4: https://lore.kernel.org/all/[email protected]/
Link v5: https://lore.kernel.org/all/[email protected]/
Link v6: https://lore.kernel.org/all/[email protected]/
Link v7: https://lore.kernel.org/all/[email protected]/
Link v8: https://lore.kernel.org/all/[email protected]/
Link v9: https://lore.kernel.org/all/[email protected]/
Link v10: https://lore.kernel.org/all/[email protected]/
Version v11 has been more involved and I focused on two main things: extending the xg mobile (which the ROG ally is one of the few models that supports it) and properly handling hid packet sizes because it has been a constant source of issues and fight in the past and I want to put an end to both of those.
I kindly ask Rebecca and Paolo to try using the new asus_report_len() to see if its also suitable for theirs Zenbook Duo devices and if so to adopt it in theirs patches as well.
Link of a relevant patchset: https://lore.kernel.org/all/[email protected]/
Version v10 drops a patch already merged as part of XG mobile and applies the patchset on top of for-next/7.4.
With v8 the driver does sysfs_notify after successfully changing configuration attributes so that userspace can be notified of the changes immediately.
With v7 the series is rebased on top of the asus tree: the first patch of v6 has been applied upstream as 8099f9da59f1 ("HID: asus: do not send keyboard init reports to touchpads"), so this series is now 12 patches.
The probe paths of the ally interfaces now reserve the interface role while their initialization is running, the configuration attributes work from the moment their files exist and a failed initialization rolls the configuration back instead of leaving the created sysfs groups behind. The response curve is no longer configured entry by entry: a single attribute per joystick now takes the whole curve in one validated write, and a companion index file documents what each value is.
With v5 I returned to the original manual management of memory and sysfs groups as going devm did not play well with the general "plugin" architecture of the ally portion of the driver and has caused more bugs than it solved: sashiko-bot will have to come to terms with hand-managed memory this time.
Changelog:
-v2
- HID: asus: add support for ROG Ally handhelds
- release all vendor buttons on resume
- align arguments of hid_asus_ally_raw_event()
- HID: asus: add gamepad configuration
- clean up comments and kernel-doc
- HID: asus: add vibration strength configuration
- clean up comments and kernel-doc
- HID: asus: add joysticks inner and outer range configuration
- clean up comments and kernel-doc
- HID: asus: add triggers inner and outer range configuration
- clean up comments and kernel-doc
- HID: asus: add joysticks anti-deadzone configuration
- clean up comments and kernel-doc
- HID: asus: add support for response curve
- normalize kernel-doc comment
- HID: asus: add support to force feedback
- remove a stale comment
- HID: asus: add support for gamepad mode
- remove a stale comment, normalize kernel-doc
- HID: asus: add support for turbo buttons
- remove stale comments, normalize kernel-doc
- HID: asus: add support for btn remapping
- convert function comments to kernel-doc
-v3
- HID: asus: add support for ROG Ally handhelds
- take references on input devices to avoid use-after-free on unbind
- reject probes from USB devices other than the recorded controller
- add open/close callbacks to the gamepad input device
- keyboard_input is populated after hid_hw_start(): issue does not apply
- HID: asus: add gamepad configuration
- restore cached Xbox controller mode after an MCU reset
- convert remaining scoped_guard() users to guard()
- avoid printing "(null)" for unnamed sysfs groups
- HID: asus: add vibration strength configuration
- send MCU commands through ally_gamepad_send_packet()
- HID: asus: add joysticks inner and outer range configuration
- send MCU commands through ally_gamepad_send_packet()
- gate sysfs groups and callbacks on capabilities
- HID: asus: add triggers inner and outer range configuration
- send MCU commands through ally_gamepad_send_packet()
- gate sysfs groups and callbacks on capabilities
- HID: asus: add joysticks anti-deadzone configuration
- send MCU commands through ally_gamepad_send_packet()
- gate sysfs groups and callbacks on capabilities
- HID: asus: add support for response curve
- send MCU commands through ally_gamepad_send_packet()
- check resp_curve_support in show callbacks too
- HID: asus: add support to force feedback
- read ally_x_hdev under ally_data_lock and take a reference on it
- publish ally_x_hdev before registering the input device
- HID: asus: add support for gamepad mode
- convert remaining scoped_guard() users to guard()
- HID: asus: add support for turbo buttons
- pass the config pointer directly to ally_config_remove()
- run ally_config_remove() outside ally_data_lock
- restore cached values when the hardware rejects an update
- check turbo_support in show callbacks too
- reported scoped_guard warning verified as false positive
- HID: asus: add support for btn remapping
- look up default mappings by name instead of array index
- resend the old mapping when a sequential update fails midway
- allow macros regardless of the gamepad mode
- rename PAD_* codes to proper input event names
- convert remaining scoped_guard() users to guard()
- v4
- HID: asus: do not send keyboard init reports to touchpads
- Added to avoid regressting touchpads and as guidance for maintainers
- HID: asus: reinitialize the device after exiting a sleep state
- Follow the modified logic of the previous patch
- Addressed issues of sashiko-bot albeit one was a false positive
- Rework patches: I made some disasters rebasing in v3
- v5
- HID: asus: add support for ROG Ally handhelds
- Fix UAF due to inverted input device ownership in hid_asus_ally_remove()
- Reject probes claiming an interface role that is already owned
- HID: asus: add gamepad configuration
- Keep the configuration allocated until module removal instead of devm-managing it: the sysfs callbacks must be able to outlive probe
- Undo sysfs group creation manually on the error path and in ally_config_remove(): creation and removal are now symmetric
- Add payloads size check in ally_alloc_cmd()
- HID: asus: add vibration strength configuration
- Restore the cached vibration intensity after an MCU reset
- HID: asus: add joysticks inner and outer range configuration
- Restore the cached joystick thresholds after an MCU reset
- Remove the calibration sysfs groups manually like the other ones
- HID: asus: add triggers inner and outer range configuration
- Restore the cached trigger ranges after an MCU reset
- HID: asus: add joysticks anti-deadzone configuration
- Restore the cached anti-deadzone values after an MCU reset
- HID: asus: add support for response curve
- Restore the cached response curves after an MCU reset
- Keep holding config_mutex until the curve packet has been sent
- HID: asus: add support to force feedback
- Serialize the cross-interface force-feedback sends with the gamepad interface removal by holding intf_mutex in the callers
- HID: asus: add support for gamepad mode
- Restore the recorded gamepad mode after an MCU reset
- HID: asus: add support for turbo buttons
- Fix UAF allocating the turbo button entries with kcalloc
- Restore the cached turbo parameters after an MCU reset
- HID: asus: add support for btn remapping
- Keep the button mappings outside of the controller lifetime, so that the recorded configuration survives a re-probe
- Re-apply the recorded mappings after an MCU reset
- Fetch the configuration through ally_get_config()
- v6
- HID: asus: add support for ROG Ally handhelds
- Add a maximum size check in ally_alloc_cmd()
- Drop the dead x_hdev snapshot taken in hid_asus_ally_raw_event()
- Drop the unused hdev parameter of handle_ctrl_alt_del()
- Drop the unused hdev/report parameters of ally_x_raw_event()
- Turn the "// F20;" comment into a proper "/* F20 */"
- HID: asus: add gamepad configuration
- Publish the configuration (ally->config under ally_data_lock) before the sysfs groups are created and withdraw it on the error
path: attributes could become visible while ally_get_config() still returned NULL and the restore path silently did nothing
- Record the Xbox controller mode in the first-setup defaults and apply the recorded value from hid_asus_ally_init()
- properly rebase ally_alloc_cmd(): make it no change from previous patch
- HID: asus: add joysticks inner and outer range configuration
- Drop the unused cfg parameter of ally_set_joystick_thresholds()
- HID: asus: add triggers inner and outer range configuration
- Drop the unused cfg parameter of ally_set_trigger_ranges()
- Fix a commit message typo: "sensitivity"
- HID: asus: add support for response curve
- Add ally_resp_curve_is_valid() and use it in the init/resume restore path exactly like the apply path does
- Drop the unused struct device * parameter of the four response-curve helpers
- HID: asus: add support to force feedback
- Drop the unused pointers from ally_x_send_ff_report() and ally_x_send_ff_off()
- HID: asus: add support for gamepad mode
- Apply the mode exactly once from hid_asus_ally_init()
- HID: asus: add support for turbo buttons
- Remove the calibration groups created before a sysfs failure
- HID: asus: add support for btn remapping
- Do not gate remapping by turbo
- Reject NULL remap/macro pointers in ally_set_button_mapping()
- Carry the ally pointer in struct button_remap_attr
- Fix the "hypen" comment typo
- v7
- The "HID: asus: do not send keyboard init reports to touchpads" patch is not part of this series anymore: it has been applied upstream as 8099f9da59f1 and the series is rebased on top of it
- HID: asus: add support for ROG Ally handhelds
- Reserve the probed interface role for the whole duration of its sleeping initialization: a concurrent probe of the same role could pass the singleton checks before the first one published its pointer
- HID: asus: add gamepad configuration
- Publish drvdata->rog_ally before the configuration attributes are
created: the callbacks fetched it before asus_probe() published it and returned -ENODEV in the meantime
- Roll the configuration back with ally_config_remove() when hid_asus_ally_init() fails, instead of leaking the created sysfs groups and the button entries
- HID: asus: add support for response curve
- Replace response_curve_pct_1..4, response_curve_move_1..4 and the apply attributes with one response_curve attribute per joystick that takes the whole curve in a single validated all-or-nothing write:
staged partial edits reaching the MCU on resume cannot happen anymore
- Add a read-only response_curve_index attribute documenting the meaning of each value
- Restore both curves unconditionally on init/resume: only validated curves are recorded now
- HID: asus: add support for btn remapping
- Drop the stale cfg->turbo_support gate in ally_config_create():
remapping attributes are created for every device with button directories, not only turbo-capable ones
- v8
- HID: asus: add support for ROG Ally handhelds
- Release the probe reservation when the interface pointer is
published: the flags were only cleared on failure, leaving them stuck set after the first bind and rejecting every later probe of that role
- Reserve the keyboard interface role too: its check and its publication were two separate locked sections, so concurrent probes of the same role could both publish
- Keep the recorded USB device while any probe is still in flight
- HID: asus: add gamepad configuration
- Report the missing Xbox controller capability with -EOPNOTSUPP
- HID: asus: add vibration strength configuration
- Wake pollers with sysfs_notify() after a successful write
- HID: asus: add joysticks inner and outer range configuration
- Wake pollers with sysfs_notify() after a successful write
- HID: asus: add triggers inner and outer range configuration
- Wake pollers with sysfs_notify() after a successful write
- HID: asus: add joysticks anti-deadzone configuration
- Wake pollers with sysfs_notify() after a successful write
- HID: asus: add support for response curve
- Wake pollers with sysfs_notify() after a successful curve write
- HID: asus: add support to force feedback
- Clear the rumble update latch on setup so a fresh interface cannot inherit one left over from a teardown race
- HID: asus: add support for gamepad mode
- Return -EIO for invalid gamepad modes stored in the device
- Wake pollers with sysfs_notify() after a successful write
- HID: asus: add support for turbo buttons
- Wake pollers with sysfs_notify() after a successful write
- HID: asus: add support for btn remapping
- Return -EIO if a mapping is ever found unset instead of dereferencing it
- Wake pollers with sysfs_notify() after remap and macro writes, and after a mode switch re-defaults the mappings
- v9
- HID: asus: add support to force feedback
- Hold intf_mutex while unpublishing the gamepad interface in the setup error path, like hid_asus_ally_remove() does
- Improve gamepad interface device lock around the initialization force-feedback send
- HID: asus: add support for btn remapping
- Unpublish button_entries under config_mutex before removing the attributes and freeing the array
- v10
- HID: asus: reinitialize the device after exiting a sleep state
- Dropped as already merged in XG Mobile patchset
- v11
- HID: asus: add support for ROG Ally handhelds
- Split the chained data[1] = data[3] = 0x00 assignment into two statements
- Align the hid_asus_ally_raw_event() continuation line with the opening parenthesis
- HID: asus: add gamepad configuration
- Add the missing blank line between struct hid_driver and asus_init()
- HID: asus: add joysticks inner and outer range configuration
- Align the sysfs_remove_group() and hid_warn() continuation lines with the opening parenthesis
- HID: asus: add triggers inner and outer range configuration
- Align the hid_warn() continuation lines with the opening parenthesis
- HID: asus: add joysticks anti-deadzone configuration
- Align the hid_warn() continuation lines with the opening parenthesis
- HID: asus: add support for response curve
- Drop the duplicate blank line before ally_config_attrs[]
- HID: asus: add support to force feedback
- Add the missing blank line before the static_assert
- Align the hid_warn() continuation line with the opening parenthesis
- HID: asus: add support for btn remapping
- Align the hid_warn() and ally_set_button_mapping() continuation lines with the opening parenthesis
- HID: asus: size feature reports from the HID report descriptor
- New patch
- HID: asus: identify XG Mobile stations and bind the LED to the reported model
- New patch
- HID: asus: expose XG Mobile identity and firmware information
- New patch
- HID: asus: size ally gamepad commands from the HID report descriptor
- New patch
- platform/x86: asus-armoury: pick the eGPU activation from the reported GPU
- New patch
- HID: asus: report the XG Mobile GPU vendor to asus-wmi
- New patch
- MAINTAINERS: add maintainer for the ASUS HID driver
- New patch
Denis Benato (18):
HID: asus: add support for ROG Ally handhelds
HID: asus: add gamepad configuration
HID: asus: add vibration strength configuration
HID: asus: add joysticks inner and outer range configuration
HID: asus: add triggers inner and outer range configuration
HID: asus: add joysticks anti-deadzone configuration
HID: asus: add support for response curve
HID: asus: add support to force feedback
HID: asus: add support for gamepad mode
HID: asus: add support for turbo buttons
HID: asus: add support for btn remapping
HID: asus: size feature reports from the HID report descriptor
HID: asus: identify XG Mobile stations and bind the LED to the
reported model
HID: asus: expose XG Mobile identity and firmware information
HID: asus: size ally gamepad commands from the HID report descriptor platform/x86: asus-armoury: pick the eGPU activation from the reported
GPU
HID: asus: report the XG Mobile GPU vendor to asus-wmi
MAINTAINERS: add maintainer for the ASUS HID driver
MAINTAINERS | 6 + drivers/hid/Kconfig | 1 + drivers/hid/hid-asus.c | 5457 +++++++++++++++++++- drivers/platform/x86/asus-armoury.c | 53 +- drivers/platform/x86/asus-wmi.c | 29 + include/linux/platform_data/x86/asus-wmi.h | 11 + 6 files changed, 5474 insertions(+), 83 deletions(-)
Signature · 1 line
2.47.3
Denis Benato
maintainerPATCH v11 1/18 HID: asus: add support for ROG Ally handhelds
Implement the core functionality for ASUS handhelds:
- extend functionality of hid-asus if driver is enabled
- initialise the device to a "ready" state
- implement Ally X input mapping
- emit a single proper event for AC button long press
- reject probes from a different controller and from duplicated interfaces of the same one, which could evict the shared state
--- drivers/hid/hid-asus.c | 996 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 996 insertions(+)
drivers/hid/hid-asus.c +996 -0
@@ -24,12 +24,18 @@#include <linux/cleanup.h> #include <linux/dmi.h> #include <linux/hid.h> +#include <linux/jiffies.h> +#include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/platform_data/x86/asus-wmi.h> +#include <linux/types.h> #include <linux/input/mt.h> #include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */ #include <linux/power_supply.h> +#include <linux/stddef.h> #include <linux/leds.h> +#include <linux/unaligned.h> #include "hid-ids.h"
@@ -37,6 +43,9 @@ MODULE_AUTHOR("Yusuke Fujimaki <[email protected]>");MODULE_AUTHOR ("Brendan McGrath <[email protected]>" );MODULE_AUTHOR ("Victor Vlasenko <[email protected]>" );MODULE_AUTHOR ("Frederik Wenigwieser <[email protected]>" ); +MODULE_AUTHOR ("Denis Benato <[email protected]>" ); +MODULE_AUTHOR ("Luke Jones <[email protected]>" ); +MODULE_AUTHOR ("Khamunetri Clark <[email protected]>" );MODULE_DESCRIPTION ("Asus HID Keyboard and TouchPad" );#define T100_TPAD_INTF 2
@@ -57,6 +66,19 @@ MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");#define ROG_ALLY_X_MIN_MCU 313#define ROG_ALLY_MIN_MCU 319 +#define HID_ALLY_INTF_KEYBOARD_IN 0x81 +#define HID_ALLY_INTF_CFG_IN 0x83 +#define HID_ALLY_X_INTF_IN 0x87 + +#define HID_ALLY_GET_REPORT_ID 0x0D +#define HID_ALLY_SET_REPORT_ID 0x5A +#define HID_ALLY_FEATURE_CODE_PAGE 0xD1 + +#define HID_ALLY_X_INPUT_REPORT_SIZE 16 +#define HID_ALLY_X_INPUT_REPORT 0x0B + +#define HID_ALLY_READY_MAX_TRIES 6 +/* Spurious HID codes sent by QUIRK_ROG_NKEY_KEYBOARD devices */ #define ASUS_SPURIOUS_CODE_0XEA 0xea#define ASUS_SPURIOUS_CODE_0XEC 0xec
@@ -161,6 +183,40 @@ struct asus_touchpad_info {int report_size; }; +struct ally_handheld { +/* All read/write to IN interfaces must lock */ +struct mutex intf_mutex; +/* +* USB device of the connected controller, held with a reference: +* at most one ROG Ally can exist, so probes from any other USB +* device are rejected to protect the shared state. +*/ +struct usb_device *udev; +struct hid_device *cfg_hdev; + +struct input_dev *ally_x_input; +struct hid_device *ally_x_hdev; +/* +* Probe reservations: set under ally_data_lock when a probe takes +* a role on, cleared when that probe publishes its pointer or +* fails, so a concurrent second probe of the same role is rejected +* instead of racing the first one through its sleeping +* initialization (or its check-to-publication window, for the +* keyboard role). +*/ +bool cfg_probe_in_progress; +bool ally_x_probe_in_progress; +bool keyboard_probe_in_progress; + +struct hid_device *keyboard_hdev; +struct input_dev *keyboard_input; + +u8 cad_sequence_state; +unsigned long cad_last_event_time; + +struct delayed_work resume_work; +}; +struct asus_drvdata {unsigned long quirks;struct hid_device *hdev;
@@ -168,6 +224,7 @@ struct asus_drvdata {struct input_dev *tp_kbd_input;struct asus_worker *worker;unsigned int kbd_backlight_brightness; +struct ally_handheld *rog_ally;const struct asus_touchpad_info *tp;struct power_supply *battery;struct power_supply_desc battery_desc;
@@ -240,12 +297,906 @@ static const struct asus_touchpad_info medion_e1239t_tp = { .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */, }; +enum ally_command_codes { +CMD_SET_GAMEPAD_MODE =0x01 , +CMD_SET_MAPPING =0x02 , +CMD_SET_JOYSTICK_MAPPING =0x03 , +CMD_SET_JOYSTICK_DEADZONE =0x04 , +CMD_SET_TRIGGER_RANGE =0x05 , +CMD_SET_VIBRATION_INTENSITY =0x06 , +CMD_LED_CONTROL =0x08 , +CMD_CHECK_READY =0x0A , +CMD_SET_XBOX_CONTROLLER =0x0B , +CMD_CHECK_XBOX_SUPPORT =0x0C , +CMD_USER_CAL_DATA =0x0D , +CMD_CHECK_USER_CAL_SUPPORT =0x0E , +CMD_SET_TURBO_PARAMS =0x0F , +CMD_CHECK_TURBO_SUPPORT =0x10 , +CMD_CHECK_RESP_CURVE_SUPPORT =0x12 , +CMD_SET_RESP_CURVE =0x13 , +CMD_CHECK_DIR_TO_BTN_SUPPORT =0x14 , +CMD_SET_GYRO_PARAMS =0x15 , +CMD_CHECK_GYRO_TO_JOYSTICK =0x16 , +CMD_CHECK_ANTI_DEADZONE =0x17 , +CMD_SET_ANTI_DEADZONE =0x18 , +}; + +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = { +0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB +}; + +/* +* The ROG Ally device presents multiple USB interfaces (keyboard, mouse, gamepad, +* and custom configuration interface) that bind to the same module. Since only +* one ROG Ally device can be connected at a time, we use a single global static +* ally_handheld structure to share state across these separate HID interfaces. +*/ +static void ally_resume_work_fn (struct work_struct *work); + +/* +* Changes to ally_drvdata must lock: the raw_event callbacks, which may +* run in atomic (URB completion) context, also take this lock, so it must +* be a spinlock. +*/ +static DEFINE_SPINLOCK (ally_data_lock); +static struct ally_handheld ally_drvdata = { + .intf_mutex =__MUTEX_INITIALIZER (ally_drvdata.intf_mutex), +/* +* Initialised statically so it is always safe to cancel, whichever +* of the interfaces probed or failed to probe. +*/ + .resume_work =__DELAYED_WORK_INITIALIZER (ally_drvdata.resume_work, + ally_resume_work_fn,0 ), +}; + +/* +* Drop the recorded USB device if no Ally interface is bound anymore and +* none is initializing either: probe failures never run remove(), so +* without this cleanup a failed probe would leave a stale record behind +* and the driver would then reject the real controller when it +* reconnects. A probe whose role is reserved still needs the record. +*/ +static void ally_put_udev_if_orphaned (void ) +{ +struct usb_device *udev; +unsigned long flags; + +spin_lock_irqsave (&ally_data_lock, flags); +if (!ally_drvdata.udev || ally_drvdata.keyboard_hdev || + ally_drvdata.cfg_hdev || ally_drvdata.ally_x_hdev || + ally_drvdata.cfg_probe_in_progress || + ally_drvdata.ally_x_probe_in_progress || + ally_drvdata.keyboard_probe_in_progress) { +spin_unlock_irqrestore (&ally_data_lock, flags); +return ; + } + + udev = ally_drvdata.udev; + ally_drvdata.udev =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags); + +usb_put_dev (udev); +} + +/* +* Release the role reservation taken by hid_asus_ally_probe(): a failed +* probe must free the role again, or every later probe of that role +* would be rejected. A successful probe releases it when it publishes +* its pointer. +*/ +static void ally_probe_release_role (int ep) +{ +unsigned long flags; + +spin_lock_irqsave (&ally_data_lock, flags); +if (ep ==HID_ALLY_INTF_CFG_IN ) + ally_drvdata.cfg_probe_in_progress = false; +else if (ep ==HID_ALLY_X_INTF_IN ) + ally_drvdata.ally_x_probe_in_progress = false; +else if (ep ==HID_ALLY_INTF_KEYBOARD_IN ) + ally_drvdata.keyboard_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +} +static const u8 asus_report_id_init[] = {FEATURE_KBD_REPORT_ID ,FEATURE_KBD_LED_REPORT_ID1 ,FEATURE_KBD_LED_REPORT_ID2 }; +static inline int ally_dev_set_report (struct hid_device *hdev,const u8 *buf,size_t len) +{ + u8 *dmabuf __free(kfree) = kmemdup(buf, len, GFP_KERNEL); +if (!dmabuf) +return -ENOMEM ; + +return hid_hw_raw_request (hdev, buf[0 ], dmabuf, len, +HID_FEATURE_REPORT ,HID_REQ_SET_REPORT ); +} + +static inline int ally_dev_get_report (struct hid_device *hdev,u8 *out,size_t len) +{ +return hid_hw_raw_request (hdev,HID_ALLY_GET_REPORT_ID , out, len, +HID_FEATURE_REPORT ,HID_REQ_GET_REPORT ); +} + +static void ally_resume_work_fn (struct work_struct *work) +{ + struct ally_handheld *ally = container_of(work, struct ally_handheld, + resume_work.work); +struct input_dev *keyboard_input, *x_input; +unsigned long flags; + +/* +* Snapshot the very pointers that get dereferenced under the lock +* protecting them, and take a reference on each input_dev: probe +* sets keyboard_hdev even when the interface exposes no input_dev, +* removal clears the two fields one after the other, and the input +* devices are freed with their own interface. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + keyboard_input =input_get_device (ally->keyboard_input); + x_input =input_get_device (ally->ally_x_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* +* Force release all vendor buttons to prevent "stuck" ghosting on +* resume (workaround for Ally X USB re-probing during suspend/resume). +*/ +if (keyboard_input) { +input_report_key (keyboard_input,KEY_F16 ,0 ); +input_report_key (keyboard_input,KEY_F17 ,0 ); +input_report_key (keyboard_input,KEY_F18 ,0 ); +input_report_key (keyboard_input,KEY_F19 ,0 ); +input_report_key (keyboard_input,KEY_F20 ,0 ); +input_report_key (keyboard_input,KEY_PROG1 ,0 ); +input_sync (keyboard_input); +input_put_device (keyboard_input); + } + +if (x_input) { +input_report_key (x_input,KEY_F16 ,0 ); +input_report_key (x_input,KEY_F17 ,0 ); +input_report_key (x_input,KEY_F18 ,0 ); +input_report_key (x_input,KEY_PROG1 ,0 ); +input_sync (x_input); +input_put_device (x_input); + } +} + +/** +* handle_ctrl_alt_del() - detect a left button long press +* @ally: ally handheld structure holding the sequence state +* @data: raw report buffer, rewritten in place when the sequence matches +* @size: length of @data in bytes +* +* The Ally left button emits a sequence of ctrl+alt+del events. Capture that +* and emit only a single code for that single event. +* +* Return: true iff the event has been managed +*/ +static bool handle_ctrl_alt_del (struct ally_handheld *ally,u8 *data,int size) +{ +bool time_is_past =time_after (jiffies, ally->cad_last_event_time +msecs_to_jiffies (100 )); + +if (size <16 || data[0 ] !=0x01 ) +return false; + +if (ally->cad_sequence_state >0 && time_is_past) + ally->cad_sequence_state =0 ; + + ally->cad_last_event_time = jiffies; + +switch (ally->cad_sequence_state) { +case 0 : +if (data[1 ] ==0x01 && data[2 ] ==0x00 && data[3 ] ==0x00 ) { + ally->cad_sequence_state =1 ; + data[1 ] =0x00 ; +return true; + } +break ; +case 1 : +if (data[1 ] ==0x05 && data[2 ] ==0x00 && data[3 ] ==0x00 ) { + ally->cad_sequence_state =2 ; + data[1 ] =0x00 ; +return true; + } +break ; +case 2 : +if (data[1 ] ==0x05 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =3 ; + data[1 ] =0x00 ; + data[3 ] =0x6F ;/* F20 */ +return true; + } +break ; +case 3 : +if (data[1 ] ==0x04 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =4 ; + data[1 ] =0x00 ; + data[3 ] =0x00 ; +return true; + } +break ; +case 4 : +if (data[1 ] ==0x00 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =5 ; + data[3 ] =0x00 ; +return true; + } +break ; + } + ally->cad_sequence_state =0 ; +return false; +} + +static bool handle_ally_event (struct hid_device *hdev,struct ally_handheld *ally, +u8 *data,int size) +{ +struct input_dev *keyboard_input; +unsigned long flags; +int keycode =0 ; + +if (size <2 ) +return false; + +if (data[0 ] ==0x5A ) { +switch (data[1 ]) { +case 0x38 : + keycode =KEY_F19 ; +break ; +case 0xA6 : + keycode =KEY_F16 ; +break ; +case 0xA7 : + keycode =KEY_F17 ; +break ; +case 0xA8 : + keycode =KEY_F18 ; +break ; +default : +return false; + } + +/* +* Take a reference on the input_dev before dropping the lock: +* the keyboard interface can be unbound concurrently, and its +* input_dev is freed with it. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + keyboard_input =input_get_device (ally->keyboard_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (!keyboard_input) +return false; + +input_report_key (keyboard_input, keycode,1 ); +input_sync (keyboard_input); +input_report_key (keyboard_input, keycode,0 ); +input_sync (keyboard_input); +input_put_device (keyboard_input); +return true; + } +return false; +} + +/** +* ally_gamepad_send_packet() - Send a raw packet to the gamepad device +* @ally: ally handheld structure +* @hdev: HID device +* @buf: buffer containing the packet data +* @len: length of data to send +* +* Return: count of data transferred, negative if error +*/ +static int ally_gamepad_send_packet (struct ally_handheld *ally, +struct hid_device *hdev,const u8 *buf,size_t len) +{ +guard (mutex)(&ally->intf_mutex); +return ally_dev_set_report (hdev, buf, len); +} + +/** +* ally_gamepad_send_receive_packet() - Send a packet and receive the response +* @ally: ally handheld structure +* @hdev: HID device +* @buf: buffer containing the packet data to send and receive response in +* @len: length of buffer +* +* Return: count of data transferred, negative if error +*/ +static int ally_gamepad_send_receive_packet (struct ally_handheld *ally, +struct hid_device *hdev, +u8 *buf,size_t len) +{ +int ret; + + scoped_guard(mutex, &ally->intf_mutex) { + ret =ally_dev_set_report (hdev, buf, len); +if (ret >=0 ) { +memset (buf,0 , len); + ret =ally_dev_get_report (hdev, buf, len); + } + } + +return ret; +} + +/** +* ally_alloc_cmd() - Construct a command buffer for the gamepad +* @cmd: Command code to send +* @payload: Optional payload data to include in the command +* @payload_size: Size of the payload data +* +* The constructed buffer is 64 bytes long, and it is the caller +* responsibility to free the buffer using kfree(). +* +* Return: the newly allocated buffer containing the command, or NULL on +* allocation failure or if the payload would not fit in the report +*/ +static u8 *ally_alloc_cmd (u8 cmd,const u8 *payload,u8 payload_size) +{ +u8 *hidbuf; + +/* +* The payload is written after the 4-byte command header: +* reject one that would spill past the end of the report. +*/ +if (payload_size >ROG_ALLY_REPORT_SIZE -4 ) +return NULL ; + + hidbuf =kzalloc (ROG_ALLY_REPORT_SIZE ,GFP_KERNEL ); + +if (!hidbuf) +return NULL ; + + hidbuf[0 ] =HID_ALLY_SET_REPORT_ID ; + hidbuf[1 ] =HID_ALLY_FEATURE_CODE_PAGE ; + hidbuf[2 ] = cmd; + hidbuf[3 ] = payload_size; + +if (payload_size >0 && payload) +memcpy (&hidbuf[4 ], payload, payload_size); + +return hidbuf; +} + +/** +* ally_gamepad_check_ready() - Wait for the gamepad MCU to report ready +* @ally: ally handheld structure +* @hdev: HID device +* +* This should be called before any remapping attempts, and on driver +* init/resume, after the asus handshake has been performed on the +* configuration endpoint. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_gamepad_check_ready (struct ally_handheld *ally,struct hid_device *hdev) +{ +u8 payload[] = {0x00 }; +int ret; + +for (int i =0 ; i <HID_ALLY_READY_MAX_TRIES ; i++) { + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_CHECK_READY, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_receive_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_dbg (hdev,"ROG Ally check %d/%d failed: %d\n" , i, +HID_ALLY_READY_MAX_TRIES , ret); +continue ; + } + +if (buf[2 ] ==CMD_CHECK_READY ) +return 0 ; + +usleep_range (1000 ,2000 ); + } + +hid_err (hdev,"ROG Ally never responded with a ready\n" ); +return -ENODEV ; +} + +static int ally_get_endpoint_address (struct hid_device *hdev) +{ +struct usb_host_endpoint *ep; +struct usb_interface *intf; + +if (!hid_is_usb (hdev)) +return -ENODEV ; + + intf =to_usb_interface (hdev->dev.parent); +if (!intf || !intf->cur_altsetting) +return -ENODEV ; + +if (intf->cur_altsetting->desc.bNumEndpoints ==0 ) +return -ENODEV ; + + ep = intf->cur_altsetting->endpoint; +if (!ep) +return -ENODEV ; + +return ep->desc.bEndpointAddress; +} + +struct ally_x_input_report { +__le16 x, y; +__le16 rx, ry; +__le16 z, rz; +u8 buttons[3 ]; +} __packed; + +/* The hatswitch outputs integers, we use them to index this X|Y pair */ +static const int hat_values[][2 ] = { + {0 ,0 }, {0 ,-1 }, {1 ,-1 }, {1 ,0 }, {1 ,1 }, + {0 ,1 }, {-1 ,1 }, {-1 ,0 }, {-1 ,-1 }, +}; + +/** +* ally_x_raw_event() - Parse and report an Ally X gamepad input report +* @input: input device to report the events on +* @data: raw report buffer +* @size: length of @data in bytes +* +* Return: true if the event was handled, otherwise false +*/ +static bool ally_x_raw_event (struct input_dev *input,u8 *data,int size) +{ +struct ally_x_input_report *in_report; +u16 x, y, rx, ry, z, rz; +u8 byte; + +if (!input) +return false; + +if (size <1 ) +return false; + +/* +* The size check above only guarantees one valid byte: do not +* read the code of a truncated report. +*/ +if (data[0 ] ==0x5A ) { +if (size <2 ) +return false; + +input_report_key (input,KEY_PROG1 , data[1 ] ==0x38 ); +input_report_key (input,KEY_F16 , data[1 ] ==0xA6 ); +input_report_key (input,KEY_F17 , data[1 ] ==0xA7 ); +input_report_key (input,KEY_F18 , data[1 ] ==0xA8 ); +input_sync (input); + +return data[1 ] ==0xA6 || data[1 ] ==0xA7 || data[1 ] ==0xA8 || data[1 ] ==0x38 ; + } + +if (data[0 ] !=HID_ALLY_X_INPUT_REPORT ) +return false; + +/* +* hid-core only guarantees size >= 1 and does not zero-pad short +* reports before ->raw_event, so a truncated transfer would leave the +* payload below pointing at stale DMA buffer contents. +*/ +if (size <1 +sizeof (*in_report)) +return false; + + in_report = (struct ally_x_input_report *)&data[1 ]; + +/* USB HID payloads are little-endian: convert them explicitly. */ + x =get_unaligned_le16 (&in_report->x); + y =get_unaligned_le16 (&in_report->y); + rx =get_unaligned_le16 (&in_report->rx); + ry =get_unaligned_le16 (&in_report->ry); + z =get_unaligned_le16 (&in_report->z); + rz =get_unaligned_le16 (&in_report->rz); + +input_report_abs (input,ABS_X , x -32768 ); +input_report_abs (input,ABS_Y , y -32768 ); +input_report_abs (input,ABS_RX , rx -32768 ); +input_report_abs (input,ABS_RY , ry -32768 ); +input_report_abs (input,ABS_Z , z); +input_report_abs (input,ABS_RZ , rz); + + byte = in_report->buttons[0 ]; +input_report_key (input,BTN_A , byte &BIT (0 )); +input_report_key (input,BTN_B , byte &BIT (1 )); +input_report_key (input,BTN_X , byte &BIT (2 )); +input_report_key (input,BTN_Y , byte &BIT (3 )); +input_report_key (input,BTN_TL , byte &BIT (4 )); +input_report_key (input,BTN_TR , byte &BIT (5 )); +input_report_key (input,BTN_SELECT , byte &BIT (6 )); +input_report_key (input,BTN_START , byte &BIT (7 )); + + byte = in_report->buttons[1 ]; +input_report_key (input,BTN_THUMBL , byte &BIT (0 )); +input_report_key (input,BTN_THUMBR , byte &BIT (1 )); +input_report_key (input,BTN_MODE , byte &BIT (2 )); + +/* The hatswitch byte is device-controlled; treat anything the table +* does not cover as centred rather than indexing out of bounds. +*/ + byte = in_report->buttons[2 ]; +if (byte >=ARRAY_SIZE (hat_values)) + byte =0 ; +input_report_abs (input,ABS_HAT0X , hat_values[byte][0 ]); +input_report_abs (input,ABS_HAT0Y , hat_values[byte][1 ]); + +input_sync (input); + +return true; +} + +static int ally_x_input_open (struct input_dev *dev) +{ +struct hid_device *hdev =input_get_drvdata (dev); + +return hid_hw_open (hdev); +} + +static void ally_x_input_close (struct input_dev *dev) +{ +hid_hw_close (input_get_drvdata (dev)); +} + +static struct input_dev *ally_x_alloc_input_dev (struct hid_device *hdev) +{ +struct input_dev *input_dev =input_allocate_device (); + +if (!input_dev) +return ERR_PTR (-ENOMEM ); + + input_dev->dev.parent = &hdev->dev; + input_dev->id.bustype = hdev->bus; + input_dev->id.vendor = hdev->vendor; + input_dev->id.product = hdev->product; + input_dev->id.version = hdev->version; + input_dev->uniq = hdev->uniq; + input_dev->name ="ASUS ROG Ally X Gamepad" ; + +input_set_drvdata (input_dev, hdev); +/* +* Let the input core hold the hardware open while the device node +* is in use: without these the interface could be runtime-suspended +* and drop events. +*/ + input_dev->open = ally_x_input_open; + input_dev->close = ally_x_input_close; + +return input_dev; +} + +static int ally_x_setup_input (struct hid_device *hdev,struct ally_handheld *ally) +{ +struct input_dev *input =ally_x_alloc_input_dev (hdev); +unsigned long flags; +int ret; + +if (IS_ERR (input)) +return PTR_ERR (input); + +input_set_abs_params (input,ABS_X ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_Y ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_RX ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_RY ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_Z ,0 ,1023 ,0 ,0 ); +input_set_abs_params (input,ABS_RZ ,0 ,1023 ,0 ,0 ); +input_set_abs_params (input,ABS_HAT0X ,-1 ,1 ,0 ,0 ); +input_set_abs_params (input,ABS_HAT0Y ,-1 ,1 ,0 ,0 ); +input_set_capability (input,EV_KEY ,BTN_A ); +input_set_capability (input,EV_KEY ,BTN_B ); +input_set_capability (input,EV_KEY ,BTN_X ); +input_set_capability (input,EV_KEY ,BTN_Y ); +input_set_capability (input,EV_KEY ,BTN_TL ); +input_set_capability (input,EV_KEY ,BTN_TR ); +input_set_capability (input,EV_KEY ,BTN_SELECT ); +input_set_capability (input,EV_KEY ,BTN_START ); +input_set_capability (input,EV_KEY ,BTN_MODE ); +input_set_capability (input,EV_KEY ,BTN_THUMBL ); +input_set_capability (input,EV_KEY ,BTN_THUMBR ); + +input_set_capability (input,EV_KEY ,KEY_PROG1 ); +input_set_capability (input,EV_KEY ,KEY_F16 ); +input_set_capability (input,EV_KEY ,KEY_F17 ); +input_set_capability (input,EV_KEY ,KEY_F18 ); +input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY ); +input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY1 ); + + ret =input_register_device (input); +if (ret) { +hid_err (hdev,"Failed to register Ally X gamepad device: %d\n" , ret); + goto ally_x_setup_input_err; + } + +/* Publish the input_dev only when it is fully set up. */ +spin_lock_irqsave (&ally_data_lock, flags); + ally->ally_x_input = input; +spin_unlock_irqrestore (&ally_data_lock, flags); + +return 0 ; +ally_x_setup_input_err: +input_free_device (input); +return ret; +} + +static int hid_asus_ally_init (struct hid_device *hdev,struct ally_handheld *ally) +{ +int ret; + +/* Failure at this point is non-critical */ + ret =ally_gamepad_send_packet (ally, hdev,ALLY_FORCE_FEEDBACK_OFF , +sizeof (ALLY_FORCE_FEEDBACK_OFF )); +if (ret <0 ) +hid_err (hdev,"Ally failed to init force-feedback off: %d\n" , ret); + +return 0 ; +} + +/** +* hid_asus_ally_raw_event() - Route raw reports from the Ally interfaces +* @hdev: HID device +* @ally: ally handheld structure +* @report: HID report the raw data arrived in +* @data: raw report buffer +* @size: length of @data in bytes +* +* Called from the raw_event callback, which may run in atomic (URB +* completion) context: only spinlock-protected accesses to the shared +* state are allowed here. +* +* Return: true if the event was handled, otherwise false +*/ +static bool hid_asus_ally_raw_event (struct hid_device *hdev,struct ally_handheld *ally, +struct hid_report *report,u8 *data,int size) +{ +struct input_dev *x_input; +unsigned long flags; +bool handled; + +if (!ally) +return false; + +switch (ally_get_endpoint_address (hdev)) { +case HID_ALLY_X_INTF_IN : +/* +* Take a reference on the input_dev while using it: the +* gamepad interface can be unbound concurrently, and its +* input_dev is freed with it. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + x_input =input_get_device (ally->ally_x_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + + handled =ally_x_raw_event (x_input, data, size); +input_put_device (x_input); +if (handled) +return true; +break ; +case HID_ALLY_INTF_CFG_IN : +if (handle_ally_event (hdev, ally, data, size)) +return true; +break ; +case HID_ALLY_INTF_KEYBOARD_IN : +if (handle_ctrl_alt_del (ally, data, size)) +return false; +break ; +default : +break ; + } + +return false; +} + +/** +* hid_asus_ally_probe() - Initialize the ROG Ally HID extension +* @hdev: HID device +* +* This module works alongside the main Asus HID driver to handle +* Ally-specific features and quirks. +* +* Return: an ally_handheld struct pointer on success, an ERR_PTR on +* failure. The caller is not expected to use the returned pointer, but it +* should check for errors by using IS_ERR and PTR_ERR and pass NULL to +* other functions if there was an error. +*/ +static struct ally_handheld *hid_asus_ally_probe (struct hid_device *hdev) +{ +unsigned long flags; +int ret, ep =ally_get_endpoint_address (hdev); +struct usb_device *udev; +struct hid_input *hidinput; + +if (ep <0 ) +return ERR_PTR (ep); + +/* +* The ROG Ally controller is integrated into a handheld PC, so at +* most one device can exist and the shared global state relies on +* that: reject a probe from a different USB device, or a spoofed +* peripheral could overwrite the state of the real controller. The +* recorded device is dropped when the last interface of the +* controller is unbound, so the re-enumeration the embedded +* controller performs after a suspend cycle (it cuts the controller +* power when powersave is enabled) is still accepted. +*/ + udev =interface_to_usbdev (to_usb_interface (hdev->dev.parent)); + +spin_lock_irqsave (&ally_data_lock, flags); +if (ally_drvdata.udev && ally_drvdata.udev != udev) { +spin_unlock_irqrestore (&ally_data_lock, flags); +hid_err (hdev,"A ROG Ally controller is already connected\n" ); +return ERR_PTR (-ENODEV ); + } + +/* +* Interfaces of an already connected controller are equally +* untrusted: a crafted descriptor could present the same +* endpoint twice, and a second probe of one role would evict +* the state recorded by the first one, whose input_dev would +* then never be unregistered. The probe reservations make the +* check hold across the sleeping initialization below, which +* runs before the role pointer is published. +*/ +if ((ep ==HID_ALLY_INTF_CFG_IN && + (ally_drvdata.cfg_hdev || ally_drvdata.cfg_probe_in_progress)) || + (ep ==HID_ALLY_X_INTF_IN && + (ally_drvdata.ally_x_hdev || ally_drvdata.ally_x_probe_in_progress)) || + (ep ==HID_ALLY_INTF_KEYBOARD_IN && + (ally_drvdata.keyboard_hdev || ally_drvdata.keyboard_probe_in_progress))) { +spin_unlock_irqrestore (&ally_data_lock, flags); +hid_err (hdev, +"An interface of a ROG Ally controller is already connected\n" ); +return ERR_PTR (-ENODEV ); + } + +/* Reserve the role for as long as the initialization below runs. */ +if (ep ==HID_ALLY_INTF_CFG_IN ) + ally_drvdata.cfg_probe_in_progress = true; +else if (ep ==HID_ALLY_X_INTF_IN ) + ally_drvdata.ally_x_probe_in_progress = true; +else if (ep ==HID_ALLY_INTF_KEYBOARD_IN ) + ally_drvdata.keyboard_probe_in_progress = true; + +if (!ally_drvdata.udev) { +usb_get_dev (udev); + ally_drvdata.udev = udev; + } +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* +* The interface initialization sleeps (it performs USB transfers), +* so it must not run under ally_data_lock: the role stays reserved +* while it runs, and the pointers are published only once it +* succeeded. +*/ +switch (ep) { +case HID_ALLY_INTF_CFG_IN : +/* +* This function assumes the asus-specific initialization +* to have been performed already at this point. +*/ + ret =ally_gamepad_check_ready (&ally_drvdata, hdev); +if (ret <0 ) { +hid_err (hdev,"ROG Ally device is not ready: %d\n" , ret); +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (ret); + } + + ret =hid_asus_ally_init (hdev, &ally_drvdata); +if (ret <0 ) { +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (ret); + } + +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.cfg_hdev = hdev; + ally_drvdata.cfg_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +case HID_ALLY_X_INTF_IN : +/* This will create and populate ally_x_input */ + ret =ally_x_setup_input (hdev, &ally_drvdata); +if (ret) { +hid_err (hdev,"Failed to create Ally X gamepad device.\n" ); +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (ret); + } + +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.ally_x_hdev = hdev; + ally_drvdata.ally_x_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +case HID_ALLY_INTF_KEYBOARD_IN : +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.keyboard_hdev = hdev; + ally_drvdata.keyboard_probe_in_progress = false; +if (!list_empty (&hdev->inputs)) { + hidinput = list_first_entry(&hdev->inputs, struct hid_input, list); + ally_drvdata.keyboard_input = hidinput->input; + } +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +default : +/* This is normally supposed to happen */ +break ; + } + +return &ally_drvdata; +} + +static void hid_asus_ally_remove (struct hid_device *hdev,struct ally_handheld *ally) +{ +struct input_dev *x_input =NULL ; +unsigned long flags; + +if (!ally) +return ; + +/* +* Any of the three interfaces can own an input_dev the resume work +* reports through, and they are torn down in an arbitrary order, so +* drain it before clearing anything. Cancel outside ally_data_lock so +* a handler that wants the lock cannot deadlock against us. +*/ +cancel_delayed_work_sync (&ally->resume_work); + +spin_lock_irqsave (&ally_data_lock, flags); +if (ally->ally_x_hdev == hdev) { + x_input = ally->ally_x_input; + ally->ally_x_input =NULL ; + ally->ally_x_hdev =NULL ; + } + +/* +* The keyboard interface is torn down before the config one, and +* its input_dev is freed with it. handle_ally_event() and +* ally_resume_work_fn() both report keys through it from the +* config endpoint, so drop the references here or they dangle. +*/ +if (ally->keyboard_hdev == hdev) { + ally->keyboard_input =NULL ; + ally->keyboard_hdev =NULL ; + } +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (x_input) +input_unregister_device (x_input); + +/* +* Drop the recorded USB device when the last interface of the +* controller has been unbound: the driver is then ready to accept +* the controller again when it reconnects. +*/ +ally_put_udev_if_orphaned (); +} + +static int hid_asus_ally_reset_resume (struct hid_device *hdev,struct ally_handheld *ally) +{ +int ep, ret; + +/* +* The extensions failed to probe and the device is operating as a +* generic HID device: do not fail the resume because of that. +*/ +if (!ally) +return 0 ; + + ep =ally_get_endpoint_address (hdev); +if (ep !=HID_ALLY_INTF_CFG_IN ) +return 0 ; + + ret =hid_asus_ally_init (hdev, ally); +if (ret <0 ) +return ret; + +return 0 ; +} +/* * Send events to asus-wmi driver for handling special keys */
@@ -513,6 +1464,17 @@ static int asus_raw_event(struct hid_device *hdev,if (drvdata->quirks &QUIRK_MEDION_E1239T )return asus_e1239t_event (drvdata, data, size); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { +/* +* Return -1 to suppress further processing by the generic HID +* input parser for reports we fully handle for the Gamepad (0x0B): +* letting 0x0B reach the default parser creates a generic gamepad +* causing Steam Input overlaps (i.e. L1 stuck on screenshot). +*/ +if (hid_asus_ally_raw_event (hdev, drvdata->rog_ally, report, data, size)) +return -1 ; + } +/* * Skip these report ID, the device emits a continuous stream associated * with the AURA mode it is in which looks like an 'echo'.
@@ -1306,6 +2268,7 @@ static int asus_input_mapping(struct hid_device *hdev,case 0x5f :asus_map_key_clear (KEY_PROG2 );break ;/* S-shaped programmable key */ case 0x6b :asus_map_key_clear (KEY_F21 );break ;/* ASUS touchpad toggle */ case 0x38 :asus_map_key_clear (KEY_PROG1 );break ;/* ROG key */ +case 0x93 :asus_map_key_clear (KEY_PROG1 );break ;/* ROG Ally X AC button */ case 0xba :asus_map_key_clear (KEY_PROG2 );break ;/* Fn+C ASUS Splendid */ case 0x5c :asus_map_key_clear (KEY_PROG3 );break ;/* Fn+Space Power4Gear */ case 0x99 :asus_map_key_clear (KEY_PROG4 );break ;/* Fn+F5 "fan" symbol */
@@ -1480,6 +2443,8 @@ static int asus_xgm_init(struct hid_device *hdev, struct asus_drvdata *drvdata)static int __maybe_unused asus_resume (struct hid_device *hdev) {struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +int ep;/* * If we have a backlight listener registered, restore the previous state,
@@ -1489,12 +2454,19 @@ static int __maybe_unused asus_resume(struct hid_device *hdev)if (drvdata->listener.brightness_set)asus_kbd_backlight_set (&drvdata->listener, drvdata->kbd_backlight_brightness); +if (ally && (drvdata->quirks &QUIRK_ROG_ALLY_XPAD )) { + ep =ally_get_endpoint_address (hdev); +if (ep ==HID_ALLY_INTF_CFG_IN ) +schedule_delayed_work (&ally->resume_work,msecs_to_jiffies (500 )); + } +return 0 ; }static int __maybe_unused asus_reset_resume (struct hid_device *hdev) {struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +int ret;if (!drvdata->tp)asus_initialize_reports (hdev);
@@ -1502,6 +2474,14 @@ static int __maybe_unused asus_reset_resume(struct hid_device *hdev)if (drvdata->tp)return asus_start_multitouch (hdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { + ret =hid_asus_ally_reset_resume (hdev, drvdata->rog_ally); +if (ret) { +hid_err (hdev,"Failed to resume ROG Ally HID extensions: %d\n" , ret); +return ret; + } + } +return 0 ; }
@@ -1509,6 +2489,7 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id) {struct hid_report_enum *rep_enum;struct asus_drvdata *drvdata; +struct ally_handheld *ally;struct hid_report *rep;bool is_vendor = false;int ret;
@@ -1632,6 +2613,15 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id) (asus_kbd_register_leds(hdev)))hid_warn (hdev,"Failed to initialize backlight.\n" ); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { + ally =hid_asus_ally_probe (hdev); +if (IS_ERR (ally)) +hid_err (hdev,"Failed to initialize ROG Ally HID extensions: %ld\n" , +PTR_ERR (ally)); +else + drvdata->rog_ally = ally; + } +/* * For ROG keyboards, skip rename for consistency and ->input check as * some devices do not have inputs.
@@ -1666,6 +2656,9 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)if (drvdata->xgm_led)devm_led_classdev_unregister (&hdev->dev, &drvdata->xgm_led->cdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) +hid_asus_ally_remove (hdev, drvdata->rog_ally); +asus_worker_stop (drvdata->worker);hid_hw_stop (hdev);return ret;
@@ -1681,6 +2674,9 @@ static void asus_remove(struct hid_device *hdev)if (drvdata->xgm_led)devm_led_classdev_unregister (&hdev->dev, &drvdata->xgm_led->cdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) +hid_asus_ally_remove (hdev, drvdata->rog_ally); +asus_worker_stop (drvdata->worker);hid_hw_stop (hdev); }
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] HID: asus: untracked endpoints claim udev in hid_asus_ally_probe() - [Low] HID: asus: duplicate…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] HID: asus: untracked endpoints claim udev in hid_asus_ally_probe()
- [Low] HID: asus: duplicate and unused BTN_TRIGGER_HAPPY1 capability in ally_x_setup_input() --
commit 2082b890016be96a18a64dd50b259758d2e05629
Author: Denis Benato <[email protected]>
HID: asus: add support for ROG Ally handhelds
This commit adds ROG Ally handheld support to hid-asus, including device readiness checks, Ally X gamepad input mapping, AC button long-press handling, and shared state tracking across the controller's USB interfaces.
Quoted 4 lines
drivers/hid/hid-asus.c +0
-0
[ ... ]
Quoted 1 line
drivers/hid/hid-asus.c +0
-0@@ -240,12 +297,906 @@ static const struct asus_touchpad_info medion_e1239t_tp = {
[ ... ]
Quoted 2 lines
drivers/hid/hid-asus.c +2
-0+static bool ally_x_raw_event (struct input_dev *input,u8 *data,int size) +{
[ ... ]
Quoted 4 lines
drivers/hid/hid-asus.c +4
-0+ byte = in_report->buttons[1 ]; +input_report_key (input,BTN_THUMBL , byte &BIT (0 )); +input_report_key (input,BTN_THUMBR , byte &BIT (1 )); +input_report_key (input,BTN_MODE , byte &BIT (2 ));
[ ... ]
Quoted 2 lines
drivers/hid/hid-asus.c +2
-0+static int ally_x_setup_input (struct hid_device *hdev,struct ally_handheld *ally) +{
[ ... ]
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 904
Quoted 6 lines
drivers/hid/hid-asus.c +6
-0+input_set_capability (input,EV_KEY ,KEY_PROG1 ); +input_set_capability (input,EV_KEY ,KEY_F16 ); +input_set_capability (input,EV_KEY ,KEY_F17 ); +input_set_capability (input,EV_KEY ,KEY_F18 ); +input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY ); +input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY1 );
[Severity: Low]
In include/uapi/linux/input-event-codes.h, BTN_TRIGGER_HAPPY and BTN_TRIGGER_HAPPY1 both expand to 0x2c0, while the second button is BTN_TRIGGER_HAPPY2 (0x2c1).
At the same time, ally_x_raw_event() only reports bits 0..2 from in_report->buttons[1] and never emits any BTN_TRIGGER_HAPPY* events.
Were these two capabilities intended to be BTN_TRIGGER_HAPPY1 and BTN_TRIGGER_HAPPY2 with corresponding reporting in ally_x_raw_event(), or should they be dropped from ally_x_setup_input()?
Quoted 2 lines
drivers/hid/hid-asus.c +2
-0+ + ret =input_register_device (input);
[ ... ]
Quoted 2 lines
drivers/hid/hid-asus.c +2
-0+static struct ally_handheld *hid_asus_ally_probe (struct hid_device *hdev) +{
[ ... ]
Quoted 21 lines
drivers/hid/hid-asus.c +21
-0+/* Reserve the role for as long as the initialization below runs. */ +if (ep ==HID_ALLY_INTF_CFG_IN ) + ally_drvdata.cfg_probe_in_progress = true; +else if (ep ==HID_ALLY_X_INTF_IN ) + ally_drvdata.ally_x_probe_in_progress = true; +else if (ep ==HID_ALLY_INTF_KEYBOARD_IN ) + ally_drvdata.keyboard_probe_in_progress = true; + +if (!ally_drvdata.udev) { +usb_get_dev (udev); + ally_drvdata.udev = udev; + } +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* +* The interface initialization sleeps (it performs USB transfers), +* so it must not run under ally_data_lock: the role stays reserved +* while it runs, and the pointers are published only once it +* succeeded. +*/ +switch (ep) {
[ ... ]
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1130
Quoted 7 lines
drivers/hid/hid-asus.c +7
-0+default : +/* This is normally supposed to happen */ +break ; + } + +return &ally_drvdata; +}
[Severity: Medium]
Can an untracked endpoint on the composite USB device (such as the mouse interface, which has an endpoint address other than HID_ALLY_INTF_CFG_IN, HID_ALLY_X_INTF_IN, or HID_ALLY_INTF_KEYBOARD_IN) cause ally_drvdata.udev to get out of sync with the bound interfaces?
In hid_asus_ally_probe(), any endpoint matching QUIRK_ROG_ALLY_XPAD acquires ally_drvdata.udev before switching on ep, and then hits the default branch without setting any *_hdev pointer or *_probe_in_progress flag, while still returning &ally_drvdata to be stored in drvdata->rog_ally.
Because ally_put_udev_if_orphaned() only checks the three tracked roles:
if (!ally_drvdata.udev || ally_drvdata.keyboard_hdev ||
ally_drvdata.cfg_hdev || ally_drvdata.ally_x_hdev ||
ally_drvdata.cfg_probe_in_progress ||
ally_drvdata.ally_x_probe_in_progress ||
ally_drvdata.keyboard_probe_in_progress) {
spin_unlock_irqrestore(&ally_data_lock, flags);
return;
}two regressions seem possible:
1. If a default-branch interface probes first and a tracked role interface subsequently fails in hid_asus_ally_probe() (or if the three tracked role interfaces are later unbound via sysfs while the default-branch interface remains bound), ally_put_udev_if_orphaned() sees all tracked pointers and reservation flags cleared and drops ally_drvdata.udev while the default-branch interface still holds drvdata->rog_ally = &ally_drvdata. Unbinding that interface later will run hid_asus_ally_remove() -> cancel_delayed_work_sync() and ally_put_udev_if_orphaned() against global state that may now belong to another connected controller.
2. Conversely, if only a default-branch interface binds (for example, if the three tracked role interfaces fail in hid_parse() or hid_hw_start() before reaching hid_asus_ally_probe(), or on a device with the Ally VID/PID exposing only other endpoints), ally_drvdata.udev stays pinned with no tracked role bound, preventing any other controller from probing.
Should hid_asus_ally_probe() return NULL (or avoid claiming ally_drvdata.udev and &ally_drvdata) when ep is not one of HID_ALLY_INTF_CFG_IN, HID_ALLY_X_INTF_IN, or HID_ALLY_INTF_KEYBOARD_IN?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ilpo Järvinen
maintainerQuoted 8 lines · Denis Benato
Implement the core functionality for ASUS handhelds:
- extend functionality of hid-asus if driver is enabled
- initialise the device to a "ready" state
- implement Ally X input mapping
- emit a single proper event for AC button long press
- reject probes from a different controller and from duplicated interfaces of the same one, which could evict the shared state
Quoted 5 lines
Signed-off-by Khamunetri Clark <[email protected]>Signed-off-by Denis Benato <[email protected]>Signed-off-by Matthew Schwartz <[email protected]>Signed-off-by Luke Jones <[email protected]>Signed-off-by Jonathan LoBue <[email protected]>
Shouldn't you be using some Co-developed-by tags too?
Quoted 177 lines
--- drivers/hid/hid-asus.c | 996 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 996 insertions(+)drivers/hid/hid-asus.c +124
-0@@ -24,12 +24,18 @@#include <linux/cleanup.h> #include <linux/dmi.h> #include <linux/hid.h> +#include <linux/jiffies.h> +#include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/platform_data/x86/asus-wmi.h> +#include <linux/types.h> #include <linux/input/mt.h> #include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */ #include <linux/power_supply.h> +#include <linux/stddef.h> #include <linux/leds.h> +#include <linux/unaligned.h> #include "hid-ids.h" @@ -37,6 +43,9 @@ MODULE_AUTHOR("Yusuke Fujimaki <[email protected]>");MODULE_AUTHOR ("Brendan McGrath <[email protected]>" );MODULE_AUTHOR ("Victor Vlasenko <[email protected]>" );MODULE_AUTHOR ("Frederik Wenigwieser <[email protected]>" ); +MODULE_AUTHOR ("Denis Benato <[email protected]>" ); +MODULE_AUTHOR ("Luke Jones <[email protected]>" ); +MODULE_AUTHOR ("Khamunetri Clark <[email protected]>" );MODULE_DESCRIPTION ("Asus HID Keyboard and TouchPad" );#define T100_TPAD_INTF 2@@ -57,6 +66,19 @@ MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");#define ROG_ALLY_X_MIN_MCU 313#define ROG_ALLY_MIN_MCU 319 +#define HID_ALLY_INTF_KEYBOARD_IN 0x81 +#define HID_ALLY_INTF_CFG_IN 0x83 +#define HID_ALLY_X_INTF_IN 0x87 + +#define HID_ALLY_GET_REPORT_ID 0x0D +#define HID_ALLY_SET_REPORT_ID 0x5A +#define HID_ALLY_FEATURE_CODE_PAGE 0xD1 + +#define HID_ALLY_X_INPUT_REPORT_SIZE 16 +#define HID_ALLY_X_INPUT_REPORT 0x0B + +#define HID_ALLY_READY_MAX_TRIES 6 +/* Spurious HID codes sent by QUIRK_ROG_NKEY_KEYBOARD devices */ #define ASUS_SPURIOUS_CODE_0XEA 0xea#define ASUS_SPURIOUS_CODE_0XEC 0xec@@ -161,6 +183,40 @@ struct asus_touchpad_info {int report_size; }; +struct ally_handheld { +/* All read/write to IN interfaces must lock */ +struct mutex intf_mutex; +/* +* USB device of the connected controller, held with a reference: +* at most one ROG Ally can exist, so probes from any other USB +* device are rejected to protect the shared state. +*/ +struct usb_device *udev; +struct hid_device *cfg_hdev; + +struct input_dev *ally_x_input; +struct hid_device *ally_x_hdev; +/* +* Probe reservations: set under ally_data_lock when a probe takes +* a role on, cleared when that probe publishes its pointer or +* fails, so a concurrent second probe of the same role is rejected +* instead of racing the first one through its sleeping +* initialization (or its check-to-publication window, for the +* keyboard role). +*/ +bool cfg_probe_in_progress; +bool ally_x_probe_in_progress; +bool keyboard_probe_in_progress; + +struct hid_device *keyboard_hdev; +struct input_dev *keyboard_input; + +u8 cad_sequence_state; +unsigned long cad_last_event_time; + +struct delayed_work resume_work; +}; +struct asus_drvdata {unsigned long quirks;struct hid_device *hdev;@@ -168,6 +224,7 @@ struct asus_drvdata {struct input_dev *tp_kbd_input;struct asus_worker *worker;unsigned int kbd_backlight_brightness; +struct ally_handheld *rog_ally;const struct asus_touchpad_info *tp;struct power_supply *battery;struct power_supply_desc battery_desc;@@ -240,12 +297,906 @@ static const struct asus_touchpad_info medion_e1239t_tp = { .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */, }; +enum ally_command_codes { +CMD_SET_GAMEPAD_MODE =0x01 , +CMD_SET_MAPPING =0x02 , +CMD_SET_JOYSTICK_MAPPING =0x03 , +CMD_SET_JOYSTICK_DEADZONE =0x04 , +CMD_SET_TRIGGER_RANGE =0x05 , +CMD_SET_VIBRATION_INTENSITY =0x06 , +CMD_LED_CONTROL =0x08 , +CMD_CHECK_READY =0x0A , +CMD_SET_XBOX_CONTROLLER =0x0B , +CMD_CHECK_XBOX_SUPPORT =0x0C , +CMD_USER_CAL_DATA =0x0D , +CMD_CHECK_USER_CAL_SUPPORT =0x0E , +CMD_SET_TURBO_PARAMS =0x0F , +CMD_CHECK_TURBO_SUPPORT =0x10 , +CMD_CHECK_RESP_CURVE_SUPPORT =0x12 , +CMD_SET_RESP_CURVE =0x13 , +CMD_CHECK_DIR_TO_BTN_SUPPORT =0x14 , +CMD_SET_GYRO_PARAMS =0x15 , +CMD_CHECK_GYRO_TO_JOYSTICK =0x16 , +CMD_CHECK_ANTI_DEADZONE =0x17 , +CMD_SET_ANTI_DEADZONE =0x18 , +}; + +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = { +0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB +}; + +/* +* The ROG Ally device presents multiple USB interfaces (keyboard, mouse, gamepad, +* and custom configuration interface) that bind to the same module. Since only +* one ROG Ally device can be connected at a time, we use a single global static +* ally_handheld structure to share state across these separate HID interfaces. +*/ +static void ally_resume_work_fn (struct work_struct *work); + +/* +* Changes to ally_drvdata must lock: the raw_event callbacks, which may +* run in atomic (URB completion) context, also take this lock, so it must +* be a spinlock. +*/ +static DEFINE_SPINLOCK (ally_data_lock); +static struct ally_handheld ally_drvdata = { + .intf_mutex =__MUTEX_INITIALIZER (ally_drvdata.intf_mutex), +/* +* Initialised statically so it is always safe to cancel, whichever +* of the interfaces probed or failed to probe. +*/ + .resume_work =__DELAYED_WORK_INITIALIZER (ally_drvdata.resume_work, + ally_resume_work_fn,0 ), +}; + +/* +* Drop the recorded USB device if no Ally interface is bound anymore and +* none is initializing either: probe failures never run remove(), so +* without this cleanup a failed probe would leave a stale record behind +* and the driver would then reject the real controller when it +* reconnects. A probe whose role is reserved still needs the record. +*/ +static void ally_put_udev_if_orphaned (void ) +{ +struct usb_device *udev; +unsigned long flags; + +spin_lock_irqsave (&ally_data_lock, flags); + if (!ally_drvdata.udev || ally_drvdata.keyboard_hdev || + ally_drvdata.cfg_hdev || ally_drvdata.ally_x_hdev ||
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 376
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ ally_drvdata.cfg_probe_in_progress || + ally_drvdata.ally_x_probe_in_progress || + ally_drvdata.keyboard_probe_in_progress) { +spin_unlock_irqrestore (&ally_data_lock, flags); +return ; + } + + udev = ally_drvdata.udev; + ally_drvdata.udev =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags);
Please use scoped guard() so duplicating unlock is avoided.
Quoted 39 lines
drivers/hid/hid-asus.c +33
-0+ +usb_put_dev (udev); +} + +/* +* Release the role reservation taken by hid_asus_ally_probe(): a failed +* probe must free the role again, or every later probe of that role +* would be rejected. A successful probe releases it when it publishes +* its pointer. +*/ +static void ally_probe_release_role (int ep) +{ +unsigned long flags; + +spin_lock_irqsave (&ally_data_lock, flags); +if (ep ==HID_ALLY_INTF_CFG_IN ) + ally_drvdata.cfg_probe_in_progress = false; +else if (ep ==HID_ALLY_X_INTF_IN ) + ally_drvdata.ally_x_probe_in_progress = false; +else if (ep ==HID_ALLY_INTF_KEYBOARD_IN ) + ally_drvdata.keyboard_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +} +static const u8 asus_report_id_init[] = {FEATURE_KBD_REPORT_ID ,FEATURE_KBD_LED_REPORT_ID1 ,FEATURE_KBD_LED_REPORT_ID2 }; +static inline int ally_dev_set_report (struct hid_device *hdev,const u8 *buf,size_t len) +{ + u8 *dmabuf __free(kfree) = kmemdup(buf, len, GFP_KERNEL); +if (!dmabuf) +return -ENOMEM ; + +return hid_hw_raw_request (hdev, buf[0 ], dmabuf, len, +HID_FEATURE_REPORT ,HID_REQ_SET_REPORT ); +}
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 425
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +static inline int ally_dev_get_report (struct hid_device *hdev,u8 *out,size_t len) +{ +return hid_hw_raw_request (hdev,HID_ALLY_GET_REPORT_ID , out, len, +HID_FEATURE_REPORT ,HID_REQ_GET_REPORT ); +} + +static void ally_resume_work_fn (struct work_struct *work) +{ + struct ally_handheld *ally = container_of(work, struct ally_handheld,
Include for container_of().
Quoted 41 lines
drivers/hid/hid-asus.c +41
-0+ resume_work.work); +struct input_dev *keyboard_input, *x_input; +unsigned long flags; + +/* +* Snapshot the very pointers that get dereferenced under the lock +* protecting them, and take a reference on each input_dev: probe +* sets keyboard_hdev even when the interface exposes no input_dev, +* removal clears the two fields one after the other, and the input +* devices are freed with their own interface. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + keyboard_input =input_get_device (ally->keyboard_input); + x_input =input_get_device (ally->ally_x_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* +* Force release all vendor buttons to prevent "stuck" ghosting on +* resume (workaround for Ally X USB re-probing during suspend/resume). +*/ +if (keyboard_input) { +input_report_key (keyboard_input,KEY_F16 ,0 ); +input_report_key (keyboard_input,KEY_F17 ,0 ); +input_report_key (keyboard_input,KEY_F18 ,0 ); +input_report_key (keyboard_input,KEY_F19 ,0 ); +input_report_key (keyboard_input,KEY_F20 ,0 ); +input_report_key (keyboard_input,KEY_PROG1 ,0 ); +input_sync (keyboard_input); +input_put_device (keyboard_input); + } + +if (x_input) { +input_report_key (x_input,KEY_F16 ,0 ); +input_report_key (x_input,KEY_F17 ,0 ); +input_report_key (x_input,KEY_F18 ,0 ); +input_report_key (x_input,KEY_PROG1 ,0 ); +input_sync (x_input); +input_put_device (x_input); + } +} +
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 476
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+/** +* handle_ctrl_alt_del() - detect a left button long press +* @ally: ally handheld structure holding the sequence state +* @data: raw report buffer, rewritten in place when the sequence matches +* @size: length of @data in bytes +* +* The Ally left button emits a sequence of ctrl+alt+del events. Capture that +* and emit only a single code for that single event. +* +* Return: true iff the event has been managed
Add .
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 482
Quoted 6 lines
drivers/hid/hid-asus.c +6
-0+*/ +static bool handle_ctrl_alt_del (struct ally_handheld *ally,u8 *data,int size) +{ +bool time_is_past =time_after (jiffies, ally->cad_last_event_time +msecs_to_jiffies (100 )); + +if (size <16 || data[0 ] !=0x01 )
You're conjuring...
Quoted 36 lines
drivers/hid/hid-asus.c +36
-0+return false; + +if (ally->cad_sequence_state >0 && time_is_past) + ally->cad_sequence_state =0 ; + + ally->cad_last_event_time = jiffies; + +switch (ally->cad_sequence_state) { +case 0 : +if (data[1 ] ==0x01 && data[2 ] ==0x00 && data[3 ] ==0x00 ) { + ally->cad_sequence_state =1 ; + data[1 ] =0x00 ; +return true; + } +break ; +case 1 : +if (data[1 ] ==0x05 && data[2 ] ==0x00 && data[3 ] ==0x00 ) { + ally->cad_sequence_state =2 ; + data[1 ] =0x00 ; +return true; + } +break ; +case 2 : +if (data[1 ] ==0x05 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =3 ; + data[1 ] =0x00 ; + data[3 ] =0x6F ;/* F20 */ +return true; + } +break ; +case 3 : +if (data[1 ] ==0x04 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =4 ; + data[1 ] =0x00 ; + data[3 ] =0x00 ; +return true;
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 528
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ } +break ; +case 4 : +if (data[1 ] ==0x00 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =5 ; + data[3 ] =0x00 ; +return true; + } +break ; + }
...There were lots of literals in this function. I suggest naming most of them with defines for better readability.
Quoted 8 lines
drivers/hid/hid-asus.c +8
-0+ ally->cad_sequence_state =0 ; +return false; +} + +static bool handle_ally_event (struct hid_device *hdev,struct ally_handheld *ally, +u8 *data,int size) +{ +struct input_dev *keyboard_input;
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 546
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+unsigned long flags; +int keycode =0 ; + +if (size <2 ) +return false; + +if (data[0 ] ==0x5A ) { +switch (data[1 ]) { +case 0x38 : + keycode =KEY_F19 ;
Here's a good example for what I'm after:
That data[0] == 0x5A line tells me nothing what the check is for (I guess the same goes for most readers of this code!). If you'd name 0x5A with a define, the problem would likely vanish, I'm not sure if [0] needs name too or not.
In contrast, the case + keycode combination might not necessarily need naming 0x38 because the meaning can be inherited (I don't mind if you name 0x38 as well but it doesn't seem as big problem as 0x5A).
Quoted 62 lines
drivers/hid/hid-asus.c +62
-0+break ; +case 0xA6 : + keycode =KEY_F16 ; +break ; +case 0xA7 : + keycode =KEY_F17 ; +break ; +case 0xA8 : + keycode =KEY_F18 ; +break ; +default : +return false; + } + +/* +* Take a reference on the input_dev before dropping the lock: +* the keyboard interface can be unbound concurrently, and its +* input_dev is freed with it. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + keyboard_input =input_get_device (ally->keyboard_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (!keyboard_input) +return false; + +input_report_key (keyboard_input, keycode,1 ); +input_sync (keyboard_input); +input_report_key (keyboard_input, keycode,0 ); +input_sync (keyboard_input); +input_put_device (keyboard_input); +return true; + } +return false; +} + +/** +* ally_gamepad_send_packet() - Send a raw packet to the gamepad device +* @ally: ally handheld structure +* @hdev: HID device +* @buf: buffer containing the packet data +* @len: length of data to send +* +* Return: count of data transferred, negative if error +*/ +static int ally_gamepad_send_packet (struct ally_handheld *ally, +struct hid_device *hdev,const u8 *buf,size_t len) +{ +guard (mutex)(&ally->intf_mutex); +return ally_dev_set_report (hdev, buf, len); +} + +/** +* ally_gamepad_send_receive_packet() - Send a packet and receive the response +* @ally: ally handheld structure +* @hdev: HID device +* @buf: buffer containing the packet data to send and receive response in +* @len: length of buffer +* +* Return: count of data transferred, negative if error +*/ +static int ally_gamepad_send_receive_packet(struct ally_handheld *ally,
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 618
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+struct hid_device *hdev, + u8 *buf, size_t len) +{ + int ret; + + scoped_guard(mutex, &ally->intf_mutex) { + ret =ally_dev_set_report (hdev, buf, len); +if (ret >=0 ) { +memset (buf,0 , len); + ret =ally_dev_get_report (hdev, buf, len);
Thanks to using guard, you can revese the logic, handle errors first and return immediately, in both cases.
Also, this function doesn't need scoped_guard() but could use plain guard()
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 622
Quoted 4 lines
drivers/hid/hid-asus.c +4
-0+ } + } + +return ret;
This becomes plain return 0; when you return errors right away
=> simpler code flow.
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 635
Quoted 13 lines
drivers/hid/hid-asus.c +13
-0+} + +/** +* ally_alloc_cmd() - Construct a command buffer for the gamepad +* @cmd: Command code to send +* @payload: Optional payload data to include in the command +* @payload_size: Size of the payload data +* +* The constructed buffer is 64 bytes long, and it is the caller +* responsibility to free the buffer using kfree(). +* +* Return: the newly allocated buffer containing the command, or NULL on +* allocation failure or if the payload would not fit in the report
Add .
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 645
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+*/ +static u8 *ally_alloc_cmd (u8 cmd,const u8 *payload,u8 payload_size) +{ +u8 *hidbuf; + +/* +* The payload is written after the 4-byte command header: +* reject one that would spill past the end of the report. +*/ +if (payload_size >ROG_ALLY_REPORT_SIZE -4 )
Name 4 with define or make a struct so you can use sizeof().
Quoted 22 lines
drivers/hid/hid-asus.c +22
-0+return NULL ; + + hidbuf =kzalloc (ROG_ALLY_REPORT_SIZE ,GFP_KERNEL ); + +if (!hidbuf) +return NULL ; + + hidbuf[0 ] =HID_ALLY_SET_REPORT_ID ; + hidbuf[1 ] =HID_ALLY_FEATURE_CODE_PAGE ; + hidbuf[2 ] = cmd; + hidbuf[3 ] = payload_size; + +if (payload_size >0 && payload) +memcpy (&hidbuf[4 ], payload, payload_size); + +return hidbuf; +} + +/** +* ally_gamepad_check_ready() - Wait for the gamepad MCU to report ready +* @ally: ally handheld structure +* @hdev: HID device
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 677
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* +* This should be called before any remapping attempts, and on driver +* init/resume, after the asus handshake has been performed on the +* configuration endpoint. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_gamepad_check_ready (struct ally_handheld *ally,struct hid_device *hdev) +{ +u8 payload[] = {0x00 };
= {} is enough to initialize.
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 685
Quoted 8 lines
drivers/hid/hid-asus.c +8
-0+int ret; + +for (int i =0 ; i <HID_ALLY_READY_MAX_TRIES ; i++) { + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_CHECK_READY, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_receive_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE );
ally_gamepad_send_packet()'s buf is const so why you need to alloc it over and over again?
Arguably, I'm not sure if this retry loop is expected to execute many times.
Quoted 126 lines
drivers/hid/hid-asus.c +126
-0+if (ret <0 ) { +hid_dbg (hdev,"ROG Ally check %d/%d failed: %d\n" , i, +HID_ALLY_READY_MAX_TRIES , ret); +continue ; + } + +if (buf[2 ] ==CMD_CHECK_READY ) +return 0 ; + +usleep_range (1000 ,2000 ); + } + +hid_err (hdev,"ROG Ally never responded with a ready\n" ); +return -ENODEV ; +} + +static int ally_get_endpoint_address (struct hid_device *hdev) +{ +struct usb_host_endpoint *ep; +struct usb_interface *intf; + +if (!hid_is_usb (hdev)) +return -ENODEV ; + + intf =to_usb_interface (hdev->dev.parent); +if (!intf || !intf->cur_altsetting) +return -ENODEV ; + +if (intf->cur_altsetting->desc.bNumEndpoints ==0 ) +return -ENODEV ; + + ep = intf->cur_altsetting->endpoint; +if (!ep) +return -ENODEV ; + +return ep->desc.bEndpointAddress; +} + +struct ally_x_input_report { +__le16 x, y; +__le16 rx, ry; +__le16 z, rz; +u8 buttons[3 ]; +} __packed; + +/* The hatswitch outputs integers, we use them to index this X|Y pair */ +static const int hat_values[][2 ] = { + {0 ,0 }, {0 ,-1 }, {1 ,-1 }, {1 ,0 }, {1 ,1 }, + {0 ,1 }, {-1 ,1 }, {-1 ,0 }, {-1 ,-1 }, +}; + +/** +* ally_x_raw_event() - Parse and report an Ally X gamepad input report +* @input: input device to report the events on +* @data: raw report buffer +* @size: length of @data in bytes +* +* Return: true if the event was handled, otherwise false +*/ +static bool ally_x_raw_event (struct input_dev *input,u8 *data,int size) +{ +struct ally_x_input_report *in_report; +u16 x, y, rx, ry, z, rz; +u8 byte; + +if (!input) +return false; + +if (size <1 ) +return false; + +/* +* The size check above only guarantees one valid byte: do not +* read the code of a truncated report. +*/ +if (data[0 ] ==0x5A ) { +if (size <2 ) +return false; + +input_report_key (input,KEY_PROG1 , data[1 ] ==0x38 ); +input_report_key (input,KEY_F16 , data[1 ] ==0xA6 ); +input_report_key (input,KEY_F17 , data[1 ] ==0xA7 ); +input_report_key (input,KEY_F18 , data[1 ] ==0xA8 ); +input_sync (input); + +return data[1 ] ==0xA6 || data[1 ] ==0xA7 || data[1 ] ==0xA8 || data[1 ] ==0x38 ; + } + +if (data[0 ] !=HID_ALLY_X_INPUT_REPORT ) +return false; + +/* +* hid-core only guarantees size >= 1 and does not zero-pad short +* reports before ->raw_event, so a truncated transfer would leave the +* payload below pointing at stale DMA buffer contents. +*/ +if (size <1 +sizeof (*in_report)) +return false; + + in_report = (struct ally_x_input_report *)&data[1 ]; + +/* USB HID payloads are little-endian: convert them explicitly. */ + x =get_unaligned_le16 (&in_report->x); + y =get_unaligned_le16 (&in_report->y); + rx =get_unaligned_le16 (&in_report->rx); + ry =get_unaligned_le16 (&in_report->ry); + z =get_unaligned_le16 (&in_report->z); + rz =get_unaligned_le16 (&in_report->rz); + +input_report_abs (input,ABS_X , x -32768 ); +input_report_abs (input,ABS_Y , y -32768 ); +input_report_abs (input,ABS_RX , rx -32768 ); +input_report_abs (input,ABS_RY , ry -32768 ); +input_report_abs (input,ABS_Z , z); +input_report_abs (input,ABS_RZ , rz); + + byte = in_report->buttons[0 ]; +input_report_key (input,BTN_A , byte &BIT (0 )); +input_report_key (input,BTN_B , byte &BIT (1 )); +input_report_key (input,BTN_X , byte &BIT (2 )); +input_report_key (input,BTN_Y , byte &BIT (3 )); +input_report_key (input,BTN_TL , byte &BIT (4 )); +input_report_key (input,BTN_TR , byte &BIT (5 )); +input_report_key (input,BTN_SELECT , byte &BIT (6 )); +input_report_key (input,BTN_START , byte &BIT (7 )); +
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 821
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ byte = in_report->buttons[1 ]; +input_report_key (input,BTN_THUMBL , byte &BIT (0 )); +input_report_key (input,BTN_THUMBR , byte &BIT (1 )); +input_report_key (input,BTN_MODE , byte &BIT (2 )); + +/* The hatswitch byte is device-controlled; treat anything the table +* does not cover as centred rather than indexing out of bounds. +*/ + byte = in_report->buttons[2 ]; +if (byte >=ARRAY_SIZE (hat_values))
Add include.
Quoted 17 lines
drivers/hid/hid-asus.c +17
-0+ byte =0 ; +input_report_abs (input,ABS_HAT0X , hat_values[byte][0 ]); +input_report_abs (input,ABS_HAT0Y , hat_values[byte][1 ]); + +input_sync (input); + +return true; +} + +static int ally_x_input_open (struct input_dev *dev) +{ +struct hid_device *hdev =input_get_drvdata (dev); + +return hid_hw_open (hdev); +} + +static void ally_x_input_close (struct input_dev *dev)
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 848
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+{ +hid_hw_close (input_get_drvdata (dev)); +} + +static struct input_dev *ally_x_alloc_input_dev (struct hid_device *hdev) +{ +struct input_dev *input_dev =input_allocate_device (); + +if (!input_dev) +return ERR_PTR (-ENOMEM );
Add include.
Quoted 71 lines
drivers/hid/hid-asus.c +71
-0+ + input_dev->dev.parent = &hdev->dev; + input_dev->id.bustype = hdev->bus; + input_dev->id.vendor = hdev->vendor; + input_dev->id.product = hdev->product; + input_dev->id.version = hdev->version; + input_dev->uniq = hdev->uniq; + input_dev->name ="ASUS ROG Ally X Gamepad" ; + +input_set_drvdata (input_dev, hdev); +/* +* Let the input core hold the hardware open while the device node +* is in use: without these the interface could be runtime-suspended +* and drop events. +*/ + input_dev->open = ally_x_input_open; + input_dev->close = ally_x_input_close; + +return input_dev; +} + +static int ally_x_setup_input (struct hid_device *hdev,struct ally_handheld *ally) +{ +struct input_dev *input =ally_x_alloc_input_dev (hdev); +unsigned long flags; +int ret; + +if (IS_ERR (input)) +return PTR_ERR (input); + +input_set_abs_params (input,ABS_X ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_Y ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_RX ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_RY ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_Z ,0 ,1023 ,0 ,0 ); +input_set_abs_params (input,ABS_RZ ,0 ,1023 ,0 ,0 ); +input_set_abs_params (input,ABS_HAT0X ,-1 ,1 ,0 ,0 ); +input_set_abs_params (input,ABS_HAT0Y ,-1 ,1 ,0 ,0 ); +input_set_capability (input,EV_KEY ,BTN_A ); +input_set_capability (input,EV_KEY ,BTN_B ); +input_set_capability (input,EV_KEY ,BTN_X ); +input_set_capability (input,EV_KEY ,BTN_Y ); +input_set_capability (input,EV_KEY ,BTN_TL ); +input_set_capability (input,EV_KEY ,BTN_TR ); +input_set_capability (input,EV_KEY ,BTN_SELECT ); +input_set_capability (input,EV_KEY ,BTN_START ); +input_set_capability (input,EV_KEY ,BTN_MODE ); +input_set_capability (input,EV_KEY ,BTN_THUMBL ); +input_set_capability (input,EV_KEY ,BTN_THUMBR ); + +input_set_capability (input,EV_KEY ,KEY_PROG1 ); +input_set_capability (input,EV_KEY ,KEY_F16 ); +input_set_capability (input,EV_KEY ,KEY_F17 ); +input_set_capability (input,EV_KEY ,KEY_F18 ); +input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY ); +input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY1 ); + + ret =input_register_device (input); +if (ret) { +hid_err (hdev,"Failed to register Ally X gamepad device: %d\n" , ret); + goto ally_x_setup_input_err; + } + +/* Publish the input_dev only when it is fully set up. */ +spin_lock_irqsave (&ally_data_lock, flags); + ally->ally_x_input = input; +spin_unlock_irqrestore (&ally_data_lock, flags); + +return 0 ; +ally_x_setup_input_err: +input_free_device (input);
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 929
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+return ret; +} + +static int hid_asus_ally_init (struct hid_device *hdev,struct ally_handheld *ally) +{ +int ret; + +/* Failure at this point is non-critical */ + ret =ally_gamepad_send_packet (ally, hdev,ALLY_FORCE_FEEDBACK_OFF , +sizeof (ALLY_FORCE_FEEDBACK_OFF ));
Don't name a real array with caps.
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+if (ret <0 ) +hid_err (hdev,"Ally failed to init force-feedback off: %d\n" , ret); + +return 0 ; +} + +/** +* hid_asus_ally_raw_event() - Route raw reports from the Ally interfaces +* @hdev: HID device
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 948
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* @ally: ally handheld structure +* @report: HID report the raw data arrived in +* @data: raw report buffer +* @size: length of @data in bytes +* +* Called from the raw_event callback, which may run in atomic (URB +* completion) context: only spinlock-protected accesses to the shared +* state are allowed here. +* +* Return: true if the event was handled, otherwise false
.
Quoted 13 lines
drivers/hid/hid-asus.c +13
-0+*/ +static bool hid_asus_ally_raw_event (struct hid_device *hdev,struct ally_handheld *ally, +struct hid_report *report,u8 *data,int size) +{ +struct input_dev *x_input; +unsigned long flags; +bool handled; + +if (!ally) +return false; + +switch (ally_get_endpoint_address (hdev)) { +case HID_ALLY_X_INTF_IN :
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 971
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+/* +* Take a reference on the input_dev while using it: the +* gamepad interface can be unbound concurrently, and its +* input_dev is freed with it. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + x_input =input_get_device (ally->ally_x_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + + handled =ally_x_raw_event (x_input, data, size);
Since ypu've dropped the lock, couldn't things change right away after the unlock?
Perhaps there's something else that prevents problems but I generally feel unconvinced by how ally_data_lock is used. Given what I see, ally_data_lock feels either unnecessarily (or is an overkill for where a barrier would be enough to synchronize with the init) or doesn't protect everything properly (if uninit can race with this code).
Quoted 46 lines
drivers/hid/hid-asus.c +46
-0+input_put_device (x_input); +if (handled) +return true; +break ; +case HID_ALLY_INTF_CFG_IN : +if (handle_ally_event (hdev, ally, data, size)) +return true; +break ; +case HID_ALLY_INTF_KEYBOARD_IN : +if (handle_ctrl_alt_del (ally, data, size)) +return false; +break ; +default : +break ; + } + +return false; +} + +/** +* hid_asus_ally_probe() - Initialize the ROG Ally HID extension +* @hdev: HID device +* +* This module works alongside the main Asus HID driver to handle +* Ally-specific features and quirks. +* +* Return: an ally_handheld struct pointer on success, an ERR_PTR on +* failure. The caller is not expected to use the returned pointer, but it +* should check for errors by using IS_ERR and PTR_ERR and pass NULL to +* other functions if there was an error. +*/ +static struct ally_handheld *hid_asus_ally_probe (struct hid_device *hdev) +{ +unsigned long flags; +int ret, ep =ally_get_endpoint_address (hdev); +struct usb_device *udev; +struct hid_input *hidinput; + +if (ep <0 ) +return ERR_PTR (ep); + +/* +* The ROG Ally controller is integrated into a handheld PC, so at +* most one device can exist and the shared global state relies on +* that: reject a probe from a different USB device, or a spoofed +* peripheral could overwrite the state of the real controller. The
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1027
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* recorded device is dropped when the last interface of the +* controller is unbound, so the re-enumeration the embedded +* controller performs after a suspend cycle (it cuts the controller +* power when powersave is enabled) is still accepted. +*/ + udev =interface_to_usbdev (to_usb_interface (hdev->dev.parent)); + +spin_lock_irqsave (&ally_data_lock, flags); +if (ally_drvdata.udev && ally_drvdata.udev != udev) { +spin_unlock_irqrestore (&ally_data_lock, flags);
scoped_guard() would avoid having to duplicate unlock but I'm not sure if hid_err() is happy being called with a spinlock held...
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1029
Quoted 2 lines
drivers/hid/hid-asus.c +2
-0+hid_err (hdev,"A ROG Ally controller is already connected\n" ); +return ERR_PTR (-ENODEV );
Shouldn't this be -EBUSY instead (the error message hints to that)?
Quoted 11 lines
drivers/hid/hid-asus.c +11
-0+ } + +/* +* Interfaces of an already connected controller are equally +* untrusted: a crafted descriptor could present the same +* endpoint twice, and a second probe of one role would evict +* the state recorded by the first one, whose input_dev would +* then never be unregistered. The probe reservations make the +* check hold across the sleeping initialization below, which +* runs before the role pointer is published. +*/
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1050
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+if ((ep ==HID_ALLY_INTF_CFG_IN && + (ally_drvdata.cfg_hdev || ally_drvdata.cfg_probe_in_progress)) || + (ep ==HID_ALLY_X_INTF_IN && + (ally_drvdata.ally_x_hdev || ally_drvdata.ally_x_probe_in_progress)) || + (ep ==HID_ALLY_INTF_KEYBOARD_IN && + (ally_drvdata.keyboard_hdev || ally_drvdata.keyboard_probe_in_progress))) { +spin_unlock_irqrestore (&ally_data_lock, flags); +hid_err (hdev, +"An interface of a ROG Ally controller is already connected\n" ); +return ERR_PTR (-ENODEV );
Same here?
Quoted 23 lines
drivers/hid/hid-asus.c +23
-0+ } + +/* Reserve the role for as long as the initialization below runs. */ +if (ep ==HID_ALLY_INTF_CFG_IN ) + ally_drvdata.cfg_probe_in_progress = true; +else if (ep ==HID_ALLY_X_INTF_IN ) + ally_drvdata.ally_x_probe_in_progress = true; +else if (ep ==HID_ALLY_INTF_KEYBOARD_IN ) + ally_drvdata.keyboard_probe_in_progress = true; + +if (!ally_drvdata.udev) { +usb_get_dev (udev); + ally_drvdata.udev = udev; + } +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* +* The interface initialization sleeps (it performs USB transfers), +* so it must not run under ally_data_lock: the role stays reserved +* while it runs, and the pointers are published only once it +* succeeded. +*/ +switch (ep) {
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1083
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+case HID_ALLY_INTF_CFG_IN : +/* +* This function assumes the asus-specific initialization +* to have been performed already at this point. +*/ + ret =ally_gamepad_check_ready (&ally_drvdata, hdev); +if (ret <0 ) { +hid_err (hdev,"ROG Ally device is not ready: %d\n" , ret); +ally_probe_release_role (ep); +ally_put_udev_if_orphaned ();
How about sharing the rollback path, there seems to be multiple of these.
Quoted 104 lines
drivers/hid/hid-asus.c +104
-0+return ERR_PTR (ret); + } + + ret =hid_asus_ally_init (hdev, &ally_drvdata); +if (ret <0 ) { +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (ret); + } + +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.cfg_hdev = hdev; + ally_drvdata.cfg_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +case HID_ALLY_X_INTF_IN : +/* This will create and populate ally_x_input */ + ret =ally_x_setup_input (hdev, &ally_drvdata); +if (ret) { +hid_err (hdev,"Failed to create Ally X gamepad device.\n" ); +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (ret); + } + +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.ally_x_hdev = hdev; + ally_drvdata.ally_x_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +case HID_ALLY_INTF_KEYBOARD_IN : +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.keyboard_hdev = hdev; + ally_drvdata.keyboard_probe_in_progress = false; +if (!list_empty (&hdev->inputs)) { + hidinput = list_first_entry(&hdev->inputs, struct hid_input, list); + ally_drvdata.keyboard_input = hidinput->input; + } +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +default : +/* This is normally supposed to happen */ +break ; + } + +return &ally_drvdata; +} + +static void hid_asus_ally_remove (struct hid_device *hdev,struct ally_handheld *ally) +{ +struct input_dev *x_input =NULL ; +unsigned long flags; + +if (!ally) +return ; + +/* +* Any of the three interfaces can own an input_dev the resume work +* reports through, and they are torn down in an arbitrary order, so +* drain it before clearing anything. Cancel outside ally_data_lock so +* a handler that wants the lock cannot deadlock against us. +*/ +cancel_delayed_work_sync (&ally->resume_work); + +spin_lock_irqsave (&ally_data_lock, flags); +if (ally->ally_x_hdev == hdev) { + x_input = ally->ally_x_input; + ally->ally_x_input =NULL ; + ally->ally_x_hdev =NULL ; + } + +/* +* The keyboard interface is torn down before the config one, and +* its input_dev is freed with it. handle_ally_event() and +* ally_resume_work_fn() both report keys through it from the +* config endpoint, so drop the references here or they dangle. +*/ +if (ally->keyboard_hdev == hdev) { + ally->keyboard_input =NULL ; + ally->keyboard_hdev =NULL ; + } +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (x_input) +input_unregister_device (x_input); + +/* +* Drop the recorded USB device when the last interface of the +* controller has been unbound: the driver is then ready to accept +* the controller again when it reconnects. +*/ +ally_put_udev_if_orphaned (); +} + +static int hid_asus_ally_reset_resume (struct hid_device *hdev,struct ally_handheld *ally) +{ +int ep, ret; + +/* +* The extensions failed to probe and the device is operating as a +* generic HID device: do not fail the resume because of that. +*/ +if (!ally) +return 0 ;
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1197
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ + ep =ally_get_endpoint_address (hdev); +if (ep !=HID_ALLY_INTF_CFG_IN ) +return 0 ; + + ret =hid_asus_ally_init (hdev, ally); +if (ret <0 ) +return ret; + +return 0 ;
Why can't you just return directly what hid_asus_ally_init() returned?
Quoted 120 lines
drivers/hid/hid-asus.c +47
-0+} +/* * Send events to asus-wmi driver for handling special keys */ @@ -513,6 +1464,17 @@ static int asus_raw_event(struct hid_device *hdev,if (drvdata->quirks &QUIRK_MEDION_E1239T )return asus_e1239t_event (drvdata, data, size); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { +/* +* Return -1 to suppress further processing by the generic HID +* input parser for reports we fully handle for the Gamepad (0x0B): +* letting 0x0B reach the default parser creates a generic gamepad +* causing Steam Input overlaps (i.e. L1 stuck on screenshot). +*/ +if (hid_asus_ally_raw_event (hdev, drvdata->rog_ally, report, data, size)) +return -1 ; + } +/* * Skip these report ID, the device emits a continuous stream associated * with the AURA mode it is in which looks like an 'echo'. @@ -1306,6 +2268,7 @@ static int asus_input_mapping(struct hid_device *hdev,case 0x5f :asus_map_key_clear (KEY_PROG2 );break ;/* S-shaped programmable key */ case 0x6b :asus_map_key_clear (KEY_F21 );break ;/* ASUS touchpad toggle */ case 0x38 :asus_map_key_clear (KEY_PROG1 );break ;/* ROG key */ +case 0x93 :asus_map_key_clear (KEY_PROG1 );break ;/* ROG Ally X AC button */ case 0xba :asus_map_key_clear (KEY_PROG2 );break ;/* Fn+C ASUS Splendid */ case 0x5c :asus_map_key_clear (KEY_PROG3 );break ;/* Fn+Space Power4Gear */ case 0x99 :asus_map_key_clear (KEY_PROG4 );break ;/* Fn+F5 "fan" symbol */ @@ -1480,6 +2443,8 @@ static int asus_xgm_init(struct hid_device *hdev, struct asus_drvdata *drvdata)static int __maybe_unused asus_resume (struct hid_device *hdev) {struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +int ep;/* * If we have a backlight listener registered, restore the previous state, @@ -1489,12 +2454,19 @@ static int __maybe_unused asus_resume(struct hid_device *hdev)if (drvdata->listener.brightness_set)asus_kbd_backlight_set (&drvdata->listener, drvdata->kbd_backlight_brightness); +if (ally && (drvdata->quirks &QUIRK_ROG_ALLY_XPAD )) { + ep =ally_get_endpoint_address (hdev); +if (ep ==HID_ALLY_INTF_CFG_IN ) +schedule_delayed_work (&ally->resume_work,msecs_to_jiffies (500 )); + } +return 0 ; }static int __maybe_unused asus_reset_resume (struct hid_device *hdev) {struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +int ret;if (!drvdata->tp)asus_initialize_reports (hdev);@@ -1502,6 +2474,14 @@ static int __maybe_unused asus_reset_resume(struct hid_device *hdev)if (drvdata->tp)return asus_start_multitouch (hdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { + ret =hid_asus_ally_reset_resume (hdev, drvdata->rog_ally); +if (ret) { +hid_err (hdev,"Failed to resume ROG Ally HID extensions: %d\n" , ret); +return ret; + } + } +return 0 ; }@@ -1509,6 +2489,7 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id) {struct hid_report_enum *rep_enum;struct asus_drvdata *drvdata; +struct ally_handheld *ally;struct hid_report *rep;bool is_vendor = false;int ret;@@ -1632,6 +2613,15 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id) (asus_kbd_register_leds(hdev)))hid_warn (hdev,"Failed to initialize backlight.\n" ); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { + ally =hid_asus_ally_probe (hdev); +if (IS_ERR (ally)) +hid_err (hdev,"Failed to initialize ROG Ally HID extensions: %ld\n" , +PTR_ERR (ally)); +else + drvdata->rog_ally = ally; + } +/* * For ROG keyboards, skip rename for consistency and ->input check as * some devices do not have inputs. @@ -1666,6 +2656,9 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)if (drvdata->xgm_led)devm_led_classdev_unregister (&hdev->dev, &drvdata->xgm_led->cdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) +hid_asus_ally_remove (hdev, drvdata->rog_ally); +asus_worker_stop (drvdata->worker);hid_hw_stop (hdev);return ret;@@ -1681,6 +2674,9 @@ static void asus_remove(struct hid_device *hdev)if (drvdata->xgm_led)devm_led_classdev_unregister (&hdev->dev, &drvdata->xgm_led->cdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) +hid_asus_ally_remove (hdev, drvdata->rog_ally); +asus_worker_stop (drvdata->worker);hid_hw_stop (hdev); }
Signature · 1 line
i.
Denis Benato
maintainerQuoted 554 lines · Ilpo Järvinen
Quoted 8 lines · Denis Benato
Implement the core functionality for ASUS handhelds:
- extend functionality of hid-asus if driver is enabled
- initialise the device to a "ready" state
- implement Ally X input mapping
- emit a single proper event for AC button long press
- reject probes from a different controller and from duplicated interfaces of the same one, which could evict the shared stateQuoted 5 lines
Signed-off-by Khamunetri Clark <[email protected]>Signed-off-by Denis Benato <[email protected]>Signed-off-by Matthew Schwartz <[email protected]>Signed-off-by Luke Jones <[email protected]>Signed-off-by Jonathan LoBue <[email protected]>Shouldn't you be using some Co-developed-by tags too?
Quoted 177 lines
--- drivers/hid/hid-asus.c | 996 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 996 insertions(+)drivers/hid/hid-asus.c +124
-0@@ -24,12 +24,18 @@#include <linux/cleanup.h> #include <linux/dmi.h> #include <linux/hid.h> +#include <linux/jiffies.h> +#include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/platform_data/x86/asus-wmi.h> +#include <linux/types.h> #include <linux/input/mt.h> #include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */ #include <linux/power_supply.h> +#include <linux/stddef.h> #include <linux/leds.h> +#include <linux/unaligned.h> #include "hid-ids.h" @@ -37,6 +43,9 @@ MODULE_AUTHOR("Yusuke Fujimaki <[email protected]>");MODULE_AUTHOR ("Brendan McGrath <[email protected]>" );MODULE_AUTHOR ("Victor Vlasenko <[email protected]>" );MODULE_AUTHOR ("Frederik Wenigwieser <[email protected]>" ); +MODULE_AUTHOR ("Denis Benato <[email protected]>" ); +MODULE_AUTHOR ("Luke Jones <[email protected]>" ); +MODULE_AUTHOR ("Khamunetri Clark <[email protected]>" );MODULE_DESCRIPTION ("Asus HID Keyboard and TouchPad" );#define T100_TPAD_INTF 2@@ -57,6 +66,19 @@ MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");#define ROG_ALLY_X_MIN_MCU 313#define ROG_ALLY_MIN_MCU 319 +#define HID_ALLY_INTF_KEYBOARD_IN 0x81 +#define HID_ALLY_INTF_CFG_IN 0x83 +#define HID_ALLY_X_INTF_IN 0x87 + +#define HID_ALLY_GET_REPORT_ID 0x0D +#define HID_ALLY_SET_REPORT_ID 0x5A +#define HID_ALLY_FEATURE_CODE_PAGE 0xD1 + +#define HID_ALLY_X_INPUT_REPORT_SIZE 16 +#define HID_ALLY_X_INPUT_REPORT 0x0B + +#define HID_ALLY_READY_MAX_TRIES 6 +/* Spurious HID codes sent by QUIRK_ROG_NKEY_KEYBOARD devices */ #define ASUS_SPURIOUS_CODE_0XEA 0xea#define ASUS_SPURIOUS_CODE_0XEC 0xec@@ -161,6 +183,40 @@ struct asus_touchpad_info {int report_size; }; +struct ally_handheld { +/* All read/write to IN interfaces must lock */ +struct mutex intf_mutex; +/* +* USB device of the connected controller, held with a reference: +* at most one ROG Ally can exist, so probes from any other USB +* device are rejected to protect the shared state. +*/ +struct usb_device *udev; +struct hid_device *cfg_hdev; + +struct input_dev *ally_x_input; +struct hid_device *ally_x_hdev; +/* +* Probe reservations: set under ally_data_lock when a probe takes +* a role on, cleared when that probe publishes its pointer or +* fails, so a concurrent second probe of the same role is rejected +* instead of racing the first one through its sleeping +* initialization (or its check-to-publication window, for the +* keyboard role). +*/ +bool cfg_probe_in_progress; +bool ally_x_probe_in_progress; +bool keyboard_probe_in_progress; + +struct hid_device *keyboard_hdev; +struct input_dev *keyboard_input; + +u8 cad_sequence_state; +unsigned long cad_last_event_time; + +struct delayed_work resume_work; +}; +struct asus_drvdata {unsigned long quirks;struct hid_device *hdev;@@ -168,6 +224,7 @@ struct asus_drvdata {struct input_dev *tp_kbd_input;struct asus_worker *worker;unsigned int kbd_backlight_brightness; +struct ally_handheld *rog_ally;const struct asus_touchpad_info *tp;struct power_supply *battery;struct power_supply_desc battery_desc;@@ -240,12 +297,906 @@ static const struct asus_touchpad_info medion_e1239t_tp = { .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */, }; +enum ally_command_codes { +CMD_SET_GAMEPAD_MODE =0x01 , +CMD_SET_MAPPING =0x02 , +CMD_SET_JOYSTICK_MAPPING =0x03 , +CMD_SET_JOYSTICK_DEADZONE =0x04 , +CMD_SET_TRIGGER_RANGE =0x05 , +CMD_SET_VIBRATION_INTENSITY =0x06 , +CMD_LED_CONTROL =0x08 , +CMD_CHECK_READY =0x0A , +CMD_SET_XBOX_CONTROLLER =0x0B , +CMD_CHECK_XBOX_SUPPORT =0x0C , +CMD_USER_CAL_DATA =0x0D , +CMD_CHECK_USER_CAL_SUPPORT =0x0E , +CMD_SET_TURBO_PARAMS =0x0F , +CMD_CHECK_TURBO_SUPPORT =0x10 , +CMD_CHECK_RESP_CURVE_SUPPORT =0x12 , +CMD_SET_RESP_CURVE =0x13 , +CMD_CHECK_DIR_TO_BTN_SUPPORT =0x14 , +CMD_SET_GYRO_PARAMS =0x15 , +CMD_CHECK_GYRO_TO_JOYSTICK =0x16 , +CMD_CHECK_ANTI_DEADZONE =0x17 , +CMD_SET_ANTI_DEADZONE =0x18 , +}; + +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = { +0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB +}; + +/* +* The ROG Ally device presents multiple USB interfaces (keyboard, mouse, gamepad, +* and custom configuration interface) that bind to the same module. Since only +* one ROG Ally device can be connected at a time, we use a single global static +* ally_handheld structure to share state across these separate HID interfaces. +*/ +static void ally_resume_work_fn (struct work_struct *work); + +/* +* Changes to ally_drvdata must lock: the raw_event callbacks, which may +* run in atomic (URB completion) context, also take this lock, so it must +* be a spinlock. +*/ +static DEFINE_SPINLOCK (ally_data_lock); +static struct ally_handheld ally_drvdata = { + .intf_mutex =__MUTEX_INITIALIZER (ally_drvdata.intf_mutex), +/* +* Initialised statically so it is always safe to cancel, whichever +* of the interfaces probed or failed to probe. +*/ + .resume_work =__DELAYED_WORK_INITIALIZER (ally_drvdata.resume_work, + ally_resume_work_fn,0 ), +}; + +/* +* Drop the recorded USB device if no Ally interface is bound anymore and +* none is initializing either: probe failures never run remove(), so +* without this cleanup a failed probe would leave a stale record behind +* and the driver would then reject the real controller when it +* reconnects. A probe whose role is reserved still needs the record. +*/ +static void ally_put_udev_if_orphaned (void ) +{ +struct usb_device *udev; +unsigned long flags; + +spin_lock_irqsave (&ally_data_lock, flags); + if (!ally_drvdata.udev || ally_drvdata.keyboard_hdev || + ally_drvdata.cfg_hdev || ally_drvdata.ally_x_hdev ||drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 376
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ ally_drvdata.cfg_probe_in_progress || + ally_drvdata.ally_x_probe_in_progress || + ally_drvdata.keyboard_probe_in_progress) { +spin_unlock_irqrestore (&ally_data_lock, flags); +return ; + } + + udev = ally_drvdata.udev; + ally_drvdata.udev =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags);Please use scoped
guard()so duplicating unlock is avoided.Quoted 39 lines
drivers/hid/hid-asus.c +33
-0+ +usb_put_dev (udev); +} + +/* +* Release the role reservation taken by hid_asus_ally_probe(): a failed +* probe must free the role again, or every later probe of that role +* would be rejected. A successful probe releases it when it publishes +* its pointer. +*/ +static void ally_probe_release_role (int ep) +{ +unsigned long flags; + +spin_lock_irqsave (&ally_data_lock, flags); +if (ep ==HID_ALLY_INTF_CFG_IN ) + ally_drvdata.cfg_probe_in_progress = false; +else if (ep ==HID_ALLY_X_INTF_IN ) + ally_drvdata.ally_x_probe_in_progress = false; +else if (ep ==HID_ALLY_INTF_KEYBOARD_IN ) + ally_drvdata.keyboard_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +} +static const u8 asus_report_id_init[] = {FEATURE_KBD_REPORT_ID ,FEATURE_KBD_LED_REPORT_ID1 ,FEATURE_KBD_LED_REPORT_ID2 }; +static inline int ally_dev_set_report (struct hid_device *hdev,const u8 *buf,size_t len) +{ + u8 *dmabuf __free(kfree) = kmemdup(buf, len, GFP_KERNEL); +if (!dmabuf) +return -ENOMEM ; + +return hid_hw_raw_request (hdev, buf[0 ], dmabuf, len, +HID_FEATURE_REPORT ,HID_REQ_SET_REPORT ); +}drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 425
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +static inline int ally_dev_get_report (struct hid_device *hdev,u8 *out,size_t len) +{ +return hid_hw_raw_request (hdev,HID_ALLY_GET_REPORT_ID , out, len, +HID_FEATURE_REPORT ,HID_REQ_GET_REPORT ); +} + +static void ally_resume_work_fn (struct work_struct *work) +{ + struct ally_handheld *ally = container_of(work, struct ally_handheld,Include for
container_of().Quoted 41 lines
drivers/hid/hid-asus.c +41
-0+ resume_work.work); +struct input_dev *keyboard_input, *x_input; +unsigned long flags; + +/* +* Snapshot the very pointers that get dereferenced under the lock +* protecting them, and take a reference on each input_dev: probe +* sets keyboard_hdev even when the interface exposes no input_dev, +* removal clears the two fields one after the other, and the input +* devices are freed with their own interface. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + keyboard_input =input_get_device (ally->keyboard_input); + x_input =input_get_device (ally->ally_x_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* +* Force release all vendor buttons to prevent "stuck" ghosting on +* resume (workaround for Ally X USB re-probing during suspend/resume). +*/ +if (keyboard_input) { +input_report_key (keyboard_input,KEY_F16 ,0 ); +input_report_key (keyboard_input,KEY_F17 ,0 ); +input_report_key (keyboard_input,KEY_F18 ,0 ); +input_report_key (keyboard_input,KEY_F19 ,0 ); +input_report_key (keyboard_input,KEY_F20 ,0 ); +input_report_key (keyboard_input,KEY_PROG1 ,0 ); +input_sync (keyboard_input); +input_put_device (keyboard_input); + } + +if (x_input) { +input_report_key (x_input,KEY_F16 ,0 ); +input_report_key (x_input,KEY_F17 ,0 ); +input_report_key (x_input,KEY_F18 ,0 ); +input_report_key (x_input,KEY_PROG1 ,0 ); +input_sync (x_input); +input_put_device (x_input); + } +} +drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 476
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+/** +* handle_ctrl_alt_del() - detect a left button long press +* @ally: ally handheld structure holding the sequence state +* @data: raw report buffer, rewritten in place when the sequence matches +* @size: length of @data in bytes +* +* The Ally left button emits a sequence of ctrl+alt+del events. Capture that +* and emit only a single code for that single event. +* +* Return: true iff the event has been managed Add .
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 482
Quoted 6 lines
drivers/hid/hid-asus.c +6
-0+*/ +static bool handle_ctrl_alt_del (struct ally_handheld *ally,u8 *data,int size) +{ +bool time_is_past =time_after (jiffies, ally->cad_last_event_time +msecs_to_jiffies (100 )); + +if (size <16 || data[0 ] !=0x01 )You're conjuring...
Quoted 36 lines
drivers/hid/hid-asus.c +36
-0+return false; + +if (ally->cad_sequence_state >0 && time_is_past) + ally->cad_sequence_state =0 ; + + ally->cad_last_event_time = jiffies; + +switch (ally->cad_sequence_state) { +case 0 : +if (data[1 ] ==0x01 && data[2 ] ==0x00 && data[3 ] ==0x00 ) { + ally->cad_sequence_state =1 ; + data[1 ] =0x00 ; +return true; + } +break ; +case 1 : +if (data[1 ] ==0x05 && data[2 ] ==0x00 && data[3 ] ==0x00 ) { + ally->cad_sequence_state =2 ; + data[1 ] =0x00 ; +return true; + } +break ; +case 2 : +if (data[1 ] ==0x05 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =3 ; + data[1 ] =0x00 ; + data[3 ] =0x6F ;/* F20 */ +return true; + } +break ; +case 3 : +if (data[1 ] ==0x04 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =4 ; + data[1 ] =0x00 ; + data[3 ] =0x00 ; +return true;drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 528
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ } +break ; +case 4 : +if (data[1 ] ==0x00 && data[2 ] ==0x00 && data[3 ] ==0x4c ) { + ally->cad_sequence_state =5 ; + data[3 ] =0x00 ; +return true; + } +break ; + }...There were lots of literals in this function. I suggest naming most of them with defines for better readability.
Quoted 8 lines
drivers/hid/hid-asus.c +8
-0+ ally->cad_sequence_state =0 ; +return false; +} + +static bool handle_ally_event (struct hid_device *hdev,struct ally_handheld *ally, +u8 *data,int size) +{ +struct input_dev *keyboard_input;drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 546
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+unsigned long flags; +int keycode =0 ; + +if (size <2 ) +return false; + +if (data[0 ] ==0x5A ) { +switch (data[1 ]) { +case 0x38 : + keycode =KEY_F19 ;Here's a good example for what I'm after:
That data[0] ==
0x5Aline tells me nothing what the check is for (I guess the same goes for most readers of this code!). If you'd name0x5Awith a define, the problem would likely vanish, I'm not sure if [0] needs name too or not.In contrast, the case + keycode combination might not necessarily need naming
0x38because the meaning can be inherited (I don't mind if you name0x38as well but it doesn't seem as big problem as0x5A).Quoted 62 lines
drivers/hid/hid-asus.c +62
-0+break ; +case 0xA6 : + keycode =KEY_F16 ; +break ; +case 0xA7 : + keycode =KEY_F17 ; +break ; +case 0xA8 : + keycode =KEY_F18 ; +break ; +default : +return false; + } + +/* +* Take a reference on the input_dev before dropping the lock: +* the keyboard interface can be unbound concurrently, and its +* input_dev is freed with it. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + keyboard_input =input_get_device (ally->keyboard_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (!keyboard_input) +return false; + +input_report_key (keyboard_input, keycode,1 ); +input_sync (keyboard_input); +input_report_key (keyboard_input, keycode,0 ); +input_sync (keyboard_input); +input_put_device (keyboard_input); +return true; + } +return false; +} + +/** +* ally_gamepad_send_packet() - Send a raw packet to the gamepad device +* @ally: ally handheld structure +* @hdev: HID device +* @buf: buffer containing the packet data +* @len: length of data to send +* +* Return: count of data transferred, negative if error +*/ +static int ally_gamepad_send_packet (struct ally_handheld *ally, +struct hid_device *hdev,const u8 *buf,size_t len) +{ +guard (mutex)(&ally->intf_mutex); +return ally_dev_set_report (hdev, buf, len); +} + +/** +* ally_gamepad_send_receive_packet() - Send a packet and receive the response +* @ally: ally handheld structure +* @hdev: HID device +* @buf: buffer containing the packet data to send and receive response in +* @len: length of buffer +* +* Return: count of data transferred, negative if error +*/ +static int ally_gamepad_send_receive_packet(struct ally_handheld *ally,drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 618
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+struct hid_device *hdev, + u8 *buf, size_t len) +{ + int ret; + + scoped_guard(mutex, &ally->intf_mutex) { + ret =ally_dev_set_report (hdev, buf, len); +if (ret >=0 ) { +memset (buf,0 , len); + ret =ally_dev_get_report (hdev, buf, len);Thanks to using guard, you can revese the logic, handle errors first and return immediately, in both cases.
Also, this function doesn't need
scoped_guard()but could use plainguard()drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 622
Quoted 4 lines
drivers/hid/hid-asus.c +4
-0+ } + } + +return ret;This becomes plain return 0; when you return errors right away
=> simpler code flow.
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 635
Quoted 13 lines
drivers/hid/hid-asus.c +13
-0+} + +/** +* ally_alloc_cmd() - Construct a command buffer for the gamepad +* @cmd: Command code to send +* @payload: Optional payload data to include in the command +* @payload_size: Size of the payload data +* +* The constructed buffer is 64 bytes long, and it is the caller +* responsibility to free the buffer using kfree(). +* +* Return: the newly allocated buffer containing the command, or NULL on +* allocation failure or if the payload would not fit in the report Add .
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 645
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+*/ +static u8 *ally_alloc_cmd (u8 cmd,const u8 *payload,u8 payload_size) +{ +u8 *hidbuf; + +/* +* The payload is written after the 4-byte command header: +* reject one that would spill past the end of the report. +*/ +if (payload_size >ROG_ALLY_REPORT_SIZE -4 )Name 4 with define or make a struct so you can use
sizeof().Quoted 22 lines
drivers/hid/hid-asus.c +22
-0+return NULL ; + + hidbuf =kzalloc (ROG_ALLY_REPORT_SIZE ,GFP_KERNEL ); + +if (!hidbuf) +return NULL ; + + hidbuf[0 ] =HID_ALLY_SET_REPORT_ID ; + hidbuf[1 ] =HID_ALLY_FEATURE_CODE_PAGE ; + hidbuf[2 ] = cmd; + hidbuf[3 ] = payload_size; + +if (payload_size >0 && payload) +memcpy (&hidbuf[4 ], payload, payload_size); + +return hidbuf; +} + +/** +* ally_gamepad_check_ready() - Wait for the gamepad MCU to report ready +* @ally: ally handheld structure +* @hdev: HID device drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 677
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* +* This should be called before any remapping attempts, and on driver +* init/resume, after the asus handshake has been performed on the +* configuration endpoint. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_gamepad_check_ready (struct ally_handheld *ally,struct hid_device *hdev) +{ +u8 payload[] = {0x00 };= {} is enough to initialize.
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 685
Quoted 8 lines
drivers/hid/hid-asus.c +8
-0+int ret; + +for (int i =0 ; i <HID_ALLY_READY_MAX_TRIES ; i++) { + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_CHECK_READY, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_receive_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE );ally_gamepad_send_packet()'s buf is const so why you need to alloc it over and over again?
Quoted 2 lines
Arguably, I'm not sure if this retry loop is expected to execute many times.
no the used buffer is u8 *buf (non const) and it gets overwritten by the method with the MCU response.
Since I am not sure if while not ready it can write other bytes I have opted to free it and reallocate it with correct data sice it is what gets sent and also what is returned.
Also yes, that cycle has to run many times to await for the MCU to come back:
it has caused quite a few troubles in the past.
Quoted 661 lines
Quoted 126 lines
drivers/hid/hid-asus.c +126
-0+if (ret <0 ) { +hid_dbg (hdev,"ROG Ally check %d/%d failed: %d\n" , i, +HID_ALLY_READY_MAX_TRIES , ret); +continue ; + } + +if (buf[2 ] ==CMD_CHECK_READY ) +return 0 ; + +usleep_range (1000 ,2000 ); + } + +hid_err (hdev,"ROG Ally never responded with a ready\n" ); +return -ENODEV ; +} + +static int ally_get_endpoint_address (struct hid_device *hdev) +{ +struct usb_host_endpoint *ep; +struct usb_interface *intf; + +if (!hid_is_usb (hdev)) +return -ENODEV ; + + intf =to_usb_interface (hdev->dev.parent); +if (!intf || !intf->cur_altsetting) +return -ENODEV ; + +if (intf->cur_altsetting->desc.bNumEndpoints ==0 ) +return -ENODEV ; + + ep = intf->cur_altsetting->endpoint; +if (!ep) +return -ENODEV ; + +return ep->desc.bEndpointAddress; +} + +struct ally_x_input_report { +__le16 x, y; +__le16 rx, ry; +__le16 z, rz; +u8 buttons[3 ]; +} __packed; + +/* The hatswitch outputs integers, we use them to index this X|Y pair */ +static const int hat_values[][2 ] = { + {0 ,0 }, {0 ,-1 }, {1 ,-1 }, {1 ,0 }, {1 ,1 }, + {0 ,1 }, {-1 ,1 }, {-1 ,0 }, {-1 ,-1 }, +}; + +/** +* ally_x_raw_event() - Parse and report an Ally X gamepad input report +* @input: input device to report the events on +* @data: raw report buffer +* @size: length of @data in bytes +* +* Return: true if the event was handled, otherwise false +*/ +static bool ally_x_raw_event (struct input_dev *input,u8 *data,int size) +{ +struct ally_x_input_report *in_report; +u16 x, y, rx, ry, z, rz; +u8 byte; + +if (!input) +return false; + +if (size <1 ) +return false; + +/* +* The size check above only guarantees one valid byte: do not +* read the code of a truncated report. +*/ +if (data[0 ] ==0x5A ) { +if (size <2 ) +return false; + +input_report_key (input,KEY_PROG1 , data[1 ] ==0x38 ); +input_report_key (input,KEY_F16 , data[1 ] ==0xA6 ); +input_report_key (input,KEY_F17 , data[1 ] ==0xA7 ); +input_report_key (input,KEY_F18 , data[1 ] ==0xA8 ); +input_sync (input); + +return data[1 ] ==0xA6 || data[1 ] ==0xA7 || data[1 ] ==0xA8 || data[1 ] ==0x38 ; + } + +if (data[0 ] !=HID_ALLY_X_INPUT_REPORT ) +return false; + +/* +* hid-core only guarantees size >= 1 and does not zero-pad short +* reports before ->raw_event, so a truncated transfer would leave the +* payload below pointing at stale DMA buffer contents. +*/ +if (size <1 +sizeof (*in_report)) +return false; + + in_report = (struct ally_x_input_report *)&data[1 ]; + +/* USB HID payloads are little-endian: convert them explicitly. */ + x =get_unaligned_le16 (&in_report->x); + y =get_unaligned_le16 (&in_report->y); + rx =get_unaligned_le16 (&in_report->rx); + ry =get_unaligned_le16 (&in_report->ry); + z =get_unaligned_le16 (&in_report->z); + rz =get_unaligned_le16 (&in_report->rz); + +input_report_abs (input,ABS_X , x -32768 ); +input_report_abs (input,ABS_Y , y -32768 ); +input_report_abs (input,ABS_RX , rx -32768 ); +input_report_abs (input,ABS_RY , ry -32768 ); +input_report_abs (input,ABS_Z , z); +input_report_abs (input,ABS_RZ , rz); + + byte = in_report->buttons[0 ]; +input_report_key (input,BTN_A , byte &BIT (0 )); +input_report_key (input,BTN_B , byte &BIT (1 )); +input_report_key (input,BTN_X , byte &BIT (2 )); +input_report_key (input,BTN_Y , byte &BIT (3 )); +input_report_key (input,BTN_TL , byte &BIT (4 )); +input_report_key (input,BTN_TR , byte &BIT (5 )); +input_report_key (input,BTN_SELECT , byte &BIT (6 )); +input_report_key (input,BTN_START , byte &BIT (7 )); +drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 821
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ byte = in_report->buttons[1 ]; +input_report_key (input,BTN_THUMBL , byte &BIT (0 )); +input_report_key (input,BTN_THUMBR , byte &BIT (1 )); +input_report_key (input,BTN_MODE , byte &BIT (2 )); + +/* The hatswitch byte is device-controlled; treat anything the table +* does not cover as centred rather than indexing out of bounds. +*/ + byte = in_report->buttons[2 ]; +if (byte >=ARRAY_SIZE (hat_values))Add include.
Quoted 17 lines
drivers/hid/hid-asus.c +17
-0+ byte =0 ; +input_report_abs (input,ABS_HAT0X , hat_values[byte][0 ]); +input_report_abs (input,ABS_HAT0Y , hat_values[byte][1 ]); + +input_sync (input); + +return true; +} + +static int ally_x_input_open (struct input_dev *dev) +{ +struct hid_device *hdev =input_get_drvdata (dev); + +return hid_hw_open (hdev); +} + +static void ally_x_input_close (struct input_dev *dev)drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 848
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+{ +hid_hw_close (input_get_drvdata (dev)); +} + +static struct input_dev *ally_x_alloc_input_dev (struct hid_device *hdev) +{ +struct input_dev *input_dev =input_allocate_device (); + +if (!input_dev) +return ERR_PTR (-ENOMEM );Add include.
Quoted 71 lines
drivers/hid/hid-asus.c +71
-0+ + input_dev->dev.parent = &hdev->dev; + input_dev->id.bustype = hdev->bus; + input_dev->id.vendor = hdev->vendor; + input_dev->id.product = hdev->product; + input_dev->id.version = hdev->version; + input_dev->uniq = hdev->uniq; + input_dev->name ="ASUS ROG Ally X Gamepad" ; + +input_set_drvdata (input_dev, hdev); +/* +* Let the input core hold the hardware open while the device node +* is in use: without these the interface could be runtime-suspended +* and drop events. +*/ + input_dev->open = ally_x_input_open; + input_dev->close = ally_x_input_close; + +return input_dev; +} + +static int ally_x_setup_input (struct hid_device *hdev,struct ally_handheld *ally) +{ +struct input_dev *input =ally_x_alloc_input_dev (hdev); +unsigned long flags; +int ret; + +if (IS_ERR (input)) +return PTR_ERR (input); + +input_set_abs_params (input,ABS_X ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_Y ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_RX ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_RY ,-32768 ,32767 ,0 ,0 ); +input_set_abs_params (input,ABS_Z ,0 ,1023 ,0 ,0 ); +input_set_abs_params (input,ABS_RZ ,0 ,1023 ,0 ,0 ); +input_set_abs_params (input,ABS_HAT0X ,-1 ,1 ,0 ,0 ); +input_set_abs_params (input,ABS_HAT0Y ,-1 ,1 ,0 ,0 ); +input_set_capability (input,EV_KEY ,BTN_A ); +input_set_capability (input,EV_KEY ,BTN_B ); +input_set_capability (input,EV_KEY ,BTN_X ); +input_set_capability (input,EV_KEY ,BTN_Y ); +input_set_capability (input,EV_KEY ,BTN_TL ); +input_set_capability (input,EV_KEY ,BTN_TR ); +input_set_capability (input,EV_KEY ,BTN_SELECT ); +input_set_capability (input,EV_KEY ,BTN_START ); +input_set_capability (input,EV_KEY ,BTN_MODE ); +input_set_capability (input,EV_KEY ,BTN_THUMBL ); +input_set_capability (input,EV_KEY ,BTN_THUMBR ); + +input_set_capability (input,EV_KEY ,KEY_PROG1 ); +input_set_capability (input,EV_KEY ,KEY_F16 ); +input_set_capability (input,EV_KEY ,KEY_F17 ); +input_set_capability (input,EV_KEY ,KEY_F18 ); +input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY ); +input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY1 ); + + ret =input_register_device (input); +if (ret) { +hid_err (hdev,"Failed to register Ally X gamepad device: %d\n" , ret); + goto ally_x_setup_input_err; + } + +/* Publish the input_dev only when it is fully set up. */ +spin_lock_irqsave (&ally_data_lock, flags); + ally->ally_x_input = input; +spin_unlock_irqrestore (&ally_data_lock, flags); + +return 0 ; +ally_x_setup_input_err: +input_free_device (input);drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 929
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+return ret; +} + +static int hid_asus_ally_init (struct hid_device *hdev,struct ally_handheld *ally) +{ +int ret; + +/* Failure at this point is non-critical */ + ret =ally_gamepad_send_packet (ally, hdev,ALLY_FORCE_FEEDBACK_OFF , +sizeof (ALLY_FORCE_FEEDBACK_OFF ));Don't name a real array with caps.
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+if (ret <0 ) +hid_err (hdev,"Ally failed to init force-feedback off: %d\n" , ret); + +return 0 ; +} + +/** +* hid_asus_ally_raw_event() - Route raw reports from the Ally interfaces +* @hdev: HID device drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 948
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* @ally: ally handheld structure +* @report: HID report the raw data arrived in +* @data: raw report buffer +* @size: length of @data in bytes +* +* Called from the raw_event callback, which may run in atomic (URB +* completion) context: only spinlock-protected accesses to the shared +* state are allowed here. +* +* Return: true if the event was handled, otherwise false .
Quoted 13 lines
drivers/hid/hid-asus.c +13
-0+*/ +static bool hid_asus_ally_raw_event (struct hid_device *hdev,struct ally_handheld *ally, +struct hid_report *report,u8 *data,int size) +{ +struct input_dev *x_input; +unsigned long flags; +bool handled; + +if (!ally) +return false; + +switch (ally_get_endpoint_address (hdev)) { +case HID_ALLY_X_INTF_IN :drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 971
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+/* +* Take a reference on the input_dev while using it: the +* gamepad interface can be unbound concurrently, and its +* input_dev is freed with it. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + x_input =input_get_device (ally->ally_x_input); +spin_unlock_irqrestore (&ally_data_lock, flags); + + handled =ally_x_raw_event (x_input, data, size);Since ypu've dropped the lock, couldn't things change right away after the unlock?
Perhaps there's something else that prevents problems but I generally feel unconvinced by how
ally_data_lockis used. Given what I see,ally_data_lockfeels either unnecessarily (or is an overkill for where a barrier would be enough to synchronize with the init) or doesn't protect everything properly (if uninit can race with this code).Quoted 46 lines
drivers/hid/hid-asus.c +46
-0+input_put_device (x_input); +if (handled) +return true; +break ; +case HID_ALLY_INTF_CFG_IN : +if (handle_ally_event (hdev, ally, data, size)) +return true; +break ; +case HID_ALLY_INTF_KEYBOARD_IN : +if (handle_ctrl_alt_del (ally, data, size)) +return false; +break ; +default : +break ; + } + +return false; +} + +/** +* hid_asus_ally_probe() - Initialize the ROG Ally HID extension +* @hdev: HID device +* +* This module works alongside the main Asus HID driver to handle +* Ally-specific features and quirks. +* +* Return: an ally_handheld struct pointer on success, an ERR_PTR on +* failure. The caller is not expected to use the returned pointer, but it +* should check for errors by using IS_ERR and PTR_ERR and pass NULL to +* other functions if there was an error. +*/ +static struct ally_handheld *hid_asus_ally_probe (struct hid_device *hdev) +{ +unsigned long flags; +int ret, ep =ally_get_endpoint_address (hdev); +struct usb_device *udev; +struct hid_input *hidinput; + +if (ep <0 ) +return ERR_PTR (ep); + +/* +* The ROG Ally controller is integrated into a handheld PC, so at +* most one device can exist and the shared global state relies on +* that: reject a probe from a different USB device, or a spoofed +* peripheral could overwrite the state of the real controller. The drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1027
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* recorded device is dropped when the last interface of the +* controller is unbound, so the re-enumeration the embedded +* controller performs after a suspend cycle (it cuts the controller +* power when powersave is enabled) is still accepted. +*/ + udev =interface_to_usbdev (to_usb_interface (hdev->dev.parent)); + +spin_lock_irqsave (&ally_data_lock, flags); +if (ally_drvdata.udev && ally_drvdata.udev != udev) { +spin_unlock_irqrestore (&ally_data_lock, flags);
scoped_guard()would avoid having to duplicate unlock but I'm not sure ifhid_err()is happy being called with a spinlock held...drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1029
Quoted 2 lines
drivers/hid/hid-asus.c +2
-0+hid_err (hdev,"A ROG Ally controller is already connected\n" ); +return ERR_PTR (-ENODEV );Shouldn't this be
-EBUSYinstead (the error message hints to that)?Quoted 11 lines
drivers/hid/hid-asus.c +11
-0+ } + +/* +* Interfaces of an already connected controller are equally +* untrusted: a crafted descriptor could present the same +* endpoint twice, and a second probe of one role would evict +* the state recorded by the first one, whose input_dev would +* then never be unregistered. The probe reservations make the +* check hold across the sleeping initialization below, which +* runs before the role pointer is published. +*/ drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1050
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+if ((ep ==HID_ALLY_INTF_CFG_IN && + (ally_drvdata.cfg_hdev || ally_drvdata.cfg_probe_in_progress)) || + (ep ==HID_ALLY_X_INTF_IN && + (ally_drvdata.ally_x_hdev || ally_drvdata.ally_x_probe_in_progress)) || + (ep ==HID_ALLY_INTF_KEYBOARD_IN && + (ally_drvdata.keyboard_hdev || ally_drvdata.keyboard_probe_in_progress))) { +spin_unlock_irqrestore (&ally_data_lock, flags); +hid_err (hdev, +"An interface of a ROG Ally controller is already connected\n" ); +return ERR_PTR (-ENODEV );Same here?
Quoted 23 lines
drivers/hid/hid-asus.c +23
-0+ } + +/* Reserve the role for as long as the initialization below runs. */ +if (ep ==HID_ALLY_INTF_CFG_IN ) + ally_drvdata.cfg_probe_in_progress = true; +else if (ep ==HID_ALLY_X_INTF_IN ) + ally_drvdata.ally_x_probe_in_progress = true; +else if (ep ==HID_ALLY_INTF_KEYBOARD_IN ) + ally_drvdata.keyboard_probe_in_progress = true; + +if (!ally_drvdata.udev) { +usb_get_dev (udev); + ally_drvdata.udev = udev; + } +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* +* The interface initialization sleeps (it performs USB transfers), +* so it must not run under ally_data_lock: the role stays reserved +* while it runs, and the pointers are published only once it +* succeeded. +*/ +switch (ep) {drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1083
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+case HID_ALLY_INTF_CFG_IN : +/* +* This function assumes the asus-specific initialization +* to have been performed already at this point. +*/ + ret =ally_gamepad_check_ready (&ally_drvdata, hdev); +if (ret <0 ) { +hid_err (hdev,"ROG Ally device is not ready: %d\n" , ret); +ally_probe_release_role (ep); +ally_put_udev_if_orphaned ();How about sharing the rollback path, there seems to be multiple of these.
Quoted 104 lines
drivers/hid/hid-asus.c +104
-0+return ERR_PTR (ret); + } + + ret =hid_asus_ally_init (hdev, &ally_drvdata); +if (ret <0 ) { +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (ret); + } + +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.cfg_hdev = hdev; + ally_drvdata.cfg_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +case HID_ALLY_X_INTF_IN : +/* This will create and populate ally_x_input */ + ret =ally_x_setup_input (hdev, &ally_drvdata); +if (ret) { +hid_err (hdev,"Failed to create Ally X gamepad device.\n" ); +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (ret); + } + +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.ally_x_hdev = hdev; + ally_drvdata.ally_x_probe_in_progress = false; +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +case HID_ALLY_INTF_KEYBOARD_IN : +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.keyboard_hdev = hdev; + ally_drvdata.keyboard_probe_in_progress = false; +if (!list_empty (&hdev->inputs)) { + hidinput = list_first_entry(&hdev->inputs, struct hid_input, list); + ally_drvdata.keyboard_input = hidinput->input; + } +spin_unlock_irqrestore (&ally_data_lock, flags); +break ; +default : +/* This is normally supposed to happen */ +break ; + } + +return &ally_drvdata; +} + +static void hid_asus_ally_remove (struct hid_device *hdev,struct ally_handheld *ally) +{ +struct input_dev *x_input =NULL ; +unsigned long flags; + +if (!ally) +return ; + +/* +* Any of the three interfaces can own an input_dev the resume work +* reports through, and they are torn down in an arbitrary order, so +* drain it before clearing anything. Cancel outside ally_data_lock so +* a handler that wants the lock cannot deadlock against us. +*/ +cancel_delayed_work_sync (&ally->resume_work); + +spin_lock_irqsave (&ally_data_lock, flags); +if (ally->ally_x_hdev == hdev) { + x_input = ally->ally_x_input; + ally->ally_x_input =NULL ; + ally->ally_x_hdev =NULL ; + } + +/* +* The keyboard interface is torn down before the config one, and +* its input_dev is freed with it. handle_ally_event() and +* ally_resume_work_fn() both report keys through it from the +* config endpoint, so drop the references here or they dangle. +*/ +if (ally->keyboard_hdev == hdev) { + ally->keyboard_input =NULL ; + ally->keyboard_hdev =NULL ; + } +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (x_input) +input_unregister_device (x_input); + +/* +* Drop the recorded USB device when the last interface of the +* controller has been unbound: the driver is then ready to accept +* the controller again when it reconnects. +*/ +ally_put_udev_if_orphaned (); +} + +static int hid_asus_ally_reset_resume (struct hid_device *hdev,struct ally_handheld *ally) +{ +int ep, ret; + +/* +* The extensions failed to probe and the device is operating as a +* generic HID device: do not fail the resume because of that. +*/ +if (!ally) +return 0 ;drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 1197
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ + ep =ally_get_endpoint_address (hdev); +if (ep !=HID_ALLY_INTF_CFG_IN ) +return 0 ; + + ret =hid_asus_ally_init (hdev, ally); +if (ret <0 ) +return ret; + +return 0 ;Why can't you just return directly what
hid_asus_ally_init()returned?Quoted 120 lines
drivers/hid/hid-asus.c +47
-0+} +/* * Send events to asus-wmi driver for handling special keys */ @@ -513,6 +1464,17 @@ static int asus_raw_event(struct hid_device *hdev,if (drvdata->quirks &QUIRK_MEDION_E1239T )return asus_e1239t_event (drvdata, data, size); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { +/* +* Return -1 to suppress further processing by the generic HID +* input parser for reports we fully handle for the Gamepad (0x0B): +* letting 0x0B reach the default parser creates a generic gamepad +* causing Steam Input overlaps (i.e. L1 stuck on screenshot). +*/ +if (hid_asus_ally_raw_event (hdev, drvdata->rog_ally, report, data, size)) +return -1 ; + } +/* * Skip these report ID, the device emits a continuous stream associated * with the AURA mode it is in which looks like an 'echo'. @@ -1306,6 +2268,7 @@ static int asus_input_mapping(struct hid_device *hdev,case 0x5f :asus_map_key_clear (KEY_PROG2 );break ;/* S-shaped programmable key */ case 0x6b :asus_map_key_clear (KEY_F21 );break ;/* ASUS touchpad toggle */ case 0x38 :asus_map_key_clear (KEY_PROG1 );break ;/* ROG key */ +case 0x93 :asus_map_key_clear (KEY_PROG1 );break ;/* ROG Ally X AC button */ case 0xba :asus_map_key_clear (KEY_PROG2 );break ;/* Fn+C ASUS Splendid */ case 0x5c :asus_map_key_clear (KEY_PROG3 );break ;/* Fn+Space Power4Gear */ case 0x99 :asus_map_key_clear (KEY_PROG4 );break ;/* Fn+F5 "fan" symbol */ @@ -1480,6 +2443,8 @@ static int asus_xgm_init(struct hid_device *hdev, struct asus_drvdata *drvdata)static int __maybe_unused asus_resume (struct hid_device *hdev) {struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +int ep;/* * If we have a backlight listener registered, restore the previous state, @@ -1489,12 +2454,19 @@ static int __maybe_unused asus_resume(struct hid_device *hdev)if (drvdata->listener.brightness_set)asus_kbd_backlight_set (&drvdata->listener, drvdata->kbd_backlight_brightness); +if (ally && (drvdata->quirks &QUIRK_ROG_ALLY_XPAD )) { + ep =ally_get_endpoint_address (hdev); +if (ep ==HID_ALLY_INTF_CFG_IN ) +schedule_delayed_work (&ally->resume_work,msecs_to_jiffies (500 )); + } +return 0 ; }static int __maybe_unused asus_reset_resume (struct hid_device *hdev) {struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +int ret;if (!drvdata->tp)asus_initialize_reports (hdev);@@ -1502,6 +2474,14 @@ static int __maybe_unused asus_reset_resume(struct hid_device *hdev)if (drvdata->tp)return asus_start_multitouch (hdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { + ret =hid_asus_ally_reset_resume (hdev, drvdata->rog_ally); +if (ret) { +hid_err (hdev,"Failed to resume ROG Ally HID extensions: %d\n" , ret); +return ret; + } + } +return 0 ; }@@ -1509,6 +2489,7 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id) {struct hid_report_enum *rep_enum;struct asus_drvdata *drvdata; +struct ally_handheld *ally;struct hid_report *rep;bool is_vendor = false;int ret;@@ -1632,6 +2613,15 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id) (asus_kbd_register_leds(hdev)))hid_warn (hdev,"Failed to initialize backlight.\n" ); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) { + ally =hid_asus_ally_probe (hdev); +if (IS_ERR (ally)) +hid_err (hdev,"Failed to initialize ROG Ally HID extensions: %ld\n" , +PTR_ERR (ally)); +else + drvdata->rog_ally = ally; + } +/* * For ROG keyboards, skip rename for consistency and ->input check as * some devices do not have inputs. @@ -1666,6 +2656,9 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)if (drvdata->xgm_led)devm_led_classdev_unregister (&hdev->dev, &drvdata->xgm_led->cdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) +hid_asus_ally_remove (hdev, drvdata->rog_ally); +asus_worker_stop (drvdata->worker);hid_hw_stop (hdev);return ret;@@ -1681,6 +2674,9 @@ static void asus_remove(struct hid_device *hdev)if (drvdata->xgm_led)devm_led_classdev_unregister (&hdev->dev, &drvdata->xgm_led->cdev); +if (drvdata->quirks &QUIRK_ROG_ALLY_XPAD ) +hid_asus_ally_remove (hdev, drvdata->rog_ally); +asus_worker_stop (drvdata->worker);hid_hw_stop (hdev); }
Ilpo Järvinen
maintainerQuoted 46 lines · Denis Benato
Quoted 44 lines · Ilpo Järvinen
Quoted 8 lines · Denis Benato
Implement the core functionality for ASUS handhelds:
- extend functionality of hid-asus if driver is enabled
- initialise the device to a "ready" state
- implement Ally X input mapping
- emit a single proper event for AC button long press
- reject probes from a different controller and from duplicated interfaces of the same one, which could evict the shared stateQuoted 5 lines
Signed-off-by Khamunetri Clark <[email protected]>Signed-off-by Denis Benato <[email protected]>Signed-off-by Matthew Schwartz <[email protected]>Signed-off-by Luke Jones <[email protected]>Signed-off-by Jonathan LoBue <[email protected]>Shouldn't you be using some Co-developed-by tags too?
Quoted 4 lines
drivers/hid/hid-asus.c +4
-0+/** +* ally_gamepad_check_ready() - Wait for the gamepad MCU to report ready +* @ally: ally handheld structure +* @hdev: HID device drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 677
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* +* This should be called before any remapping attempts, and on driver +* init/resume, after the asus handshake has been performed on the +* configuration endpoint. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_gamepad_check_ready (struct ally_handheld *ally,struct hid_device *hdev) +{ +u8 payload[] = {0x00 };= {} is enough to initialize.
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 685
Quoted 8 lines
drivers/hid/hid-asus.c +8
-0+int ret; + +for (int i =0 ; i <HID_ALLY_READY_MAX_TRIES ; i++) { + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_CHECK_READY, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_receive_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE );ally_gamepad_send_packet()'s buf is const so why you need to alloc it over and over again?
Quoted 5 lines
Quoted 2 lines
Arguably, I'm not sure if this retry loop is expected to execute many times.
no the used buffer is u8 *buf (non const) and it gets overwritten by the method with the MCU response.
Ah, yes the buf arg seems not const now that I double check. I must have accidently searched for some other function name which inputed const buf (e.g. old function name in the copy-paste buffer for some reason). I'm sorry about that.
Quoted 24 lines
Since I am not sure if while not ready it can write other bytes I have opted to free it and reallocate it with correct data sice it is what gets sent and also what is returned.
Also yes, that cycle has to run many times to await for the MCU to come back:
it has caused quite a few troubles in the past.Quoted 15 lines
Quoted 15 lines
drivers/hid/hid-asus.c +15
-0+if (ret <0 ) { +hid_dbg (hdev,"ROG Ally check %d/%d failed: %d\n" , i, +HID_ALLY_READY_MAX_TRIES , ret); +continue ; + } + +if (buf[2 ] ==CMD_CHECK_READY ) +return 0 ; + +usleep_range (1000 ,2000 ); + } + +hid_err (hdev,"ROG Ally never responded with a ready\n" ); +return -ENODEV ; +}
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
drivers/hid/hid-asus.c +6
-0+ byte = in_report->buttons[0 ]; +input_report_key (input,BTN_A , byte &BIT (0 )); +input_report_key (input,BTN_B , byte &BIT (1 )); +input_report_key (input,BTN_X , byte &BIT (2 )); +input_report_key (input,BTN_Y , byte &BIT (3 )); +input_report_key (input,BTN_TL , byte &BIT (4 ));Quoted 8 lines
drivers/hid/hid-asus.c static const struct asus_touchpad_info medion_e1239t_tp = { · line 815
Quoted 8 lines
drivers/hid/hid-asus.c +8
-0+input_report_key (input,BTN_TR , byte &BIT (5 )); +input_report_key (input,BTN_SELECT , byte &BIT (6 )); +input_report_key (input,BTN_START , byte &BIT (7 )); + + byte = in_report->buttons[1 ]; +input_report_key (input,BTN_THUMBL , byte &BIT (0 )); +input_report_key (input,BTN_THUMBR , byte &BIT (1 )); +input_report_key (input,BTN_MODE , byte &BIT (2 ));
BTW, while cutting text I noticed these BIT()s should be named with defines.
Signature · 1 line
i.
Denis Benato
maintainerPATCH v11 2/18 HID: asus: add gamepad configuration
In reply to Denis BenatoAdd the base configuration structures for the gamepad configuration, detect capabilities and initialize the device in a known state.
--- drivers/hid/hid-asus.c | 419 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 418 insertions(+), 1 deletion(-)
drivers/hid/hid-asus.c +418 -1
@@ -22,6 +22,7 @@#include <linux/acpi.h> #include <linux/cleanup.h> +#include <linux/device.h> #include <linux/dmi.h> #include <linux/hid.h> #include <linux/jiffies.h>
@@ -34,6 +35,7 @@#include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */ #include <linux/power_supply.h> #include <linux/stddef.h> +#include <linux/sysfs.h> #include <linux/leds.h> #include <linux/unaligned.h>
@@ -183,6 +185,45 @@ struct asus_touchpad_info {int report_size; }; +struct ally_config { +/* Must be locked if the data is being changed */ +struct mutex config_mutex; +bool initialized; + +/* Device capabilities flags */ +bool is_ally_x; +bool xbox_controller_support; +bool user_cal_support; +bool turbo_support; +bool resp_curve_support; +bool dir_to_btn_support; +bool gyro_support; +bool anti_deadzone_support; + +/* Current settings */ +bool xbox_controller_enabled; +u8 gamepad_mode; +u8 left_deadzone; +u8 left_outer_threshold; +u8 right_deadzone; +u8 right_outer_threshold; +u8 left_anti_deadzone; +u8 right_anti_deadzone; +u8 left_trigger_min; +u8 left_trigger_max; +u8 right_trigger_min; +u8 right_trigger_max; +}; + +/* +* The Ally configuration is allocated when the controller first appears and +* is released only on module removal: the embedded controller can disappear +* and re-enumerate at any time (it powers off during suspend when powersave +* is enabled), and on re-probe the driver must re-apply the recorded +* configuration instead of factory defaults. +*/ +static struct ally_config *ally_config; +struct ally_handheld {/* All read/write to IN interfaces must lock */ struct mutex intf_mutex;
@@ -215,6 +256,8 @@ struct ally_handheld {unsigned long cad_last_event_time;struct delayed_work resume_work; + +struct ally_config *config; };struct asus_drvdata {
@@ -661,6 +704,276 @@ static u8 *ally_alloc_cmd(u8 cmd, const u8 *payload, u8 payload_size)return hidbuf; } +/** +* ally_check_capability() - Check if a specific capability is supported +* @hdev: HID device +* @ally: ally handheld structure +* @check_cmd: capability command code to query +* +* Return: true if the capability is supported, false otherwise +*/ +static bool ally_check_capability (struct hid_device *hdev,struct ally_handheld *ally, +enum ally_command_codes check_cmd) +{ +u8 payload[] = {0x00 }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(check_cmd, payload, sizeof(payload)); +if (!buf) { +hid_err (hdev,"Failed to allocate buffer for capability check.\n" ); +return false; + } + + ret =ally_gamepad_send_receive_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to check capability 0x%02x: %d\n" , check_cmd, ret); +return false; + } + +return buf[1 ] ==HID_ALLY_FEATURE_CODE_PAGE && buf[2 ] == check_cmd && + buf[4 ] ==0x01 ; +} + +static int ally_detect_capabilities (struct hid_device *hdev,struct ally_handheld *ally, +struct ally_config *cfg) +{ +if (!hdev || !cfg || !ally) +return -EINVAL ; + + scoped_guard(mutex, &cfg->config_mutex) { + cfg->is_ally_x = (hdev->product ==USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X ); + + cfg->xbox_controller_support = +ally_check_capability (hdev, ally,CMD_CHECK_XBOX_SUPPORT ); + cfg->user_cal_support = +ally_check_capability (hdev, ally,CMD_CHECK_USER_CAL_SUPPORT ); + cfg->turbo_support = +ally_check_capability (hdev, ally,CMD_CHECK_TURBO_SUPPORT ); + cfg->resp_curve_support = +ally_check_capability (hdev, ally,CMD_CHECK_RESP_CURVE_SUPPORT ); + cfg->dir_to_btn_support = +ally_check_capability (hdev, ally,CMD_CHECK_DIR_TO_BTN_SUPPORT ); + cfg->gyro_support = +ally_check_capability (hdev, ally,CMD_CHECK_GYRO_TO_JOYSTICK ); + cfg->anti_deadzone_support = +ally_check_capability (hdev, ally,CMD_CHECK_ANTI_DEADZONE ); + } + +return 0 ; +} + +static int ally_set_xbox_controller (struct hid_device *hdev, +struct ally_handheld *ally, +struct ally_config *cfg,bool enabled) +{ +u8 payload[] = { enabled ?0x01 :0x00 }; +int ret; + +if (!cfg || !cfg->xbox_controller_support) +return -EOPNOTSUPP ; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_XBOX_CONTROLLER, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set Xbox controller mode: %d\n" , ret); +return ret; + } + + cfg->xbox_controller_enabled = enabled; +return 0 ; +} + +/** +* ally_get_config() - Get the configuration of the Ally device +* @ally: ally handheld structure +* +* Fetch the configuration published by hid_asus_ally_probe() under +* ally_data_lock: the pointer is also cleared by hid_asus_ally_remove(), +* so reading it without the lock would race with interface removal. +* +* The returned configuration outlives the sysfs callbacks using it: it is +* allocated once on the ally probe path and released only on module +* removal, when every sysfs attribute referencing it is gone. +* +* Return: the ally config, or NULL if no configuration is published +*/ +static struct ally_config *ally_get_config (struct ally_handheld *ally) +{ +struct ally_config *cfg; +unsigned long flags; + +spin_lock_irqsave (&ally_data_lock, flags); + cfg = ally->config; +spin_unlock_irqrestore (&ally_data_lock, flags); + +return cfg; +} + +static ssize_t xbox_controller_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->xbox_controller_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%d\n" , cfg->xbox_controller_enabled ?1 :0 ); +} + +static ssize_t xbox_controller_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +bool enabled; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtobool (buf, &enabled); +if (ret) +return ret; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->xbox_controller_support) +return -EOPNOTSUPP ; + + ret =ally_set_xbox_controller (hdev, ally, cfg, enabled); +if (ret <0 ) +return ret; + +sysfs_notify (&dev->kobj,NULL ,"xbox_controller" ); + +return count; +} + +static DEVICE_ATTR_RW (xbox_controller); + +static struct attribute *ally_config_attrs[] = { + &dev_attr_xbox_controller.attr, +NULL +}; + +static const struct attribute_group ally_attr_groups[] = { + { + .attrs = ally_config_attrs, + }, +}; + +/** +* ally_config_create() - Initialize configuration and create sysfs entries +* @hdev: HID device +* @ally: non-NULL ally device data with uninitialized config pointer +* +* Return: valid pointer on success, error pointer on failure +*/ +static struct ally_config *ally_config_create (struct hid_device *hdev,struct ally_handheld *ally) +{ +struct ally_config *cfg; +unsigned long flags; +int ret, sysfs_i; + +/* +* Allocate the configuration only once and keep it until module +* removal: the controller can disappear and re-enumerate while +* suspended, and the recorded settings must survive that to be +* re-applied when the controller comes back. +*/ +if (!ally_config) { + ally_config =kzalloc_obj (*ally_config,GFP_KERNEL ); +if (!ally_config) +return ERR_PTR (-ENOMEM ); + +mutex_init (&ally_config->config_mutex); + } + + cfg = ally_config; + + ret =ally_detect_capabilities (hdev, ally, cfg); +if (ret <0 ) { +hid_err (hdev,"Failed to detect Ally capabilities: %d\n" , ret); + goto ally_config_create_err; + } + +if (!cfg->initialized) { + cfg->gamepad_mode =0x01 ; + cfg->xbox_controller_enabled = true; + cfg->left_deadzone =10 ; + cfg->left_outer_threshold =90 ; + cfg->right_deadzone =10 ; + cfg->right_outer_threshold =90 ; + } + +spin_lock_irqsave (&ally_data_lock, flags); + ally->config = cfg; +spin_unlock_irqrestore (&ally_data_lock, flags); + +for (sysfs_i =0 ; sysfs_i <ARRAY_SIZE (ally_attr_groups); sysfs_i++) { + ret =sysfs_create_group (&hdev->dev.kobj, + &ally_attr_groups[sysfs_i]); +if (ret <0 ) { +hid_err (hdev,"Failed to create sysfs group '%s': %d\n" , + ally_attr_groups[sysfs_i].name ?:"" , ret); + goto ally_config_create_sysfs_err; + } + } + + cfg->initialized = true; + +return cfg; +ally_config_create_sysfs_err: +spin_lock_irqsave (&ally_data_lock, flags); + ally->config =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* The sysfs groups are not devm-managed: undo the created ones. */ +while (sysfs_i-- >0 ) +sysfs_remove_group (&hdev->dev.kobj, &ally_attr_groups[sysfs_i]); + +ally_config_create_err: +return ERR_PTR (ret); +} + +/** +* ally_config_remove() - Clean up configuration resources +* @hdev: HID device +* @cfg: ally config to clean up, may be NULL +*/ +static void ally_config_remove (struct hid_device *hdev,struct ally_config *cfg) +{ +int i; + +if (!cfg || !cfg->initialized) +return ; + +for (i =0 ; i <ARRAY_SIZE (ally_attr_groups); i++) +sysfs_remove_group (&hdev->dev.kobj, &ally_attr_groups[i]); +} +/** * ally_gamepad_check_ready() - Wait for the gamepad MCU to report ready * @ally: ally handheld structure
@@ -922,6 +1235,7 @@ static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *allstatic int hid_asus_ally_init (struct hid_device *hdev,struct ally_handheld *ally) { +struct ally_config *cfg;int ret;/* Failure at this point is non-critical */
@@ -930,6 +1244,21 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *allif (ret <0 )hid_err (hdev,"Ally failed to init force-feedback off: %d\n" , ret); + cfg =ally_get_config (ally); +if (!cfg) +return 0 ; + +/* The MCU may have just been reset: restore the cached state. */ +guard (mutex)(&cfg->config_mutex); + +if (cfg->xbox_controller_support) { + ret =ally_set_xbox_controller (hdev, ally, cfg, + cfg->xbox_controller_enabled); +if (ret <0 ) +hid_warn (hdev,"Failed to apply Xbox controller mode: %d\n" , + ret); + } +return 0 ; }
@@ -1005,11 +1334,16 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)unsigned long flags;int ret, ep =ally_get_endpoint_address (hdev);struct usb_device *udev; +struct asus_drvdata *drvdata; +struct ally_config *ally_cfg;struct hid_input *hidinput;if (ep <0 )return ERR_PTR (ep); +if (!hid_is_usb (hdev)) +return ERR_PTR (-ENODEV ); +/* * The ROG Ally controller is integrated into a handheld PC, so at * most one device can exist and the shared global state relies on
@@ -1021,6 +1355,7 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)* power when powersave is enabled) is still accepted. */ udev =interface_to_usbdev (to_usb_interface (hdev->dev.parent)); + drvdata =hid_get_drvdata (hdev);spin_lock_irqsave (&ally_data_lock, flags);if (ally_drvdata.udev && ally_drvdata.udev != udev) {
@@ -1084,8 +1419,40 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)return ERR_PTR (ret); } +/* +* Publish the shared state before the configuration creates +* its attributes: their callbacks fetch drvdata->rog_ally +* first, and the files must work from the moment they exist. +* Cleared again on the failure paths below. +*/ + drvdata->rog_ally = &ally_drvdata; + + ally_cfg =ally_config_create (hdev, &ally_drvdata); +if (IS_ERR (ally_cfg)) { +hid_err (hdev,"Failed to create Ally cfg: %ld\n" , +PTR_ERR (ally_cfg)); + drvdata->rog_ally =NULL ; +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (PTR_ERR (ally_cfg)); + } + ret =hid_asus_ally_init (hdev, &ally_drvdata);if (ret <0 ) { +/* +* Roll the configuration back like +* hid_asus_ally_remove() does: unpublish it under +* the lock first, so no sysfs callback can find it +* while ally_config_remove() removes the groups that +* were created. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.config =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags); + +ally_config_remove (hdev, ally_cfg); + + drvdata->rog_ally =NULL ;ally_probe_release_role (ep);ally_put_udev_if_orphaned ();return ERR_PTR (ret);
@@ -1132,7 +1499,9 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)static void hid_asus_ally_remove (struct hid_device *hdev,struct ally_handheld *ally) {struct input_dev *x_input =NULL ; +struct ally_config *cfg =NULL ;unsigned long flags; +bool owns_cfg;if (!ally)return ;
@@ -1162,8 +1531,25 @@ static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld * ally->keyboard_input =NULL ; ally->keyboard_hdev =NULL ; } + + owns_cfg = ally->cfg_hdev == hdev; +if (owns_cfg) { + cfg = ally->config; + ally->cfg_hdev =NULL ; + ally->config =NULL ; + } +spin_unlock_irqrestore (&ally_data_lock, flags); +/* +* The config teardown removes sysfs groups and takes sleeping locks: +* it must not run under ally_data_lock. The config pointer has been +* unpublished above, so no new sysfs callback can find it while the +* groups are being removed. +*/ +if (owns_cfg) +ally_config_remove (hdev, cfg); +if (x_input)input_unregister_device (x_input);
@@ -1190,6 +1576,16 @@ static int hid_asus_ally_reset_resume(struct hid_device *hdev, struct ally_handhif (ep !=HID_ALLY_INTF_CFG_IN )return 0 ; +/* +* This function assumes the asus-specific initialization +* to have been performed already at this point. +*/ + ret =ally_gamepad_check_ready (ally, hdev); +if (ret <0 ) { +hid_err (hdev,"ROG Ally device is not ready: %d\n" , ret); +return ret; + } + ret =hid_asus_ally_init (hdev, ally);if (ret <0 )return ret;
@@ -2878,7 +3274,28 @@ static struct hid_driver asus_driver = { .event = asus_event, .raw_event = asus_raw_event }; -module_hid_driver (asus_driver); + +static int __init asus_init (void ) +{ +return hid_register_driver (&asus_driver); +} + +/* +* The Ally configuration is not tied to any device lifetime: interface +* removal keeps it so that it can be re-applied on re-probe. Release it +* only when the module itself is removed, once no sysfs attribute can +* reach it anymore. +*/ +static void __exit asus_exit(void) +{ +hid_unregister_driver (&asus_driver); + +kfree (ally_config); + ally_config =NULL ; +} + +module_init (asus_init); +module_exit (asus_exit);MODULE_IMPORT_NS ("ASUS_WMI" );MODULE_LICENSE ("GPL" );
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v11 02/18] HID: asus: add gamepad configuration
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] HID: asus: premature cfg_hdev role release in hid_asus_ally_remove() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] HID: asus: premature cfg_hdev role release in hid_asus_ally_remove() --
commit c72354da64cec15cd42189e4692a21643e33def5
Author: Denis Benato <[email protected]>
HID: asus: add gamepad configuration
This commit adds the base configuration structures and sysfs attribute groups for ROG Ally gamepad configuration, detects hardware capabilities, and initializes the controller into a known state.
Quoted 4 lines
drivers/hid/hid-asus.c +0
-0
[ ... ]
Quoted 12 lines
drivers/hid/hid-asus.c +8
-0@@ -1162,8 +1531,25 @@ static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld * ally->keyboard_input =NULL ; ally->keyboard_hdev =NULL ; } + + owns_cfg = ally->cfg_hdev == hdev; +if (owns_cfg) { + cfg = ally->config; + ally->cfg_hdev =NULL ; + ally->config =NULL ; + } +
drivers/hid/hid-asus.c static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld * · line 1551
Quoted 10 lines
drivers/hid/hid-asus.c +8
-0spin_unlock_irqrestore (&ally_data_lock, flags); +/* +* The config teardown removes sysfs groups and takes sleeping locks: +* it must not run under ally_data_lock. The config pointer has been +* unpublished above, so no new sysfs callback can find it while the +* groups are being removed. +*/ +if (owns_cfg) +ally_config_remove (hdev, cfg);
[Severity: Medium]
In hid_asus_ally_remove(), should ally->cfg_hdev be cleared only after ally_config_remove(hdev, cfg) completes, rather than before it?
Clearing ally->cfg_hdev before ally_config_remove() releases the HID_ALLY_INTF_CFG_IN role while sysfs_remove_group() is still sleeping (for example, waiting in kernfs_drain() for an in-flight xbox_controller_store() callback). During that window:
1. A concurrent hid_asus_ally_remove() on the last sibling interface sees ally_drvdata.cfg_hdev == NULL and ally_drvdata.cfg_probe_in_progress == false in ally_put_udev_if_orphaned(), dropping ally_drvdata.udev early.
2. A concurrent hid_asus_ally_probe() for HID_ALLY_INTF_CFG_IN (from a re-enumerated device or duplicate endpoint) is no longer blocked by the role check:
if ((ep == HID_ALLY_INTF_CFG_IN && (ally_drvdata.cfg_hdev || ally_drvdata.cfg_probe_in_progress)) ||
and can enter ally_config_create() concurrently with ally_config_remove().
That allows ally_config_create() to mutate the singleton ally_config (including cfg->initialized and the capability flags) and re-publish ally->config = cfg while the old hdev's sysfs groups are still draining, which also lets sysfs callbacks on the unbinding hdev see a non-NULL ally_get_config() again.
By comparison, the hid_asus_ally_init() error path in hid_asus_ally_probe() clears ally_drvdata.config = NULL under ally_data_lock, calls ally_config_remove(), and only afterwards releases the role via ally_probe_release_role(ep) and ally_put_udev_if_orphaned().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ilpo Järvinen
maintainerRe: [PATCH v11 02/18] HID: asus: add gamepad configuration
In reply to Denis BenatoQuoted 116 lines · Denis Benato
Add the base configuration structures for the gamepad configuration, detect capabilities and initialize the device in a known state.
Signed-off-by Denis Benato <[email protected]>Signed-off-by Luke Jones <[email protected]>Signed-off-by Jonathan LoBue <[email protected]>--- drivers/hid/hid-asus.c | 419 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 418 insertions(+), 1 deletion(-)drivers/hid/hid-asus.c +70
-0@@ -22,6 +22,7 @@#include <linux/acpi.h> #include <linux/cleanup.h> +#include <linux/device.h> #include <linux/dmi.h> #include <linux/hid.h> #include <linux/jiffies.h> @@ -34,6 +35,7 @@#include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */ #include <linux/power_supply.h> #include <linux/stddef.h> +#include <linux/sysfs.h> #include <linux/leds.h> #include <linux/unaligned.h> @@ -183,6 +185,45 @@ struct asus_touchpad_info {int report_size; }; +struct ally_config { +/* Must be locked if the data is being changed */ +struct mutex config_mutex; +bool initialized; + +/* Device capabilities flags */ +bool is_ally_x; +bool xbox_controller_support; +bool user_cal_support; +bool turbo_support; +bool resp_curve_support; +bool dir_to_btn_support; +bool gyro_support; +bool anti_deadzone_support; + +/* Current settings */ +bool xbox_controller_enabled; +u8 gamepad_mode; +u8 left_deadzone; +u8 left_outer_threshold; +u8 right_deadzone; +u8 right_outer_threshold; +u8 left_anti_deadzone; +u8 right_anti_deadzone; +u8 left_trigger_min; +u8 left_trigger_max; +u8 right_trigger_min; +u8 right_trigger_max; +}; + +/* +* The Ally configuration is allocated when the controller first appears and +* is released only on module removal: the embedded controller can disappear +* and re-enumerate at any time (it powers off during suspend when powersave +* is enabled), and on re-probe the driver must re-apply the recorded +* configuration instead of factory defaults. +*/ +static struct ally_config *ally_config; +struct ally_handheld {/* All read/write to IN interfaces must lock */ struct mutex intf_mutex;@@ -215,6 +256,8 @@ struct ally_handheld {unsigned long cad_last_event_time;struct delayed_work resume_work; + +struct ally_config *config; };struct asus_drvdata {@@ -661,6 +704,276 @@ static u8 *ally_alloc_cmd(u8 cmd, const u8 *payload, u8 payload_size)return hidbuf; } +/** +* ally_check_capability() - Check if a specific capability is supported +* @hdev: HID device +* @ally: ally handheld structure +* @check_cmd: capability command code to query +* +* Return: true if the capability is supported, false otherwise +*/ +static bool ally_check_capability (struct hid_device *hdev,struct ally_handheld *ally, +enum ally_command_codes check_cmd) +{ +u8 payload[] = {0x00 }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(check_cmd, payload, sizeof(payload)); +if (!buf) { +hid_err (hdev,"Failed to allocate buffer for capability check.\n" ); +return false; + } + + ret =ally_gamepad_send_receive_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to check capability 0x%02x: %d\n" , check_cmd, ret); +return false; + } + + return buf[1] == HID_ALLY_FEATURE_CODE_PAGE && buf[2] == check_cmd &&
drivers/hid/hid-asus.c static u8 *ally_alloc_cmd(u8 cmd, const u8 *payload, u8 payload_size) · line 743
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ buf[4 ] ==0x01 ; +} + +static int ally_detect_capabilities (struct hid_device *hdev,struct ally_handheld *ally, +struct ally_config *cfg) +{ +if (!hdev || !cfg || !ally) +return -EINVAL ; + + scoped_guard(mutex, &cfg->config_mutex) {
guard() would enough.
Quoted 16 lines
drivers/hid/hid-asus.c +16
-0+ cfg->is_ally_x = (hdev->product ==USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X ); + + cfg->xbox_controller_support = +ally_check_capability (hdev, ally,CMD_CHECK_XBOX_SUPPORT ); + cfg->user_cal_support = +ally_check_capability (hdev, ally,CMD_CHECK_USER_CAL_SUPPORT ); + cfg->turbo_support = +ally_check_capability (hdev, ally,CMD_CHECK_TURBO_SUPPORT ); + cfg->resp_curve_support = +ally_check_capability (hdev, ally,CMD_CHECK_RESP_CURVE_SUPPORT ); + cfg->dir_to_btn_support = +ally_check_capability (hdev, ally,CMD_CHECK_DIR_TO_BTN_SUPPORT ); + cfg->gyro_support = +ally_check_capability (hdev, ally,CMD_CHECK_GYRO_TO_JOYSTICK ); + cfg->anti_deadzone_support = +ally_check_capability (hdev, ally,CMD_CHECK_ANTI_DEADZONE );
drivers/hid/hid-asus.c static u8 *ally_alloc_cmd(u8 cmd, const u8 *payload, u8 payload_size) · line 769
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ } + +return 0 ; +} + +static int ally_set_xbox_controller (struct hid_device *hdev, +struct ally_handheld *ally, +struct ally_config *cfg,bool enabled) +{ +u8 payload[] = { enabled ?0x01 :0x00 };
Oh okay, now I see the other 0x00 I commented in the other patch meant "disable".
Quoted 51 lines
drivers/hid/hid-asus.c +51
-0+int ret; + +if (!cfg || !cfg->xbox_controller_support) +return -EOPNOTSUPP ; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_XBOX_CONTROLLER, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set Xbox controller mode: %d\n" , ret); +return ret; + } + + cfg->xbox_controller_enabled = enabled; +return 0 ; +} + +/** +* ally_get_config() - Get the configuration of the Ally device +* @ally: ally handheld structure +* +* Fetch the configuration published by hid_asus_ally_probe() under +* ally_data_lock: the pointer is also cleared by hid_asus_ally_remove(), +* so reading it without the lock would race with interface removal. +* +* The returned configuration outlives the sysfs callbacks using it: it is +* allocated once on the ally probe path and released only on module +* removal, when every sysfs attribute referencing it is gone. +* +* Return: the ally config, or NULL if no configuration is published +*/ +static struct ally_config *ally_get_config (struct ally_handheld *ally) +{ +struct ally_config *cfg; +unsigned long flags; + +spin_lock_irqsave (&ally_data_lock, flags); + cfg = ally->config; +spin_unlock_irqrestore (&ally_data_lock, flags); + +return cfg; +} + +static ssize_t xbox_controller_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally;
drivers/hid/hid-asus.c static u8 *ally_alloc_cmd(u8 cmd, const u8 *payload, u8 payload_size) · line 830
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex);
Since ally_get_config() gave up the lock, is dereferencing cfg safe?
Quoted 15 lines
drivers/hid/hid-asus.c +15
-0+if (!cfg->xbox_controller_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%d\n" , cfg->xbox_controller_enabled ?1 :0 ); +} + +static ssize_t xbox_controller_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +bool enabled;
drivers/hid/hid-asus.c static u8 *ally_alloc_cmd(u8 cmd, const u8 *payload, u8 payload_size) · line 856
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtobool (buf, &enabled);
Include?
Quoted 45 lines
drivers/hid/hid-asus.c +45
-0+if (ret) +return ret; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->xbox_controller_support) +return -EOPNOTSUPP ; + + ret =ally_set_xbox_controller (hdev, ally, cfg, enabled); +if (ret <0 ) +return ret; + +sysfs_notify (&dev->kobj,NULL ,"xbox_controller" ); + +return count; +} + +static DEVICE_ATTR_RW (xbox_controller); + +static struct attribute *ally_config_attrs[] = { + &dev_attr_xbox_controller.attr, +NULL +}; + +static const struct attribute_group ally_attr_groups[] = { + { + .attrs = ally_config_attrs, + }, +}; + +/** +* ally_config_create() - Initialize configuration and create sysfs entries +* @hdev: HID device +* @ally: non-NULL ally device data with uninitialized config pointer +* +* Return: valid pointer on success, error pointer on failure +*/ +static struct ally_config *ally_config_create (struct hid_device *hdev,struct ally_handheld *ally) +{ +struct ally_config *cfg; +unsigned long flags; +int ret, sysfs_i; + +/* +* Allocate the configuration only once and keep it until module
drivers/hid/hid-asus.c static u8 *ally_alloc_cmd(u8 cmd, const u8 *payload, u8 payload_size) · line 911
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* removal: the controller can disappear and re-enumerate while +* suspended, and the recorded settings must survive that to be +* re-applied when the controller comes back. +*/ +if (!ally_config) { + ally_config =kzalloc_obj (*ally_config,GFP_KERNEL ); +if (!ally_config) +return ERR_PTR (-ENOMEM ); + +mutex_init (&ally_config->config_mutex);
Where's the pairing mutex_destroy()... Though I suggest using devm_mutex_init() + error handling if viable instead so you don't need to care about destroy.
Quoted 22 lines
drivers/hid/hid-asus.c +22
-0+ } + + cfg = ally_config; + + ret =ally_detect_capabilities (hdev, ally, cfg); +if (ret <0 ) { +hid_err (hdev,"Failed to detect Ally capabilities: %d\n" , ret); + goto ally_config_create_err; + } + +if (!cfg->initialized) { + cfg->gamepad_mode =0x01 ; + cfg->xbox_controller_enabled = true; + cfg->left_deadzone =10 ; + cfg->left_outer_threshold =90 ; + cfg->right_deadzone =10 ; + cfg->right_outer_threshold =90 ; + } + +spin_lock_irqsave (&ally_data_lock, flags); + ally->config = cfg; +spin_unlock_irqrestore (&ally_data_lock, flags);
drivers/hid/hid-asus.c static u8 *ally_alloc_cmd(u8 cmd, const u8 *payload, u8 payload_size) · line 943
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +for (sysfs_i =0 ; sysfs_i <ARRAY_SIZE (ally_attr_groups); sysfs_i++) { + ret =sysfs_create_group (&hdev->dev.kobj, + &ally_attr_groups[sysfs_i]); +if (ret <0 ) { +hid_err (hdev,"Failed to create sysfs group '%s': %d\n" , + ally_attr_groups[sysfs_i].name ?:"" , ret); + goto ally_config_create_sysfs_err; + } + }
Could .dev_groups be used? (I've not tried to figure out all the related code.)
Quoted 117 lines
drivers/hid/hid-asus.c +75
-0+ + cfg->initialized = true; + +return cfg; +ally_config_create_sysfs_err: +spin_lock_irqsave (&ally_data_lock, flags); + ally->config =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags); + +/* The sysfs groups are not devm-managed: undo the created ones. */ +while (sysfs_i-- >0 ) +sysfs_remove_group (&hdev->dev.kobj, &ally_attr_groups[sysfs_i]); + +ally_config_create_err: +return ERR_PTR (ret); +} + +/** +* ally_config_remove() - Clean up configuration resources +* @hdev: HID device +* @cfg: ally config to clean up, may be NULL +*/ +static void ally_config_remove (struct hid_device *hdev,struct ally_config *cfg) +{ +int i; + +if (!cfg || !cfg->initialized) +return ; + +for (i =0 ; i <ARRAY_SIZE (ally_attr_groups); i++) +sysfs_remove_group (&hdev->dev.kobj, &ally_attr_groups[i]); +} +/** * ally_gamepad_check_ready() - Wait for the gamepad MCU to report ready * @ally: ally handheld structure @@ -922,6 +1235,7 @@ static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *allstatic int hid_asus_ally_init (struct hid_device *hdev,struct ally_handheld *ally) { +struct ally_config *cfg;int ret;/* Failure at this point is non-critical */ @@ -930,6 +1244,21 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *allif (ret <0 )hid_err (hdev,"Ally failed to init force-feedback off: %d\n" , ret); + cfg =ally_get_config (ally); +if (!cfg) +return 0 ; + +/* The MCU may have just been reset: restore the cached state. */ +guard (mutex)(&cfg->config_mutex); + +if (cfg->xbox_controller_support) { + ret =ally_set_xbox_controller (hdev, ally, cfg, + cfg->xbox_controller_enabled); +if (ret <0 ) +hid_warn (hdev,"Failed to apply Xbox controller mode: %d\n" , + ret); + } +return 0 ; }@@ -1005,11 +1334,16 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)unsigned long flags;int ret, ep =ally_get_endpoint_address (hdev);struct usb_device *udev; +struct asus_drvdata *drvdata; +struct ally_config *ally_cfg;struct hid_input *hidinput;if (ep <0 )return ERR_PTR (ep); +if (!hid_is_usb (hdev)) +return ERR_PTR (-ENODEV ); +/* * The ROG Ally controller is integrated into a handheld PC, so at * most one device can exist and the shared global state relies on @@ -1021,6 +1355,7 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)* power when powersave is enabled) is still accepted. */ udev =interface_to_usbdev (to_usb_interface (hdev->dev.parent)); + drvdata =hid_get_drvdata (hdev);spin_lock_irqsave (&ally_data_lock, flags);if (ally_drvdata.udev && ally_drvdata.udev != udev) {@@ -1084,8 +1419,40 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)return ERR_PTR (ret); } +/* +* Publish the shared state before the configuration creates +* its attributes: their callbacks fetch drvdata->rog_ally +* first, and the files must work from the moment they exist. +* Cleared again on the failure paths below. +*/ + drvdata->rog_ally = &ally_drvdata; + + ally_cfg =ally_config_create (hdev, &ally_drvdata); +if (IS_ERR (ally_cfg)) { +hid_err (hdev,"Failed to create Ally cfg: %ld\n" , +PTR_ERR (ally_cfg)); + drvdata->rog_ally =NULL ; +ally_probe_release_role (ep); +ally_put_udev_if_orphaned (); +return ERR_PTR (PTR_ERR (ally_cfg)); + } + ret =hid_asus_ally_init (hdev, &ally_drvdata);if (ret <0 ) { +/* +* Roll the configuration back like
drivers/hid/hid-asus.c static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev) · line 1453
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* hid_asus_ally_remove() does: unpublish it under +* the lock first, so no sysfs callback can find it +* while ally_config_remove() removes the groups that +* were created. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + ally_drvdata.config =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags); + +ally_config_remove (hdev, ally_cfg);
I suspect there's a race... No idea why you had to take the spin lock if there isn't anything that could race with this.
Quoted 89 lines
drivers/hid/hid-asus.c +53
-1+ + drvdata->rog_ally =NULL ;ally_probe_release_role (ep);ally_put_udev_if_orphaned ();return ERR_PTR (ret);@@ -1132,7 +1499,9 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)static void hid_asus_ally_remove (struct hid_device *hdev,struct ally_handheld *ally) {struct input_dev *x_input =NULL ; +struct ally_config *cfg =NULL ;unsigned long flags; +bool owns_cfg;if (!ally)return ;@@ -1162,8 +1531,25 @@ static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld * ally->keyboard_input =NULL ; ally->keyboard_hdev =NULL ; } + + owns_cfg = ally->cfg_hdev == hdev; +if (owns_cfg) { + cfg = ally->config; + ally->cfg_hdev =NULL ; + ally->config =NULL ; + } +spin_unlock_irqrestore (&ally_data_lock, flags); +/* +* The config teardown removes sysfs groups and takes sleeping locks: +* it must not run under ally_data_lock. The config pointer has been +* unpublished above, so no new sysfs callback can find it while the +* groups are being removed. +*/ +if (owns_cfg) +ally_config_remove (hdev, cfg); +if (x_input)input_unregister_device (x_input);@@ -1190,6 +1576,16 @@ static int hid_asus_ally_reset_resume(struct hid_device *hdev, struct ally_handhif (ep !=HID_ALLY_INTF_CFG_IN )return 0 ; +/* +* This function assumes the asus-specific initialization +* to have been performed already at this point. +*/ + ret =ally_gamepad_check_ready (ally, hdev); +if (ret <0 ) { +hid_err (hdev,"ROG Ally device is not ready: %d\n" , ret); +return ret; + } + ret =hid_asus_ally_init (hdev, ally);if (ret <0 )return ret;@@ -2878,7 +3274,28 @@ static struct hid_driver asus_driver = { .event = asus_event, .raw_event = asus_raw_event }; -module_hid_driver (asus_driver); + +static int __init asus_init (void ) +{ +return hid_register_driver (&asus_driver); +} + +/* +* The Ally configuration is not tied to any device lifetime: interface +* removal keeps it so that it can be re-applied on re-probe. Release it +* only when the module itself is removed, once no sysfs attribute can +* reach it anymore. +*/ +static void __exit asus_exit(void) +{ +hid_unregister_driver (&asus_driver); + +kfree (ally_config); + ally_config =NULL ; +} + +module_init (asus_init); +module_exit (asus_exit);MODULE_IMPORT_NS ("ASUS_WMI" );MODULE_LICENSE ("GPL" );
Signature · 1 line
i.
Denis Benato
maintainerPATCH v11 3/18 HID: asus: add vibration strength configuration
In reply to Denis BenatoASUS ROG Ally handhelds support the vibration strength to be configured:
add sysfs attributes to allow userspace configure motors vibration intensity.
--- drivers/hid/hid-asus.c | 199 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+)
drivers/hid/hid-asus.c +199 -0
@@ -213,6 +213,10 @@ struct ally_config {u8 left_trigger_max;u8 right_trigger_min;u8 right_trigger_max; + +/* Vibration settings */ +u8 vibration_intensity_left; +u8 vibration_intensity_right; };/*
@@ -873,15 +877,197 @@ static ssize_t xbox_controller_store(struct device *dev,static DEVICE_ATTR_RW (xbox_controller); +/** +* ally_set_vibration_intensity() - Set vibration intensity values +* @ally: ally handheld structure +* @hdev: HID device +* @left: Left motor intensity (0-100) +* @right: Right motor intensity (0-100) +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_vibration_intensity (struct ally_handheld *ally, +struct hid_device *hdev,u8 left,u8 right) +{ +const u8 data[] = { left, right }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_VIBRATION_INTENSITY, data, sizeof(data)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set vibration intensity: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static ssize_t left_vibration_intensity_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +return sysfs_emit (buf,"%u\n" , cfg->vibration_intensity_left); +} + +static ssize_t left_vibration_intensity_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + + ret =ally_set_vibration_intensity (ally, hdev, value, + cfg->vibration_intensity_right); +if (ret <0 ) +return ret; + + cfg->vibration_intensity_left = value; + +sysfs_notify (&dev->kobj,"left_vibration" ,"intensity" ); + +return count; +} + +static ssize_t left_vibration_intensity_range_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"0 100\n" ); +} + +static ssize_t right_vibration_intensity_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +return sysfs_emit (buf,"%u\n" , cfg->vibration_intensity_right); +} + +static ssize_t right_vibration_intensity_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + + ret =ally_set_vibration_intensity (ally, hdev, + cfg->vibration_intensity_left, value); +if (ret <0 ) +return ret; + + cfg->vibration_intensity_right = value; + +sysfs_notify (&dev->kobj,"right_vibration" ,"intensity" ); + +return count; +} + +static ssize_t right_vibration_intensity_range_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"0 100\n" ); +} + +static struct device_attribute dev_attr_left_vibration_intensity = +__ATTR (intensity,0644 , left_vibration_intensity_show, left_vibration_intensity_store); + +static struct device_attribute dev_attr_left_vibration_intensity_range = +__ATTR (intensity_range,0444 , left_vibration_intensity_range_show,NULL ); + +static struct device_attribute dev_attr_right_vibration_intensity = +__ATTR (intensity,0644 , right_vibration_intensity_show, right_vibration_intensity_store); + +static struct device_attribute dev_attr_right_vibration_intensity_range = +__ATTR (intensity_range,0444 , right_vibration_intensity_range_show,NULL ); +static struct attribute *ally_config_attrs[] = { &dev_attr_xbox_controller.attr,NULL }; +static struct attribute *ally_left_vibration_attrs[] = { + &dev_attr_left_vibration_intensity.attr, + &dev_attr_left_vibration_intensity_range.attr, +NULL +}; + +static struct attribute *ally_right_vibration_attrs[] = { + &dev_attr_right_vibration_intensity.attr, + &dev_attr_right_vibration_intensity_range.attr, +NULL +}; +static const struct attribute_group ally_attr_groups[] = { { .attrs = ally_config_attrs, }, + { + .name ="left_vibration" , + .attrs = ally_left_vibration_attrs, + }, + { + .name ="right_vibration" , + .attrs = ally_right_vibration_attrs, + }, };/**
@@ -926,6 +1112,8 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al cfg->left_outer_threshold =90 ; cfg->right_deadzone =10 ; cfg->right_outer_threshold =90 ; + cfg->vibration_intensity_left =100 ; + cfg->vibration_intensity_right =100 ; }spin_lock_irqsave (&ally_data_lock, flags);
@@ -1259,6 +1447,17 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +/* +* The MCU does not keep the vibration intensity across a reset: +* send the recorded values again, whichever interface probed. +*/ + ret =ally_set_vibration_intensity (ally, hdev, + cfg->vibration_intensity_left, + cfg->vibration_intensity_right); +if (ret <0 ) +hid_warn (hdev,"Failed to restore vibration intensity: %d\n" , + ret); +return 0 ; }
Signature · 1 line
2.47.3
Denis Benato
maintainerPATCH v11 4/18 HID: asus: add joysticks inner and outer range configuration
In reply to Denis BenatoROG Ally devices supports configuring joysticks inner and outer range:
add sysfs attributes to allow userspace modifying the sensitivity of those controllers.
--- drivers/hid/hid-asus.c | 433 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 433 insertions(+)
drivers/hid/hid-asus.c +433 -0
@@ -1039,6 +1039,358 @@ static struct device_attribute dev_attr_right_vibration_intensity =static struct device_attribute dev_attr_right_vibration_intensity_range =__ATTR (intensity_range,0444 , right_vibration_intensity_range_show,NULL ); +/** +* ally_set_joystick_thresholds() - Generic function to set joystick ranges +* @ally: ally handheld structure +* @hdev: HID device +* @left_it: inner threshold (deadzone) of the left stick (0-50) +* @left_ot: outer threshold of the left stick (70-100) +* @right_it: inner threshold (deadzone) of the right stick (0-50) +* @right_ot: outer threshold of the right stick (70-100) +* +* This function sends the command to set both inner and outer threshold +* for the left and right joysticks. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_joystick_thresholds (struct ally_handheld *ally, +struct hid_device *hdev, +u8 left_it,u8 left_ot,u8 right_it,u8 right_ot) +{ +const u8 payload[] = { left_it, left_ot, right_it, right_ot }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_JOYSTICK_DEADZONE, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set joystick ranges: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static ssize_t left_joystick_inner_threshold_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->left_deadzone); +} + +static ssize_t left_joystick_inner_threshold_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >50 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_thresholds (ally, hdev, + value, + cfg->left_outer_threshold, + cfg->right_deadzone, + cfg->right_outer_threshold); +if (ret) +return ret; + + cfg->left_deadzone = value; + +sysfs_notify (&dev->kobj,"left_joystick_axis" ,"inner_threshold" ); + +return count; +} + +static ssize_t left_joystick_inner_threshold_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"0 50\n" ); +} + +static ssize_t left_joystick_outer_threshold_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->left_outer_threshold); +} + +static ssize_t left_joystick_outer_threshold_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value <70 || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_thresholds (ally, hdev, + cfg->left_deadzone, + value, + cfg->right_deadzone, + cfg->right_outer_threshold); +if (ret) +return ret; + + cfg->left_outer_threshold = value; + +sysfs_notify (&dev->kobj,"left_joystick_axis" ,"outer_threshold" ); + +return count; +} + +static ssize_t left_joystick_outer_threshold_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"70 100\n" ); +} + +static ssize_t right_joystick_inner_threshold_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->right_deadzone); +} + +static ssize_t right_joystick_inner_threshold_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >50 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_thresholds (ally, hdev, + cfg->left_deadzone, + cfg->left_outer_threshold, + value, + cfg->right_outer_threshold); +if (ret) +return ret; + + cfg->right_deadzone = value; + +sysfs_notify (&dev->kobj,"right_joystick_axis" ,"inner_threshold" ); + +return count; +} + +static ssize_t right_joystick_inner_threshold_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"0 50\n" ); +} + +static ssize_t right_joystick_outer_threshold_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->right_outer_threshold); +} + +static ssize_t right_joystick_outer_threshold_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value <70 || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_thresholds (ally, hdev, + cfg->left_deadzone, + cfg->left_outer_threshold, + cfg->right_deadzone, + value); +if (ret) +return ret; + + cfg->right_outer_threshold = value; + +sysfs_notify (&dev->kobj,"right_joystick_axis" ,"outer_threshold" ); + +return count; +} + +static ssize_t right_joystick_outer_threshold_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"70 100\n" ); +} + +static struct device_attribute dev_attr_left_joystick_inner_threshold = +__ATTR (inner_threshold,0644 , left_joystick_inner_threshold_show, + left_joystick_inner_threshold_store); + +static struct device_attribute dev_attr_left_joystick_inner_threshold_range = +__ATTR (inner_threshold_range,0444 , left_joystick_inner_threshold_range_show,NULL ); + +static struct device_attribute dev_attr_left_joystick_outer_threshold = +__ATTR (outer_threshold,0644 , left_joystick_outer_threshold_show, + left_joystick_outer_threshold_store); + +static struct device_attribute dev_attr_left_joystick_outer_threshold_range = +__ATTR (outer_threshold_range,0444 , left_joystick_outer_threshold_range_show,NULL ); + +static struct device_attribute dev_attr_right_joystick_inner_threshold = +__ATTR (inner_threshold,0644 , right_joystick_inner_threshold_show, + right_joystick_inner_threshold_store); + +static struct device_attribute dev_attr_right_joystick_inner_threshold_range = +__ATTR (inner_threshold_range,0444 , right_joystick_inner_threshold_range_show,NULL ); + +static struct device_attribute dev_attr_right_joystick_outer_threshold = +__ATTR (outer_threshold,0644 , right_joystick_outer_threshold_show, + right_joystick_outer_threshold_store); + +static struct device_attribute dev_attr_right_joystick_outer_threshold_range = +__ATTR (outer_threshold_range,0444 , right_joystick_outer_threshold_range_show,NULL ); +static struct attribute *ally_config_attrs[] = { &dev_attr_xbox_controller.attr,NULL
@@ -1056,6 +1408,22 @@ static struct attribute *ally_right_vibration_attrs[] = {NULL }; +static struct attribute *left_joystick_axis_attrs[] = { + &dev_attr_left_joystick_inner_threshold.attr, + &dev_attr_left_joystick_outer_threshold.attr, + &dev_attr_left_joystick_inner_threshold_range.attr, + &dev_attr_left_joystick_outer_threshold_range.attr, +NULL +}; + +static struct attribute *right_joystick_axis_attrs[] = { + &dev_attr_right_joystick_inner_threshold.attr, + &dev_attr_right_joystick_outer_threshold.attr, + &dev_attr_right_joystick_inner_threshold_range.attr, + &dev_attr_right_joystick_outer_threshold_range.attr, +NULL +}; +static const struct attribute_group ally_attr_groups[] = { { .attrs = ally_config_attrs,
@@ -1070,6 +1438,28 @@ static const struct attribute_group ally_attr_groups[] = { }, }; +/* +* The joystick and trigger range calibration attributes are tied to the +* user-calibration capability: their sysfs groups are registered only +* when the device supports configuring those parameters, and the show +* and store callbacks reject accesses with -EOPNOTSUPP regardless, in +* case the groups are registered for another supported feature. +*/ +static const struct attribute_group ally_left_joystick_axis_group = { + .name ="left_joystick_axis" , + .attrs = left_joystick_axis_attrs, +}; + +static const struct attribute_group ally_right_joystick_axis_group = { + .name ="right_joystick_axis" , + .attrs = right_joystick_axis_attrs, +}; + +static const struct attribute_group *const ally_cal_attr_groups[] = { + &ally_left_joystick_axis_group, + &ally_right_joystick_axis_group, +}; +/** * ally_config_create() - Initialize configuration and create sysfs entries * @hdev: HID device
@@ -1130,6 +1520,25 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al } } +/* Skip the calibration groups when the capability is missing. */ +if (cfg->user_cal_support) { +int cal_i; + +for (cal_i =0 ; cal_i <ARRAY_SIZE (ally_cal_attr_groups); + cal_i++) { + ret =sysfs_create_group (&hdev->dev.kobj, + ally_cal_attr_groups[cal_i]); +if (ret <0 ) { +hid_err (hdev,"Failed to create sysfs group '%s': %d\n" , + ally_cal_attr_groups[cal_i]->name, ret); +while (cal_i-- >0 ) +sysfs_remove_group (&hdev->dev.kobj, + ally_cal_attr_groups[cal_i]); + goto ally_config_create_sysfs_err; + } + } + } + cfg->initialized = true;return cfg;
@@ -1160,6 +1569,18 @@ static void ally_config_remove(struct hid_device *hdev, struct ally_config *cfg)for (i =0 ; i <ARRAY_SIZE (ally_attr_groups); i++)sysfs_remove_group (&hdev->dev.kobj, &ally_attr_groups[i]); + +/* +* The calibration groups are removed with the same gating used to +* create them, so a device without those capabilities does not +* trigger a "not found" warning. +*/ +if (cfg->user_cal_support || cfg->anti_deadzone_support || + cfg->resp_curve_support) { +for (i =0 ; i <ARRAY_SIZE (ally_cal_attr_groups); i++) +sysfs_remove_group (&hdev->dev.kobj, + ally_cal_attr_groups[i]); + } }/**
@@ -1458,6 +1879,18 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *allhid_warn (hdev,"Failed to restore vibration intensity: %d\n" , ret); +if (cfg->user_cal_support) { + ret =ally_set_joystick_thresholds (ally, hdev, + cfg->left_deadzone, + cfg->left_outer_threshold, + cfg->right_deadzone, + cfg->right_outer_threshold); +if (ret <0 ) +hid_warn (hdev, +"Failed to restore joystick thresholds: %d\n" , + ret); + } +return 0 ; }
Signature · 1 line
2.47.3
Ilpo Järvinen
maintainerRe: [PATCH v11 04/18] HID: asus: add joysticks inner and outer range configuration
Quoted 303 lines · Denis Benato
ROG Ally devices supports configuring joysticks inner and outer range:
add sysfs attributes to allow userspace modifying the sensitivity of those controllers.Signed-off-by Denis Benato <[email protected]>Signed-off-by Luke Jones <[email protected]>--- drivers/hid/hid-asus.c | 433 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 433 insertions(+)drivers/hid/hid-asus.c +285
-0@@ -1039,6 +1039,358 @@ static struct device_attribute dev_attr_right_vibration_intensity =static struct device_attribute dev_attr_right_vibration_intensity_range =__ATTR (intensity_range,0444 , right_vibration_intensity_range_show,NULL ); +/** +* ally_set_joystick_thresholds() - Generic function to set joystick ranges +* @ally: ally handheld structure +* @hdev: HID device +* @left_it: inner threshold (deadzone) of the left stick (0-50) +* @left_ot: outer threshold of the left stick (70-100) +* @right_it: inner threshold (deadzone) of the right stick (0-50) +* @right_ot: outer threshold of the right stick (70-100) +* +* This function sends the command to set both inner and outer threshold +* for the left and right joysticks. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_joystick_thresholds (struct ally_handheld *ally, +struct hid_device *hdev, +u8 left_it,u8 left_ot,u8 right_it,u8 right_ot) +{ +const u8 payload[] = { left_it, left_ot, right_it, right_ot }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_JOYSTICK_DEADZONE, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set joystick ranges: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static ssize_t left_joystick_inner_threshold_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->left_deadzone); +} + +static ssize_t left_joystick_inner_threshold_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >50 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_thresholds (ally, hdev, + value, + cfg->left_outer_threshold, + cfg->right_deadzone, + cfg->right_outer_threshold); +if (ret) +return ret; + + cfg->left_deadzone = value; + +sysfs_notify (&dev->kobj,"left_joystick_axis" ,"inner_threshold" ); + +return count; +} + +static ssize_t left_joystick_inner_threshold_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"0 50\n" ); +} + +static ssize_t left_joystick_outer_threshold_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->left_outer_threshold); +} + +static ssize_t left_joystick_outer_threshold_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value <70 || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_thresholds (ally, hdev, + cfg->left_deadzone, + value, + cfg->right_deadzone, + cfg->right_outer_threshold); +if (ret) +return ret; + + cfg->left_outer_threshold = value; + +sysfs_notify (&dev->kobj,"left_joystick_axis" ,"outer_threshold" ); + +return count; +} + +static ssize_t left_joystick_outer_threshold_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"70 100\n" ); +} + +static ssize_t right_joystick_inner_threshold_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->right_deadzone); +} + +static ssize_t right_joystick_inner_threshold_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >50 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_thresholds (ally, hdev, + cfg->left_deadzone, + cfg->left_outer_threshold, + value, + cfg->right_outer_threshold); +if (ret) +return ret; + + cfg->right_deadzone = value; + +sysfs_notify (&dev->kobj,"right_joystick_axis" ,"inner_threshold" ); + +return count; +} + +static ssize_t right_joystick_inner_threshold_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"0 50\n" ); +} + +static ssize_t right_joystick_outer_threshold_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->right_outer_threshold); +} + +static ssize_t right_joystick_outer_threshold_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret;
drivers/hid/hid-asus.c static struct device_attribute dev_attr_right_vibration_intensity = · line 1336
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value <70 || value >100 )
I'd prefer binding values that are not 0 or 100 with defines. You print the ranges with string literals and they are linked to checks here so the code should link those by using same define.
Quoted 177 lines
drivers/hid/hid-asus.c +138
-0+return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_thresholds (ally, hdev, + cfg->left_deadzone, + cfg->left_outer_threshold, + cfg->right_deadzone, + value); +if (ret) +return ret; + + cfg->right_outer_threshold = value; + +sysfs_notify (&dev->kobj,"right_joystick_axis" ,"outer_threshold" ); + +return count; +} + +static ssize_t right_joystick_outer_threshold_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"70 100\n" ); +} + +static struct device_attribute dev_attr_left_joystick_inner_threshold = +__ATTR (inner_threshold,0644 , left_joystick_inner_threshold_show, + left_joystick_inner_threshold_store); + +static struct device_attribute dev_attr_left_joystick_inner_threshold_range = +__ATTR (inner_threshold_range,0444 , left_joystick_inner_threshold_range_show,NULL ); + +static struct device_attribute dev_attr_left_joystick_outer_threshold = +__ATTR (outer_threshold,0644 , left_joystick_outer_threshold_show, + left_joystick_outer_threshold_store); + +static struct device_attribute dev_attr_left_joystick_outer_threshold_range = +__ATTR (outer_threshold_range,0444 , left_joystick_outer_threshold_range_show,NULL ); + +static struct device_attribute dev_attr_right_joystick_inner_threshold = +__ATTR (inner_threshold,0644 , right_joystick_inner_threshold_show, + right_joystick_inner_threshold_store); + +static struct device_attribute dev_attr_right_joystick_inner_threshold_range = +__ATTR (inner_threshold_range,0444 , right_joystick_inner_threshold_range_show,NULL ); + +static struct device_attribute dev_attr_right_joystick_outer_threshold = +__ATTR (outer_threshold,0644 , right_joystick_outer_threshold_show, + right_joystick_outer_threshold_store); + +static struct device_attribute dev_attr_right_joystick_outer_threshold_range = +__ATTR (outer_threshold_range,0444 , right_joystick_outer_threshold_range_show,NULL ); +static struct attribute *ally_config_attrs[] = { &dev_attr_xbox_controller.attr,NULL @@ -1056,6 +1408,22 @@ static struct attribute *ally_right_vibration_attrs[] = {NULL }; +static struct attribute *left_joystick_axis_attrs[] = { + &dev_attr_left_joystick_inner_threshold.attr, + &dev_attr_left_joystick_outer_threshold.attr, + &dev_attr_left_joystick_inner_threshold_range.attr, + &dev_attr_left_joystick_outer_threshold_range.attr, +NULL +}; + +static struct attribute *right_joystick_axis_attrs[] = { + &dev_attr_right_joystick_inner_threshold.attr, + &dev_attr_right_joystick_outer_threshold.attr, + &dev_attr_right_joystick_inner_threshold_range.attr, + &dev_attr_right_joystick_outer_threshold_range.attr, +NULL +}; +static const struct attribute_group ally_attr_groups[] = { { .attrs = ally_config_attrs,@@ -1070,6 +1438,28 @@ static const struct attribute_group ally_attr_groups[] = { }, }; +/* +* The joystick and trigger range calibration attributes are tied to the +* user-calibration capability: their sysfs groups are registered only +* when the device supports configuring those parameters, and the show +* and store callbacks reject accesses with -EOPNOTSUPP regardless, in +* case the groups are registered for another supported feature. +*/ +static const struct attribute_group ally_left_joystick_axis_group = { + .name ="left_joystick_axis" , + .attrs = left_joystick_axis_attrs, +}; + +static const struct attribute_group ally_right_joystick_axis_group = { + .name ="right_joystick_axis" , + .attrs = right_joystick_axis_attrs, +}; + +static const struct attribute_group *const ally_cal_attr_groups[] = { + &ally_left_joystick_axis_group, + &ally_right_joystick_axis_group, +}; +/** * ally_config_create() - Initialize configuration and create sysfs entries * @hdev: HID device @@ -1130,6 +1520,25 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al } } +/* Skip the calibration groups when the capability is missing. */ +if (cfg->user_cal_support) { +int cal_i; + +for (cal_i =0 ; cal_i <ARRAY_SIZE (ally_cal_attr_groups); + cal_i++) { + ret =sysfs_create_group (&hdev->dev.kobj, + ally_cal_attr_groups[cal_i]); +if (ret <0 ) { +hid_err (hdev,"Failed to create sysfs group '%s': %d\n" , + ally_cal_attr_groups[cal_i]->name, ret); +while (cal_i-- >0 ) +sysfs_remove_group (&hdev->dev.kobj, + ally_cal_attr_groups[cal_i]); + goto ally_config_create_sysfs_err; + } + } + } + cfg->initialized = true;return cfg;@@ -1160,6 +1569,18 @@ static void ally_config_remove(struct hid_device *hdev, struct ally_config *cfg)for (i =0 ; i <ARRAY_SIZE (ally_attr_groups); i++)sysfs_remove_group (&hdev->dev.kobj, &ally_attr_groups[i]); + +/* +* The calibration groups are removed with the same gating used to +* create them, so a device without those capabilities does not +* trigger a "not found" warning. +*/ +if (cfg->user_cal_support || cfg->anti_deadzone_support || + cfg->resp_curve_support) { +for (i =0 ; i <ARRAY_SIZE (ally_cal_attr_groups); i++) +sysfs_remove_group (&hdev->dev.kobj, + ally_cal_attr_groups[i]); + } }/** @@ -1458,6 +1879,18 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *allhid_warn (hdev,"Failed to restore vibration intensity: %d\n" , ret); +if (cfg->user_cal_support) { + ret =ally_set_joystick_thresholds (ally, hdev, + cfg->left_deadzone, + cfg->left_outer_threshold, + cfg->right_deadzone, + cfg->right_outer_threshold); +if (ret <0 ) +hid_warn (hdev, +"Failed to restore joystick thresholds: %d\n" , + ret); + } +return 0 ; }
Signature · 1 line
i.
Denis Benato
maintainerPATCH v11 5/18 HID: asus: add triggers inner and outer range configuration
In reply to Denis BenatoROG Ally devices allows configuring inner and outer ranges for triggers buttons on the back: allow userspace to configure the sensitivity by exposing sysfs attributes.
--- drivers/hid/hid-asus.c | 393 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 393 insertions(+)
drivers/hid/hid-asus.c +393 -0
@@ -1391,6 +1391,357 @@ static struct device_attribute dev_attr_right_joystick_outer_threshold =static struct device_attribute dev_attr_right_joystick_outer_threshold_range =__ATTR (outer_threshold_range,0444 , right_joystick_outer_threshold_range_show,NULL ); +/** +* ally_set_trigger_ranges() - Generic function to set triggers ranges +* @ally: ally handheld structure +* @hdev: HID device +* @left_it: lower limit of the left trigger range (0-50) +* @left_ot: upper limit of the left trigger range (70-100) +* @right_it: lower limit of the right trigger range (0-50) +* @right_ot: upper limit of the right trigger range (70-100) +* +* This function sends the command to set both inner and outer threshold +* for the left and right triggers. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_trigger_ranges (struct ally_handheld *ally, +struct hid_device *hdev, +u8 left_it,u8 left_ot,u8 right_it,u8 right_ot) +{ +const u8 payload[] = { left_it, left_ot, right_it, right_ot }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_TRIGGER_RANGE, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set trigger ranges: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static ssize_t left_trigger_range_lower_limit_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->left_trigger_min); +} + +static ssize_t left_trigger_range_lower_limit_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >50 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_trigger_ranges (ally, hdev, + value, + cfg->left_trigger_max, + cfg->right_trigger_min, + cfg->right_trigger_max); +if (ret) +return ret; + + cfg->left_trigger_min = value; + +sysfs_notify (&dev->kobj,"left_trigger" ,"range_lower_limit" ); + +return count; +} + +static ssize_t left_trigger_range_lower_limit_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"0 50\n" ); +} + +static ssize_t right_trigger_range_upper_limit_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->right_trigger_max); +} + +static ssize_t right_trigger_range_upper_limit_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value <70 || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_trigger_ranges (ally, hdev, + cfg->left_trigger_min, + cfg->left_trigger_max, + cfg->right_trigger_min, + value); +if (ret) +return ret; + + cfg->right_trigger_max = value; + +sysfs_notify (&dev->kobj,"right_trigger" ,"range_upper_limit" ); + +return count; +} + +static ssize_t right_trigger_range_upper_limit_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"70 100\n" ); +} + +static ssize_t right_trigger_range_lower_limit_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->right_trigger_min); +} + +static ssize_t right_trigger_range_lower_limit_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >50 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_trigger_ranges (ally, hdev, + cfg->left_trigger_min, + cfg->left_trigger_max, + value, + cfg->right_trigger_max); +if (ret) +return ret; + + cfg->right_trigger_min = value; + +sysfs_notify (&dev->kobj,"right_trigger" ,"range_lower_limit" ); + +return count; +} + +static ssize_t right_trigger_range_lower_limit_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"0 50\n" ); +} + +static ssize_t left_trigger_range_upper_limit_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + +return sysfs_emit (buf,"%u\n" , cfg->left_trigger_max); +} + +static ssize_t left_trigger_range_upper_limit_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value <70 || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->user_cal_support) +return -EOPNOTSUPP ; + + ret =ally_set_trigger_ranges (ally, hdev, + cfg->left_trigger_min, + value, + cfg->right_trigger_min, + cfg->right_trigger_max); +if (ret) +return ret; + + cfg->left_trigger_max = value; + +sysfs_notify (&dev->kobj,"left_trigger" ,"range_upper_limit" ); + +return count; +} + +static ssize_t left_trigger_range_upper_limit_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"70 100\n" ); +} + +static struct device_attribute dev_attr_left_trigger_range_lower_limit = +__ATTR (range_lower_limit,0644 , left_trigger_range_lower_limit_show, + left_trigger_range_lower_limit_store); + +static struct device_attribute dev_attr_left_trigger_range_lower_limit_range = +__ATTR (range_lower_limit_range,0444 , left_trigger_range_lower_limit_range_show,NULL ); + +static struct device_attribute dev_attr_left_trigger_range_upper_limit = +__ATTR (range_upper_limit,0644 , left_trigger_range_upper_limit_show, + left_trigger_range_upper_limit_store); + +static struct device_attribute dev_attr_left_trigger_range_upper_limit_range = +__ATTR (range_upper_limit_range,0444 , left_trigger_range_upper_limit_range_show,NULL ); + +static struct device_attribute dev_attr_right_trigger_range_lower_limit = +__ATTR (range_lower_limit,0644 , right_trigger_range_lower_limit_show, + right_trigger_range_lower_limit_store); + +static struct device_attribute dev_attr_right_trigger_range_lower_limit_range = +__ATTR (range_lower_limit_range,0444 , right_trigger_range_lower_limit_range_show,NULL ); + +static struct device_attribute dev_attr_right_trigger_range_upper_limit = +__ATTR (range_upper_limit,0644 , right_trigger_range_upper_limit_show, + right_trigger_range_upper_limit_store); + +static struct device_attribute dev_attr_right_trigger_range_upper_limit_range = +__ATTR (range_upper_limit_range,0444 , right_trigger_range_upper_limit_range_show,NULL ); +static struct attribute *ally_config_attrs[] = { &dev_attr_xbox_controller.attr,NULL
@@ -1424,6 +1775,22 @@ static struct attribute *right_joystick_axis_attrs[] = {NULL }; +static struct attribute *left_trigger_attrs[] = { + &dev_attr_left_trigger_range_lower_limit.attr, + &dev_attr_left_trigger_range_upper_limit.attr, + &dev_attr_left_trigger_range_lower_limit_range.attr, + &dev_attr_left_trigger_range_upper_limit_range.attr, +NULL +}; + +static struct attribute *right_trigger_attrs[] = { + &dev_attr_right_trigger_range_lower_limit.attr, + &dev_attr_right_trigger_range_upper_limit.attr, + &dev_attr_right_trigger_range_lower_limit_range.attr, + &dev_attr_right_trigger_range_upper_limit_range.attr, +NULL +}; +static const struct attribute_group ally_attr_groups[] = { { .attrs = ally_config_attrs,
@@ -1455,9 +1822,21 @@ static const struct attribute_group ally_right_joystick_axis_group = { .attrs = right_joystick_axis_attrs, }; +static const struct attribute_group ally_left_trigger_group = { + .name ="left_trigger" , + .attrs = left_trigger_attrs, +}; + +static const struct attribute_group ally_right_trigger_group = { + .name ="right_trigger" , + .attrs = right_trigger_attrs, +}; +static const struct attribute_group *const ally_cal_attr_groups[] = { &ally_left_joystick_axis_group, &ally_right_joystick_axis_group, + &ally_left_trigger_group, + &ally_right_trigger_group, };/**
@@ -1502,6 +1881,10 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al cfg->left_outer_threshold =90 ; cfg->right_deadzone =10 ; cfg->right_outer_threshold =90 ; + cfg->left_trigger_min =0 ; + cfg->left_trigger_max =100 ; + cfg->right_trigger_min =0 ; + cfg->right_trigger_max =100 ; cfg->vibration_intensity_left =100 ; cfg->vibration_intensity_right =100 ; }
@@ -1889,6 +2272,16 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *allhid_warn (hdev,"Failed to restore joystick thresholds: %d\n" , ret); + + ret =ally_set_trigger_ranges (ally, hdev, + cfg->left_trigger_min, + cfg->left_trigger_max, + cfg->right_trigger_min, + cfg->right_trigger_max); +if (ret <0 ) +hid_warn (hdev, +"Failed to restore trigger ranges: %d\n" , + ret); }return 0 ;
Signature · 1 line
2.47.3
Denis Benato
maintainerPATCH v11 6/18 HID: asus: add joysticks anti-deadzone configuration
In reply to Denis BenatoROG Ally devices allow configuring the anti-deadzone parameter for the resistive joysticks devices as over time those develops drift, therefore allow userspace to configure the anti-deadzone by exposing relevant sysfs attributes.
--- drivers/hid/hid-asus.c | 215 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 211 insertions(+), 4 deletions(-)
drivers/hid/hid-asus.c +211 -4
@@ -1391,6 +1391,197 @@ static struct device_attribute dev_attr_right_joystick_outer_threshold =static struct device_attribute dev_attr_right_joystick_outer_threshold_range =__ATTR (outer_threshold_range,0444 , right_joystick_outer_threshold_range_show,NULL ); +/** +* ally_set_anti_deadzone() - Set anti-deadzone values for joysticks +* @ally: ally handheld structure +* @hdev: HID device +* @cfg: ally config +* @left_adz: left joystick anti-deadzone value (0-100) +* @right_adz: right joystick anti-deadzone value (0-100) +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_anti_deadzone (struct ally_handheld *ally, +struct hid_device *hdev,struct ally_config *cfg, +u8 left_adz,u8 right_adz) +{ +const u8 payload[] = { left_adz, right_adz }; +int ret; + +if (!cfg->anti_deadzone_support) { +hid_dbg (hdev,"Anti-deadzone not supported on this device\n" ); +return -EOPNOTSUPP ; + } + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_ANTI_DEADZONE, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set anti-deadzone values: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static ssize_t left_joystick_anti_deadzone_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->anti_deadzone_support) { +hid_dbg (hdev,"Anti-deadzone not supported on this device\n" ); +return -EOPNOTSUPP ; + } + +return sysfs_emit (buf,"%u\n" , cfg->left_anti_deadzone); +} + +static ssize_t left_joystick_anti_deadzone_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->anti_deadzone_support) { +hid_dbg (hdev,"Anti-deadzone not supported on this device\n" ); +return -EOPNOTSUPP ; + } + + ret =ally_set_anti_deadzone (ally, hdev, cfg, value, cfg->right_anti_deadzone); +if (ret) +return ret; + + cfg->left_anti_deadzone = value; + +sysfs_notify (&dev->kobj,"left_joystick_axis" ,"anti_deadzone" ); + +return count; +} + +static ssize_t left_joystick_anti_deadzone_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"0 100\n" ); +} + +static ssize_t right_joystick_anti_deadzone_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->anti_deadzone_support) { +hid_dbg (hdev,"Anti-deadzone not supported on this device\n" ); +return -EOPNOTSUPP ; + } + +return sysfs_emit (buf,"%u\n" , cfg->right_anti_deadzone); +} + +static ssize_t right_joystick_anti_deadzone_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *const ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 value; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret || value >100 ) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->anti_deadzone_support) { +hid_dbg (hdev,"Anti-deadzone not supported on this device\n" ); +return -EOPNOTSUPP ; + } + + ret =ally_set_anti_deadzone (ally, hdev, cfg, cfg->left_anti_deadzone, value); +if (ret) +return ret; + + cfg->right_anti_deadzone = value; + +sysfs_notify (&dev->kobj,"right_joystick_axis" ,"anti_deadzone" ); + +return count; +} + +static ssize_t right_joystick_anti_deadzone_range_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"0 100\n" ); +} + +static struct device_attribute dev_attr_left_joystick_anti_deadzone = +__ATTR (anti_deadzone,0644 , left_joystick_anti_deadzone_show, + left_joystick_anti_deadzone_store); + +static struct device_attribute dev_attr_left_joystick_anti_deadzone_range = +__ATTR (anti_deadzone_range,0444 , left_joystick_anti_deadzone_range_show,NULL ); + +static struct device_attribute dev_attr_right_joystick_anti_deadzone = +__ATTR (anti_deadzone,0644 , right_joystick_anti_deadzone_show, + right_joystick_anti_deadzone_store); + +static struct device_attribute dev_attr_right_joystick_anti_deadzone_range = +__ATTR (anti_deadzone_range,0444 , right_joystick_anti_deadzone_range_show,NULL ); +/** * ally_set_trigger_ranges() - Generic function to set triggers ranges * @ally: ally handheld structure
@@ -1764,6 +1955,8 @@ static struct attribute *left_joystick_axis_attrs[] = { &dev_attr_left_joystick_outer_threshold.attr, &dev_attr_left_joystick_inner_threshold_range.attr, &dev_attr_left_joystick_outer_threshold_range.attr, + &dev_attr_left_joystick_anti_deadzone.attr, + &dev_attr_left_joystick_anti_deadzone_range.attr, NULL };
@@ -1772,6 +1965,8 @@ static struct attribute *right_joystick_axis_attrs[] = { &dev_attr_right_joystick_outer_threshold.attr, &dev_attr_right_joystick_inner_threshold_range.attr, &dev_attr_right_joystick_outer_threshold_range.attr, + &dev_attr_right_joystick_anti_deadzone.attr, + &dev_attr_right_joystick_anti_deadzone_range.attr, NULL };
@@ -1903,8 +2098,11 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al } } -/* Skip the calibration groups when the capability is missing. */ -if (cfg->user_cal_support) { +/* +* Skip the calibration and anti-deadzone groups when none of the +* features they expose is supported. +*/ +if (cfg->user_cal_support || cfg->anti_deadzone_support) {int cal_i; for (cal_i = 0; cal_i < ARRAY_SIZE(ally_cal_attr_groups);
@@ -1958,8 +2156,7 @@ static void ally_config_remove(struct hid_device *hdev, struct ally_config *cfg)* create them, so a device without those capabilities does not * trigger a "not found" warning. */ -if (cfg->user_cal_support || cfg->anti_deadzone_support || - cfg->resp_curve_support) { +if (cfg->user_cal_support || cfg->anti_deadzone_support) {for (i =0 ; i <ARRAY_SIZE (ally_cal_attr_groups); i++)sysfs_remove_group (&hdev->dev.kobj, ally_cal_attr_groups[i]);
@@ -2284,6 +2481,16 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +if (cfg->anti_deadzone_support) { + ret =ally_set_anti_deadzone (ally, hdev, cfg, + cfg->left_anti_deadzone, + cfg->right_anti_deadzone); +if (ret <0 ) +hid_warn (hdev, +"Failed to restore anti-deadzone: %d\n" , + ret); + } +return 0 ; }
Signature · 1 line
2.47.3
Denis Benato
maintainerPATCH v11 7/18 HID: asus: add support for response curve
In reply to Denis BenatoROG ally devices allows configuring the response curve of both joysticks, therefore add the ability of userspace to modify the response curve by exposing relevant sysfs attributes.
--- drivers/hid/hid-asus.c | 297 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 294 insertions(+), 3 deletions(-)
drivers/hid/hid-asus.c +294 -3
@@ -22,6 +22,7 @@#include <linux/acpi.h> #include <linux/cleanup.h> +#include <linux/ctype.h> #include <linux/device.h> #include <linux/dmi.h> #include <linux/hid.h>
@@ -185,6 +186,18 @@ struct asus_touchpad_info {int report_size; }; +struct ally_joystick_resp_curve_param { +u8 move; +u8 resp; +} __packed; + +struct ally_joystick_resp_curve { +struct ally_joystick_resp_curve_param entry_1; +struct ally_joystick_resp_curve_param entry_2; +struct ally_joystick_resp_curve_param entry_3; +struct ally_joystick_resp_curve_param entry_4; +} __packed; +struct ally_config {/* Must be locked if the data is being changed */ struct mutex config_mutex;
@@ -217,6 +230,9 @@ struct ally_config {/* Vibration settings */ u8 vibration_intensity_left;u8 vibration_intensity_right; + +struct ally_joystick_resp_curve left_curve; +struct ally_joystick_resp_curve right_curve; };/*
@@ -1933,6 +1949,235 @@ static struct device_attribute dev_attr_right_trigger_range_upper_limit =static struct device_attribute dev_attr_right_trigger_range_upper_limit_range =__ATTR (range_upper_limit_range,0444 , right_trigger_range_upper_limit_range_show,NULL ); +enum ally_joystick_side { +JOYSTICK_LEFT =0 , +JOYSTICK_RIGHT , +}; + +/** +* ally_set_joystick_resp_curve() - Set joystick response curve parameters +* @ally: ally handheld structure +* @hdev: HID device +* @side: which joystick side (0=left, 1=right) +* @curve: response curve parameter structure +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_joystick_resp_curve (struct ally_handheld *ally, +struct hid_device *hdev,enum ally_joystick_side side, +struct ally_joystick_resp_curve *curve) +{ +const u8 payload[] = { side, + curve->entry_1.move, curve->entry_1.resp, + curve->entry_2.move, curve->entry_2.resp, + curve->entry_3.move, curve->entry_3.resp, + curve->entry_4.move, curve->entry_4.resp + }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_RESP_CURVE, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) +return ret; + +return 0 ; +} + +/** +* ally_resp_curve_is_valid() - Check that a response curve is valid +* @curve: response curve parameter structure +* +* Return: true if the curve is valid, false otherwise +*/ +static bool ally_resp_curve_is_valid (const struct ally_joystick_resp_curve *curve) +{ +return curve->entry_1.move < curve->entry_2.move && + curve->entry_2.move < curve->entry_3.move && + curve->entry_3.move < curve->entry_4.move; +} + +static ssize_t response_curve_show (struct ally_config *cfg,bool is_left,char *buf) +{ +const struct ally_joystick_resp_curve *curve; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->resp_curve_support) +return -EOPNOTSUPP ; + + curve = is_left ? &cfg->left_curve : &cfg->right_curve; + +return sysfs_emit (buf,"%u %u %u %u %u %u %u %u\n" , + curve->entry_1.move, curve->entry_1.resp, + curve->entry_2.move, curve->entry_2.resp, + curve->entry_3.move, curve->entry_3.resp, + curve->entry_4.move, curve->entry_4.resp); +} + +static ssize_t response_curve_store (struct device *dev,struct ally_handheld *ally, +struct hid_device *hdev,struct ally_config *cfg, +bool is_left,const char *buf,size_t count) +{ +struct ally_joystick_resp_curve curve; +struct ally_joystick_resp_curve *cached; +unsigned int v[8 ]; +int used =0 ; +int ret, i; + +/* +* Parse and validate the whole curve before taking the lock: a +* write is all-or-nothing and a rejected one must leave the +* recorded curve untouched. +*/ + ret =sscanf (buf,"%u %u %u %u %u %u %u %u%n" , + &v[0 ], &v[1 ], &v[2 ], &v[3 ], &v[4 ], &v[5 ], &v[6 ], &v[7 ], &used); +if (ret !=8 ) +return -EINVAL ; + +for (i =0 ; i <ARRAY_SIZE (v); i++) { +if (v[i] >100 ) +return -EINVAL ; + } + +for (; buf[used]; used++) { +if (!isspace (buf[used])) +return -EINVAL ; + } + + curve.entry_1.move = v[0 ]; + curve.entry_1.resp = v[1 ]; + curve.entry_2.move = v[2 ]; + curve.entry_2.resp = v[3 ]; + curve.entry_3.move = v[4 ]; + curve.entry_3.resp = v[5 ]; + curve.entry_4.move = v[6 ]; + curve.entry_4.resp = v[7 ]; + +if (!ally_resp_curve_is_valid (&curve)) +return -EINVAL ; + +guard (mutex)(&cfg->config_mutex); + +if (!cfg->resp_curve_support) +return -EOPNOTSUPP ; + + ret =ally_set_joystick_resp_curve (ally, hdev, + is_left ?JOYSTICK_LEFT :JOYSTICK_RIGHT , + &curve); +if (ret <0 ) { +hid_err (hdev,"Failed to set joystick response curve: %d\n" , ret); +return ret; + } + +/* Only a curve the controller accepted becomes the recorded one. */ + cached = is_left ? &cfg->left_curve : &cfg->right_curve; + *cached = curve; + +sysfs_notify (&dev->kobj, + is_left ?"left_joystick_axis" :"right_joystick_axis" , +"response_curve" ); + +return count; +} + +static ssize_t left_response_curve_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +return response_curve_show (cfg, true, buf); +} + +static ssize_t left_response_curve_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +return response_curve_store (dev, ally, hdev, cfg, true, buf, count); +} + +static ssize_t right_response_curve_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +return response_curve_show (cfg, false, buf); +} + +static ssize_t right_response_curve_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +return response_curve_store (dev, ally, hdev, cfg, false, buf, count); +} + +static ssize_t response_curve_index_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf, +"move_1 resp_1 move_2 resp_2 move_3 resp_3 move_4 resp_4\n" +"\n" +"move_N: joystick deflection percentage at which curve point N\n" +"is placed (0-100, each move_N must be greater than the\n" +"previous one). resp_N: response percentage applied from\n" +"point N on (0-100).\n" ); +} + +static struct device_attribute dev_attr_left_response_curve = +__ATTR (response_curve,0644 , left_response_curve_show, left_response_curve_store); +static struct device_attribute dev_attr_right_response_curve = +__ATTR (response_curve,0644 , right_response_curve_show, right_response_curve_store); +static struct device_attribute dev_attr_left_response_curve_index = +__ATTR (response_curve_index,0444 , response_curve_index_show,NULL ); +static struct device_attribute dev_attr_right_response_curve_index = +__ATTR (response_curve_index,0444 , response_curve_index_show,NULL ); +static struct attribute *ally_config_attrs[] = { &dev_attr_xbox_controller.attr,NULL
@@ -1957,6 +2202,8 @@ static struct attribute *left_joystick_axis_attrs[] = { &dev_attr_left_joystick_outer_threshold_range.attr, &dev_attr_left_joystick_anti_deadzone.attr, &dev_attr_left_joystick_anti_deadzone_range.attr, + &dev_attr_left_response_curve.attr, + &dev_attr_left_response_curve_index.attr, NULL };
@@ -1967,6 +2214,8 @@ static struct attribute *right_joystick_axis_attrs[] = { &dev_attr_right_joystick_outer_threshold_range.attr, &dev_attr_right_joystick_anti_deadzone.attr, &dev_attr_right_joystick_anti_deadzone_range.attr, + &dev_attr_right_response_curve.attr, + &dev_attr_right_response_curve_index.attr, NULL };
@@ -2082,6 +2331,25 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al cfg->right_trigger_max =100 ; cfg->vibration_intensity_left =100 ; cfg->vibration_intensity_right =100 ; + +/* Initialize default response curve values (linear) */ + cfg->left_curve.entry_1.move =0 ; + cfg->left_curve.entry_1.resp =0 ; + cfg->left_curve.entry_2.move =33 ; + cfg->left_curve.entry_2.resp =33 ; + cfg->left_curve.entry_3.move =66 ; + cfg->left_curve.entry_3.resp =66 ; + cfg->left_curve.entry_4.move =100 ; + cfg->left_curve.entry_4.resp =100 ; + + cfg->right_curve.entry_1.move =0 ; + cfg->right_curve.entry_1.resp =0 ; + cfg->right_curve.entry_2.move =33 ; + cfg->right_curve.entry_2.resp =33 ; + cfg->right_curve.entry_3.move =66 ; + cfg->right_curve.entry_3.resp =66 ; + cfg->right_curve.entry_4.move =100 ; + cfg->right_curve.entry_4.resp =100 ; }spin_lock_irqsave (&ally_data_lock, flags);
@@ -2099,10 +2367,11 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al }/* -* Skip the calibration and anti-deadzone groups when none of the -* features they expose is supported. +* Skip the calibration, anti-deadzone and response curve groups when +* none of the features they expose is supported. */ -if (cfg->user_cal_support || cfg->anti_deadzone_support) { +if (cfg->user_cal_support || cfg->anti_deadzone_support || + cfg->resp_curve_support) {int cal_i; for (cal_i = 0; cal_i < ARRAY_SIZE(ally_cal_attr_groups);
@@ -2491,6 +2760,28 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +if (cfg->resp_curve_support) { +/* +* The MCU does not keep the response curve across a reset: +* send the recorded curves again. A write to the sysfs +* attribute validates and applies the curve atomically, so +* the cache always holds one the controller accepted. +*/ + ret =ally_set_joystick_resp_curve (ally, hdev,JOYSTICK_LEFT , + &cfg->left_curve); +if (ret <0 ) +hid_warn (hdev, +"Failed to restore left response curve: %d\n" , + ret); + + ret =ally_set_joystick_resp_curve (ally, hdev,JOYSTICK_RIGHT , + &cfg->right_curve); +if (ret <0 ) +hid_warn (hdev, +"Failed to restore right response curve: %d\n" , + ret); + } +return 0 ; }
Signature · 1 line
2.47.3
Ilpo Järvinen
maintainerRe: [PATCH v11 07/18] HID: asus: add support for response curve
Quoted 23 lines · Denis Benato
ROG ally devices allows configuring the response curve of both joysticks, therefore add the ability of userspace to modify the response curve by exposing relevant sysfs attributes.
Signed-off-by Denis Benato <[email protected]>Signed-off-by Luke Jones <[email protected]>--- drivers/hid/hid-asus.c | 297 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 294 insertions(+), 3 deletions(-)drivers/hid/hid-asus.c +1
-0@@ -22,6 +22,7 @@#include <linux/acpi.h> #include <linux/cleanup.h> +#include <linux/ctype.h> #include <linux/device.h> #include <linux/dmi.h> #include <linux/hid.h>
drivers/hid/hid-asus.c struct asus_touchpad_info { · line 192
Quoted 7 lines
drivers/hid/hid-asus.c +4
-0@@ -185,6 +186,18 @@ struct asus_touchpad_info {int report_size; }; +struct ally_joystick_resp_curve_param { +u8 move; +u8 resp; +} __packed;
Add include (if I didn't mentioned it already with some earlier patch, I don't recall anymore :-)).
Signature · 1 line
i.
Denis Benato
maintainerRe: [PATCH v11 07/18] HID: asus: add support for response curve
Quoted 28 lines · Ilpo Järvinen
Quoted 26 lines · Denis Benato
ROG ally devices allows configuring the response curve of both joysticks, therefore add the ability of userspace to modify the response curve by exposing relevant sysfs attributes.
Signed-off-by Denis Benato <[email protected]>Signed-off-by Luke Jones <[email protected]>--- drivers/hid/hid-asus.c | 297 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 294 insertions(+), 3 deletions(-)drivers/hid/hid-asus.c +1
-0@@ -22,6 +22,7 @@#include <linux/acpi.h> #include <linux/cleanup.h> +#include <linux/ctype.h> #include <linux/device.h> #include <linux/dmi.h> #include <linux/hid.h> @@ -185,6 +186,18 @@ struct asus_touchpad_info {int report_size; };
Quoted 6 lines
drivers/hid/hid-asus.c struct asus_touchpad_info { · line 192
Quoted 4 lines
drivers/hid/hid-asus.c +4
-0+struct ally_joystick_resp_curve_param { +u8 move; +u8 resp; +} __packed;Add include (if I didn't mentioned it already with some earlier patch, I don't recall anymore :-)).
Which one? I only see drivers either not including it directly or highly hardware-coupled including the one for the architecture, what about a driver that must work for x86 and arm64?
Ilpo Järvinen
maintainerRe: [PATCH v11 07/18] HID: asus: add support for response curve
Quoted 37 lines · Denis Benato
Quoted 28 lines · Ilpo Järvinen
Quoted 26 lines · Denis Benato
ROG ally devices allows configuring the response curve of both joysticks, therefore add the ability of userspace to modify the response curve by exposing relevant sysfs attributes.
Signed-off-by Denis Benato <[email protected]>Signed-off-by Luke Jones <[email protected]>--- drivers/hid/hid-asus.c | 297 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 294 insertions(+), 3 deletions(-)drivers/hid/hid-asus.c +1
-0@@ -22,6 +22,7 @@#include <linux/acpi.h> #include <linux/cleanup.h> +#include <linux/ctype.h> #include <linux/device.h> #include <linux/dmi.h> #include <linux/hid.h> @@ -185,6 +186,18 @@ struct asus_touchpad_info {int report_size; };Quoted 6 lines
drivers/hid/hid-asus.c struct asus_touchpad_info { · line 192
Quoted 4 lines
drivers/hid/hid-asus.c +4
-0+struct ally_joystick_resp_curve_param { +u8 move; +u8 resp; +} __packed;Add include (if I didn't mentioned it already with some earlier patch, I don't recall anymore :-)).
Quoted 2 lines
Which one? I only see drivers either not including it directly or highly hardware-coupled including the one for the architecture, what about a driver that must work for x86 and arm64?
__packed comes through linux/compiler.h
It's actually in include/linux/compiler_attributes.h but I assume we can rely on compiler.h including it for the foreseable future (guaranteed indirect include path).
Signature · 1 line
i.
Denis Benato
maintainerPATCH v11 8/18 HID: asus: add support to force feedback
In reply to Denis BenatoUnlike ROG ally the X version and following ones uses DInput protocol and the force feedback needs to be implemented as its protocol is vendor-specific, therefore add support for FF_RUMBLE with magnitude scaling on a work-queue based approach to avoid using possibly sleeping calls in atomic context.
--- drivers/hid/Kconfig | 1 + drivers/hid/hid-asus.c | 321 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 297 insertions(+), 25 deletions(-)
drivers/hid/Kconfig +1 -0
@@ -189,6 +189,7 @@ config HID_ASUS depends on USB_HID depends on LEDS_CLASS depends on ASUS_WMI || ASUS_WMI=n + select INPUT_FF_MEMLESS select POWER_SUPPLY help Support for Asus notebook built-in keyboard and touchpad via i2c, and
drivers/hid/hid-asus.c +296 -25
412 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v11 08/18] HID: asus: add support to force feedback
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] HID: asus: TOCTOU race on ally_x_hdev in hid_asus_ally_init() causes UAF - [Medium] HID: asus…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] HID: asus: TOCTOU race on ally_x_hdev in hid_asus_ally_init() causes UAF
- [Medium] HID: asus: FF init race and missing error cleanup in ally_x_setup_input()
- [Medium] HID: asus: race in hid_asus_ally_remove() and ally_x_ff_work_fn() teardown --
commit aa3d72be03857627e94d5d35f54f6366760f62af
Author: Denis Benato <[email protected]>
HID: asus: add support to force feedback
This commit adds force-feedback (FF_RUMBLE) support for the ASUS ROG Ally X gamepad interface using input_ff_create_memless() and a workqueue to send output reports asynchronously.
Quoted 4 lines
drivers/hid/hid-asus.c +0
-0
[ ... ]
Quoted 47 lines
drivers/hid/hid-asus.c +36
-0@@ -2674,6 +2812,36 @@ static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *allinput_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY );input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY1 ); +memcpy (&ally->ff_packet,ALLY_FORCE_FEEDBACK_OFF ,sizeof (ally->ff_packet)); +/* +* A play_effect() racing the previous teardown can have left the +* update latch set: a fresh interface must not inherit it, or the +* first rumble work would send a packet nobody asked for. +*/ + ally->update_ff = false; + ally->ff_work_initialized = true; + +input_set_capability (input,EV_FF ,FF_RUMBLE ); + +/* +* A registered device advertising FF_RUMBLE without the memless core +* behind it would crash input_ff_upload(): fail the whole setup instead. +*/ + ret =input_ff_create_memless (input,NULL , ally_x_play_effect); +if (ret) { +hid_err (hdev,"Failed to create force-feedback: %d\n" , ret); + goto ally_x_setup_input_err; + } + +/* +* Publish the interface before the input device becomes visible to +* userspace: an effect uploaded right after registration would +* otherwise find a NULL hdev and get dropped. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + ally->ally_x_hdev = hdev; +spin_unlock_irqrestore (&ally_data_lock, flags); + ret =input_register_device (input);if (ret) {hid_err (hdev,"Failed to register Ally X gamepad device: %d\n" , ret);@@ -2687,20 +2855,61 @@ static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *allreturn 0 ; ally_x_setup_input_err: +/* +* Unpublish like hid_asus_ally_remove() does, intf_mutex held: the +* cross-interface senders snapshot ally_x_hdev under ally_data_lock +* and send under intf_mutex, so the unpublish must take both in the +* same order to keep the handshake meaningful. +*/
drivers/hid/hid-asus.c static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *all · line 2873
Quoted 10 lines
drivers/hid/hid-asus.c +7
-0+ scoped_guard(mutex, &ally->intf_mutex) { +spin_lock_irqsave (&ally_data_lock, flags); +if (ally->ally_x_hdev == hdev) + ally->ally_x_hdev =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags); + } +input_free_device (input);return ret; }
[Severity: Medium]
In ally_x_setup_input(), ally->ff_packet, ally->update_ff, and ally->ff_work_initialized are updated without holding ally->ff_lock. In addition, if input_ff_create_memless() or input_register_device() fails and jumps to ally_x_setup_input_err, ally->ff_work_initialized remains set to true and ally->ff_work is not cancelled, while hid_asus_ally_probe() returns an error before setting drvdata->rog_ally (leaving hid_asus_ally_remove() as a no-op).
Also, ally_x_setup_input() publishes ally->ally_x_hdev and calls input_register_device(input) before hid_asus_ally_probe() sends the initial ally_x_send_ff_off(hdev) packet (and hid_asus_ally_probe() then assigns ally_drvdata.ally_x_hdev = hdev a second time). If userspace opens the new input device immediately upon registration and triggers an effect via ally_x_play_effect() -> ally_x_ff_work_fn(), can the late ally_x_send_ff_off(hdev) call in hid_asus_ally_probe() overwrite and silence that active rumble effect?
Should the initial ally_x_send_ff_off(hdev) happen before registering the input device, with ally->ff_packet, ally->update_ff, and ally->ff_work_initialized initialized under ally->ff_lock and cleaned up on the ally_x_setup_input_err path?
Quoted 34 lines
drivers/hid/hid-asus.c +23
-5static int hid_asus_ally_init (struct hid_device *hdev,struct ally_handheld *ally) { +struct hid_device *x_hdev;struct ally_config *cfg; +unsigned long flags;int ret; -/* Failure at this point is non-critical */ - ret =ally_gamepad_send_packet (ally, hdev,ALLY_FORCE_FEEDBACK_OFF , -sizeof (ALLY_FORCE_FEEDBACK_OFF )); -if (ret <0 ) -hid_err (hdev,"Ally failed to init force-feedback off: %d\n" , ret); +/* +* Snapshot the gamepad interface and send the force-feedback "off" +* packet through it. usbhid_stop() frees urbout before NULLing it +* while usbhid_output_report() dereferences it unlocked, so a send +* overlapping the transport teardown would be a use-after-free: the +* device lock is taken around the send because the HID core holds +* it across remove() and the hid_hw_stop() that follows, making the +* two mutually exclusive. It is taken before intf_mutex, the same +* order the core's remove path uses, so no lock inversion exists. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + x_hdev = ally->ally_x_hdev; +if (x_hdev) +get_device (&x_hdev->dev); +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (x_hdev) { +/* Failure at this point is non-critical */ + scoped_guard(mutex, &x_hdev->dev.mutex) { + scoped_guard(mutex, &ally->intf_mutex) { + ret =ally_x_send_ff_off (x_hdev);
drivers/hid/hid-asus.c static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *all · line 2912
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +if (ret <0 ) +hid_err (hdev, +"Ally failed to init force-feedback off: %d\n" , + ret); + } + } + +put_device (&x_hdev->dev); + }
[Severity: Critical]
Can this sequence in hid_asus_ally_init() race with gamepad interface removal and cause a use-after-free of usbhid (x_hdev->driver_data)?
In hid_asus_ally_init(), x_hdev is snapshotted from ally->ally_x_hdev under ally_data_lock before acquiring x_hdev->dev.mutex and ally->intf_mutex, and ally->ally_x_hdev == x_hdev is not re-checked after the mutexes are acquired.
If the gamepad interface is concurrently being disconnected or unbound:
CPU 0 (cfg probe or reset_resume) CPU 1 (gamepad disconnect / unbind)
--------------------------------- -----------------------------------hid_asus_ally_init()
spin_lock_irqsave(&ally_data_lock)
x_hdev = ally->ally_x_hdev;
get_device(&x_hdev->dev);
spin_unlock_irqrestore(&ally_data_lock)
usbhid_disconnect()
hid_destroy_device(x_hdev)
device_release_driver_internal()
mutex_lock(&x_hdev->dev.mutex)
asus_remove()
hid_asus_ally_remove()
ally->ally_x_hdev = NULL;
hid_hw_stop(x_hdev)
usbhid_stop(x_hdev)
mutex_unlock(&x_hdev->dev.mutex)
kfree(usbhid);
mutex_lock(&x_hdev->dev.mutex)
mutex_lock(&ally->intf_mutex)
ally_x_send_ff_off(x_hdev)
hid_hw_output_report(x_hdev, ...)
usbhid_output_report(x_hdev, ...)
usbhid = x_hdev->driver_data;
if (!usbhid->urbout) <-- UAF!Because get_device(&x_hdev->dev) only pins struct hid_device and not struct usbhid_device, usbhid_disconnect() frees usbhid as soon as hid_destroy_device() releases x_hdev->dev.mutex:
drivers/hid/usbhid/hid-core.c:usbhid_disconnect() {
...
hid_destroy_device(hid);
kfree(usbhid);
}When hid_asus_ally_init() then acquires x_hdev->dev.mutex and calls ally_x_send_ff_off(x_hdev), usbhid_output_report() dereferences the freed x_hdev->driver_data (or, on sysfs driver unbind without USB disconnect, accesses a stopped transport after hid_hw_stop()).
Should hid_asus_ally_init() re-verify that ally->ally_x_hdev == x_hdev under ally_data_lock after acquiring x_hdev->dev.mutex and ally->intf_mutex before calling ally_x_send_ff_off(x_hdev)?
[ ... ]
Quoted 59 lines
drivers/hid/hid-asus.c +42
-9@@ -3038,31 +3259,81 @@ static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld *cancel_delayed_work_sync (&ally->resume_work);spin_lock_irqsave (&ally_data_lock, flags); -if (ally->ally_x_hdev == hdev) { - x_input = ally->ally_x_input; - ally->ally_x_input =NULL ; - ally->ally_x_hdev =NULL ; + owns_xpad = ally->ally_x_hdev == hdev; +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (owns_xpad) { +/* +* Stop queueing force-feedback work before the gamepad +* pointers are cleared: play_effect() tests the flag under the +* same lock, so no work can be queued past the cancel below. +*/ +scoped_guard (spinlock_irqsave, &ally->ff_lock) + ally->ff_work_initialized = false; + +/* +* cancel_work_sync() may sleep: keep it out of ally_data_lock, +* but run it before the pointers are cleared so in-flight work +* cannot outlive the interface it sends through. +*/ +cancel_work_sync (&ally->ff_work); + +/* +* The input core can no longer stop effects through the +* disabled work: quiesce any rumble still playing ourselves, +* or the device would keep vibrating after being unbound. +* +* Serialize the packet with the other force-feedback senders: +* intf_mutex is taken again below for the pointer +* unpublishing, but the two critical sections never nest. +*/ + scoped_guard(mutex, &ally->intf_mutex) { +if (ally_x_send_ff_off (hdev) <0 ) +hid_warn (hdev, +"Failed to stop force-feedback\n" ); + } }/* -* The keyboard interface is torn down before the config one, and -* its input_dev is freed with it. handle_ally_event() and -* ally_resume_work_fn() both report keys through it from the -* config endpoint, so drop the references here or they dangle. +* Serialize with hid_asus_ally_init(): it sends the force-feedback +* "off" packet through the gamepad interface recorded here, whose +* transport is stopped by hid_hw_stop() as soon as this function +* returns. Holding intf_mutex while the gamepad pointers are +* unpublished makes init's snapshot-and-send atomic with the +* removal: the packet either reaches a transport that is still +* running, or is not sent at all. The lock must be released before +* the sysfs teardown below, or an in-flight sysfs store blocked on +* it would deadlock against kernfs waiting for the callback. */ -if (ally->keyboard_hdev == hdev) {
drivers/hid/hid-asus.c static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld * · line 3314
Quoted 10 lines
drivers/hid/hid-asus.c +7
-3- ally->keyboard_input =NULL ; - ally->keyboard_hdev =NULL ; - } + scoped_guard(mutex, &ally->intf_mutex) { +spin_lock_irqsave (&ally_data_lock, flags); +if (owns_xpad) { + x_input = ally->ally_x_input; + ally->ally_x_input =NULL ; + ally->ally_x_hdev =NULL ; + }
[Severity: Medium]
In hid_asus_ally_remove(), ally->ff_work_initialized is cleared, ally->ff_work is cancelled, and ally->ally_x_hdev is set to NULL before input_unregister_device(x_input) is called further down in the function.
Until input_unregister_device(x_input) disconnects evdev (flushing active effects via input_flush_device() -> input_ff_flush() -> erase_effect() -> ml_ff_playback()) and stops the ff-memless timer via ml_ff_stop(), ally_x_play_effect() can still run on x_input. Because clearing ally->ally_x_hdev releases the HID_ALLY_X_INTF_IN role reservation in hid_asus_ally_probe(), a concurrent re-probe can enter ally_x_setup_input() and re-enable ally->ff_work_initialized while x_input's teardown in input_unregister_device(x_input) is still invoking ally_x_play_effect().
Similarly, in ally_x_ff_work_fn(), hdev = ally->ally_x_hdev is snapshotted under ally_data_lock before acquiring ally->intf_mutex, without re-checking that ally->ally_x_hdev == hdev once ally->intf_mutex is held (which can race with the ally_x_setup_input_err unpublish path).
Would it be safer to unregister x_input before disabling and cancelling ally->ff_work and clearing ally->ally_x_hdev, and to re-check ally->ally_x_hdev == hdev under ally->intf_mutex in ally_x_ff_work_fn()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v11 08/18] HID: asus: add support to force feedback
In reply to Denis BenatoHi Denis,
I tested this on Asus T100TA, it seems to work fine.
Please see my other comments below.
Quoted 19 lines · Denis Benato
Unlike ROG ally the X version and following ones uses DInput protocol and the force feedback needs to be implemented as its protocol is vendor-specific, therefore add support for
FF_RUMBLEwith magnitude scaling on a work-queue based approach to avoid using possibly sleeping calls in atomic context.Signed-off-by Denis Benato <[email protected]>Signed-off-by Khamunetri Clark <[email protected]>Signed-off-by Luke Jones <[email protected]>--- drivers/hid/Kconfig | 1 + drivers/hid/hid-asus.c | 321 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 297 insertions(+), 25 deletions(-)drivers/hid/Kconfig +0
-0
drivers/hid/Kconfig config HID_ASUS · line 192
Quoted 4 lines
drivers/hid/Kconfig +1
-0@@ -189,6 +189,7 @@ config HID_ASUS depends on USB_HID depends on LEDS_CLASS depends on ASUS_WMI || ASUS_WMI=n + select INPUT_FF_MEMLESS
Tabs, please.
Quoted 32 lines
drivers/hid/Kconfig +0
-0select POWER_SUPPLY help Support for Asus notebook built-in keyboard and touchpad via i2c, anddrivers/hid/hid-asus.c +17
-0@@ -244,6 +244,23 @@ struct ally_config {*/ static struct ally_config *ally_config; +/* XInput force-feedback report (output report 0x0d, gamepad interface) */ +struct ff_data { +u8 enable; +u8 magnitude_left; +u8 magnitude_right; +u8 magnitude_strong; +u8 magnitude_weak; +u8 pulse_sustain_10ms; +u8 pulse_release_10ms; +u8 loop_count; +} __packed; + +struct ff_report { +u8 report_id; +struct ff_data ff; +} __packed; +struct ally_handheld {/* All read/write to IN interfaces must lock */ struct mutex intf_mutex;
drivers/hid/hid-asus.c struct ally_handheld { · line 294
Quoted 9 lines
drivers/hid/hid-asus.c +6
-0@@ -269,6 +286,13 @@ struct ally_handheld {bool ally_x_probe_in_progress;bool keyboard_probe_in_progress; +struct ff_report ff_packet; +struct work_struct ff_work; +/* Serializes ff_packet and update_ff between play_effect and ff_work */ +spinlock_t ff_lock; +bool ff_work_initialized; +bool update_ff;
Arranging these with the rest of the bools would save some memory.
Quoted 65 lines
drivers/hid/hid-asus.c +25
-1+struct hid_device *keyboard_hdev;struct input_dev *keyboard_input;@@ -384,10 +408,15 @@ enum ally_command_codes { CMD_SET_ANTI_DEADZONE = 0x18, }; +/* XInput rumble magnitudes use the hardware's 0..100 intensity range. */ +#define ALLY_FF_MAX_INTENSITY 100 +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = {0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB }; +static_assert (sizeof (struct ff_report ) ==sizeof (ALLY_FORCE_FEEDBACK_OFF )); +/* * The ROG Ally device presents multiple USB interfaces (keyboard, mouse, gamepad, * and custom configuration interface) that bind to the same module. Since only @@ -395,6 +424,7 @@ static const u8 ALLY_FORCE_FEEDBACK_OFF[] = {* ally_handheld structure to share state across these separate HID interfaces. */ static void ally_resume_work_fn (struct work_struct *work); +static void ally_x_ff_work_fn (struct work_struct *work);/* * Changes to ally_drvdata must lock: the raw_event callbacks, which may @@ -410,6 +440,13 @@ static struct ally_handheld ally_drvdata = {*/ .resume_work = __DELAYED_WORK_INITIALIZER(ally_drvdata.resume_work, ally_resume_work_fn, 0), +/* +* Initialised statically for the same reason as resume_work: remove() +* drains the work whichever of the interfaces probed or failed to +* probe, so it must be safe to cancel unconditionally. +*/ + .ff_work = __WORK_INITIALIZER(ally_drvdata.ff_work, ally_x_ff_work_fn), + .ff_lock = __SPIN_LOCK_UNLOCKED(ally_drvdata.ff_lock), };/* @@ -2425,7 +2462,8 @@ static void ally_config_remove(struct hid_device *hdev, struct ally_config *cfg)* create them, so a device without those capabilities does not * trigger a "not found" warning. */ -if (cfg->user_cal_support || cfg->anti_deadzone_support) { +if (cfg->user_cal_support || cfg->anti_deadzone_support || + cfg->resp_curve_support) {for (i =0 ; i <ARRAY_SIZE (ally_cal_attr_groups); i++)sysfs_remove_group (&hdev->dev.kobj, ally_cal_attr_groups[i]);@@ -2611,6 +2649,106 @@ static void ally_x_input_close(struct input_dev *dev)hid_hw_close (input_get_drvdata (dev)); } +/** +* ally_x_send_ff_report() - Send a force-feedback report to the gamepad +* @hdev: HID device +* @buf: buffer containing the report to send +* @len: length of the report +* +* The gamepad interface consumes force-feedback packets as output reports, +* unlike the config interface which expects feature reports: a rumble packet +* sent as HID_REQ_SET_REPORT would be rejected by the hardware.
drivers/hid/hid-asus.c static void ally_x_input_close(struct input_dev *dev) · line 2670
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* +* The caller must hold ally->intf_mutex, so that a send cannot race with +* the gamepad interface being unbound and its transport being stopped. +* +* Return: count of data transferred, negative if error +*/ +static int ally_x_send_ff_report (struct hid_device *hdev, +const u8 *buf,size_t len) +{ + u8 *dmabuf __free(kfree) = kmemdup(buf, len, GFP_KERNEL);
It's not very nice to assign variables and then check for errors. I'd thus separate the declaration and the assignment.
Quoted 18 lines
drivers/hid/hid-asus.c +18
-0+if (!dmabuf) +return -ENOMEM ; + +return hid_hw_output_report (hdev, dmabuf, len); +} + +static int ally_x_send_ff_off (struct hid_device *hdev) +{ +return ally_x_send_ff_report (hdev,ALLY_FORCE_FEEDBACK_OFF , +sizeof (ALLY_FORCE_FEEDBACK_OFF )); +} + +static void ally_x_ff_work_fn (struct work_struct *work) +{ +struct ally_handheld *ally = + container_of(work, struct ally_handheld, ff_work); +struct hid_device *hdev =NULL ; +struct ff_report report;
drivers/hid/hid-asus.c static void ally_x_input_close(struct input_dev *dev) · line 2698
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+unsigned long flags; +bool update = false; +int ret; + + scoped_guard(spinlock_irqsave, &ally->ff_lock) { +if (ally->update_ff) { + report = ally->ff_packet; + ally->update_ff = false; + update = true; + }
} else {
return;
}And you could omit update?
Quoted 79 lines
drivers/hid/hid-asus.c +72
-0+ } + +if (!update) +return ; + +/* +* The hdev pointer is published and cleared under ally_data_lock: +* take a reference on it so the gamepad interface cannot be freed +* under us while the report is being sent. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + hdev = ally->ally_x_hdev; +if (hdev) +get_device (&hdev->dev); +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (!hdev) +return ; + +/* Serialize with the interface removal paths and other senders. */ + scoped_guard(mutex, &ally->intf_mutex) { + ret =ally_x_send_ff_report (hdev, (u8 *)&report, +sizeof (report)); +if (ret <0 ) +hid_err (hdev,"Failed to send force-feedback: %d\n" , + ret); + } + +put_device (&hdev->dev); +} + +static int ally_x_play_effect (struct input_dev *idev,void *data, +struct ff_effect *effect) +{ +struct ally_handheld *ally = &ally_drvdata; + +if (effect->type !=FF_RUMBLE ) +return 0 ; + + scoped_guard(spinlock_irqsave, &ally->ff_lock) { + ally->ff_packet.ff.magnitude_strong = + effect->u.rumble.strong_magnitude *ALLY_FF_MAX_INTENSITY /65535 ; + ally->ff_packet.ff.magnitude_weak = + effect->u.rumble.weak_magnitude *ALLY_FF_MAX_INTENSITY /65535 ; + ally->update_ff = true; + +if (ally->ff_work_initialized) +schedule_work (&ally->ff_work); + } + +return 0 ; +} +static struct input_dev *ally_x_alloc_input_dev (struct hid_device *hdev) {struct input_dev *input_dev =input_allocate_device ();@@ -2674,6 +2812,36 @@ static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *allinput_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY );input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY1 ); +memcpy (&ally->ff_packet,ALLY_FORCE_FEEDBACK_OFF ,sizeof (ally->ff_packet)); +/* +* A play_effect() racing the previous teardown can have left the +* update latch set: a fresh interface must not inherit it, or the +* first rumble work would send a packet nobody asked for. +*/ + ally->update_ff = false; + ally->ff_work_initialized = true; + +input_set_capability (input,EV_FF ,FF_RUMBLE ); + +/* +* A registered device advertising FF_RUMBLE without the memless core +* behind it would crash input_ff_upload(): fail the whole setup instead. +*/ + ret =input_ff_create_memless (input,NULL , ally_x_play_effect); +if (ret) { +hid_err (hdev,"Failed to create force-feedback: %d\n" , ret); + goto ally_x_setup_input_err;
drivers/hid/hid-asus.c static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *all · line 2843
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ } + +/* +* Publish the interface before the input device becomes visible to +* userspace: an effect uploaded right after registration would +* otherwise find a NULL hdev and get dropped. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + ally->ally_x_hdev = hdev; +spin_unlock_irqrestore (&ally_data_lock, flags);
You're using scoped_guard() elsewhere in the patch but not here. Why?
There's also at least one case above, too.
Quoted 9 lines
drivers/hid/hid-asus.c +2
-0+ ret =input_register_device (input);if (ret) {hid_err (hdev,"Failed to register Ally X gamepad device: %d\n" , ret);@@ -2687,20 +2855,61 @@ static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *allreturn 0 ; ally_x_setup_input_err: +/*
drivers/hid/hid-asus.c static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *all · line 2868
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* Unpublish like hid_asus_ally_remove() does, intf_mutex held: the +* cross-interface senders snapshot ally_x_hdev under ally_data_lock +* and send under intf_mutex, so the unpublish must take both in the +* same order to keep the handshake meaningful. +*/ + scoped_guard(mutex, &ally->intf_mutex) { +spin_lock_irqsave (&ally_data_lock, flags); +if (ally->ally_x_hdev == hdev) + ally->ally_x_hdev =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags);
This gets interesting. :-)
Quoted 23 lines
drivers/hid/hid-asus.c +9
-5+ } +input_free_device (input);return ret; }static int hid_asus_ally_init (struct hid_device *hdev,struct ally_handheld *ally) { +struct hid_device *x_hdev;struct ally_config *cfg; +unsigned long flags;int ret; -/* Failure at this point is non-critical */ - ret =ally_gamepad_send_packet (ally, hdev,ALLY_FORCE_FEEDBACK_OFF , -sizeof (ALLY_FORCE_FEEDBACK_OFF )); -if (ret <0 ) -hid_err (hdev,"Ally failed to init force-feedback off: %d\n" , ret); +/* +* Snapshot the gamepad interface and send the force-feedback "off" +* packet through it. usbhid_stop() frees urbout before NULLing it +* while usbhid_output_report() dereferences it unlocked, so a send +* overlapping the transport teardown would be a use-after-free: the
drivers/hid/hid-asus.c static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *all · line 2896
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* device lock is taken around the send because the HID core holds +* it across remove() and the hid_hw_stop() that follows, making the +* two mutually exclusive. It is taken before intf_mutex, the same +* order the core's remove path uses, so no lock inversion exists. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + x_hdev = ally->ally_x_hdev; +if (x_hdev) +get_device (&x_hdev->dev); +spin_unlock_irqrestore (&ally_data_lock, flags);
Here, too.
Quoted 132 lines
drivers/hid/hid-asus.c +88
-18+ +if (x_hdev) { +/* Failure at this point is non-critical */ + scoped_guard(mutex, &x_hdev->dev.mutex) { + scoped_guard(mutex, &ally->intf_mutex) { + ret =ally_x_send_ff_off (x_hdev); + +if (ret <0 ) +hid_err (hdev, +"Ally failed to init force-feedback off: %d\n" , + ret); + } + } + +put_device (&x_hdev->dev); + } cfg =ally_get_config (ally);if (!cfg)@@ -2996,6 +3205,17 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)return ERR_PTR (ret); } +/* +* Make sure rumble starts disabled: this is the interface that +* owns the force-feedback output report. Failure is non-critical. +*/ + scoped_guard(mutex, &ally_drvdata.intf_mutex) { + ret =ally_x_send_ff_off (hdev); +if (ret <0 ) +hid_warn (hdev,"Failed to disable force-feedback: %d\n" , + ret); + } +spin_lock_irqsave (&ally_data_lock, flags); ally_drvdata.ally_x_hdev = hdev; ally_drvdata.ally_x_probe_in_progress = false;@@ -3024,6 +3244,7 @@ static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld *struct input_dev *x_input =NULL ;struct ally_config *cfg =NULL ;unsigned long flags; +bool owns_xpad;bool owns_cfg;if (!ally)@@ -3038,31 +3259,81 @@ static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld *cancel_delayed_work_sync (&ally->resume_work);spin_lock_irqsave (&ally_data_lock, flags); -if (ally->ally_x_hdev == hdev) { - x_input = ally->ally_x_input; - ally->ally_x_input =NULL ; - ally->ally_x_hdev =NULL ; + owns_xpad = ally->ally_x_hdev == hdev; +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (owns_xpad) { +/* +* Stop queueing force-feedback work before the gamepad +* pointers are cleared: play_effect() tests the flag under the +* same lock, so no work can be queued past the cancel below. +*/ +scoped_guard (spinlock_irqsave, &ally->ff_lock) + ally->ff_work_initialized = false; + +/* +* cancel_work_sync() may sleep: keep it out of ally_data_lock, +* but run it before the pointers are cleared so in-flight work +* cannot outlive the interface it sends through. +*/ +cancel_work_sync (&ally->ff_work); + +/* +* The input core can no longer stop effects through the +* disabled work: quiesce any rumble still playing ourselves, +* or the device would keep vibrating after being unbound. +* +* Serialize the packet with the other force-feedback senders: +* intf_mutex is taken again below for the pointer +* unpublishing, but the two critical sections never nest. +*/ + scoped_guard(mutex, &ally->intf_mutex) { +if (ally_x_send_ff_off (hdev) <0 ) +hid_warn (hdev, +"Failed to stop force-feedback\n" ); + } }/* -* The keyboard interface is torn down before the config one, and -* its input_dev is freed with it. handle_ally_event() and -* ally_resume_work_fn() both report keys through it from the -* config endpoint, so drop the references here or they dangle. +* Serialize with hid_asus_ally_init(): it sends the force-feedback +* "off" packet through the gamepad interface recorded here, whose +* transport is stopped by hid_hw_stop() as soon as this function +* returns. Holding intf_mutex while the gamepad pointers are +* unpublished makes init's snapshot-and-send atomic with the +* removal: the packet either reaches a transport that is still +* running, or is not sent at all. The lock must be released before +* the sysfs teardown below, or an in-flight sysfs store blocked on +* it would deadlock against kernfs waiting for the callback. */ -if (ally->keyboard_hdev == hdev) { - ally->keyboard_input =NULL ; - ally->keyboard_hdev =NULL ; - } + scoped_guard(mutex, &ally->intf_mutex) { +spin_lock_irqsave (&ally_data_lock, flags); +if (owns_xpad) { + x_input = ally->ally_x_input; + ally->ally_x_input =NULL ; + ally->ally_x_hdev =NULL ; + } - owns_cfg = ally->cfg_hdev == hdev; -if (owns_cfg) { - cfg = ally->config; - ally->cfg_hdev =NULL ; - ally->config =NULL ; - } +/* +* The keyboard interface is torn down before the +* config one, and its input_dev is freed with it. +* handle_ally_event() and ally_resume_work_fn() both +* report keys through it from the config endpoint, so +* drop the references here or they dangle. +*/ +if (ally->keyboard_hdev == hdev) { + ally->keyboard_input =NULL ; + ally->keyboard_hdev =NULL ; + }
drivers/hid/hid-asus.c static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld * · line 3335
Quoted 10 lines
drivers/hid/hid-asus.c +8
-1-spin_unlock_irqrestore (&ally_data_lock, flags); + owns_cfg = ally->cfg_hdev == hdev; +if (owns_cfg) { + cfg = ally->config; + ally->cfg_hdev =NULL ; + ally->config =NULL ; + } + +spin_unlock_irqrestore (&ally_data_lock, flags);
Ditto.
Quoted 4 lines
drivers/hid/hid-asus.c +1
-0+ }/* * The config teardown removes sysfs groups and takes sleeping locks:
Signature · 3 lines
Kind regards, Sakari Ailus
Ilpo Järvinen
maintainerRe: [PATCH v11 08/18] HID: asus: add support to force feedback
Quoted 147 lines · Sakari Ailus
Hi Denis,
I tested this on Asus T100TA, it seems to work fine.
Please see my other comments below.
Quoted 19 lines · Denis Benato
Unlike ROG ally the X version and following ones uses DInput protocol and the force feedback needs to be implemented as its protocol is vendor-specific, therefore add support for
FF_RUMBLEwith magnitude scaling on a work-queue based approach to avoid using possibly sleeping calls in atomic context.Signed-off-by Denis Benato <[email protected]>Signed-off-by Khamunetri Clark <[email protected]>Signed-off-by Luke Jones <[email protected]>--- drivers/hid/Kconfig | 1 + drivers/hid/hid-asus.c | 321 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 297 insertions(+), 25 deletions(-)drivers/hid/Kconfig +0
-0drivers/hid/Kconfig config HID_ASUS · line 192
Quoted 4 lines
drivers/hid/Kconfig +1
-0@@ -189,6 +189,7 @@ config HID_ASUS depends on USB_HID depends on LEDS_CLASS depends on ASUS_WMI || ASUS_WMI=n + select INPUT_FF_MEMLESSTabs, please.
Quoted 32 lines
drivers/hid/Kconfig +0
-0select POWER_SUPPLY help Support for Asus notebook built-in keyboard and touchpad via i2c, anddrivers/hid/hid-asus.c +17
-0@@ -244,6 +244,23 @@ struct ally_config {*/ static struct ally_config *ally_config; +/* XInput force-feedback report (output report 0x0d, gamepad interface) */ +struct ff_data { +u8 enable; +u8 magnitude_left; +u8 magnitude_right; +u8 magnitude_strong; +u8 magnitude_weak; +u8 pulse_sustain_10ms; +u8 pulse_release_10ms; +u8 loop_count; +} __packed; + +struct ff_report { +u8 report_id; +struct ff_data ff; +} __packed; +struct ally_handheld {/* All read/write to IN interfaces must lock */ struct mutex intf_mutex;drivers/hid/hid-asus.c struct ally_handheld { · line 294
Quoted 9 lines
drivers/hid/hid-asus.c +6
-0@@ -269,6 +286,13 @@ struct ally_handheld {bool ally_x_probe_in_progress;bool keyboard_probe_in_progress; +struct ff_report ff_packet; +struct work_struct ff_work; +/* Serializes ff_packet and update_ff between play_effect and ff_work */ +spinlock_t ff_lock; +bool ff_work_initialized; +bool update_ff;Arranging these with the rest of the bools would save some memory.
Quoted 70 lines
drivers/hid/hid-asus.c +30
-1+struct hid_device *keyboard_hdev;struct input_dev *keyboard_input;@@ -384,10 +408,15 @@ enum ally_command_codes { CMD_SET_ANTI_DEADZONE = 0x18, }; +/* XInput rumble magnitudes use the hardware's 0..100 intensity range. */ +#define ALLY_FF_MAX_INTENSITY 100 +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = {0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB }; +static_assert (sizeof (struct ff_report ) ==sizeof (ALLY_FORCE_FEEDBACK_OFF )); +/* * The ROG Ally device presents multiple USB interfaces (keyboard, mouse, gamepad, * and custom configuration interface) that bind to the same module. Since only @@ -395,6 +424,7 @@ static const u8 ALLY_FORCE_FEEDBACK_OFF[] = {* ally_handheld structure to share state across these separate HID interfaces. */ static void ally_resume_work_fn (struct work_struct *work); +static void ally_x_ff_work_fn (struct work_struct *work);/* * Changes to ally_drvdata must lock: the raw_event callbacks, which may @@ -410,6 +440,13 @@ static struct ally_handheld ally_drvdata = {*/ .resume_work = __DELAYED_WORK_INITIALIZER(ally_drvdata.resume_work, ally_resume_work_fn, 0), +/* +* Initialised statically for the same reason as resume_work: remove() +* drains the work whichever of the interfaces probed or failed to +* probe, so it must be safe to cancel unconditionally. +*/ + .ff_work = __WORK_INITIALIZER(ally_drvdata.ff_work, ally_x_ff_work_fn), + .ff_lock = __SPIN_LOCK_UNLOCKED(ally_drvdata.ff_lock), };/* @@ -2425,7 +2462,8 @@ static void ally_config_remove(struct hid_device *hdev, struct ally_config *cfg)* create them, so a device without those capabilities does not * trigger a "not found" warning. */ -if (cfg->user_cal_support || cfg->anti_deadzone_support) { +if (cfg->user_cal_support || cfg->anti_deadzone_support || + cfg->resp_curve_support) {for (i =0 ; i <ARRAY_SIZE (ally_cal_attr_groups); i++)sysfs_remove_group (&hdev->dev.kobj, ally_cal_attr_groups[i]);@@ -2611,6 +2649,106 @@ static void ally_x_input_close(struct input_dev *dev)hid_hw_close (input_get_drvdata (dev)); } +/** +* ally_x_send_ff_report() - Send a force-feedback report to the gamepad +* @hdev: HID device +* @buf: buffer containing the report to send +* @len: length of the report +* +* The gamepad interface consumes force-feedback packets as output reports, +* unlike the config interface which expects feature reports: a rumble packet +* sent as HID_REQ_SET_REPORT would be rejected by the hardware. +* +* The caller must hold ally->intf_mutex, so that a send cannot race with +* the gamepad interface being unbound and its transport being stopped. +* +* Return: count of data transferred, negative if error
Quoted 8 lines
drivers/hid/hid-asus.c static void ally_x_input_close(struct input_dev *dev) · line 2670
Quoted 5 lines
drivers/hid/hid-asus.c +5
-0+*/ +static int ally_x_send_ff_report (struct hid_device *hdev, +const u8 *buf,size_t len) +{ + u8 *dmabuf __free(kfree) = kmemdup(buf, len, GFP_KERNEL);It's not very nice to assign variables and then check for errors. I'd thus separate the declaration and the assignment.
I actually thought the same thing first.
But then stopped and realized, it's mid-function declaration of a cleanup variable exactly following the guidelines (the long comment in cleanup.h).
So this function doesn't really have variable declaration block at all, making the mid-function variable declaration look like one. I understand it looks confusing when encountering this for the first time, it was for me as well.
Quoted 5 lines
Quoted 5 lines
drivers/hid/hid-asus.c +5
-0+if (!dmabuf) +return -ENOMEM ; + +return hid_hw_output_report (hdev, dmabuf, len); +}
Signature · 1 line
i.
Re: [PATCH v11 08/18] HID: asus: add support to force feedback
Hi Ilpo,
Quoted 7 lines · Ilpo Järvinen
Quoted 4 lines
drivers/hid/hid-asus.c static void ally_x_input_close(struct input_dev *dev) · line 2670
Quoted 1 line
drivers/hid/hid-asus.c +1
-0+ u8 *dmabuf __free(kfree) = kmemdup(buf, len, GFP_KERNEL);It's not very nice to assign variables and then check for errors. I'd thus separate the declaration and the assignment.
I actually thought the same thing first.
Quoted 7 lines
But then stopped and realized, it's mid-function declaration of a cleanup variable exactly following the guidelines (the long comment in
cleanup.h).So this function doesn't really have variable declaration block at all, making the mid-function variable declaration look like one. I understand it looks confusing when encountering this for the first time, it was for me as well.
Indeed. I missed __free() there -- one shouldn't review patches too early in the morning!
Signature · 3 lines
Regards, Sakari Ailus
Ilpo Järvinen
maintainerRe: [PATCH v11 08/18] HID: asus: add support to force feedback
In reply to Denis BenatoQuoted 19 lines · Denis Benato
Unlike ROG ally the X version and following ones uses DInput protocol and the force feedback needs to be implemented as its protocol is vendor-specific, therefore add support for
FF_RUMBLEwith magnitude scaling on a work-queue based approach to avoid using possibly sleeping calls in atomic context.Signed-off-by Denis Benato <[email protected]>Signed-off-by Khamunetri Clark <[email protected]>Signed-off-by Luke Jones <[email protected]>--- drivers/hid/Kconfig | 1 + drivers/hid/hid-asus.c | 321 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 297 insertions(+), 25 deletions(-)drivers/hid/Kconfig +0
-0
drivers/hid/Kconfig config HID_ASUS · line 192
Quoted 4 lines
drivers/hid/Kconfig +1
-0@@ -189,6 +189,7 @@ config HID_ASUS depends on USB_HID depends on LEDS_CLASS depends on ASUS_WMI || ASUS_WMI=n + select INPUT_FF_MEMLESS
Different indentation characters?
Quoted 116 lines
drivers/hid/Kconfig +0
-0select POWER_SUPPLY help Support for Asus notebook built-in keyboard and touchpad via i2c, anddrivers/hid/hid-asus.c +58
-1@@ -244,6 +244,23 @@ struct ally_config {*/ static struct ally_config *ally_config; +/* XInput force-feedback report (output report 0x0d, gamepad interface) */ +struct ff_data { +u8 enable; +u8 magnitude_left; +u8 magnitude_right; +u8 magnitude_strong; +u8 magnitude_weak; +u8 pulse_sustain_10ms; +u8 pulse_release_10ms; +u8 loop_count; +} __packed; + +struct ff_report { +u8 report_id; +struct ff_data ff; +} __packed; +struct ally_handheld {/* All read/write to IN interfaces must lock */ struct mutex intf_mutex;@@ -269,6 +286,13 @@ struct ally_handheld {bool ally_x_probe_in_progress;bool keyboard_probe_in_progress; +struct ff_report ff_packet; +struct work_struct ff_work; +/* Serializes ff_packet and update_ff between play_effect and ff_work */ +spinlock_t ff_lock; +bool ff_work_initialized; +bool update_ff; +struct hid_device *keyboard_hdev;struct input_dev *keyboard_input;@@ -384,10 +408,15 @@ enum ally_command_codes { CMD_SET_ANTI_DEADZONE = 0x18, }; +/* XInput rumble magnitudes use the hardware's 0..100 intensity range. */ +#define ALLY_FF_MAX_INTENSITY 100 +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = {0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB }; +static_assert (sizeof (struct ff_report ) ==sizeof (ALLY_FORCE_FEEDBACK_OFF )); +/* * The ROG Ally device presents multiple USB interfaces (keyboard, mouse, gamepad, * and custom configuration interface) that bind to the same module. Since only @@ -395,6 +424,7 @@ static const u8 ALLY_FORCE_FEEDBACK_OFF[] = {* ally_handheld structure to share state across these separate HID interfaces. */ static void ally_resume_work_fn (struct work_struct *work); +static void ally_x_ff_work_fn (struct work_struct *work);/* * Changes to ally_drvdata must lock: the raw_event callbacks, which may @@ -410,6 +440,13 @@ static struct ally_handheld ally_drvdata = {*/ .resume_work = __DELAYED_WORK_INITIALIZER(ally_drvdata.resume_work, ally_resume_work_fn, 0), +/* +* Initialised statically for the same reason as resume_work: remove() +* drains the work whichever of the interfaces probed or failed to +* probe, so it must be safe to cancel unconditionally. +*/ + .ff_work = __WORK_INITIALIZER(ally_drvdata.ff_work, ally_x_ff_work_fn), + .ff_lock = __SPIN_LOCK_UNLOCKED(ally_drvdata.ff_lock), };/* @@ -2425,7 +2462,8 @@ static void ally_config_remove(struct hid_device *hdev, struct ally_config *cfg)* create them, so a device without those capabilities does not * trigger a "not found" warning. */ -if (cfg->user_cal_support || cfg->anti_deadzone_support) { +if (cfg->user_cal_support || cfg->anti_deadzone_support || + cfg->resp_curve_support) {for (i =0 ; i <ARRAY_SIZE (ally_cal_attr_groups); i++)sysfs_remove_group (&hdev->dev.kobj, ally_cal_attr_groups[i]);@@ -2611,6 +2649,106 @@ static void ally_x_input_close(struct input_dev *dev)hid_hw_close (input_get_drvdata (dev)); } +/** +* ally_x_send_ff_report() - Send a force-feedback report to the gamepad +* @hdev: HID device +* @buf: buffer containing the report to send +* @len: length of the report +* +* The gamepad interface consumes force-feedback packets as output reports, +* unlike the config interface which expects feature reports: a rumble packet +* sent as HID_REQ_SET_REPORT would be rejected by the hardware. +* +* The caller must hold ally->intf_mutex, so that a send cannot race with +* the gamepad interface being unbound and its transport being stopped. +* +* Return: count of data transferred, negative if error +*/ +static int ally_x_send_ff_report (struct hid_device *hdev, +const u8 *buf,size_t len) +{ + u8 *dmabuf __free(kfree) = kmemdup(buf, len, GFP_KERNEL);
drivers/hid/hid-asus.c static void ally_x_input_close(struct input_dev *dev) · line 2680
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+if (!dmabuf) +return -ENOMEM ; + +return hid_hw_output_report (hdev, dmabuf, len); +} + +static int ally_x_send_ff_off (struct hid_device *hdev) +{ +return ally_x_send_ff_report (hdev,ALLY_FORCE_FEEDBACK_OFF , +sizeof (ALLY_FORCE_FEEDBACK_OFF ));
Is this a real array here as well? Using caps for them is misleading. (I didn't bother checking, it likely is similar to the other case).
Quoted 52 lines
drivers/hid/hid-asus.c +52
-0+} + +static void ally_x_ff_work_fn (struct work_struct *work) +{ +struct ally_handheld *ally = + container_of(work, struct ally_handheld, ff_work); +struct hid_device *hdev =NULL ; +struct ff_report report; +unsigned long flags; +bool update = false; +int ret; + + scoped_guard(spinlock_irqsave, &ally->ff_lock) { +if (ally->update_ff) { + report = ally->ff_packet; + ally->update_ff = false; + update = true; + } + } + +if (!update) +return ; + +/* +* The hdev pointer is published and cleared under ally_data_lock: +* take a reference on it so the gamepad interface cannot be freed +* under us while the report is being sent. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + hdev = ally->ally_x_hdev; +if (hdev) +get_device (&hdev->dev); +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (!hdev) +return ; + +/* Serialize with the interface removal paths and other senders. */ + scoped_guard(mutex, &ally->intf_mutex) { + ret =ally_x_send_ff_report (hdev, (u8 *)&report, +sizeof (report)); +if (ret <0 ) +hid_err (hdev,"Failed to send force-feedback: %d\n" , + ret); + } + +put_device (&hdev->dev); +} + +static int ally_x_play_effect (struct input_dev *idev,void *data, +struct ff_effect *effect) +{
drivers/hid/hid-asus.c static void ally_x_input_close(struct input_dev *dev) · line 2742
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+struct ally_handheld *ally = &ally_drvdata; + +if (effect->type !=FF_RUMBLE ) +return 0 ; + + scoped_guard(spinlock_irqsave, &ally->ff_lock) { + ally->ff_packet.ff.magnitude_strong = + effect->u.rumble.strong_magnitude *ALLY_FF_MAX_INTENSITY /65535 ; + ally->ff_packet.ff.magnitude_weak = + effect->u.rumble.weak_magnitude *ALLY_FF_MAX_INTENSITY /65535 ;
U16_MAX x2 ? Check that you've the include if using it.
Quoted 13 lines
drivers/hid/hid-asus.c +9
-0+ ally->update_ff = true; + +if (ally->ff_work_initialized) +schedule_work (&ally->ff_work); + } + +return 0 ; +} +static struct input_dev *ally_x_alloc_input_dev (struct hid_device *hdev) {struct input_dev *input_dev =input_allocate_device ();
drivers/hid/hid-asus.c static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *all · line 2815
Quoted 4 lines
drivers/hid/hid-asus.c +1
-0@@ -2674,6 +2812,36 @@ static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *allinput_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY );input_set_capability (input,EV_KEY ,BTN_TRIGGER_HAPPY1 ); +memcpy (&ally->ff_packet,ALLY_FORCE_FEEDBACK_OFF ,sizeof (ally->ff_packet));
A real array named with caps?
Quoted 227 lines
drivers/hid/hid-asus.c +156
-24+/* +* A play_effect() racing the previous teardown can have left the +* update latch set: a fresh interface must not inherit it, or the +* first rumble work would send a packet nobody asked for. +*/ + ally->update_ff = false; + ally->ff_work_initialized = true; + +input_set_capability (input,EV_FF ,FF_RUMBLE ); + +/* +* A registered device advertising FF_RUMBLE without the memless core +* behind it would crash input_ff_upload(): fail the whole setup instead. +*/ + ret =input_ff_create_memless (input,NULL , ally_x_play_effect); +if (ret) { +hid_err (hdev,"Failed to create force-feedback: %d\n" , ret); + goto ally_x_setup_input_err; + } + +/* +* Publish the interface before the input device becomes visible to +* userspace: an effect uploaded right after registration would +* otherwise find a NULL hdev and get dropped. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + ally->ally_x_hdev = hdev; +spin_unlock_irqrestore (&ally_data_lock, flags); + ret =input_register_device (input);if (ret) {hid_err (hdev,"Failed to register Ally X gamepad device: %d\n" , ret);@@ -2687,20 +2855,61 @@ static int ally_x_setup_input(struct hid_device *hdev, struct ally_handheld *allreturn 0 ; ally_x_setup_input_err: +/* +* Unpublish like hid_asus_ally_remove() does, intf_mutex held: the +* cross-interface senders snapshot ally_x_hdev under ally_data_lock +* and send under intf_mutex, so the unpublish must take both in the +* same order to keep the handshake meaningful. +*/ + scoped_guard(mutex, &ally->intf_mutex) { +spin_lock_irqsave (&ally_data_lock, flags); +if (ally->ally_x_hdev == hdev) + ally->ally_x_hdev =NULL ; +spin_unlock_irqrestore (&ally_data_lock, flags); + } +input_free_device (input);return ret; }static int hid_asus_ally_init (struct hid_device *hdev,struct ally_handheld *ally) { +struct hid_device *x_hdev;struct ally_config *cfg; +unsigned long flags;int ret; -/* Failure at this point is non-critical */ - ret =ally_gamepad_send_packet (ally, hdev,ALLY_FORCE_FEEDBACK_OFF , -sizeof (ALLY_FORCE_FEEDBACK_OFF )); -if (ret <0 ) -hid_err (hdev,"Ally failed to init force-feedback off: %d\n" , ret); +/* +* Snapshot the gamepad interface and send the force-feedback "off" +* packet through it. usbhid_stop() frees urbout before NULLing it +* while usbhid_output_report() dereferences it unlocked, so a send +* overlapping the transport teardown would be a use-after-free: the +* device lock is taken around the send because the HID core holds +* it across remove() and the hid_hw_stop() that follows, making the +* two mutually exclusive. It is taken before intf_mutex, the same +* order the core's remove path uses, so no lock inversion exists. +*/ +spin_lock_irqsave (&ally_data_lock, flags); + x_hdev = ally->ally_x_hdev; +if (x_hdev) +get_device (&x_hdev->dev); +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (x_hdev) { +/* Failure at this point is non-critical */ + scoped_guard(mutex, &x_hdev->dev.mutex) { + scoped_guard(mutex, &ally->intf_mutex) { + ret =ally_x_send_ff_off (x_hdev); + +if (ret <0 ) +hid_err (hdev, +"Ally failed to init force-feedback off: %d\n" , + ret); + } + } + +put_device (&x_hdev->dev); + } cfg =ally_get_config (ally);if (!cfg)@@ -2996,6 +3205,17 @@ static struct ally_handheld *hid_asus_ally_probe(struct hid_device *hdev)return ERR_PTR (ret); } +/* +* Make sure rumble starts disabled: this is the interface that +* owns the force-feedback output report. Failure is non-critical. +*/ + scoped_guard(mutex, &ally_drvdata.intf_mutex) { + ret =ally_x_send_ff_off (hdev); +if (ret <0 ) +hid_warn (hdev,"Failed to disable force-feedback: %d\n" , + ret); + } +spin_lock_irqsave (&ally_data_lock, flags); ally_drvdata.ally_x_hdev = hdev; ally_drvdata.ally_x_probe_in_progress = false;@@ -3024,6 +3244,7 @@ static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld *struct input_dev *x_input =NULL ;struct ally_config *cfg =NULL ;unsigned long flags; +bool owns_xpad;bool owns_cfg;if (!ally)@@ -3038,31 +3259,81 @@ static void hid_asus_ally_remove(struct hid_device *hdev, struct ally_handheld *cancel_delayed_work_sync (&ally->resume_work);spin_lock_irqsave (&ally_data_lock, flags); -if (ally->ally_x_hdev == hdev) { - x_input = ally->ally_x_input; - ally->ally_x_input =NULL ; - ally->ally_x_hdev =NULL ; + owns_xpad = ally->ally_x_hdev == hdev; +spin_unlock_irqrestore (&ally_data_lock, flags); + +if (owns_xpad) { +/* +* Stop queueing force-feedback work before the gamepad +* pointers are cleared: play_effect() tests the flag under the +* same lock, so no work can be queued past the cancel below. +*/ +scoped_guard (spinlock_irqsave, &ally->ff_lock) + ally->ff_work_initialized = false; + +/* +* cancel_work_sync() may sleep: keep it out of ally_data_lock, +* but run it before the pointers are cleared so in-flight work +* cannot outlive the interface it sends through. +*/ +cancel_work_sync (&ally->ff_work); + +/* +* The input core can no longer stop effects through the +* disabled work: quiesce any rumble still playing ourselves, +* or the device would keep vibrating after being unbound. +* +* Serialize the packet with the other force-feedback senders: +* intf_mutex is taken again below for the pointer +* unpublishing, but the two critical sections never nest. +*/ + scoped_guard(mutex, &ally->intf_mutex) { +if (ally_x_send_ff_off (hdev) <0 ) +hid_warn (hdev, +"Failed to stop force-feedback\n" ); + } }/* -* The keyboard interface is torn down before the config one, and -* its input_dev is freed with it. handle_ally_event() and -* ally_resume_work_fn() both report keys through it from the -* config endpoint, so drop the references here or they dangle. +* Serialize with hid_asus_ally_init(): it sends the force-feedback +* "off" packet through the gamepad interface recorded here, whose +* transport is stopped by hid_hw_stop() as soon as this function +* returns. Holding intf_mutex while the gamepad pointers are +* unpublished makes init's snapshot-and-send atomic with the +* removal: the packet either reaches a transport that is still +* running, or is not sent at all. The lock must be released before +* the sysfs teardown below, or an in-flight sysfs store blocked on +* it would deadlock against kernfs waiting for the callback. */ -if (ally->keyboard_hdev == hdev) { - ally->keyboard_input =NULL ; - ally->keyboard_hdev =NULL ; - } + scoped_guard(mutex, &ally->intf_mutex) { +spin_lock_irqsave (&ally_data_lock, flags); +if (owns_xpad) { + x_input = ally->ally_x_input; + ally->ally_x_input =NULL ; + ally->ally_x_hdev =NULL ; + } - owns_cfg = ally->cfg_hdev == hdev; -if (owns_cfg) { - cfg = ally->config; - ally->cfg_hdev =NULL ; - ally->config =NULL ; - } +/* +* The keyboard interface is torn down before the +* config one, and its input_dev is freed with it. +* handle_ally_event() and ally_resume_work_fn() both +* report keys through it from the config endpoint, so +* drop the references here or they dangle. +*/ +if (ally->keyboard_hdev == hdev) { + ally->keyboard_input =NULL ; + ally->keyboard_hdev =NULL ; + } -spin_unlock_irqrestore (&ally_data_lock, flags); + owns_cfg = ally->cfg_hdev == hdev; +if (owns_cfg) { + cfg = ally->config; + ally->cfg_hdev =NULL ; + ally->config =NULL ; + } + +spin_unlock_irqrestore (&ally_data_lock, flags); + }/* * The config teardown removes sysfs groups and takes sleeping locks:
Signature · 1 line
i.
Denis Benato
maintainerPATCH v11 9/18 HID: asus: add support for gamepad mode
In reply to Denis BenatoROG Ally devices can emulate either a mouse+keyboard (desktop mode) or an gamepad device (xbox360 controller in ROG ally and a custom DInput device on newer models): add support for switching the current controller mode.
--- drivers/hid/hid-asus.c | 158 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 157 insertions(+), 1 deletion(-)
drivers/hid/hid-asus.c +157 -1
@@ -198,6 +198,11 @@ struct ally_joystick_resp_curve {struct ally_joystick_resp_curve_param entry_4; } __packed; +enum ally_gamepad_mode_index { +ALLY_GAMEPAD_MODE_GAMEPAD =0x01 , +ALLY_GAMEPAD_MODE_KEYBOARD =0x02 , +}; +struct ally_config {/* Must be locked if the data is being changed */ struct mutex config_mutex;
@@ -411,6 +416,15 @@ enum ally_command_codes {/* XInput rumble magnitudes use the hardware's 0..100 intensity range. */ #define ALLY_FF_MAX_INTENSITY 100 +static const char *const ally_gamepad_mode_text[] = { +"gamepad" ,"desktop" +}; + +static const u8 ally_gamepad_mode[] = { +ALLY_GAMEPAD_MODE_GAMEPAD , +ALLY_GAMEPAD_MODE_KEYBOARD +}; +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = {0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB };
@@ -930,6 +944,137 @@ static ssize_t xbox_controller_store(struct device *dev,static DEVICE_ATTR_RW (xbox_controller); +/** +* ally_set_gamepad_mode() - Set the gamepad operating mode +* @ally: ally handheld structure +* @hdev: HID device +* @mode: gamepad mode to set +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_gamepad_mode (struct ally_handheld *ally,struct hid_device *hdev,u8 mode) +{ +u8 payload[] = { mode }; +int ret; + +if (mode <ALLY_GAMEPAD_MODE_GAMEPAD || + mode >ALLY_GAMEPAD_MODE_KEYBOARD ) { +hid_err (hdev,"Invalid gamepad mode: %u\n" , mode); +return -EINVAL ; + } + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_GAMEPAD_MODE, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set gamepad mode: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static ssize_t gamepad_mode_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 mode_byte; +int i; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + + mode_byte = cfg->gamepad_mode; + +for (i =0 ; i <ARRAY_SIZE (ally_gamepad_mode); i++) { +if (ally_gamepad_mode[i] == mode_byte) +return sysfs_emit (buf,"%s\n" , ally_gamepad_mode_text[i]); + } + +/* +* The recorded mode only comes from the table above: an unknown +* byte means memory corruption, not a userspace-visible value. +*/ +return -EIO ; +} + +static ssize_t gamepad_mode_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 mode_byte; +int mode; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + mode =sysfs_match_string (ally_gamepad_mode_text, buf); +if (mode <0 ) { +hid_err (hdev,"Unknown gamepad mode\n" ); +return mode; + } + +/* Convert the index of the text mode array to the byte +* that will be accepted by the ally MCU. +*/ + mode_byte = ally_gamepad_mode[mode]; + +guard (mutex)(&cfg->config_mutex); + + ret =ally_set_gamepad_mode (ally, hdev, mode_byte); +if (ret <0 ) +return ret; + + cfg->gamepad_mode = mode_byte; + +hid_dbg (hdev,"Set gamepad mode to %s\n" , ally_gamepad_mode_text[mode]); + +sysfs_notify (&dev->kobj,NULL ,"gamepad_mode" ); + +return count; +} + +static ssize_t gamepad_mode_index_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int i; +ssize_t len =0 ; + +for (i =0 ; i <ARRAY_SIZE (ally_gamepad_mode_text); i++) { +if (!ally_gamepad_mode_text[i] || ally_gamepad_mode_text[i][0 ] =='\0' ) +continue ; + len +=sysfs_emit_at (buf, len,"%s " , ally_gamepad_mode_text[i]); + } + +/* Replace the last space with a newline */ +if (len >0 ) + buf[len -1 ] ='\n' ; + +return len; +} + +static DEVICE_ATTR_RW (gamepad_mode); +static DEVICE_ATTR_RO (gamepad_mode_index); +/** * ally_set_vibration_intensity() - Set vibration intensity values * @ally: ally handheld structure
@@ -2217,6 +2362,8 @@ static struct device_attribute dev_attr_right_response_curve_index =static struct attribute *ally_config_attrs[] = { &dev_attr_xbox_controller.attr, + &dev_attr_gamepad_mode.attr, + &dev_attr_gamepad_mode_index.attr,NULL };
@@ -2356,7 +2503,7 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al }if (!cfg->initialized) { - cfg->gamepad_mode =0x01 ; + cfg->gamepad_mode =ALLY_GAMEPAD_MODE_GAMEPAD ; cfg->xbox_controller_enabled = true; cfg->left_deadzone =10 ; cfg->left_outer_threshold =90 ;
@@ -2926,6 +3073,15 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +/* +* An MCU reset switches the gamepad mode back to its default: +* send the recorded one first, so that every other setting +* restored below is applied on top of the expected mode. +*/ + ret =ally_set_gamepad_mode (ally, hdev, cfg->gamepad_mode); +if (ret <0 ) +hid_warn (hdev,"Failed to restore gamepad mode: %d\n" , ret); +/* * The MCU does not keep the vibration intensity across a reset: * send the recorded values again, whichever interface probed.
Signature · 1 line
2.47.3
Ilpo Järvinen
maintainerRe: [PATCH v11 09/18] HID: asus: add support for gamepad mode
Quoted 28 lines · Denis Benato
ROG Ally devices can emulate either a mouse+keyboard (desktop mode) or an gamepad device (xbox360 controller in ROG ally and a custom DInput device on newer models): add support for switching the current controller mode.
Signed-off-by Luke Jones <[email protected]>Signed-off-by Denis Benato <[email protected]>--- drivers/hid/hid-asus.c | 158 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 157 insertions(+), 1 deletion(-)drivers/hid/hid-asus.c +5
-0@@ -198,6 +198,11 @@ struct ally_joystick_resp_curve {struct ally_joystick_resp_curve_param entry_4; } __packed; +enum ally_gamepad_mode_index { +ALLY_GAMEPAD_MODE_GAMEPAD =0x01 , +ALLY_GAMEPAD_MODE_KEYBOARD =0x02 , +}; +struct ally_config {/* Must be locked if the data is being changed */ struct mutex config_mutex;
drivers/hid/hid-asus.c enum ally_command_codes { · line 420
Quoted 5 lines
drivers/hid/hid-asus.c +2
-0@@ -411,6 +416,15 @@ enum ally_command_codes {/* XInput rumble magnitudes use the hardware's 0..100 intensity range. */ #define ALLY_FF_MAX_INTENSITY 100 +static const char *const ally_gamepad_mode_text[] = { +"gamepad" ,"desktop"
So this paired with ally_gamepad_mode, because ally_gamepad_mode_index is off-by-one?
drivers/hid/hid-asus.c enum ally_command_codes { · line 425
Quoted 5 lines
drivers/hid/hid-asus.c +5
-0+}; + +static const u8 ally_gamepad_mode[] = { +ALLY_GAMEPAD_MODE_GAMEPAD , +ALLY_GAMEPAD_MODE_KEYBOARD
Almost duplicate of _index variant?
Couldn't you use [xx] = style initializers with ally_gamepad_mode_text[] instead?
Quoted 116 lines
drivers/hid/hid-asus.c +109
-0+}; +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = {0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB };@@ -930,6 +944,137 @@ static ssize_t xbox_controller_store(struct device *dev,static DEVICE_ATTR_RW (xbox_controller); +/** +* ally_set_gamepad_mode() - Set the gamepad operating mode +* @ally: ally handheld structure +* @hdev: HID device +* @mode: gamepad mode to set +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_gamepad_mode (struct ally_handheld *ally,struct hid_device *hdev,u8 mode) +{ +u8 payload[] = { mode }; +int ret; + +if (mode <ALLY_GAMEPAD_MODE_GAMEPAD || + mode >ALLY_GAMEPAD_MODE_KEYBOARD ) { +hid_err (hdev,"Invalid gamepad mode: %u\n" , mode); +return -EINVAL ; + } + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_GAMEPAD_MODE, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set gamepad mode: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static ssize_t gamepad_mode_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 mode_byte; +int i; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + + mode_byte = cfg->gamepad_mode; + +for (i =0 ; i <ARRAY_SIZE (ally_gamepad_mode); i++) { +if (ally_gamepad_mode[i] == mode_byte) +return sysfs_emit (buf,"%s\n" , ally_gamepad_mode_text[i]); + } + +/* +* The recorded mode only comes from the table above: an unknown +* byte means memory corruption, not a userspace-visible value. +*/ +return -EIO ; +} + +static ssize_t gamepad_mode_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 mode_byte; +int mode; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + mode =sysfs_match_string (ally_gamepad_mode_text, buf); +if (mode <0 ) { +hid_err (hdev,"Unknown gamepad mode\n" ); +return mode; + } + +/* Convert the index of the text mode array to the byte +* that will be accepted by the ally MCU. +*/ + mode_byte = ally_gamepad_mode[mode]; + +guard (mutex)(&cfg->config_mutex); + + ret =ally_set_gamepad_mode (ally, hdev, mode_byte); +if (ret <0 ) +return ret; + + cfg->gamepad_mode = mode_byte; + +hid_dbg (hdev,"Set gamepad mode to %s\n" , ally_gamepad_mode_text[mode]); + +sysfs_notify (&dev->kobj,NULL ,"gamepad_mode" ); + +return count; +}
drivers/hid/hid-asus.c static ssize_t xbox_controller_store(struct device *dev, · line 1063
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +static ssize_t gamepad_mode_index_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int i; +ssize_t len =0 ; + +for (i =0 ; i <ARRAY_SIZE (ally_gamepad_mode_text); i++) { +if (!ally_gamepad_mode_text[i] || ally_gamepad_mode_text[i][0 ] =='\0' )
[i][0] == '\0' is not true for anything??
Quoted 52 lines
drivers/hid/hid-asus.c +26
-1+continue ; + len +=sysfs_emit_at (buf, len,"%s " , ally_gamepad_mode_text[i]); + } + +/* Replace the last space with a newline */ +if (len >0 ) + buf[len -1 ] ='\n' ; + +return len; +} + +static DEVICE_ATTR_RW (gamepad_mode); +static DEVICE_ATTR_RO (gamepad_mode_index); +/** * ally_set_vibration_intensity() - Set vibration intensity values * @ally: ally handheld structure @@ -2217,6 +2362,8 @@ static struct device_attribute dev_attr_right_response_curve_index =static struct attribute *ally_config_attrs[] = { &dev_attr_xbox_controller.attr, + &dev_attr_gamepad_mode.attr, + &dev_attr_gamepad_mode_index.attr,NULL };@@ -2356,7 +2503,7 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al }if (!cfg->initialized) { - cfg->gamepad_mode =0x01 ; + cfg->gamepad_mode =ALLY_GAMEPAD_MODE_GAMEPAD ; cfg->xbox_controller_enabled = true; cfg->left_deadzone =10 ; cfg->left_outer_threshold =90 ;@@ -2926,6 +3073,15 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +/* +* An MCU reset switches the gamepad mode back to its default: +* send the recorded one first, so that every other setting +* restored below is applied on top of the expected mode. +*/ + ret =ally_set_gamepad_mode (ally, hdev, cfg->gamepad_mode); +if (ret <0 ) +hid_warn (hdev,"Failed to restore gamepad mode: %d\n" , ret); +/* * The MCU does not keep the vibration intensity across a reset: * send the recorded values again, whichever interface probed.
Signature · 1 line
i.
Denis Benato
maintainerRe: [PATCH v11 09/18] HID: asus: add support for gamepad mode
Quoted 45 lines · Ilpo Järvinen
Quoted 28 lines · Denis Benato
ROG Ally devices can emulate either a mouse+keyboard (desktop mode) or an gamepad device (xbox360 controller in ROG ally and a custom DInput device on newer models): add support for switching the current controller mode.
Signed-off-by Luke Jones <[email protected]>Signed-off-by Denis Benato <[email protected]>--- drivers/hid/hid-asus.c | 158 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 157 insertions(+), 1 deletion(-)drivers/hid/hid-asus.c +5
-0@@ -198,6 +198,11 @@ struct ally_joystick_resp_curve {struct ally_joystick_resp_curve_param entry_4; } __packed; +enum ally_gamepad_mode_index { +ALLY_GAMEPAD_MODE_GAMEPAD =0x01 , +ALLY_GAMEPAD_MODE_KEYBOARD =0x02 , +}; +struct ally_config {/* Must be locked if the data is being changed */ struct mutex config_mutex;drivers/hid/hid-asus.c enum ally_command_codes { · line 420
Quoted 5 lines
drivers/hid/hid-asus.c +2
-0@@ -411,6 +416,15 @@ enum ally_command_codes {/* XInput rumble magnitudes use the hardware's 0..100 intensity range. */ #define ALLY_FF_MAX_INTENSITY 100 +static const char *const ally_gamepad_mode_text[] = { +"gamepad" ,"desktop" So this paired with
ally_gamepad_mode, becauseally_gamepad_mode_indexis off-by-one?drivers/hid/hid-asus.c enum ally_command_codes { · line 425
Quoted 5 lines
drivers/hid/hid-asus.c +5
-0+}; + +static const u8 ally_gamepad_mode[] = { +ALLY_GAMEPAD_MODE_GAMEPAD , +ALLY_GAMEPAD_MODE_KEYBOARD Almost duplicate of
_indexvariant?
Quoted 2 lines
Couldn't you use [xx] = style initializers with ally_gamepad_mode_text[] instead?
Yes, I could and it's what Luke did, but this would make the search asymmetric and more difficult so I decided this made other parts easier to understand.
Also I don't like treating enums as int and vice-versa because it can introduce over time subtle bugs...
And I wanted something that made it easier in the future to add more modes since I know there is at least one more: "bios".
But that requires more time and the benefits of it are unclear so I focused on how to make it easier to extend this in a clear way.
Quoted 180 lines
Quoted 116 lines
drivers/hid/hid-asus.c +109
-0+}; +static const u8 ALLY_FORCE_FEEDBACK_OFF [] = {0x0D ,0x0F ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x00 ,0xEB };@@ -930,6 +944,137 @@ static ssize_t xbox_controller_store(struct device *dev,static DEVICE_ATTR_RW (xbox_controller); +/** +* ally_set_gamepad_mode() - Set the gamepad operating mode +* @ally: ally handheld structure +* @hdev: HID device +* @mode: gamepad mode to set +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_gamepad_mode (struct ally_handheld *ally,struct hid_device *hdev,u8 mode) +{ +u8 payload[] = { mode }; +int ret; + +if (mode <ALLY_GAMEPAD_MODE_GAMEPAD || + mode >ALLY_GAMEPAD_MODE_KEYBOARD ) { +hid_err (hdev,"Invalid gamepad mode: %u\n" , mode); +return -EINVAL ; + } + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_GAMEPAD_MODE, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set gamepad mode: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static ssize_t gamepad_mode_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 mode_byte; +int i; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +guard (mutex)(&cfg->config_mutex); + + mode_byte = cfg->gamepad_mode; + +for (i =0 ; i <ARRAY_SIZE (ally_gamepad_mode); i++) { +if (ally_gamepad_mode[i] == mode_byte) +return sysfs_emit (buf,"%s\n" , ally_gamepad_mode_text[i]); + } + +/* +* The recorded mode only comes from the table above: an unknown +* byte means memory corruption, not a userspace-visible value. +*/ +return -EIO ; +} + +static ssize_t gamepad_mode_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_config *cfg; +u8 mode_byte; +int mode; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + mode =sysfs_match_string (ally_gamepad_mode_text, buf); +if (mode <0 ) { +hid_err (hdev,"Unknown gamepad mode\n" ); +return mode; + } + +/* Convert the index of the text mode array to the byte +* that will be accepted by the ally MCU. +*/ + mode_byte = ally_gamepad_mode[mode]; + +guard (mutex)(&cfg->config_mutex); + + ret =ally_set_gamepad_mode (ally, hdev, mode_byte); +if (ret <0 ) +return ret; + + cfg->gamepad_mode = mode_byte; + +hid_dbg (hdev,"Set gamepad mode to %s\n" , ally_gamepad_mode_text[mode]); + +sysfs_notify (&dev->kobj,NULL ,"gamepad_mode" ); + +return count; +}drivers/hid/hid-asus.c static ssize_t xbox_controller_store(struct device *dev, · line 1063
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +static ssize_t gamepad_mode_index_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int i; +ssize_t len =0 ; + +for (i =0 ; i <ARRAY_SIZE (ally_gamepad_mode_text); i++) { +if (!ally_gamepad_mode_text[i] || ally_gamepad_mode_text[i][0 ] =='\0' )[i][0] == '\0' is not true for anything??
Quoted 52 lines
drivers/hid/hid-asus.c +26
-1+continue ; + len +=sysfs_emit_at (buf, len,"%s " , ally_gamepad_mode_text[i]); + } + +/* Replace the last space with a newline */ +if (len >0 ) + buf[len -1 ] ='\n' ; + +return len; +} + +static DEVICE_ATTR_RW (gamepad_mode); +static DEVICE_ATTR_RO (gamepad_mode_index); +/** * ally_set_vibration_intensity() - Set vibration intensity values * @ally: ally handheld structure @@ -2217,6 +2362,8 @@ static struct device_attribute dev_attr_right_response_curve_index =static struct attribute *ally_config_attrs[] = { &dev_attr_xbox_controller.attr, + &dev_attr_gamepad_mode.attr, + &dev_attr_gamepad_mode_index.attr,NULL };@@ -2356,7 +2503,7 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al }if (!cfg->initialized) { - cfg->gamepad_mode =0x01 ; + cfg->gamepad_mode =ALLY_GAMEPAD_MODE_GAMEPAD ; cfg->xbox_controller_enabled = true; cfg->left_deadzone =10 ; cfg->left_outer_threshold =90 ;@@ -2926,6 +3073,15 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +/* +* An MCU reset switches the gamepad mode back to its default: +* send the recorded one first, so that every other setting +* restored below is applied on top of the expected mode. +*/ + ret =ally_set_gamepad_mode (ally, hdev, cfg->gamepad_mode); +if (ret <0 ) +hid_warn (hdev,"Failed to restore gamepad mode: %d\n" , ret); +/* * The MCU does not keep the vibration intensity across a reset: * send the recorded values again, whichever interface probed.
Denis Benato
maintainerPATCH v11 10/18 HID: asus: add support for turbo buttons
In reply to Denis BenatoROG ally devices supports a feature called turbo buttons that allows the user to quickly toggle button state by keeping a button pressed, therefore allow userspace to configure the feature by exposing relevant sysfs attributes.
--- drivers/hid/hid-asus.c | 463 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 463 insertions(+)
drivers/hid/hid-asus.c +463 -0
@@ -198,6 +198,99 @@ struct ally_joystick_resp_curve {struct ally_joystick_resp_curve_param entry_4; } __packed; +/* Button identifiers for the turbo attribute system */ +enum ally_button_id { +ALLY_BTN_A , +ALLY_BTN_B , +ALLY_BTN_X , +ALLY_BTN_Y , +ALLY_BTN_LB , +ALLY_BTN_RB , +ALLY_BTN_DU , +ALLY_BTN_DD , +ALLY_BTN_DL , +ALLY_BTN_DR , +ALLY_BTN_J0B , +ALLY_BTN_J1B , +ALLY_BTN_MENU , +ALLY_BTN_VIEW , +ALLY_BTN_M1 , +ALLY_BTN_M2 , +ALLY_BTN_MAX +}; + +/* Names for the button directories in sysfs */ +static const char *const ally_button_names[ALLY_BTN_MAX ] = { + [ALLY_BTN_A ] ="btn_a" , + [ALLY_BTN_B ] ="btn_b" , + [ALLY_BTN_X ] ="btn_x" , + [ALLY_BTN_Y ] ="btn_y" , + [ALLY_BTN_LB ] ="btn_lb" , + [ALLY_BTN_RB ] ="btn_rb" , + [ALLY_BTN_DU ] ="dpad_up" , + [ALLY_BTN_DD ] ="dpad_down" , + [ALLY_BTN_DL ] ="dpad_left" , + [ALLY_BTN_DR ] ="dpad_right" , + [ALLY_BTN_J0B ] ="btn_l3" , + [ALLY_BTN_J1B ] ="btn_r3" , + [ALLY_BTN_MENU ] ="btn_menu" , + [ALLY_BTN_VIEW ] ="btn_view" , + [ALLY_BTN_M1 ] ="btn_m1" , + [ALLY_BTN_M2 ] ="btn_m2" , +}; + +/* +* Button turbo parameters structure +* Each button can have: +* - turbo: Turbo press interval in multiples of 50ms (0 = disabled, 1-20 = 50ms-1000ms) +* - toggle: Toggle interval (0 = disabled) +*/ +struct ally_btn_turbo_params { +u8 turbo; +u8 toggle; +} __packed; + +#define ALLY_TURBO_PERIOD_MIN 0 +#define ALLY_TURBO_PERIOD_MAX 20 +#define ALLY_TOGGLE_PERIOD_MIN 0 +#define ALLY_TOGGLE_PERIOD_MAX 255 + +/* Collection of all button turbo settings */ +struct ally_turbo_config { +struct ally_btn_turbo_params btn_du; +struct ally_btn_turbo_params btn_dd; +struct ally_btn_turbo_params btn_dl; +struct ally_btn_turbo_params btn_dr; +struct ally_btn_turbo_params btn_j0b; +struct ally_btn_turbo_params btn_j1b; +struct ally_btn_turbo_params btn_lb; +struct ally_btn_turbo_params btn_rb; +struct ally_btn_turbo_params btn_a; +struct ally_btn_turbo_params btn_b; +struct ally_btn_turbo_params btn_x; +struct ally_btn_turbo_params btn_y; +struct ally_btn_turbo_params btn_view; +struct ally_btn_turbo_params btn_menu; +struct ally_btn_turbo_params btn_m2; +struct ally_btn_turbo_params btn_m1; +}; + +struct ally_btn_sysfs_entry { +struct attribute_group group; +struct attribute *attrs[5 ];/* turbo_period + toggle_period + ranges + NULL */ +/* +* The entries are created while hid_asus_ally_probe() is still running, +* before asus_probe() publishes drvdata->rog_ally: carrying the shared +* state pointer here avoids dereferencing that not-yet-assigned field. +*/ +struct ally_handheld *ally; +struct ally_config *cfg; +struct hid_device *hdev; +enum ally_button_id btn; +struct device_attribute attr_turbo_period; +struct device_attribute attr_toggle_period; +}; +enum ally_gamepad_mode_index {ALLY_GAMEPAD_MODE_GAMEPAD =0x01 ,ALLY_GAMEPAD_MODE_KEYBOARD =0x02 ,
@@ -236,6 +329,9 @@ struct ally_config {u8 vibration_intensity_left;u8 vibration_intensity_right; +struct ally_turbo_config turbo; +struct ally_btn_sysfs_entry *button_entries; +struct ally_joystick_resp_curve left_curve;struct ally_joystick_resp_curve right_curve; };
@@ -2467,6 +2563,343 @@ static const struct attribute_group *const ally_cal_attr_groups[] = { &ally_right_trigger_group, }; +/** +* ally_set_turbo_params() - Set turbo parameters for all buttons +* @ally: ally handheld structure +* @hdev: HID device +* @cfg: ally config structure +* +* The payload mirrors the whole per-button turbo configuration, so the +* caller must hold cfg->config_mutex: building it locklessly could send a +* mix of parameters from concurrent sysfs writes to the hardware. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_turbo_params (struct ally_handheld *ally, +struct hid_device *hdev, +struct ally_config *cfg) +{ +struct ally_turbo_config *turbo = &cfg->turbo; +const u8 payload[] = { + turbo->btn_du.turbo, + turbo->btn_du.toggle, + turbo->btn_dd.turbo, + turbo->btn_dd.toggle, + turbo->btn_dl.turbo, + turbo->btn_dl.toggle, + turbo->btn_dr.turbo, + turbo->btn_dr.toggle, + turbo->btn_j0b.turbo, + turbo->btn_j0b.toggle, + turbo->btn_j1b.turbo, + turbo->btn_j1b.toggle, + turbo->btn_lb.turbo, + turbo->btn_lb.toggle, + turbo->btn_rb.turbo, + turbo->btn_rb.toggle, + turbo->btn_a.turbo, + turbo->btn_a.toggle, + turbo->btn_b.turbo, + turbo->btn_b.toggle, + turbo->btn_x.turbo, + turbo->btn_x.toggle, + turbo->btn_y.turbo, + turbo->btn_y.toggle, + turbo->btn_view.turbo, + turbo->btn_view.toggle, + turbo->btn_menu.turbo, + turbo->btn_menu.toggle, + turbo->btn_m2.turbo, + turbo->btn_m2.toggle, + turbo->btn_m1.turbo, + turbo->btn_m1.toggle, + }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_TURBO_PARAMS, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set turbo parameters: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static struct ally_btn_turbo_params *ally_btn_get_turbo_params (struct ally_config *cfg, +enum ally_button_id btn) +{ +switch (btn) { +case ALLY_BTN_DU :return &cfg->turbo.btn_du; +case ALLY_BTN_DD :return &cfg->turbo.btn_dd; +case ALLY_BTN_DL :return &cfg->turbo.btn_dl; +case ALLY_BTN_DR :return &cfg->turbo.btn_dr; +case ALLY_BTN_J0B :return &cfg->turbo.btn_j0b; +case ALLY_BTN_J1B :return &cfg->turbo.btn_j1b; +case ALLY_BTN_LB :return &cfg->turbo.btn_lb; +case ALLY_BTN_RB :return &cfg->turbo.btn_rb; +case ALLY_BTN_A :return &cfg->turbo.btn_a; +case ALLY_BTN_B :return &cfg->turbo.btn_b; +case ALLY_BTN_X :return &cfg->turbo.btn_x; +case ALLY_BTN_Y :return &cfg->turbo.btn_y; +case ALLY_BTN_VIEW :return &cfg->turbo.btn_view; +case ALLY_BTN_MENU :return &cfg->turbo.btn_menu; +case ALLY_BTN_M2 :return &cfg->turbo.btn_m2; +case ALLY_BTN_M1 :return &cfg->turbo.btn_m1; +default :return NULL ; + } +} + +static ssize_t btn_turbo_period_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ + struct ally_btn_sysfs_entry *entry = container_of(attr, struct ally_btn_sysfs_entry, + attr_turbo_period); +struct ally_btn_turbo_params *params; + +if (!entry->cfg->turbo_support) +return -EOPNOTSUPP ; + + params =ally_btn_get_turbo_params (entry->cfg, entry->btn); +if (!params) +return -ENODEV ; + +guard (mutex)(&entry->cfg->config_mutex); + +return sysfs_emit (buf,"%hhu\n" , params->turbo); +} + +static ssize_t btn_turbo_period_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ + struct ally_btn_sysfs_entry *entry = container_of(attr, struct ally_btn_sysfs_entry, + attr_turbo_period); +struct ally_btn_turbo_params *params; +u8 old_value, value; +int ret; + +if (!entry->cfg->turbo_support) +return -EOPNOTSUPP ; + + params =ally_btn_get_turbo_params (entry->cfg, entry->btn); +if (!params) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret) +return ret; + +if (value <ALLY_TURBO_PERIOD_MIN || value >ALLY_TURBO_PERIOD_MAX ) +return -EINVAL ; + +guard (mutex)(&entry->cfg->config_mutex); + + old_value = params->turbo; + params->turbo = value; + ret =ally_set_turbo_params (entry->ally, entry->hdev, entry->cfg); +if (ret) { +/* +* The hardware rejected the update: restore the old +* value or a later write to another button would +* flush this stale one to the device. +*/ + params->turbo = old_value; +return ret; + } + +sysfs_notify (&entry->hdev->dev.kobj, entry->group.name,"turbo_period" ); + +return count; +} + +static ssize_t btn_toggle_period_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ + struct ally_btn_sysfs_entry *entry = container_of(attr, struct ally_btn_sysfs_entry, + attr_toggle_period); +struct ally_btn_turbo_params *params; + +if (!entry->cfg->turbo_support) +return -EOPNOTSUPP ; + + params =ally_btn_get_turbo_params (entry->cfg, entry->btn); +if (!params) +return -ENODEV ; + +guard (mutex)(&entry->cfg->config_mutex); + +return sysfs_emit (buf,"%hhu\n" , params->toggle); +} + +static ssize_t btn_toggle_period_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ + struct ally_btn_sysfs_entry *entry = container_of(attr, struct ally_btn_sysfs_entry, + attr_toggle_period); +struct ally_btn_turbo_params *params; +u8 old_value, value; +int ret; + +if (!entry->cfg->turbo_support) +return -EOPNOTSUPP ; + + params =ally_btn_get_turbo_params (entry->cfg, entry->btn); +if (!params) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret) +return ret; + +if (value <ALLY_TOGGLE_PERIOD_MIN || value >ALLY_TOGGLE_PERIOD_MAX ) +return -EINVAL ; + +guard (mutex)(&entry->cfg->config_mutex); + + old_value = params->toggle; + params->toggle = value; + ret =ally_set_turbo_params (entry->ally, entry->hdev, entry->cfg); +if (ret) { +/* +* The hardware rejected the update: restore the old +* value or a later write to another button would +* flush this stale one to the device. +*/ + params->toggle = old_value; +return ret; + } + +sysfs_notify (&entry->hdev->dev.kobj, entry->group.name,"toggle_period" ); + +return count; +} + +static ssize_t btn_turbo_period_range_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"0 20\n" ); +} + +static ssize_t btn_toggle_period_range_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"0 255\n" ); +} + +static struct device_attribute dev_attr_btn_turbo_period_range = +__ATTR (turbo_period_range,0444 , btn_turbo_period_range_show,NULL ); + +static struct device_attribute dev_attr_btn_toggle_period_range = +__ATTR (toggle_period_range,0444 , btn_toggle_period_range_show,NULL ); + +static void ally_btn_turbo_init_attrs (struct ally_btn_sysfs_entry *entry) +{ +sysfs_attr_init (&entry->attr_turbo_period.attr); + entry->attr_turbo_period.attr.name ="turbo_period" ; + entry->attr_turbo_period.attr.mode =0644 ; + entry->attr_turbo_period.show = btn_turbo_period_show; + entry->attr_turbo_period.store = btn_turbo_period_store; + +sysfs_attr_init (&entry->attr_toggle_period.attr); + entry->attr_toggle_period.attr.name ="toggle_period" ; + entry->attr_toggle_period.attr.mode =0644 ; + entry->attr_toggle_period.show = btn_toggle_period_show; + entry->attr_toggle_period.store = btn_toggle_period_store; + entry->attrs[0 ] = &entry->attr_turbo_period.attr; + entry->attrs[1 ] = &entry->attr_toggle_period.attr; + entry->attrs[2 ] = &dev_attr_btn_turbo_period_range.attr; + entry->attrs[3 ] = &dev_attr_btn_toggle_period_range.attr; + entry->attrs[4 ] =NULL ; +} + +/** +* ally_create_button_attributes() - Create turbo button attributes +* @hdev: HID device +* @ally: ally handheld structure +* @cfg: ally config structure +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_create_button_attributes (struct hid_device *hdev, +struct ally_handheld *ally, +struct ally_config *cfg) +{ +struct ally_btn_sysfs_entry *entries; +int i, ret; + +if (!cfg->turbo_support) +return 0 ; + + entries =kcalloc (ALLY_BTN_MAX ,sizeof (*entries),GFP_KERNEL ); +if (!entries) +return -ENOMEM ; + + cfg->button_entries = entries; + +for (i =0 ; i <ALLY_BTN_MAX ; i++) { + entries[i].ally = ally; + entries[i].cfg = cfg; + entries[i].hdev = hdev; + entries[i].btn = i; + +if (!ally_btn_get_turbo_params (cfg, i)) { +hid_err (hdev,"Invalid button id %d for turbo attributes\n" , i); + ret = -EINVAL ; + goto err_cleanup; + } + +ally_btn_turbo_init_attrs (&entries[i]); + + entries[i].group.name = ally_button_names[i]; + entries[i].group.attrs = entries[i].attrs; + + ret =sysfs_create_group (&hdev->dev.kobj, &entries[i].group); +if (ret <0 ) { +hid_err (hdev,"Failed to create sysfs group for %s: %d\n" , + ally_button_names[i], ret); + goto err_cleanup; + } + } + +return 0 ; + +err_cleanup: +while (--i >=0 ) +sysfs_remove_group (&hdev->dev.kobj, &entries[i].group); + +kfree (entries); + + cfg->button_entries =NULL ; + +return ret; +} + +/** +* ally_remove_button_attributes() - Remove turbo button attributes +* @hdev: HID device +* @cfg: ally config structure +*/ +static void ally_remove_button_attributes (struct hid_device *hdev,struct ally_config *cfg) +{ +struct ally_btn_sysfs_entry *entries; +int i; + +if (!cfg || !cfg->button_entries) +return ; + + entries = cfg->button_entries; + +for (i =0 ; i <ALLY_BTN_MAX ; i++) +sysfs_remove_group (&hdev->dev.kobj, &entries[i].group); + +kfree (entries); + + cfg->button_entries =NULL ; +} +/** * ally_config_create() - Initialize configuration and create sysfs entries * @hdev: HID device
@@ -2478,6 +2911,7 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al {struct ally_config *cfg;unsigned long flags; +int cal_created =0 ;int ret, sysfs_i;/*
@@ -2571,12 +3005,30 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al goto ally_config_create_sysfs_err; } } + +/* All the groups exist now: the error path must remove them. */ + cal_created =ARRAY_SIZE (ally_cal_attr_groups); + } + +if (cfg->turbo_support) { + ret =ally_create_button_attributes (hdev, ally, cfg); +if (ret <0 ) { +hid_err (hdev,"Failed to create button attributes: %d\n" , ret); + goto ally_config_create_sysfs_err; + } } cfg->initialized = true;return cfg; ally_config_create_sysfs_err: +if (cfg->button_entries) +ally_remove_button_attributes (hdev, cfg); + +while (cal_created-- >0 ) +sysfs_remove_group (&hdev->dev.kobj, + ally_cal_attr_groups[cal_created]); +spin_lock_irqsave (&ally_data_lock, flags); ally->config =NULL ;spin_unlock_irqrestore (&ally_data_lock, flags);
@@ -2601,6 +3053,9 @@ static void ally_config_remove(struct hid_device *hdev, struct ally_config *cfg)if (!cfg || !cfg->initialized)return ; +if (cfg->button_entries) +ally_remove_button_attributes (hdev, cfg); +for (i =0 ; i <ARRAY_SIZE (ally_attr_groups); i++)sysfs_remove_group (&hdev->dev.kobj, &ally_attr_groups[i]);
@@ -3147,6 +3602,14 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +if (cfg->turbo_support) { + ret =ally_set_turbo_params (ally, hdev, cfg); +if (ret <0 ) +hid_warn (hdev, +"Failed to restore turbo parameters: %d\n" , + ret); + } +return 0 ; }
Signature · 1 line
2.47.3
Ilpo Järvinen
maintainerRe: [PATCH v11 10/18] HID: asus: add support for turbo buttons
Quoted 269 lines · Denis Benato
ROG ally devices supports a feature called turbo buttons that allows the user to quickly toggle button state by keeping a button pressed, therefore allow userspace to configure the feature by exposing relevant sysfs attributes.
Signed-off-by Denis Benato <[email protected]>Signed-off-by Khamunetri Clark <[email protected]>Signed-off-by Luke Jones <[email protected]>Signed-off-by Jonathan LoBue <[email protected]>--- drivers/hid/hid-asus.c | 463 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 463 insertions(+)drivers/hid/hid-asus.c +234
-0@@ -198,6 +198,99 @@ struct ally_joystick_resp_curve {struct ally_joystick_resp_curve_param entry_4; } __packed; +/* Button identifiers for the turbo attribute system */ +enum ally_button_id { +ALLY_BTN_A , +ALLY_BTN_B , +ALLY_BTN_X , +ALLY_BTN_Y , +ALLY_BTN_LB , +ALLY_BTN_RB , +ALLY_BTN_DU , +ALLY_BTN_DD , +ALLY_BTN_DL , +ALLY_BTN_DR , +ALLY_BTN_J0B , +ALLY_BTN_J1B , +ALLY_BTN_MENU , +ALLY_BTN_VIEW , +ALLY_BTN_M1 , +ALLY_BTN_M2 , +ALLY_BTN_MAX +}; + +/* Names for the button directories in sysfs */ +static const char *const ally_button_names[ALLY_BTN_MAX ] = { + [ALLY_BTN_A ] ="btn_a" , + [ALLY_BTN_B ] ="btn_b" , + [ALLY_BTN_X ] ="btn_x" , + [ALLY_BTN_Y ] ="btn_y" , + [ALLY_BTN_LB ] ="btn_lb" , + [ALLY_BTN_RB ] ="btn_rb" , + [ALLY_BTN_DU ] ="dpad_up" , + [ALLY_BTN_DD ] ="dpad_down" , + [ALLY_BTN_DL ] ="dpad_left" , + [ALLY_BTN_DR ] ="dpad_right" , + [ALLY_BTN_J0B ] ="btn_l3" , + [ALLY_BTN_J1B ] ="btn_r3" , + [ALLY_BTN_MENU ] ="btn_menu" , + [ALLY_BTN_VIEW ] ="btn_view" , + [ALLY_BTN_M1 ] ="btn_m1" , + [ALLY_BTN_M2 ] ="btn_m2" , +}; + +/* +* Button turbo parameters structure +* Each button can have: +* - turbo: Turbo press interval in multiples of 50ms (0 = disabled, 1-20 = 50ms-1000ms) +* - toggle: Toggle interval (0 = disabled) +*/ +struct ally_btn_turbo_params { +u8 turbo; +u8 toggle; +} __packed; + +#define ALLY_TURBO_PERIOD_MIN 0 +#define ALLY_TURBO_PERIOD_MAX 20 +#define ALLY_TOGGLE_PERIOD_MIN 0 +#define ALLY_TOGGLE_PERIOD_MAX 255 + +/* Collection of all button turbo settings */ +struct ally_turbo_config { +struct ally_btn_turbo_params btn_du; +struct ally_btn_turbo_params btn_dd; +struct ally_btn_turbo_params btn_dl; +struct ally_btn_turbo_params btn_dr; +struct ally_btn_turbo_params btn_j0b; +struct ally_btn_turbo_params btn_j1b; +struct ally_btn_turbo_params btn_lb; +struct ally_btn_turbo_params btn_rb; +struct ally_btn_turbo_params btn_a; +struct ally_btn_turbo_params btn_b; +struct ally_btn_turbo_params btn_x; +struct ally_btn_turbo_params btn_y; +struct ally_btn_turbo_params btn_view; +struct ally_btn_turbo_params btn_menu; +struct ally_btn_turbo_params btn_m2; +struct ally_btn_turbo_params btn_m1; +}; + +struct ally_btn_sysfs_entry { +struct attribute_group group; +struct attribute *attrs[5 ];/* turbo_period + toggle_period + ranges + NULL */ +/* +* The entries are created while hid_asus_ally_probe() is still running, +* before asus_probe() publishes drvdata->rog_ally: carrying the shared +* state pointer here avoids dereferencing that not-yet-assigned field. +*/ +struct ally_handheld *ally; +struct ally_config *cfg; +struct hid_device *hdev; +enum ally_button_id btn; +struct device_attribute attr_turbo_period; +struct device_attribute attr_toggle_period; +}; +enum ally_gamepad_mode_index {ALLY_GAMEPAD_MODE_GAMEPAD =0x01 ,ALLY_GAMEPAD_MODE_KEYBOARD =0x02 ,@@ -236,6 +329,9 @@ struct ally_config {u8 vibration_intensity_left;u8 vibration_intensity_right; +struct ally_turbo_config turbo; +struct ally_btn_sysfs_entry *button_entries; +struct ally_joystick_resp_curve left_curve;struct ally_joystick_resp_curve right_curve; };@@ -2467,6 +2563,343 @@ static const struct attribute_group *const ally_cal_attr_groups[] = { &ally_right_trigger_group, }; +/** +* ally_set_turbo_params() - Set turbo parameters for all buttons +* @ally: ally handheld structure +* @hdev: HID device +* @cfg: ally config structure +* +* The payload mirrors the whole per-button turbo configuration, so the +* caller must hold cfg->config_mutex: building it locklessly could send a +* mix of parameters from concurrent sysfs writes to the hardware. +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_set_turbo_params (struct ally_handheld *ally, +struct hid_device *hdev, +struct ally_config *cfg) +{ +struct ally_turbo_config *turbo = &cfg->turbo; +const u8 payload[] = { + turbo->btn_du.turbo, + turbo->btn_du.toggle, + turbo->btn_dd.turbo, + turbo->btn_dd.toggle, + turbo->btn_dl.turbo, + turbo->btn_dl.toggle, + turbo->btn_dr.turbo, + turbo->btn_dr.toggle, + turbo->btn_j0b.turbo, + turbo->btn_j0b.toggle, + turbo->btn_j1b.turbo, + turbo->btn_j1b.toggle, + turbo->btn_lb.turbo, + turbo->btn_lb.toggle, + turbo->btn_rb.turbo, + turbo->btn_rb.toggle, + turbo->btn_a.turbo, + turbo->btn_a.toggle, + turbo->btn_b.turbo, + turbo->btn_b.toggle, + turbo->btn_x.turbo, + turbo->btn_x.toggle, + turbo->btn_y.turbo, + turbo->btn_y.toggle, + turbo->btn_view.turbo, + turbo->btn_view.toggle, + turbo->btn_menu.turbo, + turbo->btn_menu.toggle, + turbo->btn_m2.turbo, + turbo->btn_m2.toggle, + turbo->btn_m1.turbo, + turbo->btn_m1.toggle, + }; +int ret; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_TURBO_PARAMS, payload, sizeof(payload)); +if (!buf) +return -ENOMEM ; + + ret =ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +if (ret <0 ) { +hid_err (hdev,"Failed to set turbo parameters: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static struct ally_btn_turbo_params *ally_btn_get_turbo_params (struct ally_config *cfg, +enum ally_button_id btn) +{ +switch (btn) { +case ALLY_BTN_DU :return &cfg->turbo.btn_du; +case ALLY_BTN_DD :return &cfg->turbo.btn_dd; +case ALLY_BTN_DL :return &cfg->turbo.btn_dl; +case ALLY_BTN_DR :return &cfg->turbo.btn_dr; +case ALLY_BTN_J0B :return &cfg->turbo.btn_j0b; +case ALLY_BTN_J1B :return &cfg->turbo.btn_j1b; +case ALLY_BTN_LB :return &cfg->turbo.btn_lb; +case ALLY_BTN_RB :return &cfg->turbo.btn_rb; +case ALLY_BTN_A :return &cfg->turbo.btn_a; +case ALLY_BTN_B :return &cfg->turbo.btn_b; +case ALLY_BTN_X :return &cfg->turbo.btn_x; +case ALLY_BTN_Y :return &cfg->turbo.btn_y; +case ALLY_BTN_VIEW :return &cfg->turbo.btn_view; +case ALLY_BTN_MENU :return &cfg->turbo.btn_menu; +case ALLY_BTN_M2 :return &cfg->turbo.btn_m2; +case ALLY_BTN_M1 :return &cfg->turbo.btn_m1; +default :return NULL ; + } +} + +static ssize_t btn_turbo_period_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ + struct ally_btn_sysfs_entry *entry = container_of(attr, struct ally_btn_sysfs_entry, + attr_turbo_period); +struct ally_btn_turbo_params *params; + +if (!entry->cfg->turbo_support) +return -EOPNOTSUPP ; + + params =ally_btn_get_turbo_params (entry->cfg, entry->btn); +if (!params) +return -ENODEV ; + +guard (mutex)(&entry->cfg->config_mutex); + +return sysfs_emit (buf,"%hhu\n" , params->turbo); +} + +static ssize_t btn_turbo_period_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ + struct ally_btn_sysfs_entry *entry = container_of(attr, struct ally_btn_sysfs_entry, + attr_turbo_period); +struct ally_btn_turbo_params *params; +u8 old_value, value; +int ret; + +if (!entry->cfg->turbo_support) +return -EOPNOTSUPP ; + + params =ally_btn_get_turbo_params (entry->cfg, entry->btn); +if (!params) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret) +return ret; + +if (value <ALLY_TURBO_PERIOD_MIN || value >ALLY_TURBO_PERIOD_MAX ) +return -EINVAL ; + +guard (mutex)(&entry->cfg->config_mutex); + + old_value = params->turbo; + params->turbo = value; + ret =ally_set_turbo_params (entry->ally, entry->hdev, entry->cfg); +if (ret) {
drivers/hid/hid-asus.c static const struct attribute_group *const ally_cal_attr_groups[] = { · line 2713
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+/* +* The hardware rejected the update: restore the old +* value or a later write to another button would +* flush this stale one to the device. +*/ + params->turbo = old_value; +return ret; + } + +sysfs_notify (&entry->hdev->dev.kobj, entry->group.name,"turbo_period" );
??
Quoted 150 lines
drivers/hid/hid-asus.c +150
-0+ +return count; +} + +static ssize_t btn_toggle_period_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ + struct ally_btn_sysfs_entry *entry = container_of(attr, struct ally_btn_sysfs_entry, + attr_toggle_period); +struct ally_btn_turbo_params *params; + +if (!entry->cfg->turbo_support) +return -EOPNOTSUPP ; + + params =ally_btn_get_turbo_params (entry->cfg, entry->btn); +if (!params) +return -ENODEV ; + +guard (mutex)(&entry->cfg->config_mutex); + +return sysfs_emit (buf,"%hhu\n" , params->toggle); +} + +static ssize_t btn_toggle_period_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ + struct ally_btn_sysfs_entry *entry = container_of(attr, struct ally_btn_sysfs_entry, + attr_toggle_period); +struct ally_btn_turbo_params *params; +u8 old_value, value; +int ret; + +if (!entry->cfg->turbo_support) +return -EOPNOTSUPP ; + + params =ally_btn_get_turbo_params (entry->cfg, entry->btn); +if (!params) +return -ENODEV ; + + ret =kstrtou8 (buf,10 , &value); +if (ret) +return ret; + +if (value <ALLY_TOGGLE_PERIOD_MIN || value >ALLY_TOGGLE_PERIOD_MAX ) +return -EINVAL ; + +guard (mutex)(&entry->cfg->config_mutex); + + old_value = params->toggle; + params->toggle = value; + ret =ally_set_turbo_params (entry->ally, entry->hdev, entry->cfg); +if (ret) { +/* +* The hardware rejected the update: restore the old +* value or a later write to another button would +* flush this stale one to the device. +*/ + params->toggle = old_value; +return ret; + } + +sysfs_notify (&entry->hdev->dev.kobj, entry->group.name,"toggle_period" ); + +return count; +} + +static ssize_t btn_turbo_period_range_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"0 20\n" ); +} + +static ssize_t btn_toggle_period_range_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"0 255\n" ); +} + +static struct device_attribute dev_attr_btn_turbo_period_range = +__ATTR (turbo_period_range,0444 , btn_turbo_period_range_show,NULL ); + +static struct device_attribute dev_attr_btn_toggle_period_range = +__ATTR (toggle_period_range,0444 , btn_toggle_period_range_show,NULL ); + +static void ally_btn_turbo_init_attrs (struct ally_btn_sysfs_entry *entry) +{ +sysfs_attr_init (&entry->attr_turbo_period.attr); + entry->attr_turbo_period.attr.name ="turbo_period" ; + entry->attr_turbo_period.attr.mode =0644 ; + entry->attr_turbo_period.show = btn_turbo_period_show; + entry->attr_turbo_period.store = btn_turbo_period_store; + +sysfs_attr_init (&entry->attr_toggle_period.attr); + entry->attr_toggle_period.attr.name ="toggle_period" ; + entry->attr_toggle_period.attr.mode =0644 ; + entry->attr_toggle_period.show = btn_toggle_period_show; + entry->attr_toggle_period.store = btn_toggle_period_store; + entry->attrs[0 ] = &entry->attr_turbo_period.attr; + entry->attrs[1 ] = &entry->attr_toggle_period.attr; + entry->attrs[2 ] = &dev_attr_btn_turbo_period_range.attr; + entry->attrs[3 ] = &dev_attr_btn_toggle_period_range.attr; + entry->attrs[4 ] =NULL ; +} + +/** +* ally_create_button_attributes() - Create turbo button attributes +* @hdev: HID device +* @ally: ally handheld structure +* @cfg: ally config structure +* +* Return: 0 on success, negative errno on failure +*/ +static int ally_create_button_attributes (struct hid_device *hdev, +struct ally_handheld *ally, +struct ally_config *cfg) +{ +struct ally_btn_sysfs_entry *entries; +int i, ret; + +if (!cfg->turbo_support) +return 0 ; + + entries =kcalloc (ALLY_BTN_MAX ,sizeof (*entries),GFP_KERNEL ); +if (!entries) +return -ENOMEM ; + + cfg->button_entries = entries; + +for (i =0 ; i <ALLY_BTN_MAX ; i++) { + entries[i].ally = ally; + entries[i].cfg = cfg; + entries[i].hdev = hdev; + entries[i].btn = i; + +if (!ally_btn_get_turbo_params (cfg, i)) { +hid_err (hdev,"Invalid button id %d for turbo attributes\n" , i); + ret = -EINVAL ; + goto err_cleanup; + } + +ally_btn_turbo_init_attrs (&entries[i]); + + entries[i].group.name = ally_button_names[i]; + entries[i].group.attrs = entries[i].attrs; + + ret =sysfs_create_group (&hdev->dev.kobj, &entries[i].group); +if (ret <0 ) { +hid_err (hdev,"Failed to create sysfs group for %s: %d\n" , + ally_button_names[i], ret); + goto err_cleanup;
drivers/hid/hid-asus.c static const struct attribute_group *const ally_cal_attr_groups[] = { · line 2873
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ } + } + +return 0 ; + +err_cleanup: +while (--i >=0 ) +sysfs_remove_group (&hdev->dev.kobj, &entries[i].group); + +kfree (entries);
While using local variable within the loop is fine, this cleanup code would be more obvious if you'd write this as kfree(cfg->button_entries) and remove the empty line in between them as well.
Quoted 16 lines
drivers/hid/hid-asus.c +16
-0+ cfg->button_entries =NULL ; + +return ret; +} + +/** +* ally_remove_button_attributes() - Remove turbo button attributes +* @hdev: HID device +* @cfg: ally config structure +*/ +static void ally_remove_button_attributes (struct hid_device *hdev,struct ally_config *cfg) +{ +struct ally_btn_sysfs_entry *entries; +int i; + +if (!cfg || !cfg->button_entries)
drivers/hid/hid-asus.c static const struct attribute_group *const ally_cal_attr_groups[] = { · line 2900
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+return ; + + entries = cfg->button_entries; + +for (i =0 ; i <ALLY_BTN_MAX ; i++) +sysfs_remove_group (&hdev->dev.kobj, &entries[i].group); + +kfree (entries); + + cfg->button_entries =NULL ;
Same here.
Quoted 70 lines
drivers/hid/hid-asus.c +32
-0+} +/** * ally_config_create() - Initialize configuration and create sysfs entries * @hdev: HID device @@ -2478,6 +2911,7 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al {struct ally_config *cfg;unsigned long flags; +int cal_created =0 ;int ret, sysfs_i;/* @@ -2571,12 +3005,30 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al goto ally_config_create_sysfs_err; } } + +/* All the groups exist now: the error path must remove them. */ + cal_created =ARRAY_SIZE (ally_cal_attr_groups); + } + +if (cfg->turbo_support) { + ret =ally_create_button_attributes (hdev, ally, cfg); +if (ret <0 ) { +hid_err (hdev,"Failed to create button attributes: %d\n" , ret); + goto ally_config_create_sysfs_err; + } } cfg->initialized = true;return cfg; ally_config_create_sysfs_err: +if (cfg->button_entries) +ally_remove_button_attributes (hdev, cfg); + +while (cal_created-- >0 ) +sysfs_remove_group (&hdev->dev.kobj, + ally_cal_attr_groups[cal_created]); +spin_lock_irqsave (&ally_data_lock, flags); ally->config =NULL ;spin_unlock_irqrestore (&ally_data_lock, flags);@@ -2601,6 +3053,9 @@ static void ally_config_remove(struct hid_device *hdev, struct ally_config *cfg)if (!cfg || !cfg->initialized)return ; +if (cfg->button_entries) +ally_remove_button_attributes (hdev, cfg); +for (i =0 ; i <ARRAY_SIZE (ally_attr_groups); i++)sysfs_remove_group (&hdev->dev.kobj, &ally_attr_groups[i]);@@ -3147,6 +3602,14 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +if (cfg->turbo_support) { + ret =ally_set_turbo_params (ally, hdev, cfg); +if (ret <0 ) +hid_warn (hdev, +"Failed to restore turbo parameters: %d\n" , + ret); + } +return 0 ; }
Signature · 1 line
i.
Denis Benato
maintainerPATCH v11 11/18 HID: asus: add support for btn remapping
In reply to Denis BenatoAdd support for buttons remapping: the ability to assign different button events to each one of the physical button on the device.
Each button exposes a remap_index sysfs attribute listing the valid remap target names for the current gamepad mode: M1 and M2 can be remapped to anything at any time, while other buttons may only be remapped to gamepad codes in gamepad mode and keyboard/mouse/media codes in desktop mode.
When the gamepad mode is changed the firmware automatically restores its internal defaults, so the software cache is refreshed to reflect the hardware state.
--- drivers/hid/hid-asus.c | 1059 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 1037 insertions(+), 22 deletions(-)
drivers/hid/hid-asus.c +1037 -22
@@ -275,9 +275,25 @@ struct ally_turbo_config {struct ally_btn_turbo_params btn_m1; }; +/* Button remap attribute structure */ +struct button_remap_attr { +struct device_attribute dev_attr; +enum ally_button_id button_id; +bool is_macro; +/* +* The attributes are created while hid_asus_ally_probe() is still +* running, before asus_probe() publishes drvdata->rog_ally: carry +* the pointer like the turbo entries do, so the callbacks work +* from the moment the files exist. +*/ +struct ally_handheld *ally; +}; + +#define to_button_remap_attr (x) container_of(x, struct button_remap_attr, dev_attr) +struct ally_btn_sysfs_entry {struct attribute_group group; -struct attribute *attrs[5 ];/* turbo_period + toggle_period + ranges + NULL */ +struct attribute *attrs[8 ];/* turbo + ranges + remap + macro + remap_index + NULL */ /* * The entries are created while hid_asus_ally_probe() is still running, * before asus_probe() publishes drvdata->rog_ally: carrying the shared
@@ -289,6 +305,9 @@ struct ally_btn_sysfs_entry {enum ally_button_id btn;struct device_attribute attr_turbo_period;struct device_attribute attr_toggle_period; +struct button_remap_attr *remap_attr; +struct button_remap_attr *macro_attr; +struct device_attribute attr_remap_index; };enum ally_gamepad_mode_index {
@@ -296,6 +315,35 @@ enum ally_gamepad_mode_index { ALLY_GAMEPAD_MODE_KEYBOARD = 0x02, }; +/* Button pair indexes for mapping commands */ +enum btn_pair_index { +BTN_PAIR_DPAD_UPDOWN =0x01 , +BTN_PAIR_DPAD_LEFTRIGHT =0x02 , +BTN_PAIR_STICK_LR =0x03 , +BTN_PAIR_BUMPER_LR =0x04 , +BTN_PAIR_AB =0x05 , +BTN_PAIR_XY =0x06 , +BTN_PAIR_VIEW_MENU =0x07 , +BTN_PAIR_M1M2 =0x08 , +BTN_PAIR_TRIGGER_LR =0x09 , +}; + +struct button_map { +const struct btn_code_map *remap; +const struct btn_code_map *macro; +}; + +struct button_pair_map { +enum btn_pair_index pair_index; +struct button_map first; +struct button_map second; +}; + +/* Store button mapping per gamepad mode */ +struct ally_button_mapping { +struct button_pair_map button_pairs[9 ];/* 9 button pairs */ +}; +struct ally_config {/* Must be locked if the data is being changed */ struct mutex config_mutex;
@@ -331,6 +379,8 @@ struct ally_config {struct ally_turbo_config turbo;struct ally_btn_sysfs_entry *button_entries; +/* Button mappings of the current gamepad mode */ +struct ally_button_mapping button_mappings;struct ally_joystick_resp_curve left_curve;struct ally_joystick_resp_curve right_curve;
@@ -1072,6 +1122,295 @@ static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device *return 0 ; } +enum btn_map_type { +BTN_TYPE_NONE =0 , +BTN_TYPE_PAD =0x01 , +BTN_TYPE_KB =0x02 , +BTN_TYPE_MOUSE =0x03 , +BTN_TYPE_MEDIA =0x05 , +}; + +struct btn_code_map { +unsigned char type; +unsigned char value; +const char *name; +}; + +static const struct btn_code_map ally_btn_codes[] = { + {BTN_TYPE_NONE ,0x00 ,"NONE" }, +/* Gamepad button codes */ + {BTN_TYPE_PAD ,0x01 ,"BTN_SOUTH" }, + {BTN_TYPE_PAD ,0x02 ,"BTN_EAST" }, + {BTN_TYPE_PAD ,0x03 ,"BTN_NORTH" }, + {BTN_TYPE_PAD ,0x04 ,"BTN_WEST" }, + {BTN_TYPE_PAD ,0x05 ,"BTN_TL" }, + {BTN_TYPE_PAD ,0x06 ,"BTN_TR" }, + {BTN_TYPE_PAD ,0x07 ,"BTN_THUMBL" }, + {BTN_TYPE_PAD ,0x08 ,"BTN_THUMBR" }, + {BTN_TYPE_PAD ,0x09 ,"ABS_HAT0Y_NEG" }, + {BTN_TYPE_PAD ,0x0A ,"ABS_HAT0Y_POS" }, + {BTN_TYPE_PAD ,0x0B ,"ABS_HAT0X_NEG" }, + {BTN_TYPE_PAD ,0x0C ,"ABS_HAT0X_POS" }, + {BTN_TYPE_PAD ,0x0D ,"ABS_Z" }, + {BTN_TYPE_PAD ,0x0E ,"ABS_RZ" }, + {BTN_TYPE_PAD ,0x11 ,"BTN_SELECT" }, + {BTN_TYPE_PAD ,0x12 ,"BTN_START" }, + {BTN_TYPE_PAD ,0x13 ,"BTN_MODE" }, + +/* Keyboard button codes */ + {BTN_TYPE_KB ,0x8E ,"FN_M2" }, + {BTN_TYPE_KB ,0x8F ,"FN_M1" }, + {BTN_TYPE_KB ,0x76 ,"KEY_ESC" }, + {BTN_TYPE_KB ,0x05 ,"KEY_F1" }, + {BTN_TYPE_KB ,0x06 ,"KEY_F2" }, + {BTN_TYPE_KB ,0x04 ,"KEY_F3" }, + {BTN_TYPE_KB ,0x0C ,"KEY_F4" }, + {BTN_TYPE_KB ,0x03 ,"KEY_F5" }, + {BTN_TYPE_KB ,0x0B ,"KEY_F6" }, + {BTN_TYPE_KB ,0x80 ,"KEY_F7" }, + {BTN_TYPE_KB ,0x0A ,"KEY_F8" }, + {BTN_TYPE_KB ,0x01 ,"KEY_F9" }, + {BTN_TYPE_KB ,0x09 ,"KEY_F10" }, + {BTN_TYPE_KB ,0x78 ,"KEY_F11" }, + {BTN_TYPE_KB ,0x07 ,"KEY_F12" }, + {BTN_TYPE_KB ,0x08 ,"KEY_F13" }, + {BTN_TYPE_KB ,0x10 ,"KEY_F14" }, + {BTN_TYPE_KB ,0x18 ,"KEY_F15" }, + {BTN_TYPE_KB ,0x0E ,"KEY_GRAVE" },// backtick + {BTN_TYPE_KB ,0x16 ,"KEY_1" }, + {BTN_TYPE_KB ,0x1E ,"KEY_2" }, + {BTN_TYPE_KB ,0x26 ,"KEY_3" }, + {BTN_TYPE_KB ,0x25 ,"KEY_4" }, + {BTN_TYPE_KB ,0x2E ,"KEY_5" }, + {BTN_TYPE_KB ,0x36 ,"KEY_6" }, + {BTN_TYPE_KB ,0x3D ,"KEY_7" }, + {BTN_TYPE_KB ,0x3E ,"KEY_8" }, + {BTN_TYPE_KB ,0x46 ,"KEY_9" }, + {BTN_TYPE_KB ,0x45 ,"KEY_0" }, + {BTN_TYPE_KB ,0x4E ,"KEY_MINUS" },// hyphen + {BTN_TYPE_KB ,0x55 ,"KEY_EQUAL" }, + {BTN_TYPE_KB ,0x66 ,"KEY_BACKSPACE" }, + {BTN_TYPE_KB ,0x0D ,"KEY_TAB" }, + {BTN_TYPE_KB ,0x15 ,"KEY_Q" }, + {BTN_TYPE_KB ,0x1D ,"KEY_W" }, + {BTN_TYPE_KB ,0x24 ,"KEY_E" }, + {BTN_TYPE_KB ,0x2D ,"KEY_R" }, + {BTN_TYPE_KB ,0x2C ,"KEY_T" }, + {BTN_TYPE_KB ,0x35 ,"KEY_Y" }, + {BTN_TYPE_KB ,0x3C ,"KEY_U" }, + {BTN_TYPE_KB ,0x43 ,"KEY_I" }, + {BTN_TYPE_KB ,0x44 ,"KEY_O" }, + {BTN_TYPE_KB ,0x4D ,"KEY_P" }, + {BTN_TYPE_KB ,0x54 ,"KEY_LEFTBRACE" }, + {BTN_TYPE_KB ,0x5B ,"KEY_RIGHTBRACE" }, + {BTN_TYPE_KB ,0x5D ,"KEY_BACKSLASH" }, + {BTN_TYPE_KB ,0x58 ,"KEY_CAPSLOCK" }, + {BTN_TYPE_KB ,0x1C ,"KEY_A" }, + {BTN_TYPE_KB ,0x1B ,"KEY_S" }, + {BTN_TYPE_KB ,0x23 ,"KEY_D" }, + {BTN_TYPE_KB ,0x2B ,"KEY_F" }, + {BTN_TYPE_KB ,0x34 ,"KEY_G" }, + {BTN_TYPE_KB ,0x33 ,"KEY_H" }, + {BTN_TYPE_KB ,0x3B ,"KEY_J" }, + {BTN_TYPE_KB ,0x42 ,"KEY_K" }, + {BTN_TYPE_KB ,0x4B ,"KEY_L" }, + {BTN_TYPE_KB ,0x4C ,"KEY_SEMICOLON" }, + {BTN_TYPE_KB ,0x52 ,"KEY_APOSTROPHE" }, + {BTN_TYPE_KB ,0x5A ,"KEY_ENTER" }, + {BTN_TYPE_KB ,0x88 ,"KEY_LEFTSHIFT" }, + {BTN_TYPE_KB ,0x1A ,"KEY_Z" }, + {BTN_TYPE_KB ,0x22 ,"KEY_X" }, + {BTN_TYPE_KB ,0x21 ,"KEY_C" }, + {BTN_TYPE_KB ,0x2A ,"KEY_V" }, + {BTN_TYPE_KB ,0x32 ,"KEY_B" }, + {BTN_TYPE_KB ,0x31 ,"KEY_N" }, + {BTN_TYPE_KB ,0x3A ,"KEY_M" }, + {BTN_TYPE_KB ,0x41 ,"KEY_COMMA" }, + {BTN_TYPE_KB ,0x49 ,"KEY_DOT" }, + {BTN_TYPE_KB ,0x4A ,"KEY_SLASH" }, + {BTN_TYPE_KB ,0x89 ,"KEY_RIGHTSHIFT" }, + {BTN_TYPE_KB ,0x82 ,"KEY_LEFTMETA" }, + {BTN_TYPE_KB ,0x8A ,"KEY_LEFTALT" }, + {BTN_TYPE_KB ,0x29 ,"KEY_SPACE" }, + {BTN_TYPE_KB ,0x8B ,"KEY_RIGHTALT" }, + {BTN_TYPE_KB ,0x84 ,"KEY_COMPOSE" }, + {BTN_TYPE_KB ,0x8D ,"KEY_RIGHTCTRL" }, + {BTN_TYPE_KB ,0xC3 ,"KEY_SYSRQ" }, + {BTN_TYPE_KB ,0x7E ,"KEY_SCROLLLOCK" }, + {BTN_TYPE_KB ,0x91 ,"KEY_PAUSE" }, + {BTN_TYPE_KB ,0xC2 ,"KEY_INSERT" }, + {BTN_TYPE_KB ,0x94 ,"KEY_HOME" }, + {BTN_TYPE_KB ,0x96 ,"KEY_PAGEUP" }, + {BTN_TYPE_KB ,0xC0 ,"KEY_DELETE" }, + {BTN_TYPE_KB ,0x95 ,"KEY_END" }, + {BTN_TYPE_KB ,0x97 ,"KEY_PAGEDOWN" }, + {BTN_TYPE_KB ,0x98 ,"KEY_UP" }, + {BTN_TYPE_KB ,0x99 ,"KEY_DOWN" }, + {BTN_TYPE_KB ,0x9A ,"KEY_LEFT" }, + {BTN_TYPE_KB ,0x9B ,"KEY_RIGHT" }, + +/* Numpad button codes */ + {BTN_TYPE_KB ,0x77 ,"KEY_NUMLOCK" }, + {BTN_TYPE_KB ,0x90 ,"KEY_KPSLASH" }, + {BTN_TYPE_KB ,0x7C ,"KEY_KPASTERISK" }, + {BTN_TYPE_KB ,0x7B ,"KEY_KPMINUS" }, + {BTN_TYPE_KB ,0x70 ,"KEY_KP0" }, + {BTN_TYPE_KB ,0x69 ,"KEY_KP1" }, + {BTN_TYPE_KB ,0x72 ,"KEY_KP2" }, + {BTN_TYPE_KB ,0x7A ,"KEY_KP3" }, + {BTN_TYPE_KB ,0x6B ,"KEY_KP4" }, + {BTN_TYPE_KB ,0x73 ,"KEY_KP5" }, + {BTN_TYPE_KB ,0x74 ,"KEY_KP6" }, + {BTN_TYPE_KB ,0x6C ,"KEY_KP7" }, + {BTN_TYPE_KB ,0x75 ,"KEY_KP8" }, + {BTN_TYPE_KB ,0x7D ,"KEY_KP9" }, + {BTN_TYPE_KB ,0x79 ,"KEY_KPPLUS" }, + {BTN_TYPE_KB ,0x81 ,"KEY_KPENTER" }, + {BTN_TYPE_KB ,0x71 ,"KEY_KPDOT" }, + +/* Mouse button codes */ + {BTN_TYPE_MOUSE ,0x01 ,"BTN_LEFT" }, + {BTN_TYPE_MOUSE ,0x02 ,"BTN_RIGHT" }, + {BTN_TYPE_MOUSE ,0x03 ,"BTN_MIDDLE" }, + {BTN_TYPE_MOUSE ,0x04 ,"REL_WHEEL_HI_RES_UP" }, + {BTN_TYPE_MOUSE ,0x05 ,"REL_WHEEL_HI_RES_DOWN" }, + +/* Media button codes */ + {BTN_TYPE_MEDIA ,0x16 ,"MEDIA_SCREENSHOT" }, + {BTN_TYPE_MEDIA ,0x19 ,"MEDIA_SHOW_KEYBOARD" }, + {BTN_TYPE_MEDIA ,0x1C ,"MEDIA_SHOW_DESKTOP" }, + {BTN_TYPE_MEDIA ,0x1E ,"MEDIA_START_RECORDING" }, + {BTN_TYPE_MEDIA ,0x01 ,"MEDIA_MIC_OFF" }, + {BTN_TYPE_MEDIA ,0x02 ,"MEDIA_VOL_DOWN" }, + {BTN_TYPE_MEDIA ,0x03 ,"MEDIA_VOL_UP" }, +}; + +static const size_t keymap_len =ARRAY_SIZE (ally_btn_codes); + +static const struct btn_code_map *find_button_by_name (const char *name); + +static void ally_set_default_gamepad_mapping (struct ally_button_mapping *map) +{ +int i; + +/* Set all pair indexes and initialize to NONE */ +for (i =0 ; i <9 ; i++) { + map->button_pairs[i].pair_index = i +1 ; + map->button_pairs[i].first.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].first.macro = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.macro = +find_button_by_name ("NONE" ); + } + + map->button_pairs[BTN_PAIR_AB -1 ].first.remap = +find_button_by_name ("BTN_SOUTH" ); + map->button_pairs[BTN_PAIR_AB -1 ].second.remap = +find_button_by_name ("BTN_EAST" ); + + map->button_pairs[BTN_PAIR_XY -1 ].first.remap = +find_button_by_name ("BTN_NORTH" ); + map->button_pairs[BTN_PAIR_XY -1 ].second.remap = +find_button_by_name ("BTN_WEST" ); + + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].first.remap = +find_button_by_name ("BTN_TL" ); + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].second.remap = +find_button_by_name ("BTN_TR" ); + + map->button_pairs[BTN_PAIR_STICK_LR -1 ].first.remap = +find_button_by_name ("BTN_THUMBL" ); + map->button_pairs[BTN_PAIR_STICK_LR -1 ].second.remap = +find_button_by_name ("BTN_THUMBR" ); + + map->button_pairs[BTN_PAIR_DPAD_UPDOWN -1 ].first.remap = +find_button_by_name ("ABS_HAT0Y_NEG" ); + map->button_pairs[BTN_PAIR_DPAD_UPDOWN -1 ].second.remap = +find_button_by_name ("ABS_HAT0Y_POS" ); + + map->button_pairs[BTN_PAIR_DPAD_LEFTRIGHT -1 ].first.remap = +find_button_by_name ("ABS_HAT0X_NEG" ); + map->button_pairs[BTN_PAIR_DPAD_LEFTRIGHT -1 ].second.remap = +find_button_by_name ("ABS_HAT0X_POS" ); + + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].first.remap = +find_button_by_name ("ABS_Z" ); + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].second.remap = +find_button_by_name ("ABS_RZ" ); + + map->button_pairs[BTN_PAIR_VIEW_MENU -1 ].first.remap = +find_button_by_name ("BTN_SELECT" ); + map->button_pairs[BTN_PAIR_VIEW_MENU -1 ].second.remap = +find_button_by_name ("BTN_START" ); + + map->button_pairs[BTN_PAIR_M1M2 -1 ].first.remap = +find_button_by_name ("FN_M2" ); + map->button_pairs[BTN_PAIR_M1M2 -1 ].second.remap = +find_button_by_name ("FN_M1" ); +} + +static void ally_set_default_keyboard_mapping (struct ally_button_mapping *map) +{ +int i; + +/* Set all pair indexes and initialize to NONE */ +for (i =0 ; i <9 ; i++) { + map->button_pairs[i].pair_index = i +1 ; + map->button_pairs[i].first.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].first.macro = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.macro = +find_button_by_name ("NONE" ); + } + +/* +* Desktop mode defaults: keyboard and mouse mappings. +* Buttons not listed here (dpad, view, menu) remain NONE. +*/ + +/* btn_a => KEY_SPACE, btn_b => KEY_E */ + map->button_pairs[BTN_PAIR_AB -1 ].first.remap = +find_button_by_name ("KEY_SPACE" ); + map->button_pairs[BTN_PAIR_AB -1 ].second.remap = +find_button_by_name ("KEY_E" ); + +/* btn_x => KEY_R, btn_y => KEY_F */ + map->button_pairs[BTN_PAIR_XY -1 ].first.remap = +find_button_by_name ("KEY_R" ); + map->button_pairs[BTN_PAIR_XY -1 ].second.remap = +find_button_by_name ("KEY_F" ); + +/* LB => wheel up, RB => wheel down */ + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].first.remap = +find_button_by_name ("REL_WHEEL_HI_RES_UP" ); + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].second.remap = +find_button_by_name ("REL_WHEEL_HI_RES_DOWN" ); + +/* left stick click => left click, right stick click => left shift */ + map->button_pairs[BTN_PAIR_STICK_LR -1 ].first.remap = +find_button_by_name ("BTN_LEFT" ); + map->button_pairs[BTN_PAIR_STICK_LR -1 ].second.remap = +find_button_by_name ("KEY_LEFTSHIFT" ); + +/* LT => left click, RT => right click */ + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].first.remap = +find_button_by_name ("BTN_LEFT" ); + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].second.remap = +find_button_by_name ("BTN_RIGHT" ); + +/* M2 => FN_M2, M1 => FN_M1 */ + map->button_pairs[BTN_PAIR_M1M2 -1 ].first.remap = +find_button_by_name ("FN_M2" ); + map->button_pairs[BTN_PAIR_M1M2 -1 ].second.remap = +find_button_by_name ("FN_M1" ); +} +static ssize_t gamepad_mode_show (struct device *dev,struct device_attribute *attr,char *buf) {struct hid_device *hdev =to_hid_device (dev);
@@ -1113,7 +1452,7 @@ static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *astruct ally_config *cfg;u8 mode_byte;int mode; -int ret; +int ret, i;if (!ally)return -ENODEV ;
@@ -1141,6 +1480,33 @@ static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *a cfg->gamepad_mode = mode_byte; +/* +* When the gamepad mode changes, the hardware automatically +* resets all button mappings to its internal defaults for the +* new mode. +* +* Refresh the software cache so that subsequent +* reads from sysfs reflect what the firmware is actually +* using. +*/ +if (mode_byte ==ALLY_GAMEPAD_MODE_GAMEPAD ) +ally_set_default_gamepad_mapping (&cfg->button_mappings); +else if (mode_byte ==ALLY_GAMEPAD_MODE_KEYBOARD ) +ally_set_default_keyboard_mapping (&cfg->button_mappings); + +/* +* The re-defaulted mappings are what the remap and macro files +* report: wake anyone polling them about the new values. +*/ +if (cfg->button_entries) { +for (i =0 ; i <ALLY_BTN_MAX ; i++) { +sysfs_notify (&dev->kobj, cfg->button_entries[i].group.name, +"remap" ); +sysfs_notify (&dev->kobj, cfg->button_entries[i].group.name, +"macro" ); + } + } +hid_dbg (hdev,"Set gamepad mode to %s\n" , ally_gamepad_mode_text[mode]);sysfs_notify (&dev->kobj,NULL ,"gamepad_mode" );
@@ -2815,6 +3181,575 @@ static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) entry->attrs[4 ] =NULL ; } +/** +* find_button_by_name() - Find a button code map by its name +* @name: name of the button code to look up +* +* Return: the matching button code map, or NULL if not found +*/ +static const struct btn_code_map *find_button_by_name (const char *name) +{ +int i; + +for (i =0 ; i < keymap_len; i++) { +if (strcmp (ally_btn_codes[i].name, name) ==0 ) +return &ally_btn_codes[i]; + } + +return NULL ; +} + +/** +* ally_remap_code_valid() - Check if a remap target is valid for a button +* @btn: button to remap +* @gamepad_mode: gamepad mode to validate the remap against +* @code: remap target to check +* @is_macro: whether the target is assigned to the macro mapping +* +* M1 and M2 can be remapped to anything at any time. +* All other buttons may only be remapped to gamepad buttons (BTN_TYPE_PAD) +* when in gamepad mode, and to keyboard/mouse/media codes when in desktop +* (keyboard) mode. BTN_TYPE_NONE (unmapped) is always allowed. +* Macro mappings are executed by the MCU as input sequences on the virtual +* keyboard and are accepted regardless of the current gamepad mode. +* +* Return: true if the remap is valid, false otherwise +*/ +static bool ally_remap_code_valid (enum ally_button_id btn,u8 gamepad_mode, +const struct btn_code_map *code,bool is_macro) +{ +if (!code) +return false; + +/* M1 and M2 can be remapped to everything */ +if (btn ==ALLY_BTN_M1 || btn ==ALLY_BTN_M2 ) +return true; + +/* NONE is always valid */ +if (code->type ==BTN_TYPE_NONE ) +return true; + +/* Macros are not restricted by the controller emulation mode */ +if (is_macro) +return true; + +if (gamepad_mode ==ALLY_GAMEPAD_MODE_GAMEPAD ) +return code->type ==BTN_TYPE_PAD ; + +/* Desktop / keyboard mode: allow keyboard, mouse and media */ +return code->type ==BTN_TYPE_KB || + code->type ==BTN_TYPE_MOUSE || + code->type ==BTN_TYPE_MEDIA ; +} + +/** +* ally_set_button_mapping() - Set the button mapping for a button pair +* @hdev: HID device +* @ally: ally handheld structure +* @mapping: button pair mapping to send to the device +* +* Return: count of data transferred, negative if error +*/ +static int ally_set_button_mapping (struct hid_device *hdev,struct ally_handheld *ally, +struct button_pair_map *mapping) +{ +/* The MCU mapping block is four consecutive 11-byte entries starting at +* buf[5]: first remap 5-15, first macro 16-26, second remap 27-37, +* second macro 38-48 (see hid-asus-ally __btn_pair_to_pkt, BTN_CODE_LEN). +*/ +u8 macro_bytes[11 ] = {0 }; +u8 btn_bytes[11 ] = {0 }; + +if (!mapping || !mapping->first.remap || !mapping->first.macro || + !mapping->second.remap || !mapping->second.macro) +return -EINVAL ; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_MAPPING, NULL, 0); +if (!buf) +return -ENOMEM ; + +/* This packet is slightly different from the other +* as before the packet length there is an extra byte +* which is the pair index. +*/ + buf[3 ] = mapping->pair_index; + buf[4 ] =0x2C ;/* Length */ + +/* First button mapping */ + buf[5 ] = mapping->first.remap->type; +/* Fill in bytes 6-14 with button code */ +if (mapping->first.remap->type) { +memset (btn_bytes,0 ,sizeof (btn_bytes)); + btn_bytes[0 ] = mapping->first.remap->type; + +/* Value byte position depends on type: pad=1, kb=2, media=3, +* mouse=4 (see hid-asus-ally BTN_CODE definitions). +*/ +switch (mapping->first.remap->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + btn_bytes[1 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_KB : + btn_bytes[2 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_MEDIA : + btn_bytes[3 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_MOUSE : + btn_bytes[4 ] = mapping->first.remap->value; +break ; + } +memcpy (&buf[5 ], btn_bytes,11 ); + } + +/* Macro mapping for first button if any */ + buf[16 ] = mapping->first.macro->type; +if (mapping->first.macro->type) { +memset (macro_bytes,0 ,sizeof (macro_bytes)); + macro_bytes[0 ] = mapping->first.macro->type; + +switch (mapping->first.macro->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + macro_bytes[1 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_KB : + macro_bytes[2 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_MEDIA : + macro_bytes[3 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_MOUSE : + macro_bytes[4 ] = mapping->first.macro->value; +break ; + } +memcpy (&buf[16 ], macro_bytes,11 ); + } + +/* Second button mapping */ + buf[27 ] = mapping->second.remap->type; +/* Fill in bytes 28-36 with button code */ +if (mapping->second.remap->type) { +memset (btn_bytes,0 ,sizeof (btn_bytes)); + btn_bytes[0 ] = mapping->second.remap->type; + +switch (mapping->second.remap->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + btn_bytes[1 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_KB : + btn_bytes[2 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_MEDIA : + btn_bytes[3 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_MOUSE : + btn_bytes[4 ] = mapping->second.remap->value; +break ; + } +memcpy (&buf[27 ], btn_bytes,11 ); + } + +/* Macro mapping for second button if any */ + buf[38 ] = mapping->second.macro->type; +if (mapping->second.macro->type) { +memset (macro_bytes,0 ,sizeof (macro_bytes)); + macro_bytes[0 ] = mapping->second.macro->type; + +switch (mapping->second.macro->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + macro_bytes[1 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_KB : + macro_bytes[2 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_MEDIA : + macro_bytes[3 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_MOUSE : + macro_bytes[4 ] = mapping->second.macro->value; +break ; + } +memcpy (&buf[38 ], macro_bytes,11 ); + } + +return ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +} + +/** +* ally_apply_button_mappings() - Push the recorded mappings of the current +* gamepad mode to the controller +* @hdev: HID device +* @ally: ally handheld structure +* @cfg: ally config structure +* +* An MCU reset restores the firmware default mappings: push the recorded +* ones again so that the hardware keeps matching what userspace +* configured. The caller must hold cfg->config_mutex, exactly like the +* sysfs store callbacks that update single pairs. +*/ +static void ally_apply_button_mappings (struct hid_device *hdev, +struct ally_handheld *ally, +struct ally_config *cfg) +{ +struct ally_button_mapping *mode_map; +int i, ret; + + mode_map = &cfg->button_mappings; + +for (i =0 ; i <ARRAY_SIZE (mode_map->button_pairs); i++) { + mode_map->button_pairs[i].pair_index = i +1 ; + + ret =ally_set_button_mapping (hdev, ally, + &mode_map->button_pairs[i]); +if (ret <0 ) { +hid_warn (hdev, +"Failed to restore button mappings: %d\n" , + ret); +return ; + } + } +} + +/** +* get_button_pair_info() - Get the pair index and position of a button +* @button_id: button to look up +* @pair_idx: output pointer for the pair index of the button +* @is_first: output pointer for the position of the button within the pair +* +* Return: 0 on success, negative errno on failure +*/ +static int get_button_pair_info (enum ally_button_id button_id, +enum btn_pair_index *pair_idx, +bool *is_first) +{ +switch (button_id) { +case ALLY_BTN_DU : + *pair_idx =BTN_PAIR_DPAD_UPDOWN ; + *is_first = true; +break ; +case ALLY_BTN_DD : + *pair_idx =BTN_PAIR_DPAD_UPDOWN ; + *is_first = false; +break ; +case ALLY_BTN_DL : + *pair_idx =BTN_PAIR_DPAD_LEFTRIGHT ; + *is_first = true; +break ; +case ALLY_BTN_DR : + *pair_idx =BTN_PAIR_DPAD_LEFTRIGHT ; + *is_first = false; +break ; +case ALLY_BTN_J0B : + *pair_idx =BTN_PAIR_STICK_LR ; + *is_first = true; +break ; +case ALLY_BTN_J1B : + *pair_idx =BTN_PAIR_STICK_LR ; + *is_first = false; +break ; +case ALLY_BTN_LB : + *pair_idx =BTN_PAIR_BUMPER_LR ; + *is_first = true; +break ; +case ALLY_BTN_RB : + *pair_idx =BTN_PAIR_BUMPER_LR ; + *is_first = false; +break ; +case ALLY_BTN_A : + *pair_idx =BTN_PAIR_AB ; + *is_first = true; +break ; +case ALLY_BTN_B : + *pair_idx =BTN_PAIR_AB ; + *is_first = false; +break ; +case ALLY_BTN_X : + *pair_idx =BTN_PAIR_XY ; + *is_first = true; +break ; +case ALLY_BTN_Y : + *pair_idx =BTN_PAIR_XY ; + *is_first = false; +break ; +case ALLY_BTN_VIEW : + *pair_idx =BTN_PAIR_VIEW_MENU ; + *is_first = true; +break ; +case ALLY_BTN_MENU : + *pair_idx =BTN_PAIR_VIEW_MENU ; + *is_first = false; +break ; +case ALLY_BTN_M1 : + *pair_idx =BTN_PAIR_M1M2 ; + *is_first = false; +break ; +case ALLY_BTN_M2 : + *pair_idx =BTN_PAIR_M1M2 ; + *is_first = true; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static ssize_t button_remap_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct button_remap_attr *btn_attr =to_button_remap_attr (attr); +struct ally_handheld *ally = btn_attr->ally; +const struct btn_code_map *code; +struct ally_config *cfg; +enum ally_button_id button_id = btn_attr->button_id; +enum btn_pair_index pair_idx; +struct button_pair_map *pair; +struct button_map *btn_map; +bool is_first; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =get_button_pair_info (button_id, &pair_idx, &is_first); +if (ret <0 ) +return ret; + +guard (mutex)(&cfg->config_mutex); + + pair = &cfg->button_mappings.button_pairs[pair_idx -1 ]; + btn_map = is_first ? &pair->first : &pair->second; + code = btn_attr->is_macro ? btn_map->macro : btn_map->remap; + +/* +* The mappings are never NULL: the defaults, the mode-switch +* re-defaults and the store rollback all guarantee it. A miss +* means a broken default table, so fail loudly instead of +* dereferencing. +*/ +if (!code) +return -EIO ; + +if (code->type ==BTN_TYPE_NONE ) +return sysfs_emit (buf,"NONE\n" ); + +return sysfs_emit (buf,"%s\n" , code->name); +} + +static ssize_t button_remap_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct button_remap_attr *btn_attr =to_button_remap_attr (attr); +struct ally_handheld *ally = btn_attr->ally; +enum ally_button_id button_id = btn_attr->button_id; +struct ally_button_mapping *mode_map; +const struct btn_code_map *code; +const struct btn_code_map *old_code; +enum btn_pair_index pair_idx; +struct button_pair_map *pair; +struct button_map *btn_map; +struct ally_config *cfg; +char btn_name[32 ]; +bool is_first; +bool undo_hw; +int ret, i; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +if (sscanf (buf,"%31s" , btn_name) !=1 ) +return -EINVAL ; + +/* Handle "NONE" specially */ +if (strcmp (btn_name,"NONE" ) ==0 ) { + code = &ally_btn_codes[0 ];/* NONE entry */ + }else { + code =find_button_by_name (btn_name); +if (!code) +return -EINVAL ; + } + + ret =get_button_pair_info (button_id, &pair_idx, &is_first); +if (ret <0 ) +return ret; + +guard (mutex)(&cfg->config_mutex); + +/* +* Validate the remap target while holding the lock: the +* gamepad mode can change concurrently, and the check must +* be done against the very mode the mapping is written to. +*/ +if (!ally_remap_code_valid (button_id, cfg->gamepad_mode, code, + btn_attr->is_macro)) +return -EINVAL ; + +/* Access the mapping for current gamepad mode */ + pair = &cfg->button_mappings.button_pairs[pair_idx -1 ]; + btn_map = is_first ? &pair->first : &pair->second; + +if (btn_attr->is_macro) { + old_code = btn_map->macro; + btn_map->macro = code; + }else { + old_code = btn_map->remap; + btn_map->remap = code; + } + +/* Update pair index */ + pair->pair_index = pair_idx; + +/* +* Send mapping to device with the caveat that first +* generation devices require individual button pair updates. +*/ +if (cfg->is_ally_x) { + ret =ally_set_button_mapping (hdev, ally, pair); + undo_hw = false; + }else { + mode_map = &cfg->button_mappings; + ret =0 ; +for (i =0 ; i <9 ; i++) { + mode_map->button_pairs[i].pair_index = i +1 ; + ret =ally_set_button_mapping (hdev, ally, + &mode_map->button_pairs[i]); +if (ret <0 ) +break ; + } + +/* +* Pairs are pushed one at a time: remember whether +* the target pair reached the hardware before a +* later pair failed, so the rollback below can undo +* it on the device too. +*/ + undo_hw = ret <0 && i > pair_idx -1 ; + } + +/* +* The hardware rejected the update: restore the previous +* mapping so that the software cache keeps describing the +* state the device is actually in. +*/ +if (ret <0 ) { +if (btn_attr->is_macro) + btn_map->macro = old_code; +else + btn_map->remap = old_code; + +/* +* The target pair was already committed before a +* later pair failed: push the restored mapping once +* more so the hardware matches the cache again. Best +* effort only: the device is misbehaving already. +*/ +if (undo_hw) +ally_set_button_mapping (hdev, ally, pair); + +hid_err (hdev,"Failed to set button mapping: %d\n" , ret); +return ret; + } + +sysfs_notify (&dev->kobj, ally_button_names[btn_attr->button_id], + btn_attr->is_macro ?"macro" :"remap" ); + +return count; +} + +/** +* button_remap_attr_create() - Create a button remap attribute +* @ally: ally handheld structure +* @button_id: button the attribute is for +* @is_macro: whether the attribute controls the macro mapping +* +* Return: the newly created attribute, or NULL on allocation failure +*/ +static struct button_remap_attr *button_remap_attr_create (struct ally_handheld *ally, +enum ally_button_id button_id, +bool is_macro) +{ + struct button_remap_attr *attr __free(kfree) = kzalloc_obj(*attr); +if (!attr) +return NULL ; + + attr->ally = ally; + attr->button_id = button_id; + attr->is_macro = is_macro; +sysfs_attr_init (&attr->dev_attr.attr); + attr->dev_attr.attr.name = is_macro ?"macro" :"remap" ; + attr->dev_attr.attr.mode =0644 ; + attr->dev_attr.show = button_remap_show; + attr->dev_attr.store = button_remap_store; + +return no_free_ptr (attr); +} + +/** +* btn_remap_index_show() - Show the list of valid remap targets for a button +* @dev: device the attribute belongs to +* @attr: attribute being read +* @buf: buffer to write the list into +* +* M1/M2 list everything; other buttons list only the codes valid for the +* active mode (gamepad buttons in gamepad mode, keyboard / mouse / media +* in desktop mode). +* +* Return: number of characters written +*/ +static ssize_t btn_remap_index_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_btn_sysfs_entry *entry =container_of (attr, + struct ally_btn_sysfs_entry, attr_remap_index); +struct ally_config *cfg; +enum ally_button_id btn = entry->btn; +u8 mode; +ssize_t len =0 ; +int i; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +scoped_guard (mutex, &cfg->config_mutex) + mode = cfg->gamepad_mode; + +for (i =0 ; i < keymap_len; i++) { +if (!ally_remap_code_valid (btn, mode, &ally_btn_codes[i], false)) +continue ; + len +=sysfs_emit_at (buf, len,"%s " , ally_btn_codes[i].name); + } + +if (len >0 ) + buf[len -1 ] ='\n' ; + +return len; +} +/** * ally_create_button_attributes() - Create turbo button attributes * @hdev: HID device
@@ -2824,15 +3759,12 @@ static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry)* Return: 0 on success, negative errno on failure */ static int ally_create_button_attributes (struct hid_device *hdev, -struct ally_handheld *ally, -struct ally_config *cfg) +struct ally_handheld *ally, +struct ally_config *cfg) {struct ally_btn_sysfs_entry *entries;int i, ret; -if (!cfg->turbo_support) -return 0 ; - entries =kcalloc (ALLY_BTN_MAX ,sizeof (*entries),GFP_KERNEL );if (!entries)return -ENOMEM ;
@@ -2845,13 +3777,53 @@ static int ally_create_button_attributes(struct hid_device *hdev, entries[i].hdev = hdev; entries[i].btn = i; -if (!ally_btn_get_turbo_params (cfg, i)) { -hid_err (hdev,"Invalid button id %d for turbo attributes\n" , i); - ret = -EINVAL ; +if (cfg->turbo_support) { +if (!ally_btn_get_turbo_params (cfg, i)) { +hid_err (hdev,"Invalid button id %d for turbo attributes\n" , i); + ret = -EINVAL ; + goto err_cleanup; + } + +ally_btn_turbo_init_attrs (&entries[i]); + } + + entries[i].remap_attr =button_remap_attr_create (ally, i, false); +if (!entries[i].remap_attr) { + ret = -ENOMEM ; goto err_cleanup; } -ally_btn_turbo_init_attrs (&entries[i]); + entries[i].macro_attr =button_remap_attr_create (ally, i, true); +if (!entries[i].macro_attr) { + ret = -ENOMEM ; + goto err_cleanup; + } + +/* Initialize the remap_index attribute */ +sysfs_attr_init (&entries[i].attr_remap_index.attr); + entries[i].attr_remap_index.attr.name ="remap_index" ; + entries[i].attr_remap_index.attr.mode =0444 ; + entries[i].attr_remap_index.show = btn_remap_index_show; + entries[i].attr_remap_index.store =NULL ; + +/* Set up attributes array based on what's supported */ +if (cfg->turbo_support) { + entries[i].attrs[4 ] = + &entries[i].remap_attr->dev_attr.attr; + entries[i].attrs[5 ] = + &entries[i].macro_attr->dev_attr.attr; + entries[i].attrs[6 ] = + &entries[i].attr_remap_index.attr; + entries[i].attrs[7 ] =NULL ; + }else { + entries[i].attrs[0 ] = + &entries[i].remap_attr->dev_attr.attr; + entries[i].attrs[1 ] = + &entries[i].macro_attr->dev_attr.attr; + entries[i].attrs[2 ] = + &entries[i].attr_remap_index.attr; + entries[i].attrs[3 ] =NULL ; + } entries[i].group.name = ally_button_names[i]; entries[i].group.attrs = entries[i].attrs;
@@ -2867,12 +3839,27 @@ static int ally_create_button_attributes(struct hid_device *hdev,return 0 ; err_cleanup: +/* +* Unpublish first: the readers of button_entries (like the +* gamepad_mode_store() cross-notification) hold config_mutex and +* must observe either the live array or NULL, never a freed one. +* The lock is released again before the removals, so a straggler +* callback waiting on it can drain against the not yet freed +* entries. +*/ + scoped_guard(mutex, &cfg->config_mutex) { + cfg->button_entries =NULL ; + } +while (--i >=0 )sysfs_remove_group (&hdev->dev.kobj, &entries[i].group); -kfree (entries); +for (i =0 ; i <ALLY_BTN_MAX ; i++) { +kfree (entries[i].remap_attr); +kfree (entries[i].macro_attr); + } - cfg->button_entries =NULL ; +kfree (entries);return ret; }
@@ -2892,12 +3879,26 @@ static void ally_remove_button_attributes(struct hid_device *hdev, struct ally_c entries = cfg->button_entries; -for (i =0 ; i <ALLY_BTN_MAX ; i++) +/* +* Unpublish first: the readers of button_entries (like the +* gamepad_mode_store() cross-notification) hold config_mutex and +* must observe either the live array or NULL, never a freed one. +* The lock is released again before the removals, so a straggler +* callback waiting on it can drain against the not yet freed +* entries, and sysfs_remove_group() waits for the last ones to +* finish before the memory goes away. +*/ + scoped_guard(mutex, &cfg->config_mutex) { + cfg->button_entries =NULL ; + } + +for (i =0 ; i <ALLY_BTN_MAX ; i++) {sysfs_remove_group (&hdev->dev.kobj, &entries[i].group); +kfree (entries[i].remap_attr); +kfree (entries[i].macro_attr); + }kfree (entries); - - cfg->button_entries =NULL ; }/**
@@ -2938,6 +3939,13 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct alif (!cfg->initialized) { cfg->gamepad_mode =ALLY_GAMEPAD_MODE_GAMEPAD ; +/* +* Populate the button mappings for the default mode: a mode +* switch re-defaults them, while on a later probe they hold +* the values the user configured, which are what the restore +* path re-applies to the controller. +*/ +ally_set_default_gamepad_mapping (&cfg->button_mappings); cfg->xbox_controller_enabled = true; cfg->left_deadzone =10 ; cfg->left_outer_threshold =90 ;
@@ -3010,12 +4018,15 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al cal_created =ARRAY_SIZE (ally_cal_attr_groups); } -if (cfg->turbo_support) { - ret =ally_create_button_attributes (hdev, ally, cfg); -if (ret <0 ) { -hid_err (hdev,"Failed to create button attributes: %d\n" , ret); - goto ally_config_create_sysfs_err; - } +/* +* The button attributes carry the remap/macro attributes too, so +* they are created for every device with button directories, not +* only the ones with turbo support. +*/ + ret =ally_create_button_attributes (hdev, ally, cfg); +if (ret <0 ) { +hid_err (hdev,"Failed to create button attributes: %d\n" , ret); + goto ally_config_create_sysfs_err; } cfg->initialized = true;
@@ -3610,6 +4621,10 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +/* The mappings exist only if the button attributes were created. */ +if (cfg->button_entries) +ally_apply_button_mappings (hdev, ally, cfg); +return 0 ; }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v11 11/18] HID: asus: add support for btn remapping
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] HID: asus: race publishing button_entries in ally_create_button_attributes() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] HID: asus: race publishing button_entries in ally_create_button_attributes() --
commit 72fcba2fa957c8a91f51c8bd2f81b7d77bbc08de
Author: Denis Benato <[email protected]>
HID: asus: add support for btn remapping
This commit adds sysfs support for remapping physical buttons and macros on ROG Ally handheld devices, including per-button remap_index attributes and automatic cache refreshes when the gamepad mode changes.
Quoted 4 lines
drivers/hid/hid-asus.c +0
-0
[ ... ]
Quoted 30 lines
drivers/hid/hid-asus.c +26
-0@@ -1141,6 +1480,33 @@ static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *a cfg->gamepad_mode = mode_byte; +/* +* When the gamepad mode changes, the hardware automatically +* resets all button mappings to its internal defaults for the +* new mode. +* +* Refresh the software cache so that subsequent +* reads from sysfs reflect what the firmware is actually +* using. +*/ +if (mode_byte ==ALLY_GAMEPAD_MODE_GAMEPAD ) +ally_set_default_gamepad_mapping (&cfg->button_mappings); +else if (mode_byte ==ALLY_GAMEPAD_MODE_KEYBOARD ) +ally_set_default_keyboard_mapping (&cfg->button_mappings); + +/* +* The re-defaulted mappings are what the remap and macro files +* report: wake anyone polling them about the new values. +*/ +if (cfg->button_entries) { +for (i =0 ; i <ALLY_BTN_MAX ; i++) { +sysfs_notify (&dev->kobj, cfg->button_entries[i].group.name, +"remap" ); +sysfs_notify (&dev->kobj, cfg->button_entries[i].group.name, +"macro" ); + } + }
[ ... ]
Quoted 81 lines
drivers/hid/hid-asus.c +47
-9@@ -2824,15 +3759,12 @@ static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry)* Return: 0 on success, negative errno on failure */ static int ally_create_button_attributes (struct hid_device *hdev, -struct ally_handheld *ally, -struct ally_config *cfg) +struct ally_handheld *ally, +struct ally_config *cfg) {struct ally_btn_sysfs_entry *entries;int i, ret; -if (!cfg->turbo_support) -return 0 ; - entries =kcalloc (ALLY_BTN_MAX ,sizeof (*entries),GFP_KERNEL );if (!entries)return -ENOMEM ;@@ -2845,13 +3777,53 @@ static int ally_create_button_attributes(struct hid_device *hdev, entries[i].hdev = hdev; entries[i].btn = i; -if (!ally_btn_get_turbo_params (cfg, i)) { -hid_err (hdev,"Invalid button id %d for turbo attributes\n" , i); - ret = -EINVAL ; +if (cfg->turbo_support) { +if (!ally_btn_get_turbo_params (cfg, i)) { +hid_err (hdev,"Invalid button id %d for turbo attributes\n" , i); + ret = -EINVAL ; + goto err_cleanup; + } + +ally_btn_turbo_init_attrs (&entries[i]); + } + + entries[i].remap_attr =button_remap_attr_create (ally, i, false); +if (!entries[i].remap_attr) { + ret = -ENOMEM ; goto err_cleanup; } -ally_btn_turbo_init_attrs (&entries[i]); + entries[i].macro_attr =button_remap_attr_create (ally, i, true); +if (!entries[i].macro_attr) { + ret = -ENOMEM ; + goto err_cleanup; + } + +/* Initialize the remap_index attribute */ +sysfs_attr_init (&entries[i].attr_remap_index.attr); + entries[i].attr_remap_index.attr.name ="remap_index" ; + entries[i].attr_remap_index.attr.mode =0444 ; + entries[i].attr_remap_index.show = btn_remap_index_show; + entries[i].attr_remap_index.store =NULL ; + +/* Set up attributes array based on what's supported */ +if (cfg->turbo_support) { + entries[i].attrs[4 ] = + &entries[i].remap_attr->dev_attr.attr; + entries[i].attrs[5 ] = + &entries[i].macro_attr->dev_attr.attr; + entries[i].attrs[6 ] = + &entries[i].attr_remap_index.attr; + entries[i].attrs[7 ] =NULL ; + }else { + entries[i].attrs[0 ] = + &entries[i].remap_attr->dev_attr.attr; + entries[i].attrs[1 ] = + &entries[i].macro_attr->dev_attr.attr; + entries[i].attrs[2 ] = + &entries[i].attr_remap_index.attr; + entries[i].attrs[3 ] =NULL ; + } entries[i].group.name = ally_button_names[i]; entries[i].group.attrs = entries[i].attrs;@@ -2867,12 +3839,27 @@ static int ally_create_button_attributes(struct hid_device *hdev,return 0 ; err_cleanup: +/*
drivers/hid/hid-asus.c static int ally_create_button_attributes(struct hid_device *hdev, · line 3852
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* Unpublish first: the readers of button_entries (like the +* gamepad_mode_store() cross-notification) hold config_mutex and +* must observe either the live array or NULL, never a freed one. +* The lock is released again before the removals, so a straggler +* callback waiting on it can drain against the not yet freed +* entries. +*/ + scoped_guard(mutex, &cfg->config_mutex) { + cfg->button_entries =NULL ; + }
[Severity: Medium]
Can gamepad_mode_store() observe a partially initialized cfg->button_entries array while ally_create_button_attributes() is still running?
In hid_asus_ally_probe() -> ally_config_create(), ally->config is published and ally_attr_groups[0] (which exposes gamepad_mode) is registered with sysfs_create_group() before ally_create_button_attributes() is called:
drivers/hid/hid-asus.c:ally_config_create() {
...
spin_lock_irqsave(&ally_data_lock, flags);
ally->config = cfg;
spin_unlock_irqrestore(&ally_data_lock, flags);
for (sysfs_i = 0; sysfs_i < ARRAY_SIZE(ally_attr_groups); sysfs_i++) {
ret = sysfs_create_group(&hdev->dev.kobj,
&ally_attr_groups[sysfs_i]);
...
ret = ally_create_button_attributes(hdev, ally, cfg);
}At the start of ally_create_button_attributes(), cfg->button_entries = entries is assigned without holding cfg->config_mutex and before the loop initializes entries[i].group.name and creates the sysfs groups:
drivers/hid/hid-asus.c:ally_create_button_attributes() {
entries = kcalloc(ALLY_BTN_MAX, sizeof(*entries), GFP_KERNEL);
if (!entries)
return -ENOMEM;
cfg->button_entries = entries;
for (i = 0; i < ALLY_BTN_MAX; i++) {
...
entries[i].group.name = ally_button_names[i];
...
}If userspace writes to gamepad_mode while this loop is running (or before err_cleanup acquires cfg->config_mutex on failure), gamepad_mode_store() acquires cfg->config_mutex, sees a non-NULL cfg->button_entries, and iterates over all ALLY_BTN_MAX entries, racing with the unlocked writes to entries[i].group.name and passing NULL as the directory name to sysfs_notify() for entries that have not been initialized yet.
Should cfg->button_entries = entries instead be published under cfg->config_mutex only after all ALLY_BTN_MAX groups have been initialized and created?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Ilpo Järvinen
maintainerRe: [PATCH v11 11/18] HID: asus: add support for btn remapping
In reply to Denis BenatoQuoted 264 lines · Denis Benato
Add support for buttons remapping: the ability to assign different button events to each one of the physical button on the device.
Each button exposes a
remap_indexsysfs attribute listing the valid remap target names for the current gamepad mode: M1 and M2 can be remapped to anything at any time, while other buttons may only be remapped to gamepad codes in gamepad mode and keyboard/mouse/media codes in desktop mode.When the gamepad mode is changed the firmware automatically restores its internal defaults, so the software cache is refreshed to reflect the hardware state.
Signed-off-by Luke Jones <[email protected]>Signed-off-by Denis Benato <[email protected]>Signed-off-by Jonathan LoBue <[email protected]>--- drivers/hid/hid-asus.c | 1059 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 1037 insertions(+), 22 deletions(-)drivers/hid/hid-asus.c +205
-1@@ -275,9 +275,25 @@ struct ally_turbo_config {struct ally_btn_turbo_params btn_m1; }; +/* Button remap attribute structure */ +struct button_remap_attr { +struct device_attribute dev_attr; +enum ally_button_id button_id; +bool is_macro; +/* +* The attributes are created while hid_asus_ally_probe() is still +* running, before asus_probe() publishes drvdata->rog_ally: carry +* the pointer like the turbo entries do, so the callbacks work +* from the moment the files exist. +*/ +struct ally_handheld *ally; +}; + +#define to_button_remap_attr (x) container_of(x, struct button_remap_attr, dev_attr) +struct ally_btn_sysfs_entry {struct attribute_group group; -struct attribute *attrs[5 ];/* turbo_period + toggle_period + ranges + NULL */ +struct attribute *attrs[8 ];/* turbo + ranges + remap + macro + remap_index + NULL */ /* * The entries are created while hid_asus_ally_probe() is still running, * before asus_probe() publishes drvdata->rog_ally: carrying the shared @@ -289,6 +305,9 @@ struct ally_btn_sysfs_entry {enum ally_button_id btn;struct device_attribute attr_turbo_period;struct device_attribute attr_toggle_period; +struct button_remap_attr *remap_attr; +struct button_remap_attr *macro_attr; +struct device_attribute attr_remap_index; };enum ally_gamepad_mode_index {@@ -296,6 +315,35 @@ enum ally_gamepad_mode_index { ALLY_GAMEPAD_MODE_KEYBOARD = 0x02, }; +/* Button pair indexes for mapping commands */ +enum btn_pair_index { +BTN_PAIR_DPAD_UPDOWN =0x01 , +BTN_PAIR_DPAD_LEFTRIGHT =0x02 , +BTN_PAIR_STICK_LR =0x03 , +BTN_PAIR_BUMPER_LR =0x04 , +BTN_PAIR_AB =0x05 , +BTN_PAIR_XY =0x06 , +BTN_PAIR_VIEW_MENU =0x07 , +BTN_PAIR_M1M2 =0x08 , +BTN_PAIR_TRIGGER_LR =0x09 , +}; + +struct button_map { +const struct btn_code_map *remap; +const struct btn_code_map *macro; +}; + +struct button_pair_map { +enum btn_pair_index pair_index; +struct button_map first; +struct button_map second; +}; + +/* Store button mapping per gamepad mode */ +struct ally_button_mapping { +struct button_pair_map button_pairs[9 ];/* 9 button pairs */ +}; +struct ally_config {/* Must be locked if the data is being changed */ struct mutex config_mutex;@@ -331,6 +379,8 @@ struct ally_config {struct ally_turbo_config turbo;struct ally_btn_sysfs_entry *button_entries; +/* Button mappings of the current gamepad mode */ +struct ally_button_mapping button_mappings;struct ally_joystick_resp_curve left_curve;struct ally_joystick_resp_curve right_curve;@@ -1072,6 +1122,295 @@ static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device *return 0 ; } +enum btn_map_type { +BTN_TYPE_NONE =0 , +BTN_TYPE_PAD =0x01 , +BTN_TYPE_KB =0x02 , +BTN_TYPE_MOUSE =0x03 , +BTN_TYPE_MEDIA =0x05 , +}; + +struct btn_code_map { +unsigned char type; +unsigned char value; +const char *name; +}; + +static const struct btn_code_map ally_btn_codes[] = { + {BTN_TYPE_NONE ,0x00 ,"NONE" }, +/* Gamepad button codes */ + {BTN_TYPE_PAD ,0x01 ,"BTN_SOUTH" }, + {BTN_TYPE_PAD ,0x02 ,"BTN_EAST" }, + {BTN_TYPE_PAD ,0x03 ,"BTN_NORTH" }, + {BTN_TYPE_PAD ,0x04 ,"BTN_WEST" }, + {BTN_TYPE_PAD ,0x05 ,"BTN_TL" }, + {BTN_TYPE_PAD ,0x06 ,"BTN_TR" }, + {BTN_TYPE_PAD ,0x07 ,"BTN_THUMBL" }, + {BTN_TYPE_PAD ,0x08 ,"BTN_THUMBR" }, + {BTN_TYPE_PAD ,0x09 ,"ABS_HAT0Y_NEG" }, + {BTN_TYPE_PAD ,0x0A ,"ABS_HAT0Y_POS" }, + {BTN_TYPE_PAD ,0x0B ,"ABS_HAT0X_NEG" }, + {BTN_TYPE_PAD ,0x0C ,"ABS_HAT0X_POS" }, + {BTN_TYPE_PAD ,0x0D ,"ABS_Z" }, + {BTN_TYPE_PAD ,0x0E ,"ABS_RZ" }, + {BTN_TYPE_PAD ,0x11 ,"BTN_SELECT" }, + {BTN_TYPE_PAD ,0x12 ,"BTN_START" }, + {BTN_TYPE_PAD ,0x13 ,"BTN_MODE" }, + +/* Keyboard button codes */ + {BTN_TYPE_KB ,0x8E ,"FN_M2" }, + {BTN_TYPE_KB ,0x8F ,"FN_M1" }, + {BTN_TYPE_KB ,0x76 ,"KEY_ESC" }, + {BTN_TYPE_KB ,0x05 ,"KEY_F1" }, + {BTN_TYPE_KB ,0x06 ,"KEY_F2" }, + {BTN_TYPE_KB ,0x04 ,"KEY_F3" }, + {BTN_TYPE_KB ,0x0C ,"KEY_F4" }, + {BTN_TYPE_KB ,0x03 ,"KEY_F5" }, + {BTN_TYPE_KB ,0x0B ,"KEY_F6" }, + {BTN_TYPE_KB ,0x80 ,"KEY_F7" }, + {BTN_TYPE_KB ,0x0A ,"KEY_F8" }, + {BTN_TYPE_KB ,0x01 ,"KEY_F9" }, + {BTN_TYPE_KB ,0x09 ,"KEY_F10" }, + {BTN_TYPE_KB ,0x78 ,"KEY_F11" }, + {BTN_TYPE_KB ,0x07 ,"KEY_F12" }, + {BTN_TYPE_KB ,0x08 ,"KEY_F13" }, + {BTN_TYPE_KB ,0x10 ,"KEY_F14" }, + {BTN_TYPE_KB ,0x18 ,"KEY_F15" }, + {BTN_TYPE_KB ,0x0E ,"KEY_GRAVE" },// backtick + {BTN_TYPE_KB ,0x16 ,"KEY_1" }, + {BTN_TYPE_KB ,0x1E ,"KEY_2" }, + {BTN_TYPE_KB ,0x26 ,"KEY_3" }, + {BTN_TYPE_KB ,0x25 ,"KEY_4" }, + {BTN_TYPE_KB ,0x2E ,"KEY_5" }, + {BTN_TYPE_KB ,0x36 ,"KEY_6" }, + {BTN_TYPE_KB ,0x3D ,"KEY_7" }, + {BTN_TYPE_KB ,0x3E ,"KEY_8" }, + {BTN_TYPE_KB ,0x46 ,"KEY_9" }, + {BTN_TYPE_KB ,0x45 ,"KEY_0" }, + {BTN_TYPE_KB ,0x4E ,"KEY_MINUS" },// hyphen + {BTN_TYPE_KB ,0x55 ,"KEY_EQUAL" }, + {BTN_TYPE_KB ,0x66 ,"KEY_BACKSPACE" }, + {BTN_TYPE_KB ,0x0D ,"KEY_TAB" }, + {BTN_TYPE_KB ,0x15 ,"KEY_Q" }, + {BTN_TYPE_KB ,0x1D ,"KEY_W" }, + {BTN_TYPE_KB ,0x24 ,"KEY_E" }, + {BTN_TYPE_KB ,0x2D ,"KEY_R" }, + {BTN_TYPE_KB ,0x2C ,"KEY_T" }, + {BTN_TYPE_KB ,0x35 ,"KEY_Y" }, + {BTN_TYPE_KB ,0x3C ,"KEY_U" }, + {BTN_TYPE_KB ,0x43 ,"KEY_I" }, + {BTN_TYPE_KB ,0x44 ,"KEY_O" }, + {BTN_TYPE_KB ,0x4D ,"KEY_P" }, + {BTN_TYPE_KB ,0x54 ,"KEY_LEFTBRACE" }, + {BTN_TYPE_KB ,0x5B ,"KEY_RIGHTBRACE" }, + {BTN_TYPE_KB ,0x5D ,"KEY_BACKSLASH" }, + {BTN_TYPE_KB ,0x58 ,"KEY_CAPSLOCK" }, + {BTN_TYPE_KB ,0x1C ,"KEY_A" }, + {BTN_TYPE_KB ,0x1B ,"KEY_S" }, + {BTN_TYPE_KB ,0x23 ,"KEY_D" }, + {BTN_TYPE_KB ,0x2B ,"KEY_F" }, + {BTN_TYPE_KB ,0x34 ,"KEY_G" }, + {BTN_TYPE_KB ,0x33 ,"KEY_H" }, + {BTN_TYPE_KB ,0x3B ,"KEY_J" }, + {BTN_TYPE_KB ,0x42 ,"KEY_K" }, + {BTN_TYPE_KB ,0x4B ,"KEY_L" }, + {BTN_TYPE_KB ,0x4C ,"KEY_SEMICOLON" }, + {BTN_TYPE_KB ,0x52 ,"KEY_APOSTROPHE" }, + {BTN_TYPE_KB ,0x5A ,"KEY_ENTER" }, + {BTN_TYPE_KB ,0x88 ,"KEY_LEFTSHIFT" }, + {BTN_TYPE_KB ,0x1A ,"KEY_Z" }, + {BTN_TYPE_KB ,0x22 ,"KEY_X" }, + {BTN_TYPE_KB ,0x21 ,"KEY_C" }, + {BTN_TYPE_KB ,0x2A ,"KEY_V" }, + {BTN_TYPE_KB ,0x32 ,"KEY_B" }, + {BTN_TYPE_KB ,0x31 ,"KEY_N" }, + {BTN_TYPE_KB ,0x3A ,"KEY_M" }, + {BTN_TYPE_KB ,0x41 ,"KEY_COMMA" }, + {BTN_TYPE_KB ,0x49 ,"KEY_DOT" }, + {BTN_TYPE_KB ,0x4A ,"KEY_SLASH" }, + {BTN_TYPE_KB ,0x89 ,"KEY_RIGHTSHIFT" }, + {BTN_TYPE_KB ,0x82 ,"KEY_LEFTMETA" }, + {BTN_TYPE_KB ,0x8A ,"KEY_LEFTALT" }, + {BTN_TYPE_KB ,0x29 ,"KEY_SPACE" }, + {BTN_TYPE_KB ,0x8B ,"KEY_RIGHTALT" }, + {BTN_TYPE_KB ,0x84 ,"KEY_COMPOSE" }, + {BTN_TYPE_KB ,0x8D ,"KEY_RIGHTCTRL" }, + {BTN_TYPE_KB ,0xC3 ,"KEY_SYSRQ" }, + {BTN_TYPE_KB ,0x7E ,"KEY_SCROLLLOCK" }, + {BTN_TYPE_KB ,0x91 ,"KEY_PAUSE" }, + {BTN_TYPE_KB ,0xC2 ,"KEY_INSERT" }, + {BTN_TYPE_KB ,0x94 ,"KEY_HOME" }, + {BTN_TYPE_KB ,0x96 ,"KEY_PAGEUP" }, + {BTN_TYPE_KB ,0xC0 ,"KEY_DELETE" }, + {BTN_TYPE_KB ,0x95 ,"KEY_END" }, + {BTN_TYPE_KB ,0x97 ,"KEY_PAGEDOWN" }, + {BTN_TYPE_KB ,0x98 ,"KEY_UP" }, + {BTN_TYPE_KB ,0x99 ,"KEY_DOWN" }, + {BTN_TYPE_KB ,0x9A ,"KEY_LEFT" }, + {BTN_TYPE_KB ,0x9B ,"KEY_RIGHT" }, + +/* Numpad button codes */ + {BTN_TYPE_KB ,0x77 ,"KEY_NUMLOCK" }, + {BTN_TYPE_KB ,0x90 ,"KEY_KPSLASH" }, + {BTN_TYPE_KB ,0x7C ,"KEY_KPASTERISK" }, + {BTN_TYPE_KB ,0x7B ,"KEY_KPMINUS" }, + {BTN_TYPE_KB ,0x70 ,"KEY_KP0" }, + {BTN_TYPE_KB ,0x69 ,"KEY_KP1" }, + {BTN_TYPE_KB ,0x72 ,"KEY_KP2" }, + {BTN_TYPE_KB ,0x7A ,"KEY_KP3" }, + {BTN_TYPE_KB ,0x6B ,"KEY_KP4" }, + {BTN_TYPE_KB ,0x73 ,"KEY_KP5" }, + {BTN_TYPE_KB ,0x74 ,"KEY_KP6" }, + {BTN_TYPE_KB ,0x6C ,"KEY_KP7" }, + {BTN_TYPE_KB ,0x75 ,"KEY_KP8" }, + {BTN_TYPE_KB ,0x7D ,"KEY_KP9" }, + {BTN_TYPE_KB ,0x79 ,"KEY_KPPLUS" }, + {BTN_TYPE_KB ,0x81 ,"KEY_KPENTER" }, + {BTN_TYPE_KB ,0x71 ,"KEY_KPDOT" }, + +/* Mouse button codes */ + {BTN_TYPE_MOUSE ,0x01 ,"BTN_LEFT" }, + {BTN_TYPE_MOUSE ,0x02 ,"BTN_RIGHT" }, + {BTN_TYPE_MOUSE ,0x03 ,"BTN_MIDDLE" }, + {BTN_TYPE_MOUSE ,0x04 ,"REL_WHEEL_HI_RES_UP" }, + {BTN_TYPE_MOUSE ,0x05 ,"REL_WHEEL_HI_RES_DOWN" }, + +/* Media button codes */
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1288
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ { BTN_TYPE_MEDIA, 0x16, "MEDIA_SCREENSHOT" }, + { BTN_TYPE_MEDIA, 0x19, "MEDIA_SHOW_KEYBOARD" }, + { BTN_TYPE_MEDIA, 0x1C, "MEDIA_SHOW_DESKTOP" }, + { BTN_TYPE_MEDIA, 0x1E, "MEDIA_START_RECORDING" }, + { BTN_TYPE_MEDIA, 0x01, "MEDIA_MIC_OFF" }, + { BTN_TYPE_MEDIA, 0x02, "MEDIA_VOL_DOWN" }, + { BTN_TYPE_MEDIA, 0x03, "MEDIA_VOL_UP" }, +}; + +static const size_t keymap_len =ARRAY_SIZE (ally_btn_codes);
Why is this a variable???? Can't the users just directly use ARRAY_SIZE on the array????
At v11 it's odd to find this much garbage still remaining... :-(
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1290
Quoted 1 line
drivers/hid/hid-asus.c +1
-0+static const struct btn_code_map *find_button_by_name (const char *name);
Could you reorder the code such that this is not necessary. Having the code here would be more logical as well because that relates to the array.
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1297
Quoted 7 lines
drivers/hid/hid-asus.c +7
-0+ +static void ally_set_default_gamepad_mapping (struct ally_button_mapping *map) +{ +int i; + +/* Set all pair indexes and initialize to NONE */ +for (i =0 ; i <9 ; i++) {
ARRAY_SIZE()
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1306
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+ map->button_pairs[i].pair_index = i +1 ; + map->button_pairs[i].first.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].first.macro = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.macro = +find_button_by_name ("NONE" );
Why does this have to search for the NONE button multiple times?
Quoted 54 lines
drivers/hid/hid-asus.c +54
-0+ } + + map->button_pairs[BTN_PAIR_AB -1 ].first.remap = +find_button_by_name ("BTN_SOUTH" ); + map->button_pairs[BTN_PAIR_AB -1 ].second.remap = +find_button_by_name ("BTN_EAST" ); + + map->button_pairs[BTN_PAIR_XY -1 ].first.remap = +find_button_by_name ("BTN_NORTH" ); + map->button_pairs[BTN_PAIR_XY -1 ].second.remap = +find_button_by_name ("BTN_WEST" ); + + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].first.remap = +find_button_by_name ("BTN_TL" ); + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].second.remap = +find_button_by_name ("BTN_TR" ); + + map->button_pairs[BTN_PAIR_STICK_LR -1 ].first.remap = +find_button_by_name ("BTN_THUMBL" ); + map->button_pairs[BTN_PAIR_STICK_LR -1 ].second.remap = +find_button_by_name ("BTN_THUMBR" ); + + map->button_pairs[BTN_PAIR_DPAD_UPDOWN -1 ].first.remap = +find_button_by_name ("ABS_HAT0Y_NEG" ); + map->button_pairs[BTN_PAIR_DPAD_UPDOWN -1 ].second.remap = +find_button_by_name ("ABS_HAT0Y_POS" ); + + map->button_pairs[BTN_PAIR_DPAD_LEFTRIGHT -1 ].first.remap = +find_button_by_name ("ABS_HAT0X_NEG" ); + map->button_pairs[BTN_PAIR_DPAD_LEFTRIGHT -1 ].second.remap = +find_button_by_name ("ABS_HAT0X_POS" ); + + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].first.remap = +find_button_by_name ("ABS_Z" ); + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].second.remap = +find_button_by_name ("ABS_RZ" ); + + map->button_pairs[BTN_PAIR_VIEW_MENU -1 ].first.remap = +find_button_by_name ("BTN_SELECT" ); + map->button_pairs[BTN_PAIR_VIEW_MENU -1 ].second.remap = +find_button_by_name ("BTN_START" ); + + map->button_pairs[BTN_PAIR_M1M2 -1 ].first.remap = +find_button_by_name ("FN_M2" ); + map->button_pairs[BTN_PAIR_M1M2 -1 ].second.remap = +find_button_by_name ("FN_M1" ); +} + +static void ally_set_default_keyboard_mapping (struct ally_button_mapping *map) +{ +int i; + +/* Set all pair indexes and initialize to NONE */ +for (i =0 ; i <9 ; i++) {
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1370
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ map->button_pairs[i].pair_index = i +1 ; + map->button_pairs[i].first.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].first.macro = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.macro = +find_button_by_name ("NONE" ); + }
Duplicated code with the very same problems!
Quoted 94 lines
drivers/hid/hid-asus.c +72
-1+/* +* Desktop mode defaults: keyboard and mouse mappings. +* Buttons not listed here (dpad, view, menu) remain NONE. +*/ + +/* btn_a => KEY_SPACE, btn_b => KEY_E */ + map->button_pairs[BTN_PAIR_AB -1 ].first.remap = +find_button_by_name ("KEY_SPACE" ); + map->button_pairs[BTN_PAIR_AB -1 ].second.remap = +find_button_by_name ("KEY_E" ); + +/* btn_x => KEY_R, btn_y => KEY_F */ + map->button_pairs[BTN_PAIR_XY -1 ].first.remap = +find_button_by_name ("KEY_R" ); + map->button_pairs[BTN_PAIR_XY -1 ].second.remap = +find_button_by_name ("KEY_F" ); + +/* LB => wheel up, RB => wheel down */ + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].first.remap = +find_button_by_name ("REL_WHEEL_HI_RES_UP" ); + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].second.remap = +find_button_by_name ("REL_WHEEL_HI_RES_DOWN" ); + +/* left stick click => left click, right stick click => left shift */ + map->button_pairs[BTN_PAIR_STICK_LR -1 ].first.remap = +find_button_by_name ("BTN_LEFT" ); + map->button_pairs[BTN_PAIR_STICK_LR -1 ].second.remap = +find_button_by_name ("KEY_LEFTSHIFT" ); + +/* LT => left click, RT => right click */ + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].first.remap = +find_button_by_name ("BTN_LEFT" ); + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].second.remap = +find_button_by_name ("BTN_RIGHT" ); + +/* M2 => FN_M2, M1 => FN_M1 */ + map->button_pairs[BTN_PAIR_M1M2 -1 ].first.remap = +find_button_by_name ("FN_M2" ); + map->button_pairs[BTN_PAIR_M1M2 -1 ].second.remap = +find_button_by_name ("FN_M1" ); +} +static ssize_t gamepad_mode_show (struct device *dev,struct device_attribute *attr,char *buf) {struct hid_device *hdev =to_hid_device (dev);@@ -1113,7 +1452,7 @@ static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *astruct ally_config *cfg;u8 mode_byte;int mode; -int ret; +int ret, i;if (!ally)return -ENODEV ;@@ -1141,6 +1480,33 @@ static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *a cfg->gamepad_mode = mode_byte; +/* +* When the gamepad mode changes, the hardware automatically +* resets all button mappings to its internal defaults for the +* new mode. +* +* Refresh the software cache so that subsequent +* reads from sysfs reflect what the firmware is actually +* using. +*/ +if (mode_byte ==ALLY_GAMEPAD_MODE_GAMEPAD ) +ally_set_default_gamepad_mapping (&cfg->button_mappings); +else if (mode_byte ==ALLY_GAMEPAD_MODE_KEYBOARD ) +ally_set_default_keyboard_mapping (&cfg->button_mappings); + +/* +* The re-defaulted mappings are what the remap and macro files +* report: wake anyone polling them about the new values. +*/ +if (cfg->button_entries) { +for (i =0 ; i <ALLY_BTN_MAX ; i++) { +sysfs_notify (&dev->kobj, cfg->button_entries[i].group.name, +"remap" ); +sysfs_notify (&dev->kobj, cfg->button_entries[i].group.name, +"macro" ); + } + } +hid_dbg (hdev,"Set gamepad mode to %s\n" , ally_gamepad_mode_text[mode]);sysfs_notify (&dev->kobj,NULL ,"gamepad_mode" );@@ -2815,6 +3181,575 @@ static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) entry->attrs[4 ] =NULL ; } +/** +* find_button_by_name() - Find a button code map by its name
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3195
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* @name: name of the button code to look up +* +* Return: the matching button code map, or NULL if not found +*/ +static const struct btn_code_map *find_button_by_name (const char *name) +{ +int i; + +for (i =0 ; i < keymap_len; i++) { +if (strcmp (ally_btn_codes[i].name, name) ==0 )
!strcmp()
You probably should check if you've the include for strcmp(), could be necessary for some other patch too, I've lost track what includes have been added so far.
Quoted 51 lines
drivers/hid/hid-asus.c +51
-0+return &ally_btn_codes[i]; + } + +return NULL ; +} + +/** +* ally_remap_code_valid() - Check if a remap target is valid for a button +* @btn: button to remap +* @gamepad_mode: gamepad mode to validate the remap against +* @code: remap target to check +* @is_macro: whether the target is assigned to the macro mapping +* +* M1 and M2 can be remapped to anything at any time. +* All other buttons may only be remapped to gamepad buttons (BTN_TYPE_PAD) +* when in gamepad mode, and to keyboard/mouse/media codes when in desktop +* (keyboard) mode. BTN_TYPE_NONE (unmapped) is always allowed. +* Macro mappings are executed by the MCU as input sequences on the virtual +* keyboard and are accepted regardless of the current gamepad mode. +* +* Return: true if the remap is valid, false otherwise +*/ +static bool ally_remap_code_valid (enum ally_button_id btn,u8 gamepad_mode, +const struct btn_code_map *code,bool is_macro) +{ +if (!code) +return false; + +/* M1 and M2 can be remapped to everything */ +if (btn ==ALLY_BTN_M1 || btn ==ALLY_BTN_M2 ) +return true; + +/* NONE is always valid */ +if (code->type ==BTN_TYPE_NONE ) +return true; + +/* Macros are not restricted by the controller emulation mode */ +if (is_macro) +return true; + +if (gamepad_mode ==ALLY_GAMEPAD_MODE_GAMEPAD ) +return code->type ==BTN_TYPE_PAD ; + +/* Desktop / keyboard mode: allow keyboard, mouse and media */ +return code->type ==BTN_TYPE_KB || + code->type ==BTN_TYPE_MOUSE || + code->type ==BTN_TYPE_MEDIA ; +} + +/** +* ally_set_button_mapping() - Set the button mapping for a button pair
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3256
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* @hdev: HID device +* @ally: ally handheld structure +* @mapping: button pair mapping to send to the device +* +* Return: count of data transferred, negative if error +*/ +static int ally_set_button_mapping (struct hid_device *hdev,struct ally_handheld *ally, +struct button_pair_map *mapping) +{ +/* The MCU mapping block is four consecutive 11-byte entries starting at
Inconsistent multi-line comment style.
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3258
Quoted 2 lines
drivers/hid/hid-asus.c +2
-0+* buf[5]: first remap 5-15, first macro 16-26, second remap 27-37, +* second macro 38-48 (see hid-asus-ally __btn_pair_to_pkt, BTN_CODE_LEN).
I didn't find __btn_pair_to_pkt from anywhere?? Nor BTN_CODE_LEN??
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3261
Quoted 3 lines
drivers/hid/hid-asus.c +3
-0+*/ +u8 macro_bytes[11 ] = {0 }; +u8 btn_bytes[11 ] = {0 };
= {} initializes to default values which is 0 for u8.
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3271
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +if (!mapping || !mapping->first.remap || !mapping->first.macro || + !mapping->second.remap || !mapping->second.macro) +return -EINVAL ; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_MAPPING, NULL, 0); +if (!buf) +return -ENOMEM ; + +/* This packet is slightly different from the other
Comment style.
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3276
Quoted 5 lines
drivers/hid/hid-asus.c +5
-0+* as before the packet length there is an extra byte +* which is the pair index. +*/ + buf[3 ] = mapping->pair_index; + buf[4 ] =0x2C ;/* Length */
Use named define or sizeof() (if viable).
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3285
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+ +/* First button mapping */ + buf[5 ] = mapping->first.remap->type; +/* Fill in bytes 6-14 with button code */ +if (mapping->first.remap->type) { +memset (btn_bytes,0 ,sizeof (btn_bytes)); + btn_bytes[0 ] = mapping->first.remap->type; + +/* Value byte position depends on type: pad=1, kb=2, media=3,
Comment style.
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+* mouse=4 (see hid-asus-ally BTN_CODE definitions). +*/ +switch (mapping->first.remap->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + btn_bytes[1 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_KB :
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3304
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ btn_bytes[2 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_MEDIA : + btn_bytes[3 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_MOUSE : + btn_bytes[4 ] = mapping->first.remap->value; +break ; + } +memcpy (&buf[5 ], btn_bytes,11 );
Magic 11. Please name properly.
I wonder though if there should be helpers for these with a few parameters as these seem mostly just duplicated multiple times.
Quoted 15 lines
drivers/hid/hid-asus.c +15
-0+ } + +/* Macro mapping for first button if any */ + buf[16 ] = mapping->first.macro->type; +if (mapping->first.macro->type) { +memset (macro_bytes,0 ,sizeof (macro_bytes)); + macro_bytes[0 ] = mapping->first.macro->type; + +switch (mapping->first.macro->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + macro_bytes[1 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_KB :
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3329
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ macro_bytes[2 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_MEDIA : + macro_bytes[3 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_MOUSE : + macro_bytes[4 ] = mapping->first.macro->value; +break ; + } +memcpy (&buf[16 ], macro_bytes,11 );
Magic 11.
Quoted 16 lines
drivers/hid/hid-asus.c +16
-0+ } + +/* Second button mapping */ + buf[27 ] = mapping->second.remap->type; +/* Fill in bytes 28-36 with button code */ +if (mapping->second.remap->type) { +memset (btn_bytes,0 ,sizeof (btn_bytes)); + btn_bytes[0 ] = mapping->second.remap->type; + +switch (mapping->second.remap->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + btn_bytes[1 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_KB :
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3355
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ btn_bytes[2 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_MEDIA : + btn_bytes[3 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_MOUSE : + btn_bytes[4 ] = mapping->second.remap->value; +break ; + } +memcpy (&buf[27 ], btn_bytes,11 );
Ditto.
Quoted 15 lines
drivers/hid/hid-asus.c +15
-0+ } + +/* Macro mapping for second button if any */ + buf[38 ] = mapping->second.macro->type; +if (mapping->second.macro->type) { +memset (macro_bytes,0 ,sizeof (macro_bytes)); + macro_bytes[0 ] = mapping->second.macro->type; + +switch (mapping->second.macro->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + macro_bytes[1 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_KB :
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3380
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ macro_bytes[2 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_MEDIA : + macro_bytes[3 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_MOUSE : + macro_bytes[4 ] = mapping->second.macro->value; +break ; + } +memcpy (&buf[38 ], macro_bytes,11 );
Ditto.
Quoted 156 lines
drivers/hid/hid-asus.c +156
-0+ } + +return ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +} + +/** +* ally_apply_button_mappings() - Push the recorded mappings of the current +* gamepad mode to the controller +* @hdev: HID device +* @ally: ally handheld structure +* @cfg: ally config structure +* +* An MCU reset restores the firmware default mappings: push the recorded +* ones again so that the hardware keeps matching what userspace +* configured. The caller must hold cfg->config_mutex, exactly like the +* sysfs store callbacks that update single pairs. +*/ +static void ally_apply_button_mappings (struct hid_device *hdev, +struct ally_handheld *ally, +struct ally_config *cfg) +{ +struct ally_button_mapping *mode_map; +int i, ret; + + mode_map = &cfg->button_mappings; + +for (i =0 ; i <ARRAY_SIZE (mode_map->button_pairs); i++) { + mode_map->button_pairs[i].pair_index = i +1 ; + + ret =ally_set_button_mapping (hdev, ally, + &mode_map->button_pairs[i]); +if (ret <0 ) { +hid_warn (hdev, +"Failed to restore button mappings: %d\n" , + ret); +return ; + } + } +} + +/** +* get_button_pair_info() - Get the pair index and position of a button +* @button_id: button to look up +* @pair_idx: output pointer for the pair index of the button +* @is_first: output pointer for the position of the button within the pair +* +* Return: 0 on success, negative errno on failure +*/ +static int get_button_pair_info (enum ally_button_id button_id, +enum btn_pair_index *pair_idx, +bool *is_first) +{ +switch (button_id) { +case ALLY_BTN_DU : + *pair_idx =BTN_PAIR_DPAD_UPDOWN ; + *is_first = true; +break ; +case ALLY_BTN_DD : + *pair_idx =BTN_PAIR_DPAD_UPDOWN ; + *is_first = false; +break ; +case ALLY_BTN_DL : + *pair_idx =BTN_PAIR_DPAD_LEFTRIGHT ; + *is_first = true; +break ; +case ALLY_BTN_DR : + *pair_idx =BTN_PAIR_DPAD_LEFTRIGHT ; + *is_first = false; +break ; +case ALLY_BTN_J0B : + *pair_idx =BTN_PAIR_STICK_LR ; + *is_first = true; +break ; +case ALLY_BTN_J1B : + *pair_idx =BTN_PAIR_STICK_LR ; + *is_first = false; +break ; +case ALLY_BTN_LB : + *pair_idx =BTN_PAIR_BUMPER_LR ; + *is_first = true; +break ; +case ALLY_BTN_RB : + *pair_idx =BTN_PAIR_BUMPER_LR ; + *is_first = false; +break ; +case ALLY_BTN_A : + *pair_idx =BTN_PAIR_AB ; + *is_first = true; +break ; +case ALLY_BTN_B : + *pair_idx =BTN_PAIR_AB ; + *is_first = false; +break ; +case ALLY_BTN_X : + *pair_idx =BTN_PAIR_XY ; + *is_first = true; +break ; +case ALLY_BTN_Y : + *pair_idx =BTN_PAIR_XY ; + *is_first = false; +break ; +case ALLY_BTN_VIEW : + *pair_idx =BTN_PAIR_VIEW_MENU ; + *is_first = true; +break ; +case ALLY_BTN_MENU : + *pair_idx =BTN_PAIR_VIEW_MENU ; + *is_first = false; +break ; +case ALLY_BTN_M1 : + *pair_idx =BTN_PAIR_M1M2 ; + *is_first = false; +break ; +case ALLY_BTN_M2 : + *pair_idx =BTN_PAIR_M1M2 ; + *is_first = true; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static ssize_t button_remap_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct button_remap_attr *btn_attr =to_button_remap_attr (attr); +struct ally_handheld *ally = btn_attr->ally; +const struct btn_code_map *code; +struct ally_config *cfg; +enum ally_button_id button_id = btn_attr->button_id; +enum btn_pair_index pair_idx; +struct button_pair_map *pair; +struct button_map *btn_map; +bool is_first; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =get_button_pair_info (button_id, &pair_idx, &is_first); +if (ret <0 ) +return ret; + +guard (mutex)(&cfg->config_mutex); + + pair = &cfg->button_mappings.button_pairs[pair_idx -1 ]; + btn_map = is_first ? &pair->first : &pair->second; + code = btn_attr->is_macro ? btn_map->macro : btn_map->remap; + +/*
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3546
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* The mappings are never NULL: the defaults, the mode-switch +* re-defaults and the store rollback all guarantee it. A miss +* means a broken default table, so fail loudly instead of +* dereferencing. +*/ +if (!code) +return -EIO ; + +if (code->type ==BTN_TYPE_NONE ) +return sysfs_emit (buf,"NONE\n" );
Wouldn't it emit "NONE" anyway with the normal return so why you need special print for this separately?
Quoted 25 lines
drivers/hid/hid-asus.c +25
-0+ +return sysfs_emit (buf,"%s\n" , code->name); +} + +static ssize_t button_remap_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct button_remap_attr *btn_attr =to_button_remap_attr (attr); +struct ally_handheld *ally = btn_attr->ally; +enum ally_button_id button_id = btn_attr->button_id; +struct ally_button_mapping *mode_map; +const struct btn_code_map *code; +const struct btn_code_map *old_code; +enum btn_pair_index pair_idx; +struct button_pair_map *pair; +struct button_map *btn_map; +struct ally_config *cfg; +char btn_name[32 ]; +bool is_first; +bool undo_hw; +int ret, i; + +if (!ally)
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3581
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +if (sscanf (buf,"%31s" , btn_name) !=1 ) +return -EINVAL ; + +/* Handle "NONE" specially */
Yeah yeah, and now you better must explain me why???? This is totally useless comment and helps zero to explain why the code is making things in a stupid way.
Won't find_button_by_name() return the very same entry???
(You can see probably I'm starting to get tired of reading this kind of things at this point... ...I've read quite many lines by now.)
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3582
Quoted 1 line
drivers/hid/hid-asus.c +1
-0+if (strcmp (btn_name,"NONE" ) ==0 ) {
!strcmp(), but it looks totally useless check (see above).
Quoted 37 lines
drivers/hid/hid-asus.c +37
-0+ code = &ally_btn_codes[0 ];/* NONE entry */ + }else { + code =find_button_by_name (btn_name); +if (!code) +return -EINVAL ; + } + + ret =get_button_pair_info (button_id, &pair_idx, &is_first); +if (ret <0 ) +return ret; + +guard (mutex)(&cfg->config_mutex); + +/* +* Validate the remap target while holding the lock: the +* gamepad mode can change concurrently, and the check must +* be done against the very mode the mapping is written to. +*/ +if (!ally_remap_code_valid (button_id, cfg->gamepad_mode, code, + btn_attr->is_macro)) +return -EINVAL ; + +/* Access the mapping for current gamepad mode */ + pair = &cfg->button_mappings.button_pairs[pair_idx -1 ]; + btn_map = is_first ? &pair->first : &pair->second; + +if (btn_attr->is_macro) { + old_code = btn_map->macro; + btn_map->macro = code; + }else { + old_code = btn_map->remap; + btn_map->remap = code; + } + +/* Update pair index */ + pair->pair_index = pair_idx; +
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3629
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+/* +* Send mapping to device with the caveat that first +* generation devices require individual button pair updates. +*/ +if (cfg->is_ally_x) { + ret =ally_set_button_mapping (hdev, ally, pair); + undo_hw = false; + }else { + mode_map = &cfg->button_mappings; + ret =0 ;
Is this required?
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3630
Quoted 1 line
drivers/hid/hid-asus.c +1
-0+for (i =0 ; i <9 ; i++) {
ARRAY_SIZE()
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3635
Quoted 5 lines
drivers/hid/hid-asus.c +5
-0+ mode_map->button_pairs[i].pair_index = i +1 ; + ret =ally_set_button_mapping (hdev, ally, + &mode_map->button_pairs[i]); +if (ret <0 ) +break ;
IMO, it would be better to use goto to deal with these errors (here and above in the if blcok).
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3644
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+ } + +/* +* Pairs are pushed one at a time: remember whether +* the target pair reached the hardware before a +* later pair failed, so the rollback below can undo +* it on the device too. +*/ + undo_hw = ret <0 && i > pair_idx -1 ;
Do this above before goto.
Quoted 18 lines
drivers/hid/hid-asus.c +18
-0+ } + +/* +* The hardware rejected the update: restore the previous +* mapping so that the software cache keeps describing the +* state the device is actually in. +*/ +if (ret <0 ) { +if (btn_attr->is_macro) + btn_map->macro = old_code; +else + btn_map->remap = old_code; + +/* +* The target pair was already committed before a +* later pair failed: push the restored mapping once +* more so the hardware matches the cache again. Best +* effort only: the device is misbehaving already.
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3672
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+*/ +if (undo_hw) +ally_set_button_mapping (hdev, ally, pair); + +hid_err (hdev,"Failed to set button mapping: %d\n" , ret); +return ret; + } + +sysfs_notify (&dev->kobj, ally_button_names[btn_attr->button_id], + btn_attr->is_macro ?"macro" :"remap" );
I suggest adding a local string_choices.h style str_macro_remap() for this as it seems to occur a few time.
Quoted 20 lines
drivers/hid/hid-asus.c +20
-0+ +return count; +} + +/** +* button_remap_attr_create() - Create a button remap attribute +* @ally: ally handheld structure +* @button_id: button the attribute is for +* @is_macro: whether the attribute controls the macro mapping +* +* Return: the newly created attribute, or NULL on allocation failure +*/ +static struct button_remap_attr *button_remap_attr_create (struct ally_handheld *ally, +enum ally_button_id button_id, +bool is_macro) +{ + struct button_remap_attr *attr __free(kfree) = kzalloc_obj(*attr); +if (!attr) +return NULL ; +
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3702
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ attr->ally = ally; + attr->button_id = button_id; + attr->is_macro = is_macro; +sysfs_attr_init (&attr->dev_attr.attr); + attr->dev_attr.attr.name = is_macro ?"macro" :"remap" ; + attr->dev_attr.attr.mode =0644 ; + attr->dev_attr.show = button_remap_show; + attr->dev_attr.store = button_remap_store; + +return no_free_ptr (attr);
You don't need to use __free()/no_free_ptr() in this function because only return is due to alloc fail check (attr is NULL at that point and kfree(NULL) is no-op).
Quoted 115 lines
drivers/hid/hid-asus.c +85
-9+} + +/** +* btn_remap_index_show() - Show the list of valid remap targets for a button +* @dev: device the attribute belongs to +* @attr: attribute being read +* @buf: buffer to write the list into +* +* M1/M2 list everything; other buttons list only the codes valid for the +* active mode (gamepad buttons in gamepad mode, keyboard / mouse / media +* in desktop mode). +* +* Return: number of characters written +*/ +static ssize_t btn_remap_index_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct hid_device *hdev =to_hid_device (dev); +struct asus_drvdata *drvdata =hid_get_drvdata (hdev); +struct ally_handheld *ally = drvdata->rog_ally; +struct ally_btn_sysfs_entry *entry =container_of (attr, + struct ally_btn_sysfs_entry, attr_remap_index); +struct ally_config *cfg; +enum ally_button_id btn = entry->btn; +u8 mode; +ssize_t len =0 ; +int i; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +scoped_guard (mutex, &cfg->config_mutex) + mode = cfg->gamepad_mode; + +for (i =0 ; i < keymap_len; i++) { +if (!ally_remap_code_valid (btn, mode, &ally_btn_codes[i], false)) +continue ; + len +=sysfs_emit_at (buf, len,"%s " , ally_btn_codes[i].name); + } + +if (len >0 ) + buf[len -1 ] ='\n' ; + +return len; +} +/** * ally_create_button_attributes() - Create turbo button attributes * @hdev: HID device @@ -2824,15 +3759,12 @@ static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry)* Return: 0 on success, negative errno on failure */ static int ally_create_button_attributes (struct hid_device *hdev, -struct ally_handheld *ally, -struct ally_config *cfg) +struct ally_handheld *ally, +struct ally_config *cfg) {struct ally_btn_sysfs_entry *entries;int i, ret; -if (!cfg->turbo_support) -return 0 ; - entries =kcalloc (ALLY_BTN_MAX ,sizeof (*entries),GFP_KERNEL );if (!entries)return -ENOMEM ;@@ -2845,13 +3777,53 @@ static int ally_create_button_attributes(struct hid_device *hdev, entries[i].hdev = hdev; entries[i].btn = i; -if (!ally_btn_get_turbo_params (cfg, i)) { -hid_err (hdev,"Invalid button id %d for turbo attributes\n" , i); - ret = -EINVAL ; +if (cfg->turbo_support) { +if (!ally_btn_get_turbo_params (cfg, i)) { +hid_err (hdev,"Invalid button id %d for turbo attributes\n" , i); + ret = -EINVAL ; + goto err_cleanup; + } + +ally_btn_turbo_init_attrs (&entries[i]); + } + + entries[i].remap_attr =button_remap_attr_create (ally, i, false); +if (!entries[i].remap_attr) { + ret = -ENOMEM ; goto err_cleanup; } -ally_btn_turbo_init_attrs (&entries[i]); + entries[i].macro_attr =button_remap_attr_create (ally, i, true); +if (!entries[i].macro_attr) { + ret = -ENOMEM ; + goto err_cleanup; + } + +/* Initialize the remap_index attribute */ +sysfs_attr_init (&entries[i].attr_remap_index.attr); + entries[i].attr_remap_index.attr.name ="remap_index" ; + entries[i].attr_remap_index.attr.mode =0444 ; + entries[i].attr_remap_index.show = btn_remap_index_show; + entries[i].attr_remap_index.store =NULL ; + +/* Set up attributes array based on what's supported */ +if (cfg->turbo_support) { + entries[i].attrs[4 ] = + &entries[i].remap_attr->dev_attr.attr; + entries[i].attrs[5 ] = + &entries[i].macro_attr->dev_attr.attr; + entries[i].attrs[6] =
drivers/hid/hid-asus.c static int ally_create_button_attributes(struct hid_device *hdev, · line 3825
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ &entries[i].attr_remap_index.attr; + entries[i].attrs[7 ] =NULL ; + }else { + entries[i].attrs[0 ] = + &entries[i].remap_attr->dev_attr.attr; + entries[i].attrs[1 ] = + &entries[i].macro_attr->dev_attr.attr; + entries[i].attrs[2 ] = + &entries[i].attr_remap_index.attr; + entries[i].attrs[3 ] =NULL ;
So only the attrs index changed?? Why all the duplication?
Quoted 112 lines
drivers/hid/hid-asus.c +55
-11+ } entries[i].group.name = ally_button_names[i]; entries[i].group.attrs = entries[i].attrs;@@ -2867,12 +3839,27 @@ static int ally_create_button_attributes(struct hid_device *hdev,return 0 ; err_cleanup: +/* +* Unpublish first: the readers of button_entries (like the +* gamepad_mode_store() cross-notification) hold config_mutex and +* must observe either the live array or NULL, never a freed one. +* The lock is released again before the removals, so a straggler +* callback waiting on it can drain against the not yet freed +* entries. +*/ + scoped_guard(mutex, &cfg->config_mutex) { + cfg->button_entries =NULL ; + } +while (--i >=0 )sysfs_remove_group (&hdev->dev.kobj, &entries[i].group); -kfree (entries); +for (i =0 ; i <ALLY_BTN_MAX ; i++) { +kfree (entries[i].remap_attr); +kfree (entries[i].macro_attr); + } - cfg->button_entries =NULL ; +kfree (entries);return ret; }@@ -2892,12 +3879,26 @@ static void ally_remove_button_attributes(struct hid_device *hdev, struct ally_c entries = cfg->button_entries; -for (i =0 ; i <ALLY_BTN_MAX ; i++) +/* +* Unpublish first: the readers of button_entries (like the +* gamepad_mode_store() cross-notification) hold config_mutex and +* must observe either the live array or NULL, never a freed one. +* The lock is released again before the removals, so a straggler +* callback waiting on it can drain against the not yet freed +* entries, and sysfs_remove_group() waits for the last ones to +* finish before the memory goes away. +*/ + scoped_guard(mutex, &cfg->config_mutex) { + cfg->button_entries =NULL ; + } + +for (i =0 ; i <ALLY_BTN_MAX ; i++) {sysfs_remove_group (&hdev->dev.kobj, &entries[i].group); +kfree (entries[i].remap_attr); +kfree (entries[i].macro_attr); + }kfree (entries); - - cfg->button_entries =NULL ; }/** @@ -2938,6 +3939,13 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct alif (!cfg->initialized) { cfg->gamepad_mode =ALLY_GAMEPAD_MODE_GAMEPAD ; +/* +* Populate the button mappings for the default mode: a mode +* switch re-defaults them, while on a later probe they hold +* the values the user configured, which are what the restore +* path re-applies to the controller. +*/ +ally_set_default_gamepad_mapping (&cfg->button_mappings); cfg->xbox_controller_enabled = true; cfg->left_deadzone =10 ; cfg->left_outer_threshold =90 ;@@ -3010,12 +4018,15 @@ static struct ally_config *ally_config_create(struct hid_device *hdev, struct al cal_created =ARRAY_SIZE (ally_cal_attr_groups); } -if (cfg->turbo_support) { - ret =ally_create_button_attributes (hdev, ally, cfg); -if (ret <0 ) { -hid_err (hdev,"Failed to create button attributes: %d\n" , ret); - goto ally_config_create_sysfs_err; - } +/* +* The button attributes carry the remap/macro attributes too, so +* they are created for every device with button directories, not +* only the ones with turbo support. +*/ + ret =ally_create_button_attributes (hdev, ally, cfg); +if (ret <0 ) { +hid_err (hdev,"Failed to create button attributes: %d\n" , ret); + goto ally_config_create_sysfs_err; } cfg->initialized = true;@@ -3610,6 +4621,10 @@ static int hid_asus_ally_init(struct hid_device *hdev, struct ally_handheld *all ret); } +/* The mappings exist only if the button attributes were created. */ +if (cfg->button_entries) +ally_apply_button_mappings (hdev, ally, cfg); +return 0 ; }
Signature · 1 line
i.
Denis Benato
maintainerRe: [PATCH v11 11/18] HID: asus: add support for btn remapping
Quoted 270 lines · Ilpo Järvinen
Quoted 268 lines · Denis Benato
Add support for buttons remapping: the ability to assign different button events to each one of the physical button on the device.
Each button exposes a
remap_indexsysfs attribute listing the valid remap target names for the current gamepad mode: M1 and M2 can be remapped to anything at any time, while other buttons may only be remapped to gamepad codes in gamepad mode and keyboard/mouse/media codes in desktop mode.When the gamepad mode is changed the firmware automatically restores its internal defaults, so the software cache is refreshed to reflect the hardware state.
Signed-off-by Luke Jones <[email protected]>Signed-off-by Denis Benato <[email protected]>Signed-off-by Jonathan LoBue <[email protected]>--- drivers/hid/hid-asus.c | 1059 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 1037 insertions(+), 22 deletions(-)drivers/hid/hid-asus.c +209
-1@@ -275,9 +275,25 @@ struct ally_turbo_config {struct ally_btn_turbo_params btn_m1; }; +/* Button remap attribute structure */ +struct button_remap_attr { +struct device_attribute dev_attr; +enum ally_button_id button_id; +bool is_macro; +/* +* The attributes are created while hid_asus_ally_probe() is still +* running, before asus_probe() publishes drvdata->rog_ally: carry +* the pointer like the turbo entries do, so the callbacks work +* from the moment the files exist. +*/ +struct ally_handheld *ally; +}; + +#define to_button_remap_attr (x) container_of(x, struct button_remap_attr, dev_attr) +struct ally_btn_sysfs_entry {struct attribute_group group; -struct attribute *attrs[5 ];/* turbo_period + toggle_period + ranges + NULL */ +struct attribute *attrs[8 ];/* turbo + ranges + remap + macro + remap_index + NULL */ /* * The entries are created while hid_asus_ally_probe() is still running, * before asus_probe() publishes drvdata->rog_ally: carrying the shared @@ -289,6 +305,9 @@ struct ally_btn_sysfs_entry {enum ally_button_id btn;struct device_attribute attr_turbo_period;struct device_attribute attr_toggle_period; +struct button_remap_attr *remap_attr; +struct button_remap_attr *macro_attr; +struct device_attribute attr_remap_index; };enum ally_gamepad_mode_index {@@ -296,6 +315,35 @@ enum ally_gamepad_mode_index { ALLY_GAMEPAD_MODE_KEYBOARD = 0x02, }; +/* Button pair indexes for mapping commands */ +enum btn_pair_index { +BTN_PAIR_DPAD_UPDOWN =0x01 , +BTN_PAIR_DPAD_LEFTRIGHT =0x02 , +BTN_PAIR_STICK_LR =0x03 , +BTN_PAIR_BUMPER_LR =0x04 , +BTN_PAIR_AB =0x05 , +BTN_PAIR_XY =0x06 , +BTN_PAIR_VIEW_MENU =0x07 , +BTN_PAIR_M1M2 =0x08 , +BTN_PAIR_TRIGGER_LR =0x09 , +}; + +struct button_map { +const struct btn_code_map *remap; +const struct btn_code_map *macro; +}; + +struct button_pair_map { +enum btn_pair_index pair_index; +struct button_map first; +struct button_map second; +}; + +/* Store button mapping per gamepad mode */ +struct ally_button_mapping { +struct button_pair_map button_pairs[9 ];/* 9 button pairs */ +}; +struct ally_config {/* Must be locked if the data is being changed */ struct mutex config_mutex;@@ -331,6 +379,8 @@ struct ally_config {struct ally_turbo_config turbo;struct ally_btn_sysfs_entry *button_entries; +/* Button mappings of the current gamepad mode */ +struct ally_button_mapping button_mappings;struct ally_joystick_resp_curve left_curve;struct ally_joystick_resp_curve right_curve;@@ -1072,6 +1122,295 @@ static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device *return 0 ; } +enum btn_map_type { +BTN_TYPE_NONE =0 , +BTN_TYPE_PAD =0x01 , +BTN_TYPE_KB =0x02 , +BTN_TYPE_MOUSE =0x03 , +BTN_TYPE_MEDIA =0x05 , +}; + +struct btn_code_map { +unsigned char type; +unsigned char value; +const char *name; +}; + +static const struct btn_code_map ally_btn_codes[] = { + {BTN_TYPE_NONE ,0x00 ,"NONE" }, +/* Gamepad button codes */ + {BTN_TYPE_PAD ,0x01 ,"BTN_SOUTH" }, + {BTN_TYPE_PAD ,0x02 ,"BTN_EAST" }, + {BTN_TYPE_PAD ,0x03 ,"BTN_NORTH" }, + {BTN_TYPE_PAD ,0x04 ,"BTN_WEST" }, + {BTN_TYPE_PAD ,0x05 ,"BTN_TL" }, + {BTN_TYPE_PAD ,0x06 ,"BTN_TR" }, + {BTN_TYPE_PAD ,0x07 ,"BTN_THUMBL" }, + {BTN_TYPE_PAD ,0x08 ,"BTN_THUMBR" }, + {BTN_TYPE_PAD ,0x09 ,"ABS_HAT0Y_NEG" }, + {BTN_TYPE_PAD ,0x0A ,"ABS_HAT0Y_POS" }, + {BTN_TYPE_PAD ,0x0B ,"ABS_HAT0X_NEG" }, + {BTN_TYPE_PAD ,0x0C ,"ABS_HAT0X_POS" }, + {BTN_TYPE_PAD ,0x0D ,"ABS_Z" }, + {BTN_TYPE_PAD ,0x0E ,"ABS_RZ" }, + {BTN_TYPE_PAD ,0x11 ,"BTN_SELECT" }, + {BTN_TYPE_PAD ,0x12 ,"BTN_START" }, + {BTN_TYPE_PAD ,0x13 ,"BTN_MODE" }, + +/* Keyboard button codes */ + {BTN_TYPE_KB ,0x8E ,"FN_M2" }, + {BTN_TYPE_KB ,0x8F ,"FN_M1" }, + {BTN_TYPE_KB ,0x76 ,"KEY_ESC" }, + {BTN_TYPE_KB ,0x05 ,"KEY_F1" }, + {BTN_TYPE_KB ,0x06 ,"KEY_F2" }, + {BTN_TYPE_KB ,0x04 ,"KEY_F3" }, + {BTN_TYPE_KB ,0x0C ,"KEY_F4" }, + {BTN_TYPE_KB ,0x03 ,"KEY_F5" }, + {BTN_TYPE_KB ,0x0B ,"KEY_F6" }, + {BTN_TYPE_KB ,0x80 ,"KEY_F7" }, + {BTN_TYPE_KB ,0x0A ,"KEY_F8" }, + {BTN_TYPE_KB ,0x01 ,"KEY_F9" }, + {BTN_TYPE_KB ,0x09 ,"KEY_F10" }, + {BTN_TYPE_KB ,0x78 ,"KEY_F11" }, + {BTN_TYPE_KB ,0x07 ,"KEY_F12" }, + {BTN_TYPE_KB ,0x08 ,"KEY_F13" }, + {BTN_TYPE_KB ,0x10 ,"KEY_F14" }, + {BTN_TYPE_KB ,0x18 ,"KEY_F15" }, + {BTN_TYPE_KB ,0x0E ,"KEY_GRAVE" },// backtick + {BTN_TYPE_KB ,0x16 ,"KEY_1" }, + {BTN_TYPE_KB ,0x1E ,"KEY_2" }, + {BTN_TYPE_KB ,0x26 ,"KEY_3" }, + {BTN_TYPE_KB ,0x25 ,"KEY_4" }, + {BTN_TYPE_KB ,0x2E ,"KEY_5" }, + {BTN_TYPE_KB ,0x36 ,"KEY_6" }, + {BTN_TYPE_KB ,0x3D ,"KEY_7" }, + {BTN_TYPE_KB ,0x3E ,"KEY_8" }, + {BTN_TYPE_KB ,0x46 ,"KEY_9" }, + {BTN_TYPE_KB ,0x45 ,"KEY_0" }, + {BTN_TYPE_KB ,0x4E ,"KEY_MINUS" },// hyphen + {BTN_TYPE_KB ,0x55 ,"KEY_EQUAL" }, + {BTN_TYPE_KB ,0x66 ,"KEY_BACKSPACE" }, + {BTN_TYPE_KB ,0x0D ,"KEY_TAB" }, + {BTN_TYPE_KB ,0x15 ,"KEY_Q" }, + {BTN_TYPE_KB ,0x1D ,"KEY_W" }, + {BTN_TYPE_KB ,0x24 ,"KEY_E" }, + {BTN_TYPE_KB ,0x2D ,"KEY_R" }, + {BTN_TYPE_KB ,0x2C ,"KEY_T" }, + {BTN_TYPE_KB ,0x35 ,"KEY_Y" }, + {BTN_TYPE_KB ,0x3C ,"KEY_U" }, + {BTN_TYPE_KB ,0x43 ,"KEY_I" }, + {BTN_TYPE_KB ,0x44 ,"KEY_O" }, + {BTN_TYPE_KB ,0x4D ,"KEY_P" }, + {BTN_TYPE_KB ,0x54 ,"KEY_LEFTBRACE" }, + {BTN_TYPE_KB ,0x5B ,"KEY_RIGHTBRACE" }, + {BTN_TYPE_KB ,0x5D ,"KEY_BACKSLASH" }, + {BTN_TYPE_KB ,0x58 ,"KEY_CAPSLOCK" }, + {BTN_TYPE_KB ,0x1C ,"KEY_A" }, + {BTN_TYPE_KB ,0x1B ,"KEY_S" }, + {BTN_TYPE_KB ,0x23 ,"KEY_D" }, + {BTN_TYPE_KB ,0x2B ,"KEY_F" }, + {BTN_TYPE_KB ,0x34 ,"KEY_G" }, + {BTN_TYPE_KB ,0x33 ,"KEY_H" }, + {BTN_TYPE_KB ,0x3B ,"KEY_J" }, + {BTN_TYPE_KB ,0x42 ,"KEY_K" }, + {BTN_TYPE_KB ,0x4B ,"KEY_L" }, + {BTN_TYPE_KB ,0x4C ,"KEY_SEMICOLON" }, + {BTN_TYPE_KB ,0x52 ,"KEY_APOSTROPHE" }, + {BTN_TYPE_KB ,0x5A ,"KEY_ENTER" }, + {BTN_TYPE_KB ,0x88 ,"KEY_LEFTSHIFT" }, + {BTN_TYPE_KB ,0x1A ,"KEY_Z" }, + {BTN_TYPE_KB ,0x22 ,"KEY_X" }, + {BTN_TYPE_KB ,0x21 ,"KEY_C" }, + {BTN_TYPE_KB ,0x2A ,"KEY_V" }, + {BTN_TYPE_KB ,0x32 ,"KEY_B" }, + {BTN_TYPE_KB ,0x31 ,"KEY_N" }, + {BTN_TYPE_KB ,0x3A ,"KEY_M" }, + {BTN_TYPE_KB ,0x41 ,"KEY_COMMA" }, + {BTN_TYPE_KB ,0x49 ,"KEY_DOT" }, + {BTN_TYPE_KB ,0x4A ,"KEY_SLASH" }, + {BTN_TYPE_KB ,0x89 ,"KEY_RIGHTSHIFT" }, + {BTN_TYPE_KB ,0x82 ,"KEY_LEFTMETA" }, + {BTN_TYPE_KB ,0x8A ,"KEY_LEFTALT" }, + {BTN_TYPE_KB ,0x29 ,"KEY_SPACE" }, + {BTN_TYPE_KB ,0x8B ,"KEY_RIGHTALT" }, + {BTN_TYPE_KB ,0x84 ,"KEY_COMPOSE" }, + {BTN_TYPE_KB ,0x8D ,"KEY_RIGHTCTRL" }, + {BTN_TYPE_KB ,0xC3 ,"KEY_SYSRQ" }, + {BTN_TYPE_KB ,0x7E ,"KEY_SCROLLLOCK" }, + {BTN_TYPE_KB ,0x91 ,"KEY_PAUSE" }, + {BTN_TYPE_KB ,0xC2 ,"KEY_INSERT" }, + {BTN_TYPE_KB ,0x94 ,"KEY_HOME" }, + {BTN_TYPE_KB ,0x96 ,"KEY_PAGEUP" }, + {BTN_TYPE_KB ,0xC0 ,"KEY_DELETE" }, + {BTN_TYPE_KB ,0x95 ,"KEY_END" }, + {BTN_TYPE_KB ,0x97 ,"KEY_PAGEDOWN" }, + {BTN_TYPE_KB ,0x98 ,"KEY_UP" }, + {BTN_TYPE_KB ,0x99 ,"KEY_DOWN" }, + {BTN_TYPE_KB ,0x9A ,"KEY_LEFT" }, + {BTN_TYPE_KB ,0x9B ,"KEY_RIGHT" }, + +/* Numpad button codes */ + {BTN_TYPE_KB ,0x77 ,"KEY_NUMLOCK" }, + {BTN_TYPE_KB ,0x90 ,"KEY_KPSLASH" }, + {BTN_TYPE_KB ,0x7C ,"KEY_KPASTERISK" }, + {BTN_TYPE_KB ,0x7B ,"KEY_KPMINUS" }, + {BTN_TYPE_KB ,0x70 ,"KEY_KP0" }, + {BTN_TYPE_KB ,0x69 ,"KEY_KP1" }, + {BTN_TYPE_KB ,0x72 ,"KEY_KP2" }, + {BTN_TYPE_KB ,0x7A ,"KEY_KP3" }, + {BTN_TYPE_KB ,0x6B ,"KEY_KP4" }, + {BTN_TYPE_KB ,0x73 ,"KEY_KP5" }, + {BTN_TYPE_KB ,0x74 ,"KEY_KP6" }, + {BTN_TYPE_KB ,0x6C ,"KEY_KP7" }, + {BTN_TYPE_KB ,0x75 ,"KEY_KP8" }, + {BTN_TYPE_KB ,0x7D ,"KEY_KP9" }, + {BTN_TYPE_KB ,0x79 ,"KEY_KPPLUS" }, + {BTN_TYPE_KB ,0x81 ,"KEY_KPENTER" }, + {BTN_TYPE_KB ,0x71 ,"KEY_KPDOT" }, + +/* Mouse button codes */ + {BTN_TYPE_MOUSE ,0x01 ,"BTN_LEFT" }, + {BTN_TYPE_MOUSE ,0x02 ,"BTN_RIGHT" }, + {BTN_TYPE_MOUSE ,0x03 ,"BTN_MIDDLE" }, + {BTN_TYPE_MOUSE ,0x04 ,"REL_WHEEL_HI_RES_UP" }, + {BTN_TYPE_MOUSE ,0x05 ,"REL_WHEEL_HI_RES_DOWN" }, + +/* Media button codes */ + {BTN_TYPE_MEDIA ,0x16 ,"MEDIA_SCREENSHOT" }, + {BTN_TYPE_MEDIA ,0x19 ,"MEDIA_SHOW_KEYBOARD" }, + {BTN_TYPE_MEDIA ,0x1C ,"MEDIA_SHOW_DESKTOP" }, + {BTN_TYPE_MEDIA ,0x1E ,"MEDIA_START_RECORDING" },
Quoted 8 lines
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1288
Quoted 6 lines
drivers/hid/hid-asus.c +6
-0+ { BTN_TYPE_MEDIA, 0x01, "MEDIA_MIC_OFF" }, + { BTN_TYPE_MEDIA, 0x02, "MEDIA_VOL_DOWN" }, + { BTN_TYPE_MEDIA, 0x03, "MEDIA_VOL_UP" }, +}; + +static const size_t keymap_len =ARRAY_SIZE (ally_btn_codes);Why is this a variable???? Can't the users just directly use
ARRAY_SIZEon the array????
Not understood: you say variable, but both are const(s), isn't const the exact opposite of variable?
I am stepping away for today, but I have cleaned up much of this mess.
Quoted 1 line
At v11 it's odd to find this much garbage still remaining... :-(
At a certain (late) point of the development I noticed that remapping buttons on the X applied immediately, while on my Ally 1 it had to go through apply_all old sysfs interface, so I attempted to unify them late on development, sorry.
Quoted 999 lines
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1290
Quoted 1 line
drivers/hid/hid-asus.c +1
-0+static const struct btn_code_map *find_button_by_name (const char *name);Could you reorder the code such that this is not necessary. Having the code here would be more logical as well because that relates to the array.
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1297
Quoted 7 lines
drivers/hid/hid-asus.c +7
-0+ +static void ally_set_default_gamepad_mapping (struct ally_button_mapping *map) +{ +int i; + +/* Set all pair indexes and initialize to NONE */ +for (i =0 ; i <9 ; i++) {
ARRAY_SIZE()drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1306
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+ map->button_pairs[i].pair_index = i +1 ; + map->button_pairs[i].first.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].first.macro = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.macro = +find_button_by_name ("NONE" );Why does this have to search for the NONE button multiple times?
Quoted 54 lines
drivers/hid/hid-asus.c +54
-0+ } + + map->button_pairs[BTN_PAIR_AB -1 ].first.remap = +find_button_by_name ("BTN_SOUTH" ); + map->button_pairs[BTN_PAIR_AB -1 ].second.remap = +find_button_by_name ("BTN_EAST" ); + + map->button_pairs[BTN_PAIR_XY -1 ].first.remap = +find_button_by_name ("BTN_NORTH" ); + map->button_pairs[BTN_PAIR_XY -1 ].second.remap = +find_button_by_name ("BTN_WEST" ); + + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].first.remap = +find_button_by_name ("BTN_TL" ); + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].second.remap = +find_button_by_name ("BTN_TR" ); + + map->button_pairs[BTN_PAIR_STICK_LR -1 ].first.remap = +find_button_by_name ("BTN_THUMBL" ); + map->button_pairs[BTN_PAIR_STICK_LR -1 ].second.remap = +find_button_by_name ("BTN_THUMBR" ); + + map->button_pairs[BTN_PAIR_DPAD_UPDOWN -1 ].first.remap = +find_button_by_name ("ABS_HAT0Y_NEG" ); + map->button_pairs[BTN_PAIR_DPAD_UPDOWN -1 ].second.remap = +find_button_by_name ("ABS_HAT0Y_POS" ); + + map->button_pairs[BTN_PAIR_DPAD_LEFTRIGHT -1 ].first.remap = +find_button_by_name ("ABS_HAT0X_NEG" ); + map->button_pairs[BTN_PAIR_DPAD_LEFTRIGHT -1 ].second.remap = +find_button_by_name ("ABS_HAT0X_POS" ); + + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].first.remap = +find_button_by_name ("ABS_Z" ); + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].second.remap = +find_button_by_name ("ABS_RZ" ); + + map->button_pairs[BTN_PAIR_VIEW_MENU -1 ].first.remap = +find_button_by_name ("BTN_SELECT" ); + map->button_pairs[BTN_PAIR_VIEW_MENU -1 ].second.remap = +find_button_by_name ("BTN_START" ); + + map->button_pairs[BTN_PAIR_M1M2 -1 ].first.remap = +find_button_by_name ("FN_M2" ); + map->button_pairs[BTN_PAIR_M1M2 -1 ].second.remap = +find_button_by_name ("FN_M1" ); +} + +static void ally_set_default_keyboard_mapping (struct ally_button_mapping *map) +{ +int i; + +/* Set all pair indexes and initialize to NONE */ +for (i =0 ; i <9 ; i++) {drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1370
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ map->button_pairs[i].pair_index = i +1 ; + map->button_pairs[i].first.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].first.macro = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.remap = +find_button_by_name ("NONE" ); + map->button_pairs[i].second.macro = +find_button_by_name ("NONE" ); + }Duplicated code with the very same problems!
Quoted 94 lines
drivers/hid/hid-asus.c +72
-1+/* +* Desktop mode defaults: keyboard and mouse mappings. +* Buttons not listed here (dpad, view, menu) remain NONE. +*/ + +/* btn_a => KEY_SPACE, btn_b => KEY_E */ + map->button_pairs[BTN_PAIR_AB -1 ].first.remap = +find_button_by_name ("KEY_SPACE" ); + map->button_pairs[BTN_PAIR_AB -1 ].second.remap = +find_button_by_name ("KEY_E" ); + +/* btn_x => KEY_R, btn_y => KEY_F */ + map->button_pairs[BTN_PAIR_XY -1 ].first.remap = +find_button_by_name ("KEY_R" ); + map->button_pairs[BTN_PAIR_XY -1 ].second.remap = +find_button_by_name ("KEY_F" ); + +/* LB => wheel up, RB => wheel down */ + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].first.remap = +find_button_by_name ("REL_WHEEL_HI_RES_UP" ); + map->button_pairs[BTN_PAIR_BUMPER_LR -1 ].second.remap = +find_button_by_name ("REL_WHEEL_HI_RES_DOWN" ); + +/* left stick click => left click, right stick click => left shift */ + map->button_pairs[BTN_PAIR_STICK_LR -1 ].first.remap = +find_button_by_name ("BTN_LEFT" ); + map->button_pairs[BTN_PAIR_STICK_LR -1 ].second.remap = +find_button_by_name ("KEY_LEFTSHIFT" ); + +/* LT => left click, RT => right click */ + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].first.remap = +find_button_by_name ("BTN_LEFT" ); + map->button_pairs[BTN_PAIR_TRIGGER_LR -1 ].second.remap = +find_button_by_name ("BTN_RIGHT" ); + +/* M2 => FN_M2, M1 => FN_M1 */ + map->button_pairs[BTN_PAIR_M1M2 -1 ].first.remap = +find_button_by_name ("FN_M2" ); + map->button_pairs[BTN_PAIR_M1M2 -1 ].second.remap = +find_button_by_name ("FN_M1" ); +} +static ssize_t gamepad_mode_show (struct device *dev,struct device_attribute *attr,char *buf) {struct hid_device *hdev =to_hid_device (dev);@@ -1113,7 +1452,7 @@ static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *astruct ally_config *cfg;u8 mode_byte;int mode; -int ret; +int ret, i;if (!ally)return -ENODEV ;@@ -1141,6 +1480,33 @@ static ssize_t gamepad_mode_store(struct device *dev, struct device_attribute *a cfg->gamepad_mode = mode_byte; +/* +* When the gamepad mode changes, the hardware automatically +* resets all button mappings to its internal defaults for the +* new mode. +* +* Refresh the software cache so that subsequent +* reads from sysfs reflect what the firmware is actually +* using. +*/ +if (mode_byte ==ALLY_GAMEPAD_MODE_GAMEPAD ) +ally_set_default_gamepad_mapping (&cfg->button_mappings); +else if (mode_byte ==ALLY_GAMEPAD_MODE_KEYBOARD ) +ally_set_default_keyboard_mapping (&cfg->button_mappings); + +/* +* The re-defaulted mappings are what the remap and macro files +* report: wake anyone polling them about the new values. +*/ +if (cfg->button_entries) { +for (i =0 ; i <ALLY_BTN_MAX ; i++) { +sysfs_notify (&dev->kobj, cfg->button_entries[i].group.name, +"remap" ); +sysfs_notify (&dev->kobj, cfg->button_entries[i].group.name, +"macro" ); + } + } +hid_dbg (hdev,"Set gamepad mode to %s\n" , ally_gamepad_mode_text[mode]);sysfs_notify (&dev->kobj,NULL ,"gamepad_mode" );@@ -2815,6 +3181,575 @@ static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) entry->attrs[4 ] =NULL ; } +/** +* find_button_by_name() - Find a button code map by its name drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3195
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* @name: name of the button code to look up +* +* Return: the matching button code map, or NULL if not found +*/ +static const struct btn_code_map *find_button_by_name (const char *name) +{ +int i; + +for (i =0 ; i < keymap_len; i++) { +if (strcmp (ally_btn_codes[i].name, name) ==0 )!strcmp()
You probably should check if you've the include for
strcmp(), could be necessary for some other patch too, I've lost track what includes have been added so far.Quoted 51 lines
drivers/hid/hid-asus.c +51
-0+return &ally_btn_codes[i]; + } + +return NULL ; +} + +/** +* ally_remap_code_valid() - Check if a remap target is valid for a button +* @btn: button to remap +* @gamepad_mode: gamepad mode to validate the remap against +* @code: remap target to check +* @is_macro: whether the target is assigned to the macro mapping +* +* M1 and M2 can be remapped to anything at any time. +* All other buttons may only be remapped to gamepad buttons (BTN_TYPE_PAD) +* when in gamepad mode, and to keyboard/mouse/media codes when in desktop +* (keyboard) mode. BTN_TYPE_NONE (unmapped) is always allowed. +* Macro mappings are executed by the MCU as input sequences on the virtual +* keyboard and are accepted regardless of the current gamepad mode. +* +* Return: true if the remap is valid, false otherwise +*/ +static bool ally_remap_code_valid (enum ally_button_id btn,u8 gamepad_mode, +const struct btn_code_map *code,bool is_macro) +{ +if (!code) +return false; + +/* M1 and M2 can be remapped to everything */ +if (btn ==ALLY_BTN_M1 || btn ==ALLY_BTN_M2 ) +return true; + +/* NONE is always valid */ +if (code->type ==BTN_TYPE_NONE ) +return true; + +/* Macros are not restricted by the controller emulation mode */ +if (is_macro) +return true; + +if (gamepad_mode ==ALLY_GAMEPAD_MODE_GAMEPAD ) +return code->type ==BTN_TYPE_PAD ; + +/* Desktop / keyboard mode: allow keyboard, mouse and media */ +return code->type ==BTN_TYPE_KB || + code->type ==BTN_TYPE_MOUSE || + code->type ==BTN_TYPE_MEDIA ; +} + +/** +* ally_set_button_mapping() - Set the button mapping for a button pair drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3256
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* @hdev: HID device +* @ally: ally handheld structure +* @mapping: button pair mapping to send to the device +* +* Return: count of data transferred, negative if error +*/ +static int ally_set_button_mapping (struct hid_device *hdev,struct ally_handheld *ally, +struct button_pair_map *mapping) +{ +/* The MCU mapping block is four consecutive 11-byte entries starting at Inconsistent multi-line comment style.
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3258
Quoted 2 lines
drivers/hid/hid-asus.c +2
-0+* buf[5]: first remap 5-15, first macro 16-26, second remap 27-37, +* second macro 38-48 (see hid-asus-ally __btn_pair_to_pkt, BTN_CODE_LEN). I didn't find
__btn_pair_to_pktfrom anywhere?? NorBTN_CODE_LEN??drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3261
Quoted 3 lines
drivers/hid/hid-asus.c +3
-0+*/ +u8 macro_bytes[11 ] = {0 }; +u8 btn_bytes[11 ] = {0 };= {} initializes to default values which is 0 for u8.
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3271
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ +if (!mapping || !mapping->first.remap || !mapping->first.macro || + !mapping->second.remap || !mapping->second.macro) +return -EINVAL ; + + u8 *buf __free(kfree) = ally_alloc_cmd(CMD_SET_MAPPING, NULL, 0); +if (!buf) +return -ENOMEM ; + +/* This packet is slightly different from the other Comment style.
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3276
Quoted 5 lines
drivers/hid/hid-asus.c +5
-0+* as before the packet length there is an extra byte +* which is the pair index. +*/ + buf[3 ] = mapping->pair_index; + buf[4 ] =0x2C ;/* Length */ Use named define or
sizeof()(if viable).drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3285
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+ +/* First button mapping */ + buf[5 ] = mapping->first.remap->type; +/* Fill in bytes 6-14 with button code */ +if (mapping->first.remap->type) { +memset (btn_bytes,0 ,sizeof (btn_bytes)); + btn_bytes[0 ] = mapping->first.remap->type; + +/* Value byte position depends on type: pad=1, kb=2, media=3, Comment style.
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+* mouse=4 (see hid-asus-ally BTN_CODE definitions). +*/ +switch (mapping->first.remap->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + btn_bytes[1 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_KB :drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3304
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ btn_bytes[2 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_MEDIA : + btn_bytes[3 ] = mapping->first.remap->value; +break ; +case BTN_TYPE_MOUSE : + btn_bytes[4 ] = mapping->first.remap->value; +break ; + } +memcpy (&buf[5 ], btn_bytes,11 );Magic 11. Please name properly.
I wonder though if there should be helpers for these with a few parameters as these seem mostly just duplicated multiple times.
Quoted 15 lines
drivers/hid/hid-asus.c +15
-0+ } + +/* Macro mapping for first button if any */ + buf[16 ] = mapping->first.macro->type; +if (mapping->first.macro->type) { +memset (macro_bytes,0 ,sizeof (macro_bytes)); + macro_bytes[0 ] = mapping->first.macro->type; + +switch (mapping->first.macro->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + macro_bytes[1 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_KB :drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3329
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ macro_bytes[2 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_MEDIA : + macro_bytes[3 ] = mapping->first.macro->value; +break ; +case BTN_TYPE_MOUSE : + macro_bytes[4 ] = mapping->first.macro->value; +break ; + } +memcpy (&buf[16 ], macro_bytes,11 );Magic 11.
Quoted 16 lines
drivers/hid/hid-asus.c +16
-0+ } + +/* Second button mapping */ + buf[27 ] = mapping->second.remap->type; +/* Fill in bytes 28-36 with button code */ +if (mapping->second.remap->type) { +memset (btn_bytes,0 ,sizeof (btn_bytes)); + btn_bytes[0 ] = mapping->second.remap->type; + +switch (mapping->second.remap->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + btn_bytes[1 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_KB :drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3355
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ btn_bytes[2 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_MEDIA : + btn_bytes[3 ] = mapping->second.remap->value; +break ; +case BTN_TYPE_MOUSE : + btn_bytes[4 ] = mapping->second.remap->value; +break ; + } +memcpy (&buf[27 ], btn_bytes,11 );Ditto.
Quoted 15 lines
drivers/hid/hid-asus.c +15
-0+ } + +/* Macro mapping for second button if any */ + buf[38 ] = mapping->second.macro->type; +if (mapping->second.macro->type) { +memset (macro_bytes,0 ,sizeof (macro_bytes)); + macro_bytes[0 ] = mapping->second.macro->type; + +switch (mapping->second.macro->type) { +case BTN_TYPE_NONE : +break ; +case BTN_TYPE_PAD : + macro_bytes[1 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_KB :drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3380
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+ macro_bytes[2 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_MEDIA : + macro_bytes[3 ] = mapping->second.macro->value; +break ; +case BTN_TYPE_MOUSE : + macro_bytes[4 ] = mapping->second.macro->value; +break ; + } +memcpy (&buf[38 ], macro_bytes,11 );Ditto.
Quoted 156 lines
drivers/hid/hid-asus.c +156
-0+ } + +return ally_gamepad_send_packet (ally, hdev, buf,ROG_ALLY_REPORT_SIZE ); +} + +/** +* ally_apply_button_mappings() - Push the recorded mappings of the current +* gamepad mode to the controller +* @hdev: HID device +* @ally: ally handheld structure +* @cfg: ally config structure +* +* An MCU reset restores the firmware default mappings: push the recorded +* ones again so that the hardware keeps matching what userspace +* configured. The caller must hold cfg->config_mutex, exactly like the +* sysfs store callbacks that update single pairs. +*/ +static void ally_apply_button_mappings (struct hid_device *hdev, +struct ally_handheld *ally, +struct ally_config *cfg) +{ +struct ally_button_mapping *mode_map; +int i, ret; + + mode_map = &cfg->button_mappings; + +for (i =0 ; i <ARRAY_SIZE (mode_map->button_pairs); i++) { + mode_map->button_pairs[i].pair_index = i +1 ; + + ret =ally_set_button_mapping (hdev, ally, + &mode_map->button_pairs[i]); +if (ret <0 ) { +hid_warn (hdev, +"Failed to restore button mappings: %d\n" , + ret); +return ; + } + } +} + +/** +* get_button_pair_info() - Get the pair index and position of a button +* @button_id: button to look up +* @pair_idx: output pointer for the pair index of the button +* @is_first: output pointer for the position of the button within the pair +* +* Return: 0 on success, negative errno on failure +*/ +static int get_button_pair_info (enum ally_button_id button_id, +enum btn_pair_index *pair_idx, +bool *is_first) +{ +switch (button_id) { +case ALLY_BTN_DU : + *pair_idx =BTN_PAIR_DPAD_UPDOWN ; + *is_first = true; +break ; +case ALLY_BTN_DD : + *pair_idx =BTN_PAIR_DPAD_UPDOWN ; + *is_first = false; +break ; +case ALLY_BTN_DL : + *pair_idx =BTN_PAIR_DPAD_LEFTRIGHT ; + *is_first = true; +break ; +case ALLY_BTN_DR : + *pair_idx =BTN_PAIR_DPAD_LEFTRIGHT ; + *is_first = false; +break ; +case ALLY_BTN_J0B : + *pair_idx =BTN_PAIR_STICK_LR ; + *is_first = true; +break ; +case ALLY_BTN_J1B : + *pair_idx =BTN_PAIR_STICK_LR ; + *is_first = false; +break ; +case ALLY_BTN_LB : + *pair_idx =BTN_PAIR_BUMPER_LR ; + *is_first = true; +break ; +case ALLY_BTN_RB : + *pair_idx =BTN_PAIR_BUMPER_LR ; + *is_first = false; +break ; +case ALLY_BTN_A : + *pair_idx =BTN_PAIR_AB ; + *is_first = true; +break ; +case ALLY_BTN_B : + *pair_idx =BTN_PAIR_AB ; + *is_first = false; +break ; +case ALLY_BTN_X : + *pair_idx =BTN_PAIR_XY ; + *is_first = true; +break ; +case ALLY_BTN_Y : + *pair_idx =BTN_PAIR_XY ; + *is_first = false; +break ; +case ALLY_BTN_VIEW : + *pair_idx =BTN_PAIR_VIEW_MENU ; + *is_first = true; +break ; +case ALLY_BTN_MENU : + *pair_idx =BTN_PAIR_VIEW_MENU ; + *is_first = false; +break ; +case ALLY_BTN_M1 : + *pair_idx =BTN_PAIR_M1M2 ; + *is_first = false; +break ; +case ALLY_BTN_M2 : + *pair_idx =BTN_PAIR_M1M2 ; + *is_first = true; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static ssize_t button_remap_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct button_remap_attr *btn_attr =to_button_remap_attr (attr); +struct ally_handheld *ally = btn_attr->ally; +const struct btn_code_map *code; +struct ally_config *cfg; +enum ally_button_id button_id = btn_attr->button_id; +enum btn_pair_index pair_idx; +struct button_pair_map *pair; +struct button_map *btn_map; +bool is_first; +int ret; + +if (!ally) +return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + + ret =get_button_pair_info (button_id, &pair_idx, &is_first); +if (ret <0 ) +return ret; + +guard (mutex)(&cfg->config_mutex); + + pair = &cfg->button_mappings.button_pairs[pair_idx -1 ]; + btn_map = is_first ? &pair->first : &pair->second; + code = btn_attr->is_macro ? btn_map->macro : btn_map->remap; + +/* drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3546
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+* The mappings are never NULL: the defaults, the mode-switch +* re-defaults and the store rollback all guarantee it. A miss +* means a broken default table, so fail loudly instead of +* dereferencing. +*/ +if (!code) +return -EIO ; + +if (code->type ==BTN_TYPE_NONE ) +return sysfs_emit (buf,"NONE\n" );Wouldn't it emit "NONE" anyway with the normal return so why you need special print for this separately?
Quoted 25 lines
drivers/hid/hid-asus.c +25
-0+ +return sysfs_emit (buf,"%s\n" , code->name); +} + +static ssize_t button_remap_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct hid_device *hdev =to_hid_device (dev); +struct button_remap_attr *btn_attr =to_button_remap_attr (attr); +struct ally_handheld *ally = btn_attr->ally; +enum ally_button_id button_id = btn_attr->button_id; +struct ally_button_mapping *mode_map; +const struct btn_code_map *code; +const struct btn_code_map *old_code; +enum btn_pair_index pair_idx; +struct button_pair_map *pair; +struct button_map *btn_map; +struct ally_config *cfg; +char btn_name[32 ]; +bool is_first; +bool undo_hw; +int ret, i; + +if (!ally)drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3581
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+return -ENODEV ; + + cfg =ally_get_config (ally); +if (!cfg) +return -ENODEV ; + +if (sscanf (buf,"%31s" , btn_name) !=1 ) +return -EINVAL ; + +/* Handle "NONE" specially */ Yeah yeah, and now you better must explain me why???? This is totally useless comment and helps zero to explain why the code is making things in a stupid way.
Won't
find_button_by_name()return the very same entry???(You can see probably I'm starting to get tired of reading this kind of things at this point... ...I've read quite many lines by now.)
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3582
Quoted 1 line
drivers/hid/hid-asus.c +1
-0+if (strcmp (btn_name,"NONE" ) ==0 ) {!strcmp(), but it looks totally useless check (see above).
Quoted 37 lines
drivers/hid/hid-asus.c +37
-0+ code = &ally_btn_codes[0 ];/* NONE entry */ + }else { + code =find_button_by_name (btn_name); +if (!code) +return -EINVAL ; + } + + ret =get_button_pair_info (button_id, &pair_idx, &is_first); +if (ret <0 ) +return ret; + +guard (mutex)(&cfg->config_mutex); + +/* +* Validate the remap target while holding the lock: the +* gamepad mode can change concurrently, and the check must +* be done against the very mode the mapping is written to. +*/ +if (!ally_remap_code_valid (button_id, cfg->gamepad_mode, code, + btn_attr->is_macro)) +return -EINVAL ; + +/* Access the mapping for current gamepad mode */ + pair = &cfg->button_mappings.button_pairs[pair_idx -1 ]; + btn_map = is_first ? &pair->first : &pair->second; + +if (btn_attr->is_macro) { + old_code = btn_map->macro; + btn_map->macro = code; + }else { + old_code = btn_map->remap; + btn_map->remap = code; + } + +/* Update pair index */ + pair->pair_index = pair_idx; +drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3629
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+/* +* Send mapping to device with the caveat that first +* generation devices require individual button pair updates. +*/ +if (cfg->is_ally_x) { + ret =ally_set_button_mapping (hdev, ally, pair); + undo_hw = false; + }else { + mode_map = &cfg->button_mappings; + ret =0 ;Is this required?
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3630
Quoted 1 line
drivers/hid/hid-asus.c +1
-0+for (i =0 ; i <9 ; i++) {
ARRAY_SIZE()drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3635
Quoted 5 lines
drivers/hid/hid-asus.c +5
-0+ mode_map->button_pairs[i].pair_index = i +1 ; + ret =ally_set_button_mapping (hdev, ally, + &mode_map->button_pairs[i]); +if (ret <0 ) +break ;IMO, it would be better to use goto to deal with these errors (here and above in the if blcok).
drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3644
Quoted 9 lines
drivers/hid/hid-asus.c +9
-0+ } + +/* +* Pairs are pushed one at a time: remember whether +* the target pair reached the hardware before a +* later pair failed, so the rollback below can undo +* it on the device too. +*/ + undo_hw = ret <0 && i > pair_idx -1 ;Do this above before goto.
Quoted 18 lines
drivers/hid/hid-asus.c +18
-0+ } + +/* +* The hardware rejected the update: restore the previous +* mapping so that the software cache keeps describing the +* state the device is actually in. +*/ +if (ret <0 ) { +if (btn_attr->is_macro) + btn_map->macro = old_code; +else + btn_map->remap = old_code; + +/* +* The target pair was already committed before a +* later pair failed: push the restored mapping once +* more so the hardware matches the cache again. Best +* effort only: the device is misbehaving already. drivers/hid/hid-asus.c static void ally_btn_turbo_init_attrs(struct ally_btn_sysfs_entry *entry) · line 3672
Quoted 10 lines
drivers/hid/hid-asus.c +10
-0+*/ +if (undo_hw) +ally_set_button_mapping (hdev, ally, pair); + +hid_err (hdev,"Failed to set button mapping: %d\n" , ret); +return ret; + } + +sysfs_notify (&dev->kobj, ally_button_names[btn_attr->button_id], + btn_attr->is_macro ?"macro" :"remap" );I suggest adding a local
string_choices.hstylestr_macro_remap()for this as it seems to occur a few time.271 lines not shown · raw message
Part of this message is not shown here. View the raw message.
Ilpo Järvinen
maintainerRe: [PATCH v11 11/18] HID: asus: add support for btn remapping
Quoted 281 lines · Denis Benato
Quoted 270 lines · Ilpo Järvinen
Quoted 268 lines · Denis Benato
Add support for buttons remapping: the ability to assign different button events to each one of the physical button on the device.
Each button exposes a
remap_indexsysfs attribute listing the valid remap target names for the current gamepad mode: M1 and M2 can be remapped to anything at any time, while other buttons may only be remapped to gamepad codes in gamepad mode and keyboard/mouse/media codes in desktop mode.When the gamepad mode is changed the firmware automatically restores its internal defaults, so the software cache is refreshed to reflect the hardware state.
Signed-off-by Luke Jones <[email protected]>Signed-off-by Denis Benato <[email protected]>Signed-off-by Jonathan LoBue <[email protected]>--- drivers/hid/hid-asus.c | 1059 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 1037 insertions(+), 22 deletions(-)drivers/hid/hid-asus.c +209
-1@@ -275,9 +275,25 @@ struct ally_turbo_config {struct ally_btn_turbo_params btn_m1; }; +/* Button remap attribute structure */ +struct button_remap_attr { +struct device_attribute dev_attr; +enum ally_button_id button_id; +bool is_macro; +/* +* The attributes are created while hid_asus_ally_probe() is still +* running, before asus_probe() publishes drvdata->rog_ally: carry +* the pointer like the turbo entries do, so the callbacks work +* from the moment the files exist. +*/ +struct ally_handheld *ally; +}; + +#define to_button_remap_attr (x) container_of(x, struct button_remap_attr, dev_attr) +struct ally_btn_sysfs_entry {struct attribute_group group; -struct attribute *attrs[5 ];/* turbo_period + toggle_period + ranges + NULL */ +struct attribute *attrs[8 ];/* turbo + ranges + remap + macro + remap_index + NULL */ /* * The entries are created while hid_asus_ally_probe() is still running, * before asus_probe() publishes drvdata->rog_ally: carrying the shared @@ -289,6 +305,9 @@ struct ally_btn_sysfs_entry {enum ally_button_id btn;struct device_attribute attr_turbo_period;struct device_attribute attr_toggle_period; +struct button_remap_attr *remap_attr; +struct button_remap_attr *macro_attr; +struct device_attribute attr_remap_index; };enum ally_gamepad_mode_index {@@ -296,6 +315,35 @@ enum ally_gamepad_mode_index { ALLY_GAMEPAD_MODE_KEYBOARD = 0x02, }; +/* Button pair indexes for mapping commands */ +enum btn_pair_index { +BTN_PAIR_DPAD_UPDOWN =0x01 , +BTN_PAIR_DPAD_LEFTRIGHT =0x02 , +BTN_PAIR_STICK_LR =0x03 , +BTN_PAIR_BUMPER_LR =0x04 , +BTN_PAIR_AB =0x05 , +BTN_PAIR_XY =0x06 , +BTN_PAIR_VIEW_MENU =0x07 , +BTN_PAIR_M1M2 =0x08 , +BTN_PAIR_TRIGGER_LR =0x09 , +}; + +struct button_map { +const struct btn_code_map *remap; +const struct btn_code_map *macro; +}; + +struct button_pair_map { +enum btn_pair_index pair_index; +struct button_map first; +struct button_map second; +}; + +/* Store button mapping per gamepad mode */ +struct ally_button_mapping { +struct button_pair_map button_pairs[9 ];/* 9 button pairs */ +}; +struct ally_config {/* Must be locked if the data is being changed */ struct mutex config_mutex;@@ -331,6 +379,8 @@ struct ally_config {struct ally_turbo_config turbo;struct ally_btn_sysfs_entry *button_entries; +/* Button mappings of the current gamepad mode */ +struct ally_button_mapping button_mappings;struct ally_joystick_resp_curve left_curve;struct ally_joystick_resp_curve right_curve;@@ -1072,6 +1122,295 @@ static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device *return 0 ; } +enum btn_map_type { +BTN_TYPE_NONE =0 , +BTN_TYPE_PAD =0x01 , +BTN_TYPE_KB =0x02 , +BTN_TYPE_MOUSE =0x03 , +BTN_TYPE_MEDIA =0x05 , +}; + +struct btn_code_map { +unsigned char type; +unsigned char value; +const char *name; +}; + +static const struct btn_code_map ally_btn_codes[] = { + {BTN_TYPE_NONE ,0x00 ,"NONE" }, +/* Gamepad button codes */ + {BTN_TYPE_PAD ,0x01 ,"BTN_SOUTH" }, + {BTN_TYPE_PAD ,0x02 ,"BTN_EAST" }, + {BTN_TYPE_PAD ,0x03 ,"BTN_NORTH" }, + {BTN_TYPE_PAD ,0x04 ,"BTN_WEST" }, + {BTN_TYPE_PAD ,0x05 ,"BTN_TL" }, + {BTN_TYPE_PAD ,0x06 ,"BTN_TR" }, + {BTN_TYPE_PAD ,0x07 ,"BTN_THUMBL" }, + {BTN_TYPE_PAD ,0x08 ,"BTN_THUMBR" }, + {BTN_TYPE_PAD ,0x09 ,"ABS_HAT0Y_NEG" }, + {BTN_TYPE_PAD ,0x0A ,"ABS_HAT0Y_POS" }, + {BTN_TYPE_PAD ,0x0B ,"ABS_HAT0X_NEG" }, + {BTN_TYPE_PAD ,0x0C ,"ABS_HAT0X_POS" }, + {BTN_TYPE_PAD ,0x0D ,"ABS_Z" }, + {BTN_TYPE_PAD ,0x0E ,"ABS_RZ" }, + {BTN_TYPE_PAD ,0x11 ,"BTN_SELECT" }, + {BTN_TYPE_PAD ,0x12 ,"BTN_START" }, + {BTN_TYPE_PAD ,0x13 ,"BTN_MODE" }, + +/* Keyboard button codes */ + {BTN_TYPE_KB ,0x8E ,"FN_M2" }, + {BTN_TYPE_KB ,0x8F ,"FN_M1" }, + {BTN_TYPE_KB ,0x76 ,"KEY_ESC" }, + {BTN_TYPE_KB ,0x05 ,"KEY_F1" }, + {BTN_TYPE_KB ,0x06 ,"KEY_F2" }, + {BTN_TYPE_KB ,0x04 ,"KEY_F3" }, + {BTN_TYPE_KB ,0x0C ,"KEY_F4" }, + {BTN_TYPE_KB ,0x03 ,"KEY_F5" }, + {BTN_TYPE_KB ,0x0B ,"KEY_F6" }, + {BTN_TYPE_KB ,0x80 ,"KEY_F7" }, + {BTN_TYPE_KB ,0x0A ,"KEY_F8" }, + {BTN_TYPE_KB ,0x01 ,"KEY_F9" }, + {BTN_TYPE_KB ,0x09 ,"KEY_F10" }, + {BTN_TYPE_KB ,0x78 ,"KEY_F11" }, + {BTN_TYPE_KB ,0x07 ,"KEY_F12" }, + {BTN_TYPE_KB ,0x08 ,"KEY_F13" }, + {BTN_TYPE_KB ,0x10 ,"KEY_F14" }, + {BTN_TYPE_KB ,0x18 ,"KEY_F15" }, + {BTN_TYPE_KB ,0x0E ,"KEY_GRAVE" },// backtick + {BTN_TYPE_KB ,0x16 ,"KEY_1" }, + {BTN_TYPE_KB ,0x1E ,"KEY_2" }, + {BTN_TYPE_KB ,0x26 ,"KEY_3" }, + {BTN_TYPE_KB ,0x25 ,"KEY_4" }, + {BTN_TYPE_KB ,0x2E ,"KEY_5" }, + {BTN_TYPE_KB ,0x36 ,"KEY_6" }, + {BTN_TYPE_KB ,0x3D ,"KEY_7" }, + {BTN_TYPE_KB ,0x3E ,"KEY_8" }, + {BTN_TYPE_KB ,0x46 ,"KEY_9" }, + {BTN_TYPE_KB ,0x45 ,"KEY_0" }, + {BTN_TYPE_KB ,0x4E ,"KEY_MINUS" },// hyphen + {BTN_TYPE_KB ,0x55 ,"KEY_EQUAL" }, + {BTN_TYPE_KB ,0x66 ,"KEY_BACKSPACE" }, + {BTN_TYPE_KB ,0x0D ,"KEY_TAB" }, + {BTN_TYPE_KB ,0x15 ,"KEY_Q" }, + {BTN_TYPE_KB ,0x1D ,"KEY_W" }, + {BTN_TYPE_KB ,0x24 ,"KEY_E" }, + {BTN_TYPE_KB ,0x2D ,"KEY_R" }, + {BTN_TYPE_KB ,0x2C ,"KEY_T" }, + {BTN_TYPE_KB ,0x35 ,"KEY_Y" }, + {BTN_TYPE_KB ,0x3C ,"KEY_U" }, + {BTN_TYPE_KB ,0x43 ,"KEY_I" }, + {BTN_TYPE_KB ,0x44 ,"KEY_O" }, + {BTN_TYPE_KB ,0x4D ,"KEY_P" }, + {BTN_TYPE_KB ,0x54 ,"KEY_LEFTBRACE" }, + {BTN_TYPE_KB ,0x5B ,"KEY_RIGHTBRACE" }, + {BTN_TYPE_KB ,0x5D ,"KEY_BACKSLASH" }, + {BTN_TYPE_KB ,0x58 ,"KEY_CAPSLOCK" }, + {BTN_TYPE_KB ,0x1C ,"KEY_A" }, + {BTN_TYPE_KB ,0x1B ,"KEY_S" }, + {BTN_TYPE_KB ,0x23 ,"KEY_D" }, + {BTN_TYPE_KB ,0x2B ,"KEY_F" }, + {BTN_TYPE_KB ,0x34 ,"KEY_G" }, + {BTN_TYPE_KB ,0x33 ,"KEY_H" }, + {BTN_TYPE_KB ,0x3B ,"KEY_J" }, + {BTN_TYPE_KB ,0x42 ,"KEY_K" }, + {BTN_TYPE_KB ,0x4B ,"KEY_L" }, + {BTN_TYPE_KB ,0x4C ,"KEY_SEMICOLON" }, + {BTN_TYPE_KB ,0x52 ,"KEY_APOSTROPHE" }, + {BTN_TYPE_KB ,0x5A ,"KEY_ENTER" }, + {BTN_TYPE_KB ,0x88 ,"KEY_LEFTSHIFT" }, + {BTN_TYPE_KB ,0x1A ,"KEY_Z" }, + {BTN_TYPE_KB ,0x22 ,"KEY_X" }, + {BTN_TYPE_KB ,0x21 ,"KEY_C" }, + {BTN_TYPE_KB ,0x2A ,"KEY_V" }, + {BTN_TYPE_KB ,0x32 ,"KEY_B" }, + {BTN_TYPE_KB ,0x31 ,"KEY_N" }, + {BTN_TYPE_KB ,0x3A ,"KEY_M" }, + {BTN_TYPE_KB ,0x41 ,"KEY_COMMA" }, + {BTN_TYPE_KB ,0x49 ,"KEY_DOT" }, + {BTN_TYPE_KB ,0x4A ,"KEY_SLASH" }, + {BTN_TYPE_KB ,0x89 ,"KEY_RIGHTSHIFT" }, + {BTN_TYPE_KB ,0x82 ,"KEY_LEFTMETA" }, + {BTN_TYPE_KB ,0x8A ,"KEY_LEFTALT" }, + {BTN_TYPE_KB ,0x29 ,"KEY_SPACE" }, + {BTN_TYPE_KB ,0x8B ,"KEY_RIGHTALT" }, + {BTN_TYPE_KB ,0x84 ,"KEY_COMPOSE" }, + {BTN_TYPE_KB ,0x8D ,"KEY_RIGHTCTRL" }, + {BTN_TYPE_KB ,0xC3 ,"KEY_SYSRQ" }, + {BTN_TYPE_KB ,0x7E ,"KEY_SCROLLLOCK" }, + {BTN_TYPE_KB ,0x91 ,"KEY_PAUSE" }, + {BTN_TYPE_KB ,0xC2 ,"KEY_INSERT" }, + {BTN_TYPE_KB ,0x94 ,"KEY_HOME" }, + {BTN_TYPE_KB ,0x96 ,"KEY_PAGEUP" }, + {BTN_TYPE_KB ,0xC0 ,"KEY_DELETE" }, + {BTN_TYPE_KB ,0x95 ,"KEY_END" }, + {BTN_TYPE_KB ,0x97 ,"KEY_PAGEDOWN" }, + {BTN_TYPE_KB ,0x98 ,"KEY_UP" }, + {BTN_TYPE_KB ,0x99 ,"KEY_DOWN" }, + {BTN_TYPE_KB ,0x9A ,"KEY_LEFT" }, + {BTN_TYPE_KB ,0x9B ,"KEY_RIGHT" }, + +/* Numpad button codes */ + {BTN_TYPE_KB ,0x77 ,"KEY_NUMLOCK" }, + {BTN_TYPE_KB ,0x90 ,"KEY_KPSLASH" }, + {BTN_TYPE_KB ,0x7C ,"KEY_KPASTERISK" }, + {BTN_TYPE_KB ,0x7B ,"KEY_KPMINUS" }, + {BTN_TYPE_KB ,0x70 ,"KEY_KP0" }, + {BTN_TYPE_KB ,0x69 ,"KEY_KP1" }, + {BTN_TYPE_KB ,0x72 ,"KEY_KP2" }, + {BTN_TYPE_KB ,0x7A ,"KEY_KP3" }, + {BTN_TYPE_KB ,0x6B ,"KEY_KP4" }, + {BTN_TYPE_KB ,0x73 ,"KEY_KP5" }, + {BTN_TYPE_KB ,0x74 ,"KEY_KP6" }, + {BTN_TYPE_KB ,0x6C ,"KEY_KP7" }, + {BTN_TYPE_KB ,0x75 ,"KEY_KP8" }, + {BTN_TYPE_KB ,0x7D ,"KEY_KP9" }, + {BTN_TYPE_KB ,0x79 ,"KEY_KPPLUS" }, + {BTN_TYPE_KB ,0x81 ,"KEY_KPENTER" }, + {BTN_TYPE_KB ,0x71 ,"KEY_KPDOT" }, + +/* Mouse button codes */ + {BTN_TYPE_MOUSE ,0x01 ,"BTN_LEFT" }, + {BTN_TYPE_MOUSE ,0x02 ,"BTN_RIGHT" }, + {BTN_TYPE_MOUSE ,0x03 ,"BTN_MIDDLE" }, + {BTN_TYPE_MOUSE ,0x04 ,"REL_WHEEL_HI_RES_UP" }, + {BTN_TYPE_MOUSE ,0x05 ,"REL_WHEEL_HI_RES_DOWN" }, + +/* Media button codes */ + {BTN_TYPE_MEDIA ,0x16 ,"MEDIA_SCREENSHOT" }, + {BTN_TYPE_MEDIA ,0x19 ,"MEDIA_SHOW_KEYBOARD" }, + {BTN_TYPE_MEDIA ,0x1C ,"MEDIA_SHOW_DESKTOP" }, + {BTN_TYPE_MEDIA ,0x1E ,"MEDIA_START_RECORDING" },Quoted 8 lines
drivers/hid/hid-asus.c static int ally_set_gamepad_mode(struct ally_handheld *ally, struct hid_device * · line 1288
Quoted 6 lines
drivers/hid/hid-asus.c +6
-0+ { BTN_TYPE_MEDIA, 0x01, "MEDIA_MIC_OFF" }, + { BTN_TYPE_MEDIA, 0x02, "MEDIA_VOL_DOWN" }, + { BTN_TYPE_MEDIA, 0x03, "MEDIA_VOL_UP" }, +}; + +static const size_t keymap_len =ARRAY_SIZE (ally_btn_codes);Why is this a variable???? Can't the users just directly use
ARRAY_SIZEon the array????
Quoted 2 lines
Not understood: you say variable, but both are const(s), isn't const the exact opposite of variable?
My point was that you should use ARRAY_SIZE(ally_btn_codes) directly in the 2 place (IIRC) instead of keymap_len. Then you can just drop keymap_len entirely.
Quoted 9 lines
I am stepping away for today, but I have cleaned up much of this mess.
Quoted 1 line
At v11 it's odd to find this much garbage still remaining... :-(
At a certain (late) point of the development I noticed that remapping buttons on the X applied immediately, while on my Ally 1 it had to go through
apply_allold sysfs interface, so I attempted to unify them late on development, sorry.
I think my comment was a bit uncalled for anyway, but please see it in the light how much lines I had read at that point.
Signature · 1 line
i.