Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "speakup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "spk_types.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "spk_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) struct spk_ldisc_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 	char buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	struct completion completion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	bool buf_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static struct spk_synth *spk_ttyio_synth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static struct tty_struct *speakup_tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /* mutex to protect against speakup_tty disappearing from underneath us while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * we are using it. this can happen when the device physically unplugged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * while in use. it also serialises access to speakup_tty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static DEFINE_MUTEX(speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static int ser_to_dev(int ser, dev_t *dev_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	if (ser < 0 || ser > (255 - 64)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	*dev_no = MKDEV(4, (64 + ser));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	/* use ser only when dev is not specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (strcmp(synth->dev_name, SYNTH_DEFAULT_DEV) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	    synth->ser == SYNTH_DEFAULT_SER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		return tty_dev_name_to_number(synth->dev_name, dev_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	return ser_to_dev(synth->ser, dev_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int spk_ttyio_ldisc_open(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct spk_ldisc_data *ldisc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (tty != speakup_tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		/* Somebody tried to use this line discipline outside speakup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (!tty->ops->write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	ldisc_data = kmalloc(sizeof(*ldisc_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (!ldisc_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	init_completion(&ldisc_data->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ldisc_data->buf_free = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	tty->disc_data = ldisc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void spk_ttyio_ldisc_close(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	mutex_lock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	kfree(speakup_tty->disc_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	speakup_tty = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static int spk_ttyio_receive_buf2(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				  const unsigned char *cp, char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct spk_ldisc_data *ldisc_data = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (spk_ttyio_synth->read_buff_add) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			spk_ttyio_synth->read_buff_add(cp[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (!ldisc_data->buf_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		/* ttyio_in will tty_schedule_flip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/* Make sure the consumer has read buf before we have seen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * buf_free == true and overwrite buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	ldisc_data->buf = cp[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	ldisc_data->buf_free = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	complete(&ldisc_data->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static struct tty_ldisc_ops spk_ttyio_ldisc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	.owner          = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	.magic          = TTY_LDISC_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	.name           = "speakup_ldisc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	.open           = spk_ttyio_ldisc_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	.close          = spk_ttyio_ldisc_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	.receive_buf2	= spk_ttyio_receive_buf2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int spk_ttyio_out(struct spk_synth *in_synth, const char ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static void spk_ttyio_send_xchar(char ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static unsigned char spk_ttyio_in(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static unsigned char spk_ttyio_in_nowait(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void spk_ttyio_flush_buffer(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct spk_io_ops spk_ttyio_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.synth_out = spk_ttyio_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	.synth_out_unicode = spk_ttyio_out_unicode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	.send_xchar = spk_ttyio_send_xchar,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	.tiocmset = spk_ttyio_tiocmset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	.synth_in = spk_ttyio_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	.synth_in_nowait = spk_ttyio_in_nowait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	.flush_buffer = spk_ttyio_flush_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	.wait_for_xmitr = spk_ttyio_wait_for_xmitr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) EXPORT_SYMBOL_GPL(spk_ttyio_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static inline void get_termios(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			       struct ktermios *out_termios)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	down_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	*out_termios = tty->termios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct tty_struct *tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	struct ktermios tmp_termios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	dev_t dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	ret = get_dev_to_use(synth, &dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	tty = tty_kopen(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (IS_ERR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return PTR_ERR(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (tty->ops->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		ret = tty->ops->open(tty, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		tty_unlock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	clear_bit(TTY_HUPPED, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	/* ensure hardware flow control is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	get_termios(tty, &tmp_termios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (!(tmp_termios.c_cflag & CRTSCTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		tmp_termios.c_cflag |= CRTSCTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		tty_set_termios(tty, &tmp_termios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		 * check c_cflag to see if it's updated as tty_set_termios
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		 * may not return error even when no tty bits are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		 * changed by the request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		get_termios(tty, &tmp_termios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (!(tmp_termios.c_cflag & CRTSCTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			pr_warn("speakup: Failed to set hardware flow control\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	tty_unlock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	mutex_lock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	speakup_tty = tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	ret = tty_set_ldisc(tty, N_SPEAKUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		speakup_tty = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		/* Success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	tty_lock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (tty->ops->close)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		tty->ops->close(tty, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	tty_unlock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	tty_kclose(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) void spk_ttyio_register_ldisc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) void spk_ttyio_unregister_ldisc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (tty_unregister_ldisc(N_SPEAKUP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		pr_warn("speakup: Couldn't unregister ldisc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	mutex_lock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			/* No room */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			pr_warn("%s: I/O error, deactivating speakup\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				in_synth->long_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			/* No synth any more, so nobody will restart TTYs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			 * and we thus need to do it ourselves.  Now that there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			 * is no synth we can let application flood anyway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			in_synth->alive = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			speakup_start_ttys();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static int spk_ttyio_out_unicode(struct spk_synth *in_synth, u16 ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (ch < 0x80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		ret = spk_ttyio_out(in_synth, ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	} else if (ch < 0x800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		ret  = spk_ttyio_out(in_synth, 0xc0 | (ch >> 6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		ret  = spk_ttyio_out(in_synth, 0xe0 | (ch >> 12));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		ret &= spk_ttyio_out(in_synth, 0x80 | ((ch >> 6) & 0x3f));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		ret &= spk_ttyio_out(in_synth, 0x80 | (ch & 0x3f));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static int check_tty(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (!tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		pr_warn("%s: I/O error, deactivating speakup\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			spk_ttyio_synth->long_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		/* No synth any more, so nobody will restart TTYs, and we thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		 * need to do it ourselves.  Now that there is no synth we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		 * let application flood anyway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		spk_ttyio_synth->alive = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		speakup_start_ttys();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static void spk_ttyio_send_xchar(char ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	mutex_lock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (check_tty(speakup_tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (speakup_tty->ops->send_xchar)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		speakup_tty->ops->send_xchar(speakup_tty, ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	mutex_lock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (check_tty(speakup_tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (speakup_tty->ops->tiocmset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		speakup_tty->ops->tiocmset(speakup_tty, set, clear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int spk_ttyio_wait_for_xmitr(struct spk_synth *in_synth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static unsigned char ttyio_in(int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	char rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (!timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		if (!try_wait_for_completion(&ldisc_data->completion))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			return 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	} else if (wait_for_completion_timeout(&ldisc_data->completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 					usecs_to_jiffies(timeout)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		pr_warn("spk_ttyio: timeout (%d)  while waiting for input\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	rv = ldisc_data->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	/* Make sure we have read buf before we set buf_free to let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	 * the producer overwrite it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	ldisc_data->buf_free = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	/* Let TTY push more characters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	tty_schedule_flip(speakup_tty->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static unsigned char spk_ttyio_in(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	return ttyio_in(SPK_SYNTH_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static unsigned char spk_ttyio_in_nowait(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	u8 rv = ttyio_in(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return (rv == 0xff) ? 0 : rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static void spk_ttyio_flush_buffer(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	mutex_lock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (check_tty(speakup_tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (speakup_tty->ops->flush_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		speakup_tty->ops->flush_buffer(speakup_tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	mutex_unlock(&speakup_tty_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int spk_ttyio_synth_probe(struct spk_synth *synth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	int rv = spk_ttyio_initialise_ldisc(synth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	synth->alive = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	spk_ttyio_synth = synth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) void spk_ttyio_release(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (!speakup_tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	tty_lock(speakup_tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (speakup_tty->ops->close)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		speakup_tty->ops->close(speakup_tty, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	tty_ldisc_flush(speakup_tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	tty_unlock(speakup_tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	tty_kclose(speakup_tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) EXPORT_SYMBOL_GPL(spk_ttyio_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	u_char ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	while ((ch = *buff)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		if (ch == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			ch = synth->procspeech;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		if (tty_write_room(speakup_tty) < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		    !synth->io_ops->synth_out(synth, ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			return buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		buff++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);