i2c: gpio: fault-injector: refactor incomplete transfer
[linux/fpc-iii.git] / drivers / staging / speakup / synth.c
blob7deeb706101855bbe119f29a5739ece23dd14a84
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/ctype.h> /* for isdigit() and friends */
4 #include <linux/fs.h>
5 #include <linux/mm.h> /* for verify_area */
6 #include <linux/errno.h> /* for -EBUSY */
7 #include <linux/ioport.h> /* for check_region, request_region */
8 #include <linux/interrupt.h>
9 #include <linux/delay.h> /* for loops_per_sec */
10 #include <linux/kmod.h>
11 #include <linux/jiffies.h>
12 #include <linux/uaccess.h> /* for copy_from_user */
13 #include <linux/sched.h>
14 #include <linux/timer.h>
15 #include <linux/kthread.h>
17 #include "spk_priv.h"
18 #include "speakup.h"
19 #include "serialio.h"
21 #define MAXSYNTHS 16 /* Max number of synths in array. */
22 static struct spk_synth *synths[MAXSYNTHS + 1];
23 struct spk_synth *synth;
24 char spk_pitch_buff[32] = "";
25 static int module_status;
26 bool spk_quiet_boot;
28 struct speakup_info_t speakup_info = {
30 * This spinlock is used to protect the entire speakup machinery, and
31 * must be taken at each kernel->speakup transition and released at
32 * each corresponding speakup->kernel transition.
34 * The progression thread only interferes with the speakup machinery
35 * through the synth buffer, so only needs to take the lock
36 * while tinkering with the buffer.
38 * We use spin_lock/trylock_irqsave and spin_unlock_irqrestore with this
39 * spinlock because speakup needs to disable the keyboard IRQ.
41 .spinlock = __SPIN_LOCK_UNLOCKED(speakup_info.spinlock),
42 .flushing = 0,
44 EXPORT_SYMBOL_GPL(speakup_info);
46 static int do_synth_init(struct spk_synth *in_synth);
49 * Main loop of the progression thread: keep eating from the buffer
50 * and push to the serial port, waiting as needed
52 * For devices that have a "full" notification mechanism, the driver can
53 * adapt the loop the way they prefer.
55 static void _spk_do_catch_up(struct spk_synth *synth, int unicode)
57 u16 ch;
58 unsigned long flags;
59 unsigned long jiff_max;
60 struct var_t *delay_time;
61 struct var_t *full_time;
62 struct var_t *jiffy_delta;
63 int jiffy_delta_val;
64 int delay_time_val;
65 int full_time_val;
66 int ret;
68 jiffy_delta = spk_get_var(JIFFY);
69 full_time = spk_get_var(FULL);
70 delay_time = spk_get_var(DELAY);
72 spin_lock_irqsave(&speakup_info.spinlock, flags);
73 jiffy_delta_val = jiffy_delta->u.n.value;
74 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
76 jiff_max = jiffies + jiffy_delta_val;
77 while (!kthread_should_stop()) {
78 spin_lock_irqsave(&speakup_info.spinlock, flags);
79 if (speakup_info.flushing) {
80 speakup_info.flushing = 0;
81 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
82 synth->flush(synth);
83 continue;
85 if (!unicode)
86 synth_buffer_skip_nonlatin1();
87 if (synth_buffer_empty()) {
88 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
89 break;
91 ch = synth_buffer_peek();
92 set_current_state(TASK_INTERRUPTIBLE);
93 full_time_val = full_time->u.n.value;
94 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
95 if (ch == '\n')
96 ch = synth->procspeech;
97 if (unicode)
98 ret = synth->io_ops->synth_out_unicode(synth, ch);
99 else
100 ret = synth->io_ops->synth_out(synth, ch);
101 if (!ret) {
102 schedule_timeout(msecs_to_jiffies(full_time_val));
103 continue;
105 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
106 spin_lock_irqsave(&speakup_info.spinlock, flags);
107 jiffy_delta_val = jiffy_delta->u.n.value;
108 delay_time_val = delay_time->u.n.value;
109 full_time_val = full_time->u.n.value;
110 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
111 if (synth->io_ops->synth_out(synth, synth->procspeech))
112 schedule_timeout(
113 msecs_to_jiffies(delay_time_val));
114 else
115 schedule_timeout(
116 msecs_to_jiffies(full_time_val));
117 jiff_max = jiffies + jiffy_delta_val;
119 set_current_state(TASK_RUNNING);
120 spin_lock_irqsave(&speakup_info.spinlock, flags);
121 synth_buffer_getc();
122 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
124 synth->io_ops->synth_out(synth, synth->procspeech);
127 void spk_do_catch_up(struct spk_synth *synth)
129 _spk_do_catch_up(synth, 0);
131 EXPORT_SYMBOL_GPL(spk_do_catch_up);
133 void spk_do_catch_up_unicode(struct spk_synth *synth)
135 _spk_do_catch_up(synth, 1);
137 EXPORT_SYMBOL_GPL(spk_do_catch_up_unicode);
139 void spk_synth_flush(struct spk_synth *synth)
141 synth->io_ops->flush_buffer();
142 synth->io_ops->synth_out(synth, synth->clear);
144 EXPORT_SYMBOL_GPL(spk_synth_flush);
146 unsigned char spk_synth_get_index(struct spk_synth *synth)
148 return synth->io_ops->synth_in_nowait();
150 EXPORT_SYMBOL_GPL(spk_synth_get_index);
152 int spk_synth_is_alive_nop(struct spk_synth *synth)
154 synth->alive = 1;
155 return 1;
157 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
159 int spk_synth_is_alive_restart(struct spk_synth *synth)
161 if (synth->alive)
162 return 1;
163 if (spk_wait_for_xmitr(synth) > 0) {
164 /* restart */
165 synth->alive = 1;
166 synth_printf("%s", synth->init);
167 return 2; /* reenabled */
169 pr_warn("%s: can't restart synth\n", synth->long_name);
170 return 0;
172 EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
174 static void thread_wake_up(struct timer_list *unused)
176 wake_up_interruptible_all(&speakup_event);
179 static DEFINE_TIMER(thread_timer, thread_wake_up);
181 void synth_start(void)
183 struct var_t *trigger_time;
185 if (!synth->alive) {
186 synth_buffer_clear();
187 return;
189 trigger_time = spk_get_var(TRIGGER);
190 if (!timer_pending(&thread_timer))
191 mod_timer(&thread_timer, jiffies +
192 msecs_to_jiffies(trigger_time->u.n.value));
195 void spk_do_flush(void)
197 if (!synth)
198 return;
200 speakup_info.flushing = 1;
201 synth_buffer_clear();
202 if (synth->alive) {
203 if (spk_pitch_shift) {
204 synth_printf("%s", spk_pitch_buff);
205 spk_pitch_shift = 0;
208 wake_up_interruptible_all(&speakup_event);
209 wake_up_process(speakup_task);
212 void synth_write(const char *buf, size_t count)
214 while (count--)
215 synth_buffer_add(*buf++);
216 synth_start();
219 void synth_printf(const char *fmt, ...)
221 va_list args;
222 unsigned char buf[160], *p;
223 int r;
225 va_start(args, fmt);
226 r = vsnprintf(buf, sizeof(buf), fmt, args);
227 va_end(args);
228 if (r > sizeof(buf) - 1)
229 r = sizeof(buf) - 1;
231 p = buf;
232 while (r--)
233 synth_buffer_add(*p++);
234 synth_start();
236 EXPORT_SYMBOL_GPL(synth_printf);
238 void synth_putwc(u16 wc)
240 synth_buffer_add(wc);
242 EXPORT_SYMBOL_GPL(synth_putwc);
244 void synth_putwc_s(u16 wc)
246 synth_buffer_add(wc);
247 synth_start();
249 EXPORT_SYMBOL_GPL(synth_putwc_s);
251 void synth_putws(const u16 *buf)
253 const u16 *p;
255 for (p = buf; *p; p++)
256 synth_buffer_add(*p);
258 EXPORT_SYMBOL_GPL(synth_putws);
260 void synth_putws_s(const u16 *buf)
262 synth_putws(buf);
263 synth_start();
265 EXPORT_SYMBOL_GPL(synth_putws_s);
267 static int index_count;
268 static int sentence_count;
270 void spk_reset_index_count(int sc)
272 static int first = 1;
274 if (first)
275 first = 0;
276 else
277 synth->get_index(synth);
278 index_count = 0;
279 sentence_count = sc;
282 int synth_supports_indexing(void)
284 if (synth->get_index)
285 return 1;
286 return 0;
289 void synth_insert_next_index(int sent_num)
291 int out;
293 if (synth->alive) {
294 if (sent_num == 0) {
295 synth->indexing.currindex++;
296 index_count++;
297 if (synth->indexing.currindex >
298 synth->indexing.highindex)
299 synth->indexing.currindex =
300 synth->indexing.lowindex;
303 out = synth->indexing.currindex * 10 + sent_num;
304 synth_printf(synth->indexing.command, out, out);
308 void spk_get_index_count(int *linecount, int *sentcount)
310 int ind = synth->get_index(synth);
312 if (ind) {
313 sentence_count = ind % 10;
315 if ((ind / 10) <= synth->indexing.currindex)
316 index_count = synth->indexing.currindex - (ind / 10);
317 else
318 index_count = synth->indexing.currindex
319 - synth->indexing.lowindex
320 + synth->indexing.highindex - (ind / 10) + 1;
322 *sentcount = sentence_count;
323 *linecount = index_count;
326 static struct resource synth_res;
328 int synth_request_region(unsigned long start, unsigned long n)
330 struct resource *parent = &ioport_resource;
332 memset(&synth_res, 0, sizeof(synth_res));
333 synth_res.name = synth->name;
334 synth_res.start = start;
335 synth_res.end = start + n - 1;
336 synth_res.flags = IORESOURCE_BUSY;
337 return request_resource(parent, &synth_res);
339 EXPORT_SYMBOL_GPL(synth_request_region);
341 int synth_release_region(unsigned long start, unsigned long n)
343 return release_resource(&synth_res);
345 EXPORT_SYMBOL_GPL(synth_release_region);
347 struct var_t synth_time_vars[] = {
348 { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } },
349 { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } },
350 { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } },
351 { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } },
352 V_LAST_VAR
355 /* called by: speakup_init() */
356 int synth_init(char *synth_name)
358 int i;
359 int ret = 0;
360 struct spk_synth *synth = NULL;
362 if (!synth_name)
363 return 0;
365 if (strcmp(synth_name, "none") == 0) {
366 mutex_lock(&spk_mutex);
367 synth_release();
368 mutex_unlock(&spk_mutex);
369 return 0;
372 mutex_lock(&spk_mutex);
373 /* First, check if we already have it loaded. */
374 for (i = 0; i < MAXSYNTHS && synths[i]; i++)
375 if (strcmp(synths[i]->name, synth_name) == 0)
376 synth = synths[i];
378 /* If we got one, initialize it now. */
379 if (synth)
380 ret = do_synth_init(synth);
381 else
382 ret = -ENODEV;
383 mutex_unlock(&spk_mutex);
385 return ret;
388 /* called by: synth_add() */
389 static int do_synth_init(struct spk_synth *in_synth)
391 struct var_t *var;
393 synth_release();
394 if (in_synth->checkval != SYNTH_CHECK)
395 return -EINVAL;
396 synth = in_synth;
397 synth->alive = 0;
398 pr_warn("synth probe\n");
399 if (synth->probe(synth) < 0) {
400 pr_warn("%s: device probe failed\n", in_synth->name);
401 synth = NULL;
402 return -ENODEV;
404 synth_time_vars[0].u.n.value =
405 synth_time_vars[0].u.n.default_val = synth->delay;
406 synth_time_vars[1].u.n.value =
407 synth_time_vars[1].u.n.default_val = synth->trigger;
408 synth_time_vars[2].u.n.value =
409 synth_time_vars[2].u.n.default_val = synth->jiffies;
410 synth_time_vars[3].u.n.value =
411 synth_time_vars[3].u.n.default_val = synth->full;
412 synth_printf("%s", synth->init);
413 for (var = synth->vars;
414 (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
415 speakup_register_var(var);
416 if (!spk_quiet_boot)
417 synth_printf("%s found\n", synth->long_name);
418 if (synth->attributes.name &&
419 sysfs_create_group(speakup_kobj, &synth->attributes) < 0)
420 return -ENOMEM;
421 synth_flags = synth->flags;
422 wake_up_interruptible_all(&speakup_event);
423 if (speakup_task)
424 wake_up_process(speakup_task);
425 return 0;
428 void synth_release(void)
430 struct var_t *var;
431 unsigned long flags;
433 if (!synth)
434 return;
435 spin_lock_irqsave(&speakup_info.spinlock, flags);
436 pr_info("releasing synth %s\n", synth->name);
437 synth->alive = 0;
438 del_timer(&thread_timer);
439 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
440 if (synth->attributes.name)
441 sysfs_remove_group(speakup_kobj, &synth->attributes);
442 for (var = synth->vars; var->var_id != MAXVARS; var++)
443 speakup_unregister_var(var->var_id);
444 synth->release();
445 synth = NULL;
448 /* called by: all_driver_init() */
449 int synth_add(struct spk_synth *in_synth)
451 int i;
452 int status = 0;
454 mutex_lock(&spk_mutex);
455 for (i = 0; i < MAXSYNTHS && synths[i]; i++)
456 /* synth_remove() is responsible for rotating the array down */
457 if (in_synth == synths[i]) {
458 mutex_unlock(&spk_mutex);
459 return 0;
461 if (i == MAXSYNTHS) {
462 pr_warn("Error: attempting to add a synth past end of array\n");
463 mutex_unlock(&spk_mutex);
464 return -1;
467 if (in_synth->startup)
468 status = do_synth_init(in_synth);
470 if (!status) {
471 synths[i++] = in_synth;
472 synths[i] = NULL;
475 mutex_unlock(&spk_mutex);
476 return status;
478 EXPORT_SYMBOL_GPL(synth_add);
480 void synth_remove(struct spk_synth *in_synth)
482 int i;
484 mutex_lock(&spk_mutex);
485 if (synth == in_synth)
486 synth_release();
487 for (i = 0; synths[i]; i++) {
488 if (in_synth == synths[i])
489 break;
491 for ( ; synths[i]; i++) /* compress table */
492 synths[i] = synths[i + 1];
493 module_status = 0;
494 mutex_unlock(&spk_mutex);
496 EXPORT_SYMBOL_GPL(synth_remove);
498 short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };