All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] HID: nintendo: check analog user calibration for plausibility
@ 2022-09-14  7:43 Johnothan King
  2022-09-14 19:33 ` Silvan Jegen
  2022-09-20 10:21 ` Benjamin Tissoires
  0 siblings, 2 replies; 4+ messages in thread
From: Johnothan King @ 2022-09-14  7:43 UTC (permalink / raw)
  To: Daniel J. Ogorchock, Jiri Kosina, Benjamin Tissoires,
	linux-input, linux-kernel

Arne Wendt writes:
  Cheap clone controllers may (falsely) report as having a user
  calibration for the analog sticks in place, but return
  wrong/impossible values for the actual calibration data.
  In the present case at mine, the controller reports having a
  user calibration in place and successfully executes the read
  commands. The reported user calibration however is
  min = center = max = 0.

  This pull request addresses problems of this kind by checking the
  provided user calibration-data for plausibility (min < center < max)
  and falling back to the default values if implausible.

I'll note that I was experiencing a crash because of this bug when using
the GuliKit KingKong 2 controller. The crash manifests as a divide by
zero error in the kernel logs:
kernel: divide error: 0000 [#1] PREEMPT SMP NOPTI

Link: https://github.com/nicman23/dkms-hid-nintendo/pull/25
Link: https://github.com/DanielOgorchock/linux/issues/36
Co-authored-by: Arne Wendt <arne.wendt@tuhh.de>
Signed-off-by: Johnothan King <johnothanking@protonmail.com>
---
 drivers/hid/hid-nintendo.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c
index 6028af3c3aae..7f287f6a75f5 100644
--- a/drivers/hid/hid-nintendo.c
+++ b/drivers/hid/hid-nintendo.c
@@ -793,7 +793,17 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
 					    &ctlr->left_stick_cal_x,
 					    &ctlr->left_stick_cal_y,
 					    true);
-	if (ret) {
+
+	/*
+	 * Check whether read succeeded and perform plausibility check
+	 * for retrieved values.
+	 */
+	if (ret ||
+		ctlr->left_stick_cal_x.min >= ctlr->left_stick_cal_x.center ||
+		ctlr->left_stick_cal_x.center >= ctlr->left_stick_cal_x.max ||
+		ctlr->left_stick_cal_y.min >= ctlr->left_stick_cal_y.center ||
+		ctlr->left_stick_cal_y.center >= ctlr->left_stick_cal_y.max
+	) {
 		hid_warn(ctlr->hdev,
 			 "Failed to read left stick cal, using dflts; e=%d\n",
 			 ret);
@@ -812,7 +822,17 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
 					    &ctlr->right_stick_cal_x,
 					    &ctlr->right_stick_cal_y,
 					    false);
-	if (ret) {
+
+	/*
+	 * Check whether read succeeded and perform plausibility check
+	 * for retrieved values.
+	 */
+	if (ret ||
+		ctlr->right_stick_cal_x.min >= ctlr->right_stick_cal_x.center ||
+		ctlr->right_stick_cal_x.center >= ctlr->right_stick_cal_x.max ||
+		ctlr->right_stick_cal_y.min >= ctlr->right_stick_cal_y.center ||
+		ctlr->right_stick_cal_y.center >= ctlr->right_stick_cal_y.max
+	) {
 		hid_warn(ctlr->hdev,
 			 "Failed to read right stick cal, using dflts; e=%d\n",
 			 ret);
-- 
2.37.3



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] HID: nintendo: check analog user calibration for plausibility
  2022-09-14  7:43 [PATCH] HID: nintendo: check analog user calibration for plausibility Johnothan King
@ 2022-09-14 19:33 ` Silvan Jegen
  2022-09-20 10:21 ` Benjamin Tissoires
  1 sibling, 0 replies; 4+ messages in thread
From: Silvan Jegen @ 2022-09-14 19:33 UTC (permalink / raw)
  To: Johnothan King
  Cc: Daniel J. Ogorchock, Jiri Kosina, Benjamin Tissoires,
	linux-input, linux-kernel

Hi

Johnothan King <johnothanking@protonmail.com> wrote:
> Arne Wendt writes:
>   Cheap clone controllers may (falsely) report as having a user
>   calibration for the analog sticks in place, but return
>   wrong/impossible values for the actual calibration data.
>   In the present case at mine, the controller reports having a
>   user calibration in place and successfully executes the read
>   commands. The reported user calibration however is
>   min = center = max = 0.
> 
>   This pull request addresses problems of this kind by checking the
>   provided user calibration-data for plausibility (min < center < max)
>   and falling back to the default values if implausible.
> 
> I'll note that I was experiencing a crash because of this bug when using
> the GuliKit KingKong 2 controller. The crash manifests as a divide by
> zero error in the kernel logs:
> kernel: divide error: 0000 [#1] PREEMPT SMP NOPTI
> 
> Link: https://github.com/nicman23/dkms-hid-nintendo/pull/25
> Link: https://github.com/DanielOgorchock/linux/issues/36
> Co-authored-by: Arne Wendt <arne.wendt@tuhh.de>
> Signed-off-by: Johnothan King <johnothanking@protonmail.com>
> ---
>  drivers/hid/hid-nintendo.c | 24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)

Reviewed-by: Silvan Jegen <s.jegen@gmail.com>

I have actually hit this issue as well ...


Cheers,
Silvan

> 
> diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c
> index 6028af3c3aae..7f287f6a75f5 100644
> --- a/drivers/hid/hid-nintendo.c
> +++ b/drivers/hid/hid-nintendo.c
> @@ -793,7 +793,17 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
>  					    &ctlr->left_stick_cal_x,
>  					    &ctlr->left_stick_cal_y,
>  					    true);
> -	if (ret) {
> +
> +	/*
> +	 * Check whether read succeeded and perform plausibility check
> +	 * for retrieved values.
> +	 */
> +	if (ret ||
> +		ctlr->left_stick_cal_x.min >= ctlr->left_stick_cal_x.center ||
> +		ctlr->left_stick_cal_x.center >= ctlr->left_stick_cal_x.max ||
> +		ctlr->left_stick_cal_y.min >= ctlr->left_stick_cal_y.center ||
> +		ctlr->left_stick_cal_y.center >= ctlr->left_stick_cal_y.max
> +	) {
>  		hid_warn(ctlr->hdev,
>  			 "Failed to read left stick cal, using dflts; e=%d\n",
>  			 ret);
> @@ -812,7 +822,17 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
>  					    &ctlr->right_stick_cal_x,
>  					    &ctlr->right_stick_cal_y,
>  					    false);
> -	if (ret) {
> +
> +	/*
> +	 * Check whether read succeeded and perform plausibility check
> +	 * for retrieved values.
> +	 */
> +	if (ret ||
> +		ctlr->right_stick_cal_x.min >= ctlr->right_stick_cal_x.center ||
> +		ctlr->right_stick_cal_x.center >= ctlr->right_stick_cal_x.max ||
> +		ctlr->right_stick_cal_y.min >= ctlr->right_stick_cal_y.center ||
> +		ctlr->right_stick_cal_y.center >= ctlr->right_stick_cal_y.max
> +	) {
>  		hid_warn(ctlr->hdev,
>  			 "Failed to read right stick cal, using dflts; e=%d\n",
>  			 ret);



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] HID: nintendo: check analog user calibration for plausibility
  2022-09-14  7:43 [PATCH] HID: nintendo: check analog user calibration for plausibility Johnothan King
  2022-09-14 19:33 ` Silvan Jegen
@ 2022-09-20 10:21 ` Benjamin Tissoires
  2022-09-20 16:53   ` Johnothan King
  1 sibling, 1 reply; 4+ messages in thread
From: Benjamin Tissoires @ 2022-09-20 10:21 UTC (permalink / raw)
  To: Johnothan King
  Cc: Daniel J. Ogorchock, Jiri Kosina, linux-input, linux-kernel

On Wed, Sep 14, 2022 at 9:51 AM Johnothan King
<johnothanking@protonmail.com> wrote:
>
> Arne Wendt writes:
>   Cheap clone controllers may (falsely) report as having a user
>   calibration for the analog sticks in place, but return
>   wrong/impossible values for the actual calibration data.
>   In the present case at mine, the controller reports having a
>   user calibration in place and successfully executes the read
>   commands. The reported user calibration however is
>   min = center = max = 0.
>
>   This pull request addresses problems of this kind by checking the
>   provided user calibration-data for plausibility (min < center < max)
>   and falling back to the default values if implausible.
>
> I'll note that I was experiencing a crash because of this bug when using
> the GuliKit KingKong 2 controller. The crash manifests as a divide by
> zero error in the kernel logs:
> kernel: divide error: 0000 [#1] PREEMPT SMP NOPTI
>
> Link: https://github.com/nicman23/dkms-hid-nintendo/pull/25
> Link: https://github.com/DanielOgorchock/linux/issues/36
> Co-authored-by: Arne Wendt <arne.wendt@tuhh.de>
> Signed-off-by: Johnothan King <johnothanking@protonmail.com>
> ---
>  drivers/hid/hid-nintendo.c | 24 ++++++++++++++++++++++--
>  1 file changed, 22 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c
> index 6028af3c3aae..7f287f6a75f5 100644
> --- a/drivers/hid/hid-nintendo.c
> +++ b/drivers/hid/hid-nintendo.c
> @@ -793,7 +793,17 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
>                                             &ctlr->left_stick_cal_x,
>                                             &ctlr->left_stick_cal_y,
>                                             true);
> -       if (ret) {
> +
> +       /*
> +        * Check whether read succeeded and perform plausibility check
> +        * for retrieved values.
> +        */
> +       if (ret ||
> +               ctlr->left_stick_cal_x.min >= ctlr->left_stick_cal_x.center ||
> +               ctlr->left_stick_cal_x.center >= ctlr->left_stick_cal_x.max ||
> +               ctlr->left_stick_cal_y.min >= ctlr->left_stick_cal_y.center ||
> +               ctlr->left_stick_cal_y.center >= ctlr->left_stick_cal_y.max
> +       ) {
>                 hid_warn(ctlr->hdev,
>                          "Failed to read left stick cal, using dflts; e=%d\n",
>                          ret);
> @@ -812,7 +822,17 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
>                                             &ctlr->right_stick_cal_x,
>                                             &ctlr->right_stick_cal_y,
>                                             false);
> -       if (ret) {
> +
> +       /*
> +        * Check whether read succeeded and perform plausibility check
> +        * for retrieved values.
> +        */
> +       if (ret ||
> +               ctlr->right_stick_cal_x.min >= ctlr->right_stick_cal_x.center ||
> +               ctlr->right_stick_cal_x.center >= ctlr->right_stick_cal_x.max ||
> +               ctlr->right_stick_cal_y.min >= ctlr->right_stick_cal_y.center ||
> +               ctlr->right_stick_cal_y.center >= ctlr->right_stick_cal_y.max

Wouldn't it make sense to have a function that takes a single struct
joycon_stick_cal pointer and does the check?
This would make things more readable IMO.

Cheers,
Benjamin

> +       ) {
>                 hid_warn(ctlr->hdev,
>                          "Failed to read right stick cal, using dflts; e=%d\n",
>                          ret);
> --
> 2.37.3
>
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] HID: nintendo: check analog user calibration for plausibility
  2022-09-20 10:21 ` Benjamin Tissoires
@ 2022-09-20 16:53   ` Johnothan King
  0 siblings, 0 replies; 4+ messages in thread
From: Johnothan King @ 2022-09-20 16:53 UTC (permalink / raw)
  To: Benjamin Tissoires
  Cc: Daniel J. Ogorchock, Jiri Kosina, linux-input, linux-kernel

> Wouldn't it make sense to have a function that takes a single struct
> joycon_stick_cal pointer and does the check?

I acknowledge the current code can be simplified further. I will submit a second version of this patch that moves the plausibility check to joycon_read_stick_calibration() and adds a joycon_use_default_calibration() function. This will avoid the code duplication present in the current patch.

In the v2 patch the check will still use both the cal_x and cal_y pointers. However, since the check will be reduced to just one if statement, that should be enough to simplify the code. The new function in the v2 patch (joycon_use_default_calibration()) was implemented so that I could simplify the existing default stick calibration code for reuse with both the left and right sticks. The new function in effect reduces four lines of code to one:
    -ctlr->left_stick_cal_x.center = DFLT_STICK_CAL_CEN;
    -ctlr->left_stick_cal_y.center = DFLT_STICK_CAL_CEN;
    -ctlr->right_stick_cal_x.center = DFLT_STICK_CAL_CEN;
    -ctlr->right_stick_cal_y.center = DFLT_STICK_CAL_CEN;
    +cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN;

- Johnothan King

------- Original Message -------
On Tuesday, September 20th, 2022 at 3:21 AM, Benjamin Tissoires <benjamin.tissoires@redhat.com> wrote:


> On Wed, Sep 14, 2022 at 9:51 AM Johnothan King
> johnothanking@protonmail.com wrote:
> 
> > Arne Wendt writes:
> > Cheap clone controllers may (falsely) report as having a user
> > calibration for the analog sticks in place, but return
> > wrong/impossible values for the actual calibration data.
> > In the present case at mine, the controller reports having a
> > user calibration in place and successfully executes the read
> > commands. The reported user calibration however is
> > min = center = max = 0.
> > 
> > This pull request addresses problems of this kind by checking the
> > provided user calibration-data for plausibility (min < center < max)
> > and falling back to the default values if implausible.
> > 
> > I'll note that I was experiencing a crash because of this bug when using
> > the GuliKit KingKong 2 controller. The crash manifests as a divide by
> > zero error in the kernel logs:
> > kernel: divide error: 0000 [#1] PREEMPT SMP NOPTI
> > 
> > Link: https://github.com/nicman23/dkms-hid-nintendo/pull/25
> > Link: https://github.com/DanielOgorchock/linux/issues/36
> > Co-authored-by: Arne Wendt arne.wendt@tuhh.de
> > Signed-off-by: Johnothan King johnothanking@protonmail.com
> > ---
> > drivers/hid/hid-nintendo.c | 24 ++++++++++++++++++++++--
> > 1 file changed, 22 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c
> > index 6028af3c3aae..7f287f6a75f5 100644
> > --- a/drivers/hid/hid-nintendo.c
> > +++ b/drivers/hid/hid-nintendo.c
> > @@ -793,7 +793,17 @@ static int joycon_request_calibration(struct joycon_ctlr ctlr)
> > &ctlr->left_stick_cal_x,
> > &ctlr->left_stick_cal_y,
> > true);
> > - if (ret) {
> > +
> > + /
> > + * Check whether read succeeded and perform plausibility check
> > + * for retrieved values.
> > + */
> > + if (ret ||
> > + ctlr->left_stick_cal_x.min >= ctlr->left_stick_cal_x.center ||
> > + ctlr->left_stick_cal_x.center >= ctlr->left_stick_cal_x.max ||
> > + ctlr->left_stick_cal_y.min >= ctlr->left_stick_cal_y.center ||
> > + ctlr->left_stick_cal_y.center >= ctlr->left_stick_cal_y.max
> > + ) {
> > hid_warn(ctlr->hdev,
> > "Failed to read left stick cal, using dflts; e=%d\n",
> > ret);
> > @@ -812,7 +822,17 @@ static int joycon_request_calibration(struct joycon_ctlr ctlr)
> > &ctlr->right_stick_cal_x,
> > &ctlr->right_stick_cal_y,
> > false);
> > - if (ret) {
> > +
> > + /
> > + * Check whether read succeeded and perform plausibility check
> > + * for retrieved values.
> > + */
> > + if (ret ||
> > + ctlr->right_stick_cal_x.min >= ctlr->right_stick_cal_x.center ||
> > + ctlr->right_stick_cal_x.center >= ctlr->right_stick_cal_x.max ||
> > + ctlr->right_stick_cal_y.min >= ctlr->right_stick_cal_y.center ||
> > + ctlr->right_stick_cal_y.center >= ctlr->right_stick_cal_y.max
> 
> 
> Wouldn't it make sense to have a function that takes a single struct
> joycon_stick_cal pointer and does the check?
> This would make things more readable IMO.
> 
> Cheers,
> Benjamin
> 
> > + ) {
> > hid_warn(ctlr->hdev,
> > "Failed to read right stick cal, using dflts; e=%d\n",
> > ret);
> > --
> > 2.37.3

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-20 16:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-14  7:43 [PATCH] HID: nintendo: check analog user calibration for plausibility Johnothan King
2022-09-14 19:33 ` Silvan Jegen
2022-09-20 10:21 ` Benjamin Tissoires
2022-09-20 16:53   ` Johnothan King

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.