Merge remote-tracking branch 'lsk/v3.10/topic/cpufreq' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / staging / speakup / selection.c
1 #include <linux/slab.h> /* for kmalloc */
2 #include <linux/consolemap.h>
3 #include <linux/interrupt.h>
4 #include <linux/sched.h>
5 #include <linux/device.h> /* for dev_warn */
6 #include <linux/selection.h>
7 #include <linux/workqueue.h>
8 #include <linux/tty.h>
9 #include <asm/cmpxchg.h>
10
11 #include "speakup.h"
12
13 /* ------ cut and paste ----- */
14 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
15 #define ishardspace(c)      ((c) == ' ')
16
17 unsigned short spk_xs, spk_ys, spk_xe, spk_ye; /* our region points */
18
19 /* Variables for selection control. */
20 /* must not be deallocated */
21 struct vc_data *spk_sel_cons;
22 /* cleared by clear_selection */
23 static int sel_start = -1;
24 static int sel_end;
25 static int sel_buffer_lth;
26 static char *sel_buffer;
27
28 static unsigned char sel_pos(int n)
29 {
30         return inverse_translate(spk_sel_cons,
31                 screen_glyph(spk_sel_cons, n), 0);
32 }
33
34 void speakup_clear_selection(void)
35 {
36         sel_start = -1;
37 }
38
39 /* does screen address p correspond to character at LH/RH edge of screen? */
40 static int atedge(const int p, int size_row)
41 {
42         return !(p % size_row) || !((p + 2) % size_row);
43 }
44
45 /* constrain v such that v <= u */
46 static unsigned short limit(const unsigned short v, const unsigned short u)
47 {
48         return (v > u) ? u : v;
49 }
50
51 int speakup_set_selection(struct tty_struct *tty)
52 {
53         int new_sel_start, new_sel_end;
54         char *bp, *obp;
55         int i, ps, pe;
56         struct vc_data *vc = vc_cons[fg_console].d;
57
58         spk_xs = limit(spk_xs, vc->vc_cols - 1);
59         spk_ys = limit(spk_ys, vc->vc_rows - 1);
60         spk_xe = limit(spk_xe, vc->vc_cols - 1);
61         spk_ye = limit(spk_ye, vc->vc_rows - 1);
62         ps = spk_ys * vc->vc_size_row + (spk_xs << 1);
63         pe = spk_ye * vc->vc_size_row + (spk_xe << 1);
64
65         if (ps > pe) {
66                 /* make sel_start <= sel_end */
67                 int tmp = ps;
68                 ps = pe;
69                 pe = tmp;
70         }
71
72         if (spk_sel_cons != vc_cons[fg_console].d) {
73                 speakup_clear_selection();
74                 spk_sel_cons = vc_cons[fg_console].d;
75                 dev_warn(tty->dev,
76                         "Selection: mark console not the same as cut\n");
77                 return -EINVAL;
78         }
79
80         new_sel_start = ps;
81         new_sel_end = pe;
82
83         /* select to end of line if on trailing space */
84         if (new_sel_end > new_sel_start &&
85             !atedge(new_sel_end, vc->vc_size_row) &&
86             ishardspace(sel_pos(new_sel_end))) {
87                 for (pe = new_sel_end + 2; ; pe += 2)
88                         if (!ishardspace(sel_pos(pe)) ||
89                             atedge(pe, vc->vc_size_row))
90                                 break;
91                 if (ishardspace(sel_pos(pe)))
92                         new_sel_end = pe;
93         }
94         if ((new_sel_start == sel_start) && (new_sel_end == sel_end))
95                 return 0; /* no action required */
96
97         sel_start = new_sel_start;
98         sel_end = new_sel_end;
99         /* Allocate a new buffer before freeing the old one ... */
100         bp = kmalloc((sel_end-sel_start)/2+1, GFP_ATOMIC);
101         if (!bp) {
102                 speakup_clear_selection();
103                 return -ENOMEM;
104         }
105         kfree(sel_buffer);
106         sel_buffer = bp;
107
108         obp = bp;
109         for (i = sel_start; i <= sel_end; i += 2) {
110                 *bp = sel_pos(i);
111                 if (!ishardspace(*bp++))
112                         obp = bp;
113                 if (!((i + 2) % vc->vc_size_row)) {
114                         /* strip trailing blanks from line and add newline,
115                            unless non-space at end of line. */
116                         if (obp != bp) {
117                                 bp = obp;
118                                 *bp++ = '\r';
119                         }
120                         obp = bp;
121                 }
122         }
123         sel_buffer_lth = bp - sel_buffer;
124         return 0;
125 }
126
127 struct speakup_paste_work {
128         struct work_struct work;
129         struct tty_struct *tty;
130 };
131
132 static void __speakup_paste_selection(struct work_struct *work)
133 {
134         struct speakup_paste_work *spw =
135                 container_of(work, struct speakup_paste_work, work);
136         struct tty_struct *tty = xchg(&spw->tty, NULL);
137         struct vc_data *vc = (struct vc_data *) tty->driver_data;
138         int pasted = 0, count;
139         struct tty_ldisc *ld;
140         DECLARE_WAITQUEUE(wait, current);
141
142         ld = tty_ldisc_ref_wait(tty);
143
144         /* FIXME: this is completely unsafe */
145         add_wait_queue(&vc->paste_wait, &wait);
146         while (sel_buffer && sel_buffer_lth > pasted) {
147                 set_current_state(TASK_INTERRUPTIBLE);
148                 if (test_bit(TTY_THROTTLED, &tty->flags)) {
149                         schedule();
150                         continue;
151                 }
152                 count = sel_buffer_lth - pasted;
153                 count = min_t(int, count, tty->receive_room);
154                 ld->ops->receive_buf(tty, sel_buffer + pasted, NULL, count);
155                 pasted += count;
156         }
157         remove_wait_queue(&vc->paste_wait, &wait);
158         current->state = TASK_RUNNING;
159
160         tty_ldisc_deref(ld);
161         tty_kref_put(tty);
162 }
163
164 static struct speakup_paste_work speakup_paste_work = {
165         .work = __WORK_INITIALIZER(speakup_paste_work.work,
166                                    __speakup_paste_selection)
167 };
168
169 int speakup_paste_selection(struct tty_struct *tty)
170 {
171         if (cmpxchg(&speakup_paste_work.tty, NULL, tty) != NULL)
172                 return -EBUSY;
173
174         tty_kref_get(tty);
175         schedule_work_on(WORK_CPU_UNBOUND, &speakup_paste_work.work);
176         return 0;
177 }
178
179 void speakup_cancel_paste(void)
180 {
181         cancel_work_sync(&speakup_paste_work.work);
182         tty_kref_put(speakup_paste_work.tty);
183 }