]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/speakup/spk_ttyio.c
Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.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 = tty_register_ldisc(N_SPEAKUP, &spk_ttyio_ldisc_ops);
158         if (ret) {
159                 pr_err("Error registering line discipline.\n");
160                 return ret;
161         }
162
163         ret = get_dev_to_use(synth, &dev);
164         if (ret)
165                 return ret;
166
167         tty = tty_open_by_driver(dev, NULL, NULL);
168         if (IS_ERR(tty))
169                 return PTR_ERR(tty);
170
171         if (tty->ops->open)
172                 ret = tty->ops->open(tty, NULL);
173         else
174                 ret = -ENODEV;
175
176         if (ret) {
177                 tty_unlock(tty);
178                 return ret;
179         }
180
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);
187                 /*
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.
190                  */
191                 get_termios(tty, &tmp_termios);
192                 if (!(tmp_termios.c_cflag & CRTSCTS))
193                         pr_warn("speakup: Failed to set hardware flow control\n");
194         }
195
196         tty_unlock(tty);
197
198         ret = tty_set_ldisc(tty, N_SPEAKUP);
199
200         return ret;
201 }
202
203 static int spk_ttyio_out(struct spk_synth *in_synth, const char ch)
204 {
205         if (in_synth->alive && speakup_tty && speakup_tty->ops->write) {
206                 int ret = speakup_tty->ops->write(speakup_tty, &ch, 1);
207
208                 if (ret == 0)
209                         /* No room */
210                         return 0;
211                 if (ret < 0) {
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
216                          */
217                         in_synth->alive = 0;
218                         speakup_start_ttys();
219                         return 0;
220                 }
221                 return 1;
222         }
223         return 0;
224 }
225
226 static void spk_ttyio_send_xchar(char ch)
227 {
228         speakup_tty->ops->send_xchar(speakup_tty, ch);
229 }
230
231 static void spk_ttyio_tiocmset(unsigned int set, unsigned int clear)
232 {
233         speakup_tty->ops->tiocmset(speakup_tty, set, clear);
234 }
235
236 static unsigned char ttyio_in(int timeout)
237 {
238         struct spk_ldisc_data *ldisc_data = speakup_tty->disc_data;
239         char rv;
240
241         if (down_timeout(&ldisc_data->sem, usecs_to_jiffies(timeout)) == -ETIME) {
242                 if (timeout)
243                         pr_warn("spk_ttyio: timeout (%d)  while waiting for input\n",
244                                 timeout);
245                 return 0xff;
246         }
247
248         rv = ldisc_data->buf;
249         /* Make sure we have read buf before we set buf_free to let
250          * the producer overwrite it */
251         mb();
252         ldisc_data->buf_free = true;
253         /* Let TTY push more characters */
254         tty_schedule_flip(speakup_tty->port);
255
256         return rv;
257 }
258
259 static unsigned char spk_ttyio_in(void)
260 {
261         return ttyio_in(SPK_SYNTH_TIMEOUT);
262 }
263
264 static unsigned char spk_ttyio_in_nowait(void)
265 {
266         u8 rv = ttyio_in(0);
267
268         return (rv == 0xff) ? 0 : rv;
269 }
270
271 static void spk_ttyio_flush_buffer(void)
272 {
273         if (speakup_tty->ops->flush_buffer)
274                 speakup_tty->ops->flush_buffer(speakup_tty);
275 }
276
277 int spk_ttyio_synth_probe(struct spk_synth *synth)
278 {
279         int rv = spk_ttyio_initialise_ldisc(synth);
280
281         if (rv)
282                 return rv;
283
284         synth->alive = 1;
285         spk_ttyio_synth = synth;
286
287         return 0;
288 }
289 EXPORT_SYMBOL_GPL(spk_ttyio_synth_probe);
290
291 void spk_ttyio_release(void)
292 {
293         if (!speakup_tty)
294                 return;
295
296         tty_lock(speakup_tty);
297
298         if (speakup_tty->ops->close)
299                 speakup_tty->ops->close(speakup_tty, NULL);
300
301         tty_ldisc_flush(speakup_tty);
302         tty_unlock(speakup_tty);
303         tty_ldisc_release(speakup_tty);
304 }
305 EXPORT_SYMBOL_GPL(spk_ttyio_release);
306
307 const char *spk_ttyio_synth_immediate(struct spk_synth *synth, const char *buff)
308 {
309         u_char ch;
310
311         while ((ch = *buff)) {
312                 if (ch == '\n')
313                         ch = synth->procspeech;
314                 if (tty_write_room(speakup_tty) < 1 || !synth->io_ops->synth_out(synth, ch))
315                         return buff;
316                 buff++;
317         }
318         return NULL;
319 }
320 EXPORT_SYMBOL_GPL(spk_ttyio_synth_immediate);