]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/staging/speakup/spk_ttyio.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[mirror_ubuntu-artful-kernel.git] / drivers / staging / speakup / spk_ttyio.c
1 #include <linux/types.h>
2 #include <linux/tty.h>
3 #include <linux/tty_flip.h>
4 #include <linux/slab.h>
5
6 #include "speakup.h"
7 #include "spk_types.h"
8 #include "spk_priv.h"
9
10 #define DEV_PREFIX_LP "lp"
11
12 static const char * const lp_supported[] = { "acntsa", "bns", "dummy",
13 "txprt" };
14
15 struct spk_ldisc_data {
16 char buf;
17 struct semaphore sem;
18 bool buf_free;
19 };
20
21 static struct spk_synth *spk_ttyio_synth;
22 static struct tty_struct *speakup_tty;
23
24 static int ser_to_dev(int ser, dev_t *dev_no)
25 {
26 if (ser < 0 || ser > (255 - 64)) {
27 pr_err("speakup: Invalid ser param. Must be between 0 and 191 inclusive.\n");
28 return -EINVAL;
29 }
30
31 *dev_no = MKDEV(4, (64 + ser));
32 return 0;
33 }
34
35 static int get_dev_to_use(struct spk_synth *synth, dev_t *dev_no)
36 {
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),
44 synth->name) < 0) {
45 int i;
46
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]);
50 pr_cont("\n");
51
52 return -ENOTSUPP;
53 }
54
55 return tty_dev_name_to_number(synth->dev_name, dev_no);
56 }
57
58 return ser_to_dev(synth->ser, dev_no);
59 }
60
61 static int spk_ttyio_ldisc_open(struct tty_struct *tty)
62 {
63 struct spk_ldisc_data *ldisc_data;
64
65 if (tty->ops->write == NULL)
66 return -EOPNOTSUPP;
67 speakup_tty = tty;
68
69 ldisc_data = kmalloc(sizeof(struct spk_ldisc_data), GFP_KERNEL);
70 if (!ldisc_data) {
71 pr_err("speakup: Failed to allocate ldisc_data.\n");
72 return -ENOMEM;
73 }
74
75 sema_init(&ldisc_data->sem, 0);
76 ldisc_data->buf_free = true;
77 speakup_tty->disc_data = ldisc_data;
78
79 return 0;
80 }
81
82 static void spk_ttyio_ldisc_close(struct tty_struct *tty)
83 {
84 kfree(speakup_tty->disc_data);
85 speakup_tty = NULL;
86 }
87
88 static int spk_ttyio_receive_buf2(struct tty_struct *tty,
89 const unsigned char *cp, char *fp, int count)
90 {
91 struct spk_ldisc_data *ldisc_data = tty->disc_data;
92
93 if (spk_ttyio_synth->read_buff_add) {
94 int i;
95
96 for (i = 0; i < count; i++)
97 spk_ttyio_synth->read_buff_add(cp[i]);
98
99 return count;
100 }
101
102 if (!ldisc_data->buf_free)
103 /* ttyio_in will tty_schedule_flip */
104 return 0;
105
106 /* Make sure the consumer has read buf before we have seen
107 * buf_free == true and overwrite buf */
108 mb();
109
110 ldisc_data->buf = cp[0];
111 ldisc_data->buf_free = false;
112 up(&ldisc_data->sem);
113
114 return 1;
115 }
116
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,
124 };
125
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);
132
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,
140 };
141 EXPORT_SYMBOL_GPL(spk_ttyio_ops);
142
143 static inline void get_termios(struct tty_struct *tty, struct ktermios *out_termios)
144 {
145 down_read(&tty->termios_rwsem);
146 *out_termios = tty->termios;
147 up_read(&tty->termios_rwsem);
148 }
149
150 static int spk_ttyio_initialise_ldisc(struct spk_synth *synth)
151 {
152 int ret = 0;
153 struct tty_struct *tty;
154 struct ktermios tmp_termios;
155 dev_t dev;
156
157 ret = get_dev_to_use(synth, &dev);
158 if (ret)
159 return ret;
160
161 tty = tty_open_by_driver(dev, NULL, NULL);
162 if (IS_ERR(tty))
163 return PTR_ERR(tty);
164
165 if (tty->ops->open)
166 ret = tty->ops->open(tty, NULL);
167 else
168 ret = -ENODEV;
169
170 if (ret) {
171 tty_unlock(tty);
172 return ret;
173 }
174
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);
181 /*
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.
184 */
185 get_termios(tty, &tmp_termios);
186 if (!(tmp_termios.c_cflag & CRTSCTS))
187 pr_warn("speakup: Failed to set hardware flow control\n");
188 }
189
190 tty_unlock(tty);
191
192 ret = tty_set_ldisc(tty, N_SPEAKUP);
193 if (ret)
194 pr_err("speakup: Failed to set N_SPEAKUP on tty\n");
195
196 return ret;
197 }
198
199 void spk_ttyio_register_ldisc(void)
200 {
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");
203 }
204
205 void spk_ttyio_unregister_ldisc(void)
206 {
207 if (tty_unregister_ldisc(N_SPEAKUP))
208 pr_warn("speakup: Couldn't unregister ldisc\n");
209 }
210
211 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
212 {
213 if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
214 int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
215
216 if (ret == 0)
217 /* No room */
218 return 0;
219 if (ret < 0) {
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
224 */
225 in_synth->alive = 0;
226 speakup_start_ttys();
227 return 0;
228 }
229 return 1;
230 }
231 return 0;
232 }
233
234 static void spk_ttyio_send_xchar(char ch)
235 {
236 speakup_tty->ops->send_xchar(speakup_tty, ch);
237 }
238
239 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
240 {
241 speakup_tty->ops->tiocmset(speakup_tty, set, clear);
242 }
243
244 static unsigned char ttyio_in(int timeout)
245 {
246 struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
247 char rv;
248
249 if (down_timeout(&ldisc_data->sem, usecs_to_jiffies(timeout)) == -ETIME) {
250 if (timeout)
251 pr_warn("spk_ttyio: timeout (%d) while waiting for input\n",
252 timeout);
253 return 0xff;
254 }
255
256 rv = ldisc_data->buf;
257 /* Make sure we have read buf before we set buf_free to let
258 * the producer overwrite it */
259 mb();
260 ldisc_data->buf_free = true;
261 /* Let TTY push more characters */
262 tty_schedule_flip(speakup_tty->port);
263
264 return rv;
265 }
266
267 static unsigned char spk_ttyio_in(void)
268 {
269 return ttyio_in(SPK_SYNTH_TIMEOUT);
270 }
271
272 static unsigned char spk_ttyio_in_nowait(void)
273 {
274 u8 rv = ttyio_in(0);
275
276 return (rv == 0xff) ? 0 : rv;
277 }
278
279 static void spk_ttyio_flush_buffer(void)
280 {
281 if (speakup_tty->ops->flush_buffer)
282 speakup_tty->ops->flush_buffer(speakup_tty);
283 }
284
285 int spk_ttyio_synth_probe(struct spk_synth *synth)
286 {
287 int rv = spk_ttyio_initialise_ldisc(synth);
288
289 if (rv)
290 return rv;
291
292 synth->alive = 1;
293 spk_ttyio_synth = synth;
294
295 return 0;
296 }
297 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
298
299 void spk_ttyio_release(void)
300 {
301 if (!speakup_tty)
302 return;
303
304 tty_lock(speakup_tty);
305
306 if (speakup_tty->ops->close)
307 speakup_tty->ops->close(speakup_tty, NULL);
308
309 tty_ldisc_flush(speakup_tty);
310 tty_unlock(speakup_tty);
311 tty_release_struct(speakup_tty, speakup_tty->index);
312 }
313 EXPORT_SYMBOL_GPL(spk_ttyio_release);
314
315 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
316 {
317 u_char ch;
318
319 while ((ch = *buff)) {
320 if (ch == '\n')
321 ch = synth->procspeech;
322 if (tty_write_room(speakup_tty) < 1 || !synth->io_ops->synth_out(synth, ch))
323 return buff;
324 buff++;
325 }
326 return NULL;
327 }
328 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);