1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
4 #include <linux/tty_flip.h>
5 #include <linux/slab.h>
11 struct spk_ldisc_data {
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.
23 static DEFINE_MUTEX(speakup_tty_mutex);
25 static int ser_to_dev(int ser, dev_t *dev_no)
27 if (ser < 0 || ser > (255 - 64)) {
28 pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
32 *dev_no = MKDEV(4, (64 + ser));
36 static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
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);
43 return ser_to_dev(synth->ser, dev_no);
46 static int spk_ttyio_ldisc_open(struct tty_struct *tty)
48 struct spk_ldisc_data *ldisc_data;
50 if (tty != speakup_tty)
51 /* Somebody tried to use this line discipline outside speakup */
54 if (tty->ops->write == NULL)
57 ldisc_data = kmalloc(sizeof(struct spk_ldisc_data), GFP_KERNEL);
61 sema_init(&ldisc_data->sem, 0);
62 ldisc_data->buf_free = true;
63 tty->disc_data = ldisc_data;
68 static void spk_ttyio_ldisc_close(struct tty_struct *tty)
70 mutex_lock(&speakup_tty_mutex);
71 kfree(speakup_tty->disc_data);
73 mutex_unlock(&speakup_tty_mutex);
76 static int spk_ttyio_receive_buf2(struct tty_struct *tty,
77 const unsigned char *cp, char *fp, int count)
79 struct spk_ldisc_data *ldisc_data = tty->disc_data;
81 if (spk_ttyio_synth->read_buff_add) {
84 for (i = 0; i < count; i++)
85 spk_ttyio_synth->read_buff_add(cp[i]);
90 if (!ldisc_data->buf_free)
91 /* ttyio_in will tty_schedule_flip */
94 /* Make sure the consumer has read buf before we have seen
95 * buf_free == true and overwrite buf
99 ldisc_data->buf = cp[0];
100 ldisc_data->buf_free = false;
101 up(&ldisc_data->sem);
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,
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);
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,
132 EXPORT_SYMBOL_GPL(spk_ttyio_ops);
134 static inline void get_termios(struct tty_struct *tty, struct ktermios *out_termios)
136 down_read(&tty->termios_rwsem);
137 *out_termios = tty->termios;
138 up_read(&tty->termios_rwsem);
141 static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
144 struct tty_struct *tty;
145 struct ktermios tmp_termios;
148 ret = get_dev_to_use(synth, &dev);
152 tty = tty_kopen(dev);
157 ret = tty->ops->open(tty, NULL);
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);
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.
176 get_termios(tty, &tmp_termios);
177 if (!(tmp_termios.c_cflag & CRTSCTS))
178 pr_warn("speakup: Failed to set hardware flow control\n");
183 mutex_lock(&speakup_tty_mutex);
185 ret = tty_set_ldisc(tty, N_SPEAKUP);
188 mutex_unlock(&speakup_tty_mutex);
194 pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
198 tty->ops->close(tty, NULL);
206 void spk_ttyio_register_ldisc(void)
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");
212 void spk_ttyio_unregister_ldisc(void)
214 if (tty_unregister_ldisc(N_SPEAKUP))
215 pr_warn("speakup: Couldn't unregister ldisc\n");
218 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
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);
224 mutex_unlock(&speakup_tty_mutex);
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
235 speakup_start_ttys();
241 mutex_unlock(&speakup_tty_mutex);
245 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
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));
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));
262 static int check_tty(struct tty_struct *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
271 spk_ttyio_synth->alive = 0;
272 speakup_start_ttys();
279 static void spk_ttyio_send_xchar(char ch)
281 mutex_lock(&speakup_tty_mutex);
282 if (check_tty(speakup_tty)) {
283 mutex_unlock(&speakup_tty_mutex);
287 if (speakup_tty->ops->send_xchar)
288 speakup_tty->ops->send_xchar(speakup_tty, ch);
289 mutex_unlock(&speakup_tty_mutex);
292 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
294 mutex_lock(&speakup_tty_mutex);
295 if (check_tty(speakup_tty)) {
296 mutex_unlock(&speakup_tty_mutex);
300 if (speakup_tty->ops->tiocmset)
301 speakup_tty->ops->tiocmset(speakup_tty, set, clear);
302 mutex_unlock(&speakup_tty_mutex);
305 static unsigned char ttyio_in(int timeout)
307 struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
310 if (down_timeout(&ldisc_data->sem, usecs_to_jiffies(timeout)) == -ETIME) {
312 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
317 rv = ldisc_data->buf;
318 /* Make sure we have read buf before we set buf_free to let
319 * the producer overwrite it
322 ldisc_data->buf_free = true;
323 /* Let TTY push more characters */
324 tty_schedule_flip(speakup_tty->port);
329 static unsigned char spk_ttyio_in(void)
331 return ttyio_in(SPK_SYNTH_TIMEOUT);
334 static unsigned char spk_ttyio_in_nowait(void)
338 return (rv == 0xff) ? 0 : rv;
341 static void spk_ttyio_flush_buffer(void)
343 mutex_lock(&speakup_tty_mutex);
344 if (check_tty(speakup_tty)) {
345 mutex_unlock(&speakup_tty_mutex);
349 if (speakup_tty->ops->flush_buffer)
350 speakup_tty->ops->flush_buffer(speakup_tty);
352 mutex_unlock(&speakup_tty_mutex);
355 int spk_ttyio_synth_probe(struct spk_synth *synth)
357 int rv = spk_ttyio_initialise_ldisc(synth);
363 spk_ttyio_synth = synth;
367 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
369 void spk_ttyio_release(void)
374 tty_lock(speakup_tty);
376 if (speakup_tty->ops->close)
377 speakup_tty->ops->close(speakup_tty, NULL);
379 tty_ldisc_flush(speakup_tty);
380 tty_unlock(speakup_tty);
381 tty_kclose(speakup_tty);
383 EXPORT_SYMBOL_GPL(spk_ttyio_release);
385 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
389 while ((ch = *buff)) {
391 ch = synth->procspeech;
392 if (tty_write_room(speakup_tty) < 1 || !synth->io_ops->synth_out(synth, ch))
398 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);