blob: 7caf369571f16a99a4df7124a99e74c5ede0ecfa [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 *
Jiri Slaby80ff8a82008-02-07 00:16:51 -08005 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
6 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Jiri Slaby1c456072008-02-07 00:16:46 -08008 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
Alan Cox8eb04cf2008-11-11 14:48:44 +000013 * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
14 * www.moxa.com.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * - Fixed x86_64 cleanness
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/ioport.h>
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/delay.h>
35#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070036#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Manuel Zerpies5a3c6b2512011-06-16 14:07:22 +020038#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
41#include <asm/irq.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jiri Slaby4463cc52021-06-18 08:14:10 +020044/*
45 * Semi-public control interfaces
46 */
47
48/*
49 * MOXA ioctls
50 */
51
52#define MOXA 0x400
Jiri Slaby4463cc52021-06-18 08:14:10 +020053#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
Jiri Slaby4463cc52021-06-18 08:14:10 +020062/* --------------------------------------------------- */
63
Jiri Slaby4463cc52021-06-18 08:14:10 +020064/*
65 * Follow just what Moxa Must chip defines.
66 *
67 * When LCR register (offset 0x03) writes the following value, the Must chip
68 * will enter enchance mode. And write value on EFR (offset 0x02) bit 6,7 to
69 * change bank.
70 */
71#define MOXA_MUST_ENTER_ENCHANCE 0xBF
72
73/* when enhance mode enabled, access on general bank register */
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 */
79/* enchance register bank select and enchance mode setting register */
80/* when LCR register equals to 0xBF */
81#define MOXA_MUST_EFR_REGISTER 0x02
82#define MOXA_MUST_EFR_EFRB_ENABLE 0x10 /* enchance mode enable */
83/* enchance register bank set 0, 1, 2 */
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define MXSERMAJOR 174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -0800154#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
155#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define WAKEUP_CHARS 256
158
Jiri Slabya6970c32021-06-18 08:14:41 +0200159#define MXSER_BAUD_BASE 921600
Jiri Slabyd811b262021-06-18 08:14:42 +0200160#define MXSER_CUSTOM_DIVISOR (MXSER_BAUD_BASE * 16)
Jiri Slabya6970c32021-06-18 08:14:41 +0200161
Jiri Slabye129def2008-07-22 11:20:34 +0100162#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -0800163#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +0100164#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby502f2952009-09-10 12:20:07 +0200165#define PCI_DEVICE_ID_CP112UL 0x1120
Jiri Slaby1c456072008-02-07 00:16:46 -0800166#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800167#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -0800168#define PCI_DEVICE_ID_CB134I 0x1341
169#define PCI_DEVICE_ID_CP138U 0x1380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jiri Slaby1c456072008-02-07 00:16:46 -0800171#define MXSER_HIGHBAUD 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jiri Slabye4558362021-06-18 08:14:14 +0200173enum mxser_must_hwid {
174 MOXA_OTHER_UART = 0x00,
175 MOXA_MUST_MU150_HWID = 0x01,
176 MOXA_MUST_MU860_HWID = 0x02,
177};
178
Jiri Slaby1c456072008-02-07 00:16:46 -0800179static const struct {
Jiri Slabydc33f642021-06-18 08:14:11 +0200180 u8 type;
181 u8 fifo_size;
182 u8 rx_high_water;
183 u8 rx_low_water;
184 speed_t max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800185} Gpci_uart_info[] = {
Jiri Slabydc33f642021-06-18 08:14:11 +0200186 { MOXA_OTHER_UART, 16, 14, 1, 921600 },
187 { MOXA_MUST_MU150_HWID, 64, 48, 16, 230400 },
188 { MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
Jiri Slaby1c456072008-02-07 00:16:46 -0800190#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Jiri Slaby1c456072008-02-07 00:16:46 -0800192struct mxser_cardinfo {
193 char *name;
194 unsigned int nports;
195 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Jiri Slaby1c456072008-02-07 00:16:46 -0800198static const struct mxser_cardinfo mxser_cards[] = {
Jiri Slaby15254902021-06-18 08:14:08 +0200199/* 0*/ { "C168H/PCI series", 8, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800200 { "C104H/PCI series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800201 { "CP-132 series", 2, },
202 { "CP-114 series", 4, },
Jiri Slaby15254902021-06-18 08:14:08 +0200203 { "CT-114 series", 4, },
204/* 5*/ { "CP-102 series", 2, MXSER_HIGHBAUD },
Jiri Slaby1c456072008-02-07 00:16:46 -0800205 { "CP-104U series", 4, },
206 { "CP-168U series", 8, },
207 { "CP-132U series", 2, },
Jiri Slaby15254902021-06-18 08:14:08 +0200208 { "CP-134U series", 4, },
209/*10*/ { "CP-104JU series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800210 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
211 { "CP-118U series", 8, },
212 { "CP-102UL series", 2, },
Jiri Slaby15254902021-06-18 08:14:08 +0200213 { "CP-102U series", 2, },
214/*15*/ { "CP-118EL series", 8, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800215 { "CP-168EL series", 8, },
216 { "CP-104EL series", 4, },
217 { "CB-108 series", 8, },
Jiri Slaby15254902021-06-18 08:14:08 +0200218 { "CB-114 series", 4, },
219/*20*/ { "CB-134I series", 4, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800220 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800221 { "POS-104UL series", 4, },
Jiri Slabye129def2008-07-22 11:20:34 +0100222 { "CP-114UL series", 4, },
Jiri Slaby15254902021-06-18 08:14:08 +0200223 { "CP-102UF series", 2, },
224/*25*/ { "CP-112UL series", 2, },
Jiri Slaby1c456072008-02-07 00:16:46 -0800225};
226
227/* driver_data correspond to the lines in the structure above
228 see also ISA probe function before you change something */
Arvind Yadav3385ecf2017-07-23 15:31:01 +0530229static const struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby15254902021-06-18 08:14:08 +0200230 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 0 },
231 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 1 },
232 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 2 },
233 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 3 },
234 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 4 },
235 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 5 },
236 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 6 },
237 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 7 },
238 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 8 },
239 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 9 },
240 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 10 },
241 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 11 },
242 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 12 },
243 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 13 },
244 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 14 },
245 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 15 },
246 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 16 },
247 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 17 },
248 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 18 },
249 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 19 },
250 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 20 },
251 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 21 },
252 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 22 },
253 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 23 },
254 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 24 },
255 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 25 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800256 { }
257};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/* Variables for insmod */
263
264MODULE_AUTHOR("Casper Yang");
265MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800266module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267MODULE_LICENSE("GPL");
268
Jiri Slaby1c456072008-02-07 00:16:46 -0800269struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jiri Slaby1c456072008-02-07 00:16:46 -0800271struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100272 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800273 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800274
275 unsigned long ioaddr;
276 unsigned long opmode_ioaddr;
Jiri Slaby1c456072008-02-07 00:16:46 -0800277
Jiri Slabydc33f642021-06-18 08:14:11 +0200278 u8 rx_high_water;
279 u8 rx_low_water;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800281
Jiri Slabya93963e2021-06-18 08:14:24 +0200282 unsigned char x_char; /* xon/xoff character */
283 u8 IER; /* Interrupt Enable Register */
284 u8 MCR; /* Modem control register */
Jiri Slaby1c456072008-02-07 00:16:46 -0800285
Jiri Slaby1c456072008-02-07 00:16:46 -0800286 unsigned char ldisc_stop_rx;
287
Jiri Slaby1c456072008-02-07 00:16:46 -0800288 struct async_icount icount; /* kernel counters for 4 input interrupts */
Jiri Slaby104583b2017-09-12 12:39:54 +0200289 unsigned int timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800290
Jiri Slabya93963e2021-06-18 08:14:24 +0200291 u8 read_status_mask;
292 u8 ignore_status_mask;
Jiri Slabydc33f642021-06-18 08:14:11 +0200293 u8 xmit_fifo_size;
Jiri Slaby02e43142021-06-18 08:14:25 +0200294 unsigned int xmit_head;
295 unsigned int xmit_tail;
296 unsigned int xmit_cnt;
Peter Hurleycd7b4b32016-01-10 14:51:38 -0800297 int closing;
Jiri Slaby1c456072008-02-07 00:16:46 -0800298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800300};
301
302struct mxser_board {
303 unsigned int idx;
304 int irq;
305 const struct mxser_cardinfo *info;
306 unsigned long vector;
Jiri Slaby1c456072008-02-07 00:16:46 -0800307
Jiri Slabye4558362021-06-18 08:14:14 +0200308 enum mxser_must_hwid must_hwid;
Jiri Slaby928f9462021-06-18 08:14:16 +0200309 speed_t max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800310
311 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
Jiri Slaby1c456072008-02-07 00:16:46 -0800314static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Jiri Slabyedb7d272021-06-18 08:14:46 +0200317static u8 __mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set,
318 bool restore_LCR)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700319{
Jiri Slabyedb7d272021-06-18 08:14:46 +0200320 u8 oldlcr, efr;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700321
322 oldlcr = inb(baseio + UART_LCR);
323 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
324
325 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
Jiri Slabyedb7d272021-06-18 08:14:46 +0200326 efr &= ~clear;
327 efr |= set;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700328
329 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700330
Jiri Slabyedb7d272021-06-18 08:14:46 +0200331 if (restore_LCR)
332 outb(oldlcr, baseio + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700333
Jiri Slabyedb7d272021-06-18 08:14:46 +0200334 return oldlcr;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700335}
336
337static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
338{
339 u8 oldlcr;
340 u8 efr;
341
342 oldlcr = inb(baseio + UART_LCR);
343 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
344
345 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
346 efr &= ~MOXA_MUST_EFR_BANK_MASK;
347 efr |= MOXA_MUST_EFR_BANK0;
348
349 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
350 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
351 outb(oldlcr, baseio + UART_LCR);
352}
353
354static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
355{
356 u8 oldlcr;
357 u8 efr;
358
359 oldlcr = inb(baseio + UART_LCR);
360 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
361
362 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
363 efr &= ~MOXA_MUST_EFR_BANK_MASK;
364 efr |= MOXA_MUST_EFR_BANK0;
365
366 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
367 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
368 outb(oldlcr, baseio + UART_LCR);
369}
370
371static void mxser_set_must_fifo_value(struct mxser_port *info)
372{
373 u8 oldlcr;
374 u8 efr;
375
376 oldlcr = inb(info->ioaddr + UART_LCR);
377 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
378
379 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
380 efr &= ~MOXA_MUST_EFR_BANK_MASK;
381 efr |= MOXA_MUST_EFR_BANK1;
382
383 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
Jiri Slabydc33f642021-06-18 08:14:11 +0200384 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
385 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
386 outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700387 outb(oldlcr, info->ioaddr + UART_LCR);
388}
389
390static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
391{
392 u8 oldlcr;
393 u8 efr;
394
395 oldlcr = inb(baseio + UART_LCR);
396 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
397
398 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
399 efr &= ~MOXA_MUST_EFR_BANK_MASK;
400 efr |= MOXA_MUST_EFR_BANK2;
401
402 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
403 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
404 outb(oldlcr, baseio + UART_LCR);
405}
406
407static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
408{
409 u8 oldlcr;
410 u8 efr;
411
412 oldlcr = inb(baseio + UART_LCR);
413 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
414
415 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
416 efr &= ~MOXA_MUST_EFR_BANK_MASK;
417 efr |= MOXA_MUST_EFR_BANK2;
418
419 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
420 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
421 outb(oldlcr, baseio + UART_LCR);
422}
423
Jiri Slabyedb7d272021-06-18 08:14:46 +0200424static void mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set)
425{
426 __mxser_must_set_EFR(baseio, clear, set, true);
427}
428
429static void mxser_must_set_enhance_mode(unsigned long baseio, bool enable)
430{
431 mxser_must_set_EFR(baseio,
432 enable ? 0 : MOXA_MUST_EFR_EFRB_ENABLE,
433 enable ? MOXA_MUST_EFR_EFRB_ENABLE : 0);
434}
435
Christoph Hellwig148ff862008-04-30 00:54:29 -0700436static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
437{
438 u8 oldlcr;
439 u8 efr;
440
441 oldlcr = inb(baseio + UART_LCR);
442 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
443
444 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
445 efr &= ~MOXA_MUST_EFR_SF_MASK;
446
447 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
448 outb(oldlcr, baseio + UART_LCR);
449}
450
451static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
452{
453 u8 oldlcr;
454 u8 efr;
455
456 oldlcr = inb(baseio + UART_LCR);
457 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
458
459 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
460 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
461 efr |= MOXA_MUST_EFR_SF_TX1;
462
463 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
464 outb(oldlcr, baseio + UART_LCR);
465}
466
467static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
468{
469 u8 oldlcr;
470 u8 efr;
471
472 oldlcr = inb(baseio + UART_LCR);
473 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
474
475 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
476 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
477
478 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
479 outb(oldlcr, baseio + UART_LCR);
480}
481
482static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
483{
484 u8 oldlcr;
485 u8 efr;
486
487 oldlcr = inb(baseio + UART_LCR);
488 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
489
490 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
491 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
492 efr |= MOXA_MUST_EFR_SF_RX1;
493
494 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
495 outb(oldlcr, baseio + UART_LCR);
496}
497
498static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
499{
500 u8 oldlcr;
501 u8 efr;
502
503 oldlcr = inb(baseio + UART_LCR);
504 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
505
506 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
507 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
508
509 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
510 outb(oldlcr, baseio + UART_LCR);
511}
512
Jiri Slabye4558362021-06-18 08:14:14 +0200513static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 u8 oldmcr, hwid;
516 int i;
517
518 outb(0, io + UART_LCR);
Jiri Slabyedb7d272021-06-18 08:14:46 +0200519 mxser_must_set_enhance_mode(io, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 oldmcr = inb(io + UART_MCR);
521 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700522 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if ((hwid = inb(io + UART_MCR)) != 0) {
524 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700525 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
Christoph Hellwig148ff862008-04-30 00:54:29 -0700528 mxser_get_must_hardware_id(io, &hwid);
Jiri Slabye4558362021-06-18 08:14:14 +0200529 for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
Jiri Slaby1c456072008-02-07 00:16:46 -0800530 if (hwid == Gpci_uart_info[i].type)
Jiri Slabye4558362021-06-18 08:14:14 +0200531 return hwid;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return MOXA_OTHER_UART;
534}
535
Jiri Slaby1c456072008-02-07 00:16:46 -0800536static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 int i;
539
540 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 info->rx_high_water = 1;
542 info->rx_low_water = 1;
543 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800544 } else
545 for (i = 0; i < UART_INFO_NUM; i++)
Jiri Slaby292955a2021-06-18 08:14:13 +0200546 if (info->board->must_hwid == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
548 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
Jiri Slabydc33f642021-06-18 08:14:11 +0200549 info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Alan Cox31f35932009-01-02 13:45:05 +0000554static int mxser_carrier_raised(struct tty_port *port)
555{
556 struct mxser_port *mp = container_of(port, struct mxser_port, port);
557 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
558}
559
Alan Coxfcc8ac12009-06-11 12:24:17 +0100560static void mxser_dtr_rts(struct tty_port *port, int on)
Alan Cox5d951fb2009-01-02 13:45:19 +0000561{
562 struct mxser_port *mp = container_of(port, struct mxser_port, port);
563 unsigned long flags;
564
565 spin_lock_irqsave(&mp->slock, flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +0100566 if (on)
567 outb(inb(mp->ioaddr + UART_MCR) |
568 UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
569 else
570 outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
571 mp->ioaddr + UART_MCR);
Alan Cox5d951fb2009-01-02 13:45:19 +0000572 spin_unlock_irqrestore(&mp->slock, flags);
573}
574
Jiri Slabydc33f642021-06-18 08:14:11 +0200575static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
Jiri Slaby1c456072008-02-07 00:16:46 -0800576{
Alan Cox216ba022008-10-13 10:40:19 +0100577 struct mxser_port *info = tty->driver_data;
Jiri Slaby104583b2017-09-12 12:39:54 +0200578 unsigned int quot = 0, baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800579 unsigned char cval;
Jiri Slaby104583b2017-09-12 12:39:54 +0200580 u64 timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800581
Alan Cox216ba022008-10-13 10:40:19 +0100582 if (!info->ioaddr)
Jiri Slaby1c456072008-02-07 00:16:46 -0800583 return -1;
584
Jiri Slaby928f9462021-06-18 08:14:16 +0200585 if (newspd > info->board->max_baud)
Jiri Slaby1c456072008-02-07 00:16:46 -0800586 return -1;
587
588 if (newspd == 134) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200589 quot = 2 * MXSER_BAUD_BASE / 269;
Alan Cox216ba022008-10-13 10:40:19 +0100590 tty_encode_baud_rate(tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800591 } else if (newspd) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200592 quot = MXSER_BAUD_BASE / newspd;
Jiri Slaby1c456072008-02-07 00:16:46 -0800593 if (quot == 0)
594 quot = 1;
Jiri Slabya6970c32021-06-18 08:14:41 +0200595 baud = MXSER_BAUD_BASE / quot;
Alan Cox216ba022008-10-13 10:40:19 +0100596 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800597 } else {
598 quot = 0;
599 }
600
Jiri Slaby104583b2017-09-12 12:39:54 +0200601 /*
602 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
603 * u64 domain
604 */
605 timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
Jiri Slabya6970c32021-06-18 08:14:41 +0200606 do_div(timeout, MXSER_BAUD_BASE);
Jiri Slaby104583b2017-09-12 12:39:54 +0200607 info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
Jiri Slaby1c456072008-02-07 00:16:46 -0800608
609 if (quot) {
610 info->MCR |= UART_MCR_DTR;
611 outb(info->MCR, info->ioaddr + UART_MCR);
612 } else {
613 info->MCR &= ~UART_MCR_DTR;
614 outb(info->MCR, info->ioaddr + UART_MCR);
615 return 0;
616 }
617
618 cval = inb(info->ioaddr + UART_LCR);
619
620 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
621
622 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
623 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
624 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
625
626#ifdef BOTHER
Alan Cox216ba022008-10-13 10:40:19 +0100627 if (C_BAUD(tty) == BOTHER) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200628 quot = MXSER_BAUD_BASE % newspd;
Jiri Slaby1c456072008-02-07 00:16:46 -0800629 quot *= 8;
630 if (quot % newspd > newspd / 2) {
631 quot /= newspd;
632 quot++;
633 } else
634 quot /= newspd;
635
Christoph Hellwig148ff862008-04-30 00:54:29 -0700636 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800637 } else
638#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700639 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800640
641 return 0;
642}
643
644/*
645 * This routine is called to set the UART divisor registers to match
646 * the specified baud rate for a serial port.
647 */
Jason Yanbeca62c2020-05-06 14:17:35 +0800648static void mxser_change_speed(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800649{
Alan Cox216ba022008-10-13 10:40:19 +0100650 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800651 unsigned cflag, cval, fcr;
Jiri Slaby1c456072008-02-07 00:16:46 -0800652 unsigned char status;
653
Alan Coxadc8d742012-07-14 15:31:47 +0100654 cflag = tty->termios.c_cflag;
Alan Cox216ba022008-10-13 10:40:19 +0100655 if (!info->ioaddr)
Jason Yanbeca62c2020-05-06 14:17:35 +0800656 return;
Jiri Slaby1c456072008-02-07 00:16:46 -0800657
Jiri Slabyef3dff82021-06-18 08:14:29 +0200658 mxser_set_baud(tty, tty_get_baud_rate(tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800659
660 /* byte size and parity */
661 switch (cflag & CSIZE) {
Jiri Slaby2c218322021-06-18 08:14:43 +0200662 default:
Jiri Slaby1c456072008-02-07 00:16:46 -0800663 case CS5:
Jiri Slaby2c218322021-06-18 08:14:43 +0200664 cval = UART_LCR_WLEN5;
Jiri Slaby1c456072008-02-07 00:16:46 -0800665 break;
666 case CS6:
Jiri Slaby2c218322021-06-18 08:14:43 +0200667 cval = UART_LCR_WLEN6;
Jiri Slaby1c456072008-02-07 00:16:46 -0800668 break;
669 case CS7:
Jiri Slaby2c218322021-06-18 08:14:43 +0200670 cval = UART_LCR_WLEN7;
Jiri Slaby1c456072008-02-07 00:16:46 -0800671 break;
672 case CS8:
Jiri Slaby2c218322021-06-18 08:14:43 +0200673 cval = UART_LCR_WLEN8;
Jiri Slaby1c456072008-02-07 00:16:46 -0800674 break;
Jiri Slaby1c456072008-02-07 00:16:46 -0800675 }
Jiri Slaby2c218322021-06-18 08:14:43 +0200676
Jiri Slaby1c456072008-02-07 00:16:46 -0800677 if (cflag & CSTOPB)
Jiri Slaby2c218322021-06-18 08:14:43 +0200678 cval |= UART_LCR_STOP;
Jiri Slaby1c456072008-02-07 00:16:46 -0800679 if (cflag & PARENB)
680 cval |= UART_LCR_PARITY;
681 if (!(cflag & PARODD))
682 cval |= UART_LCR_EPAR;
683 if (cflag & CMSPAR)
684 cval |= UART_LCR_SPAR;
685
686 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
Jiri Slaby292955a2021-06-18 08:14:13 +0200687 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800688 fcr = UART_FCR_ENABLE_FIFO;
689 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700690 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800691 } else
692 fcr = 0;
693 } else {
694 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slaby292955a2021-06-18 08:14:13 +0200695 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800696 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700697 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800698 } else {
Jiri Slabydc33f642021-06-18 08:14:11 +0200699 switch (info->rx_high_water) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800700 case 1:
701 fcr |= UART_FCR_TRIGGER_1;
702 break;
703 case 4:
704 fcr |= UART_FCR_TRIGGER_4;
705 break;
706 case 8:
707 fcr |= UART_FCR_TRIGGER_8;
708 break;
709 default:
710 fcr |= UART_FCR_TRIGGER_14;
711 break;
712 }
713 }
714 }
715
716 /* CTS flow control flag and modem status interrupts */
717 info->IER &= ~UART_IER_MSI;
718 info->MCR &= ~UART_MCR_AFE;
Peter Hurley5604a982016-04-09 17:53:21 -0700719 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
Jiri Slaby1c456072008-02-07 00:16:46 -0800720 if (cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800721 info->IER |= UART_IER_MSI;
Jiri Slaby292955a2021-06-18 08:14:13 +0200722 if ((info->type == PORT_16550A) || (info->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800723 info->MCR |= UART_MCR_AFE;
724 } else {
725 status = inb(info->ioaddr + UART_MSR);
Alan Cox216ba022008-10-13 10:40:19 +0100726 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800727 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100728 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800729 if (info->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200730 !info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800731 outb(info->IER & ~UART_IER_THRI,
732 info->ioaddr +
733 UART_IER);
734 info->IER |= UART_IER_THRI;
735 outb(info->IER, info->ioaddr +
736 UART_IER);
737 }
Alan Cox216ba022008-10-13 10:40:19 +0100738 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800739 }
740 } else {
741 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100742 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800743 if ((info->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200744 (!info->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800745 info->IER &= ~UART_IER_THRI;
746 outb(info->IER, info->ioaddr +
747 UART_IER);
748 }
749 }
750 }
751 }
Jiri Slaby1c456072008-02-07 00:16:46 -0800752 }
753 outb(info->MCR, info->ioaddr + UART_MCR);
Peter Hurley2d686552016-04-09 17:53:23 -0700754 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
755 if (~cflag & CLOCAL)
Jiri Slaby1c456072008-02-07 00:16:46 -0800756 info->IER |= UART_IER_MSI;
Jiri Slaby1c456072008-02-07 00:16:46 -0800757 outb(info->IER, info->ioaddr + UART_IER);
758
759 /*
760 * Set up parity check flag
761 */
762 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox216ba022008-10-13 10:40:19 +0100763 if (I_INPCK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800764 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox216ba022008-10-13 10:40:19 +0100765 if (I_BRKINT(tty) || I_PARMRK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800766 info->read_status_mask |= UART_LSR_BI;
767
768 info->ignore_status_mask = 0;
769
Alan Cox216ba022008-10-13 10:40:19 +0100770 if (I_IGNBRK(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800771 info->ignore_status_mask |= UART_LSR_BI;
772 info->read_status_mask |= UART_LSR_BI;
773 /*
774 * If we're ignore parity and break indicators, ignore
775 * overruns too. (For real raw support).
776 */
Alan Cox216ba022008-10-13 10:40:19 +0100777 if (I_IGNPAR(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800778 info->ignore_status_mask |=
779 UART_LSR_OE |
780 UART_LSR_PE |
781 UART_LSR_FE;
782 info->read_status_mask |=
783 UART_LSR_OE |
784 UART_LSR_PE |
785 UART_LSR_FE;
786 }
787 }
Jiri Slaby292955a2021-06-18 08:14:13 +0200788 if (info->board->must_hwid) {
Alan Cox216ba022008-10-13 10:40:19 +0100789 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
790 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
791 if (I_IXON(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700792 mxser_enable_must_rx_software_flow_control(
793 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800794 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700795 mxser_disable_must_rx_software_flow_control(
796 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800797 }
Alan Cox216ba022008-10-13 10:40:19 +0100798 if (I_IXOFF(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700799 mxser_enable_must_tx_software_flow_control(
800 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800801 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700802 mxser_disable_must_tx_software_flow_control(
803 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800804 }
805 }
806
807
808 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
809 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby1c456072008-02-07 00:16:46 -0800810}
811
Alan Cox216ba022008-10-13 10:40:19 +0100812static void mxser_check_modem_status(struct tty_struct *tty,
813 struct mxser_port *port, int status)
Jiri Slaby1c456072008-02-07 00:16:46 -0800814{
815 /* update input line counters */
816 if (status & UART_MSR_TERI)
817 port->icount.rng++;
818 if (status & UART_MSR_DDSR)
819 port->icount.dsr++;
820 if (status & UART_MSR_DDCD)
821 port->icount.dcd++;
822 if (status & UART_MSR_DCTS)
823 port->icount.cts++;
Alan Coxbdc04e32009-09-19 13:13:31 -0700824 wake_up_interruptible(&port->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800825
Peter Hurley2d686552016-04-09 17:53:23 -0700826 if (tty_port_check_carrier(&port->port) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800827 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100828 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800829 }
830
Huang Shijief21ec3d2012-08-22 22:13:36 -0400831 if (tty_port_cts_enabled(&port->port)) {
Alan Cox216ba022008-10-13 10:40:19 +0100832 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800833 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100834 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800835
836 if ((port->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200837 (!port->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800838 outb(port->IER & ~UART_IER_THRI,
839 port->ioaddr + UART_IER);
840 port->IER |= UART_IER_THRI;
841 outb(port->IER, port->ioaddr +
842 UART_IER);
843 }
Alan Cox216ba022008-10-13 10:40:19 +0100844 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800845 }
846 } else {
847 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100848 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800849 if (port->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200850 !port->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800851 port->IER &= ~UART_IER_THRI;
852 outb(port->IER, port->ioaddr +
853 UART_IER);
854 }
855 }
856 }
857 }
858}
859
Alan Cox67691402009-11-30 13:17:35 +0000860static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800861{
Alan Cox67691402009-11-30 13:17:35 +0000862 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800863 unsigned long page;
864 unsigned long flags;
865
866 page = __get_free_page(GFP_KERNEL);
867 if (!page)
868 return -ENOMEM;
869
870 spin_lock_irqsave(&info->slock, flags);
871
Jiri Slaby1c456072008-02-07 00:16:46 -0800872 if (!info->ioaddr || !info->type) {
Alan Cox216ba022008-10-13 10:40:19 +0100873 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800874 free_page(page);
875 spin_unlock_irqrestore(&info->slock, flags);
876 return 0;
877 }
Alan Cox67691402009-11-30 13:17:35 +0000878 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800879
880 /*
881 * Clear the FIFO buffers and disable them
882 * (they will be reenabled in mxser_change_speed())
883 */
Jiri Slaby292955a2021-06-18 08:14:13 +0200884 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800885 outb((UART_FCR_CLEAR_RCVR |
886 UART_FCR_CLEAR_XMIT |
887 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
888 else
889 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
890 info->ioaddr + UART_FCR);
891
892 /*
893 * At this point there's no way the LSR could still be 0xFF;
894 * if it is, then bail out, because there's likely no UART
895 * here.
896 */
897 if (inb(info->ioaddr + UART_LSR) == 0xff) {
898 spin_unlock_irqrestore(&info->slock, flags);
899 if (capable(CAP_SYS_ADMIN)) {
Jiri Slabyf43a510d2009-12-09 12:34:15 -0800900 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800901 return 0;
902 } else
903 return -ENODEV;
904 }
905
906 /*
907 * Clear the interrupt registers.
908 */
909 (void) inb(info->ioaddr + UART_LSR);
910 (void) inb(info->ioaddr + UART_RX);
911 (void) inb(info->ioaddr + UART_IIR);
912 (void) inb(info->ioaddr + UART_MSR);
913
914 /*
915 * Now, initialize the UART
916 */
917 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
918 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
919 outb(info->MCR, info->ioaddr + UART_MCR);
920
921 /*
922 * Finally, enable interrupts
923 */
924 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
925
Jiri Slaby292955a2021-06-18 08:14:13 +0200926 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800927 info->IER |= MOXA_MUST_IER_EGDAI;
928 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
929
930 /*
931 * And clear the interrupt registers again for luck.
932 */
933 (void) inb(info->ioaddr + UART_LSR);
934 (void) inb(info->ioaddr + UART_RX);
935 (void) inb(info->ioaddr + UART_IIR);
936 (void) inb(info->ioaddr + UART_MSR);
937
Alan Cox216ba022008-10-13 10:40:19 +0100938 clear_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800939 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
940
941 /*
942 * and set the speed of the serial port
943 */
Jiri Slaby27997072017-09-12 12:39:55 +0200944 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800945 spin_unlock_irqrestore(&info->slock, flags);
946
947 return 0;
948}
949
950/*
Alan Cox67691402009-11-30 13:17:35 +0000951 * This routine will shutdown a serial port
Jiri Slaby1c456072008-02-07 00:16:46 -0800952 */
Alan Cox67691402009-11-30 13:17:35 +0000953static void mxser_shutdown_port(struct tty_port *port)
Jiri Slaby1c456072008-02-07 00:16:46 -0800954{
Alan Cox67691402009-11-30 13:17:35 +0000955 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800956 unsigned long flags;
957
Jiri Slaby1c456072008-02-07 00:16:46 -0800958 spin_lock_irqsave(&info->slock, flags);
959
960 /*
961 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
962 * here so the queue might never be waken up
963 */
Alan Coxbdc04e32009-09-19 13:13:31 -0700964 wake_up_interruptible(&info->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800965
966 /*
Alan Cox67691402009-11-30 13:17:35 +0000967 * Free the xmit buffer, if necessary
Jiri Slaby1c456072008-02-07 00:16:46 -0800968 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100969 if (info->port.xmit_buf) {
970 free_page((unsigned long) info->port.xmit_buf);
971 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -0800972 }
973
974 info->IER = 0;
975 outb(0x00, info->ioaddr + UART_IER);
976
Jiri Slaby1c456072008-02-07 00:16:46 -0800977 /* clear Rx/Tx FIFO's */
Jiri Slaby292955a2021-06-18 08:14:13 +0200978 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800979 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
980 MOXA_MUST_FCR_GDA_MODE_ENABLE,
981 info->ioaddr + UART_FCR);
982 else
983 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
984 info->ioaddr + UART_FCR);
985
986 /* read data port to reset things */
987 (void) inb(info->ioaddr + UART_RX);
988
Jiri Slaby1c456072008-02-07 00:16:46 -0800989
Jiri Slaby292955a2021-06-18 08:14:13 +0200990 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800991 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
992
993 spin_unlock_irqrestore(&info->slock, flags);
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/*
997 * This routine is called whenever a serial port is opened. It
998 * enables interrupts for a serial port, linking in its async structure into
999 * the IRQ chain. It also performs the serial-specific
1000 * initialization for the tty structure.
1001 */
1002static int mxser_open(struct tty_struct *tty, struct file *filp)
1003{
Jiri Slaby1c456072008-02-07 00:16:46 -08001004 struct mxser_port *info;
Jiri Slaby389fc822021-06-18 08:14:38 +02001005 int line = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Jiri Slaby1c456072008-02-07 00:16:46 -08001007 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1008 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001009 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Alan Coxa2d1e352010-04-23 16:01:18 +01001011 tty->driver_data = info;
Alan Cox67691402009-11-30 13:17:35 +00001012 return tty_port_open(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Alan Cox978e5952008-04-30 00:53:59 -07001015static void mxser_flush_buffer(struct tty_struct *tty)
1016{
1017 struct mxser_port *info = tty->driver_data;
1018 char fcr;
1019 unsigned long flags;
1020
1021
1022 spin_lock_irqsave(&info->slock, flags);
1023 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1024
1025 fcr = inb(info->ioaddr + UART_FCR);
1026 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1027 info->ioaddr + UART_FCR);
1028 outb(fcr, info->ioaddr + UART_FCR);
1029
1030 spin_unlock_irqrestore(&info->slock, flags);
1031
1032 tty_wakeup(tty);
1033}
1034
1035
Alan Cox67691402009-11-30 13:17:35 +00001036static void mxser_close_port(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Alan Cox1e2b0252009-09-19 13:13:21 -07001038 struct mxser_port *info = container_of(port, struct mxser_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * At this point we stop accepting input. To do this, we
1042 * disable the receive line status interrupts, and tell the
1043 * interrupt driver to stop checking the data ready bit in the
1044 * line status register.
1045 */
1046 info->IER &= ~UART_IER_RLSI;
Jiri Slaby292955a2021-06-18 08:14:13 +02001047 if (info->board->must_hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001049
Alan Cox67691402009-11-30 13:17:35 +00001050 outb(info->IER, info->ioaddr + UART_IER);
1051 /*
1052 * Before we drop DTR, make sure the UART transmitter
1053 * has completely drained; this is especially
1054 * important if there is a transmit FIFO!
1055 */
1056 timeout = jiffies + HZ;
1057 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1058 schedule_timeout_interruptible(5);
1059 if (time_after(jiffies, timeout))
1060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Alan Cox1e2b0252009-09-19 13:13:21 -07001062}
1063
1064/*
1065 * This routine is called when the serial port gets closed. First, we
1066 * wait for the last remaining data to be sent. Then, we unlink its
1067 * async structure from the interrupt chain if necessary, and we free
1068 * that IRQ if nothing is left in the chain.
1069 */
1070static void mxser_close(struct tty_struct *tty, struct file *filp)
1071{
1072 struct mxser_port *info = tty->driver_data;
1073 struct tty_port *port = &info->port;
1074
Jiri Slaby389fc822021-06-18 08:14:38 +02001075 if (info == NULL)
Alan Cox1e2b0252009-09-19 13:13:21 -07001076 return;
1077 if (tty_port_close_start(port, tty, filp) == 0)
1078 return;
Peter Hurleycd7b4b32016-01-10 14:51:38 -08001079 info->closing = 1;
Alan Cox67691402009-11-30 13:17:35 +00001080 mutex_lock(&port->mutex);
1081 mxser_close_port(port);
Alan Cox1e2b0252009-09-19 13:13:21 -07001082 mxser_flush_buffer(tty);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001083 if (tty_port_initialized(port) && C_HUPCL(tty))
1084 tty_port_lower_dtr_rts(port);
Alan Cox67691402009-11-30 13:17:35 +00001085 mxser_shutdown_port(port);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001086 tty_port_set_initialized(port, 0);
Alan Cox67691402009-11-30 13:17:35 +00001087 mutex_unlock(&port->mutex);
Peter Hurleycd7b4b32016-01-10 14:51:38 -08001088 info->closing = 0;
Alan Coxa6614992009-01-02 13:46:50 +00001089 /* Right now the tty_port set is done outside of the close_end helper
1090 as we don't yet have everyone using refcounts */
1091 tty_port_close_end(port, tty);
1092 tty_port_tty_set(port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1096{
1097 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001098 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 unsigned long flags;
1100
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001101 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001105 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1106 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (c <= 0)
1108 break;
1109
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001110 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001112 info->xmit_head = (info->xmit_head + c) &
1113 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 info->xmit_cnt += c;
1115 spin_unlock_irqrestore(&info->slock, flags);
1116
1117 buf += c;
1118 count -= c;
1119 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001122 if (info->xmit_cnt && !tty->flow.stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001123 if (!tty->hw_stopped ||
1124 (info->type == PORT_16550A) ||
Jiri Slaby292955a2021-06-18 08:14:13 +02001125 (info->board->must_hwid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001127 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1128 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001130 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 spin_unlock_irqrestore(&info->slock, flags);
1132 }
1133 }
1134 return total;
1135}
1136
Alan Cox0be2ead2008-04-30 00:54:03 -07001137static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jiri Slaby1c456072008-02-07 00:16:46 -08001139 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 unsigned long flags;
1141
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001142 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001149 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1151 info->xmit_cnt++;
1152 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001153 if (!tty->flow.stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001154 if (!tty->hw_stopped ||
1155 (info->type == PORT_16550A) ||
Jiri Slaby292955a2021-06-18 08:14:13 +02001156 info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001158 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001160 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_unlock_irqrestore(&info->slock, flags);
1162 }
1163 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001164 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167
1168static void mxser_flush_chars(struct tty_struct *tty)
1169{
Jiri Slaby1c456072008-02-07 00:16:46 -08001170 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 unsigned long flags;
1172
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001173 if (info->xmit_cnt <= 0 || tty->flow.stopped || !info->port.xmit_buf ||
Jiri Slabyace7dd92008-07-25 01:48:22 -07001174 (tty->hw_stopped && info->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +02001175 !info->board->must_hwid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return;
1177
1178 spin_lock_irqsave(&info->slock, flags);
1179
Jiri Slaby1c456072008-02-07 00:16:46 -08001180 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001182 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 spin_unlock_irqrestore(&info->slock, flags);
1185}
1186
Jiri Slaby03b3b1a2021-05-05 11:19:15 +02001187static unsigned int mxser_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Jiri Slaby1c456072008-02-07 00:16:46 -08001189 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int ret;
1191
1192 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
Jiri Slabyace7dd92008-07-25 01:48:22 -07001193 return ret < 0 ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Jiri Slabyfff4ef12021-05-05 11:19:19 +02001196static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Jiri Slaby1c456072008-02-07 00:16:46 -08001198 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return info->xmit_cnt;
1200}
1201
Jiri Slaby1c456072008-02-07 00:16:46 -08001202/*
1203 * ------------------------------------------------------------
1204 * friends of mxser_ioctl()
1205 * ------------------------------------------------------------
1206 */
Alan Cox216ba022008-10-13 10:40:19 +01001207static int mxser_get_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001208 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Alan Cox216ba022008-10-13 10:40:19 +01001210 struct mxser_port *info = tty->driver_data;
Al Viro6da5b582018-09-11 22:22:06 -04001211 struct tty_port *port = &info->port;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001212 unsigned int closing_wait, close_delay;
Al Viro6da5b582018-09-11 22:22:06 -04001213
Al Viro6da5b582018-09-11 22:22:06 -04001214 mutex_lock(&port->mutex);
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001215
1216 close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
1217 closing_wait = info->port.closing_wait;
1218 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1219 closing_wait = jiffies_to_msecs(closing_wait) / 10;
1220
Al Viro6da5b582018-09-11 22:22:06 -04001221 ss->type = info->type,
1222 ss->line = tty->index,
1223 ss->port = info->ioaddr,
1224 ss->irq = info->board->irq,
1225 ss->flags = info->port.flags,
Jiri Slabya6970c32021-06-18 08:14:41 +02001226 ss->baud_base = MXSER_BAUD_BASE,
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001227 ss->close_delay = close_delay;
1228 ss->closing_wait = closing_wait;
Jiri Slabyd811b262021-06-18 08:14:42 +02001229 ss->custom_divisor = MXSER_CUSTOM_DIVISOR,
Al Viro6da5b582018-09-11 22:22:06 -04001230 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001231 return 0;
1232}
1233
Alan Cox216ba022008-10-13 10:40:19 +01001234static int mxser_set_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001235 struct serial_struct *ss)
Jiri Slaby1c456072008-02-07 00:16:46 -08001236{
Alan Cox216ba022008-10-13 10:40:19 +01001237 struct mxser_port *info = tty->driver_data;
Alan Cox07f86c02009-11-30 13:17:41 +00001238 struct tty_port *port = &info->port;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001239 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001240 unsigned long sl_flags;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001241 unsigned int flags, close_delay, closing_wait;
Jiri Slaby1c456072008-02-07 00:16:46 -08001242 int retval = 0;
1243
Al Viro6da5b582018-09-11 22:22:06 -04001244 if (tty_io_error(tty))
1245 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08001246
Al Viro6da5b582018-09-11 22:22:06 -04001247 mutex_lock(&port->mutex);
1248 if (!info->ioaddr) {
1249 mutex_unlock(&port->mutex);
1250 return -ENODEV;
1251 }
1252
1253 if (ss->irq != info->board->irq ||
1254 ss->port != info->ioaddr) {
1255 mutex_unlock(&port->mutex);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001256 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001257 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001258
Alan Cox07f86c02009-11-30 13:17:41 +00001259 flags = port->flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001260
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001261 close_delay = msecs_to_jiffies(ss->close_delay * 10);
1262 closing_wait = ss->closing_wait;
1263 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1264 closing_wait = msecs_to_jiffies(closing_wait * 10);
1265
Jiri Slaby1c456072008-02-07 00:16:46 -08001266 if (!capable(CAP_SYS_ADMIN)) {
Jiri Slabya6970c32021-06-18 08:14:41 +02001267 if ((ss->baud_base != MXSER_BAUD_BASE) ||
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001268 (close_delay != info->port.close_delay) ||
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001269 (closing_wait != info->port.closing_wait) ||
Al Viro6da5b582018-09-11 22:22:06 -04001270 ((ss->flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK))) {
1271 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001272 return -EPERM;
Al Viro6da5b582018-09-11 22:22:06 -04001273 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001274 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Al Viro6da5b582018-09-11 22:22:06 -04001275 (ss->flags & ASYNC_USR_MASK));
Jiri Slaby1c456072008-02-07 00:16:46 -08001276 } else {
1277 /*
1278 * OK, past this point, all the error checking has been done.
1279 * At this point, we start making changes.....
1280 */
Alan Cox07f86c02009-11-30 13:17:41 +00001281 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Al Viro6da5b582018-09-11 22:22:06 -04001282 (ss->flags & ASYNC_FLAGS));
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001283 port->close_delay = close_delay;
1284 port->closing_wait = closing_wait;
Alan Cox07f86c02009-11-30 13:17:41 +00001285 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Jiri Slabya6970c32021-06-18 08:14:41 +02001286 (ss->baud_base != MXSER_BAUD_BASE ||
Al Viro6da5b582018-09-11 22:22:06 -04001287 ss->custom_divisor !=
Jiri Slabyd811b262021-06-18 08:14:42 +02001288 MXSER_CUSTOM_DIVISOR)) {
Al Viro6da5b582018-09-11 22:22:06 -04001289 if (ss->custom_divisor == 0) {
1290 mutex_unlock(&port->mutex);
Alan Cox07f86c02009-11-30 13:17:41 +00001291 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001292 }
1293 baud = ss->baud_base / ss->custom_divisor;
Alan Cox216ba022008-10-13 10:40:19 +01001294 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001295 }
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001296
1297 info->type = ss->type;
1298
1299 process_txrx_fifo(info);
Jiri Slaby1c456072008-02-07 00:16:46 -08001300 }
1301
Peter Hurleyd41861c2016-04-09 17:53:25 -07001302 if (tty_port_initialized(port)) {
Alan Cox07f86c02009-11-30 13:17:41 +00001303 if (flags != (port->flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001304 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slaby27997072017-09-12 12:39:55 +02001305 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001306 spin_unlock_irqrestore(&info->slock, sl_flags);
1307 }
Alan Cox67691402009-11-30 13:17:35 +00001308 } else {
Alan Cox07f86c02009-11-30 13:17:41 +00001309 retval = mxser_activate(port, tty);
Alan Cox67691402009-11-30 13:17:35 +00001310 if (retval == 0)
Peter Hurleyd41861c2016-04-09 17:53:25 -07001311 tty_port_set_initialized(port, 1);
Alan Cox67691402009-11-30 13:17:35 +00001312 }
Al Viro6da5b582018-09-11 22:22:06 -04001313 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001314 return retval;
1315}
1316
1317/*
1318 * mxser_get_lsr_info - get line status register info
1319 *
1320 * Purpose: Let user call ioctl() to get info when the UART physically
1321 * is emptied. On bus types like RS485, the transmitter must
1322 * release the bus after transmitting. This must be done when
1323 * the transmit shift register is empty, not be done when the
1324 * transmit holding register is empty. This functionality
1325 * allows an RS485 driver to be written in user space.
1326 */
1327static int mxser_get_lsr_info(struct mxser_port *info,
1328 unsigned int __user *value)
1329{
1330 unsigned char status;
1331 unsigned int result;
1332 unsigned long flags;
1333
1334 spin_lock_irqsave(&info->slock, flags);
1335 status = inb(info->ioaddr + UART_LSR);
1336 spin_unlock_irqrestore(&info->slock, flags);
1337 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1338 return put_user(result, value);
1339}
1340
Alan Cox60b33c12011-02-14 16:26:14 +00001341static int mxser_tiocmget(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -08001342{
1343 struct mxser_port *info = tty->driver_data;
1344 unsigned char control, status;
1345 unsigned long flags;
1346
Peter Hurley18900ca2016-04-09 17:06:48 -07001347 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001348 return -EIO;
1349
1350 control = info->MCR;
1351
1352 spin_lock_irqsave(&info->slock, flags);
1353 status = inb(info->ioaddr + UART_MSR);
1354 if (status & UART_MSR_ANY_DELTA)
Alan Cox216ba022008-10-13 10:40:19 +01001355 mxser_check_modem_status(tty, info, status);
Jiri Slaby1c456072008-02-07 00:16:46 -08001356 spin_unlock_irqrestore(&info->slock, flags);
1357 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1358 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1359 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1360 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1361 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1362 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1363}
1364
Alan Cox20b9d172011-02-14 16:26:50 +00001365static int mxser_tiocmset(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001366 unsigned int set, unsigned int clear)
1367{
1368 struct mxser_port *info = tty->driver_data;
1369 unsigned long flags;
1370
Peter Hurley18900ca2016-04-09 17:06:48 -07001371 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001372 return -EIO;
1373
1374 spin_lock_irqsave(&info->slock, flags);
1375
1376 if (set & TIOCM_RTS)
1377 info->MCR |= UART_MCR_RTS;
1378 if (set & TIOCM_DTR)
1379 info->MCR |= UART_MCR_DTR;
1380
1381 if (clear & TIOCM_RTS)
1382 info->MCR &= ~UART_MCR_RTS;
1383 if (clear & TIOCM_DTR)
1384 info->MCR &= ~UART_MCR_DTR;
1385
1386 outb(info->MCR, info->ioaddr + UART_MCR);
1387 spin_unlock_irqrestore(&info->slock, flags);
1388 return 0;
1389}
1390
Jiri Slaby1c456072008-02-07 00:16:46 -08001391static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1392 struct async_icount *cprev)
1393{
1394 struct async_icount cnow;
1395 unsigned long flags;
1396 int ret;
1397
1398 spin_lock_irqsave(&info->slock, flags);
1399 cnow = info->icount; /* atomic copy */
1400 spin_unlock_irqrestore(&info->slock, flags);
1401
1402 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1403 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1404 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1405 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1406
1407 *cprev = cnow;
1408
1409 return ret;
1410}
1411
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001412/* We should likely switch to TIOCGRS485/TIOCSRS485. */
1413static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set,
1414 int __user *u_opmode)
1415{
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001416 int opmode, p = index % 4;
1417 int shiftbit = p * 2;
Jiri Slaby238d1172021-06-18 08:14:45 +02001418 u8 val;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001419
1420 if (port->board->must_hwid != MOXA_MUST_MU860_HWID)
1421 return -EFAULT;
1422
1423 if (set) {
1424 if (get_user(opmode, u_opmode))
1425 return -EFAULT;
1426
Jiri Slaby238d1172021-06-18 08:14:45 +02001427 if (opmode & ~OP_MODE_MASK)
1428 return -EINVAL;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001429
1430 spin_lock_irq(&port->slock);
1431 val = inb(port->opmode_ioaddr);
Jiri Slaby238d1172021-06-18 08:14:45 +02001432 val &= ~(OP_MODE_MASK << shiftbit);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001433 val |= (opmode << shiftbit);
1434 outb(val, port->opmode_ioaddr);
1435 spin_unlock_irq(&port->slock);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001436
Jiri Slaby238d1172021-06-18 08:14:45 +02001437 return 0;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001438 }
1439
Jiri Slaby238d1172021-06-18 08:14:45 +02001440 spin_lock_irq(&port->slock);
1441 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1442 spin_unlock_irq(&port->slock);
1443
1444 return put_user(opmode & OP_MODE_MASK, u_opmode);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001445}
1446
Alan Cox6caa76b2011-02-14 16:27:22 +00001447static int mxser_ioctl(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001448 unsigned int cmd, unsigned long arg)
1449{
1450 struct mxser_port *info = tty->driver_data;
1451 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 unsigned long flags;
1453 void __user *argp = (void __user *)arg;
1454
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001455 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE)
1456 return mxser_ioctl_op_mode(info, tty->index,
1457 cmd == MOXA_SET_OP_MODE, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Al Viro6da5b582018-09-11 22:22:06 -04001459 if (cmd != TIOCMIWAIT && tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001460 return -EIO;
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001464 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /*
1466 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1467 * - mask passed in arg for lines of interest
1468 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1469 * Caller should use TIOCGICOUNT to see which one it was
1470 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001471 case TIOCMIWAIT:
1472 spin_lock_irqsave(&info->slock, flags);
1473 cnow = info->icount; /* note the counters on entry */
1474 spin_unlock_irqrestore(&info->slock, flags);
1475
Alan Coxbdc04e32009-09-19 13:13:31 -07001476 return wait_event_interruptible(info->port.delta_msr_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001477 mxser_cflags_changed(info, arg, &cnow));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 default:
1479 return -ENOIOCTLCMD;
1480 }
1481 return 0;
1482}
1483
Alan Cox05871022010-09-16 18:21:52 +01001484 /*
1485 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1486 * Return: write counters to the user passed counter struct
1487 * NB: both 1->0 and 0->1 transitions are counted except for
1488 * RI where only 0->1 is counted.
1489 */
1490
1491static int mxser_get_icount(struct tty_struct *tty,
1492 struct serial_icounter_struct *icount)
1493
1494{
1495 struct mxser_port *info = tty->driver_data;
1496 struct async_icount cnow;
1497 unsigned long flags;
1498
1499 spin_lock_irqsave(&info->slock, flags);
1500 cnow = info->icount;
1501 spin_unlock_irqrestore(&info->slock, flags);
1502
1503 icount->frame = cnow.frame;
1504 icount->brk = cnow.brk;
1505 icount->overrun = cnow.overrun;
1506 icount->buf_overrun = cnow.buf_overrun;
1507 icount->parity = cnow.parity;
1508 icount->rx = cnow.rx;
1509 icount->tx = cnow.tx;
1510 icount->cts = cnow.cts;
1511 icount->dsr = cnow.dsr;
1512 icount->rng = cnow.rng;
1513 icount->dcd = cnow.dcd;
1514 return 0;
1515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517static void mxser_stoprx(struct tty_struct *tty)
1518{
Jiri Slaby1c456072008-02-07 00:16:46 -08001519 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 info->ldisc_stop_rx = 1;
1522 if (I_IXOFF(tty)) {
Jiri Slaby292955a2021-06-18 08:14:13 +02001523 if (info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001525 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001528 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001530 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 }
1533
Peter Hurley9db276f2016-01-10 20:36:15 -08001534 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001535 info->MCR &= ~UART_MCR_RTS;
1536 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538}
1539
1540/*
1541 * This routine is called by the upper-layer tty layer to signal that
1542 * incoming characters should be throttled.
1543 */
1544static void mxser_throttle(struct tty_struct *tty)
1545{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549static void mxser_unthrottle(struct tty_struct *tty)
1550{
Jiri Slaby1c456072008-02-07 00:16:46 -08001551 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001552
Jiri Slaby1c456072008-02-07 00:16:46 -08001553 /* startrx */
1554 info->ldisc_stop_rx = 0;
1555 if (I_IXOFF(tty)) {
1556 if (info->x_char)
1557 info->x_char = 0;
1558 else {
Jiri Slaby292955a2021-06-18 08:14:13 +02001559 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001560 info->IER |= MOXA_MUST_RECV_ISR;
1561 outb(info->IER, info->ioaddr + UART_IER);
1562 } else {
1563 info->x_char = START_CHAR(tty);
1564 outb(0, info->ioaddr + UART_IER);
1565 info->IER |= UART_IER_THRI;
1566 outb(info->IER, info->ioaddr + UART_IER);
1567 }
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570
Peter Hurley9db276f2016-01-10 20:36:15 -08001571 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001572 info->MCR |= UART_MCR_RTS;
1573 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575}
1576
1577/*
1578 * mxser_stop() and mxser_start()
1579 *
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001580 * This routines are called before setting or resetting tty->flow.stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * They enable or disable transmitter interrupts, as necessary.
1582 */
1583static void mxser_stop(struct tty_struct *tty)
1584{
Jiri Slaby1c456072008-02-07 00:16:46 -08001585 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 unsigned long flags;
1587
1588 spin_lock_irqsave(&info->slock, flags);
1589 if (info->IER & UART_IER_THRI) {
1590 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001591 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 spin_unlock_irqrestore(&info->slock, flags);
1594}
1595
1596static void mxser_start(struct tty_struct *tty)
1597{
Jiri Slaby1c456072008-02-07 00:16:46 -08001598 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 unsigned long flags;
1600
1601 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001602 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001603 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001605 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607 spin_unlock_irqrestore(&info->slock, flags);
1608}
1609
Jiri Slaby1c456072008-02-07 00:16:46 -08001610static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1611{
1612 struct mxser_port *info = tty->driver_data;
1613 unsigned long flags;
1614
1615 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby27997072017-09-12 12:39:55 +02001616 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001617 spin_unlock_irqrestore(&info->slock, flags);
1618
Peter Hurley9db276f2016-01-10 20:36:15 -08001619 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001620 tty->hw_stopped = 0;
1621 mxser_start(tty);
1622 }
1623
1624 /* Handle sw stopped */
Peter Hurley9db276f2016-01-10 20:36:15 -08001625 if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001626 tty->flow.stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001627
Jiri Slaby292955a2021-06-18 08:14:13 +02001628 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001629 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07001630 mxser_disable_must_rx_software_flow_control(
1631 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08001632 spin_unlock_irqrestore(&info->slock, flags);
1633 }
1634
1635 mxser_start(tty);
1636 }
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/*
1640 * mxser_wait_until_sent() --- wait until the transmitter is empty
1641 */
1642static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1643{
Jiri Slaby1c456072008-02-07 00:16:46 -08001644 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 unsigned long orig_jiffies, char_time;
Alan Cox07f86c02009-11-30 13:17:41 +00001646 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int lsr;
1648
1649 if (info->type == PORT_UNKNOWN)
1650 return;
1651
1652 if (info->xmit_fifo_size == 0)
1653 return; /* Just in case.... */
1654
1655 orig_jiffies = jiffies;
1656 /*
1657 * Set the check interval to be 1/5 of the estimated time to
1658 * send a single character, and make it at least 1. The check
1659 * interval should also be less than the timeout.
1660 *
1661 * Note: we have to use pretty tight timings here to satisfy
1662 * the NIST-PCTS.
1663 */
1664 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1665 char_time = char_time / 5;
1666 if (char_time == 0)
1667 char_time = 1;
1668 if (timeout && timeout < char_time)
1669 char_time = timeout;
1670 /*
1671 * If the transmitter hasn't cleared in twice the approximate
1672 * amount of time to send the entire FIFO, it probably won't
1673 * ever clear. This assumes the UART isn't doing flow
1674 * control, which is currently the case. Hence, if it ever
1675 * takes longer than info->timeout, this is probably due to a
1676 * UART bug of some kind. So, we clamp the timeout parameter at
1677 * 2*info->timeout.
1678 */
1679 if (!timeout || timeout > 2 * info->timeout)
1680 timeout = 2 * info->timeout;
Jiri Slaby8bab5342011-07-14 14:35:15 +02001681
Alan Cox07f86c02009-11-30 13:17:41 +00001682 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001683 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Alan Cox07f86c02009-11-30 13:17:41 +00001684 spin_unlock_irqrestore(&info->slock, flags);
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001685 schedule_timeout_interruptible(char_time);
Alan Cox07f86c02009-11-30 13:17:41 +00001686 spin_lock_irqsave(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (signal_pending(current))
1688 break;
1689 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1690 break;
1691 }
Alan Cox07f86c02009-11-30 13:17:41 +00001692 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696/*
1697 * This routine is called by tty_hangup() when a hangup is signaled.
1698 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001699static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Jiri Slaby1c456072008-02-07 00:16:46 -08001701 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 mxser_flush_buffer(tty);
Alan Cox3b6826b2009-01-02 13:45:58 +00001704 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707/*
1708 * mxser_rs_break() --- routine which turns the break handling on or off
1709 */
Alan Cox9e98966c2008-07-22 11:18:03 +01001710static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Jiri Slaby1c456072008-02-07 00:16:46 -08001712 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 unsigned long flags;
1714
1715 spin_lock_irqsave(&info->slock, flags);
1716 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08001717 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1718 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 else
Jiri Slaby1c456072008-02-07 00:16:46 -08001720 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1721 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e98966c2008-07-22 11:18:03 +01001723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001726static bool mxser_receive_chars_new(struct tty_struct *tty,
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001727 struct mxser_port *port, u8 status)
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001728{
1729 enum mxser_must_hwid hwid = port->board->must_hwid;
1730 u8 gdl;
1731
1732 if (hwid == MOXA_OTHER_UART)
1733 return false;
Jiri Slaby70640052021-06-18 08:14:26 +02001734 if (status & UART_LSR_BRK_ERROR_BITS)
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001735 return false;
1736 if (hwid == MOXA_MUST_MU860_HWID && (status & MOXA_MUST_LSR_RERR))
1737 return false;
1738 if (status & MOXA_MUST_LSR_RERR)
1739 return false;
1740
1741 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
1742 if (hwid == MOXA_MUST_MU150_HWID)
1743 gdl &= MOXA_MUST_GDL_MASK;
1744
1745 if (gdl >= tty->receive_room && !port->ldisc_stop_rx)
1746 mxser_stoprx(tty);
1747
1748 while (gdl--) {
1749 u8 ch = inb(port->ioaddr + UART_RX);
1750 tty_insert_flip_char(&port->port, ch, 0);
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001751 }
1752
1753 return true;
1754}
1755
Jiri Slaby0c419422021-06-18 08:14:22 +02001756static u8 mxser_receive_chars_old(struct tty_struct *tty,
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001757 struct mxser_port *port, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Jiri Slaby0c419422021-06-18 08:14:22 +02001759 enum mxser_must_hwid hwid = port->board->must_hwid;
1760 int recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 int ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int max = 256;
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001763 int cnt = 0;
Jiri Slaby0c419422021-06-18 08:14:22 +02001764 u8 ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 do {
1767 if (max-- < 0)
1768 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Jiri Slaby1c456072008-02-07 00:16:46 -08001770 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby0c419422021-06-18 08:14:22 +02001771 if (hwid && (status & UART_LSR_OE))
Jiri Slaby1c456072008-02-07 00:16:46 -08001772 outb(0x23, port->ioaddr + UART_FCR);
Jiri Slaby15517802021-06-18 08:14:18 +02001773 status &= port->read_status_mask;
1774 if (status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (++ignored > 100)
1776 break;
1777 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001778 char flag = 0;
Jiri Slaby70640052021-06-18 08:14:26 +02001779 if (status & UART_LSR_BRK_ERROR_BITS) {
Jiri Slaby15517802021-06-18 08:14:18 +02001780 if (status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001781 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001782 port->icount.brk++;
1783
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001784 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 do_SAK(tty);
Jiri Slaby15517802021-06-18 08:14:18 +02001786 } else if (status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001787 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08001788 port->icount.parity++;
Jiri Slaby15517802021-06-18 08:14:18 +02001789 } else if (status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001790 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08001791 port->icount.frame++;
Jiri Slaby15517802021-06-18 08:14:18 +02001792 } else if (status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001793 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08001794 port->icount.overrun++;
Jiri Slaby6de6e5c2021-06-18 08:14:23 +02001795 }
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001796 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01001797 tty_insert_flip_char(&port->port, ch, flag);
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001798 cnt++;
1799 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001800 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 break;
1803 }
1804
1805 }
1806
Jiri Slaby0c419422021-06-18 08:14:22 +02001807 if (hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Jiri Slaby15517802021-06-18 08:14:18 +02001810 status = inb(port->ioaddr + UART_LSR);
1811 } while (status & UART_LSR_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Jiri Slaby0c419422021-06-18 08:14:22 +02001813 return status;
1814}
1815
1816static u8 mxser_receive_chars(struct tty_struct *tty,
1817 struct mxser_port *port, u8 status)
1818{
Jiri Slaby0c419422021-06-18 08:14:22 +02001819 if (tty->receive_room == 0 && !port->ldisc_stop_rx)
1820 mxser_stoprx(tty);
1821
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001822 if (!mxser_receive_chars_new(tty, port, status))
1823 status = mxser_receive_chars_old(tty, port, status);
Jiri Slaby0c419422021-06-18 08:14:22 +02001824
Jiri Slaby2e124b42013-01-03 15:53:06 +01001825 tty_flip_buffer_push(&port->port);
Jiri Slaby15517802021-06-18 08:14:18 +02001826
1827 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Alan Cox216ba022008-10-13 10:40:19 +01001830static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Jiri Slaby1c456072008-02-07 00:16:46 -08001834 if (port->x_char) {
1835 outb(port->x_char, port->ioaddr + UART_TX);
1836 port->x_char = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001837 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return;
1839 }
1840
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001841 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08001842 return;
1843
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001844 if (port->xmit_cnt <= 0 || tty->flow.stopped ||
Alan Cox216ba022008-10-13 10:40:19 +01001845 (tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08001846 (port->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +02001847 (!port->board->must_hwid))) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001848 port->IER &= ~UART_IER_THRI;
1849 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return;
1851 }
1852
Jiri Slaby1c456072008-02-07 00:16:46 -08001853 cnt = port->xmit_cnt;
1854 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001856 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08001857 port->ioaddr + UART_TX);
1858 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1859 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 break;
1861 } while (--count > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Jiri Slaby1c456072008-02-07 00:16:46 -08001863 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Kulikov Vasiliy464eb8f2010-07-23 20:34:53 +04001865 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox216ba022008-10-13 10:40:19 +01001866 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Jiri Slaby1c456072008-02-07 00:16:46 -08001868 if (port->xmit_cnt <= 0) {
1869 port->IER &= ~UART_IER_THRI;
1870 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001874static bool mxser_port_isr(struct mxser_port *port)
1875{
1876 struct tty_struct *tty;
1877 u8 iir, msr, status;
1878 bool error = false;
1879
1880 iir = inb(port->ioaddr + UART_IIR);
1881 if (iir & UART_IIR_NO_INT)
1882 return true;
1883
1884 iir &= MOXA_MUST_IIR_MASK;
1885 tty = tty_port_tty_get(&port->port);
1886 if (!tty || port->closing || !tty_port_initialized(&port->port)) {
1887 status = inb(port->ioaddr + UART_LSR);
1888 outb(0x27, port->ioaddr + UART_FCR);
1889 inb(port->ioaddr + UART_MSR);
1890
1891 error = true;
1892 goto put_tty;
1893 }
1894
1895 status = inb(port->ioaddr + UART_LSR);
1896
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001897 if (port->board->must_hwid) {
1898 if (iir == MOXA_MUST_IIR_GDA ||
1899 iir == MOXA_MUST_IIR_RDA ||
1900 iir == MOXA_MUST_IIR_RTO ||
1901 iir == MOXA_MUST_IIR_LSR)
1902 status = mxser_receive_chars(tty, port, status);
1903 } else {
1904 status &= port->read_status_mask;
1905 if (status & UART_LSR_DR)
1906 status = mxser_receive_chars(tty, port, status);
1907 }
1908
1909 msr = inb(port->ioaddr + UART_MSR);
1910 if (msr & UART_MSR_ANY_DELTA)
1911 mxser_check_modem_status(tty, port, msr);
1912
1913 if (port->board->must_hwid) {
1914 if (iir == 0x02 && (status & UART_LSR_THRE))
1915 mxser_transmit_chars(tty, port);
1916 } else {
1917 if (status & UART_LSR_THRE)
1918 mxser_transmit_chars(tty, port);
1919 }
1920
1921put_tty:
1922 tty_kref_put(tty);
1923
1924 return error;
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/*
Jiri Slaby1c456072008-02-07 00:16:46 -08001928 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001930static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Jiri Slabycef222c2021-06-18 08:14:17 +02001932 struct mxser_board *brd = dev_id;
Jiri Slaby1c456072008-02-07 00:16:46 -08001933 struct mxser_port *port;
Jiri Slaby1c456072008-02-07 00:16:46 -08001934 unsigned int int_cnt, pass_counter = 0;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001935 unsigned int i, max = brd->info->nports;
Jiri Slaby1c456072008-02-07 00:16:46 -08001936 int handled = IRQ_NONE;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001937 u8 irqbits, bits, mask = BIT(max) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jiri Slaby1c456072008-02-07 00:16:46 -08001939 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001940 irqbits = inb(brd->vector) & mask;
1941 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08001942 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Jiri Slaby1c456072008-02-07 00:16:46 -08001944 handled = IRQ_HANDLED;
1945 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001946 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08001947 break;
1948 if (bits & irqbits)
1949 continue;
1950 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Jiri Slaby1c456072008-02-07 00:16:46 -08001952 int_cnt = 0;
1953 spin_lock(&port->slock);
1954 do {
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001955 if (mxser_port_isr(port))
Jiri Slaby1c456072008-02-07 00:16:46 -08001956 break;
Jiri Slaby1c456072008-02-07 00:16:46 -08001957 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1958 spin_unlock(&port->slock);
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
Jiri Slaby1c456072008-02-07 00:16:46 -08001962 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Jiri Slaby1c456072008-02-07 00:16:46 -08001965static const struct tty_operations mxser_ops = {
1966 .open = mxser_open,
1967 .close = mxser_close,
1968 .write = mxser_write,
1969 .put_char = mxser_put_char,
1970 .flush_chars = mxser_flush_chars,
1971 .write_room = mxser_write_room,
1972 .chars_in_buffer = mxser_chars_in_buffer,
1973 .flush_buffer = mxser_flush_buffer,
1974 .ioctl = mxser_ioctl,
1975 .throttle = mxser_throttle,
1976 .unthrottle = mxser_unthrottle,
1977 .set_termios = mxser_set_termios,
1978 .stop = mxser_stop,
1979 .start = mxser_start,
1980 .hangup = mxser_hangup,
1981 .break_ctl = mxser_rs_break,
1982 .wait_until_sent = mxser_wait_until_sent,
1983 .tiocmget = mxser_tiocmget,
1984 .tiocmset = mxser_tiocmset,
Al Viro6da5b582018-09-11 22:22:06 -04001985 .set_serial = mxser_set_serial_info,
1986 .get_serial = mxser_get_serial_info,
Alan Cox05871022010-09-16 18:21:52 +01001987 .get_icount = mxser_get_icount,
Jiri Slaby1c456072008-02-07 00:16:46 -08001988};
1989
Aya Mahfouz04b757d2015-12-15 01:53:36 +02001990static const struct tty_port_operations mxser_port_ops = {
Alan Cox31f35932009-01-02 13:45:05 +00001991 .carrier_raised = mxser_carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01001992 .dtr_rts = mxser_dtr_rts,
Alan Cox67691402009-11-30 13:17:35 +00001993 .activate = mxser_activate,
1994 .shutdown = mxser_shutdown_port,
Alan Cox31f35932009-01-02 13:45:05 +00001995};
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997/*
Jiri Slaby1c456072008-02-07 00:16:46 -08001998 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002000
Jiri Slaby27997072017-09-12 12:39:55 +02002001static int mxser_initbrd(struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Jiri Slaby1c456072008-02-07 00:16:46 -08002003 struct mxser_port *info;
2004 unsigned int i;
2005 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Jiri Slaby1c456072008-02-07 00:16:46 -08002007 for (i = 0; i < brd->info->nports; i++) {
2008 info = &brd->ports[i];
Alan Cox44b7d1b2008-07-16 21:57:18 +01002009 tty_port_init(&info->port);
Alan Cox31f35932009-01-02 13:45:05 +00002010 info->port.ops = &mxser_port_ops;
Jiri Slaby1c456072008-02-07 00:16:46 -08002011 info->board = brd;
Jiri Slaby1c456072008-02-07 00:16:46 -08002012 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Jiri Slaby1c456072008-02-07 00:16:46 -08002014 /* Enhance mode enabled here */
Jiri Slaby292955a2021-06-18 08:14:13 +02002015 if (brd->must_hwid != MOXA_OTHER_UART)
Jiri Slabyedb7d272021-06-18 08:14:46 +02002016 mxser_must_set_enhance_mode(info->ioaddr, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Jiri Slaby58a2ddb2021-06-18 08:14:15 +02002018 info->type = PORT_16550A;
Jiri Slaby1c456072008-02-07 00:16:46 -08002019
2020 process_txrx_fifo(info);
2021
Alan Cox44b7d1b2008-07-16 21:57:18 +01002022 info->port.close_delay = 5 * HZ / 10;
2023 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002024 spin_lock_init(&info->slock);
2025
2026 /* before set INT ISR, disable all int */
2027 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2028 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030
Jiri Slaby1c456072008-02-07 00:16:46 -08002031 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2032 brd);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002033 if (retval) {
2034 for (i = 0; i < brd->info->nports; i++)
2035 tty_port_destroy(&brd->ports[i].port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002036 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2037 "conflict with another device.\n",
2038 brd->info->name, brd->irq);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002039 }
Jiri Slabydf480512010-09-03 10:31:37 +02002040
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002041 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Jiri Slaby191c5f12012-11-15 09:49:56 +01002044static void mxser_board_remove(struct mxser_board *brd)
2045{
2046 unsigned int i;
2047
2048 for (i = 0; i < brd->info->nports; i++) {
2049 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2050 tty_port_destroy(&brd->ports[i].port);
2051 }
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002052 free_irq(brd->irq, brd);
Jiri Slaby191c5f12012-11-15 09:49:56 +01002053}
2054
Bill Pemberton9671f092012-11-19 13:21:50 -05002055static int mxser_probe(struct pci_dev *pdev,
Jiri Slaby1c456072008-02-07 00:16:46 -08002056 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Jiri Slaby1c456072008-02-07 00:16:46 -08002058 struct mxser_board *brd;
2059 unsigned int i, j;
2060 unsigned long ioaddress;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002061 struct device *tty_dev;
Jiri Slaby1c456072008-02-07 00:16:46 -08002062 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Jiri Slaby1c456072008-02-07 00:16:46 -08002064 for (i = 0; i < MXSER_BOARDS; i++)
2065 if (mxser_boards[i].info == NULL)
2066 break;
2067
2068 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002069 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2070 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002071 goto err;
2072 }
2073
2074 brd = &mxser_boards[i];
2075 brd->idx = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002076 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
Jiri Slaby1c456072008-02-07 00:16:46 -08002077 mxser_cards[ent->driver_data].name,
2078 pdev->bus->number, PCI_SLOT(pdev->devfn));
2079
2080 retval = pci_enable_device(pdev);
2081 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002082 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002083 goto err;
2084 }
2085
2086 /* io address */
2087 ioaddress = pci_resource_start(pdev, 2);
2088 retval = pci_request_region(pdev, 2, "mxser(IO)");
2089 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002090 goto err_dis;
Jiri Slaby1c456072008-02-07 00:16:46 -08002091
2092 brd->info = &mxser_cards[ent->driver_data];
2093 for (i = 0; i < brd->info->nports; i++)
2094 brd->ports[i].ioaddr = ioaddress + 8 * i;
2095
2096 /* vector */
2097 ioaddress = pci_resource_start(pdev, 3);
2098 retval = pci_request_region(pdev, 3, "mxser(vector)");
2099 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002100 goto err_zero;
Jiri Slaby1c456072008-02-07 00:16:46 -08002101 brd->vector = ioaddress;
2102
2103 /* irq */
2104 brd->irq = pdev->irq;
2105
Jiri Slaby292955a2021-06-18 08:14:13 +02002106 brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002107
Jiri Slaby928f9462021-06-18 08:14:16 +02002108 for (j = 0; j < UART_INFO_NUM; j++) {
2109 if (Gpci_uart_info[j].type == brd->must_hwid) {
2110 brd->max_baud = Gpci_uart_info[j].max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08002111
Jiri Slaby928f9462021-06-18 08:14:16 +02002112 /* exception....CP-102 */
2113 if (brd->info->flags & MXSER_HIGHBAUD)
2114 brd->max_baud = 921600;
2115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002117 }
2118
Jiri Slaby292955a2021-06-18 08:14:13 +02002119 if (brd->must_hwid == MOXA_MUST_MU860_HWID) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002120 for (i = 0; i < brd->info->nports; i++) {
2121 if (i < 4)
2122 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2123 else
2124 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002126 outb(0, ioaddress + 4); /* default set to RS232 mode */
2127 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002129
Jiri Slaby1c456072008-02-07 00:16:46 -08002130 /* mxser_initbrd will hook ISR. */
Jiri Slaby27997072017-09-12 12:39:55 +02002131 retval = mxser_initbrd(brd);
Jiri Slaby1c456072008-02-07 00:16:46 -08002132 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02002133 goto err_rel3;
Jiri Slaby1c456072008-02-07 00:16:46 -08002134
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002135 for (i = 0; i < brd->info->nports; i++) {
2136 tty_dev = tty_port_register_device(&brd->ports[i].port,
2137 mxvar_sdriver, brd->idx + i, &pdev->dev);
2138 if (IS_ERR(tty_dev)) {
2139 retval = PTR_ERR(tty_dev);
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04002140 for (; i > 0; i--)
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002141 tty_unregister_device(mxvar_sdriver,
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04002142 brd->idx + i - 1);
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002143 goto err_relbrd;
2144 }
2145 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002146
2147 pci_set_drvdata(pdev, brd);
2148
2149 return 0;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002150err_relbrd:
2151 for (i = 0; i < brd->info->nports; i++)
2152 tty_port_destroy(&brd->ports[i].port);
2153 free_irq(brd->irq, brd);
Jiri Slabydf480512010-09-03 10:31:37 +02002154err_rel3:
2155 pci_release_region(pdev, 3);
2156err_zero:
Jiri Slaby1c456072008-02-07 00:16:46 -08002157 brd->info = NULL;
Jiri Slabydf480512010-09-03 10:31:37 +02002158 pci_release_region(pdev, 2);
2159err_dis:
2160 pci_disable_device(pdev);
Jiri Slaby1c456072008-02-07 00:16:46 -08002161err:
2162 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002165static void mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Jiri Slaby1c456072008-02-07 00:16:46 -08002167 struct mxser_board *brd = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Jiri Slaby191c5f12012-11-15 09:49:56 +01002169 mxser_board_remove(brd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Jiri Slabydf480512010-09-03 10:31:37 +02002171 pci_release_region(pdev, 2);
2172 pci_release_region(pdev, 3);
2173 pci_disable_device(pdev);
Jiri Slaby1c456072008-02-07 00:16:46 -08002174 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Jiri Slaby1c456072008-02-07 00:16:46 -08002177static struct pci_driver mxser_driver = {
2178 .name = "mxser",
2179 .id_table = mxser_pcibrds,
2180 .probe = mxser_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -05002181 .remove = mxser_remove
Jiri Slaby1c456072008-02-07 00:16:46 -08002182};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Jiri Slaby1c456072008-02-07 00:16:46 -08002184static int __init mxser_module_init(void)
2185{
Jiri Slaby1df00922008-07-25 01:48:22 -07002186 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08002187
Jiri Slaby389fc822021-06-18 08:14:38 +02002188 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002189 if (!mxvar_sdriver)
2190 return -ENOMEM;
2191
Jiri Slaby1c456072008-02-07 00:16:46 -08002192 /* Initialize the tty_driver structure */
Jiri Slaby1c456072008-02-07 00:16:46 -08002193 mxvar_sdriver->name = "ttyMI";
2194 mxvar_sdriver->major = ttymajor;
2195 mxvar_sdriver->minor_start = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002196 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2197 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2198 mxvar_sdriver->init_termios = tty_std_termios;
2199 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2200 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2201 tty_set_operations(mxvar_sdriver, &mxser_ops);
2202
2203 retval = tty_register_driver(mxvar_sdriver);
2204 if (retval) {
2205 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2206 "tty driver !\n");
2207 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002209
Jiri Slaby1c456072008-02-07 00:16:46 -08002210 retval = pci_register_driver(&mxser_driver);
2211 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002212 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby29134362021-06-18 08:14:07 +02002213 goto err_unr;
Jiri Slaby1c456072008-02-07 00:16:46 -08002214 }
2215
Jiri Slaby1c456072008-02-07 00:16:46 -08002216 return 0;
2217err_unr:
2218 tty_unregister_driver(mxvar_sdriver);
2219err_put:
2220 put_tty_driver(mxvar_sdriver);
2221 return retval;
2222}
2223
2224static void __exit mxser_module_exit(void)
2225{
Jiri Slaby1c456072008-02-07 00:16:46 -08002226 pci_unregister_driver(&mxser_driver);
Jiri Slaby1c456072008-02-07 00:16:46 -08002227 tty_unregister_driver(mxvar_sdriver);
2228 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231module_init(mxser_module_init);
2232module_exit(mxser_module_exit);