* Re: [PATCH] input/psmouse: Don't hold the mutex while calling ->disconnect
From: Daniel Vetter @ 2018-10-01 9:23 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Stephen Lyons, intel-gfx, linux-input, Linux Kernel Mailing List,
Benjamin Tissoires, Arvind Yadav, Daniel Vetter
In-Reply-To: <CAKMK7uFDwANOAF3j43g4S0jQ4V40b-_oiX+QSy8SRkL26Zwxzg@mail.gmail.com>
On Wed, May 2, 2018 at 11:06 AM Daniel Vetter <daniel.vetter@ffwll.ch> wrote:
>
> On Mon, Apr 30, 2018 at 11:17 PM, Dmitry Torokhov
> <dmitry.torokhov@gmail.com> wrote:
> > Hi Daniel,
> >
> > On Mon, Apr 30, 2018 at 09:56:49PM +0200, Daniel Vetter wrote:
> >> At least trackpoint_disconnect wants to remove some sysfs files, and
> >> we can't remove sysfs files while holding psmouse_mutex:
> >>
> >> ======================================================
> >> WARNING: possible circular locking dependency detected
> >> 4.16.0-rc5-g613eb885b69e-drmtip_1+ #1 Tainted: G U
> >> ------------------------------------------------------
> >> kworker/0:3/102 is trying to acquire lock:
> >> (kn->count#130){++++}, at: [<000000009679748b>] kernfs_remove_by_name_ns+0x3b/0x80
> >>
> >> but task is already holding lock:
> >> (psmouse_mutex){+.+.}, at: [<0000000014f44bcc>] psmouse_disconnect+0x62/0x160
> >>
> >> which lock already depends on the new lock.
> >>
> >> the existing dependency chain (in reverse order) is:
> >>
> >> -> #1 (psmouse_mutex){+.+.}:
> >> psmouse_attr_set_helper+0x28/0x140
> >> kernfs_fop_write+0xfe/0x180
> >> __vfs_write+0x1e/0x130
> >> vfs_write+0xbd/0x1b0
> >> SyS_write+0x40/0xa0
> >> do_syscall_64+0x65/0x1a0
> >> entry_SYSCALL_64_after_hwframe+0x42/0xb7
> >>
> >> -> #0 (kn->count#130){++++}:
> >> __kernfs_remove+0x243/0x2b0
> >> kernfs_remove_by_name_ns+0x3b/0x80
> >> remove_files.isra.0+0x2b/0x60
> >> sysfs_remove_group+0x38/0x80
> >> sysfs_remove_groups+0x24/0x40
> >> trackpoint_disconnect+0x2c/0x50
> >> psmouse_disconnect+0x8f/0x160
> >> serio_disconnect_driver+0x28/0x40
> >> serio_driver_remove+0xc/0x10
> >> device_release_driver_internal+0x15b/0x230
> >> serio_handle_event+0x1c8/0x260
> >> process_one_work+0x215/0x620
> >> worker_thread+0x48/0x3a0
> >> kthread+0xfb/0x130
> >> ret_from_fork+0x3a/0x50
> >>
> >> other info that might help us debug this:
> >>
> >> Possible unsafe locking scenario:
> >>
> >> CPU0 CPU1
> >> ---- ----
> >> lock(psmouse_mutex);
> >> lock(kn->count#130);
> >> lock(psmouse_mutex);
> >> lock(kn->count#130);
> >>
> >> *** DEADLOCK ***
> >>
> >> 6 locks held by kworker/0:3/102:
> >> #0: ((wq_completion)"events_long"){+.+.}, at: [<000000002e408bfa>] process_one_work+0x191/0x620
> >> #1: (serio_event_work){+.+.}, at: [<000000002e408bfa>] process_one_work+0x191/0x620
> >> #2: (serio_mutex){+.+.}, at: [<00000000c8a49847>] serio_handle_event+0x23/0x260
> >> #3: (&dev->mutex){....}, at: [<00000000b55eee75>] device_release_driver_internal+0x2f/0x230
> >> #4: (&serio->drv_mutex){+.+.}, at: [<000000009719f997>] serio_disconnect_driver+0x16/0x40
> >> #5: (psmouse_mutex){+.+.}, at: [<0000000014f44bcc>] psmouse_disconnect+0x62/0x160
> >>
> >> stack backtrace:
> >> CPU: 0 PID: 102 Comm: kworker/0:3 Tainted: G U 4.16.0-rc5-g613eb885b69e-drmtip_1+ #1
> >> Hardware name: LENOVO 74591P0/74591P0, BIOS 6DET28WW (1.05 ) 07/30/2008
> >> Workqueue: events_long serio_handle_event
> >> Call Trace:
> >> dump_stack+0x5f/0x86
> >> print_circular_bug.isra.18+0x1d0/0x2c0
> >> __lock_acquire+0x14ae/0x1b60
> >> ? kernfs_remove_by_name_ns+0x20/0x80
> >> ? lock_acquire+0xaf/0x200
> >> lock_acquire+0xaf/0x200
> >> ? kernfs_remove_by_name_ns+0x3b/0x80
> >> __kernfs_remove+0x243/0x2b0
> >> ? kernfs_remove_by_name_ns+0x3b/0x80
> >> ? kernfs_name_hash+0xd/0x70
> >> ? kernfs_find_ns+0x7e/0x100
> >> kernfs_remove_by_name_ns+0x3b/0x80
> >> remove_files.isra.0+0x2b/0x60
> >> sysfs_remove_group+0x38/0x80
> >> sysfs_remove_groups+0x24/0x40
> >> trackpoint_disconnect+0x2c/0x50
> >> psmouse_disconnect+0x8f/0x160
> >> serio_disconnect_driver+0x28/0x40
> >> serio_driver_remove+0xc/0x10
> >> device_release_driver_internal+0x15b/0x230
> >> serio_handle_event+0x1c8/0x260
> >> process_one_work+0x215/0x620
> >> worker_thread+0x48/0x3a0
> >> ? _raw_spin_unlock_irqrestore+0x4c/0x60
> >> kthread+0xfb/0x130
> >> ? process_one_work+0x620/0x620
> >> ? _kthread_create_on_node+0x30/0x30
> >> ret_from_fork+0x3a/0x50
> >>
> >> Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
> >> Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> >> Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> >> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> >> Cc: Arvind Yadav <arvind.yadav.cs@gmail.com>
> >> Cc: Stephen Lyons <slysven@virginmedia.com>
> >> Cc: linux-input@vger.kernel.org
> >> ---
> >> drivers/input/mouse/psmouse-base.c | 2 ++
> >> 1 file changed, 2 insertions(+)
> >>
> >> diff --git a/drivers/input/mouse/psmouse-base.c b/drivers/input/mouse/psmouse-base.c
> >> index 8900c3166ebf..06ccd8e22f3c 100644
> >> --- a/drivers/input/mouse/psmouse-base.c
> >> +++ b/drivers/input/mouse/psmouse-base.c
> >> @@ -1484,8 +1484,10 @@ static void psmouse_disconnect(struct serio *serio)
> >> psmouse_deactivate(parent);
> >> }
> >>
> >> + mutex_unlock(&psmouse_mutex);
> >> if (psmouse->disconnect)
> >> psmouse->disconnect(psmouse);
> >> + mutex_lock(&psmouse_mutex);
> >
> > Why do you think it is proper to drop this mutex? It is introduced for a
> > reason.
> >
> > I think the trace you are seeing is due to:
> >
> > commit 988cd7afb3f37598891ca70b4c6eb914c338c46a
> > Author: Tejun Heo <tj@kernel.org>
> > Date: Mon Feb 3 14:02:58 2014 -0500
> >
> > kernfs: remove kernfs_addrm_cxt
> >
> > where we started taking kernfs_mutex when adding/removing sysfs files.
> > Ultimately I think we may have to change switching protocol to a work
> > item to be done asynchronously from writing to sysfs attribute.
>
> Well "holding a lock while adding/removing sysfs files and holding the
> same lock from sysfs read/write callbacks" is a classic deadlock. I've
> made lockdep shut up about it, I have no idea how to fix it properly.
> kernfs switching it's locking doesn't change anything here.
>
> Generally the fix is to untangle the locking: You need 1 set of locks
> to protect the datastructures exposed through sysfs, and another thing
> (maybe that's provided by serio already, I have no idea) to make sure
> you're ordering connect/disconnect handling correct and there's not
> concurrent calls to this hooks. Assuming serio does give that
> guarantee then there's no need to hold the lock while unregistering
> the sysfs files (we could perhaps push the lock dropping down into
> trackpoint_disconnect, but that's less maintainable imo since it's not
> obvious in psmouse_disconnect what's going on), and my patch is
> correct.
>
> But I didn't do a full locking audit of what exactly serio guarantees,
> and what exactly psmouse_mutex needs to protect (and where
> psmouse_mutex is only held because it's convenient).
Ping? Just noticed that I still have this bugfix hanging around.
bugfix as in "make lockdep more useful", not necessarily fixing the
locking here properly. I guess I could respin to add a FIXME comment,
but not sure how useful that would be.
-Daniel
--
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
^ permalink raw reply
* [PATCH 1/2] mfd: cros: add "base attached" MKBP switch definition
From: Dmitry Torokhov @ 2018-10-01 21:39 UTC (permalink / raw)
To: Jiri Kosina, Lee Jones
Cc: Benjamin Tissoires, linux-input, linux-kernel, Nicolas Boichat
This adds a "base attached" switch definition to the MKBP protocol that
is used by Whiskers driver to properly determine device state (clamshell
vs tablet mode).
Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
---
Lee, I was wondering if it woudl be OK for cros_ec_commands.h to be
merged through HID tree.
include/linux/mfd/cros_ec_commands.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/include/linux/mfd/cros_ec_commands.h b/include/linux/mfd/cros_ec_commands.h
index 20ee71f10865..5fd0e429f472 100644
--- a/include/linux/mfd/cros_ec_commands.h
+++ b/include/linux/mfd/cros_ec_commands.h
@@ -2132,6 +2132,7 @@ struct ec_response_get_next_event_v1 {
/* Switches */
#define EC_MKBP_LID_OPEN 0
#define EC_MKBP_TABLET_MODE 1
+#define EC_MKBP_BASE_ATTACHED 2
/*****************************************************************************/
/* Temperature sensor commands */
--
2.19.0.605.g01d371f741-goog
^ permalink raw reply related
* [PATCH 2/2] HID: google: add Whiskers driver to handle tablet mode properly
From: Dmitry Torokhov @ 2018-10-01 21:39 UTC (permalink / raw)
To: Jiri Kosina, Lee Jones
Cc: Benjamin Tissoires, linux-input, linux-kernel, Nicolas Boichat
In-Reply-To: <20181001213937.8774-1-dtor@chromium.org>
This adds dedicated "Whiskers" driver that hooks both into HID and EC to
produce proper SW_TABLET_SWITCH event from base presence bit from EC and
base state (folded/unfolded) coming from USB/HID.
Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
---
drivers/hid/Kconfig | 6 +
drivers/hid/Makefile | 1 +
drivers/hid/hid-google-hammer.c | 7 +-
drivers/hid/hid-google-whiskers.c | 382 ++++++++++++++++++++++++++++++
4 files changed, 392 insertions(+), 4 deletions(-)
create mode 100644 drivers/hid/hid-google-whiskers.c
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index e5ec47705fa2..0607f1c7129e 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -355,6 +355,12 @@ config HID_GOOGLE_HAMMER
---help---
Say Y here if you have a Google Hammer device.
+config HID_GOOGLE_WHISKERS
+ tristate "Google Whiskers Keyboard"
+ depends on HID_GOOGLE_HAMMER && MFD_CROS_EC
+ ---help---
+ Say Y here if you have a Google Whiskers device.
+
config HID_GT683R
tristate "MSI GT68xR LED support"
depends on LEDS_CLASS && USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index bd7ac53b75c5..241e583b6913 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -47,6 +47,7 @@ obj-$(CONFIG_HID_EZKEY) += hid-ezkey.o
obj-$(CONFIG_HID_GEMBIRD) += hid-gembird.o
obj-$(CONFIG_HID_GFRM) += hid-gfrm.o
obj-$(CONFIG_HID_GOOGLE_HAMMER) += hid-google-hammer.o
+obj-$(CONFIG_HID_GOOGLE_WHISKERS) += hid-google-whiskers.o
obj-$(CONFIG_HID_GT683R) += hid-gt683r.o
obj-$(CONFIG_HID_GYRATION) += hid-gyration.o
obj-$(CONFIG_HID_HOLTEK) += hid-holtek-kbd.o
diff --git a/drivers/hid/hid-google-hammer.c b/drivers/hid/hid-google-hammer.c
index 6bf4da7ad63a..81ddf9773df9 100644
--- a/drivers/hid/hid-google-hammer.c
+++ b/drivers/hid/hid-google-hammer.c
@@ -18,6 +18,7 @@
#include <linux/module.h>
#include "hid-ids.h"
+#include "hid-google-hammer.h"
#define MAX_BRIGHTNESS 100
@@ -90,8 +91,7 @@ static int hammer_register_leds(struct hid_device *hdev)
return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
}
-static int hammer_input_configured(struct hid_device *hdev,
- struct hid_input *hi)
+int hammer_input_configured(struct hid_device *hdev, struct hid_input *hi)
{
struct list_head *report_list =
&hdev->report_enum[HID_OUTPUT_REPORT].report_list;
@@ -116,6 +116,7 @@ static int hammer_input_configured(struct hid_device *hdev,
return 0;
}
+EXPORT_SYMBOL_GPL(hammer_input_configured);
static const struct hid_device_id hammer_devices[] = {
{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
@@ -124,8 +125,6 @@ static const struct hid_device_id hammer_devices[] = {
USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) },
{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) },
- { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
- USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
{ }
};
MODULE_DEVICE_TABLE(hid, hammer_devices);
diff --git a/drivers/hid/hid-google-whiskers.c b/drivers/hid/hid-google-whiskers.c
new file mode 100644
index 000000000000..ff3d2bfa1194
--- /dev/null
+++ b/drivers/hid/hid-google-whiskers.c
@@ -0,0 +1,382 @@
+// SPDX-License-Identifier: GPL-2.0+
+//
+// HID driver for Google Whiskers device.
+//
+// Copyright (c) 2018 Google Inc.
+
+#include <linux/acpi.h>
+#include <linux/hid.h>
+#include <linux/leds.h>
+#include <linux/mfd/cros_ec.h>
+#include <linux/mfd/cros_ec_commands.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/pm_wakeup.h>
+#include <linux/usb.h>
+#include <asm/unaligned.h>
+
+#include "hid-ids.h"
+#include "hid-google-hammer.h"
+
+#define HID_UP_GOOGLEVENDOR 0xffd10000
+#define HID_VD_KBD_FOLDED 0x00000019
+#define WHISKERS_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED)
+
+struct whiskers_ec {
+ struct device *dev; /* The platform device (EC) */
+ struct input_dev *input;
+ bool base_present;
+ struct notifier_block notifier;
+};
+
+static struct whiskers_ec whiskers_ec;
+static DEFINE_SPINLOCK(whiskers_ec_lock);
+static DEFINE_MUTEX(whiskers_ec_reglock);
+
+static bool whiskers_parse_base_state(const void *data)
+{
+ u32 switches = get_unaligned_le32(data);
+
+ return !!(switches & BIT(EC_MKBP_BASE_ATTACHED));
+}
+
+static int whiskers_ec_query_base(struct cros_ec_device *ec_dev, bool get_state,
+ bool *state)
+{
+ struct ec_params_mkbp_info *params;
+ struct cros_ec_command *msg;
+ int ret;
+
+ msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)),
+ GFP_KERNEL);
+ if (!msg)
+ return -ENOMEM;
+
+ msg->command = EC_CMD_MKBP_INFO;
+ msg->version = 1;
+ msg->outsize = sizeof(*params);
+ msg->insize = sizeof(u32);
+ params = (struct ec_params_mkbp_info *)msg->data;
+ params->info_type = get_state ?
+ EC_MKBP_INFO_CURRENT : EC_MKBP_INFO_SUPPORTED;
+ params->event_type = EC_MKBP_EVENT_SWITCH;
+
+ ret = cros_ec_cmd_xfer_status(ec_dev, msg);
+ if (ret >= 0) {
+ if (ret != sizeof(u32)) {
+ dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n",
+ ret, sizeof(u32));
+ ret = -EPROTO;
+ } else {
+ *state = whiskers_parse_base_state(msg->data);
+ ret = 0;
+ }
+ }
+
+ kfree(msg);
+
+ return ret;
+}
+
+static int whiskers_ec_notify(struct notifier_block *nb,
+ unsigned long queued_during_suspend,
+ void *_notify)
+{
+ struct cros_ec_device *ec = _notify;
+ unsigned long flags;
+ bool base_present;
+
+ if (ec->event_data.event_type == EC_MKBP_EVENT_SWITCH) {
+ base_present = whiskers_parse_base_state(
+ &ec->event_data.data.switches);
+ dev_dbg(whiskers_ec.dev,
+ "%s: base: %d\n", __func__, base_present);
+
+ if (device_may_wakeup(whiskers_ec.dev) ||
+ !queued_during_suspend) {
+
+ pm_wakeup_event(whiskers_ec.dev, 0);
+
+ spin_lock_irqsave(&whiskers_ec_lock, flags);
+
+ /*
+ * While input layer dedupes the events, we do not want
+ * to disrupt the state reported by the base by
+ * overriding it with state reported by the LID. Only
+ * report changes, as we assume that on attach the base
+ * is not folded.
+ */
+ if (base_present != whiskers_ec.base_present) {
+ input_report_switch(whiskers_ec.input,
+ SW_TABLET_MODE,
+ !base_present);
+ input_sync(whiskers_ec.input);
+ whiskers_ec.base_present = base_present;
+ }
+
+ spin_unlock_irqrestore(&whiskers_ec_lock, flags);
+ }
+ }
+
+ return NOTIFY_OK;
+}
+
+static __maybe_unused int whiskers_ec_resume(struct device *dev)
+{
+ struct cros_ec_device *ec = dev_get_drvdata(dev->parent);
+ bool base_present;
+ int error;
+
+ error = whiskers_ec_query_base(ec, true, &base_present);
+ if (error) {
+ dev_warn(dev, "failed to fetch base state on resume: %d\n",
+ error);
+ } else {
+ spin_lock_irq(&whiskers_ec_lock);
+
+ whiskers_ec.base_present = base_present;
+
+ /*
+ * Only report if base is disconnected. If base is connected,
+ * it will resend its state on resume, and we'll update it
+ * in whiskers_event().
+ */
+ if (!whiskers_ec.base_present) {
+ input_report_switch(whiskers_ec.input,
+ SW_TABLET_MODE, 1);
+ input_sync(whiskers_ec.input);
+ }
+
+ spin_unlock_irq(&whiskers_ec_lock);
+ }
+
+ return 0;
+}
+
+static SIMPLE_DEV_PM_OPS(whiskers_ec_pm_ops, NULL, whiskers_ec_resume);
+
+static void whiskers_ec_set_input(struct input_dev *input)
+{
+ /* Take the lock so whiskers_event does not race with us here */
+ spin_lock_irq(&whiskers_ec_lock);
+ whiskers_ec.input = input;
+ spin_unlock_irq(&whiskers_ec_lock);
+}
+
+static int __whiskers_ec_probe(struct platform_device *pdev)
+{
+ struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
+ struct input_dev *input;
+ bool base_supported;
+ int error;
+
+ error = whiskers_ec_query_base(ec, false, &base_supported);
+ if (error)
+ return error;
+
+ if (!base_supported)
+ return -ENXIO;
+
+ input = devm_input_allocate_device(&pdev->dev);
+ if (!input)
+ return -ENOMEM;
+
+ input->name = "Whiskers Tablet Mode Switch";
+
+ input->id.bustype = BUS_HOST;
+ input->id.version = 1;
+ input->id.product = 0;
+
+ input_set_capability(input, EV_SW, SW_TABLET_MODE);
+
+ error = input_register_device(input);
+ if (error) {
+ dev_err(&pdev->dev, "cannot register input device: %d\n",
+ error);
+ return error;
+ }
+
+ /* Seed the state */
+ error = whiskers_ec_query_base(ec, true, &whiskers_ec.base_present);
+ if (error) {
+ dev_err(&pdev->dev, "cannot query base state: %d\n", error);
+ return error;
+ }
+
+ input_report_switch(input, SW_TABLET_MODE, !whiskers_ec.base_present);
+
+ whiskers_ec_set_input(input);
+
+ whiskers_ec.dev = &pdev->dev;
+ whiskers_ec.notifier.notifier_call = whiskers_ec_notify;
+ error = blocking_notifier_chain_register(&ec->event_notifier,
+ &whiskers_ec.notifier);
+ if (error) {
+ dev_err(&pdev->dev, "cannot register notifier: %d\n", error);
+ whiskers_ec_set_input(NULL);
+ return error;
+ }
+
+ device_init_wakeup(&pdev->dev, true);
+ return 0;
+}
+
+static int whiskers_ec_probe(struct platform_device *pdev)
+{
+ int retval;
+
+ mutex_lock(&whiskers_ec_reglock);
+
+ if (whiskers_ec.input) {
+ retval = -EBUSY;
+ goto out;
+ }
+
+ retval = __whiskers_ec_probe(pdev);
+
+out:
+ mutex_unlock(&whiskers_ec_reglock);
+ return retval;
+}
+
+static int whiskers_ec_remove(struct platform_device *pdev)
+{
+ struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
+
+ mutex_lock(&whiskers_ec_reglock);
+
+ blocking_notifier_chain_unregister(&ec->event_notifier,
+ &whiskers_ec.notifier);
+ whiskers_ec_set_input(NULL);
+
+ mutex_unlock(&whiskers_ec_reglock);
+ return 0;
+}
+
+static const struct acpi_device_id whiskers_ec_acpi_ids[] = {
+ { "GOOG000B", 0 },
+ { }
+};
+MODULE_DEVICE_TABLE(acpi, whiskers_ec_acpi_ids);
+
+static struct platform_driver whiskers_ec_driver = {
+ .probe = whiskers_ec_probe,
+ .remove = whiskers_ec_remove,
+ .driver = {
+ .name = "whiskers_ec",
+ .acpi_match_table = ACPI_PTR(whiskers_ec_acpi_ids),
+ .pm = &whiskers_ec_pm_ops,
+ },
+};
+
+static int whiskers_input_mapping(struct hid_device *hdev, struct hid_input *hi,
+ struct hid_field *field,
+ struct hid_usage *usage,
+ unsigned long **bit, int *max)
+{
+ if (usage->hid == WHISKERS_KBD_FOLDED) {
+ /*
+ * We do not want to have this usage mapped as it will get
+ * mixed in with "base attached" signal and delivered over
+ * separate input device for tablet switch mode.
+ */
+
+ return -1;
+ }
+
+ return 0;
+}
+
+static int whiskers_event(struct hid_device *hid, struct hid_field *field,
+ struct hid_usage *usage, __s32 value)
+{
+ unsigned long flags;
+
+ if (usage->hid == WHISKERS_KBD_FOLDED) {
+ spin_lock_irqsave(&whiskers_ec_lock, flags);
+
+ hid_dbg(hid, "%s: base: %d, folded: %d\n", __func__,
+ whiskers_ec.base_present, value);
+
+ /*
+ * 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 (whiskers_ec.input && whiskers_ec.base_present) {
+ input_report_switch(whiskers_ec.input,
+ SW_TABLET_MODE, value);
+ input_sync(whiskers_ec.input);
+ }
+
+ spin_unlock_irqrestore(&whiskers_ec_lock, flags);
+ return 1; /* We handled this event */
+ }
+
+ return 0;
+}
+
+static int whiskers_probe(struct hid_device *hdev,
+ const struct hid_device_id *id)
+{
+ struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
+ int ret;
+
+ /*
+ * We always want to poll for, and handle tablet mode events, even when
+ * nobody has opened the input device. This also prevents the hid core
+ * from dropping early tablet mode events from the device.
+ */
+ if (intf->cur_altsetting->desc.bInterfaceProtocol ==
+ USB_INTERFACE_PROTOCOL_KEYBOARD)
+ hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
+
+ ret = hid_parse(hdev);
+ if (!ret)
+ ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+
+ return ret;
+}
+
+static const struct hid_device_id whiskers_hid_devices[] = {
+ { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
+ USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
+ { }
+};
+MODULE_DEVICE_TABLE(hid, whiskers_hid_devices);
+
+static struct hid_driver whiskers_hid_driver = {
+ .name = "whiskers",
+ .id_table = whiskers_hid_devices,
+ .probe = whiskers_probe,
+ .input_configured = hammer_input_configured,
+ .input_mapping = whiskers_input_mapping,
+ .event = whiskers_event,
+};
+
+static int __init whiskers_init(void)
+{
+ int error;
+
+ error = platform_driver_register(&whiskers_ec_driver);
+ if (error)
+ return error;
+
+ error = hid_register_driver(&whiskers_hid_driver);
+ if (error) {
+ platform_driver_unregister(&whiskers_ec_driver);
+ return error;
+ }
+
+ return 0;
+}
+module_init(whiskers_init);
+
+static void __exit whiskers_exit(void)
+{
+ hid_unregister_driver(&whiskers_hid_driver);
+ platform_driver_unregister(&whiskers_ec_driver);
+}
+module_exit(whiskers_exit);
+
+MODULE_LICENSE("GPL");
--
2.19.0.605.g01d371f741-goog
^ permalink raw reply related
* [PATCH] input: xilinx_ps2: Convert to using %pOFn instead of device_node.name
From: Rob Herring @ 2018-10-01 21:43 UTC (permalink / raw)
To: linux-kernel; +Cc: Dmitry Torokhov, Michal Simek, linux-input, linux-arm-kernel
In-Reply-To: <20181001214351.13230-1-robh@kernel.org>
In preparation to remove the node name pointer from struct device_node,
convert printf users to use the %pOFn format specifier.
Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Michal Simek <michal.simek@xilinx.com>
Cc: linux-input@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Signed-off-by: Rob Herring <robh@kernel.org>
---
drivers/input/serio/xilinx_ps2.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/input/serio/xilinx_ps2.c b/drivers/input/serio/xilinx_ps2.c
index 07de1b49293c..6615c02a08fd 100644
--- a/drivers/input/serio/xilinx_ps2.c
+++ b/drivers/input/serio/xilinx_ps2.c
@@ -245,7 +245,7 @@ static int xps2_of_probe(struct platform_device *ofdev)
unsigned int irq;
int error;
- dev_info(dev, "Device Tree Probing \'%s\'\n", dev->of_node->name);
+ dev_info(dev, "Device Tree Probing \'%pOFn\'\n", dev->of_node);
/* Get iospace for the device */
error = of_address_to_resource(dev->of_node, 0, &r_mem);
--
2.17.1
^ permalink raw reply related
* Re: [PATCH] input: xilinx_ps2: Convert to using %pOFn instead of device_node.name
From: Dmitry Torokhov @ 2018-10-01 23:29 UTC (permalink / raw)
To: Rob Herring; +Cc: linux-kernel, Michal Simek, linux-input, linux-arm-kernel
In-Reply-To: <20181001214351.13230-2-robh@kernel.org>
On Mon, Oct 01, 2018 at 04:43:49PM -0500, Rob Herring wrote:
> In preparation to remove the node name pointer from struct device_node,
> convert printf users to use the %pOFn format specifier.
>
> Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> Cc: Michal Simek <michal.simek@xilinx.com>
> Cc: linux-input@vger.kernel.org
> Cc: linux-arm-kernel@lists.infradead.org
> Signed-off-by: Rob Herring <robh@kernel.org>
Applied, thank you.
> ---
> drivers/input/serio/xilinx_ps2.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/input/serio/xilinx_ps2.c b/drivers/input/serio/xilinx_ps2.c
> index 07de1b49293c..6615c02a08fd 100644
> --- a/drivers/input/serio/xilinx_ps2.c
> +++ b/drivers/input/serio/xilinx_ps2.c
> @@ -245,7 +245,7 @@ static int xps2_of_probe(struct platform_device *ofdev)
> unsigned int irq;
> int error;
>
> - dev_info(dev, "Device Tree Probing \'%s\'\n", dev->of_node->name);
> + dev_info(dev, "Device Tree Probing \'%pOFn\'\n", dev->of_node);
>
> /* Get iospace for the device */
> error = of_address_to_resource(dev->of_node, 0, &r_mem);
> --
> 2.17.1
>
--
Dmitry
^ permalink raw reply
* Re: [PATCH 3/8] dt-bindings: marvell,mmp2: Add clock id for the SP clock
From: Stephen Boyd @ 2018-10-02 7:18 UTC (permalink / raw)
To: linux-kernel
Cc: linux-clk, devicetree, linux-input, Rob Herring, Mark Rutland,
Dmitry Torokhov, Michael Turquette, Lubomir Rintel
In-Reply-To: <20180910112654.42061-4-lkundrak@v3.sk>
Quoting Lubomir Rintel (2018-09-10 04:26:49)
> This is the clock for the "security processor" core.
>
> Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
> ---
Acked-by: Stephen Boyd <sboyd@kernel.org>
^ permalink raw reply
* Re: [PATCH 4/8] clk: mmp2: add SP clock
From: Stephen Boyd @ 2018-10-02 7:18 UTC (permalink / raw)
To: linux-kernel
Cc: linux-clk, devicetree, linux-input, Rob Herring, Mark Rutland,
Dmitry Torokhov, Michael Turquette, Lubomir Rintel
In-Reply-To: <20180910112654.42061-5-lkundrak@v3.sk>
Quoting Lubomir Rintel (2018-09-10 04:26:50)
> The "security processor", sometimes referred to as "wireless trusted
> module" or "generic encrypt unit" is a low-power core present on MMP2,
> that has nothing to do with security, wireless, trust or encryption.
>
> On an OLPC machine it runs CForth and serves as a keyboard controller:
> http://dev.laptop.org/git/users/wmb/cforth/tree/src/app/arm-xo-1.75/ps2.fth
>
> The register address was obtained from the OLPC kernel, since the
> datasheet seems to be the Marvell's most important business secret.
>
> Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
> ---
Acked-by: Stephen Boyd <sboyd@kernel.org>
^ permalink raw reply
* Re: [PATCH 2/2] HID: google: add Whiskers driver to handle tablet mode properly
From: Benjamin Tissoires @ 2018-10-02 8:53 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Jiri Kosina, lee.jones, open list:HID CORE LAYER, lkml,
Nicolas Boichat
In-Reply-To: <20181001213937.8774-2-dtor@chromium.org>
Hi Dmitry,
On Mon, Oct 1, 2018 at 11:39 PM Dmitry Torokhov <dtor@chromium.org> wrote:
>
> This adds dedicated "Whiskers" driver that hooks both into HID and EC to
> produce proper SW_TABLET_SWITCH event from base presence bit from EC and
> base state (folded/unfolded) coming from USB/HID.
>
> Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
> Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
> ---
I have a few comments, see below.
> drivers/hid/Kconfig | 6 +
> drivers/hid/Makefile | 1 +
> drivers/hid/hid-google-hammer.c | 7 +-
> drivers/hid/hid-google-whiskers.c | 382 ++++++++++++++++++++++++++++++
> 4 files changed, 392 insertions(+), 4 deletions(-)
> create mode 100644 drivers/hid/hid-google-whiskers.c
>
> diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
> index e5ec47705fa2..0607f1c7129e 100644
> --- a/drivers/hid/Kconfig
> +++ b/drivers/hid/Kconfig
> @@ -355,6 +355,12 @@ config HID_GOOGLE_HAMMER
> ---help---
> Say Y here if you have a Google Hammer device.
>
> +config HID_GOOGLE_WHISKERS
> + tristate "Google Whiskers Keyboard"
> + depends on HID_GOOGLE_HAMMER && MFD_CROS_EC
> + ---help---
> + Say Y here if you have a Google Whiskers device.
> +
> config HID_GT683R
> tristate "MSI GT68xR LED support"
> depends on LEDS_CLASS && USB_HID
> diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
> index bd7ac53b75c5..241e583b6913 100644
> --- a/drivers/hid/Makefile
> +++ b/drivers/hid/Makefile
> @@ -47,6 +47,7 @@ obj-$(CONFIG_HID_EZKEY) += hid-ezkey.o
> obj-$(CONFIG_HID_GEMBIRD) += hid-gembird.o
> obj-$(CONFIG_HID_GFRM) += hid-gfrm.o
> obj-$(CONFIG_HID_GOOGLE_HAMMER) += hid-google-hammer.o
> +obj-$(CONFIG_HID_GOOGLE_WHISKERS) += hid-google-whiskers.o
> obj-$(CONFIG_HID_GT683R) += hid-gt683r.o
> obj-$(CONFIG_HID_GYRATION) += hid-gyration.o
> obj-$(CONFIG_HID_HOLTEK) += hid-holtek-kbd.o
> diff --git a/drivers/hid/hid-google-hammer.c b/drivers/hid/hid-google-hammer.c
> index 6bf4da7ad63a..81ddf9773df9 100644
> --- a/drivers/hid/hid-google-hammer.c
> +++ b/drivers/hid/hid-google-hammer.c
> @@ -18,6 +18,7 @@
> #include <linux/module.h>
>
> #include "hid-ids.h"
> +#include "hid-google-hammer.h"
hid-google-hammer.h doesn't seem to be part of the patch (or in my
local tree at least).
>
> #define MAX_BRIGHTNESS 100
>
> @@ -90,8 +91,7 @@ static int hammer_register_leds(struct hid_device *hdev)
> return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
> }
>
> -static int hammer_input_configured(struct hid_device *hdev,
> - struct hid_input *hi)
> +int hammer_input_configured(struct hid_device *hdev, struct hid_input *hi)
> {
> struct list_head *report_list =
> &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
> @@ -116,6 +116,7 @@ static int hammer_input_configured(struct hid_device *hdev,
>
> return 0;
> }
> +EXPORT_SYMBOL_GPL(hammer_input_configured);
I am not sure there is a need to separate the driver in 2 for whiskers
if it still need the LED settings from hammer.
Given that the whiskers_ec struct will be populated independently,
having a common code path would not hurt hammer anyway.
>
> static const struct hid_device_id hammer_devices[] = {
> { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> @@ -124,8 +125,6 @@ static const struct hid_device_id hammer_devices[] = {
> USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) },
> { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) },
> - { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> - USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
> { }
> };
> MODULE_DEVICE_TABLE(hid, hammer_devices);
> diff --git a/drivers/hid/hid-google-whiskers.c b/drivers/hid/hid-google-whiskers.c
> new file mode 100644
> index 000000000000..ff3d2bfa1194
> --- /dev/null
> +++ b/drivers/hid/hid-google-whiskers.c
> @@ -0,0 +1,382 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// HID driver for Google Whiskers device.
> +//
> +// Copyright (c) 2018 Google Inc.
> +
> +#include <linux/acpi.h>
> +#include <linux/hid.h>
> +#include <linux/leds.h>
doesn't seem to be used
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/mfd/cros_ec_commands.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/pm_wakeup.h>
> +#include <linux/usb.h>
This is usually not a good sign in HID drivers to rely on the low
level transport.
Basically, if you replay the device through uhid, this tends to break
heavily the kernel.
> +#include <asm/unaligned.h>
> +
> +#include "hid-ids.h"
> +#include "hid-google-hammer.h"
> +
> +#define HID_UP_GOOGLEVENDOR 0xffd10000
> +#define HID_VD_KBD_FOLDED 0x00000019
> +#define WHISKERS_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED)
Huh, this is weird there is no standard HID usage for this (not that
you can do anything about it).
> +
> +struct whiskers_ec {
> + struct device *dev; /* The platform device (EC) */
> + struct input_dev *input;
> + bool base_present;
> + struct notifier_block notifier;
> +};
> +
> +static struct whiskers_ec whiskers_ec;
> +static DEFINE_SPINLOCK(whiskers_ec_lock);
> +static DEFINE_MUTEX(whiskers_ec_reglock);
> +
> +static bool whiskers_parse_base_state(const void *data)
> +{
> + u32 switches = get_unaligned_le32(data);
> +
> + return !!(switches & BIT(EC_MKBP_BASE_ATTACHED));
> +}
> +
> +static int whiskers_ec_query_base(struct cros_ec_device *ec_dev, bool get_state,
> + bool *state)
> +{
> + struct ec_params_mkbp_info *params;
> + struct cros_ec_command *msg;
> + int ret;
> +
> + msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)),
> + GFP_KERNEL);
> + if (!msg)
> + return -ENOMEM;
> +
> + msg->command = EC_CMD_MKBP_INFO;
> + msg->version = 1;
> + msg->outsize = sizeof(*params);
> + msg->insize = sizeof(u32);
> + params = (struct ec_params_mkbp_info *)msg->data;
> + params->info_type = get_state ?
> + EC_MKBP_INFO_CURRENT : EC_MKBP_INFO_SUPPORTED;
> + params->event_type = EC_MKBP_EVENT_SWITCH;
> +
> + ret = cros_ec_cmd_xfer_status(ec_dev, msg);
> + if (ret >= 0) {
> + if (ret != sizeof(u32)) {
> + dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n",
> + ret, sizeof(u32));
> + ret = -EPROTO;
> + } else {
> + *state = whiskers_parse_base_state(msg->data);
> + ret = 0;
> + }
> + }
> +
> + kfree(msg);
> +
> + return ret;
> +}
> +
> +static int whiskers_ec_notify(struct notifier_block *nb,
> + unsigned long queued_during_suspend,
> + void *_notify)
> +{
> + struct cros_ec_device *ec = _notify;
> + unsigned long flags;
> + bool base_present;
> +
> + if (ec->event_data.event_type == EC_MKBP_EVENT_SWITCH) {
> + base_present = whiskers_parse_base_state(
> + &ec->event_data.data.switches);
> + dev_dbg(whiskers_ec.dev,
> + "%s: base: %d\n", __func__, base_present);
> +
> + if (device_may_wakeup(whiskers_ec.dev) ||
> + !queued_during_suspend) {
> +
> + pm_wakeup_event(whiskers_ec.dev, 0);
> +
> + spin_lock_irqsave(&whiskers_ec_lock, flags);
> +
> + /*
> + * While input layer dedupes the events, we do not want
> + * to disrupt the state reported by the base by
> + * overriding it with state reported by the LID. Only
> + * report changes, as we assume that on attach the base
> + * is not folded.
> + */
> + if (base_present != whiskers_ec.base_present) {
> + input_report_switch(whiskers_ec.input,
> + SW_TABLET_MODE,
> + !base_present);
> + input_sync(whiskers_ec.input);
> + whiskers_ec.base_present = base_present;
> + }
> +
> + spin_unlock_irqrestore(&whiskers_ec_lock, flags);
> + }
> + }
> +
> + return NOTIFY_OK;
> +}
> +
> +static __maybe_unused int whiskers_ec_resume(struct device *dev)
> +{
> + struct cros_ec_device *ec = dev_get_drvdata(dev->parent);
> + bool base_present;
> + int error;
> +
> + error = whiskers_ec_query_base(ec, true, &base_present);
> + if (error) {
> + dev_warn(dev, "failed to fetch base state on resume: %d\n",
> + error);
> + } else {
> + spin_lock_irq(&whiskers_ec_lock);
> +
> + whiskers_ec.base_present = base_present;
> +
> + /*
> + * Only report if base is disconnected. If base is connected,
> + * it will resend its state on resume, and we'll update it
> + * in whiskers_event().
> + */
> + if (!whiskers_ec.base_present) {
> + input_report_switch(whiskers_ec.input,
> + SW_TABLET_MODE, 1);
> + input_sync(whiskers_ec.input);
> + }
> +
> + spin_unlock_irq(&whiskers_ec_lock);
> + }
> +
> + return 0;
> +}
> +
> +static SIMPLE_DEV_PM_OPS(whiskers_ec_pm_ops, NULL, whiskers_ec_resume);
> +
> +static void whiskers_ec_set_input(struct input_dev *input)
> +{
> + /* Take the lock so whiskers_event does not race with us here */
> + spin_lock_irq(&whiskers_ec_lock);
> + whiskers_ec.input = input;
> + spin_unlock_irq(&whiskers_ec_lock);
> +}
> +
> +static int __whiskers_ec_probe(struct platform_device *pdev)
> +{
> + struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> + struct input_dev *input;
> + bool base_supported;
> + int error;
> +
> + error = whiskers_ec_query_base(ec, false, &base_supported);
> + if (error)
> + return error;
> +
> + if (!base_supported)
> + return -ENXIO;
> +
> + input = devm_input_allocate_device(&pdev->dev);
> + if (!input)
> + return -ENOMEM;
> +
> + input->name = "Whiskers Tablet Mode Switch";
> +
> + input->id.bustype = BUS_HOST;
> + input->id.version = 1;
> + input->id.product = 0;
I wonder if you should not set the vendor to USB_VENDOR_ID_GOOGLE and
the product to something better.
> +
> + input_set_capability(input, EV_SW, SW_TABLET_MODE);
> +
> + error = input_register_device(input);
> + if (error) {
> + dev_err(&pdev->dev, "cannot register input device: %d\n",
> + error);
> + return error;
> + }
> +
> + /* Seed the state */
> + error = whiskers_ec_query_base(ec, true, &whiskers_ec.base_present);
> + if (error) {
> + dev_err(&pdev->dev, "cannot query base state: %d\n", error);
> + return error;
> + }
> +
> + input_report_switch(input, SW_TABLET_MODE, !whiskers_ec.base_present);
> +
> + whiskers_ec_set_input(input);
> +
> + whiskers_ec.dev = &pdev->dev;
> + whiskers_ec.notifier.notifier_call = whiskers_ec_notify;
> + error = blocking_notifier_chain_register(&ec->event_notifier,
> + &whiskers_ec.notifier);
> + if (error) {
> + dev_err(&pdev->dev, "cannot register notifier: %d\n", error);
> + whiskers_ec_set_input(NULL);
> + return error;
> + }
> +
> + device_init_wakeup(&pdev->dev, true);
> + return 0;
> +}
> +
> +static int whiskers_ec_probe(struct platform_device *pdev)
> +{
> + int retval;
> +
> + mutex_lock(&whiskers_ec_reglock);
> +
> + if (whiskers_ec.input) {
> + retval = -EBUSY;
> + goto out;
> + }
> +
> + retval = __whiskers_ec_probe(pdev);
> +
> +out:
> + mutex_unlock(&whiskers_ec_reglock);
> + return retval;
> +}
> +
> +static int whiskers_ec_remove(struct platform_device *pdev)
> +{
> + struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> +
> + mutex_lock(&whiskers_ec_reglock);
> +
> + blocking_notifier_chain_unregister(&ec->event_notifier,
> + &whiskers_ec.notifier);
> + whiskers_ec_set_input(NULL);
> +
> + mutex_unlock(&whiskers_ec_reglock);
> + return 0;
> +}
> +
> +static const struct acpi_device_id whiskers_ec_acpi_ids[] = {
> + { "GOOG000B", 0 },
> + { }
> +};
> +MODULE_DEVICE_TABLE(acpi, whiskers_ec_acpi_ids);
> +
> +static struct platform_driver whiskers_ec_driver = {
> + .probe = whiskers_ec_probe,
> + .remove = whiskers_ec_remove,
> + .driver = {
> + .name = "whiskers_ec",
> + .acpi_match_table = ACPI_PTR(whiskers_ec_acpi_ids),
> + .pm = &whiskers_ec_pm_ops,
> + },
> +};
> +
> +static int whiskers_input_mapping(struct hid_device *hdev, struct hid_input *hi,
> + struct hid_field *field,
> + struct hid_usage *usage,
> + unsigned long **bit, int *max)
> +{
> + if (usage->hid == WHISKERS_KBD_FOLDED) {
> + /*
> + * We do not want to have this usage mapped as it will get
> + * mixed in with "base attached" signal and delivered over
> + * separate input device for tablet switch mode.
> + */
> +
> + return -1;
> + }
> +
> + return 0;
> +}
> +
> +static int whiskers_event(struct hid_device *hid, struct hid_field *field,
> + struct hid_usage *usage, __s32 value)
> +{
> + unsigned long flags;
> +
> + if (usage->hid == WHISKERS_KBD_FOLDED) {
> + spin_lock_irqsave(&whiskers_ec_lock, flags);
> +
> + hid_dbg(hid, "%s: base: %d, folded: %d\n", __func__,
> + whiskers_ec.base_present, value);
> +
> + /*
> + * 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 (whiskers_ec.input && whiskers_ec.base_present) {
> + input_report_switch(whiskers_ec.input,
> + SW_TABLET_MODE, value);
> + input_sync(whiskers_ec.input);
> + }
> +
> + spin_unlock_irqrestore(&whiskers_ec_lock, flags);
> + return 1; /* We handled this event */
> + }
> +
> + return 0;
> +}
> +
> +static int whiskers_probe(struct hid_device *hdev,
> + const struct hid_device_id *id)
> +{
> + struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
As mentioned earlier, this is prone to kernel oopses when using uhid.
> + int ret;
> +
> + /*
> + * We always want to poll for, and handle tablet mode events, even when
> + * nobody has opened the input device. This also prevents the hid core
> + * from dropping early tablet mode events from the device.
> + */
> + if (intf->cur_altsetting->desc.bInterfaceProtocol ==
> + USB_INTERFACE_PROTOCOL_KEYBOARD)
Can't you rely on something embedded in the report descriptor instead?
This way you can drop the USB dependency.
> + hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
> +
> + ret = hid_parse(hdev);
> + if (!ret)
> + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
> +
> + return ret;
> +}
> +
> +static const struct hid_device_id whiskers_hid_devices[] = {
> + { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> + USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
> + { }
> +};
> +MODULE_DEVICE_TABLE(hid, whiskers_hid_devices);
> +
> +static struct hid_driver whiskers_hid_driver = {
> + .name = "whiskers",
> + .id_table = whiskers_hid_devices,
> + .probe = whiskers_probe,
> + .input_configured = hammer_input_configured,
> + .input_mapping = whiskers_input_mapping,
> + .event = whiskers_event,
> +};
> +
> +static int __init whiskers_init(void)
> +{
> + int error;
> +
> + error = platform_driver_register(&whiskers_ec_driver);
> + if (error)
> + return error;
> +
> + error = hid_register_driver(&whiskers_hid_driver);
> + if (error) {
> + platform_driver_unregister(&whiskers_ec_driver);
> + return error;
> + }
> +
> + return 0;
> +}
> +module_init(whiskers_init);
> +
> +static void __exit whiskers_exit(void)
> +{
> + hid_unregister_driver(&whiskers_hid_driver);
> + platform_driver_unregister(&whiskers_ec_driver);
> +}
> +module_exit(whiskers_exit);
> +
> +MODULE_LICENSE("GPL");
> --
> 2.19.0.605.g01d371f741-goog
>
Cheers,
Benjamin
^ permalink raw reply
* Re: [PATCH 2/2] HID: google: add Whiskers driver to handle tablet mode properly
From: Dmitry Torokhov @ 2018-10-02 18:24 UTC (permalink / raw)
To: Benjamin Tissoires
Cc: Jiri Kosina, Lee Jones, open list:HID CORE LAYER, lkml, drinkcat
In-Reply-To: <CAO-hwJKASi3+aFac8OR5XqESCF8RgB_aWZySAiMf+GHNh-2b0g@mail.gmail.com>
Hi Benjamin,
On Tue, Oct 2, 2018 at 1:53 AM Benjamin Tissoires
<benjamin.tissoires@redhat.com> wrote:
>
> Hi Dmitry,
>
> On Mon, Oct 1, 2018 at 11:39 PM Dmitry Torokhov <dtor@chromium.org> wrote:
> >
> > This adds dedicated "Whiskers" driver that hooks both into HID and EC to
> > produce proper SW_TABLET_SWITCH event from base presence bit from EC and
> > base state (folded/unfolded) coming from USB/HID.
> >
> > Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
> > Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
> > ---
>
> I have a few comments, see below.
>
> > drivers/hid/Kconfig | 6 +
> > drivers/hid/Makefile | 1 +
> > drivers/hid/hid-google-hammer.c | 7 +-
> > drivers/hid/hid-google-whiskers.c | 382 ++++++++++++++++++++++++++++++
> > 4 files changed, 392 insertions(+), 4 deletions(-)
> > create mode 100644 drivers/hid/hid-google-whiskers.c
> >
> > diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
> > index e5ec47705fa2..0607f1c7129e 100644
> > --- a/drivers/hid/Kconfig
> > +++ b/drivers/hid/Kconfig
> > @@ -355,6 +355,12 @@ config HID_GOOGLE_HAMMER
> > ---help---
> > Say Y here if you have a Google Hammer device.
> >
> > +config HID_GOOGLE_WHISKERS
> > + tristate "Google Whiskers Keyboard"
> > + depends on HID_GOOGLE_HAMMER && MFD_CROS_EC
> > + ---help---
> > + Say Y here if you have a Google Whiskers device.
> > +
> > config HID_GT683R
> > tristate "MSI GT68xR LED support"
> > depends on LEDS_CLASS && USB_HID
> > diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
> > index bd7ac53b75c5..241e583b6913 100644
> > --- a/drivers/hid/Makefile
> > +++ b/drivers/hid/Makefile
> > @@ -47,6 +47,7 @@ obj-$(CONFIG_HID_EZKEY) += hid-ezkey.o
> > obj-$(CONFIG_HID_GEMBIRD) += hid-gembird.o
> > obj-$(CONFIG_HID_GFRM) += hid-gfrm.o
> > obj-$(CONFIG_HID_GOOGLE_HAMMER) += hid-google-hammer.o
> > +obj-$(CONFIG_HID_GOOGLE_WHISKERS) += hid-google-whiskers.o
> > obj-$(CONFIG_HID_GT683R) += hid-gt683r.o
> > obj-$(CONFIG_HID_GYRATION) += hid-gyration.o
> > obj-$(CONFIG_HID_HOLTEK) += hid-holtek-kbd.o
> > diff --git a/drivers/hid/hid-google-hammer.c b/drivers/hid/hid-google-hammer.c
> > index 6bf4da7ad63a..81ddf9773df9 100644
> > --- a/drivers/hid/hid-google-hammer.c
> > +++ b/drivers/hid/hid-google-hammer.c
> > @@ -18,6 +18,7 @@
> > #include <linux/module.h>
> >
> > #include "hid-ids.h"
> > +#include "hid-google-hammer.h"
>
> hid-google-hammer.h doesn't seem to be part of the patch (or in my
> local tree at least).
Ugh, my bad.
>
> >
> > #define MAX_BRIGHTNESS 100
> >
> > @@ -90,8 +91,7 @@ static int hammer_register_leds(struct hid_device *hdev)
> > return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
> > }
> >
> > -static int hammer_input_configured(struct hid_device *hdev,
> > - struct hid_input *hi)
> > +int hammer_input_configured(struct hid_device *hdev, struct hid_input *hi)
> > {
> > struct list_head *report_list =
> > &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
> > @@ -116,6 +116,7 @@ static int hammer_input_configured(struct hid_device *hdev,
> >
> > return 0;
> > }
> > +EXPORT_SYMBOL_GPL(hammer_input_configured);
>
> I am not sure there is a need to separate the driver in 2 for whiskers
> if it still need the LED settings from hammer.
>
> Given that the whiskers_ec struct will be populated independently,
> having a common code path would not hurt hammer anyway.
The idea is that we we have device with Hammer, we do not need to load
bunch of code that deals with properly supporting Whiskers (connecting
to the EC, creating a tablet switch device).
>
> >
> > static const struct hid_device_id hammer_devices[] = {
> > { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> > @@ -124,8 +125,6 @@ static const struct hid_device_id hammer_devices[] = {
> > USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) },
> > { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> > USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) },
> > - { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> > - USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
> > { }
> > };
> > MODULE_DEVICE_TABLE(hid, hammer_devices);
> > diff --git a/drivers/hid/hid-google-whiskers.c b/drivers/hid/hid-google-whiskers.c
> > new file mode 100644
> > index 000000000000..ff3d2bfa1194
> > --- /dev/null
> > +++ b/drivers/hid/hid-google-whiskers.c
> > @@ -0,0 +1,382 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +//
> > +// HID driver for Google Whiskers device.
> > +//
> > +// Copyright (c) 2018 Google Inc.
> > +
> > +#include <linux/acpi.h>
> > +#include <linux/hid.h>
> > +#include <linux/leds.h>
>
> doesn't seem to be used
Indeed, will drop.
>
> > +#include <linux/mfd/cros_ec.h>
> > +#include <linux/mfd/cros_ec_commands.h>
> > +#include <linux/module.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/pm_wakeup.h>
> > +#include <linux/usb.h>
>
> This is usually not a good sign in HID drivers to rely on the low
> level transport.
> Basically, if you replay the device through uhid, this tends to break
> heavily the kernel.
The data parsing relies solely on HID, USB is here just to make sure
we bind to the right interface, as you can see below.
>
> > +#include <asm/unaligned.h>
> > +
> > +#include "hid-ids.h"
> > +#include "hid-google-hammer.h"
> > +
> > +#define HID_UP_GOOGLEVENDOR 0xffd10000
> > +#define HID_VD_KBD_FOLDED 0x00000019
> > +#define WHISKERS_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED)
>
> Huh, this is weird there is no standard HID usage for this (not that
> you can do anything about it).
This is really specific to this device, I am not sure if it makes
sense to standardize it. I guess we'll see if we make more of these.
>
> > +
> > +struct whiskers_ec {
> > + struct device *dev; /* The platform device (EC) */
> > + struct input_dev *input;
> > + bool base_present;
> > + struct notifier_block notifier;
> > +};
> > +
> > +static struct whiskers_ec whiskers_ec;
> > +static DEFINE_SPINLOCK(whiskers_ec_lock);
> > +static DEFINE_MUTEX(whiskers_ec_reglock);
> > +
> > +static bool whiskers_parse_base_state(const void *data)
> > +{
> > + u32 switches = get_unaligned_le32(data);
> > +
> > + return !!(switches & BIT(EC_MKBP_BASE_ATTACHED));
> > +}
> > +
> > +static int whiskers_ec_query_base(struct cros_ec_device *ec_dev, bool get_state,
> > + bool *state)
> > +{
> > + struct ec_params_mkbp_info *params;
> > + struct cros_ec_command *msg;
> > + int ret;
> > +
> > + msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)),
> > + GFP_KERNEL);
> > + if (!msg)
> > + return -ENOMEM;
> > +
> > + msg->command = EC_CMD_MKBP_INFO;
> > + msg->version = 1;
> > + msg->outsize = sizeof(*params);
> > + msg->insize = sizeof(u32);
> > + params = (struct ec_params_mkbp_info *)msg->data;
> > + params->info_type = get_state ?
> > + EC_MKBP_INFO_CURRENT : EC_MKBP_INFO_SUPPORTED;
> > + params->event_type = EC_MKBP_EVENT_SWITCH;
> > +
> > + ret = cros_ec_cmd_xfer_status(ec_dev, msg);
> > + if (ret >= 0) {
> > + if (ret != sizeof(u32)) {
> > + dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n",
> > + ret, sizeof(u32));
> > + ret = -EPROTO;
> > + } else {
> > + *state = whiskers_parse_base_state(msg->data);
> > + ret = 0;
> > + }
> > + }
> > +
> > + kfree(msg);
> > +
> > + return ret;
> > +}
> > +
> > +static int whiskers_ec_notify(struct notifier_block *nb,
> > + unsigned long queued_during_suspend,
> > + void *_notify)
> > +{
> > + struct cros_ec_device *ec = _notify;
> > + unsigned long flags;
> > + bool base_present;
> > +
> > + if (ec->event_data.event_type == EC_MKBP_EVENT_SWITCH) {
> > + base_present = whiskers_parse_base_state(
> > + &ec->event_data.data.switches);
> > + dev_dbg(whiskers_ec.dev,
> > + "%s: base: %d\n", __func__, base_present);
> > +
> > + if (device_may_wakeup(whiskers_ec.dev) ||
> > + !queued_during_suspend) {
> > +
> > + pm_wakeup_event(whiskers_ec.dev, 0);
> > +
> > + spin_lock_irqsave(&whiskers_ec_lock, flags);
> > +
> > + /*
> > + * While input layer dedupes the events, we do not want
> > + * to disrupt the state reported by the base by
> > + * overriding it with state reported by the LID. Only
> > + * report changes, as we assume that on attach the base
> > + * is not folded.
> > + */
> > + if (base_present != whiskers_ec.base_present) {
> > + input_report_switch(whiskers_ec.input,
> > + SW_TABLET_MODE,
> > + !base_present);
> > + input_sync(whiskers_ec.input);
> > + whiskers_ec.base_present = base_present;
> > + }
> > +
> > + spin_unlock_irqrestore(&whiskers_ec_lock, flags);
> > + }
> > + }
> > +
> > + return NOTIFY_OK;
> > +}
> > +
> > +static __maybe_unused int whiskers_ec_resume(struct device *dev)
> > +{
> > + struct cros_ec_device *ec = dev_get_drvdata(dev->parent);
> > + bool base_present;
> > + int error;
> > +
> > + error = whiskers_ec_query_base(ec, true, &base_present);
> > + if (error) {
> > + dev_warn(dev, "failed to fetch base state on resume: %d\n",
> > + error);
> > + } else {
> > + spin_lock_irq(&whiskers_ec_lock);
> > +
> > + whiskers_ec.base_present = base_present;
> > +
> > + /*
> > + * Only report if base is disconnected. If base is connected,
> > + * it will resend its state on resume, and we'll update it
> > + * in whiskers_event().
> > + */
> > + if (!whiskers_ec.base_present) {
> > + input_report_switch(whiskers_ec.input,
> > + SW_TABLET_MODE, 1);
> > + input_sync(whiskers_ec.input);
> > + }
> > +
> > + spin_unlock_irq(&whiskers_ec_lock);
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static SIMPLE_DEV_PM_OPS(whiskers_ec_pm_ops, NULL, whiskers_ec_resume);
> > +
> > +static void whiskers_ec_set_input(struct input_dev *input)
> > +{
> > + /* Take the lock so whiskers_event does not race with us here */
> > + spin_lock_irq(&whiskers_ec_lock);
> > + whiskers_ec.input = input;
> > + spin_unlock_irq(&whiskers_ec_lock);
> > +}
> > +
> > +static int __whiskers_ec_probe(struct platform_device *pdev)
> > +{
> > + struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> > + struct input_dev *input;
> > + bool base_supported;
> > + int error;
> > +
> > + error = whiskers_ec_query_base(ec, false, &base_supported);
> > + if (error)
> > + return error;
> > +
> > + if (!base_supported)
> > + return -ENXIO;
> > +
> > + input = devm_input_allocate_device(&pdev->dev);
> > + if (!input)
> > + return -ENOMEM;
> > +
> > + input->name = "Whiskers Tablet Mode Switch";
> > +
> > + input->id.bustype = BUS_HOST;
> > + input->id.version = 1;
> > + input->id.product = 0;
>
> I wonder if you should not set the vendor to USB_VENDOR_ID_GOOGLE and
> the product to something better.
This is not USB interface, but host one (coming from the EC), so
USB_VENDOR_ID_GOOGLE does not make sense here. I think I'll simply
remove version and product.
>
> > +
> > + input_set_capability(input, EV_SW, SW_TABLET_MODE);
> > +
> > + error = input_register_device(input);
> > + if (error) {
> > + dev_err(&pdev->dev, "cannot register input device: %d\n",
> > + error);
> > + return error;
> > + }
> > +
> > + /* Seed the state */
> > + error = whiskers_ec_query_base(ec, true, &whiskers_ec.base_present);
> > + if (error) {
> > + dev_err(&pdev->dev, "cannot query base state: %d\n", error);
> > + return error;
> > + }
> > +
> > + input_report_switch(input, SW_TABLET_MODE, !whiskers_ec.base_present);
> > +
> > + whiskers_ec_set_input(input);
> > +
> > + whiskers_ec.dev = &pdev->dev;
> > + whiskers_ec.notifier.notifier_call = whiskers_ec_notify;
> > + error = blocking_notifier_chain_register(&ec->event_notifier,
> > + &whiskers_ec.notifier);
> > + if (error) {
> > + dev_err(&pdev->dev, "cannot register notifier: %d\n", error);
> > + whiskers_ec_set_input(NULL);
> > + return error;
> > + }
> > +
> > + device_init_wakeup(&pdev->dev, true);
> > + return 0;
> > +}
> > +
> > +static int whiskers_ec_probe(struct platform_device *pdev)
> > +{
> > + int retval;
> > +
> > + mutex_lock(&whiskers_ec_reglock);
> > +
> > + if (whiskers_ec.input) {
> > + retval = -EBUSY;
> > + goto out;
> > + }
> > +
> > + retval = __whiskers_ec_probe(pdev);
> > +
> > +out:
> > + mutex_unlock(&whiskers_ec_reglock);
> > + return retval;
> > +}
> > +
> > +static int whiskers_ec_remove(struct platform_device *pdev)
> > +{
> > + struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> > +
> > + mutex_lock(&whiskers_ec_reglock);
> > +
> > + blocking_notifier_chain_unregister(&ec->event_notifier,
> > + &whiskers_ec.notifier);
> > + whiskers_ec_set_input(NULL);
> > +
> > + mutex_unlock(&whiskers_ec_reglock);
> > + return 0;
> > +}
> > +
> > +static const struct acpi_device_id whiskers_ec_acpi_ids[] = {
> > + { "GOOG000B", 0 },
> > + { }
> > +};
> > +MODULE_DEVICE_TABLE(acpi, whiskers_ec_acpi_ids);
> > +
> > +static struct platform_driver whiskers_ec_driver = {
> > + .probe = whiskers_ec_probe,
> > + .remove = whiskers_ec_remove,
> > + .driver = {
> > + .name = "whiskers_ec",
> > + .acpi_match_table = ACPI_PTR(whiskers_ec_acpi_ids),
> > + .pm = &whiskers_ec_pm_ops,
> > + },
> > +};
> > +
> > +static int whiskers_input_mapping(struct hid_device *hdev, struct hid_input *hi,
> > + struct hid_field *field,
> > + struct hid_usage *usage,
> > + unsigned long **bit, int *max)
> > +{
> > + if (usage->hid == WHISKERS_KBD_FOLDED) {
> > + /*
> > + * We do not want to have this usage mapped as it will get
> > + * mixed in with "base attached" signal and delivered over
> > + * separate input device for tablet switch mode.
> > + */
> > +
> > + return -1;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static int whiskers_event(struct hid_device *hid, struct hid_field *field,
> > + struct hid_usage *usage, __s32 value)
> > +{
> > + unsigned long flags;
> > +
> > + if (usage->hid == WHISKERS_KBD_FOLDED) {
> > + spin_lock_irqsave(&whiskers_ec_lock, flags);
> > +
> > + hid_dbg(hid, "%s: base: %d, folded: %d\n", __func__,
> > + whiskers_ec.base_present, value);
> > +
> > + /*
> > + * 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 (whiskers_ec.input && whiskers_ec.base_present) {
> > + input_report_switch(whiskers_ec.input,
> > + SW_TABLET_MODE, value);
> > + input_sync(whiskers_ec.input);
> > + }
> > +
> > + spin_unlock_irqrestore(&whiskers_ec_lock, flags);
> > + return 1; /* We handled this event */
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static int whiskers_probe(struct hid_device *hdev,
> > + const struct hid_device_id *id)
> > +{
> > + struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
>
> As mentioned earlier, this is prone to kernel oopses when using uhid.
Hmm, we have similar code in hid-google-hammer.c, I guess we need to
fix that there too.
>
> > + int ret;
> > +
> > + /*
> > + * We always want to poll for, and handle tablet mode events, even when
> > + * nobody has opened the input device. This also prevents the hid core
> > + * from dropping early tablet mode events from the device.
> > + */
> > + if (intf->cur_altsetting->desc.bInterfaceProtocol ==
> > + USB_INTERFACE_PROTOCOL_KEYBOARD)
>
> Can't you rely on something embedded in the report descriptor instead?
> This way you can drop the USB dependency.
Something like:
static bool hammer_is_keyboard(...)
{
rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
list_for_each_entry(report, &rep_enum->report_list, list) {
for (i = 0; i < report->maxfield; i++) {
if (report->field[i].application == HID_GD_KEYBOARD)
return true;
}
return false;
}
?
>
> > + hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
> > +
> > + ret = hid_parse(hdev);
> > + if (!ret)
> > + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
> > +
> > + return ret;
> > +}
> > +
> > +static const struct hid_device_id whiskers_hid_devices[] = {
> > + { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> > + USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
> > + { }
> > +};
> > +MODULE_DEVICE_TABLE(hid, whiskers_hid_devices);
> > +
> > +static struct hid_driver whiskers_hid_driver = {
> > + .name = "whiskers",
> > + .id_table = whiskers_hid_devices,
> > + .probe = whiskers_probe,
> > + .input_configured = hammer_input_configured,
> > + .input_mapping = whiskers_input_mapping,
> > + .event = whiskers_event,
> > +};
> > +
> > +static int __init whiskers_init(void)
> > +{
> > + int error;
> > +
> > + error = platform_driver_register(&whiskers_ec_driver);
> > + if (error)
> > + return error;
> > +
> > + error = hid_register_driver(&whiskers_hid_driver);
> > + if (error) {
> > + platform_driver_unregister(&whiskers_ec_driver);
> > + return error;
> > + }
> > +
> > + return 0;
> > +}
> > +module_init(whiskers_init);
> > +
> > +static void __exit whiskers_exit(void)
> > +{
> > + hid_unregister_driver(&whiskers_hid_driver);
> > + platform_driver_unregister(&whiskers_ec_driver);
> > +}
> > +module_exit(whiskers_exit);
> > +
> > +MODULE_LICENSE("GPL");
> > --
> > 2.19.0.605.g01d371f741-goog
> >
>
Thanks.
--
Dmitry
^ permalink raw reply
* Re: [PATCH] [v3] HID: add support for Apple Magic Trackpad 2
From: Sean O'Brien @ 2018-10-02 22:39 UTC (permalink / raw)
To: Benjamin Tissoires
Cc: peter.hutterer, marek.wyborski, linux-kernel, Dmitry Torokhov,
linux-input, rydberg, Jiri Kosina, claudio
In-Reply-To: <CAO-hwJJL7XsNR4iVAdxYUrmB+01aJiZ=2s1b_JemU-=YNDehMQ@mail.gmail.com>
On Mon, Oct 1, 2018 at 1:43 AM Benjamin Tissoires
<benjamin.tissoires@redhat.com> wrote:
>
> [adding Peter, for the libinput question]
>
> On Fri, Sep 21, 2018 at 1:13 AM Sean O'Brien <seobrien@chromium.org> wrote:
> >
> > USB device
> > Vendor 05ac (Apple)
> > Device 0265 (Magic Trackpad 2)
> > Bluetooth device
> > Vendor 004c (Apple)
> > Device 0265 (Magic Trackpad 2)
> >
> > Add support for Apple Magic Trackpad 2 over USB and bluetooth, putting
> > the device in multi-touch mode.
> >
> > Signed-off-by: Claudio Mettler <claudio@ponyfleisch.ch>
> > Signed-off-by: Marek Wyborski <marek.wyborski@emwesoft.com>
> > Signed-off-by: Sean O'Brien <seobrien@chromium.org>
> > ---
> >
>
> a few nitpcks:
>
> > drivers/hid/hid-ids.h | 1 +
> > drivers/hid/hid-magicmouse.c | 149 +++++++++++++++++++++++++++++++----
> > 2 files changed, 134 insertions(+), 16 deletions(-)
> >
> > diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
> > index 5146ee029db4..bb0cd212c7cc 100644
> > --- a/drivers/hid/hid-ids.h
> > +++ b/drivers/hid/hid-ids.h
> > @@ -92,6 +92,7 @@
> > #define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
> > #define USB_DEVICE_ID_APPLE_MAGICMOUSE 0x030d
> > #define USB_DEVICE_ID_APPLE_MAGICTRACKPAD 0x030e
> > +#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 0x0265
> > #define USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI 0x020e
> > #define USB_DEVICE_ID_APPLE_FOUNTAIN_ISO 0x020f
> > #define USB_DEVICE_ID_APPLE_GEYSER_ANSI 0x0214
> > diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
> > index b454c4386157..6a3a6c83e509 100644
> > --- a/drivers/hid/hid-magicmouse.c
> > +++ b/drivers/hid/hid-magicmouse.c
> > @@ -54,6 +54,8 @@ module_param(report_undeciphered, bool, 0644);
> > MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
> >
> > #define TRACKPAD_REPORT_ID 0x28
> > +#define TRACKPAD2_USB_REPORT_ID 0x02
> > +#define TRACKPAD2_BT_REPORT_ID 0x31
> > #define MOUSE_REPORT_ID 0x29
> > #define DOUBLE_REPORT_ID 0xf7
> > /* These definitions are not precise, but they're close enough. (Bits
> > @@ -91,6 +93,17 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
> > #define TRACKPAD_RES_Y \
> > ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
> >
> > +#define TRACKPAD2_DIMENSION_X (float)16000
> > +#define TRACKPAD2_MIN_X -3678
> > +#define TRACKPAD2_MAX_X 3934
> > +#define TRACKPAD2_RES_X \
> > + ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
> > +#define TRACKPAD2_DIMENSION_Y (float)11490
> > +#define TRACKPAD2_MIN_Y -2478
> > +#define TRACKPAD2_MAX_Y 2587
> > +#define TRACKPAD2_RES_Y \
> > + ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
> > +
> > /**
> > * struct magicmouse_sc - Tracks Magic Mouse-specific data.
> > * @input: Input device through which we report events.
> > @@ -183,6 +196,7 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> > {
> > struct input_dev *input = msc->input;
> > int id, x, y, size, orientation, touch_major, touch_minor, state, down;
> > + int pressure = 0;
> >
> > if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
> > id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
> > @@ -194,6 +208,20 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> > touch_minor = tdata[4];
> > state = tdata[7] & TOUCH_STATE_MASK;
> > down = state != TOUCH_STATE_NONE;
> > + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > + id = tdata[8] & 0xf;
> > + x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
> > + y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
> > + size = tdata[6];
> > + orientation = (tdata[8] >> 5) - 4;
> > + touch_major = tdata[4];
> > + touch_minor = tdata[5];
> > + /* Add to pressure to prevent libraries such as libinput
> > + * from ignoring low pressure touches
> > + */
> > + pressure = tdata[7] + 30;
>
> Peter, can you have a look?
>
> To me, while adding this threshold, you are basically fooling
> userspace, and we might want to come back to the raw values at some
> point.
> If there is a userspace problem, it has to be solved in userspace.
>
I'm fine with removing the offset. I haven't personally tested using
libinput, but the chromium OS gesture detector I've been using can
handle pressure values of 0.
> > + state = tdata[3] & 0xC0;
> > + down = state == 0x80;
> > } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
> > x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
> > @@ -215,7 +243,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> > /* If requested, emulate a scroll wheel by detecting small
> > * vertical touch motions.
> > */
> > - if (emulate_scroll_wheel) {
> > + if (emulate_scroll_wheel && (input->id.product !=
> > + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) {
> > unsigned long now = jiffies;
> > int step_x = msc->touches[id].scroll_x - x;
> > int step_y = msc->touches[id].scroll_y - y;
> > @@ -269,10 +298,14 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> > input_report_abs(input, ABS_MT_POSITION_X, x);
> > input_report_abs(input, ABS_MT_POSITION_Y, y);
> >
> > + if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
> > + input_report_abs(input, ABS_MT_PRESSURE, pressure);
> > +
> > if (report_undeciphered) {
> > if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
> > input_event(input, EV_MSC, MSC_RAW, tdata[7]);
> > - else /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > + else if (input->id.product !=
> > + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
> > input_event(input, EV_MSC, MSC_RAW, tdata[8]);
> > }
> > }
> > @@ -287,6 +320,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> >
> > switch (data[0]) {
> > case TRACKPAD_REPORT_ID:
> > + case TRACKPAD2_BT_REPORT_ID:
> > /* Expect four bytes of prefix, and N*9 bytes of touch data. */
> > if (size < 4 || ((size - 4) % 9) != 0)
> > return 0;
> > @@ -301,12 +335,22 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> > magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
> >
> > clicks = data[1];
> > + break;
> > + case TRACKPAD2_USB_REPORT_ID:
> > + /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
> > + if (size < 12 || ((size - 12) % 9) != 0)
> > + return 0;
> > + npoints = (size - 12) / 9;
> > + if (npoints > 15) {
> > + hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
> > + size);
> > + return 0;
> > + }
> > + msc->ntouches = 0;
> > + for (ii = 0; ii < npoints; ii++)
> > + magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
> >
> > - /* The following bits provide a device specific timestamp. They
> > - * are unused here.
> > - *
> > - * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
> > - */
>
> Can you keep this comment from the original version? If your touchpad
> doesn't have the timestamp bit, the old ones still have them, and
> having the decoded information might help in the future.
>
Will do.
> Cheers,
> Benjamin
>
> > + clicks = data[1];
> > break;
> > case MOUSE_REPORT_ID:
> > /* Expect six bytes of prefix, and N*8 bytes of touch data. */
> > @@ -352,6 +396,9 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> > magicmouse_emit_buttons(msc, clicks & 3);
> > input_report_rel(input, REL_X, x);
> > input_report_rel(input, REL_Y, y);
> > + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > + input_mt_sync_frame(input);
> > + input_report_key(input, BTN_MOUSE, clicks & 1);
> > } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > input_report_key(input, BTN_MOUSE, clicks & 1);
> > input_mt_report_pointer_emulation(input, true);
> > @@ -364,6 +411,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> > static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
> > {
> > int error;
> > + int mt_flags = 0;
> >
> > __set_bit(EV_KEY, input->evbit);
> >
> > @@ -380,6 +428,22 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> > __set_bit(REL_WHEEL, input->relbit);
> > __set_bit(REL_HWHEEL, input->relbit);
> > }
> > + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > + /* setting the device name to ensure the same driver settings
> > + * get loaded, whether connected through bluetooth or USB
> > + */
> > + input->name = "Apple Inc. Magic Trackpad 2";
> > +
> > + __clear_bit(EV_MSC, input->evbit);
> > + __clear_bit(BTN_0, input->keybit);
> > + __clear_bit(BTN_RIGHT, input->keybit);
> > + __clear_bit(BTN_MIDDLE, input->keybit);
> > + __set_bit(BTN_MOUSE, input->keybit);
> > + __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
> > + __set_bit(BTN_TOOL_FINGER, input->keybit);
> > +
> > + mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
> > + INPUT_MT_TRACK;
> > } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > /* input->keybit is initialized with incorrect button info
> > * for Magic Trackpad. There really is only one physical
> > @@ -402,14 +466,13 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> >
> > __set_bit(EV_ABS, input->evbit);
> >
> > - error = input_mt_init_slots(input, 16, 0);
> > + error = input_mt_init_slots(input, 16, mt_flags);
> > if (error)
> > return error;
> > input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
> > 4, 0);
> > input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
> > 4, 0);
> > - input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
> >
> > /* Note: Touch Y position from the device is inverted relative
> > * to how pointer motion is reported (and relative to how USB
> > @@ -418,6 +481,7 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> > * inverse of the reported Y.
> > */
> > if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
> > + input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
> > input_set_abs_params(input, ABS_MT_POSITION_X,
> > MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
> > input_set_abs_params(input, ABS_MT_POSITION_Y,
> > @@ -427,7 +491,25 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> > MOUSE_RES_X);
> > input_abs_set_res(input, ABS_MT_POSITION_Y,
> > MOUSE_RES_Y);
> > + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > + input_set_abs_params(input, ABS_MT_PRESSURE, 0, 283, 0, 0);
> > + input_set_abs_params(input, ABS_PRESSURE, 0, 283, 0, 0);
> > + input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
> > + input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
> > + TRACKPAD2_MAX_X, 0, 0);
> > + input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
> > + TRACKPAD2_MAX_Y, 0, 0);
> > + input_set_abs_params(input, ABS_MT_POSITION_X,
> > + TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
> > + input_set_abs_params(input, ABS_MT_POSITION_Y,
> > + TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
> > +
> > + input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
> > + input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
> > + input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
> > + input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
> > } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > + input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
> > input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
> > TRACKPAD_MAX_X, 4, 0);
> > input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
> > @@ -447,7 +529,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> >
> > input_set_events_per_packet(input, 60);
> >
> > - if (report_undeciphered) {
> > + if (report_undeciphered &&
> > + input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > __set_bit(EV_MSC, input->evbit);
> > __set_bit(MSC_RAW, input->mscbit);
> > }
> > @@ -465,7 +548,8 @@ static int magicmouse_input_mapping(struct hid_device *hdev,
> > msc->input = hi->input;
> >
> > /* Magic Trackpad does not give relative data after switching to MT */
> > - if (hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD &&
> > + if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
> > + hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
> > field->flags & HID_MAIN_ITEM_RELATIVE)
> > return -1;
> >
> > @@ -494,11 +578,20 @@ static int magicmouse_input_configured(struct hid_device *hdev,
> > static int magicmouse_probe(struct hid_device *hdev,
> > const struct hid_device_id *id)
> > {
> > - const u8 feature[] = { 0xd7, 0x01 };
> > + const u8 *feature;
> > + const u8 feature_mt[] = { 0xD7, 0x01 };
> > + const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
> > + const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
> > u8 *buf;
> > struct magicmouse_sc *msc;
> > struct hid_report *report;
> > int ret;
> > + int feature_size;
> > +
> > + if (id->vendor == USB_VENDOR_ID_APPLE &&
> > + id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
> > + hdev->type != HID_TYPE_USBMOUSE)
> > + return 0;
> >
> > msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
> > if (msc == NULL) {
> > @@ -532,7 +625,14 @@ static int magicmouse_probe(struct hid_device *hdev,
> > if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
> > report = hid_register_report(hdev, HID_INPUT_REPORT,
> > MOUSE_REPORT_ID, 0);
> > - else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > + else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > + if (id->vendor == BT_VENDOR_ID_APPLE)
> > + report = hid_register_report(hdev, HID_INPUT_REPORT,
> > + TRACKPAD2_BT_REPORT_ID, 0);
> > + else /* USB_VENDOR_ID_APPLE */
> > + report = hid_register_report(hdev, HID_INPUT_REPORT,
> > + TRACKPAD2_USB_REPORT_ID, 0);
> > + } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > report = hid_register_report(hdev, HID_INPUT_REPORT,
> > TRACKPAD_REPORT_ID, 0);
> > report = hid_register_report(hdev, HID_INPUT_REPORT,
> > @@ -546,7 +646,20 @@ static int magicmouse_probe(struct hid_device *hdev,
> > }
> > report->size = 6;
> >
> > - buf = kmemdup(feature, sizeof(feature), GFP_KERNEL);
> > + if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > + if (id->vendor == BT_VENDOR_ID_APPLE) {
> > + feature_size = sizeof(feature_mt_trackpad2_bt);
> > + feature = feature_mt_trackpad2_bt;
> > + } else { /* USB_VENDOR_ID_APPLE */
> > + feature_size = sizeof(feature_mt_trackpad2_usb);
> > + feature = feature_mt_trackpad2_usb;
> > + }
> > + } else {
> > + feature_size = sizeof(feature_mt);
> > + feature = feature_mt;
> > + }
> > +
> > + buf = kmemdup(feature, feature_size, GFP_KERNEL);
> > if (!buf) {
> > ret = -ENOMEM;
> > goto err_stop_hw;
> > @@ -560,10 +673,10 @@ static int magicmouse_probe(struct hid_device *hdev,
> > * but there seems to be no other way of switching the mode.
> > * Thus the super-ugly hacky success check below.
> > */
> > - ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(feature),
> > + ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
> > HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
> > kfree(buf);
> > - if (ret != -EIO && ret != sizeof(feature)) {
> > + if (ret != -EIO && ret != feature_size) {
> > hid_err(hdev, "unable to request touch data (%d)\n", ret);
> > goto err_stop_hw;
> > }
> > @@ -579,6 +692,10 @@ static const struct hid_device_id magic_mice[] = {
> > USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
> > { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
> > USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
> > + { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
> > + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
> > + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
> > + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
> > { }
> > };
> > MODULE_DEVICE_TABLE(hid, magic_mice);
> > --
> > 2.19.0.444.g18242da7ef-goog
> >
^ permalink raw reply
* [PATCH] [v4] HID: add support for Apple Magic Trackpad 2
From: Sean O'Brien @ 2018-10-02 22:53 UTC (permalink / raw)
To: Benjamin Tissoires
Cc: Sean O'Brien, Marek Wyborski, linux-kernel, Dmitry Torokhov,
linux-input, Henrik Rydberg, Jiri Kosina, Claudio Mettler
USB device
Vendor 05ac (Apple)
Device 0265 (Magic Trackpad 2)
Bluetooth device
Vendor 004c (Apple)
Device 0265 (Magic Trackpad 2)
Add support for Apple Magic Trackpad 2 over USB and bluetooth, putting
the device in multi-touch mode.
Signed-off-by: Claudio Mettler <claudio@ponyfleisch.ch>
Signed-off-by: Marek Wyborski <marek.wyborski@emwesoft.com>
Signed-off-by: Sean O'Brien <seobrien@chromium.org>
---
drivers/hid/hid-ids.h | 1 +
drivers/hid/hid-magicmouse.c | 142 ++++++++++++++++++++++++++++++++---
2 files changed, 132 insertions(+), 11 deletions(-)
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 5146ee029db4..bb0cd212c7cc 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -92,6 +92,7 @@
#define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
#define USB_DEVICE_ID_APPLE_MAGICMOUSE 0x030d
#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD 0x030e
+#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 0x0265
#define USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI 0x020e
#define USB_DEVICE_ID_APPLE_FOUNTAIN_ISO 0x020f
#define USB_DEVICE_ID_APPLE_GEYSER_ANSI 0x0214
diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
index b454c4386157..1d5ea678d268 100644
--- a/drivers/hid/hid-magicmouse.c
+++ b/drivers/hid/hid-magicmouse.c
@@ -54,6 +54,8 @@ module_param(report_undeciphered, bool, 0644);
MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
#define TRACKPAD_REPORT_ID 0x28
+#define TRACKPAD2_USB_REPORT_ID 0x02
+#define TRACKPAD2_BT_REPORT_ID 0x31
#define MOUSE_REPORT_ID 0x29
#define DOUBLE_REPORT_ID 0xf7
/* These definitions are not precise, but they're close enough. (Bits
@@ -91,6 +93,17 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
#define TRACKPAD_RES_Y \
((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
+#define TRACKPAD2_DIMENSION_X (float)16000
+#define TRACKPAD2_MIN_X -3678
+#define TRACKPAD2_MAX_X 3934
+#define TRACKPAD2_RES_X \
+ ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
+#define TRACKPAD2_DIMENSION_Y (float)11490
+#define TRACKPAD2_MIN_Y -2478
+#define TRACKPAD2_MAX_Y 2587
+#define TRACKPAD2_RES_Y \
+ ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
+
/**
* struct magicmouse_sc - Tracks Magic Mouse-specific data.
* @input: Input device through which we report events.
@@ -183,6 +196,7 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
{
struct input_dev *input = msc->input;
int id, x, y, size, orientation, touch_major, touch_minor, state, down;
+ int pressure = 0;
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
@@ -194,6 +208,17 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
touch_minor = tdata[4];
state = tdata[7] & TOUCH_STATE_MASK;
down = state != TOUCH_STATE_NONE;
+ } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
+ id = tdata[8] & 0xf;
+ x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
+ y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
+ size = tdata[6];
+ orientation = (tdata[8] >> 5) - 4;
+ touch_major = tdata[4];
+ touch_minor = tdata[5];
+ pressure = tdata[7];
+ state = tdata[3] & 0xC0;
+ down = state == 0x80;
} else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
@@ -215,7 +240,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
/* If requested, emulate a scroll wheel by detecting small
* vertical touch motions.
*/
- if (emulate_scroll_wheel) {
+ if (emulate_scroll_wheel && (input->id.product !=
+ USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) {
unsigned long now = jiffies;
int step_x = msc->touches[id].scroll_x - x;
int step_y = msc->touches[id].scroll_y - y;
@@ -269,10 +295,14 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
input_report_abs(input, ABS_MT_POSITION_X, x);
input_report_abs(input, ABS_MT_POSITION_Y, y);
+ if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
+ input_report_abs(input, ABS_MT_PRESSURE, pressure);
+
if (report_undeciphered) {
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
input_event(input, EV_MSC, MSC_RAW, tdata[7]);
- else /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
+ else if (input->id.product !=
+ USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
input_event(input, EV_MSC, MSC_RAW, tdata[8]);
}
}
@@ -287,6 +317,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
switch (data[0]) {
case TRACKPAD_REPORT_ID:
+ case TRACKPAD2_BT_REPORT_ID:
/* Expect four bytes of prefix, and N*9 bytes of touch data. */
if (size < 4 || ((size - 4) % 9) != 0)
return 0;
@@ -308,6 +339,22 @@ static int magicmouse_raw_event(struct hid_device *hdev,
* ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
*/
break;
+ case TRACKPAD2_USB_REPORT_ID:
+ /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
+ if (size < 12 || ((size - 12) % 9) != 0)
+ return 0;
+ npoints = (size - 12) / 9;
+ if (npoints > 15) {
+ hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
+ size);
+ return 0;
+ }
+ msc->ntouches = 0;
+ for (ii = 0; ii < npoints; ii++)
+ magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
+
+ clicks = data[1];
+ break;
case MOUSE_REPORT_ID:
/* Expect six bytes of prefix, and N*8 bytes of touch data. */
if (size < 6 || ((size - 6) % 8) != 0)
@@ -352,6 +399,9 @@ static int magicmouse_raw_event(struct hid_device *hdev,
magicmouse_emit_buttons(msc, clicks & 3);
input_report_rel(input, REL_X, x);
input_report_rel(input, REL_Y, y);
+ } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
+ input_mt_sync_frame(input);
+ input_report_key(input, BTN_MOUSE, clicks & 1);
} else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
input_report_key(input, BTN_MOUSE, clicks & 1);
input_mt_report_pointer_emulation(input, true);
@@ -364,6 +414,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
{
int error;
+ int mt_flags = 0;
__set_bit(EV_KEY, input->evbit);
@@ -380,6 +431,22 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
__set_bit(REL_WHEEL, input->relbit);
__set_bit(REL_HWHEEL, input->relbit);
}
+ } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
+ /* setting the device name to ensure the same driver settings
+ * get loaded, whether connected through bluetooth or USB
+ */
+ input->name = "Apple Inc. Magic Trackpad 2";
+
+ __clear_bit(EV_MSC, input->evbit);
+ __clear_bit(BTN_0, input->keybit);
+ __clear_bit(BTN_RIGHT, input->keybit);
+ __clear_bit(BTN_MIDDLE, input->keybit);
+ __set_bit(BTN_MOUSE, input->keybit);
+ __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
+ __set_bit(BTN_TOOL_FINGER, input->keybit);
+
+ mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
+ INPUT_MT_TRACK;
} else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
/* input->keybit is initialized with incorrect button info
* for Magic Trackpad. There really is only one physical
@@ -402,14 +469,13 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
__set_bit(EV_ABS, input->evbit);
- error = input_mt_init_slots(input, 16, 0);
+ error = input_mt_init_slots(input, 16, mt_flags);
if (error)
return error;
input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
4, 0);
input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
4, 0);
- input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
/* Note: Touch Y position from the device is inverted relative
* to how pointer motion is reported (and relative to how USB
@@ -418,6 +484,7 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
* inverse of the reported Y.
*/
if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
input_set_abs_params(input, ABS_MT_POSITION_X,
MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
input_set_abs_params(input, ABS_MT_POSITION_Y,
@@ -427,7 +494,25 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
MOUSE_RES_X);
input_abs_set_res(input, ABS_MT_POSITION_Y,
MOUSE_RES_Y);
+ } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
+ input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
+ input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
+ input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
+ TRACKPAD2_MAX_X, 0, 0);
+ input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
+ TRACKPAD2_MAX_Y, 0, 0);
+ input_set_abs_params(input, ABS_MT_POSITION_X,
+ TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
+ input_set_abs_params(input, ABS_MT_POSITION_Y,
+ TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
+
+ input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
+ input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
+ input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
+ input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
} else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
+ input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
TRACKPAD_MAX_X, 4, 0);
input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
@@ -447,7 +532,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
input_set_events_per_packet(input, 60);
- if (report_undeciphered) {
+ if (report_undeciphered &&
+ input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
__set_bit(EV_MSC, input->evbit);
__set_bit(MSC_RAW, input->mscbit);
}
@@ -465,7 +551,8 @@ static int magicmouse_input_mapping(struct hid_device *hdev,
msc->input = hi->input;
/* Magic Trackpad does not give relative data after switching to MT */
- if (hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD &&
+ if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
+ hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
field->flags & HID_MAIN_ITEM_RELATIVE)
return -1;
@@ -494,11 +581,20 @@ static int magicmouse_input_configured(struct hid_device *hdev,
static int magicmouse_probe(struct hid_device *hdev,
const struct hid_device_id *id)
{
- const u8 feature[] = { 0xd7, 0x01 };
+ const u8 *feature;
+ const u8 feature_mt[] = { 0xD7, 0x01 };
+ const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
+ const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
u8 *buf;
struct magicmouse_sc *msc;
struct hid_report *report;
int ret;
+ int feature_size;
+
+ if (id->vendor == USB_VENDOR_ID_APPLE &&
+ id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
+ hdev->type != HID_TYPE_USBMOUSE)
+ return 0;
msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
if (msc == NULL) {
@@ -532,7 +628,14 @@ static int magicmouse_probe(struct hid_device *hdev,
if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
report = hid_register_report(hdev, HID_INPUT_REPORT,
MOUSE_REPORT_ID, 0);
- else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
+ else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
+ if (id->vendor == BT_VENDOR_ID_APPLE)
+ report = hid_register_report(hdev, HID_INPUT_REPORT,
+ TRACKPAD2_BT_REPORT_ID, 0);
+ else /* USB_VENDOR_ID_APPLE */
+ report = hid_register_report(hdev, HID_INPUT_REPORT,
+ TRACKPAD2_USB_REPORT_ID, 0);
+ } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
report = hid_register_report(hdev, HID_INPUT_REPORT,
TRACKPAD_REPORT_ID, 0);
report = hid_register_report(hdev, HID_INPUT_REPORT,
@@ -546,7 +649,20 @@ static int magicmouse_probe(struct hid_device *hdev,
}
report->size = 6;
- buf = kmemdup(feature, sizeof(feature), GFP_KERNEL);
+ if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
+ if (id->vendor == BT_VENDOR_ID_APPLE) {
+ feature_size = sizeof(feature_mt_trackpad2_bt);
+ feature = feature_mt_trackpad2_bt;
+ } else { /* USB_VENDOR_ID_APPLE */
+ feature_size = sizeof(feature_mt_trackpad2_usb);
+ feature = feature_mt_trackpad2_usb;
+ }
+ } else {
+ feature_size = sizeof(feature_mt);
+ feature = feature_mt;
+ }
+
+ buf = kmemdup(feature, feature_size, GFP_KERNEL);
if (!buf) {
ret = -ENOMEM;
goto err_stop_hw;
@@ -560,10 +676,10 @@ static int magicmouse_probe(struct hid_device *hdev,
* but there seems to be no other way of switching the mode.
* Thus the super-ugly hacky success check below.
*/
- ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(feature),
+ ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
kfree(buf);
- if (ret != -EIO && ret != sizeof(feature)) {
+ if (ret != -EIO && ret != feature_size) {
hid_err(hdev, "unable to request touch data (%d)\n", ret);
goto err_stop_hw;
}
@@ -579,6 +695,10 @@ static const struct hid_device_id magic_mice[] = {
USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
+ { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
+ USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
+ USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
{ }
};
MODULE_DEVICE_TABLE(hid, magic_mice);
--
2.19.0.605.g01d371f741-goog
^ permalink raw reply related
* Re: [PATCH] [v3] HID: add support for Apple Magic Trackpad 2
From: Peter Hutterer @ 2018-10-03 0:04 UTC (permalink / raw)
To: Sean O'Brien
Cc: Benjamin Tissoires, peter.hutterer, marek.wyborski, linux-kernel,
Dmitry Torokhov, linux-input, rydberg, Jiri Kosina, claudio
In-Reply-To: <CAOOzhkp6ChopAXxDFcbz1sT17EKTGysF5hdkMkM50LGdXcnyaQ@mail.gmail.com>
On Tue, Oct 02, 2018 at 03:39:31PM -0700, Sean O'Brien wrote:
> On Mon, Oct 1, 2018 at 1:43 AM Benjamin Tissoires
> <benjamin.tissoires@redhat.com> wrote:
> >
> > [adding Peter, for the libinput question]
> >
> > On Fri, Sep 21, 2018 at 1:13 AM Sean O'Brien <seobrien@chromium.org> wrote:
> > >
> > > USB device
> > > Vendor 05ac (Apple)
> > > Device 0265 (Magic Trackpad 2)
> > > Bluetooth device
> > > Vendor 004c (Apple)
> > > Device 0265 (Magic Trackpad 2)
> > >
> > > Add support for Apple Magic Trackpad 2 over USB and bluetooth, putting
> > > the device in multi-touch mode.
> > >
> > > Signed-off-by: Claudio Mettler <claudio@ponyfleisch.ch>
> > > Signed-off-by: Marek Wyborski <marek.wyborski@emwesoft.com>
> > > Signed-off-by: Sean O'Brien <seobrien@chromium.org>
> > > ---
> > >
> >
> > a few nitpcks:
> >
> > > drivers/hid/hid-ids.h | 1 +
> > > drivers/hid/hid-magicmouse.c | 149 +++++++++++++++++++++++++++++++----
> > > 2 files changed, 134 insertions(+), 16 deletions(-)
> > >
> > > diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
> > > index 5146ee029db4..bb0cd212c7cc 100644
> > > --- a/drivers/hid/hid-ids.h
> > > +++ b/drivers/hid/hid-ids.h
> > > @@ -92,6 +92,7 @@
> > > #define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
> > > #define USB_DEVICE_ID_APPLE_MAGICMOUSE 0x030d
> > > #define USB_DEVICE_ID_APPLE_MAGICTRACKPAD 0x030e
> > > +#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 0x0265
> > > #define USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI 0x020e
> > > #define USB_DEVICE_ID_APPLE_FOUNTAIN_ISO 0x020f
> > > #define USB_DEVICE_ID_APPLE_GEYSER_ANSI 0x0214
> > > diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
> > > index b454c4386157..6a3a6c83e509 100644
> > > --- a/drivers/hid/hid-magicmouse.c
> > > +++ b/drivers/hid/hid-magicmouse.c
> > > @@ -54,6 +54,8 @@ module_param(report_undeciphered, bool, 0644);
> > > MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
> > >
> > > #define TRACKPAD_REPORT_ID 0x28
> > > +#define TRACKPAD2_USB_REPORT_ID 0x02
> > > +#define TRACKPAD2_BT_REPORT_ID 0x31
> > > #define MOUSE_REPORT_ID 0x29
> > > #define DOUBLE_REPORT_ID 0xf7
> > > /* These definitions are not precise, but they're close enough. (Bits
> > > @@ -91,6 +93,17 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
> > > #define TRACKPAD_RES_Y \
> > > ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
> > >
> > > +#define TRACKPAD2_DIMENSION_X (float)16000
> > > +#define TRACKPAD2_MIN_X -3678
> > > +#define TRACKPAD2_MAX_X 3934
> > > +#define TRACKPAD2_RES_X \
> > > + ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
> > > +#define TRACKPAD2_DIMENSION_Y (float)11490
> > > +#define TRACKPAD2_MIN_Y -2478
> > > +#define TRACKPAD2_MAX_Y 2587
> > > +#define TRACKPAD2_RES_Y \
> > > + ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
> > > +
> > > /**
> > > * struct magicmouse_sc - Tracks Magic Mouse-specific data.
> > > * @input: Input device through which we report events.
> > > @@ -183,6 +196,7 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> > > {
> > > struct input_dev *input = msc->input;
> > > int id, x, y, size, orientation, touch_major, touch_minor, state, down;
> > > + int pressure = 0;
> > >
> > > if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
> > > id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
> > > @@ -194,6 +208,20 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> > > touch_minor = tdata[4];
> > > state = tdata[7] & TOUCH_STATE_MASK;
> > > down = state != TOUCH_STATE_NONE;
> > > + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > > + id = tdata[8] & 0xf;
> > > + x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
> > > + y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
> > > + size = tdata[6];
> > > + orientation = (tdata[8] >> 5) - 4;
> > > + touch_major = tdata[4];
> > > + touch_minor = tdata[5];
> > > + /* Add to pressure to prevent libraries such as libinput
> > > + * from ignoring low pressure touches
> > > + */
> > > + pressure = tdata[7] + 30;
> >
> > Peter, can you have a look?
> >
> > To me, while adding this threshold, you are basically fooling
> > userspace, and we might want to come back to the raw values at some
> > point.
> > If there is a userspace problem, it has to be solved in userspace.
> >
>
> I'm fine with removing the offset. I haven't personally tested using
> libinput, but the chromium OS gesture detector I've been using can
> handle pressure values of 0.
libinput uses touch major/minor but only if a threshold is defined for
that device in the quirks files (matches on VID/PID/name/...).
https://gitlab.freedesktop.org/libinput/libinput/quirks/
No existing quirk will match for this device so libinput falls back to using
pressure instead. That too can be defined per-device in the quirks but for
this VID/PID it isn't so we fall back to the default thresholds guessed
based on the pressure range (12% of the range, that's where the 30 comes
from).
Adding a quirk for this device with the required major/minor threshold (or
pressure if that's more reliable) should be enough to make it work, it
definitely doesn't require a kernel workaround.
fwiw, testing against libinput is as simple as building the git repo with
meson and running:
sudo ./builddir/libinput-debug-events
or if you want some graphical debugging (requires gtk3-devel)
sudo ./builddir/libinput-debug-gui
Cheers,
Peter
>
> > > + state = tdata[3] & 0xC0;
> > > + down = state == 0x80;
> > > } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > > id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
> > > x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
> > > @@ -215,7 +243,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> > > /* If requested, emulate a scroll wheel by detecting small
> > > * vertical touch motions.
> > > */
> > > - if (emulate_scroll_wheel) {
> > > + if (emulate_scroll_wheel && (input->id.product !=
> > > + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) {
> > > unsigned long now = jiffies;
> > > int step_x = msc->touches[id].scroll_x - x;
> > > int step_y = msc->touches[id].scroll_y - y;
> > > @@ -269,10 +298,14 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> > > input_report_abs(input, ABS_MT_POSITION_X, x);
> > > input_report_abs(input, ABS_MT_POSITION_Y, y);
> > >
> > > + if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
> > > + input_report_abs(input, ABS_MT_PRESSURE, pressure);
> > > +
> > > if (report_undeciphered) {
> > > if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
> > > input_event(input, EV_MSC, MSC_RAW, tdata[7]);
> > > - else /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > > + else if (input->id.product !=
> > > + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
> > > input_event(input, EV_MSC, MSC_RAW, tdata[8]);
> > > }
> > > }
> > > @@ -287,6 +320,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> > >
> > > switch (data[0]) {
> > > case TRACKPAD_REPORT_ID:
> > > + case TRACKPAD2_BT_REPORT_ID:
> > > /* Expect four bytes of prefix, and N*9 bytes of touch data. */
> > > if (size < 4 || ((size - 4) % 9) != 0)
> > > return 0;
> > > @@ -301,12 +335,22 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> > > magicmouse_emit_touch(msc, ii, data + ii * 9 + 4);
> > >
> > > clicks = data[1];
> > > + break;
> > > + case TRACKPAD2_USB_REPORT_ID:
> > > + /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
> > > + if (size < 12 || ((size - 12) % 9) != 0)
> > > + return 0;
> > > + npoints = (size - 12) / 9;
> > > + if (npoints > 15) {
> > > + hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
> > > + size);
> > > + return 0;
> > > + }
> > > + msc->ntouches = 0;
> > > + for (ii = 0; ii < npoints; ii++)
> > > + magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
> > >
> > > - /* The following bits provide a device specific timestamp. They
> > > - * are unused here.
> > > - *
> > > - * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
> > > - */
> >
> > Can you keep this comment from the original version? If your touchpad
> > doesn't have the timestamp bit, the old ones still have them, and
> > having the decoded information might help in the future.
> >
>
> Will do.
>
> > Cheers,
> > Benjamin
> >
> > > + clicks = data[1];
> > > break;
> > > case MOUSE_REPORT_ID:
> > > /* Expect six bytes of prefix, and N*8 bytes of touch data. */
> > > @@ -352,6 +396,9 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> > > magicmouse_emit_buttons(msc, clicks & 3);
> > > input_report_rel(input, REL_X, x);
> > > input_report_rel(input, REL_Y, y);
> > > + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > > + input_mt_sync_frame(input);
> > > + input_report_key(input, BTN_MOUSE, clicks & 1);
> > > } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > > input_report_key(input, BTN_MOUSE, clicks & 1);
> > > input_mt_report_pointer_emulation(input, true);
> > > @@ -364,6 +411,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> > > static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
> > > {
> > > int error;
> > > + int mt_flags = 0;
> > >
> > > __set_bit(EV_KEY, input->evbit);
> > >
> > > @@ -380,6 +428,22 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> > > __set_bit(REL_WHEEL, input->relbit);
> > > __set_bit(REL_HWHEEL, input->relbit);
> > > }
> > > + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > > + /* setting the device name to ensure the same driver settings
> > > + * get loaded, whether connected through bluetooth or USB
> > > + */
> > > + input->name = "Apple Inc. Magic Trackpad 2";
> > > +
> > > + __clear_bit(EV_MSC, input->evbit);
> > > + __clear_bit(BTN_0, input->keybit);
> > > + __clear_bit(BTN_RIGHT, input->keybit);
> > > + __clear_bit(BTN_MIDDLE, input->keybit);
> > > + __set_bit(BTN_MOUSE, input->keybit);
> > > + __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
> > > + __set_bit(BTN_TOOL_FINGER, input->keybit);
> > > +
> > > + mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
> > > + INPUT_MT_TRACK;
> > > } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > > /* input->keybit is initialized with incorrect button info
> > > * for Magic Trackpad. There really is only one physical
> > > @@ -402,14 +466,13 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> > >
> > > __set_bit(EV_ABS, input->evbit);
> > >
> > > - error = input_mt_init_slots(input, 16, 0);
> > > + error = input_mt_init_slots(input, 16, mt_flags);
> > > if (error)
> > > return error;
> > > input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
> > > 4, 0);
> > > input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
> > > 4, 0);
> > > - input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
> > >
> > > /* Note: Touch Y position from the device is inverted relative
> > > * to how pointer motion is reported (and relative to how USB
> > > @@ -418,6 +481,7 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> > > * inverse of the reported Y.
> > > */
> > > if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
> > > + input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
> > > input_set_abs_params(input, ABS_MT_POSITION_X,
> > > MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
> > > input_set_abs_params(input, ABS_MT_POSITION_Y,
> > > @@ -427,7 +491,25 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> > > MOUSE_RES_X);
> > > input_abs_set_res(input, ABS_MT_POSITION_Y,
> > > MOUSE_RES_Y);
> > > + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > > + input_set_abs_params(input, ABS_MT_PRESSURE, 0, 283, 0, 0);
> > > + input_set_abs_params(input, ABS_PRESSURE, 0, 283, 0, 0);
> > > + input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
> > > + input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
> > > + TRACKPAD2_MAX_X, 0, 0);
> > > + input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
> > > + TRACKPAD2_MAX_Y, 0, 0);
> > > + input_set_abs_params(input, ABS_MT_POSITION_X,
> > > + TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
> > > + input_set_abs_params(input, ABS_MT_POSITION_Y,
> > > + TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
> > > +
> > > + input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
> > > + input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
> > > + input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
> > > + input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
> > > } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > > + input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
> > > input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
> > > TRACKPAD_MAX_X, 4, 0);
> > > input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
> > > @@ -447,7 +529,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> > >
> > > input_set_events_per_packet(input, 60);
> > >
> > > - if (report_undeciphered) {
> > > + if (report_undeciphered &&
> > > + input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > > __set_bit(EV_MSC, input->evbit);
> > > __set_bit(MSC_RAW, input->mscbit);
> > > }
> > > @@ -465,7 +548,8 @@ static int magicmouse_input_mapping(struct hid_device *hdev,
> > > msc->input = hi->input;
> > >
> > > /* Magic Trackpad does not give relative data after switching to MT */
> > > - if (hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD &&
> > > + if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
> > > + hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
> > > field->flags & HID_MAIN_ITEM_RELATIVE)
> > > return -1;
> > >
> > > @@ -494,11 +578,20 @@ static int magicmouse_input_configured(struct hid_device *hdev,
> > > static int magicmouse_probe(struct hid_device *hdev,
> > > const struct hid_device_id *id)
> > > {
> > > - const u8 feature[] = { 0xd7, 0x01 };
> > > + const u8 *feature;
> > > + const u8 feature_mt[] = { 0xD7, 0x01 };
> > > + const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
> > > + const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
> > > u8 *buf;
> > > struct magicmouse_sc *msc;
> > > struct hid_report *report;
> > > int ret;
> > > + int feature_size;
> > > +
> > > + if (id->vendor == USB_VENDOR_ID_APPLE &&
> > > + id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
> > > + hdev->type != HID_TYPE_USBMOUSE)
> > > + return 0;
> > >
> > > msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
> > > if (msc == NULL) {
> > > @@ -532,7 +625,14 @@ static int magicmouse_probe(struct hid_device *hdev,
> > > if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
> > > report = hid_register_report(hdev, HID_INPUT_REPORT,
> > > MOUSE_REPORT_ID, 0);
> > > - else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > > + else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > > + if (id->vendor == BT_VENDOR_ID_APPLE)
> > > + report = hid_register_report(hdev, HID_INPUT_REPORT,
> > > + TRACKPAD2_BT_REPORT_ID, 0);
> > > + else /* USB_VENDOR_ID_APPLE */
> > > + report = hid_register_report(hdev, HID_INPUT_REPORT,
> > > + TRACKPAD2_USB_REPORT_ID, 0);
> > > + } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> > > report = hid_register_report(hdev, HID_INPUT_REPORT,
> > > TRACKPAD_REPORT_ID, 0);
> > > report = hid_register_report(hdev, HID_INPUT_REPORT,
> > > @@ -546,7 +646,20 @@ static int magicmouse_probe(struct hid_device *hdev,
> > > }
> > > report->size = 6;
> > >
> > > - buf = kmemdup(feature, sizeof(feature), GFP_KERNEL);
> > > + if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> > > + if (id->vendor == BT_VENDOR_ID_APPLE) {
> > > + feature_size = sizeof(feature_mt_trackpad2_bt);
> > > + feature = feature_mt_trackpad2_bt;
> > > + } else { /* USB_VENDOR_ID_APPLE */
> > > + feature_size = sizeof(feature_mt_trackpad2_usb);
> > > + feature = feature_mt_trackpad2_usb;
> > > + }
> > > + } else {
> > > + feature_size = sizeof(feature_mt);
> > > + feature = feature_mt;
> > > + }
> > > +
> > > + buf = kmemdup(feature, feature_size, GFP_KERNEL);
> > > if (!buf) {
> > > ret = -ENOMEM;
> > > goto err_stop_hw;
> > > @@ -560,10 +673,10 @@ static int magicmouse_probe(struct hid_device *hdev,
> > > * but there seems to be no other way of switching the mode.
> > > * Thus the super-ugly hacky success check below.
> > > */
> > > - ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(feature),
> > > + ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
> > > HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
> > > kfree(buf);
> > > - if (ret != -EIO && ret != sizeof(feature)) {
> > > + if (ret != -EIO && ret != feature_size) {
> > > hid_err(hdev, "unable to request touch data (%d)\n", ret);
> > > goto err_stop_hw;
> > > }
> > > @@ -579,6 +692,10 @@ static const struct hid_device_id magic_mice[] = {
> > > USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
> > > { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
> > > USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
> > > + { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
> > > + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
> > > + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
> > > + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
> > > { }
> > > };
> > > MODULE_DEVICE_TABLE(hid, magic_mice);
> > > --
> > > 2.19.0.444.g18242da7ef-goog
> > >
^ permalink raw reply
* Re: [PATCH] [v4] HID: add support for Apple Magic Trackpad 2
From: Benjamin Tissoires @ 2018-10-03 7:38 UTC (permalink / raw)
To: seobrien
Cc: marek.wyborski, lkml, Dmitry Torokhov, open list:HID CORE LAYER,
Henrik Rydberg, Jiri Kosina, claudio
In-Reply-To: <20181002225337.7349-1-seobrien@chromium.org>
On Wed, Oct 3, 2018 at 12:53 AM Sean O'Brien <seobrien@chromium.org> wrote:
>
> USB device
> Vendor 05ac (Apple)
> Device 0265 (Magic Trackpad 2)
> Bluetooth device
> Vendor 004c (Apple)
> Device 0265 (Magic Trackpad 2)
>
> Add support for Apple Magic Trackpad 2 over USB and bluetooth, putting
> the device in multi-touch mode.
>
> Signed-off-by: Claudio Mettler <claudio@ponyfleisch.ch>
> Signed-off-by: Marek Wyborski <marek.wyborski@emwesoft.com>
> Signed-off-by: Sean O'Brien <seobrien@chromium.org>
> ---
Thanks for the quick respin.
This version is now good and looks like the rest of the code (I wish
we had a better handling than just parsing everything like we do here,
sigh).
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Cheers,
Benjamin
>
> drivers/hid/hid-ids.h | 1 +
> drivers/hid/hid-magicmouse.c | 142 ++++++++++++++++++++++++++++++++---
> 2 files changed, 132 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
> index 5146ee029db4..bb0cd212c7cc 100644
> --- a/drivers/hid/hid-ids.h
> +++ b/drivers/hid/hid-ids.h
> @@ -92,6 +92,7 @@
> #define USB_DEVICE_ID_APPLE_MIGHTYMOUSE 0x0304
> #define USB_DEVICE_ID_APPLE_MAGICMOUSE 0x030d
> #define USB_DEVICE_ID_APPLE_MAGICTRACKPAD 0x030e
> +#define USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 0x0265
> #define USB_DEVICE_ID_APPLE_FOUNTAIN_ANSI 0x020e
> #define USB_DEVICE_ID_APPLE_FOUNTAIN_ISO 0x020f
> #define USB_DEVICE_ID_APPLE_GEYSER_ANSI 0x0214
> diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
> index b454c4386157..1d5ea678d268 100644
> --- a/drivers/hid/hid-magicmouse.c
> +++ b/drivers/hid/hid-magicmouse.c
> @@ -54,6 +54,8 @@ module_param(report_undeciphered, bool, 0644);
> MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
>
> #define TRACKPAD_REPORT_ID 0x28
> +#define TRACKPAD2_USB_REPORT_ID 0x02
> +#define TRACKPAD2_BT_REPORT_ID 0x31
> #define MOUSE_REPORT_ID 0x29
> #define DOUBLE_REPORT_ID 0xf7
> /* These definitions are not precise, but they're close enough. (Bits
> @@ -91,6 +93,17 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
> #define TRACKPAD_RES_Y \
> ((TRACKPAD_MAX_Y - TRACKPAD_MIN_Y) / (TRACKPAD_DIMENSION_Y / 100))
>
> +#define TRACKPAD2_DIMENSION_X (float)16000
> +#define TRACKPAD2_MIN_X -3678
> +#define TRACKPAD2_MAX_X 3934
> +#define TRACKPAD2_RES_X \
> + ((TRACKPAD2_MAX_X - TRACKPAD2_MIN_X) / (TRACKPAD2_DIMENSION_X / 100))
> +#define TRACKPAD2_DIMENSION_Y (float)11490
> +#define TRACKPAD2_MIN_Y -2478
> +#define TRACKPAD2_MAX_Y 2587
> +#define TRACKPAD2_RES_Y \
> + ((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
> +
> /**
> * struct magicmouse_sc - Tracks Magic Mouse-specific data.
> * @input: Input device through which we report events.
> @@ -183,6 +196,7 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> {
> struct input_dev *input = msc->input;
> int id, x, y, size, orientation, touch_major, touch_minor, state, down;
> + int pressure = 0;
>
> if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
> id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
> @@ -194,6 +208,17 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> touch_minor = tdata[4];
> state = tdata[7] & TOUCH_STATE_MASK;
> down = state != TOUCH_STATE_NONE;
> + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> + id = tdata[8] & 0xf;
> + x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
> + y = -((tdata[3] << 30 | tdata[2] << 22 | tdata[1] << 14) >> 19);
> + size = tdata[6];
> + orientation = (tdata[8] >> 5) - 4;
> + touch_major = tdata[4];
> + touch_minor = tdata[5];
> + pressure = tdata[7];
> + state = tdata[3] & 0xC0;
> + down = state == 0x80;
> } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> id = (tdata[7] << 2 | tdata[6] >> 6) & 0xf;
> x = (tdata[1] << 27 | tdata[0] << 19) >> 19;
> @@ -215,7 +240,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> /* If requested, emulate a scroll wheel by detecting small
> * vertical touch motions.
> */
> - if (emulate_scroll_wheel) {
> + if (emulate_scroll_wheel && (input->id.product !=
> + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)) {
> unsigned long now = jiffies;
> int step_x = msc->touches[id].scroll_x - x;
> int step_y = msc->touches[id].scroll_y - y;
> @@ -269,10 +295,14 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
> input_report_abs(input, ABS_MT_POSITION_X, x);
> input_report_abs(input, ABS_MT_POSITION_Y, y);
>
> + if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
> + input_report_abs(input, ABS_MT_PRESSURE, pressure);
> +
> if (report_undeciphered) {
> if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
> input_event(input, EV_MSC, MSC_RAW, tdata[7]);
> - else /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> + else if (input->id.product !=
> + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2)
> input_event(input, EV_MSC, MSC_RAW, tdata[8]);
> }
> }
> @@ -287,6 +317,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
>
> switch (data[0]) {
> case TRACKPAD_REPORT_ID:
> + case TRACKPAD2_BT_REPORT_ID:
> /* Expect four bytes of prefix, and N*9 bytes of touch data. */
> if (size < 4 || ((size - 4) % 9) != 0)
> return 0;
> @@ -308,6 +339,22 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> * ts = data[1] >> 6 | data[2] << 2 | data[3] << 10;
> */
> break;
> + case TRACKPAD2_USB_REPORT_ID:
> + /* Expect twelve bytes of prefix and N*9 bytes of touch data. */
> + if (size < 12 || ((size - 12) % 9) != 0)
> + return 0;
> + npoints = (size - 12) / 9;
> + if (npoints > 15) {
> + hid_warn(hdev, "invalid size value (%d) for TRACKPAD2_USB_REPORT_ID\n",
> + size);
> + return 0;
> + }
> + msc->ntouches = 0;
> + for (ii = 0; ii < npoints; ii++)
> + magicmouse_emit_touch(msc, ii, data + ii * 9 + 12);
> +
> + clicks = data[1];
> + break;
> case MOUSE_REPORT_ID:
> /* Expect six bytes of prefix, and N*8 bytes of touch data. */
> if (size < 6 || ((size - 6) % 8) != 0)
> @@ -352,6 +399,9 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> magicmouse_emit_buttons(msc, clicks & 3);
> input_report_rel(input, REL_X, x);
> input_report_rel(input, REL_Y, y);
> + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> + input_mt_sync_frame(input);
> + input_report_key(input, BTN_MOUSE, clicks & 1);
> } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> input_report_key(input, BTN_MOUSE, clicks & 1);
> input_mt_report_pointer_emulation(input, true);
> @@ -364,6 +414,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
> static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
> {
> int error;
> + int mt_flags = 0;
>
> __set_bit(EV_KEY, input->evbit);
>
> @@ -380,6 +431,22 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> __set_bit(REL_WHEEL, input->relbit);
> __set_bit(REL_HWHEEL, input->relbit);
> }
> + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> + /* setting the device name to ensure the same driver settings
> + * get loaded, whether connected through bluetooth or USB
> + */
> + input->name = "Apple Inc. Magic Trackpad 2";
> +
> + __clear_bit(EV_MSC, input->evbit);
> + __clear_bit(BTN_0, input->keybit);
> + __clear_bit(BTN_RIGHT, input->keybit);
> + __clear_bit(BTN_MIDDLE, input->keybit);
> + __set_bit(BTN_MOUSE, input->keybit);
> + __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
> + __set_bit(BTN_TOOL_FINGER, input->keybit);
> +
> + mt_flags = INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
> + INPUT_MT_TRACK;
> } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> /* input->keybit is initialized with incorrect button info
> * for Magic Trackpad. There really is only one physical
> @@ -402,14 +469,13 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
>
> __set_bit(EV_ABS, input->evbit);
>
> - error = input_mt_init_slots(input, 16, 0);
> + error = input_mt_init_slots(input, 16, mt_flags);
> if (error)
> return error;
> input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255 << 2,
> 4, 0);
> input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255 << 2,
> 4, 0);
> - input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
>
> /* Note: Touch Y position from the device is inverted relative
> * to how pointer motion is reported (and relative to how USB
> @@ -418,6 +484,7 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> * inverse of the reported Y.
> */
> if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE) {
> + input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
> input_set_abs_params(input, ABS_MT_POSITION_X,
> MOUSE_MIN_X, MOUSE_MAX_X, 4, 0);
> input_set_abs_params(input, ABS_MT_POSITION_Y,
> @@ -427,7 +494,25 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
> MOUSE_RES_X);
> input_abs_set_res(input, ABS_MT_POSITION_Y,
> MOUSE_RES_Y);
> + } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> + input_set_abs_params(input, ABS_MT_PRESSURE, 0, 253, 0, 0);
> + input_set_abs_params(input, ABS_PRESSURE, 0, 253, 0, 0);
> + input_set_abs_params(input, ABS_MT_ORIENTATION, -3, 4, 0, 0);
> + input_set_abs_params(input, ABS_X, TRACKPAD2_MIN_X,
> + TRACKPAD2_MAX_X, 0, 0);
> + input_set_abs_params(input, ABS_Y, TRACKPAD2_MIN_Y,
> + TRACKPAD2_MAX_Y, 0, 0);
> + input_set_abs_params(input, ABS_MT_POSITION_X,
> + TRACKPAD2_MIN_X, TRACKPAD2_MAX_X, 0, 0);
> + input_set_abs_params(input, ABS_MT_POSITION_Y,
> + TRACKPAD2_MIN_Y, TRACKPAD2_MAX_Y, 0, 0);
> +
> + input_abs_set_res(input, ABS_X, TRACKPAD2_RES_X);
> + input_abs_set_res(input, ABS_Y, TRACKPAD2_RES_Y);
> + input_abs_set_res(input, ABS_MT_POSITION_X, TRACKPAD2_RES_X);
> + input_abs_set_res(input, ABS_MT_POSITION_Y, TRACKPAD2_RES_Y);
> } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> + input_set_abs_params(input, ABS_MT_ORIENTATION, -31, 32, 1, 0);
> input_set_abs_params(input, ABS_X, TRACKPAD_MIN_X,
> TRACKPAD_MAX_X, 4, 0);
> input_set_abs_params(input, ABS_Y, TRACKPAD_MIN_Y,
> @@ -447,7 +532,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
>
> input_set_events_per_packet(input, 60);
>
> - if (report_undeciphered) {
> + if (report_undeciphered &&
> + input->id.product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> __set_bit(EV_MSC, input->evbit);
> __set_bit(MSC_RAW, input->mscbit);
> }
> @@ -465,7 +551,8 @@ static int magicmouse_input_mapping(struct hid_device *hdev,
> msc->input = hi->input;
>
> /* Magic Trackpad does not give relative data after switching to MT */
> - if (hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD &&
> + if ((hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD ||
> + hi->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) &&
> field->flags & HID_MAIN_ITEM_RELATIVE)
> return -1;
>
> @@ -494,11 +581,20 @@ static int magicmouse_input_configured(struct hid_device *hdev,
> static int magicmouse_probe(struct hid_device *hdev,
> const struct hid_device_id *id)
> {
> - const u8 feature[] = { 0xd7, 0x01 };
> + const u8 *feature;
> + const u8 feature_mt[] = { 0xD7, 0x01 };
> + const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 };
> + const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 };
> u8 *buf;
> struct magicmouse_sc *msc;
> struct hid_report *report;
> int ret;
> + int feature_size;
> +
> + if (id->vendor == USB_VENDOR_ID_APPLE &&
> + id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
> + hdev->type != HID_TYPE_USBMOUSE)
> + return 0;
>
> msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL);
> if (msc == NULL) {
> @@ -532,7 +628,14 @@ static int magicmouse_probe(struct hid_device *hdev,
> if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
> report = hid_register_report(hdev, HID_INPUT_REPORT,
> MOUSE_REPORT_ID, 0);
> - else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> + else if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> + if (id->vendor == BT_VENDOR_ID_APPLE)
> + report = hid_register_report(hdev, HID_INPUT_REPORT,
> + TRACKPAD2_BT_REPORT_ID, 0);
> + else /* USB_VENDOR_ID_APPLE */
> + report = hid_register_report(hdev, HID_INPUT_REPORT,
> + TRACKPAD2_USB_REPORT_ID, 0);
> + } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD */
> report = hid_register_report(hdev, HID_INPUT_REPORT,
> TRACKPAD_REPORT_ID, 0);
> report = hid_register_report(hdev, HID_INPUT_REPORT,
> @@ -546,7 +649,20 @@ static int magicmouse_probe(struct hid_device *hdev,
> }
> report->size = 6;
>
> - buf = kmemdup(feature, sizeof(feature), GFP_KERNEL);
> + if (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
> + if (id->vendor == BT_VENDOR_ID_APPLE) {
> + feature_size = sizeof(feature_mt_trackpad2_bt);
> + feature = feature_mt_trackpad2_bt;
> + } else { /* USB_VENDOR_ID_APPLE */
> + feature_size = sizeof(feature_mt_trackpad2_usb);
> + feature = feature_mt_trackpad2_usb;
> + }
> + } else {
> + feature_size = sizeof(feature_mt);
> + feature = feature_mt;
> + }
> +
> + buf = kmemdup(feature, feature_size, GFP_KERNEL);
> if (!buf) {
> ret = -ENOMEM;
> goto err_stop_hw;
> @@ -560,10 +676,10 @@ static int magicmouse_probe(struct hid_device *hdev,
> * but there seems to be no other way of switching the mode.
> * Thus the super-ugly hacky success check below.
> */
> - ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(feature),
> + ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size,
> HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
> kfree(buf);
> - if (ret != -EIO && ret != sizeof(feature)) {
> + if (ret != -EIO && ret != feature_size) {
> hid_err(hdev, "unable to request touch data (%d)\n", ret);
> goto err_stop_hw;
> }
> @@ -579,6 +695,10 @@ static const struct hid_device_id magic_mice[] = {
> USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
> { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
> USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
> + { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
> + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
> + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
> + USB_DEVICE_ID_APPLE_MAGICTRACKPAD2), .driver_data = 0 },
> { }
> };
> MODULE_DEVICE_TABLE(hid, magic_mice);
> --
> 2.19.0.605.g01d371f741-goog
>
^ permalink raw reply
* Re: [PATCH 2/2] HID: google: add Whiskers driver to handle tablet mode properly
From: Benjamin Tissoires @ 2018-10-03 7:53 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Jiri Kosina, lee.jones, open list:HID CORE LAYER, lkml,
Nicolas Boichat
In-Reply-To: <CAE_wzQ-erV75NMwSVsrQM1FL3a6dveKKhm_+OQaoBO5v5JJyrw@mail.gmail.com>
On Tue, Oct 2, 2018 at 8:25 PM Dmitry Torokhov <dtor@chromium.org> wrote:
>
> Hi Benjamin,
>
> On Tue, Oct 2, 2018 at 1:53 AM Benjamin Tissoires
> <benjamin.tissoires@redhat.com> wrote:
> >
> > Hi Dmitry,
> >
> > On Mon, Oct 1, 2018 at 11:39 PM Dmitry Torokhov <dtor@chromium.org> wrote:
> > >
> > > This adds dedicated "Whiskers" driver that hooks both into HID and EC to
> > > produce proper SW_TABLET_SWITCH event from base presence bit from EC and
> > > base state (folded/unfolded) coming from USB/HID.
> > >
> > > Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
> > > Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
> > > ---
> >
> > I have a few comments, see below.
> >
> > > drivers/hid/Kconfig | 6 +
> > > drivers/hid/Makefile | 1 +
> > > drivers/hid/hid-google-hammer.c | 7 +-
> > > drivers/hid/hid-google-whiskers.c | 382 ++++++++++++++++++++++++++++++
> > > 4 files changed, 392 insertions(+), 4 deletions(-)
> > > create mode 100644 drivers/hid/hid-google-whiskers.c
> > >
> > > diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
> > > index e5ec47705fa2..0607f1c7129e 100644
> > > --- a/drivers/hid/Kconfig
> > > +++ b/drivers/hid/Kconfig
> > > @@ -355,6 +355,12 @@ config HID_GOOGLE_HAMMER
> > > ---help---
> > > Say Y here if you have a Google Hammer device.
> > >
> > > +config HID_GOOGLE_WHISKERS
> > > + tristate "Google Whiskers Keyboard"
> > > + depends on HID_GOOGLE_HAMMER && MFD_CROS_EC
> > > + ---help---
> > > + Say Y here if you have a Google Whiskers device.
> > > +
> > > config HID_GT683R
> > > tristate "MSI GT68xR LED support"
> > > depends on LEDS_CLASS && USB_HID
> > > diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
> > > index bd7ac53b75c5..241e583b6913 100644
> > > --- a/drivers/hid/Makefile
> > > +++ b/drivers/hid/Makefile
> > > @@ -47,6 +47,7 @@ obj-$(CONFIG_HID_EZKEY) += hid-ezkey.o
> > > obj-$(CONFIG_HID_GEMBIRD) += hid-gembird.o
> > > obj-$(CONFIG_HID_GFRM) += hid-gfrm.o
> > > obj-$(CONFIG_HID_GOOGLE_HAMMER) += hid-google-hammer.o
> > > +obj-$(CONFIG_HID_GOOGLE_WHISKERS) += hid-google-whiskers.o
> > > obj-$(CONFIG_HID_GT683R) += hid-gt683r.o
> > > obj-$(CONFIG_HID_GYRATION) += hid-gyration.o
> > > obj-$(CONFIG_HID_HOLTEK) += hid-holtek-kbd.o
> > > diff --git a/drivers/hid/hid-google-hammer.c b/drivers/hid/hid-google-hammer.c
> > > index 6bf4da7ad63a..81ddf9773df9 100644
> > > --- a/drivers/hid/hid-google-hammer.c
> > > +++ b/drivers/hid/hid-google-hammer.c
> > > @@ -18,6 +18,7 @@
> > > #include <linux/module.h>
> > >
> > > #include "hid-ids.h"
> > > +#include "hid-google-hammer.h"
> >
> > hid-google-hammer.h doesn't seem to be part of the patch (or in my
> > local tree at least).
>
> Ugh, my bad.
>
> >
> > >
> > > #define MAX_BRIGHTNESS 100
> > >
> > > @@ -90,8 +91,7 @@ static int hammer_register_leds(struct hid_device *hdev)
> > > return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
> > > }
> > >
> > > -static int hammer_input_configured(struct hid_device *hdev,
> > > - struct hid_input *hi)
> > > +int hammer_input_configured(struct hid_device *hdev, struct hid_input *hi)
> > > {
> > > struct list_head *report_list =
> > > &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
> > > @@ -116,6 +116,7 @@ static int hammer_input_configured(struct hid_device *hdev,
> > >
> > > return 0;
> > > }
> > > +EXPORT_SYMBOL_GPL(hammer_input_configured);
> >
> > I am not sure there is a need to separate the driver in 2 for whiskers
> > if it still need the LED settings from hammer.
> >
> > Given that the whiskers_ec struct will be populated independently,
> > having a common code path would not hurt hammer anyway.
>
> The idea is that we we have device with Hammer, we do not need to load
> bunch of code that deals with properly supporting Whiskers (connecting
> to the EC, creating a tablet switch device).
I understand this. However, the code is just loaded and will not be
actually used given that the ACPI PnP ID will not be present. So in
the end, you are saving a little bit of memory while making the whole
driver harder to understand as part of it is in an other place (and in
a different module).
Anyway, the policy we tend to have in the HID tree is to have one
quirked driver per vendor. It is not always the case, but that makes
things easier for users to know which HID driver they need to enable.
If you really think having a separate driver for whiskers is better, I
won't go too much against it.
>
> >
> > >
> > > static const struct hid_device_id hammer_devices[] = {
> > > { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> > > @@ -124,8 +125,6 @@ static const struct hid_device_id hammer_devices[] = {
> > > USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) },
> > > { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> > > USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) },
> > > - { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> > > - USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
> > > { }
> > > };
> > > MODULE_DEVICE_TABLE(hid, hammer_devices);
> > > diff --git a/drivers/hid/hid-google-whiskers.c b/drivers/hid/hid-google-whiskers.c
> > > new file mode 100644
> > > index 000000000000..ff3d2bfa1194
> > > --- /dev/null
> > > +++ b/drivers/hid/hid-google-whiskers.c
> > > @@ -0,0 +1,382 @@
> > > +// SPDX-License-Identifier: GPL-2.0+
> > > +//
> > > +// HID driver for Google Whiskers device.
> > > +//
> > > +// Copyright (c) 2018 Google Inc.
> > > +
> > > +#include <linux/acpi.h>
> > > +#include <linux/hid.h>
> > > +#include <linux/leds.h>
> >
> > doesn't seem to be used
>
> Indeed, will drop.
>
> >
> > > +#include <linux/mfd/cros_ec.h>
> > > +#include <linux/mfd/cros_ec_commands.h>
> > > +#include <linux/module.h>
> > > +#include <linux/platform_device.h>
> > > +#include <linux/pm_wakeup.h>
> > > +#include <linux/usb.h>
> >
> > This is usually not a good sign in HID drivers to rely on the low
> > level transport.
> > Basically, if you replay the device through uhid, this tends to break
> > heavily the kernel.
>
> The data parsing relies solely on HID, USB is here just to make sure
> we bind to the right interface, as you can see below.
Yes, but this is indeed the problem :)
When you replay a device with uhid, you don't have access to the
interface, and this is where the kernel oops happends :)
>
> >
> > > +#include <asm/unaligned.h>
> > > +
> > > +#include "hid-ids.h"
> > > +#include "hid-google-hammer.h"
> > > +
> > > +#define HID_UP_GOOGLEVENDOR 0xffd10000
> > > +#define HID_VD_KBD_FOLDED 0x00000019
> > > +#define WHISKERS_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED)
> >
> > Huh, this is weird there is no standard HID usage for this (not that
> > you can do anything about it).
>
> This is really specific to this device, I am not sure if it makes
> sense to standardize it. I guess we'll see if we make more of these.
Well, there is not much we can do apart joining the USB consortium and
pushing for a new HID standard usage.
So it was comment that doesn't need to be addressed right now :)
>
> >
> > > +
> > > +struct whiskers_ec {
> > > + struct device *dev; /* The platform device (EC) */
> > > + struct input_dev *input;
> > > + bool base_present;
> > > + struct notifier_block notifier;
> > > +};
> > > +
> > > +static struct whiskers_ec whiskers_ec;
> > > +static DEFINE_SPINLOCK(whiskers_ec_lock);
> > > +static DEFINE_MUTEX(whiskers_ec_reglock);
> > > +
> > > +static bool whiskers_parse_base_state(const void *data)
> > > +{
> > > + u32 switches = get_unaligned_le32(data);
> > > +
> > > + return !!(switches & BIT(EC_MKBP_BASE_ATTACHED));
> > > +}
> > > +
> > > +static int whiskers_ec_query_base(struct cros_ec_device *ec_dev, bool get_state,
> > > + bool *state)
> > > +{
> > > + struct ec_params_mkbp_info *params;
> > > + struct cros_ec_command *msg;
> > > + int ret;
> > > +
> > > + msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)),
> > > + GFP_KERNEL);
> > > + if (!msg)
> > > + return -ENOMEM;
> > > +
> > > + msg->command = EC_CMD_MKBP_INFO;
> > > + msg->version = 1;
> > > + msg->outsize = sizeof(*params);
> > > + msg->insize = sizeof(u32);
> > > + params = (struct ec_params_mkbp_info *)msg->data;
> > > + params->info_type = get_state ?
> > > + EC_MKBP_INFO_CURRENT : EC_MKBP_INFO_SUPPORTED;
> > > + params->event_type = EC_MKBP_EVENT_SWITCH;
> > > +
> > > + ret = cros_ec_cmd_xfer_status(ec_dev, msg);
> > > + if (ret >= 0) {
> > > + if (ret != sizeof(u32)) {
> > > + dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n",
> > > + ret, sizeof(u32));
> > > + ret = -EPROTO;
> > > + } else {
> > > + *state = whiskers_parse_base_state(msg->data);
> > > + ret = 0;
> > > + }
> > > + }
> > > +
> > > + kfree(msg);
> > > +
> > > + return ret;
> > > +}
> > > +
> > > +static int whiskers_ec_notify(struct notifier_block *nb,
> > > + unsigned long queued_during_suspend,
> > > + void *_notify)
> > > +{
> > > + struct cros_ec_device *ec = _notify;
> > > + unsigned long flags;
> > > + bool base_present;
> > > +
> > > + if (ec->event_data.event_type == EC_MKBP_EVENT_SWITCH) {
> > > + base_present = whiskers_parse_base_state(
> > > + &ec->event_data.data.switches);
> > > + dev_dbg(whiskers_ec.dev,
> > > + "%s: base: %d\n", __func__, base_present);
> > > +
> > > + if (device_may_wakeup(whiskers_ec.dev) ||
> > > + !queued_during_suspend) {
> > > +
> > > + pm_wakeup_event(whiskers_ec.dev, 0);
> > > +
> > > + spin_lock_irqsave(&whiskers_ec_lock, flags);
> > > +
> > > + /*
> > > + * While input layer dedupes the events, we do not want
> > > + * to disrupt the state reported by the base by
> > > + * overriding it with state reported by the LID. Only
> > > + * report changes, as we assume that on attach the base
> > > + * is not folded.
> > > + */
> > > + if (base_present != whiskers_ec.base_present) {
> > > + input_report_switch(whiskers_ec.input,
> > > + SW_TABLET_MODE,
> > > + !base_present);
> > > + input_sync(whiskers_ec.input);
> > > + whiskers_ec.base_present = base_present;
> > > + }
> > > +
> > > + spin_unlock_irqrestore(&whiskers_ec_lock, flags);
> > > + }
> > > + }
> > > +
> > > + return NOTIFY_OK;
> > > +}
> > > +
> > > +static __maybe_unused int whiskers_ec_resume(struct device *dev)
> > > +{
> > > + struct cros_ec_device *ec = dev_get_drvdata(dev->parent);
> > > + bool base_present;
> > > + int error;
> > > +
> > > + error = whiskers_ec_query_base(ec, true, &base_present);
> > > + if (error) {
> > > + dev_warn(dev, "failed to fetch base state on resume: %d\n",
> > > + error);
> > > + } else {
> > > + spin_lock_irq(&whiskers_ec_lock);
> > > +
> > > + whiskers_ec.base_present = base_present;
> > > +
> > > + /*
> > > + * Only report if base is disconnected. If base is connected,
> > > + * it will resend its state on resume, and we'll update it
> > > + * in whiskers_event().
> > > + */
> > > + if (!whiskers_ec.base_present) {
> > > + input_report_switch(whiskers_ec.input,
> > > + SW_TABLET_MODE, 1);
> > > + input_sync(whiskers_ec.input);
> > > + }
> > > +
> > > + spin_unlock_irq(&whiskers_ec_lock);
> > > + }
> > > +
> > > + return 0;
> > > +}
> > > +
> > > +static SIMPLE_DEV_PM_OPS(whiskers_ec_pm_ops, NULL, whiskers_ec_resume);
> > > +
> > > +static void whiskers_ec_set_input(struct input_dev *input)
> > > +{
> > > + /* Take the lock so whiskers_event does not race with us here */
> > > + spin_lock_irq(&whiskers_ec_lock);
> > > + whiskers_ec.input = input;
> > > + spin_unlock_irq(&whiskers_ec_lock);
> > > +}
> > > +
> > > +static int __whiskers_ec_probe(struct platform_device *pdev)
> > > +{
> > > + struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> > > + struct input_dev *input;
> > > + bool base_supported;
> > > + int error;
> > > +
> > > + error = whiskers_ec_query_base(ec, false, &base_supported);
> > > + if (error)
> > > + return error;
> > > +
> > > + if (!base_supported)
> > > + return -ENXIO;
> > > +
> > > + input = devm_input_allocate_device(&pdev->dev);
> > > + if (!input)
> > > + return -ENOMEM;
> > > +
> > > + input->name = "Whiskers Tablet Mode Switch";
> > > +
> > > + input->id.bustype = BUS_HOST;
> > > + input->id.version = 1;
> > > + input->id.product = 0;
> >
> > I wonder if you should not set the vendor to USB_VENDOR_ID_GOOGLE and
> > the product to something better.
>
> This is not USB interface, but host one (coming from the EC), so
> USB_VENDOR_ID_GOOGLE does not make sense here. I think I'll simply
> remove version and product.
Actually, the USB ids are already reused by the I2C devices. It makes
things simpler for hardware makers to have only one place where the
list of companies is.
Maybe we should just drop the "USB_" prefix in hid-ids.h, as it
doesn't make much sense anymore.
And I'd still rather have a proper VID/PID that userspace can rely on,
because matching on the name is doable, but sometimes names change :/
>
> >
> > > +
> > > + input_set_capability(input, EV_SW, SW_TABLET_MODE);
> > > +
> > > + error = input_register_device(input);
> > > + if (error) {
> > > + dev_err(&pdev->dev, "cannot register input device: %d\n",
> > > + error);
> > > + return error;
> > > + }
> > > +
> > > + /* Seed the state */
> > > + error = whiskers_ec_query_base(ec, true, &whiskers_ec.base_present);
> > > + if (error) {
> > > + dev_err(&pdev->dev, "cannot query base state: %d\n", error);
> > > + return error;
> > > + }
> > > +
> > > + input_report_switch(input, SW_TABLET_MODE, !whiskers_ec.base_present);
> > > +
> > > + whiskers_ec_set_input(input);
> > > +
> > > + whiskers_ec.dev = &pdev->dev;
> > > + whiskers_ec.notifier.notifier_call = whiskers_ec_notify;
> > > + error = blocking_notifier_chain_register(&ec->event_notifier,
> > > + &whiskers_ec.notifier);
> > > + if (error) {
> > > + dev_err(&pdev->dev, "cannot register notifier: %d\n", error);
> > > + whiskers_ec_set_input(NULL);
> > > + return error;
> > > + }
> > > +
> > > + device_init_wakeup(&pdev->dev, true);
> > > + return 0;
> > > +}
> > > +
> > > +static int whiskers_ec_probe(struct platform_device *pdev)
> > > +{
> > > + int retval;
> > > +
> > > + mutex_lock(&whiskers_ec_reglock);
> > > +
> > > + if (whiskers_ec.input) {
> > > + retval = -EBUSY;
> > > + goto out;
> > > + }
> > > +
> > > + retval = __whiskers_ec_probe(pdev);
> > > +
> > > +out:
> > > + mutex_unlock(&whiskers_ec_reglock);
> > > + return retval;
> > > +}
> > > +
> > > +static int whiskers_ec_remove(struct platform_device *pdev)
> > > +{
> > > + struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> > > +
> > > + mutex_lock(&whiskers_ec_reglock);
> > > +
> > > + blocking_notifier_chain_unregister(&ec->event_notifier,
> > > + &whiskers_ec.notifier);
> > > + whiskers_ec_set_input(NULL);
> > > +
> > > + mutex_unlock(&whiskers_ec_reglock);
> > > + return 0;
> > > +}
> > > +
> > > +static const struct acpi_device_id whiskers_ec_acpi_ids[] = {
> > > + { "GOOG000B", 0 },
> > > + { }
> > > +};
> > > +MODULE_DEVICE_TABLE(acpi, whiskers_ec_acpi_ids);
> > > +
> > > +static struct platform_driver whiskers_ec_driver = {
> > > + .probe = whiskers_ec_probe,
> > > + .remove = whiskers_ec_remove,
> > > + .driver = {
> > > + .name = "whiskers_ec",
> > > + .acpi_match_table = ACPI_PTR(whiskers_ec_acpi_ids),
> > > + .pm = &whiskers_ec_pm_ops,
> > > + },
> > > +};
> > > +
> > > +static int whiskers_input_mapping(struct hid_device *hdev, struct hid_input *hi,
> > > + struct hid_field *field,
> > > + struct hid_usage *usage,
> > > + unsigned long **bit, int *max)
> > > +{
> > > + if (usage->hid == WHISKERS_KBD_FOLDED) {
> > > + /*
> > > + * We do not want to have this usage mapped as it will get
> > > + * mixed in with "base attached" signal and delivered over
> > > + * separate input device for tablet switch mode.
> > > + */
> > > +
> > > + return -1;
> > > + }
> > > +
> > > + return 0;
> > > +}
> > > +
> > > +static int whiskers_event(struct hid_device *hid, struct hid_field *field,
> > > + struct hid_usage *usage, __s32 value)
> > > +{
> > > + unsigned long flags;
> > > +
> > > + if (usage->hid == WHISKERS_KBD_FOLDED) {
> > > + spin_lock_irqsave(&whiskers_ec_lock, flags);
> > > +
> > > + hid_dbg(hid, "%s: base: %d, folded: %d\n", __func__,
> > > + whiskers_ec.base_present, value);
> > > +
> > > + /*
> > > + * 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 (whiskers_ec.input && whiskers_ec.base_present) {
> > > + input_report_switch(whiskers_ec.input,
> > > + SW_TABLET_MODE, value);
> > > + input_sync(whiskers_ec.input);
> > > + }
> > > +
> > > + spin_unlock_irqrestore(&whiskers_ec_lock, flags);
> > > + return 1; /* We handled this event */
> > > + }
> > > +
> > > + return 0;
> > > +}
> > > +
> > > +static int whiskers_probe(struct hid_device *hdev,
> > > + const struct hid_device_id *id)
> > > +{
> > > + struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
> >
> > As mentioned earlier, this is prone to kernel oopses when using uhid.
>
> Hmm, we have similar code in hid-google-hammer.c, I guess we need to
> fix that there too.
If you can do that, I'll be happy :)
>
> >
> > > + int ret;
> > > +
> > > + /*
> > > + * We always want to poll for, and handle tablet mode events, even when
> > > + * nobody has opened the input device. This also prevents the hid core
> > > + * from dropping early tablet mode events from the device.
> > > + */
> > > + if (intf->cur_altsetting->desc.bInterfaceProtocol ==
> > > + USB_INTERFACE_PROTOCOL_KEYBOARD)
> >
> > Can't you rely on something embedded in the report descriptor instead?
> > This way you can drop the USB dependency.
>
> Something like:
>
> static bool hammer_is_keyboard(...)
> {
>
> rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
> list_for_each_entry(report, &rep_enum->report_list, list) {
> for (i = 0; i < report->maxfield; i++) {
> if (report->field[i].application == HID_GD_KEYBOARD)
> return true;
> }
>
> return false;
> }
>
> ?
Yep. However, you can simplify it a little bit since later HID
revisions as we now store the application in the report:
rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
list_for_each_entry(report, &rep_enum->report_list, list) {
if (report->application == HID_GD_KEYBOARD)
return true;
}
return false;
(whitespace apart)
Hopefully, this will work for you.
>
> >
> > > + hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
> > > +
> > > + ret = hid_parse(hdev);
> > > + if (!ret)
> > > + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
> > > +
> > > + return ret;
> > > +}
> > > +
> > > +static const struct hid_device_id whiskers_hid_devices[] = {
> > > + { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
> > > + USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
> > > + { }
> > > +};
> > > +MODULE_DEVICE_TABLE(hid, whiskers_hid_devices);
> > > +
> > > +static struct hid_driver whiskers_hid_driver = {
> > > + .name = "whiskers",
> > > + .id_table = whiskers_hid_devices,
> > > + .probe = whiskers_probe,
> > > + .input_configured = hammer_input_configured,
> > > + .input_mapping = whiskers_input_mapping,
> > > + .event = whiskers_event,
> > > +};
> > > +
> > > +static int __init whiskers_init(void)
> > > +{
> > > + int error;
> > > +
> > > + error = platform_driver_register(&whiskers_ec_driver);
> > > + if (error)
> > > + return error;
> > > +
> > > + error = hid_register_driver(&whiskers_hid_driver);
> > > + if (error) {
> > > + platform_driver_unregister(&whiskers_ec_driver);
> > > + return error;
> > > + }
> > > +
> > > + return 0;
> > > +}
> > > +module_init(whiskers_init);
> > > +
> > > +static void __exit whiskers_exit(void)
> > > +{
> > > + hid_unregister_driver(&whiskers_hid_driver);
> > > + platform_driver_unregister(&whiskers_ec_driver);
> > > +}
> > > +module_exit(whiskers_exit);
> > > +
> > > +MODULE_LICENSE("GPL");
> > > --
> > > 2.19.0.605.g01d371f741-goog
> > >
> >
>
> Thanks.
Cheers,
Benjamin
>
> --
> Dmitry
^ permalink raw reply
* Re: [PATCH] [v4] HID: add support for Apple Magic Trackpad 2
From: Jiri Kosina @ 2018-10-03 8:59 UTC (permalink / raw)
To: Sean O'Brien
Cc: Benjamin Tissoires, Marek Wyborski, linux-kernel, Dmitry Torokhov,
linux-input, Henrik Rydberg, Claudio Mettler
In-Reply-To: <20181002225337.7349-1-seobrien@chromium.org>
On Tue, 2 Oct 2018, Sean O'Brien wrote:
> USB device
> Vendor 05ac (Apple)
> Device 0265 (Magic Trackpad 2)
> Bluetooth device
> Vendor 004c (Apple)
> Device 0265 (Magic Trackpad 2)
>
> Add support for Apple Magic Trackpad 2 over USB and bluetooth, putting
> the device in multi-touch mode.
Applied, thanks.
--
Jiri Kosina
SUSE Labs
^ permalink raw reply
* Re: [PATCH] HID: i2c-hid: Add a small delay after powering on/off the device
From: Jiri Kosina @ 2018-10-03 9:10 UTC (permalink / raw)
To: Kai-Heng Feng; +Cc: benjamin.tissoires, hdegoede, linux-input, linux-kernel
In-Reply-To: <20181001035313.21453-1-kai.heng.feng@canonical.com>
On Mon, 1 Oct 2018, Kai-Heng Feng wrote:
> Raydium touchpanel (2386:4B33) sometimes does not workin desktop session
> although it works in display manager.
>
> During user logging, the display manager exits, close the HID device,
> then the device gets runtime suspended and powered off. The desktop
> session begins shortly after, opens the HID device, then the device gets
> runtime resumed and powered on.
>
> If the trasition from display manager to desktop sesesion is fast, the
> touchpanel cannot switch from powered off to powered on in short
> timeframe. So add a small delay to workaround the issue.
>
> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> ---
> drivers/hid/i2c-hid/i2c-hid.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c
> index f3076659361a..ff5682cc1bce 100644
> --- a/drivers/hid/i2c-hid/i2c-hid.c
> +++ b/drivers/hid/i2c-hid/i2c-hid.c
> @@ -409,6 +409,8 @@ static int i2c_hid_set_power(struct i2c_client *client, int power_state)
>
> if (ret)
> dev_err(&client->dev, "failed to change power setting.\n");
> + else
> + msleep(20);
In case there really is no other way around it, at least please add
comment explaining why this ugly msleep() is there. Thanks,
--
Jiri Kosina
SUSE Labs
^ permalink raw reply
* Re: [PATCH] HID: i2c-hid: Add a small delay after powering on/off the device
From: Kai Heng Feng @ 2018-10-03 9:21 UTC (permalink / raw)
To: Jiri Kosina; +Cc: Benjamin Tissoires, Hans de Goede, linux-input, linux-kernel
In-Reply-To: <nycvar.YFH.7.76.1810031109200.14430@cbobk.fhfr.pm>
> On Oct 3, 2018, at 5:10 PM, Jiri Kosina <jikos@kernel.org> wrote:
>
> On Mon, 1 Oct 2018, Kai-Heng Feng wrote:
>
>> Raydium touchpanel (2386:4B33) sometimes does not workin desktop session
>> although it works in display manager.
>>
>> During user logging, the display manager exits, close the HID device,
>> then the device gets runtime suspended and powered off. The desktop
>> session begins shortly after, opens the HID device, then the device gets
>> runtime resumed and powered on.
>>
>> If the trasition from display manager to desktop sesesion is fast, the
>> touchpanel cannot switch from powered off to powered on in short
>> timeframe. So add a small delay to workaround the issue.
>>
>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>> ---
>> drivers/hid/i2c-hid/i2c-hid.c | 2 ++
>> 1 file changed, 2 insertions(+)
>>
>> diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c
>> index f3076659361a..ff5682cc1bce 100644
>> --- a/drivers/hid/i2c-hid/i2c-hid.c
>> +++ b/drivers/hid/i2c-hid/i2c-hid.c
>> @@ -409,6 +409,8 @@ static int i2c_hid_set_power(struct i2c_client *client, int power_state)
>>
>> if (ret)
>> dev_err(&client->dev, "failed to change power setting.\n");
>> + else
>> + msleep(20);
>
> In case there really is no other way around it, at least please add
> comment explaining why this ugly msleep() is there. Thanks,
I am working on a better approach for this issue, I’ll send a v2 soon.
Kai-Heng
>
> --
> Jiri Kosina
> SUSE Labs
>
^ permalink raw reply
* [PATCH 0/3] HID: debug: fix the ring buffer implementation
From: Vladis Dronov @ 2018-10-03 17:19 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, linux-input, linux-kernel
This patchset is fixing some aspects of the ring buffer implementation in
drivers/hid/hid-debug.c. This implementation has certain problem points:
- it may stuck in an infinite loop
- it may return corrupted data
- a reader and a writer are not protected by spinlocks, which can lead to
the corrupted data
The suggested patchset is a new ring buffer implementation which overwrites
the oldest data in case of an overflow. One can verify the suggested ring
buffer implementation by fuzzing it with modified kernel and fuzzer-reader
at: https://gist.github.com/nefigtut/33d56e3870b67493cc867344aed2a062
Vladis Dronov (3):
HID: debug: avoid infinite loop and corrupting data
HID: debug: provide reader-writer locking for the ring buffer
HID: debug: fix ring buffer implementation
drivers/hid/hid-debug.c | 201 ++++++++++++++++++++++++++------------
include/linux/hid-debug.h | 1 +
2 files changed, 142 insertions(+), 60 deletions(-)
--
2.19.0
^ permalink raw reply
* [PATCH 1/3] HID: debug: avoid infinite loop and corrupting data
From: Vladis Dronov @ 2018-10-03 17:19 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, linux-input, linux-kernel; +Cc: Vladis Dronov
In-Reply-To: <20181003171936.11271-1-vdronov@redhat.com>
hid_debug_events_read() does not properly handle the case when tail < head
and count < HID_DEBUG_BUFSIZE - head and returns corrupted data. Also,
after commit 717adfdaf147 ("HID: debug: check length before
copy_to_user()") it can enter an infinite loop if called with count == 0.
Fix this by properly handling this case and adding a check.
Also, the function has "while (ret == 0)" loop which is not needed, remove
it.
Signed-off-by: Vladis Dronov <vdronov@redhat.com>
---
drivers/hid/hid-debug.c | 109 ++++++++++++++++++++++++----------------
1 file changed, 65 insertions(+), 44 deletions(-)
diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index b48100236df8..20580871b0ec 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -722,7 +722,7 @@ void hid_dump_input(struct hid_device *hdev, struct hid_usage *usage, __s32 valu
hid_debug_event(hdev, buf);
kfree(buf);
- wake_up_interruptible(&hdev->debug_wait);
+ wake_up_interruptible(&hdev->debug_wait);
}
EXPORT_SYMBOL_GPL(hid_dump_input);
@@ -1112,73 +1112,95 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
int ret = 0, len;
DECLARE_WAITQUEUE(wait, current);
- mutex_lock(&list->read_mutex);
- while (ret == 0) {
- if (list->head == list->tail) {
- add_wait_queue(&list->hdev->debug_wait, &wait);
- set_current_state(TASK_INTERRUPTIBLE);
+ if (!count)
+ return 0;
- while (list->head == list->tail) {
- if (file->f_flags & O_NONBLOCK) {
- ret = -EAGAIN;
- break;
- }
- if (signal_pending(current)) {
- ret = -ERESTARTSYS;
- break;
- }
+ mutex_lock(&list->read_mutex);
+ if (list->head == list->tail) {
+ add_wait_queue(&list->hdev->debug_wait, &wait);
+ set_current_state(TASK_INTERRUPTIBLE);
+
+ while (list->head == list->tail) {
+ if (file->f_flags & O_NONBLOCK) {
+ ret = -EAGAIN;
+ break;
+ }
- if (!list->hdev || !list->hdev->debug) {
- ret = -EIO;
- set_current_state(TASK_RUNNING);
- goto out;
- }
+ if (signal_pending(current)) {
+ ret = -ERESTARTSYS;
+ break;
+ }
- /* allow O_NONBLOCK from other threads */
- mutex_unlock(&list->read_mutex);
- schedule();
- mutex_lock(&list->read_mutex);
- set_current_state(TASK_INTERRUPTIBLE);
+ if (!list->hdev || !list->hdev->debug) {
+ ret = -EIO;
+ set_current_state(TASK_RUNNING);
+ goto out;
}
- set_current_state(TASK_RUNNING);
- remove_wait_queue(&list->hdev->debug_wait, &wait);
+ /* allow O_NONBLOCK from other threads */
+ mutex_unlock(&list->read_mutex);
+ schedule();
+ mutex_lock(&list->read_mutex);
+ set_current_state(TASK_INTERRUPTIBLE);
}
- if (ret)
- goto out;
+ set_current_state(TASK_RUNNING);
+ remove_wait_queue(&list->hdev->debug_wait, &wait);
+ }
+
+ if (ret)
+ goto out;
- /* pass the ringbuffer contents to userspace */
+ /* pass the ringbuffer content to userspace */
copy_rest:
- if (list->tail == list->head)
- goto out;
- if (list->tail > list->head) {
- len = list->tail - list->head;
- if (len > count)
- len = count;
+ if (list->tail == list->head)
+ goto out;
+
+ /* the data from the head to the tail in the buffer is linear */
+ if (list->tail > list->head) {
+ len = list->tail - list->head;
+ if (len > count)
+ len = count;
- if (copy_to_user(buffer + ret, &list->hid_debug_buf[list->head], len)) {
+ if (copy_to_user(buffer + ret,
+ &list->hid_debug_buf[list->head], len)) {
+ ret = -EFAULT;
+ goto out;
+ }
+ ret += len;
+ list->head += len;
+ } else {
+ len = HID_DEBUG_BUFSIZE - list->head;
+
+ /* the data is split in 2 pieces: from the head to the end of
+ * the ring buffer and from the start of the ring buffer to the
+ * tail. check if we need to copy out only the part between the
+ * head and the end.
+ */
+ if (len > count) {
+ len = count;
+
+ if (copy_to_user(buffer,
+ &list->hid_debug_buf[list->head], len)) {
ret = -EFAULT;
goto out;
}
ret += len;
list->head += len;
} else {
- len = HID_DEBUG_BUFSIZE - list->head;
- if (len > count)
- len = count;
-
- if (copy_to_user(buffer, &list->hid_debug_buf[list->head], len)) {
+ if (copy_to_user(buffer,
+ &list->hid_debug_buf[list->head], len)) {
ret = -EFAULT;
goto out;
}
list->head = 0;
ret += len;
count -= len;
+
+ /* check for a corner case when len == count */
if (count > 0)
goto copy_rest;
}
-
}
out:
mutex_unlock(&list->read_mutex);
@@ -1256,4 +1278,3 @@ void hid_debug_exit(void)
{
debugfs_remove_recursive(hid_debug_root);
}
-
--
2.19.0
^ permalink raw reply related
* [PATCH 2/3] HID: debug: provide reader-writer locking for the ring buffer
From: Vladis Dronov @ 2018-10-03 17:19 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, linux-input, linux-kernel; +Cc: Vladis Dronov
In-Reply-To: <20181003171936.11271-1-vdronov@redhat.com>
hdev->debug_list->hid_debug_buf is not protected from concurrent updates
from the writer, hid_debug_event() and reads by the reader,
hid_debug_events_read(). Fix this by adding per-list-element spinlock.
Also introduce a temporary buffer tempbuf so copy_to_user() is not called
from under a spinlock.
Signed-off-by: Vladis Dronov <vdronov@redhat.com>
---
drivers/hid/hid-debug.c | 47 +++++++++++++++++++++++----------------
include/linux/hid-debug.h | 1 +
2 files changed, 29 insertions(+), 19 deletions(-)
diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index 20580871b0ec..e827784baf1a 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -663,15 +663,17 @@ void hid_debug_event(struct hid_device *hdev, char *buf)
{
unsigned i;
struct hid_debug_list *list;
- unsigned long flags;
+ unsigned long flags, flags2;
spin_lock_irqsave(&hdev->debug_list_lock, flags);
list_for_each_entry(list, &hdev->debug_list, node) {
+ spin_lock_irqsave(&list->list_lock, flags2);
for (i = 0; buf[i]; i++)
list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] =
buf[i];
list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;
- }
+ spin_unlock_irqrestore(&list->list_lock, flags2);
+ }
spin_unlock_irqrestore(&hdev->debug_list_lock, flags);
wake_up_interruptible(&hdev->debug_wait);
@@ -1095,6 +1097,7 @@ static int hid_debug_events_open(struct inode *inode, struct file *file)
}
list->hdev = (struct hid_device *) inode->i_private;
file->private_data = list;
+ spin_lock_init(&list->list_lock);
mutex_init(&list->read_mutex);
spin_lock_irqsave(&list->hdev->debug_list_lock, flags);
@@ -1109,6 +1112,8 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
struct hid_debug_list *list = file->private_data;
+ char *tmpbuf;
+ unsigned long flags;
int ret = 0, len;
DECLARE_WAITQUEUE(wait, current);
@@ -1151,10 +1156,18 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
if (ret)
goto out;
- /* pass the ringbuffer content to userspace */
+ tmpbuf = kmalloc(min_t(size_t, count, HID_DEBUG_BUFSIZE),
+ GFP_KERNEL|__GFP_NOWARN);
+ if (!tmpbuf) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ /* lock and copy the ringbuffer content to tmpbuf */
+ spin_lock_irqsave(&list->list_lock, flags);
copy_rest:
if (list->tail == list->head)
- goto out;
+ goto out_unlock;
/* the data from the head to the tail in the buffer is linear */
if (list->tail > list->head) {
@@ -1162,11 +1174,7 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
if (len > count)
len = count;
- if (copy_to_user(buffer + ret,
- &list->hid_debug_buf[list->head], len)) {
- ret = -EFAULT;
- goto out;
- }
+ memcpy(tmpbuf + ret, &list->hid_debug_buf[list->head], len);
ret += len;
list->head += len;
} else {
@@ -1180,19 +1188,11 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
if (len > count) {
len = count;
- if (copy_to_user(buffer,
- &list->hid_debug_buf[list->head], len)) {
- ret = -EFAULT;
- goto out;
- }
+ memcpy(tmpbuf, &list->hid_debug_buf[list->head], len);
ret += len;
list->head += len;
} else {
- if (copy_to_user(buffer,
- &list->hid_debug_buf[list->head], len)) {
- ret = -EFAULT;
- goto out;
- }
+ memcpy(tmpbuf, &list->hid_debug_buf[list->head], len);
list->head = 0;
ret += len;
count -= len;
@@ -1202,6 +1202,15 @@ static ssize_t hid_debug_events_read(struct file *file, char __user *buffer,
goto copy_rest;
}
}
+
+out_unlock:
+ spin_unlock_irqrestore(&list->list_lock, flags);
+
+ /* copy out tmpbuf content to userspace */
+ if (ret && copy_to_user(buffer, tmpbuf, ret))
+ ret = -EFAULT;
+ kfree(tmpbuf);
+
out:
mutex_unlock(&list->read_mutex);
return ret;
diff --git a/include/linux/hid-debug.h b/include/linux/hid-debug.h
index 8663f216c563..f58665651cb5 100644
--- a/include/linux/hid-debug.h
+++ b/include/linux/hid-debug.h
@@ -45,6 +45,7 @@ struct hid_debug_list {
struct fasync_struct *fasync;
struct hid_device *hdev;
struct list_head node;
+ spinlock_t list_lock;
struct mutex read_mutex;
};
--
2.19.0
^ permalink raw reply related
* [PATCH 3/3] HID: debug: fix the ring buffer writer implementation
From: Vladis Dronov @ 2018-10-03 17:19 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires, linux-input, linux-kernel; +Cc: Vladis Dronov
In-Reply-To: <20181003171936.11271-1-vdronov@redhat.com>
ring buffer implementation hid_debug_event() is strange allowing a lost
of data. it does not move head pointer on append and uses per-byte for()
loop. fix this by introducing a new ring buffer implementation, which
overwrites the oldest data in case of the ring buffer overflow. it uses
some calculations for the buffer pointers but only 2 or less memcpy()
calls.
Signed-off-by: Vladis Dronov <vdronov@redhat.com>
---
drivers/hid/hid-debug.c | 66 ++++++++++++++++++++++++++++++++++++-----
1 file changed, 58 insertions(+), 8 deletions(-)
diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index de640e4132c7..63247ac4a9ce 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -661,20 +661,70 @@ EXPORT_SYMBOL_GPL(hid_dump_device);
/* enqueue string to 'events' ring buffer */
void hid_debug_event(struct hid_device *hdev, char *buf)
{
- unsigned i;
struct hid_debug_list *list;
- unsigned long flags, flags2;
+ unsigned long flags1, flags2;
+ unsigned int part1, part2;
+ size_t len;
- spin_lock_irqsave(&hdev->debug_list_lock, flags);
+ if (!buf || !buf[0])
+ return;
+
+ len = strlen(buf);
+
+ spin_lock_irqsave(&hdev->debug_list_lock, flags1);
list_for_each_entry(list, &hdev->debug_list, node) {
spin_lock_irqsave(&list->list_lock, flags2);
- for (i = 0; buf[i]; i++)
- list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] =
- buf[i];
- list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;
+
+ /* len >= BUFSIZE, copy the last HID_DEBUG_BUFSIZE-1 bytes */
+ if (len > HID_DEBUG_BUFSIZE - 1) {
+ memcpy(list->hid_debug_buf,
+ buf + len - (HID_DEBUG_BUFSIZE - 1),
+ HID_DEBUG_BUFSIZE - 1);
+
+ list->head = 0;
+ list->tail = HID_DEBUG_BUFSIZE - 1;
+ } else {
+ /* append data to the ring buffer */
+ part1 = min_t(size_t, HID_DEBUG_BUFSIZE - list->tail,
+ len);
+ part2 = len - part1;
+
+ memcpy(list->hid_debug_buf + list->tail, buf, part1);
+
+ /* check if head must be moved as the ring is full */
+ if ((list->tail < list->head) &&
+ (list->tail + part1 >= list->head)) {
+ list->head = list->tail + part1 + 1;
+
+ if (list->head >= HID_DEBUG_BUFSIZE)
+ list->head -= HID_DEBUG_BUFSIZE;
+ }
+
+ /* set tail after the end of appended data */
+ list->tail = list->tail + part1;
+ if (list->tail == HID_DEBUG_BUFSIZE) {
+ list->tail = 0;
+ if (list->head == 0)
+ list->head = 1;
+ }
+
+ /* append the 2nd part of data to the ring */
+ if (part2 > 0) {
+ memcpy(list->hid_debug_buf, buf + part1, part2);
+
+ /* check if head must be moved again as
+ * the ring is full
+ */
+ if (list->tail + part2 >= list->head)
+ list->head = list->tail + part2 + 1;
+
+ list->tail = list->tail + part2;
+ }
+ }
+
spin_unlock_irqrestore(&list->list_lock, flags2);
}
- spin_unlock_irqrestore(&hdev->debug_list_lock, flags);
+ spin_unlock_irqrestore(&hdev->debug_list_lock, flags1);
wake_up_interruptible(&hdev->debug_wait);
}
--
2.19.0
^ permalink raw reply related
* Re: [PATCH V2] hid: hid-core: Fix a sleep-in-atomic-context bug in __hid_request()
From: Jia-Ju Bai @ 2018-10-04 3:14 UTC (permalink / raw)
To: Jiri Kosina; +Cc: benjamin.tissoires, linux-input, linux-kernel
In-Reply-To: <nycvar.YFH.7.76.1809292118240.15880@cbobk.fhfr.pm>
On 2018/9/30 3:20, Jiri Kosina wrote:
> On Sat, 29 Sep 2018, Jia-Ju Bai wrote:
>
>>>> picolcd_send_and_wait (acquire a spinlock)
>>>> hid_hw_request
>>>> __hid_request
>>>> hid_alloc_report_buf(GFP_KERNEL)
>>>>
>>>> picolcd_reset (acquire a spinlock)
>>>> hid_hw_request
>>>> __hid_request
>>>> hid_alloc_report_buf(GFP_KERNEL)
>>>>
>>>> lg4ff_play (acquire a spinlock)
>>>> hid_hw_request
>>>> __hid_request
>>>> hid_alloc_report_buf(GFP_KERNEL)
>>>>
>>>> lg4ff_set_autocenter_ffex (acquire a spinlock)
>>>> hid_hw_request
>>>> __hid_request
>>>> hid_alloc_report_buf(GFP_KERNEL)
>>> Hm, so it's always drivers calling out into core in atomic context. So
>>> either we take this, and put our bets on being able to allocate the buffer
>>> without sleeping,
>> In my opinion, I prefer this way.
> Why? Forcing all the report buffer to be limited to be non-sleeping
> allocations just because of two drivers, looks like an overkill, and
> actually calls for more issues (as GFP_ATOMIC is of course in principle
> less likely to succeed).
>
Okay, I thought that using GFP_ATOMIC is the simplest way to fix these bugs.
But I check the Linux kernel code again, and find that hid_hw_request()
are called at many places.
So changing this function may affect many drivers.
I agree to only change the two drivers, and explicitly anotate
__hid_request() with might_sleep().
Best wishes,
Jia-Ju Bai
^ permalink raw reply
* [PATCH V7 0/3] da7280: haptic driver submission
From: Roy Im @ 2018-10-04 6:45 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring
Cc: Mark Rutland, Support Opensource, devicetree, linux-input,
linux-kernel
This patch adds support for the Dialog DA7280 Haptic driver IC.
In this patch set the following is provided:
[PATCH V2 1/3] MAINTAINERS file update for DA7280
[PATCH V2 2/3] DA7280 DT Binding
[PATCH V2 3/3] DA7280 Driver
This patch applies against linux-next and v4.19-rc6
Thank you,
Roy Im, Dialog Semiconductor Ltd.
Roy Im (3):
MAINTAINERS: da7280 updates to the Dialog Semiconductor search terms
dt-bindings: input: Add document bindings for DA7280
Input: new da7280 haptic driver
.../devicetree/bindings/input/dlg,da7280.txt | 105 ++
MAINTAINERS | 2 +
drivers/input/misc/Kconfig | 13 +
drivers/input/misc/Makefile | 1 +
drivers/input/misc/da7280.c | 1393 ++++++++++++++++++++
drivers/input/misc/da7280.h | 412 ++++++
6 files changed, 1926 insertions(+)
create mode 100644 Documentation/devicetree/bindings/input/dlg,da7280.txt
create mode 100644 drivers/input/misc/da7280.c
create mode 100644 drivers/input/misc/da7280.h
--
end-of-patch for PATCH V7
^ permalink raw reply
* [PATCH V7 2/3] dt-bindings: input: Add document bindings for DA7280
From: Roy Im @ 2018-10-04 6:45 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring
Cc: Mark Rutland, Support Opensource, devicetree, linux-input,
linux-kernel
In-Reply-To: <cover.1538635541.git.Roy.Im@diasemi.com>
Add device tree binding information for DA7280 haptic driver.
Example bindings for DA7280 are added.
Reviewed-by: Rob Herring <robh@kernel.org>.
Signed-off-by: Roy Im <roy.im.opensource@diasemi.com>
---
v7: No changes.
v6: No changes.
v5: Updated descriptions and fixed errors.
v4: Fixed commit message, properties.
v3: Fixed subject format.
v2: No changes
.../devicetree/bindings/input/dlg,da7280.txt | 105 ++++++++++++++++++++
1 file changed, 105 insertions(+)
create mode 100644 Documentation/devicetree/bindings/input/dlg,da7280.txt
diff --git a/Documentation/devicetree/bindings/input/dlg,da7280.txt b/Documentation/devicetree/bindings/input/dlg,da7280.txt
new file mode 100644
index 0000000..a25a12f
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/dlg,da7280.txt
@@ -0,0 +1,105 @@
+Dialog Semiconductor DA7280 Haptics bindings
+
+Required properties:
+- compatible: Should be "dlg,da7280".
+- reg: Specifies the I2C slave address.
+
+- interrupt-parent : Specifies the phandle of the interrupt controller to
+ which the IRQs from DA7280 are delivered to.
+
+- dlg,actuator-type: Set Actuator type. it should be one of:
+ "LRA" - Linear Resonance Actuator type.
+ "ERM-bar" - Bar type Eccentric Rotating Mass.
+ "ERM-coin" - Coin type Eccentric Rotating Mass.
+
+- dlg,op-mode: Haptic operation mode.
+ Possible values:
+ 1 - Direct register override(DRO) mode triggered by i2c(default),
+ 2 - PWM data source mode controlled by PWM duty,
+ 3 - Register triggered waveform memory(RTWM) mode, the pattern
+ assigned to the PS_SEQ_ID played as much times as PS_SEQ_LOOP,
+ 4 - Edge triggered waveform memory(ETWM) mode, external GPI(N)
+ control are required to enable/disable and it needs to keep
+ device enabled by sending magnitude (X > 0),
+ the pattern is assigned to the GPI(N)_SEQUENCE_ID below.
+ For more details, please see the datasheet.
+
+- dlg,nom-microvolt: Nominal actuator voltage rating.
+ Valid values: 0 - 6000000.
+- dlg,abs-max-microvolt: Absolute actuator maximum voltage rating.
+ Valid values: 0 - 6000000.
+- dlg,imax-microamp: Actuator max current rating.
+ Valid values: 0 - 252000.
+ Default: 130000.
+- dlg,impd-micro-ohms: the impedance of the actuator in micro ohms.
+ Valid values: 0 - 1500000000.
+
+Optional properties:
+- pwms : phandle to the physical PWM(Pulse Width Modulation) device.
+ PWM properties should be named "pwms". And number of cell is different
+ for each pwm device.
+ (See Documentation/devicetree/bindings/pwm/pwm.txt
+ for further information relating to pwm properties)
+
+- dlg,ps-seq-id: the PS_SEQ_ID(pattern ID in waveform memory inside chip)
+ to play back when RTWM-MODE is enabled.
+ Valid range: 0 - 15.
+- dlg,ps-seq-loop: the PS_SEQ_LOOP, Number of times the pre-stored sequence
+ pointed to by PS_SEQ_ID or GPI(N)_SEQUENCE_ID is repeated.
+ Valid range: 0 - 15.
+- dlg,gpiN-seq-id: the GPI(N)_SEQUENCE_ID, pattern to play
+ when gpi0 is triggered, 'N' must be 0 - 2.
+ Valid range: 0 - 15.
+- dlg,gpiN-mode: the pattern mode which can select either
+ "Single-pattern" or "Multi-pattern", 'N' must be 0 - 2.
+- dlg,gpiN-polarity: gpiN polarity which can be chosen among
+ "Rising-edge", "Falling-edge" and "Both-edge",
+ 'N' must be 0 - 2
+ Haptic will work by this edge option in case of ETWM mode.
+
+- dlg,resonant-freq-hz: use in case of LRA.
+ the frequency range: 50 - 300.
+ Default: 205.
+
+- dlg,bemf-sens-enable: Enable for internal loop computations.
+- dlg,freq-track-enable: Enable for resonant frequency tracking.
+- dlg,acc-enable: Enable for active acceleration.
+- dlg,rapid-stop-enable: Enable for rapid stop.
+- dlg,amp-pid-enable: Enable for the amplitude PID.
+- dlg,mem-array: Customized waveform memory(patterns) data downloaded to
+ the device during initialization. This is an array of 100 values(u8).
+
+For further information, see device datasheet.
+
+======
+
+Example:
+
+ haptics: da7280-haptics@4a {
+ compatible = "dlg,da7280";
+ reg = <0x4a>;
+ interrupt-parent = <&gpio6>;
+ interrupts = <11 IRQ_TYPE_LEVEL_LOW>;
+ dlg,actuator-type = "LRA";
+ dlg,op-mode = <1>;
+ dlg,nom-microvolt = <2000000>;
+ dlg,abs-max-microvolt = <2000000>;
+ dlg,imax-microamp = <170000>;
+ dlg,resonant-freq-hz = <180>;
+ dlg,impd-micro-ohms = <10500000>;
+ dlg,freq-track-enable;
+ dlg,rapid-stop-enable;
+ dlg,mem-array = <
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+ >;
+
+ };
--
end-of-patch for PATCH V7
^ permalink raw reply related
* [PATCH V7 1/3] MAINTAINERS: da7280 updates to the Dialog Semiconductor search terms
From: Roy Im @ 2018-10-04 6:45 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring
Cc: Mark Rutland, Support Opensource, devicetree, linux-input,
linux-kernel
In-Reply-To: <cover.1538635541.git.Roy.Im@diasemi.com>
This patch adds the da7280 bindings doc and driver to the Dialog
Semiconductor support list.
Signed-off-by: Roy Im <roy.im.opensource@diasemi.com>
---
v7: No changes.
v6: No changes.
v5: No changes.
v4: No changes.
v3: No changes.
v2: No changes.
MAINTAINERS | 2 ++
1 file changed, 2 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index b22e7fd..1af587f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4313,6 +4313,7 @@ S: Supported
F: Documentation/hwmon/da90??
F: Documentation/devicetree/bindings/mfd/da90*.txt
F: Documentation/devicetree/bindings/input/da90??-onkey.txt
+F: Documentation/devicetree/bindings/input/dlg,da72??.txt
F: Documentation/devicetree/bindings/thermal/da90??-thermal.txt
F: Documentation/devicetree/bindings/regulator/da92*.txt
F: Documentation/devicetree/bindings/watchdog/da90??-wdt.txt
@@ -4321,6 +4322,7 @@ F: drivers/gpio/gpio-da90??.c
F: drivers/hwmon/da90??-hwmon.c
F: drivers/iio/adc/da91??-*.c
F: drivers/input/misc/da90??_onkey.c
+F: drivers/input/misc/da72??.[ch]
F: drivers/input/touchscreen/da9052_tsi.c
F: drivers/leds/leds-da90??.c
F: drivers/mfd/da903x.c
--
end-of-patch for PATCH V7
^ permalink raw reply related
page: next (older) | prev (newer) | latest
- recent:[subjects (threaded)|topics (new)|topics (active)]
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox