1 #include <linux/interrupt.h>
2 #include <linux/ioport.h>
9 static void start_serial_interrupt(int irq
);
11 static struct serial_state rs_table
[] = {
14 static struct serial_state
*serstate
;
17 struct serial_state
*spk_serial_init(int index
)
19 int baud
= 9600, quot
= 0;
20 unsigned int cval
= 0;
21 int cflag
= CREAD
| HUPCL
| CLOCAL
| B9600
| CS8
;
22 struct serial_state
*ser
= NULL
;
25 ser
= rs_table
+ index
;
26 /* Divisor, bytesize and parity */
27 quot
= ser
->baud_base
/ baud
;
28 cval
= cflag
& (CSIZE
| CSTOPB
);
29 #if defined(__powerpc__) || defined(__alpha__)
31 #else /* !__powerpc__ && !__alpha__ */
33 #endif /* !__powerpc__ && !__alpha__ */
35 cval
|= UART_LCR_PARITY
;
36 if (!(cflag
& PARODD
))
37 cval
|= UART_LCR_EPAR
;
38 if (synth_request_region(ser
->port
, 8)) {
39 /* try to take it back. */
40 printk(KERN_INFO
"Ports not available, trying to steal them\n");
41 __release_region(&ioport_resource
, ser
->port
, 8);
42 err
= synth_request_region(ser
->port
, 8);
44 pr_warn("Unable to allocate port at %lx, errno %i",
50 /* Disable UART interrupts, set DTR and RTS high
52 outb(cval
| UART_LCR_DLAB
, ser
->port
+ UART_LCR
); /* set DLAB */
53 outb(quot
& 0xff, ser
->port
+ UART_DLL
); /* LS of divisor */
54 outb(quot
>> 8, ser
->port
+ UART_DLM
); /* MS of divisor */
55 outb(cval
, ser
->port
+ UART_LCR
); /* reset DLAB */
57 /* Turn off Interrupts */
58 outb(0, ser
->port
+ UART_IER
);
59 outb(UART_MCR_DTR
| UART_MCR_RTS
, ser
->port
+ UART_MCR
);
61 /* If we read 0xff from the LSR, there is no UART here. */
62 if (inb(ser
->port
+ UART_LSR
) == 0xff) {
63 synth_release_region(ser
->port
, 8);
69 speakup_info
.port_tts
= ser
->port
;
72 start_serial_interrupt(ser
->irq
);
77 static irqreturn_t
synth_readbuf_handler(int irq
, void *dev_id
)
80 /*printk(KERN_ERR "in irq\n"); */
81 /*pr_warn("in IRQ\n"); */
84 while (inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
) {
86 c
= inb_p(speakup_info
.port_tts
+UART_RX
);
87 synth
->read_buff_add((u_char
) c
);
88 /*printk(KERN_ERR "c = %d\n", c); */
89 /*pr_warn("C = %d\n", c); */
95 static void start_serial_interrupt(int irq
)
99 if (synth
->read_buff_add
== NULL
)
102 rv
= request_irq(irq
, synth_readbuf_handler
, IRQF_SHARED
,
103 "serial", (void *) synth_readbuf_handler
);
106 printk(KERN_ERR
"Unable to request Speakup serial I R Q\n");
108 outb(UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
,
109 speakup_info
.port_tts
+ UART_MCR
);
110 /* Turn on Interrupts */
111 outb(UART_IER_MSI
|UART_IER_RLSI
|UART_IER_RDI
,
112 speakup_info
.port_tts
+ UART_IER
);
113 inb(speakup_info
.port_tts
+UART_LSR
);
114 inb(speakup_info
.port_tts
+UART_RX
);
115 inb(speakup_info
.port_tts
+UART_IIR
);
116 inb(speakup_info
.port_tts
+UART_MSR
);
117 outb(1, speakup_info
.port_tts
+ UART_FCR
); /* Turn FIFO On */
120 void stop_serial_interrupt(void)
122 if (speakup_info
.port_tts
== 0)
125 if (synth
->read_buff_add
== NULL
)
128 /* Turn off interrupts */
129 outb(0, speakup_info
.port_tts
+UART_IER
);
131 free_irq(serstate
->irq
, (void *) synth_readbuf_handler
);
134 int wait_for_xmitr(void)
136 int tmout
= SPK_XMITR_TIMEOUT
;
137 if ((synth
->alive
) && (timeouts
>= NUM_DISABLE_TIMEOUTS
)) {
138 pr_warn("%s: too many timeouts, deactivating speakup\n",
141 /* No synth any more, so nobody will restart TTYs, and we thus
142 * need to do it ourselves. Now that there is no synth we can
143 * let application flood anyway */
144 speakup_start_ttys();
148 while (spk_serial_tx_busy()) {
150 pr_warn("%s: timed out (tx busy)\n", synth
->long_name
);
156 tmout
= SPK_CTS_TIMEOUT
;
157 while (!((inb_p(speakup_info
.port_tts
+ UART_MSR
)) & UART_MSR_CTS
)) {
160 /* pr_warn("%s: timed out (cts)\n",
161 * synth->long_name); */
171 unsigned char spk_serial_in(void)
173 int tmout
= SPK_SERIAL_TIMEOUT
;
175 while (!(inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
)) {
177 pr_warn("time out while waiting for input.\n");
182 return inb_p(speakup_info
.port_tts
+ UART_RX
);
184 EXPORT_SYMBOL_GPL(spk_serial_in
);
186 unsigned char spk_serial_in_nowait(void)
190 lsr
= inb_p(speakup_info
.port_tts
+ UART_LSR
);
191 if (!(lsr
& UART_LSR_DR
))
193 return inb_p(speakup_info
.port_tts
+ UART_RX
);
195 EXPORT_SYMBOL_GPL(spk_serial_in_nowait
);
197 int spk_serial_out(const char ch
)
199 if (synth
->alive
&& wait_for_xmitr()) {
200 outb_p(ch
, speakup_info
.port_tts
);
205 EXPORT_SYMBOL_GPL(spk_serial_out
);
207 void spk_serial_release(void)
209 if (speakup_info
.port_tts
== 0)
211 synth_release_region(speakup_info
.port_tts
, 8);
212 speakup_info
.port_tts
= 0;
214 EXPORT_SYMBOL_GPL(spk_serial_release
);