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