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);
120 struct spk_io_ops spk_ttyio_ops = {
121 .synth_out = spk_ttyio_out,
122 .synth_out_unicode = spk_ttyio_out_unicode,
123 .send_xchar = spk_ttyio_send_xchar,
124 .tiocmset = spk_ttyio_tiocmset,
125 .synth_in = spk_ttyio_in,
126 .synth_in_nowait = spk_ttyio_in_nowait,
127 .flush_buffer = spk_ttyio_flush_buffer,
129 EXPORT_SYMBOL_GPL(spk_ttyio_ops);
131 static inline void get_termios(struct tty_struct *tty,
132 struct ktermios *out_termios)
134 down_read(&tty->termios_rwsem);
135 *out_termios = tty->termios;
136 up_read(&tty->termios_rwsem);
139 static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
142 struct tty_struct *tty;
143 struct ktermios tmp_termios;
146 ret = get_dev_to_use(synth, &dev);
150 tty = tty_kopen(dev);
155 ret = tty->ops->open(tty, NULL);
164 clear_bit(TTY_HUPPED, &tty->flags);
165 /* ensure hardware flow control is enabled */
166 get_termios(tty, &tmp_termios);
167 if (!(tmp_termios.c_cflag & CRTSCTS)) {
168 tmp_termios.c_cflag |= CRTSCTS;
169 tty_set_termios(tty, &tmp_termios);
171 * check c_cflag to see if it's updated as tty_set_termios
172 * may not return error even when no tty bits are
173 * changed by the request.
175 get_termios(tty, &tmp_termios);
176 if (!(tmp_termios.c_cflag & CRTSCTS))
177 pr_warn("speakup: Failed to set hardware flow control\n");
182 ret = tty_set_ldisc(tty, N_SPEAKUP);
184 pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
189 void spk_ttyio_register_ldisc(void)
191 if (tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops))
192 pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
195 void spk_ttyio_unregister_ldisc(void)
197 if (tty_unregister_ldisc(N_SPEAKUP))
198 pr_warn("speakup: Couldn't unregister ldisc\n");
201 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
203 mutex_lock(&speakup_tty_mutex);
204 if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
205 int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
207 mutex_unlock(&speakup_tty_mutex);
212 pr_warn("%s: I/O error, deactivating speakup\n",
213 in_synth->long_name);
214 /* No synth any more, so nobody will restart TTYs,
215 * and we thus need to do it ourselves. Now that there
216 * is no synth we can let application flood anyway
219 speakup_start_ttys();
225 mutex_unlock(&speakup_tty_mutex);
229 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
234 ret = spk_ttyio_out(in_synth, ch);
235 } else if (ch < 0x800) {
236 ret = spk_ttyio_out(in_synth, 0xc0 | (ch >> 6));
237 ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
239 ret = spk_ttyio_out(in_synth, 0xe0 | (ch >> 12));
240 ret &= spk_ttyio_out(in_synth, 0x80 | ((ch >> 6) & 0x3f));
241 ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
246 static int check_tty(struct tty_struct *tty)
249 pr_warn("%s: I/O error, deactivating speakup\n",
250 spk_ttyio_synth->long_name);
251 /* No synth any more, so nobody will restart TTYs, and we thus
252 * need to do it ourselves. Now that there is no synth we can
253 * let application flood anyway
255 spk_ttyio_synth->alive = 0;
256 speakup_start_ttys();
263 static void spk_ttyio_send_xchar(char ch)
265 mutex_lock(&speakup_tty_mutex);
266 if (check_tty(speakup_tty)) {
267 mutex_unlock(&speakup_tty_mutex);
271 if (speakup_tty->ops->send_xchar)
272 speakup_tty->ops->send_xchar(speakup_tty, ch);
273 mutex_unlock(&speakup_tty_mutex);
276 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
278 mutex_lock(&speakup_tty_mutex);
279 if (check_tty(speakup_tty)) {
280 mutex_unlock(&speakup_tty_mutex);
284 if (speakup_tty->ops->tiocmset)
285 speakup_tty->ops->tiocmset(speakup_tty, set, clear);
286 mutex_unlock(&speakup_tty_mutex);
289 static unsigned char ttyio_in(int timeout)
291 struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
294 if (wait_for_completion_timeout(&ldisc_data->completion,
295 usecs_to_jiffies(timeout)) == 0) {
297 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
302 rv = ldisc_data->buf;
303 /* Make sure we have read buf before we set buf_free to let
304 * the producer overwrite it
307 ldisc_data->buf_free = true;
308 /* Let TTY push more characters */
309 tty_schedule_flip(speakup_tty->port);
314 static unsigned char spk_ttyio_in(void)
316 return ttyio_in(SPK_SYNTH_TIMEOUT);
319 static unsigned char spk_ttyio_in_nowait(void)
323 return (rv == 0xff) ? 0 : rv;
326 static void spk_ttyio_flush_buffer(void)
328 mutex_lock(&speakup_tty_mutex);
329 if (check_tty(speakup_tty)) {
330 mutex_unlock(&speakup_tty_mutex);
334 if (speakup_tty->ops->flush_buffer)
335 speakup_tty->ops->flush_buffer(speakup_tty);
337 mutex_unlock(&speakup_tty_mutex);
340 int spk_ttyio_synth_probe(struct spk_synth *synth)
342 int rv = spk_ttyio_initialise_ldisc(synth);
348 spk_ttyio_synth = synth;
352 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
354 void spk_ttyio_release(void)
359 tty_lock(speakup_tty);
361 if (speakup_tty->ops->close)
362 speakup_tty->ops->close(speakup_tty, NULL);
364 tty_ldisc_flush(speakup_tty);
365 tty_unlock(speakup_tty);
366 tty_kclose(speakup_tty);
368 EXPORT_SYMBOL_GPL(spk_ttyio_release);
370 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
374 while ((ch = *buff)) {
376 ch = synth->procspeech;
377 if (tty_write_room(speakup_tty) < 1 ||
378 !synth->io_ops->synth_out(synth, ch))
384 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);