xref: /linux/drivers/accessibility/speakup/speakup_decpc.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * This is the DECtalk PC speakup driver
4  *
5  * Some constants from DEC's DOS driver:
6  *      Copyright (c) by Digital Equipment Corp.
7  *
8  * 386BSD DECtalk PC driver:
9  *      Copyright (c) 1996 Brian Buhrow <buhrow@lothlorien.nfbcal.org>
10  *
11  * Linux DECtalk PC driver:
12  *      Copyright (c) 1997 Nicolas Pitre <nico@cam.org>
13  *
14  * speakup DECtalk PC Internal driver:
15  *      Copyright (c) 2003 David Borowski <david575@golden.net>
16  *
17  * All rights reserved.
18  */
19 #include <linux/jiffies.h>
20 #include <linux/sched.h>
21 #include <linux/timer.h>
22 #include <linux/kthread.h>
23 
24 #include "spk_priv.h"
25 #include "speakup.h"
26 
27 #define	MODULE_init		0x0dec	/* module in boot code */
28 #define	MODULE_self_test	0x8800	/* module in self-test */
29 #define	MODULE_reset		0xffff	/* reinit the whole module */
30 
31 #define	MODE_mask		0xf000	/* mode bits in high nibble */
32 #define	MODE_null		0x0000
33 #define	MODE_test		0x2000	/* in testing mode */
34 #define	MODE_status		0x8000
35 #define	STAT_int		0x0001	/* running in interrupt mode */
36 #define	STAT_tr_char		0x0002	/* character data to transmit */
37 #define	STAT_rr_char		0x0004	/* ready to receive char data */
38 #define	STAT_cmd_ready		0x0008	/* ready to accept commands */
39 #define	STAT_dma_ready		0x0010	/* dma command ready */
40 #define	STAT_digitized		0x0020	/* spc in digitized mode */
41 #define	STAT_new_index		0x0040	/* new last index ready */
42 #define	STAT_new_status		0x0080	/* new status posted */
43 #define	STAT_dma_state		0x0100	/* dma state toggle */
44 #define	STAT_index_valid	0x0200	/* indexs are valid */
45 #define	STAT_flushing		0x0400	/* flush in progress */
46 #define	STAT_self_test		0x0800	/* module in self test */
47 #define	MODE_ready		0xc000	/* module ready for next phase */
48 #define	READY_boot		0x0000
49 #define	READY_kernel		0x0001
50 #define	MODE_error		0xf000
51 
52 #define	CMD_mask		0xf000	/* mask for command nibble */
53 #define	CMD_null		0x0000	/* post status */
54 #define	CMD_control		0x1000	/* hard control command */
55 #define	CTRL_mask		0x0F00	/* mask off control nibble */
56 #define	CTRL_data		0x00FF	/* mask to get data byte */
57 #define	CTRL_null		0x0000	/* null control */
58 #define	CTRL_vol_up		0x0100	/* increase volume */
59 #define	CTRL_vol_down		0x0200	/* decrease volume */
60 #define	CTRL_vol_set		0x0300	/* set volume */
61 #define	CTRL_pause		0x0400	/* pause spc */
62 #define	CTRL_resume		0x0500	/* resume spc clock */
63 #define	CTRL_resume_spc		0x0001	/* resume spc soft pause */
64 #define	CTRL_flush		0x0600	/* flush all buffers */
65 #define	CTRL_int_enable		0x0700	/* enable status change ints */
66 #define	CTRL_buff_free		0x0800	/* buffer remain count */
67 #define	CTRL_buff_used		0x0900	/* buffer in use */
68 #define	CTRL_speech		0x0a00	/* immediate speech change */
69 #define	CTRL_SP_voice		0x0001	/* voice change */
70 #define	CTRL_SP_rate		0x0002	/* rate change */
71 #define	CTRL_SP_comma		0x0003	/* comma pause change */
72 #define	CTRL_SP_period		0x0004	/* period pause change */
73 #define	CTRL_SP_rate_delta	0x0005	/* delta rate change */
74 #define	CTRL_SP_get_param	0x0006	/* return the desired parameter */
75 #define	CTRL_last_index		0x0b00	/* get last index spoken */
76 #define	CTRL_io_priority	0x0c00	/* change i/o priority */
77 #define	CTRL_free_mem		0x0d00	/* get free paragraphs on module */
78 #define	CTRL_get_lang		0x0e00	/* return bitmask of loaded languages */
79 #define	CMD_test		0x2000	/* self-test request */
80 #define	TEST_mask		0x0F00	/* isolate test field */
81 #define	TEST_null		0x0000	/* no test requested */
82 #define	TEST_isa_int		0x0100	/* assert isa irq */
83 #define	TEST_echo		0x0200	/* make data in == data out */
84 #define	TEST_seg		0x0300	/* set peek/poke segment */
85 #define	TEST_off		0x0400	/* set peek/poke offset */
86 #define	TEST_peek		0x0500	/* data out == *peek */
87 #define	TEST_poke		0x0600	/* *peek == data in */
88 #define	TEST_sub_code		0x00FF	/* user defined test sub codes */
89 #define	CMD_id			0x3000	/* return software id */
90 #define	ID_null			0x0000	/* null id */
91 #define	ID_kernel		0x0100	/* kernel code executing */
92 #define	ID_boot			0x0200	/* boot code executing */
93 #define	CMD_dma			0x4000	/* force a dma start */
94 #define	CMD_reset		0x5000	/* reset module status */
95 #define	CMD_sync		0x6000	/* kernel sync command */
96 #define	CMD_char_in		0x7000	/* single character send */
97 #define	CMD_char_out		0x8000	/* single character get */
98 #define	CHAR_count_1		0x0100	/* one char in cmd_low */
99 #define	CHAR_count_2		0x0200	/* the second in data_low */
100 #define	CHAR_count_3		0x0300	/* the third in data_high */
101 #define	CMD_spc_mode		0x9000	/* change spc mode */
102 #define	CMD_spc_to_text		0x0100	/* set to text mode */
103 #define	CMD_spc_to_digit	0x0200	/* set to digital mode */
104 #define	CMD_spc_rate		0x0400	/* change spc data rate */
105 #define	CMD_error		0xf000	/* severe error */
106 
107 enum {	PRIMARY_DIC	= 0, USER_DIC, COMMAND_DIC, ABBREV_DIC };
108 
109 #define	DMA_single_in		0x01
110 #define	DMA_single_out		0x02
111 #define	DMA_buff_in		0x03
112 #define	DMA_buff_out		0x04
113 #define	DMA_control		0x05
114 #define	DT_MEM_ALLOC		0x03
115 #define	DT_SET_DIC		0x04
116 #define	DT_START_TASK		0x05
117 #define	DT_LOAD_MEM		0x06
118 #define	DT_READ_MEM		0x07
119 #define	DT_DIGITAL_IN		0x08
120 #define	DMA_sync		0x06
121 #define	DMA_sync_char		0x07
122 
123 #define DRV_VERSION "2.12"
124 #define PROCSPEECH 0x0b
125 #define SYNTH_IO_EXTENT 8
126 
127 static int synth_probe(struct spk_synth *synth);
128 static void dtpc_release(struct spk_synth *synth);
129 static const char *synth_immediate(struct spk_synth *synth, const char *buf);
130 static void do_catch_up(struct spk_synth *synth);
131 static void synth_flush(struct spk_synth *synth);
132 
133 static int synth_portlist[] = { 0x340, 0x350, 0x240, 0x250, 0 };
134 static int in_escape, is_flushing;
135 static int dt_stat, dma_state;
136 
137 
138 enum default_vars_id {
139 	CAPS_START_ID = 0, CAPS_STOP_ID,
140 	RATE_ID, PITCH_ID, INFLECTION_ID,
141 	VOL_ID, PUNCT_ID, VOICE_ID,
142 	DIRECT_ID, V_LAST_VAR_ID,
143 	NB_ID,
144 };
145 
146 
147 
148 static struct var_t vars[NB_ID] = {
149 	[CAPS_START_ID] = { CAPS_START, .u.s = {"[:dv ap 200]" } },
150 	[CAPS_STOP_ID] = { CAPS_STOP, .u.s = {"[:dv ap 100]" } },
151 	[RATE_ID] = { RATE, .u.n = {"[:ra %d]", 9, 0, 18, 150, 25, NULL } },
152 	[PITCH_ID] = { PITCH, .u.n = {"[:dv ap %d]", 80, 0, 100, 20, 0, NULL } },
153 	[INFLECTION_ID] = { INFLECTION, .u.n = {"[:dv pr %d] ", 100, 0, 10000, 0, 0, NULL } },
154 	[VOL_ID] = { VOL, .u.n = {"[:vo se %d]", 5, 0, 9, 5, 10, NULL } },
155 	[PUNCT_ID] = { PUNCT, .u.n = {"[:pu %c]", 0, 0, 2, 0, 0, "nsa" } },
156 	[VOICE_ID] = { VOICE, .u.n = {"[:n%c]", 0, 0, 9, 0, 0, "phfdburwkv" } },
157 	[DIRECT_ID] = { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
158 	V_LAST_VAR
159 };
160 
161 /*
162  * These attributes will appear in /sys/accessibility/speakup/decpc.
163  */
164 static struct kobj_attribute caps_start_attribute =
165 	__ATTR(caps_start, 0644, spk_var_show, spk_var_store);
166 static struct kobj_attribute caps_stop_attribute =
167 	__ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
168 static struct kobj_attribute pitch_attribute =
169 	__ATTR(pitch, 0644, spk_var_show, spk_var_store);
170 static struct kobj_attribute inflection_attribute =
171 	__ATTR(inflection, 0644, spk_var_show, spk_var_store);
172 static struct kobj_attribute punct_attribute =
173 	__ATTR(punct, 0644, spk_var_show, spk_var_store);
174 static struct kobj_attribute rate_attribute =
175 	__ATTR(rate, 0644, spk_var_show, spk_var_store);
176 static struct kobj_attribute voice_attribute =
177 	__ATTR(voice, 0644, spk_var_show, spk_var_store);
178 static struct kobj_attribute vol_attribute =
179 	__ATTR(vol, 0644, spk_var_show, spk_var_store);
180 
181 static struct kobj_attribute delay_time_attribute =
182 	__ATTR(delay_time, 0644, spk_var_show, spk_var_store);
183 static struct kobj_attribute direct_attribute =
184 	__ATTR(direct, 0644, spk_var_show, spk_var_store);
185 static struct kobj_attribute full_time_attribute =
186 	__ATTR(full_time, 0644, spk_var_show, spk_var_store);
187 static struct kobj_attribute jiffy_delta_attribute =
188 	__ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
189 static struct kobj_attribute trigger_time_attribute =
190 	__ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
191 
192 /*
193  * Create a group of attributes so that we can create and destroy them all
194  * at once.
195  */
196 static struct attribute *synth_attrs[] = {
197 	&caps_start_attribute.attr,
198 	&caps_stop_attribute.attr,
199 	&pitch_attribute.attr,
200 	&inflection_attribute.attr,
201 	&punct_attribute.attr,
202 	&rate_attribute.attr,
203 	&voice_attribute.attr,
204 	&vol_attribute.attr,
205 	&delay_time_attribute.attr,
206 	&direct_attribute.attr,
207 	&full_time_attribute.attr,
208 	&jiffy_delta_attribute.attr,
209 	&trigger_time_attribute.attr,
210 	NULL,	/* need to NULL terminate the list of attributes */
211 };
212 
213 static struct spk_synth synth_dec_pc = {
214 	.name = "decpc",
215 	.version = DRV_VERSION,
216 	.long_name = "Dectalk PC",
217 	.init = "[:pe -380]",
218 	.procspeech = PROCSPEECH,
219 	.delay = 500,
220 	.trigger = 50,
221 	.jiffies = 50,
222 	.full = 1000,
223 	.flags = SF_DEC,
224 	.startup = SYNTH_START,
225 	.checkval = SYNTH_CHECK,
226 	.vars = vars,
227 	.io_ops = &spk_serial_io_ops,
228 	.probe = synth_probe,
229 	.release = dtpc_release,
230 	.synth_immediate = synth_immediate,
231 	.catch_up = do_catch_up,
232 	.flush = synth_flush,
233 	.is_alive = spk_synth_is_alive_nop,
234 	.synth_adjust = NULL,
235 	.read_buff_add = NULL,
236 	.get_index = NULL,
237 	.indexing = {
238 		.command = NULL,
239 		.lowindex = 0,
240 		.highindex = 0,
241 		.currindex = 0,
242 	},
243 	.attributes = {
244 		.attrs = synth_attrs,
245 		.name = "decpc",
246 	},
247 };
248 
dt_getstatus(void)249 static int dt_getstatus(void)
250 {
251 	dt_stat = inb_p(speakup_info.port_tts) |
252 		 (inb_p(speakup_info.port_tts + 1) << 8);
253 	return dt_stat;
254 }
255 
dt_sendcmd(u_int cmd)256 static void dt_sendcmd(u_int cmd)
257 {
258 	outb_p(cmd & 0xFF, speakup_info.port_tts);
259 	outb_p((cmd >> 8) & 0xFF, speakup_info.port_tts + 1);
260 }
261 
dt_waitbit(int bit)262 static int dt_waitbit(int bit)
263 {
264 	int timeout = 100;
265 
266 	while (--timeout > 0) {
267 		if ((dt_getstatus() & bit) == bit)
268 			return 1;
269 		udelay(50);
270 	}
271 	return 0;
272 }
273 
dt_wait_dma(void)274 static int dt_wait_dma(void)
275 {
276 	int timeout = 100, state = dma_state;
277 
278 	if (!dt_waitbit(STAT_dma_ready))
279 		return 0;
280 	while (--timeout > 0) {
281 		if ((dt_getstatus() & STAT_dma_state) == state)
282 			return 1;
283 		udelay(50);
284 	}
285 	dma_state = dt_getstatus() & STAT_dma_state;
286 	return 1;
287 }
288 
dt_ctrl(u_int cmd)289 static int dt_ctrl(u_int cmd)
290 {
291 	int timeout = 10;
292 
293 	if (!dt_waitbit(STAT_cmd_ready))
294 		return -1;
295 	outb_p(0, speakup_info.port_tts + 2);
296 	outb_p(0, speakup_info.port_tts + 3);
297 	dt_getstatus();
298 	dt_sendcmd(CMD_control | cmd);
299 	outb_p(0, speakup_info.port_tts + 6);
300 	while (dt_getstatus() & STAT_cmd_ready) {
301 		udelay(20);
302 		if (--timeout == 0)
303 			break;
304 	}
305 	dt_sendcmd(CMD_null);
306 	return 0;
307 }
308 
synth_flush(struct spk_synth * synth)309 static void synth_flush(struct spk_synth *synth)
310 {
311 	int timeout = 10;
312 
313 	if (is_flushing)
314 		return;
315 	is_flushing = 4;
316 	in_escape = 0;
317 	while (dt_ctrl(CTRL_flush)) {
318 		if (--timeout == 0)
319 			break;
320 		udelay(50);
321 	}
322 	for (timeout = 0; timeout < 10; timeout++) {
323 		if (dt_waitbit(STAT_dma_ready))
324 			break;
325 		udelay(50);
326 	}
327 	outb_p(DMA_sync, speakup_info.port_tts + 4);
328 	outb_p(0, speakup_info.port_tts + 4);
329 	udelay(100);
330 	for (timeout = 0; timeout < 10; timeout++) {
331 		if (!(dt_getstatus() & STAT_flushing))
332 			break;
333 		udelay(50);
334 	}
335 	dma_state = dt_getstatus() & STAT_dma_state;
336 	dma_state ^= STAT_dma_state;
337 	is_flushing = 0;
338 }
339 
dt_sendchar(char ch)340 static int dt_sendchar(char ch)
341 {
342 	if (!dt_wait_dma())
343 		return -1;
344 	if (!(dt_stat & STAT_rr_char))
345 		return -2;
346 	outb_p(DMA_single_in, speakup_info.port_tts + 4);
347 	outb_p(ch, speakup_info.port_tts + 4);
348 	dma_state ^= STAT_dma_state;
349 	return 0;
350 }
351 
testkernel(void)352 static int testkernel(void)
353 {
354 	int status = 0;
355 
356 	if (dt_getstatus() == 0xffff) {
357 		status = -1;
358 		goto oops;
359 	}
360 	dt_sendcmd(CMD_sync);
361 	if (!dt_waitbit(STAT_cmd_ready))
362 		status = -2;
363 	else if (dt_stat & 0x8000)
364 		return 0;
365 	else if (dt_stat == 0x0dec)
366 		pr_warn("dec_pc at 0x%x, software not loaded\n",
367 			speakup_info.port_tts);
368 	status = -3;
369 oops:	synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
370 	speakup_info.port_tts = 0;
371 	return status;
372 }
373 
do_catch_up(struct spk_synth * synth)374 static void do_catch_up(struct spk_synth *synth)
375 {
376 	u_char ch;
377 	static u_char last;
378 	unsigned long flags;
379 	unsigned long jiff_max;
380 	struct var_t *jiffy_delta;
381 	struct var_t *delay_time;
382 	int jiffy_delta_val;
383 	int delay_time_val;
384 
385 	jiffy_delta = spk_get_var(JIFFY);
386 	delay_time = spk_get_var(DELAY);
387 	spin_lock_irqsave(&speakup_info.spinlock, flags);
388 	jiffy_delta_val = jiffy_delta->u.n.value;
389 	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
390 	jiff_max = jiffies + jiffy_delta_val;
391 
392 	while (!kthread_should_stop()) {
393 		spin_lock_irqsave(&speakup_info.spinlock, flags);
394 		if (speakup_info.flushing) {
395 			speakup_info.flushing = 0;
396 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
397 			synth->flush(synth);
398 			continue;
399 		}
400 		synth_buffer_skip_nonlatin1();
401 		if (synth_buffer_empty()) {
402 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
403 			break;
404 		}
405 		ch = synth_buffer_peek();
406 		set_current_state(TASK_INTERRUPTIBLE);
407 		delay_time_val = delay_time->u.n.value;
408 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
409 		if (ch == '\n')
410 			ch = 0x0D;
411 		if (dt_sendchar(ch)) {
412 			schedule_timeout(msecs_to_jiffies(delay_time_val));
413 			continue;
414 		}
415 		set_current_state(TASK_RUNNING);
416 		spin_lock_irqsave(&speakup_info.spinlock, flags);
417 		synth_buffer_getc();
418 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
419 		if (ch == '[') {
420 			in_escape = 1;
421 		} else if (ch == ']') {
422 			in_escape = 0;
423 		} else if (ch <= SPACE) {
424 			if (!in_escape && strchr(",.!?;:", last))
425 				dt_sendchar(PROCSPEECH);
426 			if (time_after_eq(jiffies, jiff_max)) {
427 				if (!in_escape)
428 					dt_sendchar(PROCSPEECH);
429 				spin_lock_irqsave(&speakup_info.spinlock,
430 						  flags);
431 				jiffy_delta_val = jiffy_delta->u.n.value;
432 				delay_time_val = delay_time->u.n.value;
433 				spin_unlock_irqrestore(&speakup_info.spinlock,
434 						       flags);
435 				schedule_timeout(msecs_to_jiffies
436 						 (delay_time_val));
437 				jiff_max = jiffies + jiffy_delta_val;
438 			}
439 		}
440 		last = ch;
441 		ch = 0;
442 	}
443 	if (!in_escape)
444 		dt_sendchar(PROCSPEECH);
445 }
446 
synth_immediate(struct spk_synth * synth,const char * buf)447 static const char *synth_immediate(struct spk_synth *synth, const char *buf)
448 {
449 	u_char ch;
450 
451 	while ((ch = *buf)) {
452 		if (ch == '\n')
453 			ch = PROCSPEECH;
454 		if (dt_sendchar(ch))
455 			return buf;
456 		buf++;
457 	}
458 	return NULL;
459 }
460 
synth_probe(struct spk_synth * synth)461 static int synth_probe(struct spk_synth *synth)
462 {
463 	int i = 0, failed = 0;
464 
465 	pr_info("Probing for %s.\n", synth->long_name);
466 	for (i = 0; synth_portlist[i]; i++) {
467 		if (synth_request_region(synth_portlist[i], SYNTH_IO_EXTENT)) {
468 			pr_warn("request_region: failed with 0x%x, %d\n",
469 				synth_portlist[i], SYNTH_IO_EXTENT);
470 			continue;
471 		}
472 		speakup_info.port_tts = synth_portlist[i];
473 		failed = testkernel();
474 		if (failed == 0)
475 			break;
476 	}
477 	if (failed) {
478 		pr_info("%s: not found\n", synth->long_name);
479 		return -ENODEV;
480 	}
481 	pr_info("%s: %03x-%03x, Driver Version %s,\n", synth->long_name,
482 		speakup_info.port_tts, speakup_info.port_tts + 7,
483 		synth->version);
484 	synth->alive = 1;
485 	return 0;
486 }
487 
dtpc_release(struct spk_synth * synth)488 static void dtpc_release(struct spk_synth *synth)
489 {
490 	spk_stop_serial_interrupt();
491 	if (speakup_info.port_tts)
492 		synth_release_region(speakup_info.port_tts, SYNTH_IO_EXTENT);
493 	speakup_info.port_tts = 0;
494 }
495 
496 module_param_named(start, synth_dec_pc.startup, short, 0444);
497 module_param_named(rate, vars[RATE_ID].u.n.default_val, int, 0444);
498 module_param_named(pitch, vars[PITCH_ID].u.n.default_val, int, 0444);
499 module_param_named(inflection, vars[INFLECTION_ID].u.n.default_val, int, 0444);
500 module_param_named(vol, vars[VOL_ID].u.n.default_val, int, 0444);
501 module_param_named(punct, vars[PUNCT_ID].u.n.default_val, int, 0444);
502 module_param_named(voice, vars[VOICE_ID].u.n.default_val, int, 0444);
503 module_param_named(direct, vars[DIRECT_ID].u.n.default_val, int, 0444);
504 
505 
506 
507 
508 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
509 MODULE_PARM_DESC(rate, "Set the rate variable on load.");
510 MODULE_PARM_DESC(pitch, "Set the pitch variable on load.");
511 MODULE_PARM_DESC(inflection, "Set the inflection variable on load.");
512 MODULE_PARM_DESC(vol, "Set the vol variable on load.");
513 MODULE_PARM_DESC(punct, "Set the punct variable on load.");
514 MODULE_PARM_DESC(voice, "Set the voice variable on load.");
515 MODULE_PARM_DESC(direct, "Set the direct variable on load.");
516 
517 module_spk_synth(synth_dec_pc);
518 
519 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
520 MODULE_AUTHOR("David Borowski");
521 MODULE_DESCRIPTION("Speakup support for DECtalk PC synthesizers");
522 MODULE_LICENSE("GPL");
523 MODULE_VERSION(DRV_VERSION);
524