1 // SPDX-License-Identifier: GPL-2.0+
3 * originally written by: Kirk Reiser <kirk@braille.uwo.ca>
4 * this version considerably modified by David Borowski, david575@rogers.com
6 * Copyright (C) 1998-99 Kirk Reiser.
7 * Copyright (C) 2003 David Borowski.
9 * specificly written as a driver for the speakup screenreview
10 * s not a general device driver.
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>
22 #define DRV_VERSION "2.20"
23 #define SYNTH_CLEAR 0x03
24 #define PROCSPEECH 0x0b
27 static inline int synth_full(void)
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);
38 static int is_flushing;
40 static DEFINE_SPINLOCK(flush_lock);
41 static DECLARE_WAIT_QUEUE_HEAD(flush);
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 { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
48 { VOL, .u.n = {"[:dv g5 %d] ", 86, 60, 86, 0, 0, NULL } },
49 { PUNCT, .u.n = {"[:pu %c] ", 0, 0, 2, 0, 0, "nsa" } },
50 { VOICE, .u.n = {"[:n%c] ", 0, 0, 9, 0, 0, "phfdburwkv" } },
51 { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
56 * These attributes will appear in /sys/accessibility/speakup/dectlk.
58 static struct kobj_attribute caps_start_attribute =
59 __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
60 static struct kobj_attribute caps_stop_attribute =
61 __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
62 static struct kobj_attribute pitch_attribute =
63 __ATTR(pitch, 0644, spk_var_show, spk_var_store);
64 static struct kobj_attribute inflection_attribute =
65 __ATTR(inflection, 0644, spk_var_show, spk_var_store);
66 static struct kobj_attribute punct_attribute =
67 __ATTR(punct, 0644, spk_var_show, spk_var_store);
68 static struct kobj_attribute rate_attribute =
69 __ATTR(rate, 0644, spk_var_show, spk_var_store);
70 static struct kobj_attribute voice_attribute =
71 __ATTR(voice, 0644, spk_var_show, spk_var_store);
72 static struct kobj_attribute vol_attribute =
73 __ATTR(vol, 0644, spk_var_show, spk_var_store);
75 static struct kobj_attribute delay_time_attribute =
76 __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
77 static struct kobj_attribute direct_attribute =
78 __ATTR(direct, 0644, spk_var_show, spk_var_store);
79 static struct kobj_attribute full_time_attribute =
80 __ATTR(full_time, 0644, spk_var_show, spk_var_store);
81 static struct kobj_attribute flush_time_attribute =
82 __ATTR(flush_time, 0644, spk_var_show, spk_var_store);
83 static struct kobj_attribute jiffy_delta_attribute =
84 __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
85 static struct kobj_attribute trigger_time_attribute =
86 __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
89 * Create a group of attributes so that we can create and destroy them all
92 static struct attribute *synth_attrs[] = {
93 &caps_start_attribute.attr,
94 &caps_stop_attribute.attr,
95 &pitch_attribute.attr,
96 &inflection_attribute.attr,
97 &punct_attribute.attr,
99 &voice_attribute.attr,
101 &delay_time_attribute.attr,
102 &direct_attribute.attr,
103 &full_time_attribute.attr,
104 &flush_time_attribute.attr,
105 &jiffy_delta_attribute.attr,
106 &trigger_time_attribute.attr,
107 NULL, /* need to NULL terminate the list of attributes */
110 static int ap_defaults[] = {122, 89, 155, 110, 208, 240, 200, 106, 306};
111 static int g5_defaults[] = {86, 81, 86, 84, 81, 80, 83, 83, 73};
113 static struct spk_synth synth_dectlk = {
115 .version = DRV_VERSION,
116 .long_name = "Dectalk Express",
117 .init = "[:error sp :name paul :rate 180 :tsr off] ",
118 .procspeech = PROCSPEECH,
119 .clear = SYNTH_CLEAR,
125 .dev_name = SYNTH_DEFAULT_DEV,
126 .startup = SYNTH_START,
127 .checkval = SYNTH_CHECK,
129 .default_pitch = ap_defaults,
130 .default_vol = g5_defaults,
131 .io_ops = &spk_ttyio_ops,
132 .probe = spk_ttyio_synth_probe,
133 .release = spk_ttyio_release,
134 .synth_immediate = spk_ttyio_synth_immediate,
135 .catch_up = do_catch_up,
136 .flush = synth_flush,
137 .is_alive = spk_synth_is_alive_restart,
138 .synth_adjust = NULL,
139 .read_buff_add = read_buff_add,
140 .get_index = get_index,
142 .command = "[:in re %d ] ",
148 .attrs = synth_attrs,
153 static int is_indnum(u_char *ch)
155 if ((*ch >= '0') && (*ch <= '9')) {
162 static u_char lastind;
164 static unsigned char get_index(struct spk_synth *synth)
173 static void read_buff_add(u_char c)
180 spin_lock_irqsave(&flush_lock, flags);
182 wake_up_interruptible(&flush);
183 spin_unlock_irqrestore(&flush_lock, flags);
184 } else if (c == 0x13) {
186 } else if (c == 0x11) {
188 } else if (is_indnum(&c)) {
193 } else if ((c > 31) && (c < 127)) {
195 lastind = (u_char)ind;
200 static void do_catch_up(struct spk_synth *synth)
202 int synth_full_val = 0;
204 static u_char last = '\0';
206 unsigned long jiff_max;
207 unsigned long timeout;
209 struct var_t *jiffy_delta;
210 struct var_t *delay_time;
211 struct var_t *flush_time;
216 jiffy_delta = spk_get_var(JIFFY);
217 delay_time = spk_get_var(DELAY);
218 flush_time = spk_get_var(FLUSH);
219 spin_lock_irqsave(&speakup_info.spinlock, flags);
220 jiffy_delta_val = jiffy_delta->u.n.value;
221 timeout_val = flush_time->u.n.value;
222 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
223 timeout = msecs_to_jiffies(timeout_val);
224 jiff_max = jiffies + jiffy_delta_val;
226 while (!kthread_should_stop()) {
227 /* if no ctl-a in 4, send data anyway */
228 spin_lock_irqsave(&flush_lock, flags);
229 while (is_flushing && timeout) {
230 prepare_to_wait(&flush, &wait, TASK_INTERRUPTIBLE);
231 spin_unlock_irqrestore(&flush_lock, flags);
232 timeout = schedule_timeout(timeout);
233 spin_lock_irqsave(&flush_lock, flags);
235 finish_wait(&flush, &wait);
237 spin_unlock_irqrestore(&flush_lock, flags);
239 spin_lock_irqsave(&speakup_info.spinlock, flags);
240 if (speakup_info.flushing) {
241 speakup_info.flushing = 0;
242 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
246 synth_buffer_skip_nonlatin1();
247 if (synth_buffer_empty()) {
248 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
251 ch = synth_buffer_peek();
252 set_current_state(TASK_INTERRUPTIBLE);
253 delay_time_val = delay_time->u.n.value;
254 synth_full_val = synth_full();
255 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
258 if (synth_full_val || !synth->io_ops->synth_out(synth, ch)) {
259 schedule_timeout(msecs_to_jiffies(delay_time_val));
262 set_current_state(TASK_RUNNING);
263 spin_lock_irqsave(&speakup_info.spinlock, flags);
265 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
268 } else if (ch == ']') {
270 } else if (ch <= SPACE) {
271 if (!in_escape && strchr(",.!?;:", last))
272 synth->io_ops->synth_out(synth, PROCSPEECH);
273 if (time_after_eq(jiffies, jiff_max)) {
275 synth->io_ops->synth_out(synth,
277 spin_lock_irqsave(&speakup_info.spinlock,
279 jiffy_delta_val = jiffy_delta->u.n.value;
280 delay_time_val = delay_time->u.n.value;
281 spin_unlock_irqrestore(&speakup_info.spinlock,
283 schedule_timeout(msecs_to_jiffies
285 jiff_max = jiffies + jiffy_delta_val;
291 synth->io_ops->synth_out(synth, PROCSPEECH);
294 static void synth_flush(struct spk_synth *synth)
297 /* if in command output ']' so we don't get an error */
298 synth->io_ops->synth_out(synth, ']');
301 synth->io_ops->flush_buffer(synth);
302 synth->io_ops->synth_out(synth, SYNTH_CLEAR);
305 module_param_named(ser, synth_dectlk.ser, int, 0444);
306 module_param_named(dev, synth_dectlk.dev_name, charp, 0444);
307 module_param_named(start, synth_dectlk.startup, short, 0444);
309 MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
310 MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
311 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
313 module_spk_synth(synth_dectlk);
315 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
316 MODULE_AUTHOR("David Borowski");
317 MODULE_DESCRIPTION("Speakup support for DECtalk Express synthesizers");
318 MODULE_LICENSE("GPL");
319 MODULE_VERSION(DRV_VERSION);