162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <linux/slab.h> /* for kmalloc */
362306a36Sopenharmony_ci#include <linux/consolemap.h>
462306a36Sopenharmony_ci#include <linux/interrupt.h>
562306a36Sopenharmony_ci#include <linux/sched.h>
662306a36Sopenharmony_ci#include <linux/device.h> /* for dev_warn */
762306a36Sopenharmony_ci#include <linux/selection.h>
862306a36Sopenharmony_ci#include <linux/workqueue.h>
962306a36Sopenharmony_ci#include <linux/tty.h>
1062306a36Sopenharmony_ci#include <linux/tty_flip.h>
1162306a36Sopenharmony_ci#include <linux/atomic.h>
1262306a36Sopenharmony_ci#include <linux/console.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include "speakup.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciunsigned short spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */
1762306a36Sopenharmony_cistruct vc_data *spk_sel_cons;
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct speakup_selection_work {
2062306a36Sopenharmony_ci	struct work_struct work;
2162306a36Sopenharmony_ci	struct tiocl_selection sel;
2262306a36Sopenharmony_ci	struct tty_struct *tty;
2362306a36Sopenharmony_ci};
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_cistatic void __speakup_set_selection(struct work_struct *work)
2662306a36Sopenharmony_ci{
2762306a36Sopenharmony_ci	struct speakup_selection_work *ssw =
2862306a36Sopenharmony_ci		container_of(work, struct speakup_selection_work, work);
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	struct tty_struct *tty;
3162306a36Sopenharmony_ci	struct tiocl_selection sel;
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	sel = ssw->sel;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	/* this ensures we copy sel before releasing the lock below */
3662306a36Sopenharmony_ci	rmb();
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	/* release the lock by setting tty of the struct to NULL */
3962306a36Sopenharmony_ci	tty = xchg(&ssw->tty, NULL);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	if (spk_sel_cons != vc_cons[fg_console].d) {
4262306a36Sopenharmony_ci		spk_sel_cons = vc_cons[fg_console].d;
4362306a36Sopenharmony_ci		pr_warn("Selection: mark console not the same as cut\n");
4462306a36Sopenharmony_ci		goto unref;
4562306a36Sopenharmony_ci	}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	console_lock();
4862306a36Sopenharmony_ci	clear_selection();
4962306a36Sopenharmony_ci	console_unlock();
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	set_selection_kernel(&sel, tty);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ciunref:
5462306a36Sopenharmony_ci	tty_kref_put(tty);
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic struct speakup_selection_work speakup_sel_work = {
5862306a36Sopenharmony_ci	.work = __WORK_INITIALIZER(speakup_sel_work.work,
5962306a36Sopenharmony_ci				   __speakup_set_selection)
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciint speakup_set_selection(struct tty_struct *tty)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	/* we get kref here first in order to avoid a subtle race when
6562306a36Sopenharmony_ci	 * cancelling selection work. getting kref first establishes the
6662306a36Sopenharmony_ci	 * invariant that if speakup_sel_work.tty is not NULL when
6762306a36Sopenharmony_ci	 * speakup_cancel_selection() is called, it must be the case that a put
6862306a36Sopenharmony_ci	 * kref is pending.
6962306a36Sopenharmony_ci	 */
7062306a36Sopenharmony_ci	tty_kref_get(tty);
7162306a36Sopenharmony_ci	if (cmpxchg(&speakup_sel_work.tty, NULL, tty)) {
7262306a36Sopenharmony_ci		tty_kref_put(tty);
7362306a36Sopenharmony_ci		return -EBUSY;
7462306a36Sopenharmony_ci	}
7562306a36Sopenharmony_ci	/* now we have the 'lock' by setting tty member of
7662306a36Sopenharmony_ci	 * speakup_selection_work. wmb() ensures that writes to
7762306a36Sopenharmony_ci	 * speakup_sel_work don't happen before cmpxchg() above.
7862306a36Sopenharmony_ci	 */
7962306a36Sopenharmony_ci	wmb();
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	speakup_sel_work.sel.xs = spk_xs + 1;
8262306a36Sopenharmony_ci	speakup_sel_work.sel.ys = spk_ys + 1;
8362306a36Sopenharmony_ci	speakup_sel_work.sel.xe = spk_xe + 1;
8462306a36Sopenharmony_ci	speakup_sel_work.sel.ye = spk_ye + 1;
8562306a36Sopenharmony_ci	speakup_sel_work.sel.sel_mode = TIOCL_SELCHAR;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	schedule_work_on(WORK_CPU_UNBOUND, &speakup_sel_work.work);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	return 0;
9062306a36Sopenharmony_ci}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_civoid speakup_cancel_selection(void)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	struct tty_struct *tty;
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	cancel_work_sync(&speakup_sel_work.work);
9762306a36Sopenharmony_ci	/* setting to null so that if work fails to run and we cancel it,
9862306a36Sopenharmony_ci	 * we can run it again without getting EBUSY forever from there on.
9962306a36Sopenharmony_ci	 * we need to use xchg here to avoid race with speakup_set_selection()
10062306a36Sopenharmony_ci	 */
10162306a36Sopenharmony_ci	tty = xchg(&speakup_sel_work.tty, NULL);
10262306a36Sopenharmony_ci	if (tty)
10362306a36Sopenharmony_ci		tty_kref_put(tty);
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic void __speakup_paste_selection(struct work_struct *work)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct speakup_selection_work *ssw =
10962306a36Sopenharmony_ci		container_of(work, struct speakup_selection_work, work);
11062306a36Sopenharmony_ci	struct tty_struct *tty = xchg(&ssw->tty, NULL);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	paste_selection(tty);
11362306a36Sopenharmony_ci	tty_kref_put(tty);
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic struct speakup_selection_work speakup_paste_work = {
11762306a36Sopenharmony_ci	.work = __WORK_INITIALIZER(speakup_paste_work.work,
11862306a36Sopenharmony_ci				   __speakup_paste_selection)
11962306a36Sopenharmony_ci};
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciint speakup_paste_selection(struct tty_struct *tty)
12262306a36Sopenharmony_ci{
12362306a36Sopenharmony_ci	tty_kref_get(tty);
12462306a36Sopenharmony_ci	if (cmpxchg(&speakup_paste_work.tty, NULL, tty)) {
12562306a36Sopenharmony_ci		tty_kref_put(tty);
12662306a36Sopenharmony_ci		return -EBUSY;
12762306a36Sopenharmony_ci	}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
13062306a36Sopenharmony_ci	return 0;
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_civoid speakup_cancel_paste(void)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	struct tty_struct *tty;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	cancel_work_sync(&speakup_paste_work.work);
13862306a36Sopenharmony_ci	tty = xchg(&speakup_paste_work.tty, NULL);
13962306a36Sopenharmony_ci	if (tty)
14062306a36Sopenharmony_ci		tty_kref_put(tty);
14162306a36Sopenharmony_ci}
142