Staging: add speakup to the staging directory
[deliverable/linux.git] / drivers / staging / speakup / speakup_dectlk.c
1 /*
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 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * specificly written as a driver for the speakup screenreview
23 * s not a general device driver.
24 */
25 #include <linux/unistd.h>
26 #include <linux/proc_fs.h>
27 #include <linux/jiffies.h>
28 #include <linux/spinlock.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/kthread.h>
32 #include "speakup.h"
33 #include "spk_priv.h"
34 #include "serialio.h"
35
36 #define DRV_VERSION "2.20"
37 #define SYNTH_CLEAR 0x03
38 #define PROCSPEECH 0x0b
39 static volatile int xoff;
40 #define synth_full() (xoff)
41
42 static void do_catch_up(struct spk_synth *synth);
43 static void synth_flush(struct spk_synth *synth);
44 static void read_buff_add(u_char c);
45 static unsigned char get_index(void);
46
47 static int in_escape;
48 static int is_flushing;
49
50 static spinlock_t flush_lock;
51 static DECLARE_WAIT_QUEUE_HEAD(flush);
52
53 static struct var_t vars[] = {
54 { 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 }},
62 V_LAST_VAR
63 };
64
65 /*
66 * These attributes will appear in /sys/accessibility/speakup/dectlk.
67 */
68 static struct kobj_attribute caps_start_attribute =
69 __ATTR(caps_start, USER_RW, spk_var_show, spk_var_store);
70 static struct kobj_attribute caps_stop_attribute =
71 __ATTR(caps_stop, USER_RW, spk_var_show, spk_var_store);
72 static struct kobj_attribute pitch_attribute =
73 __ATTR(pitch, USER_RW, spk_var_show, spk_var_store);
74 static struct kobj_attribute punct_attribute =
75 __ATTR(punct, USER_RW, spk_var_show, spk_var_store);
76 static struct kobj_attribute rate_attribute =
77 __ATTR(rate, USER_RW, spk_var_show, spk_var_store);
78 static struct kobj_attribute voice_attribute =
79 __ATTR(voice, USER_RW, spk_var_show, spk_var_store);
80 static struct kobj_attribute vol_attribute =
81 __ATTR(vol, USER_RW, spk_var_show, spk_var_store);
82
83 static struct kobj_attribute delay_time_attribute =
84 __ATTR(delay_time, ROOT_W, spk_var_show, spk_var_store);
85 static struct kobj_attribute direct_attribute =
86 __ATTR(direct, USER_RW, spk_var_show, spk_var_store);
87 static struct kobj_attribute full_time_attribute =
88 __ATTR(full_time, ROOT_W, spk_var_show, spk_var_store);
89 static struct kobj_attribute jiffy_delta_attribute =
90 __ATTR(jiffy_delta, ROOT_W, spk_var_show, spk_var_store);
91 static struct kobj_attribute trigger_time_attribute =
92 __ATTR(trigger_time, ROOT_W, spk_var_show, spk_var_store);
93
94 /*
95 * Create a group of attributes so that we can create and destroy them all
96 * at once.
97 */
98 static 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
114 static int ap_defaults[] = {122, 89, 155, 110, 208, 240, 200, 106, 306};
115 static int g5_defaults[] = {86, 81, 86, 84, 81, 80, 83, 83, 73};
116
117 static 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,
133 .probe = serial_synth_probe,
134 .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
154 static 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
163 static u_char lastind = 0;
164
165 static unsigned char get_index(void)
166 {
167 u_char rv;
168 rv = lastind;
169 lastind = 0;
170 return rv;
171 }
172
173 static void read_buff_add(u_char c)
174 {
175 static int ind = -1;
176
177 if (c == 0x01) {
178 unsigned long flags;
179 spin_lock_irqsave(&flush_lock, flags);
180 is_flushing = 0;
181 wake_up_interruptible(&flush);
182 spin_unlock_irqrestore(&flush_lock, flags);
183 } else if (c == 0x13) {
184 xoff = 1;
185 } else if (c == 0x11) {
186 xoff = 0;
187 } else if (is_indnum(&c)) {
188 if (ind == -1)
189 ind = c;
190 else
191 ind = ind * 10 + c;
192 } else if ((c > 31) && (c < 127)) {
193 if (ind != -1)
194 lastind = (u_char)ind;
195 ind = -1;
196 }
197 }
198
199 static void do_catch_up(struct spk_synth *synth)
200 {
201 static u_char ch = 0;
202 static u_char last = '\0';
203 unsigned long flags;
204 unsigned long jiff_max;
205 unsigned long timeout = msecs_to_jiffies(4000);
206 DEFINE_WAIT(wait);
207 struct var_t *jiffy_delta;
208 struct var_t *delay_time;
209 int jiffy_delta_val;
210 int delay_time_val;
211
212 jiffy_delta = get_var(JIFFY);
213 delay_time = get_var(DELAY);
214 spk_lock(flags);
215 jiffy_delta_val = jiffy_delta->u.n.value;
216 spk_unlock(flags);
217 jiff_max = jiffies + jiffy_delta_val;
218
219 while (!kthread_should_stop()) {
220 /* if no ctl-a in 4, send data anyway */
221 spin_lock_irqsave(&flush_lock, flags);
222 while (is_flushing && timeout) {
223 prepare_to_wait(&flush, &wait, TASK_INTERRUPTIBLE);
224 spin_unlock_irqrestore(&flush_lock, flags);
225 timeout = schedule_timeout(timeout);
226 spin_lock_irqsave(&flush_lock, flags);
227 }
228 finish_wait(&flush, &wait);
229 is_flushing = 0;
230 spin_unlock_irqrestore(&flush_lock, flags);
231
232 spk_lock(flags);
233 if (speakup_info.flushing) {
234 speakup_info.flushing = 0;
235 spk_unlock(flags);
236 synth->flush(synth);
237 continue;
238 }
239 if (synth_buffer_empty()) {
240 spk_unlock(flags);
241 break;
242 }
243 ch = synth_buffer_peek();
244 set_current_state(TASK_INTERRUPTIBLE);
245 delay_time_val = delay_time->u.n.value;
246 spk_unlock(flags);
247 if (ch == '\n')
248 ch = 0x0D;
249 if (synth_full() || !spk_serial_out(ch)) {
250 schedule_timeout(msecs_to_jiffies(delay_time_val));
251 continue;
252 }
253 set_current_state(TASK_RUNNING);
254 spk_lock(flags);
255 synth_buffer_getc();
256 spk_unlock(flags);
257 if (ch == '[')
258 in_escape = 1;
259 else if (ch == ']')
260 in_escape = 0;
261 else if (ch <= SPACE) {
262 if (!in_escape && strchr(",.!?;:", last))
263 spk_serial_out(PROCSPEECH);
264 if (jiffies >= jiff_max) {
265 if ( ! in_escape )
266 spk_serial_out(PROCSPEECH);
267 spk_lock(flags);
268 jiffy_delta_val = jiffy_delta->u.n.value;
269 delay_time_val = delay_time->u.n.value;
270 spk_unlock(flags);
271 schedule_timeout(msecs_to_jiffies(delay_time_val));
272 jiff_max = jiffies + jiffy_delta_val;
273 }
274 }
275 last = ch;
276 }
277 if (!in_escape)
278 spk_serial_out(PROCSPEECH);
279 }
280
281 static void synth_flush(struct spk_synth *synth)
282 {
283 if (in_escape) {
284 /* if in command output ']' so we don't get an error */
285 spk_serial_out(']');
286 }
287 in_escape = 0;
288 is_flushing = 1;
289 spk_serial_out(SYNTH_CLEAR);
290 }
291
292 module_param_named(ser, synth_dectlk.ser, int, S_IRUGO);
293 module_param_named(start, synth_dectlk.startup, short, S_IRUGO);
294
295 MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
296 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
297
298 static int __init dectlk_init(void)
299 {
300 return synth_add(&synth_dectlk);
301 }
302
303 static void __exit dectlk_exit(void)
304 {
305 synth_remove(&synth_dectlk);
306 }
307
308 module_init(dectlk_init);
309 module_exit(dectlk_exit);
310 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
311 MODULE_AUTHOR("David Borowski");
312 MODULE_DESCRIPTION("Speakup support for DECtalk Express synthesizers");
313 MODULE_LICENSE("GPL");
314 MODULE_VERSION(DRV_VERSION);
315
This page took 0.03857 seconds and 5 git commands to generate.