1 // SPDX-License-Identifier: GPL-2.0+
3 * originally written by: Kirk Reiser <kirk@braille.uwo.ca>
4 * this version considerably modified by David Borowski, david575@rogers.com
6 * Copyright (C) 1998-99 Kirk Reiser.
7 * Copyright (C) 2003 David Borowski.
9 * 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.
13 #include <linux/jiffies.h>
14 #include <linux/sched.h>
15 #include <linux/timer.h>
16 #include <linux/kthread.h>
20 #include "speakup_dtlk.h" /* local header file for DoubleTalk values */
23 #define DRV_VERSION "2.10"
24 #define PROCSPEECH 0x00
26 static int synth_probe(struct spk_synth *synth);
27 static void dtlk_release(void);
28 static const char *synth_immediate(struct spk_synth *synth, const char *buf);
29 static void do_catch_up(struct spk_synth *synth);
30 static void synth_flush(struct spk_synth *synth);
33 static int port_forced;
34 static unsigned int synth_portlist[] = {
35 0x25e, 0x29e, 0x2de, 0x31e, 0x35e, 0x39e, 0
38 static u_char synth_status;
40 static struct var_t vars[] = {
41 { 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 } },
55 * These attributes will appear in /sys/accessibility/speakup/dtlk.
57 static struct kobj_attribute caps_start_attribute =
58 __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
59 static struct kobj_attribute caps_stop_attribute =
60 __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
61 static struct kobj_attribute freq_attribute =
62 __ATTR(freq, 0644, spk_var_show, spk_var_store);
63 static struct kobj_attribute pitch_attribute =
64 __ATTR(pitch, 0644, spk_var_show, spk_var_store);
65 static struct kobj_attribute punct_attribute =
66 __ATTR(punct, 0644, spk_var_show, spk_var_store);
67 static struct kobj_attribute rate_attribute =
68 __ATTR(rate, 0644, spk_var_show, spk_var_store);
69 static struct kobj_attribute tone_attribute =
70 __ATTR(tone, 0644, spk_var_show, spk_var_store);
71 static struct kobj_attribute voice_attribute =
72 __ATTR(voice, 0644, spk_var_show, spk_var_store);
73 static struct kobj_attribute vol_attribute =
74 __ATTR(vol, 0644, spk_var_show, spk_var_store);
76 static struct kobj_attribute delay_time_attribute =
77 __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
78 static struct kobj_attribute direct_attribute =
79 __ATTR(direct, 0644, spk_var_show, spk_var_store);
80 static struct kobj_attribute full_time_attribute =
81 __ATTR(full_time, 0644, spk_var_show, spk_var_store);
82 static struct kobj_attribute jiffy_delta_attribute =
83 __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
84 static struct kobj_attribute trigger_time_attribute =
85 __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
88 * Create a group of attributes so that we can create and destroy them all
91 static struct attribute *synth_attrs[] = {
92 &caps_start_attribute.attr,
93 &caps_stop_attribute.attr,
95 &pitch_attribute.attr,
96 &punct_attribute.attr,
99 &voice_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 */
109 static struct spk_synth synth_dtlk = {
111 .version = DRV_VERSION,
112 .long_name = "DoubleTalk PC",
113 .init = "\x01@\x01\x31y",
114 .procspeech = PROCSPEECH,
115 .clear = SYNTH_CLEAR,
120 .startup = SYNTH_START,
121 .checkval = SYNTH_CHECK,
123 .io_ops = &spk_serial_io_ops,
124 .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,
132 .get_index = spk_synth_get_index,
134 .command = "\x01%di",
140 .attrs = synth_attrs,
145 static inline bool synth_readable(void)
147 synth_status = inb_p(speakup_info.port_tts + UART_RX);
148 return (synth_status & TTS_READABLE) != 0;
151 static inline bool synth_writable(void)
153 synth_status = inb_p(speakup_info.port_tts + UART_RX);
154 return (synth_status & TTS_WRITABLE) != 0;
157 static inline bool synth_full(void)
159 synth_status = inb_p(speakup_info.port_tts + UART_RX);
160 return (synth_status & TTS_ALMOST_FULL) != 0;
163 static void spk_out(const char ch)
165 int timeout = SPK_XMITR_TIMEOUT;
167 while (!synth_writable()) {
172 outb_p(ch, speakup_info.port_tts);
173 timeout = SPK_XMITR_TIMEOUT;
174 while (synth_writable()) {
181 static void do_catch_up(struct spk_synth *synth)
185 unsigned long jiff_max;
186 struct var_t *jiffy_delta;
187 struct var_t *delay_time;
191 jiffy_delta = spk_get_var(JIFFY);
192 delay_time = spk_get_var(DELAY);
193 spin_lock_irqsave(&speakup_info.spinlock, flags);
194 jiffy_delta_val = jiffy_delta->u.n.value;
195 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
196 jiff_max = jiffies + jiffy_delta_val;
197 while (!kthread_should_stop()) {
198 spin_lock_irqsave(&speakup_info.spinlock, flags);
199 if (speakup_info.flushing) {
200 speakup_info.flushing = 0;
201 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
205 synth_buffer_skip_nonlatin1();
206 if (synth_buffer_empty()) {
207 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
210 set_current_state(TASK_INTERRUPTIBLE);
211 delay_time_val = delay_time->u.n.value;
212 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
214 schedule_timeout(msecs_to_jiffies(delay_time_val));
217 set_current_state(TASK_RUNNING);
218 spin_lock_irqsave(&speakup_info.spinlock, flags);
219 ch = synth_buffer_getc();
220 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
224 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
226 spin_lock_irqsave(&speakup_info.spinlock, flags);
227 delay_time_val = delay_time->u.n.value;
228 jiffy_delta_val = jiffy_delta->u.n.value;
229 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
230 schedule_timeout(msecs_to_jiffies(delay_time_val));
231 jiff_max = jiffies + jiffy_delta_val;
237 static const char *synth_immediate(struct spk_synth *synth, const char *buf)
241 while ((ch = (u_char)*buf)) {
252 static void synth_flush(struct spk_synth *synth)
254 outb_p(SYNTH_CLEAR, speakup_info.port_tts);
255 while (synth_writable())
259 static char synth_read_tts(void)
263 while (!synth_readable())
265 ch = synth_status & 0x7f;
266 outb_p(ch, speakup_info.port_tts);
267 while (synth_readable())
272 /* interrogate the DoubleTalk PC and return its settings */
273 static struct synth_settings *synth_interrogate(struct spk_synth *synth)
276 static char buf[sizeof(struct synth_settings) + 1];
278 static struct synth_settings status;
280 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)
285 if (total < sizeof(struct synth_settings))
289 /* serial number is little endian */
290 status.serial_number = t[0] + t[1] * 256;
292 for (i = 0; *t != '\r'; t++) {
293 status.rom_version[i] = *t;
294 if (i < sizeof(status.rom_version) - 1)
297 status.rom_version[i] = 0;
300 status.punc_level = *t++;
301 status.formant_freq = *t++;
304 status.volume = *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++;
316 static int synth_probe(struct spk_synth *synth)
318 unsigned int port_val = 0;
320 struct synth_settings *sp;
322 pr_info("Probing for DoubleTalk.\n");
324 speakup_info.port_tts = port_forced;
325 pr_info("probe forced to %x by kernel command line\n",
326 speakup_info.port_tts);
327 if ((port_forced & 0xf) != 0xf)
328 pr_info("warning: port base should probably end with f\n");
329 if (synth_request_region(speakup_info.port_tts - 1,
331 pr_warn("sorry, port already reserved\n");
334 port_val = inw(speakup_info.port_tts - 1);
335 synth_lpc = speakup_info.port_tts - 1;
337 for (i = 0; synth_portlist[i]; i++) {
338 if (synth_request_region(synth_portlist[i],
341 port_val = inw(synth_portlist[i]) & 0xfbff;
342 if (port_val == 0x107f) {
343 synth_lpc = synth_portlist[i];
344 speakup_info.port_tts = synth_lpc + 1;
347 synth_release_region(synth_portlist[i],
352 if (port_val != 0x107f) {
353 pr_info("DoubleTalk PC: not found\n");
355 synth_release_region(synth_lpc, SYNTH_IO_EXTENT);
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",
362 synth->long_name, synth_lpc, synth_lpc + SYNTH_IO_EXTENT - 1,
363 sp->rom_version, sp->serial_number, synth->version);
368 static void dtlk_release(void)
370 spk_stop_serial_interrupt();
371 if (speakup_info.port_tts)
372 synth_release_region(speakup_info.port_tts - 1,
374 speakup_info.port_tts = 0;
377 module_param_hw_named(port, port_forced, int, ioport, 0444);
378 module_param_named(start, synth_dtlk.startup, short, 0444);
380 MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
381 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
383 module_spk_synth(synth_dtlk);
385 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
386 MODULE_AUTHOR("David Borowski");
387 MODULE_DESCRIPTION("Speakup support for DoubleTalk PC synthesizers");
388 MODULE_LICENSE("GPL");
389 MODULE_VERSION(DRV_VERSION);