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
= get_dev_to_use(synth
, &dev
);
161 tty
= tty_open_by_driver(dev
, NULL
, NULL
);
166 ret
= tty
->ops
->open(tty
, NULL
);
175 clear_bit(TTY_HUPPED
, &tty
->flags
);
176 /* ensure hardware flow control is enabled */
177 get_termios(tty
, &tmp_termios
);
178 if (!(tmp_termios
.c_cflag
& CRTSCTS
)) {
179 tmp_termios
.c_cflag
|= CRTSCTS
;
180 tty_set_termios(tty
, &tmp_termios
);
182 * check c_cflag to see if it's updated as tty_set_termios may not return
183 * error even when no tty bits are changed by the request.
185 get_termios(tty
, &tmp_termios
);
186 if (!(tmp_termios
.c_cflag
& CRTSCTS
))
187 pr_warn("speakup: Failed to set hardware flow control\n");
192 ret
= tty_set_ldisc(tty
, N_SPEAKUP
);
194 pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
199 void spk_ttyio_register_ldisc(void)
201 if (tty_register_ldisc(N_SPEAKUP
, &spk_ttyio_ldisc_ops
))
202 pr_warn("speakup: Error registering line discipline. Most synths won't work.\n");
205 void spk_ttyio_unregister_ldisc(void)
207 if (tty_unregister_ldisc(N_SPEAKUP
))
208 pr_warn("speakup: Couldn't unregister ldisc\n");
211 static int spk_ttyio_out(struct spk_synth
*in_synth
, const char ch
)
213 if (in_synth
->alive
&& speakup_tty
&& speakup_tty
->ops
->write
) {
214 int ret
= speakup_tty
->ops
->write(speakup_tty
, &ch
, 1);
220 pr_warn("%s: I/O error, deactivating speakup\n", in_synth
->long_name
);
221 /* No synth any more, so nobody will restart TTYs, and we thus
222 * need to do it ourselves. Now that there is no synth we can
223 * let application flood anyway
226 speakup_start_ttys();
234 static void spk_ttyio_send_xchar(char ch
)
236 speakup_tty
->ops
->send_xchar(speakup_tty
, ch
);
239 static void spk_ttyio_tiocmset(unsigned int set
, unsigned int clear
)
241 speakup_tty
->ops
->tiocmset(speakup_tty
, set
, clear
);
244 static unsigned char ttyio_in(int timeout
)
246 struct spk_ldisc_data
*ldisc_data
= speakup_tty
->disc_data
;
249 if (down_timeout(&ldisc_data
->sem
, usecs_to_jiffies(timeout
)) == -ETIME
) {
251 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
256 rv
= ldisc_data
->buf
;
257 /* Make sure we have read buf before we set buf_free to let
258 * the producer overwrite it */
260 ldisc_data
->buf_free
= true;
261 /* Let TTY push more characters */
262 tty_schedule_flip(speakup_tty
->port
);
267 static unsigned char spk_ttyio_in(void)
269 return ttyio_in(SPK_SYNTH_TIMEOUT
);
272 static unsigned char spk_ttyio_in_nowait(void)
276 return (rv
== 0xff) ? 0 : rv
;
279 static void spk_ttyio_flush_buffer(void)
281 if (speakup_tty
->ops
->flush_buffer
)
282 speakup_tty
->ops
->flush_buffer(speakup_tty
);
285 int spk_ttyio_synth_probe(struct spk_synth
*synth
)
287 int rv
= spk_ttyio_initialise_ldisc(synth
);
293 spk_ttyio_synth
= synth
;
297 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe
);
299 void spk_ttyio_release(void)
304 tty_lock(speakup_tty
);
306 if (speakup_tty
->ops
->close
)
307 speakup_tty
->ops
->close(speakup_tty
, NULL
);
309 tty_ldisc_flush(speakup_tty
);
310 tty_unlock(speakup_tty
);
311 tty_release_struct(speakup_tty
, speakup_tty
->index
);
313 EXPORT_SYMBOL_GPL(spk_ttyio_release
);
315 const char *spk_ttyio_synth_immediate(struct spk_synth
*synth
, const char *buff
)
319 while ((ch
= *buff
)) {
321 ch
= synth
->procspeech
;
322 if (tty_write_room(speakup_tty
) < 1 || !synth
->io_ops
->synth_out(synth
, ch
))
328 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate
);