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