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;
15 struct spk_synth *synth;
19 * This allows to catch within spk_ttyio_ldisc_open whether it is getting set
20 * on for a speakup-driven device.
22 static struct tty_struct *speakup_tty;
23 /* This mutex serializes the use of such global speakup_tty variable */
24 static DEFINE_MUTEX(speakup_tty_mutex);
26 static int ser_to_dev(int ser, dev_t *dev_no)
28 if (ser < 0 || ser > (255 - 64)) {
29 pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
33 *dev_no = MKDEV(4, (64 + ser));
37 static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
39 /* use ser only when dev is not specified */
40 if (strcmp(synth->dev_name, SYNTH_DEFAULT_DEV) ||
41 synth->ser == SYNTH_DEFAULT_SER)
42 return tty_dev_name_to_number(synth->dev_name, dev_no);
44 return ser_to_dev(synth->ser, dev_no);
47 static int spk_ttyio_ldisc_open(struct tty_struct *tty)
49 struct spk_ldisc_data *ldisc_data;
51 if (tty != speakup_tty)
52 /* Somebody tried to use this line discipline outside speakup */
58 ldisc_data = kmalloc(sizeof(*ldisc_data), GFP_KERNEL);
62 init_completion(&ldisc_data->completion);
63 ldisc_data->buf_free = true;
64 tty->disc_data = ldisc_data;
69 static void spk_ttyio_ldisc_close(struct tty_struct *tty)
71 kfree(tty->disc_data);
74 static int spk_ttyio_receive_buf2(struct tty_struct *tty,
75 const unsigned char *cp, char *fp, int count)
77 struct spk_ldisc_data *ldisc_data = tty->disc_data;
78 struct spk_synth *synth = ldisc_data->synth;
80 if (synth->read_buff_add) {
83 for (i = 0; i < count; i++)
84 synth->read_buff_add(cp[i]);
89 if (!ldisc_data->buf_free)
90 /* ttyio_in will tty_schedule_flip */
93 /* Make sure the consumer has read buf before we have seen
94 * buf_free == true and overwrite buf
98 ldisc_data->buf = cp[0];
99 ldisc_data->buf_free = false;
100 complete(&ldisc_data->completion);
105 static struct tty_ldisc_ops spk_ttyio_ldisc_ops = {
106 .owner = THIS_MODULE,
107 .magic = TTY_LDISC_MAGIC,
108 .name = "speakup_ldisc",
109 .open = spk_ttyio_ldisc_open,
110 .close = spk_ttyio_ldisc_close,
111 .receive_buf2 = spk_ttyio_receive_buf2,
114 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch);
115 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch);
116 static void spk_ttyio_send_xchar(struct spk_synth *in_synth, char ch);
117 static void spk_ttyio_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear);
118 static unsigned char spk_ttyio_in(struct spk_synth *in_synth);
119 static unsigned char spk_ttyio_in_nowait(struct spk_synth *in_synth);
120 static void spk_ttyio_flush_buffer(struct spk_synth *in_synth);
121 static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth);
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 .wait_for_xmitr = spk_ttyio_wait_for_xmitr,
133 EXPORT_SYMBOL_GPL(spk_ttyio_ops);
135 static inline void get_termios(struct tty_struct *tty,
136 struct ktermios *out_termios)
138 down_read(&tty->termios_rwsem);
139 *out_termios = tty->termios;
140 up_read(&tty->termios_rwsem);
143 static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
146 struct tty_struct *tty;
147 struct ktermios tmp_termios;
150 ret = get_dev_to_use(synth, &dev);
154 tty = tty_kopen_exclusive(dev);
159 ret = tty->ops->open(tty, NULL);
168 clear_bit(TTY_HUPPED, &tty->flags);
169 /* ensure hardware flow control is enabled */
170 get_termios(tty, &tmp_termios);
171 if (!(tmp_termios.c_cflag & CRTSCTS)) {
172 tmp_termios.c_cflag |= CRTSCTS;
173 tty_set_termios(tty, &tmp_termios);
175 * check c_cflag to see if it's updated as tty_set_termios
176 * may not return error even when no tty bits are
177 * changed by the request.
179 get_termios(tty, &tmp_termios);
180 if (!(tmp_termios.c_cflag & CRTSCTS))
181 pr_warn("speakup: Failed to set hardware flow control\n");
186 mutex_lock(&speakup_tty_mutex);
188 ret = tty_set_ldisc(tty, N_SPEAKUP);
190 mutex_unlock(&speakup_tty_mutex);
194 struct spk_ldisc_data *ldisc_data = tty->disc_data;
196 ldisc_data->synth = synth;
201 pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
205 tty->ops->close(tty, NULL);
213 void spk_ttyio_register_ldisc(void)
215 if (tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops))
216 pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
219 void spk_ttyio_unregister_ldisc(void)
221 if (tty_unregister_ldisc(N_SPEAKUP))
222 pr_warn("speakup: Couldn't unregister ldisc\n");
225 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
227 struct tty_struct *tty = in_synth->dev;
230 if (!in_synth->alive || !tty->ops->write)
233 ret = tty->ops->write(tty, &ch, 1);
243 pr_warn("%s: I/O error, deactivating speakup\n",
244 in_synth->long_name);
245 /* No synth any more, so nobody will restart TTYs,
246 * and we thus need to do it ourselves. Now that there
247 * is no synth we can let application flood anyway
250 speakup_start_ttys();
254 static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
259 ret = spk_ttyio_out(in_synth, ch);
260 } else if (ch < 0x800) {
261 ret = spk_ttyio_out(in_synth, 0xc0 | (ch >> 6));
262 ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
264 ret = spk_ttyio_out(in_synth, 0xe0 | (ch >> 12));
265 ret &= spk_ttyio_out(in_synth, 0x80 | ((ch >> 6) & 0x3f));
266 ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
271 static void spk_ttyio_send_xchar(struct spk_synth *in_synth, char ch)
273 struct tty_struct *tty = in_synth->dev;
275 if (tty->ops->send_xchar)
276 tty->ops->send_xchar(tty, ch);
279 static void spk_ttyio_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear)
281 struct tty_struct *tty = in_synth->dev;
283 if (tty->ops->tiocmset)
284 tty->ops->tiocmset(tty, set, clear);
287 static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth)
292 static unsigned char ttyio_in(struct spk_synth *in_synth, int timeout)
294 struct tty_struct *tty = in_synth->dev;
295 struct spk_ldisc_data *ldisc_data = tty->disc_data;
299 if (!try_wait_for_completion(&ldisc_data->completion))
301 } else if (wait_for_completion_timeout(&ldisc_data->completion,
302 usecs_to_jiffies(timeout)) == 0) {
303 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
308 rv = ldisc_data->buf;
309 /* Make sure we have read buf before we set buf_free to let
310 * the producer overwrite it
313 ldisc_data->buf_free = true;
314 /* Let TTY push more characters */
315 tty_schedule_flip(tty->port);
320 static unsigned char spk_ttyio_in(struct spk_synth *in_synth)
322 return ttyio_in(in_synth, SPK_SYNTH_TIMEOUT);
325 static unsigned char spk_ttyio_in_nowait(struct spk_synth *in_synth)
327 u8 rv = ttyio_in(in_synth, 0);
329 return (rv == 0xff) ? 0 : rv;
332 static void spk_ttyio_flush_buffer(struct spk_synth *in_synth)
334 struct tty_struct *tty = in_synth->dev;
336 if (tty->ops->flush_buffer)
337 tty->ops->flush_buffer(tty);
340 int spk_ttyio_synth_probe(struct spk_synth *synth)
342 int rv = spk_ttyio_initialise_ldisc(synth);
351 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
353 void spk_ttyio_release(struct spk_synth *in_synth)
355 struct tty_struct *tty = in_synth->dev;
360 tty->ops->close(tty, NULL);
362 tty_ldisc_flush(tty);
366 in_synth->dev = NULL;
368 EXPORT_SYMBOL_GPL(spk_ttyio_release);
370 const char *spk_ttyio_synth_immediate(struct spk_synth *in_synth, const char *buff)
372 struct tty_struct *tty = in_synth->dev;
375 while ((ch = *buff)) {
377 ch = in_synth->procspeech;
378 if (tty_write_room(tty) < 1 ||
379 !in_synth->io_ops->synth_out(in_synth, ch))
385 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);