Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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. */
21static struct spk_synth *synths[MAXSYNTHS + 1];
22struct spk_synth *synth;
23char spk_pitch_buff[32] = "";
24static int module_status;
25bool spk_quiet_boot;
26
27struct 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};
43EXPORT_SYMBOL_GPL(speakup_info);
44
45static 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 */
54void 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}
119EXPORT_SYMBOL_GPL(spk_do_catch_up);
120
121void spk_synth_flush(struct spk_synth *synth)
122{
123 synth->io_ops->synth_out(synth, synth->clear);
124}
125EXPORT_SYMBOL_GPL(spk_synth_flush);
126
127int spk_synth_is_alive_nop(struct spk_synth *synth)
128{
129 synth->alive = 1;
130 return 1;
131}
132EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
133
134int spk_synth_is_alive_restart(struct spk_synth *synth)
135{
136 if (synth->alive)
137 return 1;
138 if (spk_wait_for_xmitr(synth) > 0) {
139 /* restart */
140 synth->alive = 1;
141 synth_printf("%s", synth->init);
142 return 2; /* reenabled */
143 }
144 pr_warn("%s: can't restart synth\n", synth->long_name);
145 return 0;
146}
147EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
148
149static void thread_wake_up(u_long data)
150{
151 wake_up_interruptible_all(&speakup_event);
152}
153
154static DEFINE_TIMER(thread_timer, thread_wake_up, 0, 0);
155
156void synth_start(void)
157{
158 struct var_t *trigger_time;
159
160 if (!synth->alive) {
161 synth_buffer_clear();
162 return;
163 }
164 trigger_time = spk_get_var(TRIGGER);
165 if (!timer_pending(&thread_timer))
166 mod_timer(&thread_timer, jiffies +
167 msecs_to_jiffies(trigger_time->u.n.value));
168}
169
170void spk_do_flush(void)
171{
172 if (!synth)
173 return;
174
175 speakup_info.flushing = 1;
176 synth_buffer_clear();
177 if (synth->alive) {
178 if (spk_pitch_shift) {
179 synth_printf("%s", spk_pitch_buff);
180 spk_pitch_shift = 0;
181 }
182 }
183 wake_up_interruptible_all(&speakup_event);
184 wake_up_process(speakup_task);
185}
186
187void synth_write(const char *buf, size_t count)
188{
189 while (count--)
190 synth_buffer_add(*buf++);
191 synth_start();
192}
193
194void synth_printf(const char *fmt, ...)
195{
196 va_list args;
197 unsigned char buf[160], *p;
198 int r;
199
200 va_start(args, fmt);
201 r = vsnprintf(buf, sizeof(buf), fmt, args);
202 va_end(args);
203 if (r > sizeof(buf) - 1)
204 r = sizeof(buf) - 1;
205
206 p = buf;
207 while (r--)
208 synth_buffer_add(*p++);
209 synth_start();
210}
211EXPORT_SYMBOL_GPL(synth_printf);
212
213void synth_putwc(u16 wc)
214{
215 synth_buffer_add(wc);
216}
217EXPORT_SYMBOL_GPL(synth_putwc);
218
219void synth_putwc_s(u16 wc)
220{
221 synth_buffer_add(wc);
222 synth_start();
223}
224EXPORT_SYMBOL_GPL(synth_putwc_s);
225
226void synth_putws(const u16 *buf)
227{
228 const u16 *p;
229
230 for (p = buf; *p; p++)
231 synth_buffer_add(*p);
232}
233EXPORT_SYMBOL_GPL(synth_putws);
234
235void synth_putws_s(const u16 *buf)
236{
237 synth_putws(buf);
238 synth_start();
239}
240EXPORT_SYMBOL_GPL(synth_putws_s);
241
242static int index_count;
243static int sentence_count;
244
245void spk_reset_index_count(int sc)
246{
247 static int first = 1;
248
249 if (first)
250 first = 0;
251 else
252 synth->get_index();
253 index_count = 0;
254 sentence_count = sc;
255}
256
257int synth_supports_indexing(void)
258{
259 if (synth->get_index)
260 return 1;
261 return 0;
262}
263
264void synth_insert_next_index(int sent_num)
265{
266 int out;
267
268 if (synth->alive) {
269 if (sent_num == 0) {
270 synth->indexing.currindex++;
271 index_count++;
272 if (synth->indexing.currindex >
273 synth->indexing.highindex)
274 synth->indexing.currindex =
275 synth->indexing.lowindex;
276 }
277
278 out = synth->indexing.currindex * 10 + sent_num;
279 synth_printf(synth->indexing.command, out, out);
280 }
281}
282
283void spk_get_index_count(int *linecount, int *sentcount)
284{
285 int ind = synth->get_index();
286
287 if (ind) {
288 sentence_count = ind % 10;
289
290 if ((ind / 10) <= synth->indexing.currindex)
291 index_count = synth->indexing.currindex - (ind / 10);
292 else
293 index_count = synth->indexing.currindex
294 - synth->indexing.lowindex
295 + synth->indexing.highindex - (ind / 10) + 1;
296 }
297 *sentcount = sentence_count;
298 *linecount = index_count;
299}
300
301static struct resource synth_res;
302
303int synth_request_region(unsigned long start, unsigned long n)
304{
305 struct resource *parent = &ioport_resource;
306
307 memset(&synth_res, 0, sizeof(synth_res));
308 synth_res.name = synth->name;
309 synth_res.start = start;
310 synth_res.end = start + n - 1;
311 synth_res.flags = IORESOURCE_BUSY;
312 return request_resource(parent, &synth_res);
313}
314EXPORT_SYMBOL_GPL(synth_request_region);
315
316int synth_release_region(unsigned long start, unsigned long n)
317{
318 return release_resource(&synth_res);
319}
320EXPORT_SYMBOL_GPL(synth_release_region);
321
322struct var_t synth_time_vars[] = {
323 { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } },
324 { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } },
325 { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } },
326 { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } },
327 V_LAST_VAR
328};
329
330/* called by: speakup_init() */
331int synth_init(char *synth_name)
332{
333 int i;
334 int ret = 0;
335 struct spk_synth *synth = NULL;
336
337 if (!synth_name)
338 return 0;
339
340 if (strcmp(synth_name, "none") == 0) {
341 mutex_lock(&spk_mutex);
342 synth_release();
343 mutex_unlock(&spk_mutex);
344 return 0;
345 }
346
347 mutex_lock(&spk_mutex);
348 /* First, check if we already have it loaded. */
349 for (i = 0; i < MAXSYNTHS && synths[i]; i++)
350 if (strcmp(synths[i]->name, synth_name) == 0)
351 synth = synths[i];
352
353 /* If we got one, initialize it now. */
354 if (synth)
355 ret = do_synth_init(synth);
356 else
357 ret = -ENODEV;
358 mutex_unlock(&spk_mutex);
359
360 return ret;
361}
362
363/* called by: synth_add() */
364static int do_synth_init(struct spk_synth *in_synth)
365{
366 struct var_t *var;
367
368 synth_release();
369 if (in_synth->checkval != SYNTH_CHECK)
370 return -EINVAL;
371 synth = in_synth;
372 synth->alive = 0;
373 pr_warn("synth probe\n");
374 if (synth->probe(synth) < 0) {
375 pr_warn("%s: device probe failed\n", in_synth->name);
376 synth = NULL;
377 return -ENODEV;
378 }
379 synth_time_vars[0].u.n.value =
380 synth_time_vars[0].u.n.default_val = synth->delay;
381 synth_time_vars[1].u.n.value =
382 synth_time_vars[1].u.n.default_val = synth->trigger;
383 synth_time_vars[2].u.n.value =
384 synth_time_vars[2].u.n.default_val = synth->jiffies;
385 synth_time_vars[3].u.n.value =
386 synth_time_vars[3].u.n.default_val = synth->full;
387 synth_printf("%s", synth->init);
388 for (var = synth->vars;
389 (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
390 speakup_register_var(var);
391 if (!spk_quiet_boot)
392 synth_printf("%s found\n", synth->long_name);
393 if (synth->attributes.name &&
394 sysfs_create_group(speakup_kobj, &synth->attributes) < 0)
395 return -ENOMEM;
396 synth_flags = synth->flags;
397 wake_up_interruptible_all(&speakup_event);
398 if (speakup_task)
399 wake_up_process(speakup_task);
400 return 0;
401}
402
403void synth_release(void)
404{
405 struct var_t *var;
406 unsigned long flags;
407
408 if (!synth)
409 return;
410 spin_lock_irqsave(&speakup_info.spinlock, flags);
411 pr_info("releasing synth %s\n", synth->name);
412 synth->alive = 0;
413 del_timer(&thread_timer);
414 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
415 if (synth->attributes.name)
416 sysfs_remove_group(speakup_kobj, &synth->attributes);
417 for (var = synth->vars; var->var_id != MAXVARS; var++)
418 speakup_unregister_var(var->var_id);
419 synth->release();
420 synth = NULL;
421}
422
423/* called by: all_driver_init() */
424int synth_add(struct spk_synth *in_synth)
425{
426 int i;
427 int status = 0;
428
429 mutex_lock(&spk_mutex);
430 for (i = 0; i < MAXSYNTHS && synths[i]; i++)
431 /* synth_remove() is responsible for rotating the array down */
432 if (in_synth == synths[i]) {
433 mutex_unlock(&spk_mutex);
434 return 0;
435 }
436 if (i == MAXSYNTHS) {
437 pr_warn("Error: attempting to add a synth past end of array\n");
438 mutex_unlock(&spk_mutex);
439 return -1;
440 }
441 synths[i++] = in_synth;
442 synths[i] = NULL;
443 if (in_synth->startup)
444 status = do_synth_init(in_synth);
445 mutex_unlock(&spk_mutex);
446 return status;
447}
448EXPORT_SYMBOL_GPL(synth_add);
449
450void synth_remove(struct spk_synth *in_synth)
451{
452 int i;
453
454 mutex_lock(&spk_mutex);
455 if (synth == in_synth)
456 synth_release();
457 for (i = 0; synths[i]; i++) {
458 if (in_synth == synths[i])
459 break;
460 }
461 for ( ; synths[i]; i++) /* compress table */
462 synths[i] = synths[i + 1];
463 module_status = 0;
464 mutex_unlock(&spk_mutex);
465}
466EXPORT_SYMBOL_GPL(synth_remove);
467
468short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };