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
];
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 through
34 * the synth buffer, so only needs to take the lock while tinkering with
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
);
47 int spk_serial_synth_probe(struct spk_synth
*synth
)
49 const struct old_serial_port
*ser
;
52 if ((synth
->ser
>= SPK_LO_TTY
) && (synth
->ser
<= SPK_HI_TTY
)) {
53 ser
= spk_serial_init(synth
->ser
);
59 outb_p('\r', ser
->port
);
63 pr_warn("ttyS%i is an invalid port\n", synth
->ser
);
66 pr_info("%s: not found\n", synth
->long_name
);
69 pr_info("%s: ttyS%i, Driver Version %s\n",
70 synth
->long_name
, synth
->ser
, synth
->version
);
74 EXPORT_SYMBOL_GPL(spk_serial_synth_probe
);
76 /* Main loop of the progression thread: keep eating from the buffer
77 * and push to the serial port, waiting as needed
79 * For devices that have a "full" notification mechanism, the driver can
80 * adapt the loop the way they prefer.
82 void spk_do_catch_up(struct spk_synth
*synth
)
86 unsigned long jiff_max
;
87 struct var_t
*delay_time
;
88 struct var_t
*full_time
;
89 struct var_t
*jiffy_delta
;
94 jiffy_delta
= spk_get_var(JIFFY
);
95 full_time
= spk_get_var(FULL
);
96 delay_time
= spk_get_var(DELAY
);
98 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
99 jiffy_delta_val
= jiffy_delta
->u
.n
.value
;
100 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
102 jiff_max
= jiffies
+ jiffy_delta_val
;
103 while (!kthread_should_stop()) {
104 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
105 if (speakup_info
.flushing
) {
106 speakup_info
.flushing
= 0;
107 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
111 if (synth_buffer_empty()) {
112 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
115 ch
= synth_buffer_peek();
116 set_current_state(TASK_INTERRUPTIBLE
);
117 full_time_val
= full_time
->u
.n
.value
;
118 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
120 ch
= synth
->procspeech
;
121 if (!spk_serial_out(ch
)) {
122 schedule_timeout(msecs_to_jiffies(full_time_val
));
125 if ((jiffies
>= jiff_max
) && (ch
== SPACE
)) {
126 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
127 jiffy_delta_val
= jiffy_delta
->u
.n
.value
;
128 delay_time_val
= delay_time
->u
.n
.value
;
129 full_time_val
= full_time
->u
.n
.value
;
130 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
131 if (spk_serial_out(synth
->procspeech
))
133 msecs_to_jiffies(delay_time_val
));
136 msecs_to_jiffies(full_time_val
));
137 jiff_max
= jiffies
+ jiffy_delta_val
;
139 set_current_state(TASK_RUNNING
);
140 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
142 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
144 spk_serial_out(synth
->procspeech
);
146 EXPORT_SYMBOL_GPL(spk_do_catch_up
);
148 const char *spk_synth_immediate(struct spk_synth
*synth
, const char *buff
)
151 while ((ch
= *buff
)) {
153 ch
= synth
->procspeech
;
154 if (spk_wait_for_xmitr())
155 outb(ch
, speakup_info
.port_tts
);
162 EXPORT_SYMBOL_GPL(spk_synth_immediate
);
164 void spk_synth_flush(struct spk_synth
*synth
)
166 spk_serial_out(synth
->clear
);
168 EXPORT_SYMBOL_GPL(spk_synth_flush
);
170 int spk_synth_is_alive_nop(struct spk_synth
*synth
)
175 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop
);
177 int spk_synth_is_alive_restart(struct spk_synth
*synth
)
181 if (!synth
->alive
&& spk_wait_for_xmitr() > 0) {
184 synth_printf("%s", synth
->init
);
185 return 2; /* reenabled */
187 pr_warn("%s: can't restart synth\n", synth
->long_name
);
190 EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart
);
192 static void thread_wake_up(u_long data
)
194 wake_up_interruptible_all(&speakup_event
);
197 static DEFINE_TIMER(thread_timer
, thread_wake_up
, 0, 0);
199 void synth_start(void)
201 struct var_t
*trigger_time
;
204 synth_buffer_clear();
207 trigger_time
= spk_get_var(TRIGGER
);
208 if (!timer_pending(&thread_timer
))
209 mod_timer(&thread_timer
, jiffies
+
210 msecs_to_jiffies(trigger_time
->u
.n
.value
));
213 void spk_do_flush(void)
215 speakup_info
.flushing
= 1;
216 synth_buffer_clear();
218 if (spk_pitch_shift
) {
219 synth_printf("%s", spk_pitch_buff
);
223 wake_up_interruptible_all(&speakup_event
);
224 wake_up_process(speakup_task
);
227 void synth_write(const char *buf
, size_t count
)
230 synth_buffer_add(*buf
++);
234 void synth_printf(const char *fmt
, ...)
237 unsigned char buf
[160], *p
;
241 r
= vsnprintf(buf
, sizeof(buf
), fmt
, args
);
243 if (r
> sizeof(buf
) - 1)
248 synth_buffer_add(*p
++);
251 EXPORT_SYMBOL_GPL(synth_printf
);
253 static int index_count
;
254 static int sentence_count
;
256 void spk_reset_index_count(int sc
)
258 static int first
= 1;
267 int synth_supports_indexing(void)
269 if (synth
->get_index
!= NULL
)
274 void synth_insert_next_index(int sent_num
)
279 synth
->indexing
.currindex
++;
281 if (synth
->indexing
.currindex
>
282 synth
->indexing
.highindex
)
283 synth
->indexing
.currindex
=
284 synth
->indexing
.lowindex
;
287 out
= synth
->indexing
.currindex
* 10 + sent_num
;
288 synth_printf(synth
->indexing
.command
, out
, out
);
292 void spk_get_index_count(int *linecount
, int *sentcount
)
294 int ind
= synth
->get_index();
296 sentence_count
= ind
% 10;
298 if ((ind
/ 10) <= synth
->indexing
.currindex
)
299 index_count
= synth
->indexing
.currindex
-(ind
/10);
301 index_count
= synth
->indexing
.currindex
302 -synth
->indexing
.lowindex
303 + synth
->indexing
.highindex
-(ind
/10)+1;
306 *sentcount
= sentence_count
;
307 *linecount
= index_count
;
310 static struct resource synth_res
;
312 int synth_request_region(unsigned long start
, unsigned long n
)
314 struct resource
*parent
= &ioport_resource
;
315 memset(&synth_res
, 0, sizeof(synth_res
));
316 synth_res
.name
= synth
->name
;
317 synth_res
.start
= start
;
318 synth_res
.end
= start
+ n
- 1;
319 synth_res
.flags
= IORESOURCE_BUSY
;
320 return request_resource(parent
, &synth_res
);
322 EXPORT_SYMBOL_GPL(synth_request_region
);
324 int synth_release_region(unsigned long start
, unsigned long n
)
326 return release_resource(&synth_res
);
328 EXPORT_SYMBOL_GPL(synth_release_region
);
330 struct var_t synth_time_vars
[] = {
331 { DELAY
, .u
.n
= {NULL
, 100, 100, 2000, 0, 0, NULL
} },
332 { TRIGGER
, .u
.n
= {NULL
, 20, 10, 2000, 0, 0, NULL
} },
333 { JIFFY
, .u
.n
= {NULL
, 50, 20, 200, 0, 0, NULL
} },
334 { FULL
, .u
.n
= {NULL
, 400, 200, 60000, 0, 0, NULL
} },
338 /* called by: speakup_init() */
339 int synth_init(char *synth_name
)
343 struct spk_synth
*synth
= NULL
;
345 if (synth_name
== NULL
)
348 if (strcmp(synth_name
, "none") == 0) {
349 mutex_lock(&spk_mutex
);
351 mutex_unlock(&spk_mutex
);
355 mutex_lock(&spk_mutex
);
356 /* First, check if we already have it loaded. */
357 for (i
= 0; i
< MAXSYNTHS
&& synths
[i
] != NULL
; i
++)
358 if (strcmp(synths
[i
]->name
, synth_name
) == 0)
361 /* If we got one, initialize it now. */
363 ret
= do_synth_init(synth
);
366 mutex_unlock(&spk_mutex
);
371 /* called by: synth_add() */
372 static int do_synth_init(struct spk_synth
*in_synth
)
377 if (in_synth
->checkval
!= SYNTH_CHECK
)
381 pr_warn("synth probe\n");
382 if (synth
->probe(synth
) < 0) {
383 pr_warn("%s: device probe failed\n", in_synth
->name
);
387 synth_time_vars
[0].u
.n
.value
=
388 synth_time_vars
[0].u
.n
.default_val
= synth
->delay
;
389 synth_time_vars
[1].u
.n
.value
=
390 synth_time_vars
[1].u
.n
.default_val
= synth
->trigger
;
391 synth_time_vars
[2].u
.n
.value
=
392 synth_time_vars
[2].u
.n
.default_val
= synth
->jiffies
;
393 synth_time_vars
[3].u
.n
.value
=
394 synth_time_vars
[3].u
.n
.default_val
= synth
->full
;
395 synth_printf("%s", synth
->init
);
396 for (var
= synth
->vars
;
397 (var
->var_id
>= 0) && (var
->var_id
< MAXVARS
); var
++)
398 speakup_register_var(var
);
400 synth_printf("%s found\n", synth
->long_name
);
401 if (synth
->attributes
.name
402 && sysfs_create_group(speakup_kobj
, &(synth
->attributes
)) < 0)
404 synth_flags
= synth
->flags
;
405 wake_up_interruptible_all(&speakup_event
);
407 wake_up_process(speakup_task
);
411 void synth_release(void)
418 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
419 pr_info("releasing synth %s\n", synth
->name
);
421 del_timer(&thread_timer
);
422 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
423 if (synth
->attributes
.name
)
424 sysfs_remove_group(speakup_kobj
, &(synth
->attributes
));
425 for (var
= synth
->vars
; var
->var_id
!= MAXVARS
; var
++)
426 speakup_unregister_var(var
->var_id
);
427 spk_stop_serial_interrupt();
432 /* called by: all_driver_init() */
433 int synth_add(struct spk_synth
*in_synth
)
437 mutex_lock(&spk_mutex
);
438 for (i
= 0; i
< MAXSYNTHS
&& synths
[i
] != NULL
; i
++)
439 /* synth_remove() is responsible for rotating the array down */
440 if (in_synth
== synths
[i
]) {
441 mutex_unlock(&spk_mutex
);
444 if (i
== MAXSYNTHS
) {
445 pr_warn("Error: attempting to add a synth past end of array\n");
446 mutex_unlock(&spk_mutex
);
449 synths
[i
++] = in_synth
;
451 if (in_synth
->startup
)
452 status
= do_synth_init(in_synth
);
453 mutex_unlock(&spk_mutex
);
456 EXPORT_SYMBOL_GPL(synth_add
);
458 void synth_remove(struct spk_synth
*in_synth
)
461 mutex_lock(&spk_mutex
);
462 if (synth
== in_synth
)
464 for (i
= 0; synths
[i
] != NULL
; i
++) {
465 if (in_synth
== synths
[i
])
468 for ( ; synths
[i
] != NULL
; i
++) /* compress table */
469 synths
[i
] = synths
[i
+1];
471 mutex_unlock(&spk_mutex
);
473 EXPORT_SYMBOL_GPL(synth_remove
);
475 short spk_punc_masks
[] = { 0, SOME
, MOST
, PUNC
, PUNC
|B_SYM
};