GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / staging / speakup / speakup_decext.c
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/jiffies.h>
13 #include <linux/sched.h>
14 #include <linux/timer.h>
15 #include <linux/kthread.h>
16
17 #include "spk_priv.h"
18 #include "speakup.h"
19
20 #define DRV_VERSION "2.14"
21 #define SYNTH_CLEAR 0x03
22 #define PROCSPEECH 0x0b
23
24 static volatile unsigned char last_char;
25
26 static void read_buff_add(u_char ch)
27 {
28         last_char = ch;
29 }
30
31 static inline bool synth_full(void)
32 {
33         return last_char == 0x13;
34 }
35
36 static void do_catch_up(struct spk_synth *synth);
37 static void synth_flush(struct spk_synth *synth);
38
39 static int in_escape;
40
41 static struct var_t vars[] = {
42         { CAPS_START, .u.s = {"[:dv ap 222]" } },
43         { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
44         { RATE, .u.n = {"[:ra %d]", 7, 0, 9, 150, 25, NULL } },
45         { PITCH, .u.n = {"[:dv ap %d]", 100, 0, 100, 0, 0, NULL } },
46         { VOL, .u.n = {"[:dv gv %d]", 13, 0, 16, 0, 5, NULL } },
47         { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
48         { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
49         { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
50         V_LAST_VAR
51 };
52
53 /*
54  * These attributes will appear in /sys/accessibility/speakup/decext.
55  */
56 static struct kobj_attribute caps_start_attribute =
57         __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
58 static struct kobj_attribute caps_stop_attribute =
59         __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
60 static struct kobj_attribute pitch_attribute =
61         __ATTR(pitch, 0644, spk_var_show, spk_var_store);
62 static struct kobj_attribute punct_attribute =
63         __ATTR(punct, 0644, spk_var_show, spk_var_store);
64 static struct kobj_attribute rate_attribute =
65         __ATTR(rate, 0644, spk_var_show, spk_var_store);
66 static struct kobj_attribute voice_attribute =
67         __ATTR(voice, 0644, spk_var_show, spk_var_store);
68 static struct kobj_attribute vol_attribute =
69         __ATTR(vol, 0644, spk_var_show, spk_var_store);
70
71 static struct kobj_attribute delay_time_attribute =
72         __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
73 static struct kobj_attribute direct_attribute =
74         __ATTR(direct, 0644, spk_var_show, spk_var_store);
75 static struct kobj_attribute full_time_attribute =
76         __ATTR(full_time, 0644, spk_var_show, spk_var_store);
77 static struct kobj_attribute jiffy_delta_attribute =
78         __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
79 static struct kobj_attribute trigger_time_attribute =
80         __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
81
82 /*
83  * Create a group of attributes so that we can create and destroy them all
84  * at once.
85  */
86 static struct attribute *synth_attrs[] = {
87         &caps_start_attribute.attr,
88         &caps_stop_attribute.attr,
89         &pitch_attribute.attr,
90         &punct_attribute.attr,
91         &rate_attribute.attr,
92         &voice_attribute.attr,
93         &vol_attribute.attr,
94         &delay_time_attribute.attr,
95         &direct_attribute.attr,
96         &full_time_attribute.attr,
97         &jiffy_delta_attribute.attr,
98         &trigger_time_attribute.attr,
99         NULL,   /* need to NULL terminate the list of attributes */
100 };
101
102 static struct spk_synth synth_decext = {
103         .name = "decext",
104         .version = DRV_VERSION,
105         .long_name = "Dectalk External",
106         .init = "[:pe -380]",
107         .procspeech = PROCSPEECH,
108         .clear = SYNTH_CLEAR,
109         .delay = 500,
110         .trigger = 50,
111         .jiffies = 50,
112         .full = 40000,
113         .flags = SF_DEC,
114         .dev_name = SYNTH_DEFAULT_DEV,
115         .startup = SYNTH_START,
116         .checkval = SYNTH_CHECK,
117         .vars = vars,
118         .io_ops = &spk_ttyio_ops,
119         .probe = spk_ttyio_synth_probe,
120         .release = spk_ttyio_release,
121         .synth_immediate = spk_ttyio_synth_immediate,
122         .catch_up = do_catch_up,
123         .flush = synth_flush,
124         .is_alive = spk_synth_is_alive_restart,
125         .synth_adjust = NULL,
126         .read_buff_add = read_buff_add,
127         .get_index = NULL,
128         .indexing = {
129                 .command = NULL,
130                 .lowindex = 0,
131                 .highindex = 0,
132                 .currindex = 0,
133         },
134         .attributes = {
135                 .attrs = synth_attrs,
136                 .name = "decext",
137         },
138 };
139
140 static void do_catch_up(struct spk_synth *synth)
141 {
142         u_char ch;
143         static u_char last = '\0';
144         unsigned long flags;
145         unsigned long jiff_max;
146         struct var_t *jiffy_delta;
147         struct var_t *delay_time;
148         int jiffy_delta_val = 0;
149         int delay_time_val = 0;
150
151         jiffy_delta = spk_get_var(JIFFY);
152         delay_time = spk_get_var(DELAY);
153
154         spin_lock_irqsave(&speakup_info.spinlock, flags);
155         jiffy_delta_val = jiffy_delta->u.n.value;
156         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
157         jiff_max = jiffies + jiffy_delta_val;
158
159         while (!kthread_should_stop()) {
160                 spin_lock_irqsave(&speakup_info.spinlock, flags);
161                 if (speakup_info.flushing) {
162                         speakup_info.flushing = 0;
163                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
164                         synth->flush(synth);
165                         continue;
166                 }
167                 synth_buffer_skip_nonlatin1();
168                 if (synth_buffer_empty()) {
169                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
170                         break;
171                 }
172                 ch = synth_buffer_peek();
173                 set_current_state(TASK_INTERRUPTIBLE);
174                 delay_time_val = delay_time->u.n.value;
175                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
176                 if (ch == '\n')
177                         ch = 0x0D;
178                 if (synth_full() || !synth->io_ops->synth_out(synth, ch)) {
179                         schedule_timeout(msecs_to_jiffies(delay_time_val));
180                         continue;
181                 }
182                 set_current_state(TASK_RUNNING);
183                 spin_lock_irqsave(&speakup_info.spinlock, flags);
184                 synth_buffer_getc();
185                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
186                 if (ch == '[') {
187                         in_escape = 1;
188                 } else if (ch == ']') {
189                         in_escape = 0;
190                 } else if (ch <= SPACE) {
191                         if (!in_escape && strchr(",.!?;:", last))
192                                 synth->io_ops->synth_out(synth, PROCSPEECH);
193                         if (time_after_eq(jiffies, jiff_max)) {
194                                 if (!in_escape)
195                                         synth->io_ops->synth_out(synth, PROCSPEECH);
196                                 spin_lock_irqsave(&speakup_info.spinlock,
197                                                   flags);
198                                 jiffy_delta_val = jiffy_delta->u.n.value;
199                                 delay_time_val = delay_time->u.n.value;
200                                 spin_unlock_irqrestore(&speakup_info.spinlock,
201                                                        flags);
202                                 schedule_timeout(msecs_to_jiffies
203                                                  (delay_time_val));
204                                 jiff_max = jiffies + jiffy_delta_val;
205                         }
206                 }
207                 last = ch;
208         }
209         if (!in_escape)
210                 synth->io_ops->synth_out(synth, PROCSPEECH);
211 }
212
213 static void synth_flush(struct spk_synth *synth)
214 {
215         in_escape = 0;
216         synth->io_ops->flush_buffer();
217         synth->synth_immediate(synth, "\033P;10z\033\\");
218 }
219
220 module_param_named(ser, synth_decext.ser, int, 0444);
221 module_param_named(dev, synth_decext.dev_name, charp, 0444);
222 module_param_named(start, synth_decext.startup, short, 0444);
223
224 MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
225 MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
226 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
227
228 module_spk_synth(synth_decext);
229
230 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
231 MODULE_AUTHOR("David Borowski");
232 MODULE_DESCRIPTION("Speakup support for DECtalk External synthesizers");
233 MODULE_LICENSE("GPL");
234 MODULE_VERSION(DRV_VERSION);
235