GNU Linux-libre 4.14.259-gnu1
[releases.git] / drivers / staging / speakup / synth.c
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>
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. */
21 static struct spk_synth *synths[MAXSYNTHS + 1];
22 struct spk_synth *synth;
23 char spk_pitch_buff[32] = "";
24 static int module_status;
25 bool spk_quiet_boot;
26
27 struct speakup_info_t speakup_info = {
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          *
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.
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          */
40         .spinlock = __SPIN_LOCK_UNLOCKED(speakup_info.spinlock),
41         .flushing = 0,
42 };
43 EXPORT_SYMBOL_GPL(speakup_info);
44
45 static int do_synth_init(struct spk_synth *in_synth);
46
47 /*
48  * Main loop of the progression thread: keep eating from the buffer
49  * and push to the serial port, waiting as needed
50  *
51  * For devices that have a "full" notification mechanism, the driver can
52  * adapt the loop the way they prefer.
53  */
54 void 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
66         jiffy_delta = spk_get_var(JIFFY);
67         full_time = spk_get_var(FULL);
68         delay_time = spk_get_var(DELAY);
69
70         spin_lock_irqsave(&speakup_info.spinlock, flags);
71         jiffy_delta_val = jiffy_delta->u.n.value;
72         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
73
74         jiff_max = jiffies + jiffy_delta_val;
75         while (!kthread_should_stop()) {
76                 spin_lock_irqsave(&speakup_info.spinlock, flags);
77                 if (speakup_info.flushing) {
78                         speakup_info.flushing = 0;
79                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
80                         synth->flush(synth);
81                         continue;
82                 }
83                 synth_buffer_skip_nonlatin1();
84                 if (synth_buffer_empty()) {
85                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
86                         break;
87                 }
88                 ch = synth_buffer_peek();
89                 set_current_state(TASK_INTERRUPTIBLE);
90                 full_time_val = full_time->u.n.value;
91                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
92                 if (ch == '\n')
93                         ch = synth->procspeech;
94                 if (!synth->io_ops->synth_out(synth, ch)) {
95                         schedule_timeout(msecs_to_jiffies(full_time_val));
96                         continue;
97                 }
98                 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
99                         spin_lock_irqsave(&speakup_info.spinlock, flags);
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;
103                         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
104                         if (synth->io_ops->synth_out(synth, synth->procspeech))
105                                 schedule_timeout(
106                                         msecs_to_jiffies(delay_time_val));
107                         else
108                                 schedule_timeout(
109                                         msecs_to_jiffies(full_time_val));
110                         jiff_max = jiffies + jiffy_delta_val;
111                 }
112                 set_current_state(TASK_RUNNING);
113                 spin_lock_irqsave(&speakup_info.spinlock, flags);
114                 synth_buffer_getc();
115                 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
116         }
117         synth->io_ops->synth_out(synth, synth->procspeech);
118 }
119 EXPORT_SYMBOL_GPL(spk_do_catch_up);
120
121 void spk_synth_flush(struct spk_synth *synth)
122 {
123         synth->io_ops->flush_buffer();
124         synth->io_ops->synth_out(synth, synth->clear);
125 }
126 EXPORT_SYMBOL_GPL(spk_synth_flush);
127
128 unsigned char spk_synth_get_index(struct spk_synth *synth)
129 {
130         return synth->io_ops->synth_in_nowait();
131 }
132 EXPORT_SYMBOL_GPL(spk_synth_get_index);
133
134 int spk_synth_is_alive_nop(struct spk_synth *synth)
135 {
136         synth->alive = 1;
137         return 1;
138 }
139 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
140
141 int spk_synth_is_alive_restart(struct spk_synth *synth)
142 {
143         if (synth->alive)
144                 return 1;
145         if (spk_wait_for_xmitr(synth) > 0) {
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 }
154 EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
155
156 static void thread_wake_up(u_long data)
157 {
158         wake_up_interruptible_all(&speakup_event);
159 }
160
161 static DEFINE_TIMER(thread_timer, thread_wake_up, 0, 0);
162
163 void synth_start(void)
164 {
165         struct var_t *trigger_time;
166
167         if (!synth->alive) {
168                 synth_buffer_clear();
169                 return;
170         }
171         trigger_time = spk_get_var(TRIGGER);
172         if (!timer_pending(&thread_timer))
173                 mod_timer(&thread_timer, jiffies +
174                         msecs_to_jiffies(trigger_time->u.n.value));
175 }
176
177 void spk_do_flush(void)
178 {
179         if (!synth)
180                 return;
181
182         speakup_info.flushing = 1;
183         synth_buffer_clear();
184         if (synth->alive) {
185                 if (spk_pitch_shift) {
186                         synth_printf("%s", spk_pitch_buff);
187                         spk_pitch_shift = 0;
188                 }
189         }
190         wake_up_interruptible_all(&speakup_event);
191         wake_up_process(speakup_task);
192 }
193
194 void synth_write(const char *buf, size_t count)
195 {
196         while (count--)
197                 synth_buffer_add(*buf++);
198         synth_start();
199 }
200
201 void 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 }
218 EXPORT_SYMBOL_GPL(synth_printf);
219
220 void synth_putwc(u16 wc)
221 {
222         synth_buffer_add(wc);
223 }
224 EXPORT_SYMBOL_GPL(synth_putwc);
225
226 void synth_putwc_s(u16 wc)
227 {
228         synth_buffer_add(wc);
229         synth_start();
230 }
231 EXPORT_SYMBOL_GPL(synth_putwc_s);
232
233 void synth_putws(const u16 *buf)
234 {
235         const u16 *p;
236
237         for (p = buf; *p; p++)
238                 synth_buffer_add(*p);
239 }
240 EXPORT_SYMBOL_GPL(synth_putws);
241
242 void synth_putws_s(const u16 *buf)
243 {
244         synth_putws(buf);
245         synth_start();
246 }
247 EXPORT_SYMBOL_GPL(synth_putws_s);
248
249 static int index_count;
250 static int sentence_count;
251
252 void spk_reset_index_count(int sc)
253 {
254         static int first = 1;
255
256         if (first)
257                 first = 0;
258         else
259                 synth->get_index(synth);
260         index_count = 0;
261         sentence_count = sc;
262 }
263
264 int synth_supports_indexing(void)
265 {
266         if (synth->get_index)
267                 return 1;
268         return 0;
269 }
270
271 void synth_insert_next_index(int sent_num)
272 {
273         int out;
274
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
290 void spk_get_index_count(int *linecount, int *sentcount)
291 {
292         int ind = synth->get_index(synth);
293
294         if (ind) {
295                 sentence_count = ind % 10;
296
297                 if ((ind / 10) <= synth->indexing.currindex)
298                         index_count = synth->indexing.currindex - (ind / 10);
299                 else
300                         index_count = synth->indexing.currindex
301                                 - synth->indexing.lowindex
302                                 + synth->indexing.highindex - (ind / 10) + 1;
303         }
304         *sentcount = sentence_count;
305         *linecount = index_count;
306 }
307
308 static struct resource synth_res;
309
310 int synth_request_region(unsigned long start, unsigned long n)
311 {
312         struct resource *parent = &ioport_resource;
313
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 }
321 EXPORT_SYMBOL_GPL(synth_request_region);
322
323 int synth_release_region(unsigned long start, unsigned long n)
324 {
325         return release_resource(&synth_res);
326 }
327 EXPORT_SYMBOL_GPL(synth_release_region);
328
329 struct var_t synth_time_vars[] = {
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 } },
334         V_LAST_VAR
335 };
336
337 /* called by: speakup_init() */
338 int synth_init(char *synth_name)
339 {
340         int i;
341         int ret = 0;
342         struct spk_synth *synth = NULL;
343
344         if (!synth_name)
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. */
356         for (i = 0; i < MAXSYNTHS && synths[i]; i++)
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() */
371 static 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);
395         for (var = synth->vars;
396                 (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
397                 speakup_register_var(var);
398         if (!spk_quiet_boot)
399                 synth_printf("%s found\n", synth->long_name);
400         if (synth->attributes.name &&
401             sysfs_create_group(speakup_kobj, &synth->attributes) < 0)
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
410 void synth_release(void)
411 {
412         struct var_t *var;
413         unsigned long flags;
414
415         if (!synth)
416                 return;
417         spin_lock_irqsave(&speakup_info.spinlock, flags);
418         pr_info("releasing synth %s\n", synth->name);
419         synth->alive = 0;
420         del_timer(&thread_timer);
421         spin_unlock_irqrestore(&speakup_info.spinlock, flags);
422         if (synth->attributes.name)
423                 sysfs_remove_group(speakup_kobj, &synth->attributes);
424         for (var = synth->vars; var->var_id != MAXVARS; var++)
425                 speakup_unregister_var(var->var_id);
426         synth->release();
427         synth = NULL;
428 }
429
430 /* called by: all_driver_init() */
431 int synth_add(struct spk_synth *in_synth)
432 {
433         int i;
434         int status = 0;
435
436         mutex_lock(&spk_mutex);
437         for (i = 0; i < MAXSYNTHS && synths[i]; i++)
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         }
448
449         if (in_synth->startup)
450                 status = do_synth_init(in_synth);
451
452         if (!status) {
453                 synths[i++] = in_synth;
454                 synths[i] = NULL;
455         }
456
457         mutex_unlock(&spk_mutex);
458         return status;
459 }
460 EXPORT_SYMBOL_GPL(synth_add);
461
462 void synth_remove(struct spk_synth *in_synth)
463 {
464         int i;
465
466         mutex_lock(&spk_mutex);
467         if (synth == in_synth)
468                 synth_release();
469         for (i = 0; synths[i]; i++) {
470                 if (in_synth == synths[i])
471                         break;
472         }
473         for ( ; synths[i]; i++) /* compress table */
474                 synths[i] = synths[i + 1];
475         module_status = 0;
476         mutex_unlock(&spk_mutex);
477 }
478 EXPORT_SYMBOL_GPL(synth_remove);
479
480 short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };