diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h index ba33259f1f7c5..8dd4ff9ea52f7 100644 --- a/drivers/hid/ipts/context.h +++ b/drivers/hid/ipts/context.h @@ -15,6 +15,7 @@ #include #include #include +#include #include "mei.h" #include "resources.h" @@ -47,6 +48,14 @@ struct ipts_context { struct ipts_resources resources; struct ipts_thread receiver_loop; + + /* + * Serializes restarting the device. + */ + struct mutex control_lock; + + struct delayed_work restart_work; + unsigned int restart_attempts; }; #endif /* IPTS_CONTEXT_H */ diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c index 5360842d260ba..642e5f564e2f6 100644 --- a/drivers/hid/ipts/control.c +++ b/drivers/hid/ipts/control.c @@ -22,6 +22,9 @@ #include "spec-data.h" #include "spec-device.h" +#define IPTS_RESTART_DELAY_MS 2000 +#define IPTS_RESTART_MAX_ATTEMPTS 5 + static int ipts_control_get_device_info(struct ipts_context *ipts, struct ipts_device_info *info) { int ret = 0; @@ -345,6 +348,7 @@ int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_c int ipts_control_start(struct ipts_context *ipts) { int ret = 0; + size_t stale = 0; struct ipts_device_info info = { 0 }; if (!ipts) @@ -352,6 +356,15 @@ int ipts_control_start(struct ipts_context *ipts) dev_info(ipts->dev, "Starting IPTS\n"); + /* + * Responses to commands that were interrupted while sending (or that timed out) can + * still arrive later. Drop them, so that they are not mistaken for responses to the + * commands sent below. + */ + stale = ipts_mei_flush(&ipts->mei); + if (stale) + dev_dbg(ipts->dev, "Dropped %zu stale responses\n", stale); + ret = ipts_control_get_device_info(ipts, &info); if (ret) { dev_err(ipts->dev, "Failed to get device info: %d\n", ret); @@ -433,11 +446,14 @@ static int _ipts_control_stop(struct ipts_context *ipts) ipts_hid_disable(ipts); dev_info(ipts->dev, "Stopping IPTS\n"); + /* + * The receiver thread is gone even if this fails: the error only says that shutting + * it down did not go cleanly. Bailing out here would leave the resources and the HID + * device behind. + */ ret = ipts_receiver_stop(ipts); - if (ret) { + if (ret) dev_err(ipts->dev, "Failed to stop receiver: %d\n", ret); - return ret; - } ret = ipts_resources_free(&ipts->resources); if (ret) { @@ -451,18 +467,19 @@ static int _ipts_control_stop(struct ipts_context *ipts) int ipts_control_stop(struct ipts_context *ipts) { int ret = 0; + int hid_ret = 0; + /* + * Always free the HID device. This is called when removing the device, so nothing + * would free it later, and it would stay around when the device is probed again. + */ ret = _ipts_control_stop(ipts); - if (ret) - return ret; - ret = ipts_hid_free(ipts); - if (ret) { - dev_err(ipts->dev, "Failed to free HID device: %d\n", ret); - return ret; - } + hid_ret = ipts_hid_free(ipts); + if (hid_ret) + dev_err(ipts->dev, "Failed to free HID device: %d\n", hid_ret); - return 0; + return ret ?: hid_ret; } int ipts_control_restart(struct ipts_context *ipts) @@ -484,3 +501,67 @@ int ipts_control_restart(struct ipts_context *ipts) return 0; } + +static void ipts_control_restart_work(struct work_struct *work) +{ + int ret = 0; + struct ipts_context *ipts = + container_of(to_delayed_work(work), struct ipts_context, restart_work); + + mutex_lock(&ipts->control_lock); + + dev_info(ipts->dev, "Restarting IPTS after a failed mode switch\n"); + ret = _ipts_control_stop(ipts); + if (ret) + goto unlock; + + /* + * After a timeout, every response from the ME can arrive only when the next command is + * sent, which makes all further commands time out as well. Waiting doesn't help, but + * reconnecting does. + */ + ret = ipts_mei_reconnect(&ipts->mei); + if (ret) { + dev_err(ipts->dev, "Failed to reconnect to the ME: %d\n", ret); + goto unlock; + } + + msleep(1000); + + ret = ipts_control_start(ipts); + +unlock: + mutex_unlock(&ipts->control_lock); + + if (!ret) { + ipts->restart_attempts = 0; + return; + } + + if (++ipts->restart_attempts >= IPTS_RESTART_MAX_ATTEMPTS) { + dev_err(ipts->dev, "Giving up restarting IPTS: %d\n", ret); + ipts->restart_attempts = 0; + return; + } + + queue_delayed_work(system_freezable_wq, &ipts->restart_work, + msecs_to_jiffies(IPTS_RESTART_DELAY_MS)); +} + +void ipts_control_init_restart(struct ipts_context *ipts) +{ + mutex_init(&ipts->control_lock); + INIT_DELAYED_WORK(&ipts->restart_work, ipts_control_restart_work); + ipts->restart_attempts = 0; +} + +void ipts_control_schedule_restart(struct ipts_context *ipts) +{ + queue_delayed_work(system_freezable_wq, &ipts->restart_work, + msecs_to_jiffies(IPTS_RESTART_DELAY_MS)); +} + +void ipts_control_disable_restart(struct ipts_context *ipts) +{ + disable_delayed_work_sync(&ipts->restart_work); +} diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h index 26629c5144edb..41e97f365c805 100644 --- a/drivers/hid/ipts/control.h +++ b/drivers/hid/ipts/control.h @@ -123,4 +123,22 @@ int ipts_control_stop(struct ipts_context *ipts); */ int ipts_control_restart(struct ipts_context *ipts); +/** + * ipts_control_init_restart() - Initializes restarting the device in the background. + * @ipts: The IPTS driver context. + */ +void ipts_control_init_restart(struct ipts_context *ipts); + +/** + * ipts_control_schedule_restart() - Restarts the device in the background. + * @ipts: The IPTS driver context. + */ +void ipts_control_schedule_restart(struct ipts_context *ipts); + +/** + * ipts_control_disable_restart() - Cancels background restarts and prevents new ones. + * @ipts: The IPTS driver context. + */ +void ipts_control_disable_restart(struct ipts_context *ipts); + #endif /* IPTS_CONTROL_H */ diff --git a/drivers/hid/ipts/eds1.c b/drivers/hid/ipts/eds1.c index 7b9f54388a9f6..29a1a5fcfc37e 100644 --- a/drivers/hid/ipts/eds1.c +++ b/drivers/hid/ipts/eds1.c @@ -50,19 +50,35 @@ int ipts_eds1_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode) { int ret = 0; + enum ipts_mode old_mode; if (!ipts) return -EFAULT; + mutex_lock(&ipts->control_lock); + if (ipts->mode == mode) - return 0; + goto out; + old_mode = ipts->mode; ipts->mode = mode; ret = ipts_control_restart(ipts); - if (ret) + if (ret) { dev_err(ipts->dev, "Failed to switch modes: %d\n", ret); + /* + * The device is stopped, and the HID device is inactive, so userspace cannot + * retry. Restart the device in the previous mode. Do it in the background, + * since the current task may not be able to talk to the device anymore + * (e.g. because it has a signal pending). + */ + ipts->mode = old_mode; + ipts_control_schedule_restart(ipts); + } + +out: + mutex_unlock(&ipts->control_lock); return ret; } diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c index fb5b5c13ee3ea..a058f5b3835d5 100644 --- a/drivers/hid/ipts/main.c +++ b/drivers/hid/ipts/main.c @@ -76,6 +76,7 @@ static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id mutex_init(&ipts->feature_lock); init_completion(&ipts->feature_event); + ipts_control_init_restart(ipts); mei_cldev_set_drvdata(cldev, ipts); @@ -99,6 +100,7 @@ static void ipts_remove(struct mei_cl_device *cldev) } ipts = mei_cldev_get_drvdata(cldev); + ipts_control_disable_restart(ipts); ret = ipts_control_stop(ipts); if (ret) diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c index 1e0395ceae4a4..706da2b97340b 100644 --- a/drivers/hid/ipts/mei.c +++ b/drivers/hid/ipts/mei.c @@ -158,16 +158,64 @@ int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length) if (!data) return -EFAULT; - do { - ret = mei_cldev_send(mei->cldev, (u8 *)data, length); - } while (ret == -EINTR); - + /* + * Don't retry on -EINTR: mei_cldev_send() queues the message before it waits for the + * transfer to complete, and returns -EINTR if a signal is pending. The signal stays + * pending until we return to userspace, so retrying would loop forever (and queue + * the message again every time). + */ + ret = mei_cldev_send(mei->cldev, (u8 *)data, length); if (ret < 0) return ret; return 0; } +size_t ipts_mei_flush(struct ipts_mei *mei) +{ + size_t count = 0; + struct ipts_mei_message *entry = NULL; + struct ipts_mei_message *tmp = NULL; + + if (!mei) + return 0; + + down_write(&mei->message_lock); + + list_for_each_entry_safe(entry, tmp, &mei->messages, list) { + list_del(&entry->list); + devm_kfree(&mei->cldev->dev, entry); + count++; + } + + up_write(&mei->message_lock); + + return count; +} + +int ipts_mei_reconnect(struct ipts_mei *mei) +{ + int ret = 0; + + if (!mei) + return -EFAULT; + + if (!mei->cldev) + return -EFAULT; + + /* + * This also unregisters the receive callback, and fails only if the client was already + * gone. Try to connect anyway. + */ + mei_cldev_disable(mei->cldev); + + ret = mei_cldev_enable(mei->cldev); + if (ret) + return ret; + + return mei_cldev_register_rx_cb(mei->cldev, ipts_mei_incoming); +} + int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev) { if (!mei) diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h index 973bade6b0fdd..612e59e9f3cd3 100644 --- a/drivers/hid/ipts/mei.h +++ b/drivers/hid/ipts/mei.h @@ -54,6 +54,14 @@ int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts */ int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length); +/** + * ipts_mei_reconnect() - Disconnects from the ME client and connects again. + * @mei: The MEI device context. + * + * Return: 0 on success, <0 on error. + */ +int ipts_mei_reconnect(struct ipts_mei *mei); + /** * ipts_mei_init() - Initialize the MEI device context. * @mei: The MEI device context to initialize. @@ -63,4 +71,12 @@ int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length); */ int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev); +/** + * ipts_mei_flush() - Drops all received messages that have not been consumed. + * @mei: The IPTS MEI device context. + * + * Returns: The number of dropped messages. + */ +size_t ipts_mei_flush(struct ipts_mei *mei); + #endif /* IPTS_MEI_H */ diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c index 355e92bea26f8..be6e96879e01f 100644 --- a/drivers/hid/ipts/thread.c +++ b/drivers/hid/ipts/thread.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "thread.h" @@ -40,6 +41,8 @@ static int ipts_thread_runner(void *data) int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread), void *data, const char *name) { + struct task_struct *task = NULL; + if (!thread) return -EFAULT; @@ -52,8 +55,21 @@ int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_th thread->should_stop = false; thread->threadfn = threadfn; - thread->thread = kthread_run(ipts_thread_runner, thread, name); - return PTR_ERR_OR_ZERO(thread->thread); + task = kthread_create(ipts_thread_runner, thread, name); + if (IS_ERR(task)) { + thread->thread = NULL; + return PTR_ERR(task); + } + + /* + * The runner can return before ipts_thread_stop() calls kthread_stop(), and the task + * goes away once it has exited. Keep a reference until the thread has been stopped. + */ + get_task_struct(task); + thread->thread = task; + wake_up_process(task); + + return 0; } int ipts_thread_stop(struct ipts_thread *thread) @@ -74,7 +90,7 @@ int ipts_thread_stop(struct ipts_thread *thread) wmb(); wait_for_completion(&thread->done); - ret = kthread_stop(thread->thread); + ret = kthread_stop_put(thread->thread); thread->thread = NULL; thread->data = NULL;