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

2019-10-01 Thread Jiri Kosina
On Mon, 23 Sep 2019, Jiri Kosina wrote:

> > > > 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).

Now applied (with the fixup made). Thanks,

-- 
Jiri Kosina
SUSE Labs



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

2019-09-23 Thread Jiri Kosina
On Sun, 15 Sep 2019, Dmitry Torokhov wrote:

> > > 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,

-- 
Jiri Kosina
SUSE Labs



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

2019-09-15 Thread Dmitry Torokhov
On Sun, Sep 15, 2019 at 8:26 PM Nicolas Boichat  wrote:
>
> On Sat, Sep 14, 2019 at 6:03 AM Dmitry Torokhov  wrote:
> >
> > 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.

>
> > 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.
> >
> > Signed-off-by: Dmitry Torokhov 
> > ---
> >  drivers/hid/hid-google-hammer.c | 71 ++---
> >  1 file changed, 56 insertions(+), 15 deletions(-)
> >
> > diff --git a/drivers/hid/hid-google-hammer.c 
> > b/drivers/hid/hid-google-hammer.c
> > index 84f8c127ebdc..4f64f93ddfcb 100644
> > --- a/drivers/hid/hid-google-hammer.c
> > +++ b/drivers/hid/hid-google-hammer.c
> > @@ -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).

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.

>
> > +
> > +   dev_dbg(>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


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

2019-09-15 Thread Nicolas Boichat
On Sat, Sep 14, 2019 at 6:03 AM Dmitry Torokhov  wrote:
>
> 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/ ?

> 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.
>
> Signed-off-by: Dmitry Torokhov 
> ---
>  drivers/hid/hid-google-hammer.c | 71 ++---
>  1 file changed, 56 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/hid/hid-google-hammer.c b/drivers/hid/hid-google-hammer.c
> index 84f8c127ebdc..4f64f93ddfcb 100644
> --- a/drivers/hid/hid-google-hammer.c
> +++ b/drivers/hid/hid-google-hammer.c
> @@ -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).

> +
> +   dev_dbg(>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);
>


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

2019-09-13 Thread 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()
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.

Signed-off-by: Dmitry Torokhov 
---
 drivers/hid/hid-google-hammer.c | 71 ++---
 1 file changed, 56 insertions(+), 15 deletions(-)

diff --git a/drivers/hid/hid-google-hammer.c b/drivers/hid/hid-google-hammer.c
index 84f8c127ebdc..4f64f93ddfcb 100644
--- a/drivers/hid/hid-google-hammer.c
+++ b/drivers/hid/hid-google-hammer.c
@@ -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(>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(>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(_backlight->cdev, 0);
 
-   return devm_led_classdev_register(>dev, _backlight->cdev);
+   error = led_classdev_register(>dev, _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(_backlight->cdev);
+   kfree(kbd_backlight);
+   }
 }
 
 #define HID_UP_GOOGLEVENDOR0xffd1
@@ -376,8 +402,9 @@ static int hammer_event(struct hid_device *hid, struct 
hid_field *field,
usage->hid == WHISKERS_KBD_FOLDED) {
spin_lock_irqsave(_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 ==