Staging: add speakup to the staging directory
[deliverable/linux.git] / drivers / staging / speakup / speakup_keypc.c
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 synth_writable() (inb_p(synth_port + UART_RX) & 0x10)
37 #define synth_readable() (inb_p(synth_port + UART_RX) & 0x10)
38 #define synth_full() ((inb_p(synth_port + UART_RX) & 0x80) == 0)
39 #define PROCSPEECH 0x1f
40 #define SYNTH_CLEAR 0x03
41
42 static int synth_probe(struct spk_synth *synth);
43 static void keynote_release(void);
44 static const char *synth_immediate(struct spk_synth *synth, const char *buf);
45 static void do_catch_up(struct spk_synth *synth);
46 static void synth_flush(struct spk_synth *synth);
47
48 static int synth_port;
49 static int port_forced;
50 static unsigned int synth_portlist[] = { 0x2a8, 0 };
51
52 static struct var_t vars[] = {
53 { CAPS_START, .u.s = {"[f130]" }},
54 { CAPS_STOP, .u.s = {"[f90]" }},
55 { RATE, .u.n = {"\04%c ", 8, 0, 10, 81, -8, NULL }},
56 { PITCH, .u.n = {"[f%d]", 5, 0, 9, 40, 10, NULL }},
57 { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL }},
58 V_LAST_VAR
59 };
60
61 /*
62 * These attributes will appear in /sys/accessibility/speakup/keypc.
63 */
64 static struct kobj_attribute caps_start_attribute =
65 __ATTR(caps_start, USER_RW, spk_var_show, spk_var_store);
66 static struct kobj_attribute caps_stop_attribute =
67 __ATTR(caps_stop, USER_RW, spk_var_show, spk_var_store);
68 static struct kobj_attribute pitch_attribute =
69 __ATTR(pitch, USER_RW, spk_var_show, spk_var_store);
70 static struct kobj_attribute rate_attribute =
71 __ATTR(rate, USER_RW, spk_var_show, spk_var_store);
72
73 static struct kobj_attribute delay_time_attribute =
74 __ATTR(delay_time, ROOT_W, spk_var_show, spk_var_store);
75 static struct kobj_attribute direct_attribute =
76 __ATTR(direct, USER_RW, spk_var_show, spk_var_store);
77 static struct kobj_attribute full_time_attribute =
78 __ATTR(full_time, ROOT_W, spk_var_show, spk_var_store);
79 static struct kobj_attribute jiffy_delta_attribute =
80 __ATTR(jiffy_delta, ROOT_W, spk_var_show, spk_var_store);
81 static struct kobj_attribute trigger_time_attribute =
82 __ATTR(trigger_time, ROOT_W, spk_var_show, spk_var_store);
83
84 /*
85 * Create a group of attributes so that we can create and destroy them all
86 * at once.
87 */
88 static struct attribute *synth_attrs[] = {
89 &caps_start_attribute.attr,
90 &caps_stop_attribute.attr,
91 &pitch_attribute.attr,
92 &rate_attribute.attr,
93 &delay_time_attribute.attr,
94 &direct_attribute.attr,
95 &full_time_attribute.attr,
96 &jiffy_delta_attribute.attr,
97 &trigger_time_attribute.attr,
98 NULL, /* need to NULL terminate the list of attributes */
99 };
100
101 static struct spk_synth synth_keypc = {
102 .name = "keypc",
103 .version = DRV_VERSION,
104 .long_name = "Keynote PC",
105 .init = "[t][n7,1][n8,0]",
106 .procspeech = PROCSPEECH,
107 .clear = SYNTH_CLEAR,
108 .delay = 500,
109 .trigger = 50,
110 .jiffies = 50,
111 .full = 1000,
112 .startup = SYNTH_START,
113 .checkval = SYNTH_CHECK,
114 .vars = vars,
115 .probe = synth_probe,
116 .release = keynote_release,
117 .synth_immediate = synth_immediate,
118 .catch_up = do_catch_up,
119 .flush = synth_flush,
120 .is_alive = spk_synth_is_alive_nop,
121 .synth_adjust = NULL,
122 .read_buff_add = NULL,
123 .get_index = NULL,
124 .indexing = {
125 .command = NULL,
126 .lowindex = 0,
127 .highindex = 0,
128 .currindex = 0,
129 },
130 .attributes = {
131 .attrs = synth_attrs,
132 .name = "keypc",
133 },
134 };
135
136 static char *oops(void)
137 {
138 int s1, s2, s3, s4;
139 s1 = inb_p(synth_port);
140 s2 = inb_p(synth_port+1);
141 s3 = inb_p(synth_port+2);
142 s4 = inb_p(synth_port+3);
143 pr_warn("synth timeout %d %d %d %d\n", s1, s2, s3, s4);
144 return NULL;
145 }
146
147 static const char *synth_immediate(struct spk_synth *synth, const char *buf)
148 {
149 u_char ch;
150 int timeout;
151 while ((ch = *buf)) {
152 if (ch == '\n')
153 ch = PROCSPEECH;
154 if (synth_full())
155 return buf;
156 timeout = 1000;
157 while (synth_writable())
158 if (--timeout <= 0)
159 return oops();
160 outb_p(ch, synth_port);
161 udelay(70);
162 buf++;
163 }
164 return 0;
165 }
166
167 static void do_catch_up(struct spk_synth *synth)
168 {
169 u_char ch;
170 int timeout;
171 unsigned long flags;
172 unsigned long jiff_max;
173 struct var_t *jiffy_delta;
174 struct var_t *delay_time;
175 struct var_t *full_time;
176 int delay_time_val;
177 int full_time_val;
178 int jiffy_delta_val;
179
180 jiffy_delta = get_var(JIFFY);
181 delay_time = get_var(DELAY);
182 full_time = get_var(FULL);
183 spk_lock(flags);
184 jiffy_delta_val = jiffy_delta->u.n.value;
185 spk_unlock(flags);
186
187 jiff_max = jiffies + jiffy_delta_val;
188 while (!kthread_should_stop()) {
189 spk_lock(flags);
190 if (speakup_info.flushing) {
191 speakup_info.flushing = 0;
192 spk_unlock(flags);
193 synth->flush(synth);
194 continue;
195 }
196 if (synth_buffer_empty()) {
197 spk_unlock(flags);
198 break;
199 }
200 set_current_state(TASK_INTERRUPTIBLE);
201 full_time_val = full_time->u.n.value;
202 spk_unlock(flags);
203 if (synth_full()) {
204 schedule_timeout(msecs_to_jiffies(full_time_val));
205 continue;
206 }
207 set_current_state(TASK_RUNNING);
208 timeout = 1000;
209 while (synth_writable())
210 if (--timeout <= 0)
211 break;
212 if (timeout <= 0) {
213 oops();
214 break;
215 }
216 spk_lock(flags);
217 ch = synth_buffer_getc();
218 spk_unlock(flags);
219 if (ch == '\n')
220 ch = PROCSPEECH;
221 outb_p(ch, synth_port);
222 SWAIT;
223 if ((jiffies >= jiff_max) && (ch == SPACE)) {
224 timeout = 1000;
225 while (synth_writable())
226 if (--timeout <= 0)
227 break;
228 if (timeout <= 0) {
229 oops();
230 break;
231 }
232 outb_p(PROCSPEECH, synth_port);
233 spk_lock(flags);
234 jiffy_delta_val = jiffy_delta->u.n.value;
235 delay_time_val = delay_time->u.n.value;
236 spk_unlock(flags);
237 schedule_timeout(msecs_to_jiffies(delay_time_val));
238 jiff_max = jiffies+jiffy_delta_val;
239 }
240 }
241 timeout = 1000;
242 while (synth_writable())
243 if (--timeout <= 0)
244 break;
245 if (timeout <= 0)
246 oops();
247 else
248 outb_p(PROCSPEECH, synth_port);
249 }
250
251 static void synth_flush(struct spk_synth *synth)
252 {
253 outb_p(SYNTH_CLEAR, synth_port);
254 }
255
256 static int synth_probe(struct spk_synth *synth)
257 {
258 unsigned int port_val = 0;
259 int i = 0;
260 pr_info("Probing for %s.\n", synth->long_name);
261 if (port_forced) {
262 synth_port = port_forced;
263 pr_info("probe forced to %x by kernel command line\n",
264 synth_port);
265 if (synth_request_region(synth_port-1, SYNTH_IO_EXTENT)) {
266 pr_warn("sorry, port already reserved\n");
267 return -EBUSY;
268 }
269 port_val = inb(synth_port);
270 } else {
271 for (i = 0; synth_portlist[i]; i++) {
272 if (synth_request_region(synth_portlist[i],
273 SYNTH_IO_EXTENT)) {
274 pr_warn("request_region: failed with 0x%x, %d\n",
275 synth_portlist[i], SYNTH_IO_EXTENT);
276 continue;
277 }
278 port_val = inb(synth_portlist[i]);
279 if (port_val == 0x80) {
280 synth_port = synth_portlist[i];
281 break;
282 }
283 }
284 }
285 if (port_val != 0x80) {
286 pr_info("%s: not found\n", synth->long_name);
287 synth_release_region(synth_port, SYNTH_IO_EXTENT);
288 synth_port = 0;
289 return -ENODEV;
290 }
291 pr_info("%s: %03x-%03x, driver version %s,\n", synth->long_name,
292 synth_port, synth_port+SYNTH_IO_EXTENT-1,
293 synth->version);
294 synth->alive = 1;
295 return 0;
296 }
297
298 static void keynote_release(void)
299 {
300 if (synth_port)
301 synth_release_region(synth_port, SYNTH_IO_EXTENT);
302 synth_port = 0;
303 }
304
305 module_param_named(port, port_forced, int, S_IRUGO);
306 module_param_named(start, synth_keypc.startup, short, S_IRUGO);
307
308 MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
309 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
310
311 static int __init keypc_init(void)
312 {
313 return synth_add(&synth_keypc);
314 }
315
316 static void __exit keypc_exit(void)
317 {
318 synth_remove(&synth_keypc);
319 }
320
321 module_init(keypc_init);
322 module_exit(keypc_exit);
323 MODULE_AUTHOR("David Borowski");
324 MODULE_DESCRIPTION("Speakup support for Keynote Gold PC synthesizers");
325 MODULE_LICENSE("GPL");
326 MODULE_VERSION(DRV_VERSION);
327
This page took 0.052571 seconds and 5 git commands to generate.