Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / staging / speakup / selection.c
index f0fb00392d6b125b1aeaa2f70575b4b5c3f1ae03..f67941e78e4a45cc755897a12ba9231b53c1547f 100644 (file)
@@ -4,6 +4,8 @@
 #include <linux/sched.h>
 #include <linux/device.h> /* for dev_warn */
 #include <linux/selection.h>
+#include <linux/workqueue.h>
+#include <asm/cmpxchg.h>
 
 #include "speakup.h"
 
@@ -121,20 +123,24 @@ int speakup_set_selection(struct tty_struct *tty)
        return 0;
 }
 
-/* TODO: move to some helper thread, probably.  That'd fix having to check for
- * in_atomic().  */
-int speakup_paste_selection(struct tty_struct *tty)
+struct speakup_paste_work {
+       struct work_struct work;
+       struct tty_struct *tty;
+};
+
+static void __speakup_paste_selection(struct work_struct *work)
 {
+       struct speakup_paste_work *spw =
+               container_of(work, struct speakup_paste_work, work);
+       struct tty_struct *tty = xchg(&spw->tty, NULL);
        struct vc_data *vc = (struct vc_data *) tty->driver_data;
        int pasted = 0, count;
        DECLARE_WAITQUEUE(wait, current);
+
        add_wait_queue(&vc->paste_wait, &wait);
        while (sel_buffer && sel_buffer_lth > pasted) {
                set_current_state(TASK_INTERRUPTIBLE);
                if (test_bit(TTY_THROTTLED, &tty->flags)) {
-                       if (in_atomic())
-                               /* if we are in an interrupt handler, abort */
-                               break;
                        schedule();
                        continue;
                }
@@ -146,6 +152,26 @@ int speakup_paste_selection(struct tty_struct *tty)
        }
        remove_wait_queue(&vc->paste_wait, &wait);
        current->state = TASK_RUNNING;
+       tty_kref_put(tty);
+}
+
+static struct speakup_paste_work speakup_paste_work = {
+       .work = __WORK_INITIALIZER(speakup_paste_work.work,
+                                  __speakup_paste_selection)
+};
+
+int speakup_paste_selection(struct tty_struct *tty)
+{
+       if (cmpxchg(&speakup_paste_work.tty, NULL, tty) != NULL)
+               return -EBUSY;
+
+       tty_kref_get(tty);
+       schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
        return 0;
 }
 
+void speakup_cancel_paste(void)
+{
+       cancel_work_sync(&speakup_paste_work.work);
+       tty_kref_put(speakup_paste_work.tty);
+}