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 #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
;
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
),
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
)
59 unsigned long jiff_max
;
60 struct var_t
*delay_time
;
61 struct var_t
*full_time
;
62 struct var_t
*jiffy_delta
;
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
);
86 synth_buffer_skip_nonlatin1();
87 if (synth_buffer_empty()) {
88 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
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
);
96 ch
= synth
->procspeech
;
98 ret
= synth
->io_ops
->synth_out_unicode(synth
, ch
);
100 ret
= synth
->io_ops
->synth_out(synth
, ch
);
102 schedule_timeout(msecs_to_jiffies(full_time_val
));
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
))
113 msecs_to_jiffies(delay_time_val
));
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
);
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
)
157 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop
);
159 int spk_synth_is_alive_restart(struct spk_synth
*synth
)
163 if (spk_wait_for_xmitr(synth
) > 0) {
166 synth_printf("%s", synth
->init
);
167 return 2; /* reenabled */
169 pr_warn("%s: can't restart synth\n", synth
->long_name
);
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
;
186 synth_buffer_clear();
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)
200 speakup_info
.flushing
= 1;
201 synth_buffer_clear();
203 if (spk_pitch_shift
) {
204 synth_printf("%s", spk_pitch_buff
);
208 wake_up_interruptible_all(&speakup_event
);
209 wake_up_process(speakup_task
);
212 void synth_write(const char *buf
, size_t count
)
215 synth_buffer_add(*buf
++);
219 void synth_printf(const char *fmt
, ...)
222 unsigned char buf
[160], *p
;
226 r
= vsnprintf(buf
, sizeof(buf
), fmt
, args
);
228 if (r
> sizeof(buf
) - 1)
233 synth_buffer_add(*p
++);
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
);
249 EXPORT_SYMBOL_GPL(synth_putwc_s
);
251 void synth_putws(const u16
*buf
)
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
)
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;
277 synth
->get_index(synth
);
282 int synth_supports_indexing(void)
284 if (synth
->get_index
)
289 void synth_insert_next_index(int sent_num
)
295 synth
->indexing
.currindex
++;
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
);
313 sentence_count
= ind
% 10;
315 if ((ind
/ 10) <= synth
->indexing
.currindex
)
316 index_count
= synth
->indexing
.currindex
- (ind
/ 10);
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
} },
355 /* called by: speakup_init() */
356 int synth_init(char *synth_name
)
360 struct spk_synth
*synth
= NULL
;
365 if (strcmp(synth_name
, "none") == 0) {
366 mutex_lock(&spk_mutex
);
368 mutex_unlock(&spk_mutex
);
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)
378 /* If we got one, initialize it now. */
380 ret
= do_synth_init(synth
);
383 mutex_unlock(&spk_mutex
);
388 /* called by: synth_add() */
389 static int do_synth_init(struct spk_synth
*in_synth
)
394 if (in_synth
->checkval
!= SYNTH_CHECK
)
398 pr_warn("synth probe\n");
399 if (synth
->probe(synth
) < 0) {
400 pr_warn("%s: device probe failed\n", in_synth
->name
);
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
);
417 synth_printf("%s found\n", synth
->long_name
);
418 if (synth
->attributes
.name
&&
419 sysfs_create_group(speakup_kobj
, &synth
->attributes
) < 0)
421 synth_flags
= synth
->flags
;
422 wake_up_interruptible_all(&speakup_event
);
424 wake_up_process(speakup_task
);
428 void synth_release(void)
435 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
436 pr_info("releasing synth %s\n", synth
->name
);
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
);
448 /* called by: all_driver_init() */
449 int synth_add(struct spk_synth
*in_synth
)
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
);
461 if (i
== MAXSYNTHS
) {
462 pr_warn("Error: attempting to add a synth past end of array\n");
463 mutex_unlock(&spk_mutex
);
467 if (in_synth
->startup
)
468 status
= do_synth_init(in_synth
);
471 synths
[i
++] = in_synth
;
475 mutex_unlock(&spk_mutex
);
478 EXPORT_SYMBOL_GPL(synth_add
);
480 void synth_remove(struct spk_synth
*in_synth
)
484 mutex_lock(&spk_mutex
);
485 if (synth
== in_synth
)
487 for (i
= 0; synths
[i
]; i
++) {
488 if (in_synth
== synths
[i
])
491 for ( ; synths
[i
]; i
++) /* compress table */
492 synths
[i
] = synths
[i
+ 1];
494 mutex_unlock(&spk_mutex
);
496 EXPORT_SYMBOL_GPL(synth_remove
);
498 short spk_punc_masks
[] = { 0, SOME
, MOST
, PUNC
, PUNC
| B_SYM
};