blob: 01eddab93c66718291e087c28e120cf66a375258 [file] [log] [blame]
William Hubbsc6e3fd22010-10-07 13:20:02 -05001#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>
11#include <linux/uaccess.h> /* for copy_from_user */
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. */
21static struct spk_synth *synths[MAXSYNTHS];
William Hubbs1e560262010-10-15 22:13:37 -050022struct spk_synth *synth;
Samuel Thibaultca2beaf2013-01-02 02:37:40 +010023char spk_pitch_buff[32] = "";
William Hubbsc6e3fd22010-10-07 13:20:02 -050024static int module_status;
Samuel Thibaultca2beaf2013-01-02 02:37:40 +010025bool spk_quiet_boot;
William Hubbsc6e3fd22010-10-07 13:20:02 -050026
27struct speakup_info_t speakup_info = {
William Hubbs667b6142013-05-13 13:31:40 -050028 /*
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 *
Panir.Nyan05719ac2014-12-24 04:07:31 +000033 * 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.
William Hubbs667b6142013-05-13 13:31:40 -050036 *
37 * We use spin_lock/trylock_irqsave and spin_unlock_irqrestore with this
38 * spinlock because speakup needs to disable the keyboard IRQ.
39 */
William Hubbs1e560262010-10-15 22:13:37 -050040 .spinlock = __SPIN_LOCK_UNLOCKED(speakup_info.spinlock),
William Hubbsc6e3fd22010-10-07 13:20:02 -050041 .flushing = 0,
42};
43EXPORT_SYMBOL_GPL(speakup_info);
44
45static int do_synth_init(struct spk_synth *in_synth);
46
Samuel Thibaultca2beaf2013-01-02 02:37:40 +010047int spk_serial_synth_probe(struct spk_synth *synth)
William Hubbsc6e3fd22010-10-07 13:20:02 -050048{
Jiri Slaby3ee00172012-03-05 14:52:11 +010049 const struct old_serial_port *ser;
William Hubbsc6e3fd22010-10-07 13:20:02 -050050 int failed = 0;
51
52 if ((synth->ser >= SPK_LO_TTY) && (synth->ser <= SPK_HI_TTY)) {
53 ser = spk_serial_init(synth->ser);
54 if (ser == NULL) {
55 failed = -1;
56 } else {
57 outb_p(0, ser->port);
58 mdelay(1);
59 outb_p('\r', ser->port);
60 }
61 } else {
62 failed = -1;
63 pr_warn("ttyS%i is an invalid port\n", synth->ser);
64 }
65 if (failed) {
66 pr_info("%s: not found\n", synth->long_name);
67 return -ENODEV;
68 }
69 pr_info("%s: ttyS%i, Driver Version %s\n",
70 synth->long_name, synth->ser, synth->version);
71 synth->alive = 1;
72 return 0;
73}
Samuel Thibaultca2beaf2013-01-02 02:37:40 +010074EXPORT_SYMBOL_GPL(spk_serial_synth_probe);
William Hubbsc6e3fd22010-10-07 13:20:02 -050075
76/* Main loop of the progression thread: keep eating from the buffer
77 * and push to the serial port, waiting as needed
78 *
Justin P. Mattock83414d52012-07-09 07:30:27 -070079 * For devices that have a "full" notification mechanism, the driver can
William Hubbsc6e3fd22010-10-07 13:20:02 -050080 * adapt the loop the way they prefer.
81 */
82void spk_do_catch_up(struct spk_synth *synth)
83{
84 u_char ch;
85 unsigned long flags;
86 unsigned long jiff_max;
87 struct var_t *delay_time;
88 struct var_t *full_time;
89 struct var_t *jiffy_delta;
90 int jiffy_delta_val;
91 int delay_time_val;
92 int full_time_val;
93
Samuel Thibaultca2beaf2013-01-02 02:37:40 +010094 jiffy_delta = spk_get_var(JIFFY);
95 full_time = spk_get_var(FULL);
96 delay_time = spk_get_var(DELAY);
William Hubbsc6e3fd22010-10-07 13:20:02 -050097
William Hubbs66973302013-05-13 00:03:08 -050098 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -050099 jiffy_delta_val = jiffy_delta->u.n.value;
William Hubbs66973302013-05-13 00:03:08 -0500100 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500101
102 jiff_max = jiffies + jiffy_delta_val;
103 while (!kthread_should_stop()) {
William Hubbs66973302013-05-13 00:03:08 -0500104 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500105 if (speakup_info.flushing) {
106 speakup_info.flushing = 0;
William Hubbs66973302013-05-13 00:03:08 -0500107 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500108 synth->flush(synth);
109 continue;
110 }
111 if (synth_buffer_empty()) {
William Hubbs66973302013-05-13 00:03:08 -0500112 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500113 break;
114 }
115 ch = synth_buffer_peek();
116 set_current_state(TASK_INTERRUPTIBLE);
117 full_time_val = full_time->u.n.value;
William Hubbs66973302013-05-13 00:03:08 -0500118 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500119 if (ch == '\n')
120 ch = synth->procspeech;
121 if (!spk_serial_out(ch)) {
122 schedule_timeout(msecs_to_jiffies(full_time_val));
123 continue;
124 }
Tapasweni Pathak89021ec2014-09-21 19:22:51 +0530125 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
William Hubbs66973302013-05-13 00:03:08 -0500126 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500127 jiffy_delta_val = jiffy_delta->u.n.value;
128 delay_time_val = delay_time->u.n.value;
129 full_time_val = full_time->u.n.value;
William Hubbs66973302013-05-13 00:03:08 -0500130 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500131 if (spk_serial_out(synth->procspeech))
William Hubbs1e560262010-10-15 22:13:37 -0500132 schedule_timeout(
133 msecs_to_jiffies(delay_time_val));
William Hubbsc6e3fd22010-10-07 13:20:02 -0500134 else
William Hubbs1e560262010-10-15 22:13:37 -0500135 schedule_timeout(
136 msecs_to_jiffies(full_time_val));
William Hubbsc6e3fd22010-10-07 13:20:02 -0500137 jiff_max = jiffies + jiffy_delta_val;
138 }
139 set_current_state(TASK_RUNNING);
William Hubbs66973302013-05-13 00:03:08 -0500140 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500141 synth_buffer_getc();
William Hubbs66973302013-05-13 00:03:08 -0500142 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500143 }
144 spk_serial_out(synth->procspeech);
145}
146EXPORT_SYMBOL_GPL(spk_do_catch_up);
147
148const char *spk_synth_immediate(struct spk_synth *synth, const char *buff)
149{
150 u_char ch;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200151
William Hubbsc6e3fd22010-10-07 13:20:02 -0500152 while ((ch = *buff)) {
153 if (ch == '\n')
154 ch = synth->procspeech;
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100155 if (spk_wait_for_xmitr())
William Hubbsc6e3fd22010-10-07 13:20:02 -0500156 outb(ch, speakup_info.port_tts);
157 else
158 return buff;
159 buff++;
160 }
Sachin Kamat4e595c02013-05-13 16:08:55 +0530161 return NULL;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500162}
163EXPORT_SYMBOL_GPL(spk_synth_immediate);
164
165void spk_synth_flush(struct spk_synth *synth)
166{
167 spk_serial_out(synth->clear);
168}
169EXPORT_SYMBOL_GPL(spk_synth_flush);
170
171int spk_synth_is_alive_nop(struct spk_synth *synth)
172{
173 synth->alive = 1;
174 return 1;
175}
176EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
177
178int spk_synth_is_alive_restart(struct spk_synth *synth)
179{
180 if (synth->alive)
181 return 1;
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100182 if (!synth->alive && spk_wait_for_xmitr() > 0) {
William Hubbsc6e3fd22010-10-07 13:20:02 -0500183 /* restart */
184 synth->alive = 1;
185 synth_printf("%s", synth->init);
186 return 2; /* reenabled */
187 }
188 pr_warn("%s: can't restart synth\n", synth->long_name);
189 return 0;
190}
191EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
192
193static void thread_wake_up(u_long data)
194{
195 wake_up_interruptible_all(&speakup_event);
196}
197
198static DEFINE_TIMER(thread_timer, thread_wake_up, 0, 0);
199
200void synth_start(void)
201{
202 struct var_t *trigger_time;
203
204 if (!synth->alive) {
205 synth_buffer_clear();
206 return;
207 }
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100208 trigger_time = spk_get_var(TRIGGER);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500209 if (!timer_pending(&thread_timer))
William Hubbs1e560262010-10-15 22:13:37 -0500210 mod_timer(&thread_timer, jiffies +
211 msecs_to_jiffies(trigger_time->u.n.value));
William Hubbsc6e3fd22010-10-07 13:20:02 -0500212}
213
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100214void spk_do_flush(void)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500215{
Sasha Levin8b9c0122014-05-27 19:08:36 -0400216 if (!synth)
217 return;
218
William Hubbsc6e3fd22010-10-07 13:20:02 -0500219 speakup_info.flushing = 1;
220 synth_buffer_clear();
221 if (synth->alive) {
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100222 if (spk_pitch_shift) {
223 synth_printf("%s", spk_pitch_buff);
224 spk_pitch_shift = 0;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500225 }
226 }
227 wake_up_interruptible_all(&speakup_event);
228 wake_up_process(speakup_task);
229}
230
231void synth_write(const char *buf, size_t count)
232{
233 while (count--)
234 synth_buffer_add(*buf++);
235 synth_start();
236}
237
238void synth_printf(const char *fmt, ...)
239{
240 va_list args;
241 unsigned char buf[160], *p;
242 int r;
243
244 va_start(args, fmt);
245 r = vsnprintf(buf, sizeof(buf), fmt, args);
246 va_end(args);
247 if (r > sizeof(buf) - 1)
248 r = sizeof(buf) - 1;
249
250 p = buf;
251 while (r--)
252 synth_buffer_add(*p++);
253 synth_start();
254}
255EXPORT_SYMBOL_GPL(synth_printf);
256
William Hubbs1e560262010-10-15 22:13:37 -0500257static int index_count;
258static int sentence_count;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500259
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100260void spk_reset_index_count(int sc)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500261{
262 static int first = 1;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200263
William Hubbsc6e3fd22010-10-07 13:20:02 -0500264 if (first)
265 first = 0;
266 else
267 synth->get_index();
268 index_count = 0;
269 sentence_count = sc;
270}
271
272int synth_supports_indexing(void)
273{
274 if (synth->get_index != NULL)
275 return 1;
276 return 0;
277}
278
279void synth_insert_next_index(int sent_num)
280{
281 int out;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200282
William Hubbsc6e3fd22010-10-07 13:20:02 -0500283 if (synth->alive) {
284 if (sent_num == 0) {
285 synth->indexing.currindex++;
286 index_count++;
287 if (synth->indexing.currindex >
288 synth->indexing.highindex)
289 synth->indexing.currindex =
290 synth->indexing.lowindex;
291 }
292
293 out = synth->indexing.currindex * 10 + sent_num;
294 synth_printf(synth->indexing.command, out, out);
295 }
296}
297
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100298void spk_get_index_count(int *linecount, int *sentcount)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500299{
300 int ind = synth->get_index();
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200301
William Hubbsc6e3fd22010-10-07 13:20:02 -0500302 if (ind) {
303 sentence_count = ind % 10;
304
305 if ((ind / 10) <= synth->indexing.currindex)
306 index_count = synth->indexing.currindex-(ind/10);
307 else
William Hubbs1e560262010-10-15 22:13:37 -0500308 index_count = synth->indexing.currindex
309 -synth->indexing.lowindex
William Hubbsc6e3fd22010-10-07 13:20:02 -0500310 + synth->indexing.highindex-(ind/10)+1;
311
312 }
313 *sentcount = sentence_count;
314 *linecount = index_count;
315}
316
317static struct resource synth_res;
318
319int synth_request_region(unsigned long start, unsigned long n)
320{
321 struct resource *parent = &ioport_resource;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200322
William Hubbsc6e3fd22010-10-07 13:20:02 -0500323 memset(&synth_res, 0, sizeof(synth_res));
324 synth_res.name = synth->name;
325 synth_res.start = start;
326 synth_res.end = start + n - 1;
327 synth_res.flags = IORESOURCE_BUSY;
328 return request_resource(parent, &synth_res);
329}
330EXPORT_SYMBOL_GPL(synth_request_region);
331
332int synth_release_region(unsigned long start, unsigned long n)
333{
334 return release_resource(&synth_res);
335}
336EXPORT_SYMBOL_GPL(synth_release_region);
337
338struct var_t synth_time_vars[] = {
William Hubbs1e560262010-10-15 22:13:37 -0500339 { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } },
340 { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } },
341 { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } },
342 { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } },
William Hubbsc6e3fd22010-10-07 13:20:02 -0500343 V_LAST_VAR
344};
345
346/* called by: speakup_init() */
347int synth_init(char *synth_name)
348{
349 int i;
350 int ret = 0;
351 struct spk_synth *synth = NULL;
352
353 if (synth_name == NULL)
354 return 0;
355
356 if (strcmp(synth_name, "none") == 0) {
357 mutex_lock(&spk_mutex);
358 synth_release();
359 mutex_unlock(&spk_mutex);
360 return 0;
361 }
362
363 mutex_lock(&spk_mutex);
364 /* First, check if we already have it loaded. */
Nickolai Zeldovichae428652013-01-05 14:17:45 -0500365 for (i = 0; i < MAXSYNTHS && synths[i] != NULL; i++)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500366 if (strcmp(synths[i]->name, synth_name) == 0)
367 synth = synths[i];
368
369 /* If we got one, initialize it now. */
370 if (synth)
371 ret = do_synth_init(synth);
372 else
373 ret = -ENODEV;
374 mutex_unlock(&spk_mutex);
375
376 return ret;
377}
378
379/* called by: synth_add() */
380static int do_synth_init(struct spk_synth *in_synth)
381{
382 struct var_t *var;
383
384 synth_release();
385 if (in_synth->checkval != SYNTH_CHECK)
386 return -EINVAL;
387 synth = in_synth;
388 synth->alive = 0;
389 pr_warn("synth probe\n");
390 if (synth->probe(synth) < 0) {
391 pr_warn("%s: device probe failed\n", in_synth->name);
392 synth = NULL;
393 return -ENODEV;
394 }
395 synth_time_vars[0].u.n.value =
396 synth_time_vars[0].u.n.default_val = synth->delay;
397 synth_time_vars[1].u.n.value =
398 synth_time_vars[1].u.n.default_val = synth->trigger;
399 synth_time_vars[2].u.n.value =
400 synth_time_vars[2].u.n.default_val = synth->jiffies;
401 synth_time_vars[3].u.n.value =
402 synth_time_vars[3].u.n.default_val = synth->full;
403 synth_printf("%s", synth->init);
William Hubbs1e560262010-10-15 22:13:37 -0500404 for (var = synth->vars;
405 (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500406 speakup_register_var(var);
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100407 if (!spk_quiet_boot)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500408 synth_printf("%s found\n", synth->long_name);
409 if (synth->attributes.name
410 && sysfs_create_group(speakup_kobj, &(synth->attributes)) < 0)
411 return -ENOMEM;
412 synth_flags = synth->flags;
413 wake_up_interruptible_all(&speakup_event);
414 if (speakup_task)
415 wake_up_process(speakup_task);
416 return 0;
417}
418
419void synth_release(void)
420{
421 struct var_t *var;
422 unsigned long flags;
423
424 if (synth == NULL)
425 return;
William Hubbs66973302013-05-13 00:03:08 -0500426 spin_lock_irqsave(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500427 pr_info("releasing synth %s\n", synth->name);
428 synth->alive = 0;
429 del_timer(&thread_timer);
William Hubbs66973302013-05-13 00:03:08 -0500430 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
William Hubbsc6e3fd22010-10-07 13:20:02 -0500431 if (synth->attributes.name)
432 sysfs_remove_group(speakup_kobj, &(synth->attributes));
433 for (var = synth->vars; var->var_id != MAXVARS; var++)
434 speakup_unregister_var(var->var_id);
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100435 spk_stop_serial_interrupt();
William Hubbsc6e3fd22010-10-07 13:20:02 -0500436 synth->release();
437 synth = NULL;
438}
439
440/* called by: all_driver_init() */
441int synth_add(struct spk_synth *in_synth)
442{
443 int i;
444 int status = 0;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200445
William Hubbsc6e3fd22010-10-07 13:20:02 -0500446 mutex_lock(&spk_mutex);
Samuel Thibault6102c482013-01-07 22:03:51 +0100447 for (i = 0; i < MAXSYNTHS && synths[i] != NULL; i++)
William Hubbsc6e3fd22010-10-07 13:20:02 -0500448 /* synth_remove() is responsible for rotating the array down */
449 if (in_synth == synths[i]) {
450 mutex_unlock(&spk_mutex);
451 return 0;
452 }
453 if (i == MAXSYNTHS) {
454 pr_warn("Error: attempting to add a synth past end of array\n");
455 mutex_unlock(&spk_mutex);
456 return -1;
457 }
458 synths[i++] = in_synth;
459 synths[i] = NULL;
460 if (in_synth->startup)
461 status = do_synth_init(in_synth);
462 mutex_unlock(&spk_mutex);
463 return status;
464}
465EXPORT_SYMBOL_GPL(synth_add);
466
467void synth_remove(struct spk_synth *in_synth)
468{
469 int i;
Domagoj Trsan8e69a812014-09-09 20:04:34 +0200470
William Hubbsc6e3fd22010-10-07 13:20:02 -0500471 mutex_lock(&spk_mutex);
472 if (synth == in_synth)
473 synth_release();
474 for (i = 0; synths[i] != NULL; i++) {
475 if (in_synth == synths[i])
476 break;
477 }
478 for ( ; synths[i] != NULL; i++) /* compress table */
479 synths[i] = synths[i+1];
480 module_status = 0;
481 mutex_unlock(&spk_mutex);
482}
483EXPORT_SYMBOL_GPL(synth_remove);
484
Samuel Thibaultca2beaf2013-01-02 02:37:40 +0100485short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC|B_SYM };