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
;
50 if (tty
!= speakup_tty
)
51 /* Somebody tried to use this line discipline outside speakup */
57 ldisc_data
= kmalloc(sizeof(*ldisc_data
), GFP_KERNEL
);
61 init_completion(&ldisc_data
->completion
);
62 ldisc_data
->buf_free
= true;
63 tty
->disc_data
= ldisc_data
;
68 static void spk_ttyio_ldisc_close(struct tty_struct
*tty
)
70 mutex_lock(&speakup_tty_mutex
);
71 kfree(speakup_tty
->disc_data
);
73 mutex_unlock(&speakup_tty_mutex
);
76 static int spk_ttyio_receive_buf2(struct tty_struct
*tty
,
77 const unsigned char *cp
, char *fp
, int count
)
79 struct spk_ldisc_data
*ldisc_data
= tty
->disc_data
;
81 if (spk_ttyio_synth
->read_buff_add
) {
84 for (i
= 0; i
< count
; i
++)
85 spk_ttyio_synth
->read_buff_add(cp
[i
]);
90 if (!ldisc_data
->buf_free
)
91 /* ttyio_in will tty_schedule_flip */
94 /* Make sure the consumer has read buf before we have seen
95 * buf_free == true and overwrite buf
99 ldisc_data
->buf
= cp
[0];
100 ldisc_data
->buf_free
= false;
101 complete(&ldisc_data
->completion
);
106 static struct tty_ldisc_ops spk_ttyio_ldisc_ops
= {
107 .owner
= THIS_MODULE
,
108 .magic
= TTY_LDISC_MAGIC
,
109 .name
= "speakup_ldisc",
110 .open
= spk_ttyio_ldisc_open
,
111 .close
= spk_ttyio_ldisc_close
,
112 .receive_buf2
= spk_ttyio_receive_buf2
,
115 static int spk_ttyio_out(struct spk_synth
*in_synth
, const char ch
);
116 static int spk_ttyio_out_unicode(struct spk_synth
*in_synth
, u16 ch
);
117 static void spk_ttyio_send_xchar(char ch
);
118 static void spk_ttyio_tiocmset(unsigned int set
, unsigned int clear
);
119 static unsigned char spk_ttyio_in(void);
120 static unsigned char spk_ttyio_in_nowait(void);
121 static void spk_ttyio_flush_buffer(void);
122 static int spk_ttyio_wait_for_xmitr(struct spk_synth
*in_synth
);
124 struct spk_io_ops spk_ttyio_ops
= {
125 .synth_out
= spk_ttyio_out
,
126 .synth_out_unicode
= spk_ttyio_out_unicode
,
127 .send_xchar
= spk_ttyio_send_xchar
,
128 .tiocmset
= spk_ttyio_tiocmset
,
129 .synth_in
= spk_ttyio_in
,
130 .synth_in_nowait
= spk_ttyio_in_nowait
,
131 .flush_buffer
= spk_ttyio_flush_buffer
,
132 .wait_for_xmitr
= spk_ttyio_wait_for_xmitr
,
134 EXPORT_SYMBOL_GPL(spk_ttyio_ops
);
136 static inline void get_termios(struct tty_struct
*tty
,
137 struct ktermios
*out_termios
)
139 down_read(&tty
->termios_rwsem
);
140 *out_termios
= tty
->termios
;
141 up_read(&tty
->termios_rwsem
);
144 static int spk_ttyio_initialise_ldisc(struct spk_synth
*synth
)
147 struct tty_struct
*tty
;
148 struct ktermios tmp_termios
;
151 ret
= get_dev_to_use(synth
, &dev
);
155 tty
= tty_kopen(dev
);
160 ret
= tty
->ops
->open(tty
, NULL
);
169 clear_bit(TTY_HUPPED
, &tty
->flags
);
170 /* ensure hardware flow control is enabled */
171 get_termios(tty
, &tmp_termios
);
172 if (!(tmp_termios
.c_cflag
& CRTSCTS
)) {
173 tmp_termios
.c_cflag
|= CRTSCTS
;
174 tty_set_termios(tty
, &tmp_termios
);
176 * check c_cflag to see if it's updated as tty_set_termios
177 * may not return error even when no tty bits are
178 * changed by the request.
180 get_termios(tty
, &tmp_termios
);
181 if (!(tmp_termios
.c_cflag
& CRTSCTS
))
182 pr_warn("speakup: Failed to set hardware flow control\n");
187 mutex_lock(&speakup_tty_mutex
);
189 ret
= tty_set_ldisc(tty
, N_SPEAKUP
);
192 mutex_unlock(&speakup_tty_mutex
);
198 pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
202 tty
->ops
->close(tty
, NULL
);
210 void spk_ttyio_register_ldisc(void)
212 if (tty_register_ldisc(N_SPEAKUP
, &spk_ttyio_ldisc_ops
))
213 pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
216 void spk_ttyio_unregister_ldisc(void)
218 if (tty_unregister_ldisc(N_SPEAKUP
))
219 pr_warn("speakup: Couldn't unregister ldisc\n");
222 static int spk_ttyio_out(struct spk_synth
*in_synth
, const char ch
)
224 mutex_lock(&speakup_tty_mutex
);
225 if (in_synth
->alive
&& speakup_tty
&& speakup_tty
->ops
->write
) {
226 int ret
= speakup_tty
->ops
->write(speakup_tty
, &ch
, 1);
228 mutex_unlock(&speakup_tty_mutex
);
233 pr_warn("%s: I/O error, deactivating speakup\n",
234 in_synth
->long_name
);
235 /* No synth any more, so nobody will restart TTYs,
236 * and we thus need to do it ourselves. Now that there
237 * is no synth we can let application flood anyway
240 speakup_start_ttys();
246 mutex_unlock(&speakup_tty_mutex
);
250 static int spk_ttyio_out_unicode(struct spk_synth
*in_synth
, u16 ch
)
255 ret
= spk_ttyio_out(in_synth
, ch
);
256 } else if (ch
< 0x800) {
257 ret
= spk_ttyio_out(in_synth
, 0xc0 | (ch
>> 6));
258 ret
&= spk_ttyio_out(in_synth
, 0x80 | (ch
& 0x3f));
260 ret
= spk_ttyio_out(in_synth
, 0xe0 | (ch
>> 12));
261 ret
&= spk_ttyio_out(in_synth
, 0x80 | ((ch
>> 6) & 0x3f));
262 ret
&= spk_ttyio_out(in_synth
, 0x80 | (ch
& 0x3f));
267 static int check_tty(struct tty_struct
*tty
)
270 pr_warn("%s: I/O error, deactivating speakup\n",
271 spk_ttyio_synth
->long_name
);
272 /* No synth any more, so nobody will restart TTYs, and we thus
273 * need to do it ourselves. Now that there is no synth we can
274 * let application flood anyway
276 spk_ttyio_synth
->alive
= 0;
277 speakup_start_ttys();
284 static void spk_ttyio_send_xchar(char ch
)
286 mutex_lock(&speakup_tty_mutex
);
287 if (check_tty(speakup_tty
)) {
288 mutex_unlock(&speakup_tty_mutex
);
292 if (speakup_tty
->ops
->send_xchar
)
293 speakup_tty
->ops
->send_xchar(speakup_tty
, ch
);
294 mutex_unlock(&speakup_tty_mutex
);
297 static void spk_ttyio_tiocmset(unsigned int set
, unsigned int clear
)
299 mutex_lock(&speakup_tty_mutex
);
300 if (check_tty(speakup_tty
)) {
301 mutex_unlock(&speakup_tty_mutex
);
305 if (speakup_tty
->ops
->tiocmset
)
306 speakup_tty
->ops
->tiocmset(speakup_tty
, set
, clear
);
307 mutex_unlock(&speakup_tty_mutex
);
310 static int spk_ttyio_wait_for_xmitr(struct spk_synth
*in_synth
)
315 static unsigned char ttyio_in(int timeout
)
317 struct spk_ldisc_data
*ldisc_data
= speakup_tty
->disc_data
;
321 if (!try_wait_for_completion(&ldisc_data
->completion
))
323 } else if (wait_for_completion_timeout(&ldisc_data
->completion
,
324 usecs_to_jiffies(timeout
)) == 0) {
325 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
330 rv
= ldisc_data
->buf
;
331 /* Make sure we have read buf before we set buf_free to let
332 * the producer overwrite it
335 ldisc_data
->buf_free
= true;
336 /* Let TTY push more characters */
337 tty_schedule_flip(speakup_tty
->port
);
342 static unsigned char spk_ttyio_in(void)
344 return ttyio_in(SPK_SYNTH_TIMEOUT
);
347 static unsigned char spk_ttyio_in_nowait(void)
351 return (rv
== 0xff) ? 0 : rv
;
354 static void spk_ttyio_flush_buffer(void)
356 mutex_lock(&speakup_tty_mutex
);
357 if (check_tty(speakup_tty
)) {
358 mutex_unlock(&speakup_tty_mutex
);
362 if (speakup_tty
->ops
->flush_buffer
)
363 speakup_tty
->ops
->flush_buffer(speakup_tty
);
365 mutex_unlock(&speakup_tty_mutex
);
368 int spk_ttyio_synth_probe(struct spk_synth
*synth
)
370 int rv
= spk_ttyio_initialise_ldisc(synth
);
376 spk_ttyio_synth
= synth
;
380 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe
);
382 void spk_ttyio_release(void)
387 tty_lock(speakup_tty
);
389 if (speakup_tty
->ops
->close
)
390 speakup_tty
->ops
->close(speakup_tty
, NULL
);
392 tty_ldisc_flush(speakup_tty
);
393 tty_unlock(speakup_tty
);
394 tty_kclose(speakup_tty
);
396 EXPORT_SYMBOL_GPL(spk_ttyio_release
);
398 const char *spk_ttyio_synth_immediate(struct spk_synth
*synth
, const char *buff
)
402 while ((ch
= *buff
)) {
404 ch
= synth
->procspeech
;
405 if (tty_write_room(speakup_tty
) < 1 ||
406 !synth
->io_ops
->synth_out(synth
, ch
))
412 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate
);