@@ -1479,6 +1495,234 @@ static struct device_attribute dev_attr_right_joystick_anti_deadzone =
static struct device_attribute dev_attr_right_joystick_anti_deadzone_range =
__ATTR(anti_deadzone_range, 0444, right_joystick_anti_deadzone_range_show, NULL);
+enum ally_joystick_side {
+ JOYSTICK_LEFT = 0,
+ JOYSTICK_RIGHT,
+};
+
+/**
+ * ally_set_joystick_resp_curve() - Set a joystick response curve
+ * @hdev: HID device the command packet is sent on
+ * @side: joystick to program, %JOYSTICK_LEFT or %JOYSTICK_RIGHT
+ * @curve: response curve to apply
+ *
+ * Context: Caller holds ally_prefs.lock.
+ * Return: 0 on success, negative errno on failure.
+ */
+static int ally_set_joystick_resp_curve(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
+ };
+ size_t buf_len;
+ int ret;
+
+ u8 *buf __free(kfree) = ally_alloc_cmd(hdev, CMD_SET_RESP_CURVE, payload,
+ sizeof(payload), &buf_len);
+
+ if (!buf)
+ return -ENOMEM;
+
+ ret = ally_gamepad_send_packet(hdev, buf, buf_len);
+
+ /*
+ * Previous function returns the number of bytes sent, or a negative error code.
+ * From this function I only need either success or the failure.
+ */
+ return ret < 0 ? ret : 0;
+}
+
+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;
+}
+
+/* Linear curve. */
+static const struct ally_joystick_resp_curve ally_linear_curve = {
+ .entry_1 = { .move = 0, .resp = 0 },
+ .entry_2 = { .move = 33, .resp = 33 },
+ .entry_3 = { .move = 66, .resp = 66 },
+ .entry_4 = { .move = 100, .resp = 100 },
+};
+
+static void ally_curve_defaults(void)
+{
+ guard(mutex)(&ally_prefs.lock);
+
+ if (ally_prefs.initialized)
+ return;
+
+ ally_prefs.left_curve = ally_linear_curve;
+ ally_prefs.right_curve = ally_linear_curve;
+}
+
+/* Indexed by enum ally_joystick_side */
+static struct ally_joystick_resp_curve * const ally_resp_curves[] = {
+ [JOYSTICK_LEFT] = &ally_prefs.left_curve,
+ [JOYSTICK_RIGHT] = &ally_prefs.right_curve,
+};
+
+static ssize_t response_curve_show(enum ally_joystick_side side, char *buf)
+{
+ const struct ally_joystick_resp_curve *curve = ally_resp_curves[side];
+
+ guard(mutex)(&ally_prefs.lock);
+
+ if (!ally_prefs.resp_curve_support)
+ return -EOPNOTSUPP;
+
+ 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);
+}
+
+/**
+ * ally_parse_u8_token() - Parse one decimal u8 from a sysfs write
+ * @buf: write buffer
+ * @count: number of bytes in @buf
+ * @pos: offset in @buf to start scanning at; on success it is advanced to
+ * the first byte after the token
+ * @val: where the parsed value is stored; only written on success
+ *
+ * Skips leading whitespace, then parses an optional '+' followed by one or
+ * more decimal digits. The token, sign included, may be at most 15 characters
+ * long and its value must fit in a u8.
+ *
+ * The scan is bounded by @count, not by a terminating NUL, so an embedded NUL
+ * is a byte in the write, not the end of the buffer.
+ *
+ * Return: 0 on success, -%EINVAL if there is no number at @pos, the token is
+ * too long, or the value does not fit in a u8. On failure @pos is left at an
+ * unspecified offset.
+ */
+static int ally_parse_u8_token(const char *buf, size_t count, size_t *pos, u8 *val)
+{
+ char token[16];
+ size_t n = 0;
+
+ while (*pos < count && isspace((unsigned char)buf[*pos]))
+ (*pos)++;
+
+ if (*pos < count && buf[*pos] == '+')
+ token[n++] = buf[(*pos)++];
+
+ if (*pos >= count || !isdigit((unsigned char)buf[*pos]))
+ return -EINVAL;
+
+ while (*pos < count && isdigit((unsigned char)buf[*pos])) {
+ if (n >= sizeof(token) - 1)
+ return -EINVAL;
+ token[n++] = buf[(*pos)++];
+ }
+
+ token[n] = '\0';
+
+ return kstrtou8(token, 10, val) ? -EINVAL : 0;
+}
+
+static ssize_t response_curve_store(struct device *dev,
+ enum ally_joystick_side side,
+ const char *group, const char *buf,
+ size_t count)
+{
+ struct hid_device *hdev = to_hid_device(dev);
+ struct ally_joystick_resp_curve curve;
+ size_t pos = 0;
+ int ret, i;
+ u8 v[8];
+
+ /*
+ * 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.
+ */
+ for (i = 0; i < ARRAY_SIZE(v); i++) {
+ ret = ally_parse_u8_token(buf, count, &pos, &v[i]);
+ if (ret || v[i] > 100)
+ return -EINVAL;
+ }
+
+ for (; pos < count; pos++)
+ if (!isspace((unsigned char)buf[pos]))
+ return -EINVAL;
+
+ curve = (struct ally_joystick_resp_curve) {
+ .entry_1 = { .move = v[0], .resp = v[1] },
+ .entry_2 = { .move = v[2], .resp = v[3] },
+ .entry_3 = { .move = v[4], .resp = v[5] },
+ .entry_4 = { .move = v[6], .resp = v[7] },
+ };
+
+ if (!ally_resp_curve_is_valid(&curve))
+ return -EINVAL;
+
+ scoped_guard(mutex, &ally_prefs.lock) {
+ if (!ally_prefs.resp_curve_support)
+ return -EOPNOTSUPP;
+
+ ret = ally_set_joystick_resp_curve(hdev, side, &curve);
+ if (ret < 0) {
+ hid_err(hdev, "Failed to set joystick response curve: %d\n",
+ ret);
+ return ret;
+ }
+
+ *ally_resp_curves[side] = curve;
+ }
+
+ sysfs_notify(&dev->kobj, group, "response_curve");
+
+ return count;
+}
+
+/* Generates <side>_response_curve_{show,store}. */
+#define ALLY_RESP_CURVE_FUNCS(side, SIDE) \
+static ssize_t side##_response_curve_show(struct device *dev, \
+ struct device_attribute *attr, \
+ char *buf) \
+{ \
+ return response_curve_show(JOYSTICK_##SIDE, buf); \
+} \
+ \
+static ssize_t side##_response_curve_store(struct device *dev, \
+ struct device_attribute *attr, \
+ const char *buf, size_t count) \
+{ \
+ return response_curve_store(dev, JOYSTICK_##SIDE, \
+ #side "_joystick_axis", buf, count); \
+}
+
+ALLY_RESP_CURVE_FUNCS(left, LEFT)
+ALLY_RESP_CURVE_FUNCS(right, RIGHT)
+
+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");
+}
+
+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 *left_joystick_axis_attrs[] = {
&dev_attr_left_joystick_inner_threshold.attr,
&dev_attr_left_joystick_outer_threshold.attr,