]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/speakup/synth.c
Merge tag 'powerpc-4.13-8' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[mirror_ubuntu-artful-kernel.git] / drivers / staging / speakup / synth.c
CommitLineData
c6e3fd22
WH
1#include <linux/types.h>
2#include <linux/ctype.h> /* for isdigit() and friends */
3#include <linux/fs.h>
4#include <linux/mm.h> /* for verify_area */
5#include <linux/errno.h> /* for -EBUSY */
6#include <linux/ioport.h> /* for check_region, request_region */
7#include <linux/interrupt.h>
8#include <linux/delay.h> /* for loops_per_sec */
9#include <linux/kmod.h>
10#include <linux/jiffies.h>
d2ad9a82 11#include <linux/uaccess.h> /* for copy_from_user */
c6e3fd22
WH
12#include <linux/sched.h>
13#include <linux/timer.h>
14#include <linux/kthread.h>
15
16#include "spk_priv.h"
17#include "speakup.h"
18#include "serialio.h"
19
20#define MAXSYNTHS 16 /* Max number of synths in array. */
99a9ffac 21static struct spk_synth *synths[MAXSYNTHS + 1];
1e560261 22struct spk_synth *synth;
ca2beaf8 23char spk_pitch_buff[32] = "";
c6e3fd22 24static int module_status;
ca2beaf8 25bool spk_quiet_boot;
c6e3fd22
WH
26
27struct speakup_info_t speakup_info = {
667b6141
WH
28 /*
29 * This spinlock is used to protect the entire speakup machinery, and
30 * must be taken at each kernel->speakup transition and released at
31 * each corresponding speakup->kernel transition.
32 *
05719ac7
PN
33 * The progression thread only interferes with the speakup machinery
34 * through the synth buffer, so only needs to take the lock
35 * while tinkering with the buffer.
667b6141
WH
36 *
37 * We use spin_lock/trylock_irqsave and spin_unlock_irqrestore with this
38 * spinlock because speakup needs to disable the keyboard IRQ.
39 */
1e560261 40 .spinlock = __SPIN_LOCK_UNLOCKED(speakup_info.spinlock),
c6e3fd22
WH
41 .flushing = 0,
42};
43EXPORT_SYMBOL_GPL(speakup_info);
44
45static int do_synth_init(struct spk_synth *in_synth);
46
d2ad9a82
WF
47/*
48 * Main loop of the progression thread: keep eating from the buffer
c6e3fd22
WH
49 * and push to the serial port, waiting as needed
50 *
83414d52 51 * For devices that have a "full" notification mechanism, the driver can
c6e3fd22
WH
52 * adapt the loop the way they prefer.
53 */
54void spk_do_catch_up(struct spk_synth *synth)
55{
56 u_char ch;
57 unsigned long flags;
58 unsigned long jiff_max;
59 struct var_t *delay_time;
60 struct var_t *full_time;
61 struct var_t *jiffy_delta;
62 int jiffy_delta_val;
63 int delay_time_val;
64 int full_time_val;
65
ca2beaf8
ST
66 jiffy_delta = spk_get_var(JIFFY);
67 full_time = spk_get_var(FULL);
68 delay_time = spk_get_var(DELAY);
c6e3fd22 69
6697330a 70 spin_lock_irqsave(&speakup_info.spinlock, flags);
c6e3fd22 71 jiffy_delta_val = jiffy_delta->u.n.value;
6697330a 72 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
c6e3fd22
WH
73
74 jiff_max = jiffies + jiffy_delta_val;
75 while (!kthread_should_stop()) {
6697330a 76 spin_lock_irqsave(&speakup_info.spinlock, flags);
c6e3fd22
WH
77 if (speakup_info.flushing) {
78 speakup_info.flushing = 0;
6697330a 79 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
c6e3fd22
WH
80 synth->flush(synth);
81 continue;
82 }
89fc2ae8 83 synth_buffer_skip_nonlatin1();
c6e3fd22 84 if (synth_buffer_empty()) {
6697330a 85 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
c6e3fd22
WH
86 break;
87 }
88 ch = synth_buffer_peek();
89 set_current_state(TASK_INTERRUPTIBLE);
90 full_time_val = full_time->u.n.value;
6697330a 91 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
c6e3fd22
WH
92 if (ch == '\n')
93 ch = synth->procspeech;
1e441594 94 if (!synth->io_ops->synth_out(synth, ch)) {
c6e3fd22
WH
95 schedule_timeout(msecs_to_jiffies(full_time_val));
96 continue;
97 }
89021ecc 98 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
6697330a 99 spin_lock_irqsave(&speakup_info.spinlock, flags);
c6e3fd22
WH
100 jiffy_delta_val = jiffy_delta->u.n.value;
101 delay_time_val = delay_time->u.n.value;
102 full_time_val = full_time->u.n.value;
6697330a 103 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
1e441594 104 if (synth->io_ops->synth_out(synth, synth->procspeech))
1e560261
WH
105 schedule_timeout(
106 msecs_to_jiffies(delay_time_val));
c6e3fd22 107 else
1e560261
WH
108 schedule_timeout(
109 msecs_to_jiffies(full_time_val));
c6e3fd22
WH
110 jiff_max = jiffies + jiffy_delta_val;
111 }
112 set_current_state(TASK_RUNNING);
6697330a 113 spin_lock_irqsave(&speakup_info.spinlock, flags);
c6e3fd22 114 synth_buffer_getc();
6697330a 115 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
c6e3fd22 116 }
1e441594 117 synth->io_ops->synth_out(synth, synth->procspeech);
c6e3fd22
WH
118}
119EXPORT_SYMBOL_GPL(spk_do_catch_up);
120
c6e3fd22
WH
121void spk_synth_flush(struct spk_synth *synth)
122{
1c597367 123 synth->io_ops->flush_buffer();
1e441594 124 synth->io_ops->synth_out(synth, synth->clear);
c6e3fd22
WH
125}
126EXPORT_SYMBOL_GPL(spk_synth_flush);
127
ca693dcd
OK
128unsigned char spk_synth_get_index(struct spk_synth *synth)
129{
130 return synth->io_ops->synth_in_nowait();
131}
132EXPORT_SYMBOL_GPL(spk_synth_get_index);
133
c6e3fd22
WH
134int spk_synth_is_alive_nop(struct spk_synth *synth)
135{
136 synth->alive = 1;
137 return 1;
138}
139EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
140
141int spk_synth_is_alive_restart(struct spk_synth *synth)
142{
143 if (synth->alive)
144 return 1;
9176d156 145 if (spk_wait_for_xmitr(synth) > 0) {
c6e3fd22
WH
146 /* restart */
147 synth->alive = 1;
148 synth_printf("%s", synth->init);
149 return 2; /* reenabled */
150 }
151 pr_warn("%s: can't restart synth\n", synth->long_name);
152 return 0;
153}
154EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
155
156static void thread_wake_up(u_long data)
157{
158 wake_up_interruptible_all(&speakup_event);
159}
160
161static DEFINE_TIMER(thread_timer, thread_wake_up, 0, 0);
162
163void synth_start(void)
164{
165 struct var_t *trigger_time;
166
167 if (!synth->alive) {
168 synth_buffer_clear();
169 return;
170 }
ca2beaf8 171 trigger_time = spk_get_var(TRIGGER);
c6e3fd22 172 if (!timer_pending(&thread_timer))
1e560261
WH
173 mod_timer(&thread_timer, jiffies +
174 msecs_to_jiffies(trigger_time->u.n.value));
c6e3fd22
WH
175}
176
ca2beaf8 177void spk_do_flush(void)
c6e3fd22 178{
8b9c012a
SL
179 if (!synth)
180 return;
181
c6e3fd22
WH
182 speakup_info.flushing = 1;
183 synth_buffer_clear();
184 if (synth->alive) {
ca2beaf8
ST
185 if (spk_pitch_shift) {
186 synth_printf("%s", spk_pitch_buff);
187 spk_pitch_shift = 0;
c6e3fd22
WH
188 }
189 }
190 wake_up_interruptible_all(&speakup_event);
191 wake_up_process(speakup_task);
192}
193
194void synth_write(const char *buf, size_t count)
195{
196 while (count--)
197 synth_buffer_add(*buf++);
198 synth_start();
199}
200
201void synth_printf(const char *fmt, ...)
202{
203 va_list args;
204 unsigned char buf[160], *p;
205 int r;
206
207 va_start(args, fmt);
208 r = vsnprintf(buf, sizeof(buf), fmt, args);
209 va_end(args);
210 if (r > sizeof(buf) - 1)
211 r = sizeof(buf) - 1;
212
213 p = buf;
214 while (r--)
215 synth_buffer_add(*p++);
216 synth_start();
217}
218EXPORT_SYMBOL_GPL(synth_printf);
219
89fc2ae8
ST
220void synth_putwc(u16 wc)
221{
222 synth_buffer_add(wc);
223}
224EXPORT_SYMBOL_GPL(synth_putwc);
225
226void synth_putwc_s(u16 wc)
227{
228 synth_buffer_add(wc);
229 synth_start();
230}
231EXPORT_SYMBOL_GPL(synth_putwc_s);
232
233void synth_putws(const u16 *buf)
234{
235 const u16 *p;
236
237 for (p = buf; *p; p++)
238 synth_buffer_add(*p);
239}
240EXPORT_SYMBOL_GPL(synth_putws);
241
242void synth_putws_s(const u16 *buf)
243{
244 synth_putws(buf);
245 synth_start();
246}
247EXPORT_SYMBOL_GPL(synth_putws_s);
248
1e560261
WH
249static int index_count;
250static int sentence_count;
c6e3fd22 251
ca2beaf8 252void spk_reset_index_count(int sc)
c6e3fd22
WH
253{
254 static int first = 1;
8e69a811 255
c6e3fd22
WH
256 if (first)
257 first = 0;
258 else
ca693dcd 259 synth->get_index(synth);
c6e3fd22
WH
260 index_count = 0;
261 sentence_count = sc;
262}
263
264int synth_supports_indexing(void)
265{
2ffb795f 266 if (synth->get_index)
c6e3fd22
WH
267 return 1;
268 return 0;
269}
270
271void synth_insert_next_index(int sent_num)
272{
273 int out;
8e69a811 274
c6e3fd22
WH
275 if (synth->alive) {
276 if (sent_num == 0) {
277 synth->indexing.currindex++;
278 index_count++;
279 if (synth->indexing.currindex >
280 synth->indexing.highindex)
281 synth->indexing.currindex =
282 synth->indexing.lowindex;
283 }
284
285 out = synth->indexing.currindex * 10 + sent_num;
286 synth_printf(synth->indexing.command, out, out);
287 }
288}
289
ca2beaf8 290void spk_get_index_count(int *linecount, int *sentcount)
c6e3fd22 291{
ca693dcd 292 int ind = synth->get_index(synth);
8e69a811 293
c6e3fd22
WH
294 if (ind) {
295 sentence_count = ind % 10;
296
297 if ((ind / 10) <= synth->indexing.currindex)
c67095ce 298 index_count = synth->indexing.currindex - (ind / 10);
c6e3fd22 299 else
1e560261 300 index_count = synth->indexing.currindex
c67095ce
WF
301 - synth->indexing.lowindex
302 + synth->indexing.highindex - (ind / 10) + 1;
c6e3fd22
WH
303 }
304 *sentcount = sentence_count;
305 *linecount = index_count;
306}
307
308static struct resource synth_res;
309
310int synth_request_region(unsigned long start, unsigned long n)
311{
312 struct resource *parent = &ioport_resource;
8e69a811 313
c6e3fd22
WH
314 memset(&synth_res, 0, sizeof(synth_res));
315 synth_res.name = synth->name;
316 synth_res.start = start;
317 synth_res.end = start + n - 1;
318 synth_res.flags = IORESOURCE_BUSY;
319 return request_resource(parent, &synth_res);
320}
321EXPORT_SYMBOL_GPL(synth_request_region);
322
323int synth_release_region(unsigned long start, unsigned long n)
324{
325 return release_resource(&synth_res);
326}
327EXPORT_SYMBOL_GPL(synth_release_region);
328
329struct var_t synth_time_vars[] = {
1e560261
WH
330 { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } },
331 { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } },
332 { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } },
333 { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } },
c6e3fd22
WH
334 V_LAST_VAR
335};
336
337/* called by: speakup_init() */
338int synth_init(char *synth_name)
339{
340 int i;
341 int ret = 0;
342 struct spk_synth *synth = NULL;
343
2ffb795f 344 if (!synth_name)
c6e3fd22
WH
345 return 0;
346
347 if (strcmp(synth_name, "none") == 0) {
348 mutex_lock(&spk_mutex);
349 synth_release();
350 mutex_unlock(&spk_mutex);
351 return 0;
352 }
353
354 mutex_lock(&spk_mutex);
355 /* First, check if we already have it loaded. */
2ffb795f 356 for (i = 0; i < MAXSYNTHS && synths[i]; i++)
c6e3fd22
WH
357 if (strcmp(synths[i]->name, synth_name) == 0)
358 synth = synths[i];
359
360 /* If we got one, initialize it now. */
361 if (synth)
362 ret = do_synth_init(synth);
363 else
364 ret = -ENODEV;
365 mutex_unlock(&spk_mutex);
366
367 return ret;
368}
369
370/* called by: synth_add() */
371static int do_synth_init(struct spk_synth *in_synth)
372{
373 struct var_t *var;
374
375 synth_release();
376 if (in_synth->checkval != SYNTH_CHECK)
377 return -EINVAL;
378 synth = in_synth;
379 synth->alive = 0;
380 pr_warn("synth probe\n");
381 if (synth->probe(synth) < 0) {
382 pr_warn("%s: device probe failed\n", in_synth->name);
383 synth = NULL;
384 return -ENODEV;
385 }
386 synth_time_vars[0].u.n.value =
387 synth_time_vars[0].u.n.default_val = synth->delay;
388 synth_time_vars[1].u.n.value =
389 synth_time_vars[1].u.n.default_val = synth->trigger;
390 synth_time_vars[2].u.n.value =
391 synth_time_vars[2].u.n.default_val = synth->jiffies;
392 synth_time_vars[3].u.n.value =
393 synth_time_vars[3].u.n.default_val = synth->full;
394 synth_printf("%s", synth->init);
1e560261
WH
395 for (var = synth->vars;
396 (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
c6e3fd22 397 speakup_register_var(var);
ca2beaf8 398 if (!spk_quiet_boot)
c6e3fd22 399 synth_printf("%s found\n", synth->long_name);
904cf12d
OH
400 if (synth->attributes.name &&
401 sysfs_create_group(speakup_kobj, &synth->attributes) < 0)
c6e3fd22
WH
402 return -ENOMEM;
403 synth_flags = synth->flags;
404 wake_up_interruptible_all(&speakup_event);
405 if (speakup_task)
406 wake_up_process(speakup_task);
407 return 0;
408}
409
410void synth_release(void)
411{
412 struct var_t *var;
413 unsigned long flags;
414
2ffb795f 415 if (!synth)
c6e3fd22 416 return;
6697330a 417 spin_lock_irqsave(&speakup_info.spinlock, flags);
c6e3fd22
WH
418 pr_info("releasing synth %s\n", synth->name);
419 synth->alive = 0;
420 del_timer(&thread_timer);
6697330a 421 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
c6e3fd22 422 if (synth->attributes.name)
3adc4aae 423 sysfs_remove_group(speakup_kobj, &synth->attributes);
c6e3fd22
WH
424 for (var = synth->vars; var->var_id != MAXVARS; var++)
425 speakup_unregister_var(var->var_id);
c6e3fd22
WH
426 synth->release();
427 synth = NULL;
428}
429
430/* called by: all_driver_init() */
431int synth_add(struct spk_synth *in_synth)
432{
433 int i;
434 int status = 0;
8e69a811 435
c6e3fd22 436 mutex_lock(&spk_mutex);
2ffb795f 437 for (i = 0; i < MAXSYNTHS && synths[i]; i++)
c6e3fd22
WH
438 /* synth_remove() is responsible for rotating the array down */
439 if (in_synth == synths[i]) {
440 mutex_unlock(&spk_mutex);
441 return 0;
442 }
443 if (i == MAXSYNTHS) {
444 pr_warn("Error: attempting to add a synth past end of array\n");
445 mutex_unlock(&spk_mutex);
446 return -1;
447 }
e4dd8bca 448
c6e3fd22
WH
449 if (in_synth->startup)
450 status = do_synth_init(in_synth);
e4dd8bca
OK
451
452 if (!status) {
453 synths[i++] = in_synth;
454 synths[i] = NULL;
455 }
456
c6e3fd22
WH
457 mutex_unlock(&spk_mutex);
458 return status;
459}
460EXPORT_SYMBOL_GPL(synth_add);
461
462void synth_remove(struct spk_synth *in_synth)
463{
464 int i;
8e69a811 465
c6e3fd22
WH
466 mutex_lock(&spk_mutex);
467 if (synth == in_synth)
468 synth_release();
2ffb795f 469 for (i = 0; synths[i]; i++) {
c6e3fd22
WH
470 if (in_synth == synths[i])
471 break;
472 }
2ffb795f 473 for ( ; synths[i]; i++) /* compress table */
c67095ce 474 synths[i] = synths[i + 1];
c6e3fd22
WH
475 module_status = 0;
476 mutex_unlock(&spk_mutex);
477}
478EXPORT_SYMBOL_GPL(synth_remove);
479
c67095ce 480short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };