GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / staging / speakup / spk_ttyio.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/tty.h>
4 #include <linux/tty_flip.h>
5 #include <linux/slab.h>
6
7 #include "speakup.h"
8 #include "spk_types.h"
9 #include "spk_priv.h"
10
11 struct spk_ldisc_data {
12         char buf;
13         struct semaphore sem;
14         bool buf_free;
15 };
16
17 static struct spk_synth *spk_ttyio_synth;
18 static struct tty_struct *speakup_tty;
19 /* mutex to protect against speakup_tty disappearing from underneath us while
20  * we are using it. this can happen when the device physically unplugged,
21  * while in use. it also serialises access to speakup_tty.
22  */
23 static DEFINE_MUTEX(speakup_tty_mutex);
24
25 static int ser_to_dev(int ser, dev_t *dev_no)
26 {
27         if (ser < 0 || ser > (255 - 64)) {
28                 pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
29                 return -EINVAL;
30         }
31
32         *dev_no = MKDEV(4, (64 + ser));
33         return 0;
34 }
35
36 static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
37 {
38         /* use ser only when dev is not specified */
39         if (strcmp(synth->dev_name, SYNTH_DEFAULT_DEV) ||
40             synth->ser == SYNTH_DEFAULT_SER)
41                 return tty_dev_name_to_number(synth->dev_name, dev_no);
42
43         return ser_to_dev(synth->ser, dev_no);
44 }
45
46 static int spk_ttyio_ldisc_open(struct tty_struct *tty)
47 {
48         struct spk_ldisc_data *ldisc_data;
49
50         if (tty != speakup_tty)
51                 /* Somebody tried to use this line discipline outside speakup */
52                 return -ENODEV;
53
54         if (tty->ops->write == NULL)
55                 return -EOPNOTSUPP;
56
57         ldisc_data = kmalloc(sizeof(struct spk_ldisc_data), GFP_KERNEL);
58         if (!ldisc_data)
59                 return -ENOMEM;
60
61         sema_init(&ldisc_data->sem, 0);
62         ldisc_data->buf_free = true;
63         tty->disc_data = ldisc_data;
64
65         return 0;
66 }
67
68 static void spk_ttyio_ldisc_close(struct tty_struct *tty)
69 {
70         mutex_lock(&speakup_tty_mutex);
71         kfree(speakup_tty->disc_data);
72         speakup_tty = NULL;
73         mutex_unlock(&speakup_tty_mutex);
74 }
75
76 static int spk_ttyio_receive_buf2(struct tty_struct *tty,
77                                   const unsigned char *cp, char *fp, int count)
78 {
79         struct spk_ldisc_data *ldisc_data = tty->disc_data;
80
81         if (spk_ttyio_synth->read_buff_add) {
82                 int i;
83
84                 for (i = 0; i < count; i++)
85                         spk_ttyio_synth->read_buff_add(cp[i]);
86
87                 return count;
88         }
89
90         if (!ldisc_data->buf_free)
91                 /* ttyio_in will tty_flip_buffer_push */
92                 return 0;
93
94         /* Make sure the consumer has read buf before we have seen
95          * buf_free == true and overwrite buf
96          */
97         mb();
98
99         ldisc_data->buf = cp[0];
100         ldisc_data->buf_free = false;
101         up(&ldisc_data->sem);
102
103         return 1;
104 }
105
106 static struct tty_ldisc_ops spk_ttyio_ldisc_ops = {
107         .owner          = THIS_MODULE,
108         .magic          = TTY_LDISC_MAGIC,
109         .name           = "speakup_ldisc",
110         .open           = spk_ttyio_ldisc_open,
111         .close          = spk_ttyio_ldisc_close,
112         .receive_buf2   = spk_ttyio_receive_buf2,
113 };
114
115 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch);
116 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch);
117 static void spk_ttyio_send_xchar(char ch);
118 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear);
119 static unsigned char spk_ttyio_in(void);
120 static unsigned char spk_ttyio_in_nowait(void);
121 static void spk_ttyio_flush_buffer(void);
122
123 struct spk_io_ops spk_ttyio_ops = {
124         .synth_out = spk_ttyio_out,
125         .synth_out_unicode = spk_ttyio_out_unicode,
126         .send_xchar = spk_ttyio_send_xchar,
127         .tiocmset = spk_ttyio_tiocmset,
128         .synth_in = spk_ttyio_in,
129         .synth_in_nowait = spk_ttyio_in_nowait,
130         .flush_buffer = spk_ttyio_flush_buffer,
131 };
132 EXPORT_SYMBOL_GPL(spk_ttyio_ops);
133
134 static inline void get_termios(struct tty_struct *tty, struct ktermios *out_termios)
135 {
136         down_read(&tty->termios_rwsem);
137         *out_termios = tty->termios;
138         up_read(&tty->termios_rwsem);
139 }
140
141 static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
142 {
143         int ret = 0;
144         struct tty_struct *tty;
145         struct ktermios tmp_termios;
146         dev_t dev;
147
148         ret = get_dev_to_use(synth, &dev);
149         if (ret)
150                 return ret;
151
152         tty = tty_kopen(dev);
153         if (IS_ERR(tty))
154                 return PTR_ERR(tty);
155
156         if (tty->ops->open)
157                 ret = tty->ops->open(tty, NULL);
158         else
159                 ret = -ENODEV;
160
161         if (ret) {
162                 tty_unlock(tty);
163                 return ret;
164         }
165
166         clear_bit(TTY_HUPPED, &tty->flags);
167         /* ensure hardware flow control is enabled */
168         get_termios(tty, &tmp_termios);
169         if (!(tmp_termios.c_cflag & CRTSCTS)) {
170                 tmp_termios.c_cflag |= CRTSCTS;
171                 tty_set_termios(tty, &tmp_termios);
172                 /*
173                  * check c_cflag to see if it's updated as tty_set_termios may not return
174                  * error even when no tty bits are changed by the request.
175                  */
176                 get_termios(tty, &tmp_termios);
177                 if (!(tmp_termios.c_cflag & CRTSCTS))
178                         pr_warn("speakup: Failed to set hardware flow control\n");
179         }
180
181         tty_unlock(tty);
182
183         mutex_lock(&speakup_tty_mutex);
184         speakup_tty = tty;
185         ret = tty_set_ldisc(tty, N_SPEAKUP);
186         if (ret)
187                 speakup_tty = NULL;
188         mutex_unlock(&speakup_tty_mutex);
189
190         if (!ret)
191                 /* Success */
192                 return 0;
193
194         pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
195
196         tty_lock(tty);
197         if (tty->ops->close)
198                 tty->ops->close(tty, NULL);
199         tty_unlock(tty);
200
201         tty_kclose(tty);
202
203         return ret;
204 }
205
206 void spk_ttyio_register_ldisc(void)
207 {
208         if (tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops))
209                 pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
210 }
211
212 void spk_ttyio_unregister_ldisc(void)
213 {
214         if (tty_unregister_ldisc(N_SPEAKUP))
215                 pr_warn("speakup: Couldn't unregister ldisc\n");
216 }
217
218 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
219 {
220         mutex_lock(&speakup_tty_mutex);
221         if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
222                 int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
223
224                 mutex_unlock(&speakup_tty_mutex);
225                 if (ret == 0)
226                         /* No room */
227                         return 0;
228                 if (ret < 0) {
229                         pr_warn("%s: I/O error, deactivating speakup\n", in_synth->long_name);
230                         /* No synth any more, so nobody will restart TTYs, and we thus
231                          * need to do it ourselves.  Now that there is no synth we can
232                          * let application flood anyway
233                          */
234                         in_synth->alive = 0;
235                         speakup_start_ttys();
236                         return 0;
237                 }
238                 return 1;
239         }
240
241         mutex_unlock(&speakup_tty_mutex);
242         return 0;
243 }
244
245 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
246 {
247         int ret;
248
249         if (ch < 0x80)
250                 ret = spk_ttyio_out(in_synth, ch);
251         else if (ch < 0x800) {
252                 ret  = spk_ttyio_out(in_synth, 0xc0 | (ch >> 6));
253                 ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
254         } else {
255                 ret  = spk_ttyio_out(in_synth, 0xe0 | (ch >> 12));
256                 ret &= spk_ttyio_out(in_synth, 0x80 | ((ch >> 6) & 0x3f));
257                 ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
258         }
259         return ret;
260 }
261
262 static int check_tty(struct tty_struct *tty)
263 {
264         if (!tty) {
265                 pr_warn("%s: I/O error, deactivating speakup\n",
266                         spk_ttyio_synth->long_name);
267                 /* No synth any more, so nobody will restart TTYs, and we thus
268                  * need to do it ourselves.  Now that there is no synth we can
269                  * let application flood anyway
270                  */
271                 spk_ttyio_synth->alive = 0;
272                 speakup_start_ttys();
273                 return 1;
274         }
275
276         return 0;
277 }
278
279 static void spk_ttyio_send_xchar(char ch)
280 {
281         mutex_lock(&speakup_tty_mutex);
282         if (check_tty(speakup_tty)) {
283                 mutex_unlock(&speakup_tty_mutex);
284                 return;
285         }
286
287         if (speakup_tty->ops->send_xchar)
288                 speakup_tty->ops->send_xchar(speakup_tty, ch);
289         mutex_unlock(&speakup_tty_mutex);
290 }
291
292 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
293 {
294         mutex_lock(&speakup_tty_mutex);
295         if (check_tty(speakup_tty)) {
296                 mutex_unlock(&speakup_tty_mutex);
297                 return;
298         }
299
300         if (speakup_tty->ops->tiocmset)
301                 speakup_tty->ops->tiocmset(speakup_tty, set, clear);
302         mutex_unlock(&speakup_tty_mutex);
303 }
304
305 static unsigned char ttyio_in(int timeout)
306 {
307         struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
308         char rv;
309
310         if (down_timeout(&ldisc_data->sem, usecs_to_jiffies(timeout)) == -ETIME) {
311                 if (timeout)
312                         pr_warn("spk_ttyio: timeout (%d)  while waiting for input\n",
313                                 timeout);
314                 return 0xff;
315         }
316
317         rv = ldisc_data->buf;
318         /* Make sure we have read buf before we set buf_free to let
319          * the producer overwrite it
320          */
321         mb();
322         ldisc_data->buf_free = true;
323         /* Let TTY push more characters */
324         tty_flip_buffer_push(speakup_tty->port);
325
326         return rv;
327 }
328
329 static unsigned char spk_ttyio_in(void)
330 {
331         return ttyio_in(SPK_SYNTH_TIMEOUT);
332 }
333
334 static unsigned char spk_ttyio_in_nowait(void)
335 {
336         u8 rv = ttyio_in(0);
337
338         return (rv == 0xff) ? 0 : rv;
339 }
340
341 static void spk_ttyio_flush_buffer(void)
342 {
343         mutex_lock(&speakup_tty_mutex);
344         if (check_tty(speakup_tty)) {
345                 mutex_unlock(&speakup_tty_mutex);
346                 return;
347         }
348
349         if (speakup_tty->ops->flush_buffer)
350                 speakup_tty->ops->flush_buffer(speakup_tty);
351
352         mutex_unlock(&speakup_tty_mutex);
353 }
354
355 int spk_ttyio_synth_probe(struct spk_synth *synth)
356 {
357         int rv = spk_ttyio_initialise_ldisc(synth);
358
359         if (rv)
360                 return rv;
361
362         synth->alive = 1;
363         spk_ttyio_synth = synth;
364
365         return 0;
366 }
367 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
368
369 void spk_ttyio_release(void)
370 {
371         if (!speakup_tty)
372                 return;
373
374         tty_lock(speakup_tty);
375
376         if (speakup_tty->ops->close)
377                 speakup_tty->ops->close(speakup_tty, NULL);
378
379         tty_ldisc_flush(speakup_tty);
380         tty_unlock(speakup_tty);
381         tty_kclose(speakup_tty);
382 }
383 EXPORT_SYMBOL_GPL(spk_ttyio_release);
384
385 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
386 {
387         u_char ch;
388
389         while ((ch = *buff)) {
390                 if (ch == '\n')
391                         ch = synth->procspeech;
392                 if (tty_write_room(speakup_tty) < 1 || !synth->io_ops->synth_out(synth, ch))
393                         return buff;
394                 buff++;
395         }
396         return NULL;
397 }
398 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);