blob: 580ec796816bcf0eefdc66e23329ee10f173f19f [file] [log] [blame]
Greg Kroah-Hartman64969222018-01-11 11:08:40 +01001// SPDX-License-Identifier: GPL-2.0+
William Hubbsc6e3fd22010-10-07 13:20:02 -05002/*
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 *
William Hubbsc6e3fd22010-10-07 13:20:02 -05009 * 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"
William Hubbsc6e3fd22010-10-07 13:20:02 -050021
22#define DRV_VERSION "2.20"
23#define SYNTH_CLEAR 0x03
24#define PROCSPEECH 0x0b
Christopher Brannon4073f1b2010-10-14 19:24:00 -050025static int xoff;
26
27static inline int synth_full(void)
28{
29 return xoff;
30}
William Hubbsc6e3fd22010-10-07 13:20:02 -050031
32static void do_catch_up(struct spk_synth *synth);
33static void synth_flush(struct spk_synth *synth);
34static void read_buff_add(u_char c);
Okash Khawajaca693dc2017-04-29 20:52:58 +010035static unsigned char get_index(struct spk_synth *synth);
William Hubbsc6e3fd22010-10-07 13:20:02 -050036
37static int in_escape;
38static int is_flushing;
39
Yang Yingliangd1b928e2020-11-17 09:22:29 +080040static DEFINE_SPINLOCK(flush_lock);
William Hubbsc6e3fd22010-10-07 13:20:02 -050041static DECLARE_WAIT_QUEUE_HEAD(flush);
42
43static struct var_t vars[] = {
Christopher Brannon4073f1b2010-10-14 19:24:00 -050044 { 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 } },
Samuel Thibaultd97a9d72020-04-25 21:32:26 +020047 { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
Christopher Brannon4073f1b2010-10-14 19:24:00 -050048 { 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 } },
William Hubbsc6e3fd22010-10-07 13:20:02 -050052 V_LAST_VAR
53};
54
55/*
56 * These attributes will appear in /sys/accessibility/speakup/dectlk.
57 */
58static struct kobj_attribute caps_start_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130059 __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050060static struct kobj_attribute caps_stop_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130061 __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050062static struct kobj_attribute pitch_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130063 __ATTR(pitch, 0644, spk_var_show, spk_var_store);
Samuel Thibaultd97a9d72020-04-25 21:32:26 +020064static struct kobj_attribute inflection_attribute =
65 __ATTR(inflection, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050066static struct kobj_attribute punct_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130067 __ATTR(punct, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050068static struct kobj_attribute rate_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130069 __ATTR(rate, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050070static struct kobj_attribute voice_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130071 __ATTR(voice, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050072static struct kobj_attribute vol_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130073 __ATTR(vol, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050074
75static struct kobj_attribute delay_time_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130076 __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050077static struct kobj_attribute direct_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130078 __ATTR(direct, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050079static struct kobj_attribute full_time_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130080 __ATTR(full_time, 0644, spk_var_show, spk_var_store);
Samuel Thibault1f7c14a2021-01-28 19:01:16 +010081static struct kobj_attribute flush_time_attribute =
82 __ATTR(flush_time, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050083static struct kobj_attribute jiffy_delta_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130084 __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050085static struct kobj_attribute trigger_time_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130086 __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050087
88/*
89 * Create a group of attributes so that we can create and destroy them all
90 * at once.
91 */
92static struct attribute *synth_attrs[] = {
93 &caps_start_attribute.attr,
94 &caps_stop_attribute.attr,
95 &pitch_attribute.attr,
Samuel Thibaultd97a9d72020-04-25 21:32:26 +020096 &inflection_attribute.attr,
William Hubbsc6e3fd22010-10-07 13:20:02 -050097 &punct_attribute.attr,
98 &rate_attribute.attr,
99 &voice_attribute.attr,
100 &vol_attribute.attr,
101 &delay_time_attribute.attr,
102 &direct_attribute.attr,
103 &full_time_attribute.attr,
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100104 &flush_time_attribute.attr,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500105 &jiffy_delta_attribute.attr,
106 &trigger_time_attribute.attr,
107 NULL, /* need to NULL terminate the list of attributes */
108};
109
110static int ap_defaults[] = {122, 89, 155, 110, 208, 240, 200, 106, 306};
111static int g5_defaults[] = {86, 81, 86, 84, 81, 80, 83, 83, 73};
112
113static struct spk_synth synth_dectlk = {
114 .name = "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,
120 .delay = 500,
121 .trigger = 50,
122 .jiffies = 50,
123 .full = 40000,
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100124 .flush_time = 4000,
Okash Khawaja8a21ff72017-06-25 19:40:02 +0100125 .dev_name = SYNTH_DEFAULT_DEV,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500126 .startup = SYNTH_START,
127 .checkval = SYNTH_CHECK,
128 .vars = vars,
129 .default_pitch = ap_defaults,
130 .default_vol = g5_defaults,
Okash Khawaja470790e2017-05-15 18:45:36 +0100131 .io_ops = &spk_ttyio_ops,
132 .probe = spk_ttyio_synth_probe,
133 .release = spk_ttyio_release,
134 .synth_immediate = spk_ttyio_synth_immediate,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500135 .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,
141 .indexing = {
142 .command = "[:in re %d ] ",
143 .lowindex = 1,
144 .highindex = 8,
145 .currindex = 1,
146 },
147 .attributes = {
148 .attrs = synth_attrs,
149 .name = "dectlk",
150 },
151};
152
153static int is_indnum(u_char *ch)
154{
155 if ((*ch >= '0') && (*ch <= '9')) {
156 *ch = *ch - '0';
157 return 1;
158 }
159 return 0;
160}
161
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500162static u_char lastind;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500163
Okash Khawajaca693dc2017-04-29 20:52:58 +0100164static unsigned char get_index(struct spk_synth *synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500165{
166 u_char rv;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200167
William Hubbsc6e3fd22010-10-07 13:20:02 -0500168 rv = lastind;
169 lastind = 0;
170 return rv;
171}
172
173static void read_buff_add(u_char c)
174{
175 static int ind = -1;
176
177 if (c == 0x01) {
178 unsigned long flags;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200179
William Hubbsc6e3fd22010-10-07 13:20:02 -0500180 spin_lock_irqsave(&flush_lock, flags);
181 is_flushing = 0;
182 wake_up_interruptible(&flush);
183 spin_unlock_irqrestore(&flush_lock, flags);
184 } else if (c == 0x13) {
185 xoff = 1;
186 } else if (c == 0x11) {
187 xoff = 0;
188 } else if (is_indnum(&c)) {
189 if (ind == -1)
190 ind = c;
191 else
192 ind = ind * 10 + c;
193 } else if ((c > 31) && (c < 127)) {
194 if (ind != -1)
195 lastind = (u_char)ind;
196 ind = -1;
197 }
198}
199
200static void do_catch_up(struct spk_synth *synth)
201{
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500202 int synth_full_val = 0;
203 static u_char ch;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500204 static u_char last = '\0';
205 unsigned long flags;
206 unsigned long jiff_max;
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100207 unsigned long timeout;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500208 DEFINE_WAIT(wait);
209 struct var_t *jiffy_delta;
210 struct var_t *delay_time;
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100211 struct var_t *flush_time;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500212 int jiffy_delta_val;
213 int delay_time_val;
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100214 int timeout_val;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500215
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100216 jiffy_delta = spk_get_var(JIFFY);
217 delay_time = spk_get_var(DELAY);
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100218 flush_time = spk_get_var(FLUSH);
William Hubbs68743d62013-05-13 00:03:03 -0500219 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500220 jiffy_delta_val = jiffy_delta->u.n.value;
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100221 timeout_val = flush_time->u.n.value;
William Hubbs68743d62013-05-13 00:03:03 -0500222 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100223 timeout = msecs_to_jiffies(timeout_val);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500224 jiff_max = jiffies + jiffy_delta_val;
225
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);
234 }
235 finish_wait(&flush, &wait);
236 is_flushing = 0;
237 spin_unlock_irqrestore(&flush_lock, flags);
238
William Hubbs68743d62013-05-13 00:03:03 -0500239 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500240 if (speakup_info.flushing) {
241 speakup_info.flushing = 0;
William Hubbs68743d62013-05-13 00:03:03 -0500242 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500243 synth->flush(synth);
244 continue;
245 }
Samuel Thibault89fc2ae2017-03-04 15:01:55 +0100246 synth_buffer_skip_nonlatin1();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500247 if (synth_buffer_empty()) {
William Hubbs68743d62013-05-13 00:03:03 -0500248 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500249 break;
250 }
251 ch = synth_buffer_peek();
252 set_current_state(TASK_INTERRUPTIBLE);
253 delay_time_val = delay_time->u.n.value;
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500254 synth_full_val = synth_full();
William Hubbs68743d62013-05-13 00:03:03 -0500255 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500256 if (ch == '\n')
257 ch = 0x0D;
Okash Khawaja1e441592017-03-14 13:41:53 +0000258 if (synth_full_val || !synth->io_ops->synth_out(synth, ch)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500259 schedule_timeout(msecs_to_jiffies(delay_time_val));
260 continue;
261 }
262 set_current_state(TASK_RUNNING);
William Hubbs68743d62013-05-13 00:03:03 -0500263 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500264 synth_buffer_getc();
William Hubbs68743d62013-05-13 00:03:03 -0500265 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
Arushi Singhal0dcb2122017-03-21 17:12:28 +0530266 if (ch == '[') {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500267 in_escape = 1;
Arushi Singhal0dcb2122017-03-21 17:12:28 +0530268 } else if (ch == ']') {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500269 in_escape = 0;
Arushi Singhal0dcb2122017-03-21 17:12:28 +0530270 } else if (ch <= SPACE) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500271 if (!in_escape && strchr(",.!?;:", last))
Okash Khawaja1e441592017-03-14 13:41:53 +0000272 synth->io_ops->synth_out(synth, PROCSPEECH);
Anil Belurdd3fde12014-06-30 18:55:48 +0530273 if (time_after_eq(jiffies, jiff_max)) {
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500274 if (!in_escape)
Bhagyashri Digholee2d55012019-02-28 17:59:04 +0530275 synth->io_ops->synth_out(synth,
276 PROCSPEECH);
Aybuke Ozdemir94b9c612014-03-18 20:45:59 +0200277 spin_lock_irqsave(&speakup_info.spinlock,
Santha Meena Ramamoorthyaccb9342018-03-05 09:34:13 -0800278 flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500279 jiffy_delta_val = jiffy_delta->u.n.value;
280 delay_time_val = delay_time->u.n.value;
Aybuke Ozdemir94b9c612014-03-18 20:45:59 +0200281 spin_unlock_irqrestore(&speakup_info.spinlock,
Santha Meena Ramamoorthyaccb9342018-03-05 09:34:13 -0800282 flags);
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500283 schedule_timeout(msecs_to_jiffies
284 (delay_time_val));
William Hubbsc6e3fd22010-10-07 13:20:02 -0500285 jiff_max = jiffies + jiffy_delta_val;
286 }
287 }
288 last = ch;
289 }
290 if (!in_escape)
Okash Khawaja1e441592017-03-14 13:41:53 +0000291 synth->io_ops->synth_out(synth, PROCSPEECH);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500292}
293
294static void synth_flush(struct spk_synth *synth)
295{
Shraddha Barkeca0b6fe2015-09-05 18:58:27 +0530296 if (in_escape)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500297 /* if in command output ']' so we don't get an error */
Okash Khawaja1e441592017-03-14 13:41:53 +0000298 synth->io_ops->synth_out(synth, ']');
William Hubbsc6e3fd22010-10-07 13:20:02 -0500299 in_escape = 0;
300 is_flushing = 1;
Samuel Thibault1941ab12021-01-26 23:21:44 +0100301 synth->io_ops->flush_buffer(synth);
Okash Khawaja1e441592017-03-14 13:41:53 +0000302 synth->io_ops->synth_out(synth, SYNTH_CLEAR);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500303}
304
Derek Robson73c37002017-01-28 19:35:01 +1300305module_param_named(ser, synth_dectlk.ser, int, 0444);
Mihaela Murarub5a603d2017-09-24 11:49:41 +0300306module_param_named(dev, synth_dectlk.dev_name, charp, 0444);
Derek Robson73c37002017-01-28 19:35:01 +1300307module_param_named(start, synth_dectlk.startup, short, 0444);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500308
309MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
Okash Khawaja8a21ff72017-06-25 19:40:02 +0100310MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
William Hubbsc6e3fd22010-10-07 13:20:02 -0500311MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
312
Vaishali Thakkarae89fac2015-03-18 23:13:10 +0530313module_spk_synth(synth_dectlk);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500314
William Hubbsc6e3fd22010-10-07 13:20:02 -0500315MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
316MODULE_AUTHOR("David Borowski");
317MODULE_DESCRIPTION("Speakup support for DECtalk Express synthesizers");
318MODULE_LICENSE("GPL");
319MODULE_VERSION(DRV_VERSION);
320