162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * written by David Borowski
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2003 David Borowski.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * specifically written as a driver for the speakup screenreview
862306a36Sopenharmony_ci * package it's not a general device driver.
962306a36Sopenharmony_ci * This driver is for the Keynote Gold internal synthesizer.
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci#include <linux/jiffies.h>
1262306a36Sopenharmony_ci#include <linux/sched.h>
1362306a36Sopenharmony_ci#include <linux/timer.h>
1462306a36Sopenharmony_ci#include <linux/kthread.h>
1562306a36Sopenharmony_ci#include <linux/serial_reg.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "spk_priv.h"
1862306a36Sopenharmony_ci#include "speakup.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define DRV_VERSION "2.10"
2162306a36Sopenharmony_ci#define SYNTH_IO_EXTENT	0x04
2262306a36Sopenharmony_ci#define SWAIT udelay(70)
2362306a36Sopenharmony_ci#define PROCSPEECH 0x1f
2462306a36Sopenharmony_ci#define SYNTH_CLEAR 0x03
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic int synth_probe(struct spk_synth *synth);
2762306a36Sopenharmony_cistatic void keynote_release(struct spk_synth *synth);
2862306a36Sopenharmony_cistatic const char *synth_immediate(struct spk_synth *synth, const char *buf);
2962306a36Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth);
3062306a36Sopenharmony_cistatic void synth_flush(struct spk_synth *synth);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic int synth_port;
3362306a36Sopenharmony_cistatic int port_forced;
3462306a36Sopenharmony_cistatic unsigned int synth_portlist[] = { 0x2a8, 0 };
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cienum default_vars_id {
3862306a36Sopenharmony_ci	CAPS_START_ID = 0, CAPS_STOP_ID,
3962306a36Sopenharmony_ci	RATE_ID, PITCH_ID,
4062306a36Sopenharmony_ci	DIRECT_ID, V_LAST_VAR_ID,
4162306a36Sopenharmony_ci	NB_ID
4262306a36Sopenharmony_ci};
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistatic struct var_t vars[NB_ID] = {
4662306a36Sopenharmony_ci	[CAPS_START_ID] = { CAPS_START, .u.s = {"[f130]" } },
4762306a36Sopenharmony_ci	[CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[f90]" } },
4862306a36Sopenharmony_ci	[RATE_ID] = { RATE, .u.n = {"\04%c ", 8, 0, 10, 81, -8, NULL } },
4962306a36Sopenharmony_ci	[PITCH_ID] = { PITCH, .u.n = {"[f%d]", 5, 0, 9, 40, 10, NULL } },
5062306a36Sopenharmony_ci	[DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
5162306a36Sopenharmony_ci	V_LAST_VAR
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/*
5562306a36Sopenharmony_ci * These attributes will appear in /sys/accessibility/speakup/keypc.
5662306a36Sopenharmony_ci */
5762306a36Sopenharmony_cistatic struct kobj_attribute caps_start_attribute =
5862306a36Sopenharmony_ci	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
5962306a36Sopenharmony_cistatic struct kobj_attribute caps_stop_attribute =
6062306a36Sopenharmony_ci	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
6162306a36Sopenharmony_cistatic struct kobj_attribute pitch_attribute =
6262306a36Sopenharmony_ci	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
6362306a36Sopenharmony_cistatic struct kobj_attribute rate_attribute =
6462306a36Sopenharmony_ci	__ATTR(rate, 0644, spk_var_show, spk_var_store);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistatic struct kobj_attribute delay_time_attribute =
6762306a36Sopenharmony_ci	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
6862306a36Sopenharmony_cistatic struct kobj_attribute direct_attribute =
6962306a36Sopenharmony_ci	__ATTR(direct, 0644, spk_var_show, spk_var_store);
7062306a36Sopenharmony_cistatic struct kobj_attribute full_time_attribute =
7162306a36Sopenharmony_ci	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
7262306a36Sopenharmony_cistatic struct kobj_attribute jiffy_delta_attribute =
7362306a36Sopenharmony_ci	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
7462306a36Sopenharmony_cistatic struct kobj_attribute trigger_time_attribute =
7562306a36Sopenharmony_ci	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/*
7862306a36Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all
7962306a36Sopenharmony_ci * at once.
8062306a36Sopenharmony_ci */
8162306a36Sopenharmony_cistatic struct attribute *synth_attrs[] = {
8262306a36Sopenharmony_ci	&caps_start_attribute.attr,
8362306a36Sopenharmony_ci	&caps_stop_attribute.attr,
8462306a36Sopenharmony_ci	&pitch_attribute.attr,
8562306a36Sopenharmony_ci	&rate_attribute.attr,
8662306a36Sopenharmony_ci	&delay_time_attribute.attr,
8762306a36Sopenharmony_ci	&direct_attribute.attr,
8862306a36Sopenharmony_ci	&full_time_attribute.attr,
8962306a36Sopenharmony_ci	&jiffy_delta_attribute.attr,
9062306a36Sopenharmony_ci	&trigger_time_attribute.attr,
9162306a36Sopenharmony_ci	NULL,	/* need to NULL terminate the list of attributes */
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic struct spk_synth synth_keypc = {
9562306a36Sopenharmony_ci	.name = "keypc",
9662306a36Sopenharmony_ci	.version = DRV_VERSION,
9762306a36Sopenharmony_ci	.long_name = "Keynote PC",
9862306a36Sopenharmony_ci	.init = "[t][n7,1][n8,0]",
9962306a36Sopenharmony_ci	.procspeech = PROCSPEECH,
10062306a36Sopenharmony_ci	.clear = SYNTH_CLEAR,
10162306a36Sopenharmony_ci	.delay = 500,
10262306a36Sopenharmony_ci	.trigger = 50,
10362306a36Sopenharmony_ci	.jiffies = 50,
10462306a36Sopenharmony_ci	.full = 1000,
10562306a36Sopenharmony_ci	.startup = SYNTH_START,
10662306a36Sopenharmony_ci	.checkval = SYNTH_CHECK,
10762306a36Sopenharmony_ci	.vars = vars,
10862306a36Sopenharmony_ci	.io_ops = &spk_serial_io_ops,
10962306a36Sopenharmony_ci	.probe = synth_probe,
11062306a36Sopenharmony_ci	.release = keynote_release,
11162306a36Sopenharmony_ci	.synth_immediate = synth_immediate,
11262306a36Sopenharmony_ci	.catch_up = do_catch_up,
11362306a36Sopenharmony_ci	.flush = synth_flush,
11462306a36Sopenharmony_ci	.is_alive = spk_synth_is_alive_nop,
11562306a36Sopenharmony_ci	.synth_adjust = NULL,
11662306a36Sopenharmony_ci	.read_buff_add = NULL,
11762306a36Sopenharmony_ci	.get_index = NULL,
11862306a36Sopenharmony_ci	.indexing = {
11962306a36Sopenharmony_ci		.command = NULL,
12062306a36Sopenharmony_ci		.lowindex = 0,
12162306a36Sopenharmony_ci		.highindex = 0,
12262306a36Sopenharmony_ci		.currindex = 0,
12362306a36Sopenharmony_ci	},
12462306a36Sopenharmony_ci	.attributes = {
12562306a36Sopenharmony_ci		.attrs = synth_attrs,
12662306a36Sopenharmony_ci		.name = "keypc",
12762306a36Sopenharmony_ci	},
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic inline bool synth_writable(void)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	return (inb_p(synth_port + UART_RX) & 0x10) != 0;
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic inline bool synth_full(void)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	return (inb_p(synth_port + UART_RX) & 0x80) == 0;
13862306a36Sopenharmony_ci}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_cistatic char *oops(void)
14162306a36Sopenharmony_ci{
14262306a36Sopenharmony_ci	int s1, s2, s3, s4;
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	s1 = inb_p(synth_port);
14562306a36Sopenharmony_ci	s2 = inb_p(synth_port + 1);
14662306a36Sopenharmony_ci	s3 = inb_p(synth_port + 2);
14762306a36Sopenharmony_ci	s4 = inb_p(synth_port + 3);
14862306a36Sopenharmony_ci	pr_warn("synth timeout %d %d %d %d\n", s1, s2, s3, s4);
14962306a36Sopenharmony_ci	return NULL;
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistatic const char *synth_immediate(struct spk_synth *synth, const char *buf)
15362306a36Sopenharmony_ci{
15462306a36Sopenharmony_ci	u_char ch;
15562306a36Sopenharmony_ci	int timeout;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	while ((ch = *buf)) {
15862306a36Sopenharmony_ci		if (ch == '\n')
15962306a36Sopenharmony_ci			ch = PROCSPEECH;
16062306a36Sopenharmony_ci		if (synth_full())
16162306a36Sopenharmony_ci			return buf;
16262306a36Sopenharmony_ci		timeout = 1000;
16362306a36Sopenharmony_ci		while (synth_writable())
16462306a36Sopenharmony_ci			if (--timeout <= 0)
16562306a36Sopenharmony_ci				return oops();
16662306a36Sopenharmony_ci		outb_p(ch, synth_port);
16762306a36Sopenharmony_ci		udelay(70);
16862306a36Sopenharmony_ci		buf++;
16962306a36Sopenharmony_ci	}
17062306a36Sopenharmony_ci	return NULL;
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic void do_catch_up(struct spk_synth *synth)
17462306a36Sopenharmony_ci{
17562306a36Sopenharmony_ci	u_char ch;
17662306a36Sopenharmony_ci	int timeout;
17762306a36Sopenharmony_ci	unsigned long flags;
17862306a36Sopenharmony_ci	unsigned long jiff_max;
17962306a36Sopenharmony_ci	struct var_t *jiffy_delta;
18062306a36Sopenharmony_ci	struct var_t *delay_time;
18162306a36Sopenharmony_ci	struct var_t *full_time;
18262306a36Sopenharmony_ci	int delay_time_val;
18362306a36Sopenharmony_ci	int full_time_val;
18462306a36Sopenharmony_ci	int jiffy_delta_val;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	jiffy_delta = spk_get_var(JIFFY);
18762306a36Sopenharmony_ci	delay_time = spk_get_var(DELAY);
18862306a36Sopenharmony_ci	full_time = spk_get_var(FULL);
18962306a36Sopenharmony_ci	spin_lock_irqsave(&speakup_info.spinlock, flags);
19062306a36Sopenharmony_ci	jiffy_delta_val = jiffy_delta->u.n.value;
19162306a36Sopenharmony_ci	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	jiff_max = jiffies + jiffy_delta_val;
19462306a36Sopenharmony_ci	while (!kthread_should_stop()) {
19562306a36Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
19662306a36Sopenharmony_ci		if (speakup_info.flushing) {
19762306a36Sopenharmony_ci			speakup_info.flushing = 0;
19862306a36Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
19962306a36Sopenharmony_ci			synth->flush(synth);
20062306a36Sopenharmony_ci			continue;
20162306a36Sopenharmony_ci		}
20262306a36Sopenharmony_ci		synth_buffer_skip_nonlatin1();
20362306a36Sopenharmony_ci		if (synth_buffer_empty()) {
20462306a36Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
20562306a36Sopenharmony_ci			break;
20662306a36Sopenharmony_ci		}
20762306a36Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
20862306a36Sopenharmony_ci		full_time_val = full_time->u.n.value;
20962306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
21062306a36Sopenharmony_ci		if (synth_full()) {
21162306a36Sopenharmony_ci			schedule_timeout(msecs_to_jiffies(full_time_val));
21262306a36Sopenharmony_ci			continue;
21362306a36Sopenharmony_ci		}
21462306a36Sopenharmony_ci		set_current_state(TASK_RUNNING);
21562306a36Sopenharmony_ci		timeout = 1000;
21662306a36Sopenharmony_ci		while (synth_writable())
21762306a36Sopenharmony_ci			if (--timeout <= 0)
21862306a36Sopenharmony_ci				break;
21962306a36Sopenharmony_ci		if (timeout <= 0) {
22062306a36Sopenharmony_ci			oops();
22162306a36Sopenharmony_ci			break;
22262306a36Sopenharmony_ci		}
22362306a36Sopenharmony_ci		spin_lock_irqsave(&speakup_info.spinlock, flags);
22462306a36Sopenharmony_ci		ch = synth_buffer_getc();
22562306a36Sopenharmony_ci		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
22662306a36Sopenharmony_ci		if (ch == '\n')
22762306a36Sopenharmony_ci			ch = PROCSPEECH;
22862306a36Sopenharmony_ci		outb_p(ch, synth_port);
22962306a36Sopenharmony_ci		SWAIT;
23062306a36Sopenharmony_ci		if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
23162306a36Sopenharmony_ci			timeout = 1000;
23262306a36Sopenharmony_ci			while (synth_writable())
23362306a36Sopenharmony_ci				if (--timeout <= 0)
23462306a36Sopenharmony_ci					break;
23562306a36Sopenharmony_ci			if (timeout <= 0) {
23662306a36Sopenharmony_ci				oops();
23762306a36Sopenharmony_ci				break;
23862306a36Sopenharmony_ci			}
23962306a36Sopenharmony_ci			outb_p(PROCSPEECH, synth_port);
24062306a36Sopenharmony_ci			spin_lock_irqsave(&speakup_info.spinlock, flags);
24162306a36Sopenharmony_ci			jiffy_delta_val = jiffy_delta->u.n.value;
24262306a36Sopenharmony_ci			delay_time_val = delay_time->u.n.value;
24362306a36Sopenharmony_ci			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
24462306a36Sopenharmony_ci			schedule_timeout(msecs_to_jiffies(delay_time_val));
24562306a36Sopenharmony_ci			jiff_max = jiffies + jiffy_delta_val;
24662306a36Sopenharmony_ci		}
24762306a36Sopenharmony_ci	}
24862306a36Sopenharmony_ci	timeout = 1000;
24962306a36Sopenharmony_ci	while (synth_writable())
25062306a36Sopenharmony_ci		if (--timeout <= 0)
25162306a36Sopenharmony_ci			break;
25262306a36Sopenharmony_ci	if (timeout <= 0)
25362306a36Sopenharmony_ci		oops();
25462306a36Sopenharmony_ci	else
25562306a36Sopenharmony_ci		outb_p(PROCSPEECH, synth_port);
25662306a36Sopenharmony_ci}
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_cistatic void synth_flush(struct spk_synth *synth)
25962306a36Sopenharmony_ci{
26062306a36Sopenharmony_ci	outb_p(SYNTH_CLEAR, synth_port);
26162306a36Sopenharmony_ci}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_cistatic int synth_probe(struct spk_synth *synth)
26462306a36Sopenharmony_ci{
26562306a36Sopenharmony_ci	unsigned int port_val = 0;
26662306a36Sopenharmony_ci	int i;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	pr_info("Probing for %s.\n", synth->long_name);
26962306a36Sopenharmony_ci	if (port_forced) {
27062306a36Sopenharmony_ci		synth_port = port_forced;
27162306a36Sopenharmony_ci		pr_info("probe forced to %x by kernel command line\n",
27262306a36Sopenharmony_ci			synth_port);
27362306a36Sopenharmony_ci		if (synth_request_region(synth_port - 1, SYNTH_IO_EXTENT)) {
27462306a36Sopenharmony_ci			pr_warn("sorry, port already reserved\n");
27562306a36Sopenharmony_ci			return -EBUSY;
27662306a36Sopenharmony_ci		}
27762306a36Sopenharmony_ci		port_val = inb(synth_port);
27862306a36Sopenharmony_ci	} else {
27962306a36Sopenharmony_ci		for (i = 0; synth_portlist[i]; i++) {
28062306a36Sopenharmony_ci			if (synth_request_region(synth_portlist[i],
28162306a36Sopenharmony_ci						 SYNTH_IO_EXTENT)) {
28262306a36Sopenharmony_ci				pr_warn
28362306a36Sopenharmony_ci				    ("request_region: failed with 0x%x, %d\n",
28462306a36Sopenharmony_ci				     synth_portlist[i], SYNTH_IO_EXTENT);
28562306a36Sopenharmony_ci				continue;
28662306a36Sopenharmony_ci			}
28762306a36Sopenharmony_ci			port_val = inb(synth_portlist[i]);
28862306a36Sopenharmony_ci			if (port_val == 0x80) {
28962306a36Sopenharmony_ci				synth_port = synth_portlist[i];
29062306a36Sopenharmony_ci				break;
29162306a36Sopenharmony_ci			}
29262306a36Sopenharmony_ci		}
29362306a36Sopenharmony_ci	}
29462306a36Sopenharmony_ci	if (port_val != 0x80) {
29562306a36Sopenharmony_ci		pr_info("%s: not found\n", synth->long_name);
29662306a36Sopenharmony_ci		synth_release_region(synth_port, SYNTH_IO_EXTENT);
29762306a36Sopenharmony_ci		synth_port = 0;
29862306a36Sopenharmony_ci		return -ENODEV;
29962306a36Sopenharmony_ci	}
30062306a36Sopenharmony_ci	pr_info("%s: %03x-%03x, driver version %s,\n", synth->long_name,
30162306a36Sopenharmony_ci		synth_port, synth_port + SYNTH_IO_EXTENT - 1,
30262306a36Sopenharmony_ci		synth->version);
30362306a36Sopenharmony_ci	synth->alive = 1;
30462306a36Sopenharmony_ci	return 0;
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_cistatic void keynote_release(struct spk_synth *synth)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	spk_stop_serial_interrupt();
31062306a36Sopenharmony_ci	if (synth_port)
31162306a36Sopenharmony_ci		synth_release_region(synth_port, SYNTH_IO_EXTENT);
31262306a36Sopenharmony_ci	synth_port = 0;
31362306a36Sopenharmony_ci}
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_cimodule_param_hw_named(port, port_forced, int, ioport, 0444);
31662306a36Sopenharmony_cimodule_param_named(start, synth_keypc.startup, short, 0444);
31762306a36Sopenharmony_cimodule_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
31862306a36Sopenharmony_cimodule_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
31962306a36Sopenharmony_cimodule_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ciMODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
32262306a36Sopenharmony_ciMODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
32362306a36Sopenharmony_ciMODULE_PARM_DESC(rate, "Set the rate variable on load.");
32462306a36Sopenharmony_ciMODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
32562306a36Sopenharmony_ciMODULE_PARM_DESC(direct, "Set the direct variable on load.");
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_cimodule_spk_synth(synth_keypc);
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ciMODULE_AUTHOR("David Borowski");
33262306a36Sopenharmony_ciMODULE_DESCRIPTION("Speakup support for Keynote Gold PC synthesizers");
33362306a36Sopenharmony_ciMODULE_LICENSE("GPL");
33462306a36Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
33562306a36Sopenharmony_ci
336