Skip to contentSkip to conversation

HID: google: whiskers: more robust tablet mode detection

PATCH In review Dmitry Torokhov 3 participants 7 messages Sep 13, 2019 – Oct 1, 2019 HID

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 HID: google: whiskers: more robust tablet mode detection

The USB interface may get detected before the platform/EC one, so let's note the state of the base (if we receive event) and use it to correctly initialize the tablet mode switch state.

Also let's start the HID interface immediately when probing, this will ensure that we correctly process "base folded" events that may be sent as we initialize the base. Note that this requires us to add a release() function where we stop and close the hardware and switch the LED registration away from devm interface as we need to make sure that we destroy the LED instance before we stop the hardware.

---
 drivers/hid/hid-google-hammer.c | 71 ++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 15 deletions(-)
drivers/hid/hid-google-hammer.c +56 -15
index 84f8c127ebdc..4f64f93ddfcb 100644
@@ -35,6 +35,7 @@ struct cbas_ec {
 	struct device *dev;	/* The platform device (EC) */
 	struct input_dev *input;
 	bool base_present;
+	bool base_folded;
 	struct notifier_block notifier;
 };
 
@@ -208,7 +209,14 @@ static int __cbas_ec_probe(struct platform_device *pdev)
 		return error;
 	}
 
-	input_report_switch(input, SW_TABLET_MODE, !cbas_ec.base_present);
+	if (!cbas_ec.base_present)
+		cbas_ec.base_folded = false;
+
+	dev_dbg(&pdev->dev, "%s: base: %d, folded: %d\n", __func__,
+		cbas_ec.base_present, cbas_ec.base_folded);
+
+	input_report_switch(input, SW_TABLET_MODE,
+			    !cbas_ec.base_present || cbas_ec.base_folded);
 
 	cbas_ec_set_input(input);
 
@@ -322,10 +330,9 @@ static int hammer_kbd_brightness_set_blocking(struct led_classdev *cdev,
 static int hammer_register_leds(struct hid_device *hdev)
 {
 	struct hammer_kbd_leds *kbd_backlight;
+	int error;
 
-	kbd_backlight = devm_kzalloc(&hdev->dev,
-				     sizeof(*kbd_backlight),
-				     GFP_KERNEL);
+	kbd_backlight = kzalloc(sizeof(*kbd_backlight), GFP_KERNEL);
 	if (!kbd_backlight)
 		return -ENOMEM;
 
@@ -339,7 +346,26 @@ static int hammer_register_leds(struct hid_device *hdev)
 	/* Set backlight to 0% initially. */
 	hammer_kbd_brightness_set_blocking(&kbd_backlight->cdev, 0);
 
-	return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
+	error = led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
+	if (error)
+		goto err_free_mem;
+
+	hid_set_drvdata(hdev, kbd_backlight);
+	return 0;
+
+err_free_mem:
+	kfree(kbd_backlight);
+	return error;
+}
+
+static void hammer_unregister_leds(struct hid_device *hdev)
+{
+	struct hammer_kbd_leds *kbd_backlight = hid_get_drvdata(hdev);
+
+	if (kbd_backlight) {
+		led_classdev_unregister(&kbd_backlight->cdev);
+		kfree(kbd_backlight);
+	}
 }
 
 #define HID_UP_GOOGLEVENDOR	0xffd10000
@@ -376,8 +402,9 @@ static int hammer_event(struct hid_device *hid, struct hid_field *field,
 	    usage->hid == WHISKERS_KBD_FOLDED) {
 		spin_lock_irqsave(&cbas_ec_lock, flags);
 
+		cbas_ec.base_folded = value;
 		hid_dbg(hid, "%s: base: %d, folded: %d\n", __func__,
-			cbas_ec.base_present, value);
+			cbas_ec.base_present, cbas_ec.base_folded);
 
 		/*
 		 * We should not get event if base is detached, but in case
@@ -436,6 +463,14 @@ static int hammer_probe(struct hid_device *hdev,
 {
 	int error;
 
+	error = hid_parse(hdev);
+	if (error)
+		return error;
+
+	error = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+	if (error)
+		return error;
+
 	/*
 	 * We always want to poll for, and handle tablet mode events from
 	 * Whiskers, even when nobody has opened the input device. This also
@@ -443,16 +478,12 @@ static int hammer_probe(struct hid_device *hdev,
 	 * the device.
 	 */
 	if (hdev->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
-			hammer_is_keyboard_interface(hdev))
+	    hammer_is_keyboard_interface(hdev)) {
 		hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
-
-	error = hid_parse(hdev);
-	if (error)
-		return error;
-
-	error = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
-	if (error)
-		return error;
+		error = hid_hw_open(hdev);
+		if (error)
+			return error;
+	}
 
 	if (hammer_has_backlight_control(hdev)) {
 		error = hammer_register_leds(hdev);
@@ -465,6 +496,15 @@ static int hammer_probe(struct hid_device *hdev,
 	return 0;
 }
 
+static void hammer_remove(struct hid_device *hdev)
+{
+	if (hdev->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
+			hammer_is_keyboard_interface(hdev))
+		hid_hw_close(hdev);
+
+	hammer_unregister_leds(hdev);
+	hid_hw_stop(hdev);
+}
 
 static const struct hid_device_id hammer_devices[] = {
 	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
@@ -483,6 +523,7 @@ static struct hid_driver hammer_driver = {
 	.name = "hammer",
 	.id_table = hammer_devices,
 	.probe = hammer_probe,
+	.remove = hammer_remove,
 	.input_mapping = hammer_input_mapping,
 	.event = hammer_event,
 };
Signature · 1 line
2.23.0.237.gc6a4ce50a0-goog
#1 lore raw

PATCH 2/3 HID: google: whiskers: signal tablet mode switch on disconnect

Currently, the tablet mode switch that takes two signals as its input:
base attached switch from the EC and some GMR signal from whiskers when it's folded over. This tablet mode switch is then sent to Chrome, which changes the UI.

However, there are some units which have a lot of leakage on the ADC pins that the EC uses to determine whether or not a base is attached. This can result in the base being physically detached, but the EC thinking that it's still attached. The user would then be stuck in laptop mode and wouldn't be able to rotate their display.

To work around this let's send "tablet mode" signal when we remove HID interface, which normally happens when we physically disconnect whiskers.

---
 drivers/hid/hid-google-hammer.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/hid/hid-google-hammer.c +23 -1
index 4f64f93ddfcb..3dc6116c8f76 100644
@@ -498,11 +498,33 @@ static int hammer_probe(struct hid_device *hdev,
 
 static void hammer_remove(struct hid_device *hdev)
 {
+	unsigned long flags;
+
 	if (hdev->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
-			hammer_is_keyboard_interface(hdev))
+			hammer_is_keyboard_interface(hdev)) {
 		hid_hw_close(hdev);
 
+		/*
+		 * If we are disconnecting then most likely Whiskers is
+		 * being removed. Even if it is not removed, without proper
+		 * keyboard we should not stay in clamshell mode.
+		 *
+		 * The reason for doing it here and not waiting for signal
+		 * from EC, is that on some devices there are high leakage
+		 * on Whiskers pins and we do not detect disconnect reliably,
+		 * resulting in devices being stuck in clamshell mode.
+		 */
+		spin_lock_irqsave(&cbas_ec_lock, flags);
+		if (cbas_ec.input && cbas_ec.base_present) {
+			input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1);
+			input_sync(cbas_ec.input);
+		}
+		cbas_ec.base_present = false;
+		spin_unlock_irqrestore(&cbas_ec_lock, flags);
+	}
+
 	hammer_unregister_leds(hdev);
+
 	hid_hw_stop(hdev);
 }
 
Signature · 1 line
2.23.0.237.gc6a4ce50a0-goog
#2 lore raw

PATCH 3/3 HID: google: whiskers: signal tablet mode on connect

In reply to Dmitry Torokhov

When we receive "keyboard position" event from Whiskers we can conclude that the base is attached, even if we did not get EC event for that. We may miss EC event because there are some units which have a lot of leakage on the ADC pins that the EC uses to determine whether or not a base is attached.

---
 drivers/hid/hid-google-hammer.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/hid/hid-google-hammer.c +6 -6
index 3dc6116c8f76..31e4a39946f5 100644
@@ -402,16 +402,16 @@ static int hammer_event(struct hid_device *hid, struct hid_field *field,
 	    usage->hid == WHISKERS_KBD_FOLDED) {
 		spin_lock_irqsave(&cbas_ec_lock, flags);
 
+		/*
+		 * If we are getting events from Whiskers that means that it
+		 * is attached to the lid.
+		 */
+		cbas_ec.base_present = true;
 		cbas_ec.base_folded = value;
 		hid_dbg(hid, "%s: base: %d, folded: %d\n", __func__,
 			cbas_ec.base_present, cbas_ec.base_folded);
 
-		/*
-		 * We should not get event if base is detached, but in case
-		 * we happen to service HID and EC notifications out of order
-		 * let's still check the "base present" flag.
-		 */
-		if (cbas_ec.input && cbas_ec.base_present) {
+		if (cbas_ec.input) {
 			input_report_switch(cbas_ec.input,
 					    SW_TABLET_MODE, value);
 			input_sync(cbas_ec.input);
Signature · 1 line
2.23.0.237.gc6a4ce50a0-goog
#3 lore raw
In reply to Dmitry Torokhov
Quoted 8 lines · Dmitry Torokhov

The USB interface may get detected before the platform/EC one, so let's note the state of the base (if we receive event) and use it to correctly initialize the tablet mode switch state.

Also let's start the HID interface immediately when probing, this will ensure that we correctly process "base folded" events that may be sent as we initialize the base. Note that this requires us to add a release()

s/release/remove/ ?

Quoted 14 lines

function where we stop and close the hardware and switch the LED registration away from devm interface as we need to make sure that we destroy the LED instance before we stop the hardware.

---
 drivers/hid/hid-google-hammer.c | 71 ++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 15 deletions(-)
drivers/hid/hid-google-hammer.c +0 -0
index 84f8c127ebdc..4f64f93ddfcb 100644

drivers/hid/hid-google-hammer.c static int __cbas_ec_probe(struct platform_device *pdev) · line 213

Quoted 6 lines
drivers/hid/hid-google-hammer.c +2 -1
@@ -208,7 +209,14 @@ static int __cbas_ec_probe(struct platform_device *pdev)
                return error;
        }
 
-       input_report_switch(input, SW_TABLET_MODE, !cbas_ec.base_present);
+       if (!cbas_ec.base_present)
+               cbas_ec.base_folded = false;

I'm not sure to see why this is necessary? The folded base state should be in bss and false anyway, and even if it was true, it would not change the result of the expression below (!cbas_ec.base_present || cbas_ec.base_folded).

Quoted 9 lines
drivers/hid/hid-google-hammer.c +6 -0
+
+       dev_dbg(&pdev->dev, "%s: base: %d, folded: %d\n", __func__,
+               cbas_ec.base_present, cbas_ec.base_folded);
+
+       input_report_switch(input, SW_TABLET_MODE,
+                           !cbas_ec.base_present || cbas_ec.base_folded);
 
        cbas_ec_set_input(input);
 
#4 lore raw
Quoted 7 lines · Nicolas Boichat
Quoted 5 lines · Dmitry Torokhov

The USB interface may get detected before the platform/EC one, so let's note the state of the base (if we receive event) and use it to correctly initialize the tablet mode switch state.

Quoted 5 lines
Quoted 3 lines

Also let's start the HID interface immediately when probing, this will ensure that we correctly process "base folded" events that may be sent as we initialize the base. Note that this requires us to add a release()

s/release/remove/ ?

You are right.

Quoted 18 lines
Quoted 18 lines

function where we stop and close the hardware and switch the LED registration away from devm interface as we need to make sure that we destroy the LED instance before we stop the hardware.

---
 drivers/hid/hid-google-hammer.c | 71 ++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 15 deletions(-)
drivers/hid/hid-google-hammer.c +0 -0
index 84f8c127ebdc..4f64f93ddfcb 100644
@@ -208,7 +209,14 @@ static int __cbas_ec_probe(struct platform_device *pdev)
                return error;
        }
 
Quoted 8 lines

drivers/hid/hid-google-hammer.c static int __cbas_ec_probe(struct platform_device *pdev) · line 213

Quoted 3 lines
drivers/hid/hid-google-hammer.c +2 -1
-       input_report_switch(input, SW_TABLET_MODE, !cbas_ec.base_present);
+       if (!cbas_ec.base_present)
+               cbas_ec.base_folded = false;

I'm not sure to see why this is necessary? The folded base state should be in bss and false anyway, and even if it was true, it would not change the result of the expression below (!cbas_ec.base_present || cbas_ec.base_folded).

To have a more accurate printout generated by the line below. The fact that it is in bss it not relevant, as I can unbind and rebind the driver via sysfs, so it could have stale data from the previous run.

Quoted 10 lines

drivers/hid/hid-google-hammer.c static int __cbas_ec_probe(struct platform_device *pdev) · line 222

Quoted 10 lines
drivers/hid/hid-google-hammer.c +6 -0
+
+       dev_dbg(&pdev->dev, "%s: base: %d, folded: %d\n", __func__,
+               cbas_ec.base_present, cbas_ec.base_folded);
+
+       input_report_switch(input, SW_TABLET_MODE,
+                           !cbas_ec.base_present || cbas_ec.base_folded);
 
        cbas_ec_set_input(input);
 

Thanks,
Dmitry

maintainer
#5 lore raw
Quoted 11 lines · Dmitry Torokhov
Quoted 9 lines
Quoted 7 lines

The USB interface may get detected before the platform/EC one, so let's note the state of the base (if we receive event) and use it to correctly initialize the tablet mode switch state.

Also let's start the HID interface immediately when probing, this will ensure that we correctly process "base folded" events that may be sent as we initialize the base. Note that this requires us to add a release()

s/release/remove/ ?

You are right.

I'll fix that up when I am applying it (once 5.4 merge window is over).

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
maintainer
#6 lore raw
Quoted 8 lines · Jiri Kosina
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

The USB interface may get detected before the platform/EC one, so let's note the state of the base (if we receive event) and use it to correctly initialize the tablet mode switch state.

Also let's start the HID interface immediately when probing, this will ensure that we correctly process "base folded" events that may be sent as we initialize the base. Note that this requires us to add a release()

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

s/release/remove/ ?

You are right.

I'll fix that up when I am applying it (once 5.4 merge window is over).

Now applied (with the fixup made). Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs