1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/slab.h> /* for kmalloc */ 3 #include <linux/consolemap.h> 4 #include <linux/interrupt.h> 5 #include <linux/sched.h> 6 #include <linux/device.h> /* for dev_warn */ 7 #include <linux/selection.h> 8 #include <linux/workqueue.h> 9 #include <linux/tty.h> 10 #include <linux/tty_flip.h> 11 #include <linux/atomic.h> 12 #include <linux/console.h> 13 14 #include "speakup.h" 15 16 u16 spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */ 17 struct vc_data *spk_sel_cons; 18 19 struct speakup_selection_work { 20 struct work_struct work; 21 struct tiocl_selection sel; 22 struct tty_struct *tty; 23 }; 24 25 static void __speakup_set_selection(struct work_struct *work) 26 { 27 struct speakup_selection_work *ssw = 28 container_of(work, struct speakup_selection_work, work); 29 30 struct tty_struct *tty; 31 struct tiocl_selection sel; 32 33 sel = ssw->sel; 34 35 /* this ensures we copy sel before releasing the lock below */ 36 rmb(); 37 38 /* release the lock by setting tty of the struct to NULL */ 39 tty = xchg(&ssw->tty, NULL); 40 41 if (spk_sel_cons != vc_cons[fg_console].d) { 42 spk_sel_cons = vc_cons[fg_console].d; 43 pr_warn("Selection: mark console not the same as cut\n"); 44 goto unref; 45 } 46 47 console_lock(); 48 clear_selection(); 49 console_unlock(); 50 51 set_selection_kernel(&sel, tty); 52 53 unref: 54 tty_kref_put(tty); 55 } 56 57 static struct speakup_selection_work speakup_sel_work = { 58 .work = __WORK_INITIALIZER(speakup_sel_work.work, 59 __speakup_set_selection) 60 }; 61 62 int speakup_set_selection(struct tty_struct *tty) 63 { 64 if (!tty) 65 return -ENODEV; 66 67 /* we get kref here first in order to avoid a subtle race when 68 * cancelling selection work. getting kref first establishes the 69 * invariant that if speakup_sel_work.tty is not NULL when 70 * speakup_cancel_selection() is called, it must be the case that a put 71 * kref is pending. 72 */ 73 tty_kref_get(tty); 74 if (cmpxchg(&speakup_sel_work.tty, NULL, tty)) { 75 tty_kref_put(tty); 76 return -EBUSY; 77 } 78 /* now we have the 'lock' by setting tty member of 79 * speakup_selection_work. wmb() ensures that writes to 80 * speakup_sel_work don't happen before cmpxchg() above. 81 */ 82 wmb(); 83 84 speakup_sel_work.sel.xs = spk_xs + 1; 85 speakup_sel_work.sel.ys = spk_ys + 1; 86 speakup_sel_work.sel.xe = spk_xe + 1; 87 speakup_sel_work.sel.ye = spk_ye + 1; 88 speakup_sel_work.sel.sel_mode = TIOCL_SELCHAR; 89 90 schedule_work_on(WORK_CPU_UNBOUND, &speakup_sel_work.work); 91 92 return 0; 93 } 94 95 void speakup_cancel_selection(void) 96 { 97 struct tty_struct *tty; 98 99 cancel_work_sync(&speakup_sel_work.work); 100 /* setting to null so that if work fails to run and we cancel it, 101 * we can run it again without getting EBUSY forever from there on. 102 * we need to use xchg here to avoid race with speakup_set_selection() 103 */ 104 tty = xchg(&speakup_sel_work.tty, NULL); 105 if (tty) 106 tty_kref_put(tty); 107 } 108 109 static void __speakup_paste_selection(struct work_struct *work) 110 { 111 struct speakup_selection_work *ssw = 112 container_of(work, struct speakup_selection_work, work); 113 struct tty_struct *tty = xchg(&ssw->tty, NULL); 114 115 paste_selection(tty); 116 tty_kref_put(tty); 117 } 118 119 static struct speakup_selection_work speakup_paste_work = { 120 .work = __WORK_INITIALIZER(speakup_paste_work.work, 121 __speakup_paste_selection) 122 }; 123 124 int speakup_paste_selection(struct tty_struct *tty) 125 { 126 if (!tty) 127 return -ENODEV; 128 129 tty_kref_get(tty); 130 if (cmpxchg(&speakup_paste_work.tty, NULL, tty)) { 131 tty_kref_put(tty); 132 return -EBUSY; 133 } 134 135 schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work); 136 return 0; 137 } 138 139 void speakup_cancel_paste(void) 140 { 141 struct tty_struct *tty; 142 143 cancel_work_sync(&speakup_paste_work.work); 144 tty = xchg(&speakup_paste_work.tty, NULL); 145 if (tty) 146 tty_kref_put(tty); 147 } 148