]> Pileus Git - ~andy/linux/blob - drivers/staging/speakup/speakup_dtlk.c
Staging: add speakup to the staging directory
[~andy/linux] / drivers / staging / speakup / speakup_dtlk.c
1 /*
2  * originally written by: Kirk Reiser <kirk@braille.uwo.ca>
3 * this version considerably modified by David Borowski, david575@rogers.com
4  *
5  * Copyright (C) 1998-99  Kirk Reiser.
6  * Copyright (C) 2003 David Borowski.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * specificly written as a driver for the speakup screenreview
23  * package it's not a general device driver.
24  * This driver is for the RC Systems DoubleTalk PC internal synthesizer.
25  */
26 #include <linux/jiffies.h>
27 #include <linux/sched.h>
28 #include <linux/timer.h>
29 #include <linux/kthread.h>
30
31 #include "spk_priv.h"
32 #include "serialio.h"
33 #include "speakup_dtlk.h" /* local header file for DoubleTalk values */
34 #include "speakup.h"
35
36 #define DRV_VERSION "2.10"
37 #define PROCSPEECH 0x00
38 #define synth_readable() ((synth_status = inb_p(speakup_info.port_tts + UART_RX)) & TTS_READABLE)
39 #define synth_writable() ((synth_status = inb_p(speakup_info.port_tts + UART_RX)) & TTS_WRITABLE)
40 #define synth_full() ((synth_status = inb_p(speakup_info.port_tts + UART_RX)) & TTS_ALMOST_FULL)
41
42 static int synth_probe(struct spk_synth *synth);
43 static void dtlk_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_lpc;
49 static int port_forced;
50 static unsigned int synth_portlist[] =
51                 { 0x25e, 0x29e, 0x2de, 0x31e, 0x35e, 0x39e, 0 };
52 static u_char synth_status;
53
54 static struct var_t vars[] = {
55         { CAPS_START, .u.s = {"\x01+35p" }},
56         { CAPS_STOP, .u.s = {"\x01-35p" }},
57         { RATE, .u.n = {"\x01%ds", 8, 0, 9, 0, 0, NULL }},
58         { PITCH, .u.n = {"\x01%dp", 50, 0, 99, 0, 0, NULL }},
59         { VOL, .u.n = {"\x01%dv", 5, 0, 9, 0, 0, NULL }},
60         { TONE, .u.n = {"\x01%dx", 1, 0, 2, 0, 0, NULL }},
61         { PUNCT, .u.n = {"\x01%db", 7, 0, 15, 0, 0, NULL }},
62         { VOICE, .u.n = {"\x01%do", 0, 0, 7, 0, 0, NULL }},
63         { FREQUENCY, .u.n = {"\x01%df", 5, 0, 9, 0, 0, NULL }},
64         { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL }},
65         V_LAST_VAR
66 };
67
68 /*
69  * These attributes will appear in /sys/accessibility/speakup/dtlk.
70  */
71 static struct kobj_attribute caps_start_attribute =
72         __ATTR(caps_start, USER_RW, spk_var_show, spk_var_store);
73 static struct kobj_attribute caps_stop_attribute =
74         __ATTR(caps_stop, USER_RW, spk_var_show, spk_var_store);
75 static struct kobj_attribute freq_attribute =
76         __ATTR(freq, USER_RW, spk_var_show, spk_var_store);
77 static struct kobj_attribute pitch_attribute =
78         __ATTR(pitch, USER_RW, spk_var_show, spk_var_store);
79 static struct kobj_attribute punct_attribute =
80         __ATTR(punct, USER_RW, spk_var_show, spk_var_store);
81 static struct kobj_attribute rate_attribute =
82         __ATTR(rate, USER_RW, spk_var_show, spk_var_store);
83 static struct kobj_attribute tone_attribute =
84         __ATTR(tone, USER_RW, spk_var_show, spk_var_store);
85 static struct kobj_attribute voice_attribute =
86         __ATTR(voice, USER_RW, spk_var_show, spk_var_store);
87 static struct kobj_attribute vol_attribute =
88         __ATTR(vol, USER_RW, spk_var_show, spk_var_store);
89
90 static struct kobj_attribute delay_time_attribute =
91         __ATTR(delay_time, ROOT_W, spk_var_show, spk_var_store);
92 static struct kobj_attribute direct_attribute =
93         __ATTR(direct, USER_RW, spk_var_show, spk_var_store);
94 static struct kobj_attribute full_time_attribute =
95         __ATTR(full_time, ROOT_W, spk_var_show, spk_var_store);
96 static struct kobj_attribute jiffy_delta_attribute =
97         __ATTR(jiffy_delta, ROOT_W, spk_var_show, spk_var_store);
98 static struct kobj_attribute trigger_time_attribute =
99         __ATTR(trigger_time, ROOT_W, spk_var_show, spk_var_store);
100
101 /*
102  * Create a group of attributes so that we can create and destroy them all
103  * at once.
104  */
105 static struct attribute *synth_attrs[] = {
106         &caps_start_attribute.attr,
107         &caps_stop_attribute.attr,
108         &freq_attribute.attr,
109         &pitch_attribute.attr,
110         &punct_attribute.attr,
111         &rate_attribute.attr,
112         &tone_attribute.attr,
113         &voice_attribute.attr,
114         &vol_attribute.attr,
115         &delay_time_attribute.attr,
116         &direct_attribute.attr,
117         &full_time_attribute.attr,
118         &jiffy_delta_attribute.attr,
119         &trigger_time_attribute.attr,
120         NULL,   /* need to NULL terminate the list of attributes */
121 };
122
123 static struct spk_synth synth_dtlk = {
124         .name = "dtlk",
125         .version = DRV_VERSION,
126         .long_name = "DoubleTalk PC",
127         .init = "\x01@\x01\x31y",
128         .procspeech = PROCSPEECH,
129         .clear = SYNTH_CLEAR,
130         .delay = 500,
131         .trigger = 30,
132         .jiffies = 50,
133         .full = 1000,
134         .startup = SYNTH_START,
135         .checkval = SYNTH_CHECK,
136         .vars = vars,
137         .probe = synth_probe,
138         .release = dtlk_release,
139         .synth_immediate = synth_immediate,
140         .catch_up = do_catch_up,
141         .flush = synth_flush,
142         .is_alive = spk_synth_is_alive_nop,
143         .synth_adjust = NULL,
144         .read_buff_add = NULL,
145         .get_index = spk_serial_in_nowait,
146         .indexing = {
147                 .command = "\x01%di",
148                 .lowindex = 1,
149                 .highindex = 5,
150                 .currindex = 1,
151         },
152         .attributes = {
153                 .attrs = synth_attrs,
154                 .name = "dtlk",
155         },
156 };
157
158 static void spk_out(const char ch)
159 {
160         int timeout = SPK_XMITR_TIMEOUT;
161         while (synth_writable() == 0) {
162                 if (!--timeout)
163                         break;
164                 udelay(1);
165         }
166         outb_p(ch, speakup_info.port_tts);
167         timeout = SPK_XMITR_TIMEOUT;
168         while (synth_writable() != 0) {
169                 if (!--timeout)
170                         break;
171                 udelay(1);
172         }
173 }
174
175 static void do_catch_up(struct spk_synth *synth)
176 {
177         u_char ch;
178         unsigned long flags;
179         unsigned long jiff_max;
180         struct var_t *jiffy_delta;
181         struct var_t *delay_time;
182         int jiffy_delta_val;
183         int delay_time_val;
184
185         jiffy_delta = get_var(JIFFY);
186         delay_time = get_var(DELAY);
187         spk_lock(flags);
188         jiffy_delta_val = jiffy_delta->u.n.value;
189         spk_unlock(flags);
190         jiff_max = jiffies + jiffy_delta_val;
191         while (!kthread_should_stop()) {
192                 spk_lock(flags);
193                 if (speakup_info.flushing) {
194                         speakup_info.flushing = 0;
195                         spk_unlock(flags);
196                         synth->flush(synth);
197                         continue;
198                 }
199                 if (synth_buffer_empty()) {
200                         spk_unlock(flags);
201                         break;
202                 }
203                 set_current_state(TASK_INTERRUPTIBLE);
204                 delay_time_val = delay_time->u.n.value;
205                 spk_unlock(flags);
206                 if (synth_full()) {
207                         schedule_timeout(msecs_to_jiffies(delay_time_val));
208                         continue;
209                 }
210                 set_current_state(TASK_RUNNING);
211                 spk_lock(flags);
212                 ch = synth_buffer_getc();
213                 spk_unlock(flags);
214                 if (ch == '\n')
215                         ch = PROCSPEECH;
216                 spk_out(ch);
217                 if ((jiffies >= jiff_max) && (ch == SPACE)) {
218                         spk_out(PROCSPEECH);
219                         spk_lock(flags);
220                         delay_time_val = delay_time->u.n.value;
221                         jiffy_delta_val = jiffy_delta->u.n.value;
222                         spk_unlock(flags);
223                         schedule_timeout(msecs_to_jiffies(delay_time_val));
224                         jiff_max = jiffies + jiffy_delta_val;
225                 }
226         }
227         spk_out(PROCSPEECH);
228 }
229
230 static const char *synth_immediate(struct spk_synth *synth, const char *buf)
231 {
232         u_char ch;
233         while ((ch = (u_char)*buf)) {
234                 if (synth_full())
235                         return buf;
236                 if (ch == '\n')
237                         ch = PROCSPEECH;
238                 spk_out(ch);
239                 buf++;
240         }
241         return 0;
242 }
243
244 static void synth_flush(struct spk_synth *synth)
245 {
246         outb_p(SYNTH_CLEAR, speakup_info.port_tts);
247         while (synth_writable() != 0)
248                 cpu_relax();
249 }
250
251 static char synth_read_tts(void)
252 {
253         u_char ch;
254         while (synth_readable() == 0)
255                 cpu_relax();
256         ch = synth_status & 0x7f;
257         outb_p(ch, speakup_info.port_tts);
258         while (synth_readable() != 0)
259                 cpu_relax();
260         return (char) ch;
261 }
262
263 /* interrogate the DoubleTalk PC and return its settings */
264 static struct synth_settings *synth_interrogate(struct spk_synth *synth)
265 {
266         u_char *t;
267         static char buf[sizeof(struct synth_settings) + 1];
268         int total, i;
269         static struct synth_settings status;
270         synth_immediate(synth, "\x18\x01?");
271         for (total = 0, i = 0; i < 50; i++) {
272                 buf[total] = synth_read_tts();
273                 if (total > 2 && buf[total] == 0x7f)
274                         break;
275                 if (total < sizeof(struct synth_settings))
276                         total++;
277         }
278         t = buf;
279         /* serial number is little endian */
280         status.serial_number = t[0] + t[1]*256;
281         t += 2;
282         for (i = 0; *t != '\r'; t++) {
283                 status.rom_version[i] = *t;
284                 if (i < sizeof(status.rom_version)-1)
285                         i++;
286         }
287         status.rom_version[i] = 0;
288         t++;
289         status.mode = *t++;
290         status.punc_level = *t++;
291         status.formant_freq = *t++;
292         status.pitch = *t++;
293         status.speed = *t++;
294         status.volume = *t++;
295         status.tone = *t++;
296         status.expression = *t++;
297         status.ext_dict_loaded = *t++;
298         status.ext_dict_status = *t++;
299         status.free_ram = *t++;
300         status.articulation = *t++;
301         status.reverb = *t++;
302         status.eob = *t++;
303         return &status;
304 }
305
306 static int synth_probe(struct spk_synth *synth)
307 {
308                 unsigned int port_val = 0;
309         int i = 0;
310         struct synth_settings *sp;
311         pr_info("Probing for DoubleTalk.\n");
312         if (port_forced) {
313                 speakup_info.port_tts = port_forced;
314                 pr_info("probe forced to %x by kernel command line\n",
315                                 speakup_info.port_tts);
316                 if ((port_forced & 0xf) != 0xf)
317                         pr_info("warning: port base should probably end with f\n");
318                 if (synth_request_region(speakup_info.port_tts-1,
319                                         SYNTH_IO_EXTENT)) {
320                         pr_warn("sorry, port already reserved\n");
321                         return -EBUSY;
322                 }
323                 port_val = inw(speakup_info.port_tts-1);
324                 synth_lpc = speakup_info.port_tts-1;
325         } else {
326                 for (i = 0; synth_portlist[i]; i++) {
327                         if (synth_request_region(synth_portlist[i],
328                                                 SYNTH_IO_EXTENT))
329                                 continue;
330                         port_val = inw(synth_portlist[i]) & 0xfbff;
331                         if (port_val == 0x107f) {
332                                 synth_lpc = synth_portlist[i];
333                                 speakup_info.port_tts = synth_lpc+1;
334                                 break;
335                         }
336                         synth_release_region(synth_portlist[i],
337                                         SYNTH_IO_EXTENT);
338                 }
339         }
340         port_val &= 0xfbff;
341         if (port_val != 0x107f) {
342                 pr_info("DoubleTalk PC: not found\n");
343                 synth_release_region(synth_lpc, SYNTH_IO_EXTENT);
344                 return -ENODEV;
345         }
346         while (inw_p(synth_lpc) != 0x147f)
347                 cpu_relax(); /* wait until it's ready */
348         sp = synth_interrogate(synth);
349         pr_info("%s: %03x-%03x, ROM ver %s, s/n %u, driver: %s\n",
350                 synth->long_name, synth_lpc, synth_lpc+SYNTH_IO_EXTENT - 1,
351          sp->rom_version, sp->serial_number, synth->version);
352         synth->alive = 1;
353         return 0;
354 }
355
356 static void dtlk_release(void)
357 {
358         if (speakup_info.port_tts)
359                 synth_release_region(speakup_info.port_tts-1, SYNTH_IO_EXTENT);
360         speakup_info.port_tts = 0;
361 }
362
363 module_param_named(port, port_forced, int, S_IRUGO);
364 module_param_named(start, synth_dtlk.startup, short, S_IRUGO);
365
366 MODULE_PARM_DESC(port, "Set the port for the synthesizer (override probing).");
367 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
368
369 static int __init dtlk_init(void)
370 {
371         return synth_add(&synth_dtlk);
372 }
373
374 static void __exit dtlk_exit(void)
375 {
376         synth_remove(&synth_dtlk);
377 }
378
379 module_init(dtlk_init);
380 module_exit(dtlk_exit);
381 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
382 MODULE_AUTHOR("David Borowski");
383 MODULE_DESCRIPTION("Speakup support for DoubleTalk PC synthesizers");
384 MODULE_LICENSE("GPL");
385 MODULE_VERSION(DRV_VERSION);
386