blob: 764656759fbf42000ac3acd5339d30a127c3a779 [file] [log] [blame]
William Hubbsc6e3fd22010-10-07 13:20:02 -05001/*
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 *
William Hubbsc6e3fd22010-10-07 13:20:02 -050018 * 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#include "serialio.h"
31
32#define DRV_VERSION "2.20"
33#define SYNTH_CLEAR 0x03
34#define PROCSPEECH 0x0b
Christopher Brannon4073f1b2010-10-14 19:24:00 -050035static int xoff;
36
37static inline int synth_full(void)
38{
39 return xoff;
40}
William Hubbsc6e3fd22010-10-07 13:20:02 -050041
42static void do_catch_up(struct spk_synth *synth);
43static void synth_flush(struct spk_synth *synth);
44static void read_buff_add(u_char c);
45static unsigned char get_index(void);
46
47static int in_escape;
48static int is_flushing;
49
50static spinlock_t flush_lock;
51static DECLARE_WAIT_QUEUE_HEAD(flush);
52
53static struct var_t vars[] = {
Christopher Brannon4073f1b2010-10-14 19:24:00 -050054 { CAPS_START, .u.s = {"[:dv ap 160] " } },
55 { CAPS_STOP, .u.s = {"[:dv ap 100 ] " } },
56 { RATE, .u.n = {"[:ra %d] ", 180, 75, 650, 0, 0, NULL } },
57 { PITCH, .u.n = {"[:dv ap %d] ", 122, 50, 350, 0, 0, NULL } },
58 { VOL, .u.n = {"[:dv g5 %d] ", 86, 60, 86, 0, 0, NULL } },
59 { PUNCT, .u.n = {"[:pu %c] ", 0, 0, 2, 0, 0, "nsa" } },
60 { VOICE, .u.n = {"[:n%c] ", 0, 0, 9, 0, 0, "phfdburwkv" } },
61 { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
William Hubbsc6e3fd22010-10-07 13:20:02 -050062 V_LAST_VAR
63};
64
65/*
66 * These attributes will appear in /sys/accessibility/speakup/dectlk.
67 */
68static struct kobj_attribute caps_start_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093069 __ATTR(caps_start, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050070static struct kobj_attribute caps_stop_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093071 __ATTR(caps_stop, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050072static struct kobj_attribute pitch_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093073 __ATTR(pitch, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050074static struct kobj_attribute punct_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093075 __ATTR(punct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050076static struct kobj_attribute rate_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093077 __ATTR(rate, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050078static struct kobj_attribute voice_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093079 __ATTR(voice, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050080static struct kobj_attribute vol_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093081 __ATTR(vol, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050082
83static struct kobj_attribute delay_time_attribute =
Rusty Russell22c9bca2014-04-01 13:30:05 +103084 __ATTR(delay_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050085static struct kobj_attribute direct_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093086 __ATTR(direct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050087static struct kobj_attribute full_time_attribute =
Rusty Russell22c9bca2014-04-01 13:30:05 +103088 __ATTR(full_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050089static struct kobj_attribute jiffy_delta_attribute =
Rusty Russell22c9bca2014-04-01 13:30:05 +103090 __ATTR(jiffy_delta, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050091static struct kobj_attribute trigger_time_attribute =
Rusty Russell22c9bca2014-04-01 13:30:05 +103092 __ATTR(trigger_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050093
94/*
95 * Create a group of attributes so that we can create and destroy them all
96 * at once.
97 */
98static struct attribute *synth_attrs[] = {
99 &caps_start_attribute.attr,
100 &caps_stop_attribute.attr,
101 &pitch_attribute.attr,
102 &punct_attribute.attr,
103 &rate_attribute.attr,
104 &voice_attribute.attr,
105 &vol_attribute.attr,
106 &delay_time_attribute.attr,
107 &direct_attribute.attr,
108 &full_time_attribute.attr,
109 &jiffy_delta_attribute.attr,
110 &trigger_time_attribute.attr,
111 NULL, /* need to NULL terminate the list of attributes */
112};
113
114static int ap_defaults[] = {122, 89, 155, 110, 208, 240, 200, 106, 306};
115static int g5_defaults[] = {86, 81, 86, 84, 81, 80, 83, 83, 73};
116
117static struct spk_synth synth_dectlk = {
118 .name = "dectlk",
119 .version = DRV_VERSION,
120 .long_name = "Dectalk Express",
121 .init = "[:error sp :name paul :rate 180 :tsr off] ",
122 .procspeech = PROCSPEECH,
123 .clear = SYNTH_CLEAR,
124 .delay = 500,
125 .trigger = 50,
126 .jiffies = 50,
127 .full = 40000,
128 .startup = SYNTH_START,
129 .checkval = SYNTH_CHECK,
130 .vars = vars,
131 .default_pitch = ap_defaults,
132 .default_vol = g5_defaults,
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100133 .probe = spk_serial_synth_probe,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500134 .release = spk_serial_release,
135 .synth_immediate = spk_synth_immediate,
136 .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
165static unsigned char get_index(void)
166{
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;
208 unsigned long timeout = msecs_to_jiffies(4000);
209 DEFINE_WAIT(wait);
210 struct var_t *jiffy_delta;
211 struct var_t *delay_time;
212 int jiffy_delta_val;
213 int delay_time_val;
214
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100215 jiffy_delta = spk_get_var(JIFFY);
216 delay_time = spk_get_var(DELAY);
William Hubbs68743d62013-05-13 00:03:03 -0500217 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500218 jiffy_delta_val = jiffy_delta->u.n.value;
William Hubbs68743d62013-05-13 00:03:03 -0500219 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500220 jiff_max = jiffies + jiffy_delta_val;
221
222 while (!kthread_should_stop()) {
223 /* if no ctl-a in 4, send data anyway */
224 spin_lock_irqsave(&flush_lock, flags);
225 while (is_flushing && timeout) {
226 prepare_to_wait(&flush, &wait, TASK_INTERRUPTIBLE);
227 spin_unlock_irqrestore(&flush_lock, flags);
228 timeout = schedule_timeout(timeout);
229 spin_lock_irqsave(&flush_lock, flags);
230 }
231 finish_wait(&flush, &wait);
232 is_flushing = 0;
233 spin_unlock_irqrestore(&flush_lock, flags);
234
William Hubbs68743d62013-05-13 00:03:03 -0500235 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500236 if (speakup_info.flushing) {
237 speakup_info.flushing = 0;
William Hubbs68743d62013-05-13 00:03:03 -0500238 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500239 synth->flush(synth);
240 continue;
241 }
242 if (synth_buffer_empty()) {
William Hubbs68743d62013-05-13 00:03:03 -0500243 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500244 break;
245 }
246 ch = synth_buffer_peek();
247 set_current_state(TASK_INTERRUPTIBLE);
248 delay_time_val = delay_time->u.n.value;
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500249 synth_full_val = synth_full();
William Hubbs68743d62013-05-13 00:03:03 -0500250 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500251 if (ch == '\n')
252 ch = 0x0D;
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500253 if (synth_full_val || !spk_serial_out(ch)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500254 schedule_timeout(msecs_to_jiffies(delay_time_val));
255 continue;
256 }
257 set_current_state(TASK_RUNNING);
William Hubbs68743d62013-05-13 00:03:03 -0500258 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500259 synth_buffer_getc();
William Hubbs68743d62013-05-13 00:03:03 -0500260 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500261 if (ch == '[')
262 in_escape = 1;
263 else if (ch == ']')
264 in_escape = 0;
265 else if (ch <= SPACE) {
266 if (!in_escape && strchr(",.!?;:", last))
267 spk_serial_out(PROCSPEECH);
Anil Belurdd3fde12014-06-30 18:55:48 +0530268 if (time_after_eq(jiffies, jiff_max)) {
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500269 if (!in_escape)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500270 spk_serial_out(PROCSPEECH);
Aybuke Ozdemir94b9c612014-03-18 20:45:59 +0200271 spin_lock_irqsave(&speakup_info.spinlock,
272 flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500273 jiffy_delta_val = jiffy_delta->u.n.value;
274 delay_time_val = delay_time->u.n.value;
Aybuke Ozdemir94b9c612014-03-18 20:45:59 +0200275 spin_unlock_irqrestore(&speakup_info.spinlock,
276 flags);
Christopher Brannon4073f1b2010-10-14 19:24:00 -0500277 schedule_timeout(msecs_to_jiffies
278 (delay_time_val));
William Hubbsc6e3fd22010-10-07 13:20:02 -0500279 jiff_max = jiffies + jiffy_delta_val;
280 }
281 }
282 last = ch;
283 }
284 if (!in_escape)
285 spk_serial_out(PROCSPEECH);
286}
287
288static void synth_flush(struct spk_synth *synth)
289{
Shraddha Barkeca0b6fe2015-09-05 18:58:27 +0530290 if (in_escape)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500291 /* if in command output ']' so we don't get an error */
292 spk_serial_out(']');
William Hubbsc6e3fd22010-10-07 13:20:02 -0500293 in_escape = 0;
294 is_flushing = 1;
295 spk_serial_out(SYNTH_CLEAR);
296}
297
298module_param_named(ser, synth_dectlk.ser, int, S_IRUGO);
299module_param_named(start, synth_dectlk.startup, short, S_IRUGO);
300
301MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
302MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
303
Vaishali Thakkarae89fac2015-03-18 23:13:10 +0530304module_spk_synth(synth_dectlk);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500305
William Hubbsc6e3fd22010-10-07 13:20:02 -0500306MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
307MODULE_AUTHOR("David Borowski");
308MODULE_DESCRIPTION("Speakup support for DECtalk Express synthesizers");
309MODULE_LICENSE("GPL");
310MODULE_VERSION(DRV_VERSION);
311