linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] HID: google: Get HID report on probe to confirm tablet switch state
@ 2020-12-24  3:45 Nicolas Boichat
  2021-01-14 12:15 ` Jiri Kosina
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Boichat @ 2020-12-24  3:45 UTC (permalink / raw)
  To: Benjamin Tissoires
  Cc: Dmitry Torokhov, Nicolas Boichat, Jiri Kosina, linux-input, linux-kernel

This forces reading the base folded status anytime the device is
probed.

Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
---
Instead of all this manual parsing, it'd be easier to just call:
hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
However, that fails silently as hdev->driver_input_lock is held
during probe (or even some callbacks like input_configured.

 drivers/hid/hid-google-hammer.c | 85 +++++++++++++++++++++++++--------
 1 file changed, 66 insertions(+), 19 deletions(-)

diff --git a/drivers/hid/hid-google-hammer.c b/drivers/hid/hid-google-hammer.c
index 85a054f1ce38..d9319622da44 100644
--- a/drivers/hid/hid-google-hammer.c
+++ b/drivers/hid/hid-google-hammer.c
@@ -392,30 +392,34 @@ static int hammer_input_mapping(struct hid_device *hdev, struct hid_input *hi,
 	return 0;
 }
 
-static int hammer_event(struct hid_device *hid, struct hid_field *field,
-			struct hid_usage *usage, __s32 value)
+static void hammer_folded_event(struct hid_device *hdev, bool folded)
 {
 	unsigned long flags;
 
-	if (usage->hid == HID_USAGE_KBD_FOLDED) {
-		spin_lock_irqsave(&cbas_ec_lock, flags);
+	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);
-
-		if (cbas_ec.input) {
-			input_report_switch(cbas_ec.input,
-					    SW_TABLET_MODE, value);
-			input_sync(cbas_ec.input);
-		}
+	/*
+	 * 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 = folded;
+	hid_dbg(hdev, "%s: base: %d, folded: %d\n", __func__,
+		cbas_ec.base_present, cbas_ec.base_folded);
 
-		spin_unlock_irqrestore(&cbas_ec_lock, flags);
+	if (cbas_ec.input) {
+		input_report_switch(cbas_ec.input, SW_TABLET_MODE, folded);
+		input_sync(cbas_ec.input);
+	}
+
+	spin_unlock_irqrestore(&cbas_ec_lock, flags);
+}
+
+static int hammer_event(struct hid_device *hid, struct hid_field *field,
+			struct hid_usage *usage, __s32 value)
+{
+	if (usage->hid == HID_USAGE_KBD_FOLDED) {
+		hammer_folded_event(hid, value);
 		return 1; /* We handled this event */
 	}
 
@@ -457,6 +461,47 @@ static bool hammer_has_backlight_control(struct hid_device *hdev)
 				HID_GD_KEYBOARD, HID_AD_BRIGHTNESS);
 }
 
+static void hammer_get_folded_state(struct hid_device *hdev)
+{
+	struct hid_report *report;
+	char *buf;
+	int len, rlen;
+	int a;
+
+	report = hdev->report_enum[HID_INPUT_REPORT].report_id_hash[0x0];
+
+	if (!report || report->maxfield < 1)
+		return;
+
+	len = hid_report_len(report) + 1;
+
+	buf = kmalloc(len, GFP_KERNEL);
+	if (!buf)
+		return;
+
+	rlen = hid_hw_raw_request(hdev, report->id, buf, len, report->type, HID_REQ_GET_REPORT);
+
+	if (rlen != len) {
+		hid_warn(hdev, "Unable to read base folded state: %d (expected %d)\n", rlen, len);
+		goto out;
+	}
+
+	for (a = 0; a < report->maxfield; a++) {
+		struct hid_field *field = report->field[a];
+
+		if (field->usage->hid == HID_USAGE_KBD_FOLDED) {
+			u32 value = hid_field_extract(hdev, buf+1,
+					field->report_offset, field->report_size);
+
+			hammer_folded_event(hdev, value);
+			break;
+		}
+	}
+
+out:
+	kfree(buf);
+}
+
 static int hammer_probe(struct hid_device *hdev,
 			const struct hid_device_id *id)
 {
@@ -481,6 +526,8 @@ static int hammer_probe(struct hid_device *hdev,
 		error = hid_hw_open(hdev);
 		if (error)
 			return error;
+
+		hammer_get_folded_state(hdev);
 	}
 
 	if (hammer_has_backlight_control(hdev)) {
-- 
2.29.2.729.g45daf8777d-goog


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

* Re: [PATCH] HID: google: Get HID report on probe to confirm tablet switch state
  2020-12-24  3:45 [PATCH] HID: google: Get HID report on probe to confirm tablet switch state Nicolas Boichat
@ 2021-01-14 12:15 ` Jiri Kosina
  2021-01-15  5:34   ` Nicolas Boichat
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Kosina @ 2021-01-14 12:15 UTC (permalink / raw)
  To: Nicolas Boichat
  Cc: Benjamin Tissoires, Dmitry Torokhov, linux-input, linux-kernel

On Thu, 24 Dec 2020, Nicolas Boichat wrote:

> This forces reading the base folded status anytime the device is
> probed.

Could you please provide a little bit more verbose changelog (namely what 
is the actual problem this patch is fixing)? Thanks.

-- 
Jiri Kosina
SUSE Labs


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

* Re: [PATCH] HID: google: Get HID report on probe to confirm tablet switch state
  2021-01-14 12:15 ` Jiri Kosina
@ 2021-01-15  5:34   ` Nicolas Boichat
  0 siblings, 0 replies; 3+ messages in thread
From: Nicolas Boichat @ 2021-01-15  5:34 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Benjamin Tissoires, Dmitry Torokhov, open list:HID CORE LAYER, lkml

On Thu, Jan 14, 2021 at 8:15 PM Jiri Kosina <jikos@kernel.org> wrote:
>
> On Thu, 24 Dec 2020, Nicolas Boichat wrote:
>
> > This forces reading the base folded status anytime the device is
> > probed.
>
> Could you please provide a little bit more verbose changelog (namely what
> is the actual problem this patch is fixing)? Thanks.

Sure, I should have done this in the first place... v2 on the way.

>
> --
> Jiri Kosina
> SUSE Labs
>

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

end of thread, other threads:[~2021-01-15  5:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-24  3:45 [PATCH] HID: google: Get HID report on probe to confirm tablet switch state Nicolas Boichat
2021-01-14 12:15 ` Jiri Kosina
2021-01-15  5:34   ` Nicolas Boichat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).