1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * 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 specificly written as a driver for the speakup screenreview
10  * package and is not a general device driver.
11  * This driver is for the Aicom Acent PC internal synthesizer.
12  */
13 
14 #include <linux/jiffies.h>
15 #include <linux/sched.h>
16 #include <linux/timer.h>
17 #include <linux/kthread.h>
18 
19 #include "spk_priv.h"
20 #include "serialio.h"
21 #include "speakup.h"
22 #include "speakup_acnt.h" /* local header file for Accent values */
23 
24 #define DRV_VERSION "2.10"
25 #define PROCSPEECH '\r'
26 
27 static int synth_probe(struct spk_synth *synth);
28 static void accent_release(struct spk_synth *synth);
29 static const char *synth_immediate(struct spk_synth *synth, const char *buf);
30 static void do_catch_up(struct spk_synth *synth);
31 static void synth_flush(struct spk_synth *synth);
32 
33 static int synth_port_control;
34 static int port_forced;
35 static unsigned int synth_portlist[] = { 0x2a8, 0 };
36 
37 static struct var_t vars[] = {
38 	{ CAPS_START, .u.s = {"\033P8" } },
39 	{ CAPS_STOP, .u.s = {"\033P5" } },
40 	{ RATE, .u.n = {"\033R%c", 9, 0, 17, 0, 0, "0123456789abcdefgh" } },
41 	{ PITCH, .u.n = {"\033P%d", 5, 0, 9, 0, 0, NULL } },
42 	{ VOL, .u.n = {"\033A%d", 5, 0, 9, 0, 0, NULL } },
43 	{ TONE, .u.n = {"\033V%d", 5, 0, 9, 0, 0, NULL } },
44 	{ DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
45 	V_LAST_VAR
46 };
47 
48 /*
49  * These attributes will appear in /sys/accessibility/speakup/acntpc.
50  */
51 static struct kobj_attribute caps_start_attribute =
52 	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
53 static struct kobj_attribute caps_stop_attribute =
54 	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
55 static struct kobj_attribute pitch_attribute =
56 	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
57 static struct kobj_attribute rate_attribute =
58 	__ATTR(rate, 0644, spk_var_show, spk_var_store);
59 static struct kobj_attribute tone_attribute =
60 	__ATTR(tone, 0644, spk_var_show, spk_var_store);
61 static struct kobj_attribute vol_attribute =
62 	__ATTR(vol, 0644, spk_var_show, spk_var_store);
63 
64 static struct kobj_attribute delay_time_attribute =
65 	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
66 static struct kobj_attribute direct_attribute =
67 	__ATTR(direct, 0644, spk_var_show, spk_var_store);
68 static struct kobj_attribute full_time_attribute =
69 	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
70 static struct kobj_attribute jiffy_delta_attribute =
71 	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
72 static struct kobj_attribute trigger_time_attribute =
73 	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
74 
75 /*
76  * Create a group of attributes so that we can create and destroy them all
77  * at once.
78  */
79 static struct attribute *synth_attrs[] = {
80 	&caps_start_attribute.attr,
81 	&caps_stop_attribute.attr,
82 	&pitch_attribute.attr,
83 	&rate_attribute.attr,
84 	&tone_attribute.attr,
85 	&vol_attribute.attr,
86 	&delay_time_attribute.attr,
87 	&direct_attribute.attr,
88 	&full_time_attribute.attr,
89 	&jiffy_delta_attribute.attr,
90 	&trigger_time_attribute.attr,
91 	NULL,	/* need to NULL terminate the list of attributes */
92 };
93 
94 static struct spk_synth synth_acntpc = {
95 	.name = "acntpc",
96 	.version = DRV_VERSION,
97 	.long_name = "Accent PC",
98 	.init = "\033=X \033Oi\033T2\033=M\033N1\n",
99 	.procspeech = PROCSPEECH,
100 	.clear = SYNTH_CLEAR,
101 	.delay = 500,
102 	.trigger = 50,
103 	.jiffies = 50,
104 	.full = 1000,
105 	.startup = SYNTH_START,
106 	.checkval = SYNTH_CHECK,
107 	.vars = vars,
108 	.io_ops = &spk_serial_io_ops,
109 	.probe = synth_probe,
110 	.release = accent_release,
111 	.synth_immediate = synth_immediate,
112 	.catch_up = do_catch_up,
113 	.flush = synth_flush,
114 	.is_alive = spk_synth_is_alive_nop,
115 	.synth_adjust = NULL,
116 	.read_buff_add = NULL,
117 	.get_index = NULL,
118 	.indexing = {
119 		.command = NULL,
120 		.lowindex = 0,
121 		.highindex = 0,
122 		.currindex = 0,
123 	},
124 	.attributes = {
125 		.attrs = synth_attrs,
126 		.name = "acntpc",
127 	},
128 };
129 
130 static inline bool synth_writable(void)
131 {
132 	return inb_p(synth_port_control) & SYNTH_WRITABLE;
133 }
134 
135 static inline bool synth_full(void)
136 {
137 	return inb_p(speakup_info.port_tts + UART_RX) == 'F';
138 }
139 
140 static const char *synth_immediate(struct spk_synth *synth, const char *buf)
141 {
142 	u_char ch;
143 
144 	while ((ch = *buf)) {
145 		int timeout = SPK_XMITR_TIMEOUT;
146 
147 		if (ch == '\n')
148 			ch = PROCSPEECH;
149 		if (synth_full())
150 			return buf;
151 		while (synth_writable()) {
152 			if (!--timeout)
153 				return buf;
154 			udelay(1);
155 		}
156 		outb_p(ch, speakup_info.port_tts);
157 		buf++;
158 	}
159 	return NULL;
160 }
161 
162 static void do_catch_up(struct spk_synth *synth)
163 {
164 	u_char ch;
165 	unsigned long flags;
166 	unsigned long jiff_max;
167 	int timeout;
168 	int delay_time_val;
169 	int jiffy_delta_val;
170 	int full_time_val;
171 	struct var_t *delay_time;
172 	struct var_t *full_time;
173 	struct var_t *jiffy_delta;
174 
175 	jiffy_delta = spk_get_var(JIFFY);
176 	delay_time = spk_get_var(DELAY);
177 	full_time = spk_get_var(FULL);
178 
179 	spin_lock_irqsave(&speakup_info.spinlock, flags);
180 	jiffy_delta_val = jiffy_delta->u.n.value;
181 	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
182 
183 	jiff_max = jiffies + jiffy_delta_val;
184 	while (!kthread_should_stop()) {
185 		spin_lock_irqsave(&speakup_info.spinlock, flags);
186 		if (speakup_info.flushing) {
187 			speakup_info.flushing = 0;
188 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
189 			synth->flush(synth);
190 			continue;
191 		}
192 		synth_buffer_skip_nonlatin1();
193 		if (synth_buffer_empty()) {
194 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
195 			break;
196 		}
197 		set_current_state(TASK_INTERRUPTIBLE);
198 		full_time_val = full_time->u.n.value;
199 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
200 		if (synth_full()) {
201 			schedule_timeout(msecs_to_jiffies(full_time_val));
202 			continue;
203 		}
204 		set_current_state(TASK_RUNNING);
205 		timeout = SPK_XMITR_TIMEOUT;
206 		while (synth_writable()) {
207 			if (!--timeout)
208 				break;
209 			udelay(1);
210 		}
211 		spin_lock_irqsave(&speakup_info.spinlock, flags);
212 		ch = synth_buffer_getc();
213 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
214 		if (ch == '\n')
215 			ch = PROCSPEECH;
216 		outb_p(ch, speakup_info.port_tts);
217 		if (time_after_eq(jiffies, jiff_max) && ch == SPACE) {
218 			timeout = SPK_XMITR_TIMEOUT;
219 			while (synth_writable()) {
220 				if (!--timeout)
221 					break;
222 				udelay(1);
223 			}
224 			outb_p(PROCSPEECH, speakup_info.port_tts);
225 			spin_lock_irqsave(&speakup_info.spinlock, flags);
226 			jiffy_delta_val = jiffy_delta->u.n.value;
227 			delay_time_val = delay_time->u.n.value;
228 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
229 			schedule_timeout(msecs_to_jiffies(delay_time_val));
230 			jiff_max = jiffies + jiffy_delta_val;
231 		}
232 	}
233 	timeout = SPK_XMITR_TIMEOUT;
234 	while (synth_writable()) {
235 		if (!--timeout)
236 			break;
237 		udelay(1);
238 	}
239 	outb_p(PROCSPEECH, speakup_info.port_tts);
240 }
241 
242 static void synth_flush(struct spk_synth *synth)
243 {
244 	outb_p(SYNTH_CLEAR, speakup_info.port_tts);
245 }
246 
247 static int synth_probe(struct spk_synth *synth)
248 {
249 	unsigned int port_val = 0;
250 	int i = 0;
251 
252 	pr_info("Probing for %s.\n", synth->long_name);
253 	if (port_forced) {
254 		speakup_info.port_tts = port_forced;
255 		pr_info("probe forced to %x by kernel command line\n",
256 			speakup_info.port_tts);
257 		if (synth_request_region(speakup_info.port_tts - 1,
258 					 SYNTH_IO_EXTENT)) {
259 			pr_warn("sorry, port already reserved\n");
260 			return -EBUSY;
261 		}
262 		port_val = inw(speakup_info.port_tts - 1);
263 		synth_port_control = speakup_info.port_tts - 1;
264 	} else {
265 		for (i = 0; synth_portlist[i]; i++) {
266 			if (synth_request_region(synth_portlist[i],
267 						 SYNTH_IO_EXTENT)) {
268 				pr_warn
269 				    ("request_region: failed with 0x%x, %d\n",
270 				     synth_portlist[i], SYNTH_IO_EXTENT);
271 				continue;
272 			}
273 			port_val = inw(synth_portlist[i]) & 0xfffc;
274 			if (port_val == 0x53fc) {
275 				/* 'S' and out&input bits */
276 				synth_port_control = synth_portlist[i];
277 				speakup_info.port_tts = synth_port_control + 1;
278 				break;
279 			}
280 		}
281 	}
282 	port_val &= 0xfffc;
283 	if (port_val != 0x53fc) {
284 		/* 'S' and out&input bits */
285 		pr_info("%s: not found\n", synth->long_name);
286 		synth_release_region(synth_port_control, SYNTH_IO_EXTENT);
287 		synth_port_control = 0;
288 		return -ENODEV;
289 	}
290 	pr_info("%s: %03x-%03x, driver version %s,\n", synth->long_name,
291 		synth_port_control, synth_port_control + SYNTH_IO_EXTENT - 1,
292 		synth->version);
293 	synth->alive = 1;
294 	return 0;
295 }
296 
297 static void accent_release(struct spk_synth *synth)
298 {
299 	spk_stop_serial_interrupt();
300 	if (speakup_info.port_tts)
301 		synth_release_region(speakup_info.port_tts - 1,
302 				     SYNTH_IO_EXTENT);
303 	speakup_info.port_tts = 0;
304 }
305 
306 module_param_hw_named(port, port_forced, int, ioport, 0444);
307 module_param_named(start, synth_acntpc.startup, short, 0444);
308 
309 MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
310 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
311 
312 module_spk_synth(synth_acntpc);
313 
314 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
315 MODULE_AUTHOR("David Borowski");
316 MODULE_DESCRIPTION("Speakup support for Accent PC synthesizer");
317 MODULE_LICENSE("GPL");
318 MODULE_VERSION(DRV_VERSION);
319 
320