mxser: remove tty->driver_data NULL check
[linux-2.6-block.git] / drivers / tty / mxser.c
CommitLineData
e3b3d0f5 1// SPDX-License-Identifier: GPL-2.0+
1da177e4
LT
2/*
3 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 *
80ff8a80
JS
5 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
6 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
1da177e4 7 *
1c45607a
JS
8 * This code is loosely based on the 1.8 moxa driver which is based on
9 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
10 * others.
1da177e4 11 *
1da177e4 12 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
8eb04cf3
AC
13 * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
14 * www.moxa.com.
1da177e4 15 * - Fixed x86_64 cleanness
1da177e4
LT
16 */
17
1da177e4 18#include <linux/module.h>
1da177e4
LT
19#include <linux/errno.h>
20#include <linux/signal.h>
21#include <linux/sched.h>
22#include <linux/timer.h>
23#include <linux/interrupt.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/serial.h>
27#include <linux/serial_reg.h>
28#include <linux/major.h>
29#include <linux/string.h>
30#include <linux/fcntl.h>
31#include <linux/ptrace.h>
1da177e4
LT
32#include <linux/ioport.h>
33#include <linux/mm.h>
1da177e4
LT
34#include <linux/delay.h>
35#include <linux/pci.h>
1977f032 36#include <linux/bitops.h>
5a0e3ad6 37#include <linux/slab.h>
5a3c6b25 38#include <linux/ratelimit.h>
1da177e4 39
1da177e4
LT
40#include <asm/io.h>
41#include <asm/irq.h>
7c0f6ba6 42#include <linux/uaccess.h>
1da177e4 43
4463cc5b
JS
44/*
45 * Semi-public control interfaces
46 */
47
48/*
49 * MOXA ioctls
50 */
51
52#define MOXA 0x400
4463cc5b
JS
53#define MOXA_SET_OP_MODE (MOXA + 66)
54#define MOXA_GET_OP_MODE (MOXA + 67)
55
56#define RS232_MODE 0
57#define RS485_2WIRE_MODE 1
58#define RS422_MODE 2
59#define RS485_4WIRE_MODE 3
60#define OP_MODE_MASK 3
61
4463cc5b
JS
62/* --------------------------------------------------- */
63
4463cc5b
JS
64/*
65 * Follow just what Moxa Must chip defines.
66 *
464fbf6c
JS
67 * When LCR register (offset 0x03) is written the following value, the Must chip
68 * will enter enhanced mode. And a write to EFR (offset 0x02) bit 6,7 will
4463cc5b
JS
69 * change bank.
70 */
464fbf6c 71#define MOXA_MUST_ENTER_ENHANCED 0xBF
4463cc5b 72
464fbf6c 73/* when enhanced mode is enabled, access to general bank register */
4463cc5b
JS
74#define MOXA_MUST_GDL_REGISTER 0x07
75#define MOXA_MUST_GDL_MASK 0x7F
76#define MOXA_MUST_GDL_HAS_BAD_DATA 0x80
77
78#define MOXA_MUST_LSR_RERR 0x80 /* error in receive FIFO */
464fbf6c
JS
79/* enhanced register bank select and enhanced mode setting register */
80/* This works only when LCR register equals to 0xBF */
4463cc5b 81#define MOXA_MUST_EFR_REGISTER 0x02
464fbf6c
JS
82#define MOXA_MUST_EFR_EFRB_ENABLE 0x10 /* enhanced mode enable */
83/* enhanced register bank set 0, 1, 2 */
4463cc5b
JS
84#define MOXA_MUST_EFR_BANK0 0x00
85#define MOXA_MUST_EFR_BANK1 0x40
86#define MOXA_MUST_EFR_BANK2 0x80
87#define MOXA_MUST_EFR_BANK3 0xC0
88#define MOXA_MUST_EFR_BANK_MASK 0xC0
89
90/* set XON1 value register, when LCR=0xBF and change to bank0 */
91#define MOXA_MUST_XON1_REGISTER 0x04
92
93/* set XON2 value register, when LCR=0xBF and change to bank0 */
94#define MOXA_MUST_XON2_REGISTER 0x05
95
96/* set XOFF1 value register, when LCR=0xBF and change to bank0 */
97#define MOXA_MUST_XOFF1_REGISTER 0x06
98
99/* set XOFF2 value register, when LCR=0xBF and change to bank0 */
100#define MOXA_MUST_XOFF2_REGISTER 0x07
101
102#define MOXA_MUST_RBRTL_REGISTER 0x04
103#define MOXA_MUST_RBRTH_REGISTER 0x05
104#define MOXA_MUST_RBRTI_REGISTER 0x06
105#define MOXA_MUST_THRTL_REGISTER 0x07
106#define MOXA_MUST_ENUM_REGISTER 0x04
107#define MOXA_MUST_HWID_REGISTER 0x05
108#define MOXA_MUST_ECR_REGISTER 0x06
109#define MOXA_MUST_CSR_REGISTER 0x07
110
111#define MOXA_MUST_FCR_GDA_MODE_ENABLE 0x20 /* good data mode enable */
112#define MOXA_MUST_FCR_GDA_ONLY_ENABLE 0x10 /* only good data put into RxFIFO */
113
114#define MOXA_MUST_IER_ECTSI 0x80 /* enable CTS interrupt */
115#define MOXA_MUST_IER_ERTSI 0x40 /* enable RTS interrupt */
116#define MOXA_MUST_IER_XINT 0x20 /* enable Xon/Xoff interrupt */
117#define MOXA_MUST_IER_EGDAI 0x10 /* enable GDA interrupt */
118
119#define MOXA_MUST_RECV_ISR (UART_IER_RDI | MOXA_MUST_IER_EGDAI)
120
121/* GDA interrupt pending */
122#define MOXA_MUST_IIR_GDA 0x1C
123#define MOXA_MUST_IIR_RDA 0x04
124#define MOXA_MUST_IIR_RTO 0x0C
125#define MOXA_MUST_IIR_LSR 0x06
126
127/* received Xon/Xoff or specical interrupt pending */
128#define MOXA_MUST_IIR_XSC 0x10
129
130/* RTS/CTS change state interrupt pending */
131#define MOXA_MUST_IIR_RTSCTS 0x20
132#define MOXA_MUST_IIR_MASK 0x3E
133
134#define MOXA_MUST_MCR_XON_FLAG 0x40
135#define MOXA_MUST_MCR_XON_ANY 0x80
136#define MOXA_MUST_MCR_TX_XON 0x08
137
138#define MOXA_MUST_EFR_SF_MASK 0x0F /* software flow control on chip mask value */
139#define MOXA_MUST_EFR_SF_TX1 0x08 /* send Xon1/Xoff1 */
140#define MOXA_MUST_EFR_SF_TX2 0x04 /* send Xon2/Xoff2 */
141#define MOXA_MUST_EFR_SF_TX12 0x0C /* send Xon1,Xon2/Xoff1,Xoff2 */
142#define MOXA_MUST_EFR_SF_TX_NO 0x00 /* don't send Xon/Xoff */
143#define MOXA_MUST_EFR_SF_TX_MASK 0x0C /* Tx software flow control mask */
144#define MOXA_MUST_EFR_SF_RX_NO 0x00 /* don't receive Xon/Xoff */
145#define MOXA_MUST_EFR_SF_RX1 0x02 /* receive Xon1/Xoff1 */
146#define MOXA_MUST_EFR_SF_RX2 0x01 /* receive Xon2/Xoff2 */
147#define MOXA_MUST_EFR_SF_RX12 0x03 /* receive Xon1,Xon2/Xoff1,Xoff2 */
148#define MOXA_MUST_EFR_SF_RX_MASK 0x03 /* Rx software flow control mask */
1da177e4 149
1da177e4 150#define MXSERMAJOR 174
1da177e4 151
1da177e4 152#define MXSER_BOARDS 4 /* Max. boards */
1da177e4 153#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
1c45607a
JS
154#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
155#define MXSER_ISR_PASS_LIMIT 100
1da177e4 156
1da177e4
LT
157#define WAKEUP_CHARS 256
158
a6970c39 159#define MXSER_BAUD_BASE 921600
d811b26b 160#define MXSER_CUSTOM_DIVISOR (MXSER_BAUD_BASE * 16)
a6970c39 161
e129deff 162#define PCI_DEVICE_ID_POS104UL 0x1044
1c45607a 163#define PCI_DEVICE_ID_CB108 0x1080
e129deff 164#define PCI_DEVICE_ID_CP102UF 0x1023
502f295f 165#define PCI_DEVICE_ID_CP112UL 0x1120
1c45607a 166#define PCI_DEVICE_ID_CB114 0x1142
80ff8a80 167#define PCI_DEVICE_ID_CP114UL 0x1143
1c45607a
JS
168#define PCI_DEVICE_ID_CB134I 0x1341
169#define PCI_DEVICE_ID_CP138U 0x1380
1da177e4 170
c24c31ff
JS
171#define MXSER_NPORTS(ddata) ((ddata) & 0xffU)
172#define MXSER_HIGHBAUD 0x0100
1da177e4 173
e4558366
JS
174enum mxser_must_hwid {
175 MOXA_OTHER_UART = 0x00,
176 MOXA_MUST_MU150_HWID = 0x01,
177 MOXA_MUST_MU860_HWID = 0x02,
178};
179
1c45607a 180static const struct {
dc33f644
JS
181 u8 type;
182 u8 fifo_size;
183 u8 rx_high_water;
184 u8 rx_low_water;
185 speed_t max_baud;
1c45607a 186} Gpci_uart_info[] = {
dc33f644
JS
187 { MOXA_OTHER_UART, 16, 14, 1, 921600 },
188 { MOXA_MUST_MU150_HWID, 64, 48, 16, 230400 },
189 { MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 }
1da177e4 190};
1c45607a 191#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
1da177e4 192
1da177e4 193
1c45607a
JS
194/* driver_data correspond to the lines in the structure above
195 see also ISA probe function before you change something */
3385ecf8 196static const struct pci_device_id mxser_pcibrds[] = {
c24c31ff
JS
197 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 8 },
198 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
15254902 199 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 2 },
c24c31ff 200 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 4 },
15254902 201 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 4 },
c24c31ff
JS
202 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 2 | MXSER_HIGHBAUD },
203 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 4 },
204 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 8 },
205 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 2 },
206 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 4 },
207 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 4 },
208 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 8 }, /* RC7000 */
209 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 8 },
210 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 2 },
211 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 2 },
212 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 8 },
213 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 8 },
214 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 4 },
215 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 8 },
216 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 4 },
217 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 4 },
218 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 8 },
219 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 4 },
220 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 4 },
221 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 2 },
222 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 2 },
1c45607a 223 { }
1da177e4 224};
1da177e4
LT
225MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
226
1da177e4 227static int ttymajor = MXSERMAJOR;
1da177e4
LT
228
229/* Variables for insmod */
230
231MODULE_AUTHOR("Casper Yang");
232MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
8d3b33f6 233module_param(ttymajor, int, 0);
1da177e4
LT
234MODULE_LICENSE("GPL");
235
1c45607a
JS
236struct mxser_board;
237
238struct mxser_port {
0ad9e7d1 239 struct tty_port port;
1c45607a 240 struct mxser_board *board;
1c45607a
JS
241
242 unsigned long ioaddr;
243 unsigned long opmode_ioaddr;
1da177e4 244
dc33f644
JS
245 u8 rx_high_water;
246 u8 rx_low_water;
1da177e4 247 int type; /* UART type */
1c45607a 248
a93963e4
JS
249 unsigned char x_char; /* xon/xoff character */
250 u8 IER; /* Interrupt Enable Register */
251 u8 MCR; /* Modem control register */
d249e662 252 u8 FCR; /* FIFO control register */
1c45607a 253
19236287 254 bool ldisc_stop_rx;
1c45607a 255
1c45607a 256 struct async_icount icount; /* kernel counters for 4 input interrupts */
104583b5 257 unsigned int timeout;
1c45607a 258
a93963e4
JS
259 u8 read_status_mask;
260 u8 ignore_status_mask;
dc33f644 261 u8 xmit_fifo_size;
02e43144
JS
262 unsigned int xmit_head;
263 unsigned int xmit_tail;
264 unsigned int xmit_cnt;
1c45607a 265
1da177e4 266 spinlock_t slock;
1c45607a
JS
267};
268
269struct mxser_board {
270 unsigned int idx;
c24c31ff 271 unsigned short nports;
1c45607a 272 int irq;
1c45607a 273 unsigned long vector;
1c45607a 274
e4558366 275 enum mxser_must_hwid must_hwid;
928f9464 276 speed_t max_baud;
1c45607a 277
ad1c92ff 278 struct mxser_port ports[];
1da177e4
LT
279};
280
f8b6b327 281static DECLARE_BITMAP(mxser_boards, MXSER_BOARDS);
1da177e4 282static struct tty_driver *mxvar_sdriver;
1da177e4 283
edb7d27c
JS
284static u8 __mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set,
285 bool restore_LCR)
148ff86b 286{
edb7d27c 287 u8 oldlcr, efr;
148ff86b
CH
288
289 oldlcr = inb(baseio + UART_LCR);
464fbf6c 290 outb(MOXA_MUST_ENTER_ENHANCED, baseio + UART_LCR);
148ff86b
CH
291
292 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
edb7d27c
JS
293 efr &= ~clear;
294 efr |= set;
148ff86b
CH
295
296 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
148ff86b 297
edb7d27c
JS
298 if (restore_LCR)
299 outb(oldlcr, baseio + UART_LCR);
148ff86b 300
edb7d27c 301 return oldlcr;
148ff86b
CH
302}
303
b286484b 304static u8 mxser_must_select_bank(unsigned long baseio, u8 bank)
148ff86b 305{
b286484b
JS
306 return __mxser_must_set_EFR(baseio, MOXA_MUST_EFR_BANK_MASK, bank,
307 false);
308}
148ff86b 309
b286484b
JS
310static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
311{
312 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
148ff86b
CH
313 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
314 outb(oldlcr, baseio + UART_LCR);
315}
316
317static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
318{
b286484b 319 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
148ff86b
CH
320 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
321 outb(oldlcr, baseio + UART_LCR);
322}
323
324static void mxser_set_must_fifo_value(struct mxser_port *info)
325{
b286484b 326 u8 oldlcr = mxser_must_select_bank(info->ioaddr, MOXA_MUST_EFR_BANK1);
dc33f644
JS
327 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
328 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
329 outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
148ff86b
CH
330 outb(oldlcr, info->ioaddr + UART_LCR);
331}
332
333static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
334{
b286484b 335 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
148ff86b
CH
336 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
337 outb(oldlcr, baseio + UART_LCR);
338}
339
b286484b 340static u8 mxser_get_must_hardware_id(unsigned long baseio)
148ff86b 341{
b286484b
JS
342 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
343 u8 id = inb(baseio + MOXA_MUST_HWID_REGISTER);
148ff86b 344 outb(oldlcr, baseio + UART_LCR);
b286484b
JS
345
346 return id;
148ff86b
CH
347}
348
edb7d27c
JS
349static void mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set)
350{
351 __mxser_must_set_EFR(baseio, clear, set, true);
352}
353
354static void mxser_must_set_enhance_mode(unsigned long baseio, bool enable)
355{
356 mxser_must_set_EFR(baseio,
357 enable ? 0 : MOXA_MUST_EFR_EFRB_ENABLE,
358 enable ? MOXA_MUST_EFR_EFRB_ENABLE : 0);
359}
360
b441eb0f 361static void mxser_must_no_sw_flow_control(unsigned long baseio)
148ff86b 362{
b441eb0f 363 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_MASK, 0);
148ff86b
CH
364}
365
b441eb0f 366static void mxser_must_set_tx_sw_flow_control(unsigned long baseio, bool enable)
148ff86b 367{
b441eb0f
JS
368 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_TX_MASK,
369 enable ? MOXA_MUST_EFR_SF_TX1 : 0);
148ff86b
CH
370}
371
b441eb0f 372static void mxser_must_set_rx_sw_flow_control(unsigned long baseio, bool enable)
148ff86b 373{
b441eb0f
JS
374 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_RX_MASK,
375 enable ? MOXA_MUST_EFR_SF_RX1 : 0);
148ff86b
CH
376}
377
e4558366 378static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
1da177e4
LT
379{
380 u8 oldmcr, hwid;
381 int i;
382
383 outb(0, io + UART_LCR);
edb7d27c 384 mxser_must_set_enhance_mode(io, false);
1da177e4
LT
385 oldmcr = inb(io + UART_MCR);
386 outb(0, io + UART_MCR);
148ff86b 387 mxser_set_must_xon1_value(io, 0x11);
1da177e4
LT
388 if ((hwid = inb(io + UART_MCR)) != 0) {
389 outb(oldmcr, io + UART_MCR);
8ea2c2ec 390 return MOXA_OTHER_UART;
1da177e4
LT
391 }
392
b286484b 393 hwid = mxser_get_must_hardware_id(io);
e4558366 394 for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
1c45607a 395 if (hwid == Gpci_uart_info[i].type)
e4558366
JS
396 return hwid;
397
1da177e4
LT
398 return MOXA_OTHER_UART;
399}
400
5d1ea1ad
JS
401static bool mxser_16550A_or_MUST(struct mxser_port *info)
402{
403 return info->type == PORT_16550A || info->board->must_hwid;
404}
405
c3db20c3 406static void mxser_process_txrx_fifo(struct mxser_port *info)
1da177e4 407{
c3db20c3 408 unsigned int i;
1da177e4 409
c3db20c3 410 if (info->type == PORT_16450 || info->type == PORT_8250) {
1da177e4
LT
411 info->rx_high_water = 1;
412 info->rx_low_water = 1;
413 info->xmit_fifo_size = 1;
c3db20c3
JS
414 return;
415 }
416
417 for (i = 0; i < UART_INFO_NUM; i++)
418 if (info->board->must_hwid == Gpci_uart_info[i].type) {
419 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
420 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
421 info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
422 break;
423 }
1da177e4
LT
424}
425
740165f7
JS
426static void __mxser_start_tx(struct mxser_port *info)
427{
428 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
429 info->IER |= UART_IER_THRI;
430 outb(info->IER, info->ioaddr + UART_IER);
431}
432
433static void mxser_start_tx(struct mxser_port *info)
434{
435 unsigned long flags;
436
437 spin_lock_irqsave(&info->slock, flags);
438 __mxser_start_tx(info);
439 spin_unlock_irqrestore(&info->slock, flags);
440}
441
442static void __mxser_stop_tx(struct mxser_port *info)
443{
444 info->IER &= ~UART_IER_THRI;
445 outb(info->IER, info->ioaddr + UART_IER);
446}
447
31f35939
AC
448static int mxser_carrier_raised(struct tty_port *port)
449{
450 struct mxser_port *mp = container_of(port, struct mxser_port, port);
451 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
452}
453
fcc8ac18 454static void mxser_dtr_rts(struct tty_port *port, int on)
5d951fb4
AC
455{
456 struct mxser_port *mp = container_of(port, struct mxser_port, port);
457 unsigned long flags;
007bbdc8 458 u8 mcr;
5d951fb4
AC
459
460 spin_lock_irqsave(&mp->slock, flags);
007bbdc8 461 mcr = inb(mp->ioaddr + UART_MCR);
fcc8ac18 462 if (on)
007bbdc8 463 mcr |= UART_MCR_DTR | UART_MCR_RTS;
fcc8ac18 464 else
007bbdc8
JS
465 mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
466 outb(mcr, mp->ioaddr + UART_MCR);
5d951fb4
AC
467 spin_unlock_irqrestore(&mp->slock, flags);
468}
469
dc33f644 470static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
1da177e4 471{
216ba023 472 struct mxser_port *info = tty->driver_data;
104583b5 473 unsigned int quot = 0, baud;
1c45607a 474 unsigned char cval;
104583b5 475 u64 timeout;
1da177e4 476
928f9464 477 if (newspd > info->board->max_baud)
1c45607a 478 return -1;
1da177e4 479
1c45607a 480 if (newspd == 134) {
a6970c39 481 quot = 2 * MXSER_BAUD_BASE / 269;
216ba023 482 tty_encode_baud_rate(tty, 134, 134);
1c45607a 483 } else if (newspd) {
a6970c39 484 quot = MXSER_BAUD_BASE / newspd;
1c45607a
JS
485 if (quot == 0)
486 quot = 1;
a6970c39 487 baud = MXSER_BAUD_BASE / quot;
216ba023 488 tty_encode_baud_rate(tty, baud, baud);
1c45607a
JS
489 } else {
490 quot = 0;
491 }
1da177e4 492
104583b5
JS
493 /*
494 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
495 * u64 domain
496 */
497 timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
a6970c39 498 do_div(timeout, MXSER_BAUD_BASE);
104583b5 499 info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
1da177e4 500
1c45607a
JS
501 if (quot) {
502 info->MCR |= UART_MCR_DTR;
503 outb(info->MCR, info->ioaddr + UART_MCR);
504 } else {
505 info->MCR &= ~UART_MCR_DTR;
506 outb(info->MCR, info->ioaddr + UART_MCR);
507 return 0;
508 }
1da177e4 509
1c45607a 510 cval = inb(info->ioaddr + UART_LCR);
1da177e4 511
1c45607a 512 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
1da177e4 513
1c45607a
JS
514 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
515 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
516 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
1da177e4 517
1c45607a 518#ifdef BOTHER
216ba023 519 if (C_BAUD(tty) == BOTHER) {
a6970c39 520 quot = MXSER_BAUD_BASE % newspd;
1c45607a
JS
521 quot *= 8;
522 if (quot % newspd > newspd / 2) {
523 quot /= newspd;
524 quot++;
525 } else
526 quot /= newspd;
527
148ff86b 528 mxser_set_must_enum_value(info->ioaddr, quot);
1c45607a
JS
529 } else
530#endif
148ff86b 531 mxser_set_must_enum_value(info->ioaddr, 0);
1da177e4 532
8ea2c2ec 533 return 0;
1da177e4 534}
1da177e4 535
be486667
JS
536static void mxser_handle_cts(struct tty_struct *tty, struct mxser_port *info,
537 u8 msr)
538{
539 bool cts = msr & UART_MSR_CTS;
540
541 if (tty->hw_stopped) {
542 if (cts) {
543 tty->hw_stopped = 0;
544
5d1ea1ad 545 if (!mxser_16550A_or_MUST(info))
740165f7 546 __mxser_start_tx(info);
be486667
JS
547 tty_wakeup(tty);
548 }
549 return;
550 } else if (cts)
551 return;
552
553 tty->hw_stopped = 1;
5d1ea1ad 554 if (!mxser_16550A_or_MUST(info))
740165f7 555 __mxser_stop_tx(info);
be486667
JS
556}
557
1c45607a
JS
558/*
559 * This routine is called to set the UART divisor registers to match
560 * the specified baud rate for a serial port.
561 */
3fdfa165 562static void mxser_change_speed(struct tty_struct *tty, struct ktermios *old_termios)
1da177e4 563{
216ba023 564 struct mxser_port *info = tty->driver_data;
d249e662 565 unsigned cflag, cval;
1da177e4 566
adc8d746 567 cflag = tty->termios.c_cflag;
1da177e4 568
3fdfa165
JS
569 if (mxser_set_baud(tty, tty_get_baud_rate(tty))) {
570 /* Use previous rate on a failure */
571 if (old_termios) {
572 speed_t baud = tty_termios_baud_rate(old_termios);
573 tty_encode_baud_rate(tty, baud, baud);
574 }
575 }
1da177e4 576
1c45607a
JS
577 /* byte size and parity */
578 switch (cflag & CSIZE) {
2c21832b 579 default:
1c45607a 580 case CS5:
2c21832b 581 cval = UART_LCR_WLEN5;
1c45607a
JS
582 break;
583 case CS6:
2c21832b 584 cval = UART_LCR_WLEN6;
1c45607a
JS
585 break;
586 case CS7:
2c21832b 587 cval = UART_LCR_WLEN7;
1c45607a
JS
588 break;
589 case CS8:
2c21832b 590 cval = UART_LCR_WLEN8;
1c45607a 591 break;
1c45607a 592 }
2c21832b 593
1c45607a 594 if (cflag & CSTOPB)
2c21832b 595 cval |= UART_LCR_STOP;
1c45607a
JS
596 if (cflag & PARENB)
597 cval |= UART_LCR_PARITY;
598 if (!(cflag & PARODD))
599 cval |= UART_LCR_EPAR;
600 if (cflag & CMSPAR)
601 cval |= UART_LCR_SPAR;
1da177e4 602
d249e662 603 info->FCR = 0;
bf1434c1 604 if (info->board->must_hwid) {
d249e662 605 info->FCR |= UART_FCR_ENABLE_FIFO |
bf1434c1
JS
606 MOXA_MUST_FCR_GDA_MODE_ENABLE;
607 mxser_set_must_fifo_value(info);
608 } else if (info->type != PORT_8250 && info->type != PORT_16450) {
d249e662 609 info->FCR |= UART_FCR_ENABLE_FIFO;
bf1434c1
JS
610 switch (info->rx_high_water) {
611 case 1:
d249e662 612 info->FCR |= UART_FCR_TRIGGER_1;
bf1434c1
JS
613 break;
614 case 4:
d249e662 615 info->FCR |= UART_FCR_TRIGGER_4;
bf1434c1
JS
616 break;
617 case 8:
d249e662 618 info->FCR |= UART_FCR_TRIGGER_8;
bf1434c1
JS
619 break;
620 default:
d249e662 621 info->FCR |= UART_FCR_TRIGGER_14;
bf1434c1 622 break;
1da177e4 623 }
1da177e4
LT
624 }
625
1c45607a
JS
626 /* CTS flow control flag and modem status interrupts */
627 info->IER &= ~UART_IER_MSI;
628 info->MCR &= ~UART_MCR_AFE;
5604a98e 629 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
1c45607a 630 if (cflag & CRTSCTS) {
1c45607a 631 info->IER |= UART_IER_MSI;
5d1ea1ad 632 if (mxser_16550A_or_MUST(info)) {
1c45607a
JS
633 info->MCR |= UART_MCR_AFE;
634 } else {
be486667
JS
635 mxser_handle_cts(tty, info,
636 inb(info->ioaddr + UART_MSR));
1da177e4 637 }
1c45607a
JS
638 }
639 outb(info->MCR, info->ioaddr + UART_MCR);
2d68655d
PH
640 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
641 if (~cflag & CLOCAL)
1c45607a 642 info->IER |= UART_IER_MSI;
1c45607a
JS
643 outb(info->IER, info->ioaddr + UART_IER);
644
645 /*
646 * Set up parity check flag
647 */
648 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
216ba023 649 if (I_INPCK(tty))
1c45607a 650 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
216ba023 651 if (I_BRKINT(tty) || I_PARMRK(tty))
1c45607a 652 info->read_status_mask |= UART_LSR_BI;
1da177e4 653
1c45607a 654 info->ignore_status_mask = 0;
1da177e4 655
216ba023 656 if (I_IGNBRK(tty)) {
1c45607a
JS
657 info->ignore_status_mask |= UART_LSR_BI;
658 info->read_status_mask |= UART_LSR_BI;
8ea2c2ec 659 /*
1c45607a
JS
660 * If we're ignore parity and break indicators, ignore
661 * overruns too. (For real raw support).
8ea2c2ec 662 */
216ba023 663 if (I_IGNPAR(tty)) {
1c45607a
JS
664 info->ignore_status_mask |=
665 UART_LSR_OE |
666 UART_LSR_PE |
667 UART_LSR_FE;
668 info->read_status_mask |=
669 UART_LSR_OE |
670 UART_LSR_PE |
671 UART_LSR_FE;
672 }
1da177e4 673 }
292955a7 674 if (info->board->must_hwid) {
216ba023
AC
675 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
676 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
b441eb0f
JS
677 mxser_must_set_rx_sw_flow_control(info->ioaddr, I_IXON(tty));
678 mxser_must_set_tx_sw_flow_control(info->ioaddr, I_IXOFF(tty));
1da177e4 679 }
1da177e4 680
1da177e4 681
d249e662 682 outb(info->FCR, info->ioaddr + UART_FCR);
1c45607a 683 outb(cval, info->ioaddr + UART_LCR);
1da177e4
LT
684}
685
30f6027f
JS
686static u8 mxser_check_modem_status(struct tty_struct *tty,
687 struct mxser_port *port)
1da177e4 688{
30f6027f
JS
689 u8 msr = inb(port->ioaddr + UART_MSR);
690
691 if (!(msr & UART_MSR_ANY_DELTA))
692 return msr;
693
1c45607a 694 /* update input line counters */
30f6027f 695 if (msr & UART_MSR_TERI)
1c45607a 696 port->icount.rng++;
30f6027f 697 if (msr & UART_MSR_DDSR)
1c45607a 698 port->icount.dsr++;
30f6027f 699 if (msr & UART_MSR_DDCD)
1c45607a 700 port->icount.dcd++;
30f6027f 701 if (msr & UART_MSR_DCTS)
1c45607a 702 port->icount.cts++;
bdc04e31 703 wake_up_interruptible(&port->port.delta_msr_wait);
1da177e4 704
30f6027f
JS
705 if (tty_port_check_carrier(&port->port) && (msr & UART_MSR_DDCD)) {
706 if (msr & UART_MSR_DCD)
0ad9e7d1 707 wake_up_interruptible(&port->port.open_wait);
1c45607a 708 }
1da177e4 709
be486667 710 if (tty_port_cts_enabled(&port->port))
30f6027f
JS
711 mxser_handle_cts(tty, port, msr);
712
713 return msr;
1da177e4
LT
714}
715
ee7e5e66
JS
716static void mxser_disable_and_clear_FIFO(struct mxser_port *info)
717{
718 u8 fcr = UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
719
720 if (info->board->must_hwid)
721 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
722
723 outb(fcr, info->ioaddr + UART_FCR);
724}
725
6769140d 726static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
1da177e4 727{
6769140d 728 struct mxser_port *info = container_of(port, struct mxser_port, port);
1c45607a
JS
729 unsigned long page;
730 unsigned long flags;
1da177e4 731
1c45607a
JS
732 page = __get_free_page(GFP_KERNEL);
733 if (!page)
734 return -ENOMEM;
1da177e4 735
1c45607a 736 spin_lock_irqsave(&info->slock, flags);
1da177e4 737
987a4cfe 738 if (!info->type) {
216ba023 739 set_bit(TTY_IO_ERROR, &tty->flags);
1c45607a
JS
740 free_page(page);
741 spin_unlock_irqrestore(&info->slock, flags);
1da177e4 742 return 0;
1c45607a 743 }
6769140d 744 info->port.xmit_buf = (unsigned char *) page;
1da177e4 745
1da177e4 746 /*
1c45607a
JS
747 * Clear the FIFO buffers and disable them
748 * (they will be reenabled in mxser_change_speed())
1da177e4 749 */
ee7e5e66 750 mxser_disable_and_clear_FIFO(info);
1da177e4 751
1c45607a
JS
752 /*
753 * At this point there's no way the LSR could still be 0xFF;
754 * if it is, then bail out, because there's likely no UART
755 * here.
756 */
757 if (inb(info->ioaddr + UART_LSR) == 0xff) {
758 spin_unlock_irqrestore(&info->slock, flags);
759 if (capable(CAP_SYS_ADMIN)) {
f43a510d 760 set_bit(TTY_IO_ERROR, &tty->flags);
1c45607a
JS
761 return 0;
762 } else
763 return -ENODEV;
764 }
1da177e4 765
1c45607a
JS
766 /*
767 * Clear the interrupt registers.
768 */
769 (void) inb(info->ioaddr + UART_LSR);
770 (void) inb(info->ioaddr + UART_RX);
771 (void) inb(info->ioaddr + UART_IIR);
772 (void) inb(info->ioaddr + UART_MSR);
773
774 /*
775 * Now, initialize the UART
776 */
777 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
778 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
779 outb(info->MCR, info->ioaddr + UART_MCR);
780
781 /*
782 * Finally, enable interrupts
783 */
784 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
785
292955a7 786 if (info->board->must_hwid)
1c45607a
JS
787 info->IER |= MOXA_MUST_IER_EGDAI;
788 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
789
790 /*
791 * And clear the interrupt registers again for luck.
792 */
793 (void) inb(info->ioaddr + UART_LSR);
794 (void) inb(info->ioaddr + UART_RX);
795 (void) inb(info->ioaddr + UART_IIR);
796 (void) inb(info->ioaddr + UART_MSR);
797
216ba023 798 clear_bit(TTY_IO_ERROR, &tty->flags);
1c45607a
JS
799 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
800
801 /*
802 * and set the speed of the serial port
803 */
3fdfa165 804 mxser_change_speed(tty, NULL);
1c45607a
JS
805 spin_unlock_irqrestore(&info->slock, flags);
806
807 return 0;
808}
809
810/*
6769140d 811 * This routine will shutdown a serial port
1c45607a 812 */
6769140d 813static void mxser_shutdown_port(struct tty_port *port)
1c45607a 814{
6769140d 815 struct mxser_port *info = container_of(port, struct mxser_port, port);
1c45607a
JS
816 unsigned long flags;
817
1c45607a
JS
818 spin_lock_irqsave(&info->slock, flags);
819
820 /*
821 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
822 * here so the queue might never be waken up
823 */
bdc04e31 824 wake_up_interruptible(&info->port.delta_msr_wait);
1c45607a
JS
825
826 /*
6769140d 827 * Free the xmit buffer, if necessary
1c45607a 828 */
0ad9e7d1
AC
829 if (info->port.xmit_buf) {
830 free_page((unsigned long) info->port.xmit_buf);
831 info->port.xmit_buf = NULL;
1da177e4
LT
832 }
833
1c45607a
JS
834 info->IER = 0;
835 outb(0x00, info->ioaddr + UART_IER);
836
1c45607a 837 /* clear Rx/Tx FIFO's */
ee7e5e66 838 mxser_disable_and_clear_FIFO(info);
1c45607a
JS
839
840 /* read data port to reset things */
841 (void) inb(info->ioaddr + UART_RX);
842
1c45607a 843
292955a7 844 if (info->board->must_hwid)
b441eb0f 845 mxser_must_no_sw_flow_control(info->ioaddr);
1c45607a
JS
846
847 spin_unlock_irqrestore(&info->slock, flags);
848}
849
850/*
851 * This routine is called whenever a serial port is opened. It
852 * enables interrupts for a serial port, linking in its async structure into
853 * the IRQ chain. It also performs the serial-specific
854 * initialization for the tty structure.
855 */
856static int mxser_open(struct tty_struct *tty, struct file *filp)
857{
42ad25fc
JS
858 struct tty_port *tport = tty->port;
859 struct mxser_port *port = container_of(tport, struct mxser_port, port);
1c45607a 860
42ad25fc 861 tty->driver_data = port;
1c45607a 862
42ad25fc 863 return tty_port_open(tport, tty, filp);
1da177e4
LT
864}
865
978e595f
AC
866static void mxser_flush_buffer(struct tty_struct *tty)
867{
868 struct mxser_port *info = tty->driver_data;
978e595f
AC
869 unsigned long flags;
870
871
872 spin_lock_irqsave(&info->slock, flags);
873 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
874
d249e662 875 outb(info->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
978e595f 876 info->ioaddr + UART_FCR);
978e595f
AC
877
878 spin_unlock_irqrestore(&info->slock, flags);
879
880 tty_wakeup(tty);
881}
882
568a2b9c
JS
883/*
884 * To stop accepting input, we disable the receive line status interrupts, and
885 * tell the interrupt driver to stop checking the data ready bit in the line
886 * status register.
887 */
888static void mxser_stop_rx(struct mxser_port *info)
1da177e4 889{
1da177e4 890 info->IER &= ~UART_IER_RLSI;
292955a7 891 if (info->board->must_hwid)
1da177e4 892 info->IER &= ~MOXA_MUST_RECV_ISR;
1c45607a 893
6769140d 894 outb(info->IER, info->ioaddr + UART_IER);
1e2b0254
AC
895}
896
897/*
898 * This routine is called when the serial port gets closed. First, we
899 * wait for the last remaining data to be sent. Then, we unlink its
900 * async structure from the interrupt chain if necessary, and we free
901 * that IRQ if nothing is left in the chain.
902 */
903static void mxser_close(struct tty_struct *tty, struct file *filp)
904{
905 struct mxser_port *info = tty->driver_data;
906 struct tty_port *port = &info->port;
907
1e2b0254
AC
908 if (tty_port_close_start(port, tty, filp) == 0)
909 return;
6769140d 910 mutex_lock(&port->mutex);
568a2b9c 911 mxser_stop_rx(info);
1e2b0254 912 mxser_flush_buffer(tty);
d41861ca
PH
913 if (tty_port_initialized(port) && C_HUPCL(tty))
914 tty_port_lower_dtr_rts(port);
6769140d 915 mxser_shutdown_port(port);
d41861ca 916 tty_port_set_initialized(port, 0);
6769140d 917 mutex_unlock(&port->mutex);
a6614999
AC
918 /* Right now the tty_port set is done outside of the close_end helper
919 as we don't yet have everyone using refcounts */
920 tty_port_close_end(port, tty);
921 tty_port_tty_set(port, NULL);
1da177e4
LT
922}
923
924static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
925{
926 int c, total = 0;
1c45607a 927 struct mxser_port *info = tty->driver_data;
1da177e4
LT
928 unsigned long flags;
929
1da177e4 930 while (1) {
8ea2c2ec
JJ
931 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
932 SERIAL_XMIT_SIZE - info->xmit_head));
1da177e4
LT
933 if (c <= 0)
934 break;
935
0ad9e7d1 936 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1da177e4 937 spin_lock_irqsave(&info->slock, flags);
8ea2c2ec
JJ
938 info->xmit_head = (info->xmit_head + c) &
939 (SERIAL_XMIT_SIZE - 1);
1da177e4
LT
940 info->xmit_cnt += c;
941 spin_unlock_irqrestore(&info->slock, flags);
942
943 buf += c;
944 count -= c;
945 total += c;
1da177e4
LT
946 }
947
5d1ea1ad
JS
948 if (info->xmit_cnt && !tty->flow.stopped)
949 if (!tty->hw_stopped || mxser_16550A_or_MUST(info))
740165f7 950 mxser_start_tx(info);
5d1ea1ad 951
1da177e4
LT
952 return total;
953}
954
0be2eade 955static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4 956{
1c45607a 957 struct mxser_port *info = tty->driver_data;
1da177e4
LT
958 unsigned long flags;
959
1da177e4 960 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
0be2eade 961 return 0;
1da177e4
LT
962
963 spin_lock_irqsave(&info->slock, flags);
0ad9e7d1 964 info->port.xmit_buf[info->xmit_head++] = ch;
1da177e4
LT
965 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
966 info->xmit_cnt++;
967 spin_unlock_irqrestore(&info->slock, flags);
8aff64e0 968
0be2eade 969 return 1;
1da177e4
LT
970}
971
972
973static void mxser_flush_chars(struct tty_struct *tty)
974{
1c45607a 975 struct mxser_port *info = tty->driver_data;
1da177e4 976
5c338fbf 977 if (!info->xmit_cnt || tty->flow.stopped ||
5d1ea1ad 978 (tty->hw_stopped && !mxser_16550A_or_MUST(info)))
1da177e4
LT
979 return;
980
740165f7 981 mxser_start_tx(info);
1da177e4
LT
982}
983
03b3b1a2 984static unsigned int mxser_write_room(struct tty_struct *tty)
1da177e4 985{
1c45607a 986 struct mxser_port *info = tty->driver_data;
1da177e4
LT
987 int ret;
988
989 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
ace7dd96 990 return ret < 0 ? 0 : ret;
1da177e4
LT
991}
992
fff4ef17 993static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
1da177e4 994{
1c45607a 995 struct mxser_port *info = tty->driver_data;
1da177e4
LT
996 return info->xmit_cnt;
997}
998
1c45607a
JS
999/*
1000 * ------------------------------------------------------------
1001 * friends of mxser_ioctl()
1002 * ------------------------------------------------------------
1003 */
216ba023 1004static int mxser_get_serial_info(struct tty_struct *tty,
6da5b587 1005 struct serial_struct *ss)
1c45607a 1006{
216ba023 1007 struct mxser_port *info = tty->driver_data;
6da5b587 1008 struct tty_port *port = &info->port;
be6cf583 1009 unsigned int closing_wait, close_delay;
6da5b587 1010
6da5b587 1011 mutex_lock(&port->mutex);
be6cf583
JH
1012
1013 close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
1014 closing_wait = info->port.closing_wait;
1015 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1016 closing_wait = jiffies_to_msecs(closing_wait) / 10;
1017
2285c496
DC
1018 ss->type = info->type;
1019 ss->line = tty->index;
1020 ss->port = info->ioaddr;
1021 ss->irq = info->board->irq;
1022 ss->flags = info->port.flags;
1023 ss->baud_base = MXSER_BAUD_BASE;
be6cf583
JH
1024 ss->close_delay = close_delay;
1025 ss->closing_wait = closing_wait;
d811b26b 1026 ss->custom_divisor = MXSER_CUSTOM_DIVISOR,
6da5b587 1027 mutex_unlock(&port->mutex);
1c45607a
JS
1028 return 0;
1029}
1030
216ba023 1031static int mxser_set_serial_info(struct tty_struct *tty,
6da5b587 1032 struct serial_struct *ss)
1da177e4 1033{
216ba023 1034 struct mxser_port *info = tty->driver_data;
07f86c03 1035 struct tty_port *port = &info->port;
80ff8a80 1036 speed_t baud;
1c45607a 1037 unsigned long sl_flags;
06cc52ef 1038 unsigned int old_speed, close_delay, closing_wait;
1c45607a 1039 int retval = 0;
1da177e4 1040
6da5b587
AV
1041 if (tty_io_error(tty))
1042 return -EIO;
1043
1044 mutex_lock(&port->mutex);
1da177e4 1045
6da5b587
AV
1046 if (ss->irq != info->board->irq ||
1047 ss->port != info->ioaddr) {
1048 mutex_unlock(&port->mutex);
80ff8a80 1049 return -EINVAL;
6da5b587 1050 }
1da177e4 1051
06cc52ef 1052 old_speed = port->flags & ASYNC_SPD_MASK;
1da177e4 1053
be6cf583
JH
1054 close_delay = msecs_to_jiffies(ss->close_delay * 10);
1055 closing_wait = ss->closing_wait;
1056 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1057 closing_wait = msecs_to_jiffies(closing_wait * 10);
1058
1c45607a 1059 if (!capable(CAP_SYS_ADMIN)) {
a6970c39 1060 if ((ss->baud_base != MXSER_BAUD_BASE) ||
1b3086b6
JS
1061 (close_delay != port->close_delay) ||
1062 (closing_wait != port->closing_wait) ||
1063 ((ss->flags & ~ASYNC_USR_MASK) != (port->flags & ~ASYNC_USR_MASK))) {
6da5b587 1064 mutex_unlock(&port->mutex);
1c45607a 1065 return -EPERM;
6da5b587 1066 }
1b3086b6
JS
1067 port->flags = (port->flags & ~ASYNC_USR_MASK) |
1068 (ss->flags & ASYNC_USR_MASK);
1c45607a 1069 } else {
1da177e4 1070 /*
1c45607a
JS
1071 * OK, past this point, all the error checking has been done.
1072 * At this point, we start making changes.....
1da177e4 1073 */
07f86c03 1074 port->flags = ((port->flags & ~ASYNC_FLAGS) |
6da5b587 1075 (ss->flags & ASYNC_FLAGS));
be6cf583
JH
1076 port->close_delay = close_delay;
1077 port->closing_wait = closing_wait;
07f86c03 1078 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
a6970c39 1079 (ss->baud_base != MXSER_BAUD_BASE ||
6da5b587 1080 ss->custom_divisor !=
d811b26b 1081 MXSER_CUSTOM_DIVISOR)) {
6da5b587
AV
1082 if (ss->custom_divisor == 0) {
1083 mutex_unlock(&port->mutex);
07f86c03 1084 return -EINVAL;
6da5b587
AV
1085 }
1086 baud = ss->baud_base / ss->custom_divisor;
216ba023 1087 tty_encode_baud_rate(tty, baud, baud);
80ff8a80 1088 }
fc83815c 1089
b91cfb25 1090 info->type = ss->type;
1da177e4 1091
c3db20c3 1092 mxser_process_txrx_fifo(info);
b91cfb25 1093 }
1c45607a 1094
d41861ca 1095 if (tty_port_initialized(port)) {
06cc52ef 1096 if (old_speed != (port->flags & ASYNC_SPD_MASK)) {
1c45607a 1097 spin_lock_irqsave(&info->slock, sl_flags);
3fdfa165 1098 mxser_change_speed(tty, NULL);
1c45607a 1099 spin_unlock_irqrestore(&info->slock, sl_flags);
1da177e4 1100 }
6769140d 1101 } else {
07f86c03 1102 retval = mxser_activate(port, tty);
6769140d 1103 if (retval == 0)
d41861ca 1104 tty_port_set_initialized(port, 1);
6769140d 1105 }
6da5b587 1106 mutex_unlock(&port->mutex);
1c45607a
JS
1107 return retval;
1108}
1da177e4 1109
1c45607a
JS
1110/*
1111 * mxser_get_lsr_info - get line status register info
1112 *
1113 * Purpose: Let user call ioctl() to get info when the UART physically
1114 * is emptied. On bus types like RS485, the transmitter must
1115 * release the bus after transmitting. This must be done when
1116 * the transmit shift register is empty, not be done when the
1117 * transmit holding register is empty. This functionality
1118 * allows an RS485 driver to be written in user space.
1119 */
1120static int mxser_get_lsr_info(struct mxser_port *info,
1121 unsigned int __user *value)
1122{
1123 unsigned char status;
1124 unsigned int result;
1125 unsigned long flags;
1da177e4 1126
1c45607a
JS
1127 spin_lock_irqsave(&info->slock, flags);
1128 status = inb(info->ioaddr + UART_LSR);
1129 spin_unlock_irqrestore(&info->slock, flags);
1130 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1131 return put_user(result, value);
1132}
1da177e4 1133
60b33c13 1134static int mxser_tiocmget(struct tty_struct *tty)
1c45607a
JS
1135{
1136 struct mxser_port *info = tty->driver_data;
30f6027f 1137 unsigned char control;
1c45607a 1138 unsigned long flags;
30f6027f 1139 u8 msr;
1da177e4 1140
18900ca6 1141 if (tty_io_error(tty))
1c45607a 1142 return -EIO;
1da177e4 1143
1c45607a 1144 spin_lock_irqsave(&info->slock, flags);
202acdaa 1145 control = info->MCR;
30f6027f 1146 msr = mxser_check_modem_status(tty, info);
1c45607a 1147 spin_unlock_irqrestore(&info->slock, flags);
202acdaa 1148
1c45607a
JS
1149 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1150 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
30f6027f
JS
1151 ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1152 ((msr & UART_MSR_RI) ? TIOCM_RNG : 0) |
1153 ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1154 ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0);
1c45607a 1155}
1da177e4 1156
20b9d177 1157static int mxser_tiocmset(struct tty_struct *tty,
1c45607a
JS
1158 unsigned int set, unsigned int clear)
1159{
1160 struct mxser_port *info = tty->driver_data;
1161 unsigned long flags;
1da177e4 1162
18900ca6 1163 if (tty_io_error(tty))
1c45607a 1164 return -EIO;
1da177e4 1165
1c45607a 1166 spin_lock_irqsave(&info->slock, flags);
1da177e4 1167
1c45607a
JS
1168 if (set & TIOCM_RTS)
1169 info->MCR |= UART_MCR_RTS;
1170 if (set & TIOCM_DTR)
1171 info->MCR |= UART_MCR_DTR;
1da177e4 1172
1c45607a
JS
1173 if (clear & TIOCM_RTS)
1174 info->MCR &= ~UART_MCR_RTS;
1175 if (clear & TIOCM_DTR)
1176 info->MCR &= ~UART_MCR_DTR;
8ea2c2ec 1177
1c45607a
JS
1178 outb(info->MCR, info->ioaddr + UART_MCR);
1179 spin_unlock_irqrestore(&info->slock, flags);
1180 return 0;
1181}
1da177e4 1182
1c45607a
JS
1183static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1184 struct async_icount *cprev)
1da177e4 1185{
1c45607a
JS
1186 struct async_icount cnow;
1187 unsigned long flags;
1188 int ret;
1da177e4 1189
1c45607a
JS
1190 spin_lock_irqsave(&info->slock, flags);
1191 cnow = info->icount; /* atomic copy */
1192 spin_unlock_irqrestore(&info->slock, flags);
1da177e4 1193
1c45607a
JS
1194 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1195 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1196 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1197 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1da177e4 1198
1c45607a
JS
1199 *cprev = cnow;
1200
1201 return ret;
1202}
1203
9fae5f85
JS
1204/* We should likely switch to TIOCGRS485/TIOCSRS485. */
1205static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set,
1206 int __user *u_opmode)
1207{
9fae5f85
JS
1208 int opmode, p = index % 4;
1209 int shiftbit = p * 2;
238d117d 1210 u8 val;
9fae5f85
JS
1211
1212 if (port->board->must_hwid != MOXA_MUST_MU860_HWID)
1213 return -EFAULT;
1214
1215 if (set) {
1216 if (get_user(opmode, u_opmode))
1217 return -EFAULT;
1218
238d117d
JS
1219 if (opmode & ~OP_MODE_MASK)
1220 return -EINVAL;
9fae5f85
JS
1221
1222 spin_lock_irq(&port->slock);
1223 val = inb(port->opmode_ioaddr);
238d117d 1224 val &= ~(OP_MODE_MASK << shiftbit);
9fae5f85
JS
1225 val |= (opmode << shiftbit);
1226 outb(val, port->opmode_ioaddr);
1227 spin_unlock_irq(&port->slock);
9fae5f85 1228
238d117d 1229 return 0;
9fae5f85
JS
1230 }
1231
238d117d
JS
1232 spin_lock_irq(&port->slock);
1233 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1234 spin_unlock_irq(&port->slock);
1235
1236 return put_user(opmode & OP_MODE_MASK, u_opmode);
9fae5f85
JS
1237}
1238
6caa76b7 1239static int mxser_ioctl(struct tty_struct *tty,
1c45607a 1240 unsigned int cmd, unsigned long arg)
1da177e4 1241{
1c45607a
JS
1242 struct mxser_port *info = tty->driver_data;
1243 struct async_icount cnow;
1c45607a
JS
1244 unsigned long flags;
1245 void __user *argp = (void __user *)arg;
1da177e4 1246
9fae5f85
JS
1247 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE)
1248 return mxser_ioctl_op_mode(info, tty->index,
1249 cmd == MOXA_SET_OP_MODE, argp);
1c45607a 1250
6da5b587 1251 if (cmd != TIOCMIWAIT && tty_io_error(tty))
1c45607a
JS
1252 return -EIO;
1253
1254 switch (cmd) {
1c45607a 1255 case TIOCSERGETLSR: /* Get line status register */
9d6d162d 1256 return mxser_get_lsr_info(info, argp);
1c45607a
JS
1257 /*
1258 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1259 * - mask passed in arg for lines of interest
1260 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1261 * Caller should use TIOCGICOUNT to see which one it was
1262 */
1263 case TIOCMIWAIT:
1264 spin_lock_irqsave(&info->slock, flags);
1265 cnow = info->icount; /* note the counters on entry */
1266 spin_unlock_irqrestore(&info->slock, flags);
1267
bdc04e31 1268 return wait_event_interruptible(info->port.delta_msr_wait,
1c45607a 1269 mxser_cflags_changed(info, arg, &cnow));
1c45607a
JS
1270 default:
1271 return -ENOIOCTLCMD;
1272 }
1273 return 0;
1274}
1275
0587102c
AC
1276 /*
1277 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1278 * Return: write counters to the user passed counter struct
1279 * NB: both 1->0 and 0->1 transitions are counted except for
1280 * RI where only 0->1 is counted.
1281 */
1282
1283static int mxser_get_icount(struct tty_struct *tty,
1284 struct serial_icounter_struct *icount)
1285
1286{
1287 struct mxser_port *info = tty->driver_data;
1288 struct async_icount cnow;
1289 unsigned long flags;
1290
1291 spin_lock_irqsave(&info->slock, flags);
1292 cnow = info->icount;
1293 spin_unlock_irqrestore(&info->slock, flags);
1294
1295 icount->frame = cnow.frame;
1296 icount->brk = cnow.brk;
1297 icount->overrun = cnow.overrun;
1298 icount->buf_overrun = cnow.buf_overrun;
1299 icount->parity = cnow.parity;
1300 icount->rx = cnow.rx;
1301 icount->tx = cnow.tx;
1302 icount->cts = cnow.cts;
1303 icount->dsr = cnow.dsr;
1304 icount->rng = cnow.rng;
1305 icount->dcd = cnow.dcd;
1306 return 0;
1307}
1308
1c45607a
JS
1309static void mxser_stoprx(struct tty_struct *tty)
1310{
1311 struct mxser_port *info = tty->driver_data;
1312
19236287 1313 info->ldisc_stop_rx = true;
1c45607a 1314 if (I_IXOFF(tty)) {
292955a7 1315 if (info->board->must_hwid) {
1c45607a
JS
1316 info->IER &= ~MOXA_MUST_RECV_ISR;
1317 outb(info->IER, info->ioaddr + UART_IER);
1318 } else {
1319 info->x_char = STOP_CHAR(tty);
1320 outb(0, info->ioaddr + UART_IER);
1321 info->IER |= UART_IER_THRI;
1322 outb(info->IER, info->ioaddr + UART_IER);
1da177e4
LT
1323 }
1324 }
1325
9db276f8 1326 if (C_CRTSCTS(tty)) {
1c45607a
JS
1327 info->MCR &= ~UART_MCR_RTS;
1328 outb(info->MCR, info->ioaddr + UART_MCR);
1da177e4
LT
1329 }
1330}
1331
1332/*
1333 * This routine is called by the upper-layer tty layer to signal that
1334 * incoming characters should be throttled.
1335 */
1336static void mxser_throttle(struct tty_struct *tty)
1337{
1da177e4 1338 mxser_stoprx(tty);
1da177e4
LT
1339}
1340
1341static void mxser_unthrottle(struct tty_struct *tty)
1342{
1c45607a 1343 struct mxser_port *info = tty->driver_data;
1da177e4 1344
1c45607a 1345 /* startrx */
19236287 1346 info->ldisc_stop_rx = false;
1c45607a
JS
1347 if (I_IXOFF(tty)) {
1348 if (info->x_char)
1349 info->x_char = 0;
1350 else {
292955a7 1351 if (info->board->must_hwid) {
1c45607a
JS
1352 info->IER |= MOXA_MUST_RECV_ISR;
1353 outb(info->IER, info->ioaddr + UART_IER);
1354 } else {
1355 info->x_char = START_CHAR(tty);
1356 outb(0, info->ioaddr + UART_IER);
1357 info->IER |= UART_IER_THRI;
1358 outb(info->IER, info->ioaddr + UART_IER);
1359 }
1da177e4 1360 }
1c45607a 1361 }
1da177e4 1362
9db276f8 1363 if (C_CRTSCTS(tty)) {
1c45607a
JS
1364 info->MCR |= UART_MCR_RTS;
1365 outb(info->MCR, info->ioaddr + UART_MCR);
1da177e4
LT
1366 }
1367}
1368
1369/*
1370 * mxser_stop() and mxser_start()
1371 *
6e94dbc7 1372 * This routines are called before setting or resetting tty->flow.stopped.
1da177e4
LT
1373 * They enable or disable transmitter interrupts, as necessary.
1374 */
1375static void mxser_stop(struct tty_struct *tty)
1376{
1c45607a 1377 struct mxser_port *info = tty->driver_data;
1da177e4
LT
1378 unsigned long flags;
1379
1380 spin_lock_irqsave(&info->slock, flags);
740165f7
JS
1381 if (info->IER & UART_IER_THRI)
1382 __mxser_stop_tx(info);
1da177e4
LT
1383 spin_unlock_irqrestore(&info->slock, flags);
1384}
1385
1386static void mxser_start(struct tty_struct *tty)
1387{
1c45607a 1388 struct mxser_port *info = tty->driver_data;
1da177e4
LT
1389 unsigned long flags;
1390
1391 spin_lock_irqsave(&info->slock, flags);
5c338fbf 1392 if (info->xmit_cnt)
740165f7 1393 __mxser_start_tx(info);
1da177e4
LT
1394 spin_unlock_irqrestore(&info->slock, flags);
1395}
1396
1c45607a
JS
1397static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1398{
1399 struct mxser_port *info = tty->driver_data;
1400 unsigned long flags;
1401
1402 spin_lock_irqsave(&info->slock, flags);
3fdfa165 1403 mxser_change_speed(tty, old_termios);
1c45607a
JS
1404 spin_unlock_irqrestore(&info->slock, flags);
1405
9db276f8 1406 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
1c45607a
JS
1407 tty->hw_stopped = 0;
1408 mxser_start(tty);
1409 }
1410
1411 /* Handle sw stopped */
9db276f8 1412 if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
6e94dbc7 1413 tty->flow.stopped = 0;
1c45607a 1414
292955a7 1415 if (info->board->must_hwid) {
1c45607a 1416 spin_lock_irqsave(&info->slock, flags);
b441eb0f 1417 mxser_must_set_rx_sw_flow_control(info->ioaddr, false);
1c45607a
JS
1418 spin_unlock_irqrestore(&info->slock, flags);
1419 }
1420
1421 mxser_start(tty);
1422 }
1423}
1424
1da177e4
LT
1425/*
1426 * mxser_wait_until_sent() --- wait until the transmitter is empty
1427 */
1428static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1429{
1c45607a 1430 struct mxser_port *info = tty->driver_data;
1da177e4 1431 unsigned long orig_jiffies, char_time;
07f86c03 1432 unsigned long flags;
1da177e4
LT
1433 int lsr;
1434
1435 if (info->type == PORT_UNKNOWN)
1436 return;
1437
1438 if (info->xmit_fifo_size == 0)
1439 return; /* Just in case.... */
1440
1441 orig_jiffies = jiffies;
1442 /*
1443 * Set the check interval to be 1/5 of the estimated time to
1444 * send a single character, and make it at least 1. The check
1445 * interval should also be less than the timeout.
1446 *
1447 * Note: we have to use pretty tight timings here to satisfy
1448 * the NIST-PCTS.
1449 */
1450 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1451 char_time = char_time / 5;
1452 if (char_time == 0)
1453 char_time = 1;
1454 if (timeout && timeout < char_time)
1455 char_time = timeout;
1456 /*
1457 * If the transmitter hasn't cleared in twice the approximate
1458 * amount of time to send the entire FIFO, it probably won't
1459 * ever clear. This assumes the UART isn't doing flow
1460 * control, which is currently the case. Hence, if it ever
1461 * takes longer than info->timeout, this is probably due to a
1462 * UART bug of some kind. So, we clamp the timeout parameter at
1463 * 2*info->timeout.
1464 */
1465 if (!timeout || timeout > 2 * info->timeout)
1466 timeout = 2 * info->timeout;
8bab534b 1467
07f86c03 1468 spin_lock_irqsave(&info->slock, flags);
1c45607a 1469 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
07f86c03 1470 spin_unlock_irqrestore(&info->slock, flags);
da4cd8df 1471 schedule_timeout_interruptible(char_time);
07f86c03 1472 spin_lock_irqsave(&info->slock, flags);
1da177e4 1473 if (signal_pending(current))
1c45607a
JS
1474 break;
1475 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1476 break;
1da177e4 1477 }
07f86c03 1478 spin_unlock_irqrestore(&info->slock, flags);
1c45607a 1479 set_current_state(TASK_RUNNING);
1c45607a 1480}
1da177e4 1481
1c45607a
JS
1482/*
1483 * This routine is called by tty_hangup() when a hangup is signaled.
1484 */
1485static void mxser_hangup(struct tty_struct *tty)
1486{
1487 struct mxser_port *info = tty->driver_data;
1da177e4 1488
1c45607a 1489 mxser_flush_buffer(tty);
3b6826b2 1490 tty_port_hangup(&info->port);
1da177e4
LT
1491}
1492
1c45607a
JS
1493/*
1494 * mxser_rs_break() --- routine which turns the break handling on or off
1495 */
9e98966c 1496static int mxser_rs_break(struct tty_struct *tty, int break_state)
1da177e4 1497{
1c45607a 1498 struct mxser_port *info = tty->driver_data;
1da177e4 1499 unsigned long flags;
59908433 1500 u8 lcr;
1da177e4 1501
1c45607a 1502 spin_lock_irqsave(&info->slock, flags);
59908433 1503 lcr = inb(info->ioaddr + UART_LCR);
1c45607a 1504 if (break_state == -1)
59908433 1505 lcr |= UART_LCR_SBC;
1c45607a 1506 else
59908433
JS
1507 lcr &= ~UART_LCR_SBC;
1508 outb(lcr, info->ioaddr + UART_LCR);
1c45607a 1509 spin_unlock_irqrestore(&info->slock, flags);
59908433 1510
9e98966c 1511 return 0;
1c45607a 1512}
1da177e4 1513
e5ce1bce 1514static bool mxser_receive_chars_new(struct tty_struct *tty,
95b3ea4c 1515 struct mxser_port *port, u8 status)
e5ce1bce
JS
1516{
1517 enum mxser_must_hwid hwid = port->board->must_hwid;
1518 u8 gdl;
1519
1520 if (hwid == MOXA_OTHER_UART)
1521 return false;
7d5006d5 1522 if (status & (UART_LSR_BRK_ERROR_BITS | MOXA_MUST_LSR_RERR))
e5ce1bce
JS
1523 return false;
1524
1525 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
1526 if (hwid == MOXA_MUST_MU150_HWID)
1527 gdl &= MOXA_MUST_GDL_MASK;
1528
1529 if (gdl >= tty->receive_room && !port->ldisc_stop_rx)
1530 mxser_stoprx(tty);
1531
1532 while (gdl--) {
1533 u8 ch = inb(port->ioaddr + UART_RX);
1534 tty_insert_flip_char(&port->port, ch, 0);
e5ce1bce
JS
1535 }
1536
1537 return true;
1538}
1539
0c419421 1540static u8 mxser_receive_chars_old(struct tty_struct *tty,
95b3ea4c 1541 struct mxser_port *port, u8 status)
1c45607a 1542{
0c419421
JS
1543 enum mxser_must_hwid hwid = port->board->must_hwid;
1544 int recv_room = tty->receive_room;
1c45607a 1545 int ignored = 0;
1c45607a 1546 int max = 256;
95b3ea4c 1547 int cnt = 0;
0c419421 1548 u8 ch;
1c45607a
JS
1549
1550 do {
1551 if (max-- < 0)
1552 break;
1da177e4 1553
1c45607a 1554 ch = inb(port->ioaddr + UART_RX);
0c419421 1555 if (hwid && (status & UART_LSR_OE))
d249e662 1556 outb(port->FCR | UART_FCR_CLEAR_RCVR,
aaa28e9f 1557 port->ioaddr + UART_FCR);
15517806
JS
1558 status &= port->read_status_mask;
1559 if (status & port->ignore_status_mask) {
1c45607a
JS
1560 if (++ignored > 100)
1561 break;
1562 } else {
1563 char flag = 0;
70640052 1564 if (status & UART_LSR_BRK_ERROR_BITS) {
15517806 1565 if (status & UART_LSR_BI) {
1c45607a
JS
1566 flag = TTY_BREAK;
1567 port->icount.brk++;
1da177e4 1568
0ad9e7d1 1569 if (port->port.flags & ASYNC_SAK)
1c45607a 1570 do_SAK(tty);
15517806 1571 } else if (status & UART_LSR_PE) {
1c45607a
JS
1572 flag = TTY_PARITY;
1573 port->icount.parity++;
15517806 1574 } else if (status & UART_LSR_FE) {
1c45607a
JS
1575 flag = TTY_FRAME;
1576 port->icount.frame++;
15517806 1577 } else if (status & UART_LSR_OE) {
1c45607a
JS
1578 flag = TTY_OVERRUN;
1579 port->icount.overrun++;
6de6e5c4 1580 }
1c45607a 1581 }
92a19f9c 1582 tty_insert_flip_char(&port->port, ch, flag);
95b3ea4c
JS
1583 cnt++;
1584 if (cnt >= recv_room) {
1c45607a
JS
1585 if (!port->ldisc_stop_rx)
1586 mxser_stoprx(tty);
1587 break;
1588 }
1da177e4 1589
1c45607a 1590 }
1da177e4 1591
0c419421 1592 if (hwid)
1c45607a 1593 break;
1da177e4 1594
15517806
JS
1595 status = inb(port->ioaddr + UART_LSR);
1596 } while (status & UART_LSR_DR);
1da177e4 1597
0c419421
JS
1598 return status;
1599}
1600
1601static u8 mxser_receive_chars(struct tty_struct *tty,
1602 struct mxser_port *port, u8 status)
1603{
0c419421
JS
1604 if (tty->receive_room == 0 && !port->ldisc_stop_rx)
1605 mxser_stoprx(tty);
1606
95b3ea4c
JS
1607 if (!mxser_receive_chars_new(tty, port, status))
1608 status = mxser_receive_chars_old(tty, port, status);
0c419421 1609
2e124b4a 1610 tty_flip_buffer_push(&port->port);
15517806
JS
1611
1612 return status;
1da177e4
LT
1613}
1614
216ba023 1615static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
1da177e4 1616{
3b88dbff 1617 int count;
1da177e4 1618
1c45607a
JS
1619 if (port->x_char) {
1620 outb(port->x_char, port->ioaddr + UART_TX);
1621 port->x_char = 0;
1c45607a
JS
1622 port->icount.tx++;
1623 return;
1624 }
1da177e4 1625
265ceff7 1626 if (!port->xmit_cnt || tty->flow.stopped ||
5d1ea1ad 1627 (tty->hw_stopped && !mxser_16550A_or_MUST(port))) {
740165f7 1628 __mxser_stop_tx(port);
1c45607a 1629 return;
1da177e4
LT
1630 }
1631
1c45607a
JS
1632 count = port->xmit_fifo_size;
1633 do {
0ad9e7d1 1634 outb(port->port.xmit_buf[port->xmit_tail++],
1c45607a 1635 port->ioaddr + UART_TX);
3b88dbff
JS
1636 port->xmit_tail &= SERIAL_XMIT_SIZE - 1;
1637 port->icount.tx++;
265ceff7 1638 if (!--port->xmit_cnt)
1c45607a
JS
1639 break;
1640 } while (--count > 0);
1da177e4 1641
464eb8f5 1642 if (port->xmit_cnt < WAKEUP_CHARS)
216ba023 1643 tty_wakeup(tty);
1c45607a 1644
265ceff7 1645 if (!port->xmit_cnt)
740165f7 1646 __mxser_stop_tx(port);
1da177e4
LT
1647}
1648
9e40ea1f
JS
1649static bool mxser_port_isr(struct mxser_port *port)
1650{
1651 struct tty_struct *tty;
30f6027f 1652 u8 iir, status;
9e40ea1f
JS
1653 bool error = false;
1654
1655 iir = inb(port->ioaddr + UART_IIR);
1656 if (iir & UART_IIR_NO_INT)
1657 return true;
1658
1659 iir &= MOXA_MUST_IIR_MASK;
1660 tty = tty_port_tty_get(&port->port);
274ab58d 1661 if (!tty) {
9e40ea1f 1662 status = inb(port->ioaddr + UART_LSR);
d249e662 1663 outb(port->FCR | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
aaa28e9f 1664 port->ioaddr + UART_FCR);
9e40ea1f
JS
1665 inb(port->ioaddr + UART_MSR);
1666
1667 error = true;
1668 goto put_tty;
1669 }
1670
1671 status = inb(port->ioaddr + UART_LSR);
1672
9e40ea1f
JS
1673 if (port->board->must_hwid) {
1674 if (iir == MOXA_MUST_IIR_GDA ||
1675 iir == MOXA_MUST_IIR_RDA ||
1676 iir == MOXA_MUST_IIR_RTO ||
1677 iir == MOXA_MUST_IIR_LSR)
1678 status = mxser_receive_chars(tty, port, status);
1679 } else {
1680 status &= port->read_status_mask;
1681 if (status & UART_LSR_DR)
1682 status = mxser_receive_chars(tty, port, status);
1683 }
1684
30f6027f 1685 mxser_check_modem_status(tty, port);
9e40ea1f
JS
1686
1687 if (port->board->must_hwid) {
1688 if (iir == 0x02 && (status & UART_LSR_THRE))
1689 mxser_transmit_chars(tty, port);
1690 } else {
1691 if (status & UART_LSR_THRE)
1692 mxser_transmit_chars(tty, port);
1693 }
1694
1695put_tty:
1696 tty_kref_put(tty);
1697
1698 return error;
1699}
1700
1da177e4 1701/*
1c45607a 1702 * This is the serial driver's generic interrupt routine
1da177e4 1703 */
1c45607a 1704static irqreturn_t mxser_interrupt(int irq, void *dev_id)
1da177e4 1705{
cef222cb 1706 struct mxser_board *brd = dev_id;
1c45607a 1707 struct mxser_port *port;
1c45607a 1708 unsigned int int_cnt, pass_counter = 0;
c24c31ff 1709 unsigned int i, max = brd->nports;
1c45607a 1710 int handled = IRQ_NONE;
9cb5c9c3 1711 u8 irqbits, bits, mask = BIT(max) - 1;
1da177e4 1712
1c45607a 1713 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
9cb5c9c3
JS
1714 irqbits = inb(brd->vector) & mask;
1715 if (irqbits == mask)
1c45607a 1716 break;
1da177e4 1717
1c45607a
JS
1718 handled = IRQ_HANDLED;
1719 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
9cb5c9c3 1720 if (irqbits == mask)
1c45607a
JS
1721 break;
1722 if (bits & irqbits)
1723 continue;
1724 port = &brd->ports[i];
1725
1726 int_cnt = 0;
1727 spin_lock(&port->slock);
1728 do {
9e40ea1f 1729 if (mxser_port_isr(port))
1c45607a 1730 break;
1c45607a
JS
1731 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1732 spin_unlock(&port->slock);
1733 }
1734 }
1da177e4 1735
1c45607a
JS
1736 return handled;
1737}
1da177e4 1738
1c45607a
JS
1739static const struct tty_operations mxser_ops = {
1740 .open = mxser_open,
1741 .close = mxser_close,
1742 .write = mxser_write,
1743 .put_char = mxser_put_char,
1744 .flush_chars = mxser_flush_chars,
1745 .write_room = mxser_write_room,
1746 .chars_in_buffer = mxser_chars_in_buffer,
1747 .flush_buffer = mxser_flush_buffer,
1748 .ioctl = mxser_ioctl,
1749 .throttle = mxser_throttle,
1750 .unthrottle = mxser_unthrottle,
1751 .set_termios = mxser_set_termios,
1752 .stop = mxser_stop,
1753 .start = mxser_start,
1754 .hangup = mxser_hangup,
1755 .break_ctl = mxser_rs_break,
1756 .wait_until_sent = mxser_wait_until_sent,
1757 .tiocmget = mxser_tiocmget,
1758 .tiocmset = mxser_tiocmset,
6da5b587
AV
1759 .set_serial = mxser_set_serial_info,
1760 .get_serial = mxser_get_serial_info,
0587102c 1761 .get_icount = mxser_get_icount,
1c45607a 1762};
1da177e4 1763
04b757df 1764static const struct tty_port_operations mxser_port_ops = {
31f35939 1765 .carrier_raised = mxser_carrier_raised,
fcc8ac18 1766 .dtr_rts = mxser_dtr_rts,
6769140d
AC
1767 .activate = mxser_activate,
1768 .shutdown = mxser_shutdown_port,
31f35939
AC
1769};
1770
1c45607a
JS
1771/*
1772 * The MOXA Smartio/Industio serial driver boot-time initialization code!
1773 */
1da177e4 1774
c24c31ff 1775static void mxser_initbrd(struct mxser_board *brd, bool high_baud)
1da177e4 1776{
1c45607a
JS
1777 struct mxser_port *info;
1778 unsigned int i;
57faa7d6
JS
1779 bool is_mu860;
1780
1781 brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
1782 is_mu860 = brd->must_hwid == MOXA_MUST_MU860_HWID;
1783
1784 for (i = 0; i < UART_INFO_NUM; i++) {
1785 if (Gpci_uart_info[i].type == brd->must_hwid) {
1786 brd->max_baud = Gpci_uart_info[i].max_baud;
1787
1788 /* exception....CP-102 */
c24c31ff 1789 if (high_baud)
57faa7d6
JS
1790 brd->max_baud = 921600;
1791 break;
1792 }
1793 }
1794
1795 if (is_mu860) {
1796 /* set to RS232 mode by default */
1797 outb(0, brd->vector + 4);
1798 outb(0, brd->vector + 0x0c);
1799 }
1da177e4 1800
c24c31ff 1801 for (i = 0; i < brd->nports; i++) {
1c45607a 1802 info = &brd->ports[i];
57faa7d6
JS
1803 if (is_mu860) {
1804 if (i < 4)
1805 info->opmode_ioaddr = brd->vector + 4;
1806 else
1807 info->opmode_ioaddr = brd->vector + 0x0c;
1808 }
44b7d1b3 1809 tty_port_init(&info->port);
31f35939 1810 info->port.ops = &mxser_port_ops;
1c45607a 1811 info->board = brd;
19236287 1812 info->ldisc_stop_rx = false;
1da177e4 1813
1c45607a 1814 /* Enhance mode enabled here */
292955a7 1815 if (brd->must_hwid != MOXA_OTHER_UART)
edb7d27c 1816 mxser_must_set_enhance_mode(info->ioaddr, true);
1da177e4 1817
58a2ddb3 1818 info->type = PORT_16550A;
1da177e4 1819
c3db20c3 1820 mxser_process_txrx_fifo(info);
1da177e4 1821
44b7d1b3
AC
1822 info->port.close_delay = 5 * HZ / 10;
1823 info->port.closing_wait = 30 * HZ;
1c45607a 1824 spin_lock_init(&info->slock);
1da177e4 1825
1c45607a
JS
1826 /* before set INT ISR, disable all int */
1827 outb(inb(info->ioaddr + UART_IER) & 0xf0,
1828 info->ioaddr + UART_IER);
1829 }
1c45607a 1830}
1da177e4 1831
9671f099 1832static int mxser_probe(struct pci_dev *pdev,
1c45607a 1833 const struct pci_device_id *ent)
1da177e4 1834{
1c45607a 1835 struct mxser_board *brd;
13d4aba8 1836 unsigned int i, base;
1c45607a 1837 unsigned long ioaddress;
c24c31ff 1838 unsigned short nports = MXSER_NPORTS(ent->driver_data);
9e17df37 1839 struct device *tty_dev;
1c45607a 1840 int retval = -EINVAL;
1da177e4 1841
f8b6b327 1842 i = find_first_zero_bit(mxser_boards, MXSER_BOARDS);
1c45607a 1843 if (i >= MXSER_BOARDS) {
83766bc6
JS
1844 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
1845 "not configured\n", MXSER_BOARDS);
1c45607a
JS
1846 goto err;
1847 }
1848
ad1c92ff
JS
1849 brd = devm_kzalloc(&pdev->dev, struct_size(brd, ports, nports),
1850 GFP_KERNEL);
f8b6b327
JS
1851 if (!brd)
1852 goto err;
1853
13d4aba8 1854 brd->idx = i;
f8b6b327 1855 __set_bit(brd->idx, mxser_boards);
13d4aba8 1856 base = i * MXSER_PORTS_PER_BOARD;
1c45607a 1857
dcb04e21 1858 retval = pcim_enable_device(pdev);
1c45607a 1859 if (retval) {
83766bc6 1860 dev_err(&pdev->dev, "PCI enable failed\n");
f8b6b327 1861 goto err_zero;
1c45607a
JS
1862 }
1863
1864 /* io address */
1865 ioaddress = pci_resource_start(pdev, 2);
1866 retval = pci_request_region(pdev, 2, "mxser(IO)");
1867 if (retval)
f8b6b327 1868 goto err_zero;
1c45607a 1869
c24c31ff
JS
1870 brd->nports = nports;
1871 for (i = 0; i < nports; i++)
1c45607a
JS
1872 brd->ports[i].ioaddr = ioaddress + 8 * i;
1873
1874 /* vector */
1875 ioaddress = pci_resource_start(pdev, 3);
1876 retval = pci_request_region(pdev, 3, "mxser(vector)");
1877 if (retval)
df480518 1878 goto err_zero;
1c45607a
JS
1879 brd->vector = ioaddress;
1880
1881 /* irq */
1882 brd->irq = pdev->irq;
1883
c24c31ff 1884 mxser_initbrd(brd, ent->driver_data & MXSER_HIGHBAUD);
7f0e79dc
JS
1885
1886 retval = devm_request_irq(&pdev->dev, brd->irq, mxser_interrupt,
1887 IRQF_SHARED, "mxser", brd);
1888 if (retval) {
1889 dev_err(&pdev->dev, "request irq failed");
1890 goto err_relbrd;
1891 }
1c45607a 1892
c24c31ff 1893 for (i = 0; i < nports; i++) {
9e17df37 1894 tty_dev = tty_port_register_device(&brd->ports[i].port,
13d4aba8 1895 mxvar_sdriver, base + i, &pdev->dev);
9e17df37
AK
1896 if (IS_ERR(tty_dev)) {
1897 retval = PTR_ERR(tty_dev);
1b581f17 1898 for (; i > 0; i--)
9e17df37 1899 tty_unregister_device(mxvar_sdriver,
13d4aba8 1900 base + i - 1);
9e17df37
AK
1901 goto err_relbrd;
1902 }
1903 }
1c45607a
JS
1904
1905 pci_set_drvdata(pdev, brd);
1906
1907 return 0;
9e17df37 1908err_relbrd:
c24c31ff 1909 for (i = 0; i < nports; i++)
9e17df37 1910 tty_port_destroy(&brd->ports[i].port);
df480518 1911err_zero:
f8b6b327 1912 __clear_bit(brd->idx, mxser_boards);
1c45607a
JS
1913err:
1914 return retval;
1da177e4
LT
1915}
1916
ae8d8a14 1917static void mxser_remove(struct pci_dev *pdev)
1da177e4 1918{
1c45607a 1919 struct mxser_board *brd = pci_get_drvdata(pdev);
13d4aba8 1920 unsigned int i, base = brd->idx * MXSER_PORTS_PER_BOARD;
d450f085 1921
c24c31ff 1922 for (i = 0; i < brd->nports; i++) {
13d4aba8 1923 tty_unregister_device(mxvar_sdriver, base + i);
d450f085
JS
1924 tty_port_destroy(&brd->ports[i].port);
1925 }
1da177e4 1926
f8b6b327 1927 __clear_bit(brd->idx, mxser_boards);
1da177e4
LT
1928}
1929
1c45607a
JS
1930static struct pci_driver mxser_driver = {
1931 .name = "mxser",
1932 .id_table = mxser_pcibrds,
1933 .probe = mxser_probe,
91116cba 1934 .remove = mxser_remove
1c45607a
JS
1935};
1936
1937static int __init mxser_module_init(void)
1da177e4 1938{
1df00924 1939 int retval;
1da177e4 1940
39b7b42b
JS
1941 mxvar_sdriver = tty_alloc_driver(MXSER_PORTS, TTY_DRIVER_REAL_RAW |
1942 TTY_DRIVER_DYNAMIC_DEV);
1943 if (IS_ERR(mxvar_sdriver))
1944 return PTR_ERR(mxvar_sdriver);
1c45607a 1945
1c45607a 1946 /* Initialize the tty_driver structure */
1c45607a
JS
1947 mxvar_sdriver->name = "ttyMI";
1948 mxvar_sdriver->major = ttymajor;
1949 mxvar_sdriver->minor_start = 0;
1c45607a
JS
1950 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
1951 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
1952 mxvar_sdriver->init_termios = tty_std_termios;
1953 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
1c45607a
JS
1954 tty_set_operations(mxvar_sdriver, &mxser_ops);
1955
1956 retval = tty_register_driver(mxvar_sdriver);
1957 if (retval) {
1958 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
1959 "tty driver !\n");
1960 goto err_put;
1da177e4 1961 }
1c45607a 1962
1c45607a
JS
1963 retval = pci_register_driver(&mxser_driver);
1964 if (retval) {
83766bc6 1965 printk(KERN_ERR "mxser: can't register pci driver\n");
29134367 1966 goto err_unr;
1c45607a
JS
1967 }
1968
1c45607a
JS
1969 return 0;
1970err_unr:
1971 tty_unregister_driver(mxvar_sdriver);
1972err_put:
9f90a4dd 1973 tty_driver_kref_put(mxvar_sdriver);
1c45607a
JS
1974 return retval;
1975}
1976
1977static void __exit mxser_module_exit(void)
1978{
1c45607a 1979 pci_unregister_driver(&mxser_driver);
1c45607a 1980 tty_unregister_driver(mxvar_sdriver);
9f90a4dd 1981 tty_driver_kref_put(mxvar_sdriver);
1da177e4
LT
1982}
1983
1984module_init(mxser_module_init);
1985module_exit(mxser_module_exit);