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