blob: 78ca4987e619e287001893eb09e22f90b39f7cb9 [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 Thibaultbca828c2022-02-06 02:56:26 +010047 { PITCH, .u.n = {"[:dv ap %d] ", 122, 50, 350, 0, 0, NULL } },
Samuel Thibaultd97a9d72020-04-25 21:32:26 +020048 { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
Christopher Brannon4073f1b2010-10-14 19:24:00 -050049 { 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 } },
William Hubbsc6e3fd22010-10-07 13:20:02 -050053 V_LAST_VAR
54};
55
56/*
57 * These attributes will appear in /sys/accessibility/speakup/dectlk.
58 */
59static struct kobj_attribute caps_start_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130060 __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050061static struct kobj_attribute caps_stop_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130062 __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050063static struct kobj_attribute pitch_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130064 __ATTR(pitch, 0644, spk_var_show, spk_var_store);
Samuel Thibaultd97a9d72020-04-25 21:32:26 +020065static struct kobj_attribute inflection_attribute =
66 __ATTR(inflection, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050067static struct kobj_attribute punct_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130068 __ATTR(punct, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050069static struct kobj_attribute rate_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130070 __ATTR(rate, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050071static struct kobj_attribute voice_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130072 __ATTR(voice, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050073static struct kobj_attribute vol_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130074 __ATTR(vol, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050075
76static struct kobj_attribute delay_time_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130077 __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050078static struct kobj_attribute direct_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130079 __ATTR(direct, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050080static struct kobj_attribute full_time_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130081 __ATTR(full_time, 0644, spk_var_show, spk_var_store);
Samuel Thibault1f7c14a2021-01-28 19:01:16 +010082static struct kobj_attribute flush_time_attribute =
83 __ATTR(flush_time, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050084static struct kobj_attribute jiffy_delta_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130085 __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050086static struct kobj_attribute trigger_time_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130087 __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050088
89/*
90 * Create a group of attributes so that we can create and destroy them all
91 * at once.
92 */
93static struct attribute *synth_attrs[] = {
94 &caps_start_attribute.attr,
95 &caps_stop_attribute.attr,
96 &pitch_attribute.attr,
Samuel Thibaultd97a9d72020-04-25 21:32:26 +020097 &inflection_attribute.attr,
William Hubbsc6e3fd22010-10-07 13:20:02 -050098 &punct_attribute.attr,
99 &rate_attribute.attr,
100 &voice_attribute.attr,
101 &vol_attribute.attr,
102 &delay_time_attribute.attr,
103 &direct_attribute.attr,
104 &full_time_attribute.attr,
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100105 &flush_time_attribute.attr,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500106 &jiffy_delta_attribute.attr,
107 &trigger_time_attribute.attr,
108 NULL, /* need to NULL terminate the list of attributes */
109};
110
111static int ap_defaults[] = {122, 89, 155, 110, 208, 240, 200, 106, 306};
112static int g5_defaults[] = {86, 81, 86, 84, 81, 80, 83, 83, 73};
113
114static struct spk_synth synth_dectlk = {
115 .name = "dectlk",
116 .version = DRV_VERSION,
117 .long_name = "Dectalk Express",
118 .init = "[:error sp :name paul :rate 180 :tsr off] ",
119 .procspeech = PROCSPEECH,
120 .clear = SYNTH_CLEAR,
121 .delay = 500,
122 .trigger = 50,
123 .jiffies = 50,
124 .full = 40000,
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100125 .flush_time = 4000,
Okash Khawaja8a21ff72017-06-25 19:40:02 +0100126 .dev_name = SYNTH_DEFAULT_DEV,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500127 .startup = SYNTH_START,
128 .checkval = SYNTH_CHECK,
129 .vars = vars,
130 .default_pitch = ap_defaults,
131 .default_vol = g5_defaults,
Okash Khawaja470790e2017-05-15 18:45:36 +0100132 .io_ops = &spk_ttyio_ops,
133 .probe = spk_ttyio_synth_probe,
134 .release = spk_ttyio_release,
135 .synth_immediate = spk_ttyio_synth_immediate,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500136 .catch_up = do_catch_up,
137 .flush = synth_flush,
138 .is_alive = spk_synth_is_alive_restart,
139 .synth_adjust = NULL,
140 .read_buff_add = read_buff_add,
141 .get_index = get_index,
142 .indexing = {
143 .command = "[:in re %d ] ",
144 .lowindex = 1,
145 .highindex = 8,
146 .currindex = 1,
147 },
148 .attributes = {
149 .attrs = synth_attrs,
150 .name = "dectlk",
151 },
152};
153
154static int is_indnum(u_char *ch)
155{
156 if ((*ch >= '0') && (*ch <= '9')) {
157 *ch = *ch - '0';
158 return 1;
159 }
160 return 0;
161}
162
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500163static u_char lastind;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500164
Okash Khawajaca693dc2017-04-29 20:52:58 +0100165static unsigned char get_index(struct spk_synth *synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500166{
167 u_char rv;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200168
William Hubbsc6e3fd22010-10-07 13:20:02 -0500169 rv = lastind;
170 lastind = 0;
171 return rv;
172}
173
174static void read_buff_add(u_char c)
175{
176 static int ind = -1;
177
178 if (c == 0x01) {
179 unsigned long flags;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200180
William Hubbsc6e3fd22010-10-07 13:20:02 -0500181 spin_lock_irqsave(&flush_lock, flags);
182 is_flushing = 0;
183 wake_up_interruptible(&flush);
184 spin_unlock_irqrestore(&flush_lock, flags);
185 } else if (c == 0x13) {
186 xoff = 1;
187 } else if (c == 0x11) {
188 xoff = 0;
189 } else if (is_indnum(&c)) {
190 if (ind == -1)
191 ind = c;
192 else
193 ind = ind * 10 + c;
194 } else if ((c > 31) && (c < 127)) {
195 if (ind != -1)
196 lastind = (u_char)ind;
197 ind = -1;
198 }
199}
200
201static void do_catch_up(struct spk_synth *synth)
202{
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500203 int synth_full_val = 0;
204 static u_char ch;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500205 static u_char last = '\0';
206 unsigned long flags;
207 unsigned long jiff_max;
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100208 unsigned long timeout;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500209 DEFINE_WAIT(wait);
210 struct var_t *jiffy_delta;
211 struct var_t *delay_time;
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100212 struct var_t *flush_time;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500213 int jiffy_delta_val;
214 int delay_time_val;
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100215 int timeout_val;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500216
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100217 jiffy_delta = spk_get_var(JIFFY);
218 delay_time = spk_get_var(DELAY);
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100219 flush_time = spk_get_var(FLUSH);
William Hubbs68743d62013-05-13 00:03:03 -0500220 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500221 jiffy_delta_val = jiffy_delta->u.n.value;
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100222 timeout_val = flush_time->u.n.value;
William Hubbs68743d62013-05-13 00:03:03 -0500223 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
Samuel Thibault1f7c14a2021-01-28 19:01:16 +0100224 timeout = msecs_to_jiffies(timeout_val);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500225 jiff_max = jiffies + jiffy_delta_val;
226
227 while (!kthread_should_stop()) {
228 /* if no ctl-a in 4, send data anyway */
229 spin_lock_irqsave(&flush_lock, flags);
230 while (is_flushing && timeout) {
231 prepare_to_wait(&flush, &wait, TASK_INTERRUPTIBLE);
232 spin_unlock_irqrestore(&flush_lock, flags);
233 timeout = schedule_timeout(timeout);
234 spin_lock_irqsave(&flush_lock, flags);
235 }
236 finish_wait(&flush, &wait);
237 is_flushing = 0;
238 spin_unlock_irqrestore(&flush_lock, flags);
239
William Hubbs68743d62013-05-13 00:03:03 -0500240 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500241 if (speakup_info.flushing) {
242 speakup_info.flushing = 0;
William Hubbs68743d62013-05-13 00:03:03 -0500243 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500244 synth->flush(synth);
245 continue;
246 }
Samuel Thibault89fc2ae2017-03-04 15:01:55 +0100247 synth_buffer_skip_nonlatin1();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500248 if (synth_buffer_empty()) {
William Hubbs68743d62013-05-13 00:03:03 -0500249 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500250 break;
251 }
252 ch = synth_buffer_peek();
253 set_current_state(TASK_INTERRUPTIBLE);
254 delay_time_val = delay_time->u.n.value;
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500255 synth_full_val = synth_full();
William Hubbs68743d62013-05-13 00:03:03 -0500256 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500257 if (ch == '\n')
258 ch = 0x0D;
Okash Khawaja1e441592017-03-14 13:41:53 +0000259 if (synth_full_val || !synth->io_ops->synth_out(synth, ch)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500260 schedule_timeout(msecs_to_jiffies(delay_time_val));
261 continue;
262 }
263 set_current_state(TASK_RUNNING);
William Hubbs68743d62013-05-13 00:03:03 -0500264 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500265 synth_buffer_getc();
William Hubbs68743d62013-05-13 00:03:03 -0500266 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
Arushi Singhal0dcb2122017-03-21 17:12:28 +0530267 if (ch == '[') {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500268 in_escape = 1;
Arushi Singhal0dcb2122017-03-21 17:12:28 +0530269 } else if (ch == ']') {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500270 in_escape = 0;
Arushi Singhal0dcb2122017-03-21 17:12:28 +0530271 } else if (ch <= SPACE) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500272 if (!in_escape && strchr(",.!?;:", last))
Okash Khawaja1e441592017-03-14 13:41:53 +0000273 synth->io_ops->synth_out(synth, PROCSPEECH);
Anil Belurdd3fde12014-06-30 18:55:48 +0530274 if (time_after_eq(jiffies, jiff_max)) {
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500275 if (!in_escape)
Bhagyashri Digholee2d55012019-02-28 17:59:04 +0530276 synth->io_ops->synth_out(synth,
277 PROCSPEECH);
Aybuke Ozdemir94b9c612014-03-18 20:45:59 +0200278 spin_lock_irqsave(&speakup_info.spinlock,
Santha Meena Ramamoorthyaccb9342018-03-05 09:34:13 -0800279 flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500280 jiffy_delta_val = jiffy_delta->u.n.value;
281 delay_time_val = delay_time->u.n.value;
Aybuke Ozdemir94b9c612014-03-18 20:45:59 +0200282 spin_unlock_irqrestore(&speakup_info.spinlock,
Santha Meena Ramamoorthyaccb9342018-03-05 09:34:13 -0800283 flags);
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500284 schedule_timeout(msecs_to_jiffies
285 (delay_time_val));
William Hubbsc6e3fd22010-10-07 13:20:02 -0500286 jiff_max = jiffies + jiffy_delta_val;
287 }
288 }
289 last = ch;
290 }
291 if (!in_escape)
Okash Khawaja1e441592017-03-14 13:41:53 +0000292 synth->io_ops->synth_out(synth, PROCSPEECH);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500293}
294
295static void synth_flush(struct spk_synth *synth)
296{
Shraddha Barkeca0b6fe2015-09-05 18:58:27 +0530297 if (in_escape)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500298 /* if in command output ']' so we don't get an error */
Okash Khawaja1e441592017-03-14 13:41:53 +0000299 synth->io_ops->synth_out(synth, ']');
William Hubbsc6e3fd22010-10-07 13:20:02 -0500300 in_escape = 0;
301 is_flushing = 1;
Samuel Thibault1941ab12021-01-26 23:21:44 +0100302 synth->io_ops->flush_buffer(synth);
Okash Khawaja1e441592017-03-14 13:41:53 +0000303 synth->io_ops->synth_out(synth, SYNTH_CLEAR);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500304}
305
Derek Robson73c37002017-01-28 19:35:01 +1300306module_param_named(ser, synth_dectlk.ser, int, 0444);
Mihaela Murarub5a603d2017-09-24 11:49:41 +0300307module_param_named(dev, synth_dectlk.dev_name, charp, 0444);
Derek Robson73c37002017-01-28 19:35:01 +1300308module_param_named(start, synth_dectlk.startup, short, 0444);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500309
310MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
Okash Khawaja8a21ff72017-06-25 19:40:02 +0100311MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
William Hubbsc6e3fd22010-10-07 13:20:02 -0500312MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
313
Vaishali Thakkarae89fac2015-03-18 23:13:10 +0530314module_spk_synth(synth_dectlk);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500315
William Hubbsc6e3fd22010-10-07 13:20:02 -0500316MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
317MODULE_AUTHOR("David Borowski");
318MODULE_DESCRIPTION("Speakup support for DECtalk Express synthesizers");
319MODULE_LICENSE("GPL");
320MODULE_VERSION(DRV_VERSION);
321