blob: 345efd3344b06b11a66363add33fb2a7b62c6f67 [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 *
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 * package it's not a general device driver.
24 * This driver is for the RC Systems DoubleTalk PC internal synthesizer.
25 */
26#include <linux/jiffies.h>
27#include <linux/sched.h>
28#include <linux/timer.h>
29#include <linux/kthread.h>
30
31#include "spk_priv.h"
32#include "serialio.h"
33#include "speakup_dtlk.h" /* local header file for DoubleTalk values */
34#include "speakup.h"
35
36#define DRV_VERSION "2.10"
37#define PROCSPEECH 0x00
William Hubbsc6e3fd22010-10-07 13:20:02 -050038
39static int synth_probe(struct spk_synth *synth);
40static void dtlk_release(void);
41static const char *synth_immediate(struct spk_synth *synth, const char *buf);
42static void do_catch_up(struct spk_synth *synth);
43static void synth_flush(struct spk_synth *synth);
44
45static int synth_lpc;
46static int port_forced;
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -050047static unsigned int synth_portlist[] = {
48 0x25e, 0x29e, 0x2de, 0x31e, 0x35e, 0x39e, 0
49};
William Hubbsc6e3fd22010-10-07 13:20:02 -050050static u_char synth_status;
51
52static struct var_t vars[] = {
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -050053 { CAPS_START, .u.s = {"\x01+35p" } },
54 { CAPS_STOP, .u.s = {"\x01-35p" } },
55 { RATE, .u.n = {"\x01%ds", 8, 0, 9, 0, 0, NULL } },
56 { PITCH, .u.n = {"\x01%dp", 50, 0, 99, 0, 0, NULL } },
57 { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
58 { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
59 { PUNCT, .u.n = {"\x01%db", 7, 0, 15, 0, 0, NULL } },
60 { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
61 { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
62 { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
William Hubbsc6e3fd22010-10-07 13:20:02 -050063 V_LAST_VAR
64};
65
66/*
67 * These attributes will appear in /sys/accessibility/speakup/dtlk.
68 */
69static struct kobj_attribute caps_start_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093070 __ATTR(caps_start, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050071static struct kobj_attribute caps_stop_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093072 __ATTR(caps_stop, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050073static struct kobj_attribute freq_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093074 __ATTR(freq, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050075static struct kobj_attribute pitch_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093076 __ATTR(pitch, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050077static struct kobj_attribute punct_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093078 __ATTR(punct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050079static struct kobj_attribute rate_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093080 __ATTR(rate, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050081static struct kobj_attribute tone_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093082 __ATTR(tone, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050083static struct kobj_attribute voice_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093084 __ATTR(voice, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050085static struct kobj_attribute vol_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093086 __ATTR(vol, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050087
88static struct kobj_attribute delay_time_attribute =
Rusty Russell22c9bca2014-04-01 13:30:05 +103089 __ATTR(delay_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050090static struct kobj_attribute direct_attribute =
Rusty Russelld901aaa2014-04-24 13:57:49 +093091 __ATTR(direct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050092static struct kobj_attribute full_time_attribute =
Rusty Russell22c9bca2014-04-01 13:30:05 +103093 __ATTR(full_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050094static struct kobj_attribute jiffy_delta_attribute =
Rusty Russell22c9bca2014-04-01 13:30:05 +103095 __ATTR(jiffy_delta, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050096static struct kobj_attribute trigger_time_attribute =
Rusty Russell22c9bca2014-04-01 13:30:05 +103097 __ATTR(trigger_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050098
99/*
100 * Create a group of attributes so that we can create and destroy them all
101 * at once.
102 */
103static struct attribute *synth_attrs[] = {
104 &caps_start_attribute.attr,
105 &caps_stop_attribute.attr,
106 &freq_attribute.attr,
107 &pitch_attribute.attr,
108 &punct_attribute.attr,
109 &rate_attribute.attr,
110 &tone_attribute.attr,
111 &voice_attribute.attr,
112 &vol_attribute.attr,
113 &delay_time_attribute.attr,
114 &direct_attribute.attr,
115 &full_time_attribute.attr,
116 &jiffy_delta_attribute.attr,
117 &trigger_time_attribute.attr,
118 NULL, /* need to NULL terminate the list of attributes */
119};
120
121static struct spk_synth synth_dtlk = {
122 .name = "dtlk",
123 .version = DRV_VERSION,
124 .long_name = "DoubleTalk PC",
125 .init = "\x01@\x01\x31y",
126 .procspeech = PROCSPEECH,
127 .clear = SYNTH_CLEAR,
128 .delay = 500,
129 .trigger = 30,
130 .jiffies = 50,
131 .full = 1000,
132 .startup = SYNTH_START,
133 .checkval = SYNTH_CHECK,
134 .vars = vars,
135 .probe = synth_probe,
136 .release = dtlk_release,
137 .synth_immediate = synth_immediate,
138 .catch_up = do_catch_up,
139 .flush = synth_flush,
140 .is_alive = spk_synth_is_alive_nop,
141 .synth_adjust = NULL,
142 .read_buff_add = NULL,
143 .get_index = spk_serial_in_nowait,
144 .indexing = {
145 .command = "\x01%di",
146 .lowindex = 1,
147 .highindex = 5,
148 .currindex = 1,
149 },
150 .attributes = {
151 .attrs = synth_attrs,
152 .name = "dtlk",
153 },
154};
155
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500156static inline bool synth_readable(void)
157{
158 synth_status = inb_p(speakup_info.port_tts + UART_RX);
159 return (synth_status & TTS_READABLE) != 0;
160}
161
162static inline bool synth_writable(void)
163{
164 synth_status = inb_p(speakup_info.port_tts + UART_RX);
165 return (synth_status & TTS_WRITABLE) != 0;
166}
167
168static inline bool synth_full(void)
169{
170 synth_status = inb_p(speakup_info.port_tts + UART_RX);
171 return (synth_status & TTS_ALMOST_FULL) != 0;
172}
173
William Hubbsc6e3fd22010-10-07 13:20:02 -0500174static void spk_out(const char ch)
175{
176 int timeout = SPK_XMITR_TIMEOUT;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200177
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500178 while (!synth_writable()) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500179 if (!--timeout)
180 break;
181 udelay(1);
182 }
183 outb_p(ch, speakup_info.port_tts);
184 timeout = SPK_XMITR_TIMEOUT;
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500185 while (synth_writable()) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500186 if (!--timeout)
187 break;
188 udelay(1);
189 }
190}
191
192static void do_catch_up(struct spk_synth *synth)
193{
194 u_char ch;
195 unsigned long flags;
196 unsigned long jiff_max;
197 struct var_t *jiffy_delta;
198 struct var_t *delay_time;
199 int jiffy_delta_val;
200 int delay_time_val;
201
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100202 jiffy_delta = spk_get_var(JIFFY);
203 delay_time = spk_get_var(DELAY);
William Hubbs62c8c832013-05-13 13:31:38 -0500204 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500205 jiffy_delta_val = jiffy_delta->u.n.value;
William Hubbs62c8c832013-05-13 13:31:38 -0500206 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500207 jiff_max = jiffies + jiffy_delta_val;
208 while (!kthread_should_stop()) {
William Hubbs62c8c832013-05-13 13:31:38 -0500209 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500210 if (speakup_info.flushing) {
211 speakup_info.flushing = 0;
William Hubbs62c8c832013-05-13 13:31:38 -0500212 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500213 synth->flush(synth);
214 continue;
215 }
216 if (synth_buffer_empty()) {
William Hubbs62c8c832013-05-13 13:31:38 -0500217 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500218 break;
219 }
220 set_current_state(TASK_INTERRUPTIBLE);
221 delay_time_val = delay_time->u.n.value;
William Hubbs62c8c832013-05-13 13:31:38 -0500222 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500223 if (synth_full()) {
224 schedule_timeout(msecs_to_jiffies(delay_time_val));
225 continue;
226 }
227 set_current_state(TASK_RUNNING);
William Hubbs62c8c832013-05-13 13:31:38 -0500228 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500229 ch = synth_buffer_getc();
William Hubbs62c8c832013-05-13 13:31:38 -0500230 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500231 if (ch == '\n')
232 ch = PROCSPEECH;
233 spk_out(ch);
Esra Altintasc65cfb52014-10-07 22:45:55 +0300234 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500235 spk_out(PROCSPEECH);
William Hubbs62c8c832013-05-13 13:31:38 -0500236 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500237 delay_time_val = delay_time->u.n.value;
238 jiffy_delta_val = jiffy_delta->u.n.value;
William Hubbs62c8c832013-05-13 13:31:38 -0500239 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500240 schedule_timeout(msecs_to_jiffies(delay_time_val));
241 jiff_max = jiffies + jiffy_delta_val;
242 }
243 }
244 spk_out(PROCSPEECH);
245}
246
247static const char *synth_immediate(struct spk_synth *synth, const char *buf)
248{
249 u_char ch;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200250
William Hubbsc6e3fd22010-10-07 13:20:02 -0500251 while ((ch = (u_char)*buf)) {
252 if (synth_full())
253 return buf;
254 if (ch == '\n')
255 ch = PROCSPEECH;
256 spk_out(ch);
257 buf++;
258 }
Sachin Kamat61b7a282013-05-22 14:37:21 +0530259 return NULL;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500260}
261
262static void synth_flush(struct spk_synth *synth)
263{
264 outb_p(SYNTH_CLEAR, speakup_info.port_tts);
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500265 while (synth_writable())
William Hubbsc6e3fd22010-10-07 13:20:02 -0500266 cpu_relax();
267}
268
269static char synth_read_tts(void)
270{
271 u_char ch;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200272
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500273 while (!synth_readable())
William Hubbsc6e3fd22010-10-07 13:20:02 -0500274 cpu_relax();
275 ch = synth_status & 0x7f;
276 outb_p(ch, speakup_info.port_tts);
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500277 while (synth_readable())
William Hubbsc6e3fd22010-10-07 13:20:02 -0500278 cpu_relax();
279 return (char) ch;
280}
281
282/* interrogate the DoubleTalk PC and return its settings */
283static struct synth_settings *synth_interrogate(struct spk_synth *synth)
284{
285 u_char *t;
286 static char buf[sizeof(struct synth_settings) + 1];
287 int total, i;
288 static struct synth_settings status;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200289
William Hubbsc6e3fd22010-10-07 13:20:02 -0500290 synth_immediate(synth, "\x18\x01?");
291 for (total = 0, i = 0; i < 50; i++) {
292 buf[total] = synth_read_tts();
293 if (total > 2 && buf[total] == 0x7f)
294 break;
295 if (total < sizeof(struct synth_settings))
296 total++;
297 }
298 t = buf;
299 /* serial number is little endian */
300 status.serial_number = t[0] + t[1]*256;
301 t += 2;
302 for (i = 0; *t != '\r'; t++) {
303 status.rom_version[i] = *t;
304 if (i < sizeof(status.rom_version)-1)
305 i++;
306 }
307 status.rom_version[i] = 0;
308 t++;
309 status.mode = *t++;
310 status.punc_level = *t++;
311 status.formant_freq = *t++;
312 status.pitch = *t++;
313 status.speed = *t++;
314 status.volume = *t++;
315 status.tone = *t++;
316 status.expression = *t++;
317 status.ext_dict_loaded = *t++;
318 status.ext_dict_status = *t++;
319 status.free_ram = *t++;
320 status.articulation = *t++;
321 status.reverb = *t++;
322 status.eob = *t++;
323 return &status;
324}
325
326static int synth_probe(struct spk_synth *synth)
327{
Alan Cox7aa4d5c2014-12-15 11:33:08 +0000328 unsigned int port_val = 0;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500329 int i = 0;
330 struct synth_settings *sp;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200331
William Hubbsc6e3fd22010-10-07 13:20:02 -0500332 pr_info("Probing for DoubleTalk.\n");
333 if (port_forced) {
334 speakup_info.port_tts = port_forced;
335 pr_info("probe forced to %x by kernel command line\n",
336 speakup_info.port_tts);
337 if ((port_forced & 0xf) != 0xf)
338 pr_info("warning: port base should probably end with f\n");
339 if (synth_request_region(speakup_info.port_tts-1,
340 SYNTH_IO_EXTENT)) {
341 pr_warn("sorry, port already reserved\n");
342 return -EBUSY;
343 }
344 port_val = inw(speakup_info.port_tts-1);
345 synth_lpc = speakup_info.port_tts-1;
346 } else {
347 for (i = 0; synth_portlist[i]; i++) {
348 if (synth_request_region(synth_portlist[i],
349 SYNTH_IO_EXTENT))
350 continue;
351 port_val = inw(synth_portlist[i]) & 0xfbff;
352 if (port_val == 0x107f) {
353 synth_lpc = synth_portlist[i];
354 speakup_info.port_tts = synth_lpc+1;
355 break;
356 }
357 synth_release_region(synth_portlist[i],
358 SYNTH_IO_EXTENT);
359 }
360 }
361 port_val &= 0xfbff;
362 if (port_val != 0x107f) {
363 pr_info("DoubleTalk PC: not found\n");
Alan Cox7aa4d5c2014-12-15 11:33:08 +0000364 if (synth_lpc)
365 synth_release_region(synth_lpc, SYNTH_IO_EXTENT);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500366 return -ENODEV;
367 }
368 while (inw_p(synth_lpc) != 0x147f)
369 cpu_relax(); /* wait until it's ready */
370 sp = synth_interrogate(synth);
371 pr_info("%s: %03x-%03x, ROM ver %s, s/n %u, driver: %s\n",
372 synth->long_name, synth_lpc, synth_lpc+SYNTH_IO_EXTENT - 1,
Alan Cox7aa4d5c2014-12-15 11:33:08 +0000373 sp->rom_version, sp->serial_number, synth->version);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500374 synth->alive = 1;
375 return 0;
376}
377
378static void dtlk_release(void)
379{
380 if (speakup_info.port_tts)
381 synth_release_region(speakup_info.port_tts-1, SYNTH_IO_EXTENT);
382 speakup_info.port_tts = 0;
383}
384
385module_param_named(port, port_forced, int, S_IRUGO);
386module_param_named(start, synth_dtlk.startup, short, S_IRUGO);
387
388MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
389MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
390
Vaishali Thakkarae89fac2015-03-18 23:13:10 +0530391module_spk_synth(synth_dtlk);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500392
William Hubbsc6e3fd22010-10-07 13:20:02 -0500393MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
394MODULE_AUTHOR("David Borowski");
395MODULE_DESCRIPTION("Speakup support for DoubleTalk PC synthesizers");
396MODULE_LICENSE("GPL");
397MODULE_VERSION(DRV_VERSION);
398