f2c60e
From 47c84357d95eccd77c1320b4bca74bbec649ef3c Mon Sep 17 00:00:00 2001
f2c60e
From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
f2c60e
Date: Mon, 3 Apr 2017 18:18:21 +0200
f2c60e
Subject: [PATCH] Input: rmi4 - remove the need for artificial IRQ in case of
f2c60e
 HID
f2c60e
f2c60e
The IRQ from rmi4 may interfere with the one we currently use on i2c-hid.
f2c60e
Given that there is already a need for an external API from rmi4 to
f2c60e
forward the attention data, we can, in this particular case rely on a
f2c60e
separate workqueue to prevent cursor jumps.
f2c60e
f2c60e
Reported-by: Cameron Gutman <aicommander@gmail.com>
f2c60e
Reported-by: Thorsten Leemhuis <linux@leemhuis.info>
f2c60e
Reported-by: Jason Ekstrand <jason@jlekstrand.net>
f2c60e
Tested-by: Andrew Duggan <aduggan@synaptics.com>
f2c60e
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
f2c60e
Signed-off-by: Lyude <lyude@redhat.com>
f2c60e
---
f2c60e
 drivers/hid/hid-rmi.c           |  64 ---------------------
f2c60e
 drivers/input/rmi4/rmi_driver.c | 122 ++++++++++++++++++++++++----------------
f2c60e
 include/linux/rmi.h             |   1 +
f2c60e
 3 files changed, 75 insertions(+), 112 deletions(-)
f2c60e
f2c60e
diff --git a/drivers/hid/hid-rmi.c b/drivers/hid/hid-rmi.c
f2c60e
index 0f43c42..3f38872 100644
f2c60e
--- a/drivers/hid/hid-rmi.c
f2c60e
+++ b/drivers/hid/hid-rmi.c
f2c60e
@@ -316,19 +316,12 @@ static int rmi_input_event(struct hid_device *hdev, u8 *data, int size)
f2c60e
 {
f2c60e
 	struct rmi_data *hdata = hid_get_drvdata(hdev);
f2c60e
 	struct rmi_device *rmi_dev = hdata->xport.rmi_dev;
f2c60e
-	unsigned long flags;
f2c60e
f2c60e
 	if (!(test_bit(RMI_STARTED, &hdata->flags)))
f2c60e
 		return 0;
f2c60e
f2c60e
-	local_irq_save(flags);
f2c60e
-
f2c60e
 	rmi_set_attn_data(rmi_dev, data[1], &data[2], size - 2);
f2c60e
f2c60e
-	generic_handle_irq(hdata->rmi_irq);
f2c60e
-
f2c60e
-	local_irq_restore(flags);
f2c60e
-
f2c60e
 	return 1;
f2c60e
 }
f2c60e
f2c60e
@@ -568,56 +561,6 @@ static const struct rmi_transport_ops hid_rmi_ops = {
f2c60e
 	.reset		= rmi_hid_reset,
f2c60e
 };
f2c60e
f2c60e
-static void rmi_irq_teardown(void *data)
f2c60e
-{
f2c60e
-	struct rmi_data *hdata = data;
f2c60e
-	struct irq_domain *domain = hdata->domain;
f2c60e
-
f2c60e
-	if (!domain)
f2c60e
-		return;
f2c60e
-
f2c60e
-	irq_dispose_mapping(irq_find_mapping(domain, 0));
f2c60e
-
f2c60e
-	irq_domain_remove(domain);
f2c60e
-	hdata->domain = NULL;
f2c60e
-	hdata->rmi_irq = 0;
f2c60e
-}
f2c60e
-
f2c60e
-static int rmi_irq_map(struct irq_domain *h, unsigned int virq,
f2c60e
-		       irq_hw_number_t hw_irq_num)
f2c60e
-{
f2c60e
-	irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
f2c60e
-
f2c60e
-	return 0;
f2c60e
-}
f2c60e
-
f2c60e
-static const struct irq_domain_ops rmi_irq_ops = {
f2c60e
-	.map = rmi_irq_map,
f2c60e
-};
f2c60e
-
f2c60e
-static int rmi_setup_irq_domain(struct hid_device *hdev)
f2c60e
-{
f2c60e
-	struct rmi_data *hdata = hid_get_drvdata(hdev);
f2c60e
-	int ret;
f2c60e
-
f2c60e
-	hdata->domain = irq_domain_create_linear(hdev->dev.fwnode, 1,
f2c60e
-						 &rmi_irq_ops, hdata);
f2c60e
-	if (!hdata->domain)
f2c60e
-		return -ENOMEM;
f2c60e
-
f2c60e
-	ret = devm_add_action_or_reset(&hdev->dev, &rmi_irq_teardown, hdata);
f2c60e
-	if (ret)
f2c60e
-		return ret;
f2c60e
-
f2c60e
-	hdata->rmi_irq = irq_create_mapping(hdata->domain, 0);
f2c60e
-	if (hdata->rmi_irq <= 0) {
f2c60e
-		hid_err(hdev, "Can't allocate an IRQ\n");
f2c60e
-		return hdata->rmi_irq < 0 ? hdata->rmi_irq : -ENXIO;
f2c60e
-	}
f2c60e
-
f2c60e
-	return 0;
f2c60e
-}
f2c60e
-
f2c60e
 static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
f2c60e
 {
f2c60e
 	struct rmi_data *data = NULL;
f2c60e
@@ -689,18 +632,11 @@ static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
f2c60e
f2c60e
 	mutex_init(&data->page_mutex);
f2c60e
f2c60e
-	ret = rmi_setup_irq_domain(hdev);
f2c60e
-	if (ret) {
f2c60e
-		hid_err(hdev, "failed to allocate IRQ domain\n");
f2c60e
-		return ret;
f2c60e
-	}
f2c60e
-
f2c60e
 	if (data->device_flags & RMI_DEVICE_HAS_PHYS_BUTTONS)
f2c60e
 		rmi_hid_pdata.f30_data.disable = true;
f2c60e
f2c60e
 	data->xport.dev = hdev->dev.parent;
f2c60e
 	data->xport.pdata = rmi_hid_pdata;
f2c60e
-	data->xport.pdata.irq = data->rmi_irq;
f2c60e
 	data->xport.proto_name = "hid";
f2c60e
 	data->xport.ops = &hid_rmi_ops;
f2c60e
f2c60e
diff --git a/drivers/input/rmi4/rmi_driver.c b/drivers/input/rmi4/rmi_driver.c
f2c60e
index 141ea22..15b22db 100644
f2c60e
--- a/drivers/input/rmi4/rmi_driver.c
f2c60e
+++ b/drivers/input/rmi4/rmi_driver.c
f2c60e
@@ -209,34 +209,47 @@ void rmi_set_attn_data(struct rmi_device *rmi_dev, unsigned long irq_status,
f2c60e
 	attn_data.data = fifo_data;
f2c60e
f2c60e
 	kfifo_put(&drvdata->attn_fifo, attn_data);
f2c60e
+
f2c60e
+	schedule_work(&drvdata->attn_work);
f2c60e
 }
f2c60e
 EXPORT_SYMBOL_GPL(rmi_set_attn_data);
f2c60e
f2c60e
-static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
f2c60e
+static void attn_callback(struct work_struct *work)
f2c60e
 {
f2c60e
-	struct rmi_device *rmi_dev = dev_id;
f2c60e
-	struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
f2c60e
+	struct rmi_driver_data *drvdata = container_of(work,
f2c60e
+							struct rmi_driver_data,
f2c60e
+							attn_work);
f2c60e
 	struct rmi4_attn_data attn_data = {0};
f2c60e
 	int ret, count;
f2c60e
f2c60e
 	count = kfifo_get(&drvdata->attn_fifo, &attn_data);
f2c60e
-	if (count) {
f2c60e
-		*(drvdata->irq_status) = attn_data.irq_status;
f2c60e
-		drvdata->attn_data = attn_data;
f2c60e
-	}
f2c60e
+	if (!count)
f2c60e
+		return;
f2c60e
f2c60e
-	ret = rmi_process_interrupt_requests(rmi_dev);
f2c60e
+	*(drvdata->irq_status) = attn_data.irq_status;
f2c60e
+	drvdata->attn_data = attn_data;
f2c60e
+
f2c60e
+	ret = rmi_process_interrupt_requests(drvdata->rmi_dev);
f2c60e
 	if (ret)
f2c60e
-		rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
f2c60e
+		rmi_dbg(RMI_DEBUG_CORE, &drvdata->rmi_dev->dev,
f2c60e
 			"Failed to process interrupt request: %d\n", ret);
f2c60e
f2c60e
-	if (count) {
f2c60e
-		kfree(attn_data.data);
f2c60e
-		attn_data.data = NULL;
f2c60e
-	}
f2c60e
+	kfree(attn_data.data);
f2c60e
+	attn_data.data = NULL;
f2c60e
f2c60e
 	if (!kfifo_is_empty(&drvdata->attn_fifo))
f2c60e
-		return rmi_irq_fn(irq, dev_id);
f2c60e
+		schedule_work(&drvdata->attn_work);
f2c60e
+}
f2c60e
+
f2c60e
+static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
f2c60e
+{
f2c60e
+	struct rmi_device *rmi_dev = dev_id;
f2c60e
+	int ret;
f2c60e
+
f2c60e
+	ret = rmi_process_interrupt_requests(rmi_dev);
f2c60e
+	if (ret)
f2c60e
+		rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
f2c60e
+			"Failed to process interrupt request: %d\n", ret);
f2c60e
f2c60e
 	return IRQ_HANDLED;
f2c60e
 }
f2c60e
@@ -244,7 +257,6 @@ static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
f2c60e
 static int rmi_irq_init(struct rmi_device *rmi_dev)
f2c60e
 {
f2c60e
 	struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
f2c60e
-	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
f2c60e
 	int irq_flags = irq_get_trigger_type(pdata->irq);
f2c60e
 	int ret;
f2c60e
f2c60e
@@ -262,8 +274,6 @@ static int rmi_irq_init(struct rmi_device *rmi_dev)
f2c60e
 		return ret;
f2c60e
 	}
f2c60e
f2c60e
-	data->enabled = true;
f2c60e
-
f2c60e
 	return 0;
f2c60e
 }
f2c60e
f2c60e
@@ -912,23 +922,27 @@ void rmi_enable_irq(struct rmi_device *rmi_dev, bool clear_wake)
f2c60e
 	if (data->enabled)
f2c60e
 		goto out;
f2c60e
f2c60e
-	enable_irq(irq);
f2c60e
-	data->enabled = true;
f2c60e
-	if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
f2c60e
-		retval = disable_irq_wake(irq);
f2c60e
-		if (retval)
f2c60e
-			dev_warn(&rmi_dev->dev,
f2c60e
-				 "Failed to disable irq for wake: %d\n",
f2c60e
-				 retval);
f2c60e
-	}
f2c60e
+	if (irq) {
f2c60e
+		enable_irq(irq);
f2c60e
+		data->enabled = true;
f2c60e
+		if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
f2c60e
+			retval = disable_irq_wake(irq);
f2c60e
+			if (retval)
f2c60e
+				dev_warn(&rmi_dev->dev,
f2c60e
+					 "Failed to disable irq for wake: %d\n",
f2c60e
+					 retval);
f2c60e
+		}
f2c60e
f2c60e
-	/*
f2c60e
-	 * Call rmi_process_interrupt_requests() after enabling irq,
f2c60e
-	 * otherwise we may lose interrupt on edge-triggered systems.
f2c60e
-	 */
f2c60e
-	irq_flags = irq_get_trigger_type(pdata->irq);
f2c60e
-	if (irq_flags & IRQ_TYPE_EDGE_BOTH)
f2c60e
-		rmi_process_interrupt_requests(rmi_dev);
f2c60e
+		/*
f2c60e
+		 * Call rmi_process_interrupt_requests() after enabling irq,
f2c60e
+		 * otherwise we may lose interrupt on edge-triggered systems.
f2c60e
+		 */
f2c60e
+		irq_flags = irq_get_trigger_type(pdata->irq);
f2c60e
+		if (irq_flags & IRQ_TYPE_EDGE_BOTH)
f2c60e
+			rmi_process_interrupt_requests(rmi_dev);
f2c60e
+	} else {
f2c60e
+		data->enabled = true;
f2c60e
+	}
f2c60e
f2c60e
 out:
f2c60e
 	mutex_unlock(&data->enabled_mutex);
f2c60e
@@ -948,20 +962,22 @@ void rmi_disable_irq(struct rmi_device *rmi_dev, bool enable_wake)
f2c60e
 		goto out;
f2c60e
f2c60e
 	data->enabled = false;
f2c60e
-	disable_irq(irq);
f2c60e
-	if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
f2c60e
-		retval = enable_irq_wake(irq);
f2c60e
-		if (retval)
f2c60e
-			dev_warn(&rmi_dev->dev,
f2c60e
-				 "Failed to enable irq for wake: %d\n",
f2c60e
-				 retval);
f2c60e
-	}
f2c60e
-
f2c60e
-	/* make sure the fifo is clean */
f2c60e
-	while (!kfifo_is_empty(&data->attn_fifo)) {
f2c60e
-		count = kfifo_get(&data->attn_fifo, &attn_data);
f2c60e
-		if (count)
f2c60e
-			kfree(attn_data.data);
f2c60e
+	if (irq) {
f2c60e
+		disable_irq(irq);
f2c60e
+		if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
f2c60e
+			retval = enable_irq_wake(irq);
f2c60e
+			if (retval)
f2c60e
+				dev_warn(&rmi_dev->dev,
f2c60e
+					 "Failed to enable irq for wake: %d\n",
f2c60e
+					 retval);
f2c60e
+		}
f2c60e
+	} else {
f2c60e
+		/* make sure the fifo is clean */
f2c60e
+		while (!kfifo_is_empty(&data->attn_fifo)) {
f2c60e
+			count = kfifo_get(&data->attn_fifo, &attn_data);
f2c60e
+			if (count)
f2c60e
+				kfree(attn_data.data);
f2c60e
+		}
f2c60e
 	}
f2c60e
f2c60e
 out:
f2c60e
@@ -1000,9 +1016,12 @@ EXPORT_SYMBOL_GPL(rmi_driver_resume);
f2c60e
 static int rmi_driver_remove(struct device *dev)
f2c60e
 {
f2c60e
 	struct rmi_device *rmi_dev = to_rmi_device(dev);
f2c60e
+	struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
f2c60e
f2c60e
 	rmi_disable_irq(rmi_dev, false);
f2c60e
f2c60e
+	cancel_work_sync(&data->attn_work);
f2c60e
+
f2c60e
 	rmi_f34_remove_sysfs(rmi_dev);
f2c60e
 	rmi_free_function_list(rmi_dev);
f2c60e
f2c60e
@@ -1232,9 +1251,15 @@ static int rmi_driver_probe(struct device *dev)
f2c60e
 		}
f2c60e
 	}
f2c60e
f2c60e
-	retval = rmi_irq_init(rmi_dev);
f2c60e
-	if (retval < 0)
f2c60e
-		goto err_destroy_functions;
f2c60e
+	if (pdata->irq) {
f2c60e
+		retval = rmi_irq_init(rmi_dev);
f2c60e
+		if (retval < 0)
f2c60e
+			goto err_destroy_functions;
f2c60e
+	}
f2c60e
+
f2c60e
+	data->enabled = true;
f2c60e
+
f2c60e
+	INIT_WORK(&data->attn_work, attn_callback);
f2c60e
f2c60e
 	if (data->f01_container->dev.driver) {
f2c60e
 		/* Driver already bound, so enable ATTN now. */
f2c60e
diff --git a/include/linux/rmi.h b/include/linux/rmi.h
f2c60e
index 6412544..dc90178 100644
f2c60e
--- a/include/linux/rmi.h
f2c60e
+++ b/include/linux/rmi.h
f2c60e
@@ -364,6 +364,7 @@ struct rmi_driver_data {
f2c60e
f2c60e
 	struct rmi4_attn_data attn_data;
f2c60e
 	DECLARE_KFIFO(attn_fifo, struct rmi4_attn_data, 16);
f2c60e
+	struct work_struct attn_work;
f2c60e
 };
f2c60e
f2c60e
 int rmi_register_transport_device(struct rmi_transport_dev *xport);