18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This is the DECtalk PC speakup driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Some constants from DEC's DOS driver:
68c2ecf20Sopenharmony_ci *      Copyright (c) by Digital Equipment Corp.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * 386BSD DECtalk PC driver:
98c2ecf20Sopenharmony_ci *      Copyright (c) 1996 Brian Buhrow <buhrow@lothlorien.nfbcal.org>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Linux DECtalk PC driver:
128c2ecf20Sopenharmony_ci *      Copyright (c) 1997 Nicolas Pitre <nico@cam.org>
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * speakup DECtalk PC Internal driver:
158c2ecf20Sopenharmony_ci *      Copyright (c) 2003 David Borowski <david575@golden.net>
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * All rights reserved.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
208c2ecf20Sopenharmony_ci#include <linux/sched.h>
218c2ecf20Sopenharmony_ci#include <linux/timer.h>
228c2ecf20Sopenharmony_ci#include <linux/kthread.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "spk_priv.h"
258c2ecf20Sopenharmony_ci#include "speakup.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define	MODULE_init		0x0dec	/* module in boot code */
288c2ecf20Sopenharmony_ci#define	MODULE_self_test	0x8800	/* module in self-test */
298c2ecf20Sopenharmony_ci#define	MODULE_reset		0xffff	/* reinit the whole module */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define	MODE_mask		0xf000	/* mode bits in high nibble */
328c2ecf20Sopenharmony_ci#define	MODE_null		0x0000
338c2ecf20Sopenharmony_ci#define	MODE_test		0x2000	/* in testing mode */
348c2ecf20Sopenharmony_ci#define	MODE_status		0x8000
358c2ecf20Sopenharmony_ci#define	STAT_int		0x0001	/* running in interrupt mode */
368c2ecf20Sopenharmony_ci#define	STAT_tr_char		0x0002	/* character data to transmit */
378c2ecf20Sopenharmony_ci#define	STAT_rr_char		0x0004	/* ready to receive char data */
388c2ecf20Sopenharmony_ci#define	STAT_cmd_ready		0x0008	/* ready to accept commands */
398c2ecf20Sopenharmony_ci#define	STAT_dma_ready		0x0010	/* dma command ready */
408c2ecf20Sopenharmony_ci#define	STAT_digitized		0x0020	/* spc in digitized mode */
418c2ecf20Sopenharmony_ci#define	STAT_new_index		0x0040	/* new last index ready */
428c2ecf20Sopenharmony_ci#define	STAT_new_status		0x0080	/* new status posted */
438c2ecf20Sopenharmony_ci#define	STAT_dma_state		0x0100	/* dma state toggle */
448c2ecf20Sopenharmony_ci#define	STAT_index_valid	0x0200	/* indexs are valid */
458c2ecf20Sopenharmony_ci#define	STAT_flushing		0x0400	/* flush in progress */
468c2ecf20Sopenharmony_ci#define	STAT_self_test		0x0800	/* module in self test */
478c2ecf20Sopenharmony_ci#define	MODE_ready		0xc000	/* module ready for next phase */
488c2ecf20Sopenharmony_ci#define	READY_boot		0x0000
498c2ecf20Sopenharmony_ci#define	READY_kernel		0x0001
508c2ecf20Sopenharmony_ci#define	MODE_error		0xf000
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define	CMD_mask		0xf000	/* mask for command nibble */
538c2ecf20Sopenharmony_ci#define	CMD_null		0x0000	/* post status */
548c2ecf20Sopenharmony_ci#define	CMD_control		0x1000	/* hard control command */
558c2ecf20Sopenharmony_ci#define	CTRL_mask		0x0F00	/* mask off control nibble */
568c2ecf20Sopenharmony_ci#define	CTRL_data		0x00FF	/* mask to get data byte */
578c2ecf20Sopenharmony_ci#define	CTRL_null		0x0000	/* null control */
588c2ecf20Sopenharmony_ci#define	CTRL_vol_up		0x0100	/* increase volume */
598c2ecf20Sopenharmony_ci#define	CTRL_vol_down		0x0200	/* decrease volume */
608c2ecf20Sopenharmony_ci#define	CTRL_vol_set		0x0300	/* set volume */
618c2ecf20Sopenharmony_ci#define	CTRL_pause		0x0400	/* pause spc */
628c2ecf20Sopenharmony_ci#define	CTRL_resume		0x0500	/* resume spc clock */
638c2ecf20Sopenharmony_ci#define	CTRL_resume_spc		0x0001	/* resume spc soft pause */
648c2ecf20Sopenharmony_ci#define	CTRL_flush		0x0600	/* flush all buffers */
658c2ecf20Sopenharmony_ci#define	CTRL_int_enable		0x0700	/* enable status change ints */
668c2ecf20Sopenharmony_ci#define	CTRL_buff_free		0x0800	/* buffer remain count */
678c2ecf20Sopenharmony_ci#define	CTRL_buff_used		0x0900	/* buffer in use */
688c2ecf20Sopenharmony_ci#define	CTRL_speech		0x0a00	/* immediate speech change */
698c2ecf20Sopenharmony_ci#define	CTRL_SP_voice		0x0001	/* voice change */
708c2ecf20Sopenharmony_ci#define	CTRL_SP_rate		0x0002	/* rate change */
718c2ecf20Sopenharmony_ci#define	CTRL_SP_comma		0x0003	/* comma pause change */
728c2ecf20Sopenharmony_ci#define	CTRL_SP_period		0x0004	/* period pause change */
738c2ecf20Sopenharmony_ci#define	CTRL_SP_rate_delta	0x0005	/* delta rate change */
748c2ecf20Sopenharmony_ci#define	CTRL_SP_get_param	0x0006	/* return the desired parameter */
758c2ecf20Sopenharmony_ci#define	CTRL_last_index		0x0b00	/* get last index spoken */
768c2ecf20Sopenharmony_ci#define	CTRL_io_priority	0x0c00	/* change i/o priority */
778c2ecf20Sopenharmony_ci#define	CTRL_free_mem		0x0d00	/* get free paragraphs on module */
788c2ecf20Sopenharmony_ci#define	CTRL_get_lang		0x0e00	/* return bitmask of loaded languages */
798c2ecf20Sopenharmony_ci#define	CMD_test		0x2000	/* self-test request */
808c2ecf20Sopenharmony_ci#define	TEST_mask		0x0F00	/* isolate test field */
818c2ecf20Sopenharmony_ci#define	TEST_null		0x0000	/* no test requested */
828c2ecf20Sopenharmony_ci#define	TEST_isa_int		0x0100	/* assert isa irq */
838c2ecf20Sopenharmony_ci#define	TEST_echo		0x0200	/* make data in == data out */
848c2ecf20Sopenharmony_ci#define	TEST_seg		0x0300	/* set peek/poke segment */
858c2ecf20Sopenharmony_ci#define	TEST_off		0x0400	/* set peek/poke offset */
868c2ecf20Sopenharmony_ci#define	TEST_peek		0x0500	/* data out == *peek */
878c2ecf20Sopenharmony_ci#define	TEST_poke		0x0600	/* *peek == data in */
888c2ecf20Sopenharmony_ci#define	TEST_sub_code		0x00FF	/* user defined test sub codes */
898c2ecf20Sopenharmony_ci#define	CMD_id			0x3000	/* return software id */
908c2ecf20Sopenharmony_ci#define	ID_null			0x0000	/* null id */
918c2ecf20Sopenharmony_ci#define	ID_kernel		0x0100	/* kernel code executing */
928c2ecf20Sopenharmony_ci#define	ID_boot			0x0200	/* boot code executing */
938c2ecf20Sopenharmony_ci#define	CMD_dma			0x4000	/* force a dma start */
948c2ecf20Sopenharmony_ci#define	CMD_reset		0x5000	/* reset module status */
958c2ecf20Sopenharmony_ci#define	CMD_sync		0x6000	/* kernel sync command */
968c2ecf20Sopenharmony_ci#define	CMD_char_in		0x7000	/* single character send */
978c2ecf20Sopenharmony_ci#define	CMD_char_out		0x8000	/* single character get */
988c2ecf20Sopenharmony_ci#define	CHAR_count_1		0x0100	/* one char in cmd_low */
998c2ecf20Sopenharmony_ci#define	CHAR_count_2		0x0200	/* the second in data_low */
1008c2ecf20Sopenharmony_ci#define	CHAR_count_3		0x0300	/* the third in data_high */
1018c2ecf20Sopenharmony_ci#define	CMD_spc_mode		0x9000	/* change spc mode */
1028c2ecf20Sopenharmony_ci#define	CMD_spc_to_text		0x0100	/* set to text mode */
1038c2ecf20Sopenharmony_ci#define	CMD_spc_to_digit	0x0200	/* set to digital mode */
1048c2ecf20Sopenharmony_ci#define	CMD_spc_rate		0x0400	/* change spc data rate */
1058c2ecf20Sopenharmony_ci#define	CMD_error		0xf000	/* severe error */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cienum {	PRIMARY_DIC	= 0, USER_DIC, COMMAND_DIC, ABBREV_DIC };
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define	DMA_single_in		0x01
1108c2ecf20Sopenharmony_ci#define	DMA_single_out		0x02
1118c2ecf20Sopenharmony_ci#define	DMA_buff_in		0x03
1128c2ecf20Sopenharmony_ci#define	DMA_buff_out		0x04
1138c2ecf20Sopenharmony_ci#define	DMA_control		0x05
1148c2ecf20Sopenharmony_ci#define	DT_MEM_ALLOC		0x03
1158c2ecf20Sopenharmony_ci#define	DT_SET_DIC		0x04
1168c2ecf20Sopenharmony_ci#define	DT_START_TASK		0x05
1178c2ecf20Sopenharmony_ci#define	DT_LOAD_MEM		0x06
1188c2ecf20Sopenharmony_ci#define	DT_READ_MEM		0x07
1198c2ecf20Sopenharmony_ci#define	DT_DIGITAL_IN		0x08
1208c2ecf20Sopenharmony_ci#define	DMA_sync		0x06
1218c2ecf20Sopenharmony_ci#define	DMA_sync_char		0x07
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define DRV_VERSION "2.12"
1248c2ecf20Sopenharmony_ci#define PROCSPEECH 0x0b
1258c2ecf20Sopenharmony_ci#define SYNTH_IO_EXTENT 8
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic int synth_probe(struct spk_synth *synth);
1288c2ecf20Sopenharmony_cistatic void dtpc_release(void);
1298c2ecf20Sopenharmony_cistatic const char *synth_immediate(struct spk_synth *synth, const char *buf);
1308c2ecf20Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth);
1318c2ecf20Sopenharmony_cistatic void synth_flush(struct spk_synth *synth);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic int synth_portlist[] = { 0x340, 0x350, 0x240, 0x250, 0 };
1348c2ecf20Sopenharmony_cistatic int in_escape, is_flushing;
1358c2ecf20Sopenharmony_cistatic int dt_stat, dma_state;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic struct var_t vars[] = {
1388c2ecf20Sopenharmony_ci	{ CAPS_START, .u.s = {"[:dv ap 200]" } },
1398c2ecf20Sopenharmony_ci	{ CAPS_STOP, .u.s = {"[:dv ap 100]" } },
1408c2ecf20Sopenharmony_ci	{ RATE, .u.n = {"[:ra %d]", 9, 0, 18, 150, 25, NULL } },
1418c2ecf20Sopenharmony_ci	{ PITCH, .u.n = {"[:dv ap %d]", 80, 0, 100, 20, 0, NULL } },
1428c2ecf20Sopenharmony_ci	{ INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
1438c2ecf20Sopenharmony_ci	{ VOL, .u.n = {"[:vo se %d]", 5, 0, 9, 5, 10, NULL } },
1448c2ecf20Sopenharmony_ci	{ PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
1458c2ecf20Sopenharmony_ci	{ VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
1468c2ecf20Sopenharmony_ci	{ DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
1478c2ecf20Sopenharmony_ci	V_LAST_VAR
1488c2ecf20Sopenharmony_ci};
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/*
1518c2ecf20Sopenharmony_ci * These attributes will appear in /sys/accessibility/speakup/decpc.
1528c2ecf20Sopenharmony_ci */
1538c2ecf20Sopenharmony_cistatic struct kobj_attribute caps_start_attribute =
1548c2ecf20Sopenharmony_ci	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
1558c2ecf20Sopenharmony_cistatic struct kobj_attribute caps_stop_attribute =
1568c2ecf20Sopenharmony_ci	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
1578c2ecf20Sopenharmony_cistatic struct kobj_attribute pitch_attribute =
1588c2ecf20Sopenharmony_ci	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
1598c2ecf20Sopenharmony_cistatic struct kobj_attribute inflection_attribute =
1608c2ecf20Sopenharmony_ci	__ATTR(inflection, 0644, spk_var_show, spk_var_store);
1618c2ecf20Sopenharmony_cistatic struct kobj_attribute punct_attribute =
1628c2ecf20Sopenharmony_ci	__ATTR(punct, 0644, spk_var_show, spk_var_store);
1638c2ecf20Sopenharmony_cistatic struct kobj_attribute rate_attribute =
1648c2ecf20Sopenharmony_ci	__ATTR(rate, 0644, spk_var_show, spk_var_store);
1658c2ecf20Sopenharmony_cistatic struct kobj_attribute voice_attribute =
1668c2ecf20Sopenharmony_ci	__ATTR(voice, 0644, spk_var_show, spk_var_store);
1678c2ecf20Sopenharmony_cistatic struct kobj_attribute vol_attribute =
1688c2ecf20Sopenharmony_ci	__ATTR(vol, 0644, spk_var_show, spk_var_store);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic struct kobj_attribute delay_time_attribute =
1718c2ecf20Sopenharmony_ci	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
1728c2ecf20Sopenharmony_cistatic struct kobj_attribute direct_attribute =
1738c2ecf20Sopenharmony_ci	__ATTR(direct, 0644, spk_var_show, spk_var_store);
1748c2ecf20Sopenharmony_cistatic struct kobj_attribute full_time_attribute =
1758c2ecf20Sopenharmony_ci	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
1768c2ecf20Sopenharmony_cistatic struct kobj_attribute jiffy_delta_attribute =
1778c2ecf20Sopenharmony_ci	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
1788c2ecf20Sopenharmony_cistatic struct kobj_attribute trigger_time_attribute =
1798c2ecf20Sopenharmony_ci	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/*
1828c2ecf20Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all
1838c2ecf20Sopenharmony_ci * at once.
1848c2ecf20Sopenharmony_ci */
1858c2ecf20Sopenharmony_cistatic struct attribute *synth_attrs[] = {
1868c2ecf20Sopenharmony_ci	&caps_start_attribute.attr,
1878c2ecf20Sopenharmony_ci	&caps_stop_attribute.attr,
1888c2ecf20Sopenharmony_ci	&pitch_attribute.attr,
1898c2ecf20Sopenharmony_ci	&inflection_attribute.attr,
1908c2ecf20Sopenharmony_ci	&punct_attribute.attr,
1918c2ecf20Sopenharmony_ci	&rate_attribute.attr,
1928c2ecf20Sopenharmony_ci	&voice_attribute.attr,
1938c2ecf20Sopenharmony_ci	&vol_attribute.attr,
1948c2ecf20Sopenharmony_ci	&delay_time_attribute.attr,
1958c2ecf20Sopenharmony_ci	&direct_attribute.attr,
1968c2ecf20Sopenharmony_ci	&full_time_attribute.attr,
1978c2ecf20Sopenharmony_ci	&jiffy_delta_attribute.attr,
1988c2ecf20Sopenharmony_ci	&trigger_time_attribute.attr,
1998c2ecf20Sopenharmony_ci	NULL,	/* need to NULL terminate the list of attributes */
2008c2ecf20Sopenharmony_ci};
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic struct spk_synth synth_dec_pc = {
2038c2ecf20Sopenharmony_ci	.name = "decpc",
2048c2ecf20Sopenharmony_ci	.version = DRV_VERSION,
2058c2ecf20Sopenharmony_ci	.long_name = "Dectalk PC",
2068c2ecf20Sopenharmony_ci	.init = "[:pe -380]",
2078c2ecf20Sopenharmony_ci	.procspeech = PROCSPEECH,
2088c2ecf20Sopenharmony_ci	.delay = 500,
2098c2ecf20Sopenharmony_ci	.trigger = 50,
2108c2ecf20Sopenharmony_ci	.jiffies = 50,
2118c2ecf20Sopenharmony_ci	.full = 1000,
2128c2ecf20Sopenharmony_ci	.flags = SF_DEC,
2138c2ecf20Sopenharmony_ci	.startup = SYNTH_START,
2148c2ecf20Sopenharmony_ci	.checkval = SYNTH_CHECK,
2158c2ecf20Sopenharmony_ci	.vars = vars,
2168c2ecf20Sopenharmony_ci	.io_ops = &spk_serial_io_ops,
2178c2ecf20Sopenharmony_ci	.probe = synth_probe,
2188c2ecf20Sopenharmony_ci	.release = dtpc_release,
2198c2ecf20Sopenharmony_ci	.synth_immediate = synth_immediate,
2208c2ecf20Sopenharmony_ci	.catch_up = do_catch_up,
2218c2ecf20Sopenharmony_ci	.flush = synth_flush,
2228c2ecf20Sopenharmony_ci	.is_alive = spk_synth_is_alive_nop,
2238c2ecf20Sopenharmony_ci	.synth_adjust = NULL,
2248c2ecf20Sopenharmony_ci	.read_buff_add = NULL,
2258c2ecf20Sopenharmony_ci	.get_index = NULL,
2268c2ecf20Sopenharmony_ci	.indexing = {
2278c2ecf20Sopenharmony_ci		.command = NULL,
2288c2ecf20Sopenharmony_ci		.lowindex = 0,
2298c2ecf20Sopenharmony_ci		.highindex = 0,
2308c2ecf20Sopenharmony_ci		.currindex = 0,
2318c2ecf20Sopenharmony_ci	},
2328c2ecf20Sopenharmony_ci	.attributes = {
2338c2ecf20Sopenharmony_ci		.attrs = synth_attrs,
2348c2ecf20Sopenharmony_ci		.name = "decpc",
2358c2ecf20Sopenharmony_ci	},
2368c2ecf20Sopenharmony_ci};
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic int dt_getstatus(void)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	dt_stat = inb_p(speakup_info.port_tts) |
2418c2ecf20Sopenharmony_ci		 (inb_p(speakup_info.port_tts + 1) << 8);
2428c2ecf20Sopenharmony_ci	return dt_stat;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void dt_sendcmd(u_int cmd)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	outb_p(cmd & 0xFF, speakup_info.port_tts);
2488c2ecf20Sopenharmony_ci	outb_p((cmd >> 8) & 0xFF, speakup_info.port_tts + 1);
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic int dt_waitbit(int bit)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	int timeout = 100;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	while (--timeout > 0) {
2568c2ecf20Sopenharmony_ci		if ((dt_getstatus() & bit) == bit)
2578c2ecf20Sopenharmony_ci			return 1;
2588c2ecf20Sopenharmony_ci		udelay(50);
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci	return 0;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic int dt_wait_dma(void)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	int timeout = 100, state = dma_state;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (!dt_waitbit(STAT_dma_ready))
2688c2ecf20Sopenharmony_ci		return 0;
2698c2ecf20Sopenharmony_ci	while (--timeout > 0) {
2708c2ecf20Sopenharmony_ci		if ((dt_getstatus() & STAT_dma_state) == state)
2718c2ecf20Sopenharmony_ci			return 1;
2728c2ecf20Sopenharmony_ci		udelay(50);
2738c2ecf20Sopenharmony_ci	}
2748c2ecf20Sopenharmony_ci	dma_state = dt_getstatus() & STAT_dma_state;
2758c2ecf20Sopenharmony_ci	return 1;
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic int dt_ctrl(u_int cmd)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	int timeout = 10;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	if (!dt_waitbit(STAT_cmd_ready))
2838c2ecf20Sopenharmony_ci		return -1;
2848c2ecf20Sopenharmony_ci	outb_p(0, speakup_info.port_tts + 2);
2858c2ecf20Sopenharmony_ci	outb_p(0, speakup_info.port_tts + 3);
2868c2ecf20Sopenharmony_ci	dt_getstatus();
2878c2ecf20Sopenharmony_ci	dt_sendcmd(CMD_control | cmd);
2888c2ecf20Sopenharmony_ci	outb_p(0, speakup_info.port_tts + 6);
2898c2ecf20Sopenharmony_ci	while (dt_getstatus() & STAT_cmd_ready) {
2908c2ecf20Sopenharmony_ci		udelay(20);
2918c2ecf20Sopenharmony_ci		if (--timeout == 0)
2928c2ecf20Sopenharmony_ci			break;
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci	dt_sendcmd(CMD_null);
2958c2ecf20Sopenharmony_ci	return 0;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic void synth_flush(struct spk_synth *synth)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	int timeout = 10;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	if (is_flushing)
3038c2ecf20Sopenharmony_ci		return;
3048c2ecf20Sopenharmony_ci	is_flushing = 4;
3058c2ecf20Sopenharmony_ci	in_escape = 0;
3068c2ecf20Sopenharmony_ci	while (dt_ctrl(CTRL_flush)) {
3078c2ecf20Sopenharmony_ci		if (--timeout == 0)
3088c2ecf20Sopenharmony_ci			break;
3098c2ecf20Sopenharmony_ci		udelay(50);
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci	for (timeout = 0; timeout < 10; timeout++) {
3128c2ecf20Sopenharmony_ci		if (dt_waitbit(STAT_dma_ready))
3138c2ecf20Sopenharmony_ci			break;
3148c2ecf20Sopenharmony_ci		udelay(50);
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci	outb_p(DMA_sync, speakup_info.port_tts + 4);
3178c2ecf20Sopenharmony_ci	outb_p(0, speakup_info.port_tts + 4);
3188c2ecf20Sopenharmony_ci	udelay(100);
3198c2ecf20Sopenharmony_ci	for (timeout = 0; timeout < 10; timeout++) {
3208c2ecf20Sopenharmony_ci		if (!(dt_getstatus() & STAT_flushing))
3218c2ecf20Sopenharmony_ci			break;
3228c2ecf20Sopenharmony_ci		udelay(50);
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci	dma_state = dt_getstatus() & STAT_dma_state;
3258c2ecf20Sopenharmony_ci	dma_state ^= STAT_dma_state;
3268c2ecf20Sopenharmony_ci	is_flushing = 0;
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_cistatic int dt_sendchar(char ch)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	if (!dt_wait_dma())
3328c2ecf20Sopenharmony_ci		return -1;
3338c2ecf20Sopenharmony_ci	if (!(dt_stat & STAT_rr_char))
3348c2ecf20Sopenharmony_ci		return -2;
3358c2ecf20Sopenharmony_ci	outb_p(DMA_single_in, speakup_info.port_tts + 4);
3368c2ecf20Sopenharmony_ci	outb_p(ch, speakup_info.port_tts + 4);
3378c2ecf20Sopenharmony_ci	dma_state ^= STAT_dma_state;
3388c2ecf20Sopenharmony_ci	return 0;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic int testkernel(void)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	int status = 0;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	if (dt_getstatus() == 0xffff) {
3468c2ecf20Sopenharmony_ci		status = -1;
3478c2ecf20Sopenharmony_ci		goto oops;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci	dt_sendcmd(CMD_sync);
3508c2ecf20Sopenharmony_ci	if (!dt_waitbit(STAT_cmd_ready))
3518c2ecf20Sopenharmony_ci		status = -2;
3528c2ecf20Sopenharmony_ci	else if (dt_stat & 0x8000)
3538c2ecf20Sopenharmony_ci		return 0;
3548c2ecf20Sopenharmony_ci	else if (dt_stat == 0x0dec)
3558c2ecf20Sopenharmony_ci		pr_warn("dec_pc at 0x%x, software not loaded\n",
3568c2ecf20Sopenharmony_ci			speakup_info.port_tts);
3578c2ecf20Sopenharmony_ci	status = -3;
3588c2ecf20Sopenharmony_cioops:	synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
3598c2ecf20Sopenharmony_ci	speakup_info.port_tts = 0;
3608c2ecf20Sopenharmony_ci	return status;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	u_char ch;
3668c2ecf20Sopenharmony_ci	static u_char last;
3678c2ecf20Sopenharmony_ci	unsigned long flags;
3688c2ecf20Sopenharmony_ci	unsigned long jiff_max;
3698c2ecf20Sopenharmony_ci	struct var_t *jiffy_delta;
3708c2ecf20Sopenharmony_ci	struct var_t *delay_time;
3718c2ecf20Sopenharmony_ci	int jiffy_delta_val;
3728c2ecf20Sopenharmony_ci	int delay_time_val;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	jiffy_delta = spk_get_var(JIFFY);
3758c2ecf20Sopenharmony_ci	delay_time = spk_get_var(DELAY);
3768c2ecf20Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
3778c2ecf20Sopenharmony_ci	jiffy_delta_val = jiffy_delta->u.n.value;
3788c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
3798c2ecf20Sopenharmony_ci	jiff_max = jiffies + jiffy_delta_val;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	while (!kthread_should_stop()) {
3828c2ecf20Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
3838c2ecf20Sopenharmony_ci		if (speakup_info.flushing) {
3848c2ecf20Sopenharmony_ci			speakup_info.flushing = 0;
3858c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
3868c2ecf20Sopenharmony_ci			synth->flush(synth);
3878c2ecf20Sopenharmony_ci			continue;
3888c2ecf20Sopenharmony_ci		}
3898c2ecf20Sopenharmony_ci		synth_buffer_skip_nonlatin1();
3908c2ecf20Sopenharmony_ci		if (synth_buffer_empty()) {
3918c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
3928c2ecf20Sopenharmony_ci			break;
3938c2ecf20Sopenharmony_ci		}
3948c2ecf20Sopenharmony_ci		ch = synth_buffer_peek();
3958c2ecf20Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
3968c2ecf20Sopenharmony_ci		delay_time_val = delay_time->u.n.value;
3978c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
3988c2ecf20Sopenharmony_ci		if (ch == '\n')
3998c2ecf20Sopenharmony_ci			ch = 0x0D;
4008c2ecf20Sopenharmony_ci		if (dt_sendchar(ch)) {
4018c2ecf20Sopenharmony_ci			schedule_timeout(msecs_to_jiffies(delay_time_val));
4028c2ecf20Sopenharmony_ci			continue;
4038c2ecf20Sopenharmony_ci		}
4048c2ecf20Sopenharmony_ci		set_current_state(TASK_RUNNING);
4058c2ecf20Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
4068c2ecf20Sopenharmony_ci		synth_buffer_getc();
4078c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
4088c2ecf20Sopenharmony_ci		if (ch == '[') {
4098c2ecf20Sopenharmony_ci			in_escape = 1;
4108c2ecf20Sopenharmony_ci		} else if (ch == ']') {
4118c2ecf20Sopenharmony_ci			in_escape = 0;
4128c2ecf20Sopenharmony_ci		} else if (ch <= SPACE) {
4138c2ecf20Sopenharmony_ci			if (!in_escape && strchr(",.!?;:", last))
4148c2ecf20Sopenharmony_ci				dt_sendchar(PROCSPEECH);
4158c2ecf20Sopenharmony_ci			if (time_after_eq(jiffies, jiff_max)) {
4168c2ecf20Sopenharmony_ci				if (!in_escape)
4178c2ecf20Sopenharmony_ci					dt_sendchar(PROCSPEECH);
4188c2ecf20Sopenharmony_ci				spin_lock_irqsave(&speakup_info.spinlock,
4198c2ecf20Sopenharmony_ci						  flags);
4208c2ecf20Sopenharmony_ci				jiffy_delta_val = jiffy_delta->u.n.value;
4218c2ecf20Sopenharmony_ci				delay_time_val = delay_time->u.n.value;
4228c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&speakup_info.spinlock,
4238c2ecf20Sopenharmony_ci						       flags);
4248c2ecf20Sopenharmony_ci				schedule_timeout(msecs_to_jiffies
4258c2ecf20Sopenharmony_ci						 (delay_time_val));
4268c2ecf20Sopenharmony_ci				jiff_max = jiffies + jiffy_delta_val;
4278c2ecf20Sopenharmony_ci			}
4288c2ecf20Sopenharmony_ci		}
4298c2ecf20Sopenharmony_ci		last = ch;
4308c2ecf20Sopenharmony_ci		ch = 0;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci	if (!in_escape)
4338c2ecf20Sopenharmony_ci		dt_sendchar(PROCSPEECH);
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic const char *synth_immediate(struct spk_synth *synth, const char *buf)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	u_char ch;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	while ((ch = *buf)) {
4418c2ecf20Sopenharmony_ci		if (ch == '\n')
4428c2ecf20Sopenharmony_ci			ch = PROCSPEECH;
4438c2ecf20Sopenharmony_ci		if (dt_sendchar(ch))
4448c2ecf20Sopenharmony_ci			return buf;
4458c2ecf20Sopenharmony_ci		buf++;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci	return NULL;
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic int synth_probe(struct spk_synth *synth)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	int i = 0, failed = 0;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	pr_info("Probing for %s.\n", synth->long_name);
4558c2ecf20Sopenharmony_ci	for (i = 0; synth_portlist[i]; i++) {
4568c2ecf20Sopenharmony_ci		if (synth_request_region(synth_portlist[i], SYNTH_IO_EXTENT)) {
4578c2ecf20Sopenharmony_ci			pr_warn("request_region: failed with 0x%x, %d\n",
4588c2ecf20Sopenharmony_ci				synth_portlist[i], SYNTH_IO_EXTENT);
4598c2ecf20Sopenharmony_ci			continue;
4608c2ecf20Sopenharmony_ci		}
4618c2ecf20Sopenharmony_ci		speakup_info.port_tts = synth_portlist[i];
4628c2ecf20Sopenharmony_ci		failed = testkernel();
4638c2ecf20Sopenharmony_ci		if (failed == 0)
4648c2ecf20Sopenharmony_ci			break;
4658c2ecf20Sopenharmony_ci	}
4668c2ecf20Sopenharmony_ci	if (failed) {
4678c2ecf20Sopenharmony_ci		pr_info("%s: not found\n", synth->long_name);
4688c2ecf20Sopenharmony_ci		return -ENODEV;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci	pr_info("%s: %03x-%03x, Driver Version %s,\n", synth->long_name,
4718c2ecf20Sopenharmony_ci		speakup_info.port_tts, speakup_info.port_tts + 7,
4728c2ecf20Sopenharmony_ci		synth->version);
4738c2ecf20Sopenharmony_ci	synth->alive = 1;
4748c2ecf20Sopenharmony_ci	return 0;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic void dtpc_release(void)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	spk_stop_serial_interrupt();
4808c2ecf20Sopenharmony_ci	if (speakup_info.port_tts)
4818c2ecf20Sopenharmony_ci		synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
4828c2ecf20Sopenharmony_ci	speakup_info.port_tts = 0;
4838c2ecf20Sopenharmony_ci}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cimodule_param_named(start, synth_dec_pc.startup, short, 0444);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ciMODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cimodule_spk_synth(synth_dec_pc);
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
4928c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Borowski");
4938c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Speakup support for DECtalk PC synthesizers");
4948c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4958c2ecf20Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
496