162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * originally written by: Kirk Reiser <kirk@braille.uwo.ca>
462306a36Sopenharmony_ci * this version considerably modified by David Borowski, david575@rogers.com
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 1998-99  Kirk Reiser.
762306a36Sopenharmony_ci * Copyright (C) 2003 David Borowski.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * specifically written as a driver for the speakup screenreview
1062306a36Sopenharmony_ci * s not a general device driver.
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci#include <linux/jiffies.h>
1362306a36Sopenharmony_ci#include <linux/sched.h>
1462306a36Sopenharmony_ci#include <linux/timer.h>
1562306a36Sopenharmony_ci#include <linux/kthread.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "spk_priv.h"
1862306a36Sopenharmony_ci#include "speakup.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define DRV_VERSION "2.14"
2162306a36Sopenharmony_ci#define SYNTH_CLEAR 0x03
2262306a36Sopenharmony_ci#define PROCSPEECH 0x0b
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic volatile unsigned char last_char;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic void read_buff_add(u_char ch)
2762306a36Sopenharmony_ci{
2862306a36Sopenharmony_ci	last_char = ch;
2962306a36Sopenharmony_ci}
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistatic inline bool synth_full(void)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	return last_char == 0x13;
3462306a36Sopenharmony_ci}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth);
3762306a36Sopenharmony_cistatic void synth_flush(struct spk_synth *synth);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistatic int in_escape;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cienum default_vars_id {
4362306a36Sopenharmony_ci	CAPS_START_ID = 0, CAPS_STOP_ID,
4462306a36Sopenharmony_ci	RATE_ID, PITCH_ID, INFLECTION_ID,
4562306a36Sopenharmony_ci	VOL_ID, PUNCT_ID, VOICE_ID,
4662306a36Sopenharmony_ci	DIRECT_ID, V_LAST_ID,
4762306a36Sopenharmony_ci	NB_ID,
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic struct var_t vars[NB_ID] = {
5162306a36Sopenharmony_ci	[CAPS_START_ID] = { CAPS_START, .u.s = {"[:dv ap 222]" } },
5262306a36Sopenharmony_ci	[CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
5362306a36Sopenharmony_ci	[RATE_ID] = { RATE, .u.n = {"[:ra %d]", 7, 0, 9, 150, 25, NULL } },
5462306a36Sopenharmony_ci	[PITCH_ID] = { PITCH, .u.n = {"[:dv ap %d]", 100, 0, 100, 0, 0, NULL } },
5562306a36Sopenharmony_ci	[INFLECTION_ID] = { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
5662306a36Sopenharmony_ci	[VOL_ID] = { VOL, .u.n = {"[:dv gv %d]", 13, 0, 16, 0, 5, NULL } },
5762306a36Sopenharmony_ci	[PUNCT_ID] = { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
5862306a36Sopenharmony_ci	[VOICE_ID] = { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
5962306a36Sopenharmony_ci	[DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
6062306a36Sopenharmony_ci	V_LAST_VAR
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/*
6462306a36Sopenharmony_ci * These attributes will appear in /sys/accessibility/speakup/decext.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_cistatic struct kobj_attribute caps_start_attribute =
6762306a36Sopenharmony_ci	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
6862306a36Sopenharmony_cistatic struct kobj_attribute caps_stop_attribute =
6962306a36Sopenharmony_ci	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
7062306a36Sopenharmony_cistatic struct kobj_attribute pitch_attribute =
7162306a36Sopenharmony_ci	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
7262306a36Sopenharmony_cistatic struct kobj_attribute inflection_attribute =
7362306a36Sopenharmony_ci	__ATTR(inflection, 0644, spk_var_show, spk_var_store);
7462306a36Sopenharmony_cistatic struct kobj_attribute punct_attribute =
7562306a36Sopenharmony_ci	__ATTR(punct, 0644, spk_var_show, spk_var_store);
7662306a36Sopenharmony_cistatic struct kobj_attribute rate_attribute =
7762306a36Sopenharmony_ci	__ATTR(rate, 0644, spk_var_show, spk_var_store);
7862306a36Sopenharmony_cistatic struct kobj_attribute voice_attribute =
7962306a36Sopenharmony_ci	__ATTR(voice, 0644, spk_var_show, spk_var_store);
8062306a36Sopenharmony_cistatic struct kobj_attribute vol_attribute =
8162306a36Sopenharmony_ci	__ATTR(vol, 0644, spk_var_show, spk_var_store);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic struct kobj_attribute delay_time_attribute =
8462306a36Sopenharmony_ci	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
8562306a36Sopenharmony_cistatic struct kobj_attribute direct_attribute =
8662306a36Sopenharmony_ci	__ATTR(direct, 0644, spk_var_show, spk_var_store);
8762306a36Sopenharmony_cistatic struct kobj_attribute full_time_attribute =
8862306a36Sopenharmony_ci	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
8962306a36Sopenharmony_cistatic struct kobj_attribute jiffy_delta_attribute =
9062306a36Sopenharmony_ci	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
9162306a36Sopenharmony_cistatic struct kobj_attribute trigger_time_attribute =
9262306a36Sopenharmony_ci	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all
9662306a36Sopenharmony_ci * at once.
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_cistatic struct attribute *synth_attrs[] = {
9962306a36Sopenharmony_ci	&caps_start_attribute.attr,
10062306a36Sopenharmony_ci	&caps_stop_attribute.attr,
10162306a36Sopenharmony_ci	&pitch_attribute.attr,
10262306a36Sopenharmony_ci	&inflection_attribute.attr,
10362306a36Sopenharmony_ci	&punct_attribute.attr,
10462306a36Sopenharmony_ci	&rate_attribute.attr,
10562306a36Sopenharmony_ci	&voice_attribute.attr,
10662306a36Sopenharmony_ci	&vol_attribute.attr,
10762306a36Sopenharmony_ci	&delay_time_attribute.attr,
10862306a36Sopenharmony_ci	&direct_attribute.attr,
10962306a36Sopenharmony_ci	&full_time_attribute.attr,
11062306a36Sopenharmony_ci	&jiffy_delta_attribute.attr,
11162306a36Sopenharmony_ci	&trigger_time_attribute.attr,
11262306a36Sopenharmony_ci	NULL,	/* need to NULL terminate the list of attributes */
11362306a36Sopenharmony_ci};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cistatic struct spk_synth synth_decext = {
11662306a36Sopenharmony_ci	.name = "decext",
11762306a36Sopenharmony_ci	.version = DRV_VERSION,
11862306a36Sopenharmony_ci	.long_name = "Dectalk External",
11962306a36Sopenharmony_ci	.init = "[:pe -380]",
12062306a36Sopenharmony_ci	.procspeech = PROCSPEECH,
12162306a36Sopenharmony_ci	.clear = SYNTH_CLEAR,
12262306a36Sopenharmony_ci	.delay = 500,
12362306a36Sopenharmony_ci	.trigger = 50,
12462306a36Sopenharmony_ci	.jiffies = 50,
12562306a36Sopenharmony_ci	.full = 40000,
12662306a36Sopenharmony_ci	.flags = SF_DEC,
12762306a36Sopenharmony_ci	.dev_name = SYNTH_DEFAULT_DEV,
12862306a36Sopenharmony_ci	.startup = SYNTH_START,
12962306a36Sopenharmony_ci	.checkval = SYNTH_CHECK,
13062306a36Sopenharmony_ci	.vars = vars,
13162306a36Sopenharmony_ci	.io_ops = &spk_ttyio_ops,
13262306a36Sopenharmony_ci	.probe = spk_ttyio_synth_probe,
13362306a36Sopenharmony_ci	.release = spk_ttyio_release,
13462306a36Sopenharmony_ci	.synth_immediate = spk_ttyio_synth_immediate,
13562306a36Sopenharmony_ci	.catch_up = do_catch_up,
13662306a36Sopenharmony_ci	.flush = synth_flush,
13762306a36Sopenharmony_ci	.is_alive = spk_synth_is_alive_restart,
13862306a36Sopenharmony_ci	.synth_adjust = NULL,
13962306a36Sopenharmony_ci	.read_buff_add = read_buff_add,
14062306a36Sopenharmony_ci	.get_index = NULL,
14162306a36Sopenharmony_ci	.indexing = {
14262306a36Sopenharmony_ci		.command = NULL,
14362306a36Sopenharmony_ci		.lowindex = 0,
14462306a36Sopenharmony_ci		.highindex = 0,
14562306a36Sopenharmony_ci		.currindex = 0,
14662306a36Sopenharmony_ci	},
14762306a36Sopenharmony_ci	.attributes = {
14862306a36Sopenharmony_ci		.attrs = synth_attrs,
14962306a36Sopenharmony_ci		.name = "decext",
15062306a36Sopenharmony_ci	},
15162306a36Sopenharmony_ci};
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	u_char ch;
15662306a36Sopenharmony_ci	static u_char last = '\0';
15762306a36Sopenharmony_ci	unsigned long flags;
15862306a36Sopenharmony_ci	unsigned long jiff_max;
15962306a36Sopenharmony_ci	struct var_t *jiffy_delta;
16062306a36Sopenharmony_ci	struct var_t *delay_time;
16162306a36Sopenharmony_ci	int jiffy_delta_val = 0;
16262306a36Sopenharmony_ci	int delay_time_val = 0;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	jiffy_delta = spk_get_var(JIFFY);
16562306a36Sopenharmony_ci	delay_time = spk_get_var(DELAY);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
16862306a36Sopenharmony_ci	jiffy_delta_val = jiffy_delta->u.n.value;
16962306a36Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
17062306a36Sopenharmony_ci	jiff_max = jiffies + jiffy_delta_val;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	while (!kthread_should_stop()) {
17362306a36Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
17462306a36Sopenharmony_ci		if (speakup_info.flushing) {
17562306a36Sopenharmony_ci			speakup_info.flushing = 0;
17662306a36Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
17762306a36Sopenharmony_ci			synth->flush(synth);
17862306a36Sopenharmony_ci			continue;
17962306a36Sopenharmony_ci		}
18062306a36Sopenharmony_ci		synth_buffer_skip_nonlatin1();
18162306a36Sopenharmony_ci		if (synth_buffer_empty()) {
18262306a36Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
18362306a36Sopenharmony_ci			break;
18462306a36Sopenharmony_ci		}
18562306a36Sopenharmony_ci		ch = synth_buffer_peek();
18662306a36Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
18762306a36Sopenharmony_ci		delay_time_val = delay_time->u.n.value;
18862306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
18962306a36Sopenharmony_ci		if (ch == '\n')
19062306a36Sopenharmony_ci			ch = 0x0D;
19162306a36Sopenharmony_ci		if (synth_full() || !synth->io_ops->synth_out(synth, ch)) {
19262306a36Sopenharmony_ci			schedule_timeout(msecs_to_jiffies(delay_time_val));
19362306a36Sopenharmony_ci			continue;
19462306a36Sopenharmony_ci		}
19562306a36Sopenharmony_ci		set_current_state(TASK_RUNNING);
19662306a36Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
19762306a36Sopenharmony_ci		synth_buffer_getc();
19862306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
19962306a36Sopenharmony_ci		if (ch == '[') {
20062306a36Sopenharmony_ci			in_escape = 1;
20162306a36Sopenharmony_ci		} else if (ch == ']') {
20262306a36Sopenharmony_ci			in_escape = 0;
20362306a36Sopenharmony_ci		} else if (ch <= SPACE) {
20462306a36Sopenharmony_ci			if (!in_escape && strchr(",.!?;:", last))
20562306a36Sopenharmony_ci				synth->io_ops->synth_out(synth, PROCSPEECH);
20662306a36Sopenharmony_ci			if (time_after_eq(jiffies, jiff_max)) {
20762306a36Sopenharmony_ci				if (!in_escape)
20862306a36Sopenharmony_ci					synth->io_ops->synth_out(synth,
20962306a36Sopenharmony_ci								 PROCSPEECH);
21062306a36Sopenharmony_ci				spin_lock_irqsave(&speakup_info.spinlock,
21162306a36Sopenharmony_ci						  flags);
21262306a36Sopenharmony_ci				jiffy_delta_val = jiffy_delta->u.n.value;
21362306a36Sopenharmony_ci				delay_time_val = delay_time->u.n.value;
21462306a36Sopenharmony_ci				spin_unlock_irqrestore(&speakup_info.spinlock,
21562306a36Sopenharmony_ci						       flags);
21662306a36Sopenharmony_ci				schedule_timeout(msecs_to_jiffies
21762306a36Sopenharmony_ci						 (delay_time_val));
21862306a36Sopenharmony_ci				jiff_max = jiffies + jiffy_delta_val;
21962306a36Sopenharmony_ci			}
22062306a36Sopenharmony_ci		}
22162306a36Sopenharmony_ci		last = ch;
22262306a36Sopenharmony_ci	}
22362306a36Sopenharmony_ci	if (!in_escape)
22462306a36Sopenharmony_ci		synth->io_ops->synth_out(synth, PROCSPEECH);
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic void synth_flush(struct spk_synth *synth)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	in_escape = 0;
23062306a36Sopenharmony_ci	synth->io_ops->flush_buffer(synth);
23162306a36Sopenharmony_ci	synth->synth_immediate(synth, "\033P;10z\033\\");
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cimodule_param_named(ser, synth_decext.ser, int, 0444);
23562306a36Sopenharmony_cimodule_param_named(dev, synth_decext.dev_name, charp, 0444);
23662306a36Sopenharmony_cimodule_param_named(start, synth_decext.startup, short, 0444);
23762306a36Sopenharmony_cimodule_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
23862306a36Sopenharmony_cimodule_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
23962306a36Sopenharmony_cimodule_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);
24062306a36Sopenharmony_cimodule_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
24162306a36Sopenharmony_cimodule_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
24262306a36Sopenharmony_cimodule_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
24362306a36Sopenharmony_cimodule_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ciMODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
24762306a36Sopenharmony_ciMODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
24862306a36Sopenharmony_ciMODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
24962306a36Sopenharmony_ciMODULE_PARM_DESC(rate, "Set the rate variable on load.");
25062306a36Sopenharmony_ciMODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
25162306a36Sopenharmony_ciMODULE_PARM_DESC(inflection, "Set the inflection variable on load.");
25262306a36Sopenharmony_ciMODULE_PARM_DESC(vol, "Set the vol variable on load.");
25362306a36Sopenharmony_ciMODULE_PARM_DESC(punct, "Set the punct variable on load.");
25462306a36Sopenharmony_ciMODULE_PARM_DESC(voice, "Set the voice variable on load.");
25562306a36Sopenharmony_ciMODULE_PARM_DESC(direct, "Set the direct variable on load.");
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_cimodule_spk_synth(synth_decext);
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ciMODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
26062306a36Sopenharmony_ciMODULE_AUTHOR("David Borowski");
26162306a36Sopenharmony_ciMODULE_DESCRIPTION("Speakup support for DECtalk External synthesizers");
26262306a36Sopenharmony_ciMODULE_LICENSE("GPL");
26362306a36Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
26462306a36Sopenharmony_ci
265