@@ -1141,6 +1152,212 @@ static struct attribute *ally_right_vibration_attrs[] = {
NULL
};
+/**
+ * ally_set_joystick_thresholds() - Set the joystick inner and outer thresholds
+ * @hdev: HID device the command packet is sent on
+ * @left_it: left joystick inner threshold (deadzone)
+ * @left_ot: left joystick outer threshold
+ * @right_it: right joystick inner threshold (deadzone)
+ * @right_ot: right joystick outer threshold
+ *
+ * Context: Caller holds ally_prefs.lock.
+ * Return: 0 on success, -ENOMEM if the command buffer cannot be allocated,
+ * or a negative errno if sending the packet fails.
+ */
+static int ally_set_joystick_thresholds(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 };
+ size_t buf_len;
+ int ret;
+
+ u8 *buf __free(kfree) = ally_alloc_cmd(hdev, CMD_SET_JOYSTICK_DEADZONE,
+ payload, sizeof(payload),
+ &buf_len);
+
+ if (!buf)
+ return -ENOMEM;
+
+ ret = ally_gamepad_send_packet(hdev, buf, buf_len);
+ if (ret < 0) {
+ hid_err(hdev, "Failed to set joystick ranges: %d\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static void ally_joystick_defaults(void)
+{
+ guard(mutex)(&ally_prefs.lock);
+
+ if (ally_prefs.initialized)
+ return;
+
+ ally_prefs.left_deadzone = 10;
+ ally_prefs.left_outer_threshold = 90;
+ ally_prefs.right_deadzone = 10;
+ ally_prefs.right_outer_threshold = 90;
+}
+
+/* Indices into ally_thresholds[], in the order the MCU packet takes them. */
+enum ally_threshold {
+ ALLY_LEFT_INNER,
+ ALLY_LEFT_OUTER,
+ ALLY_RIGHT_INNER,
+ ALLY_RIGHT_OUTER,
+};
+
+static u8 *const ally_thresholds[] = {
+ [ALLY_LEFT_INNER] = &ally_prefs.left_deadzone,
+ [ALLY_LEFT_OUTER] = &ally_prefs.left_outer_threshold,
+ [ALLY_RIGHT_INNER] = &ally_prefs.right_deadzone,
+ [ALLY_RIGHT_OUTER] = &ally_prefs.right_outer_threshold,
+};
+
+static ssize_t ally_threshold_show(char *buf, enum ally_threshold which)
+{
+ guard(mutex)(&ally_prefs.lock);
+
+ if (!ally_prefs.user_cal_support)
+ return -EOPNOTSUPP;
+
+ return sysfs_emit(buf, "%u\n", *ally_thresholds[which]);
+}
+
+static ssize_t ally_threshold_store(struct device *dev, const char *buf,
+ size_t count, enum ally_threshold which,
+ int lo, int hi,
+ const char *group, const char *name)
+{
+ u8 value, v[ARRAY_SIZE(ally_thresholds)];
+ struct hid_device *hdev = to_hid_device(dev);
+ size_t i;
+ int ret;
+
+ ret = kstrtou8(buf, 10, &value);
+ if (ret || value < lo || value > hi)
+ return -EINVAL;
+
+ scoped_guard(mutex, &ally_prefs.lock) {
+ if (!ally_prefs.user_cal_support)
+ return -EOPNOTSUPP;
+
+ for (i = 0; i < ARRAY_SIZE(v); i++)
+ v[i] = *ally_thresholds[i];
+ v[which] = value;
+
+ ret = ally_set_joystick_thresholds(hdev,
+ v[ALLY_LEFT_INNER],
+ v[ALLY_LEFT_OUTER],
+ v[ALLY_RIGHT_INNER],
+ v[ALLY_RIGHT_OUTER]);
+ if (ret)
+ return ret;
+
+ *ally_thresholds[which] = value;
+ }
+
+ sysfs_notify(&dev->kobj, group, name);
+
+ return count;
+}
+
+/* Generates <side>_joystick_<kind>_threshold_{show,store,range_show}. */
+#define ALLY_THRESHOLD_FUNCS(side, SIDE, kind, KIND) \
+static ssize_t side##_joystick_##kind##_threshold_show(struct device *dev, \
+ struct device_attribute *attr, \
+ char *buf) \
+{ \
+ return ally_threshold_show(buf, ALLY_##SIDE##_##KIND); \
+} \
+ \
+static ssize_t side##_joystick_##kind##_threshold_store(struct device *dev, \
+ struct device_attribute *attr, \
+ const char *buf, size_t count) \
+{ \
+ return ally_threshold_store(dev, buf, count, ALLY_##SIDE##_##KIND, \
+ ASUS_ALLY_##KIND##_THRESHOLD_MIN, \
+ ASUS_ALLY_##KIND##_THRESHOLD_MAX, \
+ #side "_joystick_axis", #kind "_threshold"); \
+} \
+ \
+static ssize_t side##_joystick_##kind##_threshold_range_show(struct device *dev, \
+ struct device_attribute *attr, \
+ char *buf) \
+{ \
+ return sysfs_emit(buf, "%u %u\n", ASUS_ALLY_##KIND##_THRESHOLD_MIN, \
+ ASUS_ALLY_##KIND##_THRESHOLD_MAX); \
+}
+
+ALLY_THRESHOLD_FUNCS(left, LEFT, inner, INNER)
+ALLY_THRESHOLD_FUNCS(left, LEFT, outer, OUTER)
+ALLY_THRESHOLD_FUNCS(right, RIGHT, inner, INNER)
+ALLY_THRESHOLD_FUNCS(right, RIGHT, outer, OUTER)
+
+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 *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
+};
+
+/*
+ * Registered only when the device can configure these parameters. The
+ * callbacks still reject a missing capability.
+ */
+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,
+};
+
static struct attribute *ally_config_attrs[] = {
&dev_attr_xbox_controller.attr,
NULL