18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * originally written by: Kirk Reiser <kirk@braille.uwo.ca>
48c2ecf20Sopenharmony_ci * this version considerably modified by David Borowski, david575@rogers.com
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 1998-99  Kirk Reiser.
78c2ecf20Sopenharmony_ci * Copyright (C) 2003 David Borowski.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * this code is specificly written as a driver for the speakup screenreview
108c2ecf20Sopenharmony_ci * package and is not a general device driver.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
138c2ecf20Sopenharmony_ci#include <linux/sched.h>
148c2ecf20Sopenharmony_ci#include <linux/timer.h>
158c2ecf20Sopenharmony_ci#include <linux/kthread.h>
168c2ecf20Sopenharmony_ci#include <linux/serial_reg.h>	/* for UART_MCR* constants */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "spk_priv.h"
198c2ecf20Sopenharmony_ci#include "speakup.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define DRV_VERSION "2.21"
228c2ecf20Sopenharmony_ci#define SYNTH_CLEAR 0x18
238c2ecf20Sopenharmony_ci#define PROCSPEECH '\r'
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic struct var_t vars[] = {
288c2ecf20Sopenharmony_ci	{ CAPS_START, .u.s = {"cap, " } },
298c2ecf20Sopenharmony_ci	{ CAPS_STOP, .u.s = {"" } },
308c2ecf20Sopenharmony_ci	{ RATE, .u.n = {"@W%d", 6, 1, 9, 0, 0, NULL } },
318c2ecf20Sopenharmony_ci	{ PITCH, .u.n = {"@F%x", 10, 0, 15, 0, 0, NULL } },
328c2ecf20Sopenharmony_ci	{ VOL, .u.n = {"@A%x", 10, 0, 15, 0, 0, NULL } },
338c2ecf20Sopenharmony_ci	{ VOICE, .u.n = {"@V%d", 1, 1, 6, 0, 0, NULL } },
348c2ecf20Sopenharmony_ci	{ LANG, .u.n = {"@=%d,", 1, 1, 4, 0, 0, NULL } },
358c2ecf20Sopenharmony_ci	{ DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
368c2ecf20Sopenharmony_ci	V_LAST_VAR
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * These attributes will appear in /sys/accessibility/speakup/apollo.
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_cistatic struct kobj_attribute caps_start_attribute =
438c2ecf20Sopenharmony_ci	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
448c2ecf20Sopenharmony_cistatic struct kobj_attribute caps_stop_attribute =
458c2ecf20Sopenharmony_ci	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
468c2ecf20Sopenharmony_cistatic struct kobj_attribute lang_attribute =
478c2ecf20Sopenharmony_ci	__ATTR(lang, 0644, spk_var_show, spk_var_store);
488c2ecf20Sopenharmony_cistatic struct kobj_attribute pitch_attribute =
498c2ecf20Sopenharmony_ci	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
508c2ecf20Sopenharmony_cistatic struct kobj_attribute rate_attribute =
518c2ecf20Sopenharmony_ci	__ATTR(rate, 0644, spk_var_show, spk_var_store);
528c2ecf20Sopenharmony_cistatic struct kobj_attribute voice_attribute =
538c2ecf20Sopenharmony_ci	__ATTR(voice, 0644, spk_var_show, spk_var_store);
548c2ecf20Sopenharmony_cistatic struct kobj_attribute vol_attribute =
558c2ecf20Sopenharmony_ci	__ATTR(vol, 0644, spk_var_show, spk_var_store);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic struct kobj_attribute delay_time_attribute =
588c2ecf20Sopenharmony_ci	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
598c2ecf20Sopenharmony_cistatic struct kobj_attribute direct_attribute =
608c2ecf20Sopenharmony_ci	__ATTR(direct, 0644, spk_var_show, spk_var_store);
618c2ecf20Sopenharmony_cistatic struct kobj_attribute full_time_attribute =
628c2ecf20Sopenharmony_ci	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
638c2ecf20Sopenharmony_cistatic struct kobj_attribute jiffy_delta_attribute =
648c2ecf20Sopenharmony_ci	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
658c2ecf20Sopenharmony_cistatic struct kobj_attribute trigger_time_attribute =
668c2ecf20Sopenharmony_ci	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all
708c2ecf20Sopenharmony_ci * at once.
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_cistatic struct attribute *synth_attrs[] = {
738c2ecf20Sopenharmony_ci	&caps_start_attribute.attr,
748c2ecf20Sopenharmony_ci	&caps_stop_attribute.attr,
758c2ecf20Sopenharmony_ci	&lang_attribute.attr,
768c2ecf20Sopenharmony_ci	&pitch_attribute.attr,
778c2ecf20Sopenharmony_ci	&rate_attribute.attr,
788c2ecf20Sopenharmony_ci	&voice_attribute.attr,
798c2ecf20Sopenharmony_ci	&vol_attribute.attr,
808c2ecf20Sopenharmony_ci	&delay_time_attribute.attr,
818c2ecf20Sopenharmony_ci	&direct_attribute.attr,
828c2ecf20Sopenharmony_ci	&full_time_attribute.attr,
838c2ecf20Sopenharmony_ci	&jiffy_delta_attribute.attr,
848c2ecf20Sopenharmony_ci	&trigger_time_attribute.attr,
858c2ecf20Sopenharmony_ci	NULL,	/* need to NULL terminate the list of attributes */
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic struct spk_synth synth_apollo = {
898c2ecf20Sopenharmony_ci	.name = "apollo",
908c2ecf20Sopenharmony_ci	.version = DRV_VERSION,
918c2ecf20Sopenharmony_ci	.long_name = "Apollo",
928c2ecf20Sopenharmony_ci	.init = "@R3@D0@K1\r",
938c2ecf20Sopenharmony_ci	.procspeech = PROCSPEECH,
948c2ecf20Sopenharmony_ci	.clear = SYNTH_CLEAR,
958c2ecf20Sopenharmony_ci	.delay = 500,
968c2ecf20Sopenharmony_ci	.trigger = 50,
978c2ecf20Sopenharmony_ci	.jiffies = 50,
988c2ecf20Sopenharmony_ci	.full = 40000,
998c2ecf20Sopenharmony_ci	.dev_name = SYNTH_DEFAULT_DEV,
1008c2ecf20Sopenharmony_ci	.startup = SYNTH_START,
1018c2ecf20Sopenharmony_ci	.checkval = SYNTH_CHECK,
1028c2ecf20Sopenharmony_ci	.vars = vars,
1038c2ecf20Sopenharmony_ci	.io_ops = &spk_ttyio_ops,
1048c2ecf20Sopenharmony_ci	.probe = spk_ttyio_synth_probe,
1058c2ecf20Sopenharmony_ci	.release = spk_ttyio_release,
1068c2ecf20Sopenharmony_ci	.synth_immediate = spk_ttyio_synth_immediate,
1078c2ecf20Sopenharmony_ci	.catch_up = do_catch_up,
1088c2ecf20Sopenharmony_ci	.flush = spk_synth_flush,
1098c2ecf20Sopenharmony_ci	.is_alive = spk_synth_is_alive_restart,
1108c2ecf20Sopenharmony_ci	.synth_adjust = NULL,
1118c2ecf20Sopenharmony_ci	.read_buff_add = NULL,
1128c2ecf20Sopenharmony_ci	.get_index = NULL,
1138c2ecf20Sopenharmony_ci	.indexing = {
1148c2ecf20Sopenharmony_ci		.command = NULL,
1158c2ecf20Sopenharmony_ci		.lowindex = 0,
1168c2ecf20Sopenharmony_ci		.highindex = 0,
1178c2ecf20Sopenharmony_ci		.currindex = 0,
1188c2ecf20Sopenharmony_ci	},
1198c2ecf20Sopenharmony_ci	.attributes = {
1208c2ecf20Sopenharmony_ci		.attrs = synth_attrs,
1218c2ecf20Sopenharmony_ci		.name = "apollo",
1228c2ecf20Sopenharmony_ci	},
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	u_char ch;
1288c2ecf20Sopenharmony_ci	unsigned long flags;
1298c2ecf20Sopenharmony_ci	unsigned long jiff_max;
1308c2ecf20Sopenharmony_ci	struct var_t *jiffy_delta;
1318c2ecf20Sopenharmony_ci	struct var_t *delay_time;
1328c2ecf20Sopenharmony_ci	struct var_t *full_time;
1338c2ecf20Sopenharmony_ci	int full_time_val = 0;
1348c2ecf20Sopenharmony_ci	int delay_time_val = 0;
1358c2ecf20Sopenharmony_ci	int jiffy_delta_val = 0;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	jiffy_delta = spk_get_var(JIFFY);
1388c2ecf20Sopenharmony_ci	delay_time = spk_get_var(DELAY);
1398c2ecf20Sopenharmony_ci	full_time = spk_get_var(FULL);
1408c2ecf20Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
1418c2ecf20Sopenharmony_ci	jiffy_delta_val = jiffy_delta->u.n.value;
1428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1438c2ecf20Sopenharmony_ci	jiff_max = jiffies + jiffy_delta_val;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	while (!kthread_should_stop()) {
1468c2ecf20Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
1478c2ecf20Sopenharmony_ci		jiffy_delta_val = jiffy_delta->u.n.value;
1488c2ecf20Sopenharmony_ci		full_time_val = full_time->u.n.value;
1498c2ecf20Sopenharmony_ci		delay_time_val = delay_time->u.n.value;
1508c2ecf20Sopenharmony_ci		if (speakup_info.flushing) {
1518c2ecf20Sopenharmony_ci			speakup_info.flushing = 0;
1528c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1538c2ecf20Sopenharmony_ci			synth->flush(synth);
1548c2ecf20Sopenharmony_ci			continue;
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci		synth_buffer_skip_nonlatin1();
1578c2ecf20Sopenharmony_ci		if (synth_buffer_empty()) {
1588c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1598c2ecf20Sopenharmony_ci			break;
1608c2ecf20Sopenharmony_ci		}
1618c2ecf20Sopenharmony_ci		ch = synth_buffer_peek();
1628c2ecf20Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
1638c2ecf20Sopenharmony_ci		full_time_val = full_time->u.n.value;
1648c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1658c2ecf20Sopenharmony_ci		if (!synth->io_ops->synth_out(synth, ch)) {
1668c2ecf20Sopenharmony_ci			synth->io_ops->tiocmset(0, UART_MCR_RTS);
1678c2ecf20Sopenharmony_ci			synth->io_ops->tiocmset(UART_MCR_RTS, 0);
1688c2ecf20Sopenharmony_ci			schedule_timeout(msecs_to_jiffies(full_time_val));
1698c2ecf20Sopenharmony_ci			continue;
1708c2ecf20Sopenharmony_ci		}
1718c2ecf20Sopenharmony_ci		if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
1728c2ecf20Sopenharmony_ci			spin_lock_irqsave(&speakup_info.spinlock, flags);
1738c2ecf20Sopenharmony_ci			jiffy_delta_val = jiffy_delta->u.n.value;
1748c2ecf20Sopenharmony_ci			full_time_val = full_time->u.n.value;
1758c2ecf20Sopenharmony_ci			delay_time_val = delay_time->u.n.value;
1768c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1778c2ecf20Sopenharmony_ci			if (synth->io_ops->synth_out(synth, synth->procspeech))
1788c2ecf20Sopenharmony_ci				schedule_timeout(msecs_to_jiffies
1798c2ecf20Sopenharmony_ci						 (delay_time_val));
1808c2ecf20Sopenharmony_ci			else
1818c2ecf20Sopenharmony_ci				schedule_timeout(msecs_to_jiffies
1828c2ecf20Sopenharmony_ci						 (full_time_val));
1838c2ecf20Sopenharmony_ci			jiff_max = jiffies + jiffy_delta_val;
1848c2ecf20Sopenharmony_ci		}
1858c2ecf20Sopenharmony_ci		set_current_state(TASK_RUNNING);
1868c2ecf20Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
1878c2ecf20Sopenharmony_ci		synth_buffer_getc();
1888c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci	synth->io_ops->synth_out(synth, PROCSPEECH);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cimodule_param_named(ser, synth_apollo.ser, int, 0444);
1948c2ecf20Sopenharmony_cimodule_param_named(dev, synth_apollo.dev_name, charp, 0444);
1958c2ecf20Sopenharmony_cimodule_param_named(start, synth_apollo.startup, short, 0444);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
1988c2ecf20Sopenharmony_ciMODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
1998c2ecf20Sopenharmony_ciMODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cimodule_spk_synth(synth_apollo);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
2048c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Borowski");
2058c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Speakup support for Apollo II synthesizer");
2068c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2078c2ecf20Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
2088c2ecf20Sopenharmony_ci
209