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