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 pitch_buff
[32] = "";
24 static int module_status
;
27 struct speakup_info_t speakup_info
= {
28 .spinlock
= __SPIN_LOCK_UNLOCKED(speakup_info
.spinlock
),
31 EXPORT_SYMBOL_GPL(speakup_info
);
33 static int do_synth_init(struct spk_synth
*in_synth
);
35 int serial_synth_probe(struct spk_synth
*synth
)
37 struct serial_state
*ser
;
40 if ((synth
->ser
>= SPK_LO_TTY
) && (synth
->ser
<= SPK_HI_TTY
)) {
41 ser
= spk_serial_init(synth
->ser
);
47 outb_p('\r', ser
->port
);
51 pr_warn("ttyS%i is an invalid port\n", synth
->ser
);
54 pr_info("%s: not found\n", synth
->long_name
);
57 pr_info("%s: ttyS%i, Driver Version %s\n",
58 synth
->long_name
, synth
->ser
, synth
->version
);
62 EXPORT_SYMBOL_GPL(serial_synth_probe
);
64 /* Main loop of the progression thread: keep eating from the buffer
65 * and push to the serial port, waiting as needed
67 * For devices that have a "full" notification mecanism, the driver can
68 * adapt the loop the way they prefer.
70 void spk_do_catch_up(struct spk_synth
*synth
)
74 unsigned long jiff_max
;
75 struct var_t
*delay_time
;
76 struct var_t
*full_time
;
77 struct var_t
*jiffy_delta
;
82 jiffy_delta
= get_var(JIFFY
);
83 full_time
= get_var(FULL
);
84 delay_time
= get_var(DELAY
);
87 jiffy_delta_val
= jiffy_delta
->u
.n
.value
;
90 jiff_max
= jiffies
+ jiffy_delta_val
;
91 while (!kthread_should_stop()) {
93 if (speakup_info
.flushing
) {
94 speakup_info
.flushing
= 0;
99 if (synth_buffer_empty()) {
103 ch
= synth_buffer_peek();
104 set_current_state(TASK_INTERRUPTIBLE
);
105 full_time_val
= full_time
->u
.n
.value
;
108 ch
= synth
->procspeech
;
109 if (!spk_serial_out(ch
)) {
110 schedule_timeout(msecs_to_jiffies(full_time_val
));
113 if ((jiffies
>= jiff_max
) && (ch
== SPACE
)) {
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
;
119 if (spk_serial_out(synth
->procspeech
))
121 msecs_to_jiffies(delay_time_val
));
124 msecs_to_jiffies(full_time_val
));
125 jiff_max
= jiffies
+ jiffy_delta_val
;
127 set_current_state(TASK_RUNNING
);
132 spk_serial_out(synth
->procspeech
);
134 EXPORT_SYMBOL_GPL(spk_do_catch_up
);
136 const char *spk_synth_immediate(struct spk_synth
*synth
, const char *buff
)
139 while ((ch
= *buff
)) {
141 ch
= synth
->procspeech
;
142 if (wait_for_xmitr())
143 outb(ch
, speakup_info
.port_tts
);
150 EXPORT_SYMBOL_GPL(spk_synth_immediate
);
152 void spk_synth_flush(struct spk_synth
*synth
)
154 spk_serial_out(synth
->clear
);
156 EXPORT_SYMBOL_GPL(spk_synth_flush
);
158 int spk_synth_is_alive_nop(struct spk_synth
*synth
)
163 EXPORT_SYMBOL_GPL(spk_synth_is_alive_nop
);
165 int spk_synth_is_alive_restart(struct spk_synth
*synth
)
169 if (!synth
->alive
&& wait_for_xmitr() > 0) {
172 synth_printf("%s", synth
->init
);
173 return 2; /* reenabled */
175 pr_warn("%s: can't restart synth\n", synth
->long_name
);
178 EXPORT_SYMBOL_GPL(spk_synth_is_alive_restart
);
180 static void thread_wake_up(u_long data
)
182 wake_up_interruptible_all(&speakup_event
);
185 static DEFINE_TIMER(thread_timer
, thread_wake_up
, 0, 0);
187 void synth_start(void)
189 struct var_t
*trigger_time
;
192 synth_buffer_clear();
195 trigger_time
= get_var(TRIGGER
);
196 if (!timer_pending(&thread_timer
))
197 mod_timer(&thread_timer
, jiffies
+
198 msecs_to_jiffies(trigger_time
->u
.n
.value
));
203 speakup_info
.flushing
= 1;
204 synth_buffer_clear();
207 synth_printf("%s", pitch_buff
);
211 wake_up_interruptible_all(&speakup_event
);
212 wake_up_process(speakup_task
);
215 void synth_write(const char *buf
, size_t count
)
218 synth_buffer_add(*buf
++);
222 void synth_printf(const char *fmt
, ...)
225 unsigned char buf
[160], *p
;
229 r
= vsnprintf(buf
, sizeof(buf
), fmt
, args
);
231 if (r
> sizeof(buf
) - 1)
236 synth_buffer_add(*p
++);
239 EXPORT_SYMBOL_GPL(synth_printf
);
241 static int index_count
;
242 static int sentence_count
;
244 void reset_index_count(int sc
)
246 static int first
= 1;
255 int synth_supports_indexing(void)
257 if (synth
->get_index
!= NULL
)
262 void synth_insert_next_index(int sent_num
)
267 synth
->indexing
.currindex
++;
269 if (synth
->indexing
.currindex
>
270 synth
->indexing
.highindex
)
271 synth
->indexing
.currindex
=
272 synth
->indexing
.lowindex
;
275 out
= synth
->indexing
.currindex
* 10 + sent_num
;
276 synth_printf(synth
->indexing
.command
, out
, out
);
280 void get_index_count(int *linecount
, int *sentcount
)
282 int ind
= synth
->get_index();
284 sentence_count
= ind
% 10;
286 if ((ind
/ 10) <= synth
->indexing
.currindex
)
287 index_count
= synth
->indexing
.currindex
-(ind
/10);
289 index_count
= synth
->indexing
.currindex
290 -synth
->indexing
.lowindex
291 + synth
->indexing
.highindex
-(ind
/10)+1;
294 *sentcount
= sentence_count
;
295 *linecount
= index_count
;
298 static struct resource synth_res
;
300 int synth_request_region(unsigned long start
, unsigned long n
)
302 struct resource
*parent
= &ioport_resource
;
303 memset(&synth_res
, 0, sizeof(synth_res
));
304 synth_res
.name
= synth
->name
;
305 synth_res
.start
= start
;
306 synth_res
.end
= start
+ n
- 1;
307 synth_res
.flags
= IORESOURCE_BUSY
;
308 return request_resource(parent
, &synth_res
);
310 EXPORT_SYMBOL_GPL(synth_request_region
);
312 int synth_release_region(unsigned long start
, unsigned long n
)
314 return release_resource(&synth_res
);
316 EXPORT_SYMBOL_GPL(synth_release_region
);
318 struct var_t synth_time_vars
[] = {
319 { DELAY
, .u
.n
= {NULL
, 100, 100, 2000, 0, 0, NULL
} },
320 { TRIGGER
, .u
.n
= {NULL
, 20, 10, 2000, 0, 0, NULL
} },
321 { JIFFY
, .u
.n
= {NULL
, 50, 20, 200, 0, 0, NULL
} },
322 { FULL
, .u
.n
= {NULL
, 400, 200, 60000, 0, 0, NULL
} },
326 /* called by: speakup_init() */
327 int synth_init(char *synth_name
)
331 struct spk_synth
*synth
= NULL
;
333 if (synth_name
== NULL
)
336 if (strcmp(synth_name
, "none") == 0) {
337 mutex_lock(&spk_mutex
);
339 mutex_unlock(&spk_mutex
);
343 mutex_lock(&spk_mutex
);
344 /* First, check if we already have it loaded. */
345 for (i
= 0; synths
[i
] != NULL
&& i
< MAXSYNTHS
; i
++)
346 if (strcmp(synths
[i
]->name
, synth_name
) == 0)
349 /* If we got one, initialize it now. */
351 ret
= do_synth_init(synth
);
354 mutex_unlock(&spk_mutex
);
359 /* called by: synth_add() */
360 static int do_synth_init(struct spk_synth
*in_synth
)
365 if (in_synth
->checkval
!= SYNTH_CHECK
)
369 pr_warn("synth probe\n");
370 if (synth
->probe(synth
) < 0) {
371 pr_warn("%s: device probe failed\n", in_synth
->name
);
375 synth_time_vars
[0].u
.n
.value
=
376 synth_time_vars
[0].u
.n
.default_val
= synth
->delay
;
377 synth_time_vars
[1].u
.n
.value
=
378 synth_time_vars
[1].u
.n
.default_val
= synth
->trigger
;
379 synth_time_vars
[2].u
.n
.value
=
380 synth_time_vars
[2].u
.n
.default_val
= synth
->jiffies
;
381 synth_time_vars
[3].u
.n
.value
=
382 synth_time_vars
[3].u
.n
.default_val
= synth
->full
;
383 synth_printf("%s", synth
->init
);
384 for (var
= synth
->vars
;
385 (var
->var_id
>= 0) && (var
->var_id
< MAXVARS
); var
++)
386 speakup_register_var(var
);
388 synth_printf("%s found\n", synth
->long_name
);
389 if (synth
->attributes
.name
390 && sysfs_create_group(speakup_kobj
, &(synth
->attributes
)) < 0)
392 synth_flags
= synth
->flags
;
393 wake_up_interruptible_all(&speakup_event
);
395 wake_up_process(speakup_task
);
399 void synth_release(void)
407 pr_info("releasing synth %s\n", synth
->name
);
409 del_timer(&thread_timer
);
411 if (synth
->attributes
.name
)
412 sysfs_remove_group(speakup_kobj
, &(synth
->attributes
));
413 for (var
= synth
->vars
; var
->var_id
!= MAXVARS
; var
++)
414 speakup_unregister_var(var
->var_id
);
415 stop_serial_interrupt();
420 /* called by: all_driver_init() */
421 int synth_add(struct spk_synth
*in_synth
)
425 mutex_lock(&spk_mutex
);
426 for (i
= 0; synths
[i
] != NULL
&& i
< MAXSYNTHS
; i
++)
427 /* synth_remove() is responsible for rotating the array down */
428 if (in_synth
== synths
[i
]) {
429 mutex_unlock(&spk_mutex
);
432 if (i
== MAXSYNTHS
) {
433 pr_warn("Error: attempting to add a synth past end of array\n");
434 mutex_unlock(&spk_mutex
);
437 synths
[i
++] = in_synth
;
439 if (in_synth
->startup
)
440 status
= do_synth_init(in_synth
);
441 mutex_unlock(&spk_mutex
);
444 EXPORT_SYMBOL_GPL(synth_add
);
446 void synth_remove(struct spk_synth
*in_synth
)
449 mutex_lock(&spk_mutex
);
450 if (synth
== in_synth
)
452 for (i
= 0; synths
[i
] != NULL
; i
++) {
453 if (in_synth
== synths
[i
])
456 for ( ; synths
[i
] != NULL
; i
++) /* compress table */
457 synths
[i
] = synths
[i
+1];
459 mutex_unlock(&spk_mutex
);
461 EXPORT_SYMBOL_GPL(synth_remove
);
463 short punc_masks
[] = { 0, SOME
, MOST
, PUNC
, PUNC
|B_SYM
};