1 #include <linux/interrupt.h>
2 #include <linux/ioport.h>
9 #include <linux/serial_core.h>
10 /* WARNING: Do not change this to <linux/serial.h> without testing that
11 * SERIAL_PORT_DFNS does get defined to the appropriate value.
13 #include <asm/serial.h>
15 #ifndef SERIAL_PORT_DFNS
16 #define SERIAL_PORT_DFNS
19 static void start_serial_interrupt(int irq
);
21 static const struct old_serial_port rs_table
[] = {
25 static const struct old_serial_port
*serstate
;
28 static int spk_serial_out(struct spk_synth
*in_synth
, const char ch
);
29 static void spk_serial_send_xchar(char ch
);
30 static void spk_serial_tiocmset(unsigned int set
, unsigned int clear
);
31 static unsigned char spk_serial_in(void);
32 static unsigned char spk_serial_in_nowait(void);
33 static void spk_serial_flush_buffer(void);
35 struct spk_io_ops spk_serial_io_ops
= {
36 .synth_out
= spk_serial_out
,
37 .send_xchar
= spk_serial_send_xchar
,
38 .tiocmset
= spk_serial_tiocmset
,
39 .synth_in
= spk_serial_in
,
40 .synth_in_nowait
= spk_serial_in_nowait
,
41 .flush_buffer
= spk_serial_flush_buffer
,
43 EXPORT_SYMBOL_GPL(spk_serial_io_ops
);
45 const struct old_serial_port
*spk_serial_init(int index
)
47 int baud
= 9600, quot
= 0;
48 unsigned int cval
= 0;
49 int cflag
= CREAD
| HUPCL
| CLOCAL
| B9600
| CS8
;
50 const struct old_serial_port
*ser
;
53 if (index
>= ARRAY_SIZE(rs_table
)) {
54 pr_info("no port info for ttyS%d\n", index
);
57 ser
= rs_table
+ index
;
59 /* Divisor, bytesize and parity */
60 quot
= ser
->baud_base
/ baud
;
61 cval
= cflag
& (CSIZE
| CSTOPB
);
62 #if defined(__powerpc__) || defined(__alpha__)
64 #else /* !__powerpc__ && !__alpha__ */
66 #endif /* !__powerpc__ && !__alpha__ */
68 cval
|= UART_LCR_PARITY
;
69 if (!(cflag
& PARODD
))
70 cval
|= UART_LCR_EPAR
;
71 if (synth_request_region(ser
->port
, 8)) {
72 /* try to take it back. */
73 pr_info("Ports not available, trying to steal them\n");
74 __release_region(&ioport_resource
, ser
->port
, 8);
75 err
= synth_request_region(ser
->port
, 8);
77 pr_warn("Unable to allocate port at %x, errno %i",
83 /* Disable UART interrupts, set DTR and RTS high
86 outb(cval
| UART_LCR_DLAB
, ser
->port
+ UART_LCR
); /* set DLAB */
87 outb(quot
& 0xff, ser
->port
+ UART_DLL
); /* LS of divisor */
88 outb(quot
>> 8, ser
->port
+ UART_DLM
); /* MS of divisor */
89 outb(cval
, ser
->port
+ UART_LCR
); /* reset DLAB */
91 /* Turn off Interrupts */
92 outb(0, ser
->port
+ UART_IER
);
93 outb(UART_MCR_DTR
| UART_MCR_RTS
, ser
->port
+ UART_MCR
);
95 /* If we read 0xff from the LSR, there is no UART here. */
96 if (inb(ser
->port
+ UART_LSR
) == 0xff) {
97 synth_release_region(ser
->port
, 8);
103 speakup_info
.port_tts
= ser
->port
;
106 start_serial_interrupt(ser
->irq
);
111 static irqreturn_t
synth_readbuf_handler(int irq
, void *dev_id
)
116 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
117 while (inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
) {
118 c
= inb_p(speakup_info
.port_tts
+ UART_RX
);
119 synth
->read_buff_add((u_char
)c
);
121 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
125 static void start_serial_interrupt(int irq
)
129 if (!synth
->read_buff_add
)
132 rv
= request_irq(irq
, synth_readbuf_handler
, IRQF_SHARED
,
133 "serial", (void *)synth_readbuf_handler
);
136 pr_err("Unable to request Speakup serial I R Q\n");
138 outb(UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
,
139 speakup_info
.port_tts
+ UART_MCR
);
140 /* Turn on Interrupts */
141 outb(UART_IER_MSI
| UART_IER_RLSI
| UART_IER_RDI
,
142 speakup_info
.port_tts
+ UART_IER
);
143 inb(speakup_info
.port_tts
+ UART_LSR
);
144 inb(speakup_info
.port_tts
+ UART_RX
);
145 inb(speakup_info
.port_tts
+ UART_IIR
);
146 inb(speakup_info
.port_tts
+ UART_MSR
);
147 outb(1, speakup_info
.port_tts
+ UART_FCR
); /* Turn FIFO On */
150 static void spk_serial_send_xchar(char ch
)
152 int timeout
= SPK_XMITR_TIMEOUT
;
154 while (spk_serial_tx_busy()) {
159 outb(ch
, speakup_info
.port_tts
);
162 static void spk_serial_tiocmset(unsigned int set
, unsigned int clear
)
164 int old
= inb(speakup_info
.port_tts
+ UART_MCR
);
166 outb((old
& ~clear
) | set
, speakup_info
.port_tts
+ UART_MCR
);
169 int spk_serial_synth_probe(struct spk_synth
*synth
)
171 const struct old_serial_port
*ser
;
174 if ((synth
->ser
>= SPK_LO_TTY
) && (synth
->ser
<= SPK_HI_TTY
)) {
175 ser
= spk_serial_init(synth
->ser
);
179 outb_p(0, ser
->port
);
181 outb_p('\r', ser
->port
);
185 pr_warn("ttyS%i is an invalid port\n", synth
->ser
);
188 pr_info("%s: not found\n", synth
->long_name
);
191 pr_info("%s: ttyS%i, Driver Version %s\n",
192 synth
->long_name
, synth
->ser
, synth
->version
);
196 EXPORT_SYMBOL_GPL(spk_serial_synth_probe
);
198 void spk_stop_serial_interrupt(void)
200 if (speakup_info
.port_tts
== 0)
203 if (!synth
->read_buff_add
)
206 /* Turn off interrupts */
207 outb(0, speakup_info
.port_tts
+ UART_IER
);
209 free_irq(serstate
->irq
, (void *)synth_readbuf_handler
);
211 EXPORT_SYMBOL_GPL(spk_stop_serial_interrupt
);
213 int spk_wait_for_xmitr(struct spk_synth
*in_synth
)
215 int tmout
= SPK_XMITR_TIMEOUT
;
217 if ((in_synth
->alive
) && (timeouts
>= NUM_DISABLE_TIMEOUTS
)) {
218 pr_warn("%s: too many timeouts, deactivating speakup\n",
219 in_synth
->long_name
);
221 /* No synth any more, so nobody will restart TTYs, and we thus
222 * need to do it ourselves. Now that there is no synth we can
223 * let application flood anyway
225 speakup_start_ttys();
229 while (spk_serial_tx_busy()) {
231 pr_warn("%s: timed out (tx busy)\n",
232 in_synth
->long_name
);
238 tmout
= SPK_CTS_TIMEOUT
;
239 while (!((inb_p(speakup_info
.port_tts
+ UART_MSR
)) & UART_MSR_CTS
)) {
251 static unsigned char spk_serial_in(void)
253 int tmout
= SPK_SERIAL_TIMEOUT
;
255 while (!(inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
)) {
257 pr_warn("time out while waiting for input.\n");
262 return inb_p(speakup_info
.port_tts
+ UART_RX
);
265 static unsigned char spk_serial_in_nowait(void)
269 lsr
= inb_p(speakup_info
.port_tts
+ UART_LSR
);
270 if (!(lsr
& UART_LSR_DR
))
272 return inb_p(speakup_info
.port_tts
+ UART_RX
);
275 static void spk_serial_flush_buffer(void)
277 /* TODO: flush the UART 16550 buffer */
280 static int spk_serial_out(struct spk_synth
*in_synth
, const char ch
)
282 if (in_synth
->alive
&& spk_wait_for_xmitr(in_synth
)) {
283 outb_p(ch
, speakup_info
.port_tts
);
289 const char *spk_serial_synth_immediate(struct spk_synth
*synth
,
294 while ((ch
= *buff
)) {
296 ch
= synth
->procspeech
;
297 if (spk_wait_for_xmitr(synth
))
298 outb(ch
, speakup_info
.port_tts
);
305 EXPORT_SYMBOL_GPL(spk_serial_synth_immediate
);
307 void spk_serial_release(void)
309 spk_stop_serial_interrupt();
310 if (speakup_info
.port_tts
== 0)
312 synth_release_region(speakup_info
.port_tts
, 8);
313 speakup_info
.port_tts
= 0;
315 EXPORT_SYMBOL_GPL(spk_serial_release
);