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