blob: 83c795acdb1969d25b4dbaf576c2182d0ae15f96 [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
Jiri Slabyb2864842021-06-18 08:14:47 +0200337static u8 mxser_must_select_bank(unsigned long baseio, u8 bank)
338{
339 return __mxser_must_set_EFR(baseio, MOXA_MUST_EFR_BANK_MASK, bank,
340 false);
341}
342
Christoph Hellwig148ff862008-04-30 00:54:29 -0700343static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
344{
Jiri Slabyb2864842021-06-18 08:14:47 +0200345 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700346 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
347 outb(oldlcr, baseio + UART_LCR);
348}
349
350static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
351{
Jiri Slabyb2864842021-06-18 08:14:47 +0200352 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700353 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
354 outb(oldlcr, baseio + UART_LCR);
355}
356
357static void mxser_set_must_fifo_value(struct mxser_port *info)
358{
Jiri Slabyb2864842021-06-18 08:14:47 +0200359 u8 oldlcr = mxser_must_select_bank(info->ioaddr, MOXA_MUST_EFR_BANK1);
Jiri Slabydc33f642021-06-18 08:14:11 +0200360 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
361 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
362 outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700363 outb(oldlcr, info->ioaddr + UART_LCR);
364}
365
366static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
367{
Jiri Slabyb2864842021-06-18 08:14:47 +0200368 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700369 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
370 outb(oldlcr, baseio + UART_LCR);
371}
372
Jiri Slabyb2864842021-06-18 08:14:47 +0200373static u8 mxser_get_must_hardware_id(unsigned long baseio)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700374{
Jiri Slabyb2864842021-06-18 08:14:47 +0200375 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
376 u8 id = inb(baseio + MOXA_MUST_HWID_REGISTER);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700377 outb(oldlcr, baseio + UART_LCR);
Jiri Slabyb2864842021-06-18 08:14:47 +0200378
379 return id;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700380}
381
Jiri Slabyedb7d272021-06-18 08:14:46 +0200382static void mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set)
383{
384 __mxser_must_set_EFR(baseio, clear, set, true);
385}
386
387static void mxser_must_set_enhance_mode(unsigned long baseio, bool enable)
388{
389 mxser_must_set_EFR(baseio,
390 enable ? 0 : MOXA_MUST_EFR_EFRB_ENABLE,
391 enable ? MOXA_MUST_EFR_EFRB_ENABLE : 0);
392}
393
Jiri Slabyb441eb02021-06-18 08:14:48 +0200394static void mxser_must_no_sw_flow_control(unsigned long baseio)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700395{
Jiri Slabyb441eb02021-06-18 08:14:48 +0200396 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_MASK, 0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700397}
398
Jiri Slabyb441eb02021-06-18 08:14:48 +0200399static void mxser_must_set_tx_sw_flow_control(unsigned long baseio, bool enable)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700400{
Jiri Slabyb441eb02021-06-18 08:14:48 +0200401 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_TX_MASK,
402 enable ? MOXA_MUST_EFR_SF_TX1 : 0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700403}
404
Jiri Slabyb441eb02021-06-18 08:14:48 +0200405static void mxser_must_set_rx_sw_flow_control(unsigned long baseio, bool enable)
Christoph Hellwig148ff862008-04-30 00:54:29 -0700406{
Jiri Slabyb441eb02021-06-18 08:14:48 +0200407 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_RX_MASK,
408 enable ? MOXA_MUST_EFR_SF_RX1 : 0);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700409}
410
Jiri Slabye4558362021-06-18 08:14:14 +0200411static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 u8 oldmcr, hwid;
414 int i;
415
416 outb(0, io + UART_LCR);
Jiri Slabyedb7d272021-06-18 08:14:46 +0200417 mxser_must_set_enhance_mode(io, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 oldmcr = inb(io + UART_MCR);
419 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700420 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if ((hwid = inb(io + UART_MCR)) != 0) {
422 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700423 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Jiri Slabyb2864842021-06-18 08:14:47 +0200426 hwid = mxser_get_must_hardware_id(io);
Jiri Slabye4558362021-06-18 08:14:14 +0200427 for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
Jiri Slaby1c456072008-02-07 00:16:46 -0800428 if (hwid == Gpci_uart_info[i].type)
Jiri Slabye4558362021-06-18 08:14:14 +0200429 return hwid;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return MOXA_OTHER_UART;
432}
433
Jiri Slaby1c456072008-02-07 00:16:46 -0800434static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 int i;
437
438 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 info->rx_high_water = 1;
440 info->rx_low_water = 1;
441 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800442 } else
443 for (i = 0; i < UART_INFO_NUM; i++)
Jiri Slaby292955a2021-06-18 08:14:13 +0200444 if (info->board->must_hwid == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
446 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
Jiri Slabydc33f642021-06-18 08:14:11 +0200447 info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Alan Cox31f35932009-01-02 13:45:05 +0000452static int mxser_carrier_raised(struct tty_port *port)
453{
454 struct mxser_port *mp = container_of(port, struct mxser_port, port);
455 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
456}
457
Alan Coxfcc8ac12009-06-11 12:24:17 +0100458static void mxser_dtr_rts(struct tty_port *port, int on)
Alan Cox5d951fb2009-01-02 13:45:19 +0000459{
460 struct mxser_port *mp = container_of(port, struct mxser_port, port);
461 unsigned long flags;
462
463 spin_lock_irqsave(&mp->slock, flags);
Alan Coxfcc8ac12009-06-11 12:24:17 +0100464 if (on)
465 outb(inb(mp->ioaddr + UART_MCR) |
466 UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
467 else
468 outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
469 mp->ioaddr + UART_MCR);
Alan Cox5d951fb2009-01-02 13:45:19 +0000470 spin_unlock_irqrestore(&mp->slock, flags);
471}
472
Jiri Slabydc33f642021-06-18 08:14:11 +0200473static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
Jiri Slaby1c456072008-02-07 00:16:46 -0800474{
Alan Cox216ba022008-10-13 10:40:19 +0100475 struct mxser_port *info = tty->driver_data;
Jiri Slaby104583b2017-09-12 12:39:54 +0200476 unsigned int quot = 0, baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800477 unsigned char cval;
Jiri Slaby104583b2017-09-12 12:39:54 +0200478 u64 timeout;
Jiri Slaby1c456072008-02-07 00:16:46 -0800479
Alan Cox216ba022008-10-13 10:40:19 +0100480 if (!info->ioaddr)
Jiri Slaby1c456072008-02-07 00:16:46 -0800481 return -1;
482
Jiri Slaby928f9462021-06-18 08:14:16 +0200483 if (newspd > info->board->max_baud)
Jiri Slaby1c456072008-02-07 00:16:46 -0800484 return -1;
485
486 if (newspd == 134) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200487 quot = 2 * MXSER_BAUD_BASE / 269;
Alan Cox216ba022008-10-13 10:40:19 +0100488 tty_encode_baud_rate(tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800489 } else if (newspd) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200490 quot = MXSER_BAUD_BASE / newspd;
Jiri Slaby1c456072008-02-07 00:16:46 -0800491 if (quot == 0)
492 quot = 1;
Jiri Slabya6970c32021-06-18 08:14:41 +0200493 baud = MXSER_BAUD_BASE / quot;
Alan Cox216ba022008-10-13 10:40:19 +0100494 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800495 } else {
496 quot = 0;
497 }
498
Jiri Slaby104583b2017-09-12 12:39:54 +0200499 /*
500 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
501 * u64 domain
502 */
503 timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
Jiri Slabya6970c32021-06-18 08:14:41 +0200504 do_div(timeout, MXSER_BAUD_BASE);
Jiri Slaby104583b2017-09-12 12:39:54 +0200505 info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
Jiri Slaby1c456072008-02-07 00:16:46 -0800506
507 if (quot) {
508 info->MCR |= UART_MCR_DTR;
509 outb(info->MCR, info->ioaddr + UART_MCR);
510 } else {
511 info->MCR &= ~UART_MCR_DTR;
512 outb(info->MCR, info->ioaddr + UART_MCR);
513 return 0;
514 }
515
516 cval = inb(info->ioaddr + UART_LCR);
517
518 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
519
520 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
521 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
522 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
523
524#ifdef BOTHER
Alan Cox216ba022008-10-13 10:40:19 +0100525 if (C_BAUD(tty) == BOTHER) {
Jiri Slabya6970c32021-06-18 08:14:41 +0200526 quot = MXSER_BAUD_BASE % newspd;
Jiri Slaby1c456072008-02-07 00:16:46 -0800527 quot *= 8;
528 if (quot % newspd > newspd / 2) {
529 quot /= newspd;
530 quot++;
531 } else
532 quot /= newspd;
533
Christoph Hellwig148ff862008-04-30 00:54:29 -0700534 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800535 } else
536#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700537 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800538
539 return 0;
540}
541
542/*
543 * This routine is called to set the UART divisor registers to match
544 * the specified baud rate for a serial port.
545 */
Jason Yanbeca62c2020-05-06 14:17:35 +0800546static void mxser_change_speed(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800547{
Alan Cox216ba022008-10-13 10:40:19 +0100548 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800549 unsigned cflag, cval, fcr;
Jiri Slaby1c456072008-02-07 00:16:46 -0800550 unsigned char status;
551
Alan Coxadc8d742012-07-14 15:31:47 +0100552 cflag = tty->termios.c_cflag;
Alan Cox216ba022008-10-13 10:40:19 +0100553 if (!info->ioaddr)
Jason Yanbeca62c2020-05-06 14:17:35 +0800554 return;
Jiri Slaby1c456072008-02-07 00:16:46 -0800555
Jiri Slabyef3dff82021-06-18 08:14:29 +0200556 mxser_set_baud(tty, tty_get_baud_rate(tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800557
558 /* byte size and parity */
559 switch (cflag & CSIZE) {
Jiri Slaby2c218322021-06-18 08:14:43 +0200560 default:
Jiri Slaby1c456072008-02-07 00:16:46 -0800561 case CS5:
Jiri Slaby2c218322021-06-18 08:14:43 +0200562 cval = UART_LCR_WLEN5;
Jiri Slaby1c456072008-02-07 00:16:46 -0800563 break;
564 case CS6:
Jiri Slaby2c218322021-06-18 08:14:43 +0200565 cval = UART_LCR_WLEN6;
Jiri Slaby1c456072008-02-07 00:16:46 -0800566 break;
567 case CS7:
Jiri Slaby2c218322021-06-18 08:14:43 +0200568 cval = UART_LCR_WLEN7;
Jiri Slaby1c456072008-02-07 00:16:46 -0800569 break;
570 case CS8:
Jiri Slaby2c218322021-06-18 08:14:43 +0200571 cval = UART_LCR_WLEN8;
Jiri Slaby1c456072008-02-07 00:16:46 -0800572 break;
Jiri Slaby1c456072008-02-07 00:16:46 -0800573 }
Jiri Slaby2c218322021-06-18 08:14:43 +0200574
Jiri Slaby1c456072008-02-07 00:16:46 -0800575 if (cflag & CSTOPB)
Jiri Slaby2c218322021-06-18 08:14:43 +0200576 cval |= UART_LCR_STOP;
Jiri Slaby1c456072008-02-07 00:16:46 -0800577 if (cflag & PARENB)
578 cval |= UART_LCR_PARITY;
579 if (!(cflag & PARODD))
580 cval |= UART_LCR_EPAR;
581 if (cflag & CMSPAR)
582 cval |= UART_LCR_SPAR;
583
584 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
Jiri Slaby292955a2021-06-18 08:14:13 +0200585 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800586 fcr = UART_FCR_ENABLE_FIFO;
587 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700588 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800589 } else
590 fcr = 0;
591 } else {
592 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slaby292955a2021-06-18 08:14:13 +0200593 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800594 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700595 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800596 } else {
Jiri Slabydc33f642021-06-18 08:14:11 +0200597 switch (info->rx_high_water) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800598 case 1:
599 fcr |= UART_FCR_TRIGGER_1;
600 break;
601 case 4:
602 fcr |= UART_FCR_TRIGGER_4;
603 break;
604 case 8:
605 fcr |= UART_FCR_TRIGGER_8;
606 break;
607 default:
608 fcr |= UART_FCR_TRIGGER_14;
609 break;
610 }
611 }
612 }
613
614 /* CTS flow control flag and modem status interrupts */
615 info->IER &= ~UART_IER_MSI;
616 info->MCR &= ~UART_MCR_AFE;
Peter Hurley5604a982016-04-09 17:53:21 -0700617 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
Jiri Slaby1c456072008-02-07 00:16:46 -0800618 if (cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800619 info->IER |= UART_IER_MSI;
Jiri Slaby292955a2021-06-18 08:14:13 +0200620 if ((info->type == PORT_16550A) || (info->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800621 info->MCR |= UART_MCR_AFE;
622 } else {
623 status = inb(info->ioaddr + UART_MSR);
Alan Cox216ba022008-10-13 10:40:19 +0100624 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800625 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100626 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800627 if (info->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200628 !info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800629 outb(info->IER & ~UART_IER_THRI,
630 info->ioaddr +
631 UART_IER);
632 info->IER |= UART_IER_THRI;
633 outb(info->IER, info->ioaddr +
634 UART_IER);
635 }
Alan Cox216ba022008-10-13 10:40:19 +0100636 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800637 }
638 } else {
639 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100640 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800641 if ((info->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200642 (!info->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800643 info->IER &= ~UART_IER_THRI;
644 outb(info->IER, info->ioaddr +
645 UART_IER);
646 }
647 }
648 }
649 }
Jiri Slaby1c456072008-02-07 00:16:46 -0800650 }
651 outb(info->MCR, info->ioaddr + UART_MCR);
Peter Hurley2d686552016-04-09 17:53:23 -0700652 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
653 if (~cflag & CLOCAL)
Jiri Slaby1c456072008-02-07 00:16:46 -0800654 info->IER |= UART_IER_MSI;
Jiri Slaby1c456072008-02-07 00:16:46 -0800655 outb(info->IER, info->ioaddr + UART_IER);
656
657 /*
658 * Set up parity check flag
659 */
660 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox216ba022008-10-13 10:40:19 +0100661 if (I_INPCK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800662 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox216ba022008-10-13 10:40:19 +0100663 if (I_BRKINT(tty) || I_PARMRK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800664 info->read_status_mask |= UART_LSR_BI;
665
666 info->ignore_status_mask = 0;
667
Alan Cox216ba022008-10-13 10:40:19 +0100668 if (I_IGNBRK(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800669 info->ignore_status_mask |= UART_LSR_BI;
670 info->read_status_mask |= UART_LSR_BI;
671 /*
672 * If we're ignore parity and break indicators, ignore
673 * overruns too. (For real raw support).
674 */
Alan Cox216ba022008-10-13 10:40:19 +0100675 if (I_IGNPAR(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800676 info->ignore_status_mask |=
677 UART_LSR_OE |
678 UART_LSR_PE |
679 UART_LSR_FE;
680 info->read_status_mask |=
681 UART_LSR_OE |
682 UART_LSR_PE |
683 UART_LSR_FE;
684 }
685 }
Jiri Slaby292955a2021-06-18 08:14:13 +0200686 if (info->board->must_hwid) {
Alan Cox216ba022008-10-13 10:40:19 +0100687 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
688 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
Jiri Slabyb441eb02021-06-18 08:14:48 +0200689 mxser_must_set_rx_sw_flow_control(info->ioaddr, I_IXON(tty));
690 mxser_must_set_tx_sw_flow_control(info->ioaddr, I_IXOFF(tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800691 }
692
693
694 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
695 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby1c456072008-02-07 00:16:46 -0800696}
697
Alan Cox216ba022008-10-13 10:40:19 +0100698static void mxser_check_modem_status(struct tty_struct *tty,
699 struct mxser_port *port, int status)
Jiri Slaby1c456072008-02-07 00:16:46 -0800700{
701 /* update input line counters */
702 if (status & UART_MSR_TERI)
703 port->icount.rng++;
704 if (status & UART_MSR_DDSR)
705 port->icount.dsr++;
706 if (status & UART_MSR_DDCD)
707 port->icount.dcd++;
708 if (status & UART_MSR_DCTS)
709 port->icount.cts++;
Alan Coxbdc04e32009-09-19 13:13:31 -0700710 wake_up_interruptible(&port->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800711
Peter Hurley2d686552016-04-09 17:53:23 -0700712 if (tty_port_check_carrier(&port->port) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800713 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100714 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800715 }
716
Huang Shijief21ec3d2012-08-22 22:13:36 -0400717 if (tty_port_cts_enabled(&port->port)) {
Alan Cox216ba022008-10-13 10:40:19 +0100718 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800719 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100720 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800721
722 if ((port->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200723 (!port->board->must_hwid)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800724 outb(port->IER & ~UART_IER_THRI,
725 port->ioaddr + UART_IER);
726 port->IER |= UART_IER_THRI;
727 outb(port->IER, port->ioaddr +
728 UART_IER);
729 }
Alan Cox216ba022008-10-13 10:40:19 +0100730 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800731 }
732 } else {
733 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100734 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800735 if (port->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +0200736 !port->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800737 port->IER &= ~UART_IER_THRI;
738 outb(port->IER, port->ioaddr +
739 UART_IER);
740 }
741 }
742 }
743 }
744}
745
Alan Cox67691402009-11-30 13:17:35 +0000746static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800747{
Alan Cox67691402009-11-30 13:17:35 +0000748 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800749 unsigned long page;
750 unsigned long flags;
751
752 page = __get_free_page(GFP_KERNEL);
753 if (!page)
754 return -ENOMEM;
755
756 spin_lock_irqsave(&info->slock, flags);
757
Jiri Slaby1c456072008-02-07 00:16:46 -0800758 if (!info->ioaddr || !info->type) {
Alan Cox216ba022008-10-13 10:40:19 +0100759 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800760 free_page(page);
761 spin_unlock_irqrestore(&info->slock, flags);
762 return 0;
763 }
Alan Cox67691402009-11-30 13:17:35 +0000764 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800765
766 /*
767 * Clear the FIFO buffers and disable them
768 * (they will be reenabled in mxser_change_speed())
769 */
Jiri Slaby292955a2021-06-18 08:14:13 +0200770 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800771 outb((UART_FCR_CLEAR_RCVR |
772 UART_FCR_CLEAR_XMIT |
773 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
774 else
775 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
776 info->ioaddr + UART_FCR);
777
778 /*
779 * At this point there's no way the LSR could still be 0xFF;
780 * if it is, then bail out, because there's likely no UART
781 * here.
782 */
783 if (inb(info->ioaddr + UART_LSR) == 0xff) {
784 spin_unlock_irqrestore(&info->slock, flags);
785 if (capable(CAP_SYS_ADMIN)) {
Jiri Slabyf43a510d2009-12-09 12:34:15 -0800786 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800787 return 0;
788 } else
789 return -ENODEV;
790 }
791
792 /*
793 * Clear the interrupt registers.
794 */
795 (void) inb(info->ioaddr + UART_LSR);
796 (void) inb(info->ioaddr + UART_RX);
797 (void) inb(info->ioaddr + UART_IIR);
798 (void) inb(info->ioaddr + UART_MSR);
799
800 /*
801 * Now, initialize the UART
802 */
803 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
804 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
805 outb(info->MCR, info->ioaddr + UART_MCR);
806
807 /*
808 * Finally, enable interrupts
809 */
810 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
811
Jiri Slaby292955a2021-06-18 08:14:13 +0200812 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800813 info->IER |= MOXA_MUST_IER_EGDAI;
814 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
815
816 /*
817 * And clear the interrupt registers again for luck.
818 */
819 (void) inb(info->ioaddr + UART_LSR);
820 (void) inb(info->ioaddr + UART_RX);
821 (void) inb(info->ioaddr + UART_IIR);
822 (void) inb(info->ioaddr + UART_MSR);
823
Alan Cox216ba022008-10-13 10:40:19 +0100824 clear_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800825 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
826
827 /*
828 * and set the speed of the serial port
829 */
Jiri Slaby27997072017-09-12 12:39:55 +0200830 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800831 spin_unlock_irqrestore(&info->slock, flags);
832
833 return 0;
834}
835
836/*
Alan Cox67691402009-11-30 13:17:35 +0000837 * This routine will shutdown a serial port
Jiri Slaby1c456072008-02-07 00:16:46 -0800838 */
Alan Cox67691402009-11-30 13:17:35 +0000839static void mxser_shutdown_port(struct tty_port *port)
Jiri Slaby1c456072008-02-07 00:16:46 -0800840{
Alan Cox67691402009-11-30 13:17:35 +0000841 struct mxser_port *info = container_of(port, struct mxser_port, port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800842 unsigned long flags;
843
Jiri Slaby1c456072008-02-07 00:16:46 -0800844 spin_lock_irqsave(&info->slock, flags);
845
846 /*
847 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
848 * here so the queue might never be waken up
849 */
Alan Coxbdc04e32009-09-19 13:13:31 -0700850 wake_up_interruptible(&info->port.delta_msr_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800851
852 /*
Alan Cox67691402009-11-30 13:17:35 +0000853 * Free the xmit buffer, if necessary
Jiri Slaby1c456072008-02-07 00:16:46 -0800854 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100855 if (info->port.xmit_buf) {
856 free_page((unsigned long) info->port.xmit_buf);
857 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -0800858 }
859
860 info->IER = 0;
861 outb(0x00, info->ioaddr + UART_IER);
862
Jiri Slaby1c456072008-02-07 00:16:46 -0800863 /* clear Rx/Tx FIFO's */
Jiri Slaby292955a2021-06-18 08:14:13 +0200864 if (info->board->must_hwid)
Jiri Slaby1c456072008-02-07 00:16:46 -0800865 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
866 MOXA_MUST_FCR_GDA_MODE_ENABLE,
867 info->ioaddr + UART_FCR);
868 else
869 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
870 info->ioaddr + UART_FCR);
871
872 /* read data port to reset things */
873 (void) inb(info->ioaddr + UART_RX);
874
Jiri Slaby1c456072008-02-07 00:16:46 -0800875
Jiri Slaby292955a2021-06-18 08:14:13 +0200876 if (info->board->must_hwid)
Jiri Slabyb441eb02021-06-18 08:14:48 +0200877 mxser_must_no_sw_flow_control(info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800878
879 spin_unlock_irqrestore(&info->slock, flags);
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882/*
883 * This routine is called whenever a serial port is opened. It
884 * enables interrupts for a serial port, linking in its async structure into
885 * the IRQ chain. It also performs the serial-specific
886 * initialization for the tty structure.
887 */
888static int mxser_open(struct tty_struct *tty, struct file *filp)
889{
Jiri Slaby1c456072008-02-07 00:16:46 -0800890 struct mxser_port *info;
Jiri Slaby389fc822021-06-18 08:14:38 +0200891 int line = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Jiri Slaby1c456072008-02-07 00:16:46 -0800893 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
894 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700895 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Alan Coxa2d1e352010-04-23 16:01:18 +0100897 tty->driver_data = info;
Alan Cox67691402009-11-30 13:17:35 +0000898 return tty_port_open(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Alan Cox978e5952008-04-30 00:53:59 -0700901static void mxser_flush_buffer(struct tty_struct *tty)
902{
903 struct mxser_port *info = tty->driver_data;
904 char fcr;
905 unsigned long flags;
906
907
908 spin_lock_irqsave(&info->slock, flags);
909 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
910
911 fcr = inb(info->ioaddr + UART_FCR);
912 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
913 info->ioaddr + UART_FCR);
914 outb(fcr, info->ioaddr + UART_FCR);
915
916 spin_unlock_irqrestore(&info->slock, flags);
917
918 tty_wakeup(tty);
919}
920
921
Alan Cox67691402009-11-30 13:17:35 +0000922static void mxser_close_port(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Alan Cox1e2b0252009-09-19 13:13:21 -0700924 struct mxser_port *info = container_of(port, struct mxser_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 * At this point we stop accepting input. To do this, we
928 * disable the receive line status interrupts, and tell the
929 * interrupt driver to stop checking the data ready bit in the
930 * line status register.
931 */
932 info->IER &= ~UART_IER_RLSI;
Jiri Slaby292955a2021-06-18 08:14:13 +0200933 if (info->board->must_hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -0800935
Alan Cox67691402009-11-30 13:17:35 +0000936 outb(info->IER, info->ioaddr + UART_IER);
937 /*
938 * Before we drop DTR, make sure the UART transmitter
939 * has completely drained; this is especially
940 * important if there is a transmit FIFO!
941 */
942 timeout = jiffies + HZ;
943 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
944 schedule_timeout_interruptible(5);
945 if (time_after(jiffies, timeout))
946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Alan Cox1e2b0252009-09-19 13:13:21 -0700948}
949
950/*
951 * This routine is called when the serial port gets closed. First, we
952 * wait for the last remaining data to be sent. Then, we unlink its
953 * async structure from the interrupt chain if necessary, and we free
954 * that IRQ if nothing is left in the chain.
955 */
956static void mxser_close(struct tty_struct *tty, struct file *filp)
957{
958 struct mxser_port *info = tty->driver_data;
959 struct tty_port *port = &info->port;
960
Jiri Slaby389fc822021-06-18 08:14:38 +0200961 if (info == NULL)
Alan Cox1e2b0252009-09-19 13:13:21 -0700962 return;
963 if (tty_port_close_start(port, tty, filp) == 0)
964 return;
Peter Hurleycd7b4b32016-01-10 14:51:38 -0800965 info->closing = 1;
Alan Cox67691402009-11-30 13:17:35 +0000966 mutex_lock(&port->mutex);
967 mxser_close_port(port);
Alan Cox1e2b0252009-09-19 13:13:21 -0700968 mxser_flush_buffer(tty);
Peter Hurleyd41861c2016-04-09 17:53:25 -0700969 if (tty_port_initialized(port) && C_HUPCL(tty))
970 tty_port_lower_dtr_rts(port);
Alan Cox67691402009-11-30 13:17:35 +0000971 mxser_shutdown_port(port);
Peter Hurleyd41861c2016-04-09 17:53:25 -0700972 tty_port_set_initialized(port, 0);
Alan Cox67691402009-11-30 13:17:35 +0000973 mutex_unlock(&port->mutex);
Peter Hurleycd7b4b32016-01-10 14:51:38 -0800974 info->closing = 0;
Alan Coxa6614992009-01-02 13:46:50 +0000975 /* Right now the tty_port set is done outside of the close_end helper
976 as we don't yet have everyone using refcounts */
977 tty_port_close_end(port, tty);
978 tty_port_tty_set(port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
982{
983 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800984 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 unsigned long flags;
986
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100987 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700991 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
992 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (c <= 0)
994 break;
995
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100996 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700998 info->xmit_head = (info->xmit_head + c) &
999 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 info->xmit_cnt += c;
1001 spin_unlock_irqrestore(&info->slock, flags);
1002
1003 buf += c;
1004 count -= c;
1005 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001008 if (info->xmit_cnt && !tty->flow.stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001009 if (!tty->hw_stopped ||
1010 (info->type == PORT_16550A) ||
Jiri Slaby292955a2021-06-18 08:14:13 +02001011 (info->board->must_hwid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001013 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1014 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001016 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 spin_unlock_irqrestore(&info->slock, flags);
1018 }
1019 }
1020 return total;
1021}
1022
Alan Cox0be2ead2008-04-30 00:54:03 -07001023static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Jiri Slaby1c456072008-02-07 00:16:46 -08001025 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 unsigned long flags;
1027
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001028 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001035 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1037 info->xmit_cnt++;
1038 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001039 if (!tty->flow.stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001040 if (!tty->hw_stopped ||
1041 (info->type == PORT_16550A) ||
Jiri Slaby292955a2021-06-18 08:14:13 +02001042 info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001044 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001046 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 spin_unlock_irqrestore(&info->slock, flags);
1048 }
1049 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001050 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053
1054static void mxser_flush_chars(struct tty_struct *tty)
1055{
Jiri Slaby1c456072008-02-07 00:16:46 -08001056 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 unsigned long flags;
1058
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001059 if (info->xmit_cnt <= 0 || tty->flow.stopped || !info->port.xmit_buf ||
Jiri Slabyace7dd92008-07-25 01:48:22 -07001060 (tty->hw_stopped && info->type != PORT_16550A &&
Jiri Slaby292955a2021-06-18 08:14:13 +02001061 !info->board->must_hwid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return;
1063
1064 spin_lock_irqsave(&info->slock, flags);
1065
Jiri Slaby1c456072008-02-07 00:16:46 -08001066 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001068 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 spin_unlock_irqrestore(&info->slock, flags);
1071}
1072
Jiri Slaby03b3b1a2021-05-05 11:19:15 +02001073static unsigned int mxser_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Jiri Slaby1c456072008-02-07 00:16:46 -08001075 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 int ret;
1077
1078 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
Jiri Slabyace7dd92008-07-25 01:48:22 -07001079 return ret < 0 ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Jiri Slabyfff4ef12021-05-05 11:19:19 +02001082static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Jiri Slaby1c456072008-02-07 00:16:46 -08001084 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return info->xmit_cnt;
1086}
1087
Jiri Slaby1c456072008-02-07 00:16:46 -08001088/*
1089 * ------------------------------------------------------------
1090 * friends of mxser_ioctl()
1091 * ------------------------------------------------------------
1092 */
Alan Cox216ba022008-10-13 10:40:19 +01001093static int mxser_get_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001094 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Alan Cox216ba022008-10-13 10:40:19 +01001096 struct mxser_port *info = tty->driver_data;
Al Viro6da5b582018-09-11 22:22:06 -04001097 struct tty_port *port = &info->port;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001098 unsigned int closing_wait, close_delay;
Al Viro6da5b582018-09-11 22:22:06 -04001099
Al Viro6da5b582018-09-11 22:22:06 -04001100 mutex_lock(&port->mutex);
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001101
1102 close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
1103 closing_wait = info->port.closing_wait;
1104 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1105 closing_wait = jiffies_to_msecs(closing_wait) / 10;
1106
Al Viro6da5b582018-09-11 22:22:06 -04001107 ss->type = info->type,
1108 ss->line = tty->index,
1109 ss->port = info->ioaddr,
1110 ss->irq = info->board->irq,
1111 ss->flags = info->port.flags,
Jiri Slabya6970c32021-06-18 08:14:41 +02001112 ss->baud_base = MXSER_BAUD_BASE,
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001113 ss->close_delay = close_delay;
1114 ss->closing_wait = closing_wait;
Jiri Slabyd811b262021-06-18 08:14:42 +02001115 ss->custom_divisor = MXSER_CUSTOM_DIVISOR,
Al Viro6da5b582018-09-11 22:22:06 -04001116 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001117 return 0;
1118}
1119
Alan Cox216ba022008-10-13 10:40:19 +01001120static int mxser_set_serial_info(struct tty_struct *tty,
Al Viro6da5b582018-09-11 22:22:06 -04001121 struct serial_struct *ss)
Jiri Slaby1c456072008-02-07 00:16:46 -08001122{
Alan Cox216ba022008-10-13 10:40:19 +01001123 struct mxser_port *info = tty->driver_data;
Alan Cox07f86c02009-11-30 13:17:41 +00001124 struct tty_port *port = &info->port;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001125 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001126 unsigned long sl_flags;
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001127 unsigned int flags, close_delay, closing_wait;
Jiri Slaby1c456072008-02-07 00:16:46 -08001128 int retval = 0;
1129
Al Viro6da5b582018-09-11 22:22:06 -04001130 if (tty_io_error(tty))
1131 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08001132
Al Viro6da5b582018-09-11 22:22:06 -04001133 mutex_lock(&port->mutex);
1134 if (!info->ioaddr) {
1135 mutex_unlock(&port->mutex);
1136 return -ENODEV;
1137 }
1138
1139 if (ss->irq != info->board->irq ||
1140 ss->port != info->ioaddr) {
1141 mutex_unlock(&port->mutex);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001142 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001143 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001144
Alan Cox07f86c02009-11-30 13:17:41 +00001145 flags = port->flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001146
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001147 close_delay = msecs_to_jiffies(ss->close_delay * 10);
1148 closing_wait = ss->closing_wait;
1149 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1150 closing_wait = msecs_to_jiffies(closing_wait * 10);
1151
Jiri Slaby1c456072008-02-07 00:16:46 -08001152 if (!capable(CAP_SYS_ADMIN)) {
Jiri Slabya6970c32021-06-18 08:14:41 +02001153 if ((ss->baud_base != MXSER_BAUD_BASE) ||
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001154 (close_delay != info->port.close_delay) ||
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001155 (closing_wait != info->port.closing_wait) ||
Al Viro6da5b582018-09-11 22:22:06 -04001156 ((ss->flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK))) {
1157 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001158 return -EPERM;
Al Viro6da5b582018-09-11 22:22:06 -04001159 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001160 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Al Viro6da5b582018-09-11 22:22:06 -04001161 (ss->flags & ASYNC_USR_MASK));
Jiri Slaby1c456072008-02-07 00:16:46 -08001162 } else {
1163 /*
1164 * OK, past this point, all the error checking has been done.
1165 * At this point, we start making changes.....
1166 */
Alan Cox07f86c02009-11-30 13:17:41 +00001167 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Al Viro6da5b582018-09-11 22:22:06 -04001168 (ss->flags & ASYNC_FLAGS));
Johan Hovoldbe6cf582021-04-07 12:23:31 +02001169 port->close_delay = close_delay;
1170 port->closing_wait = closing_wait;
Alan Cox07f86c02009-11-30 13:17:41 +00001171 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Jiri Slabya6970c32021-06-18 08:14:41 +02001172 (ss->baud_base != MXSER_BAUD_BASE ||
Al Viro6da5b582018-09-11 22:22:06 -04001173 ss->custom_divisor !=
Jiri Slabyd811b262021-06-18 08:14:42 +02001174 MXSER_CUSTOM_DIVISOR)) {
Al Viro6da5b582018-09-11 22:22:06 -04001175 if (ss->custom_divisor == 0) {
1176 mutex_unlock(&port->mutex);
Alan Cox07f86c02009-11-30 13:17:41 +00001177 return -EINVAL;
Al Viro6da5b582018-09-11 22:22:06 -04001178 }
1179 baud = ss->baud_base / ss->custom_divisor;
Alan Cox216ba022008-10-13 10:40:19 +01001180 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001181 }
Johan Hovoldb91cfb22021-04-07 12:23:32 +02001182
1183 info->type = ss->type;
1184
1185 process_txrx_fifo(info);
Jiri Slaby1c456072008-02-07 00:16:46 -08001186 }
1187
Peter Hurleyd41861c2016-04-09 17:53:25 -07001188 if (tty_port_initialized(port)) {
Alan Cox07f86c02009-11-30 13:17:41 +00001189 if (flags != (port->flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001190 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slaby27997072017-09-12 12:39:55 +02001191 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001192 spin_unlock_irqrestore(&info->slock, sl_flags);
1193 }
Alan Cox67691402009-11-30 13:17:35 +00001194 } else {
Alan Cox07f86c02009-11-30 13:17:41 +00001195 retval = mxser_activate(port, tty);
Alan Cox67691402009-11-30 13:17:35 +00001196 if (retval == 0)
Peter Hurleyd41861c2016-04-09 17:53:25 -07001197 tty_port_set_initialized(port, 1);
Alan Cox67691402009-11-30 13:17:35 +00001198 }
Al Viro6da5b582018-09-11 22:22:06 -04001199 mutex_unlock(&port->mutex);
Jiri Slaby1c456072008-02-07 00:16:46 -08001200 return retval;
1201}
1202
1203/*
1204 * mxser_get_lsr_info - get line status register info
1205 *
1206 * Purpose: Let user call ioctl() to get info when the UART physically
1207 * is emptied. On bus types like RS485, the transmitter must
1208 * release the bus after transmitting. This must be done when
1209 * the transmit shift register is empty, not be done when the
1210 * transmit holding register is empty. This functionality
1211 * allows an RS485 driver to be written in user space.
1212 */
1213static int mxser_get_lsr_info(struct mxser_port *info,
1214 unsigned int __user *value)
1215{
1216 unsigned char status;
1217 unsigned int result;
1218 unsigned long flags;
1219
1220 spin_lock_irqsave(&info->slock, flags);
1221 status = inb(info->ioaddr + UART_LSR);
1222 spin_unlock_irqrestore(&info->slock, flags);
1223 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1224 return put_user(result, value);
1225}
1226
Alan Cox60b33c12011-02-14 16:26:14 +00001227static int mxser_tiocmget(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -08001228{
1229 struct mxser_port *info = tty->driver_data;
1230 unsigned char control, status;
1231 unsigned long flags;
1232
Peter Hurley18900ca2016-04-09 17:06:48 -07001233 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001234 return -EIO;
1235
1236 control = info->MCR;
1237
1238 spin_lock_irqsave(&info->slock, flags);
1239 status = inb(info->ioaddr + UART_MSR);
1240 if (status & UART_MSR_ANY_DELTA)
Alan Cox216ba022008-10-13 10:40:19 +01001241 mxser_check_modem_status(tty, info, status);
Jiri Slaby1c456072008-02-07 00:16:46 -08001242 spin_unlock_irqrestore(&info->slock, flags);
1243 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1244 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1245 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1246 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1247 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1248 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1249}
1250
Alan Cox20b9d172011-02-14 16:26:50 +00001251static int mxser_tiocmset(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001252 unsigned int set, unsigned int clear)
1253{
1254 struct mxser_port *info = tty->driver_data;
1255 unsigned long flags;
1256
Peter Hurley18900ca2016-04-09 17:06:48 -07001257 if (tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001258 return -EIO;
1259
1260 spin_lock_irqsave(&info->slock, flags);
1261
1262 if (set & TIOCM_RTS)
1263 info->MCR |= UART_MCR_RTS;
1264 if (set & TIOCM_DTR)
1265 info->MCR |= UART_MCR_DTR;
1266
1267 if (clear & TIOCM_RTS)
1268 info->MCR &= ~UART_MCR_RTS;
1269 if (clear & TIOCM_DTR)
1270 info->MCR &= ~UART_MCR_DTR;
1271
1272 outb(info->MCR, info->ioaddr + UART_MCR);
1273 spin_unlock_irqrestore(&info->slock, flags);
1274 return 0;
1275}
1276
Jiri Slaby1c456072008-02-07 00:16:46 -08001277static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1278 struct async_icount *cprev)
1279{
1280 struct async_icount cnow;
1281 unsigned long flags;
1282 int ret;
1283
1284 spin_lock_irqsave(&info->slock, flags);
1285 cnow = info->icount; /* atomic copy */
1286 spin_unlock_irqrestore(&info->slock, flags);
1287
1288 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1289 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1290 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1291 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1292
1293 *cprev = cnow;
1294
1295 return ret;
1296}
1297
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001298/* We should likely switch to TIOCGRS485/TIOCSRS485. */
1299static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set,
1300 int __user *u_opmode)
1301{
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001302 int opmode, p = index % 4;
1303 int shiftbit = p * 2;
Jiri Slaby238d1172021-06-18 08:14:45 +02001304 u8 val;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001305
1306 if (port->board->must_hwid != MOXA_MUST_MU860_HWID)
1307 return -EFAULT;
1308
1309 if (set) {
1310 if (get_user(opmode, u_opmode))
1311 return -EFAULT;
1312
Jiri Slaby238d1172021-06-18 08:14:45 +02001313 if (opmode & ~OP_MODE_MASK)
1314 return -EINVAL;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001315
1316 spin_lock_irq(&port->slock);
1317 val = inb(port->opmode_ioaddr);
Jiri Slaby238d1172021-06-18 08:14:45 +02001318 val &= ~(OP_MODE_MASK << shiftbit);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001319 val |= (opmode << shiftbit);
1320 outb(val, port->opmode_ioaddr);
1321 spin_unlock_irq(&port->slock);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001322
Jiri Slaby238d1172021-06-18 08:14:45 +02001323 return 0;
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001324 }
1325
Jiri Slaby238d1172021-06-18 08:14:45 +02001326 spin_lock_irq(&port->slock);
1327 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1328 spin_unlock_irq(&port->slock);
1329
1330 return put_user(opmode & OP_MODE_MASK, u_opmode);
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001331}
1332
Alan Cox6caa76b2011-02-14 16:27:22 +00001333static int mxser_ioctl(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001334 unsigned int cmd, unsigned long arg)
1335{
1336 struct mxser_port *info = tty->driver_data;
1337 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 unsigned long flags;
1339 void __user *argp = (void __user *)arg;
1340
Jiri Slaby9fae5f82021-06-18 08:14:44 +02001341 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE)
1342 return mxser_ioctl_op_mode(info, tty->index,
1343 cmd == MOXA_SET_OP_MODE, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Al Viro6da5b582018-09-11 22:22:06 -04001345 if (cmd != TIOCMIWAIT && tty_io_error(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -08001346 return -EIO;
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001350 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1353 * - mask passed in arg for lines of interest
1354 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1355 * Caller should use TIOCGICOUNT to see which one it was
1356 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001357 case TIOCMIWAIT:
1358 spin_lock_irqsave(&info->slock, flags);
1359 cnow = info->icount; /* note the counters on entry */
1360 spin_unlock_irqrestore(&info->slock, flags);
1361
Alan Coxbdc04e32009-09-19 13:13:31 -07001362 return wait_event_interruptible(info->port.delta_msr_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001363 mxser_cflags_changed(info, arg, &cnow));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 default:
1365 return -ENOIOCTLCMD;
1366 }
1367 return 0;
1368}
1369
Alan Cox05871022010-09-16 18:21:52 +01001370 /*
1371 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1372 * Return: write counters to the user passed counter struct
1373 * NB: both 1->0 and 0->1 transitions are counted except for
1374 * RI where only 0->1 is counted.
1375 */
1376
1377static int mxser_get_icount(struct tty_struct *tty,
1378 struct serial_icounter_struct *icount)
1379
1380{
1381 struct mxser_port *info = tty->driver_data;
1382 struct async_icount cnow;
1383 unsigned long flags;
1384
1385 spin_lock_irqsave(&info->slock, flags);
1386 cnow = info->icount;
1387 spin_unlock_irqrestore(&info->slock, flags);
1388
1389 icount->frame = cnow.frame;
1390 icount->brk = cnow.brk;
1391 icount->overrun = cnow.overrun;
1392 icount->buf_overrun = cnow.buf_overrun;
1393 icount->parity = cnow.parity;
1394 icount->rx = cnow.rx;
1395 icount->tx = cnow.tx;
1396 icount->cts = cnow.cts;
1397 icount->dsr = cnow.dsr;
1398 icount->rng = cnow.rng;
1399 icount->dcd = cnow.dcd;
1400 return 0;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403static void mxser_stoprx(struct tty_struct *tty)
1404{
Jiri Slaby1c456072008-02-07 00:16:46 -08001405 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 info->ldisc_stop_rx = 1;
1408 if (I_IXOFF(tty)) {
Jiri Slaby292955a2021-06-18 08:14:13 +02001409 if (info->board->must_hwid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001411 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001414 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001416 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 }
1419
Peter Hurley9db276f2016-01-10 20:36:15 -08001420 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001421 info->MCR &= ~UART_MCR_RTS;
1422 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424}
1425
1426/*
1427 * This routine is called by the upper-layer tty layer to signal that
1428 * incoming characters should be throttled.
1429 */
1430static void mxser_throttle(struct tty_struct *tty)
1431{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435static void mxser_unthrottle(struct tty_struct *tty)
1436{
Jiri Slaby1c456072008-02-07 00:16:46 -08001437 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001438
Jiri Slaby1c456072008-02-07 00:16:46 -08001439 /* startrx */
1440 info->ldisc_stop_rx = 0;
1441 if (I_IXOFF(tty)) {
1442 if (info->x_char)
1443 info->x_char = 0;
1444 else {
Jiri Slaby292955a2021-06-18 08:14:13 +02001445 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001446 info->IER |= MOXA_MUST_RECV_ISR;
1447 outb(info->IER, info->ioaddr + UART_IER);
1448 } else {
1449 info->x_char = START_CHAR(tty);
1450 outb(0, info->ioaddr + UART_IER);
1451 info->IER |= UART_IER_THRI;
1452 outb(info->IER, info->ioaddr + UART_IER);
1453 }
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
Peter Hurley9db276f2016-01-10 20:36:15 -08001457 if (C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001458 info->MCR |= UART_MCR_RTS;
1459 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461}
1462
1463/*
1464 * mxser_stop() and mxser_start()
1465 *
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001466 * This routines are called before setting or resetting tty->flow.stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * They enable or disable transmitter interrupts, as necessary.
1468 */
1469static void mxser_stop(struct tty_struct *tty)
1470{
Jiri Slaby1c456072008-02-07 00:16:46 -08001471 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 unsigned long flags;
1473
1474 spin_lock_irqsave(&info->slock, flags);
1475 if (info->IER & UART_IER_THRI) {
1476 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001477 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479 spin_unlock_irqrestore(&info->slock, flags);
1480}
1481
1482static void mxser_start(struct tty_struct *tty)
1483{
Jiri Slaby1c456072008-02-07 00:16:46 -08001484 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 unsigned long flags;
1486
1487 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001488 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001489 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001491 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493 spin_unlock_irqrestore(&info->slock, flags);
1494}
1495
Jiri Slaby1c456072008-02-07 00:16:46 -08001496static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1497{
1498 struct mxser_port *info = tty->driver_data;
1499 unsigned long flags;
1500
1501 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby27997072017-09-12 12:39:55 +02001502 mxser_change_speed(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001503 spin_unlock_irqrestore(&info->slock, flags);
1504
Peter Hurley9db276f2016-01-10 20:36:15 -08001505 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001506 tty->hw_stopped = 0;
1507 mxser_start(tty);
1508 }
1509
1510 /* Handle sw stopped */
Peter Hurley9db276f2016-01-10 20:36:15 -08001511 if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001512 tty->flow.stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001513
Jiri Slaby292955a2021-06-18 08:14:13 +02001514 if (info->board->must_hwid) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001515 spin_lock_irqsave(&info->slock, flags);
Jiri Slabyb441eb02021-06-18 08:14:48 +02001516 mxser_must_set_rx_sw_flow_control(info->ioaddr, false);
Jiri Slaby1c456072008-02-07 00:16:46 -08001517 spin_unlock_irqrestore(&info->slock, flags);
1518 }
1519
1520 mxser_start(tty);
1521 }
1522}
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524/*
1525 * mxser_wait_until_sent() --- wait until the transmitter is empty
1526 */
1527static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1528{
Jiri Slaby1c456072008-02-07 00:16:46 -08001529 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 unsigned long orig_jiffies, char_time;
Alan Cox07f86c02009-11-30 13:17:41 +00001531 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int lsr;
1533
1534 if (info->type == PORT_UNKNOWN)
1535 return;
1536
1537 if (info->xmit_fifo_size == 0)
1538 return; /* Just in case.... */
1539
1540 orig_jiffies = jiffies;
1541 /*
1542 * Set the check interval to be 1/5 of the estimated time to
1543 * send a single character, and make it at least 1. The check
1544 * interval should also be less than the timeout.
1545 *
1546 * Note: we have to use pretty tight timings here to satisfy
1547 * the NIST-PCTS.
1548 */
1549 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1550 char_time = char_time / 5;
1551 if (char_time == 0)
1552 char_time = 1;
1553 if (timeout && timeout < char_time)
1554 char_time = timeout;
1555 /*
1556 * If the transmitter hasn't cleared in twice the approximate
1557 * amount of time to send the entire FIFO, it probably won't
1558 * ever clear. This assumes the UART isn't doing flow
1559 * control, which is currently the case. Hence, if it ever
1560 * takes longer than info->timeout, this is probably due to a
1561 * UART bug of some kind. So, we clamp the timeout parameter at
1562 * 2*info->timeout.
1563 */
1564 if (!timeout || timeout > 2 * info->timeout)
1565 timeout = 2 * info->timeout;
Jiri Slaby8bab5342011-07-14 14:35:15 +02001566
Alan Cox07f86c02009-11-30 13:17:41 +00001567 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001568 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Alan Cox07f86c02009-11-30 13:17:41 +00001569 spin_unlock_irqrestore(&info->slock, flags);
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001570 schedule_timeout_interruptible(char_time);
Alan Cox07f86c02009-11-30 13:17:41 +00001571 spin_lock_irqsave(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (signal_pending(current))
1573 break;
1574 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1575 break;
1576 }
Alan Cox07f86c02009-11-30 13:17:41 +00001577 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/*
1582 * This routine is called by tty_hangup() when a hangup is signaled.
1583 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001584static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Jiri Slaby1c456072008-02-07 00:16:46 -08001586 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 mxser_flush_buffer(tty);
Alan Cox3b6826b2009-01-02 13:45:58 +00001589 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592/*
1593 * mxser_rs_break() --- routine which turns the break handling on or off
1594 */
Alan Cox9e98966c2008-07-22 11:18:03 +01001595static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Jiri Slaby1c456072008-02-07 00:16:46 -08001597 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 unsigned long flags;
1599
1600 spin_lock_irqsave(&info->slock, flags);
1601 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08001602 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1603 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 else
Jiri Slaby1c456072008-02-07 00:16:46 -08001605 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1606 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e98966c2008-07-22 11:18:03 +01001608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001611static bool mxser_receive_chars_new(struct tty_struct *tty,
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001612 struct mxser_port *port, u8 status)
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001613{
1614 enum mxser_must_hwid hwid = port->board->must_hwid;
1615 u8 gdl;
1616
1617 if (hwid == MOXA_OTHER_UART)
1618 return false;
Jiri Slaby70640052021-06-18 08:14:26 +02001619 if (status & UART_LSR_BRK_ERROR_BITS)
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001620 return false;
1621 if (hwid == MOXA_MUST_MU860_HWID && (status & MOXA_MUST_LSR_RERR))
1622 return false;
1623 if (status & MOXA_MUST_LSR_RERR)
1624 return false;
1625
1626 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
1627 if (hwid == MOXA_MUST_MU150_HWID)
1628 gdl &= MOXA_MUST_GDL_MASK;
1629
1630 if (gdl >= tty->receive_room && !port->ldisc_stop_rx)
1631 mxser_stoprx(tty);
1632
1633 while (gdl--) {
1634 u8 ch = inb(port->ioaddr + UART_RX);
1635 tty_insert_flip_char(&port->port, ch, 0);
Jiri Slabye5ce1bc2021-06-18 08:14:21 +02001636 }
1637
1638 return true;
1639}
1640
Jiri Slaby0c419422021-06-18 08:14:22 +02001641static u8 mxser_receive_chars_old(struct tty_struct *tty,
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001642 struct mxser_port *port, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Jiri Slaby0c419422021-06-18 08:14:22 +02001644 enum mxser_must_hwid hwid = port->board->must_hwid;
1645 int recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int max = 256;
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001648 int cnt = 0;
Jiri Slaby0c419422021-06-18 08:14:22 +02001649 u8 ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 do {
1652 if (max-- < 0)
1653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Jiri Slaby1c456072008-02-07 00:16:46 -08001655 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby0c419422021-06-18 08:14:22 +02001656 if (hwid && (status & UART_LSR_OE))
Jiri Slaby1c456072008-02-07 00:16:46 -08001657 outb(0x23, port->ioaddr + UART_FCR);
Jiri Slaby15517802021-06-18 08:14:18 +02001658 status &= port->read_status_mask;
1659 if (status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (++ignored > 100)
1661 break;
1662 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001663 char flag = 0;
Jiri Slaby70640052021-06-18 08:14:26 +02001664 if (status & UART_LSR_BRK_ERROR_BITS) {
Jiri Slaby15517802021-06-18 08:14:18 +02001665 if (status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001666 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001667 port->icount.brk++;
1668
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001669 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 do_SAK(tty);
Jiri Slaby15517802021-06-18 08:14:18 +02001671 } else if (status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001672 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08001673 port->icount.parity++;
Jiri Slaby15517802021-06-18 08:14:18 +02001674 } else if (status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001675 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08001676 port->icount.frame++;
Jiri Slaby15517802021-06-18 08:14:18 +02001677 } else if (status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001678 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08001679 port->icount.overrun++;
Jiri Slaby6de6e5c2021-06-18 08:14:23 +02001680 }
Denis Vlasenko3399ba52005-06-06 13:35:55 -07001681 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01001682 tty_insert_flip_char(&port->port, ch, flag);
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001683 cnt++;
1684 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001685 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 break;
1688 }
1689
1690 }
1691
Jiri Slaby0c419422021-06-18 08:14:22 +02001692 if (hwid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Jiri Slaby15517802021-06-18 08:14:18 +02001695 status = inb(port->ioaddr + UART_LSR);
1696 } while (status & UART_LSR_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Jiri Slaby0c419422021-06-18 08:14:22 +02001698 return status;
1699}
1700
1701static u8 mxser_receive_chars(struct tty_struct *tty,
1702 struct mxser_port *port, u8 status)
1703{
Jiri Slaby0c419422021-06-18 08:14:22 +02001704 if (tty->receive_room == 0 && !port->ldisc_stop_rx)
1705 mxser_stoprx(tty);
1706
Jiri Slaby95b3ea42021-06-18 08:14:37 +02001707 if (!mxser_receive_chars_new(tty, port, status))
1708 status = mxser_receive_chars_old(tty, port, status);
Jiri Slaby0c419422021-06-18 08:14:22 +02001709
Jiri Slaby2e124b42013-01-03 15:53:06 +01001710 tty_flip_buffer_push(&port->port);
Jiri Slaby15517802021-06-18 08:14:18 +02001711
1712 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Alan Cox216ba022008-10-13 10:40:19 +01001715static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Jiri Slaby1c456072008-02-07 00:16:46 -08001719 if (port->x_char) {
1720 outb(port->x_char, port->ioaddr + UART_TX);
1721 port->x_char = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001722 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return;
1724 }
1725
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001726 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08001727 return;
1728
Jiri Slaby6e94dbc2021-05-05 11:19:05 +02001729 if (port->xmit_cnt <= 0 || tty->flow.stopped ||
Alan Cox216ba022008-10-13 10:40:19 +01001730 (tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08001731 (port->type != PORT_16550A) &&
Jiri Slaby292955a2021-06-18 08:14:13 +02001732 (!port->board->must_hwid))) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001733 port->IER &= ~UART_IER_THRI;
1734 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return;
1736 }
1737
Jiri Slaby1c456072008-02-07 00:16:46 -08001738 cnt = port->xmit_cnt;
1739 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001741 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08001742 port->ioaddr + UART_TX);
1743 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1744 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 break;
1746 } while (--count > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Jiri Slaby1c456072008-02-07 00:16:46 -08001748 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Kulikov Vasiliy464eb8f2010-07-23 20:34:53 +04001750 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox216ba022008-10-13 10:40:19 +01001751 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Jiri Slaby1c456072008-02-07 00:16:46 -08001753 if (port->xmit_cnt <= 0) {
1754 port->IER &= ~UART_IER_THRI;
1755 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001759static bool mxser_port_isr(struct mxser_port *port)
1760{
1761 struct tty_struct *tty;
1762 u8 iir, msr, status;
1763 bool error = false;
1764
1765 iir = inb(port->ioaddr + UART_IIR);
1766 if (iir & UART_IIR_NO_INT)
1767 return true;
1768
1769 iir &= MOXA_MUST_IIR_MASK;
1770 tty = tty_port_tty_get(&port->port);
1771 if (!tty || port->closing || !tty_port_initialized(&port->port)) {
1772 status = inb(port->ioaddr + UART_LSR);
1773 outb(0x27, port->ioaddr + UART_FCR);
1774 inb(port->ioaddr + UART_MSR);
1775
1776 error = true;
1777 goto put_tty;
1778 }
1779
1780 status = inb(port->ioaddr + UART_LSR);
1781
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001782 if (port->board->must_hwid) {
1783 if (iir == MOXA_MUST_IIR_GDA ||
1784 iir == MOXA_MUST_IIR_RDA ||
1785 iir == MOXA_MUST_IIR_RTO ||
1786 iir == MOXA_MUST_IIR_LSR)
1787 status = mxser_receive_chars(tty, port, status);
1788 } else {
1789 status &= port->read_status_mask;
1790 if (status & UART_LSR_DR)
1791 status = mxser_receive_chars(tty, port, status);
1792 }
1793
1794 msr = inb(port->ioaddr + UART_MSR);
1795 if (msr & UART_MSR_ANY_DELTA)
1796 mxser_check_modem_status(tty, port, msr);
1797
1798 if (port->board->must_hwid) {
1799 if (iir == 0x02 && (status & UART_LSR_THRE))
1800 mxser_transmit_chars(tty, port);
1801 } else {
1802 if (status & UART_LSR_THRE)
1803 mxser_transmit_chars(tty, port);
1804 }
1805
1806put_tty:
1807 tty_kref_put(tty);
1808
1809 return error;
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/*
Jiri Slaby1c456072008-02-07 00:16:46 -08001813 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001815static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Jiri Slabycef222c2021-06-18 08:14:17 +02001817 struct mxser_board *brd = dev_id;
Jiri Slaby1c456072008-02-07 00:16:46 -08001818 struct mxser_port *port;
Jiri Slaby1c456072008-02-07 00:16:46 -08001819 unsigned int int_cnt, pass_counter = 0;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001820 unsigned int i, max = brd->info->nports;
Jiri Slaby1c456072008-02-07 00:16:46 -08001821 int handled = IRQ_NONE;
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001822 u8 irqbits, bits, mask = BIT(max) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Jiri Slaby1c456072008-02-07 00:16:46 -08001824 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001825 irqbits = inb(brd->vector) & mask;
1826 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08001827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jiri Slaby1c456072008-02-07 00:16:46 -08001829 handled = IRQ_HANDLED;
1830 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby9cb5c9c2021-06-18 08:14:20 +02001831 if (irqbits == mask)
Jiri Slaby1c456072008-02-07 00:16:46 -08001832 break;
1833 if (bits & irqbits)
1834 continue;
1835 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Jiri Slaby1c456072008-02-07 00:16:46 -08001837 int_cnt = 0;
1838 spin_lock(&port->slock);
1839 do {
Jiri Slaby9e40ea12021-06-18 08:14:19 +02001840 if (mxser_port_isr(port))
Jiri Slaby1c456072008-02-07 00:16:46 -08001841 break;
Jiri Slaby1c456072008-02-07 00:16:46 -08001842 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1843 spin_unlock(&port->slock);
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846
Jiri Slaby1c456072008-02-07 00:16:46 -08001847 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
1849
Jiri Slaby1c456072008-02-07 00:16:46 -08001850static const struct tty_operations mxser_ops = {
1851 .open = mxser_open,
1852 .close = mxser_close,
1853 .write = mxser_write,
1854 .put_char = mxser_put_char,
1855 .flush_chars = mxser_flush_chars,
1856 .write_room = mxser_write_room,
1857 .chars_in_buffer = mxser_chars_in_buffer,
1858 .flush_buffer = mxser_flush_buffer,
1859 .ioctl = mxser_ioctl,
1860 .throttle = mxser_throttle,
1861 .unthrottle = mxser_unthrottle,
1862 .set_termios = mxser_set_termios,
1863 .stop = mxser_stop,
1864 .start = mxser_start,
1865 .hangup = mxser_hangup,
1866 .break_ctl = mxser_rs_break,
1867 .wait_until_sent = mxser_wait_until_sent,
1868 .tiocmget = mxser_tiocmget,
1869 .tiocmset = mxser_tiocmset,
Al Viro6da5b582018-09-11 22:22:06 -04001870 .set_serial = mxser_set_serial_info,
1871 .get_serial = mxser_get_serial_info,
Alan Cox05871022010-09-16 18:21:52 +01001872 .get_icount = mxser_get_icount,
Jiri Slaby1c456072008-02-07 00:16:46 -08001873};
1874
Aya Mahfouz04b757d2015-12-15 01:53:36 +02001875static const struct tty_port_operations mxser_port_ops = {
Alan Cox31f35932009-01-02 13:45:05 +00001876 .carrier_raised = mxser_carrier_raised,
Alan Coxfcc8ac12009-06-11 12:24:17 +01001877 .dtr_rts = mxser_dtr_rts,
Alan Cox67691402009-11-30 13:17:35 +00001878 .activate = mxser_activate,
1879 .shutdown = mxser_shutdown_port,
Alan Cox31f35932009-01-02 13:45:05 +00001880};
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882/*
Jiri Slaby1c456072008-02-07 00:16:46 -08001883 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001885
Jiri Slaby7f0e79d2021-06-18 08:14:52 +02001886static void mxser_initbrd(struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Jiri Slaby1c456072008-02-07 00:16:46 -08001888 struct mxser_port *info;
1889 unsigned int i;
Jiri Slaby57faa7d2021-06-18 08:14:49 +02001890 bool is_mu860;
1891
1892 brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
1893 is_mu860 = brd->must_hwid == MOXA_MUST_MU860_HWID;
1894
1895 for (i = 0; i < UART_INFO_NUM; i++) {
1896 if (Gpci_uart_info[i].type == brd->must_hwid) {
1897 brd->max_baud = Gpci_uart_info[i].max_baud;
1898
1899 /* exception....CP-102 */
1900 if (brd->info->flags & MXSER_HIGHBAUD)
1901 brd->max_baud = 921600;
1902 break;
1903 }
1904 }
1905
1906 if (is_mu860) {
1907 /* set to RS232 mode by default */
1908 outb(0, brd->vector + 4);
1909 outb(0, brd->vector + 0x0c);
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Jiri Slaby1c456072008-02-07 00:16:46 -08001912 for (i = 0; i < brd->info->nports; i++) {
1913 info = &brd->ports[i];
Jiri Slaby57faa7d2021-06-18 08:14:49 +02001914 if (is_mu860) {
1915 if (i < 4)
1916 info->opmode_ioaddr = brd->vector + 4;
1917 else
1918 info->opmode_ioaddr = brd->vector + 0x0c;
1919 }
Alan Cox44b7d1b2008-07-16 21:57:18 +01001920 tty_port_init(&info->port);
Alan Cox31f35932009-01-02 13:45:05 +00001921 info->port.ops = &mxser_port_ops;
Jiri Slaby1c456072008-02-07 00:16:46 -08001922 info->board = brd;
Jiri Slaby1c456072008-02-07 00:16:46 -08001923 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Jiri Slaby1c456072008-02-07 00:16:46 -08001925 /* Enhance mode enabled here */
Jiri Slaby292955a2021-06-18 08:14:13 +02001926 if (brd->must_hwid != MOXA_OTHER_UART)
Jiri Slabyedb7d272021-06-18 08:14:46 +02001927 mxser_must_set_enhance_mode(info->ioaddr, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Jiri Slaby58a2ddb2021-06-18 08:14:15 +02001929 info->type = PORT_16550A;
Jiri Slaby1c456072008-02-07 00:16:46 -08001930
1931 process_txrx_fifo(info);
1932
Alan Cox44b7d1b2008-07-16 21:57:18 +01001933 info->port.close_delay = 5 * HZ / 10;
1934 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001935 spin_lock_init(&info->slock);
1936
1937 /* before set INT ISR, disable all int */
1938 outb(inb(info->ioaddr + UART_IER) & 0xf0,
1939 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Bill Pemberton9671f092012-11-19 13:21:50 -05001943static int mxser_probe(struct pci_dev *pdev,
Jiri Slaby1c456072008-02-07 00:16:46 -08001944 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Jiri Slaby1c456072008-02-07 00:16:46 -08001946 struct mxser_board *brd;
Jiri Slaby57faa7d2021-06-18 08:14:49 +02001947 unsigned int i;
Jiri Slaby1c456072008-02-07 00:16:46 -08001948 unsigned long ioaddress;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04001949 struct device *tty_dev;
Jiri Slaby1c456072008-02-07 00:16:46 -08001950 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Jiri Slaby1c456072008-02-07 00:16:46 -08001952 for (i = 0; i < MXSER_BOARDS; i++)
1953 if (mxser_boards[i].info == NULL)
1954 break;
1955
1956 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07001957 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
1958 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08001959 goto err;
1960 }
1961
1962 brd = &mxser_boards[i];
1963 brd->idx = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby83766bc2008-07-25 01:48:21 -07001964 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
Jiri Slaby1c456072008-02-07 00:16:46 -08001965 mxser_cards[ent->driver_data].name,
1966 pdev->bus->number, PCI_SLOT(pdev->devfn));
1967
Jiri Slabydcb04e22021-06-18 08:14:51 +02001968 retval = pcim_enable_device(pdev);
Jiri Slaby1c456072008-02-07 00:16:46 -08001969 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07001970 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08001971 goto err;
1972 }
1973
1974 /* io address */
1975 ioaddress = pci_resource_start(pdev, 2);
1976 retval = pci_request_region(pdev, 2, "mxser(IO)");
1977 if (retval)
Jiri Slabydcb04e22021-06-18 08:14:51 +02001978 goto err;
Jiri Slaby1c456072008-02-07 00:16:46 -08001979
1980 brd->info = &mxser_cards[ent->driver_data];
1981 for (i = 0; i < brd->info->nports; i++)
1982 brd->ports[i].ioaddr = ioaddress + 8 * i;
1983
1984 /* vector */
1985 ioaddress = pci_resource_start(pdev, 3);
1986 retval = pci_request_region(pdev, 3, "mxser(vector)");
1987 if (retval)
Jiri Slabydf480512010-09-03 10:31:37 +02001988 goto err_zero;
Jiri Slaby1c456072008-02-07 00:16:46 -08001989 brd->vector = ioaddress;
1990
1991 /* irq */
1992 brd->irq = pdev->irq;
1993
Jiri Slaby7f0e79d2021-06-18 08:14:52 +02001994 mxser_initbrd(brd);
1995
1996 retval = devm_request_irq(&pdev->dev, brd->irq, mxser_interrupt,
1997 IRQF_SHARED, "mxser", brd);
1998 if (retval) {
1999 dev_err(&pdev->dev, "request irq failed");
2000 goto err_relbrd;
2001 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002002
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002003 for (i = 0; i < brd->info->nports; i++) {
2004 tty_dev = tty_port_register_device(&brd->ports[i].port,
2005 mxvar_sdriver, brd->idx + i, &pdev->dev);
2006 if (IS_ERR(tty_dev)) {
2007 retval = PTR_ERR(tty_dev);
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04002008 for (; i > 0; i--)
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002009 tty_unregister_device(mxvar_sdriver,
Alexey Khoroshilov1b581f12013-04-07 23:46:47 +04002010 brd->idx + i - 1);
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002011 goto err_relbrd;
2012 }
2013 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002014
2015 pci_set_drvdata(pdev, brd);
2016
2017 return 0;
Alexey Khoroshilov9e17df32013-02-17 02:24:37 +04002018err_relbrd:
2019 for (i = 0; i < brd->info->nports; i++)
2020 tty_port_destroy(&brd->ports[i].port);
Jiri Slabydf480512010-09-03 10:31:37 +02002021err_zero:
Jiri Slaby1c456072008-02-07 00:16:46 -08002022 brd->info = NULL;
2023err:
2024 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002027static void mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Jiri Slaby1c456072008-02-07 00:16:46 -08002029 struct mxser_board *brd = pci_get_drvdata(pdev);
Jiri Slabyd450f082021-06-18 08:14:50 +02002030 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Jiri Slabyd450f082021-06-18 08:14:50 +02002032 for (i = 0; i < brd->info->nports; i++) {
2033 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2034 tty_port_destroy(&brd->ports[i].port);
2035 }
2036
Jiri Slaby1c456072008-02-07 00:16:46 -08002037 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Jiri Slaby1c456072008-02-07 00:16:46 -08002040static struct pci_driver mxser_driver = {
2041 .name = "mxser",
2042 .id_table = mxser_pcibrds,
2043 .probe = mxser_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -05002044 .remove = mxser_remove
Jiri Slaby1c456072008-02-07 00:16:46 -08002045};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Jiri Slaby1c456072008-02-07 00:16:46 -08002047static int __init mxser_module_init(void)
2048{
Jiri Slaby1df00922008-07-25 01:48:22 -07002049 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08002050
Jiri Slaby389fc822021-06-18 08:14:38 +02002051 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002052 if (!mxvar_sdriver)
2053 return -ENOMEM;
2054
Jiri Slaby1c456072008-02-07 00:16:46 -08002055 /* Initialize the tty_driver structure */
Jiri Slaby1c456072008-02-07 00:16:46 -08002056 mxvar_sdriver->name = "ttyMI";
2057 mxvar_sdriver->major = ttymajor;
2058 mxvar_sdriver->minor_start = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002059 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2060 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2061 mxvar_sdriver->init_termios = tty_std_termios;
2062 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2063 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2064 tty_set_operations(mxvar_sdriver, &mxser_ops);
2065
2066 retval = tty_register_driver(mxvar_sdriver);
2067 if (retval) {
2068 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2069 "tty driver !\n");
2070 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002072
Jiri Slaby1c456072008-02-07 00:16:46 -08002073 retval = pci_register_driver(&mxser_driver);
2074 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002075 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby29134362021-06-18 08:14:07 +02002076 goto err_unr;
Jiri Slaby1c456072008-02-07 00:16:46 -08002077 }
2078
Jiri Slaby1c456072008-02-07 00:16:46 -08002079 return 0;
2080err_unr:
2081 tty_unregister_driver(mxvar_sdriver);
2082err_put:
2083 put_tty_driver(mxvar_sdriver);
2084 return retval;
2085}
2086
2087static void __exit mxser_module_exit(void)
2088{
Jiri Slaby1c456072008-02-07 00:16:46 -08002089 pci_unregister_driver(&mxser_driver);
Jiri Slaby1c456072008-02-07 00:16:46 -08002090 tty_unregister_driver(mxvar_sdriver);
2091 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094module_init(mxser_module_init);
2095module_exit(mxser_module_exit);