1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/ctype.h> /* for isdigit() and friends */
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>
21 static LIST_HEAD(synths
);
22 struct spk_synth
*synth
;
23 char spk_pitch_buff
[32] = "";
24 static int module_status
;
27 struct speakup_info_t speakup_info
= {
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.
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.
37 * We use spin_lock/trylock_irqsave and spin_unlock_irqrestore with this
38 * spinlock because speakup needs to disable the keyboard IRQ.
40 .spinlock
= __SPIN_LOCK_UNLOCKED(speakup_info
.spinlock
),
43 EXPORT_SYMBOL_GPL(speakup_info
);
45 static int do_synth_init(struct spk_synth
*in_synth
);
48 * Main loop of the progression thread: keep eating from the buffer
49 * and push to the serial port, waiting as needed
51 * For devices that have a "full" notification mechanism, the driver can
52 * adapt the loop the way they prefer.
54 static void _spk_do_catch_up(struct spk_synth
*synth
, int unicode
)
58 unsigned long jiff_max
;
59 struct var_t
*delay_time
;
60 struct var_t
*full_time
;
61 struct var_t
*jiffy_delta
;
67 jiffy_delta
= spk_get_var(JIFFY
);
68 full_time
= spk_get_var(FULL
);
69 delay_time
= spk_get_var(DELAY
);
71 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
72 jiffy_delta_val
= jiffy_delta
->u
.n
.value
;
73 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
75 jiff_max
= jiffies
+ jiffy_delta_val
;
76 while (!kthread_should_stop()) {
77 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
78 if (speakup_info
.flushing
) {
79 speakup_info
.flushing
= 0;
80 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
85 synth_buffer_skip_nonlatin1();
86 if (synth_buffer_empty()) {
87 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
90 ch
= synth_buffer_peek();
91 set_current_state(TASK_INTERRUPTIBLE
);
92 full_time_val
= full_time
->u
.n
.value
;
93 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
95 ch
= synth
->procspeech
;
97 ret
= synth
->io_ops
->synth_out_unicode(synth
, ch
);
99 ret
= synth
->io_ops
->synth_out(synth
, ch
);
101 schedule_timeout(msecs_to_jiffies(full_time_val
));
104 if (time_after_eq(jiffies
, jiff_max
) && (ch
== SPACE
)) {
105 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
106 jiffy_delta_val
= jiffy_delta
->u
.n
.value
;
107 delay_time_val
= delay_time
->u
.n
.value
;
108 full_time_val
= full_time
->u
.n
.value
;
109 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
110 if (synth
->io_ops
->synth_out(synth
, synth
->procspeech
))
112 msecs_to_jiffies(delay_time_val
));
115 msecs_to_jiffies(full_time_val
));
116 jiff_max
= jiffies
+ jiffy_delta_val
;
118 set_current_state(TASK_RUNNING
);
119 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
121 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
123 synth
->io_ops
->synth_out(synth
, synth
->procspeech
);
126 void spk_do_catch_up(struct spk_synth
*synth
)
128 _spk_do_catch_up(synth
, 0);
130 EXPORT_SYMBOL_GPL(spk_do_catch_up
);
132 void spk_do_catch_up_unicode(struct spk_synth
*synth
)
134 _spk_do_catch_up(synth
, 1);
136 EXPORT_SYMBOL_GPL(spk_do_catch_up_unicode
);
138 void spk_synth_flush(struct spk_synth
*synth
)
140 synth
->io_ops
->flush_buffer(synth
);
141 synth
->io_ops
->synth_out(synth
, synth
->clear
);
143 EXPORT_SYMBOL_GPL(spk_synth_flush
);
145 unsigned char spk_synth_get_index(struct spk_synth
*synth
)
147 return synth
->io_ops
->synth_in_nowait(synth
);
149 EXPORT_SYMBOL_GPL(spk_synth_get_index
);
151 int spk_synth_is_alive_nop(struct spk_synth
*synth
)
156 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop
);
158 int spk_synth_is_alive_restart(struct spk_synth
*synth
)
162 if (synth
->io_ops
->wait_for_xmitr(synth
) > 0) {
165 synth_printf("%s", synth
->init
);
166 return 2; /* reenabled */
168 pr_warn("%s: can't restart synth\n", synth
->long_name
);
171 EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart
);
173 static void thread_wake_up(struct timer_list
*unused
)
175 wake_up_interruptible_all(&speakup_event
);
178 static DEFINE_TIMER(thread_timer
, thread_wake_up
);
180 void synth_start(void)
182 struct var_t
*trigger_time
;
185 synth_buffer_clear();
188 trigger_time
= spk_get_var(TRIGGER
);
189 if (!timer_pending(&thread_timer
))
190 mod_timer(&thread_timer
, jiffies
+
191 msecs_to_jiffies(trigger_time
->u
.n
.value
));
194 void spk_do_flush(void)
199 speakup_info
.flushing
= 1;
200 synth_buffer_clear();
202 if (spk_pitch_shift
) {
203 synth_printf("%s", spk_pitch_buff
);
207 wake_up_interruptible_all(&speakup_event
);
208 wake_up_process(speakup_task
);
211 void synth_write(const char *_buf
, size_t count
)
213 const unsigned char *buf
= (const unsigned char *) _buf
;
216 synth_buffer_add(*buf
++);
220 /* Consume one utf-8 character from buf (that contains up to count bytes),
221 * returns the unicode codepoint if valid, -1 otherwise.
222 * In all cases, returns the number of consumed bytes in *consumed,
223 * and the minimum number of bytes that would be needed for the next character
226 s32
synth_utf8_get(const char *buf
, size_t count
, size_t *consumed
, size_t *want
)
228 unsigned char c
= buf
[0];
229 int nbytes
= 8 - fls(c
^ 0xff);
243 /* ASCII, take as such */
249 /* 2..6-byte UTF-8 */
251 if (count
< nbytes
) {
252 /* We don't have it all */
259 value
= c
& ((1u << (7 - nbytes
)) - 1);
262 for (i
= 1; i
< nbytes
; i
++) {
264 if ((c
& 0xc0) != 0x80) {
265 /* Invalid, drop the head */
270 value
= (value
<< 6) | (c
& 0x3f);
279 void synth_writeu(const char *buf
, size_t count
)
281 size_t i
, consumed
, want
;
284 for (i
= 0; i
< count
; i
++) {
287 value
= synth_utf8_get(buf
+ i
, count
- i
, &consumed
, &want
);
289 /* Invalid or incomplete */
291 if (want
> count
- i
)
292 /* We don't have it all, stop */
299 synth_buffer_add(value
);
305 void synth_printf(const char *fmt
, ...)
308 unsigned char buf
[160];
312 r
= vsnprintf(buf
, sizeof(buf
), fmt
, args
);
314 if (r
> sizeof(buf
) - 1)
317 synth_writeu(buf
, r
);
319 EXPORT_SYMBOL_GPL(synth_printf
);
321 void synth_putwc(u16 wc
)
323 synth_buffer_add(wc
);
325 EXPORT_SYMBOL_GPL(synth_putwc
);
327 void synth_putwc_s(u16 wc
)
329 synth_buffer_add(wc
);
332 EXPORT_SYMBOL_GPL(synth_putwc_s
);
334 void synth_putws(const u16
*buf
)
338 for (p
= buf
; *p
; p
++)
339 synth_buffer_add(*p
);
341 EXPORT_SYMBOL_GPL(synth_putws
);
343 void synth_putws_s(const u16
*buf
)
348 EXPORT_SYMBOL_GPL(synth_putws_s
);
350 static int index_count
;
351 static int sentence_count
;
353 void spk_reset_index_count(int sc
)
355 static int first
= 1;
360 synth
->get_index(synth
);
365 int synth_supports_indexing(void)
367 if (synth
->get_index
)
372 void synth_insert_next_index(int sent_num
)
378 synth
->indexing
.currindex
++;
380 if (synth
->indexing
.currindex
>
381 synth
->indexing
.highindex
)
382 synth
->indexing
.currindex
=
383 synth
->indexing
.lowindex
;
386 out
= synth
->indexing
.currindex
* 10 + sent_num
;
387 synth_printf(synth
->indexing
.command
, out
, out
);
391 void spk_get_index_count(int *linecount
, int *sentcount
)
393 int ind
= synth
->get_index(synth
);
396 sentence_count
= ind
% 10;
398 if ((ind
/ 10) <= synth
->indexing
.currindex
)
399 index_count
= synth
->indexing
.currindex
- (ind
/ 10);
401 index_count
= synth
->indexing
.currindex
402 - synth
->indexing
.lowindex
403 + synth
->indexing
.highindex
- (ind
/ 10) + 1;
405 *sentcount
= sentence_count
;
406 *linecount
= index_count
;
409 static struct resource synth_res
;
411 int synth_request_region(unsigned long start
, unsigned long n
)
413 struct resource
*parent
= &ioport_resource
;
415 memset(&synth_res
, 0, sizeof(synth_res
));
416 synth_res
.name
= synth
->name
;
417 synth_res
.start
= start
;
418 synth_res
.end
= start
+ n
- 1;
419 synth_res
.flags
= IORESOURCE_BUSY
;
420 return request_resource(parent
, &synth_res
);
422 EXPORT_SYMBOL_GPL(synth_request_region
);
424 int synth_release_region(unsigned long start
, unsigned long n
)
426 return release_resource(&synth_res
);
428 EXPORT_SYMBOL_GPL(synth_release_region
);
430 struct var_t synth_time_vars
[] = {
431 { DELAY
, .u
.n
= {NULL
, 100, 100, 2000, 0, 0, NULL
} },
432 { TRIGGER
, .u
.n
= {NULL
, 20, 10, 2000, 0, 0, NULL
} },
433 { JIFFY
, .u
.n
= {NULL
, 50, 20, 200, 0, 0, NULL
} },
434 { FULL
, .u
.n
= {NULL
, 400, 200, 60000, 0, 0, NULL
} },
435 { FLUSH
, .u
.n
= {NULL
, 4000, 10, 4000, 0, 0, NULL
} },
439 /* called by: speakup_init() */
440 int synth_init(char *synth_name
)
443 struct spk_synth
*tmp
, *synth
= NULL
;
448 if (strcmp(synth_name
, "none") == 0) {
449 mutex_lock(&spk_mutex
);
451 mutex_unlock(&spk_mutex
);
455 mutex_lock(&spk_mutex
);
456 /* First, check if we already have it loaded. */
457 list_for_each_entry(tmp
, &synths
, node
) {
458 if (strcmp(tmp
->name
, synth_name
) == 0)
462 /* If we got one, initialize it now. */
464 ret
= do_synth_init(synth
);
467 mutex_unlock(&spk_mutex
);
472 /* called by: synth_add() */
473 static int do_synth_init(struct spk_synth
*in_synth
)
478 if (in_synth
->checkval
!= SYNTH_CHECK
)
482 pr_warn("synth probe\n");
483 if (synth
->probe(synth
) < 0) {
484 pr_warn("%s: device probe failed\n", in_synth
->name
);
488 synth_time_vars
[0].u
.n
.value
=
489 synth_time_vars
[0].u
.n
.default_val
= synth
->delay
;
490 synth_time_vars
[1].u
.n
.value
=
491 synth_time_vars
[1].u
.n
.default_val
= synth
->trigger
;
492 synth_time_vars
[2].u
.n
.value
=
493 synth_time_vars
[2].u
.n
.default_val
= synth
->jiffies
;
494 synth_time_vars
[3].u
.n
.value
=
495 synth_time_vars
[3].u
.n
.default_val
= synth
->full
;
496 synth_time_vars
[4].u
.n
.value
=
497 synth_time_vars
[4].u
.n
.default_val
= synth
->flush_time
;
498 synth_printf("%s", synth
->init
);
499 for (var
= synth
->vars
;
500 (var
->var_id
>= 0) && (var
->var_id
< MAXVARS
); var
++)
501 speakup_register_var(var
);
503 synth_printf("%s found\n", synth
->long_name
);
504 if (synth
->attributes
.name
&&
505 sysfs_create_group(speakup_kobj
, &synth
->attributes
) < 0)
507 synth_flags
= synth
->flags
;
508 wake_up_interruptible_all(&speakup_event
);
510 wake_up_process(speakup_task
);
514 void synth_release(void)
521 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
522 pr_info("releasing synth %s\n", synth
->name
);
524 del_timer(&thread_timer
);
525 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
526 if (synth
->attributes
.name
)
527 sysfs_remove_group(speakup_kobj
, &synth
->attributes
);
528 for (var
= synth
->vars
; var
->var_id
!= MAXVARS
; var
++)
529 speakup_unregister_var(var
->var_id
);
530 synth
->release(synth
);
534 /* called by: all_driver_init() */
535 int synth_add(struct spk_synth
*in_synth
)
538 struct spk_synth
*tmp
;
540 mutex_lock(&spk_mutex
);
542 list_for_each_entry(tmp
, &synths
, node
) {
543 if (tmp
== in_synth
) {
544 mutex_unlock(&spk_mutex
);
549 if (in_synth
->startup
)
550 status
= do_synth_init(in_synth
);
553 list_add_tail(&in_synth
->node
, &synths
);
555 mutex_unlock(&spk_mutex
);
558 EXPORT_SYMBOL_GPL(synth_add
);
560 void synth_remove(struct spk_synth
*in_synth
)
562 mutex_lock(&spk_mutex
);
563 if (synth
== in_synth
)
565 list_del(&in_synth
->node
);
567 mutex_unlock(&spk_mutex
);
569 EXPORT_SYMBOL_GPL(synth_remove
);
571 struct spk_synth
*synth_current(void)
575 EXPORT_SYMBOL_GPL(synth_current
);
577 short spk_punc_masks
[] = { 0, SOME
, MOST
, PUNC
, PUNC
| B_SYM
};