2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
7 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/timer.h>
26 #include <linux/interrupt.h>
27 #include <linux/tty.h>
28 #include <linux/tty_flip.h>
29 #include <linux/serial.h>
30 #include <linux/serial_reg.h>
31 #include <linux/major.h>
32 #include <linux/string.h>
33 #include <linux/fcntl.h>
34 #include <linux/ptrace.h>
35 #include <linux/gfp.h>
36 #include <linux/ioport.h>
38 #include <linux/delay.h>
39 #include <linux/pci.h>
40 #include <linux/bitops.h>
42 #include <asm/system.h>
45 #include <asm/uaccess.h>
49 #define MXSER_VERSION "2.0.4" /* 1.12 */
50 #define MXSERMAJOR 174
51 #define MXSERCUMAJOR 175
53 #define MXSER_BOARDS 4 /* Max. boards */
54 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
55 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
56 #define MXSER_ISR_PASS_LIMIT 100
58 #define MXSER_ERR_IOADDR -1
59 #define MXSER_ERR_IRQ -2
60 #define MXSER_ERR_IRQ_CONFLIT -3
61 #define MXSER_ERR_VECTOR -4
63 /*CheckIsMoxaMust return value*/
64 #define MOXA_OTHER_UART 0x00
65 #define MOXA_MUST_MU150_HWID 0x01
66 #define MOXA_MUST_MU860_HWID 0x02
68 #define WAKEUP_CHARS 256
70 #define UART_MCR_AFE 0x20
71 #define UART_LSR_SPECIAL 0x1E
73 #define PCI_DEVICE_ID_POS104UL 0x1044
74 #define PCI_DEVICE_ID_CB108 0x1080
75 #define PCI_DEVICE_ID_CP102UF 0x1023
76 #define PCI_DEVICE_ID_CB114 0x1142
77 #define PCI_DEVICE_ID_CP114UL 0x1143
78 #define PCI_DEVICE_ID_CB134I 0x1341
79 #define PCI_DEVICE_ID_CP138U 0x1380
82 #define C168_ASIC_ID 1
83 #define C104_ASIC_ID 2
84 #define C102_ASIC_ID 0xB
85 #define CI132_ASIC_ID 4
86 #define CI134_ASIC_ID 3
87 #define CI104J_ASIC_ID 5
89 #define MXSER_HIGHBAUD 1
92 /* This is only for PCI */
102 } Gpci_uart_info[] = {
103 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
104 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
105 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
107 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
109 struct mxser_cardinfo {
115 static const struct mxser_cardinfo mxser_cards[] = {
116 /* 0*/ { "C168 series", 8, },
117 { "C104 series", 4, },
118 { "CI-104J series", 4, },
119 { "C168H/PCI series", 8, },
120 { "C104H/PCI series", 4, },
121 /* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
122 { "CI-132 series", 4, MXSER_HAS2 },
123 { "CI-134 series", 4, },
124 { "CP-132 series", 2, },
125 { "CP-114 series", 4, },
126 /*10*/ { "CT-114 series", 4, },
127 { "CP-102 series", 2, MXSER_HIGHBAUD },
128 { "CP-104U series", 4, },
129 { "CP-168U series", 8, },
130 { "CP-132U series", 2, },
131 /*15*/ { "CP-134U series", 4, },
132 { "CP-104JU series", 4, },
133 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
134 { "CP-118U series", 8, },
135 { "CP-102UL series", 2, },
136 /*20*/ { "CP-102U series", 2, },
137 { "CP-118EL series", 8, },
138 { "CP-168EL series", 8, },
139 { "CP-104EL series", 4, },
140 { "CB-108 series", 8, },
141 /*25*/ { "CB-114 series", 4, },
142 { "CB-134I series", 4, },
143 { "CP-138U series", 8, },
144 { "POS-104UL series", 4, },
145 { "CP-114UL series", 4, },
146 /*30*/ { "CP-102UF series", 2, }
149 /* driver_data correspond to the lines in the structure above
150 see also ISA probe function before you change something */
151 static struct pci_device_id mxser_pcibrds[] = {
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
173 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
174 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
175 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
176 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
179 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
181 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
182 static int ttymajor = MXSERMAJOR;
184 /* Variables for insmod */
186 MODULE_AUTHOR("Casper Yang");
187 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
188 module_param_array(ioaddr, int, NULL, 0);
189 module_param(ttymajor, int, 0);
190 MODULE_LICENSE("GPL");
194 unsigned long rxcnt[MXSER_PORTS];
195 unsigned long txcnt[MXSER_PORTS];
202 unsigned long up_rxcnt;
203 unsigned long up_txcnt;
205 unsigned char hold_reason;
208 struct mxser_mon_ext {
209 unsigned long rx_cnt[32];
210 unsigned long tx_cnt[32];
211 unsigned long up_rxcnt[32];
212 unsigned long up_txcnt[32];
213 int modem_status[32];
227 struct tty_port port;
228 struct mxser_board *board;
230 unsigned long ioaddr;
231 unsigned long opmode_ioaddr;
235 int rx_trigger; /* Rx fifo trigger level */
237 int baud_base; /* max. speed */
238 int type; /* UART type */
240 int x_char; /* xon/xoff character */
241 int IER; /* Interrupt Enable Register */
242 int MCR; /* Modem control register */
244 unsigned char stop_rx;
245 unsigned char ldisc_stop_rx;
248 unsigned char err_shadow;
250 struct async_icount icount; /* kernel counters for 4 input interrupts */
253 int read_status_mask;
254 int ignore_status_mask;
260 struct ktermios normal_termios;
262 struct mxser_mon mon_data;
265 wait_queue_head_t delta_msr_wait;
271 const struct mxser_cardinfo *info;
272 unsigned long vector;
273 unsigned long vector_mask;
278 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
281 struct mxser_mstatus {
289 static int mxserBoardCAP[MXSER_BOARDS] = {
291 /* 0x180, 0x280, 0x200, 0x320 */
294 static struct mxser_board mxser_boards[MXSER_BOARDS];
295 static struct tty_driver *mxvar_sdriver;
296 static struct mxser_log mxvar_log;
297 static int mxser_set_baud_method[MXSER_PORTS + 1];
299 static void mxser_enable_must_enchance_mode(unsigned long baseio)
304 oldlcr = inb(baseio + UART_LCR);
305 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
307 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
308 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
310 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
311 outb(oldlcr, baseio + UART_LCR);
314 static void mxser_disable_must_enchance_mode(unsigned long baseio)
319 oldlcr = inb(baseio + UART_LCR);
320 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
322 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
323 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
325 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
326 outb(oldlcr, baseio + UART_LCR);
329 static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
334 oldlcr = inb(baseio + UART_LCR);
335 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
337 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
338 efr &= ~MOXA_MUST_EFR_BANK_MASK;
339 efr |= MOXA_MUST_EFR_BANK0;
341 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
342 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
343 outb(oldlcr, baseio + UART_LCR);
346 static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
351 oldlcr = inb(baseio + UART_LCR);
352 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
354 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
355 efr &= ~MOXA_MUST_EFR_BANK_MASK;
356 efr |= MOXA_MUST_EFR_BANK0;
358 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
359 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
360 outb(oldlcr, baseio + UART_LCR);
363 static void mxser_set_must_fifo_value(struct mxser_port *info)
368 oldlcr = inb(info->ioaddr + UART_LCR);
369 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
371 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
372 efr &= ~MOXA_MUST_EFR_BANK_MASK;
373 efr |= MOXA_MUST_EFR_BANK1;
375 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
376 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
377 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
378 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
379 outb(oldlcr, info->ioaddr + UART_LCR);
382 static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
387 oldlcr = inb(baseio + UART_LCR);
388 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
390 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
391 efr &= ~MOXA_MUST_EFR_BANK_MASK;
392 efr |= MOXA_MUST_EFR_BANK2;
394 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
395 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
396 outb(oldlcr, baseio + UART_LCR);
399 static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
404 oldlcr = inb(baseio + UART_LCR);
405 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
407 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
408 efr &= ~MOXA_MUST_EFR_BANK_MASK;
409 efr |= MOXA_MUST_EFR_BANK2;
411 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
412 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
413 outb(oldlcr, baseio + UART_LCR);
416 static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
421 oldlcr = inb(baseio + UART_LCR);
422 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
424 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
425 efr &= ~MOXA_MUST_EFR_SF_MASK;
427 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
428 outb(oldlcr, baseio + UART_LCR);
431 static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
436 oldlcr = inb(baseio + UART_LCR);
437 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
439 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
440 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
441 efr |= MOXA_MUST_EFR_SF_TX1;
443 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
444 outb(oldlcr, baseio + UART_LCR);
447 static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
452 oldlcr = inb(baseio + UART_LCR);
453 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
455 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
456 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
458 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
459 outb(oldlcr, baseio + UART_LCR);
462 static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
467 oldlcr = inb(baseio + UART_LCR);
468 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
470 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
471 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
472 efr |= MOXA_MUST_EFR_SF_RX1;
474 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
475 outb(oldlcr, baseio + UART_LCR);
478 static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
483 oldlcr = inb(baseio + UART_LCR);
484 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
486 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
487 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
489 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
490 outb(oldlcr, baseio + UART_LCR);
494 static int __devinit CheckIsMoxaMust(unsigned long io)
499 outb(0, io + UART_LCR);
500 mxser_disable_must_enchance_mode(io);
501 oldmcr = inb(io + UART_MCR);
502 outb(0, io + UART_MCR);
503 mxser_set_must_xon1_value(io, 0x11);
504 if ((hwid = inb(io + UART_MCR)) != 0) {
505 outb(oldmcr, io + UART_MCR);
506 return MOXA_OTHER_UART;
509 mxser_get_must_hardware_id(io, &hwid);
510 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
511 if (hwid == Gpci_uart_info[i].type)
514 return MOXA_OTHER_UART;
518 static void process_txrx_fifo(struct mxser_port *info)
522 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
523 info->rx_trigger = 1;
524 info->rx_high_water = 1;
525 info->rx_low_water = 1;
526 info->xmit_fifo_size = 1;
528 for (i = 0; i < UART_INFO_NUM; i++)
529 if (info->board->chip_flag == Gpci_uart_info[i].type) {
530 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
531 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
532 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
533 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
538 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
540 static unsigned char mxser_msr[MXSER_PORTS + 1];
541 unsigned char status = 0;
543 status = inb(baseaddr + UART_MSR);
545 mxser_msr[port] &= 0x0F;
546 mxser_msr[port] |= status;
547 status = mxser_msr[port];
554 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
555 struct mxser_port *port)
557 DECLARE_WAITQUEUE(wait, current);
563 * If non-blocking mode is set, or the port is not enabled,
564 * then make the check up front and then exit.
566 if ((filp->f_flags & O_NONBLOCK) ||
567 test_bit(TTY_IO_ERROR, &tty->flags)) {
568 port->port.flags |= ASYNC_NORMAL_ACTIVE;
572 if (tty->termios->c_cflag & CLOCAL)
576 * Block waiting for the carrier detect and the line to become
577 * free (i.e., not in use by the callout). While we are in
578 * this loop, port->port.count is dropped by one, so that
579 * mxser_close() knows when to free things. We restore it upon
580 * exit, either normal or abnormal.
583 add_wait_queue(&port->port.open_wait, &wait);
585 spin_lock_irqsave(&port->slock, flags);
586 if (!tty_hung_up_p(filp))
588 spin_unlock_irqrestore(&port->slock, flags);
589 port->port.blocked_open++;
591 spin_lock_irqsave(&port->slock, flags);
592 outb(inb(port->ioaddr + UART_MCR) |
593 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
594 spin_unlock_irqrestore(&port->slock, flags);
595 set_current_state(TASK_INTERRUPTIBLE);
596 if (tty_hung_up_p(filp) || !(port->port.flags & ASYNC_INITIALIZED)) {
597 if (port->port.flags & ASYNC_HUP_NOTIFY)
600 retval = -ERESTARTSYS;
603 if (!(port->port.flags & ASYNC_CLOSING) &&
605 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
607 if (signal_pending(current)) {
608 retval = -ERESTARTSYS;
613 set_current_state(TASK_RUNNING);
614 remove_wait_queue(&port->port.open_wait, &wait);
615 if (!tty_hung_up_p(filp))
617 port->port.blocked_open--;
620 port->port.flags |= ASYNC_NORMAL_ACTIVE;
624 static int mxser_set_baud(struct mxser_port *info, long newspd)
629 if (!info->port.tty || !info->port.tty->termios)
635 if (newspd > info->max_baud)
639 quot = 2 * info->baud_base / 269;
640 tty_encode_baud_rate(info->port.tty, 134, 134);
642 quot = info->baud_base / newspd;
645 baud = info->baud_base/quot;
646 tty_encode_baud_rate(info->port.tty, baud, baud);
651 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
652 info->timeout += HZ / 50; /* Add .02 seconds of slop */
655 info->MCR |= UART_MCR_DTR;
656 outb(info->MCR, info->ioaddr + UART_MCR);
658 info->MCR &= ~UART_MCR_DTR;
659 outb(info->MCR, info->ioaddr + UART_MCR);
663 cval = inb(info->ioaddr + UART_LCR);
665 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
667 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
668 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
669 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
672 if (C_BAUD(info->port.tty) == BOTHER) {
673 quot = info->baud_base % newspd;
675 if (quot % newspd > newspd / 2) {
681 mxser_set_must_enum_value(info->ioaddr, quot);
684 mxser_set_must_enum_value(info->ioaddr, 0);
690 * This routine is called to set the UART divisor registers to match
691 * the specified baud rate for a serial port.
693 static int mxser_change_speed(struct mxser_port *info,
694 struct ktermios *old_termios)
696 unsigned cflag, cval, fcr;
698 unsigned char status;
700 if (!info->port.tty || !info->port.tty->termios)
702 cflag = info->port.tty->termios->c_cflag;
706 if (mxser_set_baud_method[info->port.tty->index] == 0)
707 mxser_set_baud(info, tty_get_baud_rate(info->port.tty));
709 /* byte size and parity */
710 switch (cflag & CSIZE) {
725 break; /* too keep GCC shut... */
730 cval |= UART_LCR_PARITY;
731 if (!(cflag & PARODD))
732 cval |= UART_LCR_EPAR;
734 cval |= UART_LCR_SPAR;
736 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
737 if (info->board->chip_flag) {
738 fcr = UART_FCR_ENABLE_FIFO;
739 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
740 mxser_set_must_fifo_value(info);
744 fcr = UART_FCR_ENABLE_FIFO;
745 if (info->board->chip_flag) {
746 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
747 mxser_set_must_fifo_value(info);
749 switch (info->rx_trigger) {
751 fcr |= UART_FCR_TRIGGER_1;
754 fcr |= UART_FCR_TRIGGER_4;
757 fcr |= UART_FCR_TRIGGER_8;
760 fcr |= UART_FCR_TRIGGER_14;
766 /* CTS flow control flag and modem status interrupts */
767 info->IER &= ~UART_IER_MSI;
768 info->MCR &= ~UART_MCR_AFE;
769 if (cflag & CRTSCTS) {
770 info->port.flags |= ASYNC_CTS_FLOW;
771 info->IER |= UART_IER_MSI;
772 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
773 info->MCR |= UART_MCR_AFE;
775 status = inb(info->ioaddr + UART_MSR);
776 if (info->port.tty->hw_stopped) {
777 if (status & UART_MSR_CTS) {
778 info->port.tty->hw_stopped = 0;
779 if (info->type != PORT_16550A &&
780 !info->board->chip_flag) {
781 outb(info->IER & ~UART_IER_THRI,
784 info->IER |= UART_IER_THRI;
785 outb(info->IER, info->ioaddr +
788 tty_wakeup(info->port.tty);
791 if (!(status & UART_MSR_CTS)) {
792 info->port.tty->hw_stopped = 1;
793 if ((info->type != PORT_16550A) &&
794 (!info->board->chip_flag)) {
795 info->IER &= ~UART_IER_THRI;
796 outb(info->IER, info->ioaddr +
803 info->port.flags &= ~ASYNC_CTS_FLOW;
805 outb(info->MCR, info->ioaddr + UART_MCR);
806 if (cflag & CLOCAL) {
807 info->port.flags &= ~ASYNC_CHECK_CD;
809 info->port.flags |= ASYNC_CHECK_CD;
810 info->IER |= UART_IER_MSI;
812 outb(info->IER, info->ioaddr + UART_IER);
815 * Set up parity check flag
817 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
818 if (I_INPCK(info->port.tty))
819 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
820 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
821 info->read_status_mask |= UART_LSR_BI;
823 info->ignore_status_mask = 0;
825 if (I_IGNBRK(info->port.tty)) {
826 info->ignore_status_mask |= UART_LSR_BI;
827 info->read_status_mask |= UART_LSR_BI;
829 * If we're ignore parity and break indicators, ignore
830 * overruns too. (For real raw support).
832 if (I_IGNPAR(info->port.tty)) {
833 info->ignore_status_mask |=
837 info->read_status_mask |=
843 if (info->board->chip_flag) {
844 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(info->port.tty));
845 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(info->port.tty));
846 if (I_IXON(info->port.tty)) {
847 mxser_enable_must_rx_software_flow_control(
850 mxser_disable_must_rx_software_flow_control(
853 if (I_IXOFF(info->port.tty)) {
854 mxser_enable_must_tx_software_flow_control(
857 mxser_disable_must_tx_software_flow_control(
863 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
864 outb(cval, info->ioaddr + UART_LCR);
869 static void mxser_check_modem_status(struct mxser_port *port, int status)
871 /* update input line counters */
872 if (status & UART_MSR_TERI)
874 if (status & UART_MSR_DDSR)
876 if (status & UART_MSR_DDCD)
878 if (status & UART_MSR_DCTS)
880 port->mon_data.modem_status = status;
881 wake_up_interruptible(&port->delta_msr_wait);
883 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
884 if (status & UART_MSR_DCD)
885 wake_up_interruptible(&port->port.open_wait);
888 if (port->port.flags & ASYNC_CTS_FLOW) {
889 if (port->port.tty->hw_stopped) {
890 if (status & UART_MSR_CTS) {
891 port->port.tty->hw_stopped = 0;
893 if ((port->type != PORT_16550A) &&
894 (!port->board->chip_flag)) {
895 outb(port->IER & ~UART_IER_THRI,
896 port->ioaddr + UART_IER);
897 port->IER |= UART_IER_THRI;
898 outb(port->IER, port->ioaddr +
901 tty_wakeup(port->port.tty);
904 if (!(status & UART_MSR_CTS)) {
905 port->port.tty->hw_stopped = 1;
906 if (port->type != PORT_16550A &&
907 !port->board->chip_flag) {
908 port->IER &= ~UART_IER_THRI;
909 outb(port->IER, port->ioaddr +
917 static int mxser_startup(struct mxser_port *info)
922 page = __get_free_page(GFP_KERNEL);
926 spin_lock_irqsave(&info->slock, flags);
928 if (info->port.flags & ASYNC_INITIALIZED) {
930 spin_unlock_irqrestore(&info->slock, flags);
934 if (!info->ioaddr || !info->type) {
936 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
938 spin_unlock_irqrestore(&info->slock, flags);
941 if (info->port.xmit_buf)
944 info->port.xmit_buf = (unsigned char *) page;
947 * Clear the FIFO buffers and disable them
948 * (they will be reenabled in mxser_change_speed())
950 if (info->board->chip_flag)
951 outb((UART_FCR_CLEAR_RCVR |
952 UART_FCR_CLEAR_XMIT |
953 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
955 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
956 info->ioaddr + UART_FCR);
959 * At this point there's no way the LSR could still be 0xFF;
960 * if it is, then bail out, because there's likely no UART
963 if (inb(info->ioaddr + UART_LSR) == 0xff) {
964 spin_unlock_irqrestore(&info->slock, flags);
965 if (capable(CAP_SYS_ADMIN)) {
967 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
974 * Clear the interrupt registers.
976 (void) inb(info->ioaddr + UART_LSR);
977 (void) inb(info->ioaddr + UART_RX);
978 (void) inb(info->ioaddr + UART_IIR);
979 (void) inb(info->ioaddr + UART_MSR);
982 * Now, initialize the UART
984 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
985 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
986 outb(info->MCR, info->ioaddr + UART_MCR);
989 * Finally, enable interrupts
991 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
993 if (info->board->chip_flag)
994 info->IER |= MOXA_MUST_IER_EGDAI;
995 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
998 * And clear the interrupt registers again for luck.
1000 (void) inb(info->ioaddr + UART_LSR);
1001 (void) inb(info->ioaddr + UART_RX);
1002 (void) inb(info->ioaddr + UART_IIR);
1003 (void) inb(info->ioaddr + UART_MSR);
1006 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
1007 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1010 * and set the speed of the serial port
1012 mxser_change_speed(info, NULL);
1013 info->port.flags |= ASYNC_INITIALIZED;
1014 spin_unlock_irqrestore(&info->slock, flags);
1020 * This routine will shutdown a serial port; interrupts maybe disabled, and
1021 * DTR is dropped if the hangup on close termio flag is on.
1023 static void mxser_shutdown(struct mxser_port *info)
1025 unsigned long flags;
1027 if (!(info->port.flags & ASYNC_INITIALIZED))
1030 spin_lock_irqsave(&info->slock, flags);
1033 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1034 * here so the queue might never be waken up
1036 wake_up_interruptible(&info->delta_msr_wait);
1039 * Free the IRQ, if necessary
1041 if (info->port.xmit_buf) {
1042 free_page((unsigned long) info->port.xmit_buf);
1043 info->port.xmit_buf = NULL;
1047 outb(0x00, info->ioaddr + UART_IER);
1049 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
1050 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1051 outb(info->MCR, info->ioaddr + UART_MCR);
1053 /* clear Rx/Tx FIFO's */
1054 if (info->board->chip_flag)
1055 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1056 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1057 info->ioaddr + UART_FCR);
1059 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1060 info->ioaddr + UART_FCR);
1062 /* read data port to reset things */
1063 (void) inb(info->ioaddr + UART_RX);
1066 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1068 info->port.flags &= ~ASYNC_INITIALIZED;
1070 if (info->board->chip_flag)
1071 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1073 spin_unlock_irqrestore(&info->slock, flags);
1077 * This routine is called whenever a serial port is opened. It
1078 * enables interrupts for a serial port, linking in its async structure into
1079 * the IRQ chain. It also performs the serial-specific
1080 * initialization for the tty structure.
1082 static int mxser_open(struct tty_struct *tty, struct file *filp)
1084 struct mxser_port *info;
1085 unsigned long flags;
1089 if (line == MXSER_PORTS)
1091 if (line < 0 || line > MXSER_PORTS)
1093 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1097 tty->driver_data = info;
1098 info->port.tty = tty;
1100 * Start up serial port
1102 spin_lock_irqsave(&info->slock, flags);
1104 spin_unlock_irqrestore(&info->slock, flags);
1105 retval = mxser_startup(info);
1109 retval = mxser_block_til_ready(tty, filp, info);
1113 /* unmark here for very high baud rate (ex. 921600 bps) used */
1114 tty->low_latency = 1;
1118 static void mxser_flush_buffer(struct tty_struct *tty)
1120 struct mxser_port *info = tty->driver_data;
1122 unsigned long flags;
1125 spin_lock_irqsave(&info->slock, flags);
1126 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1128 fcr = inb(info->ioaddr + UART_FCR);
1129 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1130 info->ioaddr + UART_FCR);
1131 outb(fcr, info->ioaddr + UART_FCR);
1133 spin_unlock_irqrestore(&info->slock, flags);
1140 * This routine is called when the serial port gets closed. First, we
1141 * wait for the last remaining data to be sent. Then, we unlink its
1142 * async structure from the interrupt chain if necessary, and we free
1143 * that IRQ if nothing is left in the chain.
1145 static void mxser_close(struct tty_struct *tty, struct file *filp)
1147 struct mxser_port *info = tty->driver_data;
1149 unsigned long timeout;
1150 unsigned long flags;
1152 if (tty->index == MXSER_PORTS)
1157 spin_lock_irqsave(&info->slock, flags);
1159 if (tty_hung_up_p(filp)) {
1160 spin_unlock_irqrestore(&info->slock, flags);
1163 if ((tty->count == 1) && (info->port.count != 1)) {
1165 * Uh, oh. tty->count is 1, which means that the tty
1166 * structure will be freed. Info->port.count should always
1167 * be one in these conditions. If it's greater than
1168 * one, we've got real problems, since it means the
1169 * serial port won't be shutdown.
1171 printk(KERN_ERR "mxser_close: bad serial port count; "
1172 "tty->count is 1, info->port.count is %d\n", info->port.count);
1173 info->port.count = 1;
1175 if (--info->port.count < 0) {
1176 printk(KERN_ERR "mxser_close: bad serial port count for "
1177 "ttys%d: %d\n", tty->index, info->port.count);
1178 info->port.count = 0;
1180 if (info->port.count) {
1181 spin_unlock_irqrestore(&info->slock, flags);
1184 info->port.flags |= ASYNC_CLOSING;
1185 spin_unlock_irqrestore(&info->slock, flags);
1187 * Save the termios structure, since this port may have
1188 * separate termios for callout and dialin.
1190 if (info->port.flags & ASYNC_NORMAL_ACTIVE)
1191 info->normal_termios = *tty->termios;
1193 * Now we wait for the transmit buffer to clear; and we notify
1194 * the line discipline to only process XON/XOFF characters.
1197 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1198 tty_wait_until_sent(tty, info->port.closing_wait);
1200 * At this point we stop accepting input. To do this, we
1201 * disable the receive line status interrupts, and tell the
1202 * interrupt driver to stop checking the data ready bit in the
1203 * line status register.
1205 info->IER &= ~UART_IER_RLSI;
1206 if (info->board->chip_flag)
1207 info->IER &= ~MOXA_MUST_RECV_ISR;
1209 if (info->port.flags & ASYNC_INITIALIZED) {
1210 outb(info->IER, info->ioaddr + UART_IER);
1212 * Before we drop DTR, make sure the UART transmitter
1213 * has completely drained; this is especially
1214 * important if there is a transmit FIFO!
1216 timeout = jiffies + HZ;
1217 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1218 schedule_timeout_interruptible(5);
1219 if (time_after(jiffies, timeout))
1223 mxser_shutdown(info);
1225 mxser_flush_buffer(tty);
1226 tty_ldisc_flush(tty);
1229 info->port.tty = NULL;
1230 if (info->port.blocked_open) {
1231 if (info->port.close_delay)
1232 schedule_timeout_interruptible(info->port.close_delay);
1233 wake_up_interruptible(&info->port.open_wait);
1236 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1239 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1242 struct mxser_port *info = tty->driver_data;
1243 unsigned long flags;
1245 if (!info->port.xmit_buf)
1249 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1250 SERIAL_XMIT_SIZE - info->xmit_head));
1254 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1255 spin_lock_irqsave(&info->slock, flags);
1256 info->xmit_head = (info->xmit_head + c) &
1257 (SERIAL_XMIT_SIZE - 1);
1258 info->xmit_cnt += c;
1259 spin_unlock_irqrestore(&info->slock, flags);
1266 if (info->xmit_cnt && !tty->stopped) {
1267 if (!tty->hw_stopped ||
1268 (info->type == PORT_16550A) ||
1269 (info->board->chip_flag)) {
1270 spin_lock_irqsave(&info->slock, flags);
1271 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1273 info->IER |= UART_IER_THRI;
1274 outb(info->IER, info->ioaddr + UART_IER);
1275 spin_unlock_irqrestore(&info->slock, flags);
1281 static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
1283 struct mxser_port *info = tty->driver_data;
1284 unsigned long flags;
1286 if (!info->port.xmit_buf)
1289 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1292 spin_lock_irqsave(&info->slock, flags);
1293 info->port.xmit_buf[info->xmit_head++] = ch;
1294 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1296 spin_unlock_irqrestore(&info->slock, flags);
1297 if (!tty->stopped) {
1298 if (!tty->hw_stopped ||
1299 (info->type == PORT_16550A) ||
1300 info->board->chip_flag) {
1301 spin_lock_irqsave(&info->slock, flags);
1302 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1303 info->IER |= UART_IER_THRI;
1304 outb(info->IER, info->ioaddr + UART_IER);
1305 spin_unlock_irqrestore(&info->slock, flags);
1312 static void mxser_flush_chars(struct tty_struct *tty)
1314 struct mxser_port *info = tty->driver_data;
1315 unsigned long flags;
1317 if (info->xmit_cnt <= 0 ||
1319 !info->port.xmit_buf ||
1321 (info->type != PORT_16550A) &&
1322 (!info->board->chip_flag)
1326 spin_lock_irqsave(&info->slock, flags);
1328 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1329 info->IER |= UART_IER_THRI;
1330 outb(info->IER, info->ioaddr + UART_IER);
1332 spin_unlock_irqrestore(&info->slock, flags);
1335 static int mxser_write_room(struct tty_struct *tty)
1337 struct mxser_port *info = tty->driver_data;
1340 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1346 static int mxser_chars_in_buffer(struct tty_struct *tty)
1348 struct mxser_port *info = tty->driver_data;
1349 return info->xmit_cnt;
1353 * ------------------------------------------------------------
1354 * friends of mxser_ioctl()
1355 * ------------------------------------------------------------
1357 static int mxser_get_serial_info(struct mxser_port *info,
1358 struct serial_struct __user *retinfo)
1360 struct serial_struct tmp = {
1362 .line = info->port.tty->index,
1363 .port = info->ioaddr,
1364 .irq = info->board->irq,
1365 .flags = info->port.flags,
1366 .baud_base = info->baud_base,
1367 .close_delay = info->port.close_delay,
1368 .closing_wait = info->port.closing_wait,
1369 .custom_divisor = info->custom_divisor,
1372 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1377 static int mxser_set_serial_info(struct mxser_port *info,
1378 struct serial_struct __user *new_info)
1380 struct serial_struct new_serial;
1382 unsigned long sl_flags;
1386 if (!new_info || !info->ioaddr)
1388 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1391 if (new_serial.irq != info->board->irq ||
1392 new_serial.port != info->ioaddr)
1395 flags = info->port.flags & ASYNC_SPD_MASK;
1397 if (!capable(CAP_SYS_ADMIN)) {
1398 if ((new_serial.baud_base != info->baud_base) ||
1399 (new_serial.close_delay != info->port.close_delay) ||
1400 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
1402 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
1403 (new_serial.flags & ASYNC_USR_MASK));
1406 * OK, past this point, all the error checking has been done.
1407 * At this point, we start making changes.....
1409 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
1410 (new_serial.flags & ASYNC_FLAGS));
1411 info->port.close_delay = new_serial.close_delay * HZ / 100;
1412 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
1413 info->port.tty->low_latency =
1414 (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1415 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1416 (new_serial.baud_base != info->baud_base ||
1417 new_serial.custom_divisor !=
1418 info->custom_divisor)) {
1419 baud = new_serial.baud_base / new_serial.custom_divisor;
1420 tty_encode_baud_rate(info->port.tty, baud, baud);
1424 info->type = new_serial.type;
1426 process_txrx_fifo(info);
1428 if (info->port.flags & ASYNC_INITIALIZED) {
1429 if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
1430 spin_lock_irqsave(&info->slock, sl_flags);
1431 mxser_change_speed(info, NULL);
1432 spin_unlock_irqrestore(&info->slock, sl_flags);
1435 retval = mxser_startup(info);
1441 * mxser_get_lsr_info - get line status register info
1443 * Purpose: Let user call ioctl() to get info when the UART physically
1444 * is emptied. On bus types like RS485, the transmitter must
1445 * release the bus after transmitting. This must be done when
1446 * the transmit shift register is empty, not be done when the
1447 * transmit holding register is empty. This functionality
1448 * allows an RS485 driver to be written in user space.
1450 static int mxser_get_lsr_info(struct mxser_port *info,
1451 unsigned int __user *value)
1453 unsigned char status;
1454 unsigned int result;
1455 unsigned long flags;
1457 spin_lock_irqsave(&info->slock, flags);
1458 status = inb(info->ioaddr + UART_LSR);
1459 spin_unlock_irqrestore(&info->slock, flags);
1460 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1461 return put_user(result, value);
1464 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1466 struct mxser_port *info = tty->driver_data;
1467 unsigned char control, status;
1468 unsigned long flags;
1471 if (tty->index == MXSER_PORTS)
1472 return -ENOIOCTLCMD;
1473 if (test_bit(TTY_IO_ERROR, &tty->flags))
1476 control = info->MCR;
1478 spin_lock_irqsave(&info->slock, flags);
1479 status = inb(info->ioaddr + UART_MSR);
1480 if (status & UART_MSR_ANY_DELTA)
1481 mxser_check_modem_status(info, status);
1482 spin_unlock_irqrestore(&info->slock, flags);
1483 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1484 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1485 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1486 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1487 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1488 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1491 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1492 unsigned int set, unsigned int clear)
1494 struct mxser_port *info = tty->driver_data;
1495 unsigned long flags;
1498 if (tty->index == MXSER_PORTS)
1499 return -ENOIOCTLCMD;
1500 if (test_bit(TTY_IO_ERROR, &tty->flags))
1503 spin_lock_irqsave(&info->slock, flags);
1505 if (set & TIOCM_RTS)
1506 info->MCR |= UART_MCR_RTS;
1507 if (set & TIOCM_DTR)
1508 info->MCR |= UART_MCR_DTR;
1510 if (clear & TIOCM_RTS)
1511 info->MCR &= ~UART_MCR_RTS;
1512 if (clear & TIOCM_DTR)
1513 info->MCR &= ~UART_MCR_DTR;
1515 outb(info->MCR, info->ioaddr + UART_MCR);
1516 spin_unlock_irqrestore(&info->slock, flags);
1520 static int __init mxser_program_mode(int port)
1532 id = inb(port + 1) & 0x1F;
1533 if ((id != C168_ASIC_ID) &&
1534 (id != C104_ASIC_ID) &&
1535 (id != C102_ASIC_ID) &&
1536 (id != CI132_ASIC_ID) &&
1537 (id != CI134_ASIC_ID) &&
1538 (id != CI104J_ASIC_ID))
1540 for (i = 0, j = 0; i < 4; i++) {
1544 } else if ((j == 1) && (n == 1)) {
1555 static void __init mxser_normal_mode(int port)
1559 outb(0xA5, port + 1);
1560 outb(0x80, port + 3);
1561 outb(12, port + 0); /* 9600 bps */
1563 outb(0x03, port + 3); /* 8 data bits */
1564 outb(0x13, port + 4); /* loop back mode */
1565 for (i = 0; i < 16; i++) {
1567 if ((n & 0x61) == 0x60)
1572 outb(0x00, port + 4);
1575 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1576 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1577 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1578 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1579 #define EN_CCMD 0x000 /* Chip's command register */
1580 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1581 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1582 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1583 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1584 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1585 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1586 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1587 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1588 static int __init mxser_read_register(int port, unsigned short *regs)
1590 int i, k, value, id;
1593 id = mxser_program_mode(port);
1596 for (i = 0; i < 14; i++) {
1597 k = (i & 0x3F) | 0x180;
1598 for (j = 0x100; j > 0; j >>= 1) {
1599 outb(CHIP_CS, port);
1601 outb(CHIP_CS | CHIP_DO, port);
1602 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1604 outb(CHIP_CS, port);
1605 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1610 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1611 outb(CHIP_CS, port);
1612 outb(CHIP_CS | CHIP_SK, port);
1613 if (inb(port) & CHIP_DI)
1619 mxser_normal_mode(port);
1623 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1625 struct mxser_port *port;
1631 case MOXA_GET_MAJOR:
1632 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl %x, fix "
1633 "your userspace\n", current->comm, cmd);
1634 return put_user(ttymajor, (int __user *)argp);
1636 case MOXA_CHKPORTENABLE:
1639 for (i = 0; i < MXSER_BOARDS; i++)
1640 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1641 if (mxser_boards[i].ports[j].ioaddr)
1644 return put_user(result, (unsigned long __user *)argp);
1645 case MOXA_GETDATACOUNT:
1647 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1651 case MOXA_GETMSTATUS: {
1652 struct mxser_mstatus ms, __user *msu = argp;
1654 for (i = 0; i < MXSER_BOARDS; i++)
1655 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1656 port = &mxser_boards[i].ports[j];
1657 memset(&ms, 0, sizeof(ms));
1662 if (!port->port.tty || !port->port.tty->termios)
1663 ms.cflag = port->normal_termios.c_cflag;
1665 ms.cflag = port->port.tty->termios->c_cflag;
1667 status = inb(port->ioaddr + UART_MSR);
1668 if (status & UART_MSR_DCD)
1670 if (status & UART_MSR_DSR)
1672 if (status & UART_MSR_CTS)
1675 if (copy_to_user(msu, &ms, sizeof(ms))) {
1684 case MOXA_ASPP_MON_EXT: {
1685 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1686 unsigned int cflag, iflag, p;
1689 me = kzalloc(sizeof(*me), GFP_KERNEL);
1694 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1695 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1696 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1700 port = &mxser_boards[i].ports[j];
1704 status = mxser_get_msr(port->ioaddr, 0, p);
1706 if (status & UART_MSR_TERI)
1708 if (status & UART_MSR_DDSR)
1710 if (status & UART_MSR_DDCD)
1712 if (status & UART_MSR_DCTS)
1715 port->mon_data.modem_status = status;
1716 me->rx_cnt[p] = port->mon_data.rxcnt;
1717 me->tx_cnt[p] = port->mon_data.txcnt;
1718 me->up_rxcnt[p] = port->mon_data.up_rxcnt;
1719 me->up_txcnt[p] = port->mon_data.up_txcnt;
1720 me->modem_status[p] =
1721 port->mon_data.modem_status;
1722 me->baudrate[p] = tty_get_baud_rate(port->port.tty);
1724 if (!port->port.tty || !port->port.tty->termios) {
1725 cflag = port->normal_termios.c_cflag;
1726 iflag = port->normal_termios.c_iflag;
1728 cflag = port->port.tty->termios->c_cflag;
1729 iflag = port->port.tty->termios->c_iflag;
1732 me->databits[p] = cflag & CSIZE;
1733 me->stopbits[p] = cflag & CSTOPB;
1734 me->parity[p] = cflag & (PARENB | PARODD |
1737 if (cflag & CRTSCTS)
1738 me->flowctrl[p] |= 0x03;
1740 if (iflag & (IXON | IXOFF))
1741 me->flowctrl[p] |= 0x0C;
1743 if (port->type == PORT_16550A)
1746 opmode = inb(port->opmode_ioaddr) >>
1748 opmode &= OP_MODE_MASK;
1749 me->iftype[p] = opmode;
1753 if (copy_to_user(argp, me, sizeof(*me)))
1759 return -ENOIOCTLCMD;
1764 static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1765 struct async_icount *cprev)
1767 struct async_icount cnow;
1768 unsigned long flags;
1771 spin_lock_irqsave(&info->slock, flags);
1772 cnow = info->icount; /* atomic copy */
1773 spin_unlock_irqrestore(&info->slock, flags);
1775 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1776 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1777 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1778 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1785 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1786 unsigned int cmd, unsigned long arg)
1788 struct mxser_port *info = tty->driver_data;
1789 struct async_icount cnow;
1790 unsigned long flags;
1791 void __user *argp = (void __user *)arg;
1794 if (tty->index == MXSER_PORTS)
1795 return mxser_ioctl_special(cmd, argp);
1797 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1799 unsigned long opmode;
1800 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1802 unsigned char val, mask;
1805 if (cmd == MOXA_SET_OP_MODE) {
1806 if (get_user(opmode, (int __user *) argp))
1808 if (opmode != RS232_MODE &&
1809 opmode != RS485_2WIRE_MODE &&
1810 opmode != RS422_MODE &&
1811 opmode != RS485_4WIRE_MODE)
1816 val = inb(info->opmode_ioaddr);
1818 val |= (opmode << shiftbit);
1819 outb(val, info->opmode_ioaddr);
1824 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1825 opmode &= OP_MODE_MASK;
1827 if (put_user(opmode, (int __user *)argp))
1833 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1834 test_bit(TTY_IO_ERROR, &tty->flags))
1840 retval = mxser_get_serial_info(info, argp);
1845 retval = mxser_set_serial_info(info, argp);
1848 case TIOCSERGETLSR: /* Get line status register */
1849 return mxser_get_lsr_info(info, argp);
1851 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1852 * - mask passed in arg for lines of interest
1853 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1854 * Caller should use TIOCGICOUNT to see which one it was
1857 spin_lock_irqsave(&info->slock, flags);
1858 cnow = info->icount; /* note the counters on entry */
1859 spin_unlock_irqrestore(&info->slock, flags);
1861 return wait_event_interruptible(info->delta_msr_wait,
1862 mxser_cflags_changed(info, arg, &cnow));
1864 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1865 * Return: write counters to the user passed counter struct
1866 * NB: both 1->0 and 0->1 transitions are counted except for
1867 * RI where only 0->1 is counted.
1870 struct serial_icounter_struct icnt = { 0 };
1871 spin_lock_irqsave(&info->slock, flags);
1872 cnow = info->icount;
1873 spin_unlock_irqrestore(&info->slock, flags);
1875 icnt.frame = cnow.frame;
1876 icnt.brk = cnow.brk;
1877 icnt.overrun = cnow.overrun;
1878 icnt.buf_overrun = cnow.buf_overrun;
1879 icnt.parity = cnow.parity;
1882 icnt.cts = cnow.cts;
1883 icnt.dsr = cnow.dsr;
1884 icnt.rng = cnow.rng;
1885 icnt.dcd = cnow.dcd;
1887 return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
1889 case MOXA_HighSpeedOn:
1890 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1891 case MOXA_SDS_RSTICOUNTER:
1893 info->mon_data.rxcnt = 0;
1894 info->mon_data.txcnt = 0;
1898 case MOXA_ASPP_OQUEUE:{
1902 len = mxser_chars_in_buffer(tty);
1903 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1904 len += (lsr ? 0 : 1);
1907 return put_user(len, (int __user *)argp);
1909 case MOXA_ASPP_MON: {
1913 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1914 mxser_check_modem_status(info, status);
1916 mcr = inb(info->ioaddr + UART_MCR);
1917 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1918 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1920 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1922 if (mcr & MOXA_MUST_MCR_TX_XON)
1923 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1925 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1927 if (info->port.tty->hw_stopped)
1928 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1930 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1932 if (copy_to_user(argp, &info->mon_data,
1933 sizeof(struct mxser_mon)))
1938 case MOXA_ASPP_LSTATUS: {
1939 if (put_user(info->err_shadow, (unsigned char __user *)argp))
1942 info->err_shadow = 0;
1945 case MOXA_SET_BAUD_METHOD: {
1948 if (get_user(method, (int __user *)argp))
1950 mxser_set_baud_method[tty->index] = method;
1951 return put_user(method, (int __user *)argp);
1954 return -ENOIOCTLCMD;
1959 static void mxser_stoprx(struct tty_struct *tty)
1961 struct mxser_port *info = tty->driver_data;
1963 info->ldisc_stop_rx = 1;
1965 if (info->board->chip_flag) {
1966 info->IER &= ~MOXA_MUST_RECV_ISR;
1967 outb(info->IER, info->ioaddr + UART_IER);
1969 info->x_char = STOP_CHAR(tty);
1970 outb(0, info->ioaddr + UART_IER);
1971 info->IER |= UART_IER_THRI;
1972 outb(info->IER, info->ioaddr + UART_IER);
1976 if (info->port.tty->termios->c_cflag & CRTSCTS) {
1977 info->MCR &= ~UART_MCR_RTS;
1978 outb(info->MCR, info->ioaddr + UART_MCR);
1983 * This routine is called by the upper-layer tty layer to signal that
1984 * incoming characters should be throttled.
1986 static void mxser_throttle(struct tty_struct *tty)
1991 static void mxser_unthrottle(struct tty_struct *tty)
1993 struct mxser_port *info = tty->driver_data;
1996 info->ldisc_stop_rx = 0;
2001 if (info->board->chip_flag) {
2002 info->IER |= MOXA_MUST_RECV_ISR;
2003 outb(info->IER, info->ioaddr + UART_IER);
2005 info->x_char = START_CHAR(tty);
2006 outb(0, info->ioaddr + UART_IER);
2007 info->IER |= UART_IER_THRI;
2008 outb(info->IER, info->ioaddr + UART_IER);
2013 if (info->port.tty->termios->c_cflag & CRTSCTS) {
2014 info->MCR |= UART_MCR_RTS;
2015 outb(info->MCR, info->ioaddr + UART_MCR);
2020 * mxser_stop() and mxser_start()
2022 * This routines are called before setting or resetting tty->stopped.
2023 * They enable or disable transmitter interrupts, as necessary.
2025 static void mxser_stop(struct tty_struct *tty)
2027 struct mxser_port *info = tty->driver_data;
2028 unsigned long flags;
2030 spin_lock_irqsave(&info->slock, flags);
2031 if (info->IER & UART_IER_THRI) {
2032 info->IER &= ~UART_IER_THRI;
2033 outb(info->IER, info->ioaddr + UART_IER);
2035 spin_unlock_irqrestore(&info->slock, flags);
2038 static void mxser_start(struct tty_struct *tty)
2040 struct mxser_port *info = tty->driver_data;
2041 unsigned long flags;
2043 spin_lock_irqsave(&info->slock, flags);
2044 if (info->xmit_cnt && info->port.xmit_buf) {
2045 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
2046 info->IER |= UART_IER_THRI;
2047 outb(info->IER, info->ioaddr + UART_IER);
2049 spin_unlock_irqrestore(&info->slock, flags);
2052 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2054 struct mxser_port *info = tty->driver_data;
2055 unsigned long flags;
2057 spin_lock_irqsave(&info->slock, flags);
2058 mxser_change_speed(info, old_termios);
2059 spin_unlock_irqrestore(&info->slock, flags);
2061 if ((old_termios->c_cflag & CRTSCTS) &&
2062 !(tty->termios->c_cflag & CRTSCTS)) {
2063 tty->hw_stopped = 0;
2067 /* Handle sw stopped */
2068 if ((old_termios->c_iflag & IXON) &&
2069 !(tty->termios->c_iflag & IXON)) {
2072 if (info->board->chip_flag) {
2073 spin_lock_irqsave(&info->slock, flags);
2074 mxser_disable_must_rx_software_flow_control(
2076 spin_unlock_irqrestore(&info->slock, flags);
2084 * mxser_wait_until_sent() --- wait until the transmitter is empty
2086 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2088 struct mxser_port *info = tty->driver_data;
2089 unsigned long orig_jiffies, char_time;
2092 if (info->type == PORT_UNKNOWN)
2095 if (info->xmit_fifo_size == 0)
2096 return; /* Just in case.... */
2098 orig_jiffies = jiffies;
2100 * Set the check interval to be 1/5 of the estimated time to
2101 * send a single character, and make it at least 1. The check
2102 * interval should also be less than the timeout.
2104 * Note: we have to use pretty tight timings here to satisfy
2107 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2108 char_time = char_time / 5;
2111 if (timeout && timeout < char_time)
2112 char_time = timeout;
2114 * If the transmitter hasn't cleared in twice the approximate
2115 * amount of time to send the entire FIFO, it probably won't
2116 * ever clear. This assumes the UART isn't doing flow
2117 * control, which is currently the case. Hence, if it ever
2118 * takes longer than info->timeout, this is probably due to a
2119 * UART bug of some kind. So, we clamp the timeout parameter at
2122 if (!timeout || timeout > 2 * info->timeout)
2123 timeout = 2 * info->timeout;
2124 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2125 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2126 timeout, char_time);
2127 printk("jiff=%lu...", jiffies);
2130 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2131 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2132 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2134 schedule_timeout_interruptible(char_time);
2135 if (signal_pending(current))
2137 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2140 set_current_state(TASK_RUNNING);
2143 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2144 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2149 * This routine is called by tty_hangup() when a hangup is signaled.
2151 static void mxser_hangup(struct tty_struct *tty)
2153 struct mxser_port *info = tty->driver_data;
2155 mxser_flush_buffer(tty);
2156 mxser_shutdown(info);
2157 info->port.count = 0;
2158 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2159 info->port.tty = NULL;
2160 wake_up_interruptible(&info->port.open_wait);
2164 * mxser_rs_break() --- routine which turns the break handling on or off
2166 static int mxser_rs_break(struct tty_struct *tty, int break_state)
2168 struct mxser_port *info = tty->driver_data;
2169 unsigned long flags;
2171 spin_lock_irqsave(&info->slock, flags);
2172 if (break_state == -1)
2173 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2174 info->ioaddr + UART_LCR);
2176 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2177 info->ioaddr + UART_LCR);
2178 spin_unlock_irqrestore(&info->slock, flags);
2182 static void mxser_receive_chars(struct mxser_port *port, int *status)
2184 struct tty_struct *tty = port->port.tty;
2185 unsigned char ch, gdl;
2191 recv_room = tty->receive_room;
2192 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2195 if (port->board->chip_flag != MOXA_OTHER_UART) {
2197 if (*status & UART_LSR_SPECIAL)
2199 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2200 (*status & MOXA_MUST_LSR_RERR))
2202 if (*status & MOXA_MUST_LSR_RERR)
2205 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2207 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2208 gdl &= MOXA_MUST_GDL_MASK;
2209 if (gdl >= recv_room) {
2210 if (!port->ldisc_stop_rx)
2214 ch = inb(port->ioaddr + UART_RX);
2215 tty_insert_flip_char(tty, ch, 0);
2226 ch = inb(port->ioaddr + UART_RX);
2227 if (port->board->chip_flag && (*status & UART_LSR_OE))
2228 outb(0x23, port->ioaddr + UART_FCR);
2229 *status &= port->read_status_mask;
2230 if (*status & port->ignore_status_mask) {
2231 if (++ignored > 100)
2235 if (*status & UART_LSR_SPECIAL) {
2236 if (*status & UART_LSR_BI) {
2240 if (port->port.flags & ASYNC_SAK)
2242 } else if (*status & UART_LSR_PE) {
2244 port->icount.parity++;
2245 } else if (*status & UART_LSR_FE) {
2247 port->icount.frame++;
2248 } else if (*status & UART_LSR_OE) {
2250 port->icount.overrun++;
2254 tty_insert_flip_char(tty, ch, flag);
2256 if (cnt >= recv_room) {
2257 if (!port->ldisc_stop_rx)
2264 if (port->board->chip_flag)
2267 *status = inb(port->ioaddr + UART_LSR);
2268 } while (*status & UART_LSR_DR);
2271 mxvar_log.rxcnt[port->port.tty->index] += cnt;
2272 port->mon_data.rxcnt += cnt;
2273 port->mon_data.up_rxcnt += cnt;
2276 * We are called from an interrupt context with &port->slock
2277 * being held. Drop it temporarily in order to prevent
2278 * recursive locking.
2280 spin_unlock(&port->slock);
2281 tty_flip_buffer_push(tty);
2282 spin_lock(&port->slock);
2285 static void mxser_transmit_chars(struct mxser_port *port)
2290 outb(port->x_char, port->ioaddr + UART_TX);
2292 mxvar_log.txcnt[port->port.tty->index]++;
2293 port->mon_data.txcnt++;
2294 port->mon_data.up_txcnt++;
2299 if (port->port.xmit_buf == NULL)
2302 if ((port->xmit_cnt <= 0) || port->port.tty->stopped ||
2303 (port->port.tty->hw_stopped &&
2304 (port->type != PORT_16550A) &&
2305 (!port->board->chip_flag))) {
2306 port->IER &= ~UART_IER_THRI;
2307 outb(port->IER, port->ioaddr + UART_IER);
2311 cnt = port->xmit_cnt;
2312 count = port->xmit_fifo_size;
2314 outb(port->port.xmit_buf[port->xmit_tail++],
2315 port->ioaddr + UART_TX);
2316 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2317 if (--port->xmit_cnt <= 0)
2319 } while (--count > 0);
2320 mxvar_log.txcnt[port->port.tty->index] += (cnt - port->xmit_cnt);
2322 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2323 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2324 port->icount.tx += (cnt - port->xmit_cnt);
2326 if (port->xmit_cnt < WAKEUP_CHARS)
2327 tty_wakeup(port->port.tty);
2329 if (port->xmit_cnt <= 0) {
2330 port->IER &= ~UART_IER_THRI;
2331 outb(port->IER, port->ioaddr + UART_IER);
2336 * This is the serial driver's generic interrupt routine
2338 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2341 struct mxser_board *brd = NULL;
2342 struct mxser_port *port;
2343 int max, irqbits, bits, msr;
2344 unsigned int int_cnt, pass_counter = 0;
2345 int handled = IRQ_NONE;
2347 for (i = 0; i < MXSER_BOARDS; i++)
2348 if (dev_id == &mxser_boards[i]) {
2353 if (i == MXSER_BOARDS)
2357 max = brd->info->nports;
2358 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2359 irqbits = inb(brd->vector) & brd->vector_mask;
2360 if (irqbits == brd->vector_mask)
2363 handled = IRQ_HANDLED;
2364 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2365 if (irqbits == brd->vector_mask)
2369 port = &brd->ports[i];
2372 spin_lock(&port->slock);
2374 iir = inb(port->ioaddr + UART_IIR);
2375 if (iir & UART_IIR_NO_INT)
2377 iir &= MOXA_MUST_IIR_MASK;
2378 if (!port->port.tty ||
2379 (port->port.flags & ASYNC_CLOSING) ||
2380 !(port->port.flags &
2381 ASYNC_INITIALIZED)) {
2382 status = inb(port->ioaddr + UART_LSR);
2383 outb(0x27, port->ioaddr + UART_FCR);
2384 inb(port->ioaddr + UART_MSR);
2388 status = inb(port->ioaddr + UART_LSR);
2390 if (status & UART_LSR_PE)
2391 port->err_shadow |= NPPI_NOTIFY_PARITY;
2392 if (status & UART_LSR_FE)
2393 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2394 if (status & UART_LSR_OE)
2396 NPPI_NOTIFY_HW_OVERRUN;
2397 if (status & UART_LSR_BI)
2398 port->err_shadow |= NPPI_NOTIFY_BREAK;
2400 if (port->board->chip_flag) {
2401 if (iir == MOXA_MUST_IIR_GDA ||
2402 iir == MOXA_MUST_IIR_RDA ||
2403 iir == MOXA_MUST_IIR_RTO ||
2404 iir == MOXA_MUST_IIR_LSR)
2405 mxser_receive_chars(port,
2409 status &= port->read_status_mask;
2410 if (status & UART_LSR_DR)
2411 mxser_receive_chars(port,
2414 msr = inb(port->ioaddr + UART_MSR);
2415 if (msr & UART_MSR_ANY_DELTA)
2416 mxser_check_modem_status(port, msr);
2418 if (port->board->chip_flag) {
2419 if (iir == 0x02 && (status &
2421 mxser_transmit_chars(port);
2423 if (status & UART_LSR_THRE)
2424 mxser_transmit_chars(port);
2426 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2427 spin_unlock(&port->slock);
2435 static const struct tty_operations mxser_ops = {
2437 .close = mxser_close,
2438 .write = mxser_write,
2439 .put_char = mxser_put_char,
2440 .flush_chars = mxser_flush_chars,
2441 .write_room = mxser_write_room,
2442 .chars_in_buffer = mxser_chars_in_buffer,
2443 .flush_buffer = mxser_flush_buffer,
2444 .ioctl = mxser_ioctl,
2445 .throttle = mxser_throttle,
2446 .unthrottle = mxser_unthrottle,
2447 .set_termios = mxser_set_termios,
2449 .start = mxser_start,
2450 .hangup = mxser_hangup,
2451 .break_ctl = mxser_rs_break,
2452 .wait_until_sent = mxser_wait_until_sent,
2453 .tiocmget = mxser_tiocmget,
2454 .tiocmset = mxser_tiocmset,
2458 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2461 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2465 free_irq(brd->irq, brd);
2466 if (pdev != NULL) { /* PCI */
2468 pci_release_region(pdev, 2);
2469 pci_release_region(pdev, 3);
2472 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2473 release_region(brd->vector, 1);
2477 static int __devinit mxser_initbrd(struct mxser_board *brd,
2478 struct pci_dev *pdev)
2480 struct mxser_port *info;
2484 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2486 for (i = 0; i < brd->info->nports; i++) {
2487 info = &brd->ports[i];
2488 tty_port_init(&info->port);
2491 info->ldisc_stop_rx = 0;
2493 /* Enhance mode enabled here */
2494 if (brd->chip_flag != MOXA_OTHER_UART)
2495 mxser_enable_must_enchance_mode(info->ioaddr);
2497 info->port.flags = ASYNC_SHARE_IRQ;
2498 info->type = brd->uart_type;
2500 process_txrx_fifo(info);
2502 info->custom_divisor = info->baud_base * 16;
2503 info->port.close_delay = 5 * HZ / 10;
2504 info->port.closing_wait = 30 * HZ;
2505 info->normal_termios = mxvar_sdriver->init_termios;
2506 init_waitqueue_head(&info->delta_msr_wait);
2507 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2508 info->err_shadow = 0;
2509 spin_lock_init(&info->slock);
2511 /* before set INT ISR, disable all int */
2512 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2513 info->ioaddr + UART_IER);
2516 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2519 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2520 "conflict with another device.\n",
2521 brd->info->name, brd->irq);
2522 /* We hold resources, we need to release them. */
2523 mxser_release_res(brd, pdev, 0);
2528 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2531 unsigned short regs[16], irq;
2532 unsigned char scratch, scratch2;
2534 brd->chip_flag = MOXA_OTHER_UART;
2536 id = mxser_read_register(cap, regs);
2539 brd->info = &mxser_cards[0];
2542 brd->info = &mxser_cards[1];
2544 case CI104J_ASIC_ID:
2545 brd->info = &mxser_cards[2];
2548 brd->info = &mxser_cards[5];
2551 brd->info = &mxser_cards[6];
2554 brd->info = &mxser_cards[7];
2561 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2562 Flag-hack checks if configuration should be read as 2-port here. */
2563 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2564 irq = regs[9] & 0xF000;
2565 irq = irq | (irq >> 4);
2566 if (irq != (regs[9] & 0xFF00))
2567 return MXSER_ERR_IRQ_CONFLIT;
2568 } else if (brd->info->nports == 4) {
2569 irq = regs[9] & 0xF000;
2570 irq = irq | (irq >> 4);
2571 irq = irq | (irq >> 8);
2573 return MXSER_ERR_IRQ_CONFLIT;
2574 } else if (brd->info->nports == 8) {
2575 irq = regs[9] & 0xF000;
2576 irq = irq | (irq >> 4);
2577 irq = irq | (irq >> 8);
2578 if ((irq != regs[9]) || (irq != regs[10]))
2579 return MXSER_ERR_IRQ_CONFLIT;
2583 return MXSER_ERR_IRQ;
2584 brd->irq = ((int)(irq & 0xF000) >> 12);
2585 for (i = 0; i < 8; i++)
2586 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2587 if ((regs[12] & 0x80) == 0)
2588 return MXSER_ERR_VECTOR;
2589 brd->vector = (int)regs[11]; /* interrupt vector */
2591 brd->vector_mask = 0x00FF;
2593 brd->vector_mask = 0x000F;
2594 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2595 if (regs[12] & bits) {
2596 brd->ports[i].baud_base = 921600;
2597 brd->ports[i].max_baud = 921600;
2599 brd->ports[i].baud_base = 115200;
2600 brd->ports[i].max_baud = 115200;
2603 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2604 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2605 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2606 outb(scratch2, cap + UART_LCR);
2607 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2608 scratch = inb(cap + UART_IIR);
2611 brd->uart_type = PORT_16550A;
2613 brd->uart_type = PORT_16450;
2614 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2616 return MXSER_ERR_IOADDR;
2617 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2618 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2619 return MXSER_ERR_VECTOR;
2621 return brd->info->nports;
2624 static int __devinit mxser_probe(struct pci_dev *pdev,
2625 const struct pci_device_id *ent)
2628 struct mxser_board *brd;
2630 unsigned long ioaddress;
2631 int retval = -EINVAL;
2633 for (i = 0; i < MXSER_BOARDS; i++)
2634 if (mxser_boards[i].info == NULL)
2637 if (i >= MXSER_BOARDS) {
2638 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2639 "(maximum %d), board not configured\n", MXSER_BOARDS);
2643 brd = &mxser_boards[i];
2644 brd->idx = i * MXSER_PORTS_PER_BOARD;
2645 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2646 mxser_cards[ent->driver_data].name,
2647 pdev->bus->number, PCI_SLOT(pdev->devfn));
2649 retval = pci_enable_device(pdev);
2651 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2656 ioaddress = pci_resource_start(pdev, 2);
2657 retval = pci_request_region(pdev, 2, "mxser(IO)");
2661 brd->info = &mxser_cards[ent->driver_data];
2662 for (i = 0; i < brd->info->nports; i++)
2663 brd->ports[i].ioaddr = ioaddress + 8 * i;
2666 ioaddress = pci_resource_start(pdev, 3);
2667 retval = pci_request_region(pdev, 3, "mxser(vector)");
2670 brd->vector = ioaddress;
2673 brd->irq = pdev->irq;
2675 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2676 brd->uart_type = PORT_16550A;
2677 brd->vector_mask = 0;
2679 for (i = 0; i < brd->info->nports; i++) {
2680 for (j = 0; j < UART_INFO_NUM; j++) {
2681 if (Gpci_uart_info[j].type == brd->chip_flag) {
2682 brd->ports[i].max_baud =
2683 Gpci_uart_info[j].max_baud;
2685 /* exception....CP-102 */
2686 if (brd->info->flags & MXSER_HIGHBAUD)
2687 brd->ports[i].max_baud = 921600;
2693 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2694 for (i = 0; i < brd->info->nports; i++) {
2696 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2698 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2700 outb(0, ioaddress + 4); /* default set to RS232 mode */
2701 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2704 for (i = 0; i < brd->info->nports; i++) {
2705 brd->vector_mask |= (1 << i);
2706 brd->ports[i].baud_base = 921600;
2709 /* mxser_initbrd will hook ISR. */
2710 retval = mxser_initbrd(brd, pdev);
2714 for (i = 0; i < brd->info->nports; i++)
2715 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2717 pci_set_drvdata(pdev, brd);
2721 pci_release_region(pdev, 2);
2731 static void __devexit mxser_remove(struct pci_dev *pdev)
2733 struct mxser_board *brd = pci_get_drvdata(pdev);
2736 for (i = 0; i < brd->info->nports; i++)
2737 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2739 mxser_release_res(brd, pdev, 1);
2743 static struct pci_driver mxser_driver = {
2745 .id_table = mxser_pcibrds,
2746 .probe = mxser_probe,
2747 .remove = __devexit_p(mxser_remove)
2750 static int __init mxser_module_init(void)
2752 struct mxser_board *brd;
2754 unsigned int i, m, isaloop;
2757 pr_debug("Loading module mxser ...\n");
2759 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2763 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2766 /* Initialize the tty_driver structure */
2767 mxvar_sdriver->owner = THIS_MODULE;
2768 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2769 mxvar_sdriver->name = "ttyMI";
2770 mxvar_sdriver->major = ttymajor;
2771 mxvar_sdriver->minor_start = 0;
2772 mxvar_sdriver->num = MXSER_PORTS + 1;
2773 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2774 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2775 mxvar_sdriver->init_termios = tty_std_termios;
2776 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2777 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2778 tty_set_operations(mxvar_sdriver, &mxser_ops);
2780 retval = tty_register_driver(mxvar_sdriver);
2782 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2788 /* Start finding ISA boards here */
2789 for (isaloop = 0; isaloop < 2; isaloop++)
2790 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2792 cap = mxserBoardCAP[b]; /* predefined */
2794 cap = ioaddr[b]; /* module param */
2799 brd = &mxser_boards[m];
2800 retval = mxser_get_ISA_conf(cap, brd);
2803 printk(KERN_INFO "Found MOXA %s board "
2805 brd->info->name, ioaddr[b]);
2808 if (retval == MXSER_ERR_IRQ)
2809 printk(KERN_ERR "Invalid interrupt "
2810 "number, board not "
2812 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2813 printk(KERN_ERR "Invalid interrupt "
2814 "number, board not "
2816 else if (retval == MXSER_ERR_VECTOR)
2817 printk(KERN_ERR "Invalid interrupt "
2818 "vector, board not "
2820 else if (retval == MXSER_ERR_IOADDR)
2821 printk(KERN_ERR "Invalid I/O address, "
2822 "board not configured\n");
2828 /* mxser_initbrd will hook ISR. */
2829 if (mxser_initbrd(brd, NULL) < 0) {
2834 brd->idx = m * MXSER_PORTS_PER_BOARD;
2835 for (i = 0; i < brd->info->nports; i++)
2836 tty_register_device(mxvar_sdriver, brd->idx + i,
2842 retval = pci_register_driver(&mxser_driver);
2844 printk(KERN_ERR "Can't register pci driver\n");
2848 } /* else: we have some ISA cards under control */
2851 pr_debug("Done.\n");
2855 tty_unregister_driver(mxvar_sdriver);
2857 put_tty_driver(mxvar_sdriver);
2861 static void __exit mxser_module_exit(void)
2865 pr_debug("Unloading module mxser ...\n");
2867 pci_unregister_driver(&mxser_driver);
2869 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2870 if (mxser_boards[i].info != NULL)
2871 for (j = 0; j < mxser_boards[i].info->nports; j++)
2872 tty_unregister_device(mxvar_sdriver,
2873 mxser_boards[i].idx + j);
2874 tty_unregister_driver(mxvar_sdriver);
2875 put_tty_driver(mxvar_sdriver);
2877 for (i = 0; i < MXSER_BOARDS; i++)
2878 if (mxser_boards[i].info != NULL)
2879 mxser_release_res(&mxser_boards[i], NULL, 1);
2881 pr_debug("Done.\n");
2884 module_init(mxser_module_init);
2885 module_exit(mxser_module_exit);