blob: a9dd5c45d237465367b8f22c63816c64f459f19d [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>
Arushi Singhal4d0bdcb2017-02-12 16:15:58 +05304 * this version considerably modified by David Borowski, david575@rogers.com
William Hubbsc6e3fd22010-10-07 13:20:02 -05005 *
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 * package it's not a general device driver.
11 * This driver is for the RC Systems DoubleTalk PC internal synthesizer.
12 */
13#include <linux/jiffies.h>
14#include <linux/sched.h>
15#include <linux/timer.h>
16#include <linux/kthread.h>
17
18#include "spk_priv.h"
19#include "serialio.h"
20#include "speakup_dtlk.h" /* local header file for DoubleTalk values */
21#include "speakup.h"
22
23#define DRV_VERSION "2.10"
24#define PROCSPEECH 0x00
William Hubbsc6e3fd22010-10-07 13:20:02 -050025
26static int synth_probe(struct spk_synth *synth);
Samuel Thibault1941ab12021-01-26 23:21:44 +010027static void dtlk_release(struct spk_synth *synth);
William Hubbsc6e3fd22010-10-07 13:20:02 -050028static const char *synth_immediate(struct spk_synth *synth, const char *buf);
29static void do_catch_up(struct spk_synth *synth);
30static void synth_flush(struct spk_synth *synth);
31
32static int synth_lpc;
33static int port_forced;
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -050034static unsigned int synth_portlist[] = {
35 0x25e, 0x29e, 0x2de, 0x31e, 0x35e, 0x39e, 0
36};
Arushi Singhaldefaa9a2017-03-14 10:46:42 +053037
William Hubbsc6e3fd22010-10-07 13:20:02 -050038static u_char synth_status;
39
40static struct var_t vars[] = {
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -050041 { CAPS_START, .u.s = {"\x01+35p" } },
42 { CAPS_STOP, .u.s = {"\x01-35p" } },
43 { RATE, .u.n = {"\x01%ds", 8, 0, 9, 0, 0, NULL } },
44 { PITCH, .u.n = {"\x01%dp", 50, 0, 99, 0, 0, NULL } },
45 { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL } },
46 { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL } },
47 { PUNCT, .u.n = {"\x01%db", 7, 0, 15, 0, 0, NULL } },
48 { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL } },
49 { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL } },
50 { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
William Hubbsc6e3fd22010-10-07 13:20:02 -050051 V_LAST_VAR
52};
53
54/*
55 * These attributes will appear in /sys/accessibility/speakup/dtlk.
56 */
57static struct kobj_attribute caps_start_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130058 __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050059static struct kobj_attribute caps_stop_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130060 __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050061static struct kobj_attribute freq_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130062 __ATTR(freq, 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);
William Hubbsc6e3fd22010-10-07 13:20:02 -050065static struct kobj_attribute punct_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130066 __ATTR(punct, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050067static struct kobj_attribute rate_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130068 __ATTR(rate, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050069static struct kobj_attribute tone_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130070 __ATTR(tone, 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);
William Hubbsc6e3fd22010-10-07 13:20:02 -050082static struct kobj_attribute jiffy_delta_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130083 __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050084static struct kobj_attribute trigger_time_attribute =
Derek Robson73c37002017-01-28 19:35:01 +130085 __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
William Hubbsc6e3fd22010-10-07 13:20:02 -050086
87/*
88 * Create a group of attributes so that we can create and destroy them all
89 * at once.
90 */
91static struct attribute *synth_attrs[] = {
92 &caps_start_attribute.attr,
93 &caps_stop_attribute.attr,
94 &freq_attribute.attr,
95 &pitch_attribute.attr,
96 &punct_attribute.attr,
97 &rate_attribute.attr,
98 &tone_attribute.attr,
99 &voice_attribute.attr,
100 &vol_attribute.attr,
101 &delay_time_attribute.attr,
102 &direct_attribute.attr,
103 &full_time_attribute.attr,
104 &jiffy_delta_attribute.attr,
105 &trigger_time_attribute.attr,
106 NULL, /* need to NULL terminate the list of attributes */
107};
108
109static struct spk_synth synth_dtlk = {
110 .name = "dtlk",
111 .version = DRV_VERSION,
112 .long_name = "DoubleTalk PC",
113 .init = "\x01@\x01\x31y",
114 .procspeech = PROCSPEECH,
115 .clear = SYNTH_CLEAR,
116 .delay = 500,
117 .trigger = 30,
118 .jiffies = 50,
119 .full = 1000,
120 .startup = SYNTH_START,
121 .checkval = SYNTH_CHECK,
122 .vars = vars,
Okash Khawaja1e441592017-03-14 13:41:53 +0000123 .io_ops = &spk_serial_io_ops,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500124 .probe = synth_probe,
125 .release = dtlk_release,
126 .synth_immediate = synth_immediate,
127 .catch_up = do_catch_up,
128 .flush = synth_flush,
129 .is_alive = spk_synth_is_alive_nop,
130 .synth_adjust = NULL,
131 .read_buff_add = NULL,
Okash Khawajaca693dc2017-04-29 20:52:58 +0100132 .get_index = spk_synth_get_index,
William Hubbsc6e3fd22010-10-07 13:20:02 -0500133 .indexing = {
134 .command = "\x01%di",
135 .lowindex = 1,
136 .highindex = 5,
137 .currindex = 1,
138 },
139 .attributes = {
140 .attrs = synth_attrs,
141 .name = "dtlk",
142 },
143};
144
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500145static inline bool synth_readable(void)
146{
147 synth_status = inb_p(speakup_info.port_tts + UART_RX);
148 return (synth_status & TTS_READABLE) != 0;
149}
150
151static inline bool synth_writable(void)
152{
153 synth_status = inb_p(speakup_info.port_tts + UART_RX);
154 return (synth_status & TTS_WRITABLE) != 0;
155}
156
157static inline bool synth_full(void)
158{
159 synth_status = inb_p(speakup_info.port_tts + UART_RX);
160 return (synth_status & TTS_ALMOST_FULL) != 0;
161}
162
William Hubbsc6e3fd22010-10-07 13:20:02 -0500163static void spk_out(const char ch)
164{
165 int timeout = SPK_XMITR_TIMEOUT;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200166
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500167 while (!synth_writable()) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500168 if (!--timeout)
169 break;
170 udelay(1);
171 }
172 outb_p(ch, speakup_info.port_tts);
173 timeout = SPK_XMITR_TIMEOUT;
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500174 while (synth_writable()) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500175 if (!--timeout)
176 break;
177 udelay(1);
178 }
179}
180
181static void do_catch_up(struct spk_synth *synth)
182{
183 u_char ch;
184 unsigned long flags;
185 unsigned long jiff_max;
186 struct var_t *jiffy_delta;
187 struct var_t *delay_time;
188 int jiffy_delta_val;
189 int delay_time_val;
190
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100191 jiffy_delta = spk_get_var(JIFFY);
192 delay_time = spk_get_var(DELAY);
William Hubbs62c8c832013-05-13 13:31:38 -0500193 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500194 jiffy_delta_val = jiffy_delta->u.n.value;
William Hubbs62c8c832013-05-13 13:31:38 -0500195 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500196 jiff_max = jiffies + jiffy_delta_val;
197 while (!kthread_should_stop()) {
William Hubbs62c8c832013-05-13 13:31:38 -0500198 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500199 if (speakup_info.flushing) {
200 speakup_info.flushing = 0;
William Hubbs62c8c832013-05-13 13:31:38 -0500201 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500202 synth->flush(synth);
203 continue;
204 }
Samuel Thibault89fc2ae2017-03-04 15:01:55 +0100205 synth_buffer_skip_nonlatin1();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500206 if (synth_buffer_empty()) {
William Hubbs62c8c832013-05-13 13:31:38 -0500207 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500208 break;
209 }
210 set_current_state(TASK_INTERRUPTIBLE);
211 delay_time_val = delay_time->u.n.value;
William Hubbs62c8c832013-05-13 13:31:38 -0500212 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500213 if (synth_full()) {
214 schedule_timeout(msecs_to_jiffies(delay_time_val));
215 continue;
216 }
217 set_current_state(TASK_RUNNING);
William Hubbs62c8c832013-05-13 13:31:38 -0500218 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500219 ch = synth_buffer_getc();
William Hubbs62c8c832013-05-13 13:31:38 -0500220 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500221 if (ch == '\n')
222 ch = PROCSPEECH;
223 spk_out(ch);
Esra Altintasc65cfb52014-10-07 22:45:55 +0300224 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500225 spk_out(PROCSPEECH);
William Hubbs62c8c832013-05-13 13:31:38 -0500226 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500227 delay_time_val = delay_time->u.n.value;
228 jiffy_delta_val = jiffy_delta->u.n.value;
William Hubbs62c8c832013-05-13 13:31:38 -0500229 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500230 schedule_timeout(msecs_to_jiffies(delay_time_val));
231 jiff_max = jiffies + jiffy_delta_val;
232 }
233 }
234 spk_out(PROCSPEECH);
235}
236
237static const char *synth_immediate(struct spk_synth *synth, const char *buf)
238{
239 u_char ch;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200240
William Hubbsc6e3fd22010-10-07 13:20:02 -0500241 while ((ch = (u_char)*buf)) {
242 if (synth_full())
243 return buf;
244 if (ch == '\n')
245 ch = PROCSPEECH;
246 spk_out(ch);
247 buf++;
248 }
Sachin Kamat61b7a282013-05-22 14:37:21 +0530249 return NULL;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500250}
251
252static void synth_flush(struct spk_synth *synth)
253{
254 outb_p(SYNTH_CLEAR, speakup_info.port_tts);
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500255 while (synth_writable())
William Hubbsc6e3fd22010-10-07 13:20:02 -0500256 cpu_relax();
257}
258
259static char synth_read_tts(void)
260{
261 u_char ch;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200262
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500263 while (!synth_readable())
William Hubbsc6e3fd22010-10-07 13:20:02 -0500264 cpu_relax();
265 ch = synth_status & 0x7f;
266 outb_p(ch, speakup_info.port_tts);
Christopher Brannon2c9e0cb2010-10-14 19:23:53 -0500267 while (synth_readable())
William Hubbsc6e3fd22010-10-07 13:20:02 -0500268 cpu_relax();
Santha Meena Ramamoorthy721dfe42018-02-22 10:15:11 -0800269 return (char)ch;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500270}
271
272/* interrogate the DoubleTalk PC and return its settings */
273static struct synth_settings *synth_interrogate(struct spk_synth *synth)
274{
275 u_char *t;
276 static char buf[sizeof(struct synth_settings) + 1];
277 int total, i;
278 static struct synth_settings status;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200279
William Hubbsc6e3fd22010-10-07 13:20:02 -0500280 synth_immediate(synth, "\x18\x01?");
281 for (total = 0, i = 0; i < 50; i++) {
282 buf[total] = synth_read_tts();
283 if (total > 2 && buf[total] == 0x7f)
284 break;
285 if (total < sizeof(struct synth_settings))
286 total++;
287 }
288 t = buf;
289 /* serial number is little endian */
Santha Meena Ramamoorthy0f7be572018-02-22 10:15:09 -0800290 status.serial_number = t[0] + t[1] * 256;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500291 t += 2;
292 for (i = 0; *t != '\r'; t++) {
293 status.rom_version[i] = *t;
Arushi Singhal205931e2017-03-21 17:12:31 +0530294 if (i < sizeof(status.rom_version) - 1)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500295 i++;
296 }
297 status.rom_version[i] = 0;
298 t++;
299 status.mode = *t++;
300 status.punc_level = *t++;
301 status.formant_freq = *t++;
302 status.pitch = *t++;
303 status.speed = *t++;
304 status.volume = *t++;
305 status.tone = *t++;
306 status.expression = *t++;
307 status.ext_dict_loaded = *t++;
308 status.ext_dict_status = *t++;
309 status.free_ram = *t++;
310 status.articulation = *t++;
311 status.reverb = *t++;
312 status.eob = *t++;
313 return &status;
314}
315
316static int synth_probe(struct spk_synth *synth)
317{
Alan Cox7aa4d5c2014-12-15 11:33:08 +0000318 unsigned int port_val = 0;
Colin Ian King1f8ff522021-11-10 23:33:42 +0000319 int i;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500320 struct synth_settings *sp;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200321
William Hubbsc6e3fd22010-10-07 13:20:02 -0500322 pr_info("Probing for DoubleTalk.\n");
323 if (port_forced) {
324 speakup_info.port_tts = port_forced;
325 pr_info("probe forced to %x by kernel command line\n",
Santha Meena Ramamoorthyf6222d12018-02-22 10:15:10 -0800326 speakup_info.port_tts);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500327 if ((port_forced & 0xf) != 0xf)
328 pr_info("warning: port base should probably end with f\n");
Santha Meena Ramamoorthy0f7be572018-02-22 10:15:09 -0800329 if (synth_request_region(speakup_info.port_tts - 1,
Santha Meena Ramamoorthyf6222d12018-02-22 10:15:10 -0800330 SYNTH_IO_EXTENT)) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500331 pr_warn("sorry, port already reserved\n");
332 return -EBUSY;
333 }
Santha Meena Ramamoorthy0f7be572018-02-22 10:15:09 -0800334 port_val = inw(speakup_info.port_tts - 1);
335 synth_lpc = speakup_info.port_tts - 1;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500336 } else {
337 for (i = 0; synth_portlist[i]; i++) {
338 if (synth_request_region(synth_portlist[i],
Santha Meena Ramamoorthyf6222d12018-02-22 10:15:10 -0800339 SYNTH_IO_EXTENT))
William Hubbsc6e3fd22010-10-07 13:20:02 -0500340 continue;
341 port_val = inw(synth_portlist[i]) & 0xfbff;
342 if (port_val == 0x107f) {
343 synth_lpc = synth_portlist[i];
Santha Meena Ramamoorthy0f7be572018-02-22 10:15:09 -0800344 speakup_info.port_tts = synth_lpc + 1;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500345 break;
346 }
347 synth_release_region(synth_portlist[i],
Santha Meena Ramamoorthyf6222d12018-02-22 10:15:10 -0800348 SYNTH_IO_EXTENT);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500349 }
350 }
351 port_val &= 0xfbff;
352 if (port_val != 0x107f) {
353 pr_info("DoubleTalk PC: not found\n");
Alan Cox7aa4d5c2014-12-15 11:33:08 +0000354 if (synth_lpc)
355 synth_release_region(synth_lpc, SYNTH_IO_EXTENT);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500356 return -ENODEV;
357 }
358 while (inw_p(synth_lpc) != 0x147f)
359 cpu_relax(); /* wait until it's ready */
360 sp = synth_interrogate(synth);
361 pr_info("%s: %03x-%03x, ROM ver %s, s/n %u, driver: %s\n",
Santha Meena Ramamoorthy0f7be572018-02-22 10:15:09 -0800362 synth->long_name, synth_lpc, synth_lpc + SYNTH_IO_EXTENT - 1,
Alan Cox7aa4d5c2014-12-15 11:33:08 +0000363 sp->rom_version, sp->serial_number, synth->version);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500364 synth->alive = 1;
365 return 0;
366}
367
Samuel Thibault1941ab12021-01-26 23:21:44 +0100368static void dtlk_release(struct spk_synth *synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500369{
Okash Khawajaa50ef312017-03-14 13:41:54 +0000370 spk_stop_serial_interrupt();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500371 if (speakup_info.port_tts)
Santha Meena Ramamoorthy0f7be572018-02-22 10:15:09 -0800372 synth_release_region(speakup_info.port_tts - 1,
373 SYNTH_IO_EXTENT);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500374 speakup_info.port_tts = 0;
375}
376
David Howellsdbf05cb2017-04-04 16:54:28 +0100377module_param_hw_named(port, port_forced, int, ioport, 0444);
Derek Robson73c37002017-01-28 19:35:01 +1300378module_param_named(start, synth_dtlk.startup, short, 0444);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500379
380MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
381MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
382
Vaishali Thakkarae89fac2015-03-18 23:13:10 +0530383module_spk_synth(synth_dtlk);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500384
William Hubbsc6e3fd22010-10-07 13:20:02 -0500385MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
386MODULE_AUTHOR("David Borowski");
387MODULE_DESCRIPTION("Speakup support for DoubleTalk PC synthesizers");
388MODULE_LICENSE("GPL");
389MODULE_VERSION(DRV_VERSION);
390