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