1 #include <linux/interrupt.h>
2 #include <linux/ioport.h>
9 #ifndef SERIAL_PORT_DFNS
10 #define SERIAL_PORT_DFNS
13 static void start_serial_interrupt(int irq
);
15 static const struct old_serial_port rs_table
[] = {
18 static const struct old_serial_port
*serstate
;
21 const struct old_serial_port
*spk_serial_init(int index
)
23 int baud
= 9600, quot
= 0;
24 unsigned int cval
= 0;
25 int cflag
= CREAD
| HUPCL
| CLOCAL
| B9600
| CS8
;
26 const struct old_serial_port
*ser
= rs_table
+ index
;
29 /* Divisor, bytesize and parity */
30 quot
= ser
->baud_base
/ baud
;
31 cval
= cflag
& (CSIZE
| CSTOPB
);
32 #if defined(__powerpc__) || defined(__alpha__)
34 #else /* !__powerpc__ && !__alpha__ */
36 #endif /* !__powerpc__ && !__alpha__ */
38 cval
|= UART_LCR_PARITY
;
39 if (!(cflag
& PARODD
))
40 cval
|= UART_LCR_EPAR
;
41 if (synth_request_region(ser
->port
, 8)) {
42 /* try to take it back. */
43 pr_info("Ports not available, trying to steal them\n");
44 __release_region(&ioport_resource
, ser
->port
, 8);
45 err
= synth_request_region(ser
->port
, 8);
47 pr_warn("Unable to allocate port at %x, errno %i",
53 /* Disable UART interrupts, set DTR and RTS high
56 outb(cval
| UART_LCR_DLAB
, ser
->port
+ UART_LCR
); /* set DLAB */
57 outb(quot
& 0xff, ser
->port
+ UART_DLL
); /* LS of divisor */
58 outb(quot
>> 8, ser
->port
+ UART_DLM
); /* MS of divisor */
59 outb(cval
, ser
->port
+ UART_LCR
); /* reset DLAB */
61 /* Turn off Interrupts */
62 outb(0, ser
->port
+ UART_IER
);
63 outb(UART_MCR_DTR
| UART_MCR_RTS
, ser
->port
+ UART_MCR
);
65 /* If we read 0xff from the LSR, there is no UART here. */
66 if (inb(ser
->port
+ UART_LSR
) == 0xff) {
67 synth_release_region(ser
->port
, 8);
73 speakup_info
.port_tts
= ser
->port
;
76 start_serial_interrupt(ser
->irq
);
81 static irqreturn_t
synth_readbuf_handler(int irq
, void *dev_id
)
84 /*printk(KERN_ERR "in irq\n"); */
85 /*pr_warn("in IRQ\n"); */
88 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
89 while (inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
) {
91 c
= inb_p(speakup_info
.port_tts
+UART_RX
);
92 synth
->read_buff_add((u_char
) c
);
93 /*printk(KERN_ERR "c = %d\n", c); */
94 /*pr_warn("C = %d\n", c); */
96 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
100 static void start_serial_interrupt(int irq
)
104 if (!synth
->read_buff_add
)
107 rv
= request_irq(irq
, synth_readbuf_handler
, IRQF_SHARED
,
108 "serial", (void *) synth_readbuf_handler
);
111 pr_err("Unable to request Speakup serial I R Q\n");
113 outb(UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
,
114 speakup_info
.port_tts
+ UART_MCR
);
115 /* Turn on Interrupts */
116 outb(UART_IER_MSI
|UART_IER_RLSI
|UART_IER_RDI
,
117 speakup_info
.port_tts
+ UART_IER
);
118 inb(speakup_info
.port_tts
+UART_LSR
);
119 inb(speakup_info
.port_tts
+UART_RX
);
120 inb(speakup_info
.port_tts
+UART_IIR
);
121 inb(speakup_info
.port_tts
+UART_MSR
);
122 outb(1, speakup_info
.port_tts
+ UART_FCR
); /* Turn FIFO On */
125 void spk_stop_serial_interrupt(void)
127 if (speakup_info
.port_tts
== 0)
130 if (!synth
->read_buff_add
)
133 /* Turn off interrupts */
134 outb(0, speakup_info
.port_tts
+UART_IER
);
136 free_irq(serstate
->irq
, (void *) synth_readbuf_handler
);
139 int spk_wait_for_xmitr(void)
141 int tmout
= SPK_XMITR_TIMEOUT
;
143 if ((synth
->alive
) && (timeouts
>= NUM_DISABLE_TIMEOUTS
)) {
144 pr_warn("%s: too many timeouts, deactivating speakup\n",
147 /* No synth any more, so nobody will restart TTYs, and we thus
148 * need to do it ourselves. Now that there is no synth we can
149 * let application flood anyway
151 speakup_start_ttys();
155 while (spk_serial_tx_busy()) {
157 pr_warn("%s: timed out (tx busy)\n", synth
->long_name
);
163 tmout
= SPK_CTS_TIMEOUT
;
164 while (!((inb_p(speakup_info
.port_tts
+ UART_MSR
)) & UART_MSR_CTS
)) {
167 /* pr_warn("%s: timed out (cts)\n",
179 unsigned char spk_serial_in(void)
181 int tmout
= SPK_SERIAL_TIMEOUT
;
183 while (!(inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
)) {
185 pr_warn("time out while waiting for input.\n");
190 return inb_p(speakup_info
.port_tts
+ UART_RX
);
192 EXPORT_SYMBOL_GPL(spk_serial_in
);
194 unsigned char spk_serial_in_nowait(void)
198 lsr
= inb_p(speakup_info
.port_tts
+ UART_LSR
);
199 if (!(lsr
& UART_LSR_DR
))
201 return inb_p(speakup_info
.port_tts
+ UART_RX
);
203 EXPORT_SYMBOL_GPL(spk_serial_in_nowait
);
205 int spk_serial_out(const char ch
)
207 if (synth
->alive
&& spk_wait_for_xmitr()) {
208 outb_p(ch
, speakup_info
.port_tts
);
213 EXPORT_SYMBOL_GPL(spk_serial_out
);
215 void spk_serial_release(void)
217 if (speakup_info
.port_tts
== 0)
219 synth_release_region(speakup_info
.port_tts
, 8);
220 speakup_info
.port_tts
= 0;
222 EXPORT_SYMBOL_GPL(spk_serial_release
);