Staging: add speakup to the staging directory
[deliverable/linux.git] / drivers / staging / speakup / synth.c
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. */
21 static struct spk_synth *synths[MAXSYNTHS];
22 struct spk_synth *synth = NULL;
23 char pitch_buff[32] = "";
24 static int module_status;
25 int quiet_boot;
26
27 struct speakup_info_t speakup_info = {
28 .spinlock = SPIN_LOCK_UNLOCKED,
29 .flushing = 0,
30 };
31 EXPORT_SYMBOL_GPL(speakup_info);
32
33 static int do_synth_init(struct spk_synth *in_synth);
34
35 int serial_synth_probe(struct spk_synth *synth)
36 {
37 struct serial_state *ser;
38 int failed = 0;
39
40 if ((synth->ser >= SPK_LO_TTY) && (synth->ser <= SPK_HI_TTY)) {
41 ser = spk_serial_init(synth->ser);
42 if (ser == NULL) {
43 failed = -1;
44 } else {
45 outb_p(0, ser->port);
46 mdelay(1);
47 outb_p('\r', ser->port);
48 }
49 } else {
50 failed = -1;
51 pr_warn("ttyS%i is an invalid port\n", synth->ser);
52 }
53 if (failed) {
54 pr_info("%s: not found\n", synth->long_name);
55 return -ENODEV;
56 }
57 pr_info("%s: ttyS%i, Driver Version %s\n",
58 synth->long_name, synth->ser, synth->version);
59 synth->alive = 1;
60 return 0;
61 }
62 EXPORT_SYMBOL_GPL(serial_synth_probe);
63
64 /* Main loop of the progression thread: keep eating from the buffer
65 * and push to the serial port, waiting as needed
66 *
67 * For devices that have a "full" notification mecanism, the driver can
68 * adapt the loop the way they prefer.
69 */
70 void spk_do_catch_up(struct spk_synth *synth)
71 {
72 u_char ch;
73 unsigned long flags;
74 unsigned long jiff_max;
75 struct var_t *delay_time;
76 struct var_t *full_time;
77 struct var_t *jiffy_delta;
78 int jiffy_delta_val;
79 int delay_time_val;
80 int full_time_val;
81
82 jiffy_delta = get_var(JIFFY);
83 full_time = get_var(FULL);
84 delay_time = get_var(DELAY);
85
86 spk_lock(flags);
87 jiffy_delta_val = jiffy_delta->u.n.value;
88 spk_unlock(flags);
89
90 jiff_max = jiffies + jiffy_delta_val;
91 while (!kthread_should_stop()) {
92 spk_lock(flags);
93 if (speakup_info.flushing) {
94 speakup_info.flushing = 0;
95 spk_unlock(flags);
96 synth->flush(synth);
97 continue;
98 }
99 if (synth_buffer_empty()) {
100 spk_unlock(flags);
101 break;
102 }
103 ch = synth_buffer_peek();
104 set_current_state(TASK_INTERRUPTIBLE);
105 full_time_val = full_time->u.n.value;
106 spk_unlock(flags);
107 if (ch == '\n')
108 ch = synth->procspeech;
109 if (!spk_serial_out(ch)) {
110 schedule_timeout(msecs_to_jiffies(full_time_val));
111 continue;
112 }
113 if ((jiffies >= jiff_max) && (ch == SPACE)) {
114 spk_lock(flags);
115 jiffy_delta_val = jiffy_delta->u.n.value;
116 delay_time_val = delay_time->u.n.value;
117 full_time_val = full_time->u.n.value;
118 spk_unlock(flags);
119 if (spk_serial_out(synth->procspeech))
120 schedule_timeout(msecs_to_jiffies(delay_time_val));
121 else
122 schedule_timeout(msecs_to_jiffies(full_time_val));
123 jiff_max = jiffies + jiffy_delta_val;
124 }
125 set_current_state(TASK_RUNNING);
126 spk_lock(flags);
127 synth_buffer_getc();
128 spk_unlock(flags);
129 }
130 spk_serial_out(synth->procspeech);
131 }
132 EXPORT_SYMBOL_GPL(spk_do_catch_up);
133
134 const char *spk_synth_immediate(struct spk_synth *synth, const char *buff)
135 {
136 u_char ch;
137 while ((ch = *buff)) {
138 if (ch == '\n')
139 ch = synth->procspeech;
140 if (wait_for_xmitr())
141 outb(ch, speakup_info.port_tts);
142 else
143 return buff;
144 buff++;
145 }
146 return 0;
147 }
148 EXPORT_SYMBOL_GPL(spk_synth_immediate);
149
150 void spk_synth_flush(struct spk_synth *synth)
151 {
152 spk_serial_out(synth->clear);
153 }
154 EXPORT_SYMBOL_GPL(spk_synth_flush);
155
156 int spk_synth_is_alive_nop(struct spk_synth *synth)
157 {
158 synth->alive = 1;
159 return 1;
160 }
161 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop);
162
163 int spk_synth_is_alive_restart(struct spk_synth *synth)
164 {
165 if (synth->alive)
166 return 1;
167 if (!synth->alive && wait_for_xmitr() > 0) {
168 /* restart */
169 synth->alive = 1;
170 synth_printf("%s", synth->init);
171 return 2; /* reenabled */
172 }
173 pr_warn("%s: can't restart synth\n", synth->long_name);
174 return 0;
175 }
176 EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart);
177
178 static void thread_wake_up(u_long data)
179 {
180 wake_up_interruptible_all(&speakup_event);
181 }
182
183 static DEFINE_TIMER(thread_timer, thread_wake_up, 0, 0);
184
185 void synth_start(void)
186 {
187 struct var_t *trigger_time;
188
189 if (!synth->alive) {
190 synth_buffer_clear();
191 return;
192 }
193 trigger_time = get_var(TRIGGER);
194 if (!timer_pending(&thread_timer))
195 mod_timer(&thread_timer, jiffies + msecs_to_jiffies(trigger_time->u.n.value));
196 }
197
198 void do_flush(void)
199 {
200 speakup_info.flushing = 1;
201 synth_buffer_clear();
202 if (synth->alive) {
203 if (pitch_shift) {
204 synth_printf("%s", pitch_buff);
205 pitch_shift = 0;
206 }
207 }
208 wake_up_interruptible_all(&speakup_event);
209 wake_up_process(speakup_task);
210 }
211
212 void synth_write(const char *buf, size_t count)
213 {
214 while (count--)
215 synth_buffer_add(*buf++);
216 synth_start();
217 }
218
219 void synth_printf(const char *fmt, ...)
220 {
221 va_list args;
222 unsigned char buf[160], *p;
223 int r;
224
225 va_start(args, fmt);
226 r = vsnprintf(buf, sizeof(buf), fmt, args);
227 va_end(args);
228 if (r > sizeof(buf) - 1)
229 r = sizeof(buf) - 1;
230
231 p = buf;
232 while (r--)
233 synth_buffer_add(*p++);
234 synth_start();
235 }
236 EXPORT_SYMBOL_GPL(synth_printf);
237
238 static int index_count = 0;
239 static int sentence_count = 0;
240
241 void reset_index_count(int sc)
242 {
243 static int first = 1;
244 if (first)
245 first = 0;
246 else
247 synth->get_index();
248 index_count = 0;
249 sentence_count = sc;
250 }
251
252 int synth_supports_indexing(void)
253 {
254 if (synth->get_index != NULL)
255 return 1;
256 return 0;
257 }
258
259 void synth_insert_next_index(int sent_num)
260 {
261 int out;
262 if (synth->alive) {
263 if (sent_num == 0) {
264 synth->indexing.currindex++;
265 index_count++;
266 if (synth->indexing.currindex >
267 synth->indexing.highindex)
268 synth->indexing.currindex =
269 synth->indexing.lowindex;
270 }
271
272 out = synth->indexing.currindex * 10 + sent_num;
273 synth_printf(synth->indexing.command, out, out);
274 }
275 }
276
277 void get_index_count(int *linecount, int *sentcount)
278 {
279 int ind = synth->get_index();
280 if (ind) {
281 sentence_count = ind % 10;
282
283 if ((ind / 10) <= synth->indexing.currindex)
284 index_count = synth->indexing.currindex-(ind/10);
285 else
286 index_count = synth->indexing.currindex-synth->indexing.lowindex
287 + synth->indexing.highindex-(ind/10)+1;
288
289 }
290 *sentcount = sentence_count;
291 *linecount = index_count;
292 }
293
294 static struct resource synth_res;
295
296 int synth_request_region(unsigned long start, unsigned long n)
297 {
298 struct resource *parent = &ioport_resource;
299 memset(&synth_res, 0, sizeof(synth_res));
300 synth_res.name = synth->name;
301 synth_res.start = start;
302 synth_res.end = start + n - 1;
303 synth_res.flags = IORESOURCE_BUSY;
304 return request_resource(parent, &synth_res);
305 }
306 EXPORT_SYMBOL_GPL(synth_request_region);
307
308 int synth_release_region(unsigned long start, unsigned long n)
309 {
310 return release_resource(&synth_res);
311 }
312 EXPORT_SYMBOL_GPL(synth_release_region);
313
314 struct var_t synth_time_vars[] = {
315 { DELAY, .u.n = {NULL, 100, 100, 2000, 0, 0, NULL }},
316 { TRIGGER, .u.n = {NULL, 20, 10, 2000, 0, 0, NULL }},
317 { JIFFY, .u.n = {NULL, 50, 20, 200, 0, 0, NULL }},
318 { FULL, .u.n = {NULL, 400, 200, 60000, 0, 0, NULL }},
319 V_LAST_VAR
320 };
321
322 /* called by: speakup_init() */
323 int synth_init(char *synth_name)
324 {
325 int i;
326 int ret = 0;
327 struct spk_synth *synth = NULL;
328
329 if (synth_name == NULL)
330 return 0;
331
332 if (strcmp(synth_name, "none") == 0) {
333 mutex_lock(&spk_mutex);
334 synth_release();
335 mutex_unlock(&spk_mutex);
336 return 0;
337 }
338
339 mutex_lock(&spk_mutex);
340 /* First, check if we already have it loaded. */
341 for (i = 0; synths[i] != NULL && i < MAXSYNTHS; i++)
342 if (strcmp(synths[i]->name, synth_name) == 0)
343 synth = synths[i];
344
345 /* If we got one, initialize it now. */
346 if (synth)
347 ret = do_synth_init(synth);
348 else
349 ret = -ENODEV;
350 mutex_unlock(&spk_mutex);
351
352 return ret;
353 }
354
355 /* called by: synth_add() */
356 static int do_synth_init(struct spk_synth *in_synth)
357 {
358 struct var_t *var;
359
360 synth_release();
361 if (in_synth->checkval != SYNTH_CHECK)
362 return -EINVAL;
363 synth = in_synth;
364 synth->alive = 0;
365 pr_warn("synth probe\n");
366 if (synth->probe(synth) < 0) {
367 pr_warn("%s: device probe failed\n", in_synth->name);
368 synth = NULL;
369 return -ENODEV;
370 }
371 synth_time_vars[0].u.n.value =
372 synth_time_vars[0].u.n.default_val = synth->delay;
373 synth_time_vars[1].u.n.value =
374 synth_time_vars[1].u.n.default_val = synth->trigger;
375 synth_time_vars[2].u.n.value =
376 synth_time_vars[2].u.n.default_val = synth->jiffies;
377 synth_time_vars[3].u.n.value =
378 synth_time_vars[3].u.n.default_val = synth->full;
379 synth_printf("%s", synth->init);
380 for (var = synth->vars; (var->var_id >= 0) && (var->var_id < MAXVARS); var++)
381 speakup_register_var(var);
382 if (!quiet_boot)
383 synth_printf("%s found\n", synth->long_name);
384 if (synth->attributes.name
385 && sysfs_create_group(speakup_kobj, &(synth->attributes)) < 0)
386 return -ENOMEM;
387 synth_flags = synth->flags;
388 wake_up_interruptible_all(&speakup_event);
389 if (speakup_task)
390 wake_up_process(speakup_task);
391 return 0;
392 }
393
394 void synth_release(void)
395 {
396 struct var_t *var;
397 unsigned long flags;
398
399 if (synth == NULL)
400 return;
401 spk_lock(flags);
402 pr_info("releasing synth %s\n", synth->name);
403 synth->alive = 0;
404 del_timer(&thread_timer);
405 spk_unlock(flags);
406 if (synth->attributes.name)
407 sysfs_remove_group(speakup_kobj, &(synth->attributes));
408 for (var = synth->vars; var->var_id != MAXVARS; var++)
409 speakup_unregister_var(var->var_id);
410 stop_serial_interrupt();
411 synth->release();
412 synth = NULL;
413 }
414
415 /* called by: all_driver_init() */
416 int synth_add(struct spk_synth *in_synth)
417 {
418 int i;
419 int status = 0;
420 mutex_lock(&spk_mutex);
421 for (i = 0; synths[i] != NULL && i < MAXSYNTHS; i++)
422 /* synth_remove() is responsible for rotating the array down */
423 if (in_synth == synths[i]) {
424 mutex_unlock(&spk_mutex);
425 return 0;
426 }
427 if (i == MAXSYNTHS) {
428 pr_warn("Error: attempting to add a synth past end of array\n");
429 mutex_unlock(&spk_mutex);
430 return -1;
431 }
432 synths[i++] = in_synth;
433 synths[i] = NULL;
434 if (in_synth->startup)
435 status = do_synth_init(in_synth);
436 mutex_unlock(&spk_mutex);
437 return status;
438 }
439 EXPORT_SYMBOL_GPL(synth_add);
440
441 void synth_remove(struct spk_synth *in_synth)
442 {
443 int i;
444 mutex_lock(&spk_mutex);
445 if (synth == in_synth)
446 synth_release();
447 for (i = 0; synths[i] != NULL; i++) {
448 if (in_synth == synths[i])
449 break;
450 }
451 for ( ; synths[i] != NULL; i++) /* compress table */
452 synths[i] = synths[i+1];
453 module_status = 0;
454 mutex_unlock(&spk_mutex);
455 }
456 EXPORT_SYMBOL_GPL(synth_remove);
457
458 short punc_masks[] = { 0, SOME, MOST, PUNC, PUNC|B_SYM };
This page took 0.051157 seconds and 5 git commands to generate.