blob: d45c8afb041d8f894bd8ed212281d26f850e82fe [file] [log] [blame]
William Hubbsc6e3fd22010-10-07 13:20:02 -05001#include <linux/console.h>
William Hubbsc6e3fd22010-10-07 13:20:02 -05002#include <linux/types.h>
3#include <linux/wait.h>
4
5#include "speakup.h"
6#include "spk_priv.h"
7
Lijo Antony5c75af12013-01-08 22:39:01 +04008#define SYNTH_BUF_SIZE 8192 /* currently 8K bytes */
William Hubbsc6e3fd22010-10-07 13:20:02 -05009
Lijo Antony5c75af12013-01-08 22:39:01 +040010static u_char synth_buffer[SYNTH_BUF_SIZE]; /* guess what this is for! */
William Hubbsc6e3fd22010-10-07 13:20:02 -050011static u_char *buff_in = synth_buffer;
12static u_char *buff_out = synth_buffer;
Lijo Antony5c75af12013-01-08 22:39:01 +040013static u_char *buffer_end = synth_buffer + SYNTH_BUF_SIZE - 1;
William Hubbsc6e3fd22010-10-07 13:20:02 -050014
15/* These try to throttle applications by stopping the TTYs
16 * Note: we need to make sure that we will restart them eventually, which is
17 * usually not possible to do from the notifiers. TODO: it should be possible
18 * starting from linux 2.6.26.
19 *
20 * So we only stop when we know alive == 1 (else we discard the data anyway),
21 * and the alive synth will eventually call start_ttys from the thread context.
22 */
23void speakup_start_ttys(void)
24{
25 int i;
26
27 for (i = 0; i < MAX_NR_CONSOLES; i++) {
28 if (speakup_console[i] && speakup_console[i]->tty_stopped)
29 continue;
Greg Kroah-Hartman5b192082010-10-07 19:30:49 -070030 if ((vc_cons[i].d != NULL) && (vc_cons[i].d->port.tty != NULL))
31 start_tty(vc_cons[i].d->port.tty);
William Hubbsc6e3fd22010-10-07 13:20:02 -050032 }
33}
34EXPORT_SYMBOL_GPL(speakup_start_ttys);
35
36static void speakup_stop_ttys(void)
37{
38 int i;
39
40 for (i = 0; i < MAX_NR_CONSOLES; i++)
Greg Kroah-Hartman5b192082010-10-07 19:30:49 -070041 if ((vc_cons[i].d != NULL) && (vc_cons[i].d->port.tty != NULL))
42 stop_tty(vc_cons[i].d->port.tty);
William Hubbsc6e3fd22010-10-07 13:20:02 -050043}
44
45static int synth_buffer_free(void)
46{
Lijo Antony5c75af12013-01-08 22:39:01 +040047 int bytes_free;
William Hubbsc6e3fd22010-10-07 13:20:02 -050048
49 if (buff_in >= buff_out)
Lijo Antony5c75af12013-01-08 22:39:01 +040050 bytes_free = SYNTH_BUF_SIZE - (buff_in - buff_out);
William Hubbsc6e3fd22010-10-07 13:20:02 -050051 else
Lijo Antony5c75af12013-01-08 22:39:01 +040052 bytes_free = buff_out - buff_in;
53 return bytes_free;
William Hubbsc6e3fd22010-10-07 13:20:02 -050054}
55
56int synth_buffer_empty(void)
57{
58 return (buff_in == buff_out);
59}
60EXPORT_SYMBOL_GPL(synth_buffer_empty);
61
62void synth_buffer_add(char ch)
63{
64 if (!synth->alive) {
65 /* This makes sure that we won't stop TTYs if there is no synth
66 * to restart them */
67 return;
68 }
69 if (synth_buffer_free() <= 100) {
70 synth_start();
71 speakup_stop_ttys();
72 }
73 if (synth_buffer_free() <= 1)
74 return;
75 *buff_in++ = ch;
76 if (buff_in > buffer_end)
77 buff_in = synth_buffer;
78}
79
80char synth_buffer_getc(void)
81{
82 char ch;
83
84 if (buff_out == buff_in)
85 return 0;
86 ch = *buff_out++;
87 if (buff_out > buffer_end)
88 buff_out = synth_buffer;
89 return ch;
90}
91EXPORT_SYMBOL_GPL(synth_buffer_getc);
92
93char synth_buffer_peek(void)
94{
95 if (buff_out == buff_in)
96 return 0;
97 return *buff_out;
98}
99EXPORT_SYMBOL_GPL(synth_buffer_peek);
100
101void synth_buffer_clear(void)
102{
103 buff_in = buff_out = synth_buffer;
William Hubbsc6e3fd22010-10-07 13:20:02 -0500104}
105EXPORT_SYMBOL_GPL(synth_buffer_clear);