2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
5 * Copyright (C) 2006-2007 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
19 * - Fixed sleep with spinlock held in mxser_send_break
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial.h>
31 #include <linux/serial_reg.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/fcntl.h>
35 #include <linux/ptrace.h>
36 #include <linux/gfp.h>
37 #include <linux/ioport.h>
39 #include <linux/delay.h>
40 #include <linux/pci.h>
41 #include <linux/bitops.h>
43 #include <asm/system.h>
46 #include <asm/uaccess.h>
48 #include "mxser_new.h"
50 #define MXSER_VERSION "2.0.2" /* 1.10 */
51 #define MXSERMAJOR 174
52 #define MXSERCUMAJOR 175
54 #define MXSER_BOARDS 4 /* Max. boards */
55 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
56 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
57 #define MXSER_ISR_PASS_LIMIT 100
59 #define MXSER_ERR_IOADDR -1
60 #define MXSER_ERR_IRQ -2
61 #define MXSER_ERR_IRQ_CONFLIT -3
62 #define MXSER_ERR_VECTOR -4
64 /*CheckIsMoxaMust return value*/
65 #define MOXA_OTHER_UART 0x00
66 #define MOXA_MUST_MU150_HWID 0x01
67 #define MOXA_MUST_MU860_HWID 0x02
69 #define WAKEUP_CHARS 256
71 #define UART_MCR_AFE 0x20
72 #define UART_LSR_SPECIAL 0x1E
74 #define PCI_DEVICE_ID_CB108 0x1080
75 #define PCI_DEVICE_ID_CB114 0x1142
76 #define PCI_DEVICE_ID_CB134I 0x1341
77 #define PCI_DEVICE_ID_CP138U 0x1380
78 #define PCI_DEVICE_ID_POS104UL 0x1044
81 #define C168_ASIC_ID 1
82 #define C104_ASIC_ID 2
83 #define C102_ASIC_ID 0xB
84 #define CI132_ASIC_ID 4
85 #define CI134_ASIC_ID 3
86 #define CI104J_ASIC_ID 5
88 #define MXSER_HIGHBAUD 1
91 /* This is only for PCI */
101 } Gpci_uart_info
[] = {
102 {MOXA_OTHER_UART
, 16, 16, 16, 14, 14, 1, 921600L},
103 {MOXA_MUST_MU150_HWID
, 64, 64, 64, 48, 48, 16, 230400L},
104 {MOXA_MUST_MU860_HWID
, 128, 128, 128, 96, 96, 32, 921600L}
106 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
108 struct mxser_cardinfo
{
114 static const struct mxser_cardinfo mxser_cards
[] = {
115 /* 0*/ { 8, "C168 series", },
116 { 4, "C104 series", },
117 { 4, "CI-104J series", },
118 { 8, "C168H/PCI series", },
119 { 4, "C104H/PCI series", },
120 /* 5*/ { 4, "C102 series", MXSER_HAS2
}, /* C102-ISA */
121 { 4, "CI-132 series", MXSER_HAS2
},
122 { 4, "CI-134 series", },
123 { 2, "CP-132 series", },
124 { 4, "CP-114 series", },
125 /*10*/ { 4, "CT-114 series", },
126 { 2, "CP-102 series", MXSER_HIGHBAUD
},
127 { 4, "CP-104U series", },
128 { 8, "CP-168U series", },
129 { 2, "CP-132U series", },
130 /*15*/ { 4, "CP-134U series", },
131 { 4, "CP-104JU series", },
132 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
133 { 8, "CP-118U series", },
134 { 2, "CP-102UL series", },
135 /*20*/ { 2, "CP-102U series", },
136 { 8, "CP-118EL series", },
137 { 8, "CP-168EL series", },
138 { 4, "CP-104EL series", },
139 { 8, "CB-108 series", },
140 /*25*/ { 4, "CB-114 series", },
141 { 4, "CB-134I series", },
142 { 8, "CP-138U series", },
143 { 4, "POS-104UL series", }
146 /* driver_data correspond to the lines in the structure above
147 see also ISA probe function before you change something */
148 static struct pci_device_id mxser_pcibrds
[] = {
149 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_C168
), .driver_data
= 3 },
150 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_C104
), .driver_data
= 4 },
151 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP132
), .driver_data
= 8 },
152 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP114
), .driver_data
= 9 },
153 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CT114
), .driver_data
= 10 },
154 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP102
), .driver_data
= 11 },
155 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP104U
), .driver_data
= 12 },
156 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP168U
), .driver_data
= 13 },
157 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP132U
), .driver_data
= 14 },
158 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP134U
), .driver_data
= 15 },
159 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP104JU
),.driver_data
= 16 },
160 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_RC7000
), .driver_data
= 17 },
161 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP118U
), .driver_data
= 18 },
162 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP102UL
),.driver_data
= 19 },
163 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP102U
), .driver_data
= 20 },
164 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP118EL
),.driver_data
= 21 },
165 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP168EL
),.driver_data
= 22 },
166 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_MOXA_CP104EL
),.driver_data
= 23 },
167 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_CB108
), .driver_data
= 24 },
168 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_CB114
), .driver_data
= 25 },
169 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_CB134I
), .driver_data
= 26 },
170 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_CP138U
), .driver_data
= 27 },
171 { PCI_VDEVICE(MOXA
, PCI_DEVICE_ID_POS104UL
), .driver_data
= 28 },
174 MODULE_DEVICE_TABLE(pci
, mxser_pcibrds
);
176 static int mxvar_baud_table
[] = {
177 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
178 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
180 static unsigned int mxvar_baud_table1
[] = {
181 0, B50
, B75
, B110
, B134
, B150
, B200
, B300
, B600
, B1200
, B1800
, B2400
,
182 B4800
, B9600
, B19200
, B38400
, B57600
, B115200
, B230400
, B460800
, B921600
184 #define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
186 #define B_SPEC B2000000
188 static int ioaddr
[MXSER_BOARDS
] = { 0, 0, 0, 0 };
189 static int ttymajor
= MXSERMAJOR
;
190 static int calloutmajor
= MXSERCUMAJOR
;
192 /* Variables for insmod */
194 MODULE_AUTHOR("Casper Yang");
195 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
196 module_param_array(ioaddr
, int, NULL
, 0);
197 module_param(ttymajor
, int, 0);
198 MODULE_LICENSE("GPL");
202 unsigned long rxcnt
[MXSER_PORTS
];
203 unsigned long txcnt
[MXSER_PORTS
];
210 unsigned long up_rxcnt
;
211 unsigned long up_txcnt
;
213 unsigned char hold_reason
;
216 struct mxser_mon_ext
{
217 unsigned long rx_cnt
[32];
218 unsigned long tx_cnt
[32];
219 unsigned long up_rxcnt
[32];
220 unsigned long up_txcnt
[32];
221 int modem_status
[32];
235 struct mxser_board
*board
;
236 struct tty_struct
*tty
;
238 unsigned long ioaddr
;
239 unsigned long opmode_ioaddr
;
243 int rx_trigger
; /* Rx fifo trigger level */
245 int baud_base
; /* max. speed */
247 int type
; /* UART type */
248 int flags
; /* defined in tty.h */
251 int x_char
; /* xon/xoff character */
252 int IER
; /* Interrupt Enable Register */
253 int MCR
; /* Modem control register */
255 unsigned char stop_rx
;
256 unsigned char ldisc_stop_rx
;
260 unsigned short closing_wait
;
261 unsigned char err_shadow
;
264 int count
; /* # of fd on device */
265 int blocked_open
; /* # of blocked opens */
266 struct async_icount icount
; /* kernel counters for 4 input interrupts */
269 int read_status_mask
;
270 int ignore_status_mask
;
272 unsigned char *xmit_buf
;
277 struct ktermios normal_termios
;
279 struct mxser_mon mon_data
;
282 wait_queue_head_t open_wait
;
283 wait_queue_head_t delta_msr_wait
;
289 const struct mxser_cardinfo
*info
;
290 unsigned long vector
;
291 unsigned long vector_mask
;
296 struct mxser_port ports
[MXSER_PORTS_PER_BOARD
];
299 struct mxser_mstatus
{
307 static struct mxser_mstatus GMStatus
[MXSER_PORTS
];
309 static int mxserBoardCAP
[MXSER_BOARDS
] = {
311 /* 0x180, 0x280, 0x200, 0x320 */
314 static struct mxser_board mxser_boards
[MXSER_BOARDS
];
315 static struct tty_driver
*mxvar_sdriver
;
316 static struct mxser_log mxvar_log
;
317 static int mxvar_diagflag
;
318 static unsigned char mxser_msr
[MXSER_PORTS
+ 1];
319 static struct mxser_mon_ext mon_data_ext
;
320 static int mxser_set_baud_method
[MXSER_PORTS
+ 1];
323 static int __devinit
CheckIsMoxaMust(unsigned long io
)
328 outb(0, io
+ UART_LCR
);
329 DISABLE_MOXA_MUST_ENCHANCE_MODE(io
);
330 oldmcr
= inb(io
+ UART_MCR
);
331 outb(0, io
+ UART_MCR
);
332 SET_MOXA_MUST_XON1_VALUE(io
, 0x11);
333 if ((hwid
= inb(io
+ UART_MCR
)) != 0) {
334 outb(oldmcr
, io
+ UART_MCR
);
335 return MOXA_OTHER_UART
;
338 GET_MOXA_MUST_HARDWARE_ID(io
, &hwid
);
339 for (i
= 1; i
< UART_INFO_NUM
; i
++) { /* 0 = OTHER_UART */
340 if (hwid
== Gpci_uart_info
[i
].type
)
343 return MOXA_OTHER_UART
;
347 static void process_txrx_fifo(struct mxser_port
*info
)
351 if ((info
->type
== PORT_16450
) || (info
->type
== PORT_8250
)) {
352 info
->rx_trigger
= 1;
353 info
->rx_high_water
= 1;
354 info
->rx_low_water
= 1;
355 info
->xmit_fifo_size
= 1;
357 for (i
= 0; i
< UART_INFO_NUM
; i
++)
358 if (info
->board
->chip_flag
== Gpci_uart_info
[i
].type
) {
359 info
->rx_trigger
= Gpci_uart_info
[i
].rx_trigger
;
360 info
->rx_low_water
= Gpci_uart_info
[i
].rx_low_water
;
361 info
->rx_high_water
= Gpci_uart_info
[i
].rx_high_water
;
362 info
->xmit_fifo_size
= Gpci_uart_info
[i
].xmit_fifo_size
;
367 static unsigned char mxser_get_msr(int baseaddr
, int mode
, int port
)
369 unsigned char status
= 0;
371 status
= inb(baseaddr
+ UART_MSR
);
373 mxser_msr
[port
] &= 0x0F;
374 mxser_msr
[port
] |= status
;
375 status
= mxser_msr
[port
];
382 static int mxser_block_til_ready(struct tty_struct
*tty
, struct file
*filp
,
383 struct mxser_port
*port
)
385 DECLARE_WAITQUEUE(wait
, current
);
391 * If non-blocking mode is set, or the port is not enabled,
392 * then make the check up front and then exit.
394 if ((filp
->f_flags
& O_NONBLOCK
) ||
395 test_bit(TTY_IO_ERROR
, &tty
->flags
)) {
396 port
->flags
|= ASYNC_NORMAL_ACTIVE
;
400 if (tty
->termios
->c_cflag
& CLOCAL
)
404 * Block waiting for the carrier detect and the line to become
405 * free (i.e., not in use by the callout). While we are in
406 * this loop, port->count is dropped by one, so that
407 * mxser_close() knows when to free things. We restore it upon
408 * exit, either normal or abnormal.
411 add_wait_queue(&port
->open_wait
, &wait
);
413 spin_lock_irqsave(&port
->slock
, flags
);
414 if (!tty_hung_up_p(filp
))
416 spin_unlock_irqrestore(&port
->slock
, flags
);
417 port
->blocked_open
++;
419 spin_lock_irqsave(&port
->slock
, flags
);
420 outb(inb(port
->ioaddr
+ UART_MCR
) |
421 UART_MCR_DTR
| UART_MCR_RTS
, port
->ioaddr
+ UART_MCR
);
422 spin_unlock_irqrestore(&port
->slock
, flags
);
423 set_current_state(TASK_INTERRUPTIBLE
);
424 if (tty_hung_up_p(filp
) || !(port
->flags
& ASYNC_INITIALIZED
)) {
425 if (port
->flags
& ASYNC_HUP_NOTIFY
)
428 retval
= -ERESTARTSYS
;
431 if (!(port
->flags
& ASYNC_CLOSING
) &&
433 (inb(port
->ioaddr
+ UART_MSR
) & UART_MSR_DCD
)))
435 if (signal_pending(current
)) {
436 retval
= -ERESTARTSYS
;
441 set_current_state(TASK_RUNNING
);
442 remove_wait_queue(&port
->open_wait
, &wait
);
443 if (!tty_hung_up_p(filp
))
445 port
->blocked_open
--;
448 port
->flags
|= ASYNC_NORMAL_ACTIVE
;
452 static int mxser_set_baud(struct mxser_port
*info
, long newspd
)
458 if (!info
->tty
|| !info
->tty
->termios
)
464 if (newspd
> info
->max_baud
)
467 info
->realbaud
= newspd
;
468 for (i
= 0; i
< BAUD_TABLE_NO
; i
++)
469 if (newspd
== mxvar_baud_table
[i
])
471 if (i
== BAUD_TABLE_NO
) {
472 quot
= info
->baud_base
/ info
->speed
;
473 if (info
->speed
<= 0 || info
->speed
> info
->max_baud
)
477 quot
= (2 * info
->baud_base
/ 269);
478 tty_encode_baud_rate(info
->tty
, 134, 134);
480 quot
= info
->baud_base
/ newspd
;
483 baud
= info
->baud_base
/quot
;
484 tty_encode_baud_rate(info
->tty
, baud
, baud
);
490 info
->timeout
= ((info
->xmit_fifo_size
* HZ
* 10 * quot
) / info
->baud_base
);
491 info
->timeout
+= HZ
/ 50; /* Add .02 seconds of slop */
494 info
->MCR
|= UART_MCR_DTR
;
495 outb(info
->MCR
, info
->ioaddr
+ UART_MCR
);
497 info
->MCR
&= ~UART_MCR_DTR
;
498 outb(info
->MCR
, info
->ioaddr
+ UART_MCR
);
502 cval
= inb(info
->ioaddr
+ UART_LCR
);
504 outb(cval
| UART_LCR_DLAB
, info
->ioaddr
+ UART_LCR
); /* set DLAB */
506 outb(quot
& 0xff, info
->ioaddr
+ UART_DLL
); /* LS of divisor */
507 outb(quot
>> 8, info
->ioaddr
+ UART_DLM
); /* MS of divisor */
508 outb(cval
, info
->ioaddr
+ UART_LCR
); /* reset DLAB */
510 if (i
== BAUD_TABLE_NO
) {
511 quot
= info
->baud_base
% info
->speed
;
513 if ((quot
% info
->speed
) > (info
->speed
/ 2)) {
519 SET_MOXA_MUST_ENUM_VALUE(info
->ioaddr
, quot
);
521 SET_MOXA_MUST_ENUM_VALUE(info
->ioaddr
, 0);
527 * This routine is called to set the UART divisor registers to match
528 * the specified baud rate for a serial port.
530 static int mxser_change_speed(struct mxser_port
*info
,
531 struct ktermios
*old_termios
)
533 unsigned cflag
, cval
, fcr
;
535 unsigned char status
;
538 if (!info
->tty
|| !info
->tty
->termios
)
540 cflag
= info
->tty
->termios
->c_cflag
;
544 if (mxser_set_baud_method
[info
->tty
->index
] == 0) {
545 if ((cflag
& CBAUD
) == B_SPEC
)
548 baud
= tty_get_baud_rate(info
->tty
);
549 mxser_set_baud(info
, baud
);
552 /* byte size and parity */
553 switch (cflag
& CSIZE
) {
568 break; /* too keep GCC shut... */
573 cval
|= UART_LCR_PARITY
;
574 if (!(cflag
& PARODD
))
575 cval
|= UART_LCR_EPAR
;
577 cval
|= UART_LCR_SPAR
;
579 if ((info
->type
== PORT_8250
) || (info
->type
== PORT_16450
)) {
580 if (info
->board
->chip_flag
) {
581 fcr
= UART_FCR_ENABLE_FIFO
;
582 fcr
|= MOXA_MUST_FCR_GDA_MODE_ENABLE
;
583 SET_MOXA_MUST_FIFO_VALUE(info
);
587 fcr
= UART_FCR_ENABLE_FIFO
;
588 if (info
->board
->chip_flag
) {
589 fcr
|= MOXA_MUST_FCR_GDA_MODE_ENABLE
;
590 SET_MOXA_MUST_FIFO_VALUE(info
);
592 switch (info
->rx_trigger
) {
594 fcr
|= UART_FCR_TRIGGER_1
;
597 fcr
|= UART_FCR_TRIGGER_4
;
600 fcr
|= UART_FCR_TRIGGER_8
;
603 fcr
|= UART_FCR_TRIGGER_14
;
609 /* CTS flow control flag and modem status interrupts */
610 info
->IER
&= ~UART_IER_MSI
;
611 info
->MCR
&= ~UART_MCR_AFE
;
612 if (cflag
& CRTSCTS
) {
613 info
->flags
|= ASYNC_CTS_FLOW
;
614 info
->IER
|= UART_IER_MSI
;
615 if ((info
->type
== PORT_16550A
) || (info
->board
->chip_flag
)) {
616 info
->MCR
|= UART_MCR_AFE
;
618 status
= inb(info
->ioaddr
+ UART_MSR
);
619 if (info
->tty
->hw_stopped
) {
620 if (status
& UART_MSR_CTS
) {
621 info
->tty
->hw_stopped
= 0;
622 if (info
->type
!= PORT_16550A
&&
623 !info
->board
->chip_flag
) {
624 outb(info
->IER
& ~UART_IER_THRI
,
627 info
->IER
|= UART_IER_THRI
;
628 outb(info
->IER
, info
->ioaddr
+
631 tty_wakeup(info
->tty
);
634 if (!(status
& UART_MSR_CTS
)) {
635 info
->tty
->hw_stopped
= 1;
636 if ((info
->type
!= PORT_16550A
) &&
637 (!info
->board
->chip_flag
)) {
638 info
->IER
&= ~UART_IER_THRI
;
639 outb(info
->IER
, info
->ioaddr
+
646 info
->flags
&= ~ASYNC_CTS_FLOW
;
648 outb(info
->MCR
, info
->ioaddr
+ UART_MCR
);
649 if (cflag
& CLOCAL
) {
650 info
->flags
&= ~ASYNC_CHECK_CD
;
652 info
->flags
|= ASYNC_CHECK_CD
;
653 info
->IER
|= UART_IER_MSI
;
655 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
658 * Set up parity check flag
660 info
->read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
661 if (I_INPCK(info
->tty
))
662 info
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
663 if (I_BRKINT(info
->tty
) || I_PARMRK(info
->tty
))
664 info
->read_status_mask
|= UART_LSR_BI
;
666 info
->ignore_status_mask
= 0;
668 if (I_IGNBRK(info
->tty
)) {
669 info
->ignore_status_mask
|= UART_LSR_BI
;
670 info
->read_status_mask
|= UART_LSR_BI
;
672 * If we're ignore parity and break indicators, ignore
673 * overruns too. (For real raw support).
675 if (I_IGNPAR(info
->tty
)) {
676 info
->ignore_status_mask
|=
680 info
->read_status_mask
|=
686 if (info
->board
->chip_flag
) {
687 SET_MOXA_MUST_XON1_VALUE(info
->ioaddr
, START_CHAR(info
->tty
));
688 SET_MOXA_MUST_XOFF1_VALUE(info
->ioaddr
, STOP_CHAR(info
->tty
));
689 if (I_IXON(info
->tty
)) {
690 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info
->ioaddr
);
692 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info
->ioaddr
);
694 if (I_IXOFF(info
->tty
)) {
695 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info
->ioaddr
);
697 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info
->ioaddr
);
702 outb(fcr
, info
->ioaddr
+ UART_FCR
); /* set fcr */
703 outb(cval
, info
->ioaddr
+ UART_LCR
);
708 static void mxser_check_modem_status(struct mxser_port
*port
, int status
)
710 /* update input line counters */
711 if (status
& UART_MSR_TERI
)
713 if (status
& UART_MSR_DDSR
)
715 if (status
& UART_MSR_DDCD
)
717 if (status
& UART_MSR_DCTS
)
719 port
->mon_data
.modem_status
= status
;
720 wake_up_interruptible(&port
->delta_msr_wait
);
722 if ((port
->flags
& ASYNC_CHECK_CD
) && (status
& UART_MSR_DDCD
)) {
723 if (status
& UART_MSR_DCD
)
724 wake_up_interruptible(&port
->open_wait
);
727 if (port
->flags
& ASYNC_CTS_FLOW
) {
728 if (port
->tty
->hw_stopped
) {
729 if (status
& UART_MSR_CTS
) {
730 port
->tty
->hw_stopped
= 0;
732 if ((port
->type
!= PORT_16550A
) &&
733 (!port
->board
->chip_flag
)) {
734 outb(port
->IER
& ~UART_IER_THRI
,
735 port
->ioaddr
+ UART_IER
);
736 port
->IER
|= UART_IER_THRI
;
737 outb(port
->IER
, port
->ioaddr
+
740 tty_wakeup(port
->tty
);
743 if (!(status
& UART_MSR_CTS
)) {
744 port
->tty
->hw_stopped
= 1;
745 if (port
->type
!= PORT_16550A
&&
746 !port
->board
->chip_flag
) {
747 port
->IER
&= ~UART_IER_THRI
;
748 outb(port
->IER
, port
->ioaddr
+
756 static int mxser_startup(struct mxser_port
*info
)
761 page
= __get_free_page(GFP_KERNEL
);
765 spin_lock_irqsave(&info
->slock
, flags
);
767 if (info
->flags
& ASYNC_INITIALIZED
) {
769 spin_unlock_irqrestore(&info
->slock
, flags
);
773 if (!info
->ioaddr
|| !info
->type
) {
775 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
777 spin_unlock_irqrestore(&info
->slock
, flags
);
783 info
->xmit_buf
= (unsigned char *) page
;
786 * Clear the FIFO buffers and disable them
787 * (they will be reenabled in mxser_change_speed())
789 if (info
->board
->chip_flag
)
790 outb((UART_FCR_CLEAR_RCVR
|
791 UART_FCR_CLEAR_XMIT
|
792 MOXA_MUST_FCR_GDA_MODE_ENABLE
), info
->ioaddr
+ UART_FCR
);
794 outb((UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
),
795 info
->ioaddr
+ UART_FCR
);
798 * At this point there's no way the LSR could still be 0xFF;
799 * if it is, then bail out, because there's likely no UART
802 if (inb(info
->ioaddr
+ UART_LSR
) == 0xff) {
803 spin_unlock_irqrestore(&info
->slock
, flags
);
804 if (capable(CAP_SYS_ADMIN
)) {
806 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
813 * Clear the interrupt registers.
815 (void) inb(info
->ioaddr
+ UART_LSR
);
816 (void) inb(info
->ioaddr
+ UART_RX
);
817 (void) inb(info
->ioaddr
+ UART_IIR
);
818 (void) inb(info
->ioaddr
+ UART_MSR
);
821 * Now, initialize the UART
823 outb(UART_LCR_WLEN8
, info
->ioaddr
+ UART_LCR
); /* reset DLAB */
824 info
->MCR
= UART_MCR_DTR
| UART_MCR_RTS
;
825 outb(info
->MCR
, info
->ioaddr
+ UART_MCR
);
828 * Finally, enable interrupts
830 info
->IER
= UART_IER_MSI
| UART_IER_RLSI
| UART_IER_RDI
;
832 if (info
->board
->chip_flag
)
833 info
->IER
|= MOXA_MUST_IER_EGDAI
;
834 outb(info
->IER
, info
->ioaddr
+ UART_IER
); /* enable interrupts */
837 * And clear the interrupt registers again for luck.
839 (void) inb(info
->ioaddr
+ UART_LSR
);
840 (void) inb(info
->ioaddr
+ UART_RX
);
841 (void) inb(info
->ioaddr
+ UART_IIR
);
842 (void) inb(info
->ioaddr
+ UART_MSR
);
845 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
846 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
849 * and set the speed of the serial port
851 mxser_change_speed(info
, NULL
);
852 info
->flags
|= ASYNC_INITIALIZED
;
853 spin_unlock_irqrestore(&info
->slock
, flags
);
859 * This routine will shutdown a serial port; interrupts maybe disabled, and
860 * DTR is dropped if the hangup on close termio flag is on.
862 static void mxser_shutdown(struct mxser_port
*info
)
866 if (!(info
->flags
& ASYNC_INITIALIZED
))
869 spin_lock_irqsave(&info
->slock
, flags
);
872 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
873 * here so the queue might never be waken up
875 wake_up_interruptible(&info
->delta_msr_wait
);
878 * Free the IRQ, if necessary
880 if (info
->xmit_buf
) {
881 free_page((unsigned long) info
->xmit_buf
);
882 info
->xmit_buf
= NULL
;
886 outb(0x00, info
->ioaddr
+ UART_IER
);
888 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
))
889 info
->MCR
&= ~(UART_MCR_DTR
| UART_MCR_RTS
);
890 outb(info
->MCR
, info
->ioaddr
+ UART_MCR
);
892 /* clear Rx/Tx FIFO's */
893 if (info
->board
->chip_flag
)
894 outb(UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
|
895 MOXA_MUST_FCR_GDA_MODE_ENABLE
,
896 info
->ioaddr
+ UART_FCR
);
898 outb(UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
,
899 info
->ioaddr
+ UART_FCR
);
901 /* read data port to reset things */
902 (void) inb(info
->ioaddr
+ UART_RX
);
905 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
907 info
->flags
&= ~ASYNC_INITIALIZED
;
909 if (info
->board
->chip_flag
)
910 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info
->ioaddr
);
912 spin_unlock_irqrestore(&info
->slock
, flags
);
916 * This routine is called whenever a serial port is opened. It
917 * enables interrupts for a serial port, linking in its async structure into
918 * the IRQ chain. It also performs the serial-specific
919 * initialization for the tty structure.
921 static int mxser_open(struct tty_struct
*tty
, struct file
*filp
)
923 struct mxser_port
*info
;
928 if (line
== MXSER_PORTS
)
930 if (line
< 0 || line
> MXSER_PORTS
)
932 info
= &mxser_boards
[line
/ MXSER_PORTS_PER_BOARD
].ports
[line
% MXSER_PORTS_PER_BOARD
];
936 tty
->driver_data
= info
;
939 * Start up serial port
941 spin_lock_irqsave(&info
->slock
, flags
);
943 spin_unlock_irqrestore(&info
->slock
, flags
);
944 retval
= mxser_startup(info
);
948 retval
= mxser_block_til_ready(tty
, filp
, info
);
952 /* unmark here for very high baud rate (ex. 921600 bps) used */
953 tty
->low_latency
= 1;
958 * This routine is called when the serial port gets closed. First, we
959 * wait for the last remaining data to be sent. Then, we unlink its
960 * async structure from the interrupt chain if necessary, and we free
961 * that IRQ if nothing is left in the chain.
963 static void mxser_close(struct tty_struct
*tty
, struct file
*filp
)
965 struct mxser_port
*info
= tty
->driver_data
;
967 unsigned long timeout
;
970 if (tty
->index
== MXSER_PORTS
)
975 spin_lock_irqsave(&info
->slock
, flags
);
977 if (tty_hung_up_p(filp
)) {
978 spin_unlock_irqrestore(&info
->slock
, flags
);
981 if ((tty
->count
== 1) && (info
->count
!= 1)) {
983 * Uh, oh. tty->count is 1, which means that the tty
984 * structure will be freed. Info->count should always
985 * be one in these conditions. If it's greater than
986 * one, we've got real problems, since it means the
987 * serial port won't be shutdown.
989 printk(KERN_ERR
"mxser_close: bad serial port count; "
990 "tty->count is 1, info->count is %d\n", info
->count
);
993 if (--info
->count
< 0) {
994 printk(KERN_ERR
"mxser_close: bad serial port count for "
995 "ttys%d: %d\n", tty
->index
, info
->count
);
999 spin_unlock_irqrestore(&info
->slock
, flags
);
1002 info
->flags
|= ASYNC_CLOSING
;
1003 spin_unlock_irqrestore(&info
->slock
, flags
);
1005 * Save the termios structure, since this port may have
1006 * separate termios for callout and dialin.
1008 if (info
->flags
& ASYNC_NORMAL_ACTIVE
)
1009 info
->normal_termios
= *tty
->termios
;
1011 * Now we wait for the transmit buffer to clear; and we notify
1012 * the line discipline to only process XON/XOFF characters.
1015 if (info
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
1016 tty_wait_until_sent(tty
, info
->closing_wait
);
1018 * At this point we stop accepting input. To do this, we
1019 * disable the receive line status interrupts, and tell the
1020 * interrupt driver to stop checking the data ready bit in the
1021 * line status register.
1023 info
->IER
&= ~UART_IER_RLSI
;
1024 if (info
->board
->chip_flag
)
1025 info
->IER
&= ~MOXA_MUST_RECV_ISR
;
1027 if (info
->flags
& ASYNC_INITIALIZED
) {
1028 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1030 * Before we drop DTR, make sure the UART transmitter
1031 * has completely drained; this is especially
1032 * important if there is a transmit FIFO!
1034 timeout
= jiffies
+ HZ
;
1035 while (!(inb(info
->ioaddr
+ UART_LSR
) & UART_LSR_TEMT
)) {
1036 schedule_timeout_interruptible(5);
1037 if (time_after(jiffies
, timeout
))
1041 mxser_shutdown(info
);
1043 if (tty
->driver
->flush_buffer
)
1044 tty
->driver
->flush_buffer(tty
);
1046 tty_ldisc_flush(tty
);
1051 if (info
->blocked_open
) {
1052 if (info
->close_delay
)
1053 schedule_timeout_interruptible(info
->close_delay
);
1054 wake_up_interruptible(&info
->open_wait
);
1057 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
| ASYNC_CLOSING
);
1060 static int mxser_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
1063 struct mxser_port
*info
= tty
->driver_data
;
1064 unsigned long flags
;
1066 if (!info
->xmit_buf
)
1070 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
1071 SERIAL_XMIT_SIZE
- info
->xmit_head
));
1075 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
1076 spin_lock_irqsave(&info
->slock
, flags
);
1077 info
->xmit_head
= (info
->xmit_head
+ c
) &
1078 (SERIAL_XMIT_SIZE
- 1);
1079 info
->xmit_cnt
+= c
;
1080 spin_unlock_irqrestore(&info
->slock
, flags
);
1087 if (info
->xmit_cnt
&& !tty
->stopped
) {
1088 if (!tty
->hw_stopped
||
1089 (info
->type
== PORT_16550A
) ||
1090 (info
->board
->chip_flag
)) {
1091 spin_lock_irqsave(&info
->slock
, flags
);
1092 outb(info
->IER
& ~UART_IER_THRI
, info
->ioaddr
+
1094 info
->IER
|= UART_IER_THRI
;
1095 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1096 spin_unlock_irqrestore(&info
->slock
, flags
);
1102 static void mxser_put_char(struct tty_struct
*tty
, unsigned char ch
)
1104 struct mxser_port
*info
= tty
->driver_data
;
1105 unsigned long flags
;
1107 if (!info
->xmit_buf
)
1110 if (info
->xmit_cnt
>= SERIAL_XMIT_SIZE
- 1)
1113 spin_lock_irqsave(&info
->slock
, flags
);
1114 info
->xmit_buf
[info
->xmit_head
++] = ch
;
1115 info
->xmit_head
&= SERIAL_XMIT_SIZE
- 1;
1117 spin_unlock_irqrestore(&info
->slock
, flags
);
1118 if (!tty
->stopped
) {
1119 if (!tty
->hw_stopped
||
1120 (info
->type
== PORT_16550A
) ||
1121 info
->board
->chip_flag
) {
1122 spin_lock_irqsave(&info
->slock
, flags
);
1123 outb(info
->IER
& ~UART_IER_THRI
, info
->ioaddr
+ UART_IER
);
1124 info
->IER
|= UART_IER_THRI
;
1125 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1126 spin_unlock_irqrestore(&info
->slock
, flags
);
1132 static void mxser_flush_chars(struct tty_struct
*tty
)
1134 struct mxser_port
*info
= tty
->driver_data
;
1135 unsigned long flags
;
1137 if (info
->xmit_cnt
<= 0 ||
1141 (info
->type
!= PORT_16550A
) &&
1142 (!info
->board
->chip_flag
)
1146 spin_lock_irqsave(&info
->slock
, flags
);
1148 outb(info
->IER
& ~UART_IER_THRI
, info
->ioaddr
+ UART_IER
);
1149 info
->IER
|= UART_IER_THRI
;
1150 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1152 spin_unlock_irqrestore(&info
->slock
, flags
);
1155 static int mxser_write_room(struct tty_struct
*tty
)
1157 struct mxser_port
*info
= tty
->driver_data
;
1160 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
1166 static int mxser_chars_in_buffer(struct tty_struct
*tty
)
1168 struct mxser_port
*info
= tty
->driver_data
;
1169 return info
->xmit_cnt
;
1172 static void mxser_flush_buffer(struct tty_struct
*tty
)
1174 struct mxser_port
*info
= tty
->driver_data
;
1176 unsigned long flags
;
1179 spin_lock_irqsave(&info
->slock
, flags
);
1180 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
1182 fcr
= inb(info
->ioaddr
+ UART_FCR
);
1183 outb((fcr
| UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
),
1184 info
->ioaddr
+ UART_FCR
);
1185 outb(fcr
, info
->ioaddr
+ UART_FCR
);
1187 spin_unlock_irqrestore(&info
->slock
, flags
);
1193 * ------------------------------------------------------------
1194 * friends of mxser_ioctl()
1195 * ------------------------------------------------------------
1197 static int mxser_get_serial_info(struct mxser_port
*info
,
1198 struct serial_struct __user
*retinfo
)
1200 struct serial_struct tmp
;
1204 memset(&tmp
, 0, sizeof(tmp
));
1205 tmp
.type
= info
->type
;
1206 tmp
.line
= info
->tty
->index
;
1207 tmp
.port
= info
->ioaddr
;
1208 tmp
.irq
= info
->board
->irq
;
1209 tmp
.flags
= info
->flags
;
1210 tmp
.baud_base
= info
->baud_base
;
1211 tmp
.close_delay
= info
->close_delay
;
1212 tmp
.closing_wait
= info
->closing_wait
;
1213 tmp
.custom_divisor
= info
->custom_divisor
;
1215 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
1220 static int mxser_set_serial_info(struct mxser_port
*info
,
1221 struct serial_struct __user
*new_info
)
1223 struct serial_struct new_serial
;
1224 unsigned long sl_flags
;
1228 if (!new_info
|| !info
->ioaddr
)
1230 if (copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
1233 if ((new_serial
.irq
!= info
->board
->irq
) ||
1234 (new_serial
.port
!= info
->ioaddr
) ||
1235 (new_serial
.custom_divisor
!= info
->custom_divisor
) ||
1236 (new_serial
.baud_base
!= info
->baud_base
))
1239 flags
= info
->flags
& ASYNC_SPD_MASK
;
1241 if (!capable(CAP_SYS_ADMIN
)) {
1242 if ((new_serial
.baud_base
!= info
->baud_base
) ||
1243 (new_serial
.close_delay
!= info
->close_delay
) ||
1244 ((new_serial
.flags
& ~ASYNC_USR_MASK
) != (info
->flags
& ~ASYNC_USR_MASK
)))
1246 info
->flags
= ((info
->flags
& ~ASYNC_USR_MASK
) |
1247 (new_serial
.flags
& ASYNC_USR_MASK
));
1250 * OK, past this point, all the error checking has been done.
1251 * At this point, we start making changes.....
1253 info
->flags
= ((info
->flags
& ~ASYNC_FLAGS
) |
1254 (new_serial
.flags
& ASYNC_FLAGS
));
1255 info
->close_delay
= new_serial
.close_delay
* HZ
/ 100;
1256 info
->closing_wait
= new_serial
.closing_wait
* HZ
/ 100;
1257 info
->tty
->low_latency
=
1258 (info
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
1259 info
->tty
->low_latency
= 0;
1262 info
->type
= new_serial
.type
;
1264 process_txrx_fifo(info
);
1266 if (info
->flags
& ASYNC_INITIALIZED
) {
1267 if (flags
!= (info
->flags
& ASYNC_SPD_MASK
)) {
1268 spin_lock_irqsave(&info
->slock
, sl_flags
);
1269 mxser_change_speed(info
, NULL
);
1270 spin_unlock_irqrestore(&info
->slock
, sl_flags
);
1273 retval
= mxser_startup(info
);
1279 * mxser_get_lsr_info - get line status register info
1281 * Purpose: Let user call ioctl() to get info when the UART physically
1282 * is emptied. On bus types like RS485, the transmitter must
1283 * release the bus after transmitting. This must be done when
1284 * the transmit shift register is empty, not be done when the
1285 * transmit holding register is empty. This functionality
1286 * allows an RS485 driver to be written in user space.
1288 static int mxser_get_lsr_info(struct mxser_port
*info
,
1289 unsigned int __user
*value
)
1291 unsigned char status
;
1292 unsigned int result
;
1293 unsigned long flags
;
1295 spin_lock_irqsave(&info
->slock
, flags
);
1296 status
= inb(info
->ioaddr
+ UART_LSR
);
1297 spin_unlock_irqrestore(&info
->slock
, flags
);
1298 result
= ((status
& UART_LSR_TEMT
) ? TIOCSER_TEMT
: 0);
1299 return put_user(result
, value
);
1303 * This routine sends a break character out the serial port.
1305 static void mxser_send_break(struct mxser_port
*info
, int duration
)
1307 unsigned long flags
;
1311 set_current_state(TASK_INTERRUPTIBLE
);
1312 spin_lock_irqsave(&info
->slock
, flags
);
1313 outb(inb(info
->ioaddr
+ UART_LCR
) | UART_LCR_SBC
,
1314 info
->ioaddr
+ UART_LCR
);
1315 spin_unlock_irqrestore(&info
->slock
, flags
);
1316 schedule_timeout(duration
);
1317 spin_lock_irqsave(&info
->slock
, flags
);
1318 outb(inb(info
->ioaddr
+ UART_LCR
) & ~UART_LCR_SBC
,
1319 info
->ioaddr
+ UART_LCR
);
1320 spin_unlock_irqrestore(&info
->slock
, flags
);
1323 static int mxser_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1325 struct mxser_port
*info
= tty
->driver_data
;
1326 unsigned char control
, status
;
1327 unsigned long flags
;
1330 if (tty
->index
== MXSER_PORTS
)
1331 return -ENOIOCTLCMD
;
1332 if (test_bit(TTY_IO_ERROR
, &tty
->flags
))
1335 control
= info
->MCR
;
1337 spin_lock_irqsave(&info
->slock
, flags
);
1338 status
= inb(info
->ioaddr
+ UART_MSR
);
1339 if (status
& UART_MSR_ANY_DELTA
)
1340 mxser_check_modem_status(info
, status
);
1341 spin_unlock_irqrestore(&info
->slock
, flags
);
1342 return ((control
& UART_MCR_RTS
) ? TIOCM_RTS
: 0) |
1343 ((control
& UART_MCR_DTR
) ? TIOCM_DTR
: 0) |
1344 ((status
& UART_MSR_DCD
) ? TIOCM_CAR
: 0) |
1345 ((status
& UART_MSR_RI
) ? TIOCM_RNG
: 0) |
1346 ((status
& UART_MSR_DSR
) ? TIOCM_DSR
: 0) |
1347 ((status
& UART_MSR_CTS
) ? TIOCM_CTS
: 0);
1350 static int mxser_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1351 unsigned int set
, unsigned int clear
)
1353 struct mxser_port
*info
= tty
->driver_data
;
1354 unsigned long flags
;
1357 if (tty
->index
== MXSER_PORTS
)
1358 return -ENOIOCTLCMD
;
1359 if (test_bit(TTY_IO_ERROR
, &tty
->flags
))
1362 spin_lock_irqsave(&info
->slock
, flags
);
1364 if (set
& TIOCM_RTS
)
1365 info
->MCR
|= UART_MCR_RTS
;
1366 if (set
& TIOCM_DTR
)
1367 info
->MCR
|= UART_MCR_DTR
;
1369 if (clear
& TIOCM_RTS
)
1370 info
->MCR
&= ~UART_MCR_RTS
;
1371 if (clear
& TIOCM_DTR
)
1372 info
->MCR
&= ~UART_MCR_DTR
;
1374 outb(info
->MCR
, info
->ioaddr
+ UART_MCR
);
1375 spin_unlock_irqrestore(&info
->slock
, flags
);
1379 static int __init
mxser_program_mode(int port
)
1391 id
= inb(port
+ 1) & 0x1F;
1392 if ((id
!= C168_ASIC_ID
) &&
1393 (id
!= C104_ASIC_ID
) &&
1394 (id
!= C102_ASIC_ID
) &&
1395 (id
!= CI132_ASIC_ID
) &&
1396 (id
!= CI134_ASIC_ID
) &&
1397 (id
!= CI104J_ASIC_ID
))
1399 for (i
= 0, j
= 0; i
< 4; i
++) {
1403 } else if ((j
== 1) && (n
== 1)) {
1414 static void __init
mxser_normal_mode(int port
)
1418 outb(0xA5, port
+ 1);
1419 outb(0x80, port
+ 3);
1420 outb(12, port
+ 0); /* 9600 bps */
1422 outb(0x03, port
+ 3); /* 8 data bits */
1423 outb(0x13, port
+ 4); /* loop back mode */
1424 for (i
= 0; i
< 16; i
++) {
1426 if ((n
& 0x61) == 0x60)
1431 outb(0x00, port
+ 4);
1434 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1435 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1436 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1437 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1438 #define EN_CCMD 0x000 /* Chip's command register */
1439 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1440 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1441 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1442 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1443 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1444 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1445 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1446 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1447 static int __init
mxser_read_register(int port
, unsigned short *regs
)
1449 int i
, k
, value
, id
;
1452 id
= mxser_program_mode(port
);
1455 for (i
= 0; i
< 14; i
++) {
1456 k
= (i
& 0x3F) | 0x180;
1457 for (j
= 0x100; j
> 0; j
>>= 1) {
1458 outb(CHIP_CS
, port
);
1460 outb(CHIP_CS
| CHIP_DO
, port
);
1461 outb(CHIP_CS
| CHIP_DO
| CHIP_SK
, port
); /* A? bit of read */
1463 outb(CHIP_CS
, port
);
1464 outb(CHIP_CS
| CHIP_SK
, port
); /* A? bit of read */
1469 for (k
= 0, j
= 0x8000; k
< 16; k
++, j
>>= 1) {
1470 outb(CHIP_CS
, port
);
1471 outb(CHIP_CS
| CHIP_SK
, port
);
1472 if (inb(port
) & CHIP_DI
)
1478 mxser_normal_mode(port
);
1482 static int mxser_ioctl_special(unsigned int cmd
, void __user
*argp
)
1484 struct mxser_port
*port
;
1490 /* if (copy_to_user(argp, mxsercfg,
1491 sizeof(struct mxser_hwconf) * 4))
1495 case MOXA_GET_MAJOR
:
1496 if (copy_to_user(argp
, &ttymajor
, sizeof(int)))
1500 case MOXA_GET_CUMAJOR
:
1501 if (copy_to_user(argp
, &calloutmajor
, sizeof(int)))
1505 case MOXA_CHKPORTENABLE
:
1508 for (i
= 0; i
< MXSER_BOARDS
; i
++)
1509 for (j
= 0; j
< MXSER_PORTS_PER_BOARD
; j
++)
1510 if (mxser_boards
[i
].ports
[j
].ioaddr
)
1513 return put_user(result
, (unsigned long __user
*)argp
);
1514 case MOXA_GETDATACOUNT
:
1515 if (copy_to_user(argp
, &mxvar_log
, sizeof(mxvar_log
)))
1518 case MOXA_GETMSTATUS
:
1519 for (i
= 0; i
< MXSER_BOARDS
; i
++)
1520 for (j
= 0; j
< MXSER_PORTS_PER_BOARD
; j
++) {
1521 port
= &mxser_boards
[i
].ports
[j
];
1524 if (!port
->ioaddr
) {
1525 GMStatus
[i
].dcd
= 0;
1526 GMStatus
[i
].dsr
= 0;
1527 GMStatus
[i
].cts
= 0;
1531 if (!port
->tty
|| !port
->tty
->termios
)
1533 port
->normal_termios
.c_cflag
;
1536 port
->tty
->termios
->c_cflag
;
1538 status
= inb(port
->ioaddr
+ UART_MSR
);
1539 if (status
& 0x80 /*UART_MSR_DCD */ )
1540 GMStatus
[i
].dcd
= 1;
1542 GMStatus
[i
].dcd
= 0;
1544 if (status
& 0x20 /*UART_MSR_DSR */ )
1545 GMStatus
[i
].dsr
= 1;
1547 GMStatus
[i
].dsr
= 0;
1550 if (status
& 0x10 /*UART_MSR_CTS */ )
1551 GMStatus
[i
].cts
= 1;
1553 GMStatus
[i
].cts
= 0;
1555 if (copy_to_user(argp
, GMStatus
,
1556 sizeof(struct mxser_mstatus
) * MXSER_PORTS
))
1559 case MOXA_ASPP_MON_EXT
: {
1561 unsigned long opmode
;
1562 unsigned cflag
, iflag
;
1564 for (i
= 0; i
< MXSER_BOARDS
; i
++)
1565 for (j
= 0; j
< MXSER_PORTS_PER_BOARD
; j
++) {
1566 port
= &mxser_boards
[i
].ports
[j
];
1570 status
= mxser_get_msr(port
->ioaddr
, 0, i
);
1572 if (status
& UART_MSR_TERI
)
1574 if (status
& UART_MSR_DDSR
)
1576 if (status
& UART_MSR_DDCD
)
1578 if (status
& UART_MSR_DCTS
)
1581 port
->mon_data
.modem_status
= status
;
1582 mon_data_ext
.rx_cnt
[i
] = port
->mon_data
.rxcnt
;
1583 mon_data_ext
.tx_cnt
[i
] = port
->mon_data
.txcnt
;
1584 mon_data_ext
.up_rxcnt
[i
] =
1585 port
->mon_data
.up_rxcnt
;
1586 mon_data_ext
.up_txcnt
[i
] =
1587 port
->mon_data
.up_txcnt
;
1588 mon_data_ext
.modem_status
[i
] =
1589 port
->mon_data
.modem_status
;
1590 mon_data_ext
.baudrate
[i
] = port
->realbaud
;
1592 if (!port
->tty
|| !port
->tty
->termios
) {
1593 cflag
= port
->normal_termios
.c_cflag
;
1594 iflag
= port
->normal_termios
.c_iflag
;
1596 cflag
= port
->tty
->termios
->c_cflag
;
1597 iflag
= port
->tty
->termios
->c_iflag
;
1600 mon_data_ext
.databits
[i
] = cflag
& CSIZE
;
1602 mon_data_ext
.stopbits
[i
] = cflag
& CSTOPB
;
1604 mon_data_ext
.parity
[i
] =
1605 cflag
& (PARENB
| PARODD
| CMSPAR
);
1607 mon_data_ext
.flowctrl
[i
] = 0x00;
1609 if (cflag
& CRTSCTS
)
1610 mon_data_ext
.flowctrl
[i
] |= 0x03;
1612 if (iflag
& (IXON
| IXOFF
))
1613 mon_data_ext
.flowctrl
[i
] |= 0x0C;
1615 if (port
->type
== PORT_16550A
)
1616 mon_data_ext
.fifo
[i
] = 1;
1618 mon_data_ext
.fifo
[i
] = 0;
1622 opmode
= inb(port
->opmode_ioaddr
) >> shiftbit
;
1623 opmode
&= OP_MODE_MASK
;
1625 mon_data_ext
.iftype
[i
] = opmode
;
1628 if (copy_to_user(argp
, &mon_data_ext
,
1629 sizeof(mon_data_ext
)))
1635 return -ENOIOCTLCMD
;
1640 static int mxser_ioctl(struct tty_struct
*tty
, struct file
*file
,
1641 unsigned int cmd
, unsigned long arg
)
1643 struct mxser_port
*info
= tty
->driver_data
;
1644 struct async_icount cprev
, cnow
; /* kernel counter temps */
1645 struct serial_icounter_struct __user
*p_cuser
;
1646 unsigned long templ
;
1647 unsigned long flags
;
1649 void __user
*argp
= (void __user
*)arg
;
1652 if (tty
->index
== MXSER_PORTS
)
1653 return mxser_ioctl_special(cmd
, argp
);
1655 if (cmd
== MOXA_SET_OP_MODE
|| cmd
== MOXA_GET_OP_MODE
) {
1657 unsigned long opmode
;
1658 static unsigned char ModeMask
[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1660 unsigned char val
, mask
;
1663 if (cmd
== MOXA_SET_OP_MODE
) {
1664 if (get_user(opmode
, (int __user
*) argp
))
1666 if (opmode
!= RS232_MODE
&&
1667 opmode
!= RS485_2WIRE_MODE
&&
1668 opmode
!= RS422_MODE
&&
1669 opmode
!= RS485_4WIRE_MODE
)
1673 val
= inb(info
->opmode_ioaddr
);
1675 val
|= (opmode
<< shiftbit
);
1676 outb(val
, info
->opmode_ioaddr
);
1679 opmode
= inb(info
->opmode_ioaddr
) >> shiftbit
;
1680 opmode
&= OP_MODE_MASK
;
1681 if (copy_to_user(argp
, &opmode
, sizeof(int)))
1687 if (cmd
== MOXA_SET_SPECIAL_BAUD_RATE
) {
1690 if (get_user(speed
, (int __user
*)argp
))
1692 if (speed
<= 0 || speed
> info
->max_baud
)
1694 if (!info
->tty
|| !info
->tty
->termios
|| !info
->ioaddr
)
1696 info
->tty
->termios
->c_cflag
&= ~(CBAUD
| CBAUDEX
);
1697 for (i
= 0; i
< BAUD_TABLE_NO
; i
++)
1698 if (speed
== mxvar_baud_table
[i
])
1700 if (i
== BAUD_TABLE_NO
) {
1701 info
->tty
->termios
->c_cflag
|= B_SPEC
;
1702 } else if (speed
!= 0)
1703 info
->tty
->termios
->c_cflag
|= mxvar_baud_table1
[i
];
1705 info
->speed
= speed
;
1706 spin_lock_irqsave(&info
->slock
, flags
);
1707 mxser_change_speed(info
, NULL
);
1708 spin_unlock_irqrestore(&info
->slock
, flags
);
1711 } else if (cmd
== MOXA_GET_SPECIAL_BAUD_RATE
) {
1712 if (copy_to_user(argp
, &info
->speed
, sizeof(int)))
1717 if (cmd
!= TIOCGSERIAL
&& cmd
!= TIOCMIWAIT
&& cmd
!= TIOCGICOUNT
&&
1718 test_bit(TTY_IO_ERROR
, &tty
->flags
))
1722 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1723 retval
= tty_check_change(tty
);
1726 tty_wait_until_sent(tty
, 0);
1728 mxser_send_break(info
, HZ
/ 4); /* 1/4 second */
1730 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1731 retval
= tty_check_change(tty
);
1734 tty_wait_until_sent(tty
, 0);
1735 mxser_send_break(info
, arg
? arg
* (HZ
/ 10) : HZ
/ 4);
1738 return put_user(C_CLOCAL(tty
) ? 1 : 0, (unsigned long __user
*)argp
);
1740 if (get_user(templ
, (unsigned long __user
*) argp
))
1743 tty
->termios
->c_cflag
= ((tty
->termios
->c_cflag
& ~CLOCAL
) | (arg
? CLOCAL
: 0));
1746 return mxser_get_serial_info(info
, argp
);
1748 return mxser_set_serial_info(info
, argp
);
1749 case TIOCSERGETLSR
: /* Get line status register */
1750 return mxser_get_lsr_info(info
, argp
);
1752 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1753 * - mask passed in arg for lines of interest
1754 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1755 * Caller should use TIOCGICOUNT to see which one it was
1758 spin_lock_irqsave(&info
->slock
, flags
);
1759 cnow
= info
->icount
; /* note the counters on entry */
1760 spin_unlock_irqrestore(&info
->slock
, flags
);
1762 wait_event_interruptible(info
->delta_msr_wait
, ({
1764 spin_lock_irqsave(&info
->slock
, flags
);
1765 cnow
= info
->icount
; /* atomic copy */
1766 spin_unlock_irqrestore(&info
->slock
, flags
);
1768 ((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1769 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1770 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1771 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
));
1775 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1776 * Return: write counters to the user passed counter struct
1777 * NB: both 1->0 and 0->1 transitions are counted except for
1778 * RI where only 0->1 is counted.
1781 spin_lock_irqsave(&info
->slock
, flags
);
1782 cnow
= info
->icount
;
1783 spin_unlock_irqrestore(&info
->slock
, flags
);
1785 if (put_user(cnow
.frame
, &p_cuser
->frame
))
1787 if (put_user(cnow
.brk
, &p_cuser
->brk
))
1789 if (put_user(cnow
.overrun
, &p_cuser
->overrun
))
1791 if (put_user(cnow
.buf_overrun
, &p_cuser
->buf_overrun
))
1793 if (put_user(cnow
.parity
, &p_cuser
->parity
))
1795 if (put_user(cnow
.rx
, &p_cuser
->rx
))
1797 if (put_user(cnow
.tx
, &p_cuser
->tx
))
1799 put_user(cnow
.cts
, &p_cuser
->cts
);
1800 put_user(cnow
.dsr
, &p_cuser
->dsr
);
1801 put_user(cnow
.rng
, &p_cuser
->rng
);
1802 put_user(cnow
.dcd
, &p_cuser
->dcd
);
1804 case MOXA_HighSpeedOn
:
1805 return put_user(info
->baud_base
!= 115200 ? 1 : 0, (int __user
*)argp
);
1806 case MOXA_SDS_RSTICOUNTER
:
1807 info
->mon_data
.rxcnt
= 0;
1808 info
->mon_data
.txcnt
= 0;
1810 case MOXA_ASPP_SETBAUD
:{
1812 if (get_user(baud
, (long __user
*)argp
))
1814 spin_lock_irqsave(&info
->slock
, flags
);
1815 mxser_set_baud(info
, baud
);
1816 spin_unlock_irqrestore(&info
->slock
, flags
);
1819 case MOXA_ASPP_GETBAUD
:
1820 if (copy_to_user(argp
, &info
->realbaud
, sizeof(long)))
1825 case MOXA_ASPP_OQUEUE
:{
1828 len
= mxser_chars_in_buffer(tty
);
1830 lsr
= inb(info
->ioaddr
+ UART_LSR
) & UART_LSR_TEMT
;
1832 len
+= (lsr
? 0 : 1);
1834 if (copy_to_user(argp
, &len
, sizeof(int)))
1839 case MOXA_ASPP_MON
: {
1842 status
= mxser_get_msr(info
->ioaddr
, 1, tty
->index
);
1843 mxser_check_modem_status(info
, status
);
1845 mcr
= inb(info
->ioaddr
+ UART_MCR
);
1846 if (mcr
& MOXA_MUST_MCR_XON_FLAG
)
1847 info
->mon_data
.hold_reason
&= ~NPPI_NOTIFY_XOFFHOLD
;
1849 info
->mon_data
.hold_reason
|= NPPI_NOTIFY_XOFFHOLD
;
1851 if (mcr
& MOXA_MUST_MCR_TX_XON
)
1852 info
->mon_data
.hold_reason
&= ~NPPI_NOTIFY_XOFFXENT
;
1854 info
->mon_data
.hold_reason
|= NPPI_NOTIFY_XOFFXENT
;
1856 if (info
->tty
->hw_stopped
)
1857 info
->mon_data
.hold_reason
|= NPPI_NOTIFY_CTSHOLD
;
1859 info
->mon_data
.hold_reason
&= ~NPPI_NOTIFY_CTSHOLD
;
1861 if (copy_to_user(argp
, &info
->mon_data
,
1862 sizeof(struct mxser_mon
)))
1867 case MOXA_ASPP_LSTATUS
: {
1868 if (copy_to_user(argp
, &info
->err_shadow
,
1869 sizeof(unsigned char)))
1872 info
->err_shadow
= 0;
1875 case MOXA_SET_BAUD_METHOD
: {
1878 if (get_user(method
, (int __user
*)argp
))
1880 mxser_set_baud_method
[tty
->index
] = method
;
1881 if (copy_to_user(argp
, &method
, sizeof(int)))
1887 return -ENOIOCTLCMD
;
1892 static void mxser_stoprx(struct tty_struct
*tty
)
1894 struct mxser_port
*info
= tty
->driver_data
;
1896 info
->ldisc_stop_rx
= 1;
1898 if (info
->board
->chip_flag
) {
1899 info
->IER
&= ~MOXA_MUST_RECV_ISR
;
1900 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1902 info
->x_char
= STOP_CHAR(tty
);
1903 outb(0, info
->ioaddr
+ UART_IER
);
1904 info
->IER
|= UART_IER_THRI
;
1905 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1909 if (info
->tty
->termios
->c_cflag
& CRTSCTS
) {
1910 info
->MCR
&= ~UART_MCR_RTS
;
1911 outb(info
->MCR
, info
->ioaddr
+ UART_MCR
);
1916 * This routine is called by the upper-layer tty layer to signal that
1917 * incoming characters should be throttled.
1919 static void mxser_throttle(struct tty_struct
*tty
)
1924 static void mxser_unthrottle(struct tty_struct
*tty
)
1926 struct mxser_port
*info
= tty
->driver_data
;
1929 info
->ldisc_stop_rx
= 0;
1934 if (info
->board
->chip_flag
) {
1935 info
->IER
|= MOXA_MUST_RECV_ISR
;
1936 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1938 info
->x_char
= START_CHAR(tty
);
1939 outb(0, info
->ioaddr
+ UART_IER
);
1940 info
->IER
|= UART_IER_THRI
;
1941 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1946 if (info
->tty
->termios
->c_cflag
& CRTSCTS
) {
1947 info
->MCR
|= UART_MCR_RTS
;
1948 outb(info
->MCR
, info
->ioaddr
+ UART_MCR
);
1953 * mxser_stop() and mxser_start()
1955 * This routines are called before setting or resetting tty->stopped.
1956 * They enable or disable transmitter interrupts, as necessary.
1958 static void mxser_stop(struct tty_struct
*tty
)
1960 struct mxser_port
*info
= tty
->driver_data
;
1961 unsigned long flags
;
1963 spin_lock_irqsave(&info
->slock
, flags
);
1964 if (info
->IER
& UART_IER_THRI
) {
1965 info
->IER
&= ~UART_IER_THRI
;
1966 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1968 spin_unlock_irqrestore(&info
->slock
, flags
);
1971 static void mxser_start(struct tty_struct
*tty
)
1973 struct mxser_port
*info
= tty
->driver_data
;
1974 unsigned long flags
;
1976 spin_lock_irqsave(&info
->slock
, flags
);
1977 if (info
->xmit_cnt
&& info
->xmit_buf
) {
1978 outb(info
->IER
& ~UART_IER_THRI
, info
->ioaddr
+ UART_IER
);
1979 info
->IER
|= UART_IER_THRI
;
1980 outb(info
->IER
, info
->ioaddr
+ UART_IER
);
1982 spin_unlock_irqrestore(&info
->slock
, flags
);
1985 static void mxser_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1987 struct mxser_port
*info
= tty
->driver_data
;
1988 unsigned long flags
;
1990 spin_lock_irqsave(&info
->slock
, flags
);
1991 mxser_change_speed(info
, old_termios
);
1992 spin_unlock_irqrestore(&info
->slock
, flags
);
1994 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1995 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1996 tty
->hw_stopped
= 0;
2000 /* Handle sw stopped */
2001 if ((old_termios
->c_iflag
& IXON
) &&
2002 !(tty
->termios
->c_iflag
& IXON
)) {
2005 if (info
->board
->chip_flag
) {
2006 spin_lock_irqsave(&info
->slock
, flags
);
2007 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info
->ioaddr
);
2008 spin_unlock_irqrestore(&info
->slock
, flags
);
2016 * mxser_wait_until_sent() --- wait until the transmitter is empty
2018 static void mxser_wait_until_sent(struct tty_struct
*tty
, int timeout
)
2020 struct mxser_port
*info
= tty
->driver_data
;
2021 unsigned long orig_jiffies
, char_time
;
2024 if (info
->type
== PORT_UNKNOWN
)
2027 if (info
->xmit_fifo_size
== 0)
2028 return; /* Just in case.... */
2030 orig_jiffies
= jiffies
;
2032 * Set the check interval to be 1/5 of the estimated time to
2033 * send a single character, and make it at least 1. The check
2034 * interval should also be less than the timeout.
2036 * Note: we have to use pretty tight timings here to satisfy
2039 char_time
= (info
->timeout
- HZ
/ 50) / info
->xmit_fifo_size
;
2040 char_time
= char_time
/ 5;
2043 if (timeout
&& timeout
< char_time
)
2044 char_time
= timeout
;
2046 * If the transmitter hasn't cleared in twice the approximate
2047 * amount of time to send the entire FIFO, it probably won't
2048 * ever clear. This assumes the UART isn't doing flow
2049 * control, which is currently the case. Hence, if it ever
2050 * takes longer than info->timeout, this is probably due to a
2051 * UART bug of some kind. So, we clamp the timeout parameter at
2054 if (!timeout
|| timeout
> 2 * info
->timeout
)
2055 timeout
= 2 * info
->timeout
;
2056 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2057 printk(KERN_DEBUG
"In rs_wait_until_sent(%d) check=%lu...",
2058 timeout
, char_time
);
2059 printk("jiff=%lu...", jiffies
);
2061 while (!((lsr
= inb(info
->ioaddr
+ UART_LSR
)) & UART_LSR_TEMT
)) {
2062 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2063 printk("lsr = %d (jiff=%lu)...", lsr
, jiffies
);
2065 schedule_timeout_interruptible(char_time
);
2066 if (signal_pending(current
))
2068 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
2071 set_current_state(TASK_RUNNING
);
2073 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2074 printk("lsr = %d (jiff=%lu)...done\n", lsr
, jiffies
);
2079 * This routine is called by tty_hangup() when a hangup is signaled.
2081 static void mxser_hangup(struct tty_struct
*tty
)
2083 struct mxser_port
*info
= tty
->driver_data
;
2085 mxser_flush_buffer(tty
);
2086 mxser_shutdown(info
);
2089 info
->flags
&= ~ASYNC_NORMAL_ACTIVE
;
2091 wake_up_interruptible(&info
->open_wait
);
2095 * mxser_rs_break() --- routine which turns the break handling on or off
2097 static void mxser_rs_break(struct tty_struct
*tty
, int break_state
)
2099 struct mxser_port
*info
= tty
->driver_data
;
2100 unsigned long flags
;
2102 spin_lock_irqsave(&info
->slock
, flags
);
2103 if (break_state
== -1)
2104 outb(inb(info
->ioaddr
+ UART_LCR
) | UART_LCR_SBC
,
2105 info
->ioaddr
+ UART_LCR
);
2107 outb(inb(info
->ioaddr
+ UART_LCR
) & ~UART_LCR_SBC
,
2108 info
->ioaddr
+ UART_LCR
);
2109 spin_unlock_irqrestore(&info
->slock
, flags
);
2112 static void mxser_receive_chars(struct mxser_port
*port
, int *status
)
2114 struct tty_struct
*tty
= port
->tty
;
2115 unsigned char ch
, gdl
;
2121 recv_room
= tty
->receive_room
;
2122 if ((recv_room
== 0) && (!port
->ldisc_stop_rx
))
2125 if (port
->board
->chip_flag
!= MOXA_OTHER_UART
) {
2127 if (*status
& UART_LSR_SPECIAL
)
2129 if (port
->board
->chip_flag
== MOXA_MUST_MU860_HWID
&&
2130 (*status
& MOXA_MUST_LSR_RERR
))
2132 if (*status
& MOXA_MUST_LSR_RERR
)
2135 gdl
= inb(port
->ioaddr
+ MOXA_MUST_GDL_REGISTER
);
2137 if (port
->board
->chip_flag
== MOXA_MUST_MU150_HWID
)
2138 gdl
&= MOXA_MUST_GDL_MASK
;
2139 if (gdl
>= recv_room
) {
2140 if (!port
->ldisc_stop_rx
)
2144 ch
= inb(port
->ioaddr
+ UART_RX
);
2145 tty_insert_flip_char(tty
, ch
, 0);
2156 ch
= inb(port
->ioaddr
+ UART_RX
);
2157 if (port
->board
->chip_flag
&& (*status
& UART_LSR_OE
))
2158 outb(0x23, port
->ioaddr
+ UART_FCR
);
2159 *status
&= port
->read_status_mask
;
2160 if (*status
& port
->ignore_status_mask
) {
2161 if (++ignored
> 100)
2165 if (*status
& UART_LSR_SPECIAL
) {
2166 if (*status
& UART_LSR_BI
) {
2170 if (port
->flags
& ASYNC_SAK
)
2172 } else if (*status
& UART_LSR_PE
) {
2174 port
->icount
.parity
++;
2175 } else if (*status
& UART_LSR_FE
) {
2177 port
->icount
.frame
++;
2178 } else if (*status
& UART_LSR_OE
) {
2180 port
->icount
.overrun
++;
2184 tty_insert_flip_char(tty
, ch
, flag
);
2186 if (cnt
>= recv_room
) {
2187 if (!port
->ldisc_stop_rx
)
2194 if (port
->board
->chip_flag
)
2197 *status
= inb(port
->ioaddr
+ UART_LSR
);
2198 } while (*status
& UART_LSR_DR
);
2201 mxvar_log
.rxcnt
[port
->tty
->index
] += cnt
;
2202 port
->mon_data
.rxcnt
+= cnt
;
2203 port
->mon_data
.up_rxcnt
+= cnt
;
2206 * We are called from an interrupt context with &port->slock
2207 * being held. Drop it temporarily in order to prevent
2208 * recursive locking.
2210 spin_unlock(&port
->slock
);
2211 tty_flip_buffer_push(tty
);
2212 spin_lock(&port
->slock
);
2215 static void mxser_transmit_chars(struct mxser_port
*port
)
2220 outb(port
->x_char
, port
->ioaddr
+ UART_TX
);
2222 mxvar_log
.txcnt
[port
->tty
->index
]++;
2223 port
->mon_data
.txcnt
++;
2224 port
->mon_data
.up_txcnt
++;
2229 if (port
->xmit_buf
== 0)
2232 if ((port
->xmit_cnt
<= 0) || port
->tty
->stopped
||
2233 (port
->tty
->hw_stopped
&&
2234 (port
->type
!= PORT_16550A
) &&
2235 (!port
->board
->chip_flag
))) {
2236 port
->IER
&= ~UART_IER_THRI
;
2237 outb(port
->IER
, port
->ioaddr
+ UART_IER
);
2241 cnt
= port
->xmit_cnt
;
2242 count
= port
->xmit_fifo_size
;
2244 outb(port
->xmit_buf
[port
->xmit_tail
++],
2245 port
->ioaddr
+ UART_TX
);
2246 port
->xmit_tail
= port
->xmit_tail
& (SERIAL_XMIT_SIZE
- 1);
2247 if (--port
->xmit_cnt
<= 0)
2249 } while (--count
> 0);
2250 mxvar_log
.txcnt
[port
->tty
->index
] += (cnt
- port
->xmit_cnt
);
2252 port
->mon_data
.txcnt
+= (cnt
- port
->xmit_cnt
);
2253 port
->mon_data
.up_txcnt
+= (cnt
- port
->xmit_cnt
);
2254 port
->icount
.tx
+= (cnt
- port
->xmit_cnt
);
2256 if (port
->xmit_cnt
< WAKEUP_CHARS
)
2257 tty_wakeup(port
->tty
);
2259 if (port
->xmit_cnt
<= 0) {
2260 port
->IER
&= ~UART_IER_THRI
;
2261 outb(port
->IER
, port
->ioaddr
+ UART_IER
);
2266 * This is the serial driver's generic interrupt routine
2268 static irqreturn_t
mxser_interrupt(int irq
, void *dev_id
)
2271 struct mxser_board
*brd
= NULL
;
2272 struct mxser_port
*port
;
2273 int max
, irqbits
, bits
, msr
;
2274 unsigned int int_cnt
, pass_counter
= 0;
2275 int handled
= IRQ_NONE
;
2277 for (i
= 0; i
< MXSER_BOARDS
; i
++)
2278 if (dev_id
== &mxser_boards
[i
]) {
2283 if (i
== MXSER_BOARDS
)
2287 max
= brd
->info
->nports
;
2288 while (pass_counter
++ < MXSER_ISR_PASS_LIMIT
) {
2289 irqbits
= inb(brd
->vector
) & brd
->vector_mask
;
2290 if (irqbits
== brd
->vector_mask
)
2293 handled
= IRQ_HANDLED
;
2294 for (i
= 0, bits
= 1; i
< max
; i
++, irqbits
|= bits
, bits
<<= 1) {
2295 if (irqbits
== brd
->vector_mask
)
2299 port
= &brd
->ports
[i
];
2302 spin_lock(&port
->slock
);
2304 iir
= inb(port
->ioaddr
+ UART_IIR
);
2305 if (iir
& UART_IIR_NO_INT
)
2307 iir
&= MOXA_MUST_IIR_MASK
;
2309 (port
->flags
& ASYNC_CLOSING
) ||
2311 ASYNC_INITIALIZED
)) {
2312 status
= inb(port
->ioaddr
+ UART_LSR
);
2313 outb(0x27, port
->ioaddr
+ UART_FCR
);
2314 inb(port
->ioaddr
+ UART_MSR
);
2318 status
= inb(port
->ioaddr
+ UART_LSR
);
2320 if (status
& UART_LSR_PE
)
2321 port
->err_shadow
|= NPPI_NOTIFY_PARITY
;
2322 if (status
& UART_LSR_FE
)
2323 port
->err_shadow
|= NPPI_NOTIFY_FRAMING
;
2324 if (status
& UART_LSR_OE
)
2326 NPPI_NOTIFY_HW_OVERRUN
;
2327 if (status
& UART_LSR_BI
)
2328 port
->err_shadow
|= NPPI_NOTIFY_BREAK
;
2330 if (port
->board
->chip_flag
) {
2331 if (iir
== MOXA_MUST_IIR_GDA
||
2332 iir
== MOXA_MUST_IIR_RDA
||
2333 iir
== MOXA_MUST_IIR_RTO
||
2334 iir
== MOXA_MUST_IIR_LSR
)
2335 mxser_receive_chars(port
,
2339 status
&= port
->read_status_mask
;
2340 if (status
& UART_LSR_DR
)
2341 mxser_receive_chars(port
,
2344 msr
= inb(port
->ioaddr
+ UART_MSR
);
2345 if (msr
& UART_MSR_ANY_DELTA
)
2346 mxser_check_modem_status(port
, msr
);
2348 if (port
->board
->chip_flag
) {
2349 if (iir
== 0x02 && (status
&
2351 mxser_transmit_chars(port
);
2353 if (status
& UART_LSR_THRE
)
2354 mxser_transmit_chars(port
);
2356 } while (int_cnt
++ < MXSER_ISR_PASS_LIMIT
);
2357 spin_unlock(&port
->slock
);
2365 static const struct tty_operations mxser_ops
= {
2367 .close
= mxser_close
,
2368 .write
= mxser_write
,
2369 .put_char
= mxser_put_char
,
2370 .flush_chars
= mxser_flush_chars
,
2371 .write_room
= mxser_write_room
,
2372 .chars_in_buffer
= mxser_chars_in_buffer
,
2373 .flush_buffer
= mxser_flush_buffer
,
2374 .ioctl
= mxser_ioctl
,
2375 .throttle
= mxser_throttle
,
2376 .unthrottle
= mxser_unthrottle
,
2377 .set_termios
= mxser_set_termios
,
2379 .start
= mxser_start
,
2380 .hangup
= mxser_hangup
,
2381 .break_ctl
= mxser_rs_break
,
2382 .wait_until_sent
= mxser_wait_until_sent
,
2383 .tiocmget
= mxser_tiocmget
,
2384 .tiocmset
= mxser_tiocmset
,
2388 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2391 static void mxser_release_res(struct mxser_board
*brd
, struct pci_dev
*pdev
,
2395 free_irq(brd
->irq
, brd
);
2396 if (pdev
!= NULL
) { /* PCI */
2398 pci_release_region(pdev
, 2);
2399 pci_release_region(pdev
, 3);
2402 release_region(brd
->ports
[0].ioaddr
, 8 * brd
->info
->nports
);
2403 release_region(brd
->vector
, 1);
2407 static int __devinit
mxser_initbrd(struct mxser_board
*brd
,
2408 struct pci_dev
*pdev
)
2410 struct mxser_port
*info
;
2414 printk(KERN_INFO
"max. baud rate = %d bps.\n", brd
->ports
[0].max_baud
);
2416 for (i
= 0; i
< brd
->info
->nports
; i
++) {
2417 info
= &brd
->ports
[i
];
2420 info
->ldisc_stop_rx
= 0;
2422 /* Enhance mode enabled here */
2423 if (brd
->chip_flag
!= MOXA_OTHER_UART
)
2424 ENABLE_MOXA_MUST_ENCHANCE_MODE(info
->ioaddr
);
2426 info
->flags
= ASYNC_SHARE_IRQ
;
2427 info
->type
= brd
->uart_type
;
2429 process_txrx_fifo(info
);
2431 info
->custom_divisor
= info
->baud_base
* 16;
2432 info
->close_delay
= 5 * HZ
/ 10;
2433 info
->closing_wait
= 30 * HZ
;
2434 info
->normal_termios
= mxvar_sdriver
->init_termios
;
2435 init_waitqueue_head(&info
->open_wait
);
2436 init_waitqueue_head(&info
->delta_msr_wait
);
2438 memset(&info
->mon_data
, 0, sizeof(struct mxser_mon
));
2439 info
->err_shadow
= 0;
2440 spin_lock_init(&info
->slock
);
2442 /* before set INT ISR, disable all int */
2443 outb(inb(info
->ioaddr
+ UART_IER
) & 0xf0,
2444 info
->ioaddr
+ UART_IER
);
2447 retval
= request_irq(brd
->irq
, mxser_interrupt
, IRQF_SHARED
, "mxser",
2450 printk(KERN_ERR
"Board %s: Request irq failed, IRQ (%d) may "
2451 "conflict with another device.\n",
2452 brd
->info
->name
, brd
->irq
);
2453 /* We hold resources, we need to release them. */
2454 mxser_release_res(brd
, pdev
, 0);
2459 static int __init
mxser_get_ISA_conf(int cap
, struct mxser_board
*brd
)
2462 unsigned short regs
[16], irq
;
2463 unsigned char scratch
, scratch2
;
2465 brd
->chip_flag
= MOXA_OTHER_UART
;
2467 id
= mxser_read_register(cap
, regs
);
2470 brd
->info
= &mxser_cards
[0];
2473 brd
->info
= &mxser_cards
[1];
2475 case CI104J_ASIC_ID
:
2476 brd
->info
= &mxser_cards
[2];
2479 brd
->info
= &mxser_cards
[5];
2482 brd
->info
= &mxser_cards
[6];
2485 brd
->info
= &mxser_cards
[7];
2492 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2493 Flag-hack checks if configuration should be read as 2-port here. */
2494 if (brd
->info
->nports
== 2 || (brd
->info
->flags
& MXSER_HAS2
)) {
2495 irq
= regs
[9] & 0xF000;
2496 irq
= irq
| (irq
>> 4);
2497 if (irq
!= (regs
[9] & 0xFF00))
2498 return MXSER_ERR_IRQ_CONFLIT
;
2499 } else if (brd
->info
->nports
== 4) {
2500 irq
= regs
[9] & 0xF000;
2501 irq
= irq
| (irq
>> 4);
2502 irq
= irq
| (irq
>> 8);
2504 return MXSER_ERR_IRQ_CONFLIT
;
2505 } else if (brd
->info
->nports
== 8) {
2506 irq
= regs
[9] & 0xF000;
2507 irq
= irq
| (irq
>> 4);
2508 irq
= irq
| (irq
>> 8);
2509 if ((irq
!= regs
[9]) || (irq
!= regs
[10]))
2510 return MXSER_ERR_IRQ_CONFLIT
;
2514 return MXSER_ERR_IRQ
;
2515 brd
->irq
= ((int)(irq
& 0xF000) >> 12);
2516 for (i
= 0; i
< 8; i
++)
2517 brd
->ports
[i
].ioaddr
= (int) regs
[i
+ 1] & 0xFFF8;
2518 if ((regs
[12] & 0x80) == 0)
2519 return MXSER_ERR_VECTOR
;
2520 brd
->vector
= (int)regs
[11]; /* interrupt vector */
2522 brd
->vector_mask
= 0x00FF;
2524 brd
->vector_mask
= 0x000F;
2525 for (i
= 7, bits
= 0x0100; i
>= 0; i
--, bits
<<= 1) {
2526 if (regs
[12] & bits
) {
2527 brd
->ports
[i
].baud_base
= 921600;
2528 brd
->ports
[i
].max_baud
= 921600;
2530 brd
->ports
[i
].baud_base
= 115200;
2531 brd
->ports
[i
].max_baud
= 115200;
2534 scratch2
= inb(cap
+ UART_LCR
) & (~UART_LCR_DLAB
);
2535 outb(scratch2
| UART_LCR_DLAB
, cap
+ UART_LCR
);
2536 outb(0, cap
+ UART_EFR
); /* EFR is the same as FCR */
2537 outb(scratch2
, cap
+ UART_LCR
);
2538 outb(UART_FCR_ENABLE_FIFO
, cap
+ UART_FCR
);
2539 scratch
= inb(cap
+ UART_IIR
);
2542 brd
->uart_type
= PORT_16550A
;
2544 brd
->uart_type
= PORT_16450
;
2545 if (!request_region(brd
->ports
[0].ioaddr
, 8 * brd
->info
->nports
,
2547 return MXSER_ERR_IOADDR
;
2548 if (!request_region(brd
->vector
, 1, "mxser(vector)")) {
2549 release_region(brd
->ports
[0].ioaddr
, 8 * brd
->info
->nports
);
2550 return MXSER_ERR_VECTOR
;
2552 return brd
->info
->nports
;
2555 static int __devinit
mxser_probe(struct pci_dev
*pdev
,
2556 const struct pci_device_id
*ent
)
2559 struct mxser_board
*brd
;
2561 unsigned long ioaddress
;
2562 int retval
= -EINVAL
;
2564 for (i
= 0; i
< MXSER_BOARDS
; i
++)
2565 if (mxser_boards
[i
].info
== NULL
)
2568 if (i
>= MXSER_BOARDS
) {
2569 printk(KERN_ERR
"Too many Smartio/Industio family boards found "
2570 "(maximum %d), board not configured\n", MXSER_BOARDS
);
2574 brd
= &mxser_boards
[i
];
2575 brd
->idx
= i
* MXSER_PORTS_PER_BOARD
;
2576 printk(KERN_INFO
"Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2577 mxser_cards
[ent
->driver_data
].name
,
2578 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
));
2580 retval
= pci_enable_device(pdev
);
2582 printk(KERN_ERR
"Moxa SmartI/O PCI enable fail !\n");
2587 ioaddress
= pci_resource_start(pdev
, 2);
2588 retval
= pci_request_region(pdev
, 2, "mxser(IO)");
2592 brd
->info
= &mxser_cards
[ent
->driver_data
];
2593 for (i
= 0; i
< brd
->info
->nports
; i
++)
2594 brd
->ports
[i
].ioaddr
= ioaddress
+ 8 * i
;
2597 ioaddress
= pci_resource_start(pdev
, 3);
2598 retval
= pci_request_region(pdev
, 3, "mxser(vector)");
2601 brd
->vector
= ioaddress
;
2604 brd
->irq
= pdev
->irq
;
2606 brd
->chip_flag
= CheckIsMoxaMust(brd
->ports
[0].ioaddr
);
2607 brd
->uart_type
= PORT_16550A
;
2608 brd
->vector_mask
= 0;
2610 for (i
= 0; i
< brd
->info
->nports
; i
++) {
2611 for (j
= 0; j
< UART_INFO_NUM
; j
++) {
2612 if (Gpci_uart_info
[j
].type
== brd
->chip_flag
) {
2613 brd
->ports
[i
].max_baud
=
2614 Gpci_uart_info
[j
].max_baud
;
2616 /* exception....CP-102 */
2617 if (brd
->info
->flags
& MXSER_HIGHBAUD
)
2618 brd
->ports
[i
].max_baud
= 921600;
2624 if (brd
->chip_flag
== MOXA_MUST_MU860_HWID
) {
2625 for (i
= 0; i
< brd
->info
->nports
; i
++) {
2627 brd
->ports
[i
].opmode_ioaddr
= ioaddress
+ 4;
2629 brd
->ports
[i
].opmode_ioaddr
= ioaddress
+ 0x0c;
2631 outb(0, ioaddress
+ 4); /* default set to RS232 mode */
2632 outb(0, ioaddress
+ 0x0c); /* default set to RS232 mode */
2635 for (i
= 0; i
< brd
->info
->nports
; i
++) {
2636 brd
->vector_mask
|= (1 << i
);
2637 brd
->ports
[i
].baud_base
= 921600;
2640 /* mxser_initbrd will hook ISR. */
2641 retval
= mxser_initbrd(brd
, pdev
);
2645 for (i
= 0; i
< brd
->info
->nports
; i
++)
2646 tty_register_device(mxvar_sdriver
, brd
->idx
+ i
, &pdev
->dev
);
2648 pci_set_drvdata(pdev
, brd
);
2652 pci_release_region(pdev
, 2);
2662 static void __devexit
mxser_remove(struct pci_dev
*pdev
)
2664 struct mxser_board
*brd
= pci_get_drvdata(pdev
);
2667 for (i
= 0; i
< brd
->info
->nports
; i
++)
2668 tty_unregister_device(mxvar_sdriver
, brd
->idx
+ i
);
2670 mxser_release_res(brd
, pdev
, 1);
2674 static struct pci_driver mxser_driver
= {
2676 .id_table
= mxser_pcibrds
,
2677 .probe
= mxser_probe
,
2678 .remove
= __devexit_p(mxser_remove
)
2681 static int __init
mxser_module_init(void)
2683 struct mxser_board
*brd
;
2685 unsigned int i
, m
, isaloop
;
2688 pr_debug("Loading module mxser ...\n");
2690 mxvar_sdriver
= alloc_tty_driver(MXSER_PORTS
+ 1);
2694 printk(KERN_INFO
"MOXA Smartio/Industio family driver version %s\n",
2697 /* Initialize the tty_driver structure */
2698 mxvar_sdriver
->owner
= THIS_MODULE
;
2699 mxvar_sdriver
->magic
= TTY_DRIVER_MAGIC
;
2700 mxvar_sdriver
->name
= "ttyMI";
2701 mxvar_sdriver
->major
= ttymajor
;
2702 mxvar_sdriver
->minor_start
= 0;
2703 mxvar_sdriver
->num
= MXSER_PORTS
+ 1;
2704 mxvar_sdriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
2705 mxvar_sdriver
->subtype
= SERIAL_TYPE_NORMAL
;
2706 mxvar_sdriver
->init_termios
= tty_std_termios
;
2707 mxvar_sdriver
->init_termios
.c_cflag
= B9600
|CS8
|CREAD
|HUPCL
|CLOCAL
;
2708 mxvar_sdriver
->flags
= TTY_DRIVER_REAL_RAW
|TTY_DRIVER_DYNAMIC_DEV
;
2709 tty_set_operations(mxvar_sdriver
, &mxser_ops
);
2711 retval
= tty_register_driver(mxvar_sdriver
);
2713 printk(KERN_ERR
"Couldn't install MOXA Smartio/Industio family "
2721 /* Start finding ISA boards here */
2722 for (isaloop
= 0; isaloop
< 2; isaloop
++)
2723 for (b
= 0; b
< MXSER_BOARDS
&& m
< MXSER_BOARDS
; b
++) {
2725 cap
= mxserBoardCAP
[b
]; /* predefined */
2727 cap
= ioaddr
[b
]; /* module param */
2732 brd
= &mxser_boards
[m
];
2733 retval
= mxser_get_ISA_conf(cap
, brd
);
2736 printk(KERN_INFO
"Found MOXA %s board "
2738 brd
->info
->name
, ioaddr
[b
]);
2741 if (retval
== MXSER_ERR_IRQ
)
2742 printk(KERN_ERR
"Invalid interrupt "
2743 "number, board not "
2745 else if (retval
== MXSER_ERR_IRQ_CONFLIT
)
2746 printk(KERN_ERR
"Invalid interrupt "
2747 "number, board not "
2749 else if (retval
== MXSER_ERR_VECTOR
)
2750 printk(KERN_ERR
"Invalid interrupt "
2751 "vector, board not "
2753 else if (retval
== MXSER_ERR_IOADDR
)
2754 printk(KERN_ERR
"Invalid I/O address, "
2755 "board not configured\n");
2761 /* mxser_initbrd will hook ISR. */
2762 if (mxser_initbrd(brd
, NULL
) < 0) {
2767 brd
->idx
= m
* MXSER_PORTS_PER_BOARD
;
2768 for (i
= 0; i
< brd
->info
->nports
; i
++)
2769 tty_register_device(mxvar_sdriver
, brd
->idx
+ i
,
2775 retval
= pci_register_driver(&mxser_driver
);
2777 printk(KERN_ERR
"Can't register pci driver\n");
2781 } /* else: we have some ISA cards under control */
2784 pr_debug("Done.\n");
2788 tty_unregister_driver(mxvar_sdriver
);
2790 put_tty_driver(mxvar_sdriver
);
2794 static void __exit
mxser_module_exit(void)
2798 pr_debug("Unloading module mxser ...\n");
2800 pci_unregister_driver(&mxser_driver
);
2802 for (i
= 0; i
< MXSER_BOARDS
; i
++) /* ISA remains */
2803 if (mxser_boards
[i
].info
!= NULL
)
2804 for (j
= 0; j
< mxser_boards
[i
].info
->nports
; j
++)
2805 tty_unregister_device(mxvar_sdriver
,
2806 mxser_boards
[i
].idx
+ j
);
2807 tty_unregister_driver(mxvar_sdriver
);
2808 put_tty_driver(mxvar_sdriver
);
2810 for (i
= 0; i
< MXSER_BOARDS
; i
++)
2811 if (mxser_boards
[i
].info
!= NULL
)
2812 mxser_release_res(&mxser_boards
[i
], NULL
, 1);
2814 pr_debug("Done.\n");
2817 module_init(mxser_module_init
);
2818 module_exit(mxser_module_exit
);