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
[] = {
24 static const struct old_serial_port
*serstate
;
27 const struct old_serial_port
*spk_serial_init(int index
)
29 int baud
= 9600, quot
= 0;
30 unsigned int cval
= 0;
31 int cflag
= CREAD
| HUPCL
| CLOCAL
| B9600
| CS8
;
32 const struct old_serial_port
*ser
;
35 if (index
>= ARRAY_SIZE(rs_table
)) {
36 pr_info("no port info for ttyS%d\n", index
);
39 ser
= rs_table
+ index
;
41 /* Divisor, bytesize and parity */
42 quot
= ser
->baud_base
/ baud
;
43 cval
= cflag
& (CSIZE
| CSTOPB
);
44 #if defined(__powerpc__) || defined(__alpha__)
46 #else /* !__powerpc__ && !__alpha__ */
48 #endif /* !__powerpc__ && !__alpha__ */
50 cval
|= UART_LCR_PARITY
;
51 if (!(cflag
& PARODD
))
52 cval
|= UART_LCR_EPAR
;
53 if (synth_request_region(ser
->port
, 8)) {
54 /* try to take it back. */
55 pr_info("Ports not available, trying to steal them\n");
56 __release_region(&ioport_resource
, ser
->port
, 8);
57 err
= synth_request_region(ser
->port
, 8);
59 pr_warn("Unable to allocate port at %x, errno %i",
65 /* Disable UART interrupts, set DTR and RTS high
68 outb(cval
| UART_LCR_DLAB
, ser
->port
+ UART_LCR
); /* set DLAB */
69 outb(quot
& 0xff, ser
->port
+ UART_DLL
); /* LS of divisor */
70 outb(quot
>> 8, ser
->port
+ UART_DLM
); /* MS of divisor */
71 outb(cval
, ser
->port
+ UART_LCR
); /* reset DLAB */
73 /* Turn off Interrupts */
74 outb(0, ser
->port
+ UART_IER
);
75 outb(UART_MCR_DTR
| UART_MCR_RTS
, ser
->port
+ UART_MCR
);
77 /* If we read 0xff from the LSR, there is no UART here. */
78 if (inb(ser
->port
+ UART_LSR
) == 0xff) {
79 synth_release_region(ser
->port
, 8);
85 speakup_info
.port_tts
= ser
->port
;
88 start_serial_interrupt(ser
->irq
);
93 static irqreturn_t
synth_readbuf_handler(int irq
, void *dev_id
)
98 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
99 while (inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
) {
101 c
= inb_p(speakup_info
.port_tts
+UART_RX
);
102 synth
->read_buff_add((u_char
) c
);
104 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
108 static void start_serial_interrupt(int irq
)
112 if (!synth
->read_buff_add
)
115 rv
= request_irq(irq
, synth_readbuf_handler
, IRQF_SHARED
,
116 "serial", (void *) synth_readbuf_handler
);
119 pr_err("Unable to request Speakup serial I R Q\n");
121 outb(UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
,
122 speakup_info
.port_tts
+ UART_MCR
);
123 /* Turn on Interrupts */
124 outb(UART_IER_MSI
|UART_IER_RLSI
|UART_IER_RDI
,
125 speakup_info
.port_tts
+ UART_IER
);
126 inb(speakup_info
.port_tts
+UART_LSR
);
127 inb(speakup_info
.port_tts
+UART_RX
);
128 inb(speakup_info
.port_tts
+UART_IIR
);
129 inb(speakup_info
.port_tts
+UART_MSR
);
130 outb(1, speakup_info
.port_tts
+ UART_FCR
); /* Turn FIFO On */
133 void spk_stop_serial_interrupt(void)
135 if (speakup_info
.port_tts
== 0)
138 if (!synth
->read_buff_add
)
141 /* Turn off interrupts */
142 outb(0, speakup_info
.port_tts
+UART_IER
);
144 free_irq(serstate
->irq
, (void *) synth_readbuf_handler
);
147 int spk_wait_for_xmitr(void)
149 int tmout
= SPK_XMITR_TIMEOUT
;
151 if ((synth
->alive
) && (timeouts
>= NUM_DISABLE_TIMEOUTS
)) {
152 pr_warn("%s: too many timeouts, deactivating speakup\n",
155 /* No synth any more, so nobody will restart TTYs, and we thus
156 * need to do it ourselves. Now that there is no synth we can
157 * let application flood anyway
159 speakup_start_ttys();
163 while (spk_serial_tx_busy()) {
165 pr_warn("%s: timed out (tx busy)\n", synth
->long_name
);
171 tmout
= SPK_CTS_TIMEOUT
;
172 while (!((inb_p(speakup_info
.port_tts
+ UART_MSR
)) & UART_MSR_CTS
)) {
184 unsigned char spk_serial_in(void)
186 int tmout
= SPK_SERIAL_TIMEOUT
;
188 while (!(inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
)) {
190 pr_warn("time out while waiting for input.\n");
195 return inb_p(speakup_info
.port_tts
+ UART_RX
);
197 EXPORT_SYMBOL_GPL(spk_serial_in
);
199 unsigned char spk_serial_in_nowait(void)
203 lsr
= inb_p(speakup_info
.port_tts
+ UART_LSR
);
204 if (!(lsr
& UART_LSR_DR
))
206 return inb_p(speakup_info
.port_tts
+ UART_RX
);
208 EXPORT_SYMBOL_GPL(spk_serial_in_nowait
);
210 int spk_serial_out(const char ch
)
212 if (synth
->alive
&& spk_wait_for_xmitr()) {
213 outb_p(ch
, speakup_info
.port_tts
);
218 EXPORT_SYMBOL_GPL(spk_serial_out
);
220 void spk_serial_release(void)
222 if (speakup_info
.port_tts
== 0)
224 synth_release_region(speakup_info
.port_tts
, 8);
225 speakup_info
.port_tts
= 0;
227 EXPORT_SYMBOL_GPL(spk_serial_release
);