Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7159033ybi; Mon, 8 Jul 2019 15:51:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqz827QWY2b7Wa212R2x/YhN6xsySJPjcvNYXJIE6EfJKkn6Dz7CVJ3jNgrwja1OlrE64EN2 X-Received: by 2002:a17:902:e011:: with SMTP id ca17mr28394767plb.328.1562626270294; Mon, 08 Jul 2019 15:51:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562626270; cv=none; d=google.com; s=arc-20160816; b=ZI8kaGYeu30iVGr6CzoVSdeW+H2nlPdpx7dYVKlPnAN83ZInV30k/aKhVJhpNWwIIw G64u3oYx1I3pkA+cBtrVjQ3jhlhB52XgzWpORLJpFH7oLX+jKkJKPH15ikPsP5FQsZ+b CWuiMOtWt9hKCDSCRxmqK3kfWY/+/i2DBjwzHfW4oMI4e0HxcDza5PygVfUzyl0kaRYl sAnSFSgV+A2A58wNiz348Gv319H2cIKAB111XqRISZtlJ78jJM0RNPhBwi8pAr8Jm+3/ k87WFzC48pHpzZAC4xotA387zqNaNgf3TaWu1orSAUA+DU6LzZR7aQ2oXcxxCOB129Mw iq8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=UuwCEqDKtFNaxLgpfGBTzCIagX0sLcEdRYJOztRbLvI=; b=slB/HPSJjwY55Ve3m68z3OseGhK2HAp1fJC19mTYmcqN7Un7R5JthxRZqjAC6PUDTY bbrrz/YhWSYzVrEoNosIdWkq7FcAyFWIxheEKOhEggcv+7FC+eXUHWqayNG5r8PzjaKB TOKTsOOdbzNug9iNafdKCWxWvZsIOuhiXMjMgSF31xqy2cGkOg1jhaOch9dnpTmutOf/ x0Jc+4KBUc4oQjZWh3ZkPowhXXWd5btloVs0eHK+OguQ8+p1F4lZrodhTRxHt/khzbwY hvsQ+BxJIHY+lCx+6SGHu6Vkj6ZPO03P0PrCe0xdncpfImApUkzXy9N17T3RlpRaB14R LYrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joaomoreno-com.20150623.gappssmtp.com header.s=20150623 header.b=SV9RDrTe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y19si12900945pll.326.2019.07.08.15.50.55; Mon, 08 Jul 2019 15:51:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@joaomoreno-com.20150623.gappssmtp.com header.s=20150623 header.b=SV9RDrTe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405069AbfGHUfR (ORCPT + 99 others); Mon, 8 Jul 2019 16:35:17 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:39949 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405062AbfGHUfR (ORCPT ); Mon, 8 Jul 2019 16:35:17 -0400 Received: by mail-wm1-f68.google.com with SMTP id v19so847241wmj.5 for ; Mon, 08 Jul 2019 13:35:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joaomoreno-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=UuwCEqDKtFNaxLgpfGBTzCIagX0sLcEdRYJOztRbLvI=; b=SV9RDrTexrJPqnpIhsttuUGYNlRae4HSdIDRfIAKXbk2G4xNrnWzH5HUyDmd+K1EPP QWsmjEqM+vCPc+CUm5F4n1LUqLMGWAejLBbDn1pSh0jSTQO7fDDUSOYL8b2H/ULWjlAF Pi1Vv2OwnewEY2EVekqfy91E6YfDadXEN1Bvhz0dP7clSbwegkVKf+IRUZyLbPuhtRBZ uSRT8jJdOPMH9RgD85x1us5uuX5B+ZV3P6rGtC8ZxmIF2mWN7owcs48xiP0r4OasH2md QWIkB8dVNSIxGEL3jfUQGktgfVvpgA/BycajGr3eLSP5q4il72ZplwNGHLdMvk88kY7C 1s7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=UuwCEqDKtFNaxLgpfGBTzCIagX0sLcEdRYJOztRbLvI=; b=E547TgGqCKFgIDovV5DErQOmmiI7wXti39tk4QQXdL44sYHS51+Om6Kt1Lq7JP0D7c r+ps3/6Gc66w74u06iqte9UxfkH9CEygRiH7pyW3EoyTjf4xBq6p2U7wbYUiFjKzhqU2 7s1WnlC52QUFIyPTkgKB4YSfmm+wPC1H3kpZW9Sd4SNFGoJBGsoyhGtjMNhS672fFpYi tGuOmSL7mI49C9Xz+b7o8/ZtFm41hUl/SbCeJdGvZhqfIQzvLDU23yUz1vFZLsxUGkC9 mVlzIITlXNieqTVMNudRl/g5WID31tt6FsuHFxCs148V56u7D5I6Ws8G4E7Yh6YeRyKH IrKg== X-Gm-Message-State: APjAAAWS2zRPajv5nIS/m7hgvxM1imRsfRtPyTD5bMFrlBNN1Nnko+A6 h/kB/wnSShIo9bfMFswFARnsDL3F4kt2ljrToel6FA== X-Received: by 2002:a1c:f70c:: with SMTP id v12mr18080402wmh.42.1562618115193; Mon, 08 Jul 2019 13:35:15 -0700 (PDT) MIME-Version: 1.0 References: <20190610213106.19342-1-mail@joaomoreno.com> In-Reply-To: From: =?UTF-8?B?Sm/Do28gTW9yZW5v?= Date: Mon, 8 Jul 2019 22:35:04 +0200 Message-ID: Subject: Re: [PATCH] HID: apple: Fix stuck function keys when using FN To: Benjamin Tissoires Cc: Jiri Kosina , "open list:HID CORE LAYER" , lkml Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Benjamin, No worries, also pretty busy over here. Didn't mean to press. On Mon, 1 Jul 2019 at 10:32, Benjamin Tissoires wrote: > > Hi Jo=C3=A3o, > > On Sun, Jun 30, 2019 at 10:15 PM Jo=C3=A3o Moreno w= rote: > > > > Hi Jiri & Benjamin, > > > > Let me know if you need something else to get this patch moving forward= . This > > fixes an issue I hit daily, it would be great to get it fixed. > > Sorry for the delay, I am very busy with internal corporate stuff, and > I tried setting up a new CI system at home, and instead of spending a > couple of ours, I am down to 2 weeks of hard work, without possibility > to switch to the new right now :( > Anyway. > > > > > Thanks. > > > > On Mon, 10 Jun 2019 at 23:31, Joao Moreno wrote: > > > > > > This fixes an issue in which key down events for function keys would = be > > > repeatedly emitted even after the user has raised the physical key. F= or > > > example, the driver fails to emit the F5 key up event when going thro= ugh > > > the following steps: > > > - fnmode=3D1: hold FN, hold F5, release FN, release F5 > > > - fnmode=3D2: hold F5, hold FN, release F5, release FN > > Ouch :/ > Right?! > > > > > > The repeated F5 key down events can be easily verified using xev. > > > > > > Signed-off-by: Joao Moreno > > > --- > > > drivers/hid/hid-apple.c | 21 +++++++++++---------- > > > 1 file changed, 11 insertions(+), 10 deletions(-) > > > > > > diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c > > > index 1cb41992aaa1..81867a6fa047 100644 > > > --- a/drivers/hid/hid-apple.c > > > +++ b/drivers/hid/hid-apple.c > > > @@ -205,20 +205,21 @@ static int hidinput_apple_event(struct hid_devi= ce *hid, struct input_dev *input, > > > trans =3D apple_find_translation (table, usage->code)= ; > > > > > > if (trans) { > > > - if (test_bit(usage->code, asc->pressed_fn)) > > > - do_translate =3D 1; > > > - else if (trans->flags & APPLE_FLAG_FKEY) > > > - do_translate =3D (fnmode =3D=3D 2 && = asc->fn_on) || > > > - (fnmode =3D=3D 1 && !asc->fn_= on); > > > + int fn_on =3D value ? asc->fn_on : > > > + test_bit(usage->code, asc->pressed_fn= ); > > > + > > > + if (!value) > > > + clear_bit(usage->code, asc->pressed_f= n); > > > + else if (asc->fn_on) > > > + set_bit(usage->code, asc->pressed_fn)= ; > > I have the feeling that this is not the correct fix here. > > I might be wrong, but the following sequence might also mess up the > driver state, depending on how the reports are emitted: > - hold FN, hold F4, hold F5, release F4, release FN, release F5 > I believe this should be fine. Following the code: - hold FN, sets asc->fn_on to true - hold F4, in the trans block fn_on will be true and we'll set the F4 bit in the bitmap - hold F5, in the trans block fn_on will be true and we'll set the F5 bit - release F4, in the trans block fn_on will be true (because of the bitmap)= and we'll clear the F4 bit - release FN, asc->fn_on will be false, but it doesn't matter since... - release F5, in the trans block we'll look into the bitmap (instead of asc->fn_on), so fn_on will be true and we'll clear the F5 bit I tested it in practice using my changes: Interestingly the Apple keyboard doesn't seem to emit an even for F5 when F= 4 is pressed, seems like a hardware limitation. But F6 does work. So, when I exe= cute these events in that order, everything works as it should: xev reports the following: KeyPress F4 KeyPress F6 KeyRelease F4 KeyRelease F6 > The reason is that the driver only considers you have one key pressed > with the modifier, and as the code changed its state based on the last > value. > I believe the bitmap takes care of storing the FN state per key press. The trick I did was to check on the global `asc->fn_on` state only when a key is pressed, but check on the bitmap instead when it's released. Let me know what you think. Am I missing something here? Cheers, Jo=C3=A3o. > IMO a better fix would: > > - keep the existing `trans` mapping lookout > - whenever a `trans` mapping gets found: > * get both translated and non-translated currently reported values > (`test_bit(keycode, input_dev->key)`) > * if one of them is set to true, then consider the keycode to be the > one of the key (no matter fn_on) > -> deal with `value` with the corrected keycode > * if the key was not pressed: > -> chose the keycode based on `fn_on` and `fnmode` states > and report the key press event > > This should remove the nasty pressed_fn state which depends on the > other pressed keys. > > Cheers, > Benjamin > > > > + > > > + if (trans->flags & APPLE_FLAG_FKEY) > > > + do_translate =3D (fnmode =3D=3D 2 && = fn_on) || > > > + (fnmode =3D=3D 1 && !fn_on); > > > else > > > do_translate =3D asc->fn_on; > > > > > > if (do_translate) { > > > - if (value) > > > - set_bit(usage->code, asc->pre= ssed_fn); > > > - else > > > - clear_bit(usage->code, asc->p= ressed_fn); > > > - > > > input_event(input, usage->type, trans= ->to, > > > value); > > > > > > -- > > > 2.19.1 > > >