1 /*
2  * written by David Borowski
3  *
4  * Copyright (C) 2003 David Borowski.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * specificly written as a driver for the speakup screenreview
21  * package it's not a general device driver.
22  * This driver is for the Keynote Gold internal synthesizer.
23  */
24 #include <linux/jiffies.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/kthread.h>
28 #include <linux/serial_reg.h>
29 
30 #include "spk_priv.h"
31 #include "speakup.h"
32 
33 #define DRV_VERSION "2.10"
34 #define SYNTH_IO_EXTENT	0x04
35 #define SWAIT udelay(70)
36 #define PROCSPEECH 0x1f
37 #define SYNTH_CLEAR 0x03
38 
39 static int synth_probe(struct spk_synth *synth);
40 static void keynote_release(void);
41 static const char *synth_immediate(struct spk_synth *synth, const char *buf);
42 static void do_catch_up(struct spk_synth *synth);
43 static void synth_flush(struct spk_synth *synth);
44 
45 static int synth_port;
46 static int port_forced;
47 static unsigned int synth_portlist[] = { 0x2a8, 0 };
48 
49 static struct var_t vars[] = {
50 	{ CAPS_START, .u.s = {"[f130]" } },
51 	{ CAPS_STOP, .u.s = {"[f90]" } },
52 	{ RATE, .u.n = {"\04%c ", 8, 0, 10, 81, -8, NULL } },
53 	{ PITCH, .u.n = {"[f%d]", 5, 0, 9, 40, 10, NULL } },
54 	{ DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
55 	V_LAST_VAR
56 };
57 
58 /*
59  * These attributes will appear in /sys/accessibility/speakup/keypc.
60  */
61 static struct kobj_attribute caps_start_attribute =
62 	__ATTR(caps_start, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
63 static struct kobj_attribute caps_stop_attribute =
64 	__ATTR(caps_stop, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
65 static struct kobj_attribute pitch_attribute =
66 	__ATTR(pitch, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
67 static struct kobj_attribute rate_attribute =
68 	__ATTR(rate, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
69 
70 static struct kobj_attribute delay_time_attribute =
71 	__ATTR(delay_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
72 static struct kobj_attribute direct_attribute =
73 	__ATTR(direct, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
74 static struct kobj_attribute full_time_attribute =
75 	__ATTR(full_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
76 static struct kobj_attribute jiffy_delta_attribute =
77 	__ATTR(jiffy_delta, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
78 static struct kobj_attribute trigger_time_attribute =
79 	__ATTR(trigger_time, S_IWUSR|S_IRUGO, spk_var_show, spk_var_store);
80 
81 /*
82  * Create a group of attributes so that we can create and destroy them all
83  * at once.
84  */
85 static struct attribute *synth_attrs[] = {
86 	&caps_start_attribute.attr,
87 	&caps_stop_attribute.attr,
88 	&pitch_attribute.attr,
89 	&rate_attribute.attr,
90 	&delay_time_attribute.attr,
91 	&direct_attribute.attr,
92 	&full_time_attribute.attr,
93 	&jiffy_delta_attribute.attr,
94 	&trigger_time_attribute.attr,
95 	NULL,	/* need to NULL terminate the list of attributes */
96 };
97 
98 static struct spk_synth synth_keypc = {
99 	.name = "keypc",
100 	.version = DRV_VERSION,
101 	.long_name = "Keynote PC",
102 	.init = "[t][n7,1][n8,0]",
103 	.procspeech = PROCSPEECH,
104 	.clear = SYNTH_CLEAR,
105 	.delay = 500,
106 	.trigger = 50,
107 	.jiffies = 50,
108 	.full = 1000,
109 	.startup = SYNTH_START,
110 	.checkval = SYNTH_CHECK,
111 	.vars = vars,
112 	.probe = synth_probe,
113 	.release = keynote_release,
114 	.synth_immediate = synth_immediate,
115 	.catch_up = do_catch_up,
116 	.flush = synth_flush,
117 	.is_alive = spk_synth_is_alive_nop,
118 	.synth_adjust = NULL,
119 	.read_buff_add = NULL,
120 	.get_index = NULL,
121 	.indexing = {
122 		.command = NULL,
123 		.lowindex = 0,
124 		.highindex = 0,
125 		.currindex = 0,
126 	},
127 	.attributes = {
128 		.attrs = synth_attrs,
129 		.name = "keypc",
130 	},
131 };
132 
synth_writable(void)133 static inline bool synth_writable(void)
134 {
135 	return (inb_p(synth_port + UART_RX) & 0x10) != 0;
136 }
137 
synth_full(void)138 static inline bool synth_full(void)
139 {
140 	return (inb_p(synth_port + UART_RX) & 0x80) == 0;
141 }
142 
oops(void)143 static char *oops(void)
144 {
145 	int s1, s2, s3, s4;
146 
147 	s1 = inb_p(synth_port);
148 	s2 = inb_p(synth_port+1);
149 	s3 = inb_p(synth_port+2);
150 	s4 = inb_p(synth_port+3);
151 	pr_warn("synth timeout %d %d %d %d\n", s1, s2, s3, s4);
152 	return NULL;
153 }
154 
synth_immediate(struct spk_synth * synth,const char * buf)155 static const char *synth_immediate(struct spk_synth *synth, const char *buf)
156 {
157 	u_char ch;
158 	int timeout;
159 
160 	while ((ch = *buf)) {
161 		if (ch == '\n')
162 			ch = PROCSPEECH;
163 		if (synth_full())
164 			return buf;
165 		timeout = 1000;
166 		while (synth_writable())
167 			if (--timeout <= 0)
168 				return oops();
169 		outb_p(ch, synth_port);
170 		udelay(70);
171 		buf++;
172 	}
173 	return NULL;
174 }
175 
do_catch_up(struct spk_synth * synth)176 static void do_catch_up(struct spk_synth *synth)
177 {
178 	u_char ch;
179 	int timeout;
180 	unsigned long flags;
181 	unsigned long jiff_max;
182 	struct var_t *jiffy_delta;
183 	struct var_t *delay_time;
184 	struct var_t *full_time;
185 	int delay_time_val;
186 	int full_time_val;
187 	int jiffy_delta_val;
188 
189 	jiffy_delta = spk_get_var(JIFFY);
190 	delay_time = spk_get_var(DELAY);
191 	full_time = spk_get_var(FULL);
192 spin_lock_irqsave(&speakup_info.spinlock, flags);
193 	jiffy_delta_val = jiffy_delta->u.n.value;
194 	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
195 
196 	jiff_max = jiffies + jiffy_delta_val;
197 	while (!kthread_should_stop()) {
198 		spin_lock_irqsave(&speakup_info.spinlock, flags);
199 		if (speakup_info.flushing) {
200 			speakup_info.flushing = 0;
201 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
202 			synth->flush(synth);
203 			continue;
204 		}
205 		if (synth_buffer_empty()) {
206 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
207 			break;
208 		}
209 		set_current_state(TASK_INTERRUPTIBLE);
210 		full_time_val = full_time->u.n.value;
211 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
212 		if (synth_full()) {
213 			schedule_timeout(msecs_to_jiffies(full_time_val));
214 			continue;
215 		}
216 		set_current_state(TASK_RUNNING);
217 		timeout = 1000;
218 		while (synth_writable())
219 			if (--timeout <= 0)
220 				break;
221 		if (timeout <= 0) {
222 			oops();
223 			break;
224 		}
225 		spin_lock_irqsave(&speakup_info.spinlock, flags);
226 		ch = synth_buffer_getc();
227 		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
228 		if (ch == '\n')
229 			ch = PROCSPEECH;
230 		outb_p(ch, synth_port);
231 		SWAIT;
232 		if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
233 			timeout = 1000;
234 			while (synth_writable())
235 				if (--timeout <= 0)
236 					break;
237 			if (timeout <= 0) {
238 				oops();
239 				break;
240 			}
241 			outb_p(PROCSPEECH, synth_port);
242 			spin_lock_irqsave(&speakup_info.spinlock, flags);
243 			jiffy_delta_val = jiffy_delta->u.n.value;
244 			delay_time_val = delay_time->u.n.value;
245 			spin_unlock_irqrestore(&speakup_info.spinlock, flags);
246 			schedule_timeout(msecs_to_jiffies(delay_time_val));
247 			jiff_max = jiffies+jiffy_delta_val;
248 		}
249 	}
250 	timeout = 1000;
251 	while (synth_writable())
252 		if (--timeout <= 0)
253 			break;
254 	if (timeout <= 0)
255 		oops();
256 	else
257 		outb_p(PROCSPEECH, synth_port);
258 }
259 
synth_flush(struct spk_synth * synth)260 static void synth_flush(struct spk_synth *synth)
261 {
262 	outb_p(SYNTH_CLEAR, synth_port);
263 }
264 
synth_probe(struct spk_synth * synth)265 static int synth_probe(struct spk_synth *synth)
266 {
267 	unsigned int port_val = 0;
268 	int i = 0;
269 
270 	pr_info("Probing for %s.\n", synth->long_name);
271 	if (port_forced) {
272 		synth_port = port_forced;
273 		pr_info("probe forced to %x by kernel command line\n",
274 				synth_port);
275 		if (synth_request_region(synth_port-1, SYNTH_IO_EXTENT)) {
276 			pr_warn("sorry, port already reserved\n");
277 			return -EBUSY;
278 		}
279 		port_val = inb(synth_port);
280 	} else {
281 		for (i = 0; synth_portlist[i]; i++) {
282 			if (synth_request_region(synth_portlist[i],
283 						SYNTH_IO_EXTENT)) {
284 				pr_warn
285 				    ("request_region: failed with 0x%x, %d\n",
286 				     synth_portlist[i], SYNTH_IO_EXTENT);
287 				continue;
288 			}
289 			port_val = inb(synth_portlist[i]);
290 			if (port_val == 0x80) {
291 				synth_port = synth_portlist[i];
292 				break;
293 			}
294 		}
295 	}
296 	if (port_val != 0x80) {
297 		pr_info("%s: not found\n", synth->long_name);
298 		synth_release_region(synth_port, SYNTH_IO_EXTENT);
299 		synth_port = 0;
300 		return -ENODEV;
301 	}
302 	pr_info("%s: %03x-%03x, driver version %s,\n", synth->long_name,
303 		synth_port, synth_port+SYNTH_IO_EXTENT-1,
304 		synth->version);
305 	synth->alive = 1;
306 	return 0;
307 }
308 
keynote_release(void)309 static void keynote_release(void)
310 {
311 	if (synth_port)
312 		synth_release_region(synth_port, SYNTH_IO_EXTENT);
313 	synth_port = 0;
314 }
315 
316 module_param_named(port, port_forced, int, S_IRUGO);
317 module_param_named(start, synth_keypc.startup, short, S_IRUGO);
318 
319 MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
320 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
321 
322 module_spk_synth(synth_keypc);
323 
324 MODULE_AUTHOR("David Borowski");
325 MODULE_DESCRIPTION("Speakup support for Keynote Gold PC synthesizers");
326 MODULE_LICENSE("GPL");
327 MODULE_VERSION(DRV_VERSION);
328 
329