12067fd92SSamuel Thibault // SPDX-License-Identifier: GPL-2.0
22067fd92SSamuel Thibault #include <linux/types.h>
32067fd92SSamuel Thibault #include <linux/tty.h>
42067fd92SSamuel Thibault #include <linux/tty_flip.h>
52067fd92SSamuel Thibault #include <linux/slab.h>
62067fd92SSamuel Thibault
72067fd92SSamuel Thibault #include "speakup.h"
82067fd92SSamuel Thibault #include "spk_types.h"
92067fd92SSamuel Thibault #include "spk_priv.h"
102067fd92SSamuel Thibault
112067fd92SSamuel Thibault struct spk_ldisc_data {
122067fd92SSamuel Thibault char buf;
132067fd92SSamuel Thibault struct completion completion;
142067fd92SSamuel Thibault bool buf_free;
154f2a81f3SSamuel Thibault struct spk_synth *synth;
162067fd92SSamuel Thibault };
172067fd92SSamuel Thibault
184f2a81f3SSamuel Thibault /*
194f2a81f3SSamuel Thibault * This allows to catch within spk_ttyio_ldisc_open whether it is getting set
204f2a81f3SSamuel Thibault * on for a speakup-driven device.
212067fd92SSamuel Thibault */
224f2a81f3SSamuel Thibault static struct tty_struct *speakup_tty;
234f2a81f3SSamuel Thibault /* This mutex serializes the use of such global speakup_tty variable */
242067fd92SSamuel Thibault static DEFINE_MUTEX(speakup_tty_mutex);
252067fd92SSamuel Thibault
ser_to_dev(int ser,dev_t * dev_no)262067fd92SSamuel Thibault static int ser_to_dev(int ser, dev_t *dev_no)
272067fd92SSamuel Thibault {
282067fd92SSamuel Thibault if (ser < 0 || ser > (255 - 64)) {
292067fd92SSamuel Thibault pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
302067fd92SSamuel Thibault return -EINVAL;
312067fd92SSamuel Thibault }
322067fd92SSamuel Thibault
332067fd92SSamuel Thibault *dev_no = MKDEV(4, (64 + ser));
342067fd92SSamuel Thibault return 0;
352067fd92SSamuel Thibault }
362067fd92SSamuel Thibault
get_dev_to_use(struct spk_synth * synth,dev_t * dev_no)372067fd92SSamuel Thibault static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
382067fd92SSamuel Thibault {
392067fd92SSamuel Thibault /* use ser only when dev is not specified */
402067fd92SSamuel Thibault if (strcmp(synth->dev_name, SYNTH_DEFAULT_DEV) ||
412067fd92SSamuel Thibault synth->ser == SYNTH_DEFAULT_SER)
422067fd92SSamuel Thibault return tty_dev_name_to_number(synth->dev_name, dev_no);
432067fd92SSamuel Thibault
442067fd92SSamuel Thibault return ser_to_dev(synth->ser, dev_no);
452067fd92SSamuel Thibault }
462067fd92SSamuel Thibault
spk_ttyio_ldisc_open(struct tty_struct * tty)472067fd92SSamuel Thibault static int spk_ttyio_ldisc_open(struct tty_struct *tty)
482067fd92SSamuel Thibault {
492067fd92SSamuel Thibault struct spk_ldisc_data *ldisc_data;
502067fd92SSamuel Thibault
51f0992098SSamuel Thibault if (tty != speakup_tty)
52f0992098SSamuel Thibault /* Somebody tried to use this line discipline outside speakup */
53f0992098SSamuel Thibault return -ENODEV;
54f0992098SSamuel Thibault
552067fd92SSamuel Thibault if (!tty->ops->write)
562067fd92SSamuel Thibault return -EOPNOTSUPP;
57d4122754SSamuel Thibault
582067fd92SSamuel Thibault ldisc_data = kmalloc(sizeof(*ldisc_data), GFP_KERNEL);
59f0992098SSamuel Thibault if (!ldisc_data)
602067fd92SSamuel Thibault return -ENOMEM;
612067fd92SSamuel Thibault
622067fd92SSamuel Thibault init_completion(&ldisc_data->completion);
632067fd92SSamuel Thibault ldisc_data->buf_free = true;
64f0992098SSamuel Thibault tty->disc_data = ldisc_data;
652067fd92SSamuel Thibault
662067fd92SSamuel Thibault return 0;
672067fd92SSamuel Thibault }
682067fd92SSamuel Thibault
spk_ttyio_ldisc_close(struct tty_struct * tty)692067fd92SSamuel Thibault static void spk_ttyio_ldisc_close(struct tty_struct *tty)
702067fd92SSamuel Thibault {
714f2a81f3SSamuel Thibault kfree(tty->disc_data);
722067fd92SSamuel Thibault }
732067fd92SSamuel Thibault
spk_ttyio_receive_buf2(struct tty_struct * tty,const u8 * cp,const u8 * fp,size_t count)74a8d9cd23SJiri Slaby (SUSE) static size_t spk_ttyio_receive_buf2(struct tty_struct *tty, const u8 *cp,
75*892bc209SJiri Slaby (SUSE) const u8 *fp, size_t count)
762067fd92SSamuel Thibault {
772067fd92SSamuel Thibault struct spk_ldisc_data *ldisc_data = tty->disc_data;
784f2a81f3SSamuel Thibault struct spk_synth *synth = ldisc_data->synth;
792067fd92SSamuel Thibault
804f2a81f3SSamuel Thibault if (synth->read_buff_add) {
816dc6657dSJiri Slaby unsigned int i;
822067fd92SSamuel Thibault
832067fd92SSamuel Thibault for (i = 0; i < count; i++)
844f2a81f3SSamuel Thibault synth->read_buff_add(cp[i]);
852067fd92SSamuel Thibault
862067fd92SSamuel Thibault return count;
872067fd92SSamuel Thibault }
882067fd92SSamuel Thibault
892067fd92SSamuel Thibault if (!ldisc_data->buf_free)
90b68b9144SJiri Slaby /* ttyio_in will tty_flip_buffer_push */
912067fd92SSamuel Thibault return 0;
922067fd92SSamuel Thibault
932067fd92SSamuel Thibault /* Make sure the consumer has read buf before we have seen
942067fd92SSamuel Thibault * buf_free == true and overwrite buf
952067fd92SSamuel Thibault */
962067fd92SSamuel Thibault mb();
972067fd92SSamuel Thibault
982067fd92SSamuel Thibault ldisc_data->buf = cp[0];
992067fd92SSamuel Thibault ldisc_data->buf_free = false;
1002067fd92SSamuel Thibault complete(&ldisc_data->completion);
1012067fd92SSamuel Thibault
1022067fd92SSamuel Thibault return 1;
1032067fd92SSamuel Thibault }
1042067fd92SSamuel Thibault
1052067fd92SSamuel Thibault static struct tty_ldisc_ops spk_ttyio_ldisc_ops = {
1062067fd92SSamuel Thibault .owner = THIS_MODULE,
107fbadf70aSJiri Slaby .num = N_SPEAKUP,
1082067fd92SSamuel Thibault .name = "speakup_ldisc",
1092067fd92SSamuel Thibault .open = spk_ttyio_ldisc_open,
1102067fd92SSamuel Thibault .close = spk_ttyio_ldisc_close,
1112067fd92SSamuel Thibault .receive_buf2 = spk_ttyio_receive_buf2,
1122067fd92SSamuel Thibault };
1132067fd92SSamuel Thibault
1142067fd92SSamuel Thibault static int spk_ttyio_out(struct spk_synth *in_synth, const char ch);
1152067fd92SSamuel Thibault static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch);
1161941ab1dSSamuel Thibault static void spk_ttyio_send_xchar(struct spk_synth *in_synth, char ch);
1171941ab1dSSamuel Thibault static void spk_ttyio_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear);
1181941ab1dSSamuel Thibault static unsigned char spk_ttyio_in(struct spk_synth *in_synth);
1191941ab1dSSamuel Thibault static unsigned char spk_ttyio_in_nowait(struct spk_synth *in_synth);
1201941ab1dSSamuel Thibault static void spk_ttyio_flush_buffer(struct spk_synth *in_synth);
1212b86d9b8SSamuel Thibault static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth);
1222067fd92SSamuel Thibault
1232067fd92SSamuel Thibault struct spk_io_ops spk_ttyio_ops = {
1242067fd92SSamuel Thibault .synth_out = spk_ttyio_out,
1252067fd92SSamuel Thibault .synth_out_unicode = spk_ttyio_out_unicode,
1262067fd92SSamuel Thibault .send_xchar = spk_ttyio_send_xchar,
1272067fd92SSamuel Thibault .tiocmset = spk_ttyio_tiocmset,
1282067fd92SSamuel Thibault .synth_in = spk_ttyio_in,
1292067fd92SSamuel Thibault .synth_in_nowait = spk_ttyio_in_nowait,
1302067fd92SSamuel Thibault .flush_buffer = spk_ttyio_flush_buffer,
1312b86d9b8SSamuel Thibault .wait_for_xmitr = spk_ttyio_wait_for_xmitr,
1322067fd92SSamuel Thibault };
1332067fd92SSamuel Thibault EXPORT_SYMBOL_GPL(spk_ttyio_ops);
1342067fd92SSamuel Thibault
get_termios(struct tty_struct * tty,struct ktermios * out_termios)1352067fd92SSamuel Thibault static inline void get_termios(struct tty_struct *tty,
1362067fd92SSamuel Thibault struct ktermios *out_termios)
1372067fd92SSamuel Thibault {
1382067fd92SSamuel Thibault down_read(&tty->termios_rwsem);
1392067fd92SSamuel Thibault *out_termios = tty->termios;
1402067fd92SSamuel Thibault up_read(&tty->termios_rwsem);
1412067fd92SSamuel Thibault }
1422067fd92SSamuel Thibault
spk_ttyio_initialise_ldisc(struct spk_synth * synth)1432067fd92SSamuel Thibault static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
1442067fd92SSamuel Thibault {
1452067fd92SSamuel Thibault int ret = 0;
1462067fd92SSamuel Thibault struct tty_struct *tty;
1472067fd92SSamuel Thibault struct ktermios tmp_termios;
1482067fd92SSamuel Thibault dev_t dev;
1492067fd92SSamuel Thibault
1502067fd92SSamuel Thibault ret = get_dev_to_use(synth, &dev);
1512067fd92SSamuel Thibault if (ret)
1522067fd92SSamuel Thibault return ret;
1532067fd92SSamuel Thibault
1544ea3cd65SUwe Kleine-König tty = tty_kopen_exclusive(dev);
1552067fd92SSamuel Thibault if (IS_ERR(tty))
1562067fd92SSamuel Thibault return PTR_ERR(tty);
1572067fd92SSamuel Thibault
1582067fd92SSamuel Thibault if (tty->ops->open)
1592067fd92SSamuel Thibault ret = tty->ops->open(tty, NULL);
1602067fd92SSamuel Thibault else
1612067fd92SSamuel Thibault ret = -ENODEV;
1622067fd92SSamuel Thibault
1632067fd92SSamuel Thibault if (ret) {
1642067fd92SSamuel Thibault tty_unlock(tty);
1652067fd92SSamuel Thibault return ret;
1662067fd92SSamuel Thibault }
1672067fd92SSamuel Thibault
1682067fd92SSamuel Thibault clear_bit(TTY_HUPPED, &tty->flags);
1692067fd92SSamuel Thibault /* ensure hardware flow control is enabled */
1702067fd92SSamuel Thibault get_termios(tty, &tmp_termios);
1712067fd92SSamuel Thibault if (!(tmp_termios.c_cflag & CRTSCTS)) {
1722067fd92SSamuel Thibault tmp_termios.c_cflag |= CRTSCTS;
1732067fd92SSamuel Thibault tty_set_termios(tty, &tmp_termios);
1742067fd92SSamuel Thibault /*
1752067fd92SSamuel Thibault * check c_cflag to see if it's updated as tty_set_termios
1762067fd92SSamuel Thibault * may not return error even when no tty bits are
1772067fd92SSamuel Thibault * changed by the request.
1782067fd92SSamuel Thibault */
1792067fd92SSamuel Thibault get_termios(tty, &tmp_termios);
1802067fd92SSamuel Thibault if (!(tmp_termios.c_cflag & CRTSCTS))
1812067fd92SSamuel Thibault pr_warn("speakup: Failed to set hardware flow control\n");
1822067fd92SSamuel Thibault }
1832067fd92SSamuel Thibault
1842067fd92SSamuel Thibault tty_unlock(tty);
1852067fd92SSamuel Thibault
186f0992098SSamuel Thibault mutex_lock(&speakup_tty_mutex);
187f0992098SSamuel Thibault speakup_tty = tty;
1882067fd92SSamuel Thibault ret = tty_set_ldisc(tty, N_SPEAKUP);
189f0992098SSamuel Thibault speakup_tty = NULL;
190f0992098SSamuel Thibault mutex_unlock(&speakup_tty_mutex);
191f0992098SSamuel Thibault
1924f2a81f3SSamuel Thibault if (!ret) {
193f0992098SSamuel Thibault /* Success */
1944f2a81f3SSamuel Thibault struct spk_ldisc_data *ldisc_data = tty->disc_data;
1954f2a81f3SSamuel Thibault
1964f2a81f3SSamuel Thibault ldisc_data->synth = synth;
1974f2a81f3SSamuel Thibault synth->dev = tty;
198f0992098SSamuel Thibault return 0;
1994f2a81f3SSamuel Thibault }
200f0992098SSamuel Thibault
2012067fd92SSamuel Thibault pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
2022067fd92SSamuel Thibault
203f0992098SSamuel Thibault tty_lock(tty);
204f0992098SSamuel Thibault if (tty->ops->close)
205f0992098SSamuel Thibault tty->ops->close(tty, NULL);
206f0992098SSamuel Thibault tty_unlock(tty);
207f0992098SSamuel Thibault
208f0992098SSamuel Thibault tty_kclose(tty);
209f0992098SSamuel Thibault
2102067fd92SSamuel Thibault return ret;
2112067fd92SSamuel Thibault }
2122067fd92SSamuel Thibault
spk_ttyio_register_ldisc(void)2132067fd92SSamuel Thibault void spk_ttyio_register_ldisc(void)
2142067fd92SSamuel Thibault {
215fbadf70aSJiri Slaby if (tty_register_ldisc(&spk_ttyio_ldisc_ops))
2162067fd92SSamuel Thibault pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
2172067fd92SSamuel Thibault }
2182067fd92SSamuel Thibault
spk_ttyio_unregister_ldisc(void)2192067fd92SSamuel Thibault void spk_ttyio_unregister_ldisc(void)
2202067fd92SSamuel Thibault {
221357a6a87SJiri Slaby tty_unregister_ldisc(&spk_ttyio_ldisc_ops);
2222067fd92SSamuel Thibault }
2232067fd92SSamuel Thibault
spk_ttyio_out(struct spk_synth * in_synth,const char ch)2242067fd92SSamuel Thibault static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
2252067fd92SSamuel Thibault {
2264f2a81f3SSamuel Thibault struct tty_struct *tty = in_synth->dev;
22711742252SSamuel Thibault int ret;
2282067fd92SSamuel Thibault
22911742252SSamuel Thibault if (!in_synth->alive || !tty->ops->write)
23011742252SSamuel Thibault return 0;
23111742252SSamuel Thibault
23211742252SSamuel Thibault ret = tty->ops->write(tty, &ch, 1);
2334f2a81f3SSamuel Thibault
2342067fd92SSamuel Thibault if (ret == 0)
2352067fd92SSamuel Thibault /* No room */
2362067fd92SSamuel Thibault return 0;
23711742252SSamuel Thibault
23811742252SSamuel Thibault if (ret > 0)
23911742252SSamuel Thibault /* Success */
24011742252SSamuel Thibault return 1;
24111742252SSamuel Thibault
2422067fd92SSamuel Thibault pr_warn("%s: I/O error, deactivating speakup\n",
2432067fd92SSamuel Thibault in_synth->long_name);
2442067fd92SSamuel Thibault /* No synth any more, so nobody will restart TTYs,
2452067fd92SSamuel Thibault * and we thus need to do it ourselves. Now that there
2462067fd92SSamuel Thibault * is no synth we can let application flood anyway
2472067fd92SSamuel Thibault */
2482067fd92SSamuel Thibault in_synth->alive = 0;
2492067fd92SSamuel Thibault speakup_start_ttys();
2502067fd92SSamuel Thibault return 0;
2512067fd92SSamuel Thibault }
2522067fd92SSamuel Thibault
spk_ttyio_out_unicode(struct spk_synth * in_synth,u16 ch)2532067fd92SSamuel Thibault static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
2542067fd92SSamuel Thibault {
2552067fd92SSamuel Thibault int ret;
2562067fd92SSamuel Thibault
2572067fd92SSamuel Thibault if (ch < 0x80) {
2582067fd92SSamuel Thibault ret = spk_ttyio_out(in_synth, ch);
2592067fd92SSamuel Thibault } else if (ch < 0x800) {
2602067fd92SSamuel Thibault ret = spk_ttyio_out(in_synth, 0xc0 | (ch >> 6));
2612067fd92SSamuel Thibault ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
2622067fd92SSamuel Thibault } else {
2632067fd92SSamuel Thibault ret = spk_ttyio_out(in_synth, 0xe0 | (ch >> 12));
2642067fd92SSamuel Thibault ret &= spk_ttyio_out(in_synth, 0x80 | ((ch >> 6) & 0x3f));
2652067fd92SSamuel Thibault ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
2662067fd92SSamuel Thibault }
2672067fd92SSamuel Thibault return ret;
2682067fd92SSamuel Thibault }
2692067fd92SSamuel Thibault
spk_ttyio_send_xchar(struct spk_synth * in_synth,char ch)2701941ab1dSSamuel Thibault static void spk_ttyio_send_xchar(struct spk_synth *in_synth, char ch)
2712067fd92SSamuel Thibault {
2724f2a81f3SSamuel Thibault struct tty_struct *tty = in_synth->dev;
2732067fd92SSamuel Thibault
2744f2a81f3SSamuel Thibault if (tty->ops->send_xchar)
2754f2a81f3SSamuel Thibault tty->ops->send_xchar(tty, ch);
2762067fd92SSamuel Thibault }
2772067fd92SSamuel Thibault
spk_ttyio_tiocmset(struct spk_synth * in_synth,unsigned int set,unsigned int clear)2781941ab1dSSamuel Thibault static void spk_ttyio_tiocmset(struct spk_synth *in_synth, unsigned int set, unsigned int clear)
2792067fd92SSamuel Thibault {
2804f2a81f3SSamuel Thibault struct tty_struct *tty = in_synth->dev;
2812067fd92SSamuel Thibault
2824f2a81f3SSamuel Thibault if (tty->ops->tiocmset)
2834f2a81f3SSamuel Thibault tty->ops->tiocmset(tty, set, clear);
2842067fd92SSamuel Thibault }
2852067fd92SSamuel Thibault
spk_ttyio_wait_for_xmitr(struct spk_synth * in_synth)2862b86d9b8SSamuel Thibault static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth)
2872b86d9b8SSamuel Thibault {
2882b86d9b8SSamuel Thibault return 1;
2892b86d9b8SSamuel Thibault }
2902b86d9b8SSamuel Thibault
ttyio_in(struct spk_synth * in_synth,int timeout)2911941ab1dSSamuel Thibault static unsigned char ttyio_in(struct spk_synth *in_synth, int timeout)
2922067fd92SSamuel Thibault {
2934f2a81f3SSamuel Thibault struct tty_struct *tty = in_synth->dev;
2944f2a81f3SSamuel Thibault struct spk_ldisc_data *ldisc_data = tty->disc_data;
2952067fd92SSamuel Thibault char rv;
2962067fd92SSamuel Thibault
2973ed1cfb2SSamuel Thibault if (!timeout) {
2983ed1cfb2SSamuel Thibault if (!try_wait_for_completion(&ldisc_data->completion))
2993ed1cfb2SSamuel Thibault return 0xff;
3003ed1cfb2SSamuel Thibault } else if (wait_for_completion_timeout(&ldisc_data->completion,
3012067fd92SSamuel Thibault usecs_to_jiffies(timeout)) == 0) {
3022067fd92SSamuel Thibault pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
3032067fd92SSamuel Thibault timeout);
3042067fd92SSamuel Thibault return 0xff;
3052067fd92SSamuel Thibault }
3062067fd92SSamuel Thibault
3072067fd92SSamuel Thibault rv = ldisc_data->buf;
3082067fd92SSamuel Thibault /* Make sure we have read buf before we set buf_free to let
3092067fd92SSamuel Thibault * the producer overwrite it
3102067fd92SSamuel Thibault */
3112067fd92SSamuel Thibault mb();
3122067fd92SSamuel Thibault ldisc_data->buf_free = true;
3132067fd92SSamuel Thibault /* Let TTY push more characters */
314b68b9144SJiri Slaby tty_flip_buffer_push(tty->port);
3152067fd92SSamuel Thibault
3162067fd92SSamuel Thibault return rv;
3172067fd92SSamuel Thibault }
3182067fd92SSamuel Thibault
spk_ttyio_in(struct spk_synth * in_synth)3191941ab1dSSamuel Thibault static unsigned char spk_ttyio_in(struct spk_synth *in_synth)
3202067fd92SSamuel Thibault {
3211941ab1dSSamuel Thibault return ttyio_in(in_synth, SPK_SYNTH_TIMEOUT);
3222067fd92SSamuel Thibault }
3232067fd92SSamuel Thibault
spk_ttyio_in_nowait(struct spk_synth * in_synth)3241941ab1dSSamuel Thibault static unsigned char spk_ttyio_in_nowait(struct spk_synth *in_synth)
3252067fd92SSamuel Thibault {
3261941ab1dSSamuel Thibault u8 rv = ttyio_in(in_synth, 0);
3272067fd92SSamuel Thibault
3282067fd92SSamuel Thibault return (rv == 0xff) ? 0 : rv;
3292067fd92SSamuel Thibault }
3302067fd92SSamuel Thibault
spk_ttyio_flush_buffer(struct spk_synth * in_synth)3311941ab1dSSamuel Thibault static void spk_ttyio_flush_buffer(struct spk_synth *in_synth)
3322067fd92SSamuel Thibault {
3334f2a81f3SSamuel Thibault struct tty_struct *tty = in_synth->dev;
3342067fd92SSamuel Thibault
3354f2a81f3SSamuel Thibault if (tty->ops->flush_buffer)
3364f2a81f3SSamuel Thibault tty->ops->flush_buffer(tty);
3372067fd92SSamuel Thibault }
3382067fd92SSamuel Thibault
spk_ttyio_synth_probe(struct spk_synth * synth)3392067fd92SSamuel Thibault int spk_ttyio_synth_probe(struct spk_synth *synth)
3402067fd92SSamuel Thibault {
3412067fd92SSamuel Thibault int rv = spk_ttyio_initialise_ldisc(synth);
3422067fd92SSamuel Thibault
3432067fd92SSamuel Thibault if (rv)
3442067fd92SSamuel Thibault return rv;
3452067fd92SSamuel Thibault
3462067fd92SSamuel Thibault synth->alive = 1;
3472067fd92SSamuel Thibault
3482067fd92SSamuel Thibault return 0;
3492067fd92SSamuel Thibault }
3502067fd92SSamuel Thibault EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
3512067fd92SSamuel Thibault
spk_ttyio_release(struct spk_synth * in_synth)3521941ab1dSSamuel Thibault void spk_ttyio_release(struct spk_synth *in_synth)
3532067fd92SSamuel Thibault {
3544f2a81f3SSamuel Thibault struct tty_struct *tty = in_synth->dev;
3552067fd92SSamuel Thibault
3565abbeebdSGaosheng Cui if (tty == NULL)
3575abbeebdSGaosheng Cui return;
3585abbeebdSGaosheng Cui
3594f2a81f3SSamuel Thibault tty_lock(tty);
3602067fd92SSamuel Thibault
3614f2a81f3SSamuel Thibault if (tty->ops->close)
3624f2a81f3SSamuel Thibault tty->ops->close(tty, NULL);
3632067fd92SSamuel Thibault
3644f2a81f3SSamuel Thibault tty_ldisc_flush(tty);
3654f2a81f3SSamuel Thibault tty_unlock(tty);
3664f2a81f3SSamuel Thibault tty_kclose(tty);
3674f2a81f3SSamuel Thibault
3684f2a81f3SSamuel Thibault in_synth->dev = NULL;
3692067fd92SSamuel Thibault }
3702067fd92SSamuel Thibault EXPORT_SYMBOL_GPL(spk_ttyio_release);
3712067fd92SSamuel Thibault
spk_ttyio_synth_immediate(struct spk_synth * in_synth,const char * buff)3721941ab1dSSamuel Thibault const char *spk_ttyio_synth_immediate(struct spk_synth *in_synth, const char *buff)
3732067fd92SSamuel Thibault {
3744f2a81f3SSamuel Thibault struct tty_struct *tty = in_synth->dev;
3752067fd92SSamuel Thibault u_char ch;
3762067fd92SSamuel Thibault
3772067fd92SSamuel Thibault while ((ch = *buff)) {
3782067fd92SSamuel Thibault if (ch == '\n')
3791941ab1dSSamuel Thibault ch = in_synth->procspeech;
3804f2a81f3SSamuel Thibault if (tty_write_room(tty) < 1 ||
3811941ab1dSSamuel Thibault !in_synth->io_ops->synth_out(in_synth, ch))
3822067fd92SSamuel Thibault return buff;
3832067fd92SSamuel Thibault buff++;
3842067fd92SSamuel Thibault }
3852067fd92SSamuel Thibault return NULL;
3862067fd92SSamuel Thibault }
3872067fd92SSamuel Thibault EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);
388