GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / speakup / speakup_dectlk.c
1 /*
2  * originally written by: Kirk Reiser <kirk@braille.uwo.ca>
3  * this version considerably modified by David Borowski, david575@rogers.com
4  *
5  * Copyright (C) 1998-99  Kirk Reiser.
6  * Copyright (C) 2003 David Borowski.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * specificly written as a driver for the speakup screenreview
19  * s not a general device driver.
20  */
21 #include <linux/unistd.h>
22 #include <linux/proc_fs.h>
23 #include <linux/jiffies.h>
24 #include <linux/spinlock.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/kthread.h>
28 #include "speakup.h"
29 #include "spk_priv.h"
30
31 #define DRV_VERSION "2.20"
32 #define SYNTH_CLEAR 0x03
33 #define PROCSPEECH 0x0b
34 static int xoff;
35
36 static inline int synth_full(void)
37 {
38         return xoff;
39 }
40
41 static void do_catch_up(struct spk_synth *synth);
42 static void synth_flush(struct spk_synth *synth);
43 static void read_buff_add(u_char c);
44 static unsigned char get_index(struct spk_synth *synth);
45
46 static int in_escape;
47 static int is_flushing;
48
49 static DEFINE_SPINLOCK(flush_lock);
50 static DECLARE_WAIT_QUEUE_HEAD(flush);
51
52 static struct var_t vars[] = {
53         { CAPS_START, .u.s = {"[:dv ap 160] " } },
54         { CAPS_STOP, .u.s = {"[:dv ap 100 ] " } },
55         { RATE, .u.n = {"[:ra %d] ", 180, 75, 650, 0, 0, NULL } },
56         { PITCH, .u.n = {"[:dv ap %d] ", 122, 50, 350, 0, 0, NULL } },
57         { VOL, .u.n = {"[:dv g5 %d] ", 86, 60, 86, 0, 0, NULL } },
58         { PUNCT, .u.n = {"[:pu %c] ", 0, 0, 2, 0, 0, "nsa" } },
59         { VOICE, .u.n = {"[:n%c] ", 0, 0, 9, 0, 0, "phfdburwkv" } },
60         { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
61         V_LAST_VAR
62 };
63
64 /*
65  * These attributes will appear in /sys/accessibility/speakup/dectlk.
66  */
67 static struct kobj_attribute caps_start_attribute =
68         __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
69 static struct kobj_attribute caps_stop_attribute =
70         __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
71 static struct kobj_attribute pitch_attribute =
72         __ATTR(pitch, 0644, spk_var_show, spk_var_store);
73 static struct kobj_attribute punct_attribute =
74         __ATTR(punct, 0644, spk_var_show, spk_var_store);
75 static struct kobj_attribute rate_attribute =
76         __ATTR(rate, 0644, spk_var_show, spk_var_store);
77 static struct kobj_attribute voice_attribute =
78         __ATTR(voice, 0644, spk_var_show, spk_var_store);
79 static struct kobj_attribute vol_attribute =
80         __ATTR(vol, 0644, spk_var_show, spk_var_store);
81
82 static struct kobj_attribute delay_time_attribute =
83         __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
84 static struct kobj_attribute direct_attribute =
85         __ATTR(direct, 0644, spk_var_show, spk_var_store);
86 static struct kobj_attribute full_time_attribute =
87         __ATTR(full_time, 0644, spk_var_show, spk_var_store);
88 static struct kobj_attribute jiffy_delta_attribute =
89         __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
90 static struct kobj_attribute trigger_time_attribute =
91         __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
92
93 /*
94  * Create a group of attributes so that we can create and destroy them all
95  * at once.
96  */
97 static struct attribute *synth_attrs[] = {
98         &caps_start_attribute.attr,
99         &caps_stop_attribute.attr,
100         &pitch_attribute.attr,
101         &punct_attribute.attr,
102         &rate_attribute.attr,
103         &voice_attribute.attr,
104         &vol_attribute.attr,
105         &delay_time_attribute.attr,
106         &direct_attribute.attr,
107         &full_time_attribute.attr,
108         &jiffy_delta_attribute.attr,
109         &trigger_time_attribute.attr,
110         NULL,   /* need to NULL terminate the list of attributes */
111 };
112
113 static int ap_defaults[] = {122, 89, 155, 110, 208, 240, 200, 106, 306};
114 static int g5_defaults[] = {86, 81, 86, 84, 81, 80, 83, 83, 73};
115
116 static struct spk_synth synth_dectlk = {
117         .name = "dectlk",
118         .version = DRV_VERSION,
119         .long_name = "Dectalk Express",
120         .init = "[:error sp :name paul :rate 180 :tsr off] ",
121         .procspeech = PROCSPEECH,
122         .clear = SYNTH_CLEAR,
123         .delay = 500,
124         .trigger = 50,
125         .jiffies = 50,
126         .full = 40000,
127         .dev_name = SYNTH_DEFAULT_DEV,
128         .startup = SYNTH_START,
129         .checkval = SYNTH_CHECK,
130         .vars = vars,
131         .default_pitch = ap_defaults,
132         .default_vol = g5_defaults,
133         .io_ops = &spk_ttyio_ops,
134         .probe = spk_ttyio_synth_probe,
135         .release = spk_ttyio_release,
136         .synth_immediate = spk_ttyio_synth_immediate,
137         .catch_up = do_catch_up,
138         .flush = synth_flush,
139         .is_alive = spk_synth_is_alive_restart,
140         .synth_adjust = NULL,
141         .read_buff_add = read_buff_add,
142         .get_index = get_index,
143         .indexing = {
144                 .command = "[:in re %d ] ",
145                 .lowindex = 1,
146                 .highindex = 8,
147                 .currindex = 1,
148         },
149         .attributes = {
150                 .attrs = synth_attrs,
151                 .name = "dectlk",
152         },
153 };
154
155 static int is_indnum(u_char *ch)
156 {
157         if ((*ch >= '0') && (*ch <= '9')) {
158                 *ch = *ch - '0';
159                 return 1;
160         }
161         return 0;
162 }
163
164 static u_char lastind;
165
166 static unsigned char get_index(struct spk_synth *synth)
167 {
168         u_char rv;
169
170         rv = lastind;
171         lastind = 0;
172         return rv;
173 }
174
175 static void read_buff_add(u_char c)
176 {
177         static int ind = -1;
178
179         if (c == 0x01) {
180                 unsigned long flags;
181
182                 spin_lock_irqsave(&flush_lock, flags);
183                 is_flushing = 0;
184                 wake_up_interruptible(&flush);
185                 spin_unlock_irqrestore(&flush_lock, flags);
186         } else if (c == 0x13) {
187                 xoff = 1;
188         } else if (c == 0x11) {
189                 xoff = 0;
190         } else if (is_indnum(&c)) {
191                 if (ind == -1)
192                         ind = c;
193                 else
194                         ind = ind * 10 + c;
195         } else if ((c > 31) && (c < 127)) {
196                 if (ind != -1)
197                         lastind = (u_char)ind;
198                 ind = -1;
199         }
200 }
201
202 static void do_catch_up(struct spk_synth *synth)
203 {
204         int synth_full_val = 0;
205         static u_char ch;
206         static u_char last = '\0';
207         unsigned long flags;
208         unsigned long jiff_max;
209         unsigned long timeout = msecs_to_jiffies(4000);
210         DEFINE_WAIT(wait);
211         struct var_t *jiffy_delta;
212         struct var_t *delay_time;
213         int jiffy_delta_val;
214         int delay_time_val;
215
216         jiffy_delta = spk_get_var(JIFFY);
217         delay_time = spk_get_var(DELAY);
218         spin_lock_irqsave(&speakup_info.spinlock, flags);
219         jiffy_delta_val = jiffy_delta->u.n.value;
220         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
221         jiff_max = jiffies + jiffy_delta_val;
222
223         while (!kthread_should_stop()) {
224                 /* if no ctl-a in 4, send data anyway */
225                 spin_lock_irqsave(&flush_lock, flags);
226                 while (is_flushing && timeout) {
227                         prepare_to_wait(&flush, &wait, TASK_INTERRUPTIBLE);
228                         spin_unlock_irqrestore(&flush_lock, flags);
229                         timeout = schedule_timeout(timeout);
230                         spin_lock_irqsave(&flush_lock, flags);
231                 }
232                 finish_wait(&flush, &wait);
233                 is_flushing = 0;
234                 spin_unlock_irqrestore(&flush_lock, flags);
235
236                 spin_lock_irqsave(&speakup_info.spinlock, flags);
237                 if (speakup_info.flushing) {
238                         speakup_info.flushing = 0;
239                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
240                         synth->flush(synth);
241                         continue;
242                 }
243                 synth_buffer_skip_nonlatin1();
244                 if (synth_buffer_empty()) {
245                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
246                         break;
247                 }
248                 ch = synth_buffer_peek();
249                 set_current_state(TASK_INTERRUPTIBLE);
250                 delay_time_val = delay_time->u.n.value;
251                 synth_full_val = synth_full();
252                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
253                 if (ch == '\n')
254                         ch = 0x0D;
255                 if (synth_full_val || !synth->io_ops->synth_out(synth, ch)) {
256                         schedule_timeout(msecs_to_jiffies(delay_time_val));
257                         continue;
258                 }
259                 set_current_state(TASK_RUNNING);
260                 spin_lock_irqsave(&speakup_info.spinlock, flags);
261                 synth_buffer_getc();
262                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
263                 if (ch == '[') {
264                         in_escape = 1;
265                 } else if (ch == ']') {
266                         in_escape = 0;
267                 } else if (ch <= SPACE) {
268                         if (!in_escape && strchr(",.!?;:", last))
269                                 synth->io_ops->synth_out(synth, PROCSPEECH);
270                         if (time_after_eq(jiffies, jiff_max)) {
271                                 if (!in_escape)
272                                         synth->io_ops->synth_out(synth, PROCSPEECH);
273                                 spin_lock_irqsave(&speakup_info.spinlock,
274                                                 flags);
275                                 jiffy_delta_val = jiffy_delta->u.n.value;
276                                 delay_time_val = delay_time->u.n.value;
277                                 spin_unlock_irqrestore(&speakup_info.spinlock,
278                                                 flags);
279                                 schedule_timeout(msecs_to_jiffies
280                                                  (delay_time_val));
281                                 jiff_max = jiffies + jiffy_delta_val;
282                         }
283                 }
284                 last = ch;
285         }
286         if (!in_escape)
287                 synth->io_ops->synth_out(synth, PROCSPEECH);
288 }
289
290 static void synth_flush(struct spk_synth *synth)
291 {
292         if (in_escape)
293                 /* if in command output ']' so we don't get an error */
294                 synth->io_ops->synth_out(synth, ']');
295         in_escape = 0;
296         is_flushing = 1;
297         synth->io_ops->flush_buffer();
298         synth->io_ops->synth_out(synth, SYNTH_CLEAR);
299 }
300
301 module_param_named(ser, synth_dectlk.ser, int, 0444);
302 module_param_named(dev, synth_dectlk.dev_name, charp, S_IRUGO);
303 module_param_named(start, synth_dectlk.startup, short, 0444);
304
305 MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
306 MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
307 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
308
309 module_spk_synth(synth_dectlk);
310
311 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
312 MODULE_AUTHOR("David Borowski");
313 MODULE_DESCRIPTION("Speakup support for DECtalk Express synthesizers");
314 MODULE_LICENSE("GPL");
315 MODULE_VERSION(DRV_VERSION);
316