• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 completion completion;
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 
ser_to_dev(int ser,dev_t * dev_no)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 
get_dev_to_use(struct spk_synth * synth,dev_t * dev_no)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 
spk_ttyio_ldisc_open(struct tty_struct * tty)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)
55 		return -EOPNOTSUPP;
56 
57 	ldisc_data = kmalloc(sizeof(struct spk_ldisc_data), GFP_KERNEL);
58 	if (!ldisc_data)
59 		return -ENOMEM;
60 
61 	init_completion(&ldisc_data->completion);
62 	ldisc_data->buf_free = true;
63 	tty->disc_data = ldisc_data;
64 
65 	return 0;
66 }
67 
spk_ttyio_ldisc_close(struct tty_struct * tty)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 
spk_ttyio_receive_buf2(struct tty_struct * tty,const unsigned char * cp,char * fp,int count)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 	complete(&ldisc_data->completion);
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 
get_termios(struct tty_struct * tty,struct ktermios * out_termios)134 static inline void get_termios(struct tty_struct *tty,
135 			       struct ktermios *out_termios)
136 {
137 	down_read(&tty->termios_rwsem);
138 	*out_termios = tty->termios;
139 	up_read(&tty->termios_rwsem);
140 }
141 
spk_ttyio_initialise_ldisc(struct spk_synth * synth)142 static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
143 {
144 	int ret = 0;
145 	struct tty_struct *tty;
146 	struct ktermios tmp_termios;
147 	dev_t dev;
148 
149 	ret = get_dev_to_use(synth, &dev);
150 	if (ret)
151 		return ret;
152 
153 	tty = tty_kopen(dev);
154 	if (IS_ERR(tty))
155 		return PTR_ERR(tty);
156 
157 	if (tty->ops->open)
158 		ret = tty->ops->open(tty, NULL);
159 	else
160 		ret = -ENODEV;
161 
162 	if (ret) {
163 		tty_unlock(tty);
164 		return ret;
165 	}
166 
167 	clear_bit(TTY_HUPPED, &tty->flags);
168 	/* ensure hardware flow control is enabled */
169 	get_termios(tty, &tmp_termios);
170 	if (!(tmp_termios.c_cflag & CRTSCTS)) {
171 		tmp_termios.c_cflag |= CRTSCTS;
172 		tty_set_termios(tty, &tmp_termios);
173 		/*
174 		 * check c_cflag to see if it's updated as tty_set_termios
175 		 * may not return error even when no tty bits are
176 		 * changed by the request.
177 		 */
178 		get_termios(tty, &tmp_termios);
179 		if (!(tmp_termios.c_cflag & CRTSCTS))
180 			pr_warn("speakup: Failed to set hardware flow control\n");
181 	}
182 
183 	tty_unlock(tty);
184 
185 	mutex_lock(&speakup_tty_mutex);
186 	speakup_tty = tty;
187 	ret = tty_set_ldisc(tty, N_SPEAKUP);
188 	if (ret)
189 		speakup_tty = NULL;
190 	mutex_unlock(&speakup_tty_mutex);
191 
192 	if (!ret)
193 		/* Success */
194 		return 0;
195 
196 	pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
197 
198 	tty_lock(tty);
199 	if (tty->ops->close)
200 		tty->ops->close(tty, NULL);
201 	tty_unlock(tty);
202 
203 	tty_kclose(tty);
204 
205 	return ret;
206 }
207 
spk_ttyio_register_ldisc(void)208 void spk_ttyio_register_ldisc(void)
209 {
210 	if (tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops))
211 		pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
212 }
213 
spk_ttyio_unregister_ldisc(void)214 void spk_ttyio_unregister_ldisc(void)
215 {
216 	if (tty_unregister_ldisc(N_SPEAKUP))
217 		pr_warn("speakup: Couldn't unregister ldisc\n");
218 }
219 
spk_ttyio_out(struct spk_synth * in_synth,const char ch)220 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
221 {
222 	mutex_lock(&speakup_tty_mutex);
223 	if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
224 		int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
225 
226 		mutex_unlock(&speakup_tty_mutex);
227 		if (ret == 0)
228 			/* No room */
229 			return 0;
230 		if (ret < 0) {
231 			pr_warn("%s: I/O error, deactivating speakup\n",
232 				in_synth->long_name);
233 			/* No synth any more, so nobody will restart TTYs,
234 			 * and we thus need to do it ourselves.  Now that there
235 			 * is no synth we can let application flood anyway
236 			 */
237 			in_synth->alive = 0;
238 			speakup_start_ttys();
239 			return 0;
240 		}
241 		return 1;
242 	}
243 
244 	mutex_unlock(&speakup_tty_mutex);
245 	return 0;
246 }
247 
spk_ttyio_out_unicode(struct spk_synth * in_synth,u16 ch)248 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
249 {
250 	int ret;
251 
252 	if (ch < 0x80) {
253 		ret = spk_ttyio_out(in_synth, ch);
254 	} else if (ch < 0x800) {
255 		ret  = spk_ttyio_out(in_synth, 0xc0 | (ch >> 6));
256 		ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
257 	} else {
258 		ret  = spk_ttyio_out(in_synth, 0xe0 | (ch >> 12));
259 		ret &= spk_ttyio_out(in_synth, 0x80 | ((ch >> 6) & 0x3f));
260 		ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
261 	}
262 	return ret;
263 }
264 
check_tty(struct tty_struct * tty)265 static int check_tty(struct tty_struct *tty)
266 {
267 	if (!tty) {
268 		pr_warn("%s: I/O error, deactivating speakup\n",
269 			spk_ttyio_synth->long_name);
270 		/* No synth any more, so nobody will restart TTYs, and we thus
271 		 * need to do it ourselves.  Now that there is no synth we can
272 		 * let application flood anyway
273 		 */
274 		spk_ttyio_synth->alive = 0;
275 		speakup_start_ttys();
276 		return 1;
277 	}
278 
279 	return 0;
280 }
281 
spk_ttyio_send_xchar(char ch)282 static void spk_ttyio_send_xchar(char ch)
283 {
284 	mutex_lock(&speakup_tty_mutex);
285 	if (check_tty(speakup_tty)) {
286 		mutex_unlock(&speakup_tty_mutex);
287 		return;
288 	}
289 
290 	if (speakup_tty->ops->send_xchar)
291 		speakup_tty->ops->send_xchar(speakup_tty, ch);
292 	mutex_unlock(&speakup_tty_mutex);
293 }
294 
spk_ttyio_tiocmset(unsigned int set,unsigned int clear)295 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
296 {
297 	mutex_lock(&speakup_tty_mutex);
298 	if (check_tty(speakup_tty)) {
299 		mutex_unlock(&speakup_tty_mutex);
300 		return;
301 	}
302 
303 	if (speakup_tty->ops->tiocmset)
304 		speakup_tty->ops->tiocmset(speakup_tty, set, clear);
305 	mutex_unlock(&speakup_tty_mutex);
306 }
307 
ttyio_in(int timeout)308 static unsigned char ttyio_in(int timeout)
309 {
310 	struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
311 	char rv;
312 
313 	if (wait_for_completion_timeout(&ldisc_data->completion,
314 					usecs_to_jiffies(timeout)) == 0) {
315 		if (timeout)
316 			pr_warn("spk_ttyio: timeout (%d)  while waiting for input\n",
317 				timeout);
318 		return 0xff;
319 	}
320 
321 	rv = ldisc_data->buf;
322 	/* Make sure we have read buf before we set buf_free to let
323 	 * the producer overwrite it
324 	 */
325 	mb();
326 	ldisc_data->buf_free = true;
327 	/* Let TTY push more characters */
328 	tty_flip_buffer_push(speakup_tty->port);
329 
330 	return rv;
331 }
332 
spk_ttyio_in(void)333 static unsigned char spk_ttyio_in(void)
334 {
335 	return ttyio_in(SPK_SYNTH_TIMEOUT);
336 }
337 
spk_ttyio_in_nowait(void)338 static unsigned char spk_ttyio_in_nowait(void)
339 {
340 	u8 rv = ttyio_in(0);
341 
342 	return (rv == 0xff) ? 0 : rv;
343 }
344 
spk_ttyio_flush_buffer(void)345 static void spk_ttyio_flush_buffer(void)
346 {
347 	mutex_lock(&speakup_tty_mutex);
348 	if (check_tty(speakup_tty)) {
349 		mutex_unlock(&speakup_tty_mutex);
350 		return;
351 	}
352 
353 	if (speakup_tty->ops->flush_buffer)
354 		speakup_tty->ops->flush_buffer(speakup_tty);
355 
356 	mutex_unlock(&speakup_tty_mutex);
357 }
358 
spk_ttyio_synth_probe(struct spk_synth * synth)359 int spk_ttyio_synth_probe(struct spk_synth *synth)
360 {
361 	int rv = spk_ttyio_initialise_ldisc(synth);
362 
363 	if (rv)
364 		return rv;
365 
366 	synth->alive = 1;
367 	spk_ttyio_synth = synth;
368 
369 	return 0;
370 }
371 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
372 
spk_ttyio_release(void)373 void spk_ttyio_release(void)
374 {
375 	if (!speakup_tty)
376 		return;
377 
378 	tty_lock(speakup_tty);
379 
380 	if (speakup_tty->ops->close)
381 		speakup_tty->ops->close(speakup_tty, NULL);
382 
383 	tty_ldisc_flush(speakup_tty);
384 	tty_unlock(speakup_tty);
385 	tty_kclose(speakup_tty);
386 }
387 EXPORT_SYMBOL_GPL(spk_ttyio_release);
388 
spk_ttyio_synth_immediate(struct spk_synth * synth,const char * buff)389 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
390 {
391 	u_char ch;
392 
393 	while ((ch = *buff)) {
394 		if (ch == '\n')
395 			ch = synth->procspeech;
396 		if (tty_write_room(speakup_tty) < 1 ||
397 		    !synth->io_ops->synth_out(synth, ch))
398 			return buff;
399 		buff++;
400 	}
401 	return NULL;
402 }
403 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);
404