x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / staging / speakup / synth.c
bloba61c02ba06daec5e051eea3a1980c0b199626b1c
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>
16 #include "spk_priv.h"
17 #include "speakup.h"
18 #include "serialio.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;
25 bool spk_quiet_boot;
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),
41 .flushing = 0,
43 EXPORT_SYMBOL_GPL(speakup_info);
45 static int do_synth_init(struct spk_synth *in_synth);
47 int spk_serial_synth_probe(struct spk_synth *synth)
49 const struct old_serial_port *ser;
50 int failed = 0;
52 if ((synth->ser >= SPK_LO_TTY) && (synth->ser <= SPK_HI_TTY)) {
53 ser = spk_serial_init(synth->ser);
54 if (ser == NULL) {
55 failed = -1;
56 } else {
57 outb_p(0, ser->port);
58 mdelay(1);
59 outb_p('\r', ser->port);
61 } else {
62 failed = -1;
63 pr_warn("ttyS%i is an invalid port\n", synth->ser);
65 if (failed) {
66 pr_info("%s: not found\n", synth->long_name);
67 return -ENODEV;
69 pr_info("%s: ttyS%i, Driver Version %s\n",
70 synth->long_name, synth->ser, synth->version);
71 synth->alive = 1;
72 return 0;
74 EXPORT_SYMBOL_GPL(spk_serial_synth_probe);
77 * Main loop of the progression thread: keep eating from the buffer
78 * and push to the serial port, waiting as needed
80 * For devices that have a "full" notification mechanism, the driver can
81 * adapt the loop the way they prefer.
83 void spk_do_catch_up(struct spk_synth *synth)
85 u_char ch;
86 unsigned long flags;
87 unsigned long jiff_max;
88 struct var_t *delay_time;
89 struct var_t *full_time;
90 struct var_t *jiffy_delta;
91 int jiffy_delta_val;
92 int delay_time_val;
93 int full_time_val;
95 jiffy_delta = spk_get_var(JIFFY);
96 full_time = spk_get_var(FULL);
97 delay_time = spk_get_var(DELAY);
99 spin_lock_irqsave(&speakup_info.spinlock, flags);
100 jiffy_delta_val = jiffy_delta->u.n.value;
101 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
103 jiff_max = jiffies + jiffy_delta_val;
104 while (!kthread_should_stop()) {
105 spin_lock_irqsave(&speakup_info.spinlock, flags);
106 if (speakup_info.flushing) {
107 speakup_info.flushing = 0;
108 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
109 synth->flush(synth);
110 continue;
112 if (synth_buffer_empty()) {
113 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
114 break;
116 ch = synth_buffer_peek();
117 set_current_state(TASK_INTERRUPTIBLE);
118 full_time_val = full_time->u.n.value;
119 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
120 if (ch == '\n')
121 ch = synth->procspeech;
122 if (!spk_serial_out(ch)) {
123 schedule_timeout(msecs_to_jiffies(full_time_val));
124 continue;
126 if (time_after_eq(jiffies, jiff_max) && (ch == SPACE)) {
127 spin_lock_irqsave(&speakup_info.spinlock, flags);
128 jiffy_delta_val = jiffy_delta->u.n.value;
129 delay_time_val = delay_time->u.n.value;
130 full_time_val = full_time->u.n.value;
131 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
132 if (spk_serial_out(synth->procspeech))
133 schedule_timeout(
134 msecs_to_jiffies(delay_time_val));
135 else
136 schedule_timeout(
137 msecs_to_jiffies(full_time_val));
138 jiff_max = jiffies + jiffy_delta_val;
140 set_current_state(TASK_RUNNING);
141 spin_lock_irqsave(&speakup_info.spinlock, flags);
142 synth_buffer_getc();
143 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
145 spk_serial_out(synth->procspeech);
147 EXPORT_SYMBOL_GPL(spk_do_catch_up);
149 const char *spk_synth_immediate(struct spk_synth *synth, const char *buff)
151 u_char ch;
153 while ((ch = *buff)) {
154 if (ch == '\n')
155 ch = synth->procspeech;
156 if (spk_wait_for_xmitr())
157 outb(ch, speakup_info.port_tts);
158 else
159 return buff;
160 buff++;
162 return NULL;
164 EXPORT_SYMBOL_GPL(spk_synth_immediate);
166 void spk_synth_flush(struct spk_synth *synth)
168 spk_serial_out(synth->clear);
170 EXPORT_SYMBOL_GPL(spk_synth_flush);
172 int spk_synth_is_alive_nop(struct spk_synth *synth)
174 synth->alive = 1;
175 return 1;
177 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
179 int spk_synth_is_alive_restart(struct spk_synth *synth)
181 if (synth->alive)
182 return 1;
183 if (spk_wait_for_xmitr() > 0) {
184 /* restart */
185 synth->alive = 1;
186 synth_printf("%s", synth->init);
187 return 2; /* reenabled */
189 pr_warn("%s: can't restart synth\n", synth->long_name);
190 return 0;
192 EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
194 static void thread_wake_up(u_long data)
196 wake_up_interruptible_all(&speakup_event);
199 static DEFINE_TIMER(thread_timer, thread_wake_up, 0, 0);
201 void synth_start(void)
203 struct var_t *trigger_time;
205 if (!synth->alive) {
206 synth_buffer_clear();
207 return;
209 trigger_time = spk_get_var(TRIGGER);
210 if (!timer_pending(&thread_timer))
211 mod_timer(&thread_timer, jiffies +
212 msecs_to_jiffies(trigger_time->u.n.value));
215 void spk_do_flush(void)
217 if (!synth)
218 return;
220 speakup_info.flushing = 1;
221 synth_buffer_clear();
222 if (synth->alive) {
223 if (spk_pitch_shift) {
224 synth_printf("%s", spk_pitch_buff);
225 spk_pitch_shift = 0;
228 wake_up_interruptible_all(&speakup_event);
229 wake_up_process(speakup_task);
232 void synth_write(const char *buf, size_t count)
234 while (count--)
235 synth_buffer_add(*buf++);
236 synth_start();
239 void synth_printf(const char *fmt, ...)
241 va_list args;
242 unsigned char buf[160], *p;
243 int r;
245 va_start(args, fmt);
246 r = vsnprintf(buf, sizeof(buf), fmt, args);
247 va_end(args);
248 if (r > sizeof(buf) - 1)
249 r = sizeof(buf) - 1;
251 p = buf;
252 while (r--)
253 synth_buffer_add(*p++);
254 synth_start();
256 EXPORT_SYMBOL_GPL(synth_printf);
258 static int index_count;
259 static int sentence_count;
261 void spk_reset_index_count(int sc)
263 static int first = 1;
265 if (first)
266 first = 0;
267 else
268 synth->get_index();
269 index_count = 0;
270 sentence_count = sc;
273 int synth_supports_indexing(void)
275 if (synth->get_index != NULL)
276 return 1;
277 return 0;
280 void synth_insert_next_index(int sent_num)
282 int out;
284 if (synth->alive) {
285 if (sent_num == 0) {
286 synth->indexing.currindex++;
287 index_count++;
288 if (synth->indexing.currindex >
289 synth->indexing.highindex)
290 synth->indexing.currindex =
291 synth->indexing.lowindex;
294 out = synth->indexing.currindex * 10 + sent_num;
295 synth_printf(synth->indexing.command, out, out);
299 void spk_get_index_count(int *linecount, int *sentcount)
301 int ind = synth->get_index();
303 if (ind) {
304 sentence_count = ind % 10;
306 if ((ind / 10) <= synth->indexing.currindex)
307 index_count = synth->indexing.currindex - (ind / 10);
308 else
309 index_count = synth->indexing.currindex
310 - synth->indexing.lowindex
311 + synth->indexing.highindex - (ind / 10) + 1;
313 *sentcount = sentence_count;
314 *linecount = index_count;
317 static struct resource synth_res;
319 int synth_request_region(unsigned long start, unsigned long n)
321 struct resource *parent = &ioport_resource;
323 memset(&synth_res, 0, sizeof(synth_res));
324 synth_res.name = synth->name;
325 synth_res.start = start;
326 synth_res.end = start + n - 1;
327 synth_res.flags = IORESOURCE_BUSY;
328 return request_resource(parent, &synth_res);
330 EXPORT_SYMBOL_GPL(synth_request_region);
332 int synth_release_region(unsigned long start, unsigned long n)
334 return release_resource(&synth_res);
336 EXPORT_SYMBOL_GPL(synth_release_region);
338 struct var_t synth_time_vars[] = {
339 { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL } },
340 { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL } },
341 { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL } },
342 { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL } },
343 V_LAST_VAR
346 /* called by: speakup_init() */
347 int synth_init(char *synth_name)
349 int i;
350 int ret = 0;
351 struct spk_synth *synth = NULL;
353 if (synth_name == NULL)
354 return 0;
356 if (strcmp(synth_name, "none") == 0) {
357 mutex_lock(&spk_mutex);
358 synth_release();
359 mutex_unlock(&spk_mutex);
360 return 0;
363 mutex_lock(&spk_mutex);
364 /* First, check if we already have it loaded. */
365 for (i = 0; i < MAXSYNTHS && synths[i] != NULL; i++)
366 if (strcmp(synths[i]->name, synth_name) == 0)
367 synth = synths[i];
369 /* If we got one, initialize it now. */
370 if (synth)
371 ret = do_synth_init(synth);
372 else
373 ret = -ENODEV;
374 mutex_unlock(&spk_mutex);
376 return ret;
379 /* called by: synth_add() */
380 static int do_synth_init(struct spk_synth *in_synth)
382 struct var_t *var;
384 synth_release();
385 if (in_synth->checkval != SYNTH_CHECK)
386 return -EINVAL;
387 synth = in_synth;
388 synth->alive = 0;
389 pr_warn("synth probe\n");
390 if (synth->probe(synth) < 0) {
391 pr_warn("%s: device probe failed\n", in_synth->name);
392 synth = NULL;
393 return -ENODEV;
395 synth_time_vars[0].u.n.value =
396 synth_time_vars[0].u.n.default_val = synth->delay;
397 synth_time_vars[1].u.n.value =
398 synth_time_vars[1].u.n.default_val = synth->trigger;
399 synth_time_vars[2].u.n.value =
400 synth_time_vars[2].u.n.default_val = synth->jiffies;
401 synth_time_vars[3].u.n.value =
402 synth_time_vars[3].u.n.default_val = synth->full;
403 synth_printf("%s", synth->init);
404 for (var = synth->vars;
405 (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
406 speakup_register_var(var);
407 if (!spk_quiet_boot)
408 synth_printf("%s found\n", synth->long_name);
409 if (synth->attributes.name && sysfs_create_group(speakup_kobj,
410 &synth->attributes) < 0)
411 return -ENOMEM;
412 synth_flags = synth->flags;
413 wake_up_interruptible_all(&speakup_event);
414 if (speakup_task)
415 wake_up_process(speakup_task);
416 return 0;
419 void synth_release(void)
421 struct var_t *var;
422 unsigned long flags;
424 if (synth == NULL)
425 return;
426 spin_lock_irqsave(&speakup_info.spinlock, flags);
427 pr_info("releasing synth %s\n", synth->name);
428 synth->alive = 0;
429 del_timer(&thread_timer);
430 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
431 if (synth->attributes.name)
432 sysfs_remove_group(speakup_kobj, &synth->attributes);
433 for (var = synth->vars; var->var_id != MAXVARS; var++)
434 speakup_unregister_var(var->var_id);
435 spk_stop_serial_interrupt();
436 synth->release();
437 synth = NULL;
440 /* called by: all_driver_init() */
441 int synth_add(struct spk_synth *in_synth)
443 int i;
444 int status = 0;
446 mutex_lock(&spk_mutex);
447 for (i = 0; i < MAXSYNTHS && synths[i] != NULL; i++)
448 /* synth_remove() is responsible for rotating the array down */
449 if (in_synth == synths[i]) {
450 mutex_unlock(&spk_mutex);
451 return 0;
453 if (i == MAXSYNTHS) {
454 pr_warn("Error: attempting to add a synth past end of array\n");
455 mutex_unlock(&spk_mutex);
456 return -1;
458 synths[i++] = in_synth;
459 synths[i] = NULL;
460 if (in_synth->startup)
461 status = do_synth_init(in_synth);
462 mutex_unlock(&spk_mutex);
463 return status;
465 EXPORT_SYMBOL_GPL(synth_add);
467 void synth_remove(struct spk_synth *in_synth)
469 int i;
471 mutex_lock(&spk_mutex);
472 if (synth == in_synth)
473 synth_release();
474 for (i = 0; synths[i] != NULL; i++) {
475 if (in_synth == synths[i])
476 break;
478 for ( ; synths[i] != NULL; i++) /* compress table */
479 synths[i] = synths[i + 1];
480 module_status = 0;
481 mutex_unlock(&spk_mutex);
483 EXPORT_SYMBOL_GPL(synth_remove);
485 short spk_punc_masks[] = { 0, SOME, MOST, PUNC, PUNC | B_SYM };