Merge branch 'for-6.1/nintendo' into for-linus
authorBenjamin Tissoires <benjamin.tissoires@redhat.com>
Wed, 5 Oct 2022 09:28:48 +0000 (10:28 +0100)
committerBenjamin Tissoires <benjamin.tissoires@redhat.com>
Wed, 5 Oct 2022 09:28:48 +0000 (10:28 +0100)
- better support of Nintendo clone controllers (Icenowy Zheng and
  Johnothan King)

drivers/hid/hid-nintendo.c

index 6028af3c3aae54929f1fc354b0d7bc36ce9a4ebf..5bfc0c45046085b84f90a0c2dfb339f0ad0a6c2d 100644 (file)
@@ -760,12 +760,31 @@ static int joycon_read_stick_calibration(struct joycon_ctlr *ctlr, u16 cal_addr,
        cal_y->max = cal_y->center + y_max_above;
        cal_y->min = cal_y->center - y_min_below;
 
-       return 0;
+       /* check if calibration values are plausible */
+       if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max ||
+           cal_y->min >= cal_y->center || cal_y->center >= cal_y->max)
+               ret = -EINVAL;
+
+       return ret;
 }
 
 static const u16 DFLT_STICK_CAL_CEN = 2000;
 static const u16 DFLT_STICK_CAL_MAX = 3500;
 static const u16 DFLT_STICK_CAL_MIN = 500;
+static void joycon_use_default_calibration(struct hid_device *hdev,
+                                          struct joycon_stick_cal *cal_x,
+                                          struct joycon_stick_cal *cal_y,
+                                          const char *stick, int ret)
+{
+       hid_warn(hdev,
+                "Failed to read %s stick cal, using defaults; e=%d\n",
+                stick, ret);
+
+       cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN;
+       cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX;
+       cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN;
+}
+
 static int joycon_request_calibration(struct joycon_ctlr *ctlr)
 {
        u16 left_stick_addr = JC_CAL_FCT_DATA_LEFT_ADDR;
@@ -793,38 +812,24 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
                                            &ctlr->left_stick_cal_x,
                                            &ctlr->left_stick_cal_y,
                                            true);
-       if (ret) {
-               hid_warn(ctlr->hdev,
-                        "Failed to read left stick cal, using dflts; e=%d\n",
-                        ret);
 
-               ctlr->left_stick_cal_x.center = DFLT_STICK_CAL_CEN;
-               ctlr->left_stick_cal_x.max = DFLT_STICK_CAL_MAX;
-               ctlr->left_stick_cal_x.min = DFLT_STICK_CAL_MIN;
-
-               ctlr->left_stick_cal_y.center = DFLT_STICK_CAL_CEN;
-               ctlr->left_stick_cal_y.max = DFLT_STICK_CAL_MAX;
-               ctlr->left_stick_cal_y.min = DFLT_STICK_CAL_MIN;
-       }
+       if (ret)
+               joycon_use_default_calibration(ctlr->hdev,
+                                              &ctlr->left_stick_cal_x,
+                                              &ctlr->left_stick_cal_y,
+                                              "left", ret);
 
        /* read the right stick calibration data */
        ret = joycon_read_stick_calibration(ctlr, right_stick_addr,
                                            &ctlr->right_stick_cal_x,
                                            &ctlr->right_stick_cal_y,
                                            false);
-       if (ret) {
-               hid_warn(ctlr->hdev,
-                        "Failed to read right stick cal, using dflts; e=%d\n",
-                        ret);
-
-               ctlr->right_stick_cal_x.center = DFLT_STICK_CAL_CEN;
-               ctlr->right_stick_cal_x.max = DFLT_STICK_CAL_MAX;
-               ctlr->right_stick_cal_x.min = DFLT_STICK_CAL_MIN;
 
-               ctlr->right_stick_cal_y.center = DFLT_STICK_CAL_CEN;
-               ctlr->right_stick_cal_y.max = DFLT_STICK_CAL_MAX;
-               ctlr->right_stick_cal_y.min = DFLT_STICK_CAL_MIN;
-       }
+       if (ret)
+               joycon_use_default_calibration(ctlr->hdev,
+                                              &ctlr->right_stick_cal_x,
+                                              &ctlr->right_stick_cal_y,
+                                              "right", ret);
 
        hid_dbg(ctlr->hdev, "calibration:\n"
                            "l_x_c=%d l_x_max=%d l_x_min=%d\n"
@@ -1904,9 +1909,8 @@ static int joycon_leds_create(struct joycon_ctlr *ctlr)
                /* Set the home LED to 0 as default state */
                ret = joycon_home_led_brightness_set(led, 0);
                if (ret) {
-                       hid_err(hdev, "Failed to set home LED dflt; ret=%d\n",
-                                                                       ret);
-                       return ret;
+                       hid_warn(hdev, "Failed to set home LED default, unregistering home LED");
+                       devm_led_classdev_unregister(&hdev->dev, led);
                }
        }