1 #include <linux/types.h>
2 #include <linux/ctype.h> /* for isdigit() and friends */
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>
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;
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 void spk_do_catch_up(struct spk_synth *synth)
58 unsigned long jiff_max;
59 struct var_t *delay_time;
60 struct var_t *full_time;
61 struct var_t *jiffy_delta;
66 jiffy_delta = spk_get_var(JIFFY);
67 full_time = spk_get_var(FULL);
68 delay_time = spk_get_var(DELAY);
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);
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);
83 synth_buffer_skip_nonlatin1();
84 if (synth_buffer_empty()) {
85 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
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);
93 ch = synth->procspeech;
94 if (!synth->io_ops->synth_out(synth, ch)) {
95 schedule_timeout(msecs_to_jiffies(full_time_val));
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))
106 msecs_to_jiffies(delay_time_val));
109 msecs_to_jiffies(full_time_val));
110 jiff_max = jiffies + jiffy_delta_val;
112 set_current_state(TASK_RUNNING);
113 spin_lock_irqsave(&speakup_info.spinlock, flags);
115 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
117 synth->io_ops->synth_out(synth, synth->procspeech);
119 EXPORT_SYMBOL_GPL(spk_do_catch_up);
121 void spk_synth_flush(struct spk_synth *synth)
123 synth->io_ops->flush_buffer();
124 synth->io_ops->synth_out(synth, synth->clear);
126 EXPORT_SYMBOL_GPL(spk_synth_flush);
128 unsigned char spk_synth_get_index(struct spk_synth *synth)
130 return synth->io_ops->synth_in_nowait();
132 EXPORT_SYMBOL_GPL(spk_synth_get_index);
134 int spk_synth_is_alive_nop(struct spk_synth *synth)
139 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
141 int spk_synth_is_alive_restart(struct spk_synth *synth)
145 if (spk_wait_for_xmitr(synth) > 0) {
148 synth_printf("%s", synth->init);
149 return 2; /* reenabled */
151 pr_warn("%s: can't restart synth\n", synth->long_name);
154 EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
156 static void thread_wake_up(u_long data)
158 wake_up_interruptible_all(&speakup_event);
161 static DEFINE_TIMER(thread_timer, thread_wake_up, 0, 0);
163 void synth_start(void)
165 struct var_t *trigger_time;
168 synth_buffer_clear();
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));
177 void spk_do_flush(void)
182 speakup_info.flushing = 1;
183 synth_buffer_clear();
185 if (spk_pitch_shift) {
186 synth_printf("%s", spk_pitch_buff);
190 wake_up_interruptible_all(&speakup_event);
191 wake_up_process(speakup_task);
194 void synth_write(const char *buf, size_t count)
197 synth_buffer_add(*buf++);
201 void synth_printf(const char *fmt, ...)
204 unsigned char buf[160], *p;
208 r = vsnprintf(buf, sizeof(buf), fmt, args);
210 if (r > sizeof(buf) - 1)
215 synth_buffer_add(*p++);
218 EXPORT_SYMBOL_GPL(synth_printf);
220 void synth_putwc(u16 wc)
222 synth_buffer_add(wc);
224 EXPORT_SYMBOL_GPL(synth_putwc);
226 void synth_putwc_s(u16 wc)
228 synth_buffer_add(wc);
231 EXPORT_SYMBOL_GPL(synth_putwc_s);
233 void synth_putws(const u16 *buf)
237 for (p = buf; *p; p++)
238 synth_buffer_add(*p);
240 EXPORT_SYMBOL_GPL(synth_putws);
242 void synth_putws_s(const u16 *buf)
247 EXPORT_SYMBOL_GPL(synth_putws_s);
249 static int index_count;
250 static int sentence_count;
252 void spk_reset_index_count(int sc)
254 static int first = 1;
259 synth->get_index(synth);
264 int synth_supports_indexing(void)
266 if (synth->get_index)
271 void synth_insert_next_index(int sent_num)
277 synth->indexing.currindex++;
279 if (synth->indexing.currindex >
280 synth->indexing.highindex)
281 synth->indexing.currindex =
282 synth->indexing.lowindex;
285 out = synth->indexing.currindex * 10 + sent_num;
286 synth_printf(synth->indexing.command, out, out);
290 void spk_get_index_count(int *linecount, int *sentcount)
292 int ind = synth->get_index(synth);
295 sentence_count = ind % 10;
297 if ((ind / 10) <= synth->indexing.currindex)
298 index_count = synth->indexing.currindex - (ind / 10);
300 index_count = synth->indexing.currindex
301 - synth->indexing.lowindex
302 + synth->indexing.highindex - (ind / 10) + 1;
304 *sentcount = sentence_count;
305 *linecount = index_count;
308 static struct resource synth_res;
310 int synth_request_region(unsigned long start, unsigned long n)
312 struct resource *parent = &ioport_resource;
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);
321 EXPORT_SYMBOL_GPL(synth_request_region);
323 int synth_release_region(unsigned long start, unsigned long n)
325 return release_resource(&synth_res);
327 EXPORT_SYMBOL_GPL(synth_release_region);
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 } },
337 /* called by: speakup_init() */
338 int synth_init(char *synth_name)
342 struct spk_synth *synth = NULL;
347 if (strcmp(synth_name, "none") == 0) {
348 mutex_lock(&spk_mutex);
350 mutex_unlock(&spk_mutex);
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)
360 /* If we got one, initialize it now. */
362 ret = do_synth_init(synth);
365 mutex_unlock(&spk_mutex);
370 /* called by: synth_add() */
371 static int do_synth_init(struct spk_synth *in_synth)
376 if (in_synth->checkval != SYNTH_CHECK)
380 pr_warn("synth probe\n");
381 if (synth->probe(synth) < 0) {
382 pr_warn("%s: device probe failed\n", in_synth->name);
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);
399 synth_printf("%s found\n", synth->long_name);
400 if (synth->attributes.name &&
401 sysfs_create_group(speakup_kobj, &synth->attributes) < 0)
403 synth_flags = synth->flags;
404 wake_up_interruptible_all(&speakup_event);
406 wake_up_process(speakup_task);
410 void synth_release(void)
417 spin_lock_irqsave(&speakup_info.spinlock, flags);
418 pr_info("releasing synth %s\n", synth->name);
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);
430 /* called by: all_driver_init() */
431 int synth_add(struct spk_synth *in_synth)
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);
443 if (i == MAXSYNTHS) {
444 pr_warn("Error: attempting to add a synth past end of array\n");
445 mutex_unlock(&spk_mutex);
449 if (in_synth->startup)
450 status = do_synth_init(in_synth);
453 synths[i++] = in_synth;
457 mutex_unlock(&spk_mutex);
460 EXPORT_SYMBOL_GPL(synth_add);
462 void synth_remove(struct spk_synth *in_synth)
466 mutex_lock(&spk_mutex);
467 if (synth == in_synth)
469 for (i = 0; synths[i]; i++) {
470 if (in_synth == synths[i])
473 for ( ; synths[i]; i++) /* compress table */
474 synths[i] = synths[i + 1];
476 mutex_unlock(&spk_mutex);
478 EXPORT_SYMBOL_GPL(synth_remove);
480 short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };