xref: /OK3568_Linux_fs/kernel/drivers/tty/vt/selection.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * This module exports the functions:
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *     'int set_selection_user(struct tiocl_selection __user *,
6*4882a593Smuzhiyun  *			       struct tty_struct *)'
7*4882a593Smuzhiyun  *     'int set_selection_kernel(struct tiocl_selection *, struct tty_struct *)'
8*4882a593Smuzhiyun  *     'void clear_selection(void)'
9*4882a593Smuzhiyun  *     'int paste_selection(struct tty_struct *)'
10*4882a593Smuzhiyun  *     'int sel_loadlut(char __user *)'
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Now that /dev/vcs exists, most of this can disappear again.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/tty.h>
17*4882a593Smuzhiyun #include <linux/sched.h>
18*4882a593Smuzhiyun #include <linux/mm.h>
19*4882a593Smuzhiyun #include <linux/mutex.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/types.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/uaccess.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/kbd_kern.h>
26*4882a593Smuzhiyun #include <linux/vt_kern.h>
27*4882a593Smuzhiyun #include <linux/consolemap.h>
28*4882a593Smuzhiyun #include <linux/selection.h>
29*4882a593Smuzhiyun #include <linux/tiocl.h>
30*4882a593Smuzhiyun #include <linux/console.h>
31*4882a593Smuzhiyun #include <linux/tty_flip.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <linux/sched/signal.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
36*4882a593Smuzhiyun #define isspace(c)	((c) == ' ')
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* FIXME: all this needs locking */
39*4882a593Smuzhiyun static struct vc_selection {
40*4882a593Smuzhiyun 	struct mutex lock;
41*4882a593Smuzhiyun 	struct vc_data *cons;			/* must not be deallocated */
42*4882a593Smuzhiyun 	char *buffer;
43*4882a593Smuzhiyun 	unsigned int buf_len;
44*4882a593Smuzhiyun 	volatile int start;			/* cleared by clear_selection */
45*4882a593Smuzhiyun 	int end;
46*4882a593Smuzhiyun } vc_sel = {
47*4882a593Smuzhiyun 	.lock = __MUTEX_INITIALIZER(vc_sel.lock),
48*4882a593Smuzhiyun 	.start = -1,
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* clear_selection, highlight and highlight_pointer can be called
52*4882a593Smuzhiyun    from interrupt (via scrollback/front) */
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /* set reverse video on characters s-e of console with selection. */
highlight(const int s,const int e)55*4882a593Smuzhiyun static inline void highlight(const int s, const int e)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	invert_screen(vc_sel.cons, s, e-s+2, true);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun /* use complementary color to show the pointer */
highlight_pointer(const int where)61*4882a593Smuzhiyun static inline void highlight_pointer(const int where)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	complement_pos(vc_sel.cons, where);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun static u32
sel_pos(int n,bool unicode)67*4882a593Smuzhiyun sel_pos(int n, bool unicode)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	if (unicode)
70*4882a593Smuzhiyun 		return screen_glyph_unicode(vc_sel.cons, n / 2);
71*4882a593Smuzhiyun 	return inverse_translate(vc_sel.cons, screen_glyph(vc_sel.cons, n), 0);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun  *	clear_selection		-	remove current selection
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  *	Remove the current selection highlight, if any from the console
78*4882a593Smuzhiyun  *	holding the selection. The caller must hold the console lock.
79*4882a593Smuzhiyun  */
clear_selection(void)80*4882a593Smuzhiyun void clear_selection(void)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	highlight_pointer(-1); /* hide the pointer */
83*4882a593Smuzhiyun 	if (vc_sel.start != -1) {
84*4882a593Smuzhiyun 		highlight(vc_sel.start, vc_sel.end);
85*4882a593Smuzhiyun 		vc_sel.start = -1;
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(clear_selection);
89*4882a593Smuzhiyun 
vc_is_sel(struct vc_data * vc)90*4882a593Smuzhiyun bool vc_is_sel(struct vc_data *vc)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	return vc == vc_sel.cons;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun  * User settable table: what characters are to be considered alphabetic?
97*4882a593Smuzhiyun  * 128 bits. Locked by the console lock.
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun static u32 inwordLut[]={
100*4882a593Smuzhiyun   0x00000000, /* control chars     */
101*4882a593Smuzhiyun   0x03FFE000, /* digits and "-./"  */
102*4882a593Smuzhiyun   0x87FFFFFE, /* uppercase and '_' */
103*4882a593Smuzhiyun   0x07FFFFFE, /* lowercase         */
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun 
inword(const u32 c)106*4882a593Smuzhiyun static inline int inword(const u32 c)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /**
112*4882a593Smuzhiyun  *	set loadlut		-	load the LUT table
113*4882a593Smuzhiyun  *	@p: user table
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  *	Load the LUT table from user space. The caller must hold the console
116*4882a593Smuzhiyun  *	lock. Make a temporary copy so a partial update doesn't make a mess.
117*4882a593Smuzhiyun  */
sel_loadlut(char __user * p)118*4882a593Smuzhiyun int sel_loadlut(char __user *p)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	u32 tmplut[ARRAY_SIZE(inwordLut)];
121*4882a593Smuzhiyun 	if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
122*4882a593Smuzhiyun 		return -EFAULT;
123*4882a593Smuzhiyun 	memcpy(inwordLut, tmplut, sizeof(inwordLut));
124*4882a593Smuzhiyun 	return 0;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /* does screen address p correspond to character at LH/RH edge of screen? */
atedge(const int p,int size_row)128*4882a593Smuzhiyun static inline int atedge(const int p, int size_row)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	return (!(p % size_row)	|| !((p + 2) % size_row));
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /* stores the char in UTF8 and returns the number of bytes used (1-4) */
store_utf8(u32 c,char * p)134*4882a593Smuzhiyun static int store_utf8(u32 c, char *p)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	if (c < 0x80) {
137*4882a593Smuzhiyun 		/*  0******* */
138*4882a593Smuzhiyun 		p[0] = c;
139*4882a593Smuzhiyun 		return 1;
140*4882a593Smuzhiyun 	} else if (c < 0x800) {
141*4882a593Smuzhiyun 		/* 110***** 10****** */
142*4882a593Smuzhiyun 		p[0] = 0xc0 | (c >> 6);
143*4882a593Smuzhiyun 		p[1] = 0x80 | (c & 0x3f);
144*4882a593Smuzhiyun 		return 2;
145*4882a593Smuzhiyun 	} else if (c < 0x10000) {
146*4882a593Smuzhiyun 		/* 1110**** 10****** 10****** */
147*4882a593Smuzhiyun 		p[0] = 0xe0 | (c >> 12);
148*4882a593Smuzhiyun 		p[1] = 0x80 | ((c >> 6) & 0x3f);
149*4882a593Smuzhiyun 		p[2] = 0x80 | (c & 0x3f);
150*4882a593Smuzhiyun 		return 3;
151*4882a593Smuzhiyun 	} else if (c < 0x110000) {
152*4882a593Smuzhiyun 		/* 11110*** 10****** 10****** 10****** */
153*4882a593Smuzhiyun 		p[0] = 0xf0 | (c >> 18);
154*4882a593Smuzhiyun 		p[1] = 0x80 | ((c >> 12) & 0x3f);
155*4882a593Smuzhiyun 		p[2] = 0x80 | ((c >> 6) & 0x3f);
156*4882a593Smuzhiyun 		p[3] = 0x80 | (c & 0x3f);
157*4882a593Smuzhiyun 		return 4;
158*4882a593Smuzhiyun 	} else {
159*4882a593Smuzhiyun 		/* outside Unicode, replace with U+FFFD */
160*4882a593Smuzhiyun 		p[0] = 0xef;
161*4882a593Smuzhiyun 		p[1] = 0xbf;
162*4882a593Smuzhiyun 		p[2] = 0xbd;
163*4882a593Smuzhiyun 		return 3;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun  *	set_selection_user	-	set the current selection.
169*4882a593Smuzhiyun  *	@sel: user selection info
170*4882a593Smuzhiyun  *	@tty: the console tty
171*4882a593Smuzhiyun  *
172*4882a593Smuzhiyun  *	Invoked by the ioctl handle for the vt layer.
173*4882a593Smuzhiyun  *
174*4882a593Smuzhiyun  *	The entire selection process is managed under the console_lock. It's
175*4882a593Smuzhiyun  *	 a lot under the lock but its hardly a performance path
176*4882a593Smuzhiyun  */
set_selection_user(const struct tiocl_selection __user * sel,struct tty_struct * tty)177*4882a593Smuzhiyun int set_selection_user(const struct tiocl_selection __user *sel,
178*4882a593Smuzhiyun 		       struct tty_struct *tty)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	struct tiocl_selection v;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (copy_from_user(&v, sel, sizeof(*sel)))
183*4882a593Smuzhiyun 		return -EFAULT;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return set_selection_kernel(&v, tty);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
vc_selection_store_chars(struct vc_data * vc,bool unicode)188*4882a593Smuzhiyun static int vc_selection_store_chars(struct vc_data *vc, bool unicode)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	char *bp, *obp;
191*4882a593Smuzhiyun 	unsigned int i;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/* Allocate a new buffer before freeing the old one ... */
194*4882a593Smuzhiyun 	/* chars can take up to 4 bytes with unicode */
195*4882a593Smuzhiyun 	bp = kmalloc_array((vc_sel.end - vc_sel.start) / 2 + 1, unicode ? 4 : 1,
196*4882a593Smuzhiyun 			   GFP_KERNEL | __GFP_NOWARN);
197*4882a593Smuzhiyun 	if (!bp) {
198*4882a593Smuzhiyun 		printk(KERN_WARNING "selection: kmalloc() failed\n");
199*4882a593Smuzhiyun 		clear_selection();
200*4882a593Smuzhiyun 		return -ENOMEM;
201*4882a593Smuzhiyun 	}
202*4882a593Smuzhiyun 	kfree(vc_sel.buffer);
203*4882a593Smuzhiyun 	vc_sel.buffer = bp;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	obp = bp;
206*4882a593Smuzhiyun 	for (i = vc_sel.start; i <= vc_sel.end; i += 2) {
207*4882a593Smuzhiyun 		u32 c = sel_pos(i, unicode);
208*4882a593Smuzhiyun 		if (unicode)
209*4882a593Smuzhiyun 			bp += store_utf8(c, bp);
210*4882a593Smuzhiyun 		else
211*4882a593Smuzhiyun 			*bp++ = c;
212*4882a593Smuzhiyun 		if (!isspace(c))
213*4882a593Smuzhiyun 			obp = bp;
214*4882a593Smuzhiyun 		if (!((i + 2) % vc->vc_size_row)) {
215*4882a593Smuzhiyun 			/* strip trailing blanks from line and add newline,
216*4882a593Smuzhiyun 			   unless non-space at end of line. */
217*4882a593Smuzhiyun 			if (obp != bp) {
218*4882a593Smuzhiyun 				bp = obp;
219*4882a593Smuzhiyun 				*bp++ = '\r';
220*4882a593Smuzhiyun 			}
221*4882a593Smuzhiyun 			obp = bp;
222*4882a593Smuzhiyun 		}
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 	vc_sel.buf_len = bp - vc_sel.buffer;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	return 0;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
vc_do_selection(struct vc_data * vc,unsigned short mode,int ps,int pe)229*4882a593Smuzhiyun static int vc_do_selection(struct vc_data *vc, unsigned short mode, int ps,
230*4882a593Smuzhiyun 		int pe)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	int new_sel_start, new_sel_end, spc;
233*4882a593Smuzhiyun 	bool unicode = vt_do_kdgkbmode(fg_console) == K_UNICODE;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	switch (mode) {
236*4882a593Smuzhiyun 	case TIOCL_SELCHAR:	/* character-by-character selection */
237*4882a593Smuzhiyun 		new_sel_start = ps;
238*4882a593Smuzhiyun 		new_sel_end = pe;
239*4882a593Smuzhiyun 		break;
240*4882a593Smuzhiyun 	case TIOCL_SELWORD:	/* word-by-word selection */
241*4882a593Smuzhiyun 		spc = isspace(sel_pos(ps, unicode));
242*4882a593Smuzhiyun 		for (new_sel_start = ps; ; ps -= 2) {
243*4882a593Smuzhiyun 			if ((spc && !isspace(sel_pos(ps, unicode))) ||
244*4882a593Smuzhiyun 			    (!spc && !inword(sel_pos(ps, unicode))))
245*4882a593Smuzhiyun 				break;
246*4882a593Smuzhiyun 			new_sel_start = ps;
247*4882a593Smuzhiyun 			if (!(ps % vc->vc_size_row))
248*4882a593Smuzhiyun 				break;
249*4882a593Smuzhiyun 		}
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 		spc = isspace(sel_pos(pe, unicode));
252*4882a593Smuzhiyun 		for (new_sel_end = pe; ; pe += 2) {
253*4882a593Smuzhiyun 			if ((spc && !isspace(sel_pos(pe, unicode))) ||
254*4882a593Smuzhiyun 			    (!spc && !inword(sel_pos(pe, unicode))))
255*4882a593Smuzhiyun 				break;
256*4882a593Smuzhiyun 			new_sel_end = pe;
257*4882a593Smuzhiyun 			if (!((pe + 2) % vc->vc_size_row))
258*4882a593Smuzhiyun 				break;
259*4882a593Smuzhiyun 		}
260*4882a593Smuzhiyun 		break;
261*4882a593Smuzhiyun 	case TIOCL_SELLINE:	/* line-by-line selection */
262*4882a593Smuzhiyun 		new_sel_start = rounddown(ps, vc->vc_size_row);
263*4882a593Smuzhiyun 		new_sel_end = rounddown(pe, vc->vc_size_row) +
264*4882a593Smuzhiyun 			vc->vc_size_row - 2;
265*4882a593Smuzhiyun 		break;
266*4882a593Smuzhiyun 	case TIOCL_SELPOINTER:
267*4882a593Smuzhiyun 		highlight_pointer(pe);
268*4882a593Smuzhiyun 		return 0;
269*4882a593Smuzhiyun 	default:
270*4882a593Smuzhiyun 		return -EINVAL;
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	/* remove the pointer */
274*4882a593Smuzhiyun 	highlight_pointer(-1);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/* select to end of line if on trailing space */
277*4882a593Smuzhiyun 	if (new_sel_end > new_sel_start &&
278*4882a593Smuzhiyun 		!atedge(new_sel_end, vc->vc_size_row) &&
279*4882a593Smuzhiyun 		isspace(sel_pos(new_sel_end, unicode))) {
280*4882a593Smuzhiyun 		for (pe = new_sel_end + 2; ; pe += 2)
281*4882a593Smuzhiyun 			if (!isspace(sel_pos(pe, unicode)) ||
282*4882a593Smuzhiyun 			    atedge(pe, vc->vc_size_row))
283*4882a593Smuzhiyun 				break;
284*4882a593Smuzhiyun 		if (isspace(sel_pos(pe, unicode)))
285*4882a593Smuzhiyun 			new_sel_end = pe;
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 	if (vc_sel.start == -1)	/* no current selection */
288*4882a593Smuzhiyun 		highlight(new_sel_start, new_sel_end);
289*4882a593Smuzhiyun 	else if (new_sel_start == vc_sel.start)
290*4882a593Smuzhiyun 	{
291*4882a593Smuzhiyun 		if (new_sel_end == vc_sel.end)	/* no action required */
292*4882a593Smuzhiyun 			return 0;
293*4882a593Smuzhiyun 		else if (new_sel_end > vc_sel.end)	/* extend to right */
294*4882a593Smuzhiyun 			highlight(vc_sel.end + 2, new_sel_end);
295*4882a593Smuzhiyun 		else				/* contract from right */
296*4882a593Smuzhiyun 			highlight(new_sel_end + 2, vc_sel.end);
297*4882a593Smuzhiyun 	}
298*4882a593Smuzhiyun 	else if (new_sel_end == vc_sel.end)
299*4882a593Smuzhiyun 	{
300*4882a593Smuzhiyun 		if (new_sel_start < vc_sel.start) /* extend to left */
301*4882a593Smuzhiyun 			highlight(new_sel_start, vc_sel.start - 2);
302*4882a593Smuzhiyun 		else				/* contract from left */
303*4882a593Smuzhiyun 			highlight(vc_sel.start, new_sel_start - 2);
304*4882a593Smuzhiyun 	}
305*4882a593Smuzhiyun 	else	/* some other case; start selection from scratch */
306*4882a593Smuzhiyun 	{
307*4882a593Smuzhiyun 		clear_selection();
308*4882a593Smuzhiyun 		highlight(new_sel_start, new_sel_end);
309*4882a593Smuzhiyun 	}
310*4882a593Smuzhiyun 	vc_sel.start = new_sel_start;
311*4882a593Smuzhiyun 	vc_sel.end = new_sel_end;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	return vc_selection_store_chars(vc, unicode);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
vc_selection(struct vc_data * vc,struct tiocl_selection * v,struct tty_struct * tty)316*4882a593Smuzhiyun static int vc_selection(struct vc_data *vc, struct tiocl_selection *v,
317*4882a593Smuzhiyun 		struct tty_struct *tty)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	int ps, pe;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	poke_blanked_console();
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	if (v->sel_mode == TIOCL_SELCLEAR) {
324*4882a593Smuzhiyun 		/* useful for screendump without selection highlights */
325*4882a593Smuzhiyun 		clear_selection();
326*4882a593Smuzhiyun 		return 0;
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1);
330*4882a593Smuzhiyun 	v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1);
331*4882a593Smuzhiyun 	v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1);
332*4882a593Smuzhiyun 	v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
335*4882a593Smuzhiyun 		mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
336*4882a593Smuzhiyun 			     v->ys);
337*4882a593Smuzhiyun 		return 0;
338*4882a593Smuzhiyun 	}
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	ps = v->ys * vc->vc_size_row + (v->xs << 1);
341*4882a593Smuzhiyun 	pe = v->ye * vc->vc_size_row + (v->xe << 1);
342*4882a593Smuzhiyun 	if (ps > pe)	/* make vc_sel.start <= vc_sel.end */
343*4882a593Smuzhiyun 		swap(ps, pe);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	if (vc_sel.cons != vc) {
346*4882a593Smuzhiyun 		clear_selection();
347*4882a593Smuzhiyun 		vc_sel.cons = vc;
348*4882a593Smuzhiyun 	}
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	return vc_do_selection(vc, v->sel_mode, ps, pe);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
set_selection_kernel(struct tiocl_selection * v,struct tty_struct * tty)353*4882a593Smuzhiyun int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	int ret;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	mutex_lock(&vc_sel.lock);
358*4882a593Smuzhiyun 	console_lock();
359*4882a593Smuzhiyun 	ret = vc_selection(vc_cons[fg_console].d, v, tty);
360*4882a593Smuzhiyun 	console_unlock();
361*4882a593Smuzhiyun 	mutex_unlock(&vc_sel.lock);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	return ret;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(set_selection_kernel);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /* Insert the contents of the selection buffer into the
368*4882a593Smuzhiyun  * queue of the tty associated with the current console.
369*4882a593Smuzhiyun  * Invoked by ioctl().
370*4882a593Smuzhiyun  *
371*4882a593Smuzhiyun  * Locking: called without locks. Calls the ldisc wrongly with
372*4882a593Smuzhiyun  * unsafe methods,
373*4882a593Smuzhiyun  */
paste_selection(struct tty_struct * tty)374*4882a593Smuzhiyun int paste_selection(struct tty_struct *tty)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	struct vc_data *vc = tty->driver_data;
377*4882a593Smuzhiyun 	int	pasted = 0;
378*4882a593Smuzhiyun 	unsigned int count;
379*4882a593Smuzhiyun 	struct  tty_ldisc *ld;
380*4882a593Smuzhiyun 	DECLARE_WAITQUEUE(wait, current);
381*4882a593Smuzhiyun 	int ret = 0;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	console_lock();
384*4882a593Smuzhiyun 	poke_blanked_console();
385*4882a593Smuzhiyun 	console_unlock();
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	ld = tty_ldisc_ref_wait(tty);
388*4882a593Smuzhiyun 	if (!ld)
389*4882a593Smuzhiyun 		return -EIO;	/* ldisc was hung up */
390*4882a593Smuzhiyun 	tty_buffer_lock_exclusive(&vc->port);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	add_wait_queue(&vc->paste_wait, &wait);
393*4882a593Smuzhiyun 	mutex_lock(&vc_sel.lock);
394*4882a593Smuzhiyun 	while (vc_sel.buffer && vc_sel.buf_len > pasted) {
395*4882a593Smuzhiyun 		set_current_state(TASK_INTERRUPTIBLE);
396*4882a593Smuzhiyun 		if (signal_pending(current)) {
397*4882a593Smuzhiyun 			ret = -EINTR;
398*4882a593Smuzhiyun 			break;
399*4882a593Smuzhiyun 		}
400*4882a593Smuzhiyun 		if (tty_throttled(tty)) {
401*4882a593Smuzhiyun 			mutex_unlock(&vc_sel.lock);
402*4882a593Smuzhiyun 			schedule();
403*4882a593Smuzhiyun 			mutex_lock(&vc_sel.lock);
404*4882a593Smuzhiyun 			continue;
405*4882a593Smuzhiyun 		}
406*4882a593Smuzhiyun 		__set_current_state(TASK_RUNNING);
407*4882a593Smuzhiyun 		count = vc_sel.buf_len - pasted;
408*4882a593Smuzhiyun 		count = tty_ldisc_receive_buf(ld, vc_sel.buffer + pasted, NULL,
409*4882a593Smuzhiyun 					      count);
410*4882a593Smuzhiyun 		pasted += count;
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun 	mutex_unlock(&vc_sel.lock);
413*4882a593Smuzhiyun 	remove_wait_queue(&vc->paste_wait, &wait);
414*4882a593Smuzhiyun 	__set_current_state(TASK_RUNNING);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	tty_buffer_unlock_exclusive(&vc->port);
417*4882a593Smuzhiyun 	tty_ldisc_deref(ld);
418*4882a593Smuzhiyun 	return ret;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(paste_selection);
421