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 {
13 struct completion completion;
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;
54 ldisc_data = kmalloc(sizeof(*ldisc_data), GFP_KERNEL);
58 init_completion(&ldisc_data->completion);
59 ldisc_data->buf_free = true;
60 speakup_tty->disc_data = ldisc_data;
65 static void spk_ttyio_ldisc_close(struct tty_struct *tty)
67 mutex_lock(&speakup_tty_mutex);
68 kfree(speakup_tty->disc_data);
70 mutex_unlock(&speakup_tty_mutex);
73 static int spk_ttyio_receive_buf2(struct tty_struct *tty,
74 const unsigned char *cp, char *fp, int count)
76 struct spk_ldisc_data *ldisc_data = tty->disc_data;
78 if (spk_ttyio_synth->read_buff_add) {
81 for (i = 0; i < count; i++)
82 spk_ttyio_synth->read_buff_add(cp[i]);
87 if (!ldisc_data->buf_free)
88 /* ttyio_in will tty_schedule_flip */
91 /* Make sure the consumer has read buf before we have seen
92 * buf_free == true and overwrite buf
96 ldisc_data->buf = cp[0];
97 ldisc_data->buf_free = false;
98 complete(&ldisc_data->completion);
103 static struct tty_ldisc_ops spk_ttyio_ldisc_ops = {
104 .owner = THIS_MODULE,
105 .magic = TTY_LDISC_MAGIC,
106 .name = "speakup_ldisc",
107 .open = spk_ttyio_ldisc_open,
108 .close = spk_ttyio_ldisc_close,
109 .receive_buf2 = spk_ttyio_receive_buf2,
112 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch);
113 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch);
114 static void spk_ttyio_send_xchar(char ch);
115 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear);
116 static unsigned char spk_ttyio_in(void);
117 static unsigned char spk_ttyio_in_nowait(void);
118 static void spk_ttyio_flush_buffer(void);
119 static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth);
121 struct spk_io_ops spk_ttyio_ops = {
122 .synth_out = spk_ttyio_out,
123 .synth_out_unicode = spk_ttyio_out_unicode,
124 .send_xchar = spk_ttyio_send_xchar,
125 .tiocmset = spk_ttyio_tiocmset,
126 .synth_in = spk_ttyio_in,
127 .synth_in_nowait = spk_ttyio_in_nowait,
128 .flush_buffer = spk_ttyio_flush_buffer,
129 .wait_for_xmitr = spk_ttyio_wait_for_xmitr,
131 EXPORT_SYMBOL_GPL(spk_ttyio_ops);
133 static inline void get_termios(struct tty_struct *tty,
134 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
174 * may not return error even when no tty bits are
175 * changed by the request.
177 get_termios(tty, &tmp_termios);
178 if (!(tmp_termios.c_cflag & CRTSCTS))
179 pr_warn("speakup: Failed to set hardware flow control\n");
184 ret = tty_set_ldisc(tty, N_SPEAKUP);
186 pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
191 void spk_ttyio_register_ldisc(void)
193 if (tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops))
194 pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
197 void spk_ttyio_unregister_ldisc(void)
199 if (tty_unregister_ldisc(N_SPEAKUP))
200 pr_warn("speakup: Couldn't unregister ldisc\n");
203 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
205 mutex_lock(&speakup_tty_mutex);
206 if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
207 int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
209 mutex_unlock(&speakup_tty_mutex);
214 pr_warn("%s: I/O error, deactivating speakup\n",
215 in_synth->long_name);
216 /* No synth any more, so nobody will restart TTYs,
217 * and we thus need to do it ourselves. Now that there
218 * is no synth we can let application flood anyway
221 speakup_start_ttys();
227 mutex_unlock(&speakup_tty_mutex);
231 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
236 ret = spk_ttyio_out(in_synth, ch);
237 } else if (ch < 0x800) {
238 ret = spk_ttyio_out(in_synth, 0xc0 | (ch >> 6));
239 ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
241 ret = spk_ttyio_out(in_synth, 0xe0 | (ch >> 12));
242 ret &= spk_ttyio_out(in_synth, 0x80 | ((ch >> 6) & 0x3f));
243 ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
248 static int check_tty(struct tty_struct *tty)
251 pr_warn("%s: I/O error, deactivating speakup\n",
252 spk_ttyio_synth->long_name);
253 /* No synth any more, so nobody will restart TTYs, and we thus
254 * need to do it ourselves. Now that there is no synth we can
255 * let application flood anyway
257 spk_ttyio_synth->alive = 0;
258 speakup_start_ttys();
265 static void spk_ttyio_send_xchar(char ch)
267 mutex_lock(&speakup_tty_mutex);
268 if (check_tty(speakup_tty)) {
269 mutex_unlock(&speakup_tty_mutex);
273 if (speakup_tty->ops->send_xchar)
274 speakup_tty->ops->send_xchar(speakup_tty, ch);
275 mutex_unlock(&speakup_tty_mutex);
278 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
280 mutex_lock(&speakup_tty_mutex);
281 if (check_tty(speakup_tty)) {
282 mutex_unlock(&speakup_tty_mutex);
286 if (speakup_tty->ops->tiocmset)
287 speakup_tty->ops->tiocmset(speakup_tty, set, clear);
288 mutex_unlock(&speakup_tty_mutex);
291 static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth)
296 static unsigned char ttyio_in(int timeout)
298 struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
301 if (wait_for_completion_timeout(&ldisc_data->completion,
302 usecs_to_jiffies(timeout)) == 0) {
304 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
309 rv = ldisc_data->buf;
310 /* Make sure we have read buf before we set buf_free to let
311 * the producer overwrite it
314 ldisc_data->buf_free = true;
315 /* Let TTY push more characters */
316 tty_schedule_flip(speakup_tty->port);
321 static unsigned char spk_ttyio_in(void)
323 return ttyio_in(SPK_SYNTH_TIMEOUT);
326 static unsigned char spk_ttyio_in_nowait(void)
330 return (rv == 0xff) ? 0 : rv;
333 static void spk_ttyio_flush_buffer(void)
335 mutex_lock(&speakup_tty_mutex);
336 if (check_tty(speakup_tty)) {
337 mutex_unlock(&speakup_tty_mutex);
341 if (speakup_tty->ops->flush_buffer)
342 speakup_tty->ops->flush_buffer(speakup_tty);
344 mutex_unlock(&speakup_tty_mutex);
347 int spk_ttyio_synth_probe(struct spk_synth *synth)
349 int rv = spk_ttyio_initialise_ldisc(synth);
355 spk_ttyio_synth = synth;
359 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
361 void spk_ttyio_release(void)
366 tty_lock(speakup_tty);
368 if (speakup_tty->ops->close)
369 speakup_tty->ops->close(speakup_tty, NULL);
371 tty_ldisc_flush(speakup_tty);
372 tty_unlock(speakup_tty);
373 tty_kclose(speakup_tty);
375 EXPORT_SYMBOL_GPL(spk_ttyio_release);
377 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
381 while ((ch = *buff)) {
383 ch = synth->procspeech;
384 if (tty_write_room(speakup_tty) < 1 ||
385 !synth->io_ops->synth_out(synth, ch))
391 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);