1 #include <linux/types.h>
3 #include <linux/tty_flip.h>
4 #include <linux/slab.h>
10 struct spk_ldisc_data {
16 static struct spk_synth *spk_ttyio_synth;
17 static struct tty_struct *speakup_tty;
18 /* mutex to protect against speakup_tty disappearing from underneath us while
19 * we are using it. this can happen when the device physically unplugged,
20 * while in use. it also serialises access to speakup_tty.
22 static DEFINE_MUTEX(speakup_tty_mutex);
24 static int ser_to_dev(int ser, dev_t *dev_no)
26 if (ser < 0 || ser > (255 - 64)) {
27 pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
31 *dev_no = MKDEV(4, (64 + ser));
35 static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
37 /* use ser only when dev is not specified */
38 if (strcmp(synth->dev_name, SYNTH_DEFAULT_DEV) ||
39 synth->ser == SYNTH_DEFAULT_SER)
40 return tty_dev_name_to_number(synth->dev_name, dev_no);
42 return ser_to_dev(synth->ser, dev_no);
45 static int spk_ttyio_ldisc_open(struct tty_struct *tty)
47 struct spk_ldisc_data *ldisc_data;
49 if (tty != speakup_tty)
50 /* Somebody tried to use this line discipline outside speakup */
53 if (tty->ops->write == NULL)
56 ldisc_data = kmalloc(sizeof(struct spk_ldisc_data), GFP_KERNEL);
60 sema_init(&ldisc_data->sem, 0);
61 ldisc_data->buf_free = true;
62 tty->disc_data = ldisc_data;
67 static void spk_ttyio_ldisc_close(struct tty_struct *tty)
69 mutex_lock(&speakup_tty_mutex);
70 kfree(speakup_tty->disc_data);
72 mutex_unlock(&speakup_tty_mutex);
75 static int spk_ttyio_receive_buf2(struct tty_struct *tty,
76 const unsigned char *cp, char *fp, int count)
78 struct spk_ldisc_data *ldisc_data = tty->disc_data;
80 if (spk_ttyio_synth->read_buff_add) {
83 for (i = 0; i < count; i++)
84 spk_ttyio_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 */
97 ldisc_data->buf = cp[0];
98 ldisc_data->buf_free = false;
104 static struct tty_ldisc_ops spk_ttyio_ldisc_ops = {
105 .owner = THIS_MODULE,
106 .magic = TTY_LDISC_MAGIC,
107 .name = "speakup_ldisc",
108 .open = spk_ttyio_ldisc_open,
109 .close = spk_ttyio_ldisc_close,
110 .receive_buf2 = spk_ttyio_receive_buf2,
113 static int spk_ttyio_out(struct spk_synth *in_synth, const char 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 .send_xchar = spk_ttyio_send_xchar,
123 .tiocmset = spk_ttyio_tiocmset,
124 .synth_in = spk_ttyio_in,
125 .synth_in_nowait = spk_ttyio_in_nowait,
126 .flush_buffer = spk_ttyio_flush_buffer,
128 EXPORT_SYMBOL_GPL(spk_ttyio_ops);
130 static inline void get_termios(struct tty_struct *tty, struct ktermios *out_termios)
132 down_read(&tty->termios_rwsem);
133 *out_termios = tty->termios;
134 up_read(&tty->termios_rwsem);
137 static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
140 struct tty_struct *tty;
141 struct ktermios tmp_termios;
144 ret = get_dev_to_use(synth, &dev);
148 tty = tty_kopen(dev);
153 ret = tty->ops->open(tty, NULL);
162 clear_bit(TTY_HUPPED, &tty->flags);
163 /* ensure hardware flow control is enabled */
164 get_termios(tty, &tmp_termios);
165 if (!(tmp_termios.c_cflag & CRTSCTS)) {
166 tmp_termios.c_cflag |= CRTSCTS;
167 tty_set_termios(tty, &tmp_termios);
169 * check c_cflag to see if it's updated as tty_set_termios may not return
170 * error even when no tty bits are changed by the request.
172 get_termios(tty, &tmp_termios);
173 if (!(tmp_termios.c_cflag & CRTSCTS))
174 pr_warn("speakup: Failed to set hardware flow control\n");
179 mutex_lock(&speakup_tty_mutex);
181 ret = tty_set_ldisc(tty, N_SPEAKUP);
184 mutex_unlock(&speakup_tty_mutex);
190 pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
194 tty->ops->close(tty, NULL);
202 void spk_ttyio_register_ldisc(void)
204 if (tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops))
205 pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
208 void spk_ttyio_unregister_ldisc(void)
210 if (tty_unregister_ldisc(N_SPEAKUP))
211 pr_warn("speakup: Couldn't unregister ldisc\n");
214 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
216 mutex_lock(&speakup_tty_mutex);
217 if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
218 int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
220 mutex_unlock(&speakup_tty_mutex);
225 pr_warn("%s: I/O error, deactivating speakup\n", in_synth->long_name);
226 /* No synth any more, so nobody will restart TTYs, and we thus
227 * need to do it ourselves. Now that there is no synth we can
228 * let application flood anyway
231 speakup_start_ttys();
237 mutex_unlock(&speakup_tty_mutex);
241 static int check_tty(struct tty_struct *tty)
244 pr_warn("%s: I/O error, deactivating speakup\n",
245 spk_ttyio_synth->long_name);
246 /* No synth any more, so nobody will restart TTYs, and we thus
247 * need to do it ourselves. Now that there is no synth we can
248 * let application flood anyway
250 spk_ttyio_synth->alive = 0;
251 speakup_start_ttys();
258 static void spk_ttyio_send_xchar(char ch)
260 mutex_lock(&speakup_tty_mutex);
261 if (check_tty(speakup_tty)) {
262 mutex_unlock(&speakup_tty_mutex);
266 if (speakup_tty->ops->send_xchar)
267 speakup_tty->ops->send_xchar(speakup_tty, ch);
268 mutex_unlock(&speakup_tty_mutex);
271 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
273 mutex_lock(&speakup_tty_mutex);
274 if (check_tty(speakup_tty)) {
275 mutex_unlock(&speakup_tty_mutex);
279 if (speakup_tty->ops->tiocmset)
280 speakup_tty->ops->tiocmset(speakup_tty, set, clear);
281 mutex_unlock(&speakup_tty_mutex);
284 static unsigned char ttyio_in(int timeout)
286 struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
289 if (down_timeout(&ldisc_data->sem, usecs_to_jiffies(timeout)) == -ETIME) {
291 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
296 rv = ldisc_data->buf;
297 /* Make sure we have read buf before we set buf_free to let
298 * the producer overwrite it */
300 ldisc_data->buf_free = true;
301 /* Let TTY push more characters */
302 tty_schedule_flip(speakup_tty->port);
307 static unsigned char spk_ttyio_in(void)
309 return ttyio_in(SPK_SYNTH_TIMEOUT);
312 static unsigned char spk_ttyio_in_nowait(void)
316 return (rv == 0xff) ? 0 : rv;
319 static void spk_ttyio_flush_buffer(void)
321 mutex_lock(&speakup_tty_mutex);
322 if (check_tty(speakup_tty)) {
323 mutex_unlock(&speakup_tty_mutex);
327 if (speakup_tty->ops->flush_buffer)
328 speakup_tty->ops->flush_buffer(speakup_tty);
330 mutex_unlock(&speakup_tty_mutex);
333 int spk_ttyio_synth_probe(struct spk_synth *synth)
335 int rv = spk_ttyio_initialise_ldisc(synth);
341 spk_ttyio_synth = synth;
345 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
347 void spk_ttyio_release(void)
352 tty_lock(speakup_tty);
354 if (speakup_tty->ops->close)
355 speakup_tty->ops->close(speakup_tty, NULL);
357 tty_ldisc_flush(speakup_tty);
358 tty_unlock(speakup_tty);
359 tty_kclose(speakup_tty);
361 EXPORT_SYMBOL_GPL(spk_ttyio_release);
363 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
367 while ((ch = *buff)) {
369 ch = synth->procspeech;
370 if (tty_write_room(speakup_tty) < 1 || !synth->io_ops->synth_out(synth, ch))
376 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);