2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
32 #include <linux/kernel.h>
33 #include <linux/sched.h> /* For jiffies, task states */
34 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
35 #include <linux/delay.h> /* For udelay */
36 #include <linux/io.h> /* For read[bwl]/write[bwl] */
37 #include <linux/serial.h> /* For struct async_serial */
38 #include <linux/serial_reg.h> /* For the various UART offsets */
39 #include <linux/pci.h>
41 #include "dgnc_driver.h" /* Driver main header file */
44 #include "dgnc_trace.h"
46 static inline void cls_parse_isr(struct dgnc_board *brd, uint port);
47 static inline void cls_clear_break(struct channel_t *ch, int force);
48 static inline void cls_set_cts_flow_control(struct channel_t *ch);
49 static inline void cls_set_rts_flow_control(struct channel_t *ch);
50 static inline void cls_set_ixon_flow_control(struct channel_t *ch);
51 static inline void cls_set_ixoff_flow_control(struct channel_t *ch);
52 static inline void cls_set_no_output_flow_control(struct channel_t *ch);
53 static inline void cls_set_no_input_flow_control(struct channel_t *ch);
54 static void cls_parse_modem(struct channel_t *ch, uchar signals);
55 static void cls_tasklet(unsigned long data);
56 static void cls_vpd(struct dgnc_board *brd);
57 static void cls_uart_init(struct channel_t *ch);
58 static void cls_uart_off(struct channel_t *ch);
59 static int cls_drain(struct tty_struct *tty, uint seconds);
60 static void cls_param(struct tty_struct *tty);
61 static void cls_assert_modem_signals(struct channel_t *ch);
62 static void cls_flush_uart_write(struct channel_t *ch);
63 static void cls_flush_uart_read(struct channel_t *ch);
64 static void cls_disable_receiver(struct channel_t *ch);
65 static void cls_enable_receiver(struct channel_t *ch);
66 static void cls_send_break(struct channel_t *ch, int msecs);
67 static void cls_send_start_character(struct channel_t *ch);
68 static void cls_send_stop_character(struct channel_t *ch);
69 static void cls_copy_data_from_uart_to_queue(struct channel_t *ch);
70 static void cls_copy_data_from_queue_to_uart(struct channel_t *ch);
71 static uint cls_get_uart_bytes_left(struct channel_t *ch);
72 static void cls_send_immediate_char(struct channel_t *ch, unsigned char);
73 static irqreturn_t cls_intr(int irq, void *voidbrd);
75 struct board_ops dgnc_cls_ops = {
76 .tasklet = cls_tasklet,
78 .uart_init = cls_uart_init,
79 .uart_off = cls_uart_off,
83 .assert_modem_signals = cls_assert_modem_signals,
84 .flush_uart_write = cls_flush_uart_write,
85 .flush_uart_read = cls_flush_uart_read,
86 .disable_receiver = cls_disable_receiver,
87 .enable_receiver = cls_enable_receiver,
88 .send_break = cls_send_break,
89 .send_start_character = cls_send_start_character,
90 .send_stop_character = cls_send_stop_character,
91 .copy_data_from_queue_to_uart = cls_copy_data_from_queue_to_uart,
92 .get_uart_bytes_left = cls_get_uart_bytes_left,
93 .send_immediate_char = cls_send_immediate_char
97 static inline void cls_set_cts_flow_control(struct channel_t *ch)
99 uchar lcrb = readb(&ch->ch_cls_uart->lcr);
100 uchar ier = readb(&ch->ch_cls_uart->ier);
103 DPR_PARAM(("Setting CTSFLOW\n"));
106 * The Enhanced Register Set may only be accessed when
107 * the Line Control Register is set to 0xBFh.
109 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
111 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
113 /* Turn on CTS flow control, turn off IXON flow control */
114 isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_CTSDSR);
115 isr_fcr &= ~(UART_EXAR654_EFR_IXON);
117 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
119 /* Write old LCR value back out, which turns enhanced access off */
120 writeb(lcrb, &ch->ch_cls_uart->lcr);
122 /* Enable interrupts for CTS flow, turn off interrupts for received XOFF chars */
123 ier |= (UART_EXAR654_IER_CTSDSR);
124 ier &= ~(UART_EXAR654_IER_XOFF);
125 writeb(ier, &ch->ch_cls_uart->ier);
127 /* Set the usual FIFO values */
128 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
130 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_56 |
131 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
132 &ch->ch_cls_uart->isr_fcr);
134 ch->ch_t_tlevel = 16;
139 static inline void cls_set_ixon_flow_control(struct channel_t *ch)
141 uchar lcrb = readb(&ch->ch_cls_uart->lcr);
142 uchar ier = readb(&ch->ch_cls_uart->ier);
145 DPR_PARAM(("Setting IXON FLOW\n"));
148 * The Enhanced Register Set may only be accessed when
149 * the Line Control Register is set to 0xBFh.
151 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
153 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
155 /* Turn on IXON flow control, turn off CTS flow control */
156 isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_IXON);
157 isr_fcr &= ~(UART_EXAR654_EFR_CTSDSR);
159 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
161 /* Now set our current start/stop chars while in enhanced mode */
162 writeb(ch->ch_startc, &ch->ch_cls_uart->mcr);
163 writeb(0, &ch->ch_cls_uart->lsr);
164 writeb(ch->ch_stopc, &ch->ch_cls_uart->msr);
165 writeb(0, &ch->ch_cls_uart->spr);
167 /* Write old LCR value back out, which turns enhanced access off */
168 writeb(lcrb, &ch->ch_cls_uart->lcr);
170 /* Disable interrupts for CTS flow, turn on interrupts for received XOFF chars */
171 ier &= ~(UART_EXAR654_IER_CTSDSR);
172 ier |= (UART_EXAR654_IER_XOFF);
173 writeb(ier, &ch->ch_cls_uart->ier);
175 /* Set the usual FIFO values */
176 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
178 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
179 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
180 &ch->ch_cls_uart->isr_fcr);
185 static inline void cls_set_no_output_flow_control(struct channel_t *ch)
187 uchar lcrb = readb(&ch->ch_cls_uart->lcr);
188 uchar ier = readb(&ch->ch_cls_uart->ier);
191 DPR_PARAM(("Unsetting Output FLOW\n"));
194 * The Enhanced Register Set may only be accessed when
195 * the Line Control Register is set to 0xBFh.
197 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
199 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
201 /* Turn off IXON flow control, turn off CTS flow control */
202 isr_fcr |= (UART_EXAR654_EFR_ECB);
203 isr_fcr &= ~(UART_EXAR654_EFR_CTSDSR | UART_EXAR654_EFR_IXON);
205 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
207 /* Write old LCR value back out, which turns enhanced access off */
208 writeb(lcrb, &ch->ch_cls_uart->lcr);
210 /* Disable interrupts for CTS flow, turn off interrupts for received XOFF chars */
211 ier &= ~(UART_EXAR654_IER_CTSDSR);
212 ier &= ~(UART_EXAR654_IER_XOFF);
213 writeb(ier, &ch->ch_cls_uart->ier);
215 /* Set the usual FIFO values */
216 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
218 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
219 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
220 &ch->ch_cls_uart->isr_fcr);
222 ch->ch_r_watermark = 0;
223 ch->ch_t_tlevel = 16;
224 ch->ch_r_tlevel = 16;
229 static inline void cls_set_rts_flow_control(struct channel_t *ch)
231 uchar lcrb = readb(&ch->ch_cls_uart->lcr);
232 uchar ier = readb(&ch->ch_cls_uart->ier);
235 DPR_PARAM(("Setting RTSFLOW\n"));
238 * The Enhanced Register Set may only be accessed when
239 * the Line Control Register is set to 0xBFh.
241 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
243 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
245 /* Turn on RTS flow control, turn off IXOFF flow control */
246 isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_RTSDTR);
247 isr_fcr &= ~(UART_EXAR654_EFR_IXOFF);
249 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
251 /* Write old LCR value back out, which turns enhanced access off */
252 writeb(lcrb, &ch->ch_cls_uart->lcr);
254 /* Enable interrupts for RTS flow */
255 ier |= (UART_EXAR654_IER_RTSDTR);
256 writeb(ier, &ch->ch_cls_uart->ier);
258 /* Set the usual FIFO values */
259 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
261 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_56 |
262 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
263 &ch->ch_cls_uart->isr_fcr);
266 ch->ch_r_watermark = 4;
272 static inline void cls_set_ixoff_flow_control(struct channel_t *ch)
274 uchar lcrb = readb(&ch->ch_cls_uart->lcr);
275 uchar ier = readb(&ch->ch_cls_uart->ier);
278 DPR_PARAM(("Setting IXOFF FLOW\n"));
281 * The Enhanced Register Set may only be accessed when
282 * the Line Control Register is set to 0xBFh.
284 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
286 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
288 /* Turn on IXOFF flow control, turn off RTS flow control */
289 isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_IXOFF);
290 isr_fcr &= ~(UART_EXAR654_EFR_RTSDTR);
292 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
294 /* Now set our current start/stop chars while in enhanced mode */
295 writeb(ch->ch_startc, &ch->ch_cls_uart->mcr);
296 writeb(0, &ch->ch_cls_uart->lsr);
297 writeb(ch->ch_stopc, &ch->ch_cls_uart->msr);
298 writeb(0, &ch->ch_cls_uart->spr);
300 /* Write old LCR value back out, which turns enhanced access off */
301 writeb(lcrb, &ch->ch_cls_uart->lcr);
303 /* Disable interrupts for RTS flow */
304 ier &= ~(UART_EXAR654_IER_RTSDTR);
305 writeb(ier, &ch->ch_cls_uart->ier);
307 /* Set the usual FIFO values */
308 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
310 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
311 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
312 &ch->ch_cls_uart->isr_fcr);
317 static inline void cls_set_no_input_flow_control(struct channel_t *ch)
319 uchar lcrb = readb(&ch->ch_cls_uart->lcr);
320 uchar ier = readb(&ch->ch_cls_uart->ier);
323 DPR_PARAM(("Unsetting Input FLOW\n"));
326 * The Enhanced Register Set may only be accessed when
327 * the Line Control Register is set to 0xBFh.
329 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
331 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
333 /* Turn off IXOFF flow control, turn off RTS flow control */
334 isr_fcr |= (UART_EXAR654_EFR_ECB);
335 isr_fcr &= ~(UART_EXAR654_EFR_RTSDTR | UART_EXAR654_EFR_IXOFF);
337 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
339 /* Write old LCR value back out, which turns enhanced access off */
340 writeb(lcrb, &ch->ch_cls_uart->lcr);
342 /* Disable interrupts for RTS flow */
343 ier &= ~(UART_EXAR654_IER_RTSDTR);
344 writeb(ier, &ch->ch_cls_uart->ier);
346 /* Set the usual FIFO values */
347 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
349 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
350 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
351 &ch->ch_cls_uart->isr_fcr);
353 ch->ch_t_tlevel = 16;
354 ch->ch_r_tlevel = 16;
361 * Determines whether its time to shut off break condition.
363 * No locks are assumed to be held when calling this function.
364 * channel lock is held and released in this function.
366 static inline void cls_clear_break(struct channel_t *ch, int force)
370 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
373 DGNC_LOCK(ch->ch_lock, lock_flags);
375 /* Bail if we aren't currently sending a break. */
376 if (!ch->ch_stop_sending_break) {
377 DGNC_UNLOCK(ch->ch_lock, lock_flags);
381 /* Turn break off, and unset some variables */
382 if (ch->ch_flags & CH_BREAK_SENDING) {
383 if ((jiffies >= ch->ch_stop_sending_break) || force) {
384 uchar temp = readb(&ch->ch_cls_uart->lcr);
385 writeb((temp & ~UART_LCR_SBC), &ch->ch_cls_uart->lcr);
386 ch->ch_flags &= ~(CH_BREAK_SENDING);
387 ch->ch_stop_sending_break = 0;
388 DPR_IOCTL(("Finishing UART_LCR_SBC! finished: %lx\n", jiffies));
391 DGNC_UNLOCK(ch->ch_lock, lock_flags);
395 /* Parse the ISR register for the specific port */
396 static inline void cls_parse_isr(struct dgnc_board *brd, uint port)
398 struct channel_t *ch;
403 * No need to verify board pointer, it was already
404 * verified in the interrupt routine.
407 if (port > brd->nasync)
410 ch = brd->channels[port];
411 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
414 /* Here we try to figure out what caused the interrupt to happen */
417 isr = readb(&ch->ch_cls_uart->isr_fcr);
419 /* Bail if no pending interrupt on port */
420 if (isr & UART_IIR_NO_INT)
423 DPR_INTR(("%s:%d port: %x isr: %x\n", __FILE__, __LINE__, port, isr));
425 /* Receive Interrupt pending */
426 if (isr & (UART_IIR_RDI | UART_IIR_RDI_TIMEOUT)) {
427 /* Read data from uart -> queue */
430 cls_copy_data_from_uart_to_queue(ch);
431 dgnc_check_queue_flow_control(ch);
434 /* Transmit Hold register empty pending */
435 if (isr & UART_IIR_THRI) {
436 /* Transfer data (if any) from Write Queue -> UART. */
437 DGNC_LOCK(ch->ch_lock, lock_flags);
438 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
441 DGNC_UNLOCK(ch->ch_lock, lock_flags);
442 cls_copy_data_from_queue_to_uart(ch);
445 /* Received Xoff signal/Special character */
446 if (isr & UART_IIR_XOFF)
449 /* CTS/RTS change of state */
450 if (isr & UART_IIR_CTSRTS) {
454 * Don't need to do anything, the cls_parse_modem
455 * below will grab the updated modem signals.
459 /* Parse any modem signal changes */
460 DPR_INTR(("MOD_STAT: sending to parse_modem_sigs\n"));
461 cls_parse_modem(ch, readb(&ch->ch_cls_uart->msr));
468 * Send any/all changes to the line to the UART.
470 static void cls_param(struct tty_struct *tty)
478 struct dgnc_board *bd;
479 struct channel_t *ch;
482 if (!tty || tty->magic != TTY_MAGIC)
485 un = (struct un_t *) tty->driver_data;
486 if (!un || un->magic != DGNC_UNIT_MAGIC)
490 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
494 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
497 DPR_PARAM(("param start: tdev: %x cflags: %x oflags: %x iflags: %x\n",
498 ch->ch_tun.un_dev, ch->ch_c_cflag, ch->ch_c_oflag, ch->ch_c_iflag));
501 * If baud rate is zero, flush queues, and set mval to drop DTR.
503 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
504 ch->ch_r_head = ch->ch_r_tail = 0;
505 ch->ch_e_head = ch->ch_e_tail = 0;
506 ch->ch_w_head = ch->ch_w_tail = 0;
508 cls_flush_uart_write(ch);
509 cls_flush_uart_read(ch);
511 /* The baudrate is B0 so all modem lines are to be dropped. */
512 ch->ch_flags |= (CH_BAUD0);
513 ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
514 cls_assert_modem_signals(ch);
517 } else if (ch->ch_custom_speed) {
519 baud = ch->ch_custom_speed;
520 /* Handle transition from B0 */
521 if (ch->ch_flags & CH_BAUD0) {
522 ch->ch_flags &= ~(CH_BAUD0);
525 * Bring back up RTS and DTR...
526 * Also handle RTS or DTR toggle if set.
528 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
529 ch->ch_mostat |= (UART_MCR_RTS);
530 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
531 ch->ch_mostat |= (UART_MCR_DTR);
538 ulong bauds[4][16] = {
542 600, 1200, 1800, 2400,
543 4800, 9600, 19200, 38400 },
544 { /* slowbaud & CBAUDEX */
545 0, 57600, 115200, 230400,
546 460800, 150, 200, 921600,
547 600, 1200, 1800, 2400,
548 4800, 9600, 19200, 38400 },
550 0, 57600, 76800, 115200,
551 131657, 153600, 230400, 460800,
552 921600, 1200, 1800, 2400,
553 4800, 9600, 19200, 38400 },
554 { /* fastbaud & CBAUDEX */
555 0, 57600, 115200, 230400,
556 460800, 150, 200, 921600,
557 600, 1200, 1800, 2400,
558 4800, 9600, 19200, 38400 }
561 /* Only use the TXPrint baud rate if the terminal unit is NOT open */
562 if (!(ch->ch_tun.un_flags & UN_ISOPEN) && (un->un_type == DGNC_PRINT))
563 baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
565 baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
567 if (ch->ch_c_cflag & CBAUDEX)
570 if (ch->ch_digi.digi_flags & DIGI_FAST)
575 if ((iindex >= 0) && (iindex < 4) && (jindex >= 0) && (jindex < 16)) {
576 baud = bauds[iindex][jindex];
578 DPR_IOCTL(("baud indices were out of range (%d)(%d)",
586 /* Handle transition from B0 */
587 if (ch->ch_flags & CH_BAUD0) {
588 ch->ch_flags &= ~(CH_BAUD0);
591 * Bring back up RTS and DTR...
592 * Also handle RTS or DTR toggle if set.
594 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
595 ch->ch_mostat |= (UART_MCR_RTS);
596 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
597 ch->ch_mostat |= (UART_MCR_DTR);
601 if (ch->ch_c_cflag & PARENB) {
602 lcr |= UART_LCR_PARITY;
605 if (!(ch->ch_c_cflag & PARODD)) {
606 lcr |= UART_LCR_EPAR;
610 * Not all platforms support mark/space parity,
611 * so this will hide behind an ifdef.
614 if (ch->ch_c_cflag & CMSPAR)
615 lcr |= UART_LCR_SPAR;
618 if (ch->ch_c_cflag & CSTOPB)
619 lcr |= UART_LCR_STOP;
621 switch (ch->ch_c_cflag & CSIZE) {
623 lcr |= UART_LCR_WLEN5;
626 lcr |= UART_LCR_WLEN6;
629 lcr |= UART_LCR_WLEN7;
633 lcr |= UART_LCR_WLEN8;
637 ier = uart_ier = readb(&ch->ch_cls_uart->ier);
638 uart_lcr = readb(&ch->ch_cls_uart->lcr);
643 quot = ch->ch_bd->bd_dividend / baud;
645 if (quot != 0 && ch->ch_old_baud != baud) {
646 ch->ch_old_baud = baud;
647 writeb(UART_LCR_DLAB, &ch->ch_cls_uart->lcr);
648 writeb((quot & 0xff), &ch->ch_cls_uart->txrx);
649 writeb((quot >> 8), &ch->ch_cls_uart->ier);
650 writeb(lcr, &ch->ch_cls_uart->lcr);
654 writeb(lcr, &ch->ch_cls_uart->lcr);
656 if (ch->ch_c_cflag & CREAD) {
657 ier |= (UART_IER_RDI | UART_IER_RLSI);
660 ier &= ~(UART_IER_RDI | UART_IER_RLSI);
664 * Have the UART interrupt on modem signal changes ONLY when
665 * we are in hardware flow control mode, or CLOCAL/FORCEDCD is not set.
667 if ((ch->ch_digi.digi_flags & CTSPACE) || (ch->ch_digi.digi_flags & RTSPACE) ||
668 (ch->ch_c_cflag & CRTSCTS) || !(ch->ch_digi.digi_flags & DIGI_FORCEDCD) ||
669 !(ch->ch_c_cflag & CLOCAL))
674 ier &= ~UART_IER_MSI;
677 ier |= UART_IER_THRI;
680 writeb(ier, &ch->ch_cls_uart->ier);
682 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
683 cls_set_cts_flow_control(ch);
685 else if (ch->ch_c_iflag & IXON) {
686 /* If start/stop is set to disable, then we should disable flow control */
687 if ((ch->ch_startc == _POSIX_VDISABLE) || (ch->ch_stopc == _POSIX_VDISABLE))
688 cls_set_no_output_flow_control(ch);
690 cls_set_ixon_flow_control(ch);
693 cls_set_no_output_flow_control(ch);
696 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
697 cls_set_rts_flow_control(ch);
699 else if (ch->ch_c_iflag & IXOFF) {
700 /* If start/stop is set to disable, then we should disable flow control */
701 if ((ch->ch_startc == _POSIX_VDISABLE) || (ch->ch_stopc == _POSIX_VDISABLE))
702 cls_set_no_input_flow_control(ch);
704 cls_set_ixoff_flow_control(ch);
707 cls_set_no_input_flow_control(ch);
710 cls_assert_modem_signals(ch);
712 /* Get current status of the modem signals now */
713 cls_parse_modem(ch, readb(&ch->ch_cls_uart->msr));
718 * Our board poller function.
720 static void cls_tasklet(unsigned long data)
722 struct dgnc_board *bd = (struct dgnc_board *) data;
723 struct channel_t *ch;
729 if (!bd || bd->magic != DGNC_BOARD_MAGIC) {
730 APR(("poll_tasklet() - NULL or bad bd.\n"));
734 /* Cache a couple board values */
735 DGNC_LOCK(bd->bd_lock, lock_flags);
738 DGNC_UNLOCK(bd->bd_lock, lock_flags);
741 * Do NOT allow the interrupt routine to read the intr registers
742 * Until we release this lock.
744 DGNC_LOCK(bd->bd_intr_lock, lock_flags);
747 * If board is ready, parse deeper to see if there is anything to do.
749 if ((state == BOARD_READY) && (ports > 0)) {
751 /* Loop on each port */
752 for (i = 0; i < ports; i++) {
753 ch = bd->channels[i];
758 * NOTE: Remember you CANNOT hold any channel
759 * locks when calling input.
760 * During input processing, its possible we
761 * will call ld, which might do callbacks back
767 * Channel lock is grabbed and then released
768 * inside this routine.
770 cls_copy_data_from_queue_to_uart(ch);
771 dgnc_wakeup_writes(ch);
774 * Check carrier function.
779 * The timing check of turning off the break is done
780 * inside clear_break()
782 if (ch->ch_stop_sending_break)
783 cls_clear_break(ch, 0);
787 DGNC_UNLOCK(bd->bd_intr_lock, lock_flags);
795 * Classic specific interrupt handler.
797 static irqreturn_t cls_intr(int irq, void *voidbrd)
799 struct dgnc_board *brd = (struct dgnc_board *) voidbrd;
802 unsigned long lock_flags;
805 APR(("Received interrupt (%d) with null board associated\n", irq));
810 * Check to make sure its for us.
812 if (brd->magic != DGNC_BOARD_MAGIC) {
813 APR(("Received interrupt (%d) with a board pointer that wasn't ours!\n", irq));
817 DGNC_LOCK(brd->bd_intr_lock, lock_flags);
822 * Check the board's global interrupt offset to see if we
823 * we actually do have an interrupt pending for us.
825 poll_reg = readb(brd->re_map_membase + UART_CLASSIC_POLL_ADDR_OFFSET);
827 /* If 0, no interrupts pending */
829 DPR_INTR(("Kernel interrupted to me, but no pending interrupts...\n"));
830 DGNC_UNLOCK(brd->bd_intr_lock, lock_flags);
834 DPR_INTR(("%s:%d poll_reg: %x\n", __FILE__, __LINE__, poll_reg));
836 /* Parse each port to find out what caused the interrupt */
837 for (i = 0; i < brd->nasync; i++) {
838 cls_parse_isr(brd, i);
842 * Schedule tasklet to more in-depth servicing at a better time.
844 tasklet_schedule(&brd->helper_tasklet);
846 DGNC_UNLOCK(brd->bd_intr_lock, lock_flags);
848 DPR_INTR(("dgnc_intr finish.\n"));
853 static void cls_disable_receiver(struct channel_t *ch)
855 uchar tmp = readb(&ch->ch_cls_uart->ier);
856 tmp &= ~(UART_IER_RDI);
857 writeb(tmp, &ch->ch_cls_uart->ier);
861 static void cls_enable_receiver(struct channel_t *ch)
863 uchar tmp = readb(&ch->ch_cls_uart->ier);
864 tmp |= (UART_IER_RDI);
865 writeb(tmp, &ch->ch_cls_uart->ier);
869 static void cls_copy_data_from_uart_to_queue(struct channel_t *ch)
872 uchar linestatus = 0;
873 uchar error_mask = 0;
878 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
881 DGNC_LOCK(ch->ch_lock, lock_flags);
883 /* cache head and tail of queue */
884 head = ch->ch_r_head;
885 tail = ch->ch_r_tail;
887 /* Store how much space we have left in the queue */
888 if ((qleft = tail - head - 1) < 0)
889 qleft += RQUEUEMASK + 1;
892 * Create a mask to determine whether we should
893 * insert the character (if any) into our queue.
895 if (ch->ch_c_iflag & IGNBRK)
896 error_mask |= UART_LSR_BI;
899 linestatus = readb(&ch->ch_cls_uart->lsr);
901 if (!(linestatus & (UART_LSR_DR)))
905 * Discard character if we are ignoring the error mask.
907 if (linestatus & error_mask) {
910 discard = readb(&ch->ch_cls_uart->txrx);
915 * If our queue is full, we have no choice but to drop some data.
916 * The assumption is that HWFLOW or SWFLOW should have stopped
917 * things way way before we got to this point.
919 * I decided that I wanted to ditch the oldest data first,
920 * I hope thats okay with everyone? Yes? Good.
923 DPR_READ(("Queue full, dropping DATA:%x LSR:%x\n",
924 ch->ch_rqueue[tail], ch->ch_equeue[tail]));
926 ch->ch_r_tail = tail = (tail + 1) & RQUEUEMASK;
927 ch->ch_err_overrun++;
931 ch->ch_equeue[head] = linestatus & (UART_LSR_BI | UART_LSR_PE | UART_LSR_FE);
932 ch->ch_rqueue[head] = readb(&ch->ch_cls_uart->txrx);
933 dgnc_sniff_nowait_nolock(ch, "UART READ", ch->ch_rqueue + head, 1);
937 DPR_READ(("DATA/LSR pair: %x %x\n", ch->ch_rqueue[head], ch->ch_equeue[head]));
939 if (ch->ch_equeue[head] & UART_LSR_PE)
941 if (ch->ch_equeue[head] & UART_LSR_BI)
943 if (ch->ch_equeue[head] & UART_LSR_FE)
946 /* Add to, and flip head if needed */
947 head = (head + 1) & RQUEUEMASK;
952 * Write new final heads to channel structure.
954 ch->ch_r_head = head & RQUEUEMASK;
955 ch->ch_e_head = head & EQUEUEMASK;
957 DGNC_UNLOCK(ch->ch_lock, lock_flags);
962 * This function basically goes to sleep for secs, or until
963 * it gets signalled that the port has fully drained.
965 static int cls_drain(struct tty_struct *tty, uint seconds)
968 struct channel_t *ch;
972 if (!tty || tty->magic != TTY_MAGIC) {
976 un = (struct un_t *) tty->driver_data;
977 if (!un || un->magic != DGNC_UNIT_MAGIC) {
982 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC) {
986 DGNC_LOCK(ch->ch_lock, lock_flags);
987 un->un_flags |= UN_EMPTY;
988 DGNC_UNLOCK(ch->ch_lock, lock_flags);
991 * NOTE: Do something with time passed in.
993 rc = wait_event_interruptible(un->un_flags_wait, ((un->un_flags & UN_EMPTY) == 0));
995 /* If ret is non-zero, user ctrl-c'ed us */
997 DPR_IOCTL(("%d Drain - User ctrl c'ed\n", __LINE__));
1003 /* Channel lock MUST be held before calling this function! */
1004 static void cls_flush_uart_write(struct channel_t *ch)
1006 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC) {
1010 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT), &ch->ch_cls_uart->isr_fcr);
1013 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1017 /* Channel lock MUST be held before calling this function! */
1018 static void cls_flush_uart_read(struct channel_t *ch)
1020 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC) {
1025 * For complete POSIX compatibility, we should be purging the
1026 * read FIFO in the UART here.
1028 * However, doing the statement below also incorrectly flushes
1029 * write data as well as just basically trashing the FIFO.
1031 * I believe this is a BUG in this UART.
1032 * So for now, we will leave the code #ifdef'ed out...
1035 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR), &ch->ch_cls_uart->isr_fcr);
1041 static void cls_copy_data_from_queue_to_uart(struct channel_t *ch)
1047 uint len_written = 0;
1050 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1053 DGNC_LOCK(ch->ch_lock, lock_flags);
1055 /* No data to write to the UART */
1056 if (ch->ch_w_tail == ch->ch_w_head) {
1057 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1061 /* If port is "stopped", don't send any data to the UART */
1062 if ((ch->ch_flags & CH_FORCED_STOP) || (ch->ch_flags & CH_BREAK_SENDING)) {
1063 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1067 if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM))) {
1068 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1074 /* cache head and tail of queue */
1075 head = ch->ch_w_head & WQUEUEMASK;
1076 tail = ch->ch_w_tail & WQUEUEMASK;
1077 qlen = (head - tail) & WQUEUEMASK;
1079 /* Find minimum of the FIFO space, versus queue length */
1085 * If RTS Toggle mode is on, turn on RTS now if not already set,
1086 * and make sure we get an event when the data transfer has completed.
1088 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1089 if (!(ch->ch_mostat & UART_MCR_RTS)) {
1090 ch->ch_mostat |= (UART_MCR_RTS);
1091 cls_assert_modem_signals(ch);
1093 ch->ch_tun.un_flags |= (UN_EMPTY);
1097 * If DTR Toggle mode is on, turn on DTR now if not already set,
1098 * and make sure we get an event when the data transfer has completed.
1100 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1101 if (!(ch->ch_mostat & UART_MCR_DTR)) {
1102 ch->ch_mostat |= (UART_MCR_DTR);
1103 cls_assert_modem_signals(ch);
1105 ch->ch_tun.un_flags |= (UN_EMPTY);
1107 writeb(ch->ch_wqueue[ch->ch_w_tail], &ch->ch_cls_uart->txrx);
1108 dgnc_sniff_nowait_nolock(ch, "UART WRITE", ch->ch_wqueue + ch->ch_w_tail, 1);
1109 DPR_WRITE(("Tx data: %x\n", ch->ch_wqueue[ch->ch_w_tail]));
1111 ch->ch_w_tail &= WQUEUEMASK;
1117 if (len_written > 0)
1118 ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1120 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1126 static void cls_parse_modem(struct channel_t *ch, uchar signals)
1128 volatile uchar msignals = signals;
1130 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1133 DPR_MSIGS(("cls_parse_modem: port: %d signals: %d\n", ch->ch_portnum, msignals));
1136 * Do altpin switching. Altpin switches DCD and DSR.
1137 * This prolly breaks DSRPACE, so we should be more clever here.
1139 if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
1140 uchar mswap = signals;
1141 if (mswap & UART_MSR_DDCD) {
1142 msignals &= ~UART_MSR_DDCD;
1143 msignals |= UART_MSR_DDSR;
1145 if (mswap & UART_MSR_DDSR) {
1146 msignals &= ~UART_MSR_DDSR;
1147 msignals |= UART_MSR_DDCD;
1149 if (mswap & UART_MSR_DCD) {
1150 msignals &= ~UART_MSR_DCD;
1151 msignals |= UART_MSR_DSR;
1153 if (mswap & UART_MSR_DSR) {
1154 msignals &= ~UART_MSR_DSR;
1155 msignals |= UART_MSR_DCD;
1159 /* Scrub off lower bits. They signify delta's, which I don't care about */
1162 if (msignals & UART_MSR_DCD)
1163 ch->ch_mistat |= UART_MSR_DCD;
1165 ch->ch_mistat &= ~UART_MSR_DCD;
1167 if (msignals & UART_MSR_DSR)
1168 ch->ch_mistat |= UART_MSR_DSR;
1170 ch->ch_mistat &= ~UART_MSR_DSR;
1172 if (msignals & UART_MSR_RI)
1173 ch->ch_mistat |= UART_MSR_RI;
1175 ch->ch_mistat &= ~UART_MSR_RI;
1177 if (msignals & UART_MSR_CTS)
1178 ch->ch_mistat |= UART_MSR_CTS;
1180 ch->ch_mistat &= ~UART_MSR_CTS;
1183 DPR_MSIGS(("Port: %d DTR: %d RTS: %d CTS: %d DSR: %d " "RI: %d CD: %d\n",
1185 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_DTR),
1186 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_RTS),
1187 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_CTS),
1188 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DSR),
1189 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_RI),
1190 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DCD)));
1194 /* Make the UART raise any of the output signals we want up */
1195 static void cls_assert_modem_signals(struct channel_t *ch)
1199 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1202 out = ch->ch_mostat;
1204 if (ch->ch_flags & CH_LOOPBACK)
1205 out |= UART_MCR_LOOP;
1207 writeb(out, &ch->ch_cls_uart->mcr);
1209 /* Give time for the UART to actually drop the signals */
1214 static void cls_send_start_character(struct channel_t *ch)
1216 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1219 if (ch->ch_startc != _POSIX_VDISABLE) {
1221 writeb(ch->ch_startc, &ch->ch_cls_uart->txrx);
1226 static void cls_send_stop_character(struct channel_t *ch)
1228 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1231 if (ch->ch_stopc != _POSIX_VDISABLE) {
1232 ch->ch_xoff_sends++;
1233 writeb(ch->ch_stopc, &ch->ch_cls_uart->txrx);
1239 static void cls_uart_init(struct channel_t *ch)
1241 uchar lcrb = readb(&ch->ch_cls_uart->lcr);
1244 writeb(0, &ch->ch_cls_uart->ier);
1247 * The Enhanced Register Set may only be accessed when
1248 * the Line Control Register is set to 0xBFh.
1250 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
1252 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
1254 /* Turn on Enhanced/Extended controls */
1255 isr_fcr |= (UART_EXAR654_EFR_ECB);
1257 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
1259 /* Write old LCR value back out, which turns enhanced access off */
1260 writeb(lcrb, &ch->ch_cls_uart->lcr);
1262 /* Clear out UART and FIFO */
1263 readb(&ch->ch_cls_uart->txrx);
1265 writeb((UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT), &ch->ch_cls_uart->isr_fcr);
1268 ch->ch_flags |= (CH_FIFO_ENABLED | CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1270 readb(&ch->ch_cls_uart->lsr);
1271 readb(&ch->ch_cls_uart->msr);
1278 static void cls_uart_off(struct channel_t *ch)
1280 writeb(0, &ch->ch_cls_uart->ier);
1285 * cls_get_uarts_bytes_left.
1286 * Returns 0 is nothing left in the FIFO, returns 1 otherwise.
1288 * The channel lock MUST be held by the calling function.
1290 static uint cls_get_uart_bytes_left(struct channel_t *ch)
1295 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1298 lsr = readb(&ch->ch_cls_uart->lsr);
1300 /* Determine whether the Transmitter is empty or not */
1301 if (!(lsr & UART_LSR_TEMT)) {
1302 if (ch->ch_flags & CH_TX_FIFO_EMPTY)
1303 tasklet_schedule(&ch->ch_bd->helper_tasklet);
1307 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1317 * Starts sending a break thru the UART.
1319 * The channel lock MUST be held by the calling function.
1321 static void cls_send_break(struct channel_t *ch, int msecs)
1323 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1327 * If we receive a time of 0, this means turn off the break.
1330 /* Turn break off, and unset some variables */
1331 if (ch->ch_flags & CH_BREAK_SENDING) {
1332 uchar temp = readb(&ch->ch_cls_uart->lcr);
1333 writeb((temp & ~UART_LCR_SBC), &ch->ch_cls_uart->lcr);
1334 ch->ch_flags &= ~(CH_BREAK_SENDING);
1335 ch->ch_stop_sending_break = 0;
1336 DPR_IOCTL(("Finishing UART_LCR_SBC! finished: %lx\n", jiffies));
1342 * Set the time we should stop sending the break.
1343 * If we are already sending a break, toss away the existing
1344 * time to stop, and use this new value instead.
1346 ch->ch_stop_sending_break = jiffies + dgnc_jiffies_from_ms(msecs);
1348 /* Tell the UART to start sending the break */
1349 if (!(ch->ch_flags & CH_BREAK_SENDING)) {
1350 uchar temp = readb(&ch->ch_cls_uart->lcr);
1351 writeb((temp | UART_LCR_SBC), &ch->ch_cls_uart->lcr);
1352 ch->ch_flags |= (CH_BREAK_SENDING);
1353 DPR_IOCTL(("Port %d. Starting UART_LCR_SBC! start: %lx should end: %lx\n",
1354 ch->ch_portnum, jiffies, ch->ch_stop_sending_break));
1360 * cls_send_immediate_char.
1361 * Sends a specific character as soon as possible to the UART,
1362 * jumping over any bytes that might be in the write queue.
1364 * The channel lock MUST be held by the calling function.
1366 static void cls_send_immediate_char(struct channel_t *ch, unsigned char c)
1368 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1371 writeb(c, &ch->ch_cls_uart->txrx);
1374 static void cls_vpd(struct dgnc_board *brd)
1376 ulong vpdbase; /* Start of io base of the card */
1377 u8 __iomem *re_map_vpdbase;/* Remapped memory of the card */
1381 vpdbase = pci_resource_start(brd->pdev, 3);
1387 re_map_vpdbase = ioremap(vpdbase, 0x400);
1389 if (!re_map_vpdbase)
1392 /* Store the VPD into our buffer */
1393 for (i = 0; i < 0x40; i++) {
1394 brd->vpd[i] = readb(re_map_vpdbase + i);
1395 printk("%x ", brd->vpd[i]);
1400 iounmap(re_map_vpdbase);