Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/slab.h> /* for kmalloc */
0003 #include <linux/consolemap.h>
0004 #include <linux/interrupt.h>
0005 #include <linux/sched.h>
0006 #include <linux/device.h> /* for dev_warn */
0007 #include <linux/selection.h>
0008 #include <linux/workqueue.h>
0009 #include <linux/tty.h>
0010 #include <linux/tty_flip.h>
0011 #include <linux/atomic.h>
0012 #include <linux/console.h>
0013 
0014 #include "speakup.h"
0015 
0016 unsigned short spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */
0017 struct vc_data *spk_sel_cons;
0018 
0019 struct speakup_selection_work {
0020     struct work_struct work;
0021     struct tiocl_selection sel;
0022     struct tty_struct *tty;
0023 };
0024 
0025 static void __speakup_set_selection(struct work_struct *work)
0026 {
0027     struct speakup_selection_work *ssw =
0028         container_of(work, struct speakup_selection_work, work);
0029 
0030     struct tty_struct *tty;
0031     struct tiocl_selection sel;
0032 
0033     sel = ssw->sel;
0034 
0035     /* this ensures we copy sel before releasing the lock below */
0036     rmb();
0037 
0038     /* release the lock by setting tty of the struct to NULL */
0039     tty = xchg(&ssw->tty, NULL);
0040 
0041     if (spk_sel_cons != vc_cons[fg_console].d) {
0042         spk_sel_cons = vc_cons[fg_console].d;
0043         pr_warn("Selection: mark console not the same as cut\n");
0044         goto unref;
0045     }
0046 
0047     console_lock();
0048     clear_selection();
0049     console_unlock();
0050 
0051     set_selection_kernel(&sel, tty);
0052 
0053 unref:
0054     tty_kref_put(tty);
0055 }
0056 
0057 static struct speakup_selection_work speakup_sel_work = {
0058     .work = __WORK_INITIALIZER(speakup_sel_work.work,
0059                    __speakup_set_selection)
0060 };
0061 
0062 int speakup_set_selection(struct tty_struct *tty)
0063 {
0064     /* we get kref here first in order to avoid a subtle race when
0065      * cancelling selection work. getting kref first establishes the
0066      * invariant that if speakup_sel_work.tty is not NULL when
0067      * speakup_cancel_selection() is called, it must be the case that a put
0068      * kref is pending.
0069      */
0070     tty_kref_get(tty);
0071     if (cmpxchg(&speakup_sel_work.tty, NULL, tty)) {
0072         tty_kref_put(tty);
0073         return -EBUSY;
0074     }
0075     /* now we have the 'lock' by setting tty member of
0076      * speakup_selection_work. wmb() ensures that writes to
0077      * speakup_sel_work don't happen before cmpxchg() above.
0078      */
0079     wmb();
0080 
0081     speakup_sel_work.sel.xs = spk_xs + 1;
0082     speakup_sel_work.sel.ys = spk_ys + 1;
0083     speakup_sel_work.sel.xe = spk_xe + 1;
0084     speakup_sel_work.sel.ye = spk_ye + 1;
0085     speakup_sel_work.sel.sel_mode = TIOCL_SELCHAR;
0086 
0087     schedule_work_on(WORK_CPU_UNBOUND, &speakup_sel_work.work);
0088 
0089     return 0;
0090 }
0091 
0092 void speakup_cancel_selection(void)
0093 {
0094     struct tty_struct *tty;
0095 
0096     cancel_work_sync(&speakup_sel_work.work);
0097     /* setting to null so that if work fails to run and we cancel it,
0098      * we can run it again without getting EBUSY forever from there on.
0099      * we need to use xchg here to avoid race with speakup_set_selection()
0100      */
0101     tty = xchg(&speakup_sel_work.tty, NULL);
0102     if (tty)
0103         tty_kref_put(tty);
0104 }
0105 
0106 static void __speakup_paste_selection(struct work_struct *work)
0107 {
0108     struct speakup_selection_work *ssw =
0109         container_of(work, struct speakup_selection_work, work);
0110     struct tty_struct *tty = xchg(&ssw->tty, NULL);
0111 
0112     paste_selection(tty);
0113     tty_kref_put(tty);
0114 }
0115 
0116 static struct speakup_selection_work speakup_paste_work = {
0117     .work = __WORK_INITIALIZER(speakup_paste_work.work,
0118                    __speakup_paste_selection)
0119 };
0120 
0121 int speakup_paste_selection(struct tty_struct *tty)
0122 {
0123     tty_kref_get(tty);
0124     if (cmpxchg(&speakup_paste_work.tty, NULL, tty)) {
0125         tty_kref_put(tty);
0126         return -EBUSY;
0127     }
0128 
0129     schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
0130     return 0;
0131 }
0132 
0133 void speakup_cancel_paste(void)
0134 {
0135     struct tty_struct *tty;
0136 
0137     cancel_work_sync(&speakup_paste_work.work);
0138     tty = xchg(&speakup_paste_work.tty, NULL);
0139     if (tty)
0140         tty_kref_put(tty);
0141 }