1 #include <linux/types.h>
3 #include <linux/tty_flip.h>
4 #include <linux/slab.h>
10 #define DEV_PREFIX_LP "lp"
12 static const char * const lp_supported[] = { "acntsa", "bns", "dummy",
15 struct spk_ldisc_data {
21 static struct spk_synth *spk_ttyio_synth;
22 static struct tty_struct *speakup_tty;
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 /* for /dev/lp* check if synth is supported */
41 if (strncmp(synth->dev_name, DEV_PREFIX_LP,
42 strlen(DEV_PREFIX_LP)) == 0)
43 if (match_string(lp_supported, ARRAY_SIZE(lp_supported),
47 pr_err("speakup: lp* is only supported on:");
48 for (i = 0; i < ARRAY_SIZE(lp_supported); i++)
49 pr_cont(" %s", lp_supported[i]);
55 return tty_dev_name_to_number(synth->dev_name, dev_no);
58 return ser_to_dev(synth->ser, dev_no);
61 static int spk_ttyio_ldisc_open(struct tty_struct *tty)
63 struct spk_ldisc_data *ldisc_data;
65 if (tty->ops->write == NULL)
69 ldisc_data = kmalloc(sizeof(struct spk_ldisc_data), GFP_KERNEL);
71 pr_err("speakup: Failed to allocate ldisc_data.\n");
75 sema_init(&ldisc_data->sem, 0);
76 ldisc_data->buf_free = true;
77 speakup_tty->disc_data = ldisc_data;
82 static void spk_ttyio_ldisc_close(struct tty_struct *tty)
84 kfree(speakup_tty->disc_data);
88 static int spk_ttyio_receive_buf2(struct tty_struct *tty,
89 const unsigned char *cp, char *fp, int count)
91 struct spk_ldisc_data *ldisc_data = tty->disc_data;
93 if (spk_ttyio_synth->read_buff_add) {
96 for (i = 0; i < count; i++)
97 spk_ttyio_synth->read_buff_add(cp[i]);
102 if (!ldisc_data->buf_free)
103 /* ttyio_in will tty_schedule_flip */
106 /* Make sure the consumer has read buf before we have seen
107 * buf_free == true and overwrite buf */
110 ldisc_data->buf = cp[0];
111 ldisc_data->buf_free = false;
112 up(&ldisc_data->sem);
117 static struct tty_ldisc_ops spk_ttyio_ldisc_ops = {
118 .owner = THIS_MODULE,
119 .magic = TTY_LDISC_MAGIC,
120 .name = "speakup_ldisc",
121 .open = spk_ttyio_ldisc_open,
122 .close = spk_ttyio_ldisc_close,
123 .receive_buf2 = spk_ttyio_receive_buf2,
126 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch);
127 static void spk_ttyio_send_xchar(char ch);
128 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear);
129 static unsigned char spk_ttyio_in(void);
130 static unsigned char spk_ttyio_in_nowait(void);
131 static void spk_ttyio_flush_buffer(void);
133 struct spk_io_ops spk_ttyio_ops = {
134 .synth_out = spk_ttyio_out,
135 .send_xchar = spk_ttyio_send_xchar,
136 .tiocmset = spk_ttyio_tiocmset,
137 .synth_in = spk_ttyio_in,
138 .synth_in_nowait = spk_ttyio_in_nowait,
139 .flush_buffer = spk_ttyio_flush_buffer,
141 EXPORT_SYMBOL_GPL(spk_ttyio_ops);
143 static inline void get_termios(struct tty_struct *tty, struct ktermios *out_termios)
145 down_read(&tty->termios_rwsem);
146 *out_termios = tty->termios;
147 up_read(&tty->termios_rwsem);
150 static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
153 struct tty_struct *tty;
154 struct ktermios tmp_termios;
157 ret = tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops);
159 pr_err("Error registering line discipline.\n");
163 ret = get_dev_to_use(synth, &dev);
167 tty = tty_open_by_driver(dev, NULL, NULL);
172 ret = tty->ops->open(tty, NULL);
181 clear_bit(TTY_HUPPED, &tty->flags);
182 /* ensure hardware flow control is enabled */
183 get_termios(tty, &tmp_termios);
184 if (!(tmp_termios.c_cflag & CRTSCTS)) {
185 tmp_termios.c_cflag |= CRTSCTS;
186 tty_set_termios(tty, &tmp_termios);
188 * check c_cflag to see if it's updated as tty_set_termios may not return
189 * error even when no tty bits are changed by the request.
191 get_termios(tty, &tmp_termios);
192 if (!(tmp_termios.c_cflag & CRTSCTS))
193 pr_warn("speakup: Failed to set hardware flow control\n");
198 ret = tty_set_ldisc(tty, N_SPEAKUP);
203 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
205 if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
206 int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
212 pr_warn("%s: I/O error, deactivating speakup\n", in_synth->long_name);
213 /* No synth any more, so nobody will restart TTYs, and we thus
214 * need to do it ourselves. Now that there is no synth we can
215 * let application flood anyway
218 speakup_start_ttys();
226 static void spk_ttyio_send_xchar(char ch)
228 speakup_tty->ops->send_xchar(speakup_tty, ch);
231 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
233 speakup_tty->ops->tiocmset(speakup_tty, set, clear);
236 static unsigned char ttyio_in(int timeout)
238 struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
241 if (down_timeout(&ldisc_data->sem, usecs_to_jiffies(timeout)) == -ETIME) {
243 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
248 rv = ldisc_data->buf;
249 /* Make sure we have read buf before we set buf_free to let
250 * the producer overwrite it */
252 ldisc_data->buf_free = true;
253 /* Let TTY push more characters */
254 tty_schedule_flip(speakup_tty->port);
259 static unsigned char spk_ttyio_in(void)
261 return ttyio_in(SPK_SYNTH_TIMEOUT);
264 static unsigned char spk_ttyio_in_nowait(void)
268 return (rv == 0xff) ? 0 : rv;
271 static void spk_ttyio_flush_buffer(void)
273 if (speakup_tty->ops->flush_buffer)
274 speakup_tty->ops->flush_buffer(speakup_tty);
277 int spk_ttyio_synth_probe(struct spk_synth *synth)
279 int rv = spk_ttyio_initialise_ldisc(synth);
285 spk_ttyio_synth = synth;
289 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
291 void spk_ttyio_release(void)
296 tty_lock(speakup_tty);
298 if (speakup_tty->ops->close)
299 speakup_tty->ops->close(speakup_tty, NULL);
301 tty_ldisc_flush(speakup_tty);
302 tty_unlock(speakup_tty);
303 tty_ldisc_release(speakup_tty);
305 EXPORT_SYMBOL_GPL(spk_ttyio_release);
307 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
311 while ((ch = *buff)) {
313 ch = synth->procspeech;
314 if (tty_write_room(speakup_tty) < 1 || !synth->io_ops->synth_out(synth, ch))
320 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);