1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * originally written by: Kirk Reiser <kirk@braille.uwo.ca>
4  * this version considerably modified by David Borowski, david575@rogers.com
5  *
6  * Copyright (C) 1998-99  Kirk Reiser.
7  * Copyright (C) 2003 David Borowski.
8  *
9  * this code is specifically written as a driver for the speakup screenreview
10  * package and is not a general device driver.
11  */
12 #include <linux/jiffies.h>
13 #include <linux/sched.h>
14 #include <linux/timer.h>
15 #include <linux/kthread.h>
16 #include <linux/serial_reg.h>	/* for UART_MCR* constants */
17 
18 #include "spk_priv.h"
19 #include "speakup.h"
20 
21 #define DRV_VERSION "2.21"
22 #define SYNTH_CLEAR 0x18
23 #define PROCSPEECH '\r'
24 
25 static void do_catch_up(struct spk_synth *synth);
26 
27 static struct var_t vars[] = {
28 	{ CAPS_START, .u.s = {"cap, " } },
29 	{ CAPS_STOP, .u.s = {"" } },
30 	{ RATE, .u.n = {"@W%d", 6, 1, 9, 0, 0, NULL } },
31 	{ PITCH, .u.n = {"@F%x", 10, 0, 15, 0, 0, NULL } },
32 	{ VOL, .u.n = {"@A%x", 10, 0, 15, 0, 0, NULL } },
33 	{ VOICE, .u.n = {"@V%d", 1, 1, 6, 0, 0, NULL } },
34 	{ LANG, .u.n = {"@=%d,", 1, 1, 4, 0, 0, NULL } },
35 	{ DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
36 	V_LAST_VAR
37 };
38 
39 /*
40  * These attributes will appear in /sys/accessibility/speakup/apollo.
41  */
42 static struct kobj_attribute caps_start_attribute =
43 	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
44 static struct kobj_attribute caps_stop_attribute =
45 	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
46 static struct kobj_attribute lang_attribute =
47 	__ATTR(lang, 0644, spk_var_show, spk_var_store);
48 static struct kobj_attribute pitch_attribute =
49 	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
50 static struct kobj_attribute rate_attribute =
51 	__ATTR(rate, 0644, spk_var_show, spk_var_store);
52 static struct kobj_attribute voice_attribute =
53 	__ATTR(voice, 0644, spk_var_show, spk_var_store);
54 static struct kobj_attribute vol_attribute =
55 	__ATTR(vol, 0644, spk_var_show, spk_var_store);
56 
57 static struct kobj_attribute delay_time_attribute =
58 	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
59 static struct kobj_attribute direct_attribute =
60 	__ATTR(direct, 0644, spk_var_show, spk_var_store);
61 static struct kobj_attribute full_time_attribute =
62 	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
63 static struct kobj_attribute jiffy_delta_attribute =
64 	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
65 static struct kobj_attribute trigger_time_attribute =
66 	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
67 
68 /*
69  * Create a group of attributes so that we can create and destroy them all
70  * at once.
71  */
72 static struct attribute *synth_attrs[] = {
73 	&caps_start_attribute.attr,
74 	&caps_stop_attribute.attr,
75 	&lang_attribute.attr,
76 	&pitch_attribute.attr,
77 	&rate_attribute.attr,
78 	&voice_attribute.attr,
79 	&vol_attribute.attr,
80 	&delay_time_attribute.attr,
81 	&direct_attribute.attr,
82 	&full_time_attribute.attr,
83 	&jiffy_delta_attribute.attr,
84 	&trigger_time_attribute.attr,
85 	NULL,	/* need to NULL terminate the list of attributes */
86 };
87 
88 static struct spk_synth synth_apollo = {
89 	.name = "apollo",
90 	.version = DRV_VERSION,
91 	.long_name = "Apollo",
92 	.init = "@R3@D0@K1\r",
93 	.procspeech = PROCSPEECH,
94 	.clear = SYNTH_CLEAR,
95 	.delay = 500,
96 	.trigger = 50,
97 	.jiffies = 50,
98 	.full = 40000,
99 	.dev_name = SYNTH_DEFAULT_DEV,
100 	.startup = SYNTH_START,
101 	.checkval = SYNTH_CHECK,
102 	.vars = vars,
103 	.io_ops = &spk_ttyio_ops,
104 	.probe = spk_ttyio_synth_probe,
105 	.release = spk_ttyio_release,
106 	.synth_immediate = spk_ttyio_synth_immediate,
107 	.catch_up = do_catch_up,
108 	.flush = spk_synth_flush,
109 	.is_alive = spk_synth_is_alive_restart,
110 	.synth_adjust = NULL,
111 	.read_buff_add = NULL,
112 	.get_index = NULL,
113 	.indexing = {
114 		.command = NULL,
115 		.lowindex = 0,
116 		.highindex = 0,
117 		.currindex = 0,
118 	},
119 	.attributes = {
120 		.attrs = synth_attrs,
121 		.name = "apollo",
122 	},
123 };
124 
125 static void do_catch_up(struct spk_synth *synth)
126 {
127 	u_char ch;
128 	unsigned long flags;
129 	unsigned long jiff_max;
130 	struct var_t *jiffy_delta;
131 	struct var_t *delay_time;
132 	struct var_t *full_time;
133 	int full_time_val = 0;
134 	int delay_time_val = 0;
135 	int jiffy_delta_val = 0;
136 
137 	jiffy_delta = spk_get_var(JIFFY);
138 	delay_time = spk_get_var(DELAY);
139 	full_time = spk_get_var(FULL);
140 	spin_lock_irqsave(&speakup_info.spinlock, flags);
141 	jiffy_delta_val = jiffy_delta->u.n.value;
142 	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
143 	jiff_max = jiffies + jiffy_delta_val;
144 
145 	while (!kthread_should_stop()) {
146 		spin_lock_irqsave(&speakup_info.spinlock, flags);
147 		jiffy_delta_val = jiffy_delta->u.n.value;
148 		full_time_val = full_time->u.n.value;
149 		delay_time_val = delay_time->u.n.value;
150 		if (speakup_info.flushing) {
151 			speakup_info.flushing = 0;
152 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
153 			synth->flush(synth);
154 			continue;
155 		}
156 		synth_buffer_skip_nonlatin1();
157 		if (synth_buffer_empty()) {
158 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
159 			break;
160 		}
161 		ch = synth_buffer_peek();
162 		set_current_state(TASK_INTERRUPTIBLE);
163 		full_time_val = full_time->u.n.value;
164 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
165 		if (!synth->io_ops->synth_out(synth, ch)) {
166 			synth->io_ops->tiocmset(synth, 0, UART_MCR_RTS);
167 			synth->io_ops->tiocmset(synth, UART_MCR_RTS, 0);
168 			schedule_timeout(msecs_to_jiffies(full_time_val));
169 			continue;
170 		}
171 		if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
172 			spin_lock_irqsave(&speakup_info.spinlock, flags);
173 			jiffy_delta_val = jiffy_delta->u.n.value;
174 			full_time_val = full_time->u.n.value;
175 			delay_time_val = delay_time->u.n.value;
176 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
177 			if (synth->io_ops->synth_out(synth, synth->procspeech))
178 				schedule_timeout(msecs_to_jiffies
179 						 (delay_time_val));
180 			else
181 				schedule_timeout(msecs_to_jiffies
182 						 (full_time_val));
183 			jiff_max = jiffies + jiffy_delta_val;
184 		}
185 		set_current_state(TASK_RUNNING);
186 		spin_lock_irqsave(&speakup_info.spinlock, flags);
187 		synth_buffer_getc();
188 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
189 	}
190 	synth->io_ops->synth_out(synth, PROCSPEECH);
191 }
192 
193 module_param_named(ser, synth_apollo.ser, int, 0444);
194 module_param_named(dev, synth_apollo.dev_name, charp, 0444);
195 module_param_named(start, synth_apollo.startup, short, 0444);
196 
197 MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
198 MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
199 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
200 
201 module_spk_synth(synth_apollo);
202 
203 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
204 MODULE_AUTHOR("David Borowski");
205 MODULE_DESCRIPTION("Speakup support for Apollo II synthesizer");
206 MODULE_LICENSE("GPL");
207 MODULE_VERSION(DRV_VERSION);
208 
209