162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * This is the DECtalk PC speakup driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Some constants from DEC's DOS driver:
662306a36Sopenharmony_ci *      Copyright (c) by Digital Equipment Corp.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * 386BSD DECtalk PC driver:
962306a36Sopenharmony_ci *      Copyright (c) 1996 Brian Buhrow <buhrow@lothlorien.nfbcal.org>
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * Linux DECtalk PC driver:
1262306a36Sopenharmony_ci *      Copyright (c) 1997 Nicolas Pitre <nico@cam.org>
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * speakup DECtalk PC Internal driver:
1562306a36Sopenharmony_ci *      Copyright (c) 2003 David Borowski <david575@golden.net>
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * All rights reserved.
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_ci#include <linux/jiffies.h>
2062306a36Sopenharmony_ci#include <linux/sched.h>
2162306a36Sopenharmony_ci#include <linux/timer.h>
2262306a36Sopenharmony_ci#include <linux/kthread.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "spk_priv.h"
2562306a36Sopenharmony_ci#include "speakup.h"
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define	MODULE_init		0x0dec	/* module in boot code */
2862306a36Sopenharmony_ci#define	MODULE_self_test	0x8800	/* module in self-test */
2962306a36Sopenharmony_ci#define	MODULE_reset		0xffff	/* reinit the whole module */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#define	MODE_mask		0xf000	/* mode bits in high nibble */
3262306a36Sopenharmony_ci#define	MODE_null		0x0000
3362306a36Sopenharmony_ci#define	MODE_test		0x2000	/* in testing mode */
3462306a36Sopenharmony_ci#define	MODE_status		0x8000
3562306a36Sopenharmony_ci#define	STAT_int		0x0001	/* running in interrupt mode */
3662306a36Sopenharmony_ci#define	STAT_tr_char		0x0002	/* character data to transmit */
3762306a36Sopenharmony_ci#define	STAT_rr_char		0x0004	/* ready to receive char data */
3862306a36Sopenharmony_ci#define	STAT_cmd_ready		0x0008	/* ready to accept commands */
3962306a36Sopenharmony_ci#define	STAT_dma_ready		0x0010	/* dma command ready */
4062306a36Sopenharmony_ci#define	STAT_digitized		0x0020	/* spc in digitized mode */
4162306a36Sopenharmony_ci#define	STAT_new_index		0x0040	/* new last index ready */
4262306a36Sopenharmony_ci#define	STAT_new_status		0x0080	/* new status posted */
4362306a36Sopenharmony_ci#define	STAT_dma_state		0x0100	/* dma state toggle */
4462306a36Sopenharmony_ci#define	STAT_index_valid	0x0200	/* indexs are valid */
4562306a36Sopenharmony_ci#define	STAT_flushing		0x0400	/* flush in progress */
4662306a36Sopenharmony_ci#define	STAT_self_test		0x0800	/* module in self test */
4762306a36Sopenharmony_ci#define	MODE_ready		0xc000	/* module ready for next phase */
4862306a36Sopenharmony_ci#define	READY_boot		0x0000
4962306a36Sopenharmony_ci#define	READY_kernel		0x0001
5062306a36Sopenharmony_ci#define	MODE_error		0xf000
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define	CMD_mask		0xf000	/* mask for command nibble */
5362306a36Sopenharmony_ci#define	CMD_null		0x0000	/* post status */
5462306a36Sopenharmony_ci#define	CMD_control		0x1000	/* hard control command */
5562306a36Sopenharmony_ci#define	CTRL_mask		0x0F00	/* mask off control nibble */
5662306a36Sopenharmony_ci#define	CTRL_data		0x00FF	/* mask to get data byte */
5762306a36Sopenharmony_ci#define	CTRL_null		0x0000	/* null control */
5862306a36Sopenharmony_ci#define	CTRL_vol_up		0x0100	/* increase volume */
5962306a36Sopenharmony_ci#define	CTRL_vol_down		0x0200	/* decrease volume */
6062306a36Sopenharmony_ci#define	CTRL_vol_set		0x0300	/* set volume */
6162306a36Sopenharmony_ci#define	CTRL_pause		0x0400	/* pause spc */
6262306a36Sopenharmony_ci#define	CTRL_resume		0x0500	/* resume spc clock */
6362306a36Sopenharmony_ci#define	CTRL_resume_spc		0x0001	/* resume spc soft pause */
6462306a36Sopenharmony_ci#define	CTRL_flush		0x0600	/* flush all buffers */
6562306a36Sopenharmony_ci#define	CTRL_int_enable		0x0700	/* enable status change ints */
6662306a36Sopenharmony_ci#define	CTRL_buff_free		0x0800	/* buffer remain count */
6762306a36Sopenharmony_ci#define	CTRL_buff_used		0x0900	/* buffer in use */
6862306a36Sopenharmony_ci#define	CTRL_speech		0x0a00	/* immediate speech change */
6962306a36Sopenharmony_ci#define	CTRL_SP_voice		0x0001	/* voice change */
7062306a36Sopenharmony_ci#define	CTRL_SP_rate		0x0002	/* rate change */
7162306a36Sopenharmony_ci#define	CTRL_SP_comma		0x0003	/* comma pause change */
7262306a36Sopenharmony_ci#define	CTRL_SP_period		0x0004	/* period pause change */
7362306a36Sopenharmony_ci#define	CTRL_SP_rate_delta	0x0005	/* delta rate change */
7462306a36Sopenharmony_ci#define	CTRL_SP_get_param	0x0006	/* return the desired parameter */
7562306a36Sopenharmony_ci#define	CTRL_last_index		0x0b00	/* get last index spoken */
7662306a36Sopenharmony_ci#define	CTRL_io_priority	0x0c00	/* change i/o priority */
7762306a36Sopenharmony_ci#define	CTRL_free_mem		0x0d00	/* get free paragraphs on module */
7862306a36Sopenharmony_ci#define	CTRL_get_lang		0x0e00	/* return bitmask of loaded languages */
7962306a36Sopenharmony_ci#define	CMD_test		0x2000	/* self-test request */
8062306a36Sopenharmony_ci#define	TEST_mask		0x0F00	/* isolate test field */
8162306a36Sopenharmony_ci#define	TEST_null		0x0000	/* no test requested */
8262306a36Sopenharmony_ci#define	TEST_isa_int		0x0100	/* assert isa irq */
8362306a36Sopenharmony_ci#define	TEST_echo		0x0200	/* make data in == data out */
8462306a36Sopenharmony_ci#define	TEST_seg		0x0300	/* set peek/poke segment */
8562306a36Sopenharmony_ci#define	TEST_off		0x0400	/* set peek/poke offset */
8662306a36Sopenharmony_ci#define	TEST_peek		0x0500	/* data out == *peek */
8762306a36Sopenharmony_ci#define	TEST_poke		0x0600	/* *peek == data in */
8862306a36Sopenharmony_ci#define	TEST_sub_code		0x00FF	/* user defined test sub codes */
8962306a36Sopenharmony_ci#define	CMD_id			0x3000	/* return software id */
9062306a36Sopenharmony_ci#define	ID_null			0x0000	/* null id */
9162306a36Sopenharmony_ci#define	ID_kernel		0x0100	/* kernel code executing */
9262306a36Sopenharmony_ci#define	ID_boot			0x0200	/* boot code executing */
9362306a36Sopenharmony_ci#define	CMD_dma			0x4000	/* force a dma start */
9462306a36Sopenharmony_ci#define	CMD_reset		0x5000	/* reset module status */
9562306a36Sopenharmony_ci#define	CMD_sync		0x6000	/* kernel sync command */
9662306a36Sopenharmony_ci#define	CMD_char_in		0x7000	/* single character send */
9762306a36Sopenharmony_ci#define	CMD_char_out		0x8000	/* single character get */
9862306a36Sopenharmony_ci#define	CHAR_count_1		0x0100	/* one char in cmd_low */
9962306a36Sopenharmony_ci#define	CHAR_count_2		0x0200	/* the second in data_low */
10062306a36Sopenharmony_ci#define	CHAR_count_3		0x0300	/* the third in data_high */
10162306a36Sopenharmony_ci#define	CMD_spc_mode		0x9000	/* change spc mode */
10262306a36Sopenharmony_ci#define	CMD_spc_to_text		0x0100	/* set to text mode */
10362306a36Sopenharmony_ci#define	CMD_spc_to_digit	0x0200	/* set to digital mode */
10462306a36Sopenharmony_ci#define	CMD_spc_rate		0x0400	/* change spc data rate */
10562306a36Sopenharmony_ci#define	CMD_error		0xf000	/* severe error */
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cienum {	PRIMARY_DIC	= 0, USER_DIC, COMMAND_DIC, ABBREV_DIC };
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#define	DMA_single_in		0x01
11062306a36Sopenharmony_ci#define	DMA_single_out		0x02
11162306a36Sopenharmony_ci#define	DMA_buff_in		0x03
11262306a36Sopenharmony_ci#define	DMA_buff_out		0x04
11362306a36Sopenharmony_ci#define	DMA_control		0x05
11462306a36Sopenharmony_ci#define	DT_MEM_ALLOC		0x03
11562306a36Sopenharmony_ci#define	DT_SET_DIC		0x04
11662306a36Sopenharmony_ci#define	DT_START_TASK		0x05
11762306a36Sopenharmony_ci#define	DT_LOAD_MEM		0x06
11862306a36Sopenharmony_ci#define	DT_READ_MEM		0x07
11962306a36Sopenharmony_ci#define	DT_DIGITAL_IN		0x08
12062306a36Sopenharmony_ci#define	DMA_sync		0x06
12162306a36Sopenharmony_ci#define	DMA_sync_char		0x07
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#define DRV_VERSION "2.12"
12462306a36Sopenharmony_ci#define PROCSPEECH 0x0b
12562306a36Sopenharmony_ci#define SYNTH_IO_EXTENT 8
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic int synth_probe(struct spk_synth *synth);
12862306a36Sopenharmony_cistatic void dtpc_release(struct spk_synth *synth);
12962306a36Sopenharmony_cistatic const char *synth_immediate(struct spk_synth *synth, const char *buf);
13062306a36Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth);
13162306a36Sopenharmony_cistatic void synth_flush(struct spk_synth *synth);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic int synth_portlist[] = { 0x340, 0x350, 0x240, 0x250, 0 };
13462306a36Sopenharmony_cistatic int in_escape, is_flushing;
13562306a36Sopenharmony_cistatic int dt_stat, dma_state;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cienum default_vars_id {
13962306a36Sopenharmony_ci	CAPS_START_ID = 0, CAPS_STOP_ID,
14062306a36Sopenharmony_ci	RATE_ID, PITCH_ID, INFLECTION_ID,
14162306a36Sopenharmony_ci	VOL_ID, PUNCT_ID, VOICE_ID,
14262306a36Sopenharmony_ci	DIRECT_ID, V_LAST_VAR_ID,
14362306a36Sopenharmony_ci	NB_ID,
14462306a36Sopenharmony_ci};
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic struct var_t vars[NB_ID] = {
14962306a36Sopenharmony_ci	[CAPS_START_ID] = { CAPS_START, .u.s = {"[:dv ap 200]" } },
15062306a36Sopenharmony_ci	[CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
15162306a36Sopenharmony_ci	[RATE_ID] = { RATE, .u.n = {"[:ra %d]", 9, 0, 18, 150, 25, NULL } },
15262306a36Sopenharmony_ci	[PITCH_ID] = { PITCH, .u.n = {"[:dv ap %d]", 80, 0, 100, 20, 0, NULL } },
15362306a36Sopenharmony_ci	[INFLECTION_ID] = { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
15462306a36Sopenharmony_ci	[VOL_ID] = { VOL, .u.n = {"[:vo se %d]", 5, 0, 9, 5, 10, NULL } },
15562306a36Sopenharmony_ci	[PUNCT_ID] = { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
15662306a36Sopenharmony_ci	[VOICE_ID] = { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
15762306a36Sopenharmony_ci	[DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
15862306a36Sopenharmony_ci	V_LAST_VAR
15962306a36Sopenharmony_ci};
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/*
16262306a36Sopenharmony_ci * These attributes will appear in /sys/accessibility/speakup/decpc.
16362306a36Sopenharmony_ci */
16462306a36Sopenharmony_cistatic struct kobj_attribute caps_start_attribute =
16562306a36Sopenharmony_ci	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
16662306a36Sopenharmony_cistatic struct kobj_attribute caps_stop_attribute =
16762306a36Sopenharmony_ci	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
16862306a36Sopenharmony_cistatic struct kobj_attribute pitch_attribute =
16962306a36Sopenharmony_ci	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
17062306a36Sopenharmony_cistatic struct kobj_attribute inflection_attribute =
17162306a36Sopenharmony_ci	__ATTR(inflection, 0644, spk_var_show, spk_var_store);
17262306a36Sopenharmony_cistatic struct kobj_attribute punct_attribute =
17362306a36Sopenharmony_ci	__ATTR(punct, 0644, spk_var_show, spk_var_store);
17462306a36Sopenharmony_cistatic struct kobj_attribute rate_attribute =
17562306a36Sopenharmony_ci	__ATTR(rate, 0644, spk_var_show, spk_var_store);
17662306a36Sopenharmony_cistatic struct kobj_attribute voice_attribute =
17762306a36Sopenharmony_ci	__ATTR(voice, 0644, spk_var_show, spk_var_store);
17862306a36Sopenharmony_cistatic struct kobj_attribute vol_attribute =
17962306a36Sopenharmony_ci	__ATTR(vol, 0644, spk_var_show, spk_var_store);
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic struct kobj_attribute delay_time_attribute =
18262306a36Sopenharmony_ci	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
18362306a36Sopenharmony_cistatic struct kobj_attribute direct_attribute =
18462306a36Sopenharmony_ci	__ATTR(direct, 0644, spk_var_show, spk_var_store);
18562306a36Sopenharmony_cistatic struct kobj_attribute full_time_attribute =
18662306a36Sopenharmony_ci	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
18762306a36Sopenharmony_cistatic struct kobj_attribute jiffy_delta_attribute =
18862306a36Sopenharmony_ci	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
18962306a36Sopenharmony_cistatic struct kobj_attribute trigger_time_attribute =
19062306a36Sopenharmony_ci	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci/*
19362306a36Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all
19462306a36Sopenharmony_ci * at once.
19562306a36Sopenharmony_ci */
19662306a36Sopenharmony_cistatic struct attribute *synth_attrs[] = {
19762306a36Sopenharmony_ci	&caps_start_attribute.attr,
19862306a36Sopenharmony_ci	&caps_stop_attribute.attr,
19962306a36Sopenharmony_ci	&pitch_attribute.attr,
20062306a36Sopenharmony_ci	&inflection_attribute.attr,
20162306a36Sopenharmony_ci	&punct_attribute.attr,
20262306a36Sopenharmony_ci	&rate_attribute.attr,
20362306a36Sopenharmony_ci	&voice_attribute.attr,
20462306a36Sopenharmony_ci	&vol_attribute.attr,
20562306a36Sopenharmony_ci	&delay_time_attribute.attr,
20662306a36Sopenharmony_ci	&direct_attribute.attr,
20762306a36Sopenharmony_ci	&full_time_attribute.attr,
20862306a36Sopenharmony_ci	&jiffy_delta_attribute.attr,
20962306a36Sopenharmony_ci	&trigger_time_attribute.attr,
21062306a36Sopenharmony_ci	NULL,	/* need to NULL terminate the list of attributes */
21162306a36Sopenharmony_ci};
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic struct spk_synth synth_dec_pc = {
21462306a36Sopenharmony_ci	.name = "decpc",
21562306a36Sopenharmony_ci	.version = DRV_VERSION,
21662306a36Sopenharmony_ci	.long_name = "Dectalk PC",
21762306a36Sopenharmony_ci	.init = "[:pe -380]",
21862306a36Sopenharmony_ci	.procspeech = PROCSPEECH,
21962306a36Sopenharmony_ci	.delay = 500,
22062306a36Sopenharmony_ci	.trigger = 50,
22162306a36Sopenharmony_ci	.jiffies = 50,
22262306a36Sopenharmony_ci	.full = 1000,
22362306a36Sopenharmony_ci	.flags = SF_DEC,
22462306a36Sopenharmony_ci	.startup = SYNTH_START,
22562306a36Sopenharmony_ci	.checkval = SYNTH_CHECK,
22662306a36Sopenharmony_ci	.vars = vars,
22762306a36Sopenharmony_ci	.io_ops = &spk_serial_io_ops,
22862306a36Sopenharmony_ci	.probe = synth_probe,
22962306a36Sopenharmony_ci	.release = dtpc_release,
23062306a36Sopenharmony_ci	.synth_immediate = synth_immediate,
23162306a36Sopenharmony_ci	.catch_up = do_catch_up,
23262306a36Sopenharmony_ci	.flush = synth_flush,
23362306a36Sopenharmony_ci	.is_alive = spk_synth_is_alive_nop,
23462306a36Sopenharmony_ci	.synth_adjust = NULL,
23562306a36Sopenharmony_ci	.read_buff_add = NULL,
23662306a36Sopenharmony_ci	.get_index = NULL,
23762306a36Sopenharmony_ci	.indexing = {
23862306a36Sopenharmony_ci		.command = NULL,
23962306a36Sopenharmony_ci		.lowindex = 0,
24062306a36Sopenharmony_ci		.highindex = 0,
24162306a36Sopenharmony_ci		.currindex = 0,
24262306a36Sopenharmony_ci	},
24362306a36Sopenharmony_ci	.attributes = {
24462306a36Sopenharmony_ci		.attrs = synth_attrs,
24562306a36Sopenharmony_ci		.name = "decpc",
24662306a36Sopenharmony_ci	},
24762306a36Sopenharmony_ci};
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_cistatic int dt_getstatus(void)
25062306a36Sopenharmony_ci{
25162306a36Sopenharmony_ci	dt_stat = inb_p(speakup_info.port_tts) |
25262306a36Sopenharmony_ci		 (inb_p(speakup_info.port_tts + 1) << 8);
25362306a36Sopenharmony_ci	return dt_stat;
25462306a36Sopenharmony_ci}
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_cistatic void dt_sendcmd(u_int cmd)
25762306a36Sopenharmony_ci{
25862306a36Sopenharmony_ci	outb_p(cmd & 0xFF, speakup_info.port_tts);
25962306a36Sopenharmony_ci	outb_p((cmd >> 8) & 0xFF, speakup_info.port_tts + 1);
26062306a36Sopenharmony_ci}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_cistatic int dt_waitbit(int bit)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	int timeout = 100;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	while (--timeout > 0) {
26762306a36Sopenharmony_ci		if ((dt_getstatus() & bit) == bit)
26862306a36Sopenharmony_ci			return 1;
26962306a36Sopenharmony_ci		udelay(50);
27062306a36Sopenharmony_ci	}
27162306a36Sopenharmony_ci	return 0;
27262306a36Sopenharmony_ci}
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_cistatic int dt_wait_dma(void)
27562306a36Sopenharmony_ci{
27662306a36Sopenharmony_ci	int timeout = 100, state = dma_state;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	if (!dt_waitbit(STAT_dma_ready))
27962306a36Sopenharmony_ci		return 0;
28062306a36Sopenharmony_ci	while (--timeout > 0) {
28162306a36Sopenharmony_ci		if ((dt_getstatus() & STAT_dma_state) == state)
28262306a36Sopenharmony_ci			return 1;
28362306a36Sopenharmony_ci		udelay(50);
28462306a36Sopenharmony_ci	}
28562306a36Sopenharmony_ci	dma_state = dt_getstatus() & STAT_dma_state;
28662306a36Sopenharmony_ci	return 1;
28762306a36Sopenharmony_ci}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_cistatic int dt_ctrl(u_int cmd)
29062306a36Sopenharmony_ci{
29162306a36Sopenharmony_ci	int timeout = 10;
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	if (!dt_waitbit(STAT_cmd_ready))
29462306a36Sopenharmony_ci		return -1;
29562306a36Sopenharmony_ci	outb_p(0, speakup_info.port_tts + 2);
29662306a36Sopenharmony_ci	outb_p(0, speakup_info.port_tts + 3);
29762306a36Sopenharmony_ci	dt_getstatus();
29862306a36Sopenharmony_ci	dt_sendcmd(CMD_control | cmd);
29962306a36Sopenharmony_ci	outb_p(0, speakup_info.port_tts + 6);
30062306a36Sopenharmony_ci	while (dt_getstatus() & STAT_cmd_ready) {
30162306a36Sopenharmony_ci		udelay(20);
30262306a36Sopenharmony_ci		if (--timeout == 0)
30362306a36Sopenharmony_ci			break;
30462306a36Sopenharmony_ci	}
30562306a36Sopenharmony_ci	dt_sendcmd(CMD_null);
30662306a36Sopenharmony_ci	return 0;
30762306a36Sopenharmony_ci}
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_cistatic void synth_flush(struct spk_synth *synth)
31062306a36Sopenharmony_ci{
31162306a36Sopenharmony_ci	int timeout = 10;
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	if (is_flushing)
31462306a36Sopenharmony_ci		return;
31562306a36Sopenharmony_ci	is_flushing = 4;
31662306a36Sopenharmony_ci	in_escape = 0;
31762306a36Sopenharmony_ci	while (dt_ctrl(CTRL_flush)) {
31862306a36Sopenharmony_ci		if (--timeout == 0)
31962306a36Sopenharmony_ci			break;
32062306a36Sopenharmony_ci		udelay(50);
32162306a36Sopenharmony_ci	}
32262306a36Sopenharmony_ci	for (timeout = 0; timeout < 10; timeout++) {
32362306a36Sopenharmony_ci		if (dt_waitbit(STAT_dma_ready))
32462306a36Sopenharmony_ci			break;
32562306a36Sopenharmony_ci		udelay(50);
32662306a36Sopenharmony_ci	}
32762306a36Sopenharmony_ci	outb_p(DMA_sync, speakup_info.port_tts + 4);
32862306a36Sopenharmony_ci	outb_p(0, speakup_info.port_tts + 4);
32962306a36Sopenharmony_ci	udelay(100);
33062306a36Sopenharmony_ci	for (timeout = 0; timeout < 10; timeout++) {
33162306a36Sopenharmony_ci		if (!(dt_getstatus() & STAT_flushing))
33262306a36Sopenharmony_ci			break;
33362306a36Sopenharmony_ci		udelay(50);
33462306a36Sopenharmony_ci	}
33562306a36Sopenharmony_ci	dma_state = dt_getstatus() & STAT_dma_state;
33662306a36Sopenharmony_ci	dma_state ^= STAT_dma_state;
33762306a36Sopenharmony_ci	is_flushing = 0;
33862306a36Sopenharmony_ci}
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_cistatic int dt_sendchar(char ch)
34162306a36Sopenharmony_ci{
34262306a36Sopenharmony_ci	if (!dt_wait_dma())
34362306a36Sopenharmony_ci		return -1;
34462306a36Sopenharmony_ci	if (!(dt_stat & STAT_rr_char))
34562306a36Sopenharmony_ci		return -2;
34662306a36Sopenharmony_ci	outb_p(DMA_single_in, speakup_info.port_tts + 4);
34762306a36Sopenharmony_ci	outb_p(ch, speakup_info.port_tts + 4);
34862306a36Sopenharmony_ci	dma_state ^= STAT_dma_state;
34962306a36Sopenharmony_ci	return 0;
35062306a36Sopenharmony_ci}
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_cistatic int testkernel(void)
35362306a36Sopenharmony_ci{
35462306a36Sopenharmony_ci	int status = 0;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	if (dt_getstatus() == 0xffff) {
35762306a36Sopenharmony_ci		status = -1;
35862306a36Sopenharmony_ci		goto oops;
35962306a36Sopenharmony_ci	}
36062306a36Sopenharmony_ci	dt_sendcmd(CMD_sync);
36162306a36Sopenharmony_ci	if (!dt_waitbit(STAT_cmd_ready))
36262306a36Sopenharmony_ci		status = -2;
36362306a36Sopenharmony_ci	else if (dt_stat & 0x8000)
36462306a36Sopenharmony_ci		return 0;
36562306a36Sopenharmony_ci	else if (dt_stat == 0x0dec)
36662306a36Sopenharmony_ci		pr_warn("dec_pc at 0x%x, software not loaded\n",
36762306a36Sopenharmony_ci			speakup_info.port_tts);
36862306a36Sopenharmony_ci	status = -3;
36962306a36Sopenharmony_cioops:	synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
37062306a36Sopenharmony_ci	speakup_info.port_tts = 0;
37162306a36Sopenharmony_ci	return status;
37262306a36Sopenharmony_ci}
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth)
37562306a36Sopenharmony_ci{
37662306a36Sopenharmony_ci	u_char ch;
37762306a36Sopenharmony_ci	static u_char last;
37862306a36Sopenharmony_ci	unsigned long flags;
37962306a36Sopenharmony_ci	unsigned long jiff_max;
38062306a36Sopenharmony_ci	struct var_t *jiffy_delta;
38162306a36Sopenharmony_ci	struct var_t *delay_time;
38262306a36Sopenharmony_ci	int jiffy_delta_val;
38362306a36Sopenharmony_ci	int delay_time_val;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	jiffy_delta = spk_get_var(JIFFY);
38662306a36Sopenharmony_ci	delay_time = spk_get_var(DELAY);
38762306a36Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
38862306a36Sopenharmony_ci	jiffy_delta_val = jiffy_delta->u.n.value;
38962306a36Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
39062306a36Sopenharmony_ci	jiff_max = jiffies + jiffy_delta_val;
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	while (!kthread_should_stop()) {
39362306a36Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
39462306a36Sopenharmony_ci		if (speakup_info.flushing) {
39562306a36Sopenharmony_ci			speakup_info.flushing = 0;
39662306a36Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
39762306a36Sopenharmony_ci			synth->flush(synth);
39862306a36Sopenharmony_ci			continue;
39962306a36Sopenharmony_ci		}
40062306a36Sopenharmony_ci		synth_buffer_skip_nonlatin1();
40162306a36Sopenharmony_ci		if (synth_buffer_empty()) {
40262306a36Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
40362306a36Sopenharmony_ci			break;
40462306a36Sopenharmony_ci		}
40562306a36Sopenharmony_ci		ch = synth_buffer_peek();
40662306a36Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
40762306a36Sopenharmony_ci		delay_time_val = delay_time->u.n.value;
40862306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
40962306a36Sopenharmony_ci		if (ch == '\n')
41062306a36Sopenharmony_ci			ch = 0x0D;
41162306a36Sopenharmony_ci		if (dt_sendchar(ch)) {
41262306a36Sopenharmony_ci			schedule_timeout(msecs_to_jiffies(delay_time_val));
41362306a36Sopenharmony_ci			continue;
41462306a36Sopenharmony_ci		}
41562306a36Sopenharmony_ci		set_current_state(TASK_RUNNING);
41662306a36Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
41762306a36Sopenharmony_ci		synth_buffer_getc();
41862306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
41962306a36Sopenharmony_ci		if (ch == '[') {
42062306a36Sopenharmony_ci			in_escape = 1;
42162306a36Sopenharmony_ci		} else if (ch == ']') {
42262306a36Sopenharmony_ci			in_escape = 0;
42362306a36Sopenharmony_ci		} else if (ch <= SPACE) {
42462306a36Sopenharmony_ci			if (!in_escape && strchr(",.!?;:", last))
42562306a36Sopenharmony_ci				dt_sendchar(PROCSPEECH);
42662306a36Sopenharmony_ci			if (time_after_eq(jiffies, jiff_max)) {
42762306a36Sopenharmony_ci				if (!in_escape)
42862306a36Sopenharmony_ci					dt_sendchar(PROCSPEECH);
42962306a36Sopenharmony_ci				spin_lock_irqsave(&speakup_info.spinlock,
43062306a36Sopenharmony_ci						  flags);
43162306a36Sopenharmony_ci				jiffy_delta_val = jiffy_delta->u.n.value;
43262306a36Sopenharmony_ci				delay_time_val = delay_time->u.n.value;
43362306a36Sopenharmony_ci				spin_unlock_irqrestore(&speakup_info.spinlock,
43462306a36Sopenharmony_ci						       flags);
43562306a36Sopenharmony_ci				schedule_timeout(msecs_to_jiffies
43662306a36Sopenharmony_ci						 (delay_time_val));
43762306a36Sopenharmony_ci				jiff_max = jiffies + jiffy_delta_val;
43862306a36Sopenharmony_ci			}
43962306a36Sopenharmony_ci		}
44062306a36Sopenharmony_ci		last = ch;
44162306a36Sopenharmony_ci		ch = 0;
44262306a36Sopenharmony_ci	}
44362306a36Sopenharmony_ci	if (!in_escape)
44462306a36Sopenharmony_ci		dt_sendchar(PROCSPEECH);
44562306a36Sopenharmony_ci}
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_cistatic const char *synth_immediate(struct spk_synth *synth, const char *buf)
44862306a36Sopenharmony_ci{
44962306a36Sopenharmony_ci	u_char ch;
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	while ((ch = *buf)) {
45262306a36Sopenharmony_ci		if (ch == '\n')
45362306a36Sopenharmony_ci			ch = PROCSPEECH;
45462306a36Sopenharmony_ci		if (dt_sendchar(ch))
45562306a36Sopenharmony_ci			return buf;
45662306a36Sopenharmony_ci		buf++;
45762306a36Sopenharmony_ci	}
45862306a36Sopenharmony_ci	return NULL;
45962306a36Sopenharmony_ci}
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_cistatic int synth_probe(struct spk_synth *synth)
46262306a36Sopenharmony_ci{
46362306a36Sopenharmony_ci	int i = 0, failed = 0;
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci	pr_info("Probing for %s.\n", synth->long_name);
46662306a36Sopenharmony_ci	for (i = 0; synth_portlist[i]; i++) {
46762306a36Sopenharmony_ci		if (synth_request_region(synth_portlist[i], SYNTH_IO_EXTENT)) {
46862306a36Sopenharmony_ci			pr_warn("request_region: failed with 0x%x, %d\n",
46962306a36Sopenharmony_ci				synth_portlist[i], SYNTH_IO_EXTENT);
47062306a36Sopenharmony_ci			continue;
47162306a36Sopenharmony_ci		}
47262306a36Sopenharmony_ci		speakup_info.port_tts = synth_portlist[i];
47362306a36Sopenharmony_ci		failed = testkernel();
47462306a36Sopenharmony_ci		if (failed == 0)
47562306a36Sopenharmony_ci			break;
47662306a36Sopenharmony_ci	}
47762306a36Sopenharmony_ci	if (failed) {
47862306a36Sopenharmony_ci		pr_info("%s: not found\n", synth->long_name);
47962306a36Sopenharmony_ci		return -ENODEV;
48062306a36Sopenharmony_ci	}
48162306a36Sopenharmony_ci	pr_info("%s: %03x-%03x, Driver Version %s,\n", synth->long_name,
48262306a36Sopenharmony_ci		speakup_info.port_tts, speakup_info.port_tts + 7,
48362306a36Sopenharmony_ci		synth->version);
48462306a36Sopenharmony_ci	synth->alive = 1;
48562306a36Sopenharmony_ci	return 0;
48662306a36Sopenharmony_ci}
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_cistatic void dtpc_release(struct spk_synth *synth)
48962306a36Sopenharmony_ci{
49062306a36Sopenharmony_ci	spk_stop_serial_interrupt();
49162306a36Sopenharmony_ci	if (speakup_info.port_tts)
49262306a36Sopenharmony_ci		synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
49362306a36Sopenharmony_ci	speakup_info.port_tts = 0;
49462306a36Sopenharmony_ci}
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_cimodule_param_named(start, synth_dec_pc.startup, short, 0444);
49762306a36Sopenharmony_cimodule_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
49862306a36Sopenharmony_cimodule_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
49962306a36Sopenharmony_cimodule_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);
50062306a36Sopenharmony_cimodule_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
50162306a36Sopenharmony_cimodule_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
50262306a36Sopenharmony_cimodule_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
50362306a36Sopenharmony_cimodule_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ciMODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
50962306a36Sopenharmony_ciMODULE_PARM_DESC(rate, "Set the rate variable on load.");
51062306a36Sopenharmony_ciMODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
51162306a36Sopenharmony_ciMODULE_PARM_DESC(inflection, "Set the inflection variable on load.");
51262306a36Sopenharmony_ciMODULE_PARM_DESC(vol, "Set the vol variable on load.");
51362306a36Sopenharmony_ciMODULE_PARM_DESC(punct, "Set the punct variable on load.");
51462306a36Sopenharmony_ciMODULE_PARM_DESC(voice, "Set the voice variable on load.");
51562306a36Sopenharmony_ciMODULE_PARM_DESC(direct, "Set the direct variable on load.");
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_cimodule_spk_synth(synth_dec_pc);
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ciMODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
52062306a36Sopenharmony_ciMODULE_AUTHOR("David Borowski");
52162306a36Sopenharmony_ciMODULE_DESCRIPTION("Speakup support for DECtalk PC synthesizers");
52262306a36Sopenharmony_ciMODULE_LICENSE("GPL");
52362306a36Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
524