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