GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / speakup / speakup_apollo.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  * this code is specificly written as a driver for the speakup screenreview
19  * package and is not a general device driver.
20  */
21 #include <linux/jiffies.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/kthread.h>
25 #include <linux/serial_reg.h>   /* for UART_MCR* constants */
26
27 #include "spk_priv.h"
28 #include "speakup.h"
29
30 #define DRV_VERSION "2.21"
31 #define SYNTH_CLEAR 0x18
32 #define PROCSPEECH '\r'
33
34 static void do_catch_up(struct spk_synth *synth);
35
36 static struct var_t vars[] = {
37         { CAPS_START, .u.s = {"cap, " } },
38         { CAPS_STOP, .u.s = {"" } },
39         { RATE, .u.n = {"@W%d", 6, 1, 9, 0, 0, NULL } },
40         { PITCH, .u.n = {"@F%x", 10, 0, 15, 0, 0, NULL } },
41         { VOL, .u.n = {"@A%x", 10, 0, 15, 0, 0, NULL } },
42         { VOICE, .u.n = {"@V%d", 1, 1, 6, 0, 0, NULL } },
43         { LANG, .u.n = {"@=%d,", 1, 1, 4, 0, 0, NULL } },
44         { DIRECT, .u.n = {NULL, 0, 0, 1, 0, 0, NULL } },
45         V_LAST_VAR
46 };
47
48 /*
49  * These attributes will appear in /sys/accessibility/speakup/apollo.
50  */
51 static struct kobj_attribute caps_start_attribute =
52         __ATTR(caps_start, 0644, spk_var_show, spk_var_store);
53 static struct kobj_attribute caps_stop_attribute =
54         __ATTR(caps_stop, 0644, spk_var_show, spk_var_store);
55 static struct kobj_attribute lang_attribute =
56         __ATTR(lang, 0644, spk_var_show, spk_var_store);
57 static struct kobj_attribute pitch_attribute =
58         __ATTR(pitch, 0644, spk_var_show, spk_var_store);
59 static struct kobj_attribute rate_attribute =
60         __ATTR(rate, 0644, spk_var_show, spk_var_store);
61 static struct kobj_attribute voice_attribute =
62         __ATTR(voice, 0644, spk_var_show, spk_var_store);
63 static struct kobj_attribute vol_attribute =
64         __ATTR(vol, 0644, spk_var_show, spk_var_store);
65
66 static struct kobj_attribute delay_time_attribute =
67         __ATTR(delay_time, 0644, spk_var_show, spk_var_store);
68 static struct kobj_attribute direct_attribute =
69         __ATTR(direct, 0644, spk_var_show, spk_var_store);
70 static struct kobj_attribute full_time_attribute =
71         __ATTR(full_time, 0644, spk_var_show, spk_var_store);
72 static struct kobj_attribute jiffy_delta_attribute =
73         __ATTR(jiffy_delta, 0644, spk_var_show, spk_var_store);
74 static struct kobj_attribute trigger_time_attribute =
75         __ATTR(trigger_time, 0644, spk_var_show, spk_var_store);
76
77 /*
78  * Create a group of attributes so that we can create and destroy them all
79  * at once.
80  */
81 static struct attribute *synth_attrs[] = {
82         &caps_start_attribute.attr,
83         &caps_stop_attribute.attr,
84         &lang_attribute.attr,
85         &pitch_attribute.attr,
86         &rate_attribute.attr,
87         &voice_attribute.attr,
88         &vol_attribute.attr,
89         &delay_time_attribute.attr,
90         &direct_attribute.attr,
91         &full_time_attribute.attr,
92         &jiffy_delta_attribute.attr,
93         &trigger_time_attribute.attr,
94         NULL,   /* need to NULL terminate the list of attributes */
95 };
96
97 static struct spk_synth synth_apollo = {
98         .name = "apollo",
99         .version = DRV_VERSION,
100         .long_name = "Apollo",
101         .init = "@R3@D0@K1\r",
102         .procspeech = PROCSPEECH,
103         .clear = SYNTH_CLEAR,
104         .delay = 500,
105         .trigger = 50,
106         .jiffies = 50,
107         .full = 40000,
108         .dev_name = SYNTH_DEFAULT_DEV,
109         .startup = SYNTH_START,
110         .checkval = SYNTH_CHECK,
111         .vars = vars,
112         .io_ops = &spk_ttyio_ops,
113         .probe = spk_ttyio_synth_probe,
114         .release = spk_ttyio_release,
115         .synth_immediate = spk_ttyio_synth_immediate,
116         .catch_up = do_catch_up,
117         .flush = spk_synth_flush,
118         .is_alive = spk_synth_is_alive_restart,
119         .synth_adjust = NULL,
120         .read_buff_add = NULL,
121         .get_index = NULL,
122         .indexing = {
123                 .command = NULL,
124                 .lowindex = 0,
125                 .highindex = 0,
126                 .currindex = 0,
127         },
128         .attributes = {
129                 .attrs = synth_attrs,
130                 .name = "apollo",
131         },
132 };
133
134 static void do_catch_up(struct spk_synth *synth)
135 {
136         u_char ch;
137         unsigned long flags;
138         unsigned long jiff_max;
139         struct var_t *jiffy_delta;
140         struct var_t *delay_time;
141         struct var_t *full_time;
142         int full_time_val = 0;
143         int delay_time_val = 0;
144         int jiffy_delta_val = 0;
145
146         jiffy_delta = spk_get_var(JIFFY);
147         delay_time = spk_get_var(DELAY);
148         full_time = spk_get_var(FULL);
149         spin_lock_irqsave(&speakup_info.spinlock, flags);
150         jiffy_delta_val = jiffy_delta->u.n.value;
151         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
152         jiff_max = jiffies + jiffy_delta_val;
153
154         while (!kthread_should_stop()) {
155                 spin_lock_irqsave(&speakup_info.spinlock, flags);
156                 jiffy_delta_val = jiffy_delta->u.n.value;
157                 full_time_val = full_time->u.n.value;
158                 delay_time_val = delay_time->u.n.value;
159                 if (speakup_info.flushing) {
160                         speakup_info.flushing = 0;
161                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
162                         synth->flush(synth);
163                         continue;
164                 }
165                 synth_buffer_skip_nonlatin1();
166                 if (synth_buffer_empty()) {
167                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
168                         break;
169                 }
170                 ch = synth_buffer_peek();
171                 set_current_state(TASK_INTERRUPTIBLE);
172                 full_time_val = full_time->u.n.value;
173                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
174                 if (!synth->io_ops->synth_out(synth, ch)) {
175                         synth->io_ops->tiocmset(0, UART_MCR_RTS);
176                         synth->io_ops->tiocmset(UART_MCR_RTS, 0);
177                         schedule_timeout(msecs_to_jiffies(full_time_val));
178                         continue;
179                 }
180                 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
181                         spin_lock_irqsave(&speakup_info.spinlock, flags);
182                         jiffy_delta_val = jiffy_delta->u.n.value;
183                         full_time_val = full_time->u.n.value;
184                         delay_time_val = delay_time->u.n.value;
185                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
186                         if (synth->io_ops->synth_out(synth, synth->procspeech))
187                                 schedule_timeout(msecs_to_jiffies
188                                                  (delay_time_val));
189                         else
190                                 schedule_timeout(msecs_to_jiffies
191                                                  (full_time_val));
192                         jiff_max = jiffies + jiffy_delta_val;
193                 }
194                 set_current_state(TASK_RUNNING);
195                 spin_lock_irqsave(&speakup_info.spinlock, flags);
196                 synth_buffer_getc();
197                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
198         }
199         synth->io_ops->synth_out(synth, PROCSPEECH);
200 }
201
202 module_param_named(ser, synth_apollo.ser, int, 0444);
203 module_param_named(dev, synth_apollo.dev_name, charp, S_IRUGO);
204 module_param_named(start, synth_apollo.startup, short, 0444);
205
206 MODULE_PARM_DESC(ser, "Set the serial port for the synthesizer (0-based).");
207 MODULE_PARM_DESC(dev, "Set the device e.g. ttyUSB0, for the synthesizer.");
208 MODULE_PARM_DESC(start, "Start the synthesizer once it is loaded.");
209
210 module_spk_synth(synth_apollo);
211
212 MODULE_AUTHOR("Kirk Reiser <kirk@braille.uwo.ca>");
213 MODULE_AUTHOR("David Borowski");
214 MODULE_DESCRIPTION("Speakup support for Apollo II synthesizer");
215 MODULE_LICENSE("GPL");
216 MODULE_VERSION(DRV_VERSION);
217